lj_state.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. ** State and stack handling.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lj_state_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_buf.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_func.h"
  17. #include "lj_meta.h"
  18. #include "lj_state.h"
  19. #include "lj_frame.h"
  20. #if LJ_HASFFI
  21. #include "lj_ctype.h"
  22. #endif
  23. #include "lj_trace.h"
  24. #include "lj_dispatch.h"
  25. #include "lj_vm.h"
  26. #include "lj_prng.h"
  27. #include "lj_lex.h"
  28. #include "lj_alloc.h"
  29. #include "luajit.h"
  30. /* -- Stack handling ------------------------------------------------------ */
  31. /* Stack sizes. */
  32. #define LJ_STACK_MIN LUA_MINSTACK /* Min. stack size. */
  33. #define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */
  34. #define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */
  35. #define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA)
  36. /* Explanation of LJ_STACK_EXTRA:
  37. **
  38. ** Calls to metamethods store their arguments beyond the current top
  39. ** without checking for the stack limit. This avoids stack resizes which
  40. ** would invalidate passed TValue pointers. The stack check is performed
  41. ** later by the function header. This can safely resize the stack or raise
  42. ** an error. Thus we need some extra slots beyond the current stack limit.
  43. **
  44. ** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus
  45. ** one extra slot if mobj is not a function. Only lj_meta_tset needs 5
  46. ** slots above top, but then mobj is always a function. So we can get by
  47. ** with 5 extra slots.
  48. ** LJ_FR2: We need 2 more slots for the frame PC and the continuation PC.
  49. */
  50. /* Resize stack slots and adjust pointers in state. */
  51. static void resizestack(lua_State *L, MSize n)
  52. {
  53. TValue *st, *oldst = tvref(L->stack);
  54. ptrdiff_t delta;
  55. MSize oldsize = L->stacksize;
  56. MSize realsize = n + 1 + LJ_STACK_EXTRA;
  57. GCobj *up;
  58. lj_assertL((MSize)(tvref(L->maxstack)-oldst) == L->stacksize-LJ_STACK_EXTRA-1,
  59. "inconsistent stack size");
  60. st = (TValue *)lj_mem_realloc(L, tvref(L->stack),
  61. (MSize)(oldsize*sizeof(TValue)),
  62. (MSize)(realsize*sizeof(TValue)));
  63. setmref(L->stack, st);
  64. delta = (char *)st - (char *)oldst;
  65. setmref(L->maxstack, st + n);
  66. while (oldsize < realsize) /* Clear new slots. */
  67. setnilV(st + oldsize++);
  68. L->stacksize = realsize;
  69. if ((size_t)(mref(G(L)->jit_base, char) - (char *)oldst) < oldsize)
  70. setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta);
  71. L->base = (TValue *)((char *)L->base + delta);
  72. L->top = (TValue *)((char *)L->top + delta);
  73. for (up = gcref(L->openupval); up != NULL; up = gcnext(up))
  74. setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta));
  75. }
  76. /* Relimit stack after error, in case the limit was overdrawn. */
  77. void lj_state_relimitstack(lua_State *L)
  78. {
  79. if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1)
  80. resizestack(L, LJ_STACK_MAX);
  81. }
  82. /* Try to shrink the stack (called from GC). */
  83. void lj_state_shrinkstack(lua_State *L, MSize used)
  84. {
  85. if (L->stacksize > LJ_STACK_MAXEX)
  86. return; /* Avoid stack shrinking while handling stack overflow. */
  87. if (4*used < L->stacksize &&
  88. 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize &&
  89. /* Don't shrink stack of live trace. */
  90. (tvref(G(L)->jit_base) == NULL || obj2gco(L) != gcref(G(L)->cur_L)))
  91. resizestack(L, L->stacksize >> 1);
  92. }
  93. /* Try to grow stack. */
  94. void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)
  95. {
  96. MSize n;
  97. if (L->stacksize >= LJ_STACK_MAXEX) {
  98. /* 4. Throw 'error in error handling' when we are _over_ the limit. */
  99. if (L->stacksize > LJ_STACK_MAXEX)
  100. lj_err_throw(L, LUA_ERRERR); /* Does not invoke an error handler. */
  101. /* 1. We are _at_ the limit after the last growth. */
  102. if (L->status < LUA_ERRRUN) { /* 2. Throw 'stack overflow'. */
  103. L->status = LUA_ERRRUN; /* Prevent ending here again for pushed msg. */
  104. lj_err_msg(L, LJ_ERR_STKOV); /* May invoke an error handler. */
  105. }
  106. /* 3. Add space (over the limit) for pushed message and error handler. */
  107. }
  108. n = L->stacksize + need;
  109. if (n > LJ_STACK_MAX) {
  110. n += 2*LUA_MINSTACK;
  111. } else if (n < 2*L->stacksize) {
  112. n = 2*L->stacksize;
  113. if (n >= LJ_STACK_MAX)
  114. n = LJ_STACK_MAX;
  115. }
  116. resizestack(L, n);
  117. }
  118. void LJ_FASTCALL lj_state_growstack1(lua_State *L)
  119. {
  120. lj_state_growstack(L, 1);
  121. }
  122. static TValue *cpgrowstack(lua_State *co, lua_CFunction dummy, void *ud)
  123. {
  124. UNUSED(dummy);
  125. lj_state_growstack(co, *(MSize *)ud);
  126. return NULL;
  127. }
  128. int LJ_FASTCALL lj_state_cpgrowstack(lua_State *L, MSize need)
  129. {
  130. return lj_vm_cpcall(L, NULL, &need, cpgrowstack);
  131. }
  132. /* Allocate basic stack for new state. */
  133. static void stack_init(lua_State *L1, lua_State *L)
  134. {
  135. TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue);
  136. setmref(L1->stack, st);
  137. L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA;
  138. stend = st + L1->stacksize;
  139. setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1);
  140. setthreadV(L1, st++, L1); /* Needed for curr_funcisL() on empty stack. */
  141. if (LJ_FR2) setnilV(st++);
  142. L1->base = L1->top = st;
  143. while (st < stend) /* Clear new slots. */
  144. setnilV(st++);
  145. }
  146. /* -- State handling ------------------------------------------------------ */
  147. /* Open parts that may cause memory-allocation errors. */
  148. static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud)
  149. {
  150. global_State *g = G(L);
  151. UNUSED(dummy);
  152. UNUSED(ud);
  153. stack_init(L, L);
  154. /* NOBARRIER: State initialization, all objects are white. */
  155. setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL)));
  156. settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY));
  157. lj_str_init(L);
  158. lj_meta_init(L);
  159. lj_lex_init(L);
  160. fixstring(lj_err_str(L, LJ_ERR_ERRMEM)); /* Preallocate memory error msg. */
  161. g->gc.threshold = 4*g->gc.total;
  162. lj_trace_initstate(g);
  163. lj_err_verify();
  164. return NULL;
  165. }
  166. static void close_state(lua_State *L)
  167. {
  168. global_State *g = G(L);
  169. lj_func_closeuv(L, tvref(L->stack));
  170. lj_gc_freeall(g);
  171. lj_assertG(gcref(g->gc.root) == obj2gco(L),
  172. "main thread is not first GC object");
  173. lj_assertG(g->str.num == 0, "leaked %d strings", g->str.num);
  174. lj_trace_freestate(g);
  175. #if LJ_HASFFI
  176. lj_ctype_freestate(g);
  177. #endif
  178. lj_str_freetab(g);
  179. lj_buf_free(g, &g->tmpbuf);
  180. lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
  181. #if LJ_64
  182. if (mref(g->gc.lightudseg, uint32_t)) {
  183. MSize segnum = g->gc.lightudnum ? (2 << lj_fls(g->gc.lightudnum)) : 2;
  184. lj_mem_freevec(g, mref(g->gc.lightudseg, uint32_t), segnum, uint32_t);
  185. }
  186. #endif
  187. lj_assertG(g->gc.total == sizeof(GG_State),
  188. "memory leak of %lld bytes",
  189. (long long)(g->gc.total - sizeof(GG_State)));
  190. #ifndef LUAJIT_USE_SYSMALLOC
  191. if (g->allocf == lj_alloc_f)
  192. lj_alloc_destroy(g->allocd);
  193. else
  194. #endif
  195. g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0);
  196. }
  197. #if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC))
  198. lua_State *lj_state_newstate(lua_Alloc allocf, void *allocd)
  199. #else
  200. LUA_API lua_State *lua_newstate(lua_Alloc allocf, void *allocd)
  201. #endif
  202. {
  203. PRNGState prng;
  204. GG_State *GG;
  205. lua_State *L;
  206. global_State *g;
  207. /* We need the PRNG for the memory allocator, so initialize this first. */
  208. if (!lj_prng_seed_secure(&prng)) {
  209. lj_assertX(0, "secure PRNG seeding failed");
  210. /* Can only return NULL here, so this errors with "not enough memory". */
  211. return NULL;
  212. }
  213. #ifndef LUAJIT_USE_SYSMALLOC
  214. if (allocf == LJ_ALLOCF_INTERNAL) {
  215. allocd = lj_alloc_create(&prng);
  216. if (!allocd) return NULL;
  217. allocf = lj_alloc_f;
  218. }
  219. #endif
  220. GG = (GG_State *)allocf(allocd, NULL, 0, sizeof(GG_State));
  221. if (GG == NULL || !checkptrGC(GG)) return NULL;
  222. memset(GG, 0, sizeof(GG_State));
  223. L = &GG->L;
  224. g = &GG->g;
  225. L->gct = ~LJ_TTHREAD;
  226. L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */
  227. L->dummy_ffid = FF_C;
  228. setmref(L->glref, g);
  229. g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED;
  230. g->strempty.marked = LJ_GC_WHITE0;
  231. g->strempty.gct = ~LJ_TSTR;
  232. g->allocf = allocf;
  233. g->allocd = allocd;
  234. g->prng = prng;
  235. #ifndef LUAJIT_USE_SYSMALLOC
  236. if (allocf == lj_alloc_f) {
  237. lj_alloc_setprng(allocd, &g->prng);
  238. }
  239. #endif
  240. setgcref(g->mainthref, obj2gco(L));
  241. setgcref(g->uvhead.prev, obj2gco(&g->uvhead));
  242. setgcref(g->uvhead.next, obj2gco(&g->uvhead));
  243. g->str.mask = ~(MSize)0;
  244. setnilV(registry(L));
  245. setnilV(&g->nilnode.val);
  246. setnilV(&g->nilnode.key);
  247. #if !LJ_GC64
  248. setmref(g->nilnode.freetop, &g->nilnode);
  249. #endif
  250. lj_buf_init(NULL, &g->tmpbuf);
  251. g->gc.state = GCSpause;
  252. setgcref(g->gc.root, obj2gco(L));
  253. setmref(g->gc.sweep, &g->gc.root);
  254. g->gc.total = sizeof(GG_State);
  255. g->gc.pause = LUAI_GCPAUSE;
  256. g->gc.stepmul = LUAI_GCMUL;
  257. lj_dispatch_init((GG_State *)L);
  258. L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory error. */
  259. if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) {
  260. /* Memory allocation error: free partial state. */
  261. close_state(L);
  262. return NULL;
  263. }
  264. L->status = LUA_OK;
  265. return L;
  266. }
  267. static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud)
  268. {
  269. UNUSED(dummy);
  270. UNUSED(ud);
  271. lj_gc_finalize_cdata(L);
  272. lj_gc_finalize_udata(L);
  273. /* Frame pop omitted. */
  274. return NULL;
  275. }
  276. LUA_API void lua_close(lua_State *L)
  277. {
  278. global_State *g = G(L);
  279. int i;
  280. L = mainthread(g); /* Only the main thread can be closed. */
  281. #if LJ_HASPROFILE
  282. luaJIT_profile_stop(L);
  283. #endif
  284. setgcrefnull(g->cur_L);
  285. lj_func_closeuv(L, tvref(L->stack));
  286. lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */
  287. #if LJ_HASJIT
  288. G2J(g)->flags &= ~JIT_F_ON;
  289. G2J(g)->state = LJ_TRACE_IDLE;
  290. lj_dispatch_update(g);
  291. #endif
  292. for (i = 0;;) {
  293. hook_enter(g);
  294. L->status = LUA_OK;
  295. L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
  296. L->cframe = NULL;
  297. if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == LUA_OK) {
  298. if (++i >= 10) break;
  299. lj_gc_separateudata(g, 1); /* Separate udata again. */
  300. if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */
  301. break;
  302. }
  303. }
  304. close_state(L);
  305. }
  306. lua_State *lj_state_new(lua_State *L)
  307. {
  308. lua_State *L1 = lj_mem_newobj(L, lua_State);
  309. L1->gct = ~LJ_TTHREAD;
  310. L1->dummy_ffid = FF_C;
  311. L1->status = LUA_OK;
  312. L1->stacksize = 0;
  313. setmref(L1->stack, NULL);
  314. L1->cframe = NULL;
  315. /* NOBARRIER: The lua_State is new (marked white). */
  316. setgcrefnull(L1->openupval);
  317. setmrefr(L1->glref, L->glref);
  318. setgcrefr(L1->env, L->env);
  319. stack_init(L1, L); /* init stack */
  320. lj_assertL(iswhite(obj2gco(L1)), "new thread object is not white");
  321. return L1;
  322. }
  323. void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L)
  324. {
  325. lj_assertG(L != mainthread(g), "free of main thread");
  326. if (obj2gco(L) == gcref(g->cur_L))
  327. setgcrefnull(g->cur_L);
  328. if (gcref(L->openupval) != NULL) {
  329. lj_func_closeuv(L, tvref(L->stack));
  330. lj_trace_abort(g); /* For aa_uref soundness. */
  331. lj_assertG(gcref(L->openupval) == NULL, "stale open upvalues");
  332. }
  333. lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
  334. lj_mem_freet(g, L);
  335. }