Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
into gshep/issue-1049
  • Loading branch information
gshep committed Jun 14, 2022
2 parents a0f90d6 + d1e6a89 commit ba3b62e
Show file tree
Hide file tree
Showing 15 changed files with 517 additions and 260 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ jobs:
- name: "Build: Split examples by .opt and .meta"
run: ./scripts/gear.sh build examples-proc

- name: "Test: Gear pallet tests with lazy pages"
run: |
cargo test -p pallet-gear --features=lazy-pages --release
cargo test -p pallet-gear-debug --features=lazy-pages --release
cargo test -p pallet-gear-payment --features=lazy-pages --release
cargo test -p pallet-usage --features=lazy-pages --release
- name: "Test: Gear workspace"
run: ./scripts/gear.sh test gear --release
env:
Expand Down Expand Up @@ -192,6 +199,8 @@ jobs:
- name: "Test: Process node runtime testsuite"
run: ./scripts/gear.sh test rtest

# TODO: make also rtest run for node without lazy-pages feature (issue #1023)

- name: "Test (performance): node runtime testsuite"
if: ${{ github.ref != 'refs/heads/master' }}
id: test-performance-runtime
Expand Down
6 changes: 4 additions & 2 deletions core-backend/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,18 @@ pub trait Environment<E: Ext + IntoExtInfo + 'static>: Sized {
fn new(
ext: E,
binary: &[u8],
memory_pages: &BTreeMap<PageNumber, PageBuf>,
mem_size: WasmPageNumber,
) -> Result<Self, BackendError<Self::Error>>;

/// Returns addr to the stack end if it can be identified
fn get_stack_mem_end(&mut self) -> Option<WasmPageNumber>;

/// Get ref to mem impl
/// Get ref to mem wrapper
fn get_mem(&self) -> &dyn Memory;

/// Get mut ref to mem wrapper
fn get_mem_mut(&mut self) -> &mut dyn Memory;

