Coverage Report

Created: 2023-09-15 06:13

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