lundump.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $
  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. lu_byte x;
  43. LoadVar(S, x);
  44. return x;
  45. }
  46. static int LoadInt (LoadState *S) {
  47. int x;
  48. LoadVar(S, x);
  49. return x;
  50. }
  51. static lua_Number LoadNumber (LoadState *S) {
  52. lua_Number x;
  53. LoadVar(S, x);
  54. return x;
  55. }
  56. static lua_Integer LoadInteger (LoadState *S) {
  57. lua_Integer x;
  58. LoadVar(S, x);
  59. return x;
  60. }
  61. static TString *LoadString (LoadState *S, Proto *p) {
  62. lua_State *L = S->L;
  63. size_t size = LoadByte(S);
  64. TString *ts;
  65. if (size == 0xFF)
  66. LoadVar(S, size);
  67. if (size == 0)
  68. return NULL;
  69. else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
  70. char buff[LUAI_MAXSHORTLEN];
  71. LoadVector(S, buff, size);
  72. ts = luaS_newlstr(L, buff, size);
  73. }
  74. else { /* long string */
  75. ts = luaS_createlngstrobj(L, size);
  76. setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */
  77. luaD_inctop(L);
  78. LoadVector(S, getstr(ts), size); /* load directly in final place */
  79. L->top--; /* pop string */
  80. }
  81. luaC_objbarrier(L, p, ts);
  82. return ts;
  83. }
  84. static void LoadCode (LoadState *S, Proto *f) {
  85. int n = LoadInt(S);
  86. f->code = luaM_newvector(S->L, n, Instruction);
  87. f->sizecode = n;
  88. LoadVector(S, f->code, n);
  89. }
  90. static void LoadFunction(LoadState *S, Proto *f, TString *psource);
  91. static void LoadConstants (LoadState *S, Proto *f) {
  92. int i;
  93. int n = LoadInt(S);
  94. f->k = luaM_newvector(S->L, n, TValue);
  95. f->sizek = n;
  96. for (i = 0; i < n; i++)
  97. setnilvalue(&f->k[i]);
  98. for (i = 0; i < n; i++) {
  99. TValue *o = &f->k[i];
  100. int t = LoadByte(S);
  101. switch (t) {
  102. case LUA_TNIL:
  103. setnilvalue(o);
  104. break;
  105. case LUA_TBOOLEAN:
  106. setbvalue(o, LoadByte(S));
  107. break;
  108. case LUA_TNUMFLT:
  109. setfltvalue(o, LoadNumber(S));
  110. break;
  111. case LUA_TNUMINT:
  112. setivalue(o, LoadInteger(S));
  113. break;
  114. case LUA_TSHRSTR:
  115. case LUA_TLNGSTR:
  116. setsvalue2n(S->L, o, LoadString(S, f));
  117. break;
  118. default:
  119. lua_assert(0);
  120. }
  121. }
  122. }
  123. static void LoadProtos (LoadState *S, Proto *f) {
  124. int i;
  125. int n = LoadInt(S);
  126. f->p = luaM_newvector(S->L, n, Proto *);
  127. f->sizep = n;
  128. for (i = 0; i < n; i++)
  129. f->p[i] = NULL;
  130. for (i = 0; i < n; i++) {
  131. f->p[i] = luaF_newproto(S->L);
  132. luaC_objbarrier(S->L, f, f->p[i]);
  133. LoadFunction(S, f->p[i], f->source);
  134. }
  135. }
  136. static void LoadUpvalues (LoadState *S, Proto *f) {
  137. int i, n;
  138. n = LoadInt(S);
  139. f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
  140. f->sizeupvalues = n;
  141. for (i = 0; i < n; i++)
  142. f->upvalues[i].name = NULL;
  143. for (i = 0; i < n; i++) {
  144. f->upvalues[i].instack = LoadByte(S);
  145. f->upvalues[i].idx = LoadByte(S);
  146. }
  147. }
  148. static void LoadDebug (LoadState *S, Proto *f) {
  149. int i, n;
  150. n = LoadInt(S);
  151. f->lineinfo = luaM_newvector(S->L, n, int);
  152. f->sizelineinfo = n;
  153. LoadVector(S, f->lineinfo, n);
  154. n = LoadInt(S);
  155. f->locvars = luaM_newvector(S->L, n, LocVar);
  156. f->sizelocvars = n;
  157. for (i = 0; i < n; i++)
  158. f->locvars[i].varname = NULL;
  159. for (i = 0; i < n; i++) {
  160. f->locvars[i].varname = LoadString(S, f);
  161. f->locvars[i].startpc = LoadInt(S);
  162. f->locvars[i].endpc = LoadInt(S);
  163. }
  164. n = LoadInt(S);
  165. for (i = 0; i < n; i++)
  166. f->upvalues[i].name = LoadString(S, f);
  167. }
  168. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  169. f->source = LoadString(S, f);
  170. if (f->source == NULL) /* no source in dump? */
  171. f->source = psource; /* reuse parent's source */
  172. f->linedefined = LoadInt(S);
  173. f->lastlinedefined = LoadInt(S);
  174. f->numparams = LoadByte(S);
  175. f->is_vararg = LoadByte(S);
  176. f->maxstacksize = LoadByte(S);
  177. LoadCode(S, f);
  178. LoadConstants(S, f);
  179. LoadUpvalues(S, f);
  180. LoadProtos(S, f);
  181. LoadDebug(S, f);
  182. }
  183. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  184. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  185. size_t len = strlen(s);
  186. LoadVector(S, buff, len);
  187. if (memcmp(s, buff, len) != 0)
  188. error(S, msg);
  189. }
  190. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  191. if (LoadByte(S) != size)
  192. error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
  193. }
  194. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  195. static void checkHeader (LoadState *S) {
  196. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  197. if (LoadByte(S) != LUAC_VERSION)
  198. error(S, "version mismatch in");
  199. if (LoadByte(S) != LUAC_FORMAT)
  200. error(S, "format mismatch in");
  201. checkliteral(S, LUAC_DATA, "corrupted");
  202. checksize(S, int);
  203. checksize(S, size_t);
  204. checksize(S, Instruction);
  205. checksize(S, lua_Integer);
  206. checksize(S, lua_Number);
  207. if (LoadInteger(S) != LUAC_INT)
  208. error(S, "endianness mismatch in");
  209. if (LoadNumber(S) != LUAC_NUM)
  210. error(S, "float format mismatch in");
  211. }
  212. /*
  213. ** load precompiled chunk
  214. */
  215. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  216. LoadState S;
  217. LClosure *cl;
  218. if (*name == '@' || *name == '=')
  219. S.name = name + 1;
  220. else if (*name == LUA_SIGNATURE[0])
  221. S.name = "binary string";
  222. else
  223. S.name = name;
  224. S.L = L;
  225. S.Z = Z;
  226. checkHeader(&S);
  227. cl = luaF_newLclosure(L, LoadByte(&S));
  228. setclLvalue(L, L->top, cl);
  229. luaD_inctop(L);
  230. cl->p = luaF_newproto(L);
  231. luaC_objbarrier(L, cl, cl->p);
  232. LoadFunction(&S, cl->p, NULL);
  233. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  234. luai_verifycode(L, buff, cl->p);
  235. return cl;
  236. }