diff --git a/lib/compiler-singlepass/README.md b/lib/compiler-singlepass/README.md index 955f7c68dc5..defc7998fd7 100644 --- a/lib/compiler-singlepass/README.md +++ b/lib/compiler-singlepass/README.md @@ -5,17 +5,23 @@ This crate contains a compiler implementation based on the Singlepass linear com Supported targets: - x86_64 - aarch64 -- riscv64 (experimental) +- riscv64 + ## Usage ```rust use wasmer::{Store, sys::EngineBuilder}; use wasmer_compiler_singlepass::Singlepass; -let compiler = Singlepass::new(); +let mut compiler = Singlepass::new(); +compiler.strict_memory_boundary_checks(true); let mut store = Store::new(compiler); ``` +`strict_memory_boundary_checks(true)` forces Singlepass to emit explicit +memory bounds checks for every memory access, including static memories that +would otherwise use the unchecked fast path. + *Note: you can find a [full working example using Singlepass compiler here][example].* diff --git a/lib/compiler-singlepass/src/codegen.rs b/lib/compiler-singlepass/src/codegen.rs index 7a784daebf2..57ab6e34633 100644 --- a/lib/compiler-singlepass/src/codegen.rs +++ b/lib/compiler-singlepass/src/codegen.rs @@ -839,10 +839,11 @@ impl<'a, M: Machine> FuncGen<'a, M> { &mut self, cb: F, ) -> Result<(), CompileError> { - let need_check = match self.memory_styles[MemoryIndex::new(0)] { - MemoryStyle::Static { .. } => false, - MemoryStyle::Dynamic { .. } => true, - }; + let need_check = self.config.strict_memory_boundary_checks + || match self.memory_styles[MemoryIndex::new(0)] { + MemoryStyle::Static { .. } => false, + MemoryStyle::Dynamic { .. } => true, + }; let offset = if self.module.num_imported_memories != 0 { self.vmoffsets diff --git a/lib/compiler-singlepass/src/config.rs b/lib/compiler-singlepass/src/config.rs index 1f4b2fd65fe..79b53ae5e5f 100644 --- a/lib/compiler-singlepass/src/config.rs +++ b/lib/compiler-singlepass/src/config.rs @@ -77,6 +77,9 @@ impl SinglepassCallbacks { pub struct Singlepass { pub(crate) enable_nan_canonicalization: bool, + /// Forces strict memory boundary checks for every memory access. + pub(crate) strict_memory_boundary_checks: bool, + /// The middleware chain. pub(crate) middlewares: Vec>, @@ -92,12 +95,26 @@ impl Singlepass { pub fn new() -> Self { Self { enable_nan_canonicalization: true, + strict_memory_boundary_checks: false, middlewares: vec![], callbacks: None, num_threads: std::thread::available_parallelism().unwrap_or(NonZero::new(1).unwrap()), } } + /// Configure whether Singlepass should always emit strict memory boundary + /// checks for memory accesses. + /// + /// When enabled, Singlepass always requests explicit memory bounds checks + /// from `op_memory`, even for static memories that would normally rely + /// on the access violation signal. + pub fn strict_memory_boundary_checks(&mut self, enable: bool) -> &mut Self { + self.strict_memory_boundary_checks = enable; + self + } + + /// Configure whether Singlepass should canonicalize NaNs during code + /// generation. pub fn canonicalize_nans(&mut self, enable: bool) -> &mut Self { self.enable_nan_canonicalization = enable; self diff --git a/lib/compiler-singlepass/src/machine_riscv.rs b/lib/compiler-singlepass/src/machine_riscv.rs index 2e67ac2b26a..b2ec600fc4c 100644 --- a/lib/compiler-singlepass/src/machine_riscv.rs +++ b/lib/compiler-singlepass/src/machine_riscv.rs @@ -900,6 +900,7 @@ impl MachineRiscv { memarg: &MemArg, check_alignment: bool, value_size: usize, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -939,23 +940,25 @@ impl MachineRiscv { self.emit_relaxed_load(Size::S64, false, Location::GPR(tmp_base), base_loc)?; // Load bound into temporary register. - self.emit_relaxed_load(Size::S64, false, Location::GPR(tmp_bound), bound_loc)?; + if need_check { + self.emit_relaxed_load(Size::S64, false, Location::GPR(tmp_bound), bound_loc)?; - // Wasm -> Effective. - // Assuming we never underflow - should always be true on Linux/macOS and Windows >=8, - // since the first page from 0x0 to 0x1000 is not accepted by mmap. - self.assembler.emit_add( - Size::S64, - Location::GPR(tmp_bound), - Location::GPR(tmp_base), - Location::GPR(tmp_bound), - )?; - self.assembler.emit_sub( - Size::S64, - Location::GPR(tmp_bound), - Location::Imm64(value_size as _), - Location::GPR(tmp_bound), - )?; + // Wasm -> Effective. + // Assuming we never underflow - should always be true on Linux/macOS and Windows >=8, + // since the first page from 0x0 to 0x1000 is not accepted by mmap. + self.assembler.emit_add( + Size::S64, + Location::GPR(tmp_bound), + Location::GPR(tmp_base), + Location::GPR(tmp_bound), + )?; + self.assembler.emit_sub( + Size::S64, + Location::GPR(tmp_bound), + Location::Imm64(value_size as _), + Location::GPR(tmp_bound), + )?; + } // Load effective address. // `base_loc` and `bound_loc` becomes INVALID after this line, because `tmp_addr` @@ -1017,17 +1020,22 @@ impl MachineRiscv { // tmp_base is already unused let cond = tmp_base; - // Trap if the end address of the requested area is above that of the linear memory. - self.assembler.emit_cmp( - Condition::Le, - Location::GPR(tmp_addr), - Location::GPR(tmp_bound), - Location::GPR(cond), - )?; + if need_check { + // Trap if the end address of the requested area is above that of the linear memory. + self.assembler.emit_cmp( + Condition::Le, + Location::GPR(tmp_addr), + Location::GPR(tmp_bound), + Location::GPR(cond), + )?; - // `tmp_bound` is inclusive. So trap only if `tmp_addr > tmp_bound`. - self.assembler - .emit_on_false_label_far(Location::GPR(cond), heap_access_oob, jmp_tmp)?; + // `tmp_bound` is inclusive. So trap only if `tmp_addr > tmp_bound`. + self.assembler.emit_on_false_label_far( + Location::GPR(cond), + heap_access_oob, + jmp_tmp, + )?; + } self.release_gpr(tmp_bound); self.release_gpr(cond); @@ -3336,7 +3344,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3347,6 +3355,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3360,7 +3369,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3371,6 +3380,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3384,7 +3394,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3395,6 +3405,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3408,7 +3419,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3419,6 +3430,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3432,7 +3444,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3443,6 +3455,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3456,7 +3469,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3467,6 +3480,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3480,7 +3494,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3491,6 +3505,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3504,7 +3519,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3515,6 +3530,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3528,7 +3544,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3539,6 +3555,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3552,7 +3569,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3563,6 +3580,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3576,7 +3594,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3587,6 +3605,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3600,7 +3619,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3611,6 +3630,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3625,7 +3645,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3636,6 +3656,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3650,7 +3671,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3661,6 +3682,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3676,7 +3698,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3687,6 +3709,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3703,7 +3726,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3714,6 +3737,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3730,7 +3754,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3741,6 +3765,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3757,7 +3782,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3768,6 +3793,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3784,7 +3810,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3795,6 +3821,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3811,7 +3838,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3822,6 +3849,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3838,7 +3866,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3849,6 +3877,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3865,7 +3894,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3876,6 +3905,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3892,7 +3922,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3903,6 +3933,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -3919,7 +3950,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3930,6 +3961,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -3946,7 +3978,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3957,6 +3989,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -3973,7 +4006,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -3984,6 +4017,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4000,7 +4034,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4011,6 +4045,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4027,7 +4062,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4038,6 +4073,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4054,7 +4090,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4065,6 +4101,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4081,7 +4118,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4092,6 +4129,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4108,7 +4146,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4119,6 +4157,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4135,7 +4174,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4146,6 +4185,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4163,7 +4203,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4174,6 +4214,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4189,7 +4230,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4200,6 +4241,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4215,7 +4257,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4226,6 +4268,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4661,7 +4704,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4672,6 +4715,7 @@ impl Machine for MachineRiscv { memarg, false, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -4685,7 +4729,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4696,6 +4740,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4709,7 +4754,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4720,6 +4765,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4733,7 +4779,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4744,6 +4790,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4757,7 +4804,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4768,6 +4815,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4781,7 +4829,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4792,6 +4840,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4805,7 +4854,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4816,6 +4865,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4829,7 +4879,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4840,6 +4890,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -4853,7 +4904,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4864,6 +4915,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4877,7 +4929,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4888,6 +4940,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4901,7 +4954,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4912,6 +4965,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -4925,7 +4979,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4936,6 +4990,7 @@ impl Machine for MachineRiscv { memarg, false, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -4949,7 +5004,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4960,6 +5015,7 @@ impl Machine for MachineRiscv { memarg, false, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -4973,7 +5029,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -4984,6 +5040,7 @@ impl Machine for MachineRiscv { memarg, false, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -4997,7 +5054,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5008,6 +5065,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5021,7 +5079,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5032,6 +5090,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5046,7 +5105,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5057,6 +5116,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5071,7 +5131,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5082,6 +5142,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5096,7 +5157,7 @@ impl Machine for MachineRiscv { value: Location, memarg: &MemArg, addr: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5107,6 +5168,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5122,7 +5184,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5133,6 +5195,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5149,7 +5212,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5160,6 +5223,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5176,7 +5240,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5187,6 +5251,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5203,7 +5268,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5214,6 +5279,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5230,7 +5296,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5241,6 +5307,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5257,7 +5324,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5268,6 +5335,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5284,7 +5352,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5295,6 +5363,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5311,7 +5380,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5322,6 +5391,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5338,7 +5408,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5349,6 +5419,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5365,7 +5436,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5376,6 +5447,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5392,7 +5464,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5403,6 +5475,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5419,7 +5492,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5430,6 +5503,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5446,7 +5520,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5457,6 +5531,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5473,7 +5548,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5484,6 +5559,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5500,7 +5576,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5511,6 +5587,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5527,7 +5604,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5538,6 +5615,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5554,7 +5632,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5565,6 +5643,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5581,7 +5660,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5592,6 +5671,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5608,7 +5688,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5619,6 +5699,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5635,7 +5716,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5646,6 +5727,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5662,7 +5744,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5673,6 +5755,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5689,7 +5772,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5700,6 +5783,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5716,7 +5800,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5727,6 +5811,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5743,7 +5828,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5754,6 +5839,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5771,7 +5857,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5782,6 +5868,7 @@ impl Machine for MachineRiscv { memarg, true, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5797,7 +5884,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5808,6 +5895,7 @@ impl Machine for MachineRiscv { memarg, true, 1, + need_check, imported_memories, offset, heap_access_oob, @@ -5823,7 +5911,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5834,6 +5922,7 @@ impl Machine for MachineRiscv { memarg, true, 2, + need_check, imported_memories, offset, heap_access_oob, @@ -5849,7 +5938,7 @@ impl Machine for MachineRiscv { target: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5860,6 +5949,7 @@ impl Machine for MachineRiscv { memarg, true, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5873,7 +5963,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5884,6 +5974,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5898,7 +5989,7 @@ impl Machine for MachineRiscv { memarg: &MemArg, addr: Location, canonicalize: bool, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5909,6 +6000,7 @@ impl Machine for MachineRiscv { memarg, false, 4, + need_check, imported_memories, offset, heap_access_oob, @@ -5928,7 +6020,7 @@ impl Machine for MachineRiscv { addr: Location, memarg: &MemArg, ret: Location, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5939,6 +6031,7 @@ impl Machine for MachineRiscv { memarg, false, 8, + need_check, imported_memories, offset, heap_access_oob, @@ -5953,7 +6046,7 @@ impl Machine for MachineRiscv { memarg: &MemArg, addr: Location, canonicalize: bool, - _need_check: bool, + need_check: bool, imported_memories: bool, offset: i32, heap_access_oob: Label, @@ -5964,6 +6057,7 @@ impl Machine for MachineRiscv { memarg, false, 8, + need_check, imported_memories, offset, heap_access_oob, diff --git a/tests/compilers/issues.rs b/tests/compilers/issues.rs index 271bd122bee..7afe8fac97c 100644 --- a/tests/compilers/issues.rs +++ b/tests/compilers/issues.rs @@ -1198,3 +1198,51 @@ fn issue_6401_llvm_v128_load16x4_s_oob() -> Result<()> { Ok(()) } + +#[cfg(feature = "singlepass")] +#[compiler_test(issues)] +fn singlepass_memory_trap(mut config: crate::Config) -> Result<()> { + if config.compiler != crate::Compiler::Singlepass { + return Ok(()); + } + + let mut compiler_config = wasmer_compiler_singlepass::Singlepass::default(); + compiler_config.strict_memory_boundary_checks(true); + let mut store = Store::new(wasmer_compiler::EngineBuilder::new(compiler_config)); + + let wasm_bytes = wat2wasm( + r#" +(module + (memory (export "memory") 1) + (data (i32.const 65528) "\01\02\03\04\05\06\07\08") + (func (export "run") + i32.const 65529 + i32.const 0 + i32.const 1 + select + i64.const 0 + i64.store) +) +"# + .as_bytes(), + ) + .expect("wat2wasm must succeed"); + + let module = Module::new(&store, wasm_bytes)?; + let instance = Instance::new(&mut store, &module, &imports! {})?; + let memory = instance.exports.get_memory("memory")?; + let run = instance.exports.get_function("run")?; + + let mut before = [0_u8; 8]; + memory.view(&store).read(65528, &mut before)?; + assert_eq!(before, [1, 2, 3, 4, 5, 6, 7, 8]); + + let trap = run.call(&mut store, &[]); + assert!(trap.is_err(), "expected out-of-bounds store to trap"); + + let mut after = [0_u8; 8]; + memory.view(&store).read(65528, &mut after)?; + assert_eq!(after, before); + + Ok(()) +} diff --git a/tests/ignores.txt b/tests/ignores.txt index 5a286f6e611..99535de52e0 100644 --- a/tests/ignores.txt +++ b/tests/ignores.txt @@ -129,10 +129,14 @@ llvm+loongarch64 spec::simd_splat::llvm # Windows doesn't overcommit and fails to allocate 4GB of memory windows wasmer::max_size_of_memory -# Some AARCH64 CPU have issue with segfault writin 64bits on border page, where the 1 32bits might be written. +# Some AARCH64 CPU have issue with segfaults writing 64bits on border page, where the 1 32bits might be written. aarch64+linux spec::align aarch64+linux spec::memory_trap +# Similarly for RISC-V +riscv64+singlepass spec::align +riscv64+singlepass spec::memory_trap + spec::instance # TODO: #5816