dellingr 0.4.0

An embeddable, pure-Rust Lua VM with precise instruction-cost accounting
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
use super::Bytecode;
use super::MAX_SYNTAX_DEPTH;
use crate::instr::{Builtin, Instr};

#[derive(Clone, Debug, PartialEq, Eq)]
enum Height {
    Known(u32),
    Dyn { floor: u32 },
}

impl Height {
    fn floor(&self) -> u32 {
        match self {
            Self::Known(height) | Self::Dyn { floor: height } => *height,
        }
    }

    fn known(&self) -> Option<u32> {
        match self {
            Self::Known(height) => Some(*height),
            Self::Dyn { .. } => None,
        }
    }

    fn add(&mut self, amount: u32, pc: usize) -> Result<(), BytecodeValidationError> {
        match self {
            Self::Known(height) => {
                *height = height
                    .checked_add(amount)
                    .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
            }
            Self::Dyn { floor } => {
                *floor = floor
                    .checked_add(amount)
                    .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
            }
        }
        Ok(())
    }

    fn remove(&mut self, amount: u32, pc: usize) -> Result<(), BytecodeValidationError> {
        if self.floor() < amount {
            return Err(err(Some(pc), "operand stack underflows"));
        }
        match self {
            Self::Known(height) => {
                *height = height
                    .checked_sub(amount)
                    .ok_or_else(|| err(Some(pc), "operand stack underflows"))?;
            }
            Self::Dyn { floor } => {
                *floor = floor
                    .checked_sub(amount)
                    .ok_or_else(|| err(Some(pc), "operand stack underflows"))?;
            }
        }
        Ok(())
    }

