lundump.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. ** $Id: lundump.c $
  3. ** load precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lundump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "lundump.h"
  20. #include "lzio.h"
  21. #if !defined(luai_verifycode)
  22. #define luai_verifycode(L,f) /* empty */
  23. #endif
  24. typedef struct {
  25. lua_State *L;
  26. ZIO *Z;
  27. const char *name;
  28. Table *h; /* list for string reuse */
  29. lu_mem offset; /* current position relative to beginning of dump */
  30. lua_Integer nstr; /* number of strings in the list */
  31. lu_byte fixed; /* dump is fixed in memory */
  32. } LoadState;
  33. static l_noret error (LoadState *S, const char *why) {
  34. luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
  35. luaD_throw(S->L, LUA_ERRSYNTAX);
  36. }
  37. /*
  38. ** All high-level loads go through loadVector; you can change it to
  39. ** adapt to the endianness of the input
  40. */
  41. #define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0]))
  42. static void loadBlock (LoadState *S, void *b, size_t size) {
  43. if (luaZ_read(S->Z, b, size) != 0)
  44. error(S, "truncated chunk");
  45. S->offset += size;
  46. }
  47. static void loadAlign (LoadState *S, int align) {
  48. int padding = align - (S->offset % align);
  49. if (padding < align) { /* apd == align means no padding */
  50. lua_Integer paddingContent;
  51. loadBlock(S, &paddingContent, padding);
  52. lua_assert(S->offset % align == 0);
  53. }
  54. }
  55. #define getaddr(S,n,t) cast(t *, getaddr_(S,n,sizeof(t)))
  56. static const void *getaddr_ (LoadState *S, int n, int sz) {
  57. const void *block = luaZ_getaddr(S->Z, n * sz);
  58. if (block == NULL)
  59. error(S, "truncated fixed buffer");
  60. return block;
  61. }
  62. #define loadVar(S,x) loadVector(S,&x,1)
  63. static lu_byte loadByte (LoadState *S) {
  64. int b = zgetc(S->Z);
  65. if (b == EOZ)
  66. error(S, "truncated chunk");
  67. S->offset++;
  68. return cast_byte(b);
  69. }
  70. static size_t loadUnsigned (LoadState *S, size_t limit) {
  71. size_t x = 0;
  72. int b;
  73. limit >>= 7;
  74. do {
  75. b = loadByte(S);
  76. if (x >= limit)
  77. error(S, "integer overflow");
  78. x = (x << 7) | (b & 0x7f);
  79. } while ((b & 0x80) == 0);
  80. return x;
  81. }
  82. static size_t loadSize (LoadState *S) {
  83. return loadUnsigned(S, MAX_SIZET);
  84. }
  85. static int loadInt (LoadState *S) {
  86. return cast_int(loadUnsigned(S, INT_MAX));
  87. }
  88. static lua_Number loadNumber (LoadState *S) {
  89. lua_Number x;
  90. loadVar(S, x);
  91. return x;
  92. }
  93. static lua_Integer loadInteger (LoadState *S) {
  94. lua_Integer x;
  95. loadVar(S, x);
  96. return x;
  97. }
  98. /*
  99. ** Load a nullable string into prototype 'p'.
  100. */
  101. static TString *loadStringN (LoadState *S, Proto *p) {
  102. lua_State *L = S->L;
  103. TString *ts;
  104. TValue sv;
  105. size_t size = loadSize(S);
  106. if (size == 0) /* no string? */
  107. return NULL;
  108. else if (size == 1) { /* previously saved string? */
  109. int idx = loadInt(S); /* get its index */
  110. TValue stv;
  111. luaH_getint(S->h, idx, &stv);
  112. return tsvalue(&stv);
  113. }
  114. else if (size -= 2, size <= LUAI_MAXSHORTLEN) { /* short string? */
  115. char buff[LUAI_MAXSHORTLEN];
  116. loadVector(S, buff, size); /* load string into buffer */
  117. ts = luaS_newlstr(L, buff, size); /* create string */
  118. }
  119. else { /* long string */
  120. ts = luaS_createlngstrobj(L, size); /* create string */
  121. setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */
  122. luaD_inctop(L);
  123. loadVector(S, getlngstr(ts), size); /* load directly in final place */
  124. L->top.p--; /* pop string */
  125. }
  126. luaC_objbarrier(L, p, ts);
  127. S->nstr++; /* add string to list of saved strings */
  128. setsvalue(L, &sv, ts);
  129. luaH_setint(L, S->h, S->nstr, &sv);
  130. luaC_objbarrierback(L, obj2gco(S->h), ts);
  131. return ts;
  132. }
  133. /*
  134. ** Load a non-nullable string into prototype 'p'.
  135. */
  136. static TString *loadString (LoadState *S, Proto *p) {
  137. TString *st = loadStringN(S, p);
  138. if (st == NULL)
  139. error(S, "bad format for constant string");
  140. return st;
  141. }
  142. static void loadCode (LoadState *S, Proto *f) {
  143. int n = loadInt(S);
  144. loadAlign(S, sizeof(f->code[0]));
  145. if (S->fixed) {
  146. f->code = getaddr(S, n, Instruction);
  147. f->sizecode = n;
  148. }
  149. else {
  150. f->code = luaM_newvectorchecked(S->L, n, Instruction);
  151. f->sizecode = n;
  152. loadVector(S, f->code, n);
  153. }
  154. }
  155. static void loadFunction(LoadState *S, Proto *f);
  156. static void loadConstants (LoadState *S, Proto *f) {
  157. int i;
  158. int n = loadInt(S);
  159. f->k = luaM_newvectorchecked(S->L, n, TValue);
  160. f->sizek = n;
  161. for (i = 0; i < n; i++)
  162. setnilvalue(&f->k[i]);
  163. for (i = 0; i < n; i++) {
  164. TValue *o = &f->k[i];
  165. int t = loadByte(S);
  166. switch (t) {
  167. case LUA_VNIL:
  168. setnilvalue(o);
  169. break;
  170. case LUA_VFALSE:
  171. setbfvalue(o);
  172. break;
  173. case LUA_VTRUE:
  174. setbtvalue(o);
  175. break;
  176. case LUA_VNUMFLT:
  177. setfltvalue(o, loadNumber(S));
  178. break;
  179. case LUA_VNUMINT:
  180. setivalue(o, loadInteger(S));
  181. break;
  182. case LUA_VSHRSTR:
  183. case LUA_VLNGSTR:
  184. setsvalue2n(S->L, o, loadString(S, f));
  185. break;
  186. default: lua_assert(0);
  187. }
  188. }
  189. }
  190. static void loadProtos (LoadState *S, Proto *f) {
  191. int i;
  192. int n = loadInt(S);
  193. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  194. f->sizep = n;
  195. for (i = 0; i < n; i++)
  196. f->p[i] = NULL;
  197. for (i = 0; i < n; i++) {
  198. f->p[i] = luaF_newproto(S->L);
  199. luaC_objbarrier(S->L, f, f->p[i]);
  200. loadFunction(S, f->p[i]);
  201. }
  202. }
  203. /*
  204. ** Load the upvalues for a function. The names must be filled first,
  205. ** because the filling of the other fields can raise read errors and
  206. ** the creation of the error message can call an emergency collection;
  207. ** in that case all prototypes must be consistent for the GC.
  208. */
  209. static void loadUpvalues (LoadState *S, Proto *f) {
  210. int i, n;
  211. n = loadInt(S);
  212. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  213. f->sizeupvalues = n;
  214. for (i = 0; i < n; i++) /* make array valid for GC */
  215. f->upvalues[i].name = NULL;
  216. for (i = 0; i < n; i++) { /* following calls can raise errors */
  217. f->upvalues[i].instack = loadByte(S);
  218. f->upvalues[i].idx = loadByte(S);
  219. f->upvalues[i].kind = loadByte(S);
  220. }
  221. }
  222. static void loadDebug (LoadState *S, Proto *f) {
  223. int i, n;
  224. n = loadInt(S);
  225. if (S->fixed) {
  226. f->lineinfo = getaddr(S, n, ls_byte);
  227. f->sizelineinfo = n;
  228. }
  229. else {
  230. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  231. f->sizelineinfo = n;
  232. loadVector(S, f->lineinfo, n);
  233. }
  234. n = loadInt(S);
  235. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  236. f->sizeabslineinfo = n;
  237. for (i = 0; i < n; i++) {
  238. f->abslineinfo[i].pc = loadInt(S);
  239. f->abslineinfo[i].line = loadInt(S);
  240. }
  241. n = loadInt(S);
  242. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  243. f->sizelocvars = n;
  244. for (i = 0; i < n; i++)
  245. f->locvars[i].varname = NULL;
  246. for (i = 0; i < n; i++) {
  247. f->locvars[i].varname = loadStringN(S, f);
  248. f->locvars[i].startpc = loadInt(S);
  249. f->locvars[i].endpc = loadInt(S);
  250. }
  251. n = loadInt(S);
  252. if (n != 0) /* does it have debug information? */
  253. n = f->sizeupvalues; /* must be this many */
  254. for (i = 0; i < n; i++)
  255. f->upvalues[i].name = loadStringN(S, f);
  256. }
  257. static void loadFunction (LoadState *S, Proto *f) {
  258. f->source = loadStringN(S, f);
  259. f->linedefined = loadInt(S);
  260. f->lastlinedefined = loadInt(S);
  261. f->numparams = loadByte(S);
  262. f->flag = loadByte(S) & PF_ISVARARG; /* get only the meaningful flags */
  263. if (S->fixed)
  264. f->flag |= PF_FIXED; /* signal that code is fixed */
  265. f->maxstacksize = loadByte(S);
  266. loadCode(S, f);
  267. loadConstants(S, f);
  268. loadUpvalues(S, f);
  269. loadProtos(S, f);
  270. loadDebug(S, f);
  271. }
  272. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  273. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  274. size_t len = strlen(s);
  275. loadVector(S, buff, len);
  276. if (memcmp(s, buff, len) != 0)
  277. error(S, msg);
  278. }
  279. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  280. if (loadByte(S) != size)
  281. error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
  282. }
  283. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  284. static void checkHeader (LoadState *S) {
  285. /* skip 1st char (already read and checked) */
  286. checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
  287. if (loadByte(S) != LUAC_VERSION)
  288. error(S, "version mismatch");
  289. if (loadByte(S) != LUAC_FORMAT)
  290. error(S, "format mismatch");
  291. checkliteral(S, LUAC_DATA, "corrupted chunk");
  292. checksize(S, Instruction);
  293. checksize(S, lua_Integer);
  294. checksize(S, lua_Number);
  295. if (loadInteger(S) != LUAC_INT)
  296. error(S, "integer format mismatch");
  297. if (loadNumber(S) != LUAC_NUM)
  298. error(S, "float format mismatch");
  299. }
  300. /*
  301. ** Load precompiled chunk.
  302. */
  303. LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
  304. LoadState S;
  305. LClosure *cl;
  306. if (*name == '@' || *name == '=')
  307. S.name = name + 1;
  308. else if (*name == LUA_SIGNATURE[0])
  309. S.name = "binary string";
  310. else
  311. S.name = name;
  312. S.L = L;
  313. S.Z = Z;
  314. S.fixed = fixed;
  315. S.offset = 1; /* fist byte was already read */
  316. checkHeader(&S);
  317. cl = luaF_newLclosure(L, loadByte(&S));
  318. setclLvalue2s(L, L->top.p, cl);
  319. luaD_inctop(L);
  320. S.h = luaH_new(L); /* create list of saved strings */
  321. S.nstr = 0;
  322. sethvalue2s(L, L->top.p, S.h); /* anchor it */
  323. luaD_inctop(L);
  324. cl->p = luaF_newproto(L);
  325. luaC_objbarrier(L, cl, cl->p);
  326. loadFunction(&S, cl->p);
  327. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  328. luai_verifycode(L, cl->p);
  329. L->top.p--; /* pop table */
  330. return cl;
  331. }