Coverage Report

Created: 2025-11-11 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lundump.c
Line
Count
Source
1
/*
2
** $Id: lundump.c $
3
** load precompiled Lua chunks
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lundump_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <limits.h>
14
#include <string.h>
15
16
#include "lua.h"
17
18
#include "ldebug.h"
19
#include "ldo.h"
20
#include "lfunc.h"
21
#include "lmem.h"
22
#include "lobject.h"
23
#include "lstring.h"
24
#include "ltable.h"
25
#include "lundump.h"
26
#include "lzio.h"
27
28
29
#if !defined(luai_verifycode)
30
#define luai_verifycode(L,f)  /* empty */
31
#endif
32
33
34
typedef struct {
35
  lua_State *L;
36
  ZIO *Z;
37
  const char *name;
38
  Table *h;  /* list for string reuse */
39
  size_t offset;  /* current position relative to beginning of dump */
40
  lua_Unsigned nstr;  /* number of strings in the list */
41
  lu_byte fixed;  /* dump is fixed in memory */
42
} LoadState;
43
44
45
37.2k
static l_noret error (LoadState *S, const char *why) {
46
37.2k
  luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
47
37.2k
  luaD_throw(S->L, LUA_ERRSYNTAX);
48
37.2k
}
49
50
51
/*
52
** All high-level loads go through loadVector; you can change it to
53
** adapt to the endianness of the input
54
*/
55
569k
#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
56
57
514k
static void loadBlock (LoadState *S, void *b, size_t size) {
58
514k
  if (luaZ_read(S->Z, b, size) != 0)
59
12.1k
    error(S, "truncated chunk");
60
502k
  S->offset += size;
61
502k
}
62
63
64
54.1k
static void loadAlign (LoadState *S, unsigned align) {
65
54.1k
  unsigned padding = align - cast_uint(S->offset % align);
66
54.1k
  if (padding < align) {  /* (padding == align) means no padding */
67
49.7k
    lua_Integer paddingContent;
68
49.7k
    loadBlock(S, &paddingContent, padding);
69
49.7k
    lua_assert(S->offset % align == 0);
70
49.7k
  }
71
54.1k
}
72
73
74
0
#define getaddr(S,n,t)  cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
75
76
0
static const void *getaddr_ (LoadState *S, size_t size) {
77
0
  const void *block = luaZ_getaddr(S->Z, size);
78
0
  S->offset += size;
79
0
  if (block == NULL)
80
0
    error(S, "truncated fixed buffer");
81
0
  return block;
82
0
}
83
84
85
270k
#define loadVar(S,x)    loadVector(S,&x,1)
86
87
88
1.77M
static lu_byte loadByte (LoadState *S) {
89
1.77M
  int b = zgetc(S->Z);
90
1.77M
  if (b == EOZ)
91
7.09k
    error(S, "truncated chunk");
92
1.77M
  S->offset++;
93
1.77M
  return cast_byte(b);
94
1.77M
}
95
96
97
1.00M
static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) {
98
1.00M
  lua_Unsigned x = 0;
99
1.00M
  int b;
100
1.00M
  limit >>= 7;
101
1.14M
  do {
102
1.14M
    b = loadByte(S);
103
1.14M
    if (x > limit)
104
0
      error(S, "integer overflow");
105
1.14M
    x = (x << 7) | (b & 0x7f);
106
1.14M
  } while ((b & 0x80) != 0);
107
1.00M
  return x;
108
1.00M
}
109
110
111
211k
static size_t loadSize (LoadState *S) {
112
211k
  return cast_sizet(loadVarint(S, MAX_SIZE));
113
211k
}
114
115
116
644k
static int loadInt (LoadState *S) {
117
644k
  return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
118
644k
}
119
120
121
122
2.84k
static lua_Number loadNumber (LoadState *S) {
123
2.84k
  lua_Number x;
124
2.84k
  loadVar(S, x);
125
2.84k
  return x;
126
2.84k
}
127
128
129
9.60k
static lua_Integer loadInteger (LoadState *S) {
130
9.60k
  lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED);
131
  /* decode unsigned to signed */
132
9.60k
  if ((cx & 1) != 0)
133
2.33k
    return l_castU2S(~(cx >> 1));
134
7.26k
  else
135
7.26k
    return l_castU2S(cx >> 1);
