Coverage Report

Created: 2025-07-11 06:33

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