Skip to content

Commit

Permalink
Merge pull request paritytech#25 from gear-tech/gsobol-add-get-instan…
Browse files Browse the repository at this point in the history
…ce-ptr

add get instance ptr
  • Loading branch information
grishasobol authored and ukint-vs committed Apr 13, 2023
1 parent 0bdbfd8 commit 4fd877b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
17 changes: 13 additions & 4 deletions client/executor/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ use sc_allocator::AllocationStats;
use sc_executor_common::{
error::{Error, MessageWithBacktrace, WasmError},
runtime_blob::{DataSegmentsSnapshot, RuntimeBlob},
sandbox,
sandbox::{self, SandboxInstance},
util::MemoryTransfer,
wasm_runtime::{InvokeMethod, WasmInstance, WasmModule},
};
use sp_runtime_interface::unpack_ptr_and_len;
use sp_sandbox::env as sandbox_env;
use sp_wasm_interface::{
Function, FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, WordSize,
Function, FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, WordSize, HostPointer,
};

/// Wrapper around [`MemorRef`] that implements [`sc_allocator::Memory`].
Expand Down Expand Up @@ -389,13 +389,22 @@ impl Sandbox for FunctionExecutor {
Ok(m.memory_size())
}

fn get_buff(&mut self, memory_id: MemoryId) -> WResult<*mut u8> {
fn get_buff(&mut self, memory_id: MemoryId) -> WResult<HostPointer> {
let mut m = self
.sandbox_store
.borrow_mut()
.memory(memory_id)
.map_err(|e| format!("Cannot get wasmi memory: {}", e))?;
Ok(m.get_buff())
Ok(m.get_buff() as HostPointer)
}

fn get_instance_ptr(&mut self, instance_id: u32) -> WResult<HostPointer> {
let instance = self.sandbox_store
.borrow()
.instance(instance_id)
.map_err(|e| e.to_string())?;

Ok(instance.as_ref() as *const SandboxInstance as HostPointer)
}
}

Expand Down
16 changes: 12 additions & 4 deletions client/executor/wasmtime/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use codec::{Decode, Encode};
use sc_allocator::{AllocationStats, FreeingBumpHeapAllocator};
use sc_executor_common::{
error::Result,
sandbox::{self, SupervisorFuncIndex},
sandbox::{self, SupervisorFuncIndex, SandboxInstance},
util::MemoryTransfer,
};
use sp_sandbox::env as sandbox_env;
use sp_wasm_interface::{FunctionContext, MemoryId, Pointer, Sandbox, WordSize};
use sp_wasm_interface::{FunctionContext, MemoryId, Pointer, Sandbox, WordSize, HostPointer};

use crate::{instance_wrapper::MemoryWrapper, runtime::StoreData, util};

Expand Down Expand Up @@ -389,10 +389,18 @@ impl<'a> Sandbox for HostContext<'a> {
m.memory_grow(pages_num).map_err(|e| format!("{}", e))
}

fn get_buff(&mut self, memory_id: MemoryId) -> sp_wasm_interface::Result<*mut u8> {
fn get_buff(&mut self, memory_id: MemoryId) -> sp_wasm_interface::Result<HostPointer> {
let mut m = self.sandbox_store().memory(memory_id)
.map_err(|e| format!("Cannot get wasmer memory: {}", e))?;
Ok(m.get_buff())
Ok(m.get_buff() as HostPointer)
}

fn get_instance_ptr(&mut self, instance_id: u32) -> sp_wasm_interface::Result<HostPointer> {
let instance = self.sandbox_store()
.instance(instance_id)
.map_err(|e| e.to_string())?;

Ok(instance.as_ref() as *const SandboxInstance as HostPointer)
}
}

Expand Down
10 changes: 8 additions & 2 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

use sp_std::vec::Vec;

use sp_wasm_interface::HostPointer;

#[cfg(feature = "std")]
use tracing;

Expand Down Expand Up @@ -1728,10 +1730,14 @@ pub trait Sandbox {
.expect("Failed to get memory size from sandbox")
}

fn get_buff(&mut self, memory_idx: u32) -> u64 {
fn get_buff(&mut self, memory_idx: u32) -> HostPointer {
self.sandbox()
.get_buff(memory_idx)
.expect("Failed to get wasmo memory buffer addr from sandbox") as u64
.expect("Failed to get wasm memory buffer addr from sandbox")
}

fn get_instance_ptr(&mut self, instance_id: u32) -> HostPointer {
self.sandbox().get_instance_ptr(instance_id).expect("Failed to get instance ptr")
}
}

Expand Down
9 changes: 9 additions & 0 deletions primitives/sandbox/src/embedded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use alloc::string::String;

