lundump.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. ** $Id: lundump.c,v 2.48 2017/11/28 11:19:07 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. static TString *LoadString (LoadState *S) {
  70. size_t size = LoadSize(S);
  71. if (size == 0)
  72. return NULL;
  73. else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
  74. char buff[LUAI_MAXSHORTLEN];
  75. LoadVector(S, buff, size);
  76. return luaS_newlstr(S->L, buff, size);
  77. }
  78. else { /* long string */
  79. TString *ts = luaS_createlngstrobj(S->L, size);
  80. LoadVector(S, getstr(ts), size); /* load directly in final place */
  81. return ts;
  82. }
  83. }
  84. static void LoadCode (LoadState *S, Proto *f) {
  85. int n = LoadInt(S);
  86. f->code = luaM_newvectorchecked(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_newvectorchecked(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));
  117. break;
  118. default: lua_assert(0);
  119. }
  120. }
  121. }
  122. static void LoadProtos (LoadState *S, Proto *f) {
  123. int i;
  124. int n = LoadInt(S);
  125. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  126. f->sizep = n;
  127. for (i = 0; i < n; i++)
  128. f->p[i] = NULL;
  129. for (i = 0; i < n; i++) {
  130. f->p[i] = luaF_newproto(S->L);
  131. LoadFunction(S, f->p[i], f->source);
  132. }
  133. }
  134. static void LoadUpvalues (LoadState *S, Proto *f) {
  135. int i, n;
  136. n = LoadInt(S);
  137. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  138. f->sizeupvalues = n;
  139. for (i = 0; i < n; i++)
  140. f->upvalues[i].name = NULL;
  141. for (i = 0; i < n; i++) {
  142. f->upvalues[i].instack = LoadByte(S);
  143. f->upvalues[i].idx = LoadByte(S);
  144. }
  145. }
  146. static void LoadDebug (LoadState *S, Proto *f) {
  147. int i, n;
  148. n = LoadInt(S);
  149. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  150. f->sizelineinfo = n;
  151. LoadVector(S, f->lineinfo, n);
  152. n = LoadInt(S);
  153. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  154. f->sizeabslineinfo = n;
  155. for (i = 0; i < n; i++) {
  156. f->abslineinfo[i].pc = LoadInt(S);
  157. f->abslineinfo[i].line = LoadInt(S);
  158. }
  159. n = LoadInt(S);
  160. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  161. f->sizelocvars = n;
  162. for (i = 0; i < n; i++)
  163. f->locvars[i].varname = NULL;
  164. for (i = 0; i < n; i++) {
  165. f->locvars[i].varname = LoadString(S);
  166. f->locvars[i].startpc = LoadInt(S);
  167. f->locvars[i].endpc = LoadInt(S);
  168. }
  169. n = LoadInt(S);
  170. for (i = 0; i < n; i++)
  171. f->upvalues[i].name = LoadString(S);
  172. }
  173. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  174. f->source = LoadString(S);
  175. if (f->source == NULL) /* no source in dump? */
  176. f->source = psource; /* reuse parent's source */
  177. f->linedefined = LoadInt(S);
  178. f->lastlinedefined = LoadInt(S);
  179. f->numparams = LoadByte(S);
  180. f->is_vararg = LoadByte(S);
  181. f->maxstacksize = LoadByte(S);
  182. LoadCode(S, f);
  183. LoadConstants(S, f);
  184. LoadUpvalues(S, f);
  185. LoadProtos(S, f);
  186. LoadDebug(S, f);
  187. }
  188. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  189. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  190. size_t len = strlen(s);
  191. LoadVector(S, buff, len);
  192. if (memcmp(s, buff, len) != 0)
  193. error(S, msg);
  194. }
  195. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  196. if (LoadByte(S) != size)
  197. error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
  198. }
  199. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  200. static void checkHeader (LoadState *S) {
  201. checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
  202. if (LoadByte(S) != LUAC_VERSION)
  203. error(S, "version mismatch in");
  204. if (LoadByte(S) != LUAC_FORMAT)
  205. error(S, "format mismatch in");
  206. checkliteral(S, LUAC_DATA, "corrupted");
  207. checksize(S, int);
  208. checksize(S, size_t);
  209. checksize(S, Instruction);
  210. checksize(S, lua_Integer);
  211. checksize(S, lua_Number);
  212. if (LoadInteger(S) != LUAC_INT)
  213. error(S, "endianness mismatch in");
  214. if (LoadNumber(S) != LUAC_NUM)
  215. error(S, "float format mismatch in");
  216. }
  217. /*
  218. ** load precompiled chunk
  219. */
  220. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  221. LoadState S;
  222. LClosure *cl;
  223. if (*name == '@' || *name == '=')
  224. S.name = name + 1;
  225. else if (*name == LUA_SIGNATURE[0])
  226. S.name = "binary string";
  227. else
  228. S.name = name;
  229. S.L = L;
  230. S.Z = Z;
  231. checkHeader(&S);
  232. cl = luaF_newLclosure(L, LoadByte(&S));
  233. setclLvalue2s(L, L->top, cl);
  234. luaD_inctop(L);
  235. cl->p = luaF_newproto(L);
  236. LoadFunction(&S, cl->p, NULL);
  237. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  238. luai_verifycode(L, buff, cl->p);
  239. return cl;
  240. }