-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.lua
361 lines (311 loc) · 9.11 KB
/
parser.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
---------------------------------------------------------------------------------
-- Copyright (c) 2019 kelthuzadx<[email protected]>
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
---------------------------------------------------------------------------------
-- Binary bytecode file format
-- file:
-- 1b 4C 75 61 | Lua bytecode signature
-- [u8 version] | Version number (0x52 for Lua 5.2, etc)
-- [u8 impl] | Implementation (0 for reference impl)
-- [u8 endian] | Big-endian flag
-- [u8 intsize] | Size of integers (usually 4)
-- [u8 size_t] | Size of pointers
-- [u8 instsize] | Size of instructions (always 4)
-- [u8 numsize] | Size of Lua numbers (usually 8)
-- [u8 use_int] | Use integers instead of floats (usually for embedded)
-- 19 93 0D 0A 1A 0A | Lua magic (used to detect presence of EOL conversion)
-- [func main]
-- string:
-- [size_t size]
-- ... data
-- 00
-- func:
-- [int line_start] | debug info
-- [int line_end] | debug info
-- [u8 nparams]
-- [u8 varargflags]
-- [u8 nregisters]
-- [int ninstructions]
-- ... instructions:
-- [instsize instruction]
-- [int nconsts]
-- ... consts:
-- [u8 type]
-- type 0: | nil
-- type 1: | bool
-- [u8 value]
-- type 3: | number
-- [numsize value]
-- type 4: | string
-- [string value]
-- [int nprimitives]
-- ... primitives:
-- [func primitive]
-- [int nupvals]
-- ... upvals:
-- [u8 stack]
-- [u8 register]
-- [string source] | debug info
-- [int nlines]
-- ... lines:
-- [int line]
-- [int nlocals]
-- ... locals:
-- [string name] | debug info
-- [int startpc]
-- [int endpc]
-- [int nupvalnames]
-- ... upvalnames:
-- [string name] | debug info
require("util")
parser = {}
function parser.parse_bytecode(chunk)
local idx = 1
local previdx, len
local function read_byte()
previdx = idx
idx = idx + 1
return string.byte(chunk, previdx)
end
local function read_buf(size, notreverse)
previdx = idx
idx = idx + size
local b = string.sub(chunk, idx - size, idx - 1)
if util.config.endianness == 1 or notreverse then
return b
else
return string.reverse(b)
end
end
-- magic number
len = string.len(util.config.SIGNATURE)
if string.sub(chunk, 1, len) ~= util.config.SIGNATURE then
error("invalid lua bytecode file magic number")
end
idx = idx + len
-- version
if read_byte() ~= util.config.VERSION then
error("invlaid version")
end
-- format
if read_byte() ~= util.config.FORMAT then
error("invalid format")
end
if read_buf(string.len(util.config.LUAC_DATA), true)~= util.config.LUAC_DATA then
error("luac_data incorrect")
end
if read_byte() ~= util.config.size_int then
error("invalid size_int value")
end
if read_byte() ~= util.config.size_size_t then
error("invalid size_t")
end
if read_byte() ~= util.config.size_instruction then
error("invalid instruction")
end
if read_byte() ~= util.config.size_lua_Integer then
error("invalid lua integer")
end
if read_byte() ~= util.config.size_lua_Number then
error("invalid lua number")
end
-- endianness
read_buf(8)
-- float format
read_buf(8)
-- global closure nupvalues
read_byte()
local function read_function(funcname, level)
local func = {
stat={},
source53 = {
val=nil,
len=nil,
islngstr=nil,
},
line_defined=nil,
last_line_defined=nil,
num_params = nil,
is_vararg = nil,
max_stack = nil,
args = {},
code_size = nil,
code = {},
const_list_size = nil,
const ={},
upvalue_size = nil,
upvalue = {},
proto_size = nil,
proto = {},
line_size = nil,
line = {},
localvar_size = nil,
localvar = {},
}
local function read_int()
local x = read_buf(util.config.size_int)
if not x then
error("could not load integer")
else
local sum = 0
for i = util.config.size_int, 1, -1 do
sum = sum * 256 + string.byte(x, i)
end
if string.byte(x, util.config.size_int) > 127 then
sum = sum - math.ldexp(1, 8 * util.config.size_int)
end
if sum < 0 then error("bad integer") end
return sum
end
end
local function read_size()
local x = read_buf(util.config.size_size_t)
if not x then
return
else
local sum = 0
for i = util.config.size_size_t, 1, -1 do
sum = sum * 256 + string.byte(x, i)
end
return sum
end
end
local function read_integer()
local x = read_buf(util.config.size_lua_Integer)
if not x then
error("could not load lua_Integer")
else
local convert_func = util.convert_from[util.config.integer_type]
if not convert_func then
error("could not find conversion function for lua_Integer")
end
return convert_func(x)
end
end
local function read_num()
local x = read_buf(util.config.size_lua_Number)
if not x then
error("could not load lua_Number")
else
local convert_func = util.convert_from[util.config.number_type]
if not convert_func then
error("could not find conversion function for lua_Number")
end
return convert_func(x)
end
end
local function read_string()
local len = read_byte()
local islngstr = nil
if not len then
error("could not load String")
return
end
if len == 255 then
len = read_size()
islngstr = true
end
if len == 0 then
return nil, len, islngstr
end
if len == 1 then
return "", len, islngstr
end
local s = string.sub(chunk, idx, idx + len - 2)
idx = idx + len - 1
return s, len, islngstr
end
local function read_string53()
local str = {}
str.val, str.len, str.islngstr = read_string()
return str
end
local function get_const_val(t)
if t == util.config.LUA_TNIL then
return nil
elseif t == util.config.LUA_TBOOLEAN then
if read_byte() == 0 then return false else return true end
elseif t == util.config.LUA_TNUMFLT then
return read_num()
elseif t == util.config.LUA_TNUMINT then
return read_integer()
elseif t == util.config.LUA_TSHRSTR or t == util.config.LUA_TLNGSTR then
return read_string53().val
else
error("bad constant type "..t.." at "..previdx)
end
end
-- source file
func.source53 = read_string53()
if func.source == nil and level == 1 then func.source = funcname end
-- line where the function was defined
func.line_defined = read_int()
func.last_line_defined = read_int()
-- parameters and varargs
func.num_params = read_byte()
func.is_vararg = read_byte()
func.max_stack = read_byte()
-- code
func.code_size = read_int()
for i = 1, func.code_size do
local val = read_buf(util.config.size_instruction)
assert(#val == 4 and type(val)=="string","expect 32 bits bytecode consumed")
val = (string.byte(val,1) << 0) |
(string.byte(val,2) << 8) |
(string.byte(val,3) << 16) |
(string.byte(val,4) << 24)
assert(type(val)=="number","require number but got"..type(val))
func.code[i] = val
end
-- constant
func.const_list_size = read_int()
for i = 0, func.const_list_size-1 do
local t = read_byte()
func.const[i] = get_const_val(t)
end
-- upvalue
func.upvalue_size = read_int()
for i = 0, func.upvalue_size-1 do
func.upvalue[i] = {instack = read_byte(), index = read_byte(),name53 =nil}
end
-- prototype
func.proto_size = read_int()
for i = 0, func.proto_size-1 do
func.proto[i] = read_function(func.source53.val, level + 1)
end
-- line
func.line_size = read_int()
for i=0, func.line_size-1 do
func.line[i] = read_int()
end
-- local
func.size_localvar = read_int()
for i = 0, func.size_localvar-1 do
func.localvar[i] = {varname=read_string53(),start_pc = read_int(), end_pc =read_int()}
end
-- upvalue name
assert(read_int() == func.upvalue_size,"mismatch upvalue size and upvalue name size")
for i = 0, func.upvalue_size-1 do
func.upvalue[i].name53 = read_string53()
end
return func
end
local func = read_function("chunk", 1)
if (#chunk+1) ~= idx then error("should eof") end
return func
end
return parser