interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lundump.c

418 lines · 11057 bytes Raw Blame History
1/*
2** $Id: lundump.c $
3** load precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#define lundump_c
8#define LUA_CORE
9
10#include "lprefix.h"
11
12
13#include <limits.h>
14#include <string.h>
15
16#include "lua.h"
17
18#include "ldebug.h"
19#include "ldo.h"
20#include "lfunc.h"
21#include "lmem.h"
22#include "lobject.h"
23#include "lstring.h"
24#include "ltable.h"
25#include "lundump.h"
26#include "lzio.h"
27
28
29#if !defined(luai_verifycode)
30#define luai_verifycode(L,f) /* empty */
31#endif
32
33
34typedef struct {
35 lua_State *L;
36 ZIO *Z;
37 const char *name;
38 Table *h; /* list for string reuse */
39 size_t offset; /* current position relative to beginning of dump */
40 lua_Unsigned nstr; /* number of strings in the list */
41 lu_byte fixed; /* dump is fixed in memory */
42} LoadState;
43
44
45static l_noret error (LoadState *S, const char *why) {
46 luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
47 luaD_throw(S->L, LUA_ERRSYNTAX);
48}
49
50
51/*
52** All high-level loads go through loadVector; you can change it to
53** adapt to the endianness of the input
54*/
55#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
56
57static void loadBlock (LoadState *S, void *b, size_t size) {
58 if (luaZ_read(S->Z, b, size) != 0)
59 error(S, "truncated chunk");
60 S->offset += size;
61}
62
63
64static void loadAlign (LoadState *S, unsigned align) {
65 unsigned padding = align - cast_uint(S->offset % align);
66 if (padding < align) { /* (padding == align) means no padding */
67 lua_Integer paddingContent;
68 loadBlock(S, &paddingContent, padding);
69 lua_assert(S->offset % align == 0);
70 }
71}
72
73
74#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
75
76static const void *getaddr_ (LoadState *S, size_t size) {
77 const void *block = luaZ_getaddr(S->Z, size);
78 S->offset += size;
79 if (block == NULL)
80 error(S, "truncated fixed buffer");
81 return block;
82}
83
84
85#define loadVar(S,x) loadVector(S,&x,1)
86
87
88static lu_byte loadByte (LoadState *S) {
89 int b = zgetc(S->Z);
90 if (b == EOZ)
91 error(S, "truncated chunk");
92 S->offset++;
93 return cast_byte(b);
94}
95
96
97static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) {
98 lua_Unsigned x = 0;
99 lua_Unsigned limit_high = limit >> 7;
100 int limit_low = limit & 0x7f;
101 int b;
102 do {
103 b = loadByte(S);
104 if (x >= limit_high && (x > limit_high || (b & 0x7f) > limit_low))
105 error(S, "integer overflow");
106 x = (x << 7) | (b & 0x7f);
107 } while ((b & 0x80) != 0);
108 return x;
109}
110
111
112static int loadInt (LoadState *S) {
113 return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
114}
115
116
117
118static lua_Number loadNumber (LoadState *S) {
119 lua_Number x;
120 loadVar(S, x);
121 return x;
122}
123
124
125static lua_Integer loadInteger (LoadState *S) {
126 lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED);
127 /* decode unsigned to signed */
128 if ((cx & 1) != 0)
129 return l_castU2S(~(cx >> 1));
130 else
131 return l_castU2S(cx >> 1);
132}
133
134
135/*
136** Load a nullable string into slot 'sl' from prototype 'p'. 'size' is
137** either the string length + 1 or zero for a previously saved string.
138** The assignment to the slot and the barrier must be performed before
139** any possible GC activity, to anchor the string. (Both 'loadVector'
140** and 'luaH_setint' can call the GC.)
141*/
142static void loadString (LoadState *S, Proto *p, TString **sl) {
143 lua_State *L = S->L;
144 TString *ts;
145 TValue sv;
146 size_t size = cast_sizet(loadVarint(S, MAX_SIZE - sizeof(TString) + 1));
147 if (size == 0) { /* previously saved string? */
148 lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */
149 TValue stv;
150 if (idx == 0) { /* no string? */
151 lua_assert(*sl == NULL); /* must be prefilled */
152 return;
153 }
154 if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING)
155 error(S, "invalid string index");
156 *sl = ts = tsvalue(&stv); /* get its value */
157 luaC_objbarrier(L, p, ts);
158 return; /* do not save it again */
159 }
160 else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
161 char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */
162 loadVector(S, buff, size + 1); /* load string into buffer */
163 *sl = ts = luaS_newlstr(L, buff, size); /* create string */
164 luaC_objbarrier(L, p, ts);
165 }
166 else if (S->fixed) { /* for a fixed buffer, use a fixed string */
167 const char *s = getaddr(S, size + 1, char); /* get content address */
168 *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
169 luaC_objbarrier(L, p, ts);
170 }
171 else { /* create internal copy */
172 *sl = ts = luaS_createlngstrobj(L, size); /* create string */
173 luaC_objbarrier(L, p, ts);
174 loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */
175 }
176 /* add string to list of saved strings */
177 S->nstr++;
178 setsvalue(L, &sv, ts);
179 luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
180 luaC_objbarrierback(L, obj2gco(S->h), ts);
181}
182
183
184static void loadCode (LoadState *S, Proto *f) {
185 int n = loadInt(S);
186 loadAlign(S, sizeof(f->code[0]));
187 if (S->fixed) {
188 f->code = getaddr(S, n, Instruction);
189 f->sizecode = n;
190 }
191 else {
192 f->code = luaM_newvectorchecked(S->L, n, Instruction);
193 f->sizecode = n;
194 loadVector(S, f->code, n);
195 }
196}
197
198
199static void loadFunction(LoadState *S, Proto *f);
200
201
202static void loadConstants (LoadState *S, Proto *f) {
203 int i;
204 int n = loadInt(S);
205 f->k = luaM_newvectorchecked(S->L, n, TValue);
206 f->sizek = n;
207 for (i = 0; i < n; i++)
208 setnilvalue(&f->k[i]);
209 for (i = 0; i < n; i++) {
210 TValue *o = &f->k[i];
211 int t = loadByte(S);
212 switch (t) {
213 case LUA_VNIL:
214 setnilvalue(o);
215 break;
216 case LUA_VFALSE:
217 setbfvalue(o);
218 break;
219 case LUA_VTRUE:
220 setbtvalue(o);
221 break;
222 case LUA_VNUMFLT:
223 setfltvalue(o, loadNumber(S));
224 break;
225 case LUA_VNUMINT:
226 setivalue(o, loadInteger(S));
227 break;
228 case LUA_VSHRSTR:
229 case LUA_VLNGSTR: {
230 lua_assert(f->source == NULL);
231 loadString(S, f, &f->source); /* use 'source' to anchor string */
232 if (f->source == NULL)
233 error(S, "bad format for constant string");
234 setsvalue2n(S->L, o, f->source); /* save it in the right place */
235 f->source = NULL;
236 break;
237 }
238 default: error(S, "invalid constant");
239 }
240 }
241}
242
243
244static void loadProtos (LoadState *S, Proto *f) {
245 int i;
246 int n = loadInt(S);
247 f->p = luaM_newvectorchecked(S->L, n, Proto *);
248 f->sizep = n;
249 for (i = 0; i < n; i++)
250 f->p[i] = NULL;
251 for (i = 0; i < n; i++) {
252 f->p[i] = luaF_newproto(S->L);
253 luaC_objbarrier(S->L, f, f->p[i]);
254 loadFunction(S, f->p[i]);
255 }
256}
257
258
259/*
260** Load the upvalues for a function. The names must be filled first,
261** because the filling of the other fields can raise read errors and
262** the creation of the error message can call an emergency collection;
263** in that case all prototypes must be consistent for the GC.
264*/
265static void loadUpvalues (LoadState *S, Proto *f) {
266 int i;
267 int n = loadInt(S);
268 f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
269 f->sizeupvalues = n;
270 for (i = 0; i < n; i++) /* make array valid for GC */
271 f->upvalues[i].name = NULL;
272 for (i = 0; i < n; i++) { /* following calls can raise errors */
273 f->upvalues[i].instack = loadByte(S);
274 f->upvalues[i].idx = loadByte(S);
275 f->upvalues[i].kind = loadByte(S);
276 }
277}
278
279
280static void loadDebug (LoadState *S, Proto *f) {
281 int i;
282 int n = loadInt(S);
283 if (S->fixed) {
284 f->lineinfo = getaddr(S, n, ls_byte);
285 f->sizelineinfo = n;
286 }
287 else {
288 f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
289 f->sizelineinfo = n;
290 loadVector(S, f->lineinfo, n);
291 }
292 n = loadInt(S);
293 if (n > 0) {
294 loadAlign(S, sizeof(int));
295 if (S->fixed) {
296 f->abslineinfo = getaddr(S, n, AbsLineInfo);
297 f->sizeabslineinfo = n;
298 }
299 else {
300 f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
301 f->sizeabslineinfo = n;
302 loadVector(S, f->abslineinfo, n);
303 }
304 }
305 n = loadInt(S);
306 f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
307 f->sizelocvars = n;
308 for (i = 0; i < n; i++)
309 f->locvars[i].varname = NULL;
310 for (i = 0; i < n; i++) {
311 loadString(S, f, &f->locvars[i].varname);
312 f->locvars[i].startpc = loadInt(S);
313 f->locvars[i].endpc = loadInt(S);
314 }
315 n = loadInt(S);
316 if (n != 0) /* does it have debug information? */
317 n = f->sizeupvalues; /* must be this many */
318 for (i = 0; i < n; i++)
319 loadString(S, f, &f->upvalues[i].name);
320}
321
322
323static void loadFunction (LoadState *S, Proto *f) {
324 f->linedefined = loadInt(S);
325 f->lastlinedefined = loadInt(S);
326 f->numparams = loadByte(S);
327 /* get only the meaningful flags */
328 f->flag = cast_byte(loadByte(S) & ~PF_FIXED);
329 if (S->fixed)
330 f->flag |= PF_FIXED; /* signal that code is fixed */
331 f->maxstacksize = loadByte(S);
332 loadCode(S, f);
333 loadConstants(S, f);
334 loadUpvalues(S, f);
335 loadProtos(S, f);
336 loadString(S, f, &f->source);
337 loadDebug(S, f);
338}
339
340
341static void checkliteral (LoadState *S, const char *s, const char *msg) {
342 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
343 size_t len = strlen(s);
344 loadVector(S, buff, len);
345 if (memcmp(s, buff, len) != 0)
346 error(S, msg);
347}
348
349
350static l_noret numerror (LoadState *S, const char *what, const char *tname) {
351 const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what);
352 error(S, msg);
353}
354
355
356static void checknumsize (LoadState *S, int size, const char *tname) {
357 if (size != loadByte(S))
358 numerror(S, "size", tname);
359}
360
361
362static void checknumformat (LoadState *S, int eq, const char *tname) {
363 if (!eq)
364 numerror(S, "format", tname);
365}
366
367
368#define checknum(S,tvar,value,tname) \
369 { tvar i; checknumsize(S, sizeof(i), tname); \
370 loadVar(S, i); \
371 checknumformat(S, i == value, tname); }
372
373
374static void checkHeader (LoadState *S) {
375 /* skip 1st char (already read and checked) */
376 checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
377 if (loadByte(S) != LUAC_VERSION)
378 error(S, "version mismatch");
379 if (loadByte(S) != LUAC_FORMAT)
380 error(S, "format mismatch");
381 checkliteral(S, LUAC_DATA, "corrupted chunk");
382 checknum(S, int, LUAC_INT, "int");
383 checknum(S, Instruction, LUAC_INST, "instruction");
384 checknum(S, lua_Integer, LUAC_INT, "Lua integer");
385 checknum(S, lua_Number, LUAC_NUM, "Lua number");
386}
387
388
389/*
390** Load precompiled chunk.
391*/
392LClosure *luaU_undump (lua_State *L, ZIO *Z, Table *anchor, const char *name,
393 int fixed) {
394 LoadState S;
395 LClosure *cl;
396 if (*name == '@' || *name == '=')
397 name = name + 1;
398 else if (*name == LUA_SIGNATURE[0])
399 name = "binary string";
400 S.name = name;
401 S.L = L;
402 S.Z = Z;
403 S.fixed = cast_byte(fixed);
404 S.offset = 1; /* fist byte was already read */
405 checkHeader(&S);
406 S.h = anchor;
407 S.nstr = 0;
408 cl = luaF_newLclosure(L, loadByte(&S));
409 luaD_anchorobj(L, anchor, obj2gco(cl));
410 cl->p = luaF_newproto(L);
411 luaC_objbarrier(L, cl, cl->p);
412 loadFunction(&S, cl->p);
413 if (cl->nupvalues != cl->p->sizeupvalues)
414 error(&S, "corrupted chunk");
415 luai_verifycode(L, cl->p);
416 return cl;
417}
418