Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a6b9624
Statically register host WASM functions
koute Nov 30, 2021
1d8649f
Fix `substrate-test-client` compilation
koute Nov 30, 2021
2a4658f
Move `ExtendedHostFunctions` to `sp-wasm-interface`
koute Dec 2, 2021
878d512
Fix `sp-runtime-interface` tests' compilation
koute Dec 2, 2021
7efc4f5
Fix `sc-executor-wasmtime` tests' compilation
koute Dec 2, 2021
570788d
Use `runtime_interface` macro in `test-runner`
koute Dec 2, 2021
7610cde
Fix `sc-executor` tests' compilation
koute Dec 6, 2021
0c51a64
Reformatting/`rustfmt`
koute Dec 6, 2021
963f9e7
Add an extra comment regarding the `H` generic arg in `create_runtime`
koute Dec 6, 2021
76e8c96
Even more `rustfmt`
koute Dec 6, 2021
e853225
Depend on `wasmtime` without default features in `sp-wasm-interface`
koute Dec 6, 2021
df38788
Merge branch 'master' into master_statically_register_wasmtime_ffi
koute Dec 6, 2021
3699f6d
Merge branch 'master' into master_statically_register_wasmtime_ffi
koute Dec 9, 2021
cd14ed1
Merge branch 'master' into master_statically_register_wasmtime_ffi
koute Dec 10, 2021
f825113
Bump version of `sp-wasm-interface` to 4.0.1
koute Dec 10, 2021
8ffb42d
Bump `sp-wasm-interface` in `Cargo.lock` too
koute Dec 10, 2021
0f7ccf8
Bump all of the `sp-wasm-interface` requirements to 4.0.1
koute Dec 10, 2021
e429bb9
Revert "Bump all of the `sp-wasm-interface` requirements to 4.0.1"
koute Dec 10, 2021
9ce2fb7
Make `cargo-unleash` happy (maybe)
koute Dec 10, 2021
f9019cd
Use `cargo-unleash` to bump the crates' versions
koute Dec 13, 2021
acc2835
Merge branch 'master' into master_statically_register_wasmtime_ffi
koute Dec 13, 2021
61ce0b2
Align to review comments
koute Dec 13, 2021
bdd38da
Merge branch 'master' into master_statically_register_wasmtime_ffi
koute Dec 14, 2021
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
Prev Previous commit
Next Next commit
Reformatting/rustfmt
  • Loading branch information
koute committed Dec 6, 2021
commit 0c51a64d1d4e222eba7b41ca8c176fcd7e317eb3
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ fn generate_host_functions_struct(
}

