Coverage Report

Created: 2025-12-11 06:33

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
51.2k
static l_noret error (LoadState *S, const char *why) {
46
51.2k
  luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
47
51.2k
  luaD_throw(S->L, LUA_ERRSYNTAX);
48
51.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
321k
#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
56
57
171k
static void loadBlock (LoadState *S, void *b, size_t size) {
58
171k
  if (luaZ_read(S->Z, b, size) != 0)
59
14.3k
    error(S, "truncated chunk");
60
156k
  S->offset += size;
61
156k
}
62
63
64
15.2k
static void loadAlign (LoadState *S, unsigned align) {
65
15.2k
  unsigned padding = align - cast_uint(S->offset % align);
66
15.2k
  if (padding < align) {  /* (padding == align) means no padding */
67
12.0k
    lua_Integer paddingContent;
68
12.0k
    loadBlock(S, &paddingContent, padding);
69
12.0k
    lua_assert(S->offset % align == 0);
70
12.0k
  }
71
15.2k
}
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
186k
#define loadVar(S,x)    loadVector(S,&x,1)
86
87
88
747k
static lu_byte loadByte (LoadState *S) {
89
747k
  int b = zgetc(S->Z);
90
747k
  if (b == EOZ)
91
5.89k
    error(S, "truncated chunk");
92
741k
  S->offset++;
93
741k
  return cast_byte(b);
94
747k
}
95
96
97
431k
static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) {
98
431k
  lua_Unsigned x = 0;
99
431k
  int b;
100
431k
  limit >>= 7;
101
513k
  do {
102
513k
    b = loadByte(S);
103
513k
    if (x > limit)
104
0
      error(S, "integer overflow");
105
513k
    x = (x << 7) | (b & 0x7f);
106
513k
  } while ((b & 0x80) != 0);
107
431k
  return x;
108
431k
}
109
110
111
126k
static size_t loadSize (LoadState *S) {
112
126k
  return cast_sizet(loadVarint(S, MAX_SIZE));
113
126k
}
114
115
116
211k
static int loadInt (LoadState *S) {
117
211k
  return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
118
211k
}
119
120
121
122
2.26k
static lua_Number loadNumber (LoadState *S) {
123
2.26k
  lua_Number x;
124
2.26k
  loadVar(S, x);
125
2.26k
  return x;
126
2.26k
}
127
128
129
8.67k
static lua_Integer loadInteger (LoadState *S) {
130
8.67k
  lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED);
131
  /* decode unsigned to signed */
132
8.67k
  if ((cx & 1) != 0)
133
1.87k
    return l_castU2S(~(cx >> 1));
134
6.80k
  else
135
6.80k
    return l_castU2S(cx >> 1);
136
8.67k
}
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
126k
static void loadString (LoadState *S, Proto *p, TString **sl) {
146
126k
  lua_State *L = S->L;
147
126k
  TString *ts;
148
126k
  TValue sv;
149
126k
  size_t size = loadSize(S);
150
126k
  if (size == 0) {  /* previously saved string? */
151
84.9k
    lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED);  /* get its index */
152
84.9k
    TValue stv;
153
84.9k
    if (idx == 0) {  /* no string? */
154
2.82k
      lua_assert(*sl == NULL);  /* must be prefilled */
155
2.82k
      return;
156
2.82k
    }
157
82.0k
    if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING)
158
0
      error(S, "invalid string index");
159
164k
    *sl = ts = tsvalue(&stv);  /* get its value */
160
164k
    luaC_objbarrier(L, p, ts);
161
164k
    return;  /* do not save it again */
162
164k
  }
163
41.9k
  else if ((size -= 1) <= LUAI_MAXSHORTLEN) {  /* short string? */
164
27.1k
    char buff[LUAI_MAXSHORTLEN + 1];  /* extra space for '\0' */
165
27.1k
    loadVector(S, buff, size + 1);  /* load string into buffer */
166
27.1k
    *sl = ts = luaS_newlstr(L, buff, size);  /* create string */
167
27.1k
    luaC_objbarrier(L, p, ts);
168
27.1k
  }
169
14.8k
  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
14.8k
  else {  /* create internal copy */
175
14.8k
    *sl = ts = luaS_createlngstrobj(L, size);  /* create string */
176
14.8k
    luaC_objbarrier(L, p, ts);
177
29.6k
    loadVector(S, getlngstr(ts), size + 1);  /* load directly in final place */
178
29.6k
  }
179
  /* add string to list of saved strings */
180
27.1k
  S->nstr++;
181
41.9k
  setsvalue(L, &sv, ts);
182
41.9k
  luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
183
41.9k
  luaC_objbarrierback(L, obj2gco(S->h), ts);
