diff --git a/crates/revmc-backend/src/traits.rs b/crates/revmc-backend/src/traits.rs index 8df629f..1218c3e 100644 --- a/crates/revmc-backend/src/traits.rs +++ b/crates/revmc-backend/src/traits.rs @@ -271,8 +271,14 @@ pub trait Builder: BackendTypes + TypeMethods { fn stack_store(&mut self, value: Self::Value, slot: Self::StackSlot); fn stack_addr(&mut self, ty: Self::Type, slot: Self::StackSlot) -> Self::Value; - fn load(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value; - fn store(&mut self, value: Self::Value, ptr: Self::Value); + fn load(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value { + self.load_unaligned(ty, ptr, name) + } + fn load_unaligned(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value; + fn store(&mut self, value: Self::Value, ptr: Self::Value) { + self.store_unaligned(value, ptr); + } + fn store_unaligned(&mut self, value: Self::Value, ptr: Self::Value); fn nop(&mut self); fn ret(&mut self, values: &[Self::Value]); diff --git a/crates/revmc-cranelift/src/lib.rs b/crates/revmc-cranelift/src/lib.rs index 2242f3c..62712c3 100644 --- a/crates/revmc-cranelift/src/lib.rs +++ b/crates/revmc-cranelift/src/lib.rs @@ -465,10 +465,19 @@ impl<'a> Builder for EvmCraneliftBuilder<'a> { self.bcx.ins().load(ty, MemFlags::trusted(), ptr, 0) } + fn load_unaligned(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value { + let _ = name; + self.bcx.ins().load(ty, MemFlags::new().with_notrap(), ptr, 0) + } + fn store(&mut self, value: Self::Value, ptr: Self::Value) { self.bcx.ins().store(MemFlags::trusted(), value, ptr, 0); } + fn store_unaligned(&mut self, value: Self::Value, ptr: Self::Value) { + self.bcx.ins().store(MemFlags::new().with_notrap(), value, ptr, 0); + } + fn nop(&mut self) { self.bcx.ins().nop(); } diff --git a/crates/revmc-llvm/src/lib.rs b/crates/revmc-llvm/src/lib.rs index e14c377..9989d44 100644 --- a/crates/revmc-llvm/src/lib.rs +++ b/crates/revmc-llvm/src/lib.rs @@ -703,10 +703,21 @@ impl Builder for EvmLlvmBuilder<'_, '_> { self.bcx.build_load(ty, ptr.into_pointer_value(), name).unwrap() } + fn load_unaligned(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value { + let value = self.load(ty, ptr, name); + self.current_block().unwrap().get_last_instruction().unwrap().set_alignment(1).unwrap(); + value + } + fn store(&mut self, value: Self::Value, ptr: Self::Value) { self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); } + fn store_unaligned(&mut self, value: Self::Value, ptr: Self::Value) { + let inst = self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); + inst.set_alignment(1).unwrap(); + } + fn nop(&mut self) { // LLVM doesn't have a NOP instruction. } diff --git a/crates/revmc/src/compiler/translate.rs b/crates/revmc/src/compiler/translate.rs index fa4c6cc..bc81010 100644 --- a/crates/revmc/src/compiler/translate.rs +++ b/crates/revmc/src/compiler/translate.rs @@ -1945,14 +1945,6 @@ impl FunctionCx<'_, B> { /// - `Store8` => `fn mstore(offset: u256, value: u8, ecx: ptr) -> InstructionResult` fn build_mem_op(&mut self, kind: MemOpKind) { let is_load = matches!(kind, MemOpKind::Load); - // TODO: If `store` is inlined it can cause segfaults. https://github.com/paradigmxyz/revmc/issues/61 - if !is_load { - self.bcx.add_function_attribute( - None, - Attribute::NoInline, - FunctionAttributeLocation::Function, - ); - } let ptr_args = if is_load { &[1, 2][..] } else { &[2][..] }; for &ptr_arg in ptr_args { for attr in default_attrs::for_ref() { @@ -2046,7 +2038,7 @@ impl FunctionCx<'_, B> { let slot = self.bcx.gep(self.i8_type, buffer_ptr, &[offset], "slot"); match kind { MemOpKind::Load => { - let loaded = self.bcx.load(self.word_type, slot, "slot.value"); + let loaded = self.bcx.load_unaligned(self.word_type, slot, "slot.value"); let loaded = if cfg!(target_endian = "little") { self.bcx.bswap(loaded) } else { loaded }; self.bcx.store(loaded, value); @@ -2057,7 +2049,7 @@ impl FunctionCx<'_, B> { } else { value }; - self.bcx.store(value, slot); + self.bcx.store_unaligned(value, slot); } }