136
9.60k
}
137
138
139
/*
140
** Load a nullable string into slot 'sl' from prototype 'p'. The
141
** assignment to the slot and the barrier must be performed before any
142
** possible GC activity, to anchor the string. (Both 'loadVector' and
143
** 'luaH_setint' can call the GC.)
144
*/
145
211k
static void loadString (LoadState *S, Proto *p, TString **sl) {
146
211k
  lua_State *L = S->L;
147
211k
  TString *ts;
148
211k
  TValue sv;
149
211k
  size_t size = loadSize(S);
150
211k
  if (size == 0) {  /* previously saved string? */
151
140k
    lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED);  /* get its index */
152
140k
    TValue stv;
153
140k
    if (idx == 0) {  /* no string? */
154
21.0k
      lua_assert(*sl == NULL);  /* must be prefilled */
155
21.0k
      return;
156
21.0k
    }
157
119k
    if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING)
158
0
      error(S, "invalid string index");
159
238k
    *sl = ts = tsvalue(&stv);  /* get its value */
160
238k
    luaC_objbarrier(L, p, ts);
161
238k
    return;  /* do not save it again */
162
238k
  }
163
71.1k
  else if ((size -= 1) <= LUAI_MAXSHORTLEN) {  /* short string? */
164
37.1k
    char buff[LUAI_MAXSHORTLEN + 1];  /* extra space for '\0' */
165
37.1k
    loadVector(S, buff, size + 1);  /* load string into buffer */
166
37.1k
    *sl = ts = luaS_newlstr(L, buff, size);  /* create string */
167
37.1k
    luaC_objbarrier(L, p, ts);
168
37.1k
  }
169
34.0k
  else if (S->fixed) {  /* for a fixed buffer, use a fixed string */
170
0
    const char *s = getaddr(S, size + 1, char);  /* get content address */
171
0
    *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
172
0
    luaC_objbarrier(L, p, ts);
173
0
  }
174
34.0k
  else {  /* create internal copy */
175
34.0k
    *sl = ts = luaS_createlngstrobj(L, size);  /* create string */
176
34.0k
    luaC_objbarrier(L, p, ts);
177
68.0k
    loadVector(S, getlngstr(ts), size + 1);  /* load directly in final place */
178
68.0k
  }
179
  /* add string to list of saved strings */
180
37.1k
  S->nstr++;
181
71.1k
  setsvalue(L, &sv, ts);
182
71.1k
  luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
183
71.1k
  luaC_objbarrierback(L, obj2gco(S->h), ts);