184
41.9k
}
185
186
187
14.5k
static void loadCode (LoadState *S, Proto *f) {
188
14.5k
  int n = loadInt(S);
189
14.5k
  loadAlign(S, sizeof(f->code[0]));
190
14.5k
  if (S->fixed) {
191
0
    f->code = getaddr(S, n, Instruction);
192
0
    f->sizecode = n;
193
0
  }
194
14.5k
  else {
195
14.5k
    f->code = luaM_newvectorchecked(S->L, n, Instruction);
196
0
    f->sizecode = n;
197
14.5k
    loadVector(S, f->code, n);
198
14.5k
  }
199
14.5k
}
200
201
202
static void loadFunction(LoadState *S, Proto *f);
203
204
205
14.5k
static void loadConstants (LoadState *S, Proto *f) {
206
14.5k
  int i;
207
14.5k
  int n = loadInt(S);
208
14.5k
  f->k = luaM_newvectorchecked(S->L, n, TValue);
209
0
  f->sizek = n;
210
91.3k
  for (i = 0; i < n; i++)
211
76.8k
    setnilvalue(&f->k[i]);
212
91.3k
  for (i = 0; i < n; i++) {
213
76.8k
    TValue *o = &f->k[i];
214
76.8k
    int t = loadByte(S);
215
76.8k
    switch (t) {
216
33
      case LUA_VNIL:
217
33
        setnilvalue(o);
218
33
        break;
219
462
      case LUA_VFALSE:
220
462
        setbfvalue(o);
221
462
        break;
222
29
      case LUA_VTRUE:
223
29
        setbtvalue(o);
224
29
        break;
225
2.26k
      case LUA_VNUMFLT:
226
2.26k
        setfltvalue(o, loadNumber(S));
227
2.26k
        break;
228
8.67k
      case LUA_VNUMINT:
229
8.67k
        setivalue(o, loadInteger(S));
230
8.67k
        break;
231
50.4k
      case LUA_VSHRSTR:
232
65.3k
      case LUA_VLNGSTR: {
233
65.3k
        lua_assert(f->source == NULL);
234
65.3k
        loadString(S, f, &f->source);  /* use 'source' to anchor string */
235
65.3k
        if (f->source == NULL)
236
0
          error(S, "bad format for constant string");
237
130k
        setsvalue2n(S->L, o, f->source);  /* save it in the right place */
238
65.3k
        f->source = NULL;
239
65.3k
        break;
240
130k
      }
241
0
      default: error(S, "invalid constant");
242
76.8k
    }
243
76.8k
  }
244
14.5k
}
245
246
247
14.5k
static void loadProtos (LoadState *S, Proto *f) {
248
14.5k
  int i;
249
14.5k
  int n = loadInt(S);
250
14.5k
  f->p = luaM_newvectorchecked(S->L, n, Proto *);
251
0
  f->sizep = n;
252
23.8k
  for (i = 0; i < n; i++)
253
9.29k
    f->p[i] = NULL;
254
23.8k
  for (i = 0; i < n; i++) {
255
9.29k
    f->p[i] = luaF_newproto(S->L);
256
9.29k
    luaC_objbarrier(S->L, f, f->p[i]);
257
9.29k
    loadFunction(S, f->p[i]);
258
9.29k
  }
259
14.5k
}
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
14.5k
static void loadUpvalues (LoadState *S, Proto *f) {
269
14.5k
  int i;
270
14.5k
  int n = loadInt(S);
271
14.5k
  f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
272
0
  f->sizeupvalues = n;
273
30.0k
  for (i = 0; i < n; i++)  /* make array valid for GC */
274
15.5k
    f->upvalues[i].name = NULL;
275
30.0k
  for (i = 0; i < n; i++) {  /* following calls can raise errors */
276
15.5k
    f->upvalues[i].instack = loadByte(S);
277
15.5k
    f->upvalues[i].idx = loadByte(S);
278
15.5k
    f->upvalues[i].kind = loadByte(S);
279
15.5k
  }
280
14.5k
}
281
282
283
14.5k
static void loadDebug (LoadState *S, Proto *f) {
284
14.5k
  int i;
285
14.5k
  int n = loadInt(S);
286
14.5k
  if (S->fixed) {
287
0
    f->lineinfo = getaddr(S, n, ls_byte);
288
0
    f->sizelineinfo = n;
289
0
  }
290
14.5k
  else {
291
14.5k
    f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
292
0
    f->sizelineinfo = n;
293
14.5k
    loadVector(S, f->lineinfo, n);
294
14.5k
  }
295
14.5k
  n = loadInt(S);
296
14.5k
  if (n > 0) {
297
756
    loadAlign(S, sizeof(int));
298
756
    if (S->fixed) {
299
0
      f->abslineinfo = getaddr(S, n, AbsLineInfo);
300
0
      f->sizeabslineinfo = n;
301
0
    }
302
756
    else {
303
756
      f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
304
0
      f->sizeabslineinfo = n;
305
756
      loadVector(S, f->abslineinfo, n);
306
756
    }
307
756
  }
308
14.5k
  n = loadInt(S);
309
14.5k
  f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
310
0
  f->sizelocvars = n;
311
47.6k
  for (i = 0; i < n; i++)
312
33.1k
    f->locvars[i].varname = NULL;
313
47.6k
  for (i = 0; i < n; i++) {
314
33.1k
    loadString(S, f, &f->locvars[i].varname);
315
33.1k
    f->locvars[i].startpc = loadInt(S);
316
33.1k
    f->locvars[i].endpc = loadInt(S);
317
33.1k
  }
318
14.5k
  n = loadInt(S);
319
14.5k
  if (n != 0)  /* does it have debug information? */
320
9.61k
    n = f->sizeupvalues;  /* must be this many */
321
28.3k
  for (i = 0; i < n; i++)
322
13.8k
    loadString(S, f, &f->upvalues[i].name);
323
14.5k
}
324
325
326
14.5k
static void loadFunction (LoadState *S, Proto *f) {
327
14.5k
  f->linedefined = loadInt(S);
328
14.5k
  f->lastlinedefined = loadInt(S);
329
14.5k
  f->numparams = loadByte(S);
330
  /* get only the meaningful flags */
331
14.5k
  f->flag = cast_byte(loadByte(S) & ~PF_FIXED);
332
14.5k
  if (S->fixed)
333
0
    f->flag |= PF_FIXED;  /* signal that code is fixed */
334
14.5k
  f->maxstacksize = loadByte(S);
335
14.5k
  loadCode(S, f);
336
14.5k
  loadConstants(S, f);
337
14.5k
  loadUpvalues(S, f);
338
14.5k
  loadProtos(S, f);
339
14.5k
  loadString(S, f, &f->source);
340
14.5k
  loadDebug(S, f);
341
14.5k
}
342
343
344
63.8k
static void checkliteral (LoadState *S, const char *s, const char *msg) {
345
63.8k
  char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
346
63.8k
  size_t len = strlen(s);
347
63.8k
  loadVector(S, buff, len);
348
63.8k
  if (memcmp(s, buff, len) != 0)
349
20.2k
    error(S, msg);
350
63.8k
}
351
352
353
223
static l_noret numerror (LoadState *S, const char *what, const char *tname) {
354
223
  const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what);
