Coverage Report

Created: 2025-10-27 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/ldump.c
Line
Count
Source
1
/*
2
** $Id: ldump.c $
3
** save precompiled Lua chunks
4
** See Copyright Notice in lua.h
5
*/
6
7
#define ldump_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <limits.h>
14
#include <stddef.h>
15
16
#include "lua.h"
17
18
#include "lapi.h"
19
#include "lgc.h"
20
#include "lobject.h"
21
#include "lstate.h"
22
#include "ltable.h"
23
#include "lundump.h"
24
25
26
typedef struct {
27
  lua_State *L;
28
  lua_Writer writer;
29
  void *data;
30
  size_t offset;  /* current position relative to beginning of dump */
31
  int strip;
32
  int status;
33
  Table *h;  /* table to track saved strings */
34
  lua_Unsigned nstr;  /* counter for counting saved strings */
35
} DumpState;
36
37
38
/*
39
** All high-level dumps go through dumpVector; you can change it to
40
** change the endianness of the result
41
*/
42
11.3M
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
43
44
39.7k
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
45
46
47
/*
48
** Dump the block of memory pointed by 'b' with given 'size'.
49
** 'b' should not be NULL, except for the last call signaling the end
50
** of the dump.
51
*/
52
11.4M
static void dumpBlock (DumpState *D, const void *b, size_t size) {
53
11.4M
  if (D->status == 0) {  /* do not write anything after an error */
54
11.4M
    lua_unlock(D->L);
55
11.4M
    D->status = (*D->writer)(D->L, b, size, D->data);
56
11.4M
    lua_lock(D->L);
57
11.4M
    D->offset += size;
58
11.4M
  }
59
11.4M
}
60
61
62
/*
63
** Dump enough zeros to ensure that current position is a multiple of
64
** 'align'.
65
*/
66
180k
static void dumpAlign (DumpState *D, unsigned align) {
67
180k
  unsigned padding = align - cast_uint(D->offset % align);
68
180k
  if (padding < align) {  /* padding == align means no padding */
69
56.8k
    static lua_Integer paddingContent = 0;
70
56.8k
    lua_assert(align <= sizeof(lua_Integer));
71
56.8k
    dumpBlock(D, &paddingContent, padding);
72
56.8k
  }
73
180k
  lua_assert(D->offset % align == 0);
74
180k
}
75
76
77
6.44M
#define dumpVar(D,x)    dumpVector(D,&x,1)
78
79
80
3.78M
static void dumpByte (DumpState *D, int y) {
81
3.78M
  lu_byte x = (lu_byte)y;
82
3.78M
  dumpVar(D, x);
83
3.78M
}
84
85
86
/*
87
** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
88
** (The "+6" rounds up the division.)
89
*/
90
5.09M
#define DIBS    ((l_numbits(lua_Unsigned) + 6) / 7)
91
92
/*
93
** Dumps an unsigned integer using the MSB Varint encoding
94
*/
95
4.35M
static void dumpVarint (DumpState *D, lua_Unsigned x) {
96
4.35M
  lu_byte buff[DIBS];
97
4.35M
  unsigned n = 1;
98
4.35M
  buff[DIBS - 1] = x & 0x7f;  /* fill least-significant byte */
99
5.09M
  while ((x >>= 7) != 0)  /* fill other bytes in reverse order */
100
4.35M
    buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
101
4.35M
  dumpVector(D, buff + DIBS - n, n);
102
4.35M
}
103
104
105
229k
static void dumpSize (DumpState *D, size_t sz) {
106
229k
  dumpVarint(D, cast(lua_Unsigned, sz));
107
229k
}
108
109
110
2.61M
static void dumpInt (DumpState *D, int x) {
111
2.61M
  lua_assert(x >= 0);
112
2.61M
  dumpVarint(D, cast_uint(x));
113
2.61M
}
114
115
116
2.58M
static void dumpNumber (DumpState *D, lua_Number x) {
117
2.58M
  dumpVar(D, x);
118
2.58M
}
119
120
121
/*
122
** Signed integers are coded to keep small values small. (Coding -1 as
123
** 0xfff...fff would use too many bytes to save a quite common value.)
124
** A non-negative x is coded as 2x; a negative x is coded as -2x - 1.
125
** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...)
126
*/
127
40.4k
static void dumpInteger (DumpState *D, lua_Integer x) {
128
40.4k
  lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x)
