1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy ** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $
4eda14cbcSMatt Macy ** Global State
5eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6eda14cbcSMatt Macy */
7eda14cbcSMatt Macy
8eda14cbcSMatt Macy
9eda14cbcSMatt Macy #define lstate_c
10eda14cbcSMatt Macy #define LUA_CORE
11eda14cbcSMatt Macy
12eda14cbcSMatt Macy #include <sys/lua/lua.h>
13eda14cbcSMatt Macy
14eda14cbcSMatt Macy #include "lapi.h"
15eda14cbcSMatt Macy #include "ldebug.h"
16eda14cbcSMatt Macy #include "ldo.h"
17eda14cbcSMatt Macy #include "lfunc.h"
18eda14cbcSMatt Macy #include "lgc.h"
19eda14cbcSMatt Macy #include "llex.h"
20eda14cbcSMatt Macy #include "lmem.h"
21eda14cbcSMatt Macy #include "lstate.h"
22eda14cbcSMatt Macy #include "lstring.h"
23eda14cbcSMatt Macy #include "ltable.h"
24eda14cbcSMatt Macy #include "ltm.h"
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy #if !defined(LUAI_GCPAUSE)
28eda14cbcSMatt Macy #define LUAI_GCPAUSE 200 /* 200% */
29eda14cbcSMatt Macy #endif
30eda14cbcSMatt Macy
31eda14cbcSMatt Macy #if !defined(LUAI_GCMAJOR)
32eda14cbcSMatt Macy #define LUAI_GCMAJOR 200 /* 200% */
33eda14cbcSMatt Macy #endif
34eda14cbcSMatt Macy
35eda14cbcSMatt Macy #if !defined(LUAI_GCMUL)
36eda14cbcSMatt Macy #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
37eda14cbcSMatt Macy #endif
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy
40eda14cbcSMatt Macy #define MEMERRMSG "not enough memory"
41eda14cbcSMatt Macy
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy /*
44eda14cbcSMatt Macy ** a macro to help the creation of a unique random seed when a state is
45eda14cbcSMatt Macy ** created; the seed is used to randomize hashes.
46eda14cbcSMatt Macy */
47eda14cbcSMatt Macy #if !defined(luai_makeseed)
48eda14cbcSMatt Macy #define luai_makeseed() cast(unsigned int, gethrtime())
49eda14cbcSMatt Macy #endif
50eda14cbcSMatt Macy
51eda14cbcSMatt Macy
52eda14cbcSMatt Macy
53eda14cbcSMatt Macy /*
54eda14cbcSMatt Macy ** thread state + extra space
55eda14cbcSMatt Macy */
56eda14cbcSMatt Macy typedef struct LX {
57eda14cbcSMatt Macy #if defined(LUAI_EXTRASPACE)
58eda14cbcSMatt Macy char buff[LUAI_EXTRASPACE];
59eda14cbcSMatt Macy #endif
60eda14cbcSMatt Macy lua_State l;
61eda14cbcSMatt Macy } LX;
62eda14cbcSMatt Macy
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy /*
65eda14cbcSMatt Macy ** Main thread combines a thread state and the global state
66eda14cbcSMatt Macy */
67eda14cbcSMatt Macy typedef struct LG {
68eda14cbcSMatt Macy LX l;
69eda14cbcSMatt Macy global_State g;
70eda14cbcSMatt Macy } LG;
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy
73eda14cbcSMatt Macy
74eda14cbcSMatt Macy #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
75eda14cbcSMatt Macy
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy /*
78eda14cbcSMatt Macy ** Compute an initial seed as random as possible. In ANSI, rely on
79eda14cbcSMatt Macy ** Address Space Layout Randomization (if present) to increase
80eda14cbcSMatt Macy ** randomness..
81eda14cbcSMatt Macy */
82eda14cbcSMatt Macy #define addbuff(b,p,e) \
83eda14cbcSMatt Macy { size_t t = cast(size_t, e); \
84eda14cbcSMatt Macy memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
85eda14cbcSMatt Macy
makeseed(lua_State * L)86eda14cbcSMatt Macy static unsigned int makeseed (lua_State *L) {
87eda14cbcSMatt Macy char buff[4 * sizeof(size_t)];
88eda14cbcSMatt Macy unsigned int h = luai_makeseed();
89eda14cbcSMatt Macy int p = 0;
90eda14cbcSMatt Macy addbuff(buff, p, L); /* heap variable */
91eda14cbcSMatt Macy addbuff(buff, p, &h); /* local variable */
92eda14cbcSMatt Macy addbuff(buff, p, luaO_nilobject); /* global variable */
93eda14cbcSMatt Macy addbuff(buff, p, &lua_newstate); /* public function */
94eda14cbcSMatt Macy lua_assert(p == sizeof(buff));
95eda14cbcSMatt Macy return luaS_hash(buff, p, h);
96eda14cbcSMatt Macy }
97eda14cbcSMatt Macy
98eda14cbcSMatt Macy
99eda14cbcSMatt Macy /*
100eda14cbcSMatt Macy ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
101eda14cbcSMatt Macy ** invariant
102eda14cbcSMatt Macy */
luaE_setdebt(global_State * g,l_mem debt)103eda14cbcSMatt Macy void luaE_setdebt (global_State *g, l_mem debt) {
104eda14cbcSMatt Macy g->totalbytes -= (debt - g->GCdebt);
105eda14cbcSMatt Macy g->GCdebt = debt;
106eda14cbcSMatt Macy }
107eda14cbcSMatt Macy
108eda14cbcSMatt Macy
luaE_extendCI(lua_State * L)109eda14cbcSMatt Macy CallInfo *luaE_extendCI (lua_State *L) {
110eda14cbcSMatt Macy CallInfo *ci = luaM_new(L, CallInfo);
111eda14cbcSMatt Macy lua_assert(L->ci->next == NULL);
112eda14cbcSMatt Macy L->ci->next = ci;
113eda14cbcSMatt Macy ci->previous = L->ci;
114eda14cbcSMatt Macy ci->next = NULL;
115eda14cbcSMatt Macy return ci;
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy
luaE_freeCI(lua_State * L)119eda14cbcSMatt Macy void luaE_freeCI (lua_State *L) {
120eda14cbcSMatt Macy CallInfo *ci = L->ci;
121eda14cbcSMatt Macy CallInfo *next = ci->next;
122eda14cbcSMatt Macy ci->next = NULL;
123eda14cbcSMatt Macy while ((ci = next) != NULL) {
124eda14cbcSMatt Macy next = ci->next;
125eda14cbcSMatt Macy luaM_free(L, ci);
126eda14cbcSMatt Macy }
127eda14cbcSMatt Macy }
128eda14cbcSMatt Macy
129eda14cbcSMatt Macy
stack_init(lua_State * L1,lua_State * L)130eda14cbcSMatt Macy static void stack_init (lua_State *L1, lua_State *L) {
131eda14cbcSMatt Macy int i; CallInfo *ci;
132eda14cbcSMatt Macy /* initialize stack array */
133eda14cbcSMatt Macy L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
134eda14cbcSMatt Macy L1->stacksize = BASIC_STACK_SIZE;
135eda14cbcSMatt Macy for (i = 0; i < BASIC_STACK_SIZE; i++)
136eda14cbcSMatt Macy setnilvalue(L1->stack + i); /* erase new stack */
137eda14cbcSMatt Macy L1->top = L1->stack;
138eda14cbcSMatt Macy L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
139eda14cbcSMatt Macy /* initialize first ci */
140eda14cbcSMatt Macy ci = &L1->base_ci;
141eda14cbcSMatt Macy ci->next = ci->previous = NULL;
142eda14cbcSMatt Macy ci->callstatus = 0;
143eda14cbcSMatt Macy ci->func = L1->top;
144eda14cbcSMatt Macy setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
145eda14cbcSMatt Macy ci->top = L1->top + LUA_MINSTACK;
146eda14cbcSMatt Macy L1->ci = ci;
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy
149eda14cbcSMatt Macy
freestack(lua_State * L)150eda14cbcSMatt Macy static void freestack (lua_State *L) {
151eda14cbcSMatt Macy if (L->stack == NULL)
152eda14cbcSMatt Macy return; /* stack not completely built yet */
153eda14cbcSMatt Macy L->ci = &L->base_ci; /* free the entire 'ci' list */
154eda14cbcSMatt Macy luaE_freeCI(L);
155eda14cbcSMatt Macy luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
156eda14cbcSMatt Macy }
157eda14cbcSMatt Macy
158eda14cbcSMatt Macy
159eda14cbcSMatt Macy /*
160eda14cbcSMatt Macy ** Create registry table and its predefined values
161eda14cbcSMatt Macy */
init_registry(lua_State * L,global_State * g)162eda14cbcSMatt Macy static void init_registry (lua_State *L, global_State *g) {
163eda14cbcSMatt Macy TValue mt;
164eda14cbcSMatt Macy /* create registry */
165eda14cbcSMatt Macy Table *registry = luaH_new(L);
166eda14cbcSMatt Macy sethvalue(L, &g->l_registry, registry);
167eda14cbcSMatt Macy luaH_resize(L, registry, LUA_RIDX_LAST, 0);
168eda14cbcSMatt Macy /* registry[LUA_RIDX_MAINTHREAD] = L */
169eda14cbcSMatt Macy setthvalue(L, &mt, L);
170eda14cbcSMatt Macy luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
171eda14cbcSMatt Macy /* registry[LUA_RIDX_GLOBALS] = table of globals */
172eda14cbcSMatt Macy sethvalue(L, &mt, luaH_new(L));
173eda14cbcSMatt Macy luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy
176eda14cbcSMatt Macy
177eda14cbcSMatt Macy /*
178eda14cbcSMatt Macy ** open parts of the state that may cause memory-allocation errors
179eda14cbcSMatt Macy */
f_luaopen(lua_State * L,void * ud)180eda14cbcSMatt Macy static void f_luaopen (lua_State *L, void *ud) {
181eda14cbcSMatt Macy global_State *g = G(L);
182eda14cbcSMatt Macy UNUSED(ud);
183eda14cbcSMatt Macy stack_init(L, L); /* init stack */
184eda14cbcSMatt Macy init_registry(L, g);
185eda14cbcSMatt Macy luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
186eda14cbcSMatt Macy luaT_init(L);
187eda14cbcSMatt Macy luaX_init(L);
188eda14cbcSMatt Macy /* pre-create memory-error message */
189eda14cbcSMatt Macy g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
190eda14cbcSMatt Macy luaS_fix(g->memerrmsg); /* it should never be collected */
191eda14cbcSMatt Macy g->gcrunning = 1; /* allow gc */
192eda14cbcSMatt Macy g->version = lua_version(NULL);
193eda14cbcSMatt Macy luai_userstateopen(L);
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy
196eda14cbcSMatt Macy
197eda14cbcSMatt Macy /*
198eda14cbcSMatt Macy ** preinitialize a state with consistent values without allocating
199eda14cbcSMatt Macy ** any memory (to avoid errors)
200eda14cbcSMatt Macy */
preinit_state(lua_State * L,global_State * g)201eda14cbcSMatt Macy static void preinit_state (lua_State *L, global_State *g) {
202eda14cbcSMatt Macy G(L) = g;
203eda14cbcSMatt Macy L->stack = NULL;
204eda14cbcSMatt Macy L->ci = NULL;
205eda14cbcSMatt Macy L->stacksize = 0;
206eda14cbcSMatt Macy L->errorJmp = NULL;
207eda14cbcSMatt Macy L->nCcalls = 0;
208eda14cbcSMatt Macy L->hook = NULL;
209eda14cbcSMatt Macy L->hookmask = 0;
210eda14cbcSMatt Macy L->basehookcount = 0;
211eda14cbcSMatt Macy L->allowhook = 1;
212eda14cbcSMatt Macy resethookcount(L);
213eda14cbcSMatt Macy L->openupval = NULL;
214eda14cbcSMatt Macy L->nny = 1;
215eda14cbcSMatt Macy L->status = LUA_OK;
216eda14cbcSMatt Macy L->errfunc = 0;
217eda14cbcSMatt Macy L->runerror = 0;
218eda14cbcSMatt Macy }
219eda14cbcSMatt Macy
220eda14cbcSMatt Macy
close_state(lua_State * L)221eda14cbcSMatt Macy static void close_state (lua_State *L) {
222eda14cbcSMatt Macy global_State *g = G(L);
223eda14cbcSMatt Macy luaF_close(L, L->stack); /* close all upvalues for this thread */
224eda14cbcSMatt Macy luaC_freeallobjects(L); /* collect all objects */
225eda14cbcSMatt Macy if (g->version) /* closing a fully built state? */
226eda14cbcSMatt Macy luai_userstateclose(L);
227eda14cbcSMatt Macy luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
228eda14cbcSMatt Macy luaZ_freebuffer(L, &g->buff);
229eda14cbcSMatt Macy freestack(L);
230eda14cbcSMatt Macy lua_assert(gettotalbytes(g) == sizeof(LG));
231eda14cbcSMatt Macy (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
232eda14cbcSMatt Macy }
233eda14cbcSMatt Macy
234eda14cbcSMatt Macy
lua_newthread(lua_State * L)235eda14cbcSMatt Macy LUA_API lua_State *lua_newthread (lua_State *L) {
236eda14cbcSMatt Macy lua_State *L1;
237eda14cbcSMatt Macy lua_lock(L);
238eda14cbcSMatt Macy luaC_checkGC(L);
239eda14cbcSMatt Macy L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
240eda14cbcSMatt Macy setthvalue(L, L->top, L1);
241eda14cbcSMatt Macy api_incr_top(L);
242eda14cbcSMatt Macy preinit_state(L1, G(L));
243eda14cbcSMatt Macy L1->hookmask = L->hookmask;
244eda14cbcSMatt Macy L1->basehookcount = L->basehookcount;
245eda14cbcSMatt Macy L1->hook = L->hook;
246eda14cbcSMatt Macy resethookcount(L1);
247eda14cbcSMatt Macy luai_userstatethread(L, L1);
248eda14cbcSMatt Macy stack_init(L1, L); /* init stack */
249eda14cbcSMatt Macy lua_unlock(L);
250eda14cbcSMatt Macy return L1;
251eda14cbcSMatt Macy }
252eda14cbcSMatt Macy
253eda14cbcSMatt Macy
luaE_freethread(lua_State * L,lua_State * L1)254eda14cbcSMatt Macy void luaE_freethread (lua_State *L, lua_State *L1) {
255eda14cbcSMatt Macy LX *l = fromstate(L1);
256eda14cbcSMatt Macy luaF_close(L1, L1->stack); /* close all upvalues for this thread */
257eda14cbcSMatt Macy lua_assert(L1->openupval == NULL);
258eda14cbcSMatt Macy luai_userstatefree(L, L1);
259eda14cbcSMatt Macy freestack(L1);
260eda14cbcSMatt Macy luaM_free(L, l);
261eda14cbcSMatt Macy }
262eda14cbcSMatt Macy
263eda14cbcSMatt Macy
lua_newstate(lua_Alloc f,void * ud)264eda14cbcSMatt Macy LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
265eda14cbcSMatt Macy int i;
266eda14cbcSMatt Macy lua_State *L;
267eda14cbcSMatt Macy global_State *g;
268eda14cbcSMatt Macy LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
269eda14cbcSMatt Macy if (l == NULL) return NULL;
270eda14cbcSMatt Macy L = &l->l.l;
271eda14cbcSMatt Macy g = &l->g;
272eda14cbcSMatt Macy L->next = NULL;
273eda14cbcSMatt Macy L->tt = LUA_TTHREAD;
274eda14cbcSMatt Macy g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
275eda14cbcSMatt Macy L->marked = luaC_white(g);
276eda14cbcSMatt Macy g->gckind = KGC_NORMAL;
277eda14cbcSMatt Macy preinit_state(L, g);
278eda14cbcSMatt Macy g->frealloc = f;
279eda14cbcSMatt Macy g->ud = ud;
280eda14cbcSMatt Macy g->mainthread = L;
281eda14cbcSMatt Macy g->seed = makeseed(L);
282eda14cbcSMatt Macy g->uvhead.u.l.prev = &g->uvhead;
283eda14cbcSMatt Macy g->uvhead.u.l.next = &g->uvhead;
284eda14cbcSMatt Macy g->gcrunning = 0; /* no GC while building state */
285eda14cbcSMatt Macy g->GCestimate = 0;
286eda14cbcSMatt Macy g->strt.size = 0;
287eda14cbcSMatt Macy g->strt.nuse = 0;
288eda14cbcSMatt Macy g->strt.hash = NULL;
289eda14cbcSMatt Macy setnilvalue(&g->l_registry);
290eda14cbcSMatt Macy luaZ_initbuffer(L, &g->buff);
291eda14cbcSMatt Macy g->panic = NULL;
292eda14cbcSMatt Macy g->version = NULL;
293eda14cbcSMatt Macy g->gcstate = GCSpause;
294eda14cbcSMatt Macy g->allgc = NULL;
295eda14cbcSMatt Macy g->finobj = NULL;
296eda14cbcSMatt Macy g->tobefnz = NULL;
297eda14cbcSMatt Macy g->sweepgc = g->sweepfin = NULL;
298eda14cbcSMatt Macy g->gray = g->grayagain = NULL;
299eda14cbcSMatt Macy g->weak = g->ephemeron = g->allweak = NULL;
300eda14cbcSMatt Macy g->totalbytes = sizeof(LG);
301eda14cbcSMatt Macy g->GCdebt = 0;
302eda14cbcSMatt Macy g->gcpause = LUAI_GCPAUSE;
303eda14cbcSMatt Macy g->gcmajorinc = LUAI_GCMAJOR;
304eda14cbcSMatt Macy g->gcstepmul = LUAI_GCMUL;
305eda14cbcSMatt Macy for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
306eda14cbcSMatt Macy if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
307eda14cbcSMatt Macy /* memory allocation error: free partial state */
308eda14cbcSMatt Macy close_state(L);
309eda14cbcSMatt Macy L = NULL;
310eda14cbcSMatt Macy }
311eda14cbcSMatt Macy return L;
312eda14cbcSMatt Macy }
313eda14cbcSMatt Macy
314eda14cbcSMatt Macy
lua_close(lua_State * L)315eda14cbcSMatt Macy LUA_API void lua_close (lua_State *L) {
316eda14cbcSMatt Macy L = G(L)->mainthread; /* only the main thread can be closed */
317eda14cbcSMatt Macy lua_lock(L);
318eda14cbcSMatt Macy close_state(L);
319eda14cbcSMatt Macy }
320