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