lundump.c 6.2 KB

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