lstate.c 9.5 KB

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