184
71.1k
}
185
186
187
53.4k
static void loadCode (LoadState *S, Proto *f) {
188
53.4k
  int n = loadInt(S);
189
53.4k
  loadAlign(S, sizeof(f->code[0]));
190
53.4k
  if (S->fixed) {
191
0
    f->code = getaddr(S, n, Instruction);
192
0
    f->sizecode = n;
193
0
  }
194
53.4k
  else {
195
53.4k
    f->code = luaM_newvectorchecked(S->L, n, Instruction);
196
0
    f->sizecode = n;
197
53.4k
    loadVector(S, f->code, n);
198
53.4k
  }
199
53.4k
}
200
201
202
static void loadFunction(LoadState *S, Proto *f);
203
204
205
53.4k
static void loadConstants (LoadState *S, Proto *f) {
206
53.4k
  int i;
207
53.4k
  int n = loadInt(S);
208
53.4k
  f->k = luaM_newvectorchecked(S->L, n, TValue);
209
0
  f->sizek = n;
210
152k
  for (i = 0; i < n; i++)
211
98.6k
    setnilvalue(&f->k[i]);
212
152k
  for (i = 0; i < n; i++) {
213
98.6k
    TValue *o = &f->k[i];
214
98.6k
    int t = loadByte(S);
215
98.6k
    switch (t) {
216
155
      case LUA_VNIL:
217
155
        setnilvalue(o);
218
155
        break;
219
84
      case LUA_VFALSE:
220
84
        setbfvalue(o);
221
84
        break;
222
132
      case LUA_VTRUE:
223
132
        setbtvalue(o);
224
132
        break;
225
2.84k
      case LUA_VNUMFLT:
226
2.84k
        setfltvalue(o, loadNumber(S));
227
2.84k
        break;
228
9.60k
      case LUA_VNUMINT:
229
9.60k
        setivalue(o, loadInteger(S));
230
9.60k
        break;
231
68.8k
      case LUA_VSHRSTR:
232
85.8k
      case LUA_VLNGSTR: {
233
85.8k
        lua_assert(f->source == NULL);
234
85.8k
        loadString(S, f, &f->source);  /* use 'source' to anchor string */
235
85.8k
        if (f->source == NULL)
236
0
          error(S, "bad format for constant string");
237
171k
        setsvalue2n(S->L, o, f->source);  /* save it in the right place */
238
85.8k
        f->source = NULL;
239
85.8k
        break;
240
171k
      }
241
0
      default: error(S, "invalid constant");
242
98.6k
    }
243
98.6k
  }
244
53.4k
}
245
246
247
53.4k
static void loadProtos (LoadState *S, Proto *f) {
248
53.4k
  int i;
249
53.4k
  int n = loadInt(S);
250
53.4k
  f->p = luaM_newvectorchecked(S->L, n, Proto *);
251
0
  f->sizep = n;
252
66.3k
  for (i = 0; i < n; i++)
253
12.9k
    f->p[i] = NULL;
254
66.3k
  for (i = 0; i < n; i++) {
255
12.9k
    f->p[i] = luaF_newproto(S->L);
256
12.9k
    luaC_objbarrier(S->L, f, f->p[i]);
257
12.9k
    loadFunction(S, f->p[i]);
258
12.9k
  }
259
53.4k
}
260
261
262
/*
263
** Load the upvalues for a function. The names must be filled first,
264
** because the filling of the other fields can raise read errors and
265
** the creation of the error message can call an emergency collection;
266
** in that case all prototypes must be consistent for the GC.
267
*/
268
53.4k
static void loadUpvalues (LoadState *S, Proto *f) {
269
53.4k
  int i;
270
53.4k
  int n = loadInt(S);
271
53.4k
  f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
272
0
  f->sizeupvalues = n;
273
72.8k
  for (i = 0; i < n; i++)  /* make array valid for GC */
274
19.4k
    f->upvalues[i].name = NULL;
275
72.8k
  for (i = 0; i < n; i++) {  /* following calls can raise errors */
276
19.4k
    f->upvalues[i].instack = loadByte(S);
277
19.4k
    f->upvalues[i].idx = loadByte(S);
278
19.4k
    f->upvalues[i].kind = loadByte(S);
279
19.4k
  }
280
53.4k
}
281
282
283
53.4k
static void loadDebug (LoadState *S, Proto *f) {
284
53.4k
  int i;
285
53.4k
  int n = loadInt(S);
286
53.4k
  if (S->fixed) {
287
0
    f->lineinfo = getaddr(S, n, ls_byte);
288
0
    f->sizelineinfo = n;
289
0
  }
290
53.4k
  else {
291
53.4k
    f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
292
0
    f->sizelineinfo = n;
293
53.4k
    loadVector(S, f->lineinfo, n);
294
53.4k
  }
295
53.4k
  n = loadInt(S);
296
53.4k
  if (n > 0) {
297
785
    loadAlign(S, sizeof(int));
298
785
    if (S->fixed) {
299
0
      f->abslineinfo = getaddr(S, n, AbsLineInfo);
300
0
      f->sizeabslineinfo = n;
301
0
    }
302
785
    else {
303
785
      f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
304
0
      f->sizeabslineinfo = n;
305
785
      loadVector(S, f->abslineinfo, n);
306
785
    }
307
785
  }
308
53.4k
  n = loadInt(S);
309
53.4k
  f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
310
0
  f->sizelocvars = n;
311
108k
  for (i = 0; i < n; i++)
312
55.2k
    f->locvars[i].varname = NULL;
313
108k
  for (i = 0; i < n; i++) {
314
55.2k
    loadString(S, f, &f->locvars[i].varname);
315
55.2k
    f->locvars[i].startpc = loadInt(S);
316
55.2k
    f->locvars[i].endpc = loadInt(S);
317
55.2k
  }
318
53.4k
  n = loadInt(S);
319
53.4k
  if (n != 0)  /* does it have debug information? */
320
11.7k
    n = f->sizeupvalues;  /* must be this many */
321
70.6k
  for (i = 0; i < n; i++)
322
17.2k
    loadString(S, f, &f->upvalues[i].name);
323
53.4k
}
324
325
326
53.4k
static void loadFunction (LoadState *S, Proto *f) {
327
53.4k
  f->linedefined = loadInt(S);
328
53.4k
  f->lastlinedefined = loadInt(S);
329
53.4k
  f->numparams = loadByte(S);
330
  /* get only the meaningful flags */
331
53.4k
  f->flag = cast_byte(loadByte(S) & ~PF_FIXED);
332
53.4k
  if (S->fixed)
333
0
    f->flag |= PF_FIXED;  /* signal that code is fixed */
334
53.4k
  f->maxstacksize = loadByte(S);
335
53.4k
  loadCode(S, f);
336
53.4k
  loadConstants(S, f);
337
53.4k
  loadUpvalues(S, f);
338
53.4k
  loadProtos(S, f);
339
53.4k
  loadString(S, f, &f->source);
340
53.4k
  loadDebug(S, f);
341
53.4k
}
342
343
344
120k
static void checkliteral (LoadState *S, const char *s, const char *msg) {
345
120k
  char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
346
120k
  size_t len = strlen(s);
347
120k
  loadVector(S, buff, len);
348
120k
  if (memcmp(s, buff, len) != 0)
349
7.00k
    error(S, msg);
350
120k
}
351
352
353
296
static l_noret numerror (LoadState *S, const char *what, const char *tname) {
354
296
  const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what);
