Coverage Report

Created: 2026-01-09 06:47

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
8.99M
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
43
44
41.1k
#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
9.11M
static void dumpBlock (DumpState *D, const void *b, size_t size) {
53
9.11M
  if (D->status == 0) {  /* do not write anything after an error */
54
9.11M
    lua_unlock(D->L);
55
9.11M
    D->status = (*D->writer)(D->L, b, size, D->data);
56
9.11M
    lua_lock(D->L);
57
9.11M
    D->offset += size;
58
9.11M
  }
59
9.11M
}
60
61
62
/*
63
** Dump enough zeros to ensure that current position is a multiple of
64
** 'align'.
65
*/
66
152k
static void dumpAlign (DumpState *D, unsigned align) {
67
152k
  unsigned padding = align - cast_uint(D->offset % align);
68
152k
  if (padding < align) {  /* padding == align means no padding */
69
53.5k
    static lua_Integer paddingContent = 0;
70
53.5k
    lua_assert(align <= sizeof(lua_Integer));
71
53.5k
    dumpBlock(D, &paddingContent, padding);
72
53.5k
  }
73
152k
  lua_assert(D->offset % align == 0);
74
152k
}
75
76
77
4.37M
#define dumpVar(D,x)    dumpVector(D,&x,1)
78
79
80
2.66M
static void dumpByte (DumpState *D, int y) {
81
2.66M
  lu_byte x = (lu_byte)y;
82
2.66M
  dumpVar(D, x);
83
2.66M
}
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
4.96M
#define DIBS    ((l_numbits(lua_Unsigned) + 6) / 7)
91
92
/*
93
** Dumps an unsigned integer using the MSB Varint encoding
94
*/
95
4.14M
static void dumpVarint (DumpState *D, lua_Unsigned x) {
96
4.14M
  lu_byte buff[DIBS];
97
4.14M
  unsigned n = 1;
98
4.14M
  buff[DIBS - 1] = x & 0x7f;  /* fill least-significant byte */
99
4.96M
  while ((x >>= 7) != 0)  /* fill other bytes in reverse order */
100
4.14M
    buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
101
4.14M
  dumpVector(D, buff + DIBS - n, n);
102
4.14M
}
103
104
105
181k
static void dumpSize (DumpState *D, size_t sz) {
106
181k
  dumpVarint(D, cast(lua_Unsigned, sz));
107
181k
}
108
109
110
2.44M
static void dumpInt (DumpState *D, int x) {
111
2.44M
  lua_assert(x >= 0);
112
2.44M
  dumpVarint(D, cast_uint(x));
113
2.44M
}
114
115
116
1.62M
static void dumpNumber (DumpState *D, lua_Number x) {
117
1.62M
  dumpVar(D, x);
118
1.62M
}
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
39.0k
static void dumpInteger (DumpState *D, lua_Integer x) {
128
39.0k
  lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x)
129
39.0k
                             : (2u * ~l_castS2U(x)) + 1;
130
39.0k
  dumpVarint(D, cx);
