Skip to content

Commit

Permalink
winch: Ensure correct handling of libcalls
Browse files Browse the repository at this point in the history
This commit fixes a fuzz bug where modules involving known libcalls
would fail to compile given that they were unconditionally treated as
colocated libcalls.

This bug is only reproducible in non sse41 environments, given that some
operations like `floor` default to libcalls in this case. The
`use_colocated_libcalls` setting is not configurable within Wasmtime and
as such, they should be loaded into a register prior to emitting the
call. This will also ensure that the right 8-byte absolute relocation is
used.
  • Loading branch information
saulecabrera committed Feb 15, 2024
1 parent 120e6b2 commit 4fa2d3b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cranelift/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub use crate::machinst::buffer::{
};
pub use crate::machinst::{
CompiledCode, Final, MachBuffer, MachBufferFinalized, MachInst, MachInstEmit,
MachInstEmitState, MachLabel, RealReg, Reg, TextSectionBuilder, VCodeConstantData,
VCodeConstants, Writable,
MachInstEmitState, MachLabel, RealReg, Reg, RelocDistance, TextSectionBuilder,
VCodeConstantData, VCodeConstants, Writable,
};

mod alias_analysis;
Expand Down
22 changes: 16 additions & 6 deletions winch/codegen/src/isa/x64/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use cranelift_codegen::{
},
},
settings, Final, MachBuffer, MachBufferFinalized, MachInstEmit, MachInstEmitState, MachLabel,
VCodeConstantData, VCodeConstants, Writable,
RelocDistance, VCodeConstantData, VCodeConstants, Writable,
};

use super::address::Address;
Expand Down Expand Up @@ -1261,13 +1261,23 @@ impl Assembler {
}

