lstate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. ci->u.c.k = NULL;
  168. ci->nresults = 0;
  169. setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
  170. L1->top++;
  171. ci->top = L1->top + LUA_MINSTACK;
  172. L1->ci = ci;
  173. }
  174. static void freestack (lua_State *L) {
  175. if (L->stack == NULL)
  176. return; /* stack not completely built yet */
  177. L->ci = &L->base_ci; /* free the entire 'ci' list */
  178. luaE_freeCI(L);
  179. lua_assert(L->nci == 0);
  180. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  181. }
  182. /*
  183. ** Create registry table and its predefined values
  184. */
  185. static void init_registry (lua_State *L, global_State *g) {
  186. TValue temp;
  187. /* create registry */
  188. Table *registry = luaH_new(L);
  189. sethvalue(L, &g->l_registry, registry);
  190. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  191. /* registry[LUA_RIDX_MAINTHREAD] = L */
  192. setthvalue(L, &temp, L); /* temp = L */
  193. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  194. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  195. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  196. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  197. }
  198. /*
  199. ** open parts of the state that may cause memory-allocation errors.
  200. ** ('g->nilvalue' being a nil value flags that the state was completely
  201. ** build.)
  202. */
  203. static void f_luaopen (lua_State *L, void *ud) {
  204. global_State *g = G(L);
  205. UNUSED(ud);
  206. stack_init(L, L); /* init stack */
  207. init_registry(L, g);
  208. luaS_init(L);
  209. luaT_init(L);
  210. luaX_init(L);
  211. g->gcrunning = 1; /* allow gc */
  212. setnilvalue(&g->nilvalue);
  213. luai_userstateopen(L);
  214. }
  215. /*
  216. ** preinitialize a thread with consistent values without allocating
  217. ** any memory (to avoid errors)
  218. */
  219. static void preinit_thread (lua_State *L, global_State *g) {
  220. G(L) = g;
  221. L->stack = NULL;
  222. L->ci = NULL;
  223. L->nci = 0;
  224. L->stacksize = 0;
  225. L->twups = L; /* thread has no upvalues */
  226. L->errorJmp = NULL;
  227. L->nCcalls = 0;
  228. L->hook = NULL;
  229. L->hookmask = 0;
  230. L->basehookcount = 0;
  231. L->allowhook = 1;
  232. resethookcount(L);
  233. L->openupval = NULL;
  234. L->status = LUA_OK;
  235. L->errfunc = 0;
  236. }
  237. static void close_state (lua_State *L) {
  238. global_State *g = G(L);
  239. luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */
  240. luaC_freeallobjects(L); /* collect all objects */
  241. if (ttisnil(&g->nilvalue)) /* closing a fully built state? */
  242. luai_userstateclose(L);
  243. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  244. freestack(L);
  245. lua_assert(gettotalbytes(g) == sizeof(LG));
  246. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  247. }
  248. LUA_API lua_State *lua_newthread (lua_State *L) {
  249. global_State *g = G(L);
  250. lua_State *L1;
  251. lua_lock(L);
  252. luaC_checkGC(L);
  253. /* create new thread */
  254. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  255. L1->marked = luaC_white(g);
  256. L1->tt = LUA_TTHREAD;
  257. /* link it on list 'allgc' */
  258. L1->next = g->allgc;
  259. g->allgc = obj2gco(L1);
  260. /* anchor it on L stack */
  261. setthvalue2s(L, L->top, L1);
  262. api_incr_top(L);
  263. preinit_thread(L1, g);
  264. L1->hookmask = L->hookmask;
  265. L1->basehookcount = L->basehookcount;
  266. L1->hook = L->hook;
  267. resethookcount(L1);
  268. /* initialize L1 extra space */
  269. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  270. LUA_EXTRASPACE);
  271. luai_userstatethread(L, L1);
  272. stack_init(L1, L); /* init stack */
  273. lua_unlock(L);
  274. return L1;
  275. }
  276. void luaE_freethread (lua_State *L, lua_State *L1) {
  277. LX *l = fromstate(L1);
  278. luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */
  279. lua_assert(L1->openupval == NULL);
  280. luai_userstatefree(L, L1);
  281. freestack(L1);
  282. luaM_free(L, l);
  283. }
  284. int lua_resetthread (lua_State *L) {
  285. CallInfo *ci;
  286. int status;
  287. lua_lock(L);
  288. ci = &L->base_ci;
  289. status = luaF_close(L, L->stack, CLOSEPROTECT);
  290. setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
  291. if (status != CLOSEPROTECT) /* real errors? */
  292. luaD_seterrorobj(L, status, L->stack + 1);
  293. else {
  294. status = LUA_OK;
  295. L->top = L->stack + 1;
  296. }
  297. ci->callstatus = CIST_C;
  298. ci->func = L->stack;
  299. ci->top = L->top + LUA_MINSTACK;
  300. L->ci = ci;
  301. L->status = status;
  302. lua_unlock(L);
  303. return status;
  304. }
  305. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  306. int i;
  307. lua_State *L;
  308. global_State *g;
  309. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  310. if (l == NULL) return NULL;
  311. L = &l->l.l;
  312. g = &l->g;
  313. L->tt = LUA_TTHREAD;
  314. g->currentwhite = bitmask(WHITE0BIT);
  315. L->marked = luaC_white(g);
  316. preinit_thread(L, g);
  317. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  318. L->next = NULL;
  319. g->frealloc = f;
  320. g->ud = ud;
  321. g->warnf = NULL;
  322. g->ud_warn = NULL;
  323. g->mainthread = L;
  324. g->seed = luai_makeseed(L);
  325. g->gcrunning = 0; /* no GC while building state */
  326. g->strt.size = g->strt.nuse = 0;
  327. g->strt.hash = NULL;
  328. setnilvalue(&g->l_registry);
  329. g->panic = NULL;
  330. g->gcstate = GCSpause;
  331. g->gckind = KGC_INC;
  332. g->gcemergency = 0;
  333. g->finobj = g->tobefnz = g->fixedgc = NULL;
  334. g->survival = g->old = g->reallyold = NULL;
  335. g->finobjsur = g->finobjold = g->finobjrold = NULL;
  336. g->sweepgc = NULL;
  337. g->gray = g->grayagain = NULL;
  338. g->weak = g->ephemeron = g->allweak = NULL;
  339. g->twups = NULL;
  340. g->totalbytes = sizeof(LG);
  341. g->GCdebt = 0;
  342. g->lastatomic = 0;
  343. setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
  344. setgcparam(g->gcpause, LUAI_GCPAUSE);
  345. setgcparam(g->gcstepmul, LUAI_GCMUL);
  346. g->gcstepsize = LUAI_GCSTEPSIZE;
  347. setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
  348. g->genminormul = LUAI_GENMINORMUL;
  349. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  350. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  351. /* memory allocation error: free partial state */
  352. close_state(L);
  353. L = NULL;
  354. }
  355. return L;
  356. }
  357. LUA_API void lua_close (lua_State *L) {
  358. L = G(L)->mainthread; /* only the main thread can be closed */
  359. lua_lock(L);
  360. close_state(L);
  361. }
  362. void luaE_warning (lua_State *L, const char *msg, int tocont) {
  363. lua_WarnFunction wf = G(L)->warnf;
  364. if (wf != NULL)
  365. wf(G(L)->ud_warn, msg, tocont);
  366. }