Skip to content

Commit 56cae56

Browse files
authored
Fix ExtendedOpcode::MAX for Pulley (#12867)
This was off-by-one which could lead to possible undefined behavior in Miri and at runtime when disassembling invalid opcodes. This isn't reachable from Wasmtime itself since Cranelift only generates valid opcodes, but it's nonetheless reachable via `wasmtime objdump` and still good to fix.
1 parent 425a601 commit 56cae56

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

pulley/src/interp/tail_loop.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ static OPCODE_HANDLER_TABLE: [Handler; Opcode::MAX as usize + 1] = {
128128
for_each_op!(define_opcode_handler_table)
129129
};
130130

131-
// same as above, but without a +1 for handling of extended ops as this is the
132-
// extended ops.
133-
static EXTENDED_OPCODE_HANDLER_TABLE: [Handler; ExtendedOpcode::MAX as usize] = {
131+
static EXTENDED_OPCODE_HANDLER_TABLE: [Handler; ExtendedOpcode::MAX as usize + 1] = {
134132
macro_rules! define_extended_opcode_handler_table {
135133
($(
136134
$( #[$attr:meta] )*

pulley/src/opcode.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ macro_rules! define_extended_opcode {
7676
/// The value of the maximum defined extended opcode.
7777
pub const MAX: u16 = $(
7878
if true { 1 } else { ExtendedOpcode::$name as u16 } +
79-
)* 0;
79+
)* 0 - 1;
8080
}
8181
};
8282
}
@@ -104,3 +104,16 @@ impl ExtendedOpcode {
104104
unsafe { core::mem::transmute(byte) }
105105
}
106106
}
107+
108+
#[cfg(test)]
109+
mod tests {
110+
use super::*;
111+
112+
#[test]
113+
fn max_values() {
114+
assert!(Opcode::new(Opcode::MAX).is_some());
115+
assert!(Opcode::new(Opcode::MAX + 1).is_none());
116+
assert!(ExtendedOpcode::new(ExtendedOpcode::MAX).is_some());
117+
assert!(ExtendedOpcode::new(ExtendedOpcode::MAX + 1).is_none());
118+
}
119+
}

0 commit comments

Comments
 (0)