Repository: bytecodealliance/wasmtime
Pull request #7950 (2024-02-16 01:14:55)

Allow `traps.rs` tests to pass even when `RUST_BACKTRACE=1` on nightly Rust
Instead of asserting exact error messages, which `anyhow` auto adds Rust backtrace to on nightly, assert that the important bits are there.

Split out from #7943.

<!--
Please make sure you include the following information:

- If this work has been discussed elsewhere, please include a link to that
  conversation. If it was discussed in an issue, just mention "issue #...".

- Explain why this change is needed. If the details are in an issue already,
  this can be brief.

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->



Patch:
diff --git a/cranelift/codegen/src/lib.rs b/cranelift/codegen/src/lib.rs
index 1908bdd3d..72b67f129 100644
--- a/cranelift/codegen/src/lib.rs
+++ b/cranelift/codegen/src/lib.rs
@@ -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;
diff --git a/winch/codegen/src/isa/x64/asm.rs b/winch/codegen/src/isa/x64/asm.rs
index 752feb621..a535407b7 100644
--- a/winch/codegen/src/isa/x64/asm.rs
+++ b/winch/codegen/src/isa/x64/asm.rs
@@ -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;
@@ -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.
diff --git a/winch/codegen/src/isa/x64/masm.rs b/winch/codegen/src/isa/x64/masm.rs
index fe919f0f5..ec98a4165 100644
--- a/winch/codegen/src/isa/x64/masm.rs
+++ b/winch/codegen/src/isa/x64/masm.rs
@@ -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
     }


Test patch:
diff --git a/tests/all/traps.rs b/tests/all/traps.rs
index 1cfc97ae6..e7f3596a2 100644
--- a/tests/all/traps.rs
+++ b/tests/all/traps.rs
@@ -307,8 +307,8 @@ fn trap_display_pretty() -> Result<()> {
     let run_func = instance.get_typed_func::<(), ()>(&mut store, "bar")?;
 
     let e = run_func.call(&mut store, ()).unwrap_err();
-    assert_eq!(
-        format!("{:?}", e),
+    let e = format!("{e:?}");
+    assert!(e.contains(
         "\
 error while executing at wasm backtrace:
     0:   0x23 - m!die
@@ -319,7 +319,7 @@ error while executing at wasm backtrace:
 Caused by:
     wasm trap: wasm `unreachable` instruction executed\
 "
-    );
+    ));
     Ok(())
 }
 
@@ -351,8 +351,8 @@ fn trap_display_multi_module() -> Result<()> {
     let bar2 = instance.get_typed_func::<(), ()>(&mut store, "bar2")?;
 
     let e = bar2.call(&mut store, ()).unwrap_err();
-    assert_eq!(
-        format!("{e:?}"),
+    let e = format!("{e:?}");
+    assert!(e.contains(
         "\
 error while executing at wasm backtrace:
     0:   0x23 - a!die
@@ -365,7 +365,7 @@ error while executing at wasm backtrace:
 Caused by:
     wasm trap: wasm `unreachable` instruction executed\
 "
-    );
+    ));
     Ok(())
 }
 
@@ -592,11 +592,10 @@ fn start_trap_pretty() -> Result<()> {
     let module = Module::new(store.engine(), wat)?;
     let e = match Instance::new(&mut store, &module, &[]) {
         Ok(_) => panic!("expected failure"),
-        Err(e) => e,
+        Err(e) => format!("{e:?}"),
     };
 
-    assert_eq!(
-        format!("{e:?}"),
+    assert!(e.contains(
         "\
 error while executing at wasm backtrace:
     0:   0x1d - m!die
@@ -607,7 +606,7 @@ error while executing at wasm backtrace:
 Caused by:
     wasm trap: wasm `unreachable` instruction executed\
 "
-    );
+    ));
     Ok(())
 }
 
@@ -800,8 +799,8 @@ fn no_hint_even_with_dwarf_info() -> Result<()> {
         "#,
     )?;
     let trap = Instance::new(&mut store, &module, &[]).unwrap_err();
