lstate.c 9.4 KB

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