#crate_::sp_wasm_interface::if_wasmtime_is_enabled! {
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error> where T: #crate_::sp_wasm_interface::HostFunctionRegistry {
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error>
where T: #crate_::sp_wasm_interface::HostFunctionRegistry
{
#(#register_bodies)*
Ok(())
}
Expand Down Expand Up @@ -235,7 +237,10 @@ fn generate_host_function_implementation(
ffi_args_prototype.push(quote! { #ffi_name: #ffi_ty });
ffi_names.push(quote! { #ffi_name });

let convert_arg_error = format!("could not marshal the '{}' argument through the WASM FFI boundary while executing '{}' from interface '{}'", host_name_ident, method.sig.ident, trait_name);
let convert_arg_error = format!(
"could not marshal the '{}' argument through the WASM FFI boundary while executing '{}' from interface '{}'",
host_name_ident, method.sig.ident, trait_name
);
convert_args_static_ffi_to_host.push(quote! {
let mut #host_name = <#host_ty as #crate_::host::FromFFIValue>::from_ffi_value(__function_context__, #ffi_name)
.map_err(|err| format!("{}: {}", err, #convert_arg_error))?;
Expand All @@ -256,10 +261,14 @@ fn generate_host_function_implementation(
});
}

let arg_count_mismatch_error = format!("number of arguments given to '{}' from interface '{}' does not match the expected number of arguments!", method.sig.ident, trait_name);
convert_args_dynamic_ffi_to_static_ffi.push(quote!{
let arg_count_mismatch_error = format!(
"number of arguments given to '{}' from interface '{}' does not match the expected number of arguments!",
method.sig.ident, trait_name
);
convert_args_dynamic_ffi_to_static_ffi.push(quote! {
let #ffi_name = args.next().ok_or_else(|| #arg_count_mismatch_error.to_owned())?;
let #ffi_name: #ffi_ty = #crate_::sp_wasm_interface::TryFromValue::try_from_value(#ffi_name).ok_or_else(|| #convert_arg_error.to_owned())?;
let #ffi_name: #ffi_ty = #crate_::sp_wasm_interface::TryFromValue::try_from_value(#ffi_name)
.ok_or_else(|| #convert_arg_error.to_owned())?;
});
}

Expand Down Expand Up @@ -342,7 +351,9 @@ fn generate_host_function_implementation(
let register_body = quote! {
registry.register_static(
#crate_::sp_wasm_interface::Function::name(&#struct_name),
|mut caller: #crate_::sp_wasm_interface::wasmtime::Caller<T::State>, #(#ffi_args_prototype),*| -> std::result::Result<#ffi_return_ty, #crate_::sp_wasm_interface::wasmtime::Trap> {
|mut caller: #crate_::sp_wasm_interface::wasmtime::Caller<T::State>, #(#ffi_args_prototype),*|
-> std::result::Result<#ffi_return_ty, #crate_::sp_wasm_interface::wasmtime::Trap>
{
T::with_function_context(caller, move |__function_context__| {
#struct_name::call(
__function_context__,
Expand Down
45 changes: 35 additions & 10 deletions primitives/wasm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,15 @@ if_wasmtime_is_enabled! {
type State;
type Error;
type FunctionContext: FunctionContext;
fn with_function_context<R>(caller: wasmtime::Caller<Self::State>, callback: impl FnOnce(&mut dyn FunctionContext) -> R) -> R;
fn register_static<Params, Results>(&mut self, fn_name: &str, func: impl wasmtime::IntoFunc<Self::State, Params, Results> + 'static) -> core::result::Result<(), Self::Error>;
fn with_function_context<R>(
caller: wasmtime::Caller<Self::State>,
callback: impl FnOnce(&mut dyn FunctionContext) -> R,
) -> R;
fn register_static<Params, Results>(
&mut self,
fn_name: &str,
func: impl wasmtime::IntoFunc<Self::State, Params, Results> + 'static,
) -> core::result::Result<(), Self::Error>;
}
}

Expand All @@ -374,7 +381,9 @@ pub trait HostFunctions: 'static + Send + Sync {

if_wasmtime_is_enabled! {
/// Statically registers the host functions.
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error> where T: HostFunctionRegistry;
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error>
where
T: HostFunctionRegistry;
}
}

Expand Down Expand Up @@ -423,22 +432,38 @@ where
}

if_wasmtime_is_enabled! {
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error> where T: HostFunctionRegistry {
struct Proxy<'a, T> where T: HostFunctionRegistry {
fn register_static<T>(registry: &mut T) -> core::result::Result<(), T::Error>
where
T: HostFunctionRegistry,
{
struct Proxy<'a, T>
where
T: HostFunctionRegistry,
{
registry: &'a mut T,
seen: std::collections::HashSet<String>
seen: std::collections::HashSet<String>,
}
impl<'a, T> HostFunctionRegistry for Proxy<'a, T> where T: HostFunctionRegistry {
impl<'a, T> HostFunctionRegistry for Proxy<'a, T>
where
T: HostFunctionRegistry,
{
type State = T::State;
type Error = T::Error;
type FunctionContext = T::FunctionContext;
fn with_function_context<R>(caller: wasmtime::Caller<Self::State>, callback: impl FnOnce(&mut dyn FunctionContext) -> R) -> R {
fn with_function_context<R>(
caller: wasmtime::Caller<Self::State>,
callback: impl FnOnce(&mut dyn FunctionContext) -> R,
) -> R {
T::with_function_context(caller, callback)
}

fn register_static<Params, Results>(&mut self, fn_name: &str, func: impl wasmtime::IntoFunc<Self::State, Params, Results> + 'static) -> core::result::Result<(), Self::Error> {
fn register_static<Params, Results>(
&mut self,
fn_name: &str,
func: impl wasmtime::IntoFunc<Self::State, Params, Results> + 'static,
) -> core::result::Result<(), Self::Error> {
if self.seen.contains(fn_name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is to prevent double registration?

Maybe some debug log would be nice that says that we filtered this one out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but only in a sense that since this is explicitly used to override the functions from the Base with the functions from the Overlay it is expected that there will be some duplicates.

Yeah I guess I can add a debug log here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've also added a little bit more strict checking here to disambiguate between duplicates in Base vs duplicates in Overlay.

return Ok(());
return Ok(())
}

self.seen.insert(fn_name.to_owned());
Expand Down