lundump.c 8.2 KB

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