lundump.c 6.9 KB

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