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

Merged
merged 40 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
6c9449c
Update lazy-pages-fuzzer wasmi version
ByteNacked Sep 29, 2024
733e6b0
Update tests
ByteNacked Sep 30, 2024
4192e81
Generic refcellstore
ByteNacked Sep 30, 2024
d94a1bc
WIP
ByteNacked Oct 1, 2024
51825c3
WIP2
ByteNacked Oct 2, 2024
911f3a9
WIP3
ByteNacked Oct 2, 2024
2335b3d
WIP4
ByteNacked Oct 2, 2024
67719b6
WIP5
ByteNacked Oct 2, 2024
2869fb8
WIP6
ByteNacked Oct 2, 2024
b4dcd1c
Switch sandbox to wasmi
ByteNacked Oct 2, 2024
25620bd
Fix double borrow
ByteNacked Oct 9, 2024
4a65c73
Fix wrong mem buffer size
ByteNacked Oct 10, 2024
4adef59
Fix rc cycle
ByteNacked Oct 14, 2024
6975f42
Add cli param
ByteNacked Oct 14, 2024
9e4ad10
Fix clippy
ByteNacked Oct 14, 2024
4bd475b
Update doc
ByteNacked Oct 15, 2024
f9795f4
Unify wasmi dep
ByteNacked Oct 15, 2024
03d93b3
Rename sandbox-wasmi dep
ByteNacked Oct 15, 2024
6361454
Fixup
ByteNacked Oct 15, 2024
4602f20
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Oct 15, 2024
59bf430
Trigger CI
ByteNacked Oct 15, 2024
16a91be
Remove whitespaces
ByteNacked Oct 15, 2024
a844d90
Add global sandbox backend ty
ByteNacked Oct 15, 2024
dc28e12
Bump wasmi version to 0.38
ByteNacked Oct 17, 2024
1d3bbc9
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Oct 19, 2024
d2f1135
Review fixes
ByteNacked Oct 20, 2024
1016959
Review fixes II
ByteNacked Oct 20, 2024
b721f08
Update comment
ByteNacked Oct 23, 2024
7501add
Enfoce backend drop order
ByteNacked Oct 23, 2024
c0926ea
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Oct 23, 2024
31caeca
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Oct 24, 2024
0e0b72f
Fix fs cache race
ByteNacked Oct 24, 2024
e668ef5
Review fixes
ByteNacked Oct 24, 2024
0151249
Fix duplicate symbols error
ByteNacked Oct 25, 2024
bd085fb
Increase recursion limit
ByteNacked Oct 28, 2024
0c7f825
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Oct 29, 2024
bc89443
Add comment
ByteNacked Nov 4, 2024
e39d143
Merge remote-tracking branch 'origin/master' into rmasl-update-sandbo…
ByteNacked Nov 4, 2024
4da949c
Merge branch 'master' into rmasl-update-sandbox-wasmi-version
ByteNacked Nov 4, 2024
b576fbe
Update doc-comment
ByteNacked Nov 4, 2024
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 @@ -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))
}
ark0f marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading