Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: lstate.c,v 1.12 2023/06/08 21:12:08 nikita Exp $	*/
      2 
      3 /*
      4 ** Id: lstate.c
      5 ** Global State
      6 ** See Copyright Notice in lua.h
      7 */
      8 
      9 #define lstate_c
     10 #define LUA_CORE
     11 
     12 #include "lprefix.h"
     13 
     14 
     15 #ifndef _KERNEL
     16 #include <stddef.h>
     17 #include <string.h>
     18 #endif /* _KERNEL */
     19 
     20 #include "lua.h"
     21 
     22 #include "lapi.h"
     23 #include "ldebug.h"
     24 #include "ldo.h"
     25 #include "lfunc.h"
     26 #include "lgc.h"
     27 #include "llex.h"
     28 #include "lmem.h"
     29 #include "lstate.h"
     30 #include "lstring.h"
     31 #include "ltable.h"
     32 #include "ltm.h"
     33 
     34 
     35 
     36 /*
     37 ** thread state + extra space
     38 */
     39 typedef struct LX {
     40   lu_byte extra_[LUA_EXTRASPACE];
     41   lua_State l;
     42 } LX;
     43 
     44 
     45 /*
     46 ** Main thread combines a thread state and the global state
     47 */
     48 typedef struct LG {
     49   LX l;
     50   global_State g;
     51 } LG;
     52 
     53 
     54 
     55 #define fromstate(L)	(cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
     56 
     57 
     58 /*
     59 ** A macro to create a "random" seed when a state is created;
     60 ** the seed is used to randomize string hashes.
     61 */
     62 #if !defined(luai_makeseed)
     63 
     64 #include <time.h>
     65 
     66 /*
     67 ** Compute an initial seed with some level of randomness.
     68 ** Rely on Address Space Layout Randomization (if present) and
     69 ** current time.
     70 */
     71 #define addbuff(b,p,e) \
     72   { size_t t = cast_sizet(e); \
     73     memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
     74 
     75 static unsigned int luai_makeseed (lua_State *L) {
     76   char buff[3 * sizeof(size_t)];
     77   unsigned int h = cast_uint(time(NULL));
     78   int p = 0;
     79   addbuff(buff, p, L);  /* heap variable */
     80   addbuff(buff, p, &h);  /* local variable */
     81   addbuff(buff, p, &lua_newstate);  /* public function */
     82   lua_assert(p == sizeof(buff));
     83   return luaS_hash(buff, p, h);
     84 }
     85 
     86 #endif
     87 
     88 
     89 /*
     90 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
     91 ** invariant (and avoiding underflows in 'totalbytes')
     92 */
     93 void luaE_setdebt (global_State *g, l_mem debt) {
     94   l_mem tb = gettotalbytes(g);
     95   lua_assert(tb > 0);
     96   if (debt < tb - MAX_LMEM)
     97     debt = tb - MAX_LMEM;  /* will make 'totalbytes == MAX_LMEM' */
     98   g->totalbytes = tb - debt;
     99   g->GCdebt = debt;
    100 }
    101 
    102 
    103 LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
    104   UNUSED(L); UNUSED(limit);
    105   return LUAI_MAXCCALLS;  /* warning?? */
    106 }
    107 
    108 
    109 CallInfo *luaE_extendCI (lua_State *L) {
    110   CallInfo *ci;
    111   lua_assert(L->ci->next == NULL);
    112   ci = luaM_new(L, CallInfo);
    113   lua_assert(L->ci->next == NULL);
    114   L->ci->next = ci;
    115   ci->previous = L->ci;
    116   ci->next = NULL;
    117   ci->u.l.trap = 0;
    118   L->nci++;
    119   return ci;
    120 }
    121 
    122 
    123 /*
    124 ** free all CallInfo structures not in use by a thread
    125 */
    126 void luaE_freeCI (lua_State *L) {
    127   CallInfo *ci = L->ci;
    128   CallInfo *next = ci->next;
    129   ci->next = NULL;
    130   while ((ci = next) != NULL) {
    131     next = ci->next;
    132     luaM_free(L, ci);
    133     L->nci--;
    134   }
    135 }
    136 
    137 
    138 /*
    139 ** free half of the CallInfo structures not in use by a thread,
    140 ** keeping the first one.
    141 */
    142 void luaE_shrinkCI (lua_State *L) {
    143   CallInfo *ci = L->ci->next;  /* first free CallInfo */
    144   CallInfo *next;
    145   if (ci == NULL)
    146     return;  /* no extra elements */
    147   while ((next = ci->next) != NULL) {  /* two extra elements? */
    148     CallInfo *next2 = next->next;  /* next's next */
    149     ci->next = next2;  /* remove next from the list */
    150     L->nci--;
    151     luaM_free(L, next);  /* free next */
    152     if (next2 == NULL)
    153       break;  /* no more elements */
    154     else {
    155       next2->previous = ci;
    156       ci = next2;  /* continue */
    157     }
    158   }
    159 }
    160 
    161 
    162 /*
    163 ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
    164 ** If equal, raises an overflow error. If value is larger than
    165 ** LUAI_MAXCCALLS (which means it is handling an overflow) but
    166 ** not much larger, does not report an error (to allow overflow
    167 ** handling to work).
    168 */
    169 void luaE_checkcstack (lua_State *L) {
    170   if (getCcalls(L) == LUAI_MAXCCALLS)
    171     luaG_runerror(L, "C stack overflow");
    172   else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
    173     luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
    174 }
    175 
    176 
    177 LUAI_FUNC void luaE_incCstack (lua_State *L) {
    178   L->nCcalls++;
    179   if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
    180     luaE_checkcstack(L);
    181 }
    182 
    183 
    184 static void stack_init (lua_State *L1, lua_State *L) {
    185   int i; CallInfo *ci;
    186   /* initialize stack array */
    187   L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
    188   L1->tbclist.p = L1->stack.p;
    189   for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
    190     setnilvalue(s2v(L1->stack.p + i));  /* erase new stack */
    191   L1->top.p = L1->stack.p;
    192   L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
    193   /* initialize first ci */
    194   ci = &L1->base_ci;
    195   ci->next = ci->previous = NULL;
    196   ci->callstatus = CIST_C;
    197   ci->func.p = L1->top.p;
    198   ci->u.c.k = NULL;
    199   ci->nresults = 0;
    200   setnilvalue(s2v(L1->top.p));  /* 'function' entry for this 'ci' */
    201   L1->top.p++;
    202   ci->top.p = L1->top.p + LUA_MINSTACK;
    203   L1->ci = ci;
    204 }
    205 
    206 
    207 static void freestack (lua_State *L) {
    208   if (L->stack.p == NULL)
    209     return;  /* stack not completely built yet */
    210   L->ci = &L->base_ci;  /* free the entire 'ci' list */
    211   luaE_freeCI(L);
    212   lua_assert(L->nci == 0);
    213   luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK);  /* free stack */
    214 }
    215 
    216 
    217 /*
    218 ** Create registry table and its predefined values
    219 */
    220 static void init_registry (lua_State *L, global_State *g) {
    221   /* create registry */
    222   Table *registry = luaH_new(L);
    223   sethvalue(L, &g->l_registry, registry);
    224   luaH_resize(L, registry, LUA_RIDX_LAST, 0);
    225   /* registry[LUA_RIDX_MAINTHREAD] = L */
    226   setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L);
    227   /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
    228   sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
    229 }
    230 
    231 
    232 /*
    233 ** open parts of the state that may cause memory-allocation errors.
    234 */
    235 static void f_luaopen (lua_State *L, void *ud) {
    236   global_State *g = G(L);
    237   UNUSED(ud);
    238   stack_init(L, L);  /* init stack */
    239   init_registry(L, g);
    240   luaS_init(L);
    241   luaT_init(L);
    242   luaX_init(L);
    243   g->gcstp = 0;  /* allow gc */
    244   setnilvalue(&g->nilvalue);  /* now state is complete */
    245   luai_userstateopen(L);
    246 }
    247 
    248 
    249 /*
    250 ** preinitialize a thread with consistent values without allocating
    251 ** any memory (to avoid errors)
    252 */
    253 static void preinit_thread (lua_State *L, global_State *g) {
    254   G(L) = g;
    255   L->stack.p = NULL;
    256   L->ci = NULL;
    257   L->nci = 0;
    258   L->twups = L;  /* thread has no upvalues */
    259   L->nCcalls = 0;
    260   L->errorJmp = NULL;
    261   L->hook = NULL;
    262   L->hookmask = 0;
    263   L->basehookcount = 0;
    264   L->allowhook = 1;
    265   resethookcount(L);
    266   L->openupval = NULL;
    267   L->status = LUA_OK;
    268   L->errfunc = 0;
    269   L->oldpc = 0;
    270 }
    271 
    272 
    273 static void close_state (lua_State *L) {
    274   global_State *g = G(L);
    275   if (!completestate(g))  /* closing a partially built state? */
    276     luaC_freeallobjects(L);  /* just collect its objects */
    277   else {  /* closing a fully built state */
    278     L->ci = &L->base_ci;  /* unwind CallInfo list */
    279     luaD_closeprotected(L, 1, LUA_OK);  /* close all upvalues */
    280     luaC_freeallobjects(L);  /* collect all objects */
    281     luai_userstateclose(L);
    282   }
    283   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
    284   freestack(L);
    285   lua_assert(gettotalbytes(g) == sizeof(LG));
    286   (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);  /* free main block */
    287 }
    288 
    289 
    290 LUA_API lua_State *lua_newthread (lua_State *L) {
    291   global_State *g = G(L);
    292   GCObject *o;
    293   lua_State *L1;
    294   lua_lock(L);
    295   luaC_checkGC(L);
    296   /* create new thread */
    297   o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
    298   L1 = gco2th(o);
    299   /* anchor it on L stack */
    300   setthvalue2s(L, L->top.p, L1);
    301   api_incr_top(L);
    302   preinit_thread(L1, g);
    303   L1->hookmask = L->hookmask;
    304   L1->basehookcount = L->basehookcount;
    305   L1->hook = L->hook;
    306   resethookcount(L1);
    307   /* initialize L1 extra space */
    308   memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
    309          LUA_EXTRASPACE);
    310   luai_userstatethread(L, L1);
    311   stack_init(L1, L);  /* init stack */
    312   lua_unlock(L);
    313   return L1;
    314 }
    315 
    316 
    317 void luaE_freethread (lua_State *L, lua_State *L1) {
    318   LX *l = fromstate(L1);
    319   luaF_closeupval(L1, L1->stack.p);  /* close all upvalues */
    320   lua_assert(L1->openupval == NULL);
    321   luai_userstatefree(L, L1);
    322   freestack(L1);
    323   luaM_free(L, l);
    324 }
    325 
    326 
    327 int luaE_resetthread (lua_State *L, int status) {
    328   CallInfo *ci = L->ci = &L->base_ci;  /* unwind CallInfo list */
    329   setnilvalue(s2v(L->stack.p));  /* 'function' entry for basic 'ci' */
    330   ci->func.p = L->stack.p;
    331   ci->callstatus = CIST_C;
    332   if (status == LUA_YIELD)
    333     status = LUA_OK;
    334   L->status = LUA_OK;  /* so it can run __close metamethods */
    335   status = luaD_closeprotected(L, 1, status);
    336   if (status != LUA_OK)  /* errors? */
    337     luaD_seterrorobj(L, status, L->stack.p + 1);
    338   else
    339     L->top.p = L->stack.p + 1;
    340   ci->top.p = L->top.p + LUA_MINSTACK;
    341   luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
    342   return status;
    343 }
    344 
    345 
    346 LUA_API int lua_closethread (lua_State *L, lua_State *from) {
    347   int status;
    348   lua_lock(L);
    349   L->nCcalls = (from) ? getCcalls(from) : 0;
    350   status = luaE_resetthread(L, L->status);
    351   lua_unlock(L);
    352   return status;
    353 }
    354 
    355 
    356 /*
    357 ** Deprecated! Use 'lua_closethread' instead.
    358 */
    359 LUA_API int lua_resetthread (lua_State *L) {
    360   return lua_closethread(L, NULL);
    361 }
    362 
    363 
    364 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
    365   int i;
    366   lua_State *L;
    367   global_State *g;
    368   LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
    369   if (l == NULL) return NULL;
    370   L = &l->l.l;
    371   g = &l->g;
    372   L->tt = LUA_VTHREAD;
    373   g->currentwhite = bitmask(WHITE0BIT);
    374   L->marked = luaC_white(g);
    375   preinit_thread(L, g);
    376   g->allgc = obj2gco(L);  /* by now, only object is the main thread */
    377   L->next = NULL;
    378   incnny(L);  /* main thread is always non yieldable */
    379   g->frealloc = f;
    380   g->ud = ud;
    381   g->warnf = NULL;
    382   g->ud_warn = NULL;
    383   g->mainthread = L;
    384   g->seed = luai_makeseed(L);
    385   g->gcstp = GCSTPGC;  /* no GC while building state */
    386   g->strt.size = g->strt.nuse = 0;
    387   g->strt.hash = NULL;
    388   setnilvalue(&g->l_registry);
    389   g->panic = NULL;
    390   g->gcstate = GCSpause;
    391   g->gckind = KGC_INC;
    392   g->gcstopem = 0;
    393   g->gcemergency = 0;
    394   g->finobj = g->tobefnz = g->fixedgc = NULL;
    395   g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
    396   g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
    397   g->sweepgc = NULL;
    398   g->gray = g->grayagain = NULL;
    399   g->weak = g->ephemeron = g->allweak = NULL;
    400   g->twups = NULL;
    401   g->totalbytes = sizeof(LG);
    402   g->GCdebt = 0;
    403   g->lastatomic = 0;
    404   setivalue(&g->nilvalue, 0);  /* to signal that state is not yet built */
    405   setgcparam(g->gcpause, LUAI_GCPAUSE);
    406   setgcparam(g->gcstepmul, LUAI_GCMUL);
    407   g->gcstepsize = LUAI_GCSTEPSIZE;
    408   setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
    409   g->genminormul = LUAI_GENMINORMUL;
    410   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
    411   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
    412     /* memory allocation error: free partial state */
    413     close_state(L);
    414     L = NULL;
    415   }
    416   return L;
    417 }
    418 
    419 
    420 LUA_API void lua_close (lua_State *L) {
    421   lua_lock(L);
    422   L = G(L)->mainthread;  /* only the main thread can be closed */
    423   close_state(L);
    424 }
    425 
    426 
    427 void luaE_warning (lua_State *L, const char *msg, int tocont) {
    428   lua_WarnFunction wf = G(L)->warnf;
    429   if (wf != NULL)
    430     wf(G(L)->ud_warn, msg, tocont);
    431 }
    432 
    433 
    434 /*
    435 ** Generate a warning from an error message
    436 */
    437 void luaE_warnerror (lua_State *L, const char *where) {
    438   TValue *errobj = s2v(L->top.p - 1);  /* error object */
    439   const char *msg = (ttisstring(errobj))
    440                   ? svalue(errobj)
    441                   : "error object is not a string";
    442   /* produce warning "error in %s (%s)" (where, msg) */
    443   luaE_warning(L, "error in ", 1);
    444   luaE_warning(L, where, 1);
    445   luaE_warning(L, " (", 1);
    446   luaE_warning(L, msg, 1);
    447   luaE_warning(L, ")", 0);
    448 }
    449 
    450