Skip to content

Commit

Permalink
Merge #925
Browse files Browse the repository at this point in the history
925: feat(runtime-core) Support closures with a captured environment as host functions r=Hywan a=Hywan

Reboot of #882 and #219.

For the moment, host functions (aka imported functions) can be regular function pointers, or (as a side-effect) closures without a captured environment. This PR extends the support of host functions to closures with a captured environment. This is required for many other features (incl. the Python integration, the Ruby integration, WebAssembly Interface Types [see #787], and so on).

This PR is the culmination of previous ones, notably #915, #916 and #917. 

### General idea

The user-defined host function is wrapped inside a `wrap` function. This wrapper function initially receives a `vm::Ctx` as its first argument, which is passed to the host function when necessary. The patch keeps this behavior but it comes from `vm::FuncCtx`, which is a new structure. A `vm::FuncCtx` is held by `vm::ImportedFunc` such as:

```rust
#[repr(C)]
pub struct ImportedFunc {
    pub(crate) func: *const Func,
    pub(crate) func_ctx: NonNull<FuncCtx>,
}
```

where `vm::FuncCtx` is:

```rust
#[repr(C)]
pub struct FuncCtx {
    pub(crate) vmctx: NonNull<Ctx>,
    pub(crate) func_env: Option<NonNull<FuncEnv>>,
}
```

where `vm::FuncEnv` is:

```rust
#[repr(transparent)]
pub struct FuncEnv(pub(self) *mut c_void);
```

i.e. a raw opaque pointer.

So the wrapper function of a host function receives a `vm::Ctx`, which is used to find out the associated `FuncCtx` (by using the import backing), which holds `vm::FuncEnv`. It holds a pointer to the closure captured environment.

### Implementation details

#### How to get a pointer to a closure captured environment

A closure with a captured environment has a memory size greater than zero. This is how we detect it:

```rust
if mem::size_of::<Self>() != 0 { … }
```

To get a pointer to its captured environment, we use this statement:

```rust
NonNull::new(Box::into_raw(Box::new(self))).map(NonNull::cast)
```

(in `typed_func.rs`, in the `wrap` functions).

To reconstruct the closure based on the pointer, we use this statement:

```rust
let func: &FN = {
    let func: NonNull<FN> = func_env.cast();
    &*func.as_ptr()
};
```

That's basically how it works. And that's the core idea of this patch.

As a side effect, we have removed an undefined behavior (UB) in 2 places: The `mem::transmute(&())` has been removed (it was used to get the function pointer of `FN`). The transmute is replaced by `FuncEnv`, which provides a unified API, erasing the difference between host functions as closures with a captured environment, and host functions as function pointer. For a reason I ignore, the UB wasn't showing himself until this PR and a modification in the Singlepass backend. But now it's fixed.

#### Impact on `Backing`

After the modification on the `typed_func` and the `vm` modules, this is the other core idea of this patch: Updating the `backing` module so that `vm::ImportedFunc` replaces `vm::Ctx` by `vm::FuncCtx`.

When creating `vm::ImportedFunc`, a new `vm::FuncCtx` is created and its pointer is used. We are purposely leaking `vm::FuncCtx` so that the pointer is always valid. Hence the specific `Drop` implementation on `ImportBacking` to dereference the pointer, and to drop it properly.

#### Impact on the backends

Since the structure of `vm::ImportedFunc` has changed, backends must be updated. We must deref `FuncCtx` to reach its `vmctx` field.

#### Impact on `Instance`

Because `vm::ImportedFunc` has changed, it has a minor impact on the `instance` module, nothing crazy though.

Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Nov 12, 2019
2 parents 3991db5 + 5d6c74b commit 365bcd6
Show file tree
Hide file tree
Showing 12 changed files with 673 additions and 252 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## **[Unreleased]**

## 0.10.0 - 2019-11-11
- [#925](https://github.com/wasmerio/wasmer/pull/925) Host functions can be closures with a captured environment.
- [#917](https://github.com/wasmerio/wasmer/pull/917) Host functions (aka imported functions) may not have `&mut vm::Ctx` as first argument, i.e. the presence of the `&mut vm::Ctx` argument is optional.
- [#915](https://github.com/wasmerio/wasmer/pull/915) All backends share the same definition of `Trampoline` (defined in `wasmer-runtime-core`).

## 0.10.1 - 2019-11-11

- [#952](https://github.com/wasmerio/wasmer/pull/952) Use C preprocessor to properly hide trampoline functions on Windows and non-x86_64 targets.

Expand Down
31 changes: 22 additions & 9 deletions lib/clif-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ impl FuncEnvironment for FunctionEnvironment {
}

/// Generates a call IR with `callee` and `call_args` and inserts it at `pos`
/// TODO: add support for imported functions
///
/// It's about generating code that calls a local or imported function; in
/// WebAssembly: `(call $foo)`.
fn translate_call(
&mut self,
mut pos: FuncCursor,
Expand Down Expand Up @@ -763,20 +765,31 @@ impl FuncEnvironment for FunctionEnvironment {
readonly: true,
});

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

let imported_func_ctx_vmctx_addr =
pos.func.create_global_value(ir::GlobalValueData::Load {
base: imported_func_ctx_addr,
offset: (vm::FuncCtx::offset_vmctx() 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_func_ctx_vmctx_addr = pos
.ins()
.global_value(ptr_type, imported_func_ctx_vmctx_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_func_ctx_vmctx_addr);
args.extend(call_args.iter().cloned());

Ok(pos
Expand Down
17 changes: 13 additions & 4 deletions lib/llvm-backend/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,13 @@ impl Intrinsics {
context.struct_type(&[i8_ptr_ty_basic, i64_ty_basic, i8_ptr_ty_basic], false);
let local_table_ty = local_memory_ty;
let local_global_ty = i64_ty;
let imported_func_ty =
context.struct_type(&[i8_ptr_ty_basic, ctx_ptr_ty.as_basic_type_enum()], false);
let func_ctx_ty =
context.struct_type(&[ctx_ptr_ty.as_basic_type_enum(), i8_ptr_ty_basic], false);
let func_ctx_ptr_ty = func_ctx_ty.ptr_type(AddressSpace::Generic);
let imported_func_ty = context.struct_type(
&[i8_ptr_ty_basic, func_ctx_ptr_ty.as_basic_type_enum()],
false,
);
let sigindex_ty = i32_ty;
let rt_intrinsics_ty = i8_ty;
let stack_lower_bound_ty = i8_ty;
Expand Down Expand Up @@ -1066,16 +1071,20 @@ impl<'a> CtxType<'a> {
"imported_func_ptr",
)
};
let (func_ptr_ptr, ctx_ptr_ptr) = unsafe {
let (func_ptr_ptr, func_ctx_ptr_ptr) = unsafe {
(
cache_builder.build_struct_gep(imported_func_ptr, 0, "func_ptr_ptr"),
cache_builder.build_struct_gep(imported_func_ptr, 1, "ctx_ptr_ptr"),
cache_builder.build_struct_gep(imported_func_ptr, 1, "func_ctx_ptr_ptr"),
)
};

let func_ptr = cache_builder
.build_load(func_ptr_ptr, "func_ptr")
.into_pointer_value();
let func_ctx_ptr = cache_builder
.build_load(func_ctx_ptr_ptr, "func_ctx_ptr")
.into_pointer_value();
let ctx_ptr_ptr = unsafe { cache_builder.build_struct_gep(func_ctx_ptr, 0, "ctx_ptr") };
let ctx_ptr = cache_builder
.build_load(ctx_ptr_ptr, "ctx_ptr")
.into_pointer_value();
Expand Down
2 changes: 1 addition & 1 deletion lib/llvm-backend/src/stackmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl StackmapEntry {
ValueSemantic::ImportedFuncCtx(idx) => MachineValue::VmctxDeref(vec![
Ctx::offset_imported_funcs() as usize,
vm::ImportedFunc::size() as usize * idx
+ vm::ImportedFunc::offset_vmctx() as usize,
+ vm::ImportedFunc::offset_func_ctx() as usize,
0,
]),
ValueSemantic::DynamicSigindice(idx) => {
Expand Down
Loading

0 comments on commit 365bcd6

Please sign in to comment.