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

refactor(sandbox): Update WASMI in gear-sandbox #2931

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 37 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gsdk/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async fn test_calculate_handle_gas() -> Result<()> {
)
.await?;

assert!(signer.api().gprog(pid).await.is_ok());
signer.api().gprog(pid).await.unwrap();

// 2. calculate handle gas and send message.
let gas_info = signer
Expand Down
2 changes: 1 addition & 1 deletion sandbox/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ environmental = "1.1.3"
thiserror.workspace = true
log = { workspace = true, features = ["std"] }
wasmer = { version = "2.2", features = ["singlepass"] }
wasmi = { git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext", features = ["virtual_memory"] }
wasmi = { git = "https://github.com/gear-tech/wasmi", branch = "al-modify-globals", features = ["virtual_memory"] }
sp-allocator = { workspace = true, features = ["std"] }
sp-wasm-interface = { workspace = true, features = ["std"] }
gear-sandbox-env = { workspace = true, features = ["std"] }
Expand Down
2 changes: 0 additions & 2 deletions sandbox/host/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ pub enum Error {
AbortedDueToTrap(MessageWithBacktrace),
}

impl wasmi::HostError for Error {}

impl From<&'static str> for Error {
fn from(err: &'static str) -> Error {
Error::Other(err.into())
Expand Down
66 changes: 35 additions & 31 deletions sandbox/host/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
mod wasmer_backend;
mod wasmi_backend;

use std::{collections::HashMap, pin::Pin, rc::Rc};
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap},
pin::Pin,
rc::Rc,
};

use codec::Decode;
use gear_sandbox_env as sandbox_env;
Expand All @@ -42,7 +47,7 @@ use self::{
},
wasmi_backend::{
get_global as wasmi_get_global, instantiate as wasmi_instantiate, invoke as wasmi_invoke,
new_memory as wasmi_new_memory, set_global as wasmi_set_global,
new_memory as wasmi_new_memory, set_global as wasmi_set_global, Backend as WasmiBackend,
MemoryWrapper as WasmiMemoryWrapper,
},
};
Expand Down Expand Up @@ -171,19 +176,15 @@ pub trait SandboxContext {
fn deallocate_memory(&mut self, ptr: Pointer<u8>) -> sp_wasm_interface::Result<()>;
}

/// Implementation of [`Externals`] that allows execution of guest module with
/// [externals][`Externals`] that might refer functions defined by supervisor.
///
/// [`Externals`]: ../wasmi/trait.Externals.html
pub struct GuestExternals<'a> {
/// Instance of sandboxed module to be dispatched
sandbox_instance: &'a SandboxInstance,
}

