lstate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. ** $Id: lstate.c $
  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).
  79. ** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than
  80. ** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means
  81. ** it has just entered the "overflow zone", so the function raises an
  82. ** overflow error.
  83. ** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2
  84. ** (which means it is already handling an overflow) but smaller than
  85. ** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message
  86. ** handling to work).
  87. ** Otherwise, report a stack overflow while handling a stack overflow
  88. ** (probably caused by a repeating error in the message handling
  89. ** function).
  90. */
  91. void luaE_enterCcall (lua_State *L) {
  92. int ncalls = getCcalls(L);
  93. L->nCcalls++;
  94. if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */
  95. luaE_freeCI(L); /* release unused CIs */
  96. ncalls = getCcalls(L); /* update call count */
  97. if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */
  98. if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) {
  99. /* no error before increments; raise the error now */
  100. L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */
  101. luaG_runerror(L, "C stack overflow");
  102. }
  103. else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3)))
  104. luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
  105. }
  106. }
  107. }
  108. CallInfo *luaE_extendCI (lua_State *L) {
  109. CallInfo *ci;
  110. lua_assert(L->ci->next == NULL);
  111. luaE_enterCcall(L);
  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. ** free all CallInfo structures not in use by a thread
  123. */
  124. void luaE_freeCI (lua_State *L) {
  125. CallInfo *ci = L->ci;
  126. CallInfo *next = ci->next;
  127. ci->next = NULL;
  128. L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
  129. while ((ci = next) != NULL) {
  130. next = ci->next;
  131. luaM_free(L, ci);
  132. L->nci--;
  133. }
  134. L->nCcalls += L->nci; /* adjust result */
  135. }
  136. /*
  137. ** free half of the CallInfo structures not in use by a thread
  138. */
  139. void luaE_shrinkCI (lua_State *L) {
  140. CallInfo *ci = L->ci;
  141. CallInfo *next2; /* next's next */
  142. L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
  143. /* while there are two nexts */
  144. while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
  145. luaM_free(L, ci->next); /* free next */
  146. L->nci--;
  147. ci->next = next2; /* remove 'next' from the list */
  148. next2->previous = ci;
  149. ci = next2; /* keep next's next */
  150. }
  151. L->nCcalls += L->nci; /* adjust result */
  152. }
  153. static void stack_init (lua_State *L1, lua_State *L) {
  154. int i; CallInfo *ci;
  155. /* initialize stack array */
  156. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
  157. L1->stacksize = BASIC_STACK_SIZE;
  158. for (i = 0; i < BASIC_STACK_SIZE; i++)
  159. setnilvalue(s2v(L1->stack + i)); /* erase new stack */
  160. L1->top = L1->stack;
  161. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  162. /* initialize first ci */
  163. ci = &L1->base_ci;
  164. ci->next = ci->previous = NULL;
  165. ci->callstatus = CIST_C;
  166. ci->func = L1->top;
  167. setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
  168. L1->top++;
  169. ci->top = L1->top + LUA_MINSTACK;
  170. L1->ci = ci;
  171. }
  172. static void freestack (lua_State *L) {
  173. if (L->stack == NULL)
  174. return; /* stack not completely built yet */
  175. L->ci = &L->base_ci; /* free the entire 'ci' list */
  176. luaE_freeCI(L);
  177. lua_assert(L->nci == 0);
  178. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  179. }
  180. /*
  181. ** Create registry table and its predefined values
  182. */
  183. static void init_registry (lua_State *L, global_State *g) {
  184. TValue temp;
  185. /* create registry */
  186. Table *registry = luaH_new(L);
  187. sethvalue(L, &g->l_registry, registry);
  188. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  189. /* registry[LUA_RIDX_MAINTHREAD] = L */
  190. setthvalue(L, &temp, L); /* temp = L */
  191. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  192. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  193. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  194. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  195. }
  196. /*
  197. ** open parts of the state that may cause memory-allocation errors.
  198. ** ('g->nilvalue' being a nil value flags that the state was completely
  199. ** build.)
  200. */
  201. static void f_luaopen (lua_State *L, void *ud) {
  202. global_State *g = G(L);
  203. UNUSED(ud);
  204. stack_init(L, L); /* init stack */
  205. init_registry(L, g);
  206. luaS_init(L);
  207. luaT_init(L);
  208. luaX_init(L);
  209. g->gcrunning = 1; /* allow gc */
  210. setnilvalue(&g->nilvalue);
  211. luai_userstateopen(L);
  212. }
  213. /*
  214. ** preinitialize a thread with consistent values without allocating
  215. ** any memory (to avoid errors)
  216. */
  217. static void preinit_thread (lua_State *L, global_State *g) {
  218. G(L) = g;
  219. L->stack = NULL;
  220. L->ci = NULL;
  221. L->nci = 0;
  222. L->stacksize = 0;
  223. L->twups = L; /* thread has no upvalues */
  224. L->errorJmp = NULL;
  225. L->nCcalls = 0;
  226. L->hook = NULL;
  227. L->hookmask = 0;
  228. L->basehookcount = 0;
  229. L->allowhook = 1;
  230. resethookcount(L);
  231. L->openupval = NULL;
  232. L->status = LUA_OK;
  233. L->errfunc = 0;
  234. }
  235. static void close_state (lua_State *L) {
  236. global_State *g = G(L);
  237. luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */
  238. luaC_freeallobjects(L); /* collect all objects */
  239. if (ttisnil(&g->nilvalue)) /* closing a fully built state? */
  240. luai_userstateclose(L);
  241. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  242. freestack(L);
  243. lua_assert(gettotalbytes(g) == sizeof(LG));
  244. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  245. }
  246. LUA_API lua_State *lua_newthread (lua_State *L) {
  247. global_State *g = G(L);
  248. lua_State *L1;
  249. lua_lock(L);
  250. luaC_checkGC(L);
  251. /* create new thread */
  252. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  253. L1->marked = luaC_white(g);
  254. L1->tt = LUA_TTHREAD;
  255. /* link it on list 'allgc' */
  256. L1->next = g->allgc;
  257. g->allgc = obj2gco(L1);
  258. /* anchor it on L stack */
  259. setthvalue2s(L, L->top, L1);
  260. api_incr_top(L);
  261. preinit_thread(L1, g);
  262. L1->hookmask = L->hookmask;
  263. L1->basehookcount = L->basehookcount;
  264. L1->hook = L->hook;
  265. resethookcount(L1);
  266. /* initialize L1 extra space */
  267. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  268. LUA_EXTRASPACE);
  269. luai_userstatethread(L, L1);
  270. stack_init(L1, L); /* init stack */
  271. lua_unlock(L);
  272. return L1;
  273. }
  274. void luaE_freethread (lua_State *L, lua_State *L1) {
  275. LX *l = fromstate(L1);
  276. luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */
  277. lua_assert(L1->openupval == NULL);
  278. luai_userstatefree(L, L1);
  279. freestack(L1);
  280. luaM_free(L, l);
  281. }
  282. int lua_resetthread (lua_State *L) {
  283. CallInfo *ci;
  284. int status;
  285. lua_lock(L);
  286. ci = &L->base_ci;
  287. status = luaF_close(L, L->stack, CLOSEPROTECT);
  288. setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
  289. if (status != CLOSEPROTECT) /* real errors? */
  290. luaD_seterrorobj(L, status, L->stack + 1);
  291. else {
  292. status = LUA_OK;
  293. L->top = L->stack + 1;
  294. }
  295. ci->callstatus = CIST_C;
  296. ci->func = L->stack;
  297. ci->top = L->top + LUA_MINSTACK;
  298. L->ci = ci;
  299. L->status = status;
  300. lua_unlock(L);
  301. return status;
  302. }
  303. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  304. int i;
  305. lua_State *L;
  306. global_State *g;
  307. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  308. if (l == NULL) return NULL;
  309. L = &l->l.l;
  310. g = &l->g;
  311. L->tt = LUA_TTHREAD;
  312. g->currentwhite = bitmask(WHITE0BIT);
  313. L->marked = luaC_white(g);
  314. preinit_thread(L, g);
  315. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  316. L->next = NULL;
  317. g->frealloc = f;
  318. g->ud = ud;
  319. g->warnf = NULL;
  320. g->ud_warn = NULL;
  321. g->mainthread = L;
  322. g->seed = luai_makeseed(L);
  323. g->gcrunning = 0; /* no GC while building state */
  324. g->strt.size = g->strt.nuse = 0;
  325. g->strt.hash = NULL;
  326. setnilvalue(&g->l_registry);
  327. g->panic = NULL;
  328. g->gcstate = GCSpause;
  329. g->gckind = KGC_INC;
  330. g->gcemergency = 0;
  331. g->finobj = g->tobefnz = g->fixedgc = NULL;
  332. g->survival = g->old = g->reallyold = NULL;
  333. g->finobjsur = g->finobjold = g->finobjrold = NULL;
  334. g->sweepgc = NULL;
  335. g->gray = g->grayagain = NULL;
  336. g->weak = g->ephemeron = g->allweak = NULL;
  337. g->twups = NULL;
  338. g->totalbytes = sizeof(LG);
  339. g->GCdebt = 0;
  340. g->lastatomic = 0;
  341. setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
  342. setgcparam(g->gcpause, LUAI_GCPAUSE);
  343. setgcparam(g->gcstepmul, LUAI_GCMUL);
  344. g->gcstepsize = LUAI_GCSTEPSIZE;
  345. setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
  346. g->genminormul = LUAI_GENMINORMUL;
  347. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  348. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  349. /* memory allocation error: free partial state */
  350. close_state(L);
  351. L = NULL;
  352. }
  353. return L;
  354. }
  355. LUA_API void lua_close (lua_State *L) {
  356. L = G(L)->mainthread; /* only the main thread can be closed */
  357. lua_lock(L);
  358. close_state(L);
  359. }
  360. void luaE_warning (lua_State *L, const char *msg, int tocont) {
  361. lua_WarnFunction wf = G(L)->warnf;
  362. if (wf != NULL)
  363. wf(G(L)->ud_warn, msg, tocont);
  364. }