355
296
  error(S, msg);
356
296
}
357
358
359
162k
static void checknumsize (LoadState *S, int size, const char *tname) {
360
162k
  if (size != loadByte(S))
361
54
    numerror(S, "size", tname);
362
162k
}
363
364
365
162k
static void checknumformat (LoadState *S, int eq, const char *tname) {
366
162k
  if (!eq)
367
242
    numerror(S, "format", tname);
368
162k
}
369
370
371
#define checknum(S,tvar,value,tname)  \
372
267k
  { tvar i; checknumsize(S, sizeof(i), tname); \
373
267k
    loadVar(S, i); \
374
267k
    checknumformat(S, i == value, tname); }
375
376
377
77.6k
static void checkHeader (LoadState *S) {
378
  /* skip 1st char (already read and checked) */
379
77.6k
  checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
380
77.6k
  if (loadByte(S) != LUAC_VERSION)
381
7.13k
    error(S, "version mismatch");
382
70.5k
  if (loadByte(S) != LUAC_FORMAT)
383
3.57k
    error(S, "format mismatch");
384
66.9k
  checkliteral(S, LUAC_DATA, "corrupted chunk");
385
66.9k
  checknum(S, int, LUAC_INT, "int");
386
66.9k
  checknum(S, Instruction, LUAC_INST, "instruction");
387
66.9k
  checknum(S, lua_Integer, LUAC_INT, "Lua integer");
388
66.9k
  checknum(S, lua_Number, LUAC_NUM, "Lua number");
389
66.9k
}
390
391
392
/*
393
** Load precompiled chunk.
394
*/
395
77.6k
LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
396
77.6k
  LoadState S;
397
77.6k
  LClosure *cl;
398
77.6k
  if (*name == '@' || *name == '=')
399
914
    name = name + 1;
400
76.7k
  else if (*name == LUA_SIGNATURE[0])
401
39.6k
    name = "binary string";
402
77.6k
  S.name = name;
403
77.6k
  S.L = L;
404
77.6k
  S.Z = Z;
405
77.6k
  S.fixed = cast_byte(fixed);
406
77.6k
  S.offset = 1;  /* fist byte was already read */
407
77.6k
  checkHeader(&S);
408
77.6k
  cl = luaF_newLclosure(L, loadByte(&S));
409
77.6k
  setclLvalue2s(L, L->top.p, cl);
410
40.4k
  luaD_inctop(L);
411
40.4k
  S.h = luaH_new(L);  /* create list of saved strings */
412
40.4k
  S.nstr = 0;
413
40.4k
  sethvalue2s(L, L->top.p, S.h);  /* anchor it */
414
40.4k
  luaD_inctop(L);
415
40.4k
  cl->p = luaF_newproto(L);
416
40.4k
  luaC_objbarrier(L, cl, cl->p);
417
40.4k
  loadFunction(&S, cl->p);
418
40.4k
  if (cl->nupvalues != cl->p->sizeupvalues)
419
0
    error(&S, "corrupted chunk");
420
40.4k
  luai_verifycode(L, cl->p);
421
40.4k
  L->top.p--;  /* pop table */
422
40.4k
  return cl;
423
40.4k
}
424