lundump.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. size_t offset; /* current position relative to beginning of dump */
  30. lua_Unsigned 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, size_t size) {
  57. const void *block = luaZ_getaddr(S->Z, size);
  58. S->offset += size;
  59. if (block == NULL)
  60. error(S, "truncated fixed buffer");
  61. return block;
  62. }
  63. #define loadVar(S,x) loadVector(S,&x,1)
  64. static lu_byte loadByte (LoadState *S) {
  65. int b = zgetc(S->Z);
  66. if (b == EOZ)
  67. error(S, "truncated chunk");
  68. S->offset++;
  69. return cast_byte(b);
  70. }
  71. static varint_t loadVarint (LoadState *S, varint_t limit) {
  72. varint_t x = 0;
  73. int b;
  74. limit >>= 7;
  75. do {
  76. b = loadByte(S);
  77. if (x >= limit)
  78. error(S, "integer overflow");
  79. x = (x << 7) | (b & 0x7f);
  80. } while ((b & 0x80) != 0);
  81. return x;
  82. }
  83. static size_t loadSize (LoadState *S) {
  84. return cast_sizet(loadVarint(S, MAX_SIZET));
  85. }
  86. static int loadInt (LoadState *S) {
  87. return cast_int(loadVarint(S, INT_MAX));
  88. }
  89. static lua_Number loadNumber (LoadState *S) {
  90. lua_Number x;
  91. loadVar(S, x);
  92. return x;
  93. }
  94. static lua_Integer loadInteger (LoadState *S) {
  95. lua_Integer x;
  96. loadVar(S, x);
  97. return x;
  98. }
  99. /*
  100. ** Load a nullable string into slot 'sl' from prototype 'p'. The
  101. ** assignment to the slot and the barrier must be performed before any
  102. ** possible GC activity, to anchor the string. (Both 'loadVector' and
  103. ** 'luaH_setint' can call the GC.)
  104. */
  105. static void loadString (LoadState *S, Proto *p, TString **sl) {
  106. lua_State *L = S->L;
  107. TString *ts;
  108. TValue sv;
  109. size_t size = loadSize(S);
  110. if (size == 0) { /* no string? */
  111. lua_assert(*sl == NULL); /* must be prefilled */
  112. return;
  113. }
  114. else if (size == 1) { /* previously saved string? */
  115. /* get its index */
  116. lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED));
  117. TValue stv;
  118. luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */
  119. *sl = ts = tsvalue(&stv);
  120. luaC_objbarrier(L, p, ts);
  121. return; /* do not save it again */
  122. }
  123. else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */
  124. char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */
  125. loadVector(S, buff, size + 1); /* load string into buffer */
  126. *sl = ts = luaS_newlstr(L, buff, size); /* create string */
  127. luaC_objbarrier(L, p, ts);
  128. }
  129. else if (S->fixed) { /* for a fixed buffer, use a fixed string */
  130. const char *s = getaddr(S, size + 1, char); /* get content address */
  131. *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
  132. luaC_objbarrier(L, p, ts);
  133. }
  134. else { /* create internal copy */
  135. *sl = ts = luaS_createlngstrobj(L, size); /* create string */
  136. luaC_objbarrier(L, p, ts);
  137. loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */
  138. }
  139. /* add string to list of saved strings */
  140. S->nstr++;
  141. setsvalue(L, &sv, ts);
  142. luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
  143. luaC_objbarrierback(L, obj2gco(S->h), ts);
  144. }
  145. static void loadCode (LoadState *S, Proto *f) {
  146. int n = loadInt(S);
  147. loadAlign(S, sizeof(f->code[0]));
  148. if (S->fixed) {
  149. f->code = getaddr(S, n, Instruction);
  150. f->sizecode = n;
  151. }
  152. else {
  153. f->code = luaM_newvectorchecked(S->L, n, Instruction);
  154. f->sizecode = n;
  155. loadVector(S, f->code, n);
  156. }
  157. }
  158. static void loadFunction(LoadState *S, Proto *f);
  159. static void loadConstants (LoadState *S, Proto *f) {
  160. int i;
  161. int n = loadInt(S);
  162. f->k = luaM_newvectorchecked(S->L, n, TValue);
  163. f->sizek = n;
  164. for (i = 0; i < n; i++)
  165. setnilvalue(&f->k[i]);
  166. for (i = 0; i < n; i++) {
  167. TValue *o = &f->k[i];
  168. int t = loadByte(S);
  169. switch (t) {
  170. case LUA_VNIL:
  171. setnilvalue(o);
  172. break;
  173. case LUA_VFALSE:
  174. setbfvalue(o);
  175. break;
  176. case LUA_VTRUE:
  177. setbtvalue(o);
  178. break;
  179. case LUA_VNUMFLT:
  180. setfltvalue(o, loadNumber(S));
  181. break;
  182. case LUA_VNUMINT:
  183. setivalue(o, loadInteger(S));
  184. break;
  185. case LUA_VSHRSTR:
  186. case LUA_VLNGSTR: {
  187. lua_assert(f->source == NULL);
  188. loadString(S, f, &f->source); /* use 'source' to anchor string */
  189. if (f->source == NULL)
  190. error(S, "bad format for constant string");
  191. setsvalue2n(S->L, o, f->source); /* save it in the right place */
  192. f->source = NULL;
  193. break;
  194. }
  195. default: lua_assert(0);
  196. }
  197. }
  198. }
  199. static void loadProtos (LoadState *S, Proto *f) {
  200. int i;
  201. int n = loadInt(S);
  202. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  203. f->sizep = n;
  204. for (i = 0; i < n; i++)
  205. f->p[i] = NULL;
  206. for (i = 0; i < n; i++) {
  207. f->p[i] = luaF_newproto(S->L);
  208. luaC_objbarrier(S->L, f, f->p[i]);
  209. loadFunction(S, f->p[i]);
  210. }
  211. }
  212. /*
  213. ** Load the upvalues for a function. The names must be filled first,
  214. ** because the filling of the other fields can raise read errors and
  215. ** the creation of the error message can call an emergency collection;
  216. ** in that case all prototypes must be consistent for the GC.
  217. */
  218. static void loadUpvalues (LoadState *S, Proto *f) {
  219. int i, n;
  220. n = loadInt(S);
  221. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  222. f->sizeupvalues = n;
  223. for (i = 0; i < n; i++) /* make array valid for GC */
  224. f->upvalues[i].name = NULL;
  225. for (i = 0; i < n; i++) { /* following calls can raise errors */
  226. f->upvalues[i].instack = loadByte(S);
  227. f->upvalues[i].idx = loadByte(S);
  228. f->upvalues[i].kind = loadByte(S);
  229. }
  230. }
  231. static void loadDebug (LoadState *S, Proto *f) {
  232. int i, n;
  233. n = loadInt(S);
  234. if (S->fixed) {
  235. f->lineinfo = getaddr(S, n, ls_byte);
  236. f->sizelineinfo = n;
  237. }
  238. else {
  239. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  240. f->sizelineinfo = n;
  241. loadVector(S, f->lineinfo, n);
  242. }
  243. n = loadInt(S);
  244. if (n > 0) {
  245. loadAlign(S, sizeof(int));
  246. if (S->fixed) {
  247. f->abslineinfo = getaddr(S, n, AbsLineInfo);
  248. f->sizeabslineinfo = n;
  249. }
  250. else {
  251. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  252. f->sizeabslineinfo = n;
  253. loadVector(S, f->abslineinfo, n);
  254. }
  255. }
  256. n = loadInt(S);
  257. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  258. f->sizelocvars = n;
  259. for (i = 0; i < n; i++)
  260. f->locvars[i].varname = NULL;
  261. for (i = 0; i < n; i++) {
  262. loadString(S, f, &f->locvars[i].varname);
  263. f->locvars[i].startpc = loadInt(S);
  264. f->locvars[i].endpc = loadInt(S);
  265. }
  266. n = loadInt(S);
  267. if (n != 0) /* does it have debug information? */
  268. n = f->sizeupvalues; /* must be this many */
  269. for (i = 0; i < n; i++)
  270. loadString(S, f, &f->upvalues[i].name);
  271. }
  272. static void loadFunction (LoadState *S, Proto *f) {
  273. f->linedefined = loadInt(S);
  274. f->lastlinedefined = loadInt(S);
  275. f->numparams = loadByte(S);
  276. f->flag = loadByte(S) & PF_ISVARARG; /* get only the meaningful flags */
  277. if (S->fixed)
  278. f->flag |= PF_FIXED; /* signal that code is fixed */
  279. f->maxstacksize = loadByte(S);
  280. loadCode(S, f);
  281. loadConstants(S, f);
  282. loadUpvalues(S, f);
  283. loadProtos(S, f);
  284. loadString(S, f, &f->source);
  285. loadDebug(S, f);
  286. }
  287. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  288. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  289. size_t len = strlen(s);
  290. loadVector(S, buff, len);
  291. if (memcmp(s, buff, len) != 0)
  292. error(S, msg);
  293. }
  294. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  295. if (loadByte(S) != size)
  296. error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
  297. }
  298. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  299. static void checkHeader (LoadState *S) {
  300. /* skip 1st char (already read and checked) */
  301. checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
  302. if (loadByte(S) != LUAC_VERSION)
  303. error(S, "version mismatch");
  304. if (loadByte(S) != LUAC_FORMAT)
  305. error(S, "format mismatch");
  306. checkliteral(S, LUAC_DATA, "corrupted chunk");
  307. checksize(S, Instruction);
  308. checksize(S, lua_Integer);
  309. checksize(S, lua_Number);
  310. if (loadInteger(S) != LUAC_INT)
  311. error(S, "integer format mismatch");
  312. if (loadNumber(S) != LUAC_NUM)
  313. error(S, "float format mismatch");
  314. }
  315. /*
  316. ** Load precompiled chunk.
  317. */
  318. LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
  319. LoadState S;
  320. LClosure *cl;
  321. if (*name == '@' || *name == '=')
  322. S.name = name + 1;
  323. else if (*name == LUA_SIGNATURE[0])
  324. S.name = "binary string";
  325. else
  326. S.name = name;
  327. S.L = L;
  328. S.Z = Z;
  329. S.fixed = fixed;
  330. S.offset = 1; /* fist byte was already read */
  331. checkHeader(&S);
  332. cl = luaF_newLclosure(L, loadByte(&S));
  333. setclLvalue2s(L, L->top.p, cl);
  334. luaD_inctop(L);
  335. S.h = luaH_new(L); /* create list of saved strings */
  336. S.nstr = 0;
  337. sethvalue2s(L, L->top.p, S.h); /* anchor it */
  338. luaD_inctop(L);
  339. cl->p = luaF_newproto(L);
  340. luaC_objbarrier(L, cl, cl->p);
  341. loadFunction(&S, cl->p);
  342. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  343. luai_verifycode(L, cl->p);
  344. L->top.p--; /* pop table */
  345. return cl;
  346. }