Coverage Report

Created: 2023-09-11 06:55

/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 "lobject.h"
19
#include "lstate.h"
20
#include "lundump.h"
21
22
23
typedef struct {
24
  lua_State *L;
25
  lua_Writer writer;
26
  void *data;
27
  int strip;
28
  int status;
29
} DumpState;
30
31
32
/*
33
** All high-level dumps go through dumpVector; you can change it to
34
** change the endianness of the result
35
*/
36
232k
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
37
38
1.31k
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
39
40
41
233k
static void dumpBlock (DumpState *D, const void *b, size_t size) {
42
233k
  if (D->status == 0 && size > 0) {
43
232k
    lua_unlock(D->L);
44
232k
    D->status = (*D->writer)(D->L, b, size, D->data);
45
232k
    lua_lock(D->L);
46
232k
  }
47
233k
}
48
49
50
61.8k
#define dumpVar(D,x)    dumpVector(D,&x,1)
51
52
53
60.4k
static void dumpByte (DumpState *D, int y) {
54
60.4k
  lu_byte x = (lu_byte)y;
55
60.4k
  dumpVar(D, x);
56
60.4k
}
57
58
59
/*
60
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
61
** rounds up the division.)
62
*/
63
271k
#define DIBS    ((sizeof(size_t) * CHAR_BIT + 6) / 7)
64
65
126k
static void dumpSize (DumpState *D, size_t x) {
66
126k
  lu_byte buff[DIBS];
67
126k
  int n = 0;
68
144k
  do {
69
144k
    buff[DIBS - (++n)] = x & 0x7f;  /* fill buffer in reverse order */
70
144k
    x >>= 7;
71
144k
  } while (x != 0);
72
126k
  buff[DIBS - 1] |= 0x80;  /* mark last byte */
73
126k
  dumpVector(D, buff + DIBS - n, n);
74
126k
}
75
76
77
88.8k
static void dumpInt (DumpState *D, int x) {
78
88.8k
  dumpSize(D, x);
79
88.8k
}
80
81
82
713
static void dumpNumber (DumpState *D, lua_Number x) {
83
713
  dumpVar(D, x);
84
713
}
85
86
87
657
static void dumpInteger (DumpState *D, lua_Integer x) {
88
657
  dumpVar(D, x);
89
657
}
90
91
92
37.3k
static void dumpString (DumpState *D, const TString *s) {
93
37.3k
  if (s == NULL)
94
6.14k
    dumpSize(D, 0);
95
31.1k
  else {
96
31.1k
    size_t size = tsslen(s);
97
31.1k
    const char *str = getstr(s);
98
31.1k
    dumpSize(D, size + 1);
99
31.1k
    dumpVector(D, str, size);
100
31.1k
  }
101
37.3k
}
102
103
104
6.72k
static void dumpCode (DumpState *D, const Proto *f) {
105
6.72k
  dumpInt(D, f->sizecode);
106
6.72k
  dumpVector(D, f->code, f->sizecode);
107
6.72k
}
108
109
110
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
111
112
6.72k
static void dumpConstants (DumpState *D, const Proto *f) {
113
6.72k
  int i;
114
6.72k
  int n = f->sizek;
115
6.72k
  dumpInt(D, n);
116
21.5k
  for (i = 0; i < n; i++) {
117
14.8k
    const TValue *o = &f->k[i];
118
14.8k
    int tt = ttypetag(o);
119
14.8k
    dumpByte(D, tt);
120
14.8k
    switch (tt) {
121
56
      case LUA_VNUMFLT:
122
56
        dumpNumber(D, fltvalue(o));
123
56
        break;
124
0
      case LUA_VNUMINT:
125
0
        dumpInteger(D, ivalue(o));
126
0
        break;
127
14.7k
      case LUA_VSHRSTR:
128
14.7k
      case LUA_VLNGSTR:
129
14.7k
        dumpString(D, tsvalue(o));
130
14.7k
        break;
131
0
      default:
132
0
        lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
133
14.8k
    }
134
14.8k
  }
135
6.72k
}
136
137
138
6.72k
static void dumpProtos (DumpState *D, const Proto *f) {
139
6.72k
  int i;
140
6.72k
  int n = f->sizep;
141
6.72k
  dumpInt(D, n);
142
12.7k
  for (i = 0; i < n; i++)
143
6.06k
    dumpFunction(D, f->p[i], f->source);
144
6.72k
}
145
146
147
6.72k
static void dumpUpvalues (DumpState *D, const Proto *f) {
148
6.72k
  int i, n = f->sizeupvalues;
149
6.72k
  dumpInt(D, n);
150
13.8k
  for (i = 0; i < n; i++) {
151
7.17k
    dumpByte(D, f->upvalues[i].instack);
152
7.17k
    dumpByte(D, f->upvalues[i].idx);
153
7.17k
    dumpByte(D, f->upvalues[i].kind);
154
7.17k
  }
155
6.72k
}
156
157
158
6.72k
static void dumpDebug (DumpState *D, const Proto *f) {
159
6.72k
  int i, n;
160
6.72k
  n = (D->strip) ? 0 : f->sizelineinfo;
161
6.72k
  dumpInt(D, n);
162
6.72k
  dumpVector(D, f->lineinfo, n);
163
6.72k
  n = (D->strip) ? 0 : f->sizeabslineinfo;
164
6.72k
  dumpInt(D, n);
165
7.66k
  for (i = 0; i < n; i++) {
166
940
    dumpInt(D, f->abslineinfo[i].pc);
167
940
    dumpInt(D, f->abslineinfo[i].line);
168
940
  }
169
6.72k
  n = (D->strip) ? 0 : f->sizelocvars;
170
6.72k
  dumpInt(D, n);
171
16.5k
  for (i = 0; i < n; i++) {
172
9.86k
    dumpString(D, f->locvars[i].varname);
173
9.86k
    dumpInt(D, f->locvars[i].startpc);
174
9.86k
    dumpInt(D, f->locvars[i].endpc);
175
9.86k
  }
176
6.72k
  n = (D->strip) ? 0 : f->sizeupvalues;
177
6.72k
  dumpInt(D, n);
178
12.6k
  for (i = 0; i < n; i++)
179
5.95k
    dumpString(D, f->upvalues[i].name);
180
6.72k
}
181
182
183
6.72k
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
184
6.72k
  if (D->strip || f->source == psource)
