From 286775ab33e643db2f205b273bd4e35929b1c2b5 Mon Sep 17 00:00:00 2001 From: Dmitry Kashitsyn Date: Thu, 21 Apr 2022 15:06:04 +0600 Subject: [PATCH] Split `SandboxInstance::get_global_val` implementation per backend (#11234) --- client/executor/common/src/sandbox.rs | 30 +++++-------------- .../common/src/sandbox/wasmer_backend.rs | 14 +++++++++ .../common/src/sandbox/wasmi_backend.rs | 5 ++++ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/client/executor/common/src/sandbox.rs b/client/executor/common/src/sandbox.rs index a2c1f602b1c9a..98eafaf32b8f9 100644 --- a/client/executor/common/src/sandbox.rs +++ b/client/executor/common/src/sandbox.rs @@ -36,13 +36,14 @@ use std::{collections::HashMap, rc::Rc}; #[cfg(feature = "wasmer-sandbox")] use wasmer_backend::{ - instantiate as wasmer_instantiate, invoke as wasmer_invoke, new_memory as wasmer_new_memory, - Backend as WasmerBackend, MemoryWrapper as WasmerMemoryWrapper, + get_global as wasmer_get_global, instantiate as wasmer_instantiate, invoke as wasmer_invoke, + new_memory as wasmer_new_memory, Backend as WasmerBackend, + MemoryWrapper as WasmerMemoryWrapper, }; use wasmi_backend::{ - instantiate as wasmi_instantiate, invoke as wasmi_invoke, new_memory as wasmi_new_memory, - MemoryWrapper as WasmiMemoryWrapper, + get_global as wasmi_get_global, instantiate as wasmi_instantiate, invoke as wasmi_invoke, + new_memory as wasmi_new_memory, MemoryWrapper as WasmiMemoryWrapper, }; /// Index of a function inside the supervisor. @@ -213,27 +214,10 @@ impl SandboxInstance { /// Returns `Some(_)` if the global could be found. pub fn get_global_val(&self, name: &str) -> Option { match &self.backend_instance { - BackendInstance::Wasmi(wasmi_instance) => { - let wasmi_global = wasmi_instance.export_by_name(name)?.as_global()?.get(); - - Some(wasmi_global.into()) - }, + BackendInstance::Wasmi(wasmi_instance) => wasmi_get_global(wasmi_instance, name), #[cfg(feature = "wasmer-sandbox")] - BackendInstance::Wasmer(wasmer_instance) => { - use sp_wasm_interface::Value; - - let global = wasmer_instance.exports.get_global(name).ok()?; - let wasmtime_value = match global.get() { - wasmer::Val::I32(val) => Value::I32(val), - wasmer::Val::I64(val) => Value::I64(val), - wasmer::Val::F32(val) => Value::F32(f32::to_bits(val)), - wasmer::Val::F64(val) => Value::F64(f64::to_bits(val)), - _ => None?, - }; - - Some(wasmtime_value) - }, + BackendInstance::Wasmer(wasmer_instance) => wasmer_get_global(wasmer_instance, name), } } } diff --git a/client/executor/common/src/sandbox/wasmer_backend.rs b/client/executor/common/src/sandbox/wasmer_backend.rs index 44b43757148de..fd2f317457f88 100644 --- a/client/executor/common/src/sandbox/wasmer_backend.rs +++ b/client/executor/common/src/sandbox/wasmer_backend.rs @@ -432,3 +432,17 @@ impl MemoryTransfer for MemoryWrapper { } } } + +/// Get global value by name +pub fn get_global(instance: &wasmer::Instance, name: &str) -> Option { + let global = instance.exports.get_global(name).ok()?; + let wasmtime_value = match global.get() { + wasmer::Val::I32(val) => Value::I32(val), + wasmer::Val::I64(val) => Value::I64(val), + wasmer::Val::F32(val) => Value::F32(f32::to_bits(val)), + wasmer::Val::F64(val) => Value::F64(f64::to_bits(val)), + _ => None?, + }; + + Some(wasmtime_value) +} diff --git a/client/executor/common/src/sandbox/wasmi_backend.rs b/client/executor/common/src/sandbox/wasmi_backend.rs index 92bb0e1e398e0..0954287f52f6b 100644 --- a/client/executor/common/src/sandbox/wasmi_backend.rs +++ b/client/executor/common/src/sandbox/wasmi_backend.rs @@ -321,3 +321,8 @@ pub fn invoke( }) }) } + +/// Get global value by name +pub fn get_global(instance: &wasmi::ModuleRef, name: &str) -> Option { + Some(instance.export_by_name(name)?.as_global()?.get().into()) +}