Coverage Report

Created: 2025-10-13 06:32

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