/src/testdir/build/lua-master/source/lstate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lstate.c $ |
3 | | ** Global State |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lstate_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <stddef.h> |
14 | | #include <string.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lapi.h" |
19 | | #include "ldebug.h" |
20 | | #include "ldo.h" |
21 | | #include "lfunc.h" |
22 | | #include "lgc.h" |
23 | | #include "llex.h" |
24 | | #include "lmem.h" |
25 | | #include "lstate.h" |
26 | | #include "lstring.h" |
27 | | #include "ltable.h" |
28 | | #include "ltm.h" |
29 | | |
30 | | |
31 | | |
32 | | /* |
33 | | ** thread state + extra space |
34 | | */ |
35 | | typedef struct LX { |
36 | | lu_byte extra_[LUA_EXTRASPACE]; |
37 | | lua_State l; |
38 | | } LX; |
39 | | |
40 | | |
41 | | /* |
42 | | ** Main thread combines a thread state and the global state |
43 | | */ |
44 | | typedef struct LG { |
45 | | LX l; |
46 | | global_State g; |
47 | | } LG; |
48 | | |
49 | | |
50 | | |
51 | 16.1k | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) |
52 | | |
53 | | |
54 | | /* |
55 | | ** A macro to create a "random" seed when a state is created; |
56 | | ** the seed is used to randomize string hashes. |
57 | | */ |
58 | | #if !defined(luai_makeseed) |
59 | | |
60 | | #include <time.h> |
61 | | |
62 | | /* |
63 | | ** Compute an initial seed with some level of randomness. |
64 | | ** Rely on Address Space Layout Randomization (if present) and |
65 | | ** current time. |
66 | | */ |
67 | | #define addbuff(b,p,e) \ |
68 | 47.4k | { size_t t = cast_sizet(e); \ |
69 | 47.4k | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } |
70 | | |
71 | 15.8k | static unsigned int luai_makeseed (lua_State *L) { |
72 | 15.8k | char buff[3 * sizeof(size_t)]; |
73 | 15.8k | unsigned int h = cast_uint(time(NULL)); |
74 | 15.8k | int p = 0; |
75 | 15.8k | addbuff(buff, p, L); /* heap variable */ |
76 | 15.8k | addbuff(buff, p, &h); /* local variable */ |
77 | 15.8k | addbuff(buff, p, &lua_newstate); /* public function */ |
78 | 15.8k | lua_assert(p == sizeof(buff)); |
79 | 15.8k | return luaS_hash(buff, p, h); |
80 | 15.8k | } |
81 | | |
82 | | #endif |
83 | | |
84 | | |
85 | | /* |
86 | | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
87 | | ** invariant (and avoiding underflows in 'totalbytes') |
88 | | */ |
89 | 123k | void luaE_setdebt (global_State *g, l_mem debt) { |
90 | 123k | l_mem tb = gettotalbytes(g); |
91 | 123k | lua_assert(tb > 0); |
92 | 123k | if (debt < tb - MAX_LMEM) |
93 | 0 | debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ |
94 | 123k | g->totalbytes = tb - debt; |
95 | 123k | g->GCdebt = debt; |
96 | 123k | } |
97 | | |
98 | | |
99 | 0 | LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { |
100 | 0 | UNUSED(L); UNUSED(limit); |
101 | 0 | return LUAI_MAXCCALLS; /* warning?? */ |
102 | 0 | } |
103 | | |
104 | | |
105 | 1.22M | CallInfo *luaE_extendCI (lua_State *L) { |
106 | 1.22M | CallInfo *ci; |
107 | 1.22M | lua_assert(L->ci->next == NULL); |
108 | 1.22M | ci = luaM_new(L, CallInfo); |
109 | 1.22M | lua_assert(L->ci->next == NULL); |
110 | 1.22M | L->ci->next = ci; |
111 | 1.22M | ci->previous = L->ci; |
112 | 1.22M | ci->next = NULL; |
113 | 1.22M | ci->u.l.trap = 0; |
114 | 1.22M | L->nci++; |
115 | 1.22M | return ci; |
116 | 1.22M | } |
117 | | |
118 | | |
119 | | /* |
120 | | ** free all CallInfo structures not in use by a thread |
121 | | */ |
122 | 16.1k | static void freeCI (lua_State *L) { |
123 | 16.1k | CallInfo *ci = L->ci; |
124 | 16.1k | CallInfo *next = ci->next; |
125 | 16.1k | ci->next = NULL; |
126 | 630k | while ((ci = next) != NULL) { |
127 | 614k | next = ci->next; |
128 | 614k | luaM_free(L, ci); |
129 | 614k | L->nci--; |
130 | 614k | } |
131 | 16.1k | } |
132 | | |
133 | | |
134 | | /* |
135 | | ** free half of the CallInfo structures not in use by a thread, |
136 | | ** keeping the first one. |
137 | | */ |
138 | 300k | void luaE_shrinkCI (lua_State *L) { |
139 | 300k | CallInfo *ci = L->ci->next; /* first free CallInfo */ |
140 | 300k | CallInfo *next; |
141 | 300k | if (ci == NULL) |
142 | 192k | return; /* no extra elements */ |
143 | 716k | while ((next = ci->next) != NULL) { /* two extra elements? */ |
144 | 610k | CallInfo *next2 = next->next; /* next's next */ |
145 | 610k | ci->next = next2; /* remove next from the list */ |
146 | 610k | L->nci--; |
147 | 610k | luaM_free(L, next); /* free next */ |
148 | 610k | if (next2 == NULL) |
149 | 1.56k | break; /* no more elements */ |
150 | 608k | else { |
151 | 608k | next2->previous = ci; |
152 | 608k | ci = next2; /* continue */ |
153 | 608k | } |
154 | 610k | } |
155 | 108k | } |
156 | | |
157 | | |
158 | | /* |
159 | | ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. |
160 | | ** If equal, raises an overflow error. If value is larger than |
161 | | ** LUAI_MAXCCALLS (which means it is handling an overflow) but |
162 | | ** not much larger, does not report an error (to allow overflow |
163 | | ** handling to work). |
164 | | */ |
165 | 498 | void luaE_checkcstack (lua_State *L) { |
166 | 498 | if (getCcalls(L) == LUAI_MAXCCALLS) |
167 | 498 | luaG_runerror(L, "C stack overflow"); |
168 | 0 | else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) |
169 | 0 | luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ |
170 | 498 | } |
171 | | |
172 | | |
173 | 29.0M | LUAI_FUNC void luaE_incCstack (lua_State *L) { |
174 | 29.0M | L->nCcalls++; |
175 | 29.0M | if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) |
176 | 498 | luaE_checkcstack(L); |
177 | 29.0M | } |
178 | | |
179 | | |
180 | 16.1k | static void stack_init (lua_State *L1, lua_State *L) { |
181 | 16.1k | int i; CallInfo *ci; |
182 | | /* initialize stack array */ |
183 | 16.1k | L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); |
184 | 16.1k | L1->tbclist.p = L1->stack.p; |
185 | 743k | for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) |
186 | 727k | setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ |
187 | 16.1k | L1->top.p = L1->stack.p; |
188 | 16.1k | L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; |
189 | | /* initialize first ci */ |
190 | 16.1k | ci = &L1->base_ci; |
191 | 16.1k | ci->next = ci->previous = NULL; |
192 | 16.1k | ci->callstatus = CIST_C; |
193 | 16.1k | ci->func.p = L1->top.p; |
194 | 16.1k | ci->u.c.k = NULL; |
195 | 16.1k | ci->nresults = 0; |
196 | 16.1k | setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ |
197 | 16.1k | L1->top.p++; |
198 | 16.1k | ci->top.p = L1->top.p + LUA_MINSTACK; |
199 | 16.1k | L1->ci = ci; |
200 | 16.1k | } |
201 | | |
202 | | |
203 | 16.1k | static void freestack (lua_State *L) { |
204 | 16.1k | if (L->stack.p == NULL) |
205 | 0 | return; /* stack not completely built yet */ |
206 | 16.1k | L->ci = &L->base_ci; /* free the entire 'ci' list */ |
207 | 16.1k | freeCI(L); |
208 | 16.1k | lua_assert(L->nci == 0); |
209 | 16.1k | luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ |
210 | 16.1k | } |
211 | | |
212 | | |
213 | | /* |
214 | | ** Create registry table and its predefined values |
215 | | */ |
216 | 15.8k | static void init_registry (lua_State *L, global_State *g) { |
217 | | /* create registry */ |
218 | 15.8k | Table *registry = luaH_new(L); |
219 | 15.8k | sethvalue(L, &g->l_registry, registry); |
220 | 15.8k | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
221 | | /* registry[LUA_RIDX_MAINTHREAD] = L */ |
222 | 15.8k | setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); |
223 | | /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ |
224 | 15.8k | sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); |
225 | 15.8k | } |
226 | | |
227 | | |
228 | | /* |
229 | | ** open parts of the state that may cause memory-allocation errors. |
230 | | */ |
231 | 15.8k | static void f_luaopen (lua_State *L, void *ud) { |
232 | 15.8k | global_State *g = G(L); |
233 | 15.8k | UNUSED(ud); |
234 | 15.8k | stack_init(L, L); /* init stack */ |
235 | 15.8k | init_registry(L, g); |
236 | 15.8k | luaS_init(L); |
237 | 15.8k | luaT_init(L); |
238 | 15.8k | luaX_init(L); |
239 | 15.8k | g->gcstp = 0; /* allow gc */ |
240 | 15.8k | setnilvalue(&g->nilvalue); /* now state is complete */ |
241 | 15.8k | luai_userstateopen(L); |
242 | 15.8k | } |
243 | | |
244 | | |
245 | | /* |
246 | | ** preinitialize a thread with consistent values without allocating |
247 | | ** any memory (to avoid errors) |
248 | | */ |
249 | 16.1k | static void preinit_thread (lua_State *L, global_State *g) { |
250 | 16.1k | G(L) = g; |
251 | 16.1k | L->stack.p = NULL; |
252 | 16.1k | L->ci = NULL; |
253 | 16.1k | L->nci = 0; |
254 | 16.1k | L->twups = L; /* thread has no upvalues */ |
255 | 16.1k | L->nCcalls = 0; |
256 | 16.1k | L->errorJmp = NULL; |
257 | 16.1k | L->hook = NULL; |
258 | 16.1k | L->hookmask = 0; |
259 | 16.1k | L->basehookcount = 0; |
260 | 16.1k | L->allowhook = 1; |
261 | 16.1k | resethookcount(L); |
262 | 16.1k | L->openupval = NULL; |
263 | 16.1k | L->status = LUA_OK; |
264 | 16.1k | L->errfunc = 0; |
265 | 16.1k | L->oldpc = 0; |
266 | 16.1k | } |
267 | | |
268 | | |
269 | 15.8k | static void close_state (lua_State *L) { |
270 | 15.8k | global_State *g = G(L); |
271 | 15.8k | if (!completestate(g)) /* closing a partially built state? */ |
272 | 0 | luaC_freeallobjects(L); /* just collect its objects */ |
273 | 15.8k | else { /* closing a fully built state */ |
274 | 15.8k | L->ci = &L->base_ci; /* unwind CallInfo list */ |
275 | 15.8k | luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ |
276 | 15.8k | luaC_freeallobjects(L); /* collect all objects */ |
277 | 15.8k | luai_userstateclose(L); |
278 | 15.8k | } |
279 | 15.8k | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); |
280 | 15.8k | freestack(L); |
281 | 15.8k | lua_assert(gettotalbytes(g) == sizeof(LG)); |
282 | 15.8k | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ |
283 | 15.8k | } |
284 | | |
285 | | |
286 | 335 | LUA_API lua_State *lua_newthread (lua_State *L) { |
287 | 335 | global_State *g = G(L); |
288 | 335 | GCObject *o; |
289 | 335 | lua_State *L1; |
290 | 335 | lua_lock(L); |
291 | 335 | luaC_checkGC(L); |
292 | | /* create new thread */ |
293 | 335 | o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); |
294 | 335 | L1 = gco2th(o); |
295 | | /* anchor it on L stack */ |
296 | 335 | setthvalue2s(L, L->top.p, L1); |
297 | 335 | api_incr_top(L); |
298 | 335 | preinit_thread(L1, g); |
299 | 335 | L1->hookmask = L->hookmask; |
300 | 335 | L1->basehookcount = L->basehookcount; |
301 | 335 | L1->hook = L->hook; |
302 | 335 | resethookcount(L1); |
303 | | /* initialize L1 extra space */ |
304 | 335 | memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), |
305 | 335 | LUA_EXTRASPACE); |
306 | 335 | luai_userstatethread(L, L1); |
307 | 335 | stack_init(L1, L); /* init stack */ |
308 | 335 | lua_unlock(L); |
309 | 335 | return L1; |
310 | 335 | } |
311 | | |
312 | | |
313 | 335 | void luaE_freethread (lua_State *L, lua_State *L1) { |
314 | 335 | LX *l = fromstate(L1); |
315 | 335 | luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ |
316 | 335 | lua_assert(L1->openupval == NULL); |
317 | 335 | luai_userstatefree(L, L1); |
318 | 335 | freestack(L1); |
319 | 335 | luaM_free(L, l); |
320 | 335 | } |
321 | | |
322 | | |
323 | 0 | int luaE_resetthread (lua_State *L, int status) { |
324 | 0 | CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ |
325 | 0 | setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ |
326 | 0 | ci->func.p = L->stack.p; |
327 | 0 | ci->callstatus = CIST_C; |
328 | 0 | if (status == LUA_YIELD) |
329 | 0 | status = LUA_OK; |
330 | 0 | L->status = LUA_OK; /* so it can run __close metamethods */ |
331 | 0 | status = luaD_closeprotected(L, 1, status); |
332 | 0 | if (status != LUA_OK) /* errors? */ |
333 | 0 | luaD_seterrorobj(L, status, L->stack.p + 1); |
334 | 0 | else |
335 | 0 | L->top.p = L->stack.p + 1; |
336 | 0 | ci->top.p = L->top.p + LUA_MINSTACK; |
337 | 0 | luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); |
338 | 0 | return status; |
339 | 0 | } |
340 | | |
341 | | |
342 | 0 | LUA_API int lua_closethread (lua_State *L, lua_State *from) { |
343 | 0 | int status; |
344 | 0 | lua_lock(L); |
345 | 0 | L->nCcalls = (from) ? getCcalls(from) : 0; |
346 | 0 | status = luaE_resetthread(L, L->status); |
347 | 0 | lua_unlock(L); |
348 | 0 | return status; |
349 | 0 | } |
350 | | |
351 | | |
352 | | /* |
353 | | ** Deprecated! Use 'lua_closethread' instead. |
354 | | */ |
355 | 0 | LUA_API int lua_resetthread (lua_State *L) { |
356 | 0 | return lua_closethread(L, NULL); |
357 | 0 | } |
358 | | |
359 | | |
360 | 15.8k | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { |
361 | 15.8k | int i; |
362 | 15.8k | lua_State *L; |
363 | 15.8k | global_State *g; |
364 | 15.8k | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); |
365 | 15.8k | if (l == NULL) return NULL; |
366 | 15.8k | L = &l->l.l; |
367 | 15.8k | g = &l->g; |
368 | 15.8k | L->tt = LUA_VTHREAD; |
369 | 15.8k | g->currentwhite = bitmask(WHITE0BIT); |
370 | 15.8k | L->marked = luaC_white(g); |
371 | 15.8k | preinit_thread(L, g); |
372 | 15.8k | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
373 | 15.8k | L->next = NULL; |
374 | 15.8k | incnny(L); /* main thread is always non yieldable */ |
375 | 15.8k | g->frealloc = f; |
376 | 15.8k | g->ud = ud; |
377 | 15.8k | g->warnf = NULL; |
378 | 15.8k | g->ud_warn = NULL; |
379 | 15.8k | g->mainthread = L; |
380 | 15.8k | g->seed = luai_makeseed(L); |
381 | 15.8k | g->gcstp = GCSTPGC; /* no GC while building state */ |
382 | 15.8k | g->strt.size = g->strt.nuse = 0; |
383 | 15.8k | g->strt.hash = NULL; |
384 | 15.8k | setnilvalue(&g->l_registry); |
385 | 15.8k | g->panic = NULL; |
386 | 15.8k | g->gcstate = GCSpause; |
387 | 15.8k | g->gckind = KGC_INC; |
388 | 15.8k | g->gcstopem = 0; |
389 | 15.8k | g->gcemergency = 0; |
390 | 15.8k | g->finobj = g->tobefnz = g->fixedgc = NULL; |
391 | 15.8k | g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; |
392 | 15.8k | g->finobjsur = g->finobjold1 = g->finobjrold = NULL; |
393 | 15.8k | g->sweepgc = NULL; |
394 | 15.8k | g->gray = g->grayagain = NULL; |
395 | 15.8k | g->weak = g->ephemeron = g->allweak = NULL; |
396 | 15.8k | g->twups = NULL; |
397 | 15.8k | g->totalbytes = sizeof(LG); |
398 | 15.8k | g->GCdebt = 0; |
399 | 15.8k | g->lastatomic = 0; |
400 | 15.8k | setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ |
401 | 15.8k | setgcparam(g->gcpause, LUAI_GCPAUSE); |
402 | 15.8k | setgcparam(g->gcstepmul, LUAI_GCMUL); |
403 | 15.8k | g->gcstepsize = LUAI_GCSTEPSIZE; |
404 | 15.8k | setgcparam(g->genmajormul, LUAI_GENMAJORMUL); |
405 | 15.8k | g->genminormul = LUAI_GENMINORMUL; |
406 | 158k | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; |
407 | 15.8k | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
408 | | /* memory allocation error: free partial state */ |
409 | 0 | close_state(L); |
410 | 0 | L = NULL; |
411 | 0 | } |
412 | 15.8k | return L; |
413 | 15.8k | } |
414 | | |
415 | | |
416 | 15.8k | LUA_API void lua_close (lua_State *L) { |
417 | 15.8k | lua_lock(L); |
418 | 15.8k | L = G(L)->mainthread; /* only the main thread can be closed */ |
419 | 15.8k | close_state(L); |
420 | 15.8k | } |
421 | | |
422 | | |
423 | 8.01k | void luaE_warning (lua_State *L, const char *msg, int tocont) { |
424 | 8.01k | lua_WarnFunction wf = G(L)->warnf; |
425 | 8.01k | if (wf != NULL) |
426 | 8.01k | wf(G(L)->ud_warn, msg, tocont); |
427 | 8.01k | } |
428 | | |
429 | | |
430 | | /* |
431 | | ** Generate a warning from an error message |
432 | | */ |
433 | 0 | void luaE_warnerror (lua_State *L, const char *where) { |
434 | 0 | TValue *errobj = s2v(L->top.p - 1); /* error object */ |
435 | 0 | const char *msg = (ttisstring(errobj)) |
436 | 0 | ? getstr(tsvalue(errobj)) |
437 | 0 | : "error object is not a string"; |
438 | | /* produce warning "error in %s (%s)" (where, msg) */ |
439 | 0 | luaE_warning(L, "error in ", 1); |
440 | 0 | luaE_warning(L, where, 1); |
441 | 0 | luaE_warning(L, " (", 1); |
442 | 0 | luaE_warning(L, msg, 1); |
443 | 0 | luaE_warning(L, ")", 0); |
444 | 0 | } |
445 | | |