lundump.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. loadVector(S, getstr(ts), size); /* load directly in final place */
  93. }
  94. luaC_objbarrier(L, p, ts);
  95. return ts;
  96. }
  97. /*
  98. ** Load a non-nullable string into prototype 'p'.
  99. */
  100. static TString *loadString (LoadState *S, Proto *p) {
  101. TString *st = loadStringN(S, p);
  102. if (st == NULL)
  103. error(S, "bad format for constant string");
  104. return st;
  105. }
  106. static void loadCode (LoadState *S, Proto *f) {
  107. int n = loadInt(S);
  108. f->code = luaM_newvectorchecked(S->L, n, Instruction);
  109. f->sizecode = n;
  110. loadVector(S, f->code, n);
  111. }
  112. static void loadFunction(LoadState *S, Proto *f, TString *psource);
  113. static void loadConstants (LoadState *S, Proto *f) {
  114. int i;
  115. int n = loadInt(S);
  116. f->k = luaM_newvectorchecked(S->L, n, TValue);
  117. f->sizek = n;
  118. for (i = 0; i < n; i++)
  119. setnilvalue(&f->k[i]);
  120. for (i = 0; i < n; i++) {
  121. TValue *o = &f->k[i];
  122. int t = loadByte(S);
  123. switch (t) {
  124. case LUA_VNIL:
  125. setnilvalue(o);
  126. break;
  127. case LUA_VFALSE:
  128. setbfvalue(o);
  129. break;
  130. case LUA_VTRUE:
  131. setbtvalue(o);
  132. break;
  133. case LUA_VNUMFLT:
  134. setfltvalue(o, loadNumber(S));
  135. break;
  136. case LUA_VNUMINT:
  137. setivalue(o, loadInteger(S));
  138. break;
  139. case LUA_VSHRSTR:
  140. case LUA_VLNGSTR:
  141. setsvalue2n(S->L, o, loadString(S, f));
  142. break;
  143. default: lua_assert(0);
  144. }
  145. }
  146. }
  147. static void loadProtos (LoadState *S, Proto *f) {
  148. int i;
  149. int n = loadInt(S);
  150. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  151. f->sizep = n;
  152. for (i = 0; i < n; i++)
  153. f->p[i] = NULL;
  154. for (i = 0; i < n; i++) {
  155. f->p[i] = luaF_newproto(S->L);
  156. luaC_objbarrier(S->L, f, f->p[i]);
  157. loadFunction(S, f->p[i], f->source);
  158. }
  159. }
  160. static void loadUpvalues (LoadState *S, Proto *f) {
  161. int i, n;
  162. n = loadInt(S);
  163. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  164. f->sizeupvalues = n;
  165. for (i = 0; i < n; i++) {
  166. f->upvalues[i].name = NULL;
  167. f->upvalues[i].instack = loadByte(S);
  168. f->upvalues[i].idx = loadByte(S);
  169. f->upvalues[i].kind = loadByte(S);
  170. }
  171. }
  172. static void loadDebug (LoadState *S, Proto *f) {
  173. int i, n;
  174. n = loadInt(S);
  175. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  176. f->sizelineinfo = n;
  177. loadVector(S, f->lineinfo, n);
  178. n = loadInt(S);
  179. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  180. f->sizeabslineinfo = n;
  181. for (i = 0; i < n; i++) {
  182. f->abslineinfo[i].pc = loadInt(S);
  183. f->abslineinfo[i].line = loadInt(S);
  184. }
  185. n = loadInt(S);
  186. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  187. f->sizelocvars = n;
  188. for (i = 0; i < n; i++)
  189. f->locvars[i].varname = NULL;
  190. for (i = 0; i < n; i++) {
  191. f->locvars[i].varname = loadStringN(S, f);
  192. f->locvars[i].startpc = loadInt(S);
  193. f->locvars[i].endpc = loadInt(S);
  194. }
  195. n = loadInt(S);
  196. for (i = 0; i < n; i++)
  197. f->upvalues[i].name = loadStringN(S, f);
  198. }
  199. static void loadFunction (LoadState *S, Proto *f, TString *psource) {
  200. f->source = loadStringN(S, f);
  201. if (f->source == NULL) /* no source in dump? */
  202. f->source = psource; /* reuse parent's source */
  203. f->linedefined = loadInt(S);
  204. f->lastlinedefined = loadInt(S);
  205. f->numparams = loadByte(S);
  206. f->is_vararg = loadByte(S);
  207. f->maxstacksize = loadByte(S);
  208. loadCode(S, f);
  209. loadConstants(S, f);
  210. loadUpvalues(S, f);
  211. loadProtos(S, f);
  212. loadDebug(S, f);
  213. }
  214. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  215. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  216. size_t len = strlen(s);
  217. loadVector(S, buff, len);
  218. if (memcmp(s, buff, len) != 0)
  219. error(S, msg);
  220. }
  221. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  222. if (loadByte(S) != size)
  223. error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
  224. }
  225. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  226. static void checkHeader (LoadState *S) {
  227. /* skip 1st char (already read and checked) */
  228. checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
  229. if (loadByte(S) != LUAC_VERSION)
  230. error(S, "version mismatch");
  231. if (loadByte(S) != LUAC_FORMAT)
  232. error(S, "format mismatch");
  233. checkliteral(S, LUAC_DATA, "corrupted chunk");
  234. checksize(S, Instruction);
  235. checksize(S, lua_Integer);
  236. checksize(S, lua_Number);
  237. if (loadInteger(S) != LUAC_INT)
  238. error(S, "integer format mismatch");
  239. if (loadNumber(S) != LUAC_NUM)
  240. error(S, "float format mismatch");
  241. }
  242. /*
  243. ** Load precompiled chunk.
  244. */
  245. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  246. LoadState S;
  247. LClosure *cl;
  248. if (*name == '@' || *name == '=')
  249. S.name = name + 1;
  250. else if (*name == LUA_SIGNATURE[0])
  251. S.name = "binary string";
  252. else
  253. S.name = name;
  254. S.L = L;
  255. S.Z = Z;
  256. checkHeader(&S);
  257. cl = luaF_newLclosure(L, loadByte(&S));
  258. setclLvalue2s(L, L->top, cl);
  259. luaD_inctop(L);
  260. cl->p = luaF_newproto(L);
  261. luaC_objbarrier(L, cl, cl->p);
  262. loadFunction(&S, cl->p, NULL);
  263. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  264. luai_verifycode(L, cl->p);
  265. return cl;
  266. }