Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memory operations should be unaligned #67

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/revmc-backend/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
9 changes: 9 additions & 0 deletions crates/revmc-cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
11 changes: 11 additions & 0 deletions crates/revmc-llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
12 changes: 2 additions & 10 deletions crates/revmc/src/compiler/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,14 +1945,6 @@ impl<B: Backend> 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() {
Expand Down Expand Up @@ -2046,7 +2038,7 @@ impl<B: Backend> 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);
Expand All @@ -2057,7 +2049,7 @@ impl<B: Backend> FunctionCx<'_, B> {
} else {
value
};
self.bcx.store(value, slot);
self.bcx.store_unaligned(value, slot);
}
}

Expand Down
Loading