-    assert_eq!(
-        format!("{trap:?}"),
+    let trap = format!("{trap:?}");
+    assert!(trap.contains(
         "\
 error while executing at wasm backtrace:
     0:   0x1a - <unknown>!start
@@ -809,7 +808,8 @@ error while executing at wasm backtrace:
 Caused by:
     wasm trap: wasm `unreachable` instruction executed\
 "
-    );
+    ));
+    assert!(!trap.contains("WASM_BACKTRACE_DETAILS"));
     Ok(())
 }
 
@@ -833,17 +833,16 @@ fn hint_with_dwarf_info() -> Result<()> {
         "#,
     )?;
     let trap = Instance::new(&mut store, &module, &[]).unwrap_err();
-    assert_eq!(
-        format!("{trap:?}"),
+    let trap = format!("{trap:?}");
+    assert!(trap.contains(
         "\
 error while executing at wasm backtrace:
     0:   0x1a - <unknown>!start
 note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
 
 Caused by:
-    wasm trap: wasm `unreachable` instruction executed\
-"
-    );
+    wasm trap: wasm `unreachable` instruction executed"
+    ));
     Ok(())
 }
 
diff --git a/winch/filetests/filetests/x64/f32_ceil/f32_ceil_param.wat b/winch/filetests/filetests/x64/f32_ceil/f32_ceil_param.wat
index dee7b1302..cf9c0b168 100644
--- a/winch/filetests/filetests/x64/f32_ceil/f32_ceil_param.wat
+++ b/winch/filetests/filetests/x64/f32_ceil/f32_ceil_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f32_floor/f32_floor_param.wat b/winch/filetests/filetests/x64/f32_floor/f32_floor_param.wat
index 235979279..5724dacbe 100644
--- a/winch/filetests/filetests/x64/f32_floor/f32_floor_param.wat
+++ b/winch/filetests/filetests/x64/f32_floor/f32_floor_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f32_nearest/f32_nearest_param.wat b/winch/filetests/filetests/x64/f32_nearest/f32_nearest_param.wat
index ab3f2c39e..c527b745d 100644
--- a/winch/filetests/filetests/x64/f32_nearest/f32_nearest_param.wat
+++ b/winch/filetests/filetests/x64/f32_nearest/f32_nearest_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f32_trunc/f32_trunc_param.wat b/winch/filetests/filetests/x64/f32_trunc/f32_trunc_param.wat
index b7a36ae1f..dbe63ac49 100644
--- a/winch/filetests/filetests/x64/f32_trunc/f32_trunc_param.wat
+++ b/winch/filetests/filetests/x64/f32_trunc/f32_trunc_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f64_ceil/f64_ceil_param.wat b/winch/filetests/filetests/x64/f64_ceil/f64_ceil_param.wat
index 7395b354c..f7b173c11 100644
--- a/winch/filetests/filetests/x64/f64_ceil/f64_ceil_param.wat
+++ b/winch/filetests/filetests/x64/f64_ceil/f64_ceil_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f64_floor/f64_floor_param.wat b/winch/filetests/filetests/x64/f64_floor/f64_floor_param.wat
index 4e7c6c198..eed312b7c 100644
--- a/winch/filetests/filetests/x64/f64_floor/f64_floor_param.wat
+++ b/winch/filetests/filetests/x64/f64_floor/f64_floor_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f64_nearest/f64_nearest_param.wat b/winch/filetests/filetests/x64/f64_nearest/f64_nearest_param.wat
index d4a1e0182..dbec0517b 100644
--- a/winch/filetests/filetests/x64/f64_nearest/f64_nearest_param.wat
+++ b/winch/filetests/filetests/x64/f64_nearest/f64_nearest_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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	
diff --git a/winch/filetests/filetests/x64/f64_trunc/f64_trunc_param.wat b/winch/filetests/filetests/x64/f64_trunc/f64_trunc_param.wat
index 87d9f5efb..3ba310800 100644
--- a/winch/filetests/filetests/x64/f64_trunc/f64_trunc_param.wat
+++ b/winch/filetests/filetests/x64/f64_trunc/f64_trunc_param.wat
@@ -12,7 +12,7 @@
 ;;      	 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]
@@ -20,10 +20,12 @@
 ;;      	 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