2 ** State and stack handling.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
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
26 #include "lj_dispatch.h"
33 /* -- Stack handling ------------------------------------------------------ */
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
);
61 MSize oldsize
= L
->stacksize
;
62 MSize realsize
= n
+ 1 + LJ_STACK_EXTRA
;
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
)
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
) {
120 } else if (n
< 2*L
->stacksize
) {
122 if (n
>= LJ_STACK_MAX
)
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
)
136 lj_state_growstack(co
, *(MSize
*)ud
);
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. */
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
);
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
));
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
);
182 static void close_state(lua_State
*L
)
184 global_State
*g
= G(L
);
185 lj_func_closeuv(L
, tvref(L
->stack
));
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
);
192 lj_ctype_freestate(g
);
195 lj_buf_free(g
, &g
->tmpbuf
);
196 lj_mem_freevec(g
, tvref(L
->stack
), L
->stacksize
, TValue
);
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);
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
);
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
)
217 LUA_API lua_State
*lua_newstate(lua_Alloc allocf
, void *allocd
)
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". */
230 #ifndef LUAJIT_USE_SYSMALLOC
231 if (allocf
== LJ_ALLOCF_INTERNAL
) {
232 allocd
= lj_alloc_create(&prng
);
233 if (!allocd
) return NULL
;
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
));
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
;
252 #ifndef LUAJIT_USE_SYSMALLOC
253 if (allocf
== lj_alloc_f
) {
254 lj_alloc_setprng(allocd
, &g
->prng
);
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
);
265 setmref(g
->nilnode
.freetop
, &g
->nilnode
);
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. */
285 static TValue
*cpfinalize(lua_State
*L
, lua_CFunction dummy
, void *ud
)
289 lj_gc_finalize_cdata(L
);
290 lj_gc_finalize_udata(L
);
291 /* Frame pop omitted. */
295 LUA_API
void lua_close(lua_State
*L
)
297 global_State
*g
= G(L
);
299 L
= mainthread(g
); /* Only the main thread can be closed. */
301 luaJIT_profile_stop(L
);
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. */
307 G2J(g
)->flags
&= ~JIT_F_ON
;
308 G2J(g
)->state
= LJ_TRACE_IDLE
;
309 lj_dispatch_update(g
);
314 L
->base
= L
->top
= tvref(L
->stack
) + 1 + LJ_FR2
;
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. */
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
;
333 setmref(L1
->stack
, 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");
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
);