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

chore(sandbox): use sp-wasm-interface-common instead of sp-wasm-interface #3603

Merged
merged 6 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ jobs:
run: ./scripts/gear.sh test gear --exclude gclient --exclude gcli --exclude gsdk --release --locked

- name: "Test: gsdk tests"
run: ./scripts/gear.sh test gsdk --release
run: ./scripts/gear.sh test gsdk --release --retries 3

- name: "Test: `gcli`"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./scripts/gear.sh test gcli --release --locked --retries 3

- name: "Test: Client tests"
run: ./scripts/gear.sh test client --release
run: ./scripts/gear.sh test client --release --retries 3
clearloop marked this conversation as resolved.
Show resolved Hide resolved

- name: "Test: Runtime benchmarks and benchmark tests work"
run: |
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ sp-transaction-storage-proof = { version = "4.0.0-dev", git = "https://github.co
sp-trie = { version = "7.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes", default-features = false }
sp-version = { version = "5.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes", default-features = false }
sp-wasm-interface = { version = "7.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes", default-features = false }
sp-wasm-interface-common = { version = "7.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes", default-features = false }
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes" }
substrate-frame-rpc-system = { version = "4.0.0-dev", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes" }
substrate-rpc-client = { version = "0.10.0-dev", git = "https://github.com/gear-tech/substrate.git", branch = "gear-polkadot-v0.9.41-canary-no-sandbox-revert-oom-changes" }
Expand Down
4 changes: 2 additions & 2 deletions sandbox/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ targets = ["x86_64-unknown-linux-gnu"]
codec.workspace = true
sp-core.workspace = true
sp-std.workspace = true
sp-wasm-interface = { workspace = true, default-features = false }
sp-wasm-interface-common = { workspace = true, default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"sp-core/std",
"sp-std/std",
"sp-wasm-interface/std",
"sp-wasm-interface-common/std",
]
2 changes: 1 addition & 1 deletion sandbox/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use alloc::string::String;
use codec::{Decode, Encode};
use sp_core::RuntimeDebug;
use sp_std::vec::Vec;
use sp_wasm_interface::ReturnValue;
use sp_wasm_interface_common::ReturnValue;

#[derive(Clone, Copy, Debug)]
pub enum Instantiate {
Expand Down
2 changes: 1 addition & 1 deletion sandbox/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sandbox-wasmer.workspace = true
sandbox-wasmer-types.workspace = true
wasmi = { git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext", features = ["virtual_memory"] }
sp-allocator = { workspace = true, features = ["std"] }
sp-wasm-interface = { 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
Expand Down
24 changes: 11 additions & 13 deletions sandbox/host/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{collections::HashMap, pin::Pin, rc::Rc};
use codec::Decode;
use env::Instantiate;
use gear_sandbox_env as sandbox_env;
use sp_wasm_interface::{Pointer, WordSize};
use sp_wasm_interface_common::{Pointer, Value, WordSize};

use crate::{
error::{self, Result},
Expand All @@ -50,6 +50,8 @@ use self::{

pub use gear_sandbox_env as env;

type SandboxResult<T> = core::result::Result<T, String>;

/// Index of a function inside the supervisor.
///
/// This is a typically an index in the default table of the supervisor, however
Expand Down Expand Up @@ -143,11 +145,7 @@ pub trait SandboxContext {
) -> Result<i64>;

/// Read memory from `address` into a vector.
fn read_memory_into(
&self,
address: Pointer<u8>,
dest: &mut [u8],
) -> sp_wasm_interface::Result<()>;
fn read_memory_into(&self, address: Pointer<u8>, dest: &mut [u8]) -> SandboxResult<()>;

/// Read memory into the given `dest` buffer from `address`.
fn read_memory(&self, address: Pointer<u8>, size: WordSize) -> Result<Vec<u8>> {
Expand All @@ -157,13 +155,13 @@ pub trait SandboxContext {
}

/// Write the given data at `address` into the memory.
fn write_memory(&mut self, address: Pointer<u8>, data: &[u8]) -> sp_wasm_interface::Result<()>;
fn write_memory(&mut self, address: Pointer<u8>, data: &[u8]) -> SandboxResult<()>;

/// Allocate a memory instance of `size` bytes.
fn allocate_memory(&mut self, size: WordSize) -> sp_wasm_interface::Result<Pointer<u8>>;
fn allocate_memory(&mut self, size: WordSize) -> SandboxResult<Pointer<u8>>;

/// Deallocate a given memory instance.
fn deallocate_memory(&mut self, ptr: Pointer<u8>) -> sp_wasm_interface::Result<()>;
fn deallocate_memory(&mut self, ptr: Pointer<u8>) -> SandboxResult<()>;
}

/// Implementation of [`Externals`] that allows execution of guest module with
Expand Down Expand Up @@ -211,9 +209,9 @@ impl SandboxInstance {
pub fn invoke(
&self,
export_name: &str,
args: &[sp_wasm_interface::Value],
args: &[Value],
sandbox_context: &mut dyn SandboxContext,
) -> std::result::Result<Option<sp_wasm_interface::Value>, error::Error> {
) -> std::result::Result<Option<Value>, error::Error> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => {
wasmi_invoke(self, wasmi_instance, export_name, args, sandbox_context)
Expand All @@ -228,7 +226,7 @@ impl SandboxInstance {
/// Get the value from a global with the given `name`.
///
/// Returns `Some(_)` if the global could be found.
pub fn get_global_val(&self, name: &str) -> Option<sp_wasm_interface::Value> {
pub fn get_global_val(&self, name: &str) -> Option<Value> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => wasmi_get_global(wasmi_instance, name),

Expand All @@ -242,7 +240,7 @@ impl SandboxInstance {
pub fn set_global_val(
&self,
name: &str,
value: sp_wasm_interface::Value,
value: Value,
) -> std::result::Result<Option<()>, error::Error> {
match &self.backend_instance {
BackendInstance::Wasmi(wasmi_instance) => wasmi_set_global(wasmi_instance, name, value),
Expand Down
4 changes: 2 additions & 2 deletions sandbox/host/src/sandbox/wasmer_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ use sandbox_wasmer_types::TrapCode;

use codec::{Decode, Encode};
use gear_sandbox_env::{HostError, Instantiate, WasmReturnValue, GLOBAL_NAME_GAS};
use sp_wasm_interface::{util, Pointer, ReturnValue, Value, WordSize};
use sp_wasm_interface_common::{Pointer, ReturnValue, Value, WordSize};

use crate::{
error::{Error, Result},
sandbox::{
BackendInstance, GuestEnvironment, InstantiationError, Memory, SandboxContext,
SandboxInstance, SupervisorFuncIndex,
},
util::MemoryTransfer,
util::{self, MemoryTransfer},
};

environmental::environmental!(SandboxContextStore: trait SandboxContext);
Expand Down
4 changes: 2 additions & 2 deletions sandbox/host/src/sandbox/wasmi_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt;

use codec::{Decode, Encode};
use gear_sandbox_env::HostError;
use sp_wasm_interface::{util, Pointer, ReturnValue, Value, WordSize};
use sp_wasm_interface_common::{Pointer, ReturnValue, Value, WordSize};
use wasmi::{
memory_units::Pages, ImportResolver, MemoryInstance, Module, ModuleInstance, RuntimeArgs,
RuntimeValue, Trap, TrapCode,
Expand All @@ -34,7 +34,7 @@ use crate::{
BackendInstance, GuestEnvironment, GuestExternals, GuestFuncIndex, Imports,
InstantiationError, Memory, SandboxContext, SandboxInstance,
},
util::MemoryTransfer,
util::{self, MemoryTransfer},
};

environmental::environmental!(SandboxContextStore: trait SandboxContext);
Expand Down
10 changes: 9 additions & 1 deletion sandbox/host/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
//! Utilities used by all backends

use crate::error::Result;
use sp_wasm_interface::Pointer;
use core::ops::Range;
use sp_wasm_interface_common::Pointer;

/// Provides safe memory access interface using an external buffer
pub trait MemoryTransfer {
Expand Down Expand Up @@ -49,3 +50,10 @@ pub trait MemoryTransfer {
/// Returns host pointer to the wasm memory buffer.
fn get_buff(&mut self) -> *mut u8;
}

/// Construct a range from an offset to a data length after the offset.
/// Returns None if the end of the range would exceed some maximum offset.
pub fn checked_range(offset: usize, len: usize, max: usize) -> Option<Range<usize>> {
clearloop marked this conversation as resolved.
Show resolved Hide resolved
let end = offset.checked_add(len)?;
(end <= max).then(|| offset..end)
}