lundump.c 11 KB

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