-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytecode_parser.lua
More file actions
255 lines (223 loc) · 6.21 KB
/
bytecode_parser.lua
File metadata and controls
255 lines (223 loc) · 6.21 KB
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
-- hacked up parser from LBI
local lua_opcode_types = {
"ABC", "ABx", "ABC", "ABC",
"ABC", "ABx", "ABC", "ABx",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "AsBx", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "AsBx",
"AsBx", "ABC", "ABC", "ABC",
"ABx", "ABC",
}
--- Extract bits from an integer
--@author: Stravant
local function get_bits(input, n, n2)
if n2 then
local total = 0
local digitn = 0
for i = n, n2 do
total = total + 2^digitn*get_bits(input, i)
digitn = digitn + 1
end
return total
else
local pn = 2^(n-1)
return (input % (pn + pn) >= pn) and 1 or 0
end
end
local function decode_bytecode(bytecode)
local index = 1
local big_endian = false
local int_size;
local size_t;
-- Actual binary decoding functions. Dependant on the bytecode.
local get_int, get_size_t;
-- Binary decoding helper functions
local get_int8, get_int32, get_int64, get_float64, get_string;
do
function get_int8()
local a = bytecode:byte(index, index);
index = index + 1
return a
end
function get_int32()
local a, b, c, d = bytecode:byte(index, index + 3);
index = index + 4;
return d*16777216 + c*65536 + b*256 + a
end
function get_int64()
local a = get_int32();
local b = get_int32();
return b*4294967296 + a;
end
function get_float64()
local a = get_int32()
local b = get_int32()
return (-2*get_bits(b, 32)+1)*(2^(get_bits(b, 21, 31)-1023))*
((get_bits(b, 1, 20)*(2^32) + a)/(2^52)+1)
end
function get_string(len)
local str;
if len then
str = bytecode:sub(index, index + len - 1);
index = index + len;
else
len = get_size_t();
if len == 0 then return; end
str = bytecode:sub(index, index + len - 1);
index = index + len;
end
return str;
end
end
local function decode_chunk()
local chunk;
local instructions = {};
local constants = {};
local prototypes = {};
local debug = {
lines = {};
};
chunk = {
code = instructions;
k = constants;
p = prototypes;
--debug = debug;
lineinfo = {};
upvalues = {};
locvars = {};
};
local num;
chunk.source = get_string();-- Function name
chunk.lineDefined = get_int(); -- First line
chunk.lastlinedefined = get_int(); -- Last line
if chunk.name then chunk.name = chunk.name:sub(1, -2); end
chunk.nups = get_int8();
chunk.numparams = get_int8();
chunk.is_vararg = get_int8();
chunk.maxstacksize = get_int8();
-- TODO: realign lists to 1
-- Decode instructions
do
num = get_int();
chunk.sizecode = num;
for i = 1, num do
local instruction = {
-- opcode = opcode number;
-- type = [ABC, ABx, AsBx]
-- A, B, C, Bx, or sBx depending on type
};
local data = get_int32();
local opcode = get_bits(data, 1, 6);
local type = lua_opcode_types[opcode + 1];
if instructions[i - 2] ~= nil then
local last_op = instructions[i - 2]
if last_op.OP == 34 then -- SETLIST
if last_op.C == 0 then
instruction.IS_SETLIST_C_VALUE = true
instruction.RAW_DATA = data
end
end
end
instruction.OP = opcode;
--instruction.type = type;
instruction.A = get_bits(data, 7, 14);
if type == "ABC" then
instruction.B = get_bits(data, 24, 32);
instruction.C = get_bits(data, 15, 23);
elseif type == "ABx" or type == "AsBx" then
instruction.Bx = get_bits(data, 15, 32);
--elseif type == "AsBx" then
--instruction.sBx = get_bits(data, 15, 32) - 131071;
end
instructions[i - 1] = instruction;
end
end
-- Decode constants
do
num = get_int();
chunk.sizek = num;
for i = 1, num do
local constant = {
-- type = constant type;
-- data = constant data;
};
local type = get_int8();
--constant.type = type;
if type == 1 then
constant.value = (get_int8() ~= 0);
elseif type == 3 then
constant.value = get_float64();
elseif type == 4 then
constant.value = get_string():sub(1, -2);
end
constants[i-1] = constant;
end
end
-- Decode Prototypes
do
num = get_int();
chunk.sizep = num;
for i = 1, num do
prototypes[i-1] = decode_chunk();
end
end
-- Decode debug info
-- Not all of which is used yet.
do
-- line numbers
local data = debug.lines
num = get_int();
chunk.sizelineinfo = num;
for i = 1, num do
chunk.lineinfo[i - 1] = get_int32();
end
-- locals
num = get_int();
chunk.sizelocvars = num;
for i = 1, num do
local str = get_string():sub(1, -2); -- local name
local startpc = get_int32(); -- local start PC
local endpc = get_int32(); -- local end PC
chunk.locvars[i - 1] = { varname = str, startpc = startpc, endpc = endpc }
end
-- upvalues
num = get_int();
chunk.sizeupvalues = num;
for i = 1, num do
chunk.upvalues[i - 1] = get_string(); -- upvalue name
end
end
return chunk;
end
-- Verify bytecode header
do
assert(get_string(4) == "\27Lua", "Lua bytecode expected.");
assert(get_int8() == 0x51, "Only Lua 5.1 is supported.");
get_int8(); -- Oficial bytecode
big_endian = (get_int8() == 0);
int_size = get_int8();
size_t = get_int8();
if int_size == 4 then
get_int = get_int32;
elseif int_size == 8 then
get_int = get_int64;
else
-- TODO: refactor errors into table
error("Unsupported bytecode target platform");
end
if size_t == 4 then
get_size_t = get_int32;
elseif size_t == 8 then
get_size_t = get_int64;
else
error("Unsupported bytecode target platform");
end
assert(get_string(3) == "\4\8\0",
"Unsupported bytecode target platform");
end
return decode_chunk();
end
return { decode_chunk = decode_bytecode }