lundump.c 10 KB

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