Skip to content

Commit

Permalink
feat(sandbox-host): update sandbox host wasmi version (#4282)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteNacked authored Nov 4, 2024
1 parent b8c21ea commit 6eab909
Show file tree
Hide file tree
Showing 23 changed files with 882 additions and 554 deletions.
83 changes: 81 additions & 2 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ ethexe-rpc = { path = "ethexe/rpc", default-features = false }
ethexe-common = { path = "ethexe/common" }

# Common executor between `sandbox-host` and `lazy-pages-fuzzer`
sandbox-wasmi = { package = "wasmi", git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext", features = [
"virtual_memory",
] }
wasmi = { package = "wasmi", version = "0.38"}

# Substrate deps
binary-merkle-tree = { version = "4.0.0-dev", git = "https://github.com/gear-tech/polkadot-sdk.git", branch = "gear-v1.4.0", default-features = false }
Expand Down Expand Up @@ -503,6 +501,7 @@ demo-wat = { path = "examples/wat" }
#
# TODO: remove these dependencies (from this file?) or add more docs.

atomic_enum = "0.3.0"
cfg-if = "1.0.0" # gear-lazy-pages
cargo-http-registry = "0.1.6" # crates-io
errno = "0.3" # gear-lazy-pages
Expand Down
1 change: 1 addition & 0 deletions node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ clap = { workspace = true, features = ["derive"] }
mimalloc = { workspace = true, default-features = false }
log = { workspace = true, features = ["std"] }
futures.workspace = true
derive_more.workspace = true

# Gear
runtime-primitives.workspace = true
Expand Down
27 changes: 27 additions & 0 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use clap::Parser;
use std::str::FromStr;

#[allow(missing_docs)]
#[derive(Debug, Clone, Parser, derive_more::Display)]
pub enum SandboxBackend {
#[display(fmt = "wasmer")]
Wasmer,
#[display(fmt = "wasmi")]
Wasmi,
}

// TODO: use `derive_more::FromStr` when derive_more dependency is updated to 1.0
impl FromStr for SandboxBackend {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"wasmer" => Ok(SandboxBackend::Wasmer),
"wasmi" => Ok(SandboxBackend::Wasmi),
_ => Err(format!("Unknown sandbox executor: {}", s)),
}
}
}

