Coverage Report

Created: 2025-08-09 06:54

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