Coverage Report

Created: 2026-01-17 06:38

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