Skip to content
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

fix(runtime-core) Avoid crashing when missing host functions are allowed #1128

Merged
merged 6 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 16 additions & 8 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ use crate::{
sig_registry::SigRegistry,
structures::{BoxedMap, Map, SliceMap, TypedIndex},
table::Table,
typed_func::{always_trap, Func},
types::{
ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex,
Initializer, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, LocalOrImport,
LocalTableIndex, SigIndex, Value,
},
vm,
};
use std::{
fmt::Debug,
ptr::{self, NonNull},
slice,
};
use std::{fmt::Debug, ptr::NonNull, slice};

/// Size of the array for internal instance usage
pub const INTERNALS_SIZE: usize = 256;
Expand Down Expand Up @@ -563,7 +560,9 @@ impl Drop for ImportBacking {
fn drop(&mut self) {
// Properly drop the `vm::FuncCtx` in `vm::ImportedFunc`.
for (_imported_func_index, imported_func) in (*self.vm_functions).iter_mut() {
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.func_ctx.as_ptr()) };
if !imported_func.func_ctx.as_ptr().is_null() {
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.func_ctx.as_ptr()) };
}
}
}
}
Expand Down Expand Up @@ -650,9 +649,18 @@ fn import_functions(
}
None => {
if imports.allow_missing_functions {
let always_trap = Func::new(always_trap);

functions.push(vm::ImportedFunc {
func: ptr::null(),
func_ctx: unsafe { NonNull::new_unchecked(ptr::null_mut()) }, // TODO: Non-sense…
func: always_trap.get_vm_func().as_ptr(),
func_ctx: NonNull::new(Box::into_raw(Box::new(vm::FuncCtx {
// ^^^^^^^^ `vm::FuncCtx` is purposely leaked.
// It is dropped by the specific `Drop`
// implementation of `ImportBacking`.
vmctx: NonNull::new(vmctx).expect("`vmctx` must not be null."),
func_env: None,
})))
.unwrap(),
});
} else {
link_errors.push(LinkError::ImportNotFound {
Expand Down
16 changes: 11 additions & 5 deletions lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ where
_phantom: PhantomData,
}
}

/// Get the underlying func pointer.
pub fn get_vm_func(&self) -> NonNull<vm::Func> {
self.func
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?

Copy link
Contributor Author

@Hywan Hywan Jan 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nop. It was implemented for Func<Kind = Wasm, …> only, now it's implemented for Kind = Wasm and Kind = Host.

}

impl<'a, Args, Rets> Func<'a, Args, Rets, Host>
Expand Down Expand Up @@ -303,6 +298,11 @@ where
pub fn returns(&self) -> &'static [Type] {
Rets::types()
}

/// Get the underlying func pointer.
pub fn get_vm_func(&self) -> NonNull<vm::Func> {
self.func
}
}

impl WasmTypeList for Infallible {
Expand Down Expand Up @@ -733,6 +733,12 @@ where
}
}

/// Function that always fails. It can be used as a placeholder when a
/// host function is missing for instance.
pub(crate) fn always_trap() -> Result<(), &'static str> {
Err("not implemented")
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime/tests/error_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ fn error_propagation() {

let instance = module
.instantiate(&imports! {
"env" => {
"ret_err" => Func::new(ret_err),
},
"env" => {
"ret_err" => Func::new(ret_err),
},
})
.unwrap();

Expand Down