Skip to content

Commit

Permalink
Migrate libcalls to checking for traps
Browse files Browse the repository at this point in the history
This commit is the equivalent of bytecodealliance#9675 for libcalls used in core wasm.
All libcalls now communicate whether or not they trapped through their
return value instead of implicitly calling `longjmp` to exit from the
libcall. This is to make integration with Pulley easier to avoid the
need to `longjmp` over Pulley execution.

Libcall definitions have changed where appropriate and the
`catch_unwind_and_record_trap` function introduced in bytecodealliance#9675 was
refactored to better support multiple types of values being returned
from libcalls (instead of just `Result<()>`).

Note that changes have been made to both the Cranelift translation layer
and the Winch translation layer for this as the ABI of various libcalls
are all changing.
  • Loading branch information
alexcrichton committed Dec 4, 2024
1 parent 3b6dc59 commit 4fef924
Show file tree
Hide file tree
Showing 37 changed files with 1,538 additions and 854 deletions.
24 changes: 13 additions & 11 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct Compiler {
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<path::PathBuf>,
#[cfg_attr(not(feature = "wmemcheck"), allow(dead_code))]
wmemcheck: bool,
}

Expand Down Expand Up @@ -227,14 +228,7 @@ impl wasmtime_environ::Compiler for Compiler {
context.func.collect_debug_info();
}

let mut func_env = FuncEnvironment::new(
isa,
translation,
types,
&self.tunables,
self.wmemcheck,
wasm_func_ty,
);
let mut func_env = FuncEnvironment::new(self, translation, types, wasm_func_ty);

// The `stack_limit` global value below is the implementation of stack
// overflow checks in Wasmtime.
Expand Down Expand Up @@ -602,7 +596,7 @@ impl wasmtime_environ::Compiler for Compiler {
let isa = &*self.isa;
let ptr_size = isa.pointer_bytes();
let pointer_type = isa.pointer_type();
let sigs = BuiltinFunctionSignatures::new(isa, &self.tunables);
let sigs = BuiltinFunctionSignatures::new(self);
let wasm_sig = sigs.wasm_signature(index);
let host_sig = sigs.host_signature(index);

Expand Down Expand Up @@ -861,7 +855,7 @@ impl Compiler {
/// Additionally in the future for pulley this will emit a special trap
/// opcode for Pulley itself to cease interpretation and exit the
/// interpreter.
fn raise_if_host_trapped(
pub fn raise_if_host_trapped(
&self,
builder: &mut FunctionBuilder<'_>,
vmctx: ir::Value,
Expand All @@ -878,7 +872,7 @@ impl Compiler {
builder.seal_block(continuation_block);

builder.switch_to_block(trapped_block);
let sigs = BuiltinFunctionSignatures::new(&*self.isa, &self.tunables);
let sigs = BuiltinFunctionSignatures::new(self);
let sig = sigs.host_signature(BuiltinFunctionIndex::raise());
self.call_builtin(builder, vmctx, &[vmctx], BuiltinFunctionIndex::raise(), sig);
builder.ins().trap(TRAP_INTERNAL_ASSERT);
Expand Down Expand Up @@ -918,6 +912,14 @@ impl Compiler {
let sig = builder.func.import_signature(sig);
self.call_indirect_host(builder, sig, func_addr, args)
}

pub fn isa(&self) -> &dyn TargetIsa {
&*self.isa
}

pub fn tunables(&self) -> &Tunables {
&self.tunables
}
}

struct FunctionCompiler<'a> {
Expand Down
Loading

0 comments on commit 4fef924

Please sign in to comment.