Coverage Report

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