Skip to content

Commit

Permalink
atomic heap address compute align check
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Jan 13, 2025
1 parent 691957e commit 0b53b93
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 26 deletions.
2 changes: 0 additions & 2 deletions crates/wast-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,9 @@ impl WastTest {
"spec_testsuite/simd_store8_lane.wast",
// thread related failures
"proposals/threads/atomic.wast",
"misc_testsuite/threads/load-store-alignment.wast",
"misc_testsuite/threads/wait_notify.wast",
"misc_testsuite/threads/atomics_wait_address.wast",
"misc_testsuite/threads/atomics_notify.wast",
"misc_testsuite/threads/load-store-alignment.wast",
];

if unsupported.iter().any(|part| self.path.ends_with(part)) {
Expand Down
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/load/i32_atomic_load.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
;; movq 0x10(%r11), %r11
;; addq $0x20, %r11
;; cmpq %rsp, %r11
;; ja 0x47
;; ja 0x57
;; 1c: movq %rdi, %r14
;; subq $0x20, %rsp
;; movq %rdi, 0x18(%rsp)
;; movq %rsi, 0x10(%rsp)
;; movl %edx, 0xc(%rsp)
;; movl 0xc(%rsp), %eax
;; andl $3, %eax
;; cmpl $0, %eax
;; jne 0x59
;; 41: movl 0xc(%rsp), %eax
;; movq 0x58(%r14), %r11
;; movq (%r11), %rcx
;; addq %rax, %rcx
;; movl (%rcx), %eax
;; addq $0x20, %rsp
;; popq %rbp
;; retq
;; 47: ud2
;; 57: ud2
;; 59: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/load/i32_atomic_load16_u.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
;; movq 0x10(%r11), %r11
;; addq $0x20, %r11
;; cmpq %rsp, %r11
;; ja 0x49
;; ja 0x5b
;; 1c: movq %rdi, %r14
;; subq $0x20, %rsp
;; movq %rdi, 0x18(%rsp)
;; movq %rsi, 0x10(%rsp)
;; movl %edx, 0xc(%rsp)
;; movl 0xc(%rsp), %eax
;; andw $1, %ax
;; cmpw $0, %ax
;; jne 0x5d
;; 43: movl 0xc(%rsp), %eax
;; movq 0x58(%r14), %r11
;; movq (%r11), %rcx
;; addq %rax, %rcx
;; movzwq (%rcx), %rax
;; addq $0x20, %rsp
;; popq %rbp
;; retq
;; 49: ud2
;; 5b: ud2
;; 5d: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/load/i64_atomic_load.wat
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x41
;; ja 0x54
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0, %eax
;; andq $7, %rax
;; cmpq $0, %rax
;; jne 0x56
;; 3f: movl $0, %eax
;; movq 0x60(%r14), %rcx
;; addq %rax, %rcx
;; movq (%rcx), %rax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 41: ud2
;; 54: ud2
;; 56: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/load/i64_atomic_load16_u.wat
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x42
;; ja 0x55
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0, %eax
;; andw $1, %ax
;; cmpw $0, %ax
;; jne 0x57
;; 3f: movl $0, %eax
;; movq 0x60(%r14), %rcx
;; addq %rax, %rcx
;; movzwq (%rcx), %rax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 42: ud2
;; 55: ud2
;; 57: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/load/i64_atomic_load32_u.wat
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x40
;; ja 0x51
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0, %eax
;; andl $3, %eax
;; cmpl $0, %eax
;; jne 0x53
;; 3d: movl $0, %eax
;; movq 0x60(%r14), %rcx
;; addq %rax, %rcx
;; movl (%rcx), %eax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 40: ud2
;; 51: ud2
;; 53: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/store/i32_atomic_store.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x4b
;; ja 0x5c
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andl $3, %ecx
;; cmpl $0, %ecx
;; jne 0x5e
;; 42: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
Expand All @@ -26,4 +30,5 @@
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 4b: ud2
;; 5c: ud2
;; 5e: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/store/i32_atomic_store16.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x4c
;; ja 0x5f
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andw $1, %cx
;; cmpw $0, %cx
;; jne 0x61
;; 44: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
Expand All @@ -26,4 +30,5 @@
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 4c: ud2
;; 5f: ud2
;; 61: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/store/i64_atomic_store.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x4e
;; ja 0x61
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andq $7, %rcx
;; cmpq $0, %rcx
;; jne 0x63
;; 46: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
Expand All @@ -26,4 +30,5 @@
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 4e: ud2
;; 61: ud2
;; 63: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/store/i64_atomic_store16.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x4e
;; ja 0x61
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andw $1, %cx
;; cmpw $0, %cx
;; jne 0x63
;; 46: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
Expand All @@ -26,4 +30,5 @@
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 4e: ud2
;; 61: ud2
;; 63: ud2
9 changes: 7 additions & 2 deletions tests/disas/winch/x64/atomic/store/i64_atomic_store32.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x4d
;; ja 0x5e
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andl $3, %ecx
;; cmpl $0, %ecx
;; jne 0x60
;; 44: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
Expand All @@ -26,4 +30,5 @@
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 4d: ud2
;; 5e: ud2
;; 60: ud2
47 changes: 43 additions & 4 deletions winch/codegen/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
codegen::BlockSig,
isa::reg::{writable, Reg},
masm::{
ExtendKind, IntCmpKind, MacroAssembler, MemOpKind, OperandSize, RegImm, SPOffset,
ExtendKind, Imm, IntCmpKind, MacroAssembler, MemOpKind, OperandSize, RegImm, SPOffset,
ShiftKind, TrapCode,
},
stack::TypedReg,
Expand All @@ -19,7 +19,7 @@ use wasmparser::{
BinaryReader, FuncValidator, MemArg, Operator, ValidatorResources, VisitOperator,
VisitSimdOperator,
};
use wasmtime_cranelift::{TRAP_BAD_SIGNATURE, TRAP_TABLE_OUT_OF_BOUNDS};
use wasmtime_cranelift::{TRAP_BAD_SIGNATURE, TRAP_HEAP_MISALIGNED, TRAP_TABLE_OUT_OF_BOUNDS};
use wasmtime_environ::{
GlobalIndex, MemoryIndex, PtrSize, TableIndex, Tunables, TypeIndex, WasmHeapType, WasmValType,
FUNCREF_MASK,
Expand Down Expand Up @@ -648,7 +648,12 @@ where
&mut self,
memarg: &MemArg,
access_size: OperandSize,
check_align: bool,
) -> Result<Option<Reg>> {
if check_align {
self.check_align(memarg, access_size)?;
}

let ptr_size: OperandSize = self.env.ptr_type().try_into()?;
let enable_spectre_mitigation = self.env.heap_access_spectre_mitigation();
let add_offset_and_access_size = |offset: ImmOffset, access_size: OperandSize| {
Expand Down Expand Up @@ -840,6 +845,40 @@ where
Ok(addr)
}

fn check_align(&mut self, memarg: &MemArg, size: OperandSize) -> Result<()> {
if size.bytes() > 1 {
let addr = *self.context.stack.peek().unwrap();
let effective_addr_reg = self.context.any_gpr(self.masm)?;
self.context
.move_val_to_reg(&addr, effective_addr_reg, self.masm)?;
if memarg.offset != 0 {
// self.masm.add(dst, lhs, rhs, size)
// self.context.builder.ins().iadd_imm(addr, memarg.offset.signed())
self.masm.add(
writable!(effective_addr_reg),
effective_addr_reg,
RegImm::Imm(Imm::I64(memarg.offset)),
size,
)?;
};
self.masm.and(
writable!(effective_addr_reg),
effective_addr_reg,
RegImm::Imm(Imm::I32(size.bytes() - 1)),
size,
)?;

self.masm
.cmp(effective_addr_reg, RegImm::Imm(Imm::i64(0)), size)?;
self.masm.trapif(IntCmpKind::Ne, TRAP_HEAP_MISALIGNED)?;

// environ.trapnz(builder, f, crate::TRAP_HEAP_MISALIGNED);
self.context.free_reg(effective_addr_reg);
}

Ok(())
}

/// Emit a WebAssembly load.
pub fn emit_wasm_load(
&mut self,
Expand All @@ -849,7 +888,7 @@ where
sextend: Option<ExtendKind>,
op_kind: MemOpKind,
) -> Result<()> {
if let Some(addr) = self.emit_compute_heap_address(&arg, size)? {
if let Some(addr) = self.emit_compute_heap_address(&arg, size, op_kind.is_atomic())? {
let dst = match ty {
WasmValType::I32 | WasmValType::I64 => self.context.any_gpr(self.masm)?,
WasmValType::F32 | WasmValType::F64 => self.context.any_fpr(self.masm)?,
Expand All @@ -875,7 +914,7 @@ where
op_kind: MemOpKind,
) -> Result<()> {
let src = self.context.pop_to_reg(self.masm, None)?;
let addr = self.emit_compute_heap_address(&arg, size)?;
let addr = self.emit_compute_heap_address(&arg, size, op_kind.is_atomic())?;
if let Some(addr) = addr {
self.masm.wasm_store(
src.reg.into(),
Expand Down
6 changes: 6 additions & 0 deletions winch/codegen/src/masm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub(crate) enum MemOpKind {
Normal,
}

impl MemOpKind {
pub(crate) fn is_atomic(&self) -> bool {
matches!(self, Self::Atomic)
}
}

#[derive(Eq, PartialEq)]
pub(crate) enum MulWideKind {
Signed,
Expand Down

0 comments on commit 0b53b93

Please sign in to comment.