/// Module instance in terms of selected backend
enum BackendInstance {
/// Wasmi module instance
Wasmi(wasmi::ModuleRef),
Wasmi {
instance: wasmi::Instance,
store: Rc<RefCell<wasmi::Store<()>>>,
exports: BTreeMap<String, wasmi::Extern>,
globals: wasmi::Globals,
},

/// Wasmer module instance
Wasmer(wasmer::Instance),
Expand All @@ -205,7 +206,6 @@ enum BackendInstance {
/// [`invoke`]: #method.invoke
pub struct SandboxInstance {
backend_instance: BackendInstance,
guest_to_supervisor_mapping: GuestToSupervisorFunctionMapping,
}

impl SandboxInstance {
Expand All @@ -220,9 +220,9 @@ impl SandboxInstance {
sandbox_context: &mut dyn SandboxContext,
) -> std::result::Result<Option<sp_wasm_interface::Value>, error::Error> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => {
wasmi_invoke(self, wasmi_instance, export_name, args, sandbox_context)
}
BackendInstance::Wasmi {
instance, store, ..
} => wasmi_invoke(instance, store.clone(), export_name, args, sandbox_context),

BackendInstance::Wasmer(wasmer_instance) => {
wasmer_invoke(wasmer_instance, export_name, args, sandbox_context)
Expand All @@ -235,7 +235,9 @@ impl SandboxInstance {
/// Returns `Some(_)` if the global could be found.
pub fn get_global_val(&self, name: &str) -> Option<sp_wasm_interface::Value> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => wasmi_get_global(wasmi_instance, name),
BackendInstance::Wasmi {
exports, globals, ..
} => wasmi_get_global(exports, globals, name),

BackendInstance::Wasmer(wasmer_instance) => wasmer_get_global(wasmer_instance, name),
}
Expand All @@ -250,7 +252,9 @@ impl SandboxInstance {
value: sp_wasm_interface::Value,
) -> std::result::Result<Option<()>, error::Error> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => wasmi_set_global(wasmi_instance, name, value),
BackendInstance::Wasmi {
exports, globals, ..
} => wasmi_set_global(exports, globals, name, value),

BackendInstance::Wasmer(wasmer_instance) => {
wasmer_set_global(wasmer_instance, name, value)
Expand Down Expand Up @@ -452,7 +456,7 @@ impl util::MemoryTransfer for Memory {
/// Information specific to a particular execution backend
enum BackendContext {
/// Wasmi specific context
Wasmi,
Wasmi(WasmiBackend),

/// Wasmer specific context
Wasmer(WasmerBackend),
Expand All @@ -461,7 +465,7 @@ enum BackendContext {
impl BackendContext {
pub fn new(backend: SandboxBackend) -> BackendContext {
match backend {
SandboxBackend::Wasmi => BackendContext::Wasmi,
SandboxBackend::Wasmi => BackendContext::Wasmi(WasmiBackend::new()),

SandboxBackend::Wasmer => BackendContext::Wasmer(WasmerBackend::new()),
}
Expand Down Expand Up @@ -504,11 +508,9 @@ impl<DT: Clone> Store<DT> {
);
self.memories.clear();

match self.backend_context {
BackendContext::Wasmi => (),
BackendContext::Wasmer(_) => {
self.backend_context = BackendContext::Wasmer(WasmerBackend::new());
}
self.backend_context = match self.backend_context {
BackendContext::Wasmi(_) => BackendContext::Wasmi(WasmiBackend::new()),
BackendContext::Wasmer(_) => BackendContext::Wasmer(WasmerBackend::new()),
}
}

Expand All @@ -520,15 +522,15 @@ impl<DT: Clone> Store<DT> {
/// Typically happens if `initial` is more than `maximum`.
pub fn new_memory(&mut self, initial: u32, maximum: u32) -> Result<u32> {
let memories = &mut self.memories;
let backend_context = &self.backend_context;
let backend_context = &mut self.backend_context;

let maximum = match maximum {
sandbox_env::MEM_UNLIMITED => None,
specified_limit => Some(specified_limit),
};

let memory = match &backend_context {
BackendContext::Wasmi => wasmi_new_memory(initial, maximum)?,
let memory = match backend_context {
BackendContext::Wasmi(context) => wasmi_new_memory(context, initial, maximum)?,

BackendContext::Wasmer(context) => wasmer_new_memory(context, initial, maximum)?,
};
Expand Down Expand Up @@ -633,10 +635,12 @@ impl<DT: Clone> Store<DT> {
guest_env: GuestEnvironment,
sandbox_context: &mut dyn SandboxContext,
) -> std::result::Result<UnregisteredInstance, InstantiationError> {
let sandbox_instance = match self.backend_context {
BackendContext::Wasmi => wasmi_instantiate(wasm, guest_env, sandbox_context)?,
let sandbox_instance = match &mut self.backend_context {
BackendContext::Wasmi(context) => {
wasmi_instantiate(context, wasm, guest_env, sandbox_context)?
}

BackendContext::Wasmer(ref context) => {
BackendContext::Wasmer(context) => {
wasmer_instantiate(context, wasm, guest_env, sandbox_context)?
}
};
Expand Down
1 change: 0 additions & 1 deletion sandbox/host/src/sandbox/wasmer_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ pub fn instantiate(

Ok(SandboxInstance {
backend_instance: BackendInstance::Wasmer(instance),
guest_to_supervisor_mapping: guest_env.guest_to_supervisor_mapping,
})
}

Expand Down
Loading
Loading