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

feat(sandbox-host): update sandbox host wasmi version #4282

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 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
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 @@ -303,9 +303,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 @@ -497,6 +495,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
2 changes: 1 addition & 1 deletion ethexe/processor/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) struct InstanceCreator {

impl InstanceCreator {
pub fn new(runtime: Vec<u8>) -> Result<Self> {
gear_runtime_interface::sandbox_init();
gear_runtime_interface::sandbox_init(gear_runtime_interface::SandboxBackend::Wasmer);

let engine = wasmtime::Engine::default();

Expand Down
2 changes: 1 addition & 1 deletion node/authorship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ type TestCase = Box<dyn Fn() + Send + 'static>;
#[test]
fn run_all_tests() {
init_logger();
gear_runtime_interface::sandbox_init();
gear_runtime_interface::sandbox_init(gear_runtime_interface::SandboxBackend::Wasmer);

use basic_tests::*;

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
1 change: 0 additions & 1 deletion node/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() -> gear_cli::Result<()> {
gear_runtime_interface::sandbox_init();
gear_cli::run()
}
23 changes: 16 additions & 7 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,13 +61,22 @@ 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))
}
ark0f marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn init() {
SANDBOXES.with(|sandboxes| {
let _store = sandboxes.borrow_mut().get(0);
pub fn init(sandbox_backend: sandbox_env::SandboxBackend) {
SANDBOX_BACKEND_TYPE.store(sandbox_backend, Ordering::SeqCst);
// At first access sandbox will be initialized with the provided backend
SANDBOXES.with_borrow_mut(|sandboxes| {
let _store = sandboxes.get(0);
})
ark0f marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
2 changes: 1 addition & 1 deletion runtime-interface/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![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;

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, init as sandbox_init, 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
4 changes: 3 additions & 1 deletion sandbox/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ 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, features = ["singlepass"] }
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"] }
wasmer-cache = { workspace = true, optional = true }
tempfile.workspace = true
uluru = { workspace = true, optional = true }
region.workspace = true

[features]
default = ["wasmer-cache", "uluru"]
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
1 change: 1 addition & 0 deletions sandbox/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

pub mod error;
pub mod sandbox;
pub(crate) mod store_refcell;
ByteNacked marked this conversation as resolved.
Show resolved Hide resolved
pub mod util;

use log as _;
Loading
Loading