129
40.4k
                             : (2u * ~l_castS2U(x)) + 1;
130
40.4k
  dumpVarint(D, cx);
131
40.4k
}
132
133
134
/*
135
** Dump a String. First dump its "size":
136
** size==0 is followed by an index and means "reuse saved string with
137
** that index"; index==0 means NULL.
138
** size>=1 is followed by the string contents with real size==size-1 and
139
** means that string, which will be saved with the next available index.
140
** The real size does not include the ending '\0' (which is not dumped),
141
** so adding 1 to it cannot overflow a size_t.
142
*/
143
964k
static void dumpString (DumpState *D, TString *ts) {
144
964k
  if (ts == NULL) {
145
9.50k
    dumpVarint(D, 0);  /* will "reuse" NULL */
146
9.50k
    dumpVarint(D, 0);  /* special index for NULL */
147
9.50k
  }
148
954k
  else {
149
954k
    TValue idx;
150
954k
    int tag = luaH_getstr(D->h, ts, &idx);
151
954k
    if (!tagisempty(tag)) {  /* string already saved? */
152
725k
      dumpVarint(D, 0);  /* reuse a saved string */
153
725k
      dumpVarint(D, l_castS2U(ivalue(&idx)));  /* index of saved string */
154
725k
    }
155
229k
    else {  /* must write and save the string */
156
229k
      TValue key, value;  /* to save the string in the hash */
157
229k
      size_t size;
158
229k
      const char *s = getlstr(ts, size);
159
229k
      dumpSize(D, size + 1);
160
229k
      dumpVector(D, s, size + 1);  /* include ending '\0' */
161
229k
      D->nstr++;  /* one more saved string */
162
229k
      setsvalue(D->L, &key, ts);  /* the string is the key */
163
229k
      setivalue(&value, l_castU2S(D->nstr));  /* its index is the value */
164
229k
      luaH_set(D->L, D->h, &key, &value);  /* h[ts] = nstr */
165
      /* integer value does not need barrier */
166
229k
    }
167
954k
  }
168
964k
}
169
170
171
173k
static void dumpCode (DumpState *D, const Proto *f) {
172
173k
  dumpInt(D, f->sizecode);
173
173k
  dumpAlign(D, sizeof(f->code[0]));
174
173k
  lua_assert(f->code != NULL);
175
173k
  dumpVector(D, f->code, cast_uint(f->sizecode));
176
173k
}
177
178
179
static void dumpFunction (DumpState *D, const Proto *f);
180
181
173k
static void dumpConstants (DumpState *D, const Proto *f) {
182
173k
  int i;
183
173k
  int n = f->sizek;
184
173k
  dumpInt(D, n);
185
3.09M
  for (i = 0; i < n; i++) {
186
2.92M
    const TValue *o = &f->k[i];
187
2.92M
    int tt = ttypetag(o);
188
2.92M
    dumpByte(D, tt);
189
2.92M
    switch (tt) {
190
2.58M
      case LUA_VNUMFLT:
191
2.58M
        dumpNumber(D, fltvalue(o));
192
2.58M
        break;
193
40.4k
      case LUA_VNUMINT:
194
40.4k
        dumpInteger(D, ivalue(o));
195
40.4k
        break;
196
247k
      case LUA_VSHRSTR:
197
293k
      case LUA_VLNGSTR:
198
293k
        dumpString(D, tsvalue(o));
199
293k
        break;
200
2.76k
      default:
201
2.76k
        lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
202
2.92M
    }
203
2.92M
  }
204
173k
}
205
206
207
173k
static void dumpProtos (DumpState *D, const Proto *f) {
208
173k
  int i;
209
173k
  int n = f->sizep;
210
173k
  dumpInt(D, n);
211
327k
  for (i = 0; i < n; i++)
212
153k
    dumpFunction(D, f->p[i]);
213
173k
}
214
215
216
173k
static void dumpUpvalues (DumpState *D, const Proto *f) {
217
173k
  int i, n = f->sizeupvalues;
218
173k
  dumpInt(D, n);
219
241k
  for (i = 0; i < n; i++) {
220
67.9k
    dumpByte(D, f->upvalues[i].instack);
221
67.9k
    dumpByte(D, f->upvalues[i].idx);
222
67.9k
    dumpByte(D, f->upvalues[i].kind);
223
67.9k
  }
224
173k
}
225
226
227
173k
static void dumpDebug (DumpState *D, const Proto *f) {
228
173k
  int i, n;
229
173k
  n = (D->strip) ? 0 : f->sizelineinfo;
230
173k
  dumpInt(D, n);
231
173k
  if (f->lineinfo != NULL)
232
172k
    dumpVector(D, f->lineinfo, cast_uint(n));
233
173k
  n = (D->strip) ? 0 : f->sizeabslineinfo;
234
173k
  dumpInt(D, n);
235
173k
  if (n > 0) {
236
    /* 'abslineinfo' is an array of structures of int's */
237
7.13k
    dumpAlign(D, sizeof(int));
238
7.13k
    dumpVector(D, f->abslineinfo, cast_uint(n));
239
7.13k
  }
240
173k
  n = (D->strip) ? 0 : f->sizelocvars;
241
173k
  dumpInt(D, n);
242
611k
  for (i = 0; i < n; i++) {
243
437k
    dumpString(D, f->locvars[i].varname);
244
437k
    dumpInt(D, f->locvars[i].startpc);
245
437k
    dumpInt(D, f->locvars[i].endpc);
246
437k
  }
247
173k
  n = (D->strip) ? 0 : f->sizeupvalues;
248
173k
  dumpInt(D, n);
249
232k
  for (i = 0; i < n; i++)
250
59.1k
    dumpString(D, f->upvalues[i].name);
251
173k
}
252
253
254
173k
static void dumpFunction (DumpState *D, const Proto *f) {
255
173k
  dumpInt(D, f->linedefined);
256
173k
  dumpInt(D, f->lastlinedefined);
257
173k
  dumpByte(D, f->numparams);
258
173k
  dumpByte(D, f->flag);
259
173k
  dumpByte(D, f->maxstacksize);
260
173k
  dumpCode(D, f);
261
173k
  dumpConstants(D, f);
262
173k
  dumpUpvalues(D, f);
263
173k
  dumpProtos(D, f);
264
173k
  dumpString(D, D->strip ? NULL : f->source);
265
173k
  dumpDebug(D, f);
266
173k
}
267
268
269
#define dumpNumInfo(D, tvar, value)  \
270
79.4k
  { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
271
272
273
19.8k
static void dumpHeader (DumpState *D) {
274
19.8k
  dumpLiteral(D, LUA_SIGNATURE);
275
19.8k
  dumpByte(D, LUAC_VERSION);
276
19.8k
  dumpByte(D, LUAC_FORMAT);
277
19.8k
  dumpLiteral(D, LUAC_DATA);
278
19.8k
  dumpNumInfo(D, int, LUAC_INT);
279
19.8k
  dumpNumInfo(D, Instruction, LUAC_INST);
280
19.8k
  dumpNumInfo(D, lua_Integer, LUAC_INT);
281
19.8k
  dumpNumInfo(D, lua_Number, LUAC_NUM);
282
19.8k
}
283
284
285
/*
286
** dump Lua function as precompiled chunk
287
*/
288
int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
289
19.8k
               int strip) {
290
19.8k
  DumpState D;
291
19.8k
  D.h = luaH_new(L);  /* aux. table to keep strings already dumped */
292
19.8k
  sethvalue2s(L, L->top.p, D.h);  /* anchor it */
293
19.8k
  L->top.p++;
294
19.8k
  D.L = L;
295
19.8k
  D.writer = w;
296
19.8k
  D.offset = 0;
297
19.8k
  D.data = data;
298
19.8k
  D.strip = strip;
299
19.8k
  D.status = 0;
300
19.8k
  D.nstr = 0;
301
19.8k
  dumpHeader(&D);
302
19.8k
  dumpByte(&D, f->sizeupvalues);
303
19.8k
  dumpFunction(&D, f);
304
  dumpBlock(&D, NULL, 0);  /* signal end of dump */
305
19.8k
  return D.status;
306
19.8k
}
307