Path: blob/main/sys/contrib/openzfs/module/lua/lstate.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $3** Global State4** See Copyright Notice in lua.h5*/678#define lstate_c9#define LUA_CORE1011#include <sys/lua/lua.h>1213#include "lapi.h"14#include "ldebug.h"15#include "ldo.h"16#include "lfunc.h"17#include "lgc.h"18#include "llex.h"19#include "lmem.h"20#include "lstate.h"21#include "lstring.h"22#include "ltable.h"23#include "ltm.h"242526#if !defined(LUAI_GCPAUSE)27#define LUAI_GCPAUSE 200 /* 200% */28#endif2930#if !defined(LUAI_GCMAJOR)31#define LUAI_GCMAJOR 200 /* 200% */32#endif3334#if !defined(LUAI_GCMUL)35#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */36#endif373839#define MEMERRMSG "not enough memory"404142/*43** a macro to help the creation of a unique random seed when a state is44** created; the seed is used to randomize hashes.45*/46#if !defined(luai_makeseed)47#define luai_makeseed() cast(unsigned int, gethrtime())48#endif49505152/*53** thread state + extra space54*/55typedef struct LX {56#if defined(LUAI_EXTRASPACE)57char buff[LUAI_EXTRASPACE];58#endif59lua_State l;60} LX;616263/*64** Main thread combines a thread state and the global state65*/66typedef struct LG {67LX l;68global_State g;69} LG;70717273#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))747576/*77** Compute an initial seed as random as possible. In ANSI, rely on78** Address Space Layout Randomization (if present) to increase79** randomness..80*/81#define addbuff(b,p,e) \82{ size_t t = cast(size_t, e); \83memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }8485static unsigned int makeseed (lua_State *L) {86char buff[4 * sizeof(size_t)];87unsigned int h = luai_makeseed();88int p = 0;89addbuff(buff, p, L); /* heap variable */90addbuff(buff, p, &h); /* local variable */91addbuff(buff, p, luaO_nilobject); /* global variable */92addbuff(buff, p, &lua_newstate); /* public function */93lua_assert(p == sizeof(buff));94return luaS_hash(buff, p, h);95}969798/*99** set GCdebt to a new value keeping the value (totalbytes + GCdebt)100** invariant101*/102void luaE_setdebt (global_State *g, l_mem debt) {103g->totalbytes -= (debt - g->GCdebt);104g->GCdebt = debt;105}106107108CallInfo *luaE_extendCI (lua_State *L) {109CallInfo *ci = luaM_new(L, CallInfo);110lua_assert(L->ci->next == NULL);111L->ci->next = ci;112ci->previous = L->ci;113ci->next = NULL;114return ci;115}116117118void luaE_freeCI (lua_State *L) {119CallInfo *ci = L->ci;120CallInfo *next = ci->next;121ci->next = NULL;122while ((ci = next) != NULL) {123next = ci->next;124luaM_free(L, ci);125}126}127128129static void stack_init (lua_State *L1, lua_State *L) {130int i; CallInfo *ci;131/* initialize stack array */132L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);133L1->stacksize = BASIC_STACK_SIZE;134for (i = 0; i < BASIC_STACK_SIZE; i++)135setnilvalue(L1->stack + i); /* erase new stack */136L1->top = L1->stack;137L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;138/* initialize first ci */139ci = &L1->base_ci;140ci->next = ci->previous = NULL;141ci->callstatus = 0;142ci->func = L1->top;143setnilvalue(L1->top++); /* 'function' entry for this 'ci' */144ci->top = L1->top + LUA_MINSTACK;145L1->ci = ci;146}147148149static void freestack (lua_State *L) {150if (L->stack == NULL)151return; /* stack not completely built yet */152L->ci = &L->base_ci; /* free the entire 'ci' list */153luaE_freeCI(L);154luaM_freearray(L, L->stack, L->stacksize); /* free stack array */155}156157158/*159** Create registry table and its predefined values160*/161static void init_registry (lua_State *L, global_State *g) {162TValue mt;163/* create registry */164Table *registry = luaH_new(L);165sethvalue(L, &g->l_registry, registry);166luaH_resize(L, registry, LUA_RIDX_LAST, 0);167/* registry[LUA_RIDX_MAINTHREAD] = L */168setthvalue(L, &mt, L);169luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);170/* registry[LUA_RIDX_GLOBALS] = table of globals */171sethvalue(L, &mt, luaH_new(L));172luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);173}174175176/*177** open parts of the state that may cause memory-allocation errors178*/179static void f_luaopen (lua_State *L, void *ud) {180global_State *g = G(L);181UNUSED(ud);182stack_init(L, L); /* init stack */183init_registry(L, g);184luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */185luaT_init(L);186luaX_init(L);187/* pre-create memory-error message */188g->memerrmsg = luaS_newliteral(L, MEMERRMSG);189luaS_fix(g->memerrmsg); /* it should never be collected */190g->gcrunning = 1; /* allow gc */191g->version = lua_version(NULL);192luai_userstateopen(L);193}194195196/*197** preinitialize a state with consistent values without allocating198** any memory (to avoid errors)199*/200static void preinit_state (lua_State *L, global_State *g) {201G(L) = g;202L->stack = NULL;203L->ci = NULL;204L->stacksize = 0;205L->errorJmp = NULL;206L->nCcalls = 0;207L->hook = NULL;208L->hookmask = 0;209L->basehookcount = 0;210L->allowhook = 1;211resethookcount(L);212L->openupval = NULL;213L->nny = 1;214L->status = LUA_OK;215L->errfunc = 0;216L->runerror = 0;217}218219220static void close_state (lua_State *L) {221global_State *g = G(L);222luaF_close(L, L->stack); /* close all upvalues for this thread */223luaC_freeallobjects(L); /* collect all objects */224if (g->version) /* closing a fully built state? */225luai_userstateclose(L);226luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);227luaZ_freebuffer(L, &g->buff);228freestack(L);229lua_assert(gettotalbytes(g) == sizeof(LG));230(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */231}232233234LUA_API lua_State *lua_newthread (lua_State *L) {235lua_State *L1;236lua_lock(L);237luaC_checkGC(L);238L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;239setthvalue(L, L->top, L1);240api_incr_top(L);241preinit_state(L1, G(L));242L1->hookmask = L->hookmask;243L1->basehookcount = L->basehookcount;244L1->hook = L->hook;245resethookcount(L1);246luai_userstatethread(L, L1);247stack_init(L1, L); /* init stack */248lua_unlock(L);249return L1;250}251252253void luaE_freethread (lua_State *L, lua_State *L1) {254LX *l = fromstate(L1);255luaF_close(L1, L1->stack); /* close all upvalues for this thread */256lua_assert(L1->openupval == NULL);257luai_userstatefree(L, L1);258freestack(L1);259luaM_free(L, l);260}261262263LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {264int i;265lua_State *L;266global_State *g;267LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));268if (l == NULL) return NULL;269L = &l->l.l;270g = &l->g;271L->next = NULL;272L->tt = LUA_TTHREAD;273g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);274L->marked = luaC_white(g);275g->gckind = KGC_NORMAL;276preinit_state(L, g);277g->frealloc = f;278g->ud = ud;279g->mainthread = L;280g->seed = makeseed(L);281g->uvhead.u.l.prev = &g->uvhead;282g->uvhead.u.l.next = &g->uvhead;283g->gcrunning = 0; /* no GC while building state */284g->GCestimate = 0;285g->strt.size = 0;286g->strt.nuse = 0;287g->strt.hash = NULL;288setnilvalue(&g->l_registry);289luaZ_initbuffer(L, &g->buff);290g->panic = NULL;291g->version = NULL;292g->gcstate = GCSpause;293g->allgc = NULL;294g->finobj = NULL;295g->tobefnz = NULL;296g->sweepgc = g->sweepfin = NULL;297g->gray = g->grayagain = NULL;298g->weak = g->ephemeron = g->allweak = NULL;299g->totalbytes = sizeof(LG);300g->GCdebt = 0;301g->gcpause = LUAI_GCPAUSE;302g->gcmajorinc = LUAI_GCMAJOR;303g->gcstepmul = LUAI_GCMUL;304for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;305if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {306/* memory allocation error: free partial state */307close_state(L);308L = NULL;309}310return L;311}312313314LUA_API void lua_close (lua_State *L) {315L = G(L)->mainthread; /* only the main thread can be closed */316lua_lock(L);317close_state(L);318}319320321