diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-19 15:31:08 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-19 15:31:08 -0300 |
| commit | f53eabeed855081fa38e9af5cf7c977915f5213f (patch) | |
| tree | 1e116377bb3666a36f76220a22e9149549b31975 | |
| parent | 39bb3cf2422603d854ee12529cc3419dc735802a (diff) | |
| download | lua-f53eabeed855081fa38e9af5cf7c977915f5213f.tar.gz lua-f53eabeed855081fa38e9af5cf7c977915f5213f.tar.bz2 lua-f53eabeed855081fa38e9af5cf7c977915f5213f.zip | |
Small changes in the header of binary files
- LUAC_VERSION is equal to LUA_VERSION_NUM, and it is stored
as an int.
- 'sizeof(int)' and 'sizeof(size_t)' removed from the header, as
the binary format does not depend on these sizes. (It uses its
own serialization for unsigned integer values.)
| -rw-r--r-- | ldump.c | 4 | ||||
| -rw-r--r-- | lundump.c | 38 | ||||
| -rw-r--r-- | lundump.h | 3 | ||||
| -rw-r--r-- | testes/calls.lua | 20 |
4 files changed, 34 insertions, 31 deletions
| @@ -198,11 +198,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { | |||
| 198 | 198 | ||
| 199 | static void DumpHeader (DumpState *D) { | 199 | static void DumpHeader (DumpState *D) { |
| 200 | DumpLiteral(LUA_SIGNATURE, D); | 200 | DumpLiteral(LUA_SIGNATURE, D); |
| 201 | DumpByte(LUAC_VERSION, D); | 201 | DumpInt(LUAC_VERSION, D); |
| 202 | DumpByte(LUAC_FORMAT, D); | 202 | DumpByte(LUAC_FORMAT, D); |
| 203 | DumpLiteral(LUAC_DATA, D); | 203 | DumpLiteral(LUAC_DATA, D); |
| 204 | DumpByte(sizeof(int), D); | ||
| 205 | DumpByte(sizeof(size_t), D); | ||
| 206 | DumpByte(sizeof(Instruction), D); | 204 | DumpByte(sizeof(Instruction), D); |
| 207 | DumpByte(sizeof(lua_Integer), D); | 205 | DumpByte(sizeof(lua_Integer), D); |
| 208 | DumpByte(sizeof(lua_Number), D); | 206 | DumpByte(sizeof(lua_Number), D); |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include "lprefix.h" | 10 | #include "lprefix.h" |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | #include <limits.h> | ||
| 13 | #include <string.h> | 14 | #include <string.h> |
| 14 | 15 | ||
| 15 | #include "lua.h" | 16 | #include "lua.h" |
| @@ -37,7 +38,7 @@ typedef struct { | |||
| 37 | 38 | ||
| 38 | 39 | ||
| 39 | static l_noret error (LoadState *S, const char *why) { | 40 | static l_noret error (LoadState *S, const char *why) { |
| 40 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); | 41 | luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); |
| 41 | luaD_throw(S->L, LUA_ERRSYNTAX); | 42 | luaD_throw(S->L, LUA_ERRSYNTAX); |
| 42 | } | 43 | } |
| 43 | 44 | ||
| @@ -50,7 +51,7 @@ static l_noret error (LoadState *S, const char *why) { | |||
| 50 | 51 | ||
| 51 | static void LoadBlock (LoadState *S, void *b, size_t size) { | 52 | static void LoadBlock (LoadState *S, void *b, size_t size) { |
| 52 | if (luaZ_read(S->Z, b, size) != 0) | 53 | if (luaZ_read(S->Z, b, size) != 0) |
| 53 | error(S, "truncated"); | 54 | error(S, "truncated chunk"); |
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | 57 | ||
| @@ -60,24 +61,32 @@ static void LoadBlock (LoadState *S, void *b, size_t size) { | |||
| 60 | static lu_byte LoadByte (LoadState *S) { | 61 | static lu_byte LoadByte (LoadState *S) { |
| 61 | int b = zgetc(S->Z); | 62 | int b = zgetc(S->Z); |
| 62 | if (b == EOZ) | 63 | if (b == EOZ) |
| 63 | error(S, "truncated"); | 64 | error(S, "truncated chunk"); |
| 64 | return cast_byte(b); | 65 | return cast_byte(b); |
| 65 | } | 66 | } |
| 66 | 67 | ||
| 67 | 68 | ||
| 68 | static size_t LoadSize (LoadState *S) { | 69 | static size_t LoadUnsigned (LoadState *S, size_t limit) { |
| 69 | size_t x = 0; | 70 | size_t x = 0; |
| 70 | int b; | 71 | int b; |
| 72 | limit >>= 7; | ||
| 71 | do { | 73 | do { |
| 72 | b = LoadByte(S); | 74 | b = LoadByte(S); |
| 75 | if (x >= limit) | ||
| 76 | error(S, "integer overflow"); | ||
| 73 | x = (x << 7) | (b & 0x7f); | 77 | x = (x << 7) | (b & 0x7f); |
| 74 | } while ((b & 0x80) == 0); | 78 | } while ((b & 0x80) == 0); |
| 75 | return x; | 79 | return x; |
| 76 | } | 80 | } |
| 77 | 81 | ||
| 78 | 82 | ||
| 83 | static size_t LoadSize (LoadState *S) { | ||
| 84 | return LoadUnsigned(S, ~(size_t)0); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 79 | static int LoadInt (LoadState *S) { | 88 | static int LoadInt (LoadState *S) { |
| 80 | return cast_int(LoadSize(S)); | 89 | return cast_int(LoadUnsigned(S, INT_MAX)); |
| 81 | } | 90 | } |
| 82 | 91 | ||
| 83 | 92 | ||
| @@ -255,28 +264,27 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) { | |||
| 255 | 264 | ||
| 256 | static void fchecksize (LoadState *S, size_t size, const char *tname) { | 265 | static void fchecksize (LoadState *S, size_t size, const char *tname) { |
| 257 | if (LoadByte(S) != size) | 266 | if (LoadByte(S) != size) |
| 258 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); | 267 | error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); |
| 259 | } | 268 | } |
| 260 | 269 | ||
| 261 | 270 | ||
| 262 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) | 271 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) |
| 263 | 272 | ||
| 264 | static void checkHeader (LoadState *S) { | 273 | static void checkHeader (LoadState *S) { |
| 265 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ | 274 | /* 1st char already checked */ |
| 266 | if (LoadByte(S) != LUAC_VERSION) | 275 | checkliteral(S, LUA_SIGNATURE + 1, "not a binary chunk"); |
| 267 | error(S, "version mismatch in"); | 276 | if (LoadInt(S) != LUAC_VERSION) |
| 277 | error(S, "version mismatch"); | ||
| 268 | if (LoadByte(S) != LUAC_FORMAT) | 278 | if (LoadByte(S) != LUAC_FORMAT) |
| 269 | error(S, "format mismatch in"); | 279 | error(S, "format mismatch"); |
| 270 | checkliteral(S, LUAC_DATA, "corrupted"); | 280 | checkliteral(S, LUAC_DATA, "corrupted chunk"); |
| 271 | checksize(S, int); | ||
| 272 | checksize(S, size_t); | ||
| 273 | checksize(S, Instruction); | 281 | checksize(S, Instruction); |
| 274 | checksize(S, lua_Integer); | 282 | checksize(S, lua_Integer); |
| 275 | checksize(S, lua_Number); | 283 | checksize(S, lua_Number); |
| 276 | if (LoadInteger(S) != LUAC_INT) | 284 | if (LoadInteger(S) != LUAC_INT) |
| 277 | error(S, "endianness mismatch in"); | 285 | error(S, "integer format mismatch"); |
| 278 | if (LoadNumber(S) != LUAC_NUM) | 286 | if (LoadNumber(S) != LUAC_NUM) |
| 279 | error(S, "float format mismatch in"); | 287 | error(S, "float format mismatch"); |
| 280 | } | 288 | } |
| 281 | 289 | ||
| 282 | 290 | ||
| @@ -18,8 +18,7 @@ | |||
| 18 | #define LUAC_INT 0x5678 | 18 | #define LUAC_INT 0x5678 |
| 19 | #define LUAC_NUM cast_num(370.5) | 19 | #define LUAC_NUM cast_num(370.5) |
| 20 | 20 | ||
| 21 | #define MYINT(s) (s[0]-'0') | 21 | #define LUAC_VERSION LUA_VERSION_NUM |
| 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) | ||
| 23 | #define LUAC_FORMAT 0 /* this is the official format */ | 22 | #define LUAC_FORMAT 0 /* this is the official format */ |
| 24 | 23 | ||
| 25 | /* load one chunk; from lundump.c */ | 24 | /* load one chunk; from lundump.c */ |
diff --git a/testes/calls.lua b/testes/calls.lua index f5108a47..941493b1 100644 --- a/testes/calls.lua +++ b/testes/calls.lua | |||
| @@ -397,17 +397,15 @@ assert((function (a) return a end)() == nil) | |||
| 397 | 397 | ||
| 398 | print("testing binary chunks") | 398 | print("testing binary chunks") |
| 399 | do | 399 | do |
| 400 | local header = string.pack("c4BBc6BBBBBj", | 400 | local header = string.pack("c4BBBc6BBBj", |
| 401 | "\27Lua", -- signature | 401 | "\27Lua", -- signature |
| 402 | 5*16 + 4, -- version 5.4 | 402 | (504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504) |
| 403 | 0, -- format | 403 | 0, -- format |
| 404 | "\x19\x93\r\n\x1a\n", -- data | 404 | "\x19\x93\r\n\x1a\n", -- data |
| 405 | string.packsize("i"), -- sizeof(int) | 405 | 4, -- size of instruction |
| 406 | string.packsize("T"), -- sizeof(size_t) | 406 | string.packsize("j"), -- sizeof(lua integer) |
| 407 | 4, -- size of instruction | 407 | string.packsize("n"), -- sizeof(lua number) |
| 408 | string.packsize("j"), -- sizeof(lua integer) | 408 | 0x5678 -- LUAC_INT |
| 409 | string.packsize("n"), -- sizeof(lua number) | ||
| 410 | 0x5678 -- LUAC_INT | ||
| 411 | -- LUAC_NUM may not have a unique binary representation (padding...) | 409 | -- LUAC_NUM may not have a unique binary representation (padding...) |
| 412 | ) | 410 | ) |
| 413 | local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) | 411 | local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) |