185
6.14k
    dumpString(D, NULL);  /* no debug info or same source as its parent */
186
579
  else
187
579
    dumpString(D, f->source);
188
6.72k
  dumpInt(D, f->linedefined);
189
6.72k
  dumpInt(D, f->lastlinedefined);
190
6.72k
  dumpByte(D, f->numparams);
191
6.72k
  dumpByte(D, f->is_vararg);
192
6.72k
  dumpByte(D, f->maxstacksize);
193
6.72k
  dumpCode(D, f);
194
6.72k
  dumpConstants(D, f);
195
6.72k
  dumpUpvalues(D, f);
196
6.72k
  dumpProtos(D, f);
197
6.72k
  dumpDebug(D, f);
198
6.72k
}
199
200
201
657
static void dumpHeader (DumpState *D) {
202
657
  dumpLiteral(D, LUA_SIGNATURE);
203
657
  dumpByte(D, LUAC_VERSION);
204
657
  dumpByte(D, LUAC_FORMAT);
205
657
  dumpLiteral(D, LUAC_DATA);
206
657
  dumpByte(D, sizeof(Instruction));
207
657
  dumpByte(D, sizeof(lua_Integer));
208
657
  dumpByte(D, sizeof(lua_Number));
209
657
  dumpInteger(D, LUAC_INT);
210
657
  dumpNumber(D, LUAC_NUM);
211
657
}
212
213
214
/*
215
** dump Lua function as precompiled chunk
216
*/
217
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
218
657
              int strip) {
219
657
  DumpState D;
220
657
  D.L = L;
221
657
  D.writer = w;
222
657
  D.data = data;
223
657
  D.strip = strip;
224
657
  D.status = 0;
225
657
  dumpHeader(&D);
226
657
  dumpByte(&D, f->sizeupvalues);
227
657
  dumpFunction(&D, f, NULL);
228
657
  return D.status;
229
657
}
230