Skip to content

Commit

Permalink
Try #1837:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Nov 27, 2020
2 parents 5847800 + 7621991 commit d214b9d
Show file tree
Hide file tree
Showing 16 changed files with 621 additions and 298 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Added

- [#1837](https://github.com/wasmerio/wasmer/pull/1837) It is now possible to use exports of an `Intance` even after the `Instance` has been freed
- [#1831](https://github.com/wasmerio/wasmer/pull/1831) Added support for Apple Silicon chips (`arm64-apple-darwin`)
- [#1649](https://github.com/wasmerio/wasmer/pull/1649) Add outline of migration to 1.0.0 docs.

Expand Down
7 changes: 6 additions & 1 deletion lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Function {
vmctx,
signature: ty.clone(),
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -152,6 +153,7 @@ impl Function {
vmctx,
signature: ty.clone(),
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -196,6 +198,7 @@ impl Function {
signature,
kind: VMFunctionKind::Static,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -252,6 +255,7 @@ impl Function {
vmctx,
signature,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -293,6 +297,7 @@ impl Function {
vmctx,
signature,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -474,7 +479,7 @@ impl Function {
Ok(results.into_boxed_slice())
}

pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self {
pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportFunction) -> Self {
if let Some(trampoline) = wasmer_export.call_trampoline {
Self {
store: store.clone(),
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Global {
Ok(())
}

pub(crate) fn from_export(store: &Store, wasmer_export: ExportGlobal) -> Self {
pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportGlobal) -> Self {
Self {
store: store.clone(),
global: wasmer_export.from,
Expand Down Expand Up @@ -218,6 +218,7 @@ impl<'a> Exportable<'a> for Global {
fn to_export(&self) -> Export {
ExportGlobal {
from: self.global.clone(),
instance_allocator: None,
}
.into()
}
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Memory {
unsafe { MemoryView::new(base as _, length as u32) }
}

pub(crate) fn from_export(store: &Store, wasmer_export: ExportMemory) -> Self {
pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportMemory) -> Self {
Self {
store: store.clone(),
memory: wasmer_export.from,
Expand Down Expand Up @@ -248,6 +248,7 @@ impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
ExportMemory {
from: self.memory.clone(),
instance_allocator: None,
}
.into()
}
Expand Down
12 changes: 6 additions & 6 deletions lib/api/src/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl Extern {
}
}

/// Create an `Extern` from an `Export`.
pub fn from_export(store: &Store, export: Export) -> Self {
/// Create an `Extern` from an `wasmer_vm::Export`.
pub fn from_vm_export(store: &Store, export: Export) -> Self {
match export {
Export::Function(f) => Self::Function(Function::from_export(store, f)),
Export::Memory(m) => Self::Memory(Memory::from_export(store, m)),
Export::Global(g) => Self::Global(Global::from_export(store, g)),
Export::Table(t) => Self::Table(Table::from_export(store, t)),
Export::Function(f) => Self::Function(Function::from_vm_export(store, f)),
Export::Memory(m) => Self::Memory(Memory::from_vm_export(store, m)),
Export::Global(g) => Self::Global(Global::from_vm_export(store, g)),
Export::Table(t) => Self::Table(Table::from_vm_export(store, t)),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Table {
Ok(())
}

pub(crate) fn from_export(store: &Store, wasmer_export: ExportTable) -> Self {
pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportTable) -> Self {
Self {
store: store.clone(),
table: wasmer_export.from,
Expand All @@ -156,6 +156,7 @@ impl<'a> Exportable<'a> for Table {
fn to_export(&self) -> Export {
ExportTable {
from: self.table.clone(),
instance_allocator: None,
}
.into()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Instance {
.map(|export| {
let name = export.name().to_string();
let export = handle.lookup(&name).expect("export");
let extern_ = Extern::from_export(store, export);
let extern_ = Extern::from_vm_export(store, export);
(name, extern_)
})
.collect::<Exports>();
Expand Down
2 changes: 2 additions & 0 deletions lib/api/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ where
signature,
kind: other.arg_kind,
call_trampoline: None,
instance_allocator: None,
}
}
}
Expand All @@ -90,6 +91,7 @@ where
signature,
kind: other.arg_kind,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ impl ValFuncRef for Val {
kind: wasmer_vm::VMFunctionKind::Static,
vmctx: item.vmctx,
call_trampoline: None,
instance_allocator: None,
};
let f = Function::from_export(store, export);
let f = Function::from_vm_export(store, export);
Self::FuncRef(f)
}
}
39 changes: 39 additions & 0 deletions lib/api/tests/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anyhow::Result;
use wasmer::*;

#[test]
fn exports_work_after_multiple_instances_have_been_freed() -> Result<()> {
let store = Store::default();
let module = Module::new(
&store,
"
(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add)
(export \"sum\" (func $sum_f)))
",
)?;

let import_object = ImportObject::new();
let instance = Instance::new(&module, &import_object)?;
let instance2 = instance.clone();
let instance3 = instance.clone();

// The function is cloned to “break” the connection with `instance`.
let sum = instance.exports.get_function("sum")?.clone();

drop(instance);
drop(instance2);
drop(instance3);

// All instances have been dropped, but `sum` continues to work!
assert_eq!(
sum.call(&[Value::I32(1), Value::I32(2)])?.into_vec(),
vec![Value::I32(3)],
);

Ok(())
}
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ unsafe fn wasi_get_imports_inner(
import_type.name()
),
}));
let inner = Extern::from_export(store, export);
let inner = Extern::from_vm_export(store, export);

Some(Box::new(wasm_extern_t {
instance: None,
Expand Down
4 changes: 2 additions & 2 deletions lib/deprecated/runtime-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ impl Module {

(
(namespace, name),
new::wasmer::Extern::from_export(store, Export::Function(function)),
new::wasmer::Extern::from_vm_export(store, Export::Function(function)),
)
}
export => (
(namespace, name),
new::wasmer::Extern::from_export(store, export),
new::wasmer::Extern::from_vm_export(store, export),
),
})
.for_each(|((namespace, name), extern_)| {
Expand Down
34 changes: 31 additions & 3 deletions lib/vm/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md

use crate::global::Global;
use crate::instance::InstanceAllocator;
use crate::memory::{Memory, MemoryStyle};
use crate::table::{Table, TableStyle};
use crate::vmcontext::{VMFunctionBody, VMFunctionEnvironment, VMFunctionKind, VMTrampoline};
Expand Down Expand Up @@ -29,15 +30,27 @@ pub enum Export {
pub struct ExportFunction {
/// The address of the native-code function.
pub address: *const VMFunctionBody,

/// Pointer to the containing `VMContext`.
pub vmctx: VMFunctionEnvironment,

/// The function type, used for compatibility checking.
pub signature: FunctionType,
/// The function kind (specifies the calling convention for the function).

/// The function kind (specifies the calling convention for the
/// function).
pub kind: VMFunctionKind,
/// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody.
/// May be None when the function is a host-function (FunctionType == Dynamic or vmctx == nullptr).

/// Address of the function call trampoline owned by the same
/// VMContext that owns the VMFunctionBody.
///
/// May be `None` when the function is a host function (`FunctionType`
/// == `Dynamic` or `vmctx` == `nullptr`).
pub call_trampoline: Option<VMTrampoline>,

/// A “reference” to the instance through the
/// `InstanceAllocator`. `None` if it is a host function.
pub instance_allocator: Option<InstanceAllocator>,
}

/// # Safety
Expand All @@ -59,13 +72,18 @@ impl From<ExportFunction> for Export {
pub struct ExportTable {
/// Pointer to the containing `Table`.
pub from: Arc<dyn Table>,

/// A “reference” to the instance through the
/// `InstanceAllocator`. `None` if it is a host function.
pub instance_allocator: Option<InstanceAllocator>,
}

/// # Safety
/// This is correct because there is no non-threadsafe logic directly in this type;
/// correct use of the raw table from multiple threads via `definition` requires `unsafe`
/// and is the responsibilty of the user of this type.
unsafe impl Send for ExportTable {}

/// # Safety
/// This is correct because the values directly in `definition` should be considered immutable
/// and the type is both `Send` and `Clone` (thus marking it `Sync` adds no new behavior, it
Expand Down Expand Up @@ -100,13 +118,18 @@ impl From<ExportTable> for Export {
pub struct ExportMemory {
/// Pointer to the containing `Memory`.
pub from: Arc<dyn Memory>,

/// A “reference” to the instance through the
/// `InstanceAllocator`. `None` if it is a host function.
pub instance_allocator: Option<InstanceAllocator>,
}

/// # Safety
/// This is correct because there is no non-threadsafe logic directly in this type;
/// correct use of the raw memory from multiple threads via `definition` requires `unsafe`
/// and is the responsibilty of the user of this type.
unsafe impl Send for ExportMemory {}

/// # Safety
/// This is correct because the values directly in `definition` should be considered immutable
/// and the type is both `Send` and `Clone` (thus marking it `Sync` adds no new behavior, it
Expand Down Expand Up @@ -141,13 +164,18 @@ impl From<ExportMemory> for Export {
pub struct ExportGlobal {
/// The global declaration, used for compatibility checking.
pub from: Arc<Global>,

/// A “reference” to the instance through the
/// `InstanceAllocator`. `None` if it is a host function.
pub instance_allocator: Option<InstanceAllocator>,
}

/// # Safety
/// This is correct because there is no non-threadsafe logic directly in this type;
/// correct use of the raw global from multiple threads via `definition` requires `unsafe`
/// and is the responsibilty of the user of this type.
unsafe impl Send for ExportGlobal {}

/// # Safety
/// This is correct because the values directly in `definition` should be considered immutable
/// from the perspective of users of this type and the type is both `Send` and `Clone` (thus
Expand Down
Loading

0 comments on commit d214b9d

Please sign in to comment.