use sp_wasm_interface::HostPointer;
use wasmi::{
memory_units::Pages, Externals, FuncInstance, FuncRef, GlobalDescriptor, GlobalRef,
ImportResolver, MemoryDescriptor, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef,
Expand Down Expand Up @@ -68,6 +69,10 @@ impl super::SandboxMemory for Memory {
fn size(&self) -> u32 {
self.memref.current_size().0 as u32
}

unsafe fn get_buff(&self) -> u64 {
self.memref.direct_access_mut().as_mut().as_mut_ptr() as usize as u64
}
}

impl Memory {
Expand Down Expand Up @@ -317,6 +322,10 @@ impl<T> super::SandboxInstance<T> for Instance<T> {
fn instance_globals(&self) -> Option<Self::InstanceGlobals> {
None
}

fn get_instance_ptr(&self) -> HostPointer {
unreachable!("Must not be called for embedded executor")
}
}

/// Convert the substrate value type to the wasmi value type.
Expand Down
15 changes: 13 additions & 2 deletions primitives/sandbox/src/host_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use codec::{Decode, Encode};

use sp_io::sandbox;
use sp_std::{marker, mem, prelude::*, rc::Rc, slice, vec};
use sp_wasm_interface::HostPointer;

use crate::{env, Error, HostFuncType, ReturnValue, Value};

Expand Down Expand Up @@ -102,11 +103,17 @@ impl super::SandboxMemory for Memory {
}

fn grow(&self, pages: u32) -> Result<u32, Error> {
todo!()
let size = self.size();
sandbox::memory_grow(self.handle.memory_idx, pages);
Ok(size)
}

fn size(&self) -> u32 {
todo!()
sandbox::memory_size(self.handle.memory_idx)
}

unsafe fn get_buff(&self) -> HostPointer {
sandbox::get_buff(self.handle.memory_idx)
}
}

Expand Down Expand Up @@ -300,6 +307,10 @@ impl<T> super::SandboxInstance<T> for Instance<T> {
fn instance_globals(&self) -> Option<Self::InstanceGlobals> {
Some(InstanceGlobals { instance_idx: Some(self.instance_idx) })
}

fn get_instance_ptr(&self) -> HostPointer {
sandbox::get_instance_ptr(self.instance_idx)
}
}

impl<T> Drop for Instance<T> {
Expand Down
9 changes: 8 additions & 1 deletion primitives/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod host_executor;
use sp_core::RuntimeDebug;
use sp_std::prelude::*;

use sp_wasm_interface::HostPointer;
pub use sp_wasm_interface::{ReturnValue, Value};

#[cfg(not(all(feature = "wasmer-sandbox", not(feature = "std"))))]
Expand Down Expand Up @@ -119,13 +120,16 @@ pub trait SandboxMemory: Sized + Clone {

/// Grow memory with provided number of pages.
///
/// Returns `Err` if attempted to allocate more memory than permited by the limit.
/// Returns `Err` if attempted to allocate more memory than permitted by the limit.
fn grow(&self, pages: u32) -> Result<u32, Error>;

/// Returns current memory size.
///
/// Maximum memory size cannot exceed 65536 pages or 4GiB.
fn size(&self) -> u32;

/// Returns pointer to the begin of wasm mem buffer
unsafe fn get_buff(&self) -> HostPointer;
}

/// Struct that can be used for defining an environment for a sandboxed module.
Expand Down Expand Up @@ -208,6 +212,9 @@ pub trait SandboxInstance<State>: Sized {
///
/// Returns `None` if the executor doesn't support the interface.
fn instance_globals(&self) -> Option<Self::InstanceGlobals>;

/// Get raw pointer to the executor host sandbox instance.
fn get_instance_ptr(&self) -> HostPointer;
}

/// Error that can occur while using this crate.
Expand Down
10 changes: 9 additions & 1 deletion primitives/wasm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ pub trait FunctionContext {
/// Sandbox memory identifier.
pub type MemoryId = u32;

/// Host pointer: suit both for 32-bit and 64-bit archs.
pub type HostPointer = u64;

/// Something that provides access to the sandbox.
pub trait Sandbox {
/// Get sandbox memory from the `memory_id` instance at `offset` into the given buffer.
Expand Down Expand Up @@ -399,7 +402,12 @@ pub trait Sandbox {
fn memory_grow(&mut self, memory_id: MemoryId, pages_num: u32) -> Result<u32>;

/// Get host pointer to the begin of wasm memory with `memory_id`.
fn get_buff(&mut self, memory_id: MemoryId) -> Result<*mut u8>;
fn get_buff(&mut self, memory_id: MemoryId) -> Result<HostPointer>;

/// Get raw pointer to the executor host sandbox instance.
///
/// Returns Err if the instance `instance_id` does not exist.
fn get_instance_ptr(&mut self, instance_id: u32) -> Result<HostPointer>;
}

if_wasmtime_is_enabled! {
Expand Down

0 comments on commit 4fd877b

Please sign in to comment.