cranelift-interpreter: Add trap on misaligned memory accesses (#5921)

* Add checks to `InterpreterState::checked_{load,store}` to trap on misaligned memory accesses
where `aligned` memory flag is set.

* Alter `stack_{load,store}` instructions to now rely on `MemFlags::new()` instead of
`MemFlags::trusted` since `InterpreterState::checked_{load,store}` is only able to
deduce type alignment and not stack slot alignment.
This commit is contained in:
Jan-Justin van Tonder
2023-03-07 01:06:19 +01:00
committed by GitHub
parent 3c9fc3ec8c
commit a2beacd288
3 changed files with 69 additions and 2 deletions

View File

@@ -173,6 +173,8 @@ where
MemoryError::InvalidEntry { .. } => TrapCode::HeapOutOfBounds,
MemoryError::OutOfBoundsStore { .. } => TrapCode::HeapOutOfBounds,
MemoryError::OutOfBoundsLoad { .. } => TrapCode::HeapOutOfBounds,
MemoryError::MisalignedLoad { .. } => TrapCode::HeapMisaligned,
MemoryError::MisalignedStore { .. } => TrapCode::HeapMisaligned,
};
// Assigns or traps depending on the value of the result
@@ -522,7 +524,7 @@ where
let load_ty = inst_context.controlling_type().unwrap();
let slot = inst.stack_slot().unwrap();
let offset = sum(imm(), args()?)? as u64;
let mem_flags = MemFlags::trusted();
let mem_flags = MemFlags::new();
assign_or_memtrap({
state
.stack_address(AddressSize::_64, slot, offset)
@@ -533,7 +535,7 @@ where
let arg = arg(0)?;
let slot = inst.stack_slot().unwrap();
let offset = sum(imm(), args_range(1..)?)? as u64;
let mem_flags = MemFlags::trusted();
let mem_flags = MemFlags::new();
continue_or_memtrap({
state
.stack_address(AddressSize::_64, slot, offset)