Coverage Report

Created: 2025-12-14 06:40

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