lundump.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. ** $Id: lundump.c,v 2.49 2017/12/07 18:59:52 roberto Exp roberto $
  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 <string.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstring.h"
  17. #include "lundump.h"
  18. #include "lzio.h"
  19. #if !defined(luai_verifycode)
  20. #define luai_verifycode(L,b,f) /* empty */
  21. #endif
  22. typedef struct {
  23. lua_State *L;
  24. ZIO *Z;
  25. const char *name;
  26. } LoadState;
  27. static l_noret error (LoadState *S, const char *why) {
  28. luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
  29. luaD_throw(S->L, LUA_ERRSYNTAX);
  30. }
  31. /*
  32. ** All high-level loads go through LoadVector; you can change it to
  33. ** adapt to the endianness of the input
  34. */
  35. #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
  36. static void LoadBlock (LoadState *S, void *b, size_t size) {
  37. if (luaZ_read(S->Z, b, size) != 0)
  38. error(S, "truncated");
  39. }
  40. #define LoadVar(S,x) LoadVector(S,&x,1)
  41. static lu_byte LoadByte (LoadState *S) {
  42. int b = zgetc(S->Z);
  43. if (b == EOZ)
  44. error(S, "truncated");
  45. return cast_byte(b);
  46. }
  47. static size_t LoadSize (LoadState *S) {
  48. size_t x = 0;
  49. int b;
  50. do {
  51. b = LoadByte(S);
  52. x = (x << 7) | (b & 0x7f);
  53. } while ((b & 0x80) == 0);
  54. return x;
  55. }
  56. static int LoadInt (LoadState *S) {
  57. return cast_int(LoadSize(S));
  58. }
  59. static lua_Number LoadNumber (LoadState *S) {
  60. lua_Number x;
  61. LoadVar(S, x);
  62. return x;
  63. }
  64. static lua_Integer LoadInteger (LoadState *S) {
  65. lua_Integer x;
  66. LoadVar(S, x);
  67. return x;
  68. }
  69. /*
  70. ** Load a nullable string
  71. */
  72. static TString *LoadStringN (LoadState *S) {
  73. size_t size = LoadSize(S);
  74. if (size == 0)
  75. return NULL;
  76. else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
  77. char buff[LUAI_MAXSHORTLEN];
  78. LoadVector(S, buff, size);
  79. return luaS_newlstr(S->L, buff, size);
  80. }
  81. else { /* long string */
  82. TString *ts = luaS_createlngstrobj(S->L, size);
  83. LoadVector(S, getstr(ts), size); /* load directly in final place */
  84. return ts;
  85. }
  86. }
  87. /*
  88. ** Load a non-nullable string.
  89. */
  90. static TString *LoadString (LoadState *S) {
  91. TString *st = LoadStringN(S);
  92. if (st == NULL)
  93. error(S, "bad format for constant string");
  94. return st;
  95. }
  96. static void LoadCode (LoadState *S, Proto *f) {
  97. int n = LoadInt(S);
  98. f->code = luaM_newvectorchecked(S->L, n, Instruction);
  99. f->sizecode = n;
  100. LoadVector(S, f->code, n);
  101. }
  102. static void LoadFunction(LoadState *S, Proto *f, TString *psource);
  103. static void LoadConstants (LoadState *S, Proto *f) {
  104. int i;
  105. int n = LoadInt(S);
  106. f->k = luaM_newvectorchecked(S->L, n, TValue);
  107. f->sizek = n;
  108. for (i = 0; i < n; i++)
  109. setnilvalue(&f->k[i]);
  110. for (i = 0; i < n; i++) {
  111. TValue *o = &f->k[i];
  112. int t = LoadByte(S);
  113. switch (t) {
  114. case LUA_TNIL:
  115. setnilvalue(o);
  116. break;
  117. case LUA_TBOOLEAN:
  118. setbvalue(o, LoadByte(S));
  119. break;
  120. case LUA_TNUMFLT:
  121. setfltvalue(o, LoadNumber(S));
  122. break;
  123. case LUA_TNUMINT:
  124. setivalue(o, LoadInteger(S));
  125. break;
  126. case LUA_TSHRSTR:
  127. case LUA_TLNGSTR:
  128. setsvalue2n(S->L, o, LoadString(S));
  129. break;
  130. default: lua_assert(0);
  131. }
  132. }
  133. }
  134. static void LoadProtos (LoadState *S, Proto *f) {
  135. int i;
  136. int n = LoadInt(S);
  137. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  138. f->sizep = n;
  139. for (i = 0; i < n; i++)
  140. f->p[i] = NULL;
  141. for (i = 0; i < n; i++) {
  142. f->p[i] = luaF_newproto(S->L);
  143. LoadFunction(S, f->p[i], f->source);
  144. }
  145. }
  146. static void LoadUpvalues (LoadState *S, Proto *f) {
  147. int i, n;
  148. n = LoadInt(S);
  149. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  150. f->sizeupvalues = n;
  151. for (i = 0; i < n; i++)
  152. f->upvalues[i].name = NULL;
  153. for (i = 0; i < n; i++) {
  154. f->upvalues[i].instack = LoadByte(S);
  155. f->upvalues[i].idx = LoadByte(S);
  156. }
  157. }
  158. static void LoadDebug (LoadState *S, Proto *f) {
  159. int i, n;
  160. n = LoadInt(S);
  161. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  162. f->sizelineinfo = n;
  163. LoadVector(S, f->lineinfo, n);
  164. n = LoadInt(S);
  165. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  166. f->sizeabslineinfo = n;
  167. for (i = 0; i < n; i++) {
  168. f->abslineinfo[i].pc = LoadInt(S);
  169. f->abslineinfo[i].line = LoadInt(S);
  170. }
  171. n = LoadInt(S);
  172. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  173. f->sizelocvars = n;
  174. for (i = 0; i < n; i++)
  175. f->locvars[i].varname = NULL;
  176. for (i = 0; i < n; i++) {
  177. f->locvars[i].varname = LoadStringN(S);
  178. f->locvars[i].startpc = LoadInt(S);
  179. f->locvars[i].endpc = LoadInt(S);
  180. }
  181. n = LoadInt(S);
  182. for (i = 0; i < n; i++)
  183. f->upvalues[i].name = LoadStringN(S);
  184. }
  185. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  186. f->source = LoadStringN(S);
  187. if (f->source == NULL) /* no source in dump? */
  188. f->source = psource; /* reuse parent's source */
  189. f->linedefined = LoadInt(S);
  190. f->lastlinedefined = LoadInt(S);
  191. f->numparams = LoadByte(S);
  192. f->is_vararg = LoadByte(S);
  193. f->maxstacksize = LoadByte(S);
  194. LoadCode(S, f);
  195. LoadConstants(S, f);
  196. LoadUpvalues(S, f);
  197. LoadProtos(S, f);
  198. LoadDebug(S, f);
  199. }
  200. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  201. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  202. size_t len = strlen(s);
  203. LoadVector(S, buff, len);
  204. if (memcmp(s, buff, len) != 0)
  205. error(S, msg);
  206. }
  207. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  208. if (LoadByte(S) != size)
  209. error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
  210. }
  211. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  212. static void checkHeader (LoadState *S) {
  213. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  214. if (LoadByte(S) != LUAC_VERSION)
  215. error(S, "version mismatch in");
  216. if (LoadByte(S) != LUAC_FORMAT)
  217. error(S, "format mismatch in");
  218. checkliteral(S, LUAC_DATA, "corrupted");
  219. checksize(S, int);
  220. checksize(S, size_t);
  221. checksize(S, Instruction);
  222. checksize(S, lua_Integer);
  223. checksize(S, lua_Number);
  224. if (LoadInteger(S) != LUAC_INT)
  225. error(S, "endianness mismatch in");
  226. if (LoadNumber(S) != LUAC_NUM)
  227. error(S, "float format mismatch in");
  228. }
  229. /*
  230. ** load precompiled chunk
  231. */
  232. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  233. LoadState S;
  234. LClosure *cl;
  235. if (*name == '@' || *name == '=')
  236. S.name = name + 1;
  237. else if (*name == LUA_SIGNATURE[0])
  238. S.name = "binary string";
  239. else
  240. S.name = name;
  241. S.L = L;
  242. S.Z = Z;
  243. checkHeader(&S);
  244. cl = luaF_newLclosure(L, LoadByte(&S));
  245. setclLvalue2s(L, L->top, cl);
  246. luaD_inctop(L);
  247. cl->p = luaF_newproto(L);
  248. LoadFunction(&S, cl->p, NULL);
  249. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  250. luai_verifycode(L, buff, cl->p);
  251. return cl;
  252. }