-
Notifications
You must be signed in to change notification settings - Fork 824
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 cyclic ref in WasmerEnv #2327
Changes from 9 commits
c3d344e
a336996
dea2abb
d3c58eb
fe8d077
f0ad0be
b319e04
9b216cc
229d0dc
729a1cb
6c33341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ use wasmer_vm::{ | |
/// with native functions. Attempting to create a native `Function` with one will | ||
/// result in a panic. | ||
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840) | ||
#[derive(Clone, PartialEq, MemoryUsage)] | ||
#[derive(PartialEq, MemoryUsage)] | ||
pub struct Function { | ||
pub(crate) store: Store, | ||
pub(crate) exported: ExportFunction, | ||
|
@@ -712,6 +712,18 @@ impl Function { | |
fn closures_unsupported_panic() -> ! { | ||
unimplemented!("Closures (functions with captured environments) are currently unsupported with native functions. See: https://github.com/wasmerio/wasmer/issues/1840") | ||
} | ||
|
||
/// Get access to the backing VM value for this extern. This function is for | ||
/// tests it should not be called by users of the Wasmer API. | ||
/// | ||
/// # Safety | ||
/// This function is unsafe to call outside of tests for the wasmer crate | ||
/// because there is no stability guarantee for the returned type and we may | ||
/// make breaking changes to it at any time or remove this method. | ||
#[doc(hidden)] | ||
pub unsafe fn get_vm_function(&self) -> &VMFunction { | ||
&self.exported.vm_function | ||
} | ||
} | ||
|
||
impl<'a> Exportable<'a> for Function { | ||
|
@@ -725,6 +737,26 @@ impl<'a> Exportable<'a> for Function { | |
_ => Err(ExportError::IncompatibleType), | ||
} | ||
} | ||
|
||
fn into_weak_instance_ref(&mut self) { | ||
self.exported | ||
.vm_function | ||
.instance_ref | ||
.as_mut() | ||
.map(|v| *v = v.downgrade()); | ||
} | ||
} | ||
|
||
impl Clone for Function { | ||
fn clone(&self) -> Self { | ||
let mut exported = self.exported.clone(); | ||
exported.vm_function.upgrade_instance_ref().unwrap(); | ||
|
||
Self { | ||
store: self.store.clone(), | ||
exported, | ||
} | ||
} | ||
} | ||
Comment on lines
+750
to
760
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this |
||
|
||
impl fmt::Debug for Function { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,15 @@ impl<'a> Exportable<'a> for Extern { | |
// Since this is already an extern, we can just return it. | ||
Ok(_extern) | ||
} | ||
|
||
fn into_weak_instance_ref(&mut self) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super fan of this function name. I think we can just have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the reason for the extra precision is due to overlap in ownership. It's not really a weak version of the thing it's being called on. That thing can still keep things alive. The only thing being made weak here is an internal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this function again, we probably need to make it unsafe because you can probably get use after free with it. I'll investigate this with a test and maybe update it |
||
match self { | ||
Self::Function(f) => f.into_weak_instance_ref(), | ||
Self::Global(g) => g.into_weak_instance_ref(), | ||
Self::Memory(m) => m.into_weak_instance_ref(), | ||
Self::Table(t) => t.into_weak_instance_ref(), | ||
} | ||
} | ||
} | ||
|
||
impl StoreObject for Extern { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on the
unsafe
to discourage usage