/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 | 9.00k | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) |
33 | | |
34 | | |
35 | | /* |
36 | | ** these macros allow user-specific actions when a thread is |
37 | | ** created/deleted |
38 | | */ |
39 | | #if !defined(luai_userstateopen) |
40 | 59.5k | #define luai_userstateopen(L) ((void)L) |
41 | | #endif |
42 | | |
43 | | #if !defined(luai_userstateclose) |
44 | 59.5k | #define luai_userstateclose(L) ((void)L) |
45 | | #endif |
46 | | |
47 | | #if !defined(luai_userstatethread) |
48 | 9.00k | #define luai_userstatethread(L,L1) ((void)L) |
49 | | #endif |
50 | | |
51 | | #if !defined(luai_userstatefree) |
52 | 9.00k | #define luai_userstatefree(L,L1) ((void)L) |
53 | | #endif |
54 | | |
55 | | |
56 | | /* |
57 | | ** set GCdebt to a new value keeping the real number of allocated |
58 | | ** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in |
59 | | ** 'GCtotalobjs'. |
60 | | */ |
61 | 2.70M | void luaE_setdebt (global_State *g, l_mem debt) { |
62 | 2.70M | l_mem tb = gettotalbytes(g); |
63 | 2.70M | lua_assert(tb > 0); |
64 | 2.70M | if (debt > MAX_LMEM - tb) |
65 | 0 | debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */ |
66 | 2.70M | g->GCtotalbytes = tb + debt; |
67 | 2.70M | g->GCdebt = debt; |
68 | 2.70M | } |
69 | | |
70 | | |
71 | 40.6M | CallInfo *luaE_extendCI (lua_State *L) { |
72 | 40.6M | CallInfo *ci; |
73 | 40.6M | lua_assert(L->ci->next == NULL); |
74 | 40.6M | ci = luaM_new(L, CallInfo); |
75 | 40.6M | lua_assert(L->ci->next == NULL); |
76 | 40.6M | L->ci->next = ci; |
77 | 40.6M | ci->previous = L->ci; |
78 | 40.6M | ci->next = NULL; |
79 | 40.6M | ci->u.l.trap = 0; |
80 | 40.6M | L->nci++; |
81 | 40.6M | return ci; |
82 | 40.6M | } |
83 | | |
84 | | |
85 | | /* |
86 | | ** free all CallInfo structures not in use by a thread |
87 | | */ |
88 | 68.5k | static void freeCI (lua_State *L) { |
89 | 68.5k | CallInfo *ci = L->ci; |
90 | 68.5k | CallInfo *next = ci->next; |
91 | 68.5k | ci->next = NULL; |
92 | 12.2M | while ((ci = next) != NULL) { |
93 | 12.2M | next = ci->next; |
94 | 12.2M | luaM_free(L, ci); |
95 | 12.2M | L->nci--; |
96 | 12.2M | } |
97 | 68.5k | } |
98 | | |
99 | | |
100 | | /* |
101 | | ** free half of the CallInfo structures not in use by a thread, |
102 | | ** keeping the first one. |
103 | | */ |
104 | 11.3M | void luaE_shrinkCI (lua_State *L) { |
105 | 11.3M | CallInfo *ci = L->ci->next; /* first free CallInfo */ |
106 | 11.3M | CallInfo *next; |
107 | 11.3M | if (ci == NULL) |
108 | 6.40M | return; /* no extra elements */ |
109 | 32.5M | while ((next = ci->next) != NULL) { /* two extra elements? */ |
110 | 28.4M | CallInfo *next2 = next->next; /* next's next */ |
111 | 28.4M | ci->next = next2; /* remove next from the list */ |
112 | 28.4M | L->nci--; |
113 | 28.4M | luaM_free(L, next); /* free next */ |
114 | 28.4M | if (next2 == NULL) |
115 | 782k | break; /* no more elements */ |
116 | 27.6M | else { |
117 | 27.6M | next2->previous = ci; |
118 | 27.6M | ci = next2; /* continue */ |
119 | 27.6M | } |
120 | 28.4M | } |
121 | 4.91M | } |
122 | | |
123 | | |
124 | | /* |
125 | | ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. |
126 | | ** If equal, raises an overflow error. If value is larger than |
127 | | ** LUAI_MAXCCALLS (which means it is handling an overflow) but |
128 | | ** not much larger, does not report an error (to allow overflow |
129 | | ** handling to work). |
130 | | */ |
131 | 6.01M | void luaE_checkcstack (lua_State *L) { |
132 | 6.01M | if (getCcalls(L) == LUAI_MAXCCALLS) |
133 | 51.3k | luaG_runerror(L, "C stack overflow"); |
134 | 5.96M | else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) |
135 | 285k | luaD_errerr(L); /* error while handling stack error */ |
136 | 6.01M | } |
137 | | |
138 | | |
139 | 191M | LUAI_FUNC void luaE_incCstack (lua_State *L) { |
140 | 191M | L->nCcalls++; |
141 | 191M | if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) |
142 | 4.76M | luaE_checkcstack(L); |
143 | 191M | } |
144 | | |
145 | | |
146 | 128k | static void resetCI (lua_State *L) { |
147 | 128k | CallInfo *ci = L->ci = &L->base_ci; |
148 | 128k | ci->func.p = L->stack.p; |
149 | 128k | setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */ |
150 | 128k | ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */ |
151 | 128k | ci->u.c.k = NULL; |
152 | 128k | ci->callstatus = CIST_C; |
153 | 128k | L->status = LUA_OK; |
154 | 128k | L->errfunc = 0; /* stack unwind can "throw away" the error function */ |
155 | 128k | } |
156 | | |
157 | | |
158 | 68.5k | static void stack_init (lua_State *L1, lua_State *L) { |
159 | 68.5k | int i; |
160 | | /* initialize stack array */ |
161 | 68.5k | L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); |
162 | 68.5k | L1->tbclist.p = L1->stack.p; |
163 | 3.15M | for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) |
164 | 3.08M | setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ |
165 | 68.5k | L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; |
166 | | /* initialize first ci */ |
167 | 68.5k | resetCI(L1); |
168 | 68.5k | L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */ |
169 | 68.5k | } |
170 | | |
171 | | |
172 | 68.5k | static void freestack (lua_State *L) { |
173 | 68.5k | if (L->stack.p == NULL) |
174 | 0 | return; /* stack not completely built yet */ |
175 | 68.5k | L->ci = &L->base_ci; /* free the entire 'ci' list */ |
176 | 68.5k | freeCI(L); |
177 | 68.5k | lua_assert(L->nci == 0); |
178 | | /* free stack */ |
179 | 68.5k | luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK)); |
180 | 68.5k | } |
181 | | |
182 | | |
183 | | /* |
184 | | ** Create registry table and its predefined values |
185 | | */ |
186 | 59.5k | static void init_registry (lua_State *L, global_State *g) { |
187 | | /* create registry */ |
188 | 59.5k | TValue aux; |
189 | 59.5k | Table *registry = luaH_new(L); |
190 | 59.5k | sethvalue(L, &g->l_registry, registry); |
191 | 59.5k | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
192 | | /* registry[1] = false */ |
193 | 59.5k | setbfvalue(&aux); |
194 | 59.5k | luaH_setint(L, registry, 1, &aux); |
195 | | /* registry[LUA_RIDX_MAINTHREAD] = L */ |
196 | 59.5k | setthvalue(L, &aux, L); |
197 | 59.5k | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); |
198 | | /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ |
199 | 59.5k | sethvalue(L, &aux, luaH_new(L)); |
200 | 59.5k | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux); |
201 | 59.5k | } |
202 | | |
203 | | |
204 | | /* |
205 | | ** open parts of the state that may cause memory-allocation errors. |
206 | | */ |
207 | 59.5k | static void f_luaopen (lua_State *L, void *ud) { |
208 | 59.5k | global_State *g = G(L); |
209 | 59.5k | UNUSED(ud); |
210 | 59.5k | stack_init(L, L); /* init stack */ |
211 | 59.5k | init_registry(L, g); |
212 | 59.5k | luaS_init(L); |
213 | 59.5k | luaT_init(L); |
214 | 59.5k | luaX_init(L); |
215 | 59.5k | g->gcstp = 0; /* allow gc */ |
216 | 59.5k | setnilvalue(&g->nilvalue); /* now state is complete */ |
217 | 59.5k | luai_userstateopen(L); |
218 | 59.5k | } |
219 | | |
220 | | |
221 | | /* |
222 | | ** preinitialize a thread with consistent values without allocating |
223 | | ** any memory (to avoid errors) |
224 | | */ |
225 | 68.5k | static void preinit_thread (lua_State *L, global_State *g) { |
226 | 68.5k | G(L) = g; |
227 | 68.5k | L->stack.p = NULL; |
228 | 68.5k | L->ci = NULL; |
229 | 68.5k | L->nci = 0; |
230 | 68.5k | L->twups = L; /* thread has no upvalues */ |
231 | 68.5k | L->nCcalls = 0; |
232 | 68.5k | L->errorJmp = NULL; |
233 | 68.5k | L->hook = NULL; |
234 | 68.5k | L->hookmask = 0; |
235 | 68.5k | L->basehookcount = 0; |
236 | 68.5k | L->allowhook = 1; |
237 | 68.5k | resethookcount(L); |
238 | 68.5k | L->openupval = NULL; |
239 | 68.5k | L->status = LUA_OK; |
240 | 68.5k | L->errfunc = 0; |
241 | 68.5k | L->oldpc = 0; |
242 | 68.5k | L->base_ci.previous = L->base_ci.next = NULL; |
243 | 68.5k | } |
244 | | |
245 | | |
246 | 2.40M | lu_mem luaE_threadsize (lua_State *L) { |
247 | 2.40M | lu_mem sz = cast(lu_mem, sizeof(LX)) |
248 | 2.40M | + cast_uint(L->nci) * sizeof(CallInfo); |
249 | 2.40M | if (L->stack.p != NULL) |
250 | 2.40M | sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); |
251 | 2.40M | return sz; |
252 | 2.40M | } |
253 | | |
254 | | |
255 | 59.5k | static void close_state (lua_State *L) { |
256 | 59.5k | global_State *g = G(L); |
257 | 59.5k | if (!completestate(g)) /* closing a partially built state? */ |
258 | 0 | luaC_freeallobjects(L); /* just collect its objects */ |
259 | 59.5k | else { /* closing a fully built state */ |
260 | 59.5k | resetCI(L); |
261 | 59.5k | luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ |
262 | 59.5k | L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */ |
263 | 59.5k | luaC_freeallobjects(L); /* collect all objects */ |
264 | 59.5k | luai_userstateclose(L); |
265 | 59.5k | } |
266 | 59.5k | luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); |
267 | 59.5k | freestack(L); |
268 | 59.5k | lua_assert(gettotalbytes(g) == sizeof(global_State)); |
269 | 59.5k | (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */ |
270 | 59.5k | } |
271 | | |
272 | | |
273 | 9.00k | LUA_API lua_State *lua_newthread (lua_State *L) { |
274 | 9.00k | global_State *g = G(L); |
275 | 9.00k | GCObject *o; |
276 | 9.00k | lua_State *L1; |
277 | 9.00k | lua_lock(L); |
278 | 9.00k | luaC_checkGC(L); |
279 | | /* create new thread */ |
280 | 9.00k | o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); |
281 | 9.00k | L1 = gco2th(o); |
282 | | /* anchor it on L stack */ |
283 | 9.00k | setthvalue2s(L, L->top.p, L1); |
284 | 9.00k | api_incr_top(L); |
285 | 9.00k | preinit_thread(L1, g); |
286 | 9.00k | L1->hookmask = L->hookmask; |
287 | 9.00k | L1->basehookcount = L->basehookcount; |
288 | 9.00k | L1->hook = L->hook; |
289 | 9.00k | resethookcount(L1); |
290 | | /* initialize L1 extra space */ |
291 | 9.00k | memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)), |
292 | 9.00k | LUA_EXTRASPACE); |
293 | 9.00k | luai_userstatethread(L, L1); |
294 | 9.00k | stack_init(L1, L); /* init stack */ |
295 | 9.00k | lua_unlock(L); |
296 | 9.00k | return L1; |
297 | 9.00k | } |
298 | | |
299 | | |
300 | 9.00k | void luaE_freethread (lua_State *L, lua_State *L1) { |
301 | 9.00k | LX *l = fromstate(L1); |
302 | 9.00k | luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ |
303 | 9.00k | lua_assert(L1->openupval == NULL); |
304 | 9.00k | luai_userstatefree(L, L1); |
305 | 9.00k | freestack(L1); |
306 | 9.00k | luaM_free(L, l); |
307 | 9.00k | } |
308 | | |
309 | | |
310 | 25 | TStatus luaE_resetthread (lua_State *L, TStatus status) { |
311 | 25 | resetCI(L); |
312 | 25 | if (status == LUA_YIELD) |
313 | 0 | status = LUA_OK; |
314 | 25 | status = luaD_closeprotected(L, 1, status); |
315 | 25 | if (status != LUA_OK) /* errors? */ |
316 | 25 | luaD_seterrorobj(L, status, L->stack.p + 1); |
317 | 0 | else |
318 | 0 | L->top.p = L->stack.p + 1; |
319 | 25 | luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0); |
320 | 25 | return status; |
321 | 25 | } |
322 | | |
323 | | |
324 | 25 | LUA_API int lua_closethread (lua_State *L, lua_State *from) { |
325 | 25 | TStatus status; |
326 | 25 | lua_lock(L); |
327 | 25 | L->nCcalls = (from) ? getCcalls(from) : 0; |
328 | 25 | status = luaE_resetthread(L, L->status); |
329 | 25 | if (L == from) /* closing itself? */ |
330 | 0 | luaD_throwbaselevel(L, status); |
331 | 25 | lua_unlock(L); |
332 | 25 | return APIstatus(status); |
333 | 25 | } |
334 | | |
335 | | |
336 | 59.5k | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { |
337 | 59.5k | int i; |
338 | 59.5k | lua_State *L; |
339 | 59.5k | global_State *g = cast(global_State*, |
340 | 59.5k | (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State))); |
341 | 59.5k | if (g == NULL) return NULL; |
342 | 59.5k | L = &g->mainth.l; |
343 | 59.5k | L->tt = LUA_VTHREAD; |
344 | 59.5k | g->currentwhite = bitmask(WHITE0BIT); |
345 | 59.5k | L->marked = luaC_white(g); |
346 | 59.5k | preinit_thread(L, g); |
347 | 59.5k | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
348 | 0 | L->next = NULL; |
349 | 59.5k | incnny(L); /* main thread is always non yieldable */ |
350 | 59.5k | g->frealloc = f; |
351 | 59.5k | g->ud = ud; |
352 | 59.5k | g->warnf = NULL; |
353 | 59.5k | g->ud_warn = NULL; |
354 | 59.5k | g->seed = seed; |
355 | 59.5k | g->gcstp = GCSTPGC; /* no GC while building state */ |
356 | 59.5k | g->strt.size = g->strt.nuse = 0; |
357 | 59.5k | g->strt.hash = NULL; |
358 | 59.5k | setnilvalue(&g->l_registry); |
359 | 59.5k | g->panic = NULL; |
360 | 59.5k | g->gcstate = GCSpause; |
361 | 59.5k | g->gckind = KGC_INC; |
362 | 59.5k | g->gcstopem = 0; |
363 | 59.5k | g->gcemergency = 0; |
364 | 59.5k | g->finobj = g->tobefnz = g->fixedgc = NULL; |
365 | 59.5k | g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; |
366 | 59.5k | g->finobjsur = g->finobjold1 = g->finobjrold = NULL; |
367 | 59.5k | g->sweepgc = NULL; |
368 | 59.5k | g->gray = g->grayagain = NULL; |
369 | 59.5k | g->weak = g->ephemeron = g->allweak = NULL; |
370 | 59.5k | g->twups = NULL; |
371 | 59.5k | g->GCtotalbytes = sizeof(global_State); |
372 | 59.5k | g->GCmarked = 0; |
373 | 59.5k | g->GCdebt = 0; |
374 | 59.5k | setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ |
375 | 59.5k | setgcparam(g, PAUSE, LUAI_GCPAUSE); |
376 | 59.5k | setgcparam(g, STEPMUL, LUAI_GCMUL); |
377 | 59.5k | setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE); |
378 | 59.5k | setgcparam(g, MINORMUL, LUAI_GENMINORMUL); |
379 | 59.5k | setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); |
380 | 59.5k | setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); |
381 | 595k | for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL; |
382 | 59.5k | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
383 | | /* memory allocation error: free partial state */ |
384 | 0 | close_state(L); |
385 | 0 | L = NULL; |
386 | 0 | } |
387 | 59.5k | return L; |
388 | 59.5k | } |
389 | | |
390 | | |
391 | 59.5k | LUA_API void lua_close (lua_State *L) { |
392 | 59.5k | lua_lock(L); |
393 | 59.5k | L = mainthread(G(L)); /* only the main thread can be closed */ |
394 | 59.5k | close_state(L); |
395 | 59.5k | } |
396 | | |
397 | | |
398 | 163k | void luaE_warning (lua_State *L, const char *msg, int tocont) { |
399 | 163k | lua_WarnFunction wf = G(L)->warnf; |
400 | 163k | if (wf != NULL) |
401 | 163k | wf(G(L)->ud_warn, msg, tocont); |
402 | 163k | } |
403 | | |
404 | | |
405 | | /* |
406 | | ** Generate a warning from an error message |
407 | | */ |
408 | 4.76k | void luaE_warnerror (lua_State *L, const char *where) { |
409 | 4.76k | TValue *errobj = s2v(L->top.p - 1); /* error object */ |
410 | 4.76k | const char *msg = (ttisstring(errobj)) |
411 | 4.76k | ? getstr(tsvalue(errobj)) |
412 | 4.76k | : "error object is not a string"; |
413 | | /* produce warning "error in %s (%s)" (where, msg) */ |
414 | 4.76k | luaE_warning(L, "error in ", 1); |
415 | 4.76k | luaE_warning(L, where, 1); |
416 | 4.76k | luaE_warning(L, " (", 1); |
417 | 4.76k | luaE_warning(L, msg, 1); |
418 | 4.76k | luaE_warning(L, ")", 0); |
419 | 4.76k | } |
420 | | |