131
39.0k
}
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
921k
static void dumpString (DumpState *D, TString *ts) {
144
921k
  if (ts == NULL) {
145
9.78k
    dumpVarint(D, 0);  /* will "reuse" NULL */
146
9.78k
    dumpVarint(D, 0);  /* special index for NULL */
147
9.78k
  }
148
912k
  else {
149
912k
    TValue idx;
150
912k
    int tag = luaH_getstr(D->h, ts, &idx);
151
912k
    if (!tagisempty(tag)) {  /* string already saved? */
152
731k
      dumpVarint(D, 0);  /* reuse a saved string */
153
731k
      dumpVarint(D, l_castS2U(ivalue(&idx)));  /* index of saved string */
154
731k
    }
155
181k
    else {  /* must write and save the string */
156
181k
      TValue key, value;  /* to save the string in the hash */
157
181k
      size_t size;
158
181k
      const char *s = getlstr(ts, size);
159
181k
      dumpSize(D, size + 1);
160
181k
      dumpVector(D, s, size + 1);  /* include ending '\0' */
161
181k
      D->nstr++;  /* one more saved string */
162
181k
      setsvalue(D->L, &key, ts);  /* the string is the key */
163
181k
      setivalue(&value, l_castU2S(D->nstr));  /* its index is the value */
164
181k
      luaH_set(D->L, D->h, &key, &value);  /* h[ts] = nstr */
165
      /* integer value does not need barrier */
166
181k
    }
167
912k
  }
168
921k
}
169
170
171
145k
static void dumpCode (DumpState *D, const Proto *f) {
172
145k
  dumpInt(D, f->sizecode);
173
145k
  dumpAlign(D, sizeof(f->code[0]));
174
145k
  lua_assert(f->code != NULL);
175
145k
  dumpVector(D, f->code, cast_uint(f->sizecode));
176
145k
}
177
178
179
static void dumpFunction (DumpState *D, const Proto *f);
180
181
145k
static void dumpConstants (DumpState *D, const Proto *f) {
182
145k
  int i;
183
145k
  int n = f->sizek;
184
145k
  dumpInt(D, n);
185
2.04M
  for (i = 0; i < n; i++) {
186
1.89M
    const TValue *o = &f->k[i];
187
1.89M
    int tt = ttypetag(o);
188
1.89M
    dumpByte(D, tt);
189
1.89M
    switch (tt) {
190
1.62M
      case LUA_VNUMFLT:
191
1.62M
        dumpNumber(D, fltvalue(o));
192
1.62M
        break;
193
39.0k
      case LUA_VNUMINT:
194
39.0k
        dumpInteger(D, ivalue(o));
195
39.0k
        break;
196
188k
      case LUA_VSHRSTR:
197
228k
      case LUA_VLNGSTR:
198
228k
        dumpString(D, tsvalue(o));
199
228k
        break;
200
4.40k
      default:
201
4.40k
        lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
202
1.89M
    }
203
1.89M
  }
204
145k
}
205
206
207
145k
static void dumpProtos (DumpState *D, const Proto *f) {
208
145k
  int i;
209
145k
  int n = f->sizep;
210
145k
  dumpInt(D, n);
211
269k
  for (i = 0; i < n; i++)
212
124k
    dumpFunction(D, f->p[i]);
213
145k
}
214
215
216
145k
static void dumpUpvalues (DumpState *D, const Proto *f) {
217
145k
  int i, n = f->sizeupvalues;
218
145k
  dumpInt(D, n);
219
208k
  for (i = 0; i < n; i++) {
220
63.3k
    dumpByte(D, f->upvalues[i].instack);
221
63.3k
    dumpByte(D, f->upvalues[i].idx);
222
63.3k
    dumpByte(D, f->upvalues[i].kind);
223
63.3k
  }
224
145k
}
225
226
227
145k
static void dumpDebug (DumpState *D, const Proto *f) {
228
145k
  int i, n;
229
145k
  n = (D->strip) ? 0 : f->sizelineinfo;
230
145k
  dumpInt(D, n);
231
145k
  if (f->lineinfo != NULL)
232
144k
    dumpVector(D, f->lineinfo, cast_uint(n));
233
145k
  n = (D->strip) ? 0 : f->sizeabslineinfo;
234
145k
  dumpInt(D, n);
235
145k
  if (n > 0) {
236
    /* 'abslineinfo' is an array of structures of int's */
237
6.89k
    dumpAlign(D, sizeof(int));
238
6.89k
    dumpVector(D, f->abslineinfo, cast_uint(n));
239
6.89k
  }
240
145k
  n = (D->strip) ? 0 : f->sizelocvars;
241
145k
  dumpInt(D, n);
242
640k
  for (i = 0; i < n; i++) {
243
495k
    dumpString(D, f->locvars[i].varname);
244
495k
    dumpInt(D, f->locvars[i].startpc);
245
495k
    dumpInt(D, f->locvars[i].endpc);
246
495k
  }
247
145k
  n = (D->strip) ? 0 : f->sizeupvalues;
248
145k
  dumpInt(D, n);
249
198k
  for (i = 0; i < n; i++)
250
52.9k
    dumpString(D, f->upvalues[i].name);
251
145k
}
252
253
254
145k
static void dumpFunction (DumpState *D, const Proto *f) {
255
145k
  dumpInt(D, f->linedefined);
256
145k
  dumpInt(D, f->lastlinedefined);
257
145k
  dumpByte(D, f->numparams);
258
145k
  dumpByte(D, f->flag);
259
145k
  dumpByte(D, f->maxstacksize);
260
145k
  dumpCode(D, f);
261
145k
  dumpConstants(D, f);
262
145k
  dumpUpvalues(D, f);
263
145k
  dumpProtos(D, f);
264
145k
  dumpString(D, D->strip ? NULL : f->source);
265
145k
  dumpDebug(D, f);
266
145k
}
267
268
269
#define dumpNumInfo(D, tvar, value)  \
270
82.2k
  { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
271
272
273
20.5k
static void dumpHeader (DumpState *D) {
274
20.5k
  dumpLiteral(D, LUA_SIGNATURE);
275
20.5k
  dumpByte(D, LUAC_VERSION);
276
20.5k
  dumpByte(D, LUAC_FORMAT);
277
20.5k
  dumpLiteral(D, LUAC_DATA);
278
20.5k
  dumpNumInfo(D, int, LUAC_INT);
279
20.5k
  dumpNumInfo(D, Instruction, LUAC_INST);
280
20.5k
  dumpNumInfo(D, lua_Integer, LUAC_INT);
281
20.5k
  dumpNumInfo(D, lua_Number, LUAC_NUM);
282
20.5k
}
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
20.5k
               int strip) {
290
20.5k
  DumpState D;
291
20.5k
  D.h = luaH_new(L);  /* aux. table to keep strings already dumped */
292
20.5k
  sethvalue2s(L, L->top.p, D.h);  /* anchor it */
293
20.5k
  L->top.p++;
294
20.5k
  D.L = L;
295
20.5k
  D.writer = w;
296
20.5k
  D.offset = 0;
297
20.5k
  D.data = data;
298
20.5k
  D.strip = strip;
299
20.5k
  D.status = 0;
300
20.5k
  D.nstr = 0;
301
20.5k
  dumpHeader(&D);
302
20.5k
  dumpByte(&D, f->sizeupvalues);
303
20.5k
  dumpFunction(&D, f);
304
  dumpBlock(&D, NULL, 0);  /* signal end of dump */
305
20.5k
  return D.status;
306
20.5k
}
307