Coverage Report

Created: 2023-08-27 06:20

/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
242k
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
37
38
3.41k
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
39
40
41
246k
static void dumpBlock (DumpState *D, const void *b, size_t size) {
42
246k
  if (D->status == 0 && size > 0) {
43
244k
    lua_unlock(D->L);
44
244k
    D->status = (*D->writer)(D->L, b, size, D->data);
45
244k
    lua_lock(D->L);
46
244k
  }
47
246k
}
48
49
50
74.4k
#define dumpVar(D,x)    dumpVector(D,&x,1)
51
52
53
70.9k
static void dumpByte (DumpState *D, int y) {
54
70.9k
  lu_byte x = (lu_byte)y;
55
70.9k
  dumpVar(D, x);
56
70.9k
}
57
58
59
/*
60
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
61
** rounds up the division.)
62
*/
63
260k
#define DIBS    ((sizeof(size_t) * CHAR_BIT + 6) / 7)
64
65
122k
static void dumpSize (DumpState *D, size_t x) {
66
122k
  lu_byte buff[DIBS];
67
122k
  int n = 0;
68
138k
  do {
69
138k
    buff[DIBS - (++n)] = x & 0x7f;  /* fill buffer in reverse order */
70
138k
    x >>= 7;
71
138k
  } while (x != 0);
72
122k
  buff[DIBS - 1] |= 0x80;  /* mark last byte */
73
122k
  dumpVector(D, buff + DIBS - n, n);
74
122k
}
75
76
77
83.6k
static void dumpInt (DumpState *D, int x) {
78
83.6k
  dumpSize(D, x);
79
83.6k
}
80
81
82
1.80k
static void dumpNumber (DumpState *D, lua_Number x) {
83
1.80k
  dumpVar(D, x);
84
1.80k
}
85
86
87
1.70k
static void dumpInteger (DumpState *D, lua_Integer x) {
88
1.70k
  dumpVar(D, x);
89
1.70k
}
90
91
92
38.6k
static void dumpString (DumpState *D, const TString *s) {
93
38.6k
  if (s == NULL)
94
5.38k
    dumpSize(D, 0);
95
33.2k
  else {
96
33.2k
    size_t size = tsslen(s);
97
33.2k
    const char *str = getstr(s);
98
33.2k
    dumpSize(D, size + 1);
99
33.2k
    dumpVector(D, str, size);
100
33.2k
  }
101
38.6k
}
102
103
104
6.48k
static void dumpCode (DumpState *D, const Proto *f) {
105
6.48k
  dumpInt(D, f->sizecode);
106
6.48k
  dumpVector(D, f->code, f->sizecode);
107
6.48k
}
108
109
110
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
111
112
6.48k
static void dumpConstants (DumpState *D, const Proto *f) {
113
6.48k
  int i;
114
6.48k
  int n = f->sizek;
115
6.48k
  dumpInt(D, n);
116
25.0k
  for (i = 0; i < n; i++) {
117
18.5k
    const TValue *o = &f->k[i];
118
18.5k
    int tt = ttypetag(o);
119
18.5k
    dumpByte(D, tt);
120
18.5k
    switch (tt) {
121
94
      case LUA_VNUMFLT:
122
94
        dumpNumber(D, fltvalue(o));
123
0
        break;
124
0
      case LUA_VNUMINT:
125
0
        dumpInteger(D, ivalue(o));
126
0
        break;
127
18.1k
      case LUA_VSHRSTR:
128
18.1k
      case LUA_VLNGSTR:
129
18.1k
        dumpString(D, tsvalue(o));
130
0
        break;
131
245
      default:
132
245
        lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
133
18.5k
    }
134
18.5k
  }
135
6.48k
}
136
137
138
6.48k
static void dumpProtos (DumpState *D, const Proto *f) {
139
6.48k
  int i;
140
6.48k
  int n = f->sizep;
141
6.48k
  dumpInt(D, n);
142
11.2k
  for (i = 0; i < n; i++)
143
4.77k
    dumpFunction(D, f->p[i], f->source);
144
6.48k
}
145
146
147
6.48k
static void dumpUpvalues (DumpState *D, const Proto *f) {
148
6.48k
  int i, n = f->sizeupvalues;
149
6.48k
  dumpInt(D, n);
150
14.0k
  for (i = 0; i < n; i++) {
151
7.56k
    dumpByte(D, f->upvalues[i].instack);
152
7.56k
    dumpByte(D, f->upvalues[i].idx);
153
7.56k
    dumpByte(D, f->upvalues[i].kind);
154
7.56k
  }
155
6.48k
}
156
157
158
6.48k
static void dumpDebug (DumpState *D, const Proto *f) {
159
6.48k
  int i, n;
160
6.48k
  n = (D->strip) ? 0 : f->sizelineinfo;
161
6.48k
  dumpInt(D, n);
162
6.48k
  dumpVector(D, f->lineinfo, n);
163
6.48k
  n = (D->strip) ? 0 : f->sizeabslineinfo;
164
6.48k
  dumpInt(D, n);
165
7.46k
  for (i = 0; i < n; i++) {
166
979
    dumpInt(D, f->abslineinfo[i].pc);
167
979
    dumpInt(D, f->abslineinfo[i].line);
168
979
  }
169
6.48k
  n = (D->strip) ? 0 : f->sizelocvars;
170
6.48k
  dumpInt(D, n);
171
14.9k
  for (i = 0; i < n; i++) {
172
8.43k
    dumpString(D, f->locvars[i].varname);
173
8.43k
    dumpInt(D, f->locvars[i].startpc);
174
8.43k
    dumpInt(D, f->locvars[i].endpc);
175
8.43k
  }
176
6.48k
  n = (D->strip) ? 0 : f->sizeupvalues;
177
6.48k
  dumpInt(D, n);
178
12.0k
  for (i = 0; i < n; i++)
179
5.51k
    dumpString(D, f->upvalues[i].name);
180
6.48k
}
181
182
183
6.48k
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
184
6.48k
  if (D->strip || f->source == psource)
