* Fix a possible use-after-free with `Global` This commit fixes an issue with the implementation of the `wasmtime::Global` type where if it previously outlived the original `Instance` it came from then you could run into a use-after-free. Now the `Global` type holds onto its underlying `InstanceHandle` to ensure it retains ownership of the underlying backing store of the global's memory. * rustfmt
70 lines
2.3 KiB
Rust
70 lines
2.3 KiB
Rust
//! Utility module to create trampolines in/out WebAssembly module.
|
|
|
|
mod create_handle;
|
|
mod func;
|
|
mod global;
|
|
mod memory;
|
|
mod table;
|
|
|
|
use self::func::create_handle_with_function;
|
|
use self::global::create_global;
|
|
use self::memory::create_handle_with_memory;
|
|
use self::table::create_handle_with_table;
|
|
use super::{Callable, FuncType, GlobalType, MemoryType, Store, TableType, Val};
|
|
use anyhow::Result;
|
|
use std::any::Any;
|
|
use std::rc::Rc;
|
|
use wasmtime_runtime::VMFunctionBody;
|
|
|
|
pub fn generate_func_export(
|
|
ft: &FuncType,
|
|
func: &Rc<dyn Callable + 'static>,
|
|
store: &Store,
|
|
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
|
let instance = create_handle_with_function(ft, func, store)?;
|
|
let export = instance.lookup("trampoline").expect("trampoline export");
|
|
Ok((instance, export))
|
|
}
|
|
|
|
/// Note that this is `unsafe` since `func` must be a valid function pointer and
|
|
/// have a signature which matches `ft`, otherwise the returned
|
|
/// instance/export/etc may exhibit undefined behavior.
|
|
pub unsafe fn generate_raw_func_export(
|
|
ft: &FuncType,
|
|
func: *mut [VMFunctionBody],
|
|
store: &Store,
|
|
state: Box<dyn Any>,
|
|
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
|
let instance = func::create_handle_with_raw_function(ft, func, store, state)?;
|
|
let export = instance.lookup("trampoline").expect("trampoline export");
|
|
Ok((instance, export))
|
|
}
|
|
|
|
pub fn generate_global_export(
|
|
store: &Store,
|
|
gt: &GlobalType,
|
|
val: Val,
|
|
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
|
let instance = create_global(store, gt, val)?;
|
|
let export = instance.lookup("global").expect("global export");
|
|
Ok((instance, export))
|
|
}
|
|
|
|
pub fn generate_memory_export(
|
|
store: &Store,
|
|
m: &MemoryType,
|
|
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
|
let instance = create_handle_with_memory(store, m)?;
|
|
let export = instance.lookup("memory").expect("memory export");
|
|
Ok((instance, export))
|
|
}
|
|
|
|
pub fn generate_table_export(
|
|
store: &Store,
|
|
t: &TableType,
|
|
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
|
let instance = create_handle_with_table(store, t)?;
|
|
let export = instance.lookup("table").expect("table export");
|
|
Ok((instance, export))
|
|
}
|