/// Run instance setup starting at `entry_point` - wasm export function name.
/// Also runs `post_execution_handler` after running instance at provided entry point.
fn execute<F, T>(
Expand Down
39 changes: 8 additions & 31 deletions core-backend/sandbox/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
memory::MemoryWrap,
};
use alloc::{
collections::{BTreeMap, BTreeSet},
collections::BTreeSet,
format,
string::{String, ToString},
vec::Vec,
Expand All @@ -36,7 +36,7 @@ use gear_backend_common::{
use gear_core::{
env::{Ext, ExtCarrier},
gas::GasAmount,
memory::{Memory, PageBuf, PageNumber, WasmPageNumber},
memory::{Memory, WasmPageNumber},
};
use gear_core_errors::MemoryError;
use sp_sandbox::{
Expand All @@ -52,6 +52,8 @@ pub enum SandboxEnvironmentError {
GetWasmExports,
#[display(fmt = "Unable to set module memory data")]
SetModuleMemoryData,
#[display(fmt = "Unable to save static pages initial data")]
SaveStaticPagesInitialData,
#[display(fmt = "Failed to create env memory")]
CreateEnvMemory,
#[display(fmt = "{}", _0)]
Expand Down Expand Up @@ -112,25 +114,6 @@ fn get_module_exports(binary: &[u8]) -> Result<Vec<String>, String> {
.collect())
}

fn set_pages(
memory: &mut impl Memory,
pages: &BTreeMap<PageNumber, PageBuf>,
) -> Result<(), String> {
let memory_size = memory.size();
for (page, buf) in pages {
if page.to_wasm_page() >= memory_size {
return Err(format!(
"{:?} is out of memory size: {:?}",
page, memory_size
));
}
memory
.write(page.offset(), &buf[..])
.map_err(|e| format!("Cannot write mem to {:?}: {:?}", page, e))?;
}
Ok(())
}

impl<E> Environment<E> for SandboxEnvironment<E>
where
E: Ext + IntoExtInfo + 'static,
Expand All @@ -141,7 +124,6 @@ where
fn new(
ext: E,
binary: &[u8],
memory_pages: &BTreeMap<PageNumber, PageBuf>,
mem_size: WasmPageNumber,
) -> Result<Self, BackendError<Self::Error>> {
let mut builder = EnvBuilder::<E> {
Expand Down Expand Up @@ -230,15 +212,6 @@ where
}
};

// Set module memory.
if let Err(e) = set_pages(&mut runtime.memory, memory_pages) {
return Err(BackendError {
reason: SandboxEnvironmentError::SetModuleMemoryData,
description: Some(format!("{:?}", e).into()),
gas_amount: runtime.ext.into_inner().into_gas_amount(),
});
}

Ok(SandboxEnvironment {
runtime,
instance,
Expand All @@ -264,6 +237,10 @@ where
&self.runtime.memory
}

fn get_mem_mut(&mut self) -> &mut dyn Memory {
&mut self.runtime.memory
}

fn execute<F, T>(
mut self,
entry_point: &str,
Expand Down
42 changes: 9 additions & 33 deletions core-backend/wasmtime/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use gear_backend_common::{
use gear_core::{
env::{ClonedExtCarrier, Ext, ExtCarrier},
gas::GasAmount,
memory::{Memory, PageBuf, PageNumber, WasmPageNumber},
memory::{Memory, WasmPageNumber},
};
use gear_core_errors::MemoryError;
use wasmtime::{
Expand All @@ -60,6 +60,8 @@ pub enum WasmtimeEnvironmentError {
InstanceCreation,
#[display(fmt = "Unable to set module memory data")]
SetModuleMemoryData,
#[display(fmt = "Unable to save static pages initial data")]
SaveStaticPagesInitialData,
#[display(fmt = "Failed to create env memory")]
CreateEnvMemory,
#[display(fmt = "{}", _0)]
Expand All @@ -75,26 +77,6 @@ pub struct WasmtimeEnvironment<E: Ext + 'static> {
instance: Instance,
}

fn set_pages<T: Ext>(
mut store: &mut Store<StoreData<T>>,
memory: &mut WasmtimeMemory,
pages: &BTreeMap<PageNumber, PageBuf>,
) -> Result<(), String> {
let memory_size = WasmPageNumber(memory.size(&mut store) as u32);
for (page, buf) in pages {
if memory_size <= page.to_wasm_page() {
return Err(format!(
"Memory size {:?} less then {:?}",
memory_size, page
));
}
memory
.write(&mut store, page.offset(), &buf[..])
.map_err(|e| format!("Cannot write to {:?}: {:?}", page, e))?;
}
Ok(())
}

impl<E> Environment<E> for WasmtimeEnvironment<E>
where
E: Ext + IntoExtInfo,
Expand All @@ -105,7 +87,6 @@ where
fn new(
ext: E,
binary: &[u8],
memory_pages: &BTreeMap<PageNumber, PageBuf>,
mem_size: WasmPageNumber,
) -> Result<Self, BackendError<Self::Error>> {
let forbidden_funcs = ext.forbidden_funcs().clone();
Expand All @@ -119,7 +100,7 @@ where
let mut store = Store::<StoreData<E>>::new(&engine, store_data);

// Creates new wasm memory
let mut memory = match WasmtimeMemory::new(&mut store, MemoryType::new(mem_size.0, None)) {
let memory = match WasmtimeMemory::new(&mut store, MemoryType::new(mem_size.0, None)) {
Ok(mem) => mem,
Err(e) => {
return Err(BackendError {
Expand Down Expand Up @@ -246,15 +227,6 @@ where
}
};

// Set module memory data
if let Err(e) = set_pages(&mut store, &mut memory, memory_pages) {
return Err(BackendError {
reason: WasmtimeEnvironmentError::SetModuleMemoryData,
description: Some(format!("{:?}", e).into()),
gas_amount: ext_carrier.into_inner().into_gas_amount(),
});
}

let memory_wrap = MemoryWrapExternal { mem: memory, store };

Ok(WasmtimeEnvironment {
Expand Down Expand Up @@ -283,10 +255,14 @@ where
})
}

fn get_mem(&self) -> &dyn gear_core::memory::Memory {
fn get_mem(&self) -> &dyn Memory {
&self.memory_wrap
}

fn get_mem_mut(&mut self) -> &mut dyn Memory {
&mut self.memory_wrap
}

fn execute<F, T>(
mut self,
entry_point: &str,
Expand Down
15 changes: 9 additions & 6 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ pub enum ExecutionErrorReason {
/// Backend error
#[display(fmt = "{}", _0)]
Backend(String),
/// Processor error
#[display(fmt = "{}", _0)]
Processor(String),
/// Ext error
#[display(fmt = "{}", _0)]
Ext(String),
Expand Down Expand Up @@ -321,12 +318,18 @@ pub enum ExecutionErrorReason {
/// Changed page has no data in initial pages
#[display(fmt = "Changed page has no data in initial pages")]
PageNoData,
/// Ext works with lazy pages, but lazy pages env is not enabled
#[display(fmt = "Ext works with lazy pages, but lazy pages env is not enabled")]
LazyPagesInconsistentState,
/// Page with data is not allocated for program
#[display(fmt = "{:?} is not allocated for program", _0)]
PageIsNotAllocated(PageNumber),
/// Lazy pages init failed for current program.
#[display(fmt = "Cannot init lazy pages for program: {}", _0)]
LazyPagesInitFailed(String),
/// Cannot read initial memory data from wasm memory.
#[display(fmt = "Cannot read data for {:?}: {}", _0, _1)]
InitialMemoryReadFailed(PageNumber, MemoryError),
/// Cannot write initial data to wasm memory.
#[display(fmt = "Cannot write intial data for {:?}: {}", _0, _1)]
InitialDataWriteFailed(PageNumber, MemoryError),
}

/// Executable actor.
Expand Down
Loading

0 comments on commit ba3b62e

Please sign in to comment.