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
#![allow(dead_code)]
use std::cell::RefCell;
use nom::{
multi::count,
number::complete::{be_u16, le_u8},
};
use nom_leb128::{leb128_u32, leb128_u64, leb128_usize};
use super::*;
const FLAG_IS_BIG_ENDIAN: u8 = 0b00000001;
const FLAG_IS_STRIPPED: u8 = 0b00000010;
const FLAG_HAS_FFI: u8 = 0b00000100;
const BCDUMP_KGC_CHILD: u8 = 0;
const BCDUMP_KGC_TAB: u8 = 1;
const BCDUMP_KGC_I64: u8 = 2;
const BCDUMP_KGC_U64: u8 = 3;
const BCDUMP_KGC_COMPLEX: u8 = 4;
const BCDUMP_KGC_STR: u8 = 5;
const BCDUMP_KTAB_NIL: u8 = 0;
const BCDUMP_KTAB_FALSE: u8 = 1;
const BCDUMP_KTAB_TRUE: u8 = 2;
const BCDUMP_KTAB_INT: u8 = 3;
const BCDUMP_KTAB_NUM: u8 = 4;
const BCDUMP_KTAB_STR: u8 = 5;
fn uleb128_33(mut input: &[u8]) -> IResult<&[u8], u32, ErrorTree<&[u8]>> {
let v;
(input, v) = le_u8(input)?;
let mut v = v as u32 >> 1; if v >= 0x40 {
let mut sh = -1i32;
v &= 0x3f;
let mut p = le_u8(input)?.1;
loop {
sh += 7;
v |= (p as u32 & 0x7f) << (sh as u32);
(input, p) = le_u8(input)?;
if p < 0x80 {
break;
} }
}
Ok((input, v))
}
pub fn lj_header(input: &[u8]) -> IResult<&[u8], LuaHeader, ErrorTree<&[u8]>> {
let (rest, (_, result)) = tuple((
tag(b"\x1bLJ"),
alt((
map(tuple((tag(b"\x01"), be_u8)), |(_, flags)| LuaHeader {
lua_version: LUAJ1,
format_version: 0,
big_endian: flags & FLAG_IS_BIG_ENDIAN != 0,
int_size: 4,
size_t_size: 4,
instruction_size: 4,
number_size: 4,
number_integral: false,
stripped: flags & FLAG_IS_STRIPPED != 0,
has_ffi: flags & FLAG_HAS_FFI != 0,
})
.context("luajit1"),
map(tuple((tag(b"\x02"), be_u8)), |(_, flags)| LuaHeader {
lua_version: LUAJ2,
format_version: 0,
big_endian: flags & FLAG_IS_BIG_ENDIAN != 0,
int_size: 4,
size_t_size: 4,
instruction_size: 4,
number_size: 4,
number_integral: false,
stripped: flags & FLAG_IS_STRIPPED != 0,
has_ffi: flags & FLAG_HAS_FFI != 0,
})
.context("luajit2"),
)),
))(input)?;
Ok((rest, result))
}
fn lj_upval(input: &[u8]) -> IResult<&[u8], UpVal, ErrorTree<&[u8]>> {
map(be_u16, |_| UpVal::default())(input)
}
fn lj_complex_constant<'a, 'h>(
stack: &'h RefCell<Vec<LuaChunk>>,
protos: &'h RefCell<Vec<LuaChunk>>,
) -> impl Parser<&'a [u8], LuaConstant, ErrorTree<&'a [u8]>> + 'h {
move |input| {
let (input, ty) = leb128_u64(input)?;
Ok(match ty as u8 {
BCDUMP_KGC_I64 => map(
tuple((nom_leb128::leb128_u32, nom_leb128::leb128_u32)),
|(lo, hi)| LuaConstant::Number(LuaNumber::Integer(lo as i64 | ((hi as i64) << 32))),
)(input)?,
BCDUMP_KGC_U64 => map(
tuple((nom_leb128::leb128_u32, nom_leb128::leb128_u32)),
|(lo, hi)| {
LuaConstant::Number(LuaNumber::Integer(
(lo as u64 | ((hi as u64) << 32)) as i64,
))
},
)(input)?,
BCDUMP_KGC_TAB => lj_tab(input)?,
BCDUMP_KGC_CHILD => match stack.borrow_mut().pop() {
Some(proto) => {
let result = LuaConstant::Proto(protos.borrow().len());
protos.borrow_mut().push(proto);
(input, result)
}
None => context("pop proto", fail).parse(input)?,
},
_ if ty >= BCDUMP_KGC_STR as u64 => {
let len = ty - BCDUMP_KGC_STR as u64;
let (input, s) = take(len as usize)(input)?;
(input, LuaConstant::String(s.to_vec()))
}
_ => unreachable!("BCDUMP_KGC: {ty}"),
})
}
}
fn lj_tab(input: &[u8]) -> IResult<&[u8], LuaConstant, ErrorTree<&[u8]>> {
let (input, (narray, nhash)) = tuple((leb128_u32, leb128_u32))(input)?;
let (input, (arr, hash)) = tuple((
count(lj_tabk, narray as _),
count(tuple((lj_tabk, lj_tabk)), nhash as _),
))(input)?;
Ok((
input,
LuaConstant::Table {
array: arr.into_boxed_slice(),
hash: hash.into_boxed_slice(),
},
))
}
fn lj_tabk(input: &[u8]) -> IResult<&[u8], LuaConstant, ErrorTree<&[u8]>> {
let (input, ty) = leb128_usize(input)?;
Ok(match ty as u8 {
BCDUMP_KTAB_NIL => (input, LuaConstant::Null),
BCDUMP_KTAB_FALSE => (input, LuaConstant::Bool(false)),
BCDUMP_KTAB_TRUE => (input, LuaConstant::Bool(true)),
BCDUMP_KTAB_INT => map(leb128_u32, |n| {
LuaConstant::Number(LuaNumber::Integer(n as _))
})(input)?,
BCDUMP_KTAB_NUM => map(complete::le_f64, |n| {
LuaConstant::Number(LuaNumber::Float(n as _))
})(input)?,
_ if ty >= BCDUMP_KTAB_STR as usize => {
let len = ty - BCDUMP_KTAB_STR as usize;
let (input, s) = take(len)(input)?;
(input, LuaConstant::String(s.to_vec()))
}
_ => unreachable!("BCDUMP_KTAB: {ty}"),
})
}
fn lj_num_constant(input: &[u8]) -> IResult<&[u8], LuaConstant, ErrorTree<&[u8]>> {
let isnum = be_u8(input)?.1 & 1 != 0;
let (input, lo) = uleb128_33(input)?;
if isnum {
map(leb128_u32, |hi| {
LuaConstant::Number(LuaNumber::Integer((hi as i64) << 32 | lo as i64))
})(input)
} else {
Ok((input, LuaConstant::Number(LuaNumber::Integer(lo as _))))
}
}
fn lj_proto<'a, 'h>(
header: &'h LuaHeader,
stack: &'h RefCell<Vec<LuaChunk>>,
) -> impl Parser<&'a [u8], Option<LuaChunk>, ErrorTree<&'a [u8]>> + 'h {
move |input| {
let (input, size) = leb128_u32.parse(input)?;
if size == 0 {
return Ok((input, None));
}
let (
mut input,
(
flags,
num_params,
framesize,
num_upvalues,
complex_constants_count,
numeric_constants_count,
instructions_count,
),
) = tuple((
be_u8, be_u8, be_u8, be_u8, leb128_u32, leb128_u32, leb128_u32,
))(input)?;
let mut line_defined = 0;
let mut numline = 0;
let mut debuginfo_size = 0;
if !header.stripped {
(input, (debuginfo_size, line_defined, numline)) =
tuple((leb128_u64, leb128_u64, leb128_u64))(input)?;
}
let last_line_defined = line_defined + numline;
let instructions;
let upvalue_infos;
let mut constants;
let num_constants;
let protos = RefCell::new(vec![]);
(
input,
(instructions, upvalue_infos, constants, num_constants),
) = tuple((
count(complete::u32(header.endian()), instructions_count as usize)
.context("count instruction"),
count(lj_upval, num_upvalues as usize).context("count upvals"),
count(
lj_complex_constant(stack, &protos),
complex_constants_count as usize,
)
.context("count complex_constant"),
count(lj_num_constant, numeric_constants_count as usize)
.context("count numeric_constants"),
))(input)?;
constants.extend(num_constants);
if debuginfo_size > 0 {
(input, _) = take(debuginfo_size as usize)(input)?;
}
Ok((
input,
Some(LuaChunk {
name: vec![],
num_upvalues,
num_params,
line_defined,
last_line_defined,
flags,
instructions,
upvalue_infos,
constants,
max_stack: framesize,
prototypes: protos.into_inner(),
..Default::default()
}),
))
}
}
pub fn lj_chunk<'h, 'a: 'h>(
header: &'h LuaHeader,
) -> impl Parser<&'a [u8], LuaChunk, ErrorTree<&'a [u8]>> + 'h {
move |mut input| {
let mut name = &b""[..];
if !header.stripped {
let namelen;
(input, namelen) = leb128_u32.parse(input)?;
(input, name) = take(namelen as usize)(input)?;
}
let protos = RefCell::new(vec![]);
while let (i, Some(proto)) = lj_proto(&header, &protos).parse(input)? {
protos.borrow_mut().push(proto);
input = i;
}
let mut protos = protos.into_inner();
Ok((
input,
if let Some(mut chunk) = protos.pop().filter(|_| protos.is_empty()) {
chunk.name = name.to_vec();
chunk
} else {
context("stack unbalanced", fail).parse(input)?.1
},
))
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ProtoFlags: u8 {
const HAS_CHILD = 0b00000001;
const IS_VARIADIC = 0b00000010;
const HAS_FFI = 0b00000100;
const JIT_DISABLED = 0b00001000;
const HAS_ILOOP = 0b0001000;
}
}