#[allow(missing_docs)]
#[derive(Debug, Parser)]
Expand All @@ -26,6 +49,10 @@ pub struct RunCmd {
#[command(flatten)]
pub base: sc_cli::RunCmd,

/// The Wasm host executor to use in program sandbox.
#[arg(long, default_value_t = SandboxBackend::Wasmer)]
pub sandbox_backend: SandboxBackend,

/// The upper limit for the amount of gas a validator can burn in one block.
#[arg(long)]
pub max_gas: Option<u64>,
Expand Down
10 changes: 9 additions & 1 deletion node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::cli::{Cli, Subcommand};
use crate::{
cli::{Cli, Subcommand},
SandboxBackend,
};
use runtime_primitives::Block;
use sc_cli::{ChainSpec, SubstrateCli};
use sc_service::config::BasePath;
Expand Down Expand Up @@ -130,6 +133,11 @@ macro_rules! unwrap_client {
pub fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();

gear_runtime_interface::sandbox_init(match cli.run.sandbox_backend {
SandboxBackend::Wasmer => gear_runtime_interface::SandboxBackend::Wasmer,
SandboxBackend::Wasmi => gear_runtime_interface::SandboxBackend::Wasmi,
});

let old_base = BasePath::from_project("", "", "gear-node");
let new_base = BasePath::from_project("", "", &Cli::executable_name());
if old_base.path().exists() && !new_base.path().exists() {
Expand Down
21 changes: 17 additions & 4 deletions runtime-interface/sandbox/src/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use core::cell::RefCell;
use core::{cell::RefCell, sync::atomic::Ordering};

use codec::{Decode, Encode};
use gear_sandbox_host::sandbox::{self as sandbox_env, env::Instantiate};
Expand All @@ -32,10 +32,10 @@ struct Sandboxes {
}

impl Sandboxes {
pub fn new() -> Self {
pub fn new(sandbox_backend: sandbox_env::SandboxBackend) -> Self {
Self {
store_data_key: 0,
store: sandbox_env::SandboxComponents::new(sandbox_env::SandboxBackend::Wasmer),
store: sandbox_env::SandboxComponents::new(sandbox_backend),
}
}

Expand All @@ -61,8 +61,21 @@ impl Sandboxes {
}
}

// Global sandbox backend type selector
static SANDBOX_BACKEND_TYPE: sandbox_env::AtomicSandboxBackend =
sandbox_env::AtomicSandboxBackend::new(sandbox_env::SandboxBackend::Wasmer);

thread_local! {
static SANDBOXES: RefCell<Sandboxes> = RefCell::new(Sandboxes::new());
static SANDBOXES: RefCell<Sandboxes> = {
let sandbox_backend = SANDBOX_BACKEND_TYPE.load(Ordering::SeqCst);
RefCell::new(Sandboxes::new(sandbox_backend))
}
}

/// Sets the global sandbox backend type.
/// Buy default, it's set to `Wasmer`, so in case of `Wasmer` it's not necessary to call this function.
pub fn init(sandbox_backend: sandbox_env::SandboxBackend) {
SANDBOX_BACKEND_TYPE.store(sandbox_backend, Ordering::SeqCst);
}

struct SupervisorContext<'a, 'b> {
Expand Down
5 changes: 4 additions & 1 deletion runtime-interface/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
pub use gear_sandbox_host::sandbox::env::Instantiate;
pub use gear_sandbox_host::sandbox::{env::Instantiate, SandboxBackend};
use sp_runtime_interface::{runtime_interface, Pointer};
use sp_wasm_interface::HostPointer;

#[cfg(feature = "std")]
pub mod detail;

#[cfg(feature = "std")]
pub use detail::init;

/// Wasm-only interface that provides functions for interacting with the sandbox.
#[runtime_interface(wasm_only)]
pub trait Sandbox {
Expand Down
4 changes: 3 additions & 1 deletion runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use {

pub use gear_sandbox_interface::sandbox;
#[cfg(feature = "std")]
pub use gear_sandbox_interface::{detail as sandbox_detail, Instantiate};
pub use gear_sandbox_interface::{
detail as sandbox_detail, init as sandbox_init, Instantiate, SandboxBackend,
};

const _: () = assert!(size_of::<HostPointer>() >= size_of::<usize>());

Expand Down
6 changes: 5 additions & 1 deletion sandbox/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ rust-version.workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
atomic_enum.workspace = true
codec = { workspace = true, features = ["std"] }
defer.workspace = true
environmental.workspace = true
thiserror.workspace = true
log = { workspace = true, features = ["std"] }
wasmer.workspace = true
wasmer-types.workspace = true
sandbox-wasmi.workspace = true
wasmi.workspace = true
sp-allocator = { workspace = true, features = ["std"] }
sp-wasm-interface-common = { workspace = true, features = ["std"] }
gear-sandbox-env = { workspace = true, features = ["std"] }
gear-wasmer-cache = { workspace = true, optional = true }
tempfile = { workspace = true, optional = true }
region.workspace = true

[features]
default = ["wasmer-cache"]
wasmer-cache = ["gear-wasmer-cache", "tempfile"]
# See wasmi/extra-checks for more information.
wasmi-extra-checks = ["wasmi/extra-checks"]
8 changes: 4 additions & 4 deletions sandbox/host/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub type Result<T> = std::result::Result<T, Error>;
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Wasmi(#[from] sandbox_wasmi::Error),
Wasmi(#[from] wasmi::Error),

#[error("Sandbox error: {0}")]
Sandbox(String),
Expand Down Expand Up @@ -107,10 +107,10 @@ pub enum Error {
AbortedDueToTrap(MessageWithBacktrace),
}

impl sandbox_wasmi::HostError for Error {}
impl wasmi::core::HostError for Error {}

impl From<&'static str> for Error {
fn from(err: &'static str) -> Error {
impl From<&'_ str> for Error {
fn from(err: &'_ str) -> Error {
Error::Other(err.into())
}
}
Expand Down
2 changes: 2 additions & 0 deletions sandbox/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ pub mod error;
pub mod sandbox;
pub mod util;

pub(crate) mod store_refcell;

use log as _;
Loading

0 comments on commit 6eab909

Please sign in to comment.