lundump.c 7.8 KB

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