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

feat(vm) Ability to use exports after the instance has been freed #1837

Merged
merged 30 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ca89bd5
feat(api) Rename `Extern::from_export` to `…::from_vm_export`.
Hywan Nov 19, 2020
f338b68
feat(vm) Change visibility of some `Instance`'s methods to `pub(self)`.
Hywan Nov 19, 2020
955068c
feat(vm) Move `Instance::(lookup|lookup_by_declaration|exports)` meth…
Hywan Nov 19, 2020
b93af3b
start big patch
Hywan Nov 23, 2020
e77afea
big patch: Start injecting `InstanceAllocator` everywhere.
Hywan Nov 23, 2020
d7bcc21
big patch: add some traces.
Hywan Nov 23, 2020
d5c7b62
Merge branch 'master' into fix-vm-leak
Hywan Nov 23, 2020
b7cdb8f
doc(vm) Code polishing and write more documentation.
Hywan Nov 24, 2020
0a63e56
fix(vm) Avoid casting to `NonNull<u8>`.
Hywan Nov 24, 2020
f4d9deb
try something different
Hywan Nov 24, 2020
7f06d4f
polish and update documentation
Hywan Nov 24, 2020
f968b24
feat(vm) Remove `InstanceHandle::from_vmctx`.
Hywan Nov 26, 2020
4d073ea
feat(vm) Rename `InstanceAllocator::instance_ref` to `::as_ref`.
Hywan Nov 26, 2020
0e1ed94
fix(vm) Fix the pointer size when doing arithmetics.
Hywan Nov 26, 2020
26d20e0
feat(vm) Move `MAX_REFCOUNT` as an associated constant of `InstanceAl…
Hywan Nov 26, 2020
df0e30b
fix(vm) Fix a typo in a panic message.
Hywan Nov 26, 2020
cae4bc6
fix(api) Fix `new_native_with_unsafe_mutable_env`.
Hywan Nov 26, 2020
081d3e3
fix(vm) Remove debugging statements.
Hywan Nov 26, 2020
e94f994
doc(vm) Add more documentation.
Hywan Nov 26, 2020
70455a9
doc(vm) Give more details in the API documentation.
Hywan Nov 26, 2020
c649bec
doc(vm) Remove a useless part of the documentation.
Hywan Nov 26, 2020
8e17a4c
fixup
Hywan Nov 26, 2020
0ec3b70
feat(vm) Change how the layout of `Instance` is calculated.
Hywan Nov 26, 2020
5a00f26
feat(vm) More functions can be constant functions.
Hywan Nov 26, 2020
dc2f5cf
doc(changelog) Add #1837.
Hywan Nov 26, 2020
286426a
doc(vm) Update documentation of exports for the `instance_allocator` …
Hywan Nov 26, 2020
85ae8c7
Merge branch 'master' into fix-vm-leak
Hywan Nov 26, 2020
7621991
test(api) Test that exports work after the instance has been freed.
Hywan Nov 27, 2020
2f9da8b
feat(vm) Mark `InstanceAllocator::new` as unsafe.
Hywan Dec 1, 2020
7973a96
Merge branch 'master' into fix-vm-leak
Hywan Dec 1, 2020
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
12 changes: 11 additions & 1 deletion lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ pub struct Function {
pub(crate) exported: ExportFunction,
}

impl Drop for Function {
fn drop(&mut self) {
println!("Dropping `wasmer::Function`");
}
}

impl Function {
/// Creates a new host `Function` (dynamic) with the provided signature.
///
Expand Down Expand Up @@ -100,6 +106,7 @@ impl Function {
vmctx,
signature: ty.clone(),
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -152,6 +159,7 @@ impl Function {
vmctx,
signature: ty.clone(),
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -196,6 +204,7 @@ impl Function {
signature,
kind: VMFunctionKind::Static,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -252,6 +261,7 @@ impl Function {
vmctx,
signature,
call_trampoline: None,
instance_allocator: None,
},
}
}
Expand Down Expand Up @@ -474,7 +484,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
8 changes: 7 additions & 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 Expand Up @@ -115,3 +115,9 @@ impl fmt::Debug for Instance {
.finish()
}
}

impl Drop for Instance {
fn drop(&mut self) {
println!("Dropping `wasmer::Instance`");
}
}
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)
}
}
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
13 changes: 13 additions & 0 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 @@ -38,6 +39,9 @@ pub struct ExportFunction {
/// 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, to ensure it's not deallocated
/// before the function.
pub instance_allocator: Option<Arc<InstanceAllocator>>,
Hywan marked this conversation as resolved.
Show resolved Hide resolved
}

/// # Safety
Expand All @@ -59,6 +63,9 @@ impl From<ExportFunction> for Export {
pub struct ExportTable {
/// Pointer to the containing `Table`.
pub from: Arc<dyn Table>,
/// A reference to the instance, to ensure it's not deallocated
/// before the table.
pub instance_allocator: Option<Arc<InstanceAllocator>>,
}

/// # Safety
Expand Down Expand Up @@ -100,6 +107,9 @@ impl From<ExportTable> for Export {
pub struct ExportMemory {
/// Pointer to the containing `Memory`.
pub from: Arc<dyn Memory>,
/// A reference to the instance, to ensure it's not deallocated
/// before the memory.
pub instance_allocator: Option<Arc<InstanceAllocator>>,
}

/// # Safety
Expand Down Expand Up @@ -141,6 +151,9 @@ impl From<ExportMemory> for Export {
pub struct ExportGlobal {
/// The global declaration, used for compatibility checking.
pub from: Arc<Global>,
/// A reference to the instance, to ensure it's not deallocated
/// before the global.
pub instance_allocator: Option<Arc<InstanceAllocator>>,
}

/// # Safety
Expand Down
Loading