Coverage Report

Created: 2025-07-18 06:59

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