Coverage Report

Created: 2025-08-25 07:03

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