9
3

lstate.c 11 KB

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