185
5.38k
    dumpString(D, NULL);  /* no debug info or same source as its parent */
186
1.10k
  else
187
1.10k
    dumpString(D, f->source);
188
6.48k
  dumpInt(D, f->linedefined);
189
6.48k
  dumpInt(D, f->lastlinedefined);
190
6.48k
  dumpByte(D, f->numparams);
191
6.48k
  dumpByte(D, f->is_vararg);
192
6.48k
  dumpByte(D, f->maxstacksize);
193
6.48k
  dumpCode(D, f);
194
6.48k
  dumpConstants(D, f);
195
6.48k
  dumpUpvalues(D, f);
196
6.48k
  dumpProtos(D, f);
197
6.48k
  dumpDebug(D, f);
198
6.48k
}
199
200
201
1.70k
static void dumpHeader (DumpState *D) {
202
1.70k
  dumpLiteral(D, LUA_SIGNATURE);
203
1.70k
  dumpByte(D, LUAC_VERSION);
204
1.70k
  dumpByte(D, LUAC_FORMAT);
205
1.70k
  dumpLiteral(D, LUAC_DATA);
206
1.70k
  dumpByte(D, sizeof(Instruction));
207
1.70k
  dumpByte(D, sizeof(lua_Integer));
208
1.70k
  dumpByte(D, sizeof(lua_Number));
209
1.70k
  dumpInteger(D, LUAC_INT);
210
1.70k
  dumpNumber(D, LUAC_NUM);
211
1.70k
}
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
1.70k
              int strip) {
219
1.70k
  DumpState D;
220
1.70k
  D.L = L;
221
1.70k
  D.writer = w;
222
1.70k
  D.data = data;
223
1.70k
  D.strip = strip;
224
1.70k
  D.status = 0;
225
1.70k
  dumpHeader(&D);
226
1.70k
  dumpByte(&D, f->sizeupvalues);
227
1.70k
  dumpFunction(&D, f, NULL);
228
1.70k
  return D.status;
229
1.70k
}
230