lstate.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. ** $Id: lstate.c,v 2.154 2018/06/15 19:31:22 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstate_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "llex.h"
  18. #include "lmem.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. /*
  24. ** thread state + extra space
  25. */
  26. typedef struct LX {
  27. lu_byte extra_[LUA_EXTRASPACE];
  28. lua_State l;
  29. } LX;
  30. /*
  31. ** Main thread combines a thread state and the global state
  32. */
  33. typedef struct LG {
  34. LX l;
  35. global_State g;
  36. } LG;
  37. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  38. /*
  39. ** A macro to create a "random" seed when a state is created;
  40. ** the seed is used to randomize string hashes.
  41. */
  42. #if !defined(luai_makeseed)
  43. #include <time.h>
  44. /*
  45. ** Compute an initial seed with some level of randomness.
  46. ** Rely on Address Space Layout Randomization (if present) and
  47. ** current time.
  48. */
  49. #define addbuff(b,p,e) \
  50. { size_t t = cast_sizet(e); \
  51. memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
  52. static unsigned int luai_makeseed (lua_State *L) {
  53. char buff[3 * sizeof(size_t)];
  54. unsigned int h = cast_uint(time(NULL));
  55. int p = 0;
  56. addbuff(buff, p, L); /* heap variable */
  57. addbuff(buff, p, &h); /* local variable */
  58. addbuff(buff, p, &lua_newstate); /* public function */
  59. lua_assert(p == sizeof(buff));
  60. return luaS_hash(buff, p, h);
  61. }
  62. #endif
  63. /*
  64. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  65. ** invariant (and avoiding underflows in 'totalbytes')
  66. */
  67. void luaE_setdebt (global_State *g, l_mem debt) {
  68. l_mem tb = gettotalbytes(g);
  69. lua_assert(tb > 0);
  70. if (debt < tb - MAX_LMEM)
  71. debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
  72. g->totalbytes = tb - debt;
  73. g->GCdebt = debt;
  74. }
  75. /*
  76. ** Increment count of "C calls" and check for overflows. In case of
  77. ** a stack overflow, check appropriate error ("regular" overflow or
  78. ** overflow while handling stack overflow). If 'nCalls' is larger than
  79. ** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
  80. ** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
  81. ** allow overflow handling to work)
  82. */
  83. void luaE_incCcalls (lua_State *L) {
  84. if (++L->nCcalls >= LUAI_MAXCCALLS) {
  85. if (L->nCcalls == LUAI_MAXCCALLS)
  86. luaG_runerror(L, "C stack overflow");
  87. else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  88. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  89. }
  90. }
  91. CallInfo *luaE_extendCI (lua_State *L) {
  92. CallInfo *ci;
  93. luaE_incCcalls(L);
  94. ci = luaM_new(L, CallInfo);
  95. lua_assert(L->ci->next == NULL);
  96. L->ci->next = ci;
  97. ci->previous = L->ci;
  98. ci->next = NULL;
  99. ci->u.l.trap = 0;
  100. L->nci++;
  101. return ci;
  102. }
  103. /*
  104. ** free all CallInfo structures not in use by a thread
  105. */
  106. void luaE_freeCI (lua_State *L) {
  107. CallInfo *ci = L->ci;
  108. CallInfo *next = ci->next;
  109. ci->next = NULL;
  110. L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */
  111. while ((ci = next) != NULL) {
  112. next = ci->next;
  113. luaM_free(L, ci);
  114. L->nci--;
  115. }
  116. L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */
  117. }
  118. /*
  119. ** free half of the CallInfo structures not in use by a thread
  120. */
  121. void luaE_shrinkCI (lua_State *L) {
  122. CallInfo *ci = L->ci;
  123. CallInfo *next2; /* next's next */
  124. L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */
  125. /* while there are two nexts */
  126. while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
  127. luaM_free(L, ci->next); /* free next */
  128. L->nci--;
  129. ci->next = next2; /* remove 'next' from the list */
  130. next2->previous = ci;
  131. ci = next2; /* keep next's next */
  132. }
  133. L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */
  134. }
  135. static void stack_init (lua_State *L1, lua_State *L) {
  136. int i; CallInfo *ci;
  137. /* initialize stack array */
  138. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
  139. L1->stacksize = BASIC_STACK_SIZE;
  140. for (i = 0; i < BASIC_STACK_SIZE; i++)
  141. setnilvalue(s2v(L1->stack + i)); /* erase new stack */
  142. L1->top = L1->stack;
  143. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  144. /* initialize first ci */
  145. ci = &L1->base_ci;
  146. ci->next = ci->previous = NULL;
  147. ci->callstatus = CIST_C;
  148. ci->func = L1->top;
  149. setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
  150. L1->top++;
  151. ci->top = L1->top + LUA_MINSTACK;
  152. L1->ci = ci;
  153. }
  154. static void freestack (lua_State *L) {
  155. if (L->stack == NULL)
  156. return; /* stack not completely built yet */
  157. L->ci = &L->base_ci; /* free the entire 'ci' list */
  158. luaE_freeCI(L);
  159. lua_assert(L->nci == 0);
  160. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  161. }
  162. /*
  163. ** Create registry table and its predefined values
  164. */
  165. static void init_registry (lua_State *L, global_State *g) {
  166. TValue temp;
  167. /* create registry */
  168. Table *registry = luaH_new(L);
  169. sethvalue(L, &g->l_registry, registry);
  170. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  171. /* registry[LUA_RIDX_MAINTHREAD] = L */
  172. setthvalue(L, &temp, L); /* temp = L */
  173. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  174. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  175. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  176. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  177. }
  178. /*
  179. ** open parts of the state that may cause memory-allocation errors.
  180. ** ('ttisnil(&g->nilvalue)'' flags that the state was completely build)
  181. */
  182. static void f_luaopen (lua_State *L, void *ud) {
  183. global_State *g = G(L);
  184. UNUSED(ud);
  185. stack_init(L, L); /* init stack */
  186. init_registry(L, g);
  187. luaS_init(L);
  188. luaT_init(L);
  189. luaX_init(L);
  190. g->gcrunning = 1; /* allow gc */
  191. setnilvalue(&g->nilvalue);
  192. luai_userstateopen(L);
  193. }
  194. /*
  195. ** preinitialize a thread with consistent values without allocating
  196. ** any memory (to avoid errors)
  197. */
  198. static void preinit_thread (lua_State *L, global_State *g) {
  199. G(L) = g;
  200. L->stack = NULL;
  201. L->ci = NULL;
  202. L->nci = 0;
  203. L->stacksize = 0;
  204. L->twups = L; /* thread has no upvalues */
  205. L->errorJmp = NULL;
  206. L->nCcalls = 0;
  207. L->hook = NULL;
  208. L->hookmask = 0;
  209. L->basehookcount = 0;
  210. L->allowhook = 1;
  211. resethookcount(L);
  212. L->openupval = NULL;
  213. L->nny = 1;
  214. L->status = LUA_OK;
  215. L->errfunc = 0;
  216. }
  217. static void close_state (lua_State *L) {
  218. global_State *g = G(L);
  219. luaF_close(L, L->stack); /* close all upvalues for this thread */
  220. luaC_freeallobjects(L); /* collect all objects */
  221. if (ttisnil(&g->nilvalue)) /* closing a fully built state? */
  222. luai_userstateclose(L);
  223. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  224. freestack(L);
  225. lua_assert(gettotalbytes(g) == sizeof(LG));
  226. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  227. }
  228. LUA_API lua_State *lua_newthread (lua_State *L) {
  229. global_State *g = G(L);
  230. lua_State *L1;
  231. lua_lock(L);
  232. luaC_checkGC(L);
  233. /* create new thread */
  234. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  235. L1->marked = luaC_white(g);
  236. L1->tt = LUA_TTHREAD;
  237. /* link it on list 'allgc' */
  238. L1->next = g->allgc;
  239. g->allgc = obj2gco(L1);
  240. /* anchor it on L stack */
  241. setthvalue2s(L, L->top, L1);
  242. api_incr_top(L);
  243. preinit_thread(L1, g);
  244. L1->hookmask = L->hookmask;
  245. L1->basehookcount = L->basehookcount;
  246. L1->hook = L->hook;
  247. resethookcount(L1);
  248. /* initialize L1 extra space */
  249. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  250. LUA_EXTRASPACE);
  251. luai_userstatethread(L, L1);
  252. stack_init(L1, L); /* init stack */
  253. lua_unlock(L);
  254. return L1;
  255. }
  256. void luaE_freethread (lua_State *L, lua_State *L1) {
  257. LX *l = fromstate(L1);
  258. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  259. lua_assert(L1->openupval == NULL);
  260. luai_userstatefree(L, L1);
  261. freestack(L1);
  262. luaM_free(L, l);
  263. }
  264. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  265. int i;
  266. lua_State *L;
  267. global_State *g;
  268. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  269. if (l == NULL) return NULL;
  270. L = &l->l.l;
  271. g = &l->g;
  272. L->tt = LUA_TTHREAD;
  273. g->currentwhite = bitmask(WHITE0BIT);
  274. L->marked = luaC_white(g);
  275. preinit_thread(L, g);
  276. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  277. L->next = NULL;
  278. g->frealloc = f;
  279. g->ud = ud;
  280. g->mainthread = L;
  281. g->seed = luai_makeseed(L);
  282. g->gcrunning = 0; /* no GC while building state */
  283. g->strt.size = g->strt.nuse = 0;
  284. g->strt.hash = NULL;
  285. setnilvalue(&g->l_registry);
  286. g->panic = NULL;
  287. g->gcstate = GCSpause;
  288. g->gckind = KGC_INC;
  289. g->gcemergency = 0;
  290. g->finobj = g->tobefnz = g->fixedgc = NULL;
  291. g->survival = g->old = g->reallyold = NULL;
  292. g->finobjsur = g->finobjold = g->finobjrold = NULL;
  293. g->sweepgc = NULL;
  294. g->gray = g->grayagain = NULL;
  295. g->weak = g->ephemeron = g->allweak = g->protogray = NULL;
  296. g->twups = NULL;
  297. g->totalbytes = sizeof(LG);
  298. g->GCdebt = 0;
  299. setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
  300. setgcparam(g->gcpause, LUAI_GCPAUSE);
  301. setgcparam(g->gcstepmul, LUAI_GCMUL);
  302. g->gcstepsize = LUAI_GCSTEPSIZE;
  303. setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
  304. g->genminormul = LUAI_GENMINORMUL;
  305. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  306. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  307. /* memory allocation error: free partial state */
  308. close_state(L);
  309. L = NULL;
  310. }
  311. return L;
  312. }
  313. LUA_API void lua_close (lua_State *L) {
  314. L = G(L)->mainthread; /* only the main thread can be closed */
  315. lua_lock(L);
  316. close_state(L);
  317. }