Skip to content

Remove ctx argument on imported functions and allow closures. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 1 addition & 87 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 11 additions & 22 deletions lib/clif-backend/src/func_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,12 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
vm::Anyfunc::offset_func() as i32,
);

let vmctx_ptr = {
let loaded_vmctx_ptr = pos.ins().load(
ptr_type,
mflags,
entry_addr,
vm::Anyfunc::offset_vmctx() as i32,
);

let argument_vmctx_ptr = pos
.func
.special_param(ir::ArgumentPurpose::VMContext)
.expect("missing vmctx parameter");

// If the loaded vmctx ptr is zero, use the caller vmctx, else use the callee (loaded) vmctx.
pos.ins()
.select(loaded_vmctx_ptr, loaded_vmctx_ptr, argument_vmctx_ptr)
};
let env_ptr = pos.ins().load(
ptr_type,
mflags,
entry_addr,
vm::Anyfunc::offset_env() as i32,
);

let found_sig = pos.ins().load(
ir::types::I32,
Expand Down Expand Up @@ -467,7 +456,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
// Build a value list for the indirect call instruction containing the call_args
// and the vmctx parameter.
let mut args = Vec::with_capacity(call_args.len() + 1);
args.push(vmctx_ptr);
args.push(env_ptr);
args.extend(call_args.iter().cloned());

Ok(pos.ins().call_indirect(sig_ref, func_ptr, &args))
Expand Down Expand Up @@ -546,20 +535,20 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
readonly: true,
});

let imported_vmctx_addr = pos.func.create_global_value(ir::GlobalValueData::Load {
let imported_env_addr = pos.func.create_global_value(ir::GlobalValueData::Load {
base: imported_func_struct_addr,
offset: (vm::ImportedFunc::offset_vmctx() as i32).into(),
offset: (vm::ImportedFunc::offset_env() as i32).into(),
global_type: ptr_type,
readonly: true,
});

let imported_func_addr = pos.ins().global_value(ptr_type, imported_func_addr);
let imported_vmctx_addr = pos.ins().global_value(ptr_type, imported_vmctx_addr);
let imported_env_addr = pos.ins().global_value(ptr_type, imported_env_addr);

let sig_ref = pos.func.dfg.ext_funcs[callee].signature;

let mut args = Vec::with_capacity(call_args.len() + 1);
args.push(imported_vmctx_addr);
args.push(imported_env_addr);
args.extend(call_args.iter().cloned());

Ok(pos
Expand Down
17 changes: 11 additions & 6 deletions lib/clif-backend/src/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ProtectedCaller for Caller {
call_protected(&self.handler_data, || unsafe {
// Leap of faith.
trampoline(
vmctx_ptr,
NonNull::new(vmctx_ptr).map(|p| p.cast()),
func_ptr,
param_vec.as_ptr(),
return_vec.as_mut_ptr(),
Expand Down Expand Up @@ -151,8 +151,13 @@ impl ProtectedCaller for Caller {

fn get_wasm_trampoline(&self, module: &ModuleInner, sig_index: SigIndex) -> Option<Wasm> {
unsafe extern "C" fn invoke(
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
ctx: *mut vm::Ctx,
trampoline: unsafe extern "C" fn(
Option<NonNull<vm::Env>>,
NonNull<vm::Func>,
*const u64,
*mut u64,
),
env: Option<NonNull<vm::Env>>,
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
Expand All @@ -164,13 +169,13 @@ impl ProtectedCaller for Caller {
#[cfg(not(target_os = "windows"))]
let res = call_protected(handler_data, || unsafe {
// Leap of faith.
trampoline(ctx, func, args, rets);
trampoline(env, func, args, rets);
})
.is_ok();

// the trampoline is called from C on windows
#[cfg(target_os = "windows")]
let res = call_protected(handler_data, trampoline, ctx, func, args, rets).is_ok();
let res = call_protected(handler_data, trampoline, env, func, args, rets).is_ok();

res
}
Expand Down Expand Up @@ -218,7 +223,7 @@ fn get_func_from_index<'a>(
let imported_func = import_backing.imported_func(imported_func_index);
(
NonNull::new(imported_func.func as *mut _).unwrap(),
Context::External(imported_func.vmctx),
Context::External(imported_func.env as _),
)
}
};
Expand Down
3 changes: 2 additions & 1 deletion lib/clif-backend/src/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl RelocSink for NullRelocSink {
fn reloc_jt(&mut self, _: u32, _: Reloc, _: ir::JumpTable) {}
}

pub type Trampoline = unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64);
pub type Trampoline =
unsafe extern "C" fn(Option<NonNull<vm::Env>>, NonNull<vm::Func>, *const u64, *mut u64);

pub struct Trampolines {
memory: Memory,
Expand Down
17 changes: 11 additions & 6 deletions lib/llvm-backend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ extern "C" {

#[allow(improper_ctypes)]
fn invoke_trampoline(
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
vmctx_ptr: *mut vm::Ctx,
trampoline: unsafe extern "C" fn(
Option<NonNull<vm::Env>>,
NonNull<vm::Func>,
*const u64,
*mut u64,
),
vmctx_ptr: Option<NonNull<vm::Env>>,
func_ptr: NonNull<vm::Func>,
params: *const u64,
results: *mut u64,
Expand Down Expand Up @@ -359,7 +364,7 @@ impl ProtectedCaller for LLVMProtectedCaller {
let mut return_vec = vec![0; signature.returns().len()];

let trampoline: unsafe extern "C" fn(
*mut vm::Ctx,
Option<NonNull<vm::Env>>,
NonNull<vm::Func>,
*const u64,
*mut u64,
Expand All @@ -383,7 +388,7 @@ impl ProtectedCaller for LLVMProtectedCaller {
let success = unsafe {
invoke_trampoline(
trampoline,
vmctx_ptr,
mem::transmute(vmctx_ptr),
func_ptr,
param_vec.as_ptr(),
return_vec.as_mut_ptr(),
Expand Down Expand Up @@ -412,7 +417,7 @@ impl ProtectedCaller for LLVMProtectedCaller {

fn get_wasm_trampoline(&self, _module: &ModuleInner, sig_index: SigIndex) -> Option<Wasm> {
let trampoline: unsafe extern "C" fn(
*mut vm::Ctx,
Option<NonNull<vm::Env>>,
NonNull<vm::Func>,
*const u64,
*mut u64,
Expand Down Expand Up @@ -468,7 +473,7 @@ fn get_func_from_index<'a>(
let imported_func = import_backing.imported_func(imported_func_index);
(
NonNull::new(imported_func.func as *mut _).unwrap(),
Context::External(imported_func.vmctx),
Context::External(imported_func.env as _),
)
}
};
Expand Down
Loading