2
0

lundump.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_TBOOLEAN:
  125. setbvalue(o, LoadByte(S));
  126. break;
  127. case LUA_TNUMFLT:
  128. setfltvalue(o, LoadNumber(S));
  129. break;
  130. case LUA_TNUMINT:
  131. setivalue(o, LoadInteger(S));
  132. break;
  133. case LUA_TSHRSTR:
  134. case LUA_TLNGSTR:
  135. setsvalue2n(S->L, o, LoadString(S));
  136. break;
  137. default: lua_assert(0);
  138. }
  139. }
  140. }
  141. static void LoadProtos (LoadState *S, Proto *f) {
  142. int i;
  143. int n = LoadInt(S);
  144. f->p = luaM_newvectorchecked(S->L, n, Proto *);
  145. f->sizep = n;
  146. for (i = 0; i < n; i++)
  147. f->p[i] = NULL;
  148. for (i = 0; i < n; i++) {
  149. f->p[i] = luaF_newproto(S->L);
  150. LoadFunction(S, f->p[i], f->source);
  151. }
  152. }
  153. static void LoadUpvalues (LoadState *S, Proto *f) {
  154. int i, n;
  155. n = LoadInt(S);
  156. f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
  157. f->sizeupvalues = n;
  158. for (i = 0; i < n; i++) {
  159. f->upvalues[i].name = NULL;
  160. f->upvalues[i].instack = LoadByte(S);
  161. f->upvalues[i].idx = LoadByte(S);
  162. f->upvalues[i].kind = LoadByte(S);
  163. }
  164. }
  165. static void LoadDebug (LoadState *S, Proto *f) {
  166. int i, n;
  167. n = LoadInt(S);
  168. f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
  169. f->sizelineinfo = n;
  170. LoadVector(S, f->lineinfo, n);
  171. n = LoadInt(S);
  172. f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
  173. f->sizeabslineinfo = n;
  174. for (i = 0; i < n; i++) {
  175. f->abslineinfo[i].pc = LoadInt(S);
  176. f->abslineinfo[i].line = LoadInt(S);
  177. }
  178. n = LoadInt(S);
  179. f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
  180. f->sizelocvars = n;
  181. for (i = 0; i < n; i++)
  182. f->locvars[i].varname = NULL;
  183. for (i = 0; i < n; i++) {
  184. f->locvars[i].varname = LoadStringN(S);
  185. f->locvars[i].startpc = LoadInt(S);
  186. f->locvars[i].endpc = LoadInt(S);
  187. }
  188. n = LoadInt(S);
  189. for (i = 0; i < n; i++)
  190. f->upvalues[i].name = LoadStringN(S);
  191. }
  192. static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
  193. f->source = LoadStringN(S);
  194. if (f->source == NULL) /* no source in dump? */
  195. f->source = psource; /* reuse parent's source */
  196. f->linedefined = LoadInt(S);
  197. f->lastlinedefined = LoadInt(S);
  198. f->numparams = LoadByte(S);
  199. f->is_vararg = LoadByte(S);
  200. f->maxstacksize = LoadByte(S);
  201. LoadCode(S, f);
  202. LoadConstants(S, f);
  203. LoadUpvalues(S, f);
  204. LoadProtos(S, f);
  205. LoadDebug(S, f);
  206. }
  207. static void checkliteral (LoadState *S, const char *s, const char *msg) {
  208. char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
  209. size_t len = strlen(s);
  210. LoadVector(S, buff, len);
  211. if (memcmp(s, buff, len) != 0)
  212. error(S, msg);
  213. }
  214. static void fchecksize (LoadState *S, size_t size, const char *tname) {
  215. if (LoadByte(S) != size)
  216. error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
  217. }
  218. #define checksize(S,t) fchecksize(S,sizeof(t),#t)
  219. static void checkHeader (LoadState *S) {
  220. /* skip 1st char (already read and checked) */
  221. checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
  222. if (LoadInt(S) != LUAC_VERSION)
  223. error(S, "version mismatch");
  224. if (LoadByte(S) != LUAC_FORMAT)
  225. error(S, "format mismatch");
  226. checkliteral(S, LUAC_DATA, "corrupted chunk");
  227. checksize(S, Instruction);
  228. checksize(S, lua_Integer);
  229. checksize(S, lua_Number);
  230. if (LoadInteger(S) != LUAC_INT)
  231. error(S, "integer format mismatch");
  232. if (LoadNumber(S) != LUAC_NUM)
  233. error(S, "float format mismatch");
  234. }
  235. /*
  236. ** load precompiled chunk
  237. */
  238. LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
  239. LoadState S;
  240. LClosure *cl;
  241. if (*name == '@' || *name == '=')
  242. S.name = name + 1;
  243. else if (*name == LUA_SIGNATURE[0])
  244. S.name = "binary string";
  245. else
  246. S.name = name;
  247. S.L = L;
  248. S.Z = Z;
  249. checkHeader(&S);
  250. cl = luaF_newLclosure(L, LoadByte(&S));
  251. setclLvalue2s(L, L->top, cl);
  252. luaD_inctop(L);
  253. cl->p = luaF_newproto(L);
  254. LoadFunction(&S, cl->p, NULL);
  255. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  256. luai_verifycode(L, buff, cl->p);
  257. return cl;
  258. }