root/lj_state.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. resizestack
  2. lj_state_relimitstack
  3. lj_state_shrinkstack
  4. lj_state_growstack
  5. lj_state_growstack1
  6. stack_init
  7. cpluaopen
  8. close_state
  9. lj_state_newstate
  10. cpfinalize
  11. lua_close
  12. lj_state_new
  13. lj_state_free

   1 /*
   2 ** State and stack handling.
   3 ** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
   4 **
   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
   7 */
   8 
   9 #define lj_state_c
  10 #define LUA_CORE
  11 
  12 #include "lj_obj.h"
  13 #include "lj_gc.h"
  14 #include "lj_err.h"
  15 #include "lj_buf.h"
  16 #include "lj_str.h"
  17 #include "lj_tab.h"
  18 #include "lj_func.h"
  19 #include "lj_meta.h"
  20 #include "lj_state.h"
  21 #include "lj_frame.h"
  22 #if LJ_HASFFI
  23 #include "lj_ctype.h"
  24 #endif
  25 #include "lj_trace.h"
  26 #include "lj_dispatch.h"
  27 #include "lj_vm.h"
  28 #include "lj_lex.h"
  29 #include "lj_alloc.h"
  30 #include "luajit.h"
  31 
  32 /* -- Stack handling ------------------------------------------------------ */
  33 
  34 /* Stack sizes. */
  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)
  39 
  40 /* Explanation of LJ_STACK_EXTRA:
  41 **
  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.
  47 **
  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.
  53 */
  54 
  55 /* Resize stack slots and adjust pointers in state. */
  56 static void resizestack(lua_State *L, MSize n)
  57 {
  58   TValue *st, *oldst = tvref(L->stack);
  59   ptrdiff_t delta;
  60   MSize oldsize = L->stacksize;
  61   MSize realsize = n + 1 + LJ_STACK_EXTRA;
  62   GCobj *up;
  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));
  79 }
  80 
  81 /* Relimit stack after error, in case the limit was overdrawn. */
  82 void lj_state_relimitstack(lua_State *L)
  83 {
  84   if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1)
  85     resizestack(L, LJ_STACK_MAX);
  86 }
  87 
  88 /* Try to shrink the stack (called from GC). */
  89 void lj_state_shrinkstack(lua_State *L, MSize used)
  90 {
  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);
  98 }
  99 
 100 /* Try to grow stack. */
 101 void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)
 102 {
 103   MSize n;
 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) {
 108     n += 2*LUA_MINSTACK;
 109   } else if (n < 2*L->stacksize) {
 110     n = 2*L->stacksize;
 111     if (n >= LJ_STACK_MAX)
 112       n = LJ_STACK_MAX;
 113   }
 114   resizestack(L, n);
 115   if (L->stacksize > LJ_STACK_MAXEX)
 116     lj_err_msg(L, LJ_ERR_STKOV);
 117 }
 118 
 119 void LJ_FASTCALL lj_state_growstack1(lua_State *L)
 120 {
 121   lj_state_growstack(L, 1);
 122 }
 123 
 124 /* Allocate basic stack for new state. */
 125 static void stack_init(lua_State *L1, lua_State *L)
 126 {
 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. */
 136     setnilV(st++);
 137 }
 138 
 139 /* -- State handling ------------------------------------------------------ */
 140 
 141 /* Open parts that may cause memory-allocation errors. */
 142 static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud)
 143 {
 144   global_State *g = G(L);
 145   UNUSED(dummy);
 146   UNUSED(ud);
 147   stack_init(L, 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);
 152   lj_meta_init(L);
 153   lj_lex_init(L);
 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);
 157   return NULL;
 158 }
 159 
 160 static void close_state(lua_State *L)
 161 {
 162   global_State *g = G(L);
 163   lj_func_closeuv(L, tvref(L->stack));
 164   lj_gc_freeall(g);
 165   lua_assert(gcref(g->gc.root) == obj2gco(L));
 166   lua_assert(g->strnum == 0);
 167   lj_trace_freestate(g);
 168 #if LJ_HASFFI
 169   lj_ctype_freestate(g);
 170 #endif
 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);
 178   else
 179 #endif
 180     g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0);
 181 }
 182 
 183 #if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC))
 184 lua_State *lj_state_newstate(lua_Alloc f, void *ud)
 185 #else
 186 LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
 187 #endif
 188 {
 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;
 201   g->allocf = f;
 202   g->allocd = ud;
 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);
 210 #if !LJ_GC64
 211   setmref(g->nilnode.freetop, &g->nilnode);
 212 #endif
 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. */
 224     close_state(L);
 225     return NULL;
 226   }
 227   L->status = LUA_OK;
 228   return L;
 229 }
 230 
 231 static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud)
 232 {
 233   UNUSED(dummy);
 234   UNUSED(ud);
 235   lj_gc_finalize_cdata(L);
 236   lj_gc_finalize_udata(L);
 237   /* Frame pop omitted. */
 238   return NULL;
 239 }
 240 
 241 LUA_API void lua_close(lua_State *L)
 242 {
 243   global_State *g = G(L);
 244   int i;
 245   L = mainthread(g);  /* Only the main thread can be closed. */
 246 #if LJ_HASPROFILE
 247   luaJIT_profile_stop(L);
 248 #endif
 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. */
 252 #if LJ_HASJIT
 253   G2J(g)->flags &= ~JIT_F_ON;
 254   G2J(g)->state = LJ_TRACE_IDLE;
 255   lj_dispatch_update(g);
 256 #endif
 257   for (i = 0;;) {
 258     hook_enter(g);
 259     L->status = LUA_OK;
 260     L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
 261     L->cframe = NULL;
 262     if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == LUA_OK) {
 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. */
 266         break;
 267     }
 268   }
 269   close_state(L);
 270 }
 271 
 272 lua_State *lj_state_new(lua_State *L)
 273 {
 274   lua_State *L1 = lj_mem_newobj(L, lua_State);
 275   L1->gct = ~LJ_TTHREAD;
 276   L1->dummy_ffid = FF_C;
 277   L1->status = LUA_OK;
 278   L1->stacksize = 0;
 279   setmref(L1->stack, NULL);
 280   L1->cframe = 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)));
 287   return L1;
 288 }
 289 
 290 void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L)
 291 {
 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);
 298   lj_mem_freet(g, L);
 299 }
 300 

/* [<][>][^][v][top][bottom][index][help] */