    fn adjust(
        &mut self,
        removed: u32,
        added: u32,
        pc: usize,
    ) -> Result<(), BytecodeValidationError> {
        self.remove(removed, pc)?;
        self.add(added, pc)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct AbsState {
    height: Height,
    call_bases: Vec<u32>,
    table_bases: Vec<u32>,
}

#[derive(Debug)]
pub(crate) struct BytecodeValidationError {
    #[cfg_attr(not(feature = "snapshot"), allow(dead_code))]
    pub(crate) instruction: Option<u32>,
    #[cfg_attr(not(feature = "snapshot"), allow(dead_code))]
    pub(crate) reason: String,
}

pub(crate) trait BytecodeView {
    fn code_len(&self) -> usize;
    fn raw_instruction(&self, pc: usize) -> u32;
    fn number_literals_len(&self) -> usize;
    fn string_literals(&self) -> &[Vec<u8>];
    fn table_templates(&self) -> &[Vec<u16>];
    fn global_cache_slots(&self) -> u8;
    fn field_cache_slots(&self) -> u8;
    fn set_field_cache_slots(&self) -> u8;
    fn num_params(&self) -> u8;
    fn num_locals(&self) -> u8;
    fn nested_len(&self) -> usize;
    fn upvalues_len(&self) -> usize;
    fn is_vararg(&self) -> bool;
    fn line_info_len(&self) -> usize;
}

impl BytecodeView for Bytecode {
    fn code_len(&self) -> usize {
        self.code.len()
    }
    fn raw_instruction(&self, pc: usize) -> u32 {
        self.code[pc].raw()
    }
    fn number_literals_len(&self) -> usize {
        self.number_literals.len()
    }
    fn string_literals(&self) -> &[Vec<u8>] {
        &self.string_literals
    }
    fn table_templates(&self) -> &[Vec<u16>] {
        &self.table_templates
    }
    fn global_cache_slots(&self) -> u8 {
        self.global_cache_slots
    }
    fn field_cache_slots(&self) -> u8 {
        self.field_cache_slots
    }
    fn set_field_cache_slots(&self) -> u8 {
        self.set_field_cache_slots
    }
    fn num_params(&self) -> u8 {
        self.num_params
    }
    fn num_locals(&self) -> u8 {
        self.num_locals
    }
    fn nested_len(&self) -> usize {
        self.nested.len()
    }
    fn upvalues_len(&self) -> usize {
        self.upvalues.len()
    }
    fn is_vararg(&self) -> bool {
        self.is_vararg
    }
    fn line_info_len(&self) -> usize {
        self.line_info.len()
    }
}

fn err(pc: Option<usize>, reason: impl Into<String>) -> BytecodeValidationError {
    BytecodeValidationError {
        instruction: pc.map(|pc| pc as u32),
        reason: reason.into(),
    }
}

fn check_index(
    pc: usize,
    value: u16,
    len: usize,
    what: &str,
) -> Result<(), BytecodeValidationError> {
    if (value as usize) < len {
        Ok(())
    } else {
        Err(err(Some(pc), format!("{what} index is out of range")))
    }
}

fn check_slots(
    pc: usize,
    start: u8,
    count: usize,
    slots: usize,
) -> Result<(), BytecodeValidationError> {
    if (start as usize)
        .checked_add(count)
        .is_some_and(|end| end <= slots)
    {
        Ok(())
    } else {
        Err(err(Some(pc), "frame slot range is out of range"))
    }
}

fn known_opcode(opcode: u8) -> bool {
    matches!(
        opcode,
        Instr::OP_NOP
            | Instr::OP_POP
            | Instr::OP_DUP
            | Instr::OP_SWAP
            | Instr::OP_NEW_TABLE
            | Instr::OP_GET_TABLE
            | Instr::OP_ADD
            | Instr::OP_SUBTRACT
            | Instr::OP_MULTIPLY
            | Instr::OP_DIVIDE
            | Instr::OP_POW
            | Instr::OP_MOD
            | Instr::OP_CONCAT
            | Instr::OP_LESS
            | Instr::OP_LESS_EQUAL
            | Instr::OP_GREATER
            | Instr::OP_GREATER_EQUAL
            | Instr::OP_EQUAL
            | Instr::OP_NOT_EQUAL
            | Instr::OP_NOT
            | Instr::OP_LENGTH
            | Instr::OP_NEGATE
            | Instr::OP_MARK_CALL_BASE
            | Instr::OP_PUSH_NIL
            | Instr::OP_NEW_TABLE_PRESIZED
            | Instr::OP_NEW_TABLE_TEMPLATE
            | Instr::OP_NEW_TABLE_TRACKED
            | Instr::OP_GET_GLOBAL
            | Instr::OP_SET_GLOBAL
            | Instr::OP_GET_LOCAL
            | Instr::OP_SET_LOCAL
            | Instr::OP_GET_UPVALUE
            | Instr::OP_SET_UPVALUE
            | Instr::OP_GET_FIELD
            | Instr::OP_INIT_INDEX
            | Instr::OP_PUSH_NUM
            | Instr::OP_PUSH_STRING
            | Instr::OP_TFOR_PREP
            | Instr::OP_RETURN
            | Instr::OP_CLOSE_UPVALUES
            | Instr::OP_CLOSURE
            | Instr::OP_VARARG
            | Instr::OP_SET_LIST
            | Instr::OP_SET_TABLE
            | Instr::OP_PUSH_BOOL
            | Instr::OP_GET_BUILTIN
            | Instr::OP_SET_BUILTIN
            | Instr::OP_SET_FIELD
            | Instr::OP_INIT_FIELD
            | Instr::OP_TFOR_CALL
            | Instr::OP_CALL
            | Instr::OP_INIT_FIELD_PINNED
            | Instr::OP_SET_FIELD_AT
            | Instr::OP_JUMP
            | Instr::OP_BRANCH_FALSE
            | Instr::OP_BRANCH_TRUE_KEEP
            | Instr::OP_BRANCH_FALSE_KEEP
            | Instr::OP_FOR_PREP
            | Instr::OP_FOR_LOOP
            | Instr::OP_TFOR_LOOP
    )
}

fn verify_stack_discipline(view: &impl BytecodeView) -> Result<(), BytecodeValidationError> {
    let code_len = view.code_len();
    let mut states = vec![None; code_len];
    states[0] = Some(AbsState {
        height: Height::Known(0),
        call_bases: Vec::new(),
        table_bases: Vec::new(),
    });
    let mut worklist = vec![0usize];

    while let Some(pc) = worklist.pop() {
        let state = match &states[pc] {
            Some(state) => state.clone(),
            None => return Err(err(Some(pc), "missing abstract stack state")),
        };
        let inst = Instr::from_raw(view.raw_instruction(pc));
        let op = inst.opcode();
        let a = inst.a();
        let b = inst.b();
        let mut next = state;

        let require_known = |height: &Height| {
            height.known().ok_or_else(|| {
                err(
                    Some(pc),
                    "control flow requires a known operand stack height",
                )
            })
        };
        let require_at_least = |height: &Height, amount: u32| {
            if height.floor() < amount {
                Err(err(Some(pc), "operand stack underflows"))
            } else {
                Ok(())
            }
        };
        let offset_requirement = |offset: u8, extra: u32| {
            u32::from(offset)
                .checked_add(extra)
                .ok_or_else(|| err(Some(pc), "operand stack height overflows"))
        };

        match op {
            Instr::OP_DUP => {
                require_at_least(&next.height, 1)?;
                next.height.add(1, pc)?;
            }
            Instr::OP_SWAP => require_at_least(&next.height, 2)?,
            Instr::OP_NEW_TABLE
            | Instr::OP_NEW_TABLE_PRESIZED
            | Instr::OP_NEW_TABLE_TEMPLATE
            | Instr::OP_PUSH_NIL
            | Instr::OP_PUSH_BOOL
            | Instr::OP_PUSH_NUM
            | Instr::OP_PUSH_STRING
            | Instr::OP_GET_GLOBAL
            | Instr::OP_GET_LOCAL
            | Instr::OP_GET_UPVALUE
            | Instr::OP_GET_BUILTIN
            | Instr::OP_CLOSURE => next.height.add(1, pc)?,
            // `GET_TABLE` pops the key and replaces the table with the result,
            // which is the same two-in one-out shape as a binary operator.
            Instr::OP_GET_TABLE
            | Instr::OP_ADD
            | Instr::OP_SUBTRACT
            | Instr::OP_MULTIPLY
            | Instr::OP_DIVIDE
            | Instr::OP_POW
            | Instr::OP_MOD
            | Instr::OP_LESS
            | Instr::OP_LESS_EQUAL
            | Instr::OP_GREATER
            | Instr::OP_GREATER_EQUAL
            | Instr::OP_EQUAL
            | Instr::OP_NOT_EQUAL => next.height.adjust(2, 1, pc)?,
            Instr::OP_CONCAT => next.height.adjust(u32::from(a), 1, pc)?,
            Instr::OP_NOT | Instr::OP_LENGTH | Instr::OP_NEGATE | Instr::OP_GET_FIELD => {
                require_at_least(&next.height, 1)?;
            }
            Instr::OP_MARK_CALL_BASE => {
                let height = require_known(&next.height)?;
                if height < 1 {
                    return Err(err(Some(pc), "call-base marker is below the active frame"));
                }
                if next.call_bases.len() >= MAX_SYNTAX_DEPTH as usize {
                    return Err(err(Some(pc), "call-base marker stack is too deep"));
                }
                next.call_bases.push(height - 1);
            }
            Instr::OP_NEW_TABLE_TRACKED => {
                let height = require_known(&next.height)?;
                if next.table_bases.len() >= MAX_SYNTAX_DEPTH as usize {
                    return Err(err(Some(pc), "table-constructor marker stack is too deep"));
                }
                next.table_bases.push(height);
                next.height.add(1, pc)?;
            }
            // A discard and a store share one operand effect: consume the top
            // value and push nothing. The store's destination is a local,
            // upvalue or global slot, none of which is on the operand stack.
            Instr::OP_POP
            | Instr::OP_SET_GLOBAL
            | Instr::OP_SET_LOCAL
            | Instr::OP_SET_UPVALUE
            | Instr::OP_SET_BUILTIN => next.height.remove(1, pc)?,
            Instr::OP_INIT_FIELD => {
                require_at_least(&next.height, offset_requirement(a, 2)?)?;
                next.height.remove(1, pc)?;
            }
            Instr::OP_INIT_FIELD_PINNED => {
                require_at_least(&next.height, 2)?;
                next.height.remove(1, pc)?;
            }
            Instr::OP_INIT_INDEX => {
                require_at_least(&next.height, offset_requirement(a, 3)?)?;
                next.height.remove(2, pc)?;
            }
            Instr::OP_SET_FIELD => {
                require_at_least(&next.height, 2)?;
                next.height.remove(2, pc)?;
            }
            Instr::OP_SET_FIELD_AT => {
                require_at_least(&next.height, offset_requirement(a, 2)?)?;
                next.height.remove(2, pc)?;
            }
            Instr::OP_SET_TABLE => {
                require_at_least(&next.height, offset_requirement(a, 3)?)?;
                next.height.remove(3, pc)?;
            }
            Instr::OP_SET_LIST if a == 0 => {
                let base = match next.table_bases.pop() {
                    Some(base) => base,
                    None => return Err(err(Some(pc), "dynamic set-list has no table marker")),
                };
                let minimum = base
                    .checked_add(1)
                    .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
                require_at_least(&next.height, minimum)?;
                next.height = Height::Known(minimum);
            }
            Instr::OP_SET_LIST => {
                let count = u32::from(a);
                require_at_least(
                    &next.height,
                    count
                        .checked_add(1)
                        .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?,
                )?;
                next.height.remove(count, pc)?;
            }
            Instr::OP_VARARG if a == u8::MAX => {
                let floor = next.height.floor();
                next.height = Height::Dyn { floor };
            }
            Instr::OP_VARARG => next.height.add(u32::from(a), pc)?,
            Instr::OP_CALL => {
                if a == u8::MAX {
                    let base = match next.call_bases.pop() {
                        Some(base) => base,
                        None => return Err(err(Some(pc), "dynamic call has no call-base marker")),
                    };
                    let minimum = base
                        .checked_add(1)
                        .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
                    require_at_least(&next.height, minimum)?;
                    next.height = if b == u8::MAX {
                        Height::Dyn { floor: base }
                    } else {
                        Height::Known(
                            base.checked_add(u32::from(b))
                                .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?,
                        )
                    };
                } else if b == u8::MAX {
                    let inputs = u32::from(a)
                        .checked_add(1)
                        .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
                    next.height.remove(inputs, pc)?;
                    let floor = next.height.floor();
                    next.height = Height::Dyn { floor };
                } else {
                    let inputs = u32::from(a)
                        .checked_add(1)
                        .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
                    next.height.adjust(inputs, u32::from(b), pc)?;
                }
            }
            Instr::OP_TFOR_PREP => next.height.remove(3, pc)?,
            // Both work entirely within the locals region: `TFOR_CALL` drives
            // the iterator through local slots and is internally balanced, and
            // `CLOSE_UPVALUES` only closes upvalues at or above a local slot.
            Instr::OP_NOP | Instr::OP_TFOR_CALL | Instr::OP_CLOSE_UPVALUES => {}
            Instr::OP_FOR_PREP => {
                require_known(&next.height)?;
                next.height.remove(3, pc)?;
            }
            Instr::OP_FOR_LOOP | Instr::OP_TFOR_LOOP | Instr::OP_JUMP => {
                require_known(&next.height)?;
            }
            Instr::OP_BRANCH_FALSE => {
                require_known(&next.height)?;
                next.height.remove(1, pc)?;
            }
            Instr::OP_BRANCH_TRUE_KEEP | Instr::OP_BRANCH_FALSE_KEEP => {
                require_known(&next.height)?;
                require_at_least(&next.height, 1)?;
            }
            Instr::OP_RETURN => {
                if !next.call_bases.is_empty() || !next.table_bases.is_empty() {
                    return Err(err(Some(pc), "return leaves a live stack marker"));
                }
                if a != u8::MAX {
                    let height = require_known(&next.height)?;
                    if height < u32::from(a) {
                        return Err(err(Some(pc), "return values exceed operand stack height"));
                    }
                }
            }
            _ => return Err(err(Some(pc), "missing operand stack effect")),
        }

        for base in next.call_bases.iter().chain(next.table_bases.iter()) {
            let minimum = base
                .checked_add(1)
                .ok_or_else(|| err(Some(pc), "operand stack height overflows"))?;
            if next.height.floor() < minimum {
                return Err(err(Some(pc), "operand stack drops below a live marker"));
            }
        }

        if op == Instr::OP_RETURN {
            continue;
        }
        let target = |inst: Instr| -> Result<usize, BytecodeValidationError> {
            let next_pc = pc
                .checked_add(1)
                .ok_or_else(|| err(Some(pc), "jump target is out of range"))?;
            if inst.sbx() >= 0 {
                next_pc
                    .checked_add(inst.sbx() as usize)
                    .ok_or_else(|| err(Some(pc), "jump target is out of range"))
            } else {
                next_pc
                    .checked_sub(inst.sbx().unsigned_abs() as usize)
                    .ok_or_else(|| err(Some(pc), "jump target is out of range"))
            }
        };
        let mut successors = Vec::with_capacity(2);
        match op {
            Instr::OP_JUMP => successors.push(target(inst)?),
            Instr::OP_BRANCH_FALSE
            | Instr::OP_BRANCH_TRUE_KEEP
            | Instr::OP_BRANCH_FALSE_KEEP
            | Instr::OP_FOR_PREP
            | Instr::OP_FOR_LOOP
            | Instr::OP_TFOR_LOOP => {
                successors.push(pc + 1);
                successors.push(target(inst)?);
            }
            _ => successors.push(pc + 1),
        }
        for successor in successors {
            match &states[successor] {
                Some(existing) if existing != &next => {
                    return Err(err(
                        Some(successor),
                        "operand stack state disagrees at control-flow join",
                    ));
                }
                Some(_) => {}
                None => {
                    states[successor] = Some(next.clone());
                    worklist.push(successor);
                }
            }
        }
    }
    Ok(())
}

pub(crate) fn validate_bytecode(view: &impl BytecodeView) -> Result<(), BytecodeValidationError> {
    let code_len = view.code_len();
    if code_len == 0 {
        return Err(err(None, "code is empty"));
    }
    if view.line_info_len() != code_len {
        return Err(err(None, "line info length does not match code length"));
    }
    for (name, len) in [
        ("table template", view.table_templates().len()),
        ("nested chunk", view.nested_len()),
        ("upvalue descriptor", view.upvalues_len()),
    ] {
        if len > 255 {
            return Err(err(None, format!("too many {name}s")));
        }
    }
    if view.number_literals_len() > usize::from(u16::MAX) + 1
        || view.string_literals().len() > usize::from(u16::MAX) + 1
    {
        return Err(err(None, "too many literals"));
    }
    if view
        .table_templates()
        .iter()
        .any(|template| template.len() > 255)
    {
        return Err(err(None, "table template is too large"));
    }
    if view.num_params() as usize + view.num_locals() as usize > 255 {
        return Err(err(None, "frame slot count exceeds 255"));
    }
    if !matches!(
        Instr::from_raw(view.raw_instruction(code_len - 1)).opcode(),
        Instr::OP_RETURN
    ) {
        return Err(err(Some(code_len - 1), "code does not end in return"));
    }
    for (template_idx, template) in view.table_templates().iter().enumerate() {
        for key in template {
            if (*key as usize) >= view.string_literals().len() {
                return Err(err(
                    None,
                    format!("table template {template_idx} has an invalid string key"),
                ));
            }
        }
    }
    let slots = view.num_params() as usize + view.num_locals() as usize;
    let mut global_slots = vec![None; view.string_literals().len()];
    let mut next_global_slot = 0usize;
    let mut next_field_slot = 0u8;
    let mut next_set_field_slot = 0usize;
    let mut next_tfor_cursor_slot = 0usize;
    for pc in 0..code_len {
        let inst = Instr::from_raw(view.raw_instruction(pc));
        let op = inst.opcode();
        if !known_opcode(op) {
            return Err(err(Some(pc), "unknown opcode"));
        }
        let a = inst.a();
        let b = inst.b();
        let c = inst.c();
        let bx = inst.bx();
        let reserved_zero = match op {
            // These forms use every operand byte. SET_GLOBAL joined the group
            // when its A byte became the biased cache index (0 = uncached);
            // TFOR_CALL joined when its C byte became the biased cursor slot.
            Instr::OP_GET_GLOBAL
            | Instr::OP_SET_GLOBAL
            | Instr::OP_TFOR_CALL
            | Instr::OP_GET_FIELD
            | Instr::OP_SET_FIELD
            | Instr::OP_SET_FIELD_AT
            | Instr::OP_INIT_FIELD
            | Instr::OP_INIT_FIELD_PINNED
            | Instr::OP_SET_LIST
            | Instr::OP_FOR_PREP
            | Instr::OP_FOR_LOOP
            | Instr::OP_TFOR_LOOP => true,
            // Literal loads put the pool id in Bx and reserve A; jumps use B+C
            // as a signed offset and likewise reserve A.
            Instr::OP_PUSH_NUM
            | Instr::OP_PUSH_STRING
            | Instr::OP_JUMP
            | Instr::OP_BRANCH_FALSE
            | Instr::OP_BRANCH_TRUE_KEEP
            | Instr::OP_BRANCH_FALSE_KEEP => a == 0,
            // CALL is the remaining two-byte form that reserves C.
            Instr::OP_CALL => c == 0,
            // All remaining known instructions are either operandless or use
            // only A, and therefore reserve B+C (and A for operandless ops).
            _ => {
                let a_is_reserved = matches!(
                    op,
                    Instr::OP_NOP
                        | Instr::OP_POP
                        | Instr::OP_DUP
                        | Instr::OP_SWAP
                        | Instr::OP_NEW_TABLE
                        | Instr::OP_GET_TABLE
                        | Instr::OP_ADD
                        | Instr::OP_SUBTRACT
                        | Instr::OP_MULTIPLY
                        | Instr::OP_DIVIDE
                        | Instr::OP_POW
                        | Instr::OP_MOD
                        | Instr::OP_LESS
                        | Instr::OP_LESS_EQUAL
                        | Instr::OP_GREATER
                        | Instr::OP_GREATER_EQUAL
                        | Instr::OP_EQUAL
                        | Instr::OP_NOT_EQUAL
                        | Instr::OP_NOT
                        | Instr::OP_LENGTH
                        | Instr::OP_NEGATE
                        | Instr::OP_PUSH_NIL
                );
                (!a_is_reserved || a == 0) && b == 0 && c == 0
            }
        };
        if !reserved_zero {
            return Err(err(
                Some(pc),
                format!(
                    "reserved operand bytes are non-zero (opcode {op}, raw {:08x})",
                    inst.raw()
                ),
            ));
        }
        match op {
            Instr::OP_PUSH_NUM => {
                check_index(pc, bx, view.number_literals_len(), "number literal")?;
            }
            Instr::OP_GET_GLOBAL | Instr::OP_SET_GLOBAL => {
                check_index(pc, bx, view.string_literals().len(), "string literal")?;
                if std::str::from_utf8(&view.string_literals()[bx as usize]).is_err() {
                    return Err(err(Some(pc), "global name operand is not UTF-8"));
                }
                // The sentinel means "deliberately uncached" and is valid
                // anywhere, not only once the slots are exhausted. The compiler
                // never emits it early, but a hand-built or deliberately
                // uncached snapshot is still semantically valid, and the
                // runtime handles it by taking the slow path.
                if op == Instr::OP_GET_GLOBAL && a != u8::MAX {
                    let expected = match global_slots[bx as usize] {
                        Some(slot) => slot,
                        None => {
                            let slot = next_global_slot;
                            if slot < u8::MAX as usize {
                                next_global_slot += 1;
                            }
                            global_slots[bx as usize] = Some(slot);
                            slot
                        }
                    };
                    if a as usize != expected {
                        return Err(err(
                            Some(pc),
                            "global cache slot does not match first-use order",
                        ));
                    }
                }
                if op == Instr::OP_SET_GLOBAL && a != 0 {
                    let expected = next_global_slot;
                    if expected >= u8::MAX as usize || a as usize - 1 != expected {
                        return Err(err(
                            Some(pc),
                            "global cache slot does not match first-use order",
                        ));
                    }
                    next_global_slot += 1;
                }
            }
            Instr::OP_GET_FIELD => {
                check_index(pc, bx, view.string_literals().len(), "string literal")?;
                // As above: the sentinel is accepted anywhere and excluded from
                // the sequential slot count.
                if a != u8::MAX {
                    if a != next_field_slot {
                        return Err(err(
                            Some(pc),
                            "field cache slot does not match instruction order",
                        ));
                    }
                    next_field_slot += 1;
                }
            }
            Instr::OP_SET_FIELD => {
                check_index(pc, bx, view.string_literals().len(), "string literal")?;
                if a != u8::MAX && a as usize != next_set_field_slot {
                    return Err(err(
                        Some(pc),
                        "set-field cache slot does not match instruction order",
                    ));
                }
                if a != u8::MAX {
                    next_set_field_slot += 1;
                }
            }
            // Every remaining opcode that names a string literal in Bx. Merged
            // into one arm so the bounds check cannot be forgotten for one of
            // them; forged bytecode reaches this path too.
            Instr::OP_PUSH_STRING
            | Instr::OP_INIT_FIELD_PINNED
            | Instr::OP_SET_FIELD_AT
            | Instr::OP_INIT_FIELD => {
                check_index(pc, bx, view.string_literals().len(), "string literal")?;
            }
            Instr::OP_NEW_TABLE_TEMPLATE => {
                check_index(
                    pc,
                    u16::from(a),
                    view.table_templates().len(),
                    "table template",
                )?;
            }
            Instr::OP_CLOSURE => check_index(pc, u16::from(a), view.nested_len(), "nested chunk")?,
            Instr::OP_GET_BUILTIN | Instr::OP_SET_BUILTIN if (a as usize) >= Builtin::COUNT => {
                return Err(err(Some(pc), "builtin slot is out of range"));
            }
            Instr::OP_GET_LOCAL | Instr::OP_SET_LOCAL => check_slots(pc, a, 1, slots)?,
            Instr::OP_GET_UPVALUE | Instr::OP_SET_UPVALUE => {
                check_index(pc, u16::from(a), view.upvalues_len(), "upvalue")?;
            }
            Instr::OP_FOR_PREP | Instr::OP_FOR_LOOP | Instr::OP_TFOR_LOOP => {
                check_slots(pc, a, 4, slots)?;
            }
            Instr::OP_TFOR_PREP => check_slots(pc, a, 3, slots)?,
            Instr::OP_TFOR_CALL => {
                if b == 0 {
                    return Err(err(Some(pc), "TFOR_CALL requires at least one result"));
                }
                check_slots(pc, a, 3 + b as usize, slots)?;
                if c != 0 {
                    if c as usize - 1 != next_tfor_cursor_slot {
                        return Err(err(
                            Some(pc),
                            "TFOR_CALL cursor slot does not match instruction order",
                        ));
                    }
                    next_tfor_cursor_slot += 1;
                }
            }
            Instr::OP_CLOSE_UPVALUES if (a as usize) > slots => {
                return Err(err(Some(pc), "close-upvalues level is out of range"));
            }
            Instr::OP_VARARG if !view.is_vararg() => {
                return Err(err(Some(pc), "vararg instruction in non-vararg function"));
            }
            Instr::OP_PUSH_BOOL if a > 1 => {
                return Err(err(Some(pc), "boolean operand is invalid"));
            }
            Instr::OP_CONCAT if a < 2 => {
                return Err(err(Some(pc), "concat requires at least two values"));
            }
            // Always 1: the marker is emitted after the callee is evaluated, so
            // the callee is the single value on top and the base is one slot
            // below it. Anything else is corrupt.
            Instr::OP_MARK_CALL_BASE if a != 1 => {
                return Err(err(Some(pc), "call-base marker adjustment is invalid"));
            }
            _ => {}
        }
        if matches!(
            op,
            Instr::OP_JUMP
                | Instr::OP_BRANCH_FALSE
                | Instr::OP_BRANCH_TRUE_KEEP
                | Instr::OP_BRANCH_FALSE_KEEP
                | Instr::OP_FOR_PREP
                | Instr::OP_FOR_LOOP
                | Instr::OP_TFOR_LOOP
        ) {
            let target = match i64::try_from(pc) {
                Ok(pc) => pc.checked_add(1),
                Err(_) => None,
            }
            .and_then(|next| next.checked_add(inst.sbx() as i64));
            if !target.is_some_and(|target| target >= 0 && target < code_len as i64) {
                return Err(err(Some(pc), "jump target is out of range"));
            }
        }
    }
    if view.global_cache_slots() as usize != next_global_slot
        || view.field_cache_slots() != next_field_slot
        || view.set_field_cache_slots() as usize != next_set_field_slot
    {
        return Err(err(None, "declared cache slots do not match instructions"));
    }
    verify_stack_discipline(view)
}