355
223
  error(S, msg);
356
223
}
357
358
359
21.4k
static void checknumsize (LoadState *S, int size, const char *tname) {
360
21.4k
  if (size != loadByte(S))
361
42
    numerror(S, "size", tname);
362
21.4k
}
363
364
365
21.3k
static void checknumformat (LoadState *S, int eq, const char *tname) {
366
21.3k
  if (!eq)
367
181
    numerror(S, "format", tname);
368
21.3k
}
369
370
371
#define checknum(S,tvar,value,tname)  \
372
183k
  { tvar i; checknumsize(S, sizeof(i), tname); \
373
183k
    loadVar(S, i); \
374
183k
    checknumformat(S, i == value, tname); }
375
376
377
56.5k
static void checkHeader (LoadState *S) {
378
  /* skip 1st char (already read and checked) */
379
56.5k
  checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
380
56.5k
  if (loadByte(S) != LUAC_VERSION)
381
7.24k
    error(S, "version mismatch");
382
49.2k
  if (loadByte(S) != LUAC_FORMAT)
383
3.32k
    error(S, "format mismatch");
384
45.9k
  checkliteral(S, LUAC_DATA, "corrupted chunk");
385
45.9k
  checknum(S, int, LUAC_INT, "int");
386
45.9k
  checknum(S, Instruction, LUAC_INST, "instruction");
387
45.9k
  checknum(S, lua_Integer, LUAC_INT, "Lua integer");
388
45.9k
  checknum(S, lua_Number, LUAC_NUM, "Lua number");
389
45.9k
}
390
391
392
/*
393
** Load precompiled chunk.
394
*/
395
56.5k
LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
396
56.5k
  LoadState S;
397
56.5k
  LClosure *cl;
398
56.5k
  if (*name == '@' || *name == '=')
399
906
    name = name + 1;
400
55.5k
  else if (*name == LUA_SIGNATURE[0])
401
45.7k
    name = "binary string";
402
56.5k
  S.name = name;
403
56.5k
  S.L = L;
404
56.5k
  S.Z = Z;
405
56.5k
  S.fixed = cast_byte(fixed);
406
56.5k
  S.offset = 1;  /* fist byte was already read */
407
56.5k
  checkHeader(&S);
408
56.5k
  cl = luaF_newLclosure(L, loadByte(&S));
409
56.5k
  setclLvalue2s(L, L->top.p, cl);
410
5.21k
  luaD_inctop(L);
411
5.21k
  S.h = luaH_new(L);  /* create list of saved strings */
412
5.21k
  S.nstr = 0;
413
5.21k
  sethvalue2s(L, L->top.p, S.h);  /* anchor it */
414
5.21k
  luaD_inctop(L);
415
5.21k
  cl->p = luaF_newproto(L);
416
5.21k
  luaC_objbarrier(L, cl, cl->p);
417
5.21k
  loadFunction(&S, cl->p);
418
5.21k
  if (cl->nupvalues != cl->p->sizeupvalues)
419
0
    error(&S, "corrupted chunk");
420
5.21k
  luai_verifycode(L, cl->p);
421
5.21k
  L->top.p--;  /* pop table */
422
5.21k
  return cl;
423
5.21k
}
424