lj_bcread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. ** Bytecode reader.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_bcread_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #include "lj_gc.h"
  9. #include "lj_err.h"
  10. #include "lj_str.h"
  11. #include "lj_tab.h"
  12. #include "lj_bc.h"
  13. #if LJ_HASFFI
  14. #include "lj_ctype.h"
  15. #include "lj_cdata.h"
  16. #include "lualib.h"
  17. #endif
  18. #include "lj_lex.h"
  19. #include "lj_bcdump.h"
  20. #include "lj_state.h"
  21. /* Reuse some lexer fields for our own purposes. */
  22. #define bcread_flags(ls) ls->level
  23. #define bcread_swap(ls) \
  24. ((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE)
  25. #define bcread_oldtop(L, ls) restorestack(L, ls->lastline)
  26. #define bcread_savetop(L, ls, top) \
  27. ls->lastline = (BCLine)savestack(L, (top))
  28. /* -- Input buffer handling ----------------------------------------------- */
  29. /* Throw reader error. */
  30. static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)
  31. {
  32. lua_State *L = ls->L;
  33. const char *name = ls->chunkarg;
  34. if (*name == BCDUMP_HEAD1) name = "(binary)";
  35. else if (*name == '@' || *name == '=') name++;
  36. lj_str_pushf(L, "%s: %s", name, err2msg(em));
  37. lj_err_throw(L, LUA_ERRSYNTAX);
  38. }
  39. /* Resize input buffer. */
  40. static void bcread_resize(LexState *ls, MSize len)
  41. {
  42. if (ls->sb.sz < len) {
  43. MSize sz = ls->sb.sz * 2;
  44. while (len > sz) sz = sz * 2;
  45. lj_str_resizebuf(ls->L, &ls->sb, sz);
  46. /* Caveat: this may change ls->sb.buf which may affect ls->p. */
  47. }
  48. }
  49. /* Refill buffer if needed. */
  50. static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
  51. {
  52. lua_assert(len != 0);
  53. if (len > LJ_MAX_MEM || ls->current < 0)
  54. bcread_error(ls, LJ_ERR_BCBAD);
  55. do {
  56. const char *buf;
  57. size_t size;
  58. if (ls->n) { /* Copy remainder to buffer. */
  59. if (ls->sb.n) { /* Move down in buffer. */
  60. lua_assert(ls->p + ls->n == ls->sb.buf + ls->sb.n);
  61. if (ls->n != ls->sb.n)
  62. memmove(ls->sb.buf, ls->p, ls->n);
  63. } else { /* Copy from buffer provided by reader. */
  64. bcread_resize(ls, len);
  65. memcpy(ls->sb.buf, ls->p, ls->n);
  66. }
  67. ls->p = ls->sb.buf;
  68. }
  69. ls->sb.n = ls->n;
  70. buf = ls->rfunc(ls->L, ls->rdata, &size); /* Get more data from reader. */
  71. if (buf == NULL || size == 0) { /* EOF? */
  72. if (need) bcread_error(ls, LJ_ERR_BCBAD);
  73. ls->current = -1; /* Only bad if we get called again. */
  74. break;
  75. }
  76. if (ls->sb.n) { /* Append to buffer. */
  77. MSize n = ls->sb.n + (MSize)size;
  78. bcread_resize(ls, n < len ? len : n);
  79. memcpy(ls->sb.buf + ls->sb.n, buf, size);
  80. ls->n = ls->sb.n = n;
  81. ls->p = ls->sb.buf;
  82. } else { /* Return buffer provided by reader. */
  83. ls->n = (MSize)size;
  84. ls->p = buf;
  85. }
  86. } while (ls->n < len);
  87. }
  88. /* Need a certain number of bytes. */
  89. static LJ_AINLINE void bcread_need(LexState *ls, MSize len)
  90. {
  91. if (LJ_UNLIKELY(ls->n < len))
  92. bcread_fill(ls, len, 1);
  93. }
  94. /* Want to read up to a certain number of bytes, but may need less. */
  95. static LJ_AINLINE void bcread_want(LexState *ls, MSize len)
  96. {
  97. if (LJ_UNLIKELY(ls->n < len))
  98. bcread_fill(ls, len, 0);
  99. }
  100. #define bcread_dec(ls) check_exp(ls->n > 0, ls->n--)
  101. #define bcread_consume(ls, len) check_exp(ls->n >= (len), ls->n -= (len))
  102. /* Return memory block from buffer. */
  103. static uint8_t *bcread_mem(LexState *ls, MSize len)
  104. {
  105. uint8_t *p = (uint8_t *)ls->p;
  106. bcread_consume(ls, len);
  107. ls->p = (char *)p + len;
  108. return p;
  109. }
  110. /* Copy memory block from buffer. */
  111. static void bcread_block(LexState *ls, void *q, MSize len)
  112. {
  113. memcpy(q, bcread_mem(ls, len), len);
  114. }
  115. /* Read byte from buffer. */
  116. static LJ_AINLINE uint32_t bcread_byte(LexState *ls)
  117. {
  118. bcread_dec(ls);
  119. return (uint32_t)(uint8_t)*ls->p++;
  120. }
  121. /* Read ULEB128 value from buffer. */
  122. static uint32_t bcread_uleb128(LexState *ls)
  123. {
  124. const uint8_t *p = (const uint8_t *)ls->p;
  125. uint32_t v = *p++;
  126. if (LJ_UNLIKELY(v >= 0x80)) {
  127. int sh = 0;
  128. v &= 0x7f;
  129. do {
  130. v |= ((*p & 0x7f) << (sh += 7));
  131. bcread_dec(ls);
  132. } while (*p++ >= 0x80);
  133. }
  134. bcread_dec(ls);
  135. ls->p = (char *)p;
  136. return v;
  137. }
  138. /* Read top 32 bits of 33 bit ULEB128 value from buffer. */
  139. static uint32_t bcread_uleb128_33(LexState *ls)
  140. {
  141. const uint8_t *p = (const uint8_t *)ls->p;
  142. uint32_t v = (*p++ >> 1);
  143. if (LJ_UNLIKELY(v >= 0x40)) {
  144. int sh = -1;
  145. v &= 0x3f;
  146. do {
  147. v |= ((*p & 0x7f) << (sh += 7));
  148. bcread_dec(ls);
  149. } while (*p++ >= 0x80);
  150. }
  151. bcread_dec(ls);
  152. ls->p = (char *)p;
  153. return v;
  154. }
  155. /* -- Bytecode reader ----------------------------------------------------- */
  156. /* Read debug info of a prototype. */
  157. static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg)
  158. {
  159. void *lineinfo = (void *)proto_lineinfo(pt);
  160. bcread_block(ls, lineinfo, sizedbg);
  161. /* Swap lineinfo if the endianess differs. */
  162. if (bcread_swap(ls) && pt->numline >= 256) {
  163. MSize i, n = pt->sizebc-1;
  164. if (pt->numline < 65536) {
  165. uint16_t *p = (uint16_t *)lineinfo;
  166. for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8));
  167. } else {
  168. uint32_t *p = (uint32_t *)lineinfo;
  169. for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]);
  170. }
  171. }
  172. }
  173. /* Find pointer to varinfo. */
  174. static const void *bcread_varinfo(GCproto *pt)
  175. {
  176. const uint8_t *p = proto_uvinfo(pt);
  177. MSize n = pt->sizeuv;
  178. if (n) while (*p++ || --n) ;
  179. return p;
  180. }
  181. /* Read a single constant key/value of a template table. */
  182. static void bcread_ktabk(LexState *ls, TValue *o)
  183. {
  184. MSize tp = bcread_uleb128(ls);
  185. if (tp >= BCDUMP_KTAB_STR) {
  186. MSize len = tp - BCDUMP_KTAB_STR;
  187. const char *p = (const char *)bcread_mem(ls, len);
  188. setstrV(ls->L, o, lj_str_new(ls->L, p, len));
  189. } else if (tp == BCDUMP_KTAB_INT) {
  190. setintV(o, (int32_t)bcread_uleb128(ls));
  191. } else if (tp == BCDUMP_KTAB_NUM) {
  192. o->u32.lo = bcread_uleb128(ls);
  193. o->u32.hi = bcread_uleb128(ls);
  194. } else {
  195. lua_assert(tp <= BCDUMP_KTAB_TRUE);
  196. setitype(o, ~tp);
  197. }
  198. }
  199. /* Read a template table. */
  200. static GCtab *bcread_ktab(LexState *ls)
  201. {
  202. MSize narray = bcread_uleb128(ls);
  203. MSize nhash = bcread_uleb128(ls);
  204. GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash));
  205. if (narray) { /* Read array entries. */
  206. MSize i;
  207. TValue *o = tvref(t->array);
  208. for (i = 0; i < narray; i++, o++)
  209. bcread_ktabk(ls, o);
  210. }
  211. if (nhash) { /* Read hash entries. */
  212. MSize i;
  213. for (i = 0; i < nhash; i++) {
  214. TValue key;
  215. bcread_ktabk(ls, &key);
  216. lua_assert(!tvisnil(&key));
  217. bcread_ktabk(ls, lj_tab_set(ls->L, t, &key));
  218. }
  219. }
  220. return t;
  221. }
  222. /* Read GC constants of a prototype. */
  223. static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc)
  224. {
  225. MSize i;
  226. GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
  227. for (i = 0; i < sizekgc; i++, kr++) {
  228. MSize tp = bcread_uleb128(ls);
  229. if (tp >= BCDUMP_KGC_STR) {
  230. MSize len = tp - BCDUMP_KGC_STR;
  231. const char *p = (const char *)bcread_mem(ls, len);
  232. setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len)));
  233. } else if (tp == BCDUMP_KGC_TAB) {
  234. setgcref(*kr, obj2gco(bcread_ktab(ls)));
  235. #if LJ_HASFFI
  236. } else if (tp != BCDUMP_KGC_CHILD) {
  237. CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE :
  238. tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64;
  239. CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8;
  240. GCcdata *cd = lj_cdata_new_(ls->L, id, sz);
  241. TValue *p = (TValue *)cdataptr(cd);
  242. setgcref(*kr, obj2gco(cd));
  243. p[0].u32.lo = bcread_uleb128(ls);
  244. p[0].u32.hi = bcread_uleb128(ls);
  245. if (tp == BCDUMP_KGC_COMPLEX) {
  246. p[1].u32.lo = bcread_uleb128(ls);
  247. p[1].u32.hi = bcread_uleb128(ls);
  248. }
  249. #endif
  250. } else {
  251. lua_State *L = ls->L;
  252. lua_assert(tp == BCDUMP_KGC_CHILD);
  253. if (L->top <= bcread_oldtop(L, ls)) /* Stack underflow? */
  254. bcread_error(ls, LJ_ERR_BCBAD);
  255. L->top--;
  256. setgcref(*kr, obj2gco(protoV(L->top)));
  257. }
  258. }
  259. }
  260. /* Read number constants of a prototype. */
  261. static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn)
  262. {
  263. MSize i;
  264. TValue *o = mref(pt->k, TValue);
  265. for (i = 0; i < sizekn; i++, o++) {
  266. int isnum = (ls->p[0] & 1);
  267. uint32_t lo = bcread_uleb128_33(ls);
  268. if (isnum) {
  269. o->u32.lo = lo;
  270. o->u32.hi = bcread_uleb128(ls);
  271. } else {
  272. setintV(o, lo);
  273. }
  274. }
  275. }
  276. /* Read bytecode instructions. */
  277. static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc)
  278. {
  279. BCIns *bc = proto_bc(pt);
  280. bc[0] = BCINS_AD((pt->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,
  281. pt->framesize, 0);
  282. bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns));
  283. /* Swap bytecode instructions if the endianess differs. */
  284. if (bcread_swap(ls)) {
  285. MSize i;
  286. for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]);
  287. }
  288. }
  289. /* Read upvalue refs. */
  290. static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv)
  291. {
  292. if (sizeuv) {
  293. uint16_t *uv = proto_uv(pt);
  294. bcread_block(ls, uv, sizeuv*2);
  295. /* Swap upvalue refs if the endianess differs. */
  296. if (bcread_swap(ls)) {
  297. MSize i;
  298. for (i = 0; i < sizeuv; i++)
  299. uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8));
  300. }
  301. }
  302. }
  303. /* Read a prototype. */
  304. static GCproto *bcread_proto(LexState *ls)
  305. {
  306. GCproto *pt;
  307. MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept;
  308. MSize ofsk, ofsuv, ofsdbg;
  309. MSize sizedbg = 0;
  310. BCLine firstline = 0, numline = 0;
  311. MSize len, startn;
  312. /* Read length. */
  313. if (ls->n > 0 && ls->p[0] == 0) { /* Shortcut EOF. */
  314. ls->n--; ls->p++;
  315. return NULL;
  316. }
  317. bcread_want(ls, 5);
  318. len = bcread_uleb128(ls);
  319. if (!len) return NULL; /* EOF */
  320. bcread_need(ls, len);
  321. startn = ls->n;
  322. /* Read prototype header. */
  323. flags = bcread_byte(ls);
  324. numparams = bcread_byte(ls);
  325. framesize = bcread_byte(ls);
  326. sizeuv = bcread_byte(ls);
  327. sizekgc = bcread_uleb128(ls);
  328. sizekn = bcread_uleb128(ls);
  329. sizebc = bcread_uleb128(ls) + 1;
  330. if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) {
  331. sizedbg = bcread_uleb128(ls);
  332. if (sizedbg) {
  333. firstline = bcread_uleb128(ls);
  334. numline = bcread_uleb128(ls);
  335. }
  336. }
  337. /* Calculate total size of prototype including all colocated arrays. */
  338. sizept = (MSize)sizeof(GCproto) +
  339. sizebc*(MSize)sizeof(BCIns) +
  340. sizekgc*(MSize)sizeof(GCRef);
  341. sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1);
  342. ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue);
  343. ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2;
  344. ofsdbg = sizept; sizept += sizedbg;
  345. /* Allocate prototype object and initialize its fields. */
  346. pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept);
  347. pt->gct = ~LJ_TPROTO;
  348. pt->numparams = (uint8_t)numparams;
  349. pt->framesize = (uint8_t)framesize;
  350. pt->sizebc = sizebc;
  351. setmref(pt->k, (char *)pt + ofsk);
  352. setmref(pt->uv, (char *)pt + ofsuv);
  353. pt->sizekgc = 0; /* Set to zero until fully initialized. */
  354. pt->sizekn = sizekn;
  355. pt->sizept = sizept;
  356. pt->sizeuv = (uint8_t)sizeuv;
  357. pt->flags = (uint8_t)flags;
  358. pt->trace = 0;
  359. setgcref(pt->chunkname, obj2gco(ls->chunkname));
  360. /* Close potentially uninitialized gap between bc and kgc. */
  361. *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0;
  362. /* Read bytecode instructions and upvalue refs. */
  363. bcread_bytecode(ls, pt, sizebc);
  364. bcread_uv(ls, pt, sizeuv);
  365. /* Read constants. */
  366. bcread_kgc(ls, pt, sizekgc);
  367. pt->sizekgc = sizekgc;
  368. bcread_knum(ls, pt, sizekn);
  369. /* Read and initialize debug info. */
  370. pt->firstline = firstline;
  371. pt->numline = numline;
  372. if (sizedbg) {
  373. MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
  374. setmref(pt->lineinfo, (char *)pt + ofsdbg);
  375. setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli);
  376. bcread_dbg(ls, pt, sizedbg);
  377. setmref(pt->varinfo, bcread_varinfo(pt));
  378. } else {
  379. setmref(pt->lineinfo, NULL);
  380. setmref(pt->uvinfo, NULL);
  381. setmref(pt->varinfo, NULL);
  382. }
  383. if (len != startn - ls->n)
  384. bcread_error(ls, LJ_ERR_BCBAD);
  385. return pt;
  386. }
  387. /* Read and check header of bytecode dump. */
  388. static int bcread_header(LexState *ls)
  389. {
  390. uint32_t flags;
  391. bcread_want(ls, 3+5+5);
  392. if (bcread_byte(ls) != BCDUMP_HEAD2 ||
  393. bcread_byte(ls) != BCDUMP_HEAD3 ||
  394. bcread_byte(ls) != BCDUMP_VERSION) return 0;
  395. bcread_flags(ls) = flags = bcread_uleb128(ls);
  396. if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0;
  397. if ((flags & BCDUMP_F_FFI)) {
  398. #if LJ_HASFFI
  399. lua_State *L = ls->L;
  400. if (!ctype_ctsG(G(L))) {
  401. ptrdiff_t oldtop = savestack(L, L->top);
  402. luaopen_ffi(L); /* Load FFI library on-demand. */
  403. L->top = restorestack(L, oldtop);
  404. }
  405. #else
  406. return 0;
  407. #endif
  408. }
  409. if ((flags & BCDUMP_F_STRIP)) {
  410. ls->chunkname = lj_str_newz(ls->L, ls->chunkarg);
  411. } else {
  412. MSize len = bcread_uleb128(ls);
  413. bcread_need(ls, len);
  414. ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len);
  415. }
  416. return 1; /* Ok. */
  417. }
  418. /* Read a bytecode dump. */
  419. GCproto *lj_bcread(LexState *ls)
  420. {
  421. lua_State *L = ls->L;
  422. lua_assert(ls->current == BCDUMP_HEAD1);
  423. bcread_savetop(L, ls, L->top);
  424. lj_str_resetbuf(&ls->sb);
  425. /* Check for a valid bytecode dump header. */
  426. if (!bcread_header(ls))
  427. bcread_error(ls, LJ_ERR_BCFMT);
  428. for (;;) { /* Process all prototypes in the bytecode dump. */
  429. GCproto *pt = bcread_proto(ls);
  430. if (!pt) break;
  431. setprotoV(L, L->top, pt);
  432. incr_top(L);
  433. }
  434. if ((int32_t)ls->n > 0 || L->top-1 != bcread_oldtop(L, ls))
  435. bcread_error(ls, LJ_ERR_BCBAD);
  436. /* Pop off last prototype. */
  437. L->top--;
  438. return protoV(L->top);
  439. }