Coverage Report

Created: 2025-08-25 07:03

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