/// Emit a call to a well-known libcall.
pub fn call_with_lib(&mut self, lib: LibCall) {
pub fn call_with_lib(&mut self, lib: LibCall, dst: Reg) {
let dest = ExternalName::LibCall(lib);
self.emit(Inst::CallKnown {
dest,
opcode: Opcode::Call,
info: None,

// `use_colocated_libcalls` is never `true` from within Wasmtime,
// so always require loading the libcall to a register and use
// a `Far` relocation distance to ensure the right relocation when
// emitting to binary.
//
// See [wasmtime::engine::Engine::check_compatible_with_shared_flag] and
// [wasmtime_cranelift_shared::obj::ModuleTextBuilder::apend_func]
self.emit(Inst::LoadExtName {
dst: Writable::from_reg(dst.into()),
name: Box::new(dest),
offset: 0,
distance: RelocDistance::Far,
});
self.call_with_reg(dst);
}

/// Emits a conditional jump to the given label.
Expand Down
2 changes: 1 addition & 1 deletion winch/codegen/src/isa/x64/masm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl Masm for MacroAssembler {
match callee {
CalleeKind::Indirect(reg) => self.asm.call_with_reg(reg),
CalleeKind::Direct(idx) => self.asm.call_with_index(idx),
CalleeKind::Known(lib) => self.asm.call_with_lib(lib),
CalleeKind::Known(lib) => self.asm.call_with_lib(lib, regs::scratch()),
};
total_stack
}
Expand Down
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f32_ceil/f32_ceil_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f30f1144240c movss dword ptr [rsp + 0xc], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f3440f107c240c movss xmm15, dword ptr [rsp + 0xc]
;; 4883ec04 sub rsp, 4
;; f3440f113c24 movss dword ptr [rsp], xmm15
;; 4883ec0c sub rsp, 0xc
;; f30f1044240c movss xmm0, dword ptr [rsp + 0xc]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c40c add rsp, 0xc
;; 4883c404 add rsp, 4
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f32_floor/f32_floor_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f30f1144240c movss dword ptr [rsp + 0xc], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f3440f107c240c movss xmm15, dword ptr [rsp + 0xc]
;; 4883ec04 sub rsp, 4
;; f3440f113c24 movss dword ptr [rsp], xmm15
;; 4883ec0c sub rsp, 0xc
;; f30f1044240c movss xmm0, dword ptr [rsp + 0xc]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c40c add rsp, 0xc
;; 4883c404 add rsp, 4
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f30f1144240c movss dword ptr [rsp + 0xc], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f3440f107c240c movss xmm15, dword ptr [rsp + 0xc]
;; 4883ec04 sub rsp, 4
;; f3440f113c24 movss dword ptr [rsp], xmm15
;; 4883ec0c sub rsp, 0xc
;; f30f1044240c movss xmm0, dword ptr [rsp + 0xc]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c40c add rsp, 0xc
;; 4883c404 add rsp, 4
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f32_trunc/f32_trunc_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f30f1144240c movss dword ptr [rsp + 0xc], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f3440f107c240c movss xmm15, dword ptr [rsp + 0xc]
;; 4883ec04 sub rsp, 4
;; f3440f113c24 movss dword ptr [rsp], xmm15
;; 4883ec0c sub rsp, 0xc
;; f30f1044240c movss xmm0, dword ptr [rsp + 0xc]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c40c add rsp, 0xc
;; 4883c404 add rsp, 4
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f64_ceil/f64_ceil_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f20f11442408 movsd qword ptr [rsp + 8], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f2440f107c2408 movsd xmm15, qword ptr [rsp + 8]
;; 4883ec08 sub rsp, 8
;; f2440f113c24 movsd qword ptr [rsp], xmm15
;; 4883ec08 sub rsp, 8
;; f20f10442408 movsd xmm0, qword ptr [rsp + 8]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c408 add rsp, 8
;; 4883c408 add rsp, 8
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f64_floor/f64_floor_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f20f11442408 movsd qword ptr [rsp + 8], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f2440f107c2408 movsd xmm15, qword ptr [rsp + 8]
;; 4883ec08 sub rsp, 8
;; f2440f113c24 movsd qword ptr [rsp], xmm15
;; 4883ec08 sub rsp, 8
;; f20f10442408 movsd xmm0, qword ptr [rsp + 8]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c408 add rsp, 8
;; 4883c408 add rsp, 8
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f20f11442408 movsd qword ptr [rsp + 8], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f2440f107c2408 movsd xmm15, qword ptr [rsp + 8]
;; 4883ec08 sub rsp, 8
;; f2440f113c24 movsd qword ptr [rsp], xmm15
;; 4883ec08 sub rsp, 8
;; f20f10442408 movsd xmm0, qword ptr [rsp + 8]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c408 add rsp, 8
;; 4883c408 add rsp, 8
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2
8 changes: 5 additions & 3 deletions winch/filetests/filetests/x64/f64_trunc/f64_trunc_param.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
;; 4d8b5e08 mov r11, qword ptr [r14 + 8]
;; 4d8b1b mov r11, qword ptr [r11]
;; 4939e3 cmp r11, rsp
;; 0f8738000000 ja 0x50
;; 0f8740000000 ja 0x58
;; 18: f20f11442408 movsd qword ptr [rsp + 8], xmm0
;; 4c893424 mov qword ptr [rsp], r14
;; f2440f107c2408 movsd xmm15, qword ptr [rsp + 8]
;; 4883ec08 sub rsp, 8
;; f2440f113c24 movsd qword ptr [rsp], xmm15
;; 4883ec08 sub rsp, 8
;; f20f10442408 movsd xmm0, qword ptr [rsp + 8]
;; e800000000 call 0x42
;; 49bb0000000000000000
;; movabs r11, 0
;; 41ffd3 call r11
;; 4883c408 add rsp, 8
;; 4883c408 add rsp, 8
;; 4883c410 add rsp, 0x10
;; 5d pop rbp
;; c3 ret
;; 50: 0f0b ud2
;; 58: 0f0b ud2

0 comments on commit 4fa2d3b

Please sign in to comment.