OSX/iOS: Fix SDK incompatibility.
[luajit-2.0.git] / src / lj_state.c
blob6fd7d9ced6006cf5ed0d4a42efaf08bea2f9ebff
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 = L->stacksize + need;
106 if (LJ_LIKELY(n < LJ_STACK_MAX)) { /* The stack can grow as requested. */
107 if (n < 2 * L->stacksize) { /* Try to double the size. */
108 n = 2 * L->stacksize;
109 if (n > LJ_STACK_MAX)
110 n = LJ_STACK_MAX;
112 resizestack(L, n);
113 } else { /* Request would overflow. Raise a stack overflow error. */
114 if (LJ_HASJIT) {
115 TValue *base = tvref(G(L)->jit_base);
116 if (base) L->base = base;
118 if (curr_funcisL(L)) {
119 L->top = curr_topL(L);
120 if (L->top > tvref(L->maxstack)) {
121 /* The current Lua frame violates the stack, so replace it with a
122 ** dummy. This can happen when BC_IFUNCF is trying to grow the stack.
124 L->top = L->base;
125 setframe_gc(L->base - 1 - LJ_FR2, obj2gco(L), LJ_TTHREAD);
128 if (L->stacksize <= LJ_STACK_MAXEX) {
129 /* An error handler might want to inspect the stack overflow error, but
130 ** will need some stack space to run in. We give it a stack size beyond
131 ** the normal limit in order to do so, then rely on lj_state_relimitstack
132 ** calls during unwinding to bring us back to a convential stack size.
133 ** The + 1 is space for the error message, and 2 * LUA_MINSTACK is for
134 ** the lj_state_checkstack() call in lj_err_run().
136 resizestack(L, LJ_STACK_MAX + 1 + 2 * LUA_MINSTACK);
137 lj_err_stkov(L); /* May invoke an error handler. */
138 } else {
139 /* If we're here, then the stack overflow error handler is requesting
140 ** to grow the stack even further. We have no choice but to abort the
141 ** error handler.
143 GCstr *em = lj_err_str(L, LJ_ERR_STKOV); /* Might OOM. */
144 setstrV(L, L->top++, em); /* There is always space to push an error. */
145 lj_err_throw(L, LUA_ERRERR); /* Does not invoke an error handler. */
150 void LJ_FASTCALL lj_state_growstack1(lua_State *L)
152 lj_state_growstack(L, 1);
155 static TValue *cpgrowstack(lua_State *co, lua_CFunction dummy, void *ud)
157 UNUSED(dummy);
158 lj_state_growstack(co, *(MSize *)ud);
159 return NULL;
162 int LJ_FASTCALL lj_state_cpgrowstack(lua_State *L, MSize need)
164 return lj_vm_cpcall(L, NULL, &need, cpgrowstack);
167 /* Allocate basic stack for new state. */
168 static void stack_init(lua_State *L1, lua_State *L)
170 TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue);
171 setmref(L1->stack, st);
172 L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA;
173 stend = st + L1->stacksize;
174 setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1);
175 setthreadV(L1, st++, L1); /* Needed for curr_funcisL() on empty stack. */
176 if (LJ_FR2) setnilV(st++);
177 L1->base = L1->top = st;
178 while (st < stend) /* Clear new slots. */
179 setnilV(st++);
182 /* -- State handling ------------------------------------------------------ */
184 /* Open parts that may cause memory-allocation errors. */
185 static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud)
187 global_State *g = G(L);
188 UNUSED(dummy);
189 UNUSED(ud);
190 stack_init(L, L);
191 /* NOBARRIER: State initialization, all objects are white. */
192 setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL)));
193 settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY));
194 lj_str_init(L);
195 lj_meta_init(L);
196 lj_lex_init(L);
197 fixstring(lj_err_str(L, LJ_ERR_ERRMEM)); /* Preallocate memory error msg. */
198 g->gc.threshold = 4*g->gc.total;
199 #if LJ_HASFFI
200 lj_ctype_initfin(L);
201 #endif
202 lj_trace_initstate(g);
203 lj_err_verify();
204 return NULL;
207 static void close_state(lua_State *L)
209 global_State *g = G(L);
210 lj_func_closeuv(L, tvref(L->stack));
211 lj_gc_freeall(g);
212 lj_assertG(gcref(g->gc.root) == obj2gco(L),
213 "main thread is not first GC object");
214 lj_assertG(g->str.num == 0, "leaked %d strings", g->str.num);
215 lj_trace_freestate(g);
216 #if LJ_HASFFI
217 lj_ctype_freestate(g);
218 #endif
219 lj_str_freetab(g);
220 lj_buf_free(g, &g->tmpbuf);
221 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
222 #if LJ_64
223 if (mref(g->gc.lightudseg, uint32_t)) {
224 MSize segnum = g->gc.lightudnum ? (2 << lj_fls(g->gc.lightudnum)) : 2;
225 lj_mem_freevec(g, mref(g->gc.lightudseg, uint32_t), segnum, uint32_t);
227 #endif
228 lj_assertG(g->gc.total == sizeof(GG_State),
229 "memory leak of %lld bytes",
230 (long long)(g->gc.total - sizeof(GG_State)));
231 #ifndef LUAJIT_USE_SYSMALLOC
232 if (g->allocf == lj_alloc_f)
233 lj_alloc_destroy(g->allocd);
234 else
235 #endif
236 g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0);
239 #if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC))
240 lua_State *lj_state_newstate(lua_Alloc allocf, void *allocd)
241 #else
242 LUA_API lua_State *lua_newstate(lua_Alloc allocf, void *allocd)
243 #endif
245 PRNGState prng;
246 GG_State *GG;
247 lua_State *L;
248 global_State *g;
249 /* We need the PRNG for the memory allocator, so initialize this first. */
250 if (!lj_prng_seed_secure(&prng)) {
251 lj_assertX(0, "secure PRNG seeding failed");
252 /* Can only return NULL here, so this errors with "not enough memory". */
253 return NULL;
255 #ifndef LUAJIT_USE_SYSMALLOC
256 if (allocf == LJ_ALLOCF_INTERNAL) {
257 allocd = lj_alloc_create(&prng);
258 if (!allocd) return NULL;
259 allocf = lj_alloc_f;
261 #endif
262 GG = (GG_State *)allocf(allocd, NULL, 0, sizeof(GG_State));
263 if (GG == NULL || !checkptrGC(GG)) return NULL;
264 memset(GG, 0, sizeof(GG_State));
265 L = &GG->L;
266 g = &GG->g;
267 L->gct = ~LJ_TTHREAD;
268 L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */
269 L->dummy_ffid = FF_C;
270 setmref(L->glref, g);
271 g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED;
272 g->strempty.marked = LJ_GC_WHITE0;
273 g->strempty.gct = ~LJ_TSTR;
274 g->allocf = allocf;
275 g->allocd = allocd;
276 g->prng = prng;
277 #ifndef LUAJIT_USE_SYSMALLOC
278 if (allocf == lj_alloc_f) {
279 lj_alloc_setprng(allocd, &g->prng);
281 #endif
282 setgcref(g->mainthref, obj2gco(L));
283 setgcref(g->uvhead.prev, obj2gco(&g->uvhead));
284 setgcref(g->uvhead.next, obj2gco(&g->uvhead));
285 g->str.mask = ~(MSize)0;
286 setnilV(registry(L));
287 setnilV(&g->nilnode.val);
288 setnilV(&g->nilnode.key);
289 #if !LJ_GC64
290 setmref(g->nilnode.freetop, &g->nilnode);
291 #endif
292 lj_buf_init(NULL, &g->tmpbuf);
293 g->gc.state = GCSpause;
294 setgcref(g->gc.root, obj2gco(L));
295 setmref(g->gc.sweep, &g->gc.root);
296 g->gc.total = sizeof(GG_State);
297 g->gc.pause = LUAI_GCPAUSE;
298 g->gc.stepmul = LUAI_GCMUL;
299 lj_dispatch_init((GG_State *)L);
300 L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory error. */
301 if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) {
302 /* Memory allocation error: free partial state. */
303 close_state(L);
304 return NULL;
306 L->status = LUA_OK;
307 return L;
310 static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud)
312 UNUSED(dummy);
313 UNUSED(ud);
314 lj_gc_finalize_cdata(L);
315 lj_gc_finalize_udata(L);
316 /* Frame pop omitted. */
317 return NULL;
320 LUA_API void lua_close(lua_State *L)
322 global_State *g = G(L);
323 int i;
324 L = mainthread(g); /* Only the main thread can be closed. */
325 #if LJ_HASPROFILE
326 luaJIT_profile_stop(L);
327 #endif
328 setgcrefnull(g->cur_L);
329 lj_func_closeuv(L, tvref(L->stack));
330 lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */
331 #if LJ_HASJIT
332 G2J(g)->flags &= ~JIT_F_ON;
333 G2J(g)->state = LJ_TRACE_IDLE;
334 lj_dispatch_update(g);
335 #endif
336 for (i = 0;;) {
337 hook_enter(g);
338 L->status = LUA_OK;
339 L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
340 L->cframe = NULL;
341 if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == LUA_OK) {
342 if (++i >= 10) break;
343 lj_gc_separateudata(g, 1); /* Separate udata again. */
344 if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */
345 break;
348 close_state(L);
351 lua_State *lj_state_new(lua_State *L)
353 lua_State *L1 = lj_mem_newobj(L, lua_State);
354 L1->gct = ~LJ_TTHREAD;
355 L1->dummy_ffid = FF_C;
356 L1->status = LUA_OK;
357 L1->stacksize = 0;
358 setmref(L1->stack, NULL);
359 L1->cframe = NULL;
360 /* NOBARRIER: The lua_State is new (marked white). */
361 setgcrefnull(L1->openupval);
362 setmrefr(L1->glref, L->glref);
363 setgcrefr(L1->env, L->env);
364 stack_init(L1, L); /* init stack */
365 lj_assertL(iswhite(obj2gco(L1)), "new thread object is not white");
366 return L1;
369 void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L)
371 lj_assertG(L != mainthread(g), "free of main thread");
372 if (obj2gco(L) == gcref(g->cur_L))
373 setgcrefnull(g->cur_L);
374 if (gcref(L->openupval) != NULL) {
375 lj_func_closeuv(L, tvref(L->stack));
376 lj_trace_abort(g); /* For aa_uref soundness. */
377 lj_assertG(gcref(L->openupval) == NULL, "stale open upvalues");
379 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
380 lj_mem_freet(g, L);