Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix `ExtendedOpcode::MAX` for Pulley (#12867)

Alex Crichton56cae563dce5
diff --git a/pulley/src/interp/tail_loop.rs b/pulley/src/interp/tail_loop.rs
index 55c5b68430..c9d7e952e9 100644
--- a/pulley/src/interp/tail_loop.rs
+++ b/pulley/src/interp/tail_loop.rs
@@ -128,9 +128,7 @@ static OPCODE_HANDLER_TABLE: [Handler; Opcode::MAX as usize + 1] = {
for_each_op!(define_opcode_handler_table)
};
-// same as above, but without a +1 for handling of extended ops as this is the
-// extended ops.
-static EXTENDED_OPCODE_HANDLER_TABLE: [Handler; ExtendedOpcode::MAX as usize] = {
+static EXTENDED_OPCODE_HANDLER_TABLE: [Handler; ExtendedOpcode::MAX as usize + 1] = {
macro_rules! define_extended_opcode_handler_table {
($(
$( #[$attr:meta] )*
diff --git a/pulley/src/opcode.rs b/pulley/src/opcode.rs
index 0400922126..0aa58ace6e 100644
--- a/pulley/src/opcode.rs
+++ b/pulley/src/opcode.rs
@@ -76,7 +76,7 @@ macro_rules! define_extended_opcode {
/// The value of the maximum defined extended opcode.
pub const MAX: u16 = $(
if true { 1 } else { ExtendedOpcode::$name as u16 } +
- )* 0;
+ )* 0 - 1;
}
};
}
@@ -104,3 +104,16 @@ impl ExtendedOpcode {
unsafe { core::mem::transmute(byte) }
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn max_values() {
+ assert!(Opcode::new(Opcode::MAX).is_some());
+ assert!(Opcode::new(Opcode::MAX + 1).is_none());
+ assert!(ExtendedOpcode::new(ExtendedOpcode::MAX).is_some());
+ assert!(ExtendedOpcode::new(ExtendedOpcode::MAX + 1).is_none());
+ }
+}