lundump.c 8.8 KB

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