2 ** State and stack handling.
3 ** Copyright (C) 2005-2015 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"
32 /* -- Stack handling ------------------------------------------------------ */
35 #define LJ_STACK_MIN LUA_MINSTACK /* Min. stack size. */
36 #define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */
37 #define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */
38 #define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA)
40 /* Explanation of LJ_STACK_EXTRA:
42 ** Calls to metamethods store their arguments beyond the current top
43 ** without checking for the stack limit. This avoids stack resizes which
44 ** would invalidate passed TValue pointers. The stack check is performed
45 ** later by the function header. This can safely resize the stack or raise
46 ** an error. Thus we need some extra slots beyond the current stack limit.
48 ** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus
49 ** one extra slot if mobj is not a function. Only lj_meta_tset needs 5
50 ** slots above top, but then mobj is always a function. So we can get by
51 ** with 5 extra slots.
52 ** LJ_FR2: We need 2 more slots for the frame PC and the continuation PC.
55 /* Resize stack slots and adjust pointers in state. */
56 static void resizestack(lua_State
*L
, MSize n
)
58 TValue
*st
, *oldst
= tvref(L
->stack
);
60 MSize oldsize
= L
->stacksize
;
61 MSize realsize
= n
+ 1 + LJ_STACK_EXTRA
;
63 lua_assert((MSize
)(tvref(L
->maxstack
)-oldst
)==L
->stacksize
-LJ_STACK_EXTRA
-1);
64 st
= (TValue
*)lj_mem_realloc(L
, tvref(L
->stack
),
65 (MSize
)(oldsize
*sizeof(TValue
)),
66 (MSize
)(realsize
*sizeof(TValue
)));
67 setmref(L
->stack
, st
);
68 delta
= (char *)st
- (char *)oldst
;
69 setmref(L
->maxstack
, st
+ n
);
70 while (oldsize
< realsize
) /* Clear new slots. */
71 setnilV(st
+ oldsize
++);
72 L
->stacksize
= realsize
;
73 if ((size_t)(mref(G(L
)->jit_base
, char) - (char *)oldst
) < oldsize
)
74 setmref(G(L
)->jit_base
, mref(G(L
)->jit_base
, char) + delta
);
75 L
->base
= (TValue
*)((char *)L
->base
+ delta
);
76 L
->top
= (TValue
*)((char *)L
->top
+ delta
);
77 for (up
= gcref(L
->openupval
); up
!= NULL
; up
= gcnext(up
))
78 setmref(gco2uv(up
)->v
, (TValue
*)((char *)uvval(gco2uv(up
)) + delta
));
81 /* Relimit stack after error, in case the limit was overdrawn. */
82 void lj_state_relimitstack(lua_State
*L
)
84 if (L
->stacksize
> LJ_STACK_MAXEX
&& L
->top
-tvref(L
->stack
) < LJ_STACK_MAX
-1)
85 resizestack(L
, LJ_STACK_MAX
);
88 /* Try to shrink the stack (called from GC). */
89 void lj_state_shrinkstack(lua_State
*L
, MSize used
)
91 if (L
->stacksize
> LJ_STACK_MAXEX
)
92 return; /* Avoid stack shrinking while handling stack overflow. */
93 if (4*used
< L
->stacksize
&&
94 2*(LJ_STACK_START
+LJ_STACK_EXTRA
) < L
->stacksize
&&
95 /* Don't shrink stack of live trace. */
96 (tvref(G(L
)->jit_base
) == NULL
|| obj2gco(L
) != gcref(G(L
)->cur_L
)))
97 resizestack(L
, L
->stacksize
>> 1);
100 /* Try to grow stack. */
101 void LJ_FASTCALL
lj_state_growstack(lua_State
*L
, MSize need
)
104 if (L
->stacksize
> LJ_STACK_MAXEX
) /* Overflow while handling overflow? */
105 lj_err_throw(L
, LUA_ERRERR
);
106 n
= L
->stacksize
+ need
;
107 if (n
> LJ_STACK_MAX
) {
109 } else if (n
< 2*L
->stacksize
) {
111 if (n
>= LJ_STACK_MAX
)
115 if (L
->stacksize
> LJ_STACK_MAXEX
)
116 lj_err_msg(L
, LJ_ERR_STKOV
);
119 void LJ_FASTCALL
lj_state_growstack1(lua_State
*L
)
121 lj_state_growstack(L
, 1);
124 /* Allocate basic stack for new state. */
125 static void stack_init(lua_State
*L1
, lua_State
*L
)
127 TValue
*stend
, *st
= lj_mem_newvec(L
, LJ_STACK_START
+LJ_STACK_EXTRA
, TValue
);
128 setmref(L1
->stack
, st
);
129 L1
->stacksize
= LJ_STACK_START
+ LJ_STACK_EXTRA
;
130 stend
= st
+ L1
->stacksize
;
131 setmref(L1
->maxstack
, stend
- LJ_STACK_EXTRA
- 1);
132 setthreadV(L1
, st
++, L1
); /* Needed for curr_funcisL() on empty stack. */
133 if (LJ_FR2
) setnilV(st
++);
134 L1
->base
= L1
->top
= st
;
135 while (st
< stend
) /* Clear new slots. */
139 /* -- State handling ------------------------------------------------------ */
141 /* Open parts that may cause memory-allocation errors. */
142 static TValue
*cpluaopen(lua_State
*L
, lua_CFunction dummy
, void *ud
)
144 global_State
*g
= G(L
);
148 /* NOBARRIER: State initialization, all objects are white. */
149 setgcref(L
->env
, obj2gco(lj_tab_new(L
, 0, LJ_MIN_GLOBAL
)));
150 settabV(L
, registry(L
), lj_tab_new(L
, 0, LJ_MIN_REGISTRY
));
151 lj_str_resize(L
, LJ_MIN_STRTAB
-1);
154 fixstring(lj_err_str(L
, LJ_ERR_ERRMEM
)); /* Preallocate memory error msg. */
155 g
->gc
.threshold
= 4*g
->gc
.total
;
156 lj_trace_initstate(g
);
160 static void close_state(lua_State
*L
)
162 global_State
*g
= G(L
);
163 lj_func_closeuv(L
, tvref(L
->stack
));
165 lua_assert(gcref(g
->gc
.root
) == obj2gco(L
));
166 lua_assert(g
->strnum
== 0);
167 lj_trace_freestate(g
);
169 lj_ctype_freestate(g
);
171 lj_mem_freevec(g
, g
->strhash
, g
->strmask
+1, GCRef
);
172 lj_buf_free(g
, &g
->tmpbuf
);
173 lj_mem_freevec(g
, tvref(L
->stack
), L
->stacksize
, TValue
);
174 lua_assert(g
->gc
.total
== sizeof(GG_State
));
175 #ifndef LUAJIT_USE_SYSMALLOC
176 if (g
->allocf
== lj_alloc_f
)
177 lj_alloc_destroy(g
->allocd
);
180 g
->allocf(g
->allocd
, G2GG(g
), sizeof(GG_State
), 0);
183 #if LJ_64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC))
184 lua_State
*lj_state_newstate(lua_Alloc f
, void *ud
)
186 LUA_API lua_State
*lua_newstate(lua_Alloc f
, void *ud
)
189 GG_State
*GG
= (GG_State
*)f(ud
, NULL
, 0, sizeof(GG_State
));
190 lua_State
*L
= &GG
->L
;
191 global_State
*g
= &GG
->g
;
192 if (GG
== NULL
|| !checkptrGC(GG
)) return NULL
;
193 memset(GG
, 0, sizeof(GG_State
));
194 L
->gct
= ~LJ_TTHREAD
;
195 L
->marked
= LJ_GC_WHITE0
| LJ_GC_FIXED
| LJ_GC_SFIXED
; /* Prevent free. */
196 L
->dummy_ffid
= FF_C
;
197 setmref(L
->glref
, g
);
198 g
->gc
.currentwhite
= LJ_GC_WHITE0
| LJ_GC_FIXED
;
199 g
->strempty
.marked
= LJ_GC_WHITE0
;
200 g
->strempty
.gct
= ~LJ_TSTR
;
203 setgcref(g
->mainthref
, obj2gco(L
));
204 setgcref(g
->uvhead
.prev
, obj2gco(&g
->uvhead
));
205 setgcref(g
->uvhead
.next
, obj2gco(&g
->uvhead
));
206 g
->strmask
= ~(MSize
)0;
207 setnilV(registry(L
));
208 setnilV(&g
->nilnode
.val
);
209 setnilV(&g
->nilnode
.key
);
211 setmref(g
->nilnode
.freetop
, &g
->nilnode
);
213 lj_buf_init(NULL
, &g
->tmpbuf
);
214 g
->gc
.state
= GCSpause
;
215 setgcref(g
->gc
.root
, obj2gco(L
));
216 setmref(g
->gc
.sweep
, &g
->gc
.root
);
217 g
->gc
.total
= sizeof(GG_State
);
218 g
->gc
.pause
= LUAI_GCPAUSE
;
219 g
->gc
.stepmul
= LUAI_GCMUL
;
220 lj_dispatch_init((GG_State
*)L
);
221 L
->status
= LUA_ERRERR
+1; /* Avoid touching the stack upon memory error. */
222 if (lj_vm_cpcall(L
, NULL
, NULL
, cpluaopen
) != 0) {
223 /* Memory allocation error: free partial state. */
231 static TValue
*cpfinalize(lua_State
*L
, lua_CFunction dummy
, void *ud
)
235 lj_gc_finalize_cdata(L
);
236 lj_gc_finalize_udata(L
);
237 /* Frame pop omitted. */
241 LUA_API
void lua_close(lua_State
*L
)
243 global_State
*g
= G(L
);
245 L
= mainthread(g
); /* Only the main thread can be closed. */
247 luaJIT_profile_stop(L
);
249 setgcrefnull(g
->cur_L
);
250 lj_func_closeuv(L
, tvref(L
->stack
));
251 lj_gc_separateudata(g
, 1); /* Separate udata which have GC metamethods. */
253 G2J(g
)->flags
&= ~JIT_F_ON
;
254 G2J(g
)->state
= LJ_TRACE_IDLE
;
255 lj_dispatch_update(g
);
260 L
->base
= L
->top
= tvref(L
->stack
) + 1 + LJ_FR2
;
262 if (lj_vm_cpcall(L
, NULL
, NULL
, cpfinalize
) == 0) {
263 if (++i
>= 10) break;
264 lj_gc_separateudata(g
, 1); /* Separate udata again. */
265 if (gcref(g
->gc
.mmudata
) == NULL
) /* Until nothing is left to do. */
272 lua_State
*lj_state_new(lua_State
*L
)
274 lua_State
*L1
= lj_mem_newobj(L
, lua_State
);
275 L1
->gct
= ~LJ_TTHREAD
;
276 L1
->dummy_ffid
= FF_C
;
279 setmref(L1
->stack
, NULL
);
281 /* NOBARRIER: The lua_State is new (marked white). */
282 setgcrefnull(L1
->openupval
);
283 setmrefr(L1
->glref
, L
->glref
);
284 setgcrefr(L1
->env
, L
->env
);
285 stack_init(L1
, L
); /* init stack */
286 lua_assert(iswhite(obj2gco(L1
)));
290 void LJ_FASTCALL
lj_state_free(global_State
*g
, lua_State
*L
)
292 lua_assert(L
!= mainthread(g
));
293 if (obj2gco(L
) == gcref(g
->cur_L
))
294 setgcrefnull(g
->cur_L
);
295 lj_func_closeuv(L
, tvref(L
->stack
));
296 lua_assert(gcref(L
->openupval
) == NULL
);
297 lj_mem_freevec(g
, tvref(L
->stack
), L
->stacksize
, TValue
);