luaur-bytecode 0.1.3

Luau bytecode format and builder (Rust).
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use crate::macros::vconst::VCONST;
use crate::macros::vconstany::VCONSTANY;
use crate::macros::vjump::VJUMP;
use crate::macros::vreg::VREG;
use crate::macros::vregrange::VREGRANGE;
use crate::macros::vupval::VUPVAL;
use crate::records::bytecode_builder::BytecodeBuilder;
use luaur_common::enums::luau_capture_type::LuauCaptureType;
use luaur_common::enums::luau_opcode::LuauOpcode;
use luaur_common::functions::get_op_length::get_op_length;
use luaur_common::macros::luau_assert::LUAU_ASSERT;
use luaur_common::macros::luau_insn_a::LUAU_INSN_A;
use luaur_common::macros::luau_insn_aux_kv_16::LUAU_INSN_AUX_KV16;
use luaur_common::macros::luau_insn_b::LUAU_INSN_B;
use luaur_common::macros::luau_insn_c::LUAU_INSN_C;
use luaur_common::macros::luau_insn_d::LUAU_INSN_D;
use luaur_common::macros::luau_insn_e::LUAU_INSN_E;
use luaur_common::macros::luau_insn_op::LUAU_INSN_OP;

impl BytecodeBuilder {
    pub fn validate_instructions(&self) {
        let current_function = self.current_function;
        LUAU_ASSERT!(current_function != !0u32);

        let func = &self.functions[current_function as usize];

        // tag instruction offsets so that we can validate jumps
        let mut insnvalid = vec![0u8; self.insns.len()];

        let mut i: usize = 0;
        while i < self.insns.len() {
            let insn = self.insns[i];
            let op: LuauOpcode = unsafe { core::mem::transmute(LUAU_INSN_OP(insn) as u8) };

            insnvalid[i] = 1;

            let op_len = get_op_length(op) as usize;
            i += op_len;
            LUAU_ASSERT!(i <= self.insns.len());
        }

        let mut open_captures: Vec<u8> = Vec::new();

        // validate individual instructions
        i = 0;
        while i < self.insns.len() {
            let insn = self.insns[i];
            let op: LuauOpcode = unsafe { core::mem::transmute(LUAU_INSN_OP(insn) as u8) };

            match op {
                LuauOpcode::LOP_LOADNIL => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                }
                LuauOpcode::LOP_LOADB => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    let b_val = LUAU_INSN_B(insn) as u8;
                    LUAU_ASSERT!(b_val == 0 || b_val == 1);
                    VJUMP!(LUAU_INSN_C(insn) as i32, i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_LOADN => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                }
                LuauOpcode::LOP_LOADK => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONSTANY!(LUAU_INSN_D(insn) as usize, self.constants);
                }
                LuauOpcode::LOP_MOVE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                }
                LuauOpcode::LOP_GETGLOBAL | LuauOpcode::LOP_SETGLOBAL => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(self.insns[i + 1] as usize, String, self.constants);
                }
                LuauOpcode::LOP_GETUPVAL | LuauOpcode::LOP_SETUPVAL => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VUPVAL!(LUAU_INSN_B(insn) as u8, func);
                }
                LuauOpcode::LOP_CLOSEUPVALS => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    while !open_captures.is_empty()
                        && *open_captures.last().unwrap() >= LUAU_INSN_A(insn) as u8
                    {
                        open_captures.pop();
                    }
                }
                LuauOpcode::LOP_GETIMPORT => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(LUAU_INSN_D(insn) as usize, Import, self.constants);
                    let id = self.insns[i + 1];
                    LUAU_ASSERT!((id >> 30) != 0); // import chain with length 1-3
                    let mut j: u32 = 0;
                    while j < (id >> 30) {
                        VCONST!(
                            ((id >> (20 - 10 * j)) & 1023) as usize,
                            String,
                            self.constants
                        );
                        j += 1;
                    }
                }
                LuauOpcode::LOP_GETTABLE | LuauOpcode::LOP_SETTABLE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                }
                LuauOpcode::LOP_GETTABLEKS | LuauOpcode::LOP_SETTABLEKS => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONST!(self.insns[i + 1] as usize, String, self.constants);
                }
                LuauOpcode::LOP_GETTABLEN | LuauOpcode::LOP_SETTABLEN => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                }
                LuauOpcode::LOP_NEWCLOSURE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    let proto_idx = LUAU_INSN_D(insn) as usize;
                    LUAU_ASSERT!(proto_idx < self.protos.len());
                    let proto_val = self.protos[proto_idx];
                    LUAU_ASSERT!(proto_val < self.functions.len() as u32);
                    let numupvalues = self.functions[proto_val as usize].numupvalues as u32;

                    let mut j: u32 = 0;
                    while j < numupvalues {
                        LUAU_ASSERT!(i + 1 + (j as usize) < self.insns.len());
                        let cinsn = self.insns[i + 1 + j as usize];
                        LUAU_ASSERT!(LUAU_INSN_OP(cinsn) == LuauOpcode::LOP_CAPTURE as u32);
                        j += 1;
                    }
                }
                LuauOpcode::LOP_NAMECALL => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONST!(self.insns[i + 1] as usize, String, self.constants);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 2]) == LuauOpcode::LOP_CALLFB as u32
                            || LUAU_INSN_OP(self.insns[i + 2]) == LuauOpcode::LOP_CALL as u32
                    );
                }
                LuauOpcode::LOP_CALL | LuauOpcode::LOP_CALLFB => {
                    let nparams = (LUAU_INSN_B(insn) as i32) - 1;
                    let nresults = (LUAU_INSN_C(insn) as i32) - 1;
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREGRANGE!(LUAU_INSN_A(insn) as u8 + 1, nparams, func);
                    VREGRANGE!(LUAU_INSN_A(insn) as u8, nresults, func);
                }
                LuauOpcode::LOP_RETURN => {
                    let nresults = (LUAU_INSN_B(insn) as i32) - 1;
                    VREGRANGE!(LUAU_INSN_A(insn) as u8, nresults, func);
                }
                LuauOpcode::LOP_JUMP => {
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_JUMPIF | LuauOpcode::LOP_JUMPIFNOT => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_JUMPIFEQ
                | LuauOpcode::LOP_JUMPIFLE
                | LuauOpcode::LOP_JUMPIFLT
                | LuauOpcode::LOP_JUMPIFNOTEQ
                | LuauOpcode::LOP_JUMPIFNOTLE
                | LuauOpcode::LOP_JUMPIFNOTLT => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(self.insns[i + 1] as u8, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_JUMPXEQKNIL | LuauOpcode::LOP_JUMPXEQKB => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_JUMPXEQKN => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(
                        (self.insns[i + 1] & 0xffffff) as usize,
                        Number,
                        self.constants
                    );
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_JUMPXEQKS => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(
                        (self.insns[i + 1] & 0xffffff) as usize,
                        String,
                        self.constants
                    );
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_ADD
                | LuauOpcode::LOP_SUB
                | LuauOpcode::LOP_MUL
                | LuauOpcode::LOP_DIV
                | LuauOpcode::LOP_MOD
                | LuauOpcode::LOP_POW => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                }
                LuauOpcode::LOP_ADDK
                | LuauOpcode::LOP_SUBK
                | LuauOpcode::LOP_MULK
                | LuauOpcode::LOP_DIVK
                | LuauOpcode::LOP_MODK
                | LuauOpcode::LOP_POWK => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONST!(LUAU_INSN_C(insn) as usize, Number, self.constants);
                }
                LuauOpcode::LOP_SUBRK | LuauOpcode::LOP_DIVRK => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(LUAU_INSN_B(insn) as usize, Number, self.constants);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                }
                LuauOpcode::LOP_AND | LuauOpcode::LOP_OR => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                }
                LuauOpcode::LOP_ANDK | LuauOpcode::LOP_ORK => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONSTANY!(LUAU_INSN_C(insn) as usize, self.constants);
                }
                LuauOpcode::LOP_CONCAT => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                    LUAU_ASSERT!(LUAU_INSN_B(insn) <= LUAU_INSN_C(insn));
                }
                LuauOpcode::LOP_NOT | LuauOpcode::LOP_MINUS | LuauOpcode::LOP_LENGTH => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                }
                LuauOpcode::LOP_NEWTABLE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                }
                LuauOpcode::LOP_DUPTABLE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(LUAU_INSN_D(insn) as usize, Table, self.constants);
                }
                LuauOpcode::LOP_SETLIST => {
                    let count = (LUAU_INSN_C(insn) as i32) - 1;
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREGRANGE!(LUAU_INSN_B(insn) as u8, count, func);
                }
                LuauOpcode::LOP_FORNPREP | LuauOpcode::LOP_FORNLOOP => {
                    VREG!(LUAU_INSN_A(insn) as u8 + 2, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_FORGPREP => {
                    VREG!(LUAU_INSN_A(insn) as u8 + 2 + 1, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_FORGLOOP => {
                    VREG!(
                        LUAU_INSN_A(insn) as u8 + 2 + (self.insns[i + 1] as u8),
                        func
                    );
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!((self.insns[i + 1] as u8) >= 1);
                }
                LuauOpcode::LOP_FORGPREP_INEXT | LuauOpcode::LOP_FORGPREP_NEXT => {
                    VREG!(LUAU_INSN_A(insn) as u8 + 4, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_GETVARARGS => {
                    let nresults = (LUAU_INSN_B(insn) as i32) - 1;
                    VREGRANGE!(LUAU_INSN_A(insn) as u8, nresults, func);
                }
                LuauOpcode::LOP_DUPCLOSURE => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONST!(LUAU_INSN_D(insn) as usize, Closure, self.constants);
                    let proto = unsafe {
                        self.constants[LUAU_INSN_D(insn) as usize]
                            .value
                            .valueClosure
                    };
                    LUAU_ASSERT!(proto < self.functions.len() as u32);
                    let numupvalues = self.functions[proto as usize].numupvalues as u32;

                    let mut j: u32 = 0;
                    while j < numupvalues {
                        LUAU_ASSERT!(i + 1 + (j as usize) < self.insns.len());
                        let cinsn = self.insns[i + 1 + j as usize];
                        LUAU_ASSERT!(LUAU_INSN_OP(cinsn) == LuauOpcode::LOP_CAPTURE as u32);
                        let capture_type = LUAU_INSN_A(cinsn) as u8;
                        LUAU_ASSERT!(
                            capture_type == LuauCaptureType::LCT_VAL as u8
                                || capture_type == LuauCaptureType::LCT_UPVAL as u8
                        );
                        j += 1;
                    }
                }
                LuauOpcode::LOP_PREPVARARGS => {
                    LUAU_ASSERT!(LUAU_INSN_A(insn) == func.numparams as u32);
                    LUAU_ASSERT!(func.isvararg);
                }
                LuauOpcode::LOP_BREAK => {}
                LuauOpcode::LOP_JUMPBACK => {
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_LOADKX => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VCONSTANY!(self.insns[i + 1] as usize, self.constants);
                }
                LuauOpcode::LOP_JUMPX => {
                    VJUMP!(LUAU_INSN_E(insn), i, self.insns, insnvalid);
                }
                LuauOpcode::LOP_FASTCALL => {
                    VJUMP!(LUAU_INSN_C(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 1 + LUAU_INSN_C(insn) as usize])
                            == LuauOpcode::LOP_CALL as u32
                    );
                }
                LuauOpcode::LOP_FASTCALL1 => {
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VJUMP!(LUAU_INSN_C(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 1 + LUAU_INSN_C(insn) as usize])
                            == LuauOpcode::LOP_CALL as u32
                    );
                }
                LuauOpcode::LOP_FASTCALL2 => {
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VJUMP!(LUAU_INSN_C(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 1 + LUAU_INSN_C(insn) as usize])
                            == LuauOpcode::LOP_CALL as u32
                    );
                    VREG!(self.insns[i + 1] as u8, func);
                }
                LuauOpcode::LOP_FASTCALL2K => {
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VJUMP!(LUAU_INSN_C(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 1 + LUAU_INSN_C(insn) as usize])
                            == LuauOpcode::LOP_CALL as u32
                    );
                    VCONSTANY!(self.insns[i + 1] as usize, self.constants);
                }
                LuauOpcode::LOP_FASTCALL3 => {
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VJUMP!(LUAU_INSN_C(insn), i, self.insns, insnvalid);
                    LUAU_ASSERT!(
                        LUAU_INSN_OP(self.insns[i + 1 + LUAU_INSN_C(insn) as usize])
                            == LuauOpcode::LOP_CALL as u32
                    );
                    VREG!((self.insns[i + 1] & 0xff) as u8, func);
                    VREG!(((self.insns[i + 1] >> 8) & 0xff) as u8, func);
                }
                LuauOpcode::LOP_COVERAGE => {}
                LuauOpcode::LOP_CAPTURE => {
                    let capture_type = LUAU_INSN_A(insn) as u8;
                    if capture_type == LuauCaptureType::LCT_VAL as u8 {
                        VREG!(LUAU_INSN_B(insn) as u8, func);
                    } else if capture_type == LuauCaptureType::LCT_REF as u8 {
                        VREG!(LUAU_INSN_B(insn) as u8, func);
                        open_captures.push(LUAU_INSN_B(insn) as u8);
                    } else if capture_type == LuauCaptureType::LCT_UPVAL as u8 {
                        VUPVAL!(LUAU_INSN_B(insn) as u8, func);
                    } else {
                        LUAU_ASSERT!(false, "Unsupported capture type");
                    }
                }
                LuauOpcode::LOP_NEWCLASSMEMBER => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    LUAU_ASSERT!(LUAU_INSN_B(insn) == 0);
                    VREG!(LUAU_INSN_C(insn) as u8, func);
                    VCONST!(self.insns[i + 1] as usize, String, self.constants);
                }
                LuauOpcode::LOP_GETUDATAKS | LuauOpcode::LOP_SETUDATAKS => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONST!(
                        LUAU_INSN_AUX_KV16(self.insns[i + 1]) as usize,
                        String,
                        self.constants
                    );
                }
                LuauOpcode::LOP_NAMECALLUDATA => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VREG!(LUAU_INSN_B(insn) as u8, func);
                    VCONST!(
                        LUAU_INSN_AUX_KV16(self.insns[i + 1]) as usize,
                        String,
                        self.constants
                    );
                    LUAU_ASSERT!(LUAU_INSN_OP(self.insns[i + 2]) == LuauOpcode::LOP_CALL as u32);
                }
                LuauOpcode::LOP_CMPPROTO => {
                    VREG!(LUAU_INSN_A(insn) as u8, func);
                    VJUMP!(LUAU_INSN_D(insn), i, self.insns, insnvalid);
                }
                _ => {
                    LUAU_ASSERT!(false, "Unsupported opcode");
                }
            }

            let op_len = get_op_length(op) as usize;
            i += op_len;
            LUAU_ASSERT!(i <= self.insns.len());
        }

        // all CAPTURE REF instructions must have a CLOSEUPVALS instruction after them in the bytecode stream
        // this doesn't guarantee safety as it doesn't perform basic block based analysis, but if this fails
        // then the bytecode is definitely unsafe to run since the compiler won't generate backwards branches
        // except for loop edges
        LUAU_ASSERT!(open_captures.is_empty());
    }
}