mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-12 05:18:43 +00:00
Ensure that Wasm function call parameters all come from the same context
This commit is contained in:
committed by
Manos Pitsidianakis
parent
738a66f719
commit
35d06ec3f2
25
lib/api/src/sys/externals/function.rs
vendored
25
lib/api/src/sys/externals/function.rs
vendored
@@ -269,6 +269,11 @@ impl Function {
|
||||
param_types, &signature,
|
||||
)));
|
||||
}
|
||||
if !arg.is_from_context(ctx.as_context_ref()) {
|
||||
return Err(RuntimeError::new(
|
||||
"cross-`Context` values are not supported",
|
||||
));
|
||||
}
|
||||
*slot = arg.as_raw(ctx.as_context_mut());
|
||||
}
|
||||
|
||||
@@ -618,7 +623,7 @@ mod inner {
|
||||
|
||||
use crate::sys::context::{ContextInner, ContextMut};
|
||||
use crate::sys::NativeWasmTypeInto;
|
||||
use crate::{AsContextMut, ExternRef, Function};
|
||||
use crate::{AsContextMut, AsContextRef, ExternRef, Function};
|
||||
|
||||
/// A trait to convert a Rust value to a `WasmNativeType` value,
|
||||
/// or to convert `WasmNativeType` value to a Rust value.
|
||||
@@ -653,6 +658,14 @@ mod inner {
|
||||
/// This method panics if `self` cannot fit in the
|
||||
/// `Self::Native` type.
|
||||
fn to_native(self) -> Self::Native;
|
||||
|
||||
/// Returns whether the given value is from the given context.
|
||||
///
|
||||
/// This always returns true for primitive types that can be used with
|
||||
/// any context.
|
||||
fn is_from_context(&self, _ctx: impl AsContextRef) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! from_to_native_wasm_type {
|
||||
@@ -722,6 +735,11 @@ mod inner {
|
||||
fn from_native(n: Self::Native) -> Self {
|
||||
n
|
||||
}
|
||||
fn is_from_context(&self, ctx: impl AsContextRef) -> bool {
|
||||
self.as_ref()
|
||||
.map(|e| e.is_from_context(ctx))
|
||||
.unwrap_or(true)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FromToNativeWasmType for Option<Function> {
|
||||
@@ -733,6 +751,11 @@ mod inner {
|
||||
fn from_native(n: Self::Native) -> Self {
|
||||
n
|
||||
}
|
||||
fn is_from_context(&self, ctx: impl AsContextRef) -> bool {
|
||||
self.as_ref()
|
||||
.map(|f| f.is_from_context(ctx))
|
||||
.unwrap_or(true)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -75,6 +75,12 @@ macro_rules! impl_native_traits {
|
||||
.as_ptr()
|
||||
.as_ref()
|
||||
};
|
||||
// Ensure all parameters come from the same context.
|
||||
if $(!FromToNativeWasmType::is_from_context(&$x, ctx.as_context_ref()) ||)* false {
|
||||
return Err(RuntimeError::new(
|
||||
"cross-`Context` values are not supported",
|
||||
));
|
||||
}
|
||||
// TODO: when `const fn` related features mature more, we can declare a single array
|
||||
// of the correct size here.
|
||||
let mut params_list = [ $( $x.to_native().into_raw(ctx.as_context_mut()) ),* ];
|
||||
|
||||
Reference in New Issue
Block a user