From 6ab7136f8a994869f8bb75b55fbf8a74e9c77e2e Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Thu, 7 Sep 2023 17:44:58 +0300 Subject: [PATCH 01/41] Move state.rs into sandbox backend --- core-backend/common/src/lib.rs | 1 - core-backend/sandbox/src/env.rs | 8 ++++++-- core-backend/sandbox/src/lib.rs | 1 + core-backend/sandbox/src/memory.rs | 7 +++---- core-backend/sandbox/src/runtime.rs | 7 +++++-- core-backend/{common => sandbox}/src/state.rs | 4 +++- 6 files changed, 18 insertions(+), 10 deletions(-) rename core-backend/{common => sandbox}/src/state.rs (92%) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 62d47e1cd99..4432d8a3236 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -32,7 +32,6 @@ pub mod mock; pub mod funcs; pub mod memory; pub mod runtime; -pub mod state; use crate::runtime::RunFallibleError; use actor_system_error::actor_system_error; diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index ca5c9f0f81a..3f334743b0b 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -18,14 +18,18 @@ //! sp-sandbox environment for running a module. -use crate::{memory::MemoryWrap, runtime, runtime::CallerWrap}; +use crate::{ + memory::MemoryWrap, + runtime, + runtime::CallerWrap, + state::{HostState, State}, +}; use alloc::{collections::BTreeSet, format}; use core::{any::Any, convert::Infallible, fmt::Display}; use gear_backend_common::{ funcs::FuncsHandler, lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, - state::{HostState, State}, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, BackendSyscallError, BackendTermination, Environment, EnvironmentError, EnvironmentExecutionResult, LimitedStr, diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index 196e42589f6..cba98cdcb59 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -25,6 +25,7 @@ extern crate alloc; pub mod env; pub mod memory; pub mod runtime; +pub mod state; pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index 56409841003..cfbc8a15fbc 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -18,7 +18,7 @@ //! sp-sandbox extensions for memory. -use gear_backend_common::state::HostState; +use crate::state::HostState; use gear_core::{ env::Externalities, memory::{HostPointer, Memory, MemoryError}, @@ -128,9 +128,8 @@ where #[cfg(test)] mod tests { use super::*; - use gear_backend_common::{ - assert_err, assert_ok, mock::MockExt, state::State, ActorTerminationReason, - }; + use crate::state::State; + use gear_backend_common::{assert_err, assert_ok, mock::MockExt, ActorTerminationReason}; use gear_core::memory::{AllocError, AllocationsContext, NoopGrowHandler}; use gear_sandbox::{AsContextExt, SandboxStore}; diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index 73dc54c5a8f..e2b27c5a2b0 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -18,7 +18,11 @@ //! sp-sandbox runtime (here it's contract execution state) realization. -use crate::{memory::MemoryWrapRef, DefaultExecutorMemory}; +use crate::{ + memory::MemoryWrapRef, + state::{HostState, State}, + DefaultExecutorMemory, +}; use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; use gear_backend_common::{ @@ -27,7 +31,6 @@ use gear_backend_common::{ WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, runtime::{RunFallibleError, Runtime as CommonRuntime}, - state::{HostState, State}, BackendExternalities, BackendState, UndefinedTerminationReason, }; use gear_core::{costs::RuntimeCosts, pages::WasmPage}; diff --git a/core-backend/common/src/state.rs b/core-backend/sandbox/src/state.rs similarity index 92% rename from core-backend/common/src/state.rs rename to core-backend/sandbox/src/state.rs index a59a7b5116b..b0c62ead3bf 100644 --- a/core-backend/common/src/state.rs +++ b/core-backend/sandbox/src/state.rs @@ -16,7 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{BackendExternalities, BackendState, BackendTermination, UndefinedTerminationReason}; +use gear_backend_common::{ + BackendExternalities, BackendState, BackendTermination, UndefinedTerminationReason, +}; pub type HostState = Option>; From a70b56c0db3f51c647ca9cd5417fb3bf0f0b04df Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Thu, 7 Sep 2023 17:49:08 +0300 Subject: [PATCH 02/41] Move funcs.rs into sandbox backend --- Cargo.lock | 4 ++-- core-backend/common/Cargo.toml | 2 -- core-backend/common/src/lib.rs | 1 - core-backend/sandbox/Cargo.toml | 5 ++++- core-backend/sandbox/src/env.rs | 2 +- core-backend/{common => sandbox}/src/funcs.rs | 18 ++++++++++-------- core-backend/sandbox/src/lib.rs | 1 + 7 files changed, 18 insertions(+), 15 deletions(-) rename core-backend/{common => sandbox}/src/funcs.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 3041ea8580e..8690ba33b7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3793,9 +3793,7 @@ name = "gear-backend-common" version = "1.0.0" dependencies = [ "actor-system-error", - "blake2-rfc", "derive_more", - "gear-backend-codegen", "gear-core", "gear-core-errors", "gear-wasm-instrument", @@ -3811,7 +3809,9 @@ dependencies = [ name = "gear-backend-sandbox" version = "0.1.0" dependencies = [ + "blake2-rfc", "derive_more", + "gear-backend-codegen", "gear-backend-common", "gear-core", "gear-core-errors", diff --git a/core-backend/common/Cargo.toml b/core-backend/common/Cargo.toml index 9c7d226c5e7..f4660752081 100644 --- a/core-backend/common/Cargo.toml +++ b/core-backend/common/Cargo.toml @@ -14,9 +14,7 @@ repository.workspace = true gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } gear-wasm-instrument.workspace = true -gear-backend-codegen.workspace = true -blake2-rfc.workspace = true derive_more.workspace = true gsys.workspace = true log.workspace = true diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 4432d8a3236..9b60f85c254 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -29,7 +29,6 @@ mod utils; #[cfg(any(feature = "mock", test))] pub mod mock; -pub mod funcs; pub mod memory; pub mod runtime; diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/sandbox/Cargo.toml index 9c0e6e710ef..5bb5ed0af99 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/sandbox/Cargo.toml @@ -9,11 +9,14 @@ license.workspace = true gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } gear-backend-common.workspace = true -gsys ={ workspace = true } +gear-backend-codegen.workspace = true +gsys = { workspace = true } gear-wasm-instrument.workspace = true gear-sandbox.workspace = true gear-sandbox-env.workspace = true + +blake2-rfc.workspace = true # Use max_level_debug feature to remove tracing in sys-calls by default. log.workspace = true derive_more.workspace = true diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index 3f334743b0b..ded0ea08af9 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -19,6 +19,7 @@ //! sp-sandbox environment for running a module. use crate::{ + funcs::FuncsHandler, memory::MemoryWrap, runtime, runtime::CallerWrap, @@ -27,7 +28,6 @@ use crate::{ use alloc::{collections::BTreeSet, format}; use core::{any::Any, convert::Infallible, fmt::Display}; use gear_backend_common::{ - funcs::FuncsHandler, lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, diff --git a/core-backend/common/src/funcs.rs b/core-backend/sandbox/src/funcs.rs similarity index 98% rename from core-backend/common/src/funcs.rs rename to core-backend/sandbox/src/funcs.rs index 31efa8b0d73..efc080413ea 100644 --- a/core-backend/common/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -18,23 +18,25 @@ //! Syscall implementations generic over wasmi and sandbox backends. -use crate::{ - memory::{MemoryAccessError, WasmMemoryRead}, - runtime::{RunFallibleError, Runtime}, - syscall_trace, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, - BackendSyscallError, MessageWaitedType, TrapExplanation, UndefinedTerminationReason, - UnrecoverableExecutionError, UnrecoverableMemoryError, PTR_SPECIAL, -}; use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; +use gear_backend_common::{ + memory::{MemoryAccessError, WasmMemoryRead}, + runtime::{RunFallibleError, Runtime}, + syscall_trace, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, + BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableMemoryError, PTR_SPECIAL, +}; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, costs::RuntimeCosts, env::{DropPayloadLockBound, Externalities}, gas::CounterType, - message::{HandlePacket, InitPacket, Payload, PayloadSizeError, ReplyPacket}, + message::{ + HandlePacket, InitPacket, MessageWaitedType, Payload, PayloadSizeError, ReplyPacket, + }, pages::{PageNumber, PageU32Size, WasmPage}, }; use gear_core_errors::{MessageError, ReplyCode, SignalCode}; diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index cba98cdcb59..41a9b9983a2 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -23,6 +23,7 @@ extern crate alloc; pub mod env; +pub mod funcs; pub mod memory; pub mod runtime; pub mod state; From 3a4453c762bceb33b1cf4f84278f9b3f8aa5766a Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Thu, 7 Sep 2023 18:01:07 +0300 Subject: [PATCH 03/41] Remove `backend_common::Runtime` --- core-backend/common/src/runtime.rs | 38 +---- core-backend/sandbox/src/funcs.rs | 239 ++++++++++++++++++---------- core-backend/sandbox/src/lib.rs | 4 +- core-backend/sandbox/src/memory.rs | 2 +- core-backend/sandbox/src/runtime.rs | 25 +-- 5 files changed, 172 insertions(+), 136 deletions(-) diff --git a/core-backend/common/src/runtime.rs b/core-backend/common/src/runtime.rs index 308d3a1db53..6d345821294 100644 --- a/core-backend/common/src/runtime.rs +++ b/core-backend/common/src/runtime.rs @@ -18,11 +18,7 @@ //! Trait that both sandbox and wasmi runtimes must implement. -use crate::{ - memory::{MemoryAccessRecorder, MemoryOwner}, - BackendExternalities, BackendState, BackendSyscallError, UndefinedTerminationReason, -}; -use gear_core::{costs::RuntimeCosts, pages::WasmPage}; +use crate::{BackendSyscallError, UndefinedTerminationReason}; use gear_core_errors::ExtError as FallibleExtError; /// Error returned from closure argument in [`Runtime::run_fallible`]. @@ -40,35 +36,3 @@ where err.into_run_fallible_error() } } - -pub trait Runtime: - MemoryOwner + MemoryAccessRecorder + BackendState -{ - type Error; - - fn unreachable_error() -> Self::Error; - - fn ext_mut(&mut self) -> &mut Ext; - - fn run_any( - &mut self, - gas: u64, - cost: RuntimeCosts, - f: F, - ) -> Result<(u64, T), Self::Error> - where - F: FnOnce(&mut Self) -> Result; - - fn run_fallible( - &mut self, - gas: u64, - res_ptr: u32, - cost: RuntimeCosts, - f: F, - ) -> Result<(u64, ()), Self::Error> - where - F: FnOnce(&mut Self) -> Result, - R: From> + Sized; - - fn alloc(&mut self, pages: u32) -> Result; -} diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index efc080413ea..74e71e2fdc0 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -18,16 +18,17 @@ //! Syscall implementations generic over wasmi and sandbox backends. +use crate::runtime::CallerWrap; use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; use gear_backend_common::{ - memory::{MemoryAccessError, WasmMemoryRead}, - runtime::{RunFallibleError, Runtime}, + memory::{MemoryAccessError, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead}, + runtime::RunFallibleError, syscall_trace, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, - BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, - UnrecoverableMemoryError, PTR_SPECIAL, + BackendState, BackendSyscallError, TrapExplanation, UndefinedTerminationReason, + UnrecoverableExecutionError, UnrecoverableMemoryError, PTR_SPECIAL, }; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, @@ -40,27 +41,30 @@ use gear_core::{ pages::{PageNumber, PageU32Size, WasmPage}, }; use gear_core_errors::{MessageError, ReplyCode, SignalCode}; +use gear_sandbox_env::HostError; use gsys::{ BlockNumberWithHash, ErrorBytes, ErrorWithBlockNumberAndValue, ErrorWithGas, ErrorWithHandle, ErrorWithHash, ErrorWithReplyCode, ErrorWithSignalCode, ErrorWithTwoHashes, Hash, HashWithValue, TwoHashesWithValue, }; -pub struct FuncsHandler { - _phantom: PhantomData<(Ext, Runtime)>, +pub struct FuncsHandler { + _phantom: PhantomData, } -impl FuncsHandler +impl FuncsHandler where Ext: BackendExternalities + 'static, Ext::UnrecoverableError: BackendSyscallError, RunFallibleError: From, Ext::AllocError: BackendAllocSyscallError, - R: Runtime, { /// !!! Usage warning: make sure to do it before any other read/write, /// because it may contain registered read. - fn register_and_read_value(ctx: &mut R, value_ptr: u32) -> Result { + fn register_and_read_value( + ctx: &mut CallerWrap<'_, '_, Ext>, + value_ptr: u32, + ) -> Result { if value_ptr != PTR_SPECIAL { let read_value = ctx.register_read_decoded(value_ptr); return ctx.read_decoded(read_value); @@ -70,7 +74,7 @@ where } fn read_message_payload( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, read_payload: WasmMemoryRead, ) -> Result { ctx.read(read_payload)? @@ -83,13 +87,13 @@ where #[allow(clippy::too_many_arguments)] #[host(fallible, wgas, cost = RuntimeCosts::Send(len))] pub fn send( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, pid_value_ptr: u32, payload_ptr: u32, len: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_hash_val = ctx.register_read_as(pid_value_ptr); let read_payload = ctx.register_read(payload_ptr, len); let HashWithValue { @@ -105,12 +109,12 @@ where #[host(fallible, wgas, cost = RuntimeCosts::SendCommit)] pub fn send_commit( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, handle: u32, pid_value_ptr: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_pid_value = ctx.register_read_as(pid_value_ptr); let HashWithValue { hash: destination, @@ -127,18 +131,18 @@ where } #[host(fallible, cost = RuntimeCosts::SendInit, err = ErrorWithHandle)] - pub fn send_init(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn send_init(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { ctx.ext_mut().send_init().map_err(Into::into) } #[host(fallible, cost = RuntimeCosts::SendPush(len), err = ErrorBytes)] pub fn send_push( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, handle: u32, payload_ptr: u32, len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_payload = ctx.register_read(payload_ptr, len); let payload = ctx.read(read_payload)?; @@ -149,13 +153,13 @@ where #[host(fallible, cost = RuntimeCosts::ReservationSend(len))] pub fn reservation_send( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, rid_pid_value_ptr: u32, payload_ptr: u32, len: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_rid_pid_value = ctx.register_read_as(rid_pid_value_ptr); let read_payload = ctx.register_read(payload_ptr, len); let TwoHashesWithValue { @@ -176,12 +180,12 @@ where #[host(fallible, cost = RuntimeCosts::ReservationSendCommit)] pub fn reservation_send_commit( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, handle: u32, rid_pid_value_ptr: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_rid_pid_value = ctx.register_read_as(rid_pid_value_ptr); let TwoHashesWithValue { hash1: reservation_id, @@ -201,12 +205,12 @@ where #[host(fallible, cost = RuntimeCosts::Read, err = ErrorBytes)] pub fn read( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, at: u32, len: u32, buffer_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let payload_lock = ctx.ext_mut().lock_payload(at, len)?; payload_lock .drop_with::(|payload_access| { @@ -221,7 +225,11 @@ where } #[host(cost = RuntimeCosts::Size)] - pub fn size(ctx: &mut R, gas: u64, size_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn size( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + size_ptr: u32, + ) -> Result<(u64, ()), HostError> { let size = ctx.ext_mut().size()? as u32; let write_size = ctx.register_write_as(size_ptr); @@ -230,14 +238,18 @@ where } #[host(cost = RuntimeCosts::Exit)] - pub fn exit(ctx: &mut R, gas: u64, inheritor_id_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn exit( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + inheritor_id_ptr: u32, + ) -> Result<(u64, ()), HostError> { let read_inheritor_id = ctx.register_read_decoded(inheritor_id_ptr); let inheritor_id = ctx.read_decoded(read_inheritor_id)?; Err(ActorTerminationReason::Exit(inheritor_id).into()) } #[host(fallible, cost = RuntimeCosts::ReplyCode, err = ErrorWithReplyCode)] - pub fn reply_code(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn reply_code(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { ctx.ext_mut() .reply_code() .map(ReplyCode::to_bytes) @@ -246,7 +258,10 @@ where // TODO: write proper benchmark #2825 #[host(fallible, cost = RuntimeCosts::ReplyCode, err = ErrorWithSignalCode)] - pub fn signal_code(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn signal_code( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + ) -> Result<(u64, ()), HostError> { ctx.ext_mut() .signal_code() .map(SignalCode::to_u32) @@ -254,7 +269,11 @@ where } #[host(cost = RuntimeCosts::Alloc(pages))] - pub fn alloc(ctx: &mut R, gas: u64, pages: u32) -> Result<(u64, u32), R::Error> { + pub fn alloc( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + pages: u32, + ) -> Result<(u64, u32), HostError> { let res = ctx.alloc(pages); let res = ctx.process_alloc_func_result(res)?; @@ -272,7 +291,11 @@ where } #[host(cost = RuntimeCosts::Free)] - pub fn free(ctx: &mut R, gas: u64, page_no: u32) -> Result<(u64, i32), R::Error> { + pub fn free( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + page_no: u32, + ) -> Result<(u64, i32), HostError> { let page = WasmPage::new(page_no).map_err(|_| { UndefinedTerminationReason::Actor(ActorTerminationReason::Trap( TrapExplanation::Unknown, @@ -295,7 +318,11 @@ where } #[host(cost = RuntimeCosts::BlockHeight)] - pub fn block_height(ctx: &mut R, gas: u64, height_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn block_height( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + height_ptr: u32, + ) -> Result<(u64, ()), HostError> { let height = ctx.ext_mut().block_height()?; let write_height = ctx.register_write_as(height_ptr); @@ -305,10 +332,10 @@ where #[host(cost = RuntimeCosts::BlockTimestamp)] pub fn block_timestamp( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, timestamp_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let timestamp = ctx.ext_mut().block_timestamp()?; let write_timestamp = ctx.register_write_as(timestamp_ptr); @@ -318,11 +345,11 @@ where #[host(cost = RuntimeCosts::Random)] pub fn random( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, subject_ptr: u32, bn_random_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_subject = ctx.register_read_decoded(subject_ptr); let write_bn_random = ctx.register_write_as(bn_random_ptr); @@ -340,12 +367,12 @@ where #[host(fallible, wgas, cost = RuntimeCosts::Reply(len))] pub fn reply( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, payload_ptr: u32, len: u32, value_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_payload = ctx.register_read(payload_ptr, len); let value = Self::register_and_read_value(ctx, value_ptr)?; let payload = Self::read_message_payload(ctx, read_payload)?; @@ -356,7 +383,11 @@ where } #[host(fallible, wgas, cost = RuntimeCosts::ReplyCommit)] - pub fn reply_commit(ctx: &mut R, gas: u64, value_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn reply_commit( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + value_ptr: u32, + ) -> Result<(u64, ()), HostError> { let value = Self::register_and_read_value(ctx, value_ptr)?; ctx.ext_mut() @@ -366,12 +397,12 @@ where #[host(fallible, cost = RuntimeCosts::ReservationReply(len))] pub fn reservation_reply( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, rid_value_ptr: u32, payload_ptr: u32, len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_rid_value = ctx.register_read_as(rid_value_ptr); let read_payload = ctx.register_read(payload_ptr, len); let HashWithValue { @@ -387,10 +418,10 @@ where #[host(fallible, cost = RuntimeCosts::ReservationReplyCommit)] pub fn reservation_reply_commit( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, rid_value_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_rid_value = ctx.register_read_as(rid_value_ptr); let HashWithValue { hash: reservation_id, @@ -406,23 +437,26 @@ where } #[host(fallible, cost = RuntimeCosts::ReplyTo)] - pub fn reply_to(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn reply_to(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { ctx.ext_mut().reply_to().map_err(Into::into) } // TODO: write proper benchmark #2825 #[host(fallible, cost = RuntimeCosts::SignalFrom)] - pub fn signal_from(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn signal_from( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + ) -> Result<(u64, ()), HostError> { ctx.ext_mut().signal_from().map_err(Into::into) } #[host(fallible, cost = RuntimeCosts::ReplyPush(len), err = ErrorBytes)] pub fn reply_push( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, payload_ptr: u32, len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_payload = ctx.register_read(payload_ptr, len); let payload = ctx.read(read_payload)?; @@ -431,12 +465,12 @@ where #[host(fallible, wgas, cost = RuntimeCosts::ReplyInput)] pub fn reply_input( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, offset: u32, len: u32, value_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { // Charge for `len` is inside `reply_push_input` let value = Self::register_and_read_value(ctx, value_ptr)?; @@ -451,11 +485,11 @@ where #[host(fallible, cost = RuntimeCosts::ReplyPushInput, err = ErrorBytes)] pub fn reply_push_input( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, offset: u32, len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { ctx.ext_mut() .reply_push_input(offset, len) .map_err(Into::into) @@ -464,13 +498,13 @@ where #[allow(clippy::too_many_arguments)] #[host(fallible, wgas, cost = RuntimeCosts::SendInput)] pub fn send_input( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, pid_value_ptr: u32, offset: u32, len: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { // Charge for `len` inside `send_push_input` let read_pid_value = ctx.register_read_as(pid_value_ptr); let HashWithValue { @@ -493,12 +527,12 @@ where #[host(fallible, cost = RuntimeCosts::SendPushInput, err = ErrorBytes)] pub fn send_push_input( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, handle: u32, offset: u32, len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { ctx.ext_mut() .send_push_input(handle, offset, len) .map_err(Into::into) @@ -506,11 +540,11 @@ where #[host(cost = RuntimeCosts::Debug(data_len))] pub fn debug( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, data_ptr: u32, data_len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_data = ctx.register_read(data_ptr, data_len); let data: RuntimeBuffer = ctx .read(read_data)? @@ -530,11 +564,11 @@ where #[host(cost = RuntimeCosts::Null)] pub fn panic( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, data_ptr: u32, data_len: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_data = ctx.register_read(data_ptr, data_len); let data = ctx.read(read_data).unwrap_or_default(); @@ -544,17 +578,17 @@ where } #[host(cost = RuntimeCosts::Null)] - pub fn oom_panic(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn oom_panic(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { Err(ActorTerminationReason::Trap(TrapExplanation::ProgramAllocOutOfBounds).into()) } #[host(fallible, cost = RuntimeCosts::ReserveGas)] pub fn reserve_gas( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, gas_value: u64, duration: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { ctx.ext_mut() .reserve_gas(gas_value, duration) .map_err(Into::into) @@ -562,11 +596,11 @@ where #[host(fallible, cost = RuntimeCosts::ReplyDeposit, err = ErrorBytes)] pub fn reply_deposit( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, message_id_ptr: u32, gas_value: u64, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_message_id = ctx.register_read_decoded(message_id_ptr); let message_id = ctx.read_decoded(read_message_id)?; @@ -577,10 +611,10 @@ where #[host(fallible, cost = RuntimeCosts::UnreserveGas, err = ErrorWithGas)] pub fn unreserve_gas( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, reservation_id_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_reservation_id = ctx.register_read_decoded(reservation_id_ptr); let reservation_id = ctx.read_decoded(read_reservation_id)?; @@ -591,17 +625,21 @@ where #[host(fallible, cost = RuntimeCosts::SystemReserveGas, err = ErrorBytes)] pub fn system_reserve_gas( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, gas_value: u64, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { ctx.ext_mut() .system_reserve_gas(gas_value) .map_err(Into::into) } #[host(cost = RuntimeCosts::GasAvailable)] - pub fn gas_available(ctx: &mut R, gas: u64, gas_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn gas_available( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + gas_ptr: u32, + ) -> Result<(u64, ()), HostError> { let gas_available = ctx.ext_mut().gas_available()?; let write_gas = ctx.register_write_as(gas_ptr); @@ -610,7 +648,11 @@ where } #[host(cost = RuntimeCosts::MsgId)] - pub fn message_id(ctx: &mut R, gas: u64, message_id_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn message_id( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + message_id_ptr: u32, + ) -> Result<(u64, ()), HostError> { let message_id = ctx.ext_mut().message_id()?; let write_message_id = ctx.register_write_as(message_id_ptr); @@ -619,7 +661,11 @@ where } #[host(cost = RuntimeCosts::ProgramId)] - pub fn program_id(ctx: &mut R, gas: u64, program_id_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn program_id( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + program_id_ptr: u32, + ) -> Result<(u64, ()), HostError> { let program_id = ctx.ext_mut().program_id()?; let write_program_id = ctx.register_write_as(program_id_ptr); @@ -629,10 +675,10 @@ where #[host(fallible, cost = RuntimeCosts::PayProgramRent, err = ErrorWithBlockNumberAndValue)] pub fn pay_program_rent( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, rent_pid_ptr: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_rent_pid = ctx.register_read_as(rent_pid_ptr); let HashWithValue { @@ -646,7 +692,11 @@ where } #[host(cost = RuntimeCosts::Source)] - pub fn source(ctx: &mut R, gas: u64, source_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn source( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + source_ptr: u32, + ) -> Result<(u64, ()), HostError> { let source = ctx.ext_mut().source()?; let write_source = ctx.register_write_as(source_ptr); @@ -655,7 +705,11 @@ where } #[host(cost = RuntimeCosts::Value)] - pub fn value(ctx: &mut R, gas: u64, value_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn value( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + value_ptr: u32, + ) -> Result<(u64, ()), HostError> { let value = ctx.ext_mut().value()?; let write_value = ctx.register_write_as(value_ptr); @@ -664,7 +718,11 @@ where } #[host(cost = RuntimeCosts::ValueAvailable)] - pub fn value_available(ctx: &mut R, gas: u64, value_ptr: u32) -> Result<(u64, ()), R::Error> { + pub fn value_available( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + value_ptr: u32, + ) -> Result<(u64, ()), HostError> { let value_available = ctx.ext_mut().value_available()?; let write_value = ctx.register_write_as(value_ptr); @@ -673,24 +731,32 @@ where } #[host(cost = RuntimeCosts::Leave)] - pub fn leave(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn leave(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { Err(ActorTerminationReason::Leave.into()) } #[host(cost = RuntimeCosts::Wait)] - pub fn wait(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn wait(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { ctx.ext_mut().wait()?; Err(ActorTerminationReason::Wait(None, MessageWaitedType::Wait).into()) } #[host(cost = RuntimeCosts::WaitFor)] - pub fn wait_for(ctx: &mut R, gas: u64, duration: u32) -> Result<(u64, ()), R::Error> { + pub fn wait_for( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + duration: u32, + ) -> Result<(u64, ()), HostError> { ctx.ext_mut().wait_for(duration)?; Err(ActorTerminationReason::Wait(Some(duration), MessageWaitedType::WaitFor).into()) } #[host(cost = RuntimeCosts::WaitUpTo)] - pub fn wait_up_to(ctx: &mut R, gas: u64, duration: u32) -> Result<(u64, ()), R::Error> { + pub fn wait_up_to( + ctx: &mut CallerWrap<'_, '_, Ext>, + gas: u64, + duration: u32, + ) -> Result<(u64, ()), HostError> { let waited_type = if ctx.ext_mut().wait_up_to(duration)? { MessageWaitedType::WaitUpToFull } else { @@ -701,11 +767,11 @@ where #[host(fallible, cost = RuntimeCosts::Wake, err = ErrorBytes)] pub fn wake( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, message_id_ptr: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_message_id = ctx.register_read_decoded(message_id_ptr); let message_id = ctx.read_decoded(read_message_id)?; @@ -715,7 +781,7 @@ where #[allow(clippy::too_many_arguments)] #[host(fallible, wgas, cost = RuntimeCosts::CreateProgram(payload_len, salt_len), err = ErrorWithTwoHashes)] pub fn create_program( - ctx: &mut R, + ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64, cid_value_ptr: u32, salt_ptr: u32, @@ -723,7 +789,7 @@ where payload_ptr: u32, payload_len: u32, delay: u32, - ) -> Result<(u64, ()), R::Error> { + ) -> Result<(u64, ()), HostError> { let read_cid_value = ctx.register_read_as(cid_value_ptr); let read_salt = ctx.register_read(salt_ptr, salt_len); let read_payload = ctx.register_read(payload_ptr, payload_len); @@ -739,7 +805,7 @@ where .map_err(Into::into) } - pub fn forbidden(ctx: &mut R, gas: u64) -> Result<(u64, ()), R::Error> { + pub fn forbidden(ctx: &mut CallerWrap<'_, '_, Ext>, gas: u64) -> Result<(u64, ()), HostError> { syscall_trace!("forbidden"); ctx.run_any(gas, RuntimeCosts::Null, |_| { @@ -747,7 +813,10 @@ where }) } - pub fn out_of_gas(ctx: &mut R, _gas: u64) -> Result<(u64, ()), R::Error> { + pub fn out_of_gas( + ctx: &mut CallerWrap<'_, '_, Ext>, + _gas: u64, + ) -> Result<(u64, ()), HostError> { syscall_trace!("out_of_gas"); let ext = ctx.ext_mut(); @@ -763,6 +832,6 @@ where let termination_reason: ActorTerminationReason = current_counter.into(); ctx.set_termination_reason(termination_reason.into()); - Err(R::unreachable_error()) + Err(HostError) } } diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index 41a9b9983a2..b1b132a6062 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -23,10 +23,10 @@ extern crate alloc; pub mod env; -pub mod funcs; +mod funcs; pub mod memory; pub mod runtime; -pub mod state; +mod state; pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index cfbc8a15fbc..96fd58d9777 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -31,7 +31,7 @@ use gear_sandbox::{ pub type DefaultExecutorMemory = gear_sandbox::default_executor::Memory; -pub(crate) struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { +pub struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { pub memory: DefaultExecutorMemory, pub caller: &'a mut Caller<'b, HostState>, } diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index e2b27c5a2b0..4c5d5882434 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -30,7 +30,7 @@ use gear_backend_common::{ MemoryAccessError, MemoryAccessManager, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, - runtime::{RunFallibleError, Runtime as CommonRuntime}, + runtime::RunFallibleError, BackendExternalities, BackendState, UndefinedTerminationReason, }; use gear_core::{costs::RuntimeCosts, pages::WasmPage}; @@ -63,25 +63,28 @@ pub(crate) fn caller_host_state_take( .unwrap_or_else(|| unreachable!("host_state must be set before execution")) } -pub(crate) struct CallerWrap<'a, 'b: 'a, Ext> { +pub struct CallerWrap<'a, 'b: 'a, Ext> { pub caller: &'a mut Caller<'b, HostState>, pub manager: MemoryAccessManager, pub memory: DefaultExecutorMemory, } -impl<'a, 'b, Ext: BackendExternalities + 'static> CommonRuntime for CallerWrap<'a, 'b, Ext> { - type Error = HostError; - - fn ext_mut(&mut self) -> &mut Ext { +impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { + pub fn ext_mut(&mut self) -> &mut Ext { &mut self.host_state_mut().ext } - fn unreachable_error() -> Self::Error { + pub fn unreachable_error() -> HostError { HostError } #[track_caller] - fn run_any(&mut self, gas: u64, cost: RuntimeCosts, f: F) -> Result<(u64, T), Self::Error> + pub fn run_any( + &mut self, + gas: u64, + cost: RuntimeCosts, + f: F, + ) -> Result<(u64, T), HostError> where F: FnOnce(&mut Self) -> Result, { @@ -101,13 +104,13 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CommonRuntime for CallerW } #[track_caller] - fn run_fallible( + pub fn run_fallible( &mut self, gas: u64, res_ptr: u32, cost: RuntimeCosts, f: F, - ) -> Result<(u64, ()), Self::Error> + ) -> Result<(u64, ()), HostError> where F: FnOnce(&mut Self) -> Result, R: From> + Sized, @@ -127,7 +130,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CommonRuntime for CallerW ) } - fn alloc(&mut self, pages: u32) -> Result::AllocError> { + pub fn alloc(&mut self, pages: u32) -> Result::AllocError> { let mut state = caller_host_state_take(self.caller); let mut mem = CallerWrap::memory(self.caller, self.memory.clone()); let res = state.ext.alloc(pages, &mut mem); From 64b298cfdedfa652084e57bf047a25d9ce8d45b8 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Thu, 7 Sep 2023 20:36:01 +0300 Subject: [PATCH 04/41] Move `syscall_trace` to funcs.rs --- core-backend/common/src/lib.rs | 35 --------------------------- core-backend/sandbox/src/funcs.rs | 40 ++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 9b60f85c254..0fe9b68669a 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -509,40 +509,5 @@ pub trait BackendTermination: Sized { } } -#[macro_export] -macro_rules! syscall_args_trace { - ($val:expr) => { - { - let s = stringify!($val); - if s.ends_with("_ptr") { - alloc::format!(", {} = {:#x?}", s, $val) - } else { - alloc::format!(", {} = {:?}", s, $val) - } - } - }; - ($val:expr, $($rest:expr),+) => { - { - let mut s = $crate::syscall_args_trace!($val); - s.push_str(&$crate::syscall_args_trace!($($rest),+)); - s - } - }; -} - -#[macro_export] -macro_rules! syscall_trace { - ($name:expr, $($args:expr),+) => { - { - $crate::log::trace!(target: "syscalls", "{}{}", $name, $crate::syscall_args_trace!($($args),+)); - } - }; - ($name:expr) => { - { - $crate::log::trace!(target: "syscalls", "{}", $name); - } - } -} - #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index 74e71e2fdc0..8d4bb06d3db 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -26,9 +26,9 @@ use gear_backend_codegen::host; use gear_backend_common::{ memory::{MemoryAccessError, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead}, runtime::RunFallibleError, - syscall_trace, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, - BackendState, BackendSyscallError, TrapExplanation, UndefinedTerminationReason, - UnrecoverableExecutionError, UnrecoverableMemoryError, PTR_SPECIAL, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendState, + BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableMemoryError, PTR_SPECIAL, }; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, @@ -48,6 +48,40 @@ use gsys::{ HashWithValue, TwoHashesWithValue, }; +#[macro_export(local_inner_macros)] +macro_rules! syscall_args_trace { + ($val:expr) => { + { + let s = ::core::stringify!($val); + if s.ends_with("_ptr") { + alloc::format!(", {} = {:#x?}", s, $val) + } else { + alloc::format!(", {} = {:?}", s, $val) + } + } + }; + ($val:expr, $($rest:expr),+) => { + { + let mut s = syscall_args_trace!($val); + s.push_str(&syscall_args_trace!($($rest),+)); + s + } + }; +} + +macro_rules! syscall_trace { + ($name:expr, $($args:expr),+) => { + { + ::log::trace!(target: "syscalls", "{}{}", $name, syscall_args_trace!($($args),+)); + } + }; + ($name:expr) => { + { + ::log::trace!(target: "syscalls", "{}", $name); + } + } +} + pub struct FuncsHandler { _phantom: PhantomData, } From 5edfec5f805d53ae64ea4abe1e4d6536b27d058a Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 12:48:15 +0300 Subject: [PATCH 05/41] Inline `BackendState` --- core-backend/common/src/lib.rs | 35 ------------------------- core-backend/sandbox/src/funcs.rs | 4 +-- core-backend/sandbox/src/runtime.rs | 40 +++++++++++++++++++++++++---- core-backend/sandbox/src/state.rs | 10 +------- 4 files changed, 38 insertions(+), 51 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 0fe9b68669a..74194520cb8 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -413,41 +413,6 @@ where PrepareMemoryError: Display; } -pub trait BackendState { - /// Set termination reason - fn set_termination_reason(&mut self, reason: UndefinedTerminationReason); - - /// Process fallible syscall function result - fn process_fallible_func_result( - &mut self, - res: Result, - ) -> Result, UndefinedTerminationReason> { - match res { - Err(RunFallibleError::FallibleExt(ext_err)) => { - let code = ext_err.to_u32(); - log::trace!(target: "syscalls", "fallible syscall error: {ext_err}"); - Ok(Err(code)) - } - Err(RunFallibleError::UndefinedTerminationReason(reason)) => Err(reason), - Ok(res) => Ok(Ok(res)), - } - } - - /// Process alloc function result - fn process_alloc_func_result( - &mut self, - res: Result, - ) -> Result, UndefinedTerminationReason> { - match res { - Ok(t) => Ok(Ok(t)), - Err(err) => match err.into_backend_error() { - Ok(ext_err) => Err(ext_err.into()), - Err(alloc_err) => Ok(Err(alloc_err)), - }, - } - } -} - /// A trait for termination of the gear sys-calls execution backend. /// /// Backend termination aims to return to the caller gear wasm program diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index 8d4bb06d3db..c2fe5777d7e 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -26,8 +26,8 @@ use gear_backend_codegen::host; use gear_backend_common::{ memory::{MemoryAccessError, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendState, - BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, UnrecoverableMemoryError, PTR_SPECIAL, }; use gear_core::{ diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index 4c5d5882434..5e18c58bead 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -31,7 +31,7 @@ use gear_backend_common::{ WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, runtime::RunFallibleError, - BackendExternalities, BackendState, UndefinedTerminationReason, + BackendAllocSyscallError, BackendExternalities, UndefinedTerminationReason, }; use gear_core::{costs::RuntimeCosts, pages::WasmPage}; use gear_sandbox::{default_executor::Caller, AsContextExt, HostError, Value}; @@ -120,7 +120,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { cost, |ctx: &mut Self| -> Result<_, UndefinedTerminationReason> { let res = f(ctx); - let res = ctx.host_state_mut().process_fallible_func_result(res)?; + let res = ctx.process_fallible_func_result(res)?; // TODO: move above or make normal process memory access. let write_res = ctx.register_write_as::(res_ptr); @@ -212,9 +212,39 @@ impl<'a, 'b, Ext> MemoryAccessRecorder for CallerWrap<'a, 'b, Ext> { } } -impl BackendState for CallerWrap<'_, '_, Ext> { - fn set_termination_reason(&mut self, reason: UndefinedTerminationReason) { - self.host_state_mut().set_termination_reason(reason); +impl CallerWrap<'_, '_, Ext> { + pub fn set_termination_reason(&mut self, reason: UndefinedTerminationReason) { + self.host_state_mut().termination_reason = reason; + } + + /// Process fallible syscall function result + pub fn process_fallible_func_result( + &mut self, + res: Result, + ) -> Result, UndefinedTerminationReason> { + match res { + Err(RunFallibleError::FallibleExt(ext_err)) => { + let code = ext_err.to_u32(); + log::trace!(target: "syscalls", "fallible syscall error: {ext_err}"); + Ok(Err(code)) + } + Err(RunFallibleError::UndefinedTerminationReason(reason)) => Err(reason), + Ok(res) => Ok(Ok(res)), + } + } + + /// Process alloc function result + pub fn process_alloc_func_result( + &mut self, + res: Result, + ) -> Result, UndefinedTerminationReason> { + match res { + Ok(t) => Ok(Ok(t)), + Err(err) => match err.into_backend_error() { + Ok(ext_err) => Err(ext_err.into()), + Err(alloc_err) => Ok(Err(alloc_err)), + }, + } } } diff --git a/core-backend/sandbox/src/state.rs b/core-backend/sandbox/src/state.rs index b0c62ead3bf..67a681bc71e 100644 --- a/core-backend/sandbox/src/state.rs +++ b/core-backend/sandbox/src/state.rs @@ -16,9 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use gear_backend_common::{ - BackendExternalities, BackendState, BackendTermination, UndefinedTerminationReason, -}; +use gear_backend_common::{BackendExternalities, BackendTermination, UndefinedTerminationReason}; pub type HostState = Option>; @@ -38,9 +36,3 @@ impl BackendTermination for State (ext, termination_reason) } } - -impl BackendState for State { - fn set_termination_reason(&mut self, reason: UndefinedTerminationReason) { - self.termination_reason = reason; - } -} From 11cc05bfe7e51056b3d0c79261f0371cbc3da664 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 12:50:48 +0300 Subject: [PATCH 06/41] Inline `BackendTermination` --- core-backend/common/src/lib.rs | 61 ------------------------------ core-backend/sandbox/src/env.rs | 3 +- core-backend/sandbox/src/lib.rs | 1 + core-backend/sandbox/src/state.rs | 62 +++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 66 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 74194520cb8..d3bb2bf1c23 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -413,66 +413,5 @@ where PrepareMemoryError: Display; } -/// A trait for termination of the gear sys-calls execution backend. -/// -/// Backend termination aims to return to the caller gear wasm program -/// execution outcome, which is the state of externalities, memory and -/// termination reason. -pub trait BackendTermination: Sized { - /// Transforms [`Self`] into tuple of externalities, memory and - /// termination reason returned after the execution. - fn into_parts(self) -> (Ext, UndefinedTerminationReason); - - /// Terminates backend work after execution. - /// - /// The function handles `res`, which is the result of gear wasm - /// program entry point invocation, and the termination reason. - /// - /// If the `res` is `Ok`, then execution considered successful - /// and the termination reason will have the corresponding value. - /// - /// If the `res` is `Err`, then execution is considered to end - /// with an error and the actual termination reason, which stores - /// more precise information about the error, is returned. - /// - /// There's a case, when `res` is `Err`, but termination reason has - /// a value for the successful ending of the execution. This is the - /// case of calling `unreachable` panic in the program. - fn terminate( - self, - res: Result, - gas: u64, - ) -> (Ext, TerminationReason) { - log::trace!("Execution result = {res:?}"); - - let (mut ext, termination_reason) = self.into_parts(); - let termination_reason = termination_reason.define(ext.current_counter_type()); - - ext.decrease_current_counter_to(gas); - - let termination_reason = if res.is_err() { - if matches!( - termination_reason, - TerminationReason::Actor(ActorTerminationReason::Success) - ) { - ActorTerminationReason::Trap(TrapExplanation::Unknown).into() - } else { - termination_reason - } - } else if matches!( - termination_reason, - TerminationReason::Actor(ActorTerminationReason::Success) - ) { - termination_reason - } else { - unreachable!( - "Termination reason is not success, but executor successfully ends execution" - ) - }; - - (ext, termination_reason) - } -} - #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index ded0ea08af9..a980efb7ccb 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -31,8 +31,7 @@ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, BackendTermination, Environment, EnvironmentError, - EnvironmentExecutionResult, LimitedStr, + BackendSyscallError, Environment, EnvironmentError, EnvironmentExecutionResult, LimitedStr, }; use gear_core::{ env::Externalities, diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index b1b132a6062..b56dbfac33d 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -21,6 +21,7 @@ #![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; +extern crate core; pub mod env; mod funcs; diff --git a/core-backend/sandbox/src/state.rs b/core-backend/sandbox/src/state.rs index 67a681bc71e..2ba51b46079 100644 --- a/core-backend/sandbox/src/state.rs +++ b/core-backend/sandbox/src/state.rs @@ -16,7 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use gear_backend_common::{BackendExternalities, BackendTermination, UndefinedTerminationReason}; +use core::fmt::Debug; +use gear_backend_common::{ + ActorTerminationReason, BackendExternalities, TerminationReason, TrapExplanation, + UndefinedTerminationReason, +}; pub type HostState = Option>; @@ -26,8 +30,10 @@ pub struct State { pub termination_reason: UndefinedTerminationReason, } -impl BackendTermination for State { - fn into_parts(self) -> (Ext, UndefinedTerminationReason) { +impl State { + /// Transforms [`Self`] into tuple of externalities, memory and + /// termination reason returned after the execution. + pub fn into_parts(self) -> (Ext, UndefinedTerminationReason) { let State { ext, termination_reason, @@ -35,4 +41,54 @@ impl BackendTermination for State } = self; (ext, termination_reason) } + + /// Terminates backend work after execution. + /// + /// The function handles `res`, which is the result of gear wasm + /// program entry point invocation, and the termination reason. + /// + /// If the `res` is `Ok`, then execution considered successful + /// and the termination reason will have the corresponding value. + /// + /// If the `res` is `Err`, then execution is considered to end + /// with an error and the actual termination reason, which stores + /// more precise information about the error, is returned. + /// + /// There's a case, when `res` is `Err`, but termination reason has + /// a value for the successful ending of the execution. This is the + /// case of calling `unreachable` panic in the program. + pub fn terminate( + self, + res: Result, + gas: u64, + ) -> (Ext, TerminationReason) { + log::trace!("Execution result = {res:?}"); + + let (mut ext, termination_reason) = self.into_parts(); + let termination_reason = termination_reason.define(ext.current_counter_type()); + + ext.decrease_current_counter_to(gas); + + let termination_reason = if res.is_err() { + if matches!( + termination_reason, + TerminationReason::Actor(ActorTerminationReason::Success) + ) { + ActorTerminationReason::Trap(TrapExplanation::Unknown).into() + } else { + termination_reason + } + } else if matches!( + termination_reason, + TerminationReason::Actor(ActorTerminationReason::Success) + ) { + termination_reason + } else { + unreachable!( + "Termination reason is not success, but executor successfully ends execution" + ) + }; + + (ext, termination_reason) + } } From 66b0a2550d129bfd697d0a736f6ba0ba6f668765 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 13:21:26 +0300 Subject: [PATCH 07/41] Inline `Environment` --- Cargo.lock | 4 +-- core-backend/common/src/lib.rs | 54 +--------------------------- core-backend/sandbox/src/env.rs | 33 ++++++++++------- core-processor/Cargo.toml | 3 +- core-processor/src/executor.rs | 44 +++++++++++++---------- core-processor/src/processing.rs | 15 ++++---- gcli/Cargo.toml | 1 - gcli/src/meta/mod.rs | 5 +-- gtest/Cargo.toml | 1 - gtest/src/manager.rs | 7 ++-- pallets/gear-debug/Cargo.toml | 1 - pallets/gear/src/benchmarking/mod.rs | 1 - pallets/gear/src/lib.rs | 2 -- pallets/gear/src/queue.rs | 2 +- pallets/gear/src/runtime_api.rs | 6 ++-- utils/wasm-instrument/src/tests.rs | 2 +- 16 files changed, 67 insertions(+), 114 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8690ba33b7c..833dab7de04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3652,7 +3652,6 @@ dependencies = [ "dirs", "env_logger", "etc", - "gear-backend-sandbox", "gear-core", "gear-core-errors", "gear-core-processor", @@ -3958,6 +3957,7 @@ dependencies = [ "enum-iterator 1.4.1", "env_logger", "gear-backend-common", + "gear-backend-sandbox", "gear-core", "gear-core-errors", "gear-lazy-pages-common", @@ -4761,7 +4761,6 @@ dependencies = [ "derive_more", "env_logger", "gear-backend-common", - "gear-backend-sandbox", "gear-core", "gear-core-errors", "gear-core-processor", @@ -7437,7 +7436,6 @@ dependencies = [ "frame-support", "frame-support-test", "frame-system", - "gear-backend-sandbox", "gear-common", "gear-core", "gear-wasm-instrument", diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index d3bb2bf1c23..8cc66debe3d 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -48,13 +48,10 @@ use gear_core::{ gas::{ChargeError, CounterType, CountersOwner, GasAmount}, ids::{CodeId, MessageId, ProgramId, ReservationId}, memory::{Memory, MemoryError, MemoryInterval, PageBuf}, - message::{ - ContextStore, Dispatch, DispatchKind, IncomingDispatch, MessageWaitedType, WasmEntryPoint, - }, + message::{ContextStore, Dispatch, IncomingDispatch, MessageWaitedType}, pages::{GearPage, WasmPage}, reservation::GasReserver, }; -use lazy_pages::GlobalsAccessConfig; use memory::ProcessAccessError; use scale_info::scale::{self, Decode, Encode}; @@ -364,54 +361,5 @@ impl } } -type EnvironmentBackendReport = - BackendReport<>::Memory, >::Ext>; - -pub type EnvironmentExecutionResult = Result< - EnvironmentBackendReport, - EnvironmentError<>::SystemError, PrepareMemoryError>, ->; - -pub trait Environment: Sized -where - EntryPoint: WasmEntryPoint, -{ - type Ext: BackendExternalities + 'static; - - /// Memory type for current environment. - type Memory: Memory; - - /// That's an error which originally comes from the primary - /// wasm execution environment (set by wasmi or sandbox). - /// So it's not the error of the `Self` itself, it's a kind - /// of wrapper over the underlying executor error. - type SystemError: Debug + Display; - - /// 1) Instantiates wasm binary. - /// 2) Creates wasm memory - /// 3) Runs `prepare_memory` to fill the memory before running instance. - /// 4) Instantiate external funcs for wasm module. - fn new( - ext: Self::Ext, - binary: &[u8], - entry_point: EntryPoint, - entries: BTreeSet, - mem_size: WasmPage, - ) -> Result>; - - /// Run instance setup starting at `entry_point` - wasm export function name. - fn execute( - self, - prepare_memory: PrepareMemory, - ) -> EnvironmentExecutionResult - where - PrepareMemory: FnOnce( - &mut Self::Memory, - Option, - GlobalsAccessConfig, - ) -> Result<(), PrepareMemoryError>, - PrepareMemoryError: Display; -} - #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index a980efb7ccb..435192fe8d3 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -26,12 +26,16 @@ use crate::{ state::{HostState, State}, }; use alloc::{collections::BTreeSet, format}; -use core::{any::Any, convert::Infallible, fmt::Display}; +use core::{ + any::Any, + convert::Infallible, + fmt::{Debug, Display}, +}; use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, Environment, EnvironmentError, EnvironmentExecutionResult, LimitedStr, + BackendSyscallError, EnvironmentError, LimitedStr, }; use gear_core::{ env::Externalities, @@ -158,6 +162,13 @@ fn store_host_state_mut( .unwrap_or_else(|| unreachable!("State must be set in `WasmiEnvironment::new`; qed")) } +type EnvironmentBackendReport = BackendReport, Ext>; + +pub type EnvironmentExecutionResult = Result< + EnvironmentBackendReport, + EnvironmentError, +>; + #[derive(Debug, derive_more::Display)] pub enum SandboxEnvironmentError { #[display(fmt = "Failed to create env memory: {_0:?}")] @@ -322,7 +333,7 @@ impl GlobalsAccessor for GlobalsAccessProvider Environment for SandboxEnvironment +impl SandboxEnvironment where EnvExt: BackendExternalities + 'static, EnvExt::UnrecoverableError: BackendSyscallError, @@ -330,17 +341,13 @@ where EnvExt::AllocError: BackendAllocSyscallError, EntryPoint: WasmEntryPoint, { - type Ext = EnvExt; - type Memory = MemoryWrap; - type SystemError = SandboxEnvironmentError; - - fn new( - ext: Self::Ext, + pub fn new( + ext: EnvExt, binary: &[u8], entry_point: EntryPoint, entries: BTreeSet, mem_size: WasmPage, - ) -> Result> { + ) -> Result> { use EnvironmentError::*; use SandboxEnvironmentError::*; @@ -406,13 +413,13 @@ where }) } - fn execute( + pub fn execute( self, prepare_memory: PrepareMemory, - ) -> EnvironmentExecutionResult + ) -> EnvironmentExecutionResult where PrepareMemory: FnOnce( - &mut Self::Memory, + &mut MemoryWrap, Option, GlobalsAccessConfig, ) -> Result<(), PrepareMemoryError>, diff --git a/core-processor/Cargo.toml b/core-processor/Cargo.toml index cc07d9088ee..d455cc66a6b 100644 --- a/core-processor/Cargo.toml +++ b/core-processor/Cargo.toml @@ -14,6 +14,7 @@ repository.workspace = true gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } gear-backend-common.workspace = true +gear-backend-sandbox.workspace = true gear-wasm-instrument.workspace = true gear-lazy-pages-common.workspace = true @@ -29,6 +30,6 @@ enum-iterator.workspace = true [features] default = ["std"] -std = ["gear-lazy-pages-common/std"] +std = ["gear-lazy-pages-common/std", "gear-backend-sandbox/std"] strict = [] mock = [] diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index c1fa182a76e..90b5a47090e 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -33,13 +33,15 @@ use alloc::{ }; use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}, - ActorTerminationReason, BackendExternalities, BackendReport, BackendSyscallError, Environment, - EnvironmentError, TerminationReason, + runtime::RunFallibleError, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, + BackendSyscallError, EnvironmentError, TerminationReason, }; +use gear_backend_sandbox::{MemoryWrap, SandboxEnvironment}; use gear_core::{ code::InstrumentedCode, env::Externalities, - gas::{CountersOwner, GasAllowanceCounter, GasCounter, ValueCounter}, + gas::{GasAllowanceCounter, GasCounter, ValueCounter}, ids::ProgramId, memory::{AllocationsContext, Memory}, message::{ @@ -135,7 +137,7 @@ fn prepare_memory( } /// Execute wasm with dispatch and return dispatch result. -pub fn execute_wasm( +pub fn execute_wasm( balance: u128, dispatch: IncomingDispatch, context: WasmExecutionContext, @@ -143,9 +145,11 @@ pub fn execute_wasm( msg_ctx_settings: ContextSettings, ) -> Result where - E: Environment, - E::Ext: ProcessorExternalities + BackendExternalities + 'static, - ::UnrecoverableError: BackendSyscallError, + Ext: ProcessorExternalities + BackendExternalities + 'static, + ::AllocError: + BackendAllocSyscallError, + RunFallibleError: From, + ::UnrecoverableError: BackendSyscallError, { let WasmExecutionContext { gas_counter, @@ -218,11 +222,11 @@ where let lazy_pages_weights = context.page_costs.lazy_pages_weights(); // Creating externalities. - let ext = E::Ext::new(context); + let ext = Ext::new(context); // Execute program in backend env. let execute = || { - let env = E::new( + let env = SandboxEnvironment::new( ext, program.code_bytes(), kind, @@ -231,7 +235,7 @@ where ) .map_err(EnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { - prepare_memory::( + prepare_memory::>( memory, program_id, static_pages, @@ -257,9 +261,9 @@ where }; // released pages initial data will be added to `pages_initial_data` after execution. - E::Ext::lazy_pages_post_execution_actions(&mut memory); + Ext::lazy_pages_post_execution_actions(&mut memory); - if !E::Ext::lazy_pages_status().is_normal() { + if !Ext::lazy_pages_status().is_normal() { termination = ext.current_counter_type().into() } @@ -338,7 +342,7 @@ where /// !!! FOR TESTING / INFORMATIONAL USAGE ONLY #[allow(clippy::too_many_arguments)] -pub fn execute_for_reply( +pub fn execute_for_reply( function: EP, instrumented_code: InstrumentedCode, allocations: Option>, @@ -348,9 +352,11 @@ pub fn execute_for_reply( block_info: BlockInfo, ) -> Result, String> where - E: Environment, - E::Ext: ProcessorExternalities + BackendExternalities + 'static, - ::UnrecoverableError: BackendSyscallError, + Ext: ProcessorExternalities + BackendExternalities + 'static, + ::AllocError: + BackendAllocSyscallError, + RunFallibleError: From, + ::UnrecoverableError: BackendSyscallError, EP: WasmEntryPoint, { let program = Program::new(program_id.unwrap_or_default(), instrumented_code); @@ -413,11 +419,11 @@ where let lazy_pages_weights = context.page_costs.lazy_pages_weights(); // Creating externalities. - let ext = E::Ext::new(context); + let ext = Ext::new(context); // Execute program in backend env. let f = || { - let env = E::new( + let env = SandboxEnvironment::new( ext, program.code_bytes(), function, @@ -426,7 +432,7 @@ where ) .map_err(EnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { - prepare_memory::( + prepare_memory::>( memory, program.id(), static_pages, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index 094dc7117be..b1c90982ff9 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -29,7 +29,8 @@ use crate::{ }; use alloc::{string::ToString, vec::Vec}; use gear_backend_common::{ - BackendExternalities, BackendSyscallError, Environment, SystemReservationContext, + runtime::RunFallibleError, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + SystemReservationContext, }; use gear_core::{ env::Externalities, @@ -40,15 +41,17 @@ use gear_core::{ use gear_core_errors::{ErrorReplyReason, SignalCode}; /// Process program & dispatch for it and return journal for updates. -pub fn process( +pub fn process( block_config: &BlockConfig, execution_context: ProcessExecutionContext, random_data: (Vec, u32), ) -> Result, SystemExecutionError> where - E: Environment, - E::Ext: ProcessorExternalities + BackendExternalities + 'static, - ::UnrecoverableError: BackendSyscallError, + Ext: ProcessorExternalities + BackendExternalities + 'static, + ::AllocError: + BackendAllocSyscallError, + RunFallibleError: From, + ::UnrecoverableError: BackendSyscallError, { use crate::precharge::SuccessfulDispatchResultKind::*; @@ -117,7 +120,7 @@ where outgoing_limit, ); - let exec_result = executor::execute_wasm::( + let exec_result = executor::execute_wasm::( balance, dispatch.clone(), execution_context, diff --git a/gcli/Cargo.toml b/gcli/Cargo.toml index 9d9821b9bf3..dc002755932 100644 --- a/gcli/Cargo.toml +++ b/gcli/Cargo.toml @@ -41,7 +41,6 @@ thiserror.workspace = true tokio = { workspace = true, features = [ "full" ] } whoami.workspace = true core-processor = { workspace = true, features = [ "std" ] } -gear-backend-sandbox = { workspace = true, features = [ "std" ] } gear-lazy-pages-common = { workspace = true, features = [ "std" ] } reqwest = { workspace = true, default-features = false, features = [ "json", "rustls-tls" ] } etc.workspace = true diff --git a/gcli/src/meta/mod.rs b/gcli/src/meta/mod.rs index 059d0fac8bc..721ab394eb8 100644 --- a/gcli/src/meta/mod.rs +++ b/gcli/src/meta/mod.rs @@ -114,10 +114,7 @@ impl Meta { )); sp_io::TestExternalities::default().execute_with(|| { - core_processor::informational::execute_for_reply::< - gear_backend_sandbox::SandboxEnvironment, - String, - >( + core_processor::informational::execute_for_reply::( method.into(), wasm, None, diff --git a/gtest/Cargo.toml b/gtest/Cargo.toml index fc1ab748a54..ee768bce827 100644 --- a/gtest/Cargo.toml +++ b/gtest/Cargo.toml @@ -9,7 +9,6 @@ license.workspace = true gear-core.workspace = true gear-core-errors.workspace = true gear-backend-common.workspace = true -gear-backend-sandbox = { workspace = true, features = ["std"] } core-processor = { workspace = true, features = ["std"] } gear-lazy-pages-common = { workspace = true, features = ["std"] } gear-wasm-builder.workspace = true diff --git a/gtest/src/manager.rs b/gtest/src/manager.rs index 54a41e9bd16..011db58dc03 100644 --- a/gtest/src/manager.rs +++ b/gtest/src/manager.rs @@ -29,7 +29,6 @@ use core_processor::{ configs::{BlockConfig, BlockInfo, PageCosts, TESTS_MAX_PAGES_NUMBER}, ContextChargedForCode, ContextChargedForInstrumentation, Ext, }; -use gear_backend_sandbox::SandboxEnvironment; use gear_core::{ code::{Code, CodeAndId, InstrumentedCode, InstrumentedCodeAndId, TryNewCodeConfig}, ids::{CodeId, MessageId, ProgramId, ReservationId}, @@ -463,7 +462,7 @@ impl ExtManager { .ok_or_else(|| TestError::ActorNotFound(*program_id))?; if let Some((_, program)) = actor.get_executable_actor_data() { - core_processor::informational::execute_for_reply::, _>( + core_processor::informational::execute_for_reply::( String::from("state"), program.code().clone(), Some(program.allocations().clone()), @@ -503,7 +502,7 @@ impl ExtManager { let mut mapping_code_payload = args.unwrap_or_default(); mapping_code_payload.append(&mut self.read_state_bytes(program_id)?); - core_processor::informational::execute_for_reply::, _>( + core_processor::informational::execute_for_reply::( String::from(fn_name), mapping_code, None, @@ -877,7 +876,7 @@ impl ExtManager { } }; - let journal = core_processor::process::>( + let journal = core_processor::process::( &block_config, (context, code, balance).into(), self.random_data.clone(), diff --git a/pallets/gear-debug/Cargo.toml b/pallets/gear-debug/Cargo.toml index f139f859414..6170e773756 100644 --- a/pallets/gear-debug/Cargo.toml +++ b/pallets/gear-debug/Cargo.toml @@ -37,7 +37,6 @@ pallet-authorship.workspace = true serde.workspace = true env_logger.workspace = true wabt.workspace = true -gear-backend-sandbox.workspace = true hex-literal.workspace = true frame-support-test = { workspace = true, features = ["std"] } pallet-timestamp = { workspace = true, features = ["std"] } diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index d41682ab355..5fa5ab0fcdd 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -85,7 +85,6 @@ use frame_support::{ traits::{Currency, Get, Hooks}, }; use frame_system::{Pallet as SystemPallet, RawOrigin}; -use gear_backend_common::Environment; use gear_backend_sandbox::{DefaultExecutorMemory, MemoryWrap}; use gear_core::{ code::{Code, CodeAndId}, diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 1363b7166a2..c80340c7996 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -85,8 +85,6 @@ use sp_std::{ prelude::*, }; -type ExecutionEnvironment = gear_backend_sandbox::SandboxEnvironment; - pub(crate) type AccountIdOf = ::AccountId; pub(crate) type CurrencyOf = ::Currency; pub(crate) type BalanceOf = as Currency>>::Balance; diff --git a/pallets/gear/src/queue.rs b/pallets/gear/src/queue.rs index 7bc4280f9a3..c4de95c7f11 100644 --- a/pallets/gear/src/queue.rs +++ b/pallets/gear/src/queue.rs @@ -137,7 +137,7 @@ where let (random, bn) = T::Randomness::random(dispatch_id.as_ref()); - let journal = core_processor::process::( + let journal = core_processor::process::( block_config, (context, code, balance).into(), (random.encode(), bn.unique_saturated_into()), diff --git a/pallets/gear/src/runtime_api.rs b/pallets/gear/src/runtime_api.rs index d3dc1d94a9a..8e615abd448 100644 --- a/pallets/gear/src/runtime_api.rs +++ b/pallets/gear/src/runtime_api.rs @@ -296,7 +296,7 @@ where timestamp: >::get().unique_saturated_into(), }; - core_processor::informational::execute_for_reply::, String>( + core_processor::informational::execute_for_reply::( function.into(), instrumented_code, None, @@ -325,7 +325,7 @@ where timestamp: >::get().unique_saturated_into(), }; - core_processor::informational::execute_for_reply::, String>( + core_processor::informational::execute_for_reply::( String::from("state"), instrumented_code, Some(allocations), @@ -351,7 +351,7 @@ where timestamp: >::get().unique_saturated_into(), }; - core_processor::informational::execute_for_reply::, String>( + core_processor::informational::execute_for_reply::( String::from("metahash"), instrumented_code, Some(allocations), diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index ac102195adf..4abba7d4ca8 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -622,7 +622,7 @@ test_gas_counter_injection! { #[test] fn test_sys_calls_table() { use gas_metering::ConstantCostRules; - use gear_backend_common::{mock::MockExt, ActorTerminationReason, BackendReport, Environment}; + use gear_backend_common::{mock::MockExt, ActorTerminationReason, BackendReport}; use gear_backend_sandbox::SandboxEnvironment; use gear_core::message::DispatchKind; use parity_wasm::builder; From e926946822083eece129f06ca60742c6e8ad43ec Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 13:34:52 +0300 Subject: [PATCH 08/41] Move `EnvironmentError` into sandbox backend --- core-backend/common/src/lib.rs | 28 +------------------------ core-backend/sandbox/src/env.rs | 37 +++++++++++++++++++++++++-------- core-processor/src/common.rs | 3 ++- core-processor/src/executor.rs | 20 ++++++++---------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 8cc66debe3d..79387d12bf4 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -36,13 +36,9 @@ use crate::runtime::RunFallibleError; use actor_system_error::actor_system_error; use alloc::{ collections::{BTreeMap, BTreeSet}, - string::String, vec::Vec, }; -use core::{ - convert::Infallible, - fmt::{Debug, Display}, -}; +use core::fmt::Debug; use gear_core::{ env::Externalities, gas::{ChargeError, CounterType, CountersOwner, GasAmount}, @@ -339,27 +335,5 @@ where pub ext: Ext, } -#[derive(Debug, derive_more::Display)] -pub enum EnvironmentError { - #[display(fmt = "Actor backend error: {_1}")] - Actor(GasAmount, String), - #[display(fmt = "System backend error: {_0}")] - System(EnvSystemError), - #[display(fmt = "Prepare error: {_1}")] - PrepareMemory(GasAmount, PrepareMemoryError), -} - -impl - EnvironmentError -{ - pub fn from_infallible(err: EnvironmentError) -> Self { - match err { - EnvironmentError::System(err) => Self::System(err), - EnvironmentError::PrepareMemory(_, err) => match err {}, - EnvironmentError::Actor(gas_amount, s) => Self::Actor(gas_amount, s), - } - } -} - #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index 435192fe8d3..8cd20c343df 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -35,10 +35,11 @@ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, EnvironmentError, LimitedStr, + BackendSyscallError, LimitedStr, }; use gear_core::{ env::Externalities, + gas::GasAmount, memory::HostPointer, message::{DispatchKind, WasmEntryPoint}, pages::{PageNumber, WasmPage}, @@ -164,13 +165,31 @@ fn store_host_state_mut( type EnvironmentBackendReport = BackendReport, Ext>; -pub type EnvironmentExecutionResult = Result< - EnvironmentBackendReport, - EnvironmentError, ->; +pub type EnvironmentExecutionResult = + Result, SandboxEnvironmentError>; #[derive(Debug, derive_more::Display)] -pub enum SandboxEnvironmentError { +pub enum SandboxEnvironmentError { + #[display(fmt = "Actor backend error: {_1}")] + Actor(GasAmount, String), + #[display(fmt = "System backend error: {_0}")] + System(SandboxSystemEnvironmentError), + #[display(fmt = "Prepare error: {_1}")] + PrepareMemory(GasAmount, PrepareMemoryError), +} + +impl SandboxEnvironmentError { + pub fn from_infallible(err: SandboxEnvironmentError) -> Self { + match err { + SandboxEnvironmentError::System(err) => Self::System(err), + SandboxEnvironmentError::PrepareMemory(_, err) => match err {}, + SandboxEnvironmentError::Actor(gas_amount, s) => Self::Actor(gas_amount, s), + } + } +} + +#[derive(Debug, derive_more::Display)] +pub enum SandboxSystemEnvironmentError { #[display(fmt = "Failed to create env memory: {_0:?}")] CreateEnvMemory(gear_sandbox::Error), #[display(fmt = "Globals are not supported")] @@ -347,9 +366,9 @@ where entry_point: EntryPoint, entries: BTreeSet, mem_size: WasmPage, - ) -> Result> { - use EnvironmentError::*; + ) -> Result> { use SandboxEnvironmentError::*; + use SandboxSystemEnvironmentError::*; let entry_forbidden = entry_point .try_into_kind() @@ -425,8 +444,8 @@ where ) -> Result<(), PrepareMemoryError>, PrepareMemoryError: Display, { - use EnvironmentError::*; use SandboxEnvironmentError::*; + use SandboxSystemEnvironmentError::*; let Self { mut instance, diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index 16d0f842f07..b6959badc93 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -28,6 +28,7 @@ use alloc::{ vec::Vec, }; use gear_backend_common::{SystemReservationContext, SystemTerminationReason, TrapExplanation}; +use gear_backend_sandbox::env::SandboxSystemEnvironmentError; use gear_core::{ gas::{GasAllowanceCounter, GasAmount, GasCounter}, ids::{CodeId, MessageId, ProgramId, ReservationId}, @@ -492,7 +493,7 @@ pub enum SystemExecutionError { PrepareMemory(SystemPrepareMemoryError), /// Environment error #[display(fmt = "Backend error: {_0}")] - Environment(String), + Environment(SandboxSystemEnvironmentError), /// Termination reason #[from] #[display(fmt = "Syscall function error: {_0}")] diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 90b5a47090e..6c734719ddd 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -35,9 +35,9 @@ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, EnvironmentError, TerminationReason, + BackendSyscallError, TerminationReason, }; -use gear_backend_sandbox::{MemoryWrap, SandboxEnvironment}; +use gear_backend_sandbox::{env::SandboxEnvironmentError, MemoryWrap, SandboxEnvironment}; use gear_core::{ code::InstrumentedCode, env::Externalities, @@ -233,7 +233,7 @@ where program.code().exports().clone(), memory_size, ) - .map_err(EnvironmentError::from_infallible)?; + .map_err(SandboxEnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { prepare_memory::>( memory, @@ -269,21 +269,19 @@ where (termination, memory, ext) } - Err(EnvironmentError::System(e)) => { - return Err(ExecutionError::System(SystemExecutionError::Environment( - e.to_string(), - ))) + Err(SandboxEnvironmentError::System(e)) => { + return Err(ExecutionError::System(SystemExecutionError::Environment(e))) } - Err(EnvironmentError::PrepareMemory(gas_amount, PrepareMemoryError::Actor(e))) => { + Err(SandboxEnvironmentError::PrepareMemory(gas_amount, PrepareMemoryError::Actor(e))) => { return Err(ExecutionError::Actor(ActorExecutionError { gas_amount, reason: ActorExecutionErrorReplyReason::PrepareMemory(e), })) } - Err(EnvironmentError::PrepareMemory(_gas_amount, PrepareMemoryError::System(e))) => { + Err(SandboxEnvironmentError::PrepareMemory(_gas_amount, PrepareMemoryError::System(e))) => { return Err(ExecutionError::System(e.into())); } - Err(EnvironmentError::Actor(gas_amount, err)) => { + Err(SandboxEnvironmentError::Actor(gas_amount, err)) => { log::trace!("ActorExecutionErrorReplyReason::Environment({err}) occurred"); return Err(ExecutionError::Actor(ActorExecutionError { gas_amount, @@ -430,7 +428,7 @@ where program.code().exports().clone(), memory_size, ) - .map_err(EnvironmentError::from_infallible)?; + .map_err(SandboxEnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { prepare_memory::>( memory, From 9b762ce5928e4ff0004923531b3512b596b18c3b Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 13:40:21 +0300 Subject: [PATCH 09/41] Move `ExtInfo` into core-processor --- core-backend/common/src/lib.rs | 29 ++------------- core-backend/common/src/mock.rs | 26 +------------ core-processor/src/executor.rs | 45 ---------------------- core-processor/src/ext.rs | 66 +++++++++++++++++++++------------ 4 files changed, 47 insertions(+), 119 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 79387d12bf4..574d3f545ce 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -34,19 +34,13 @@ pub mod runtime; use crate::runtime::RunFallibleError; use actor_system_error::actor_system_error; -use alloc::{ - collections::{BTreeMap, BTreeSet}, - vec::Vec, -}; use core::fmt::Debug; use gear_core::{ env::Externalities, gas::{ChargeError, CounterType, CountersOwner, GasAmount}, - ids::{CodeId, MessageId, ProgramId, ReservationId}, - memory::{Memory, MemoryError, MemoryInterval, PageBuf}, - message::{ContextStore, Dispatch, IncomingDispatch, MessageWaitedType}, - pages::{GearPage, WasmPage}, - reservation::GasReserver, + ids::ProgramId, + memory::MemoryInterval, + message::{IncomingDispatch, MessageWaitedType}, }; use memory::ProcessAccessError; use scale_info::scale::{self, Decode, Encode}; @@ -279,25 +273,8 @@ impl SystemReservationContext { } } -#[derive(Debug)] -pub struct ExtInfo { - pub gas_amount: GasAmount, - pub gas_reserver: GasReserver, - pub system_reservation_context: SystemReservationContext, - pub allocations: BTreeSet, - pub pages_data: BTreeMap, - pub generated_dispatches: Vec<(Dispatch, u32, Option)>, - pub awakening: Vec<(MessageId, u32)>, - pub reply_deposits: Vec<(MessageId, u64)>, - pub program_candidates_data: BTreeMap>, - pub program_rents: BTreeMap, - pub context_store: ContextStore, -} - /// Extended externalities that can manage gas counters. pub trait BackendExternalities: Externalities + CountersOwner { - fn into_ext_info(self, memory: &impl Memory) -> Result; - fn gas_amount(&self) -> GasAmount; /// Pre-process memory access if need. diff --git a/core-backend/common/src/mock.rs b/core-backend/common/src/mock.rs index 9efb5f4dc21..05f56f101b7 100644 --- a/core-backend/common/src/mock.rs +++ b/core-backend/common/src/mock.rs @@ -18,8 +18,7 @@ use crate::{ memory::ProcessAccessError, runtime::RunFallibleError, BackendAllocSyscallError, - BackendExternalities, BackendSyscallError, ExtInfo, SystemReservationContext, - UndefinedTerminationReason, + BackendExternalities, BackendSyscallError, UndefinedTerminationReason, }; use alloc::{collections::BTreeSet, vec, vec::Vec}; use core::{cell::Cell, fmt, fmt::Debug}; @@ -29,9 +28,8 @@ use gear_core::{ gas::{ChargeError, CounterType, CountersOwner, GasAmount, GasCounter, GasLeft}, ids::{MessageId, ProgramId, ReservationId}, memory::{Memory, MemoryError, MemoryInterval}, - message::{HandlePacket, IncomingDispatch, InitPacket, ReplyPacket}, + message::{HandlePacket, InitPacket, ReplyPacket}, pages::{PageNumber, PageU32Size, WasmPage, WASM_PAGE_SIZE}, - reservation::GasReserver, }; use gear_core_errors::{ReplyCode, SignalCode}; use gear_wasm_instrument::syscalls::SysCallName; @@ -277,26 +275,6 @@ impl Externalities for MockExt { } impl BackendExternalities for MockExt { - fn into_ext_info(self, _memory: &impl Memory) -> Result { - Ok(ExtInfo { - gas_amount: GasCounter::new(0).to_amount(), - gas_reserver: GasReserver::new( - &::default(), - Default::default(), - 1024, - ), - system_reservation_context: SystemReservationContext::default(), - allocations: Default::default(), - pages_data: Default::default(), - generated_dispatches: Default::default(), - awakening: Default::default(), - reply_deposits: Default::default(), - program_candidates_data: Default::default(), - program_rents: Default::default(), - context_store: Default::default(), - }) - } - fn gas_amount(&self) -> GasAmount { GasCounter::new(0).to_amount() } diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 6c734719ddd..fc9b804f1bd 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -497,51 +497,6 @@ where #[cfg(test)] mod tests { use super::*; - use gear_backend_common::lazy_pages::Status; - use gear_core::pages::WasmPage; - - struct TestExt; - struct LazyTestExt; - - impl ProcessorExternalities for TestExt { - fn new(_context: ProcessorContext) -> Self { - Self - } - - fn lazy_pages_init_for_program( - _mem: &mut impl Memory, - _prog_id: ProgramId, - _stack_end: Option, - _globals_config: GlobalsAccessConfig, - _lazy_pages_weights: LazyPagesWeights, - ) { - } - - fn lazy_pages_post_execution_actions(_mem: &mut impl Memory) {} - fn lazy_pages_status() -> Status { - Status::Normal - } - } - - impl ProcessorExternalities for LazyTestExt { - fn new(_context: ProcessorContext) -> Self { - Self - } - - fn lazy_pages_init_for_program( - _mem: &mut impl Memory, - _prog_id: ProgramId, - _stack_end: Option, - _globals_config: GlobalsAccessConfig, - _lazy_pages_weights: LazyPagesWeights, - ) { - } - - fn lazy_pages_post_execution_actions(_mem: &mut impl Memory) {} - fn lazy_pages_status() -> Status { - Status::Normal - } - } #[test] fn check_memory_insufficient() { diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 7d8098dd92e..7b35304ddff 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -26,7 +26,7 @@ use gear_backend_common::{ memory::ProcessAccessError, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - ExtInfo, SystemReservationContext, TrapExplanation, UndefinedTerminationReason, + SystemReservationContext, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, }; @@ -42,10 +42,10 @@ use gear_core::{ AllocError, AllocationsContext, GrowHandler, Memory, MemoryError, MemoryInterval, PageBuf, }, message::{ - ContextOutcomeDrain, GasLimit, HandlePacket, InitPacket, MessageContext, Packet, - ReplyPacket, + ContextOutcomeDrain, ContextStore, Dispatch, GasLimit, HandlePacket, InitPacket, + MessageContext, Packet, ReplyPacket, }, - pages::{PageU32Size, WasmPage}, + pages::{GearPage, PageU32Size, WasmPage}, reservation::GasReserver, }; use gear_core_errors::{ @@ -152,12 +152,30 @@ impl ProcessorContext { } } +#[derive(Debug)] +pub struct ExtInfo { + pub gas_amount: GasAmount, + pub gas_reserver: GasReserver, + pub system_reservation_context: SystemReservationContext, + pub allocations: BTreeSet, + pub pages_data: BTreeMap, + pub generated_dispatches: Vec<(Dispatch, u32, Option)>, + pub awakening: Vec<(MessageId, u32)>, + pub reply_deposits: Vec<(MessageId, u64)>, + pub program_candidates_data: BTreeMap>, + pub program_rents: BTreeMap, + pub context_store: ContextStore, +} + /// Trait to which ext must have to work in processor wasm executor. /// Currently used only for lazy-pages support. pub trait ProcessorExternalities { /// Create new fn new(context: ProcessorContext) -> Self; + /// Convert externalities into [`ExtInfo`]. + fn into_ext_info(self, memory: &impl Memory) -> Result; + /// Protect and save storage keys for pages which has no data fn lazy_pages_init_for_program( mem: &mut impl Memory, @@ -345,26 +363,6 @@ impl ProcessorExternalities for Ext { } } - fn lazy_pages_init_for_program( - mem: &mut impl Memory, - prog_id: ProgramId, - stack_end: Option, - globals_config: GlobalsAccessConfig, - lazy_pages_weights: LazyPagesWeights, - ) { - lazy_pages::init_for_program(mem, prog_id, stack_end, globals_config, lazy_pages_weights); - } - - fn lazy_pages_post_execution_actions(mem: &mut impl Memory) { - lazy_pages::remove_lazy_pages_prot(mem); - } - - fn lazy_pages_status() -> Status { - lazy_pages::get_status() - } -} - -impl BackendExternalities for Ext { fn into_ext_info(self, memory: &impl Memory) -> Result { let ProcessorContext { allocations_context, @@ -429,6 +427,26 @@ impl BackendExternalities for Ext { Ok(info) } + fn lazy_pages_init_for_program( + mem: &mut impl Memory, + prog_id: ProgramId, + stack_end: Option, + globals_config: GlobalsAccessConfig, + lazy_pages_weights: LazyPagesWeights, + ) { + lazy_pages::init_for_program(mem, prog_id, stack_end, globals_config, lazy_pages_weights); + } + + fn lazy_pages_post_execution_actions(mem: &mut impl Memory) { + lazy_pages::remove_lazy_pages_prot(mem); + } + + fn lazy_pages_status() -> Status { + lazy_pages::get_status() + } +} + +impl BackendExternalities for Ext { fn gas_amount(&self) -> GasAmount { self.context.gas_counter.to_amount() } From eab6671c723b58fd09f83879dff9432c54cfdf66 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 13:42:42 +0300 Subject: [PATCH 10/41] Move `SystemReservationContext` into core-processor --- core-backend/common/src/lib.rs | 26 +------------------------- core-processor/src/common.rs | 5 +++-- core-processor/src/context.rs | 24 ++++++++++++++++++++++++ core-processor/src/ext.rs | 10 ++++++---- core-processor/src/precharge.rs | 5 +++-- core-processor/src/processing.rs | 1 - 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 574d3f545ce..f151a95c27c 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -40,7 +40,7 @@ use gear_core::{ gas::{ChargeError, CounterType, CountersOwner, GasAmount}, ids::ProgramId, memory::MemoryInterval, - message::{IncomingDispatch, MessageWaitedType}, + message::MessageWaitedType, }; use memory::ProcessAccessError; use scale_info::scale::{self, Decode, Encode}; @@ -249,30 +249,6 @@ pub enum TrapExplanation { Unknown, } -#[derive(Debug, Default)] -pub struct SystemReservationContext { - /// Reservation created in current execution. - pub current_reservation: Option, - /// Reservation from `ContextStore`. - pub previous_reservation: Option, -} - -impl SystemReservationContext { - pub fn from_dispatch(dispatch: &IncomingDispatch) -> Self { - Self { - current_reservation: None, - previous_reservation: dispatch - .context() - .as_ref() - .and_then(|ctx| ctx.system_reservation()), - } - } - - pub fn has_any(&self) -> bool { - self.current_reservation.is_some() || self.previous_reservation.is_some() - } -} - /// Extended externalities that can manage gas counters. pub trait BackendExternalities: Externalities + CountersOwner { fn gas_amount(&self) -> GasAmount; diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index b6959badc93..85c3bbfe789 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -19,7 +19,8 @@ //! Common structures for processing. use crate::{ - executor::SystemPrepareMemoryError, precharge::PreChargeGasOperation, ActorPrepareMemoryError, + context::SystemReservationContext, executor::SystemPrepareMemoryError, + precharge::PreChargeGasOperation, ActorPrepareMemoryError, }; use actor_system_error::actor_system_error; use alloc::{ @@ -27,7 +28,7 @@ use alloc::{ string::String, vec::Vec, }; -use gear_backend_common::{SystemReservationContext, SystemTerminationReason, TrapExplanation}; +use gear_backend_common::{SystemTerminationReason, TrapExplanation}; use gear_backend_sandbox::env::SandboxSystemEnvironmentError; use gear_core::{ gas::{GasAllowanceCounter, GasAmount, GasCounter}, diff --git a/core-processor/src/context.rs b/core-processor/src/context.rs index 7f647aec2c0..0946b1d35d1 100644 --- a/core-processor/src/context.rs +++ b/core-processor/src/context.rs @@ -157,3 +157,27 @@ impl ProcessExecutionContext { &self.program } } + +#[derive(Debug, Default)] +pub struct SystemReservationContext { + /// Reservation created in current execution. + pub current_reservation: Option, + /// Reservation from `ContextStore`. + pub previous_reservation: Option, +} + +impl SystemReservationContext { + pub fn from_dispatch(dispatch: &IncomingDispatch) -> Self { + Self { + current_reservation: None, + previous_reservation: dispatch + .context() + .as_ref() + .and_then(|ctx| ctx.system_reservation()), + } + } + + pub fn has_any(&self) -> bool { + self.current_reservation.is_some() || self.previous_reservation.is_some() + } +} diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 7b35304ddff..09105b61b41 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -16,7 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::configs::{BlockInfo, PageCosts}; +use crate::{ + configs::{BlockInfo, PageCosts}, + context::SystemReservationContext, +}; use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, @@ -26,9 +29,8 @@ use gear_backend_common::{ memory::ProcessAccessError, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - SystemReservationContext, TrapExplanation, UndefinedTerminationReason, - UnrecoverableExecutionError, UnrecoverableExtError as UnrecoverableExtErrorCore, - UnrecoverableWaitError, + TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, }; use gear_core::{ costs::{HostFnWeights, RuntimeCosts}, diff --git a/core-processor/src/precharge.rs b/core-processor/src/precharge.rs index faad95e95d2..ccb9b0eb8bb 100644 --- a/core-processor/src/precharge.rs +++ b/core-processor/src/precharge.rs @@ -20,14 +20,15 @@ use crate::{ PrechargedDispatch, }, configs::{BlockConfig, PageCosts}, - context::{ContextChargedForCodeLength, ContextChargedForMemory, ContextData}, + context::{ + ContextChargedForCodeLength, ContextChargedForMemory, ContextData, SystemReservationContext, + }, processing::{ process_allowance_exceed, process_error, process_non_executable, process_success, }, ContextChargedForCode, ContextChargedForInstrumentation, }; use alloc::{collections::BTreeSet, vec::Vec}; -use gear_backend_common::SystemReservationContext; use gear_core::{ gas::{ChargeResult, GasAllowanceCounter, GasCounter}, ids::ProgramId, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index b1c90982ff9..c4a32bc969a 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -30,7 +30,6 @@ use crate::{ use alloc::{string::ToString, vec::Vec}; use gear_backend_common::{ runtime::RunFallibleError, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - SystemReservationContext, }; use gear_core::{ env::Externalities, From 7f23ea5fe7f60959c607483b2ddcca1cd38aa753 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 14:16:29 +0300 Subject: [PATCH 11/41] Move `BackendReport` into sandbox backend --- core-backend/common/src/lib.rs | 9 --------- core-backend/sandbox/src/env.rs | 17 ++++++++++++----- core-processor/src/executor.rs | 9 ++++++--- utils/wasm-gen/src/tests.rs | 4 ++-- utils/wasm-instrument/src/tests.rs | 4 ++-- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index f151a95c27c..41e66647645 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -279,14 +279,5 @@ pub trait BackendAllocSyscallError: Sized { fn into_backend_error(self) -> Result; } -pub struct BackendReport -where - Ext: Externalities, -{ - pub termination_reason: TerminationReason, - pub memory_wrap: EnvMem, - pub ext: Ext, -} - #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index 8cd20c343df..75d1a8ee0d5 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -34,8 +34,8 @@ use core::{ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, LimitedStr, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + LimitedStr, TerminationReason, }; use gear_core::{ env::Externalities, @@ -163,10 +163,8 @@ fn store_host_state_mut( .unwrap_or_else(|| unreachable!("State must be set in `WasmiEnvironment::new`; qed")) } -type EnvironmentBackendReport = BackendReport, Ext>; - pub type EnvironmentExecutionResult = - Result, SandboxEnvironmentError>; + Result, SandboxEnvironmentError>; #[derive(Debug, derive_more::Display)] pub enum SandboxEnvironmentError { @@ -211,6 +209,15 @@ where memory: DefaultExecutorMemory, } +pub struct BackendReport +where + Ext: Externalities + 'static, +{ + pub termination_reason: TerminationReason, + pub memory_wrap: MemoryWrap, + pub ext: Ext, +} + // A helping wrapper for `EnvironmentDefinitionBuilder` and `forbidden_funcs`. // It makes adding functions to `EnvironmentDefinitionBuilder` shorter. struct EnvBuilder { diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index fc9b804f1bd..7ae6adb43db 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -34,10 +34,13 @@ use alloc::{ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendReport, - BackendSyscallError, TerminationReason, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + TerminationReason, +}; +use gear_backend_sandbox::{ + env::{BackendReport, SandboxEnvironmentError}, + MemoryWrap, SandboxEnvironment, }; -use gear_backend_sandbox::{env::SandboxEnvironmentError, MemoryWrap, SandboxEnvironment}; use gear_core::{ code::InstrumentedCode, env::Externalities, diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index ab09d5081e8..dfddf192075 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -18,8 +18,8 @@ use super::*; use arbitrary::Unstructured; -use gear_backend_common::{BackendReport, Environment, TerminationReason, TrapExplanation}; -use gear_backend_sandbox::SandboxEnvironment; +use gear_backend_common::{TerminationReason, TrapExplanation}; +use gear_backend_sandbox::env::BackendReport; use gear_core::{ code::Code, ids::{CodeId, ProgramId}, diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 4abba7d4ca8..201428edf5f 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -622,8 +622,8 @@ test_gas_counter_injection! { #[test] fn test_sys_calls_table() { use gas_metering::ConstantCostRules; - use gear_backend_common::{mock::MockExt, ActorTerminationReason, BackendReport}; - use gear_backend_sandbox::SandboxEnvironment; + use gear_backend_common::{mock::MockExt, ActorTerminationReason}; + use gear_backend_sandbox::{env::BackendReport, SandboxEnvironment}; use gear_core::message::DispatchKind; use parity_wasm::builder; From 001d5b6684f537e6067be4119c0dec5ea8790142 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 14:48:00 +0300 Subject: [PATCH 12/41] Remove `MemoryAccessRecorder` --- core-backend/common/src/memory.rs | 33 +++---------- core-backend/sandbox/src/funcs.rs | 74 ++++++++++++++--------------- core-backend/sandbox/src/runtime.rs | 35 ++------------ 3 files changed, 46 insertions(+), 96 deletions(-) diff --git a/core-backend/common/src/memory.rs b/core-backend/common/src/memory.rs index 59a0a0a2fc3..686a194a47a 100644 --- a/core-backend/common/src/memory.rs +++ b/core-backend/common/src/memory.rs @@ -98,27 +98,6 @@ impl BackendSyscallError for MemoryAccessError { } } -/// Memory accesses recorder/registrar, which allow to register new accesses. -pub trait MemoryAccessRecorder { - /// Register new read access. - fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead; - - /// Register new read static size type access. - fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs; - - /// Register new read decoded type access. - fn register_read_decoded( - &mut self, - ptr: u32, - ) -> WasmMemoryReadDecoded; - - /// Register new write access. - fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite; - - /// Register new write static size access. - fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs; -} - pub trait MemoryOwner { /// Read from owned memory to new byte vector. fn read(&mut self, read: WasmMemoryRead) -> Result, MemoryAccessError>; @@ -177,15 +156,15 @@ impl Default for MemoryAccessManager { } } -impl MemoryAccessRecorder for MemoryAccessManager { - fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead { +impl MemoryAccessManager { + pub fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead { if size > 0 { self.reads.push(MemoryInterval { offset: ptr, size }); } WasmMemoryRead { ptr, size } } - fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs { + pub fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs { let size = size_of::() as u32; if size > 0 { self.reads.push(MemoryInterval { offset: ptr, size }); @@ -196,7 +175,7 @@ impl MemoryAccessRecorder for MemoryAccessManager { } } - fn register_read_decoded( + pub fn register_read_decoded( &mut self, ptr: u32, ) -> WasmMemoryReadDecoded { @@ -210,14 +189,14 @@ impl MemoryAccessRecorder for MemoryAccessManager { } } - fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite { + pub fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite { if size > 0 { self.writes.push(MemoryInterval { offset: ptr, size }); } WasmMemoryWrite { ptr, size } } - fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs { + pub fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs { let size = size_of::() as u32; if size > 0 { self.writes.push(MemoryInterval { offset: ptr, size }); diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index c2fe5777d7e..b84df77a056 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -24,7 +24,7 @@ use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; use gear_backend_common::{ - memory::{MemoryAccessError, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead}, + memory::{MemoryAccessError, MemoryOwner, WasmMemoryRead}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, @@ -100,7 +100,7 @@ where value_ptr: u32, ) -> Result { if value_ptr != PTR_SPECIAL { - let read_value = ctx.register_read_decoded(value_ptr); + let read_value = ctx.manager.register_read_decoded(value_ptr); return ctx.read_decoded(read_value); } @@ -128,8 +128,8 @@ where len: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_hash_val = ctx.register_read_as(pid_value_ptr); - let read_payload = ctx.register_read(payload_ptr, len); + let read_hash_val = ctx.manager.register_read_as(pid_value_ptr); + let read_payload = ctx.manager.register_read(payload_ptr, len); let HashWithValue { hash: destination, value, @@ -149,7 +149,7 @@ where pid_value_ptr: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_pid_value = ctx.register_read_as(pid_value_ptr); + let read_pid_value = ctx.manager.register_read_as(pid_value_ptr); let HashWithValue { hash: destination, value, @@ -177,7 +177,7 @@ where payload_ptr: u32, len: u32, ) -> Result<(u64, ()), HostError> { - let read_payload = ctx.register_read(payload_ptr, len); + let read_payload = ctx.manager.register_read(payload_ptr, len); let payload = ctx.read(read_payload)?; ctx.ext_mut() @@ -194,8 +194,8 @@ where len: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_rid_pid_value = ctx.register_read_as(rid_pid_value_ptr); - let read_payload = ctx.register_read(payload_ptr, len); + let read_rid_pid_value = ctx.manager.register_read_as(rid_pid_value_ptr); + let read_payload = ctx.manager.register_read(payload_ptr, len); let TwoHashesWithValue { hash1: reservation_id, hash2: destination, @@ -220,7 +220,7 @@ where rid_pid_value_ptr: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_rid_pid_value = ctx.register_read_as(rid_pid_value_ptr); + let read_rid_pid_value = ctx.manager.register_read_as(rid_pid_value_ptr); let TwoHashesWithValue { hash1: reservation_id, hash2: destination, @@ -248,7 +248,7 @@ where let payload_lock = ctx.ext_mut().lock_payload(at, len)?; payload_lock .drop_with::(|payload_access| { - let write_buffer = ctx.register_write(buffer_ptr, len); + let write_buffer = ctx.manager.register_write(buffer_ptr, len); let write_res = ctx.write(write_buffer, payload_access.as_slice()); let unlock_bound = ctx.ext_mut().unlock_payload(payload_access.into_lock()); @@ -266,7 +266,7 @@ where ) -> Result<(u64, ()), HostError> { let size = ctx.ext_mut().size()? as u32; - let write_size = ctx.register_write_as(size_ptr); + let write_size = ctx.manager.register_write_as(size_ptr); ctx.write_as(write_size, size.to_le_bytes()) .map_err(Into::into) } @@ -277,7 +277,7 @@ where gas: u64, inheritor_id_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_inheritor_id = ctx.register_read_decoded(inheritor_id_ptr); + let read_inheritor_id = ctx.manager.register_read_decoded(inheritor_id_ptr); let inheritor_id = ctx.read_decoded(read_inheritor_id)?; Err(ActorTerminationReason::Exit(inheritor_id).into()) } @@ -359,7 +359,7 @@ where ) -> Result<(u64, ()), HostError> { let height = ctx.ext_mut().block_height()?; - let write_height = ctx.register_write_as(height_ptr); + let write_height = ctx.manager.register_write_as(height_ptr); ctx.write_as(write_height, height.to_le_bytes()) .map_err(Into::into) } @@ -372,7 +372,7 @@ where ) -> Result<(u64, ()), HostError> { let timestamp = ctx.ext_mut().block_timestamp()?; - let write_timestamp = ctx.register_write_as(timestamp_ptr); + let write_timestamp = ctx.manager.register_write_as(timestamp_ptr); ctx.write_as(write_timestamp, timestamp.to_le_bytes()) .map_err(Into::into) } @@ -384,8 +384,8 @@ where subject_ptr: u32, bn_random_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_subject = ctx.register_read_decoded(subject_ptr); - let write_bn_random = ctx.register_write_as(bn_random_ptr); + let read_subject = ctx.manager.register_read_decoded(subject_ptr); + let write_bn_random = ctx.manager.register_write_as(bn_random_ptr); let raw_subject: Hash = ctx.read_decoded(read_subject)?; @@ -407,7 +407,7 @@ where len: u32, value_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_payload = ctx.register_read(payload_ptr, len); + let read_payload = ctx.manager.register_read(payload_ptr, len); let value = Self::register_and_read_value(ctx, value_ptr)?; let payload = Self::read_message_payload(ctx, read_payload)?; @@ -437,8 +437,8 @@ where payload_ptr: u32, len: u32, ) -> Result<(u64, ()), HostError> { - let read_rid_value = ctx.register_read_as(rid_value_ptr); - let read_payload = ctx.register_read(payload_ptr, len); + let read_rid_value = ctx.manager.register_read_as(rid_value_ptr); + let read_payload = ctx.manager.register_read(payload_ptr, len); let HashWithValue { hash: reservation_id, value, @@ -456,7 +456,7 @@ where gas: u64, rid_value_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_rid_value = ctx.register_read_as(rid_value_ptr); + let read_rid_value = ctx.manager.register_read_as(rid_value_ptr); let HashWithValue { hash: reservation_id, value, @@ -491,7 +491,7 @@ where payload_ptr: u32, len: u32, ) -> Result<(u64, ()), HostError> { - let read_payload = ctx.register_read(payload_ptr, len); + let read_payload = ctx.manager.register_read(payload_ptr, len); let payload = ctx.read(read_payload)?; ctx.ext_mut().reply_push(&payload).map_err(Into::into) @@ -540,7 +540,7 @@ where delay: u32, ) -> Result<(u64, ()), HostError> { // Charge for `len` inside `send_push_input` - let read_pid_value = ctx.register_read_as(pid_value_ptr); + let read_pid_value = ctx.manager.register_read_as(pid_value_ptr); let HashWithValue { hash: destination, value, @@ -579,7 +579,7 @@ where data_ptr: u32, data_len: u32, ) -> Result<(u64, ()), HostError> { - let read_data = ctx.register_read(data_ptr, data_len); + let read_data = ctx.manager.register_read(data_ptr, data_len); let data: RuntimeBuffer = ctx .read(read_data)? .try_into() @@ -603,7 +603,7 @@ where data_ptr: u32, data_len: u32, ) -> Result<(u64, ()), HostError> { - let read_data = ctx.register_read(data_ptr, data_len); + let read_data = ctx.manager.register_read(data_ptr, data_len); let data = ctx.read(read_data).unwrap_or_default(); let s = String::from_utf8_lossy(&data).to_string(); @@ -635,7 +635,7 @@ where message_id_ptr: u32, gas_value: u64, ) -> Result<(u64, ()), HostError> { - let read_message_id = ctx.register_read_decoded(message_id_ptr); + let read_message_id = ctx.manager.register_read_decoded(message_id_ptr); let message_id = ctx.read_decoded(read_message_id)?; ctx.ext_mut() @@ -649,7 +649,7 @@ where gas: u64, reservation_id_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_reservation_id = ctx.register_read_decoded(reservation_id_ptr); + let read_reservation_id = ctx.manager.register_read_decoded(reservation_id_ptr); let reservation_id = ctx.read_decoded(read_reservation_id)?; ctx.ext_mut() @@ -676,7 +676,7 @@ where ) -> Result<(u64, ()), HostError> { let gas_available = ctx.ext_mut().gas_available()?; - let write_gas = ctx.register_write_as(gas_ptr); + let write_gas = ctx.manager.register_write_as(gas_ptr); ctx.write_as(write_gas, gas_available.to_le_bytes()) .map_err(Into::into) } @@ -689,7 +689,7 @@ where ) -> Result<(u64, ()), HostError> { let message_id = ctx.ext_mut().message_id()?; - let write_message_id = ctx.register_write_as(message_id_ptr); + let write_message_id = ctx.manager.register_write_as(message_id_ptr); ctx.write_as(write_message_id, message_id.into_bytes()) .map_err(Into::into) } @@ -702,7 +702,7 @@ where ) -> Result<(u64, ()), HostError> { let program_id = ctx.ext_mut().program_id()?; - let write_program_id = ctx.register_write_as(program_id_ptr); + let write_program_id = ctx.manager.register_write_as(program_id_ptr); ctx.write_as(write_program_id, program_id.into_bytes()) .map_err(Into::into) } @@ -713,7 +713,7 @@ where gas: u64, rent_pid_ptr: u32, ) -> Result<(u64, ()), HostError> { - let read_rent_pid = ctx.register_read_as(rent_pid_ptr); + let read_rent_pid = ctx.manager.register_read_as(rent_pid_ptr); let HashWithValue { hash: program_id, @@ -733,7 +733,7 @@ where ) -> Result<(u64, ()), HostError> { let source = ctx.ext_mut().source()?; - let write_source = ctx.register_write_as(source_ptr); + let write_source = ctx.manager.register_write_as(source_ptr); ctx.write_as(write_source, source.into_bytes()) .map_err(Into::into) } @@ -746,7 +746,7 @@ where ) -> Result<(u64, ()), HostError> { let value = ctx.ext_mut().value()?; - let write_value = ctx.register_write_as(value_ptr); + let write_value = ctx.manager.register_write_as(value_ptr); ctx.write_as(write_value, value.to_le_bytes()) .map_err(Into::into) } @@ -759,7 +759,7 @@ where ) -> Result<(u64, ()), HostError> { let value_available = ctx.ext_mut().value_available()?; - let write_value = ctx.register_write_as(value_ptr); + let write_value = ctx.manager.register_write_as(value_ptr); ctx.write_as(write_value, value_available.to_le_bytes()) .map_err(Into::into) } @@ -806,7 +806,7 @@ where message_id_ptr: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_message_id = ctx.register_read_decoded(message_id_ptr); + let read_message_id = ctx.manager.register_read_decoded(message_id_ptr); let message_id = ctx.read_decoded(read_message_id)?; ctx.ext_mut().wake(message_id, delay).map_err(Into::into) @@ -824,9 +824,9 @@ where payload_len: u32, delay: u32, ) -> Result<(u64, ()), HostError> { - let read_cid_value = ctx.register_read_as(cid_value_ptr); - let read_salt = ctx.register_read(salt_ptr, salt_len); - let read_payload = ctx.register_read(payload_ptr, payload_len); + let read_cid_value = ctx.manager.register_read_as(cid_value_ptr); + let read_salt = ctx.manager.register_read(salt_ptr, salt_len); + let read_payload = ctx.manager.register_read(payload_ptr, payload_len); let HashWithValue { hash: code_id, value, diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index 5e18c58bead..d72927f8d37 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -27,8 +27,8 @@ use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; use gear_backend_common::{ memory::{ - MemoryAccessError, MemoryAccessManager, MemoryAccessRecorder, MemoryOwner, WasmMemoryRead, - WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, + MemoryAccessError, MemoryAccessManager, MemoryOwner, WasmMemoryRead, WasmMemoryReadAs, + WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, runtime::RunFallibleError, BackendAllocSyscallError, BackendExternalities, UndefinedTerminationReason, @@ -123,7 +123,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { let res = ctx.process_fallible_func_result(res)?; // TODO: move above or make normal process memory access. - let write_res = ctx.register_write_as::(res_ptr); + let write_res = ctx.manager.register_write_as::(res_ptr); ctx.write_as(write_res, R::from(res)).map_err(Into::into) }, @@ -137,9 +137,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { self.caller.data_mut().replace(state); res } -} -impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { #[track_caller] pub fn prepare( caller: &'a mut Caller<'b, HostState>, @@ -185,34 +183,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { .decrease_current_counter_to(gas_counter); res } -} - -impl<'a, 'b, Ext> MemoryAccessRecorder for CallerWrap<'a, 'b, Ext> { - fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead { - self.manager.register_read(ptr, size) - } - - fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs { - self.manager.register_read_as(ptr) - } - - fn register_read_decoded( - &mut self, - ptr: u32, - ) -> WasmMemoryReadDecoded { - self.manager.register_read_decoded(ptr) - } - - fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite { - self.manager.register_write(ptr, size) - } - - fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs { - self.manager.register_write_as(ptr) - } -} -impl CallerWrap<'_, '_, Ext> { pub fn set_termination_reason(&mut self, reason: UndefinedTerminationReason) { self.host_state_mut().termination_reason = reason; } From d6c7451ea0fe1dd32c30939aa0fda196527fdbaa Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 8 Sep 2023 14:50:16 +0300 Subject: [PATCH 13/41] Remove `MemoryOwner` --- core-backend/common/src/memory.rs | 24 ------------------------ core-backend/sandbox/src/funcs.rs | 2 +- core-backend/sandbox/src/runtime.rs | 14 +++++++------- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/core-backend/common/src/memory.rs b/core-backend/common/src/memory.rs index 686a194a47a..c0811b47263 100644 --- a/core-backend/common/src/memory.rs +++ b/core-backend/common/src/memory.rs @@ -98,30 +98,6 @@ impl BackendSyscallError for MemoryAccessError { } } -pub trait MemoryOwner { - /// Read from owned memory to new byte vector. - fn read(&mut self, read: WasmMemoryRead) -> Result, MemoryAccessError>; - - /// Read from owned memory to new object `T`. - fn read_as(&mut self, read: WasmMemoryReadAs) -> Result; - - /// Read from owned memory and decoded data into object `T`. - fn read_decoded( - &mut self, - read: WasmMemoryReadDecoded, - ) -> Result; - - /// Write data from `buff` to owned memory. - fn write(&mut self, write: WasmMemoryWrite, buff: &[u8]) -> Result<(), MemoryAccessError>; - - /// Write data from `obj` to owned memory. - fn write_as( - &mut self, - write: WasmMemoryWriteAs, - obj: T, - ) -> Result<(), MemoryAccessError>; -} - /// Memory access manager. Allows to pre-register memory accesses, /// and pre-process, them together. For example: /// ```ignore diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index b84df77a056..9ccaadf3ddf 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -24,7 +24,7 @@ use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; use gear_backend_common::{ - memory::{MemoryAccessError, MemoryOwner, WasmMemoryRead}, + memory::{MemoryAccessError, WasmMemoryRead}, runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index d72927f8d37..391d3ea7f8d 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -27,7 +27,7 @@ use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; use gear_backend_common::{ memory::{ - MemoryAccessError, MemoryAccessManager, MemoryOwner, WasmMemoryRead, WasmMemoryReadAs, + MemoryAccessError, MemoryAccessManager, WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, runtime::RunFallibleError, @@ -219,16 +219,16 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { } } -impl<'a, 'b, Ext: BackendExternalities + 'static> MemoryOwner for CallerWrap<'a, 'b, Ext> { - fn read(&mut self, read: WasmMemoryRead) -> Result, MemoryAccessError> { +impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { + pub fn read(&mut self, read: WasmMemoryRead) -> Result, MemoryAccessError> { self.with_memory(|manager, memory, gas_left| manager.read(memory, read, gas_left)) } - fn read_as(&mut self, read: WasmMemoryReadAs) -> Result { + pub fn read_as(&mut self, read: WasmMemoryReadAs) -> Result { self.with_memory(|manager, memory, gas_left| manager.read_as(memory, read, gas_left)) } - fn read_decoded( + pub fn read_decoded( &mut self, read: WasmMemoryReadDecoded, ) -> Result { @@ -237,13 +237,13 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> MemoryOwner for CallerWrap<'a, }) } - fn write(&mut self, write: WasmMemoryWrite, buff: &[u8]) -> Result<(), MemoryAccessError> { + pub fn write(&mut self, write: WasmMemoryWrite, buff: &[u8]) -> Result<(), MemoryAccessError> { self.with_memory(move |manager, memory, gas_left| { manager.write(memory, write, buff, gas_left) }) } - fn write_as( + pub fn write_as( &mut self, write: WasmMemoryWriteAs, obj: T, From 1c2239bb9dfe3e9898f196a69e53d8dfb0218038 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 22:36:34 +0300 Subject: [PATCH 14/41] Move `memory.rs` into sandbox backend --- common/lazy-pages/src/lib.rs | 3 +- core-backend/common/src/lazy_pages.rs | 15 +- core-backend/common/src/lib.rs | 27 +- core-backend/common/src/memory.rs | 380 ------------------ core-backend/sandbox/Cargo.toml | 4 + core-backend/sandbox/src/env.rs | 5 +- core-backend/sandbox/src/funcs.rs | 12 +- core-backend/sandbox/src/lib.rs | 24 ++ core-backend/sandbox/src/memory.rs | 347 +++++++++++++++- core-backend/{common => sandbox}/src/mock.rs | 13 +- core-backend/sandbox/src/runtime.rs | 16 +- core-backend/sandbox/src/state.rs | 4 +- core-backend/{common => sandbox}/src/tests.rs | 18 +- core-processor/src/executor.rs | 5 +- core-processor/src/ext.rs | 8 +- core-processor/src/processing.rs | 3 +- lazy-pages/src/host_func.rs | 2 +- runtime-interface/src/lib.rs | 3 +- utils/wasm-instrument/Cargo.toml | 2 +- utils/wasm-instrument/src/tests.rs | 4 +- 20 files changed, 433 insertions(+), 462 deletions(-) delete mode 100644 core-backend/common/src/memory.rs rename core-backend/{common => sandbox}/src/mock.rs (97%) rename core-backend/{common => sandbox}/src/tests.rs (97%) diff --git a/common/lazy-pages/src/lib.rs b/common/lazy-pages/src/lib.rs index 526717ddc61..2abfe100fb8 100644 --- a/common/lazy-pages/src/lib.rs +++ b/common/lazy-pages/src/lib.rs @@ -25,8 +25,7 @@ extern crate alloc; use byteorder::{ByteOrder, LittleEndian}; use core::fmt; use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, Status}, - memory::ProcessAccessError, + lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}, LimitedStr, }; use gear_common::Origin; diff --git a/core-backend/common/src/lazy_pages.rs b/core-backend/common/src/lazy_pages.rs index b5cdbd6c142..4d892b8f3a4 100644 --- a/core-backend/common/src/lazy_pages.rs +++ b/core-backend/common/src/lazy_pages.rs @@ -18,13 +18,20 @@ //! Core logic for usage both in runtime and in lazy-pages native part. -use core::fmt::Debug; - -use core::any::Any; +use crate::utils::LimitedStr; +use core::{any::Any, fmt::Debug}; use gear_core::{costs::CostPerPage, memory::HostPointer, pages::GearPage}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; use scale_info::scale::{self, Decode, Encode}; -use crate::utils::LimitedStr; +/// Memory access error during sys-call that lazy-pages have caught. +/// 0 index is reserved for an ok result. +#[derive(Debug, Clone, IntoPrimitive, TryFromPrimitive)] +#[repr(u8)] +pub enum ProcessAccessError { + OutOfBounds = 1, + GasLimitExceeded = 2, +} /// Informs lazy-pages whether they work with native or WASM runtime. #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 41e66647645..c1599c0a5dc 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -26,26 +26,18 @@ pub mod lazy_pages; mod utils; -#[cfg(any(feature = "mock", test))] -pub mod mock; - -pub mod memory; pub mod runtime; -use crate::runtime::RunFallibleError; use actor_system_error::actor_system_error; use core::fmt::Debug; use gear_core::{ - env::Externalities, - gas::{ChargeError, CounterType, CountersOwner, GasAmount}, + gas::{ChargeError, CounterType}, ids::ProgramId, - memory::MemoryInterval, message::MessageWaitedType, }; -use memory::ProcessAccessError; use scale_info::scale::{self, Decode, Encode}; -pub use crate::utils::LimitedStr; +pub use crate::{runtime::RunFallibleError, utils::LimitedStr}; pub use log; pub const PTR_SPECIAL: u32 = u32::MAX; @@ -249,18 +241,6 @@ pub enum TrapExplanation { Unknown, } -/// Extended externalities that can manage gas counters. -pub trait BackendExternalities: Externalities + CountersOwner { - fn gas_amount(&self) -> GasAmount; - - /// Pre-process memory access if need. - fn pre_process_memory_accesses( - reads: &[MemoryInterval], - writes: &[MemoryInterval], - gas_counter: &mut u64, - ) -> Result<(), ProcessAccessError>; -} - /// A trait for conversion of the externalities API error /// to `UndefinedTerminationReason` and `RunFallibleError`. pub trait BackendSyscallError: Sized { @@ -278,6 +258,3 @@ pub trait BackendAllocSyscallError: Sized { fn into_backend_error(self) -> Result; } - -#[cfg(test)] -mod tests; diff --git a/core-backend/common/src/memory.rs b/core-backend/common/src/memory.rs deleted file mode 100644 index c0811b47263..00000000000 --- a/core-backend/common/src/memory.rs +++ /dev/null @@ -1,380 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2023 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Work with WASM program memory in backends. - -use crate::{ - runtime::RunFallibleError, BackendExternalities, BackendSyscallError, TrapExplanation, - UndefinedTerminationReason, UnrecoverableMemoryError, -}; -use alloc::vec::Vec; -use core::{ - fmt::Debug, - marker::PhantomData, - mem, - mem::{size_of, MaybeUninit}, - result::Result, - slice, -}; -use gear_core::{ - buffer::{RuntimeBuffer, RuntimeBufferSizeError}, - memory::{Memory, MemoryError, MemoryInterval}, -}; -use gear_core_errors::MemoryError as FallibleMemoryError; -use num_enum::{IntoPrimitive, TryFromPrimitive}; -use scale_info::scale::{Decode, DecodeAll, MaxEncodedLen}; - -/// Memory access error during sys-call that lazy-pages have caught. -/// 0 index is reserved for an ok result. -#[derive(Debug, Clone, IntoPrimitive, TryFromPrimitive)] -#[repr(u8)] -pub enum ProcessAccessError { - OutOfBounds = 1, - GasLimitExceeded = 2, -} - -#[derive(Debug, Clone, derive_more::From)] -pub enum MemoryAccessError { - Memory(MemoryError), - ProcessAccess(ProcessAccessError), - RuntimeBuffer(RuntimeBufferSizeError), - // TODO: remove #2164 - Decode, -} - -impl BackendSyscallError for MemoryAccessError { - fn into_termination_reason(self) -> UndefinedTerminationReason { - match self { - MemoryAccessError::ProcessAccess(ProcessAccessError::OutOfBounds) - | MemoryAccessError::Memory(MemoryError::AccessOutOfBounds) => { - TrapExplanation::UnrecoverableExt( - UnrecoverableMemoryError::AccessOutOfBounds.into(), - ) - .into() - } - MemoryAccessError::RuntimeBuffer(RuntimeBufferSizeError) => { - TrapExplanation::UnrecoverableExt( - UnrecoverableMemoryError::RuntimeAllocOutOfBounds.into(), - ) - .into() - } - // TODO: In facts thats legacy from lazy pages V1 implementation, - // previously it was able to figure out that gas ended up in - // pre-process charges: now we need actual counter type, so - // it will be parsed and handled further (issue #3018). - MemoryAccessError::ProcessAccess(ProcessAccessError::GasLimitExceeded) => { - UndefinedTerminationReason::ProcessAccessErrorResourcesExceed - } - MemoryAccessError::Decode => unreachable!(), - } - } - - fn into_run_fallible_error(self) -> RunFallibleError { - match self { - MemoryAccessError::Memory(MemoryError::AccessOutOfBounds) - | MemoryAccessError::ProcessAccess(ProcessAccessError::OutOfBounds) => { - RunFallibleError::FallibleExt(FallibleMemoryError::AccessOutOfBounds.into()) - } - MemoryAccessError::RuntimeBuffer(RuntimeBufferSizeError) => { - RunFallibleError::FallibleExt(FallibleMemoryError::RuntimeAllocOutOfBounds.into()) - } - e => RunFallibleError::UndefinedTerminationReason(e.into_termination_reason()), - } - } -} - -/// Memory access manager. Allows to pre-register memory accesses, -/// and pre-process, them together. For example: -/// ```ignore -/// let manager = MemoryAccessManager::default(); -/// let read1 = manager.new_read(10, 20); -/// let read2 = manager.new_read_as::(100); -/// let write1 = manager.new_write_as::(190); -/// -/// // First call of read or write interface leads to pre-processing of -/// // all already registered memory accesses, and clear `self.reads` and `self.writes`. -/// let value_u128 = manager.read_as(read2).unwrap(); -/// -/// // Next calls do not lead to access pre-processing. -/// let value1 = manager.read().unwrap(); -/// manager.write_as(write1, 111).unwrap(); -/// ``` -#[derive(Debug)] -pub struct MemoryAccessManager { - // Contains non-zero length intervals only. - pub(crate) reads: Vec, - pub(crate) writes: Vec, - pub(crate) _phantom: PhantomData, -} - -impl Default for MemoryAccessManager { - fn default() -> Self { - Self { - reads: Vec::new(), - writes: Vec::new(), - _phantom: PhantomData, - } - } -} - -impl MemoryAccessManager { - pub fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead { - if size > 0 { - self.reads.push(MemoryInterval { offset: ptr, size }); - } - WasmMemoryRead { ptr, size } - } - - pub fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs { - let size = size_of::() as u32; - if size > 0 { - self.reads.push(MemoryInterval { offset: ptr, size }); - } - WasmMemoryReadAs { - ptr, - _phantom: PhantomData, - } - } - - pub fn register_read_decoded( - &mut self, - ptr: u32, - ) -> WasmMemoryReadDecoded { - let size = T::max_encoded_len() as u32; - if size > 0 { - self.reads.push(MemoryInterval { offset: ptr, size }); - } - WasmMemoryReadDecoded { - ptr, - _phantom: PhantomData, - } - } - - pub fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite { - if size > 0 { - self.writes.push(MemoryInterval { offset: ptr, size }); - } - WasmMemoryWrite { ptr, size } - } - - pub fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs { - let size = size_of::() as u32; - if size > 0 { - self.writes.push(MemoryInterval { offset: ptr, size }); - } - WasmMemoryWriteAs { - ptr, - _phantom: PhantomData, - } - } -} - -impl MemoryAccessManager { - /// Call pre-processing of registered memory accesses. Clear `self.reads` and `self.writes`. - pub(crate) fn pre_process_memory_accesses( - &mut self, - gas_counter: &mut u64, - ) -> Result<(), MemoryAccessError> { - if self.reads.is_empty() && self.writes.is_empty() { - return Ok(()); - } - - let res = Ext::pre_process_memory_accesses(&self.reads, &self.writes, gas_counter); - - self.reads.clear(); - self.writes.clear(); - - res.map_err(Into::into) - } - - /// Pre-process registered accesses if need and read data from `memory` to `buff`. - fn read_into_buf( - &mut self, - memory: &M, - ptr: u32, - buff: &mut [u8], - gas_counter: &mut u64, - ) -> Result<(), MemoryAccessError> { - self.pre_process_memory_accesses(gas_counter)?; - memory.read(ptr, buff).map_err(Into::into) - } - - /// Pre-process registered accesses if need and read data from `memory` into new vector. - pub fn read( - &mut self, - memory: &M, - read: WasmMemoryRead, - gas_counter: &mut u64, - ) -> Result, MemoryAccessError> { - let buff = if read.size == 0 { - Vec::new() - } else { - let mut buff = RuntimeBuffer::try_new_default(read.size as usize)?.into_vec(); - self.read_into_buf(memory, read.ptr, &mut buff, gas_counter)?; - buff - }; - Ok(buff) - } - - /// Pre-process registered accesses if need and read and decode data as `T` from `memory`. - pub fn read_decoded( - &mut self, - memory: &M, - read: WasmMemoryReadDecoded, - gas_counter: &mut u64, - ) -> Result { - let size = T::max_encoded_len(); - let buff = if size == 0 { - Vec::new() - } else { - let mut buff = RuntimeBuffer::try_new_default(size)?.into_vec(); - self.read_into_buf(memory, read.ptr, &mut buff, gas_counter)?; - buff - }; - let decoded = T::decode_all(&mut &buff[..]).map_err(|_| MemoryAccessError::Decode)?; - Ok(decoded) - } - - /// Pre-process registered accesses if need and read data as `T` from `memory`. - pub fn read_as( - &mut self, - memory: &M, - read: WasmMemoryReadAs, - gas_counter: &mut u64, - ) -> Result { - self.pre_process_memory_accesses(gas_counter)?; - read_memory_as(memory, read.ptr).map_err(Into::into) - } - - /// Pre-process registered accesses if need and write data from `buff` to `memory`. - pub fn write( - &mut self, - memory: &mut M, - write: WasmMemoryWrite, - buff: &[u8], - gas_counter: &mut u64, - ) -> Result<(), MemoryAccessError> { - if buff.len() != write.size as usize { - unreachable!("Backend bug error: buffer size is not equal to registered buffer size"); - } - if write.size == 0 { - Ok(()) - } else { - self.pre_process_memory_accesses(gas_counter)?; - memory.write(write.ptr, buff).map_err(Into::into) - } - } - - /// Pre-process registered accesses if need and write `obj` data to `memory`. - pub fn write_as( - &mut self, - memory: &mut M, - write: WasmMemoryWriteAs, - obj: T, - gas_counter: &mut u64, - ) -> Result<(), MemoryAccessError> { - self.pre_process_memory_accesses(gas_counter)?; - write_memory_as(memory, write.ptr, obj).map_err(Into::into) - } -} - -/// Writes object in given memory as bytes. -fn write_memory_as( - memory: &mut impl Memory, - ptr: u32, - obj: T, -) -> Result<(), MemoryError> { - let size = mem::size_of::(); - if size > 0 { - // # Safety: - // - // Given object is `Sized` and we own them in the context of calling this - // function (it's on stack), it's safe to take ptr on the object and - // represent it as slice. Object will be dropped after `memory.write` - // finished execution and no one will rely on this slice. - // - // Bytes in memory always stored continuously and without paddings, properly - // aligned due to `[repr(C, packed)]` attribute of the types we use as T. - let slice = unsafe { slice::from_raw_parts(&obj as *const T as *const u8, size) }; - - memory.write(ptr, slice) - } else { - Ok(()) - } -} - -/// Reads bytes from given pointer to construct type T from them. -fn read_memory_as(memory: &impl Memory, ptr: u32) -> Result { - let mut buf = MaybeUninit::::uninit(); - - let size = mem::size_of::(); - if size > 0 { - // # Safety: - // - // Usage of mutable slice is safe for the same reason from `write_memory_as`. - // `MaybeUninit` is presented on stack as a contiguous sequence of bytes. - // - // It's also safe to construct T from any bytes, because we use the fn - // only for reading primitive const-size types that are `[repr(C)]`, - // so they always represented from sequence of bytes. - // - // Bytes in memory always stored continuously and without paddings, properly - // aligned due to `[repr(C, packed)]` attribute of the types we use as T. - let mut_slice = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, size) }; - - memory.read(ptr, mut_slice)?; - } - - // # Safety: - // - // Assuming init is always safe here due to the fact that we read proper - // amount of bytes from the wasm memory, which is never uninited: they may - // be filled by zeroes or some trash (valid for our primitives used as T), - // but always exist. - Ok(unsafe { buf.assume_init() }) -} - -/// Read static size type access wrapper. -pub struct WasmMemoryReadAs { - pub(crate) ptr: u32, - pub(crate) _phantom: PhantomData, -} - -/// Read decoded type access wrapper. -pub struct WasmMemoryReadDecoded { - pub(crate) ptr: u32, - pub(crate) _phantom: PhantomData, -} - -/// Read access wrapper. -pub struct WasmMemoryRead { - pub(crate) ptr: u32, - pub(crate) size: u32, -} - -/// Write static size type access wrapper. -pub struct WasmMemoryWriteAs { - pub(crate) ptr: u32, - pub(crate) _phantom: PhantomData, -} - -/// Write access wrapper. -pub struct WasmMemoryWrite { - pub(crate) ptr: u32, - pub(crate) size: u32, -} diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/sandbox/Cargo.toml index 5bb5ed0af99..d99edaf8fe9 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/sandbox/Cargo.toml @@ -22,6 +22,10 @@ log.workspace = true derive_more.workspace = true codec.workspace = true +[dev-dependencies] +codec.workspace = true + [features] default = ["std"] std = ["gear-sandbox/std", "gear-wasm-instrument/std", "log/std"] +mock = [] diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index 75d1a8ee0d5..d5a74dcae68 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -24,6 +24,7 @@ use crate::{ runtime, runtime::CallerWrap, state::{HostState, State}, + BackendExternalities, }; use alloc::{collections::BTreeSet, format}; use core::{ @@ -34,8 +35,8 @@ use core::{ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - LimitedStr, TerminationReason, + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, LimitedStr, + TerminationReason, }; use gear_core::{ env::Externalities, diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index 9ccaadf3ddf..fd1327d69a5 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -18,16 +18,18 @@ //! Syscall implementations generic over wasmi and sandbox backends. -use crate::runtime::CallerWrap; +use crate::{ + memory::{MemoryAccessError, WasmMemoryRead}, + runtime::CallerWrap, + BackendExternalities, +}; use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; use gear_backend_common::{ - memory::{MemoryAccessError, WasmMemoryRead}, - runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, + BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, UnrecoverableMemoryError, PTR_SPECIAL, }; use gear_core::{ diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index b56dbfac33d..5b93a5824c0 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -26,8 +26,32 @@ extern crate core; pub mod env; mod funcs; pub mod memory; +#[cfg(any(feature = "mock", test))] +pub mod mock; pub mod runtime; mod state; pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; + +use gear_backend_common::lazy_pages::ProcessAccessError; +use gear_core::{ + env::Externalities, + gas::{CountersOwner, GasAmount}, + memory::MemoryInterval, +}; + +/// Extended externalities that can manage gas counters. +pub trait BackendExternalities: Externalities + CountersOwner { + fn gas_amount(&self) -> GasAmount; + + /// Pre-process memory access if need. + fn pre_process_memory_accesses( + reads: &[MemoryInterval], + writes: &[MemoryInterval], + gas_counter: &mut u64, + ) -> Result<(), ProcessAccessError>; +} + +#[cfg(test)] +mod tests; diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index 96fd58d9777..6fc3aace0c3 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -18,12 +18,20 @@ //! sp-sandbox extensions for memory. -use crate::state::HostState; +use crate::{state::HostState, BackendExternalities}; +use codec::{Decode, DecodeAll, MaxEncodedLen}; +use core::{marker::PhantomData, mem, mem::MaybeUninit, slice}; +use gear_backend_common::{ + lazy_pages::ProcessAccessError, runtime::RunFallibleError, BackendSyscallError, + TrapExplanation, UndefinedTerminationReason, UnrecoverableMemoryError, +}; use gear_core::{ + buffer::{RuntimeBuffer, RuntimeBufferSizeError}, env::Externalities, - memory::{HostPointer, Memory, MemoryError}, + memory::{HostPointer, Memory, MemoryError, MemoryInterval}, pages::{PageNumber, PageU32Size, WasmPage}, }; +use gear_core_errors::MemoryError as FallibleMemoryError; use gear_sandbox::{ default_executor::{Caller, Store}, SandboxMemory, @@ -124,12 +132,343 @@ where } } +#[derive(Debug, Clone, derive_more::From)] +pub enum MemoryAccessError { + Memory(MemoryError), + ProcessAccess(ProcessAccessError), + RuntimeBuffer(RuntimeBufferSizeError), + // TODO: remove #2164 + Decode, +} + +impl BackendSyscallError for MemoryAccessError { + fn into_termination_reason(self) -> UndefinedTerminationReason { + match self { + MemoryAccessError::ProcessAccess(ProcessAccessError::OutOfBounds) + | MemoryAccessError::Memory(MemoryError::AccessOutOfBounds) => { + TrapExplanation::UnrecoverableExt( + UnrecoverableMemoryError::AccessOutOfBounds.into(), + ) + .into() + } + MemoryAccessError::RuntimeBuffer(RuntimeBufferSizeError) => { + TrapExplanation::UnrecoverableExt( + UnrecoverableMemoryError::RuntimeAllocOutOfBounds.into(), + ) + .into() + } + // TODO: In facts thats legacy from lazy pages V1 implementation, + // previously it was able to figure out that gas ended up in + // pre-process charges: now we need actual counter type, so + // it will be parsed and handled further (issue #3018). + MemoryAccessError::ProcessAccess(ProcessAccessError::GasLimitExceeded) => { + UndefinedTerminationReason::ProcessAccessErrorResourcesExceed + } + MemoryAccessError::Decode => unreachable!(), + } + } + + fn into_run_fallible_error(self) -> RunFallibleError { + match self { + MemoryAccessError::Memory(MemoryError::AccessOutOfBounds) + | MemoryAccessError::ProcessAccess(ProcessAccessError::OutOfBounds) => { + RunFallibleError::FallibleExt(FallibleMemoryError::AccessOutOfBounds.into()) + } + MemoryAccessError::RuntimeBuffer(RuntimeBufferSizeError) => { + RunFallibleError::FallibleExt(FallibleMemoryError::RuntimeAllocOutOfBounds.into()) + } + e => RunFallibleError::UndefinedTerminationReason(e.into_termination_reason()), + } + } +} + +/// Memory access manager. Allows to pre-register memory accesses, +/// and pre-process, them together. For example: +/// ```ignore +/// let manager = MemoryAccessManager::default(); +/// let read1 = manager.new_read(10, 20); +/// let read2 = manager.new_read_as::(100); +/// let write1 = manager.new_write_as::(190); +/// +/// // First call of read or write interface leads to pre-processing of +/// // all already registered memory accesses, and clear `self.reads` and `self.writes`. +/// let value_u128 = manager.read_as(read2).unwrap(); +/// +/// // Next calls do not lead to access pre-processing. +/// let value1 = manager.read().unwrap(); +/// manager.write_as(write1, 111).unwrap(); +/// ``` +#[derive(Debug)] +pub struct MemoryAccessManager { + // Contains non-zero length intervals only. + pub(crate) reads: Vec, + pub(crate) writes: Vec, + pub(crate) _phantom: PhantomData, +} + +impl Default for MemoryAccessManager { + fn default() -> Self { + Self { + reads: Vec::new(), + writes: Vec::new(), + _phantom: PhantomData, + } + } +} + +impl MemoryAccessManager { + pub fn register_read(&mut self, ptr: u32, size: u32) -> WasmMemoryRead { + if size > 0 { + self.reads.push(MemoryInterval { offset: ptr, size }); + } + WasmMemoryRead { ptr, size } + } + + pub fn register_read_as(&mut self, ptr: u32) -> WasmMemoryReadAs { + let size = mem::size_of::() as u32; + if size > 0 { + self.reads.push(MemoryInterval { offset: ptr, size }); + } + WasmMemoryReadAs { + ptr, + _phantom: PhantomData, + } + } + + pub fn register_read_decoded( + &mut self, + ptr: u32, + ) -> WasmMemoryReadDecoded { + let size = T::max_encoded_len() as u32; + if size > 0 { + self.reads.push(MemoryInterval { offset: ptr, size }); + } + WasmMemoryReadDecoded { + ptr, + _phantom: PhantomData, + } + } + + pub fn register_write(&mut self, ptr: u32, size: u32) -> WasmMemoryWrite { + if size > 0 { + self.writes.push(MemoryInterval { offset: ptr, size }); + } + WasmMemoryWrite { ptr, size } + } + + pub fn register_write_as(&mut self, ptr: u32) -> WasmMemoryWriteAs { + let size = mem::size_of::() as u32; + if size > 0 { + self.writes.push(MemoryInterval { offset: ptr, size }); + } + WasmMemoryWriteAs { + ptr, + _phantom: PhantomData, + } + } +} + +impl MemoryAccessManager { + /// Call pre-processing of registered memory accesses. Clear `self.reads` and `self.writes`. + pub(crate) fn pre_process_memory_accesses( + &mut self, + gas_counter: &mut u64, + ) -> Result<(), MemoryAccessError> { + if self.reads.is_empty() && self.writes.is_empty() { + return Ok(()); + } + + let res = Ext::pre_process_memory_accesses(&self.reads, &self.writes, gas_counter); + + self.reads.clear(); + self.writes.clear(); + + res.map_err(Into::into) + } + + /// Pre-process registered accesses if need and read data from `memory` to `buff`. + fn read_into_buf( + &mut self, + memory: &M, + ptr: u32, + buff: &mut [u8], + gas_counter: &mut u64, + ) -> Result<(), MemoryAccessError> { + self.pre_process_memory_accesses(gas_counter)?; + memory.read(ptr, buff).map_err(Into::into) + } + + /// Pre-process registered accesses if need and read data from `memory` into new vector. + pub fn read( + &mut self, + memory: &M, + read: WasmMemoryRead, + gas_counter: &mut u64, + ) -> Result, MemoryAccessError> { + let buff = if read.size == 0 { + Vec::new() + } else { + let mut buff = RuntimeBuffer::try_new_default(read.size as usize)?.into_vec(); + self.read_into_buf(memory, read.ptr, &mut buff, gas_counter)?; + buff + }; + Ok(buff) + } + + /// Pre-process registered accesses if need and read and decode data as `T` from `memory`. + pub fn read_decoded( + &mut self, + memory: &M, + read: WasmMemoryReadDecoded, + gas_counter: &mut u64, + ) -> Result { + let size = T::max_encoded_len(); + let buff = if size == 0 { + Vec::new() + } else { + let mut buff = RuntimeBuffer::try_new_default(size)?.into_vec(); + self.read_into_buf(memory, read.ptr, &mut buff, gas_counter)?; + buff + }; + let decoded = T::decode_all(&mut &buff[..]).map_err(|_| MemoryAccessError::Decode)?; + Ok(decoded) + } + + /// Pre-process registered accesses if need and read data as `T` from `memory`. + pub fn read_as( + &mut self, + memory: &M, + read: WasmMemoryReadAs, + gas_counter: &mut u64, + ) -> Result { + self.pre_process_memory_accesses(gas_counter)?; + read_memory_as(memory, read.ptr).map_err(Into::into) + } + + /// Pre-process registered accesses if need and write data from `buff` to `memory`. + pub fn write( + &mut self, + memory: &mut M, + write: WasmMemoryWrite, + buff: &[u8], + gas_counter: &mut u64, + ) -> Result<(), MemoryAccessError> { + if buff.len() != write.size as usize { + unreachable!("Backend bug error: buffer size is not equal to registered buffer size"); + } + if write.size == 0 { + Ok(()) + } else { + self.pre_process_memory_accesses(gas_counter)?; + memory.write(write.ptr, buff).map_err(Into::into) + } + } + + /// Pre-process registered accesses if need and write `obj` data to `memory`. + pub fn write_as( + &mut self, + memory: &mut M, + write: WasmMemoryWriteAs, + obj: T, + gas_counter: &mut u64, + ) -> Result<(), MemoryAccessError> { + self.pre_process_memory_accesses(gas_counter)?; + write_memory_as(memory, write.ptr, obj).map_err(Into::into) + } +} + +/// Writes object in given memory as bytes. +fn write_memory_as( + memory: &mut impl Memory, + ptr: u32, + obj: T, +) -> Result<(), MemoryError> { + let size = mem::size_of::(); + if size > 0 { + // # Safety: + // + // Given object is `Sized` and we own them in the context of calling this + // function (it's on stack), it's safe to take ptr on the object and + // represent it as slice. Object will be dropped after `memory.write` + // finished execution and no one will rely on this slice. + // + // Bytes in memory always stored continuously and without paddings, properly + // aligned due to `[repr(C, packed)]` attribute of the types we use as T. + let slice = unsafe { slice::from_raw_parts(&obj as *const T as *const u8, size) }; + + memory.write(ptr, slice) + } else { + Ok(()) + } +} + +/// Reads bytes from given pointer to construct type T from them. +fn read_memory_as(memory: &impl Memory, ptr: u32) -> Result { + let mut buf = MaybeUninit::::uninit(); + + let size = mem::size_of::(); + if size > 0 { + // # Safety: + // + // Usage of mutable slice is safe for the same reason from `write_memory_as`. + // `MaybeUninit` is presented on stack as a contiguous sequence of bytes. + // + // It's also safe to construct T from any bytes, because we use the fn + // only for reading primitive const-size types that are `[repr(C)]`, + // so they always represented from sequence of bytes. + // + // Bytes in memory always stored continuously and without paddings, properly + // aligned due to `[repr(C, packed)]` attribute of the types we use as T. + let mut_slice = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, size) }; + + memory.read(ptr, mut_slice)?; + } + + // # Safety: + // + // Assuming init is always safe here due to the fact that we read proper + // amount of bytes from the wasm memory, which is never uninited: they may + // be filled by zeroes or some trash (valid for our primitives used as T), + // but always exist. + Ok(unsafe { buf.assume_init() }) +} + +/// Read static size type access wrapper. +pub struct WasmMemoryReadAs { + pub(crate) ptr: u32, + pub(crate) _phantom: PhantomData, +} + +/// Read decoded type access wrapper. +pub struct WasmMemoryReadDecoded { + pub(crate) ptr: u32, + pub(crate) _phantom: PhantomData, +} + +/// Read access wrapper. +pub struct WasmMemoryRead { + pub(crate) ptr: u32, + pub(crate) size: u32, +} + +/// Write static size type access wrapper. +pub struct WasmMemoryWriteAs { + pub(crate) ptr: u32, + pub(crate) _phantom: PhantomData, +} + +/// Write access wrapper. +pub struct WasmMemoryWrite { + pub(crate) ptr: u32, + pub(crate) size: u32, +} + /// can't be tested outside the node runtime #[cfg(test)] mod tests { use super::*; - use crate::state::State; - use gear_backend_common::{assert_err, assert_ok, mock::MockExt, ActorTerminationReason}; + use crate::{mock::MockExt, state::State}; + use gear_backend_common::{assert_err, assert_ok, ActorTerminationReason}; use gear_core::memory::{AllocError, AllocationsContext, NoopGrowHandler}; use gear_sandbox::{AsContextExt, SandboxStore}; diff --git a/core-backend/common/src/mock.rs b/core-backend/sandbox/src/mock.rs similarity index 97% rename from core-backend/common/src/mock.rs rename to core-backend/sandbox/src/mock.rs index 05f56f101b7..81c377e53c7 100644 --- a/core-backend/common/src/mock.rs +++ b/core-backend/sandbox/src/mock.rs @@ -16,12 +16,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - memory::ProcessAccessError, runtime::RunFallibleError, BackendAllocSyscallError, - BackendExternalities, BackendSyscallError, UndefinedTerminationReason, -}; +use crate::BackendExternalities; use alloc::{collections::BTreeSet, vec, vec::Vec}; +use codec::{Decode, Encode}; use core::{cell::Cell, fmt, fmt::Debug}; +use gear_backend_common::{ + lazy_pages::ProcessAccessError, runtime::RunFallibleError, BackendAllocSyscallError, + BackendSyscallError, UndefinedTerminationReason, +}; use gear_core::{ costs::RuntimeCosts, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, @@ -33,11 +35,10 @@ use gear_core::{ }; use gear_core_errors::{ReplyCode, SignalCode}; use gear_wasm_instrument::syscalls::SysCallName; -use scale_info::scale::{self, Decode, Encode}; /// Mock error #[derive(Debug, Clone, Encode, Decode)] -#[codec(crate = scale)] +#[codec(crate = codec)] pub struct Error; impl fmt::Display for Error { diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index 391d3ea7f8d..f2de4b9206f 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -19,20 +19,16 @@ //! sp-sandbox runtime (here it's contract execution state) realization. use crate::{ - memory::MemoryWrapRef, - state::{HostState, State}, - DefaultExecutorMemory, -}; -use alloc::vec::Vec; -use codec::{Decode, MaxEncodedLen}; -use gear_backend_common::{ memory::{ - MemoryAccessError, MemoryAccessManager, WasmMemoryRead, WasmMemoryReadAs, + MemoryAccessError, MemoryAccessManager, MemoryWrapRef, WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, - runtime::RunFallibleError, - BackendAllocSyscallError, BackendExternalities, UndefinedTerminationReason, + state::{HostState, State}, + BackendExternalities, DefaultExecutorMemory, }; +use alloc::vec::Vec; +use codec::{Decode, MaxEncodedLen}; +use gear_backend_common::{BackendAllocSyscallError, RunFallibleError, UndefinedTerminationReason}; use gear_core::{costs::RuntimeCosts, pages::WasmPage}; use gear_sandbox::{default_executor::Caller, AsContextExt, HostError, Value}; diff --git a/core-backend/sandbox/src/state.rs b/core-backend/sandbox/src/state.rs index 2ba51b46079..af634974c67 100644 --- a/core-backend/sandbox/src/state.rs +++ b/core-backend/sandbox/src/state.rs @@ -16,10 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use crate::BackendExternalities; use core::fmt::Debug; use gear_backend_common::{ - ActorTerminationReason, BackendExternalities, TerminationReason, TrapExplanation, - UndefinedTerminationReason, + ActorTerminationReason, TerminationReason, TrapExplanation, UndefinedTerminationReason, }; pub type HostState = Option>; diff --git a/core-backend/common/src/tests.rs b/core-backend/sandbox/src/tests.rs similarity index 97% rename from core-backend/common/src/tests.rs rename to core-backend/sandbox/src/tests.rs index b067de28eb0..f2ca2f96de9 100644 --- a/core-backend/common/src/tests.rs +++ b/core-backend/sandbox/src/tests.rs @@ -1,16 +1,18 @@ -use super::*; use crate::{ - memory::*, + memory::{ + MemoryAccessManager, WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, + WasmMemoryWrite, WasmMemoryWriteAs, + }, mock::{MockExt, MockMemory}, }; - +use codec::{self, Decode, Encode, MaxEncodedLen}; use core::{fmt::Debug, marker::PhantomData}; use gear_core::{memory::Memory, pages::WASM_PAGE_SIZE}; -use scale_info::scale::{self, Decode, Encode, MaxEncodedLen}; const GAS_COUNTER: u64 = u64::MAX; #[derive(Encode, Decode, MaxEncodedLen)] +#[codec(crate = codec)] struct ZeroSizeStruct; #[test] @@ -150,7 +152,7 @@ fn test_read_with_empty_memory_access() { #[test] fn test_read_decoded_with_valid_encoded_data() { #[derive(Encode, Decode, Debug, PartialEq)] - #[codec(crate = scale)] + #[codec(crate = codec)] struct MockEncodeData { data: u64, } @@ -182,13 +184,13 @@ fn test_read_decoded_with_invalid_encoded_data() { struct InvalidDecode {} impl Decode for InvalidDecode { - fn decode(_input: &mut T) -> Result { + fn decode(_input: &mut T) -> Result { Err("Invalid decoding".into()) } } impl Encode for InvalidDecode { - fn encode_to(&self, _dest: &mut T) {} + fn encode_to(&self, _dest: &mut T) {} } impl MaxEncodedLen for InvalidDecode { @@ -491,7 +493,7 @@ fn test_register_read_as_with_zero_size() { } #[derive(Debug, PartialEq, Eq, Encode, Decode, MaxEncodedLen)] -#[codec(crate = scale)] +#[codec(crate = codec)] struct TestStruct { a: u32, b: u64, diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 7ae6adb43db..786c4ec9231 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -34,12 +34,11 @@ use alloc::{ use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - TerminationReason, + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, TerminationReason, }; use gear_backend_sandbox::{ env::{BackendReport, SandboxEnvironmentError}, - MemoryWrap, SandboxEnvironment, + BackendExternalities, MemoryWrap, SandboxEnvironment, }; use gear_core::{ code::InstrumentedCode, diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 09105b61b41..4bfa90c18eb 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -25,13 +25,13 @@ use alloc::{ vec::Vec, }; use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, Status}, - memory::ProcessAccessError, + lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}, runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, TrapExplanation, + UndefinedTerminationReason, UnrecoverableExecutionError, UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, }; +use gear_backend_sandbox::BackendExternalities; use gear_core::{ costs::{HostFnWeights, RuntimeCosts}, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index c4a32bc969a..555e5f355c7 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -29,8 +29,9 @@ use crate::{ }; use alloc::{string::ToString, vec::Vec}; use gear_backend_common::{ - runtime::RunFallibleError, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + runtime::RunFallibleError, BackendAllocSyscallError, BackendSyscallError, }; +use gear_backend_sandbox::BackendExternalities; use gear_core::{ env::Externalities, ids::{MessageId, ProgramId}, diff --git a/lazy-pages/src/host_func.rs b/lazy-pages/src/host_func.rs index ee997244641..889c3e0223e 100644 --- a/lazy-pages/src/host_func.rs +++ b/lazy-pages/src/host_func.rs @@ -23,7 +23,7 @@ use crate::{ process::{self, AccessHandler}, LAZY_PAGES_CONTEXT, }; -use gear_backend_common::{lazy_pages::Status, memory::ProcessAccessError}; +use gear_backend_common::lazy_pages::{ProcessAccessError, Status}; use gear_core::{ self, memory::MemoryInterval, diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index e7e1d3329e4..4b3a2c31dd6 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -24,8 +24,7 @@ use byteorder::{ByteOrder, LittleEndian}; use codec::{Decode, Encode}; use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, Status}, - memory::ProcessAccessError, + lazy_pages::{GlobalsAccessConfig, ProcessAccessError, Status}, LimitedStr, }; use gear_core::{ diff --git a/utils/wasm-instrument/Cargo.toml b/utils/wasm-instrument/Cargo.toml index b09d44adca9..3e3a2ebb2e2 100644 --- a/utils/wasm-instrument/Cargo.toml +++ b/utils/wasm-instrument/Cargo.toml @@ -17,7 +17,7 @@ enum-iterator.workspace = true [dev-dependencies] wasmparser.workspace = true wat.workspace = true -gear-backend-sandbox.workspace = true +gear-backend-sandbox = { workspace = true, features = ["mock"] } gear-backend-common = { workspace = true, features = ["mock"] } gear-core.workspace = true diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 201428edf5f..70a3f281626 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -622,8 +622,8 @@ test_gas_counter_injection! { #[test] fn test_sys_calls_table() { use gas_metering::ConstantCostRules; - use gear_backend_common::{mock::MockExt, ActorTerminationReason}; - use gear_backend_sandbox::{env::BackendReport, SandboxEnvironment}; + use gear_backend_common::ActorTerminationReason; + use gear_backend_sandbox::{env::BackendReport, mock::MockExt, SandboxEnvironment}; use gear_core::message::DispatchKind; use parity_wasm::builder; From e1baf5545a13f86124cc8f07807e3243880afb38 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 22:48:14 +0300 Subject: [PATCH 15/41] Move errors into sandbox backend --- Cargo.lock | 2 +- core-backend/common/Cargo.toml | 1 - core-backend/common/src/lib.rs | 230 +------------------------- core-backend/common/src/runtime.rs | 38 ----- core-backend/sandbox/Cargo.toml | 2 + core-backend/sandbox/src/env.rs | 7 +- core-backend/sandbox/src/funcs.rs | 10 +- core-backend/sandbox/src/lib.rs | 246 +++++++++++++++++++++++++++- core-backend/sandbox/src/memory.rs | 14 +- core-backend/sandbox/src/mock.rs | 10 +- core-backend/sandbox/src/runtime.rs | 4 +- core-backend/sandbox/src/state.rs | 8 +- core-processor/src/common.rs | 5 +- core-processor/src/executor.rs | 9 +- core-processor/src/ext.rs | 14 +- core-processor/src/processing.rs | 5 +- pallets/gear/src/tests.rs | 3 +- utils/wasm-gen/src/tests.rs | 5 +- utils/wasm-instrument/src/tests.rs | 5 +- 19 files changed, 295 insertions(+), 323 deletions(-) delete mode 100644 core-backend/common/src/runtime.rs diff --git a/Cargo.lock b/Cargo.lock index 833dab7de04..ef486c88654 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3791,7 +3791,6 @@ dependencies = [ name = "gear-backend-common" version = "1.0.0" dependencies = [ - "actor-system-error", "derive_more", "gear-core", "gear-core-errors", @@ -3808,6 +3807,7 @@ dependencies = [ name = "gear-backend-sandbox" version = "0.1.0" dependencies = [ + "actor-system-error", "blake2-rfc", "derive_more", "gear-backend-codegen", diff --git a/core-backend/common/Cargo.toml b/core-backend/common/Cargo.toml index f4660752081..231001371aa 100644 --- a/core-backend/common/Cargo.toml +++ b/core-backend/common/Cargo.toml @@ -21,7 +21,6 @@ log.workspace = true num_enum.workspace = true parity-scale-codec.workspace = true scale-info = { workspace = true, features = ["derive"] } -actor-system-error.workspace = true [dev-dependencies] rand = { workspace = true, features = ["std", "std_rng"] } diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index c1599c0a5dc..c3b16a763b9 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -26,235 +26,7 @@ pub mod lazy_pages; mod utils; -pub mod runtime; - -use actor_system_error::actor_system_error; -use core::fmt::Debug; -use gear_core::{ - gas::{ChargeError, CounterType}, - ids::ProgramId, - message::MessageWaitedType, -}; -use scale_info::scale::{self, Decode, Encode}; - -pub use crate::{runtime::RunFallibleError, utils::LimitedStr}; +pub use crate::utils::LimitedStr; pub use log; pub const PTR_SPECIAL: u32 = u32::MAX; - -actor_system_error! { - pub type TerminationReason = ActorSystemError; -} - -#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)] -pub enum UndefinedTerminationReason { - Actor(ActorTerminationReason), - System(SystemTerminationReason), - /// Undefined reason because we need access to counters owner trait for RI. - ProcessAccessErrorResourcesExceed, -} - -impl UndefinedTerminationReason { - pub fn define(self, current_counter: CounterType) -> TerminationReason { - match self { - Self::Actor(r) => r.into(), - Self::System(r) => r.into(), - Self::ProcessAccessErrorResourcesExceed => { - ActorTerminationReason::from(current_counter).into() - } - } - } -} - -impl From for UndefinedTerminationReason { - fn from(err: ChargeError) -> Self { - match err { - ChargeError::GasLimitExceeded => { - ActorTerminationReason::Trap(TrapExplanation::GasLimitExceeded).into() - } - ChargeError::GasAllowanceExceeded => { - ActorTerminationReason::GasAllowanceExceeded.into() - } - } - } -} - -impl From for UndefinedTerminationReason { - fn from(trap: TrapExplanation) -> Self { - ActorTerminationReason::Trap(trap).into() - } -} - -impl From for UndefinedTerminationReason { - fn from(err: E) -> Self { - err.into_termination_reason() - } -} - -#[derive(Decode, Encode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, derive_more::From)] -#[codec(crate = scale)] -pub enum ActorTerminationReason { - Exit(ProgramId), - Leave, - Success, - Wait(Option, MessageWaitedType), - GasAllowanceExceeded, - #[from] - Trap(TrapExplanation), -} - -impl From for ActorTerminationReason { - fn from(counter_type: CounterType) -> Self { - match counter_type { - CounterType::GasLimit => Self::Trap(TrapExplanation::GasLimitExceeded), - CounterType::GasAllowance => Self::GasAllowanceExceeded, - } - } -} - -/// Non-actor related termination reason. -/// -/// ### NOTICE: -/// It's currently unused, but is left as a stub, until -/// further massive errors refactoring is done. -#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)] -pub struct SystemTerminationReason; - -/// Execution error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -pub enum UnrecoverableExecutionError { - #[display(fmt = "Invalid debug string passed in `gr_debug` sys-call")] - InvalidDebugString, - #[display(fmt = "Not enough gas for operation")] - NotEnoughGas, - #[display(fmt = "Length is overflowed to read payload")] - TooBigReadLen, - #[display(fmt = "Cannot take data in payload range from message with size")] - ReadWrongRange, -} - -/// Memory error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -pub enum UnrecoverableMemoryError { - /// The error occurs in attempt to access memory outside wasm program memory. - #[display(fmt = "Trying to access memory outside wasm program memory")] - AccessOutOfBounds, - /// The error occurs, when program tries to allocate in block-chain runtime more memory than allowed. - #[display(fmt = "Trying to allocate more memory in block-chain runtime than allowed")] - RuntimeAllocOutOfBounds, -} - -/// Wait error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -pub enum UnrecoverableWaitError { - /// An error occurs in attempt to wait for or wait up to zero blocks. - #[display(fmt = "Waiting duration cannot be zero")] - ZeroDuration, - /// An error occurs in attempt to wait after reply sent. - #[display(fmt = "`wait()` is not allowed after reply sent")] - WaitAfterReply, -} - -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -pub enum UnrecoverableExtError { - #[display(fmt = "Execution error: {_0}")] - Execution(UnrecoverableExecutionError), - #[display(fmt = "Memory error: {_0}")] - Memory(UnrecoverableMemoryError), - #[display(fmt = "Waiting error: {_0}")] - Wait(UnrecoverableWaitError), -} - -#[derive( - Decode, - Encode, - Debug, - Clone, - PartialEq, - Eq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = scale)] -pub enum TrapExplanation { - /// An error occurs in attempt to charge more gas than available during execution. - #[display(fmt = "Not enough gas to continue execution")] - GasLimitExceeded, - /// An error occurs in attempt to call forbidden sys-call. - #[display(fmt = "Unable to call a forbidden function")] - ForbiddenFunction, - /// The error occurs when a program tries to allocate more memory than - /// allowed. - #[display(fmt = "Trying to allocate more wasm program memory than allowed")] - ProgramAllocOutOfBounds, - #[display(fmt = "Sys-call unrecoverable error: {_0}")] - UnrecoverableExt(UnrecoverableExtError), - #[display(fmt = "{_0}")] - Panic(LimitedStr<'static>), - #[display(fmt = "Reason is unknown. Possibly `unreachable` instruction is occurred")] - Unknown, -} - -/// A trait for conversion of the externalities API error -/// to `UndefinedTerminationReason` and `RunFallibleError`. -pub trait BackendSyscallError: Sized { - fn into_termination_reason(self) -> UndefinedTerminationReason; - - fn into_run_fallible_error(self) -> RunFallibleError; -} - -// TODO: consider to remove this trait and use Result, GasError> instead #2571 -/// A trait for conversion of the externalities memory management error to api error. -/// -/// If the conversion fails, then `Self` is returned in the `Err` variant. -pub trait BackendAllocSyscallError: Sized { - type ExtError: BackendSyscallError; - - fn into_backend_error(self) -> Result; -} diff --git a/core-backend/common/src/runtime.rs b/core-backend/common/src/runtime.rs deleted file mode 100644 index 6d345821294..00000000000 --- a/core-backend/common/src/runtime.rs +++ /dev/null @@ -1,38 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2023 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Trait that both sandbox and wasmi runtimes must implement. - -use crate::{BackendSyscallError, UndefinedTerminationReason}; -use gear_core_errors::ExtError as FallibleExtError; - -/// Error returned from closure argument in [`Runtime::run_fallible`]. -#[derive(Debug, Clone)] -pub enum RunFallibleError { - UndefinedTerminationReason(UndefinedTerminationReason), - FallibleExt(FallibleExtError), -} - -impl From for RunFallibleError -where - E: BackendSyscallError, -{ - fn from(err: E) -> Self { - err.into_run_fallible_error() - } -} diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/sandbox/Cargo.toml index d99edaf8fe9..7c9e54cdcc7 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/sandbox/Cargo.toml @@ -16,6 +16,8 @@ gear-wasm-instrument.workspace = true gear-sandbox.workspace = true gear-sandbox-env.workspace = true +actor-system-error.workspace = true + blake2-rfc.workspace = true # Use max_level_debug feature to remove tracing in sys-calls by default. log.workspace = true diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index d5a74dcae68..84d30b5332c 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -24,7 +24,8 @@ use crate::{ runtime, runtime::CallerWrap, state::{HostState, State}, - BackendExternalities, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + RunFallibleError, TerminationReason, }; use alloc::{collections::BTreeSet, format}; use core::{ @@ -34,9 +35,7 @@ use core::{ }; use gear_backend_common::{ lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, - runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, LimitedStr, - TerminationReason, + LimitedStr, }; use gear_core::{ env::Externalities, diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index fd1327d69a5..cef256a957c 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -21,17 +21,15 @@ use crate::{ memory::{MemoryAccessError, WasmMemoryRead}, runtime::CallerWrap, - BackendExternalities, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableMemoryError, }; use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; -use gear_backend_common::{ - runtime::RunFallibleError, ActorTerminationReason, BackendAllocSyscallError, - BackendSyscallError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, - UnrecoverableMemoryError, PTR_SPECIAL, -}; +use gear_backend_common::PTR_SPECIAL; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, costs::RuntimeCosts, diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index 5b93a5824c0..39a58d23105 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -31,15 +31,223 @@ pub mod mock; pub mod runtime; mod state; +use codec::{Decode, Encode}; pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; -use gear_backend_common::lazy_pages::ProcessAccessError; +use actor_system_error::actor_system_error; +use gear_backend_common::{lazy_pages::ProcessAccessError, LimitedStr}; use gear_core::{ env::Externalities, - gas::{CountersOwner, GasAmount}, + gas::{ChargeError, CounterType, CountersOwner, GasAmount}, + ids::ProgramId, memory::MemoryInterval, + message::MessageWaitedType, }; +use gear_core_errors::ExtError as FallibleExtError; + +actor_system_error! { + pub type TerminationReason = ActorSystemError; +} + +#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)] +pub enum UndefinedTerminationReason { + Actor(ActorTerminationReason), + System(SystemTerminationReason), + /// Undefined reason because we need access to counters owner trait for RI. + ProcessAccessErrorResourcesExceed, +} + +impl UndefinedTerminationReason { + pub fn define(self, current_counter: CounterType) -> TerminationReason { + match self { + Self::Actor(r) => r.into(), + Self::System(r) => r.into(), + Self::ProcessAccessErrorResourcesExceed => { + ActorTerminationReason::from(current_counter).into() + } + } + } +} + +impl From for UndefinedTerminationReason { + fn from(err: ChargeError) -> Self { + match err { + ChargeError::GasLimitExceeded => { + ActorTerminationReason::Trap(TrapExplanation::GasLimitExceeded).into() + } + ChargeError::GasAllowanceExceeded => { + ActorTerminationReason::GasAllowanceExceeded.into() + } + } + } +} + +impl From for UndefinedTerminationReason { + fn from(trap: TrapExplanation) -> Self { + ActorTerminationReason::Trap(trap).into() + } +} + +impl From for UndefinedTerminationReason { + fn from(err: E) -> Self { + err.into_termination_reason() + } +} + +#[derive(Decode, Encode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, derive_more::From)] +#[codec(crate = codec)] +pub enum ActorTerminationReason { + Exit(ProgramId), + Leave, + Success, + Wait(Option, MessageWaitedType), + GasAllowanceExceeded, + #[from] + Trap(TrapExplanation), +} + +impl From for ActorTerminationReason { + fn from(counter_type: CounterType) -> Self { + match counter_type { + CounterType::GasLimit => Self::Trap(TrapExplanation::GasLimitExceeded), + CounterType::GasAllowance => Self::GasAllowanceExceeded, + } + } +} + +/// Non-actor related termination reason. +/// +/// ### NOTICE: +/// It's currently unused, but is left as a stub, until +/// further massive errors refactoring is done. +#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)] +pub struct SystemTerminationReason; + +/// Execution error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableExecutionError { + #[display(fmt = "Invalid debug string passed in `gr_debug` sys-call")] + InvalidDebugString, + #[display(fmt = "Not enough gas for operation")] + NotEnoughGas, + #[display(fmt = "Length is overflowed to read payload")] + TooBigReadLen, + #[display(fmt = "Cannot take data in payload range from message with size")] + ReadWrongRange, +} + +/// Memory error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableMemoryError { + /// The error occurs in attempt to access memory outside wasm program memory. + #[display(fmt = "Trying to access memory outside wasm program memory")] + AccessOutOfBounds, + /// The error occurs, when program tries to allocate in block-chain runtime more memory than allowed. + #[display(fmt = "Trying to allocate more memory in block-chain runtime than allowed")] + RuntimeAllocOutOfBounds, +} + +/// Wait error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableWaitError { + /// An error occurs in attempt to wait for or wait up to zero blocks. + #[display(fmt = "Waiting duration cannot be zero")] + ZeroDuration, + /// An error occurs in attempt to wait after reply sent. + #[display(fmt = "`wait()` is not allowed after reply sent")] + WaitAfterReply, +} + +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableExtError { + #[display(fmt = "Execution error: {_0}")] + Execution(UnrecoverableExecutionError), + #[display(fmt = "Memory error: {_0}")] + Memory(UnrecoverableMemoryError), + #[display(fmt = "Waiting error: {_0}")] + Wait(UnrecoverableWaitError), +} + +#[derive( + Decode, + Encode, + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum TrapExplanation { + /// An error occurs in attempt to charge more gas than available during execution. + #[display(fmt = "Not enough gas to continue execution")] + GasLimitExceeded, + /// An error occurs in attempt to call forbidden sys-call. + #[display(fmt = "Unable to call a forbidden function")] + ForbiddenFunction, + /// The error occurs when a program tries to allocate more memory than + /// allowed. + #[display(fmt = "Trying to allocate more wasm program memory than allowed")] + ProgramAllocOutOfBounds, + #[display(fmt = "Sys-call unrecoverable error: {_0}")] + UnrecoverableExt(UnrecoverableExtError), + #[display(fmt = "{_0}")] + Panic(LimitedStr<'static>), + #[display(fmt = "Reason is unknown. Possibly `unreachable` instruction is occurred")] + Unknown, +} /// Extended externalities that can manage gas counters. pub trait BackendExternalities: Externalities + CountersOwner { @@ -53,5 +261,39 @@ pub trait BackendExternalities: Externalities + CountersOwner { ) -> Result<(), ProcessAccessError>; } +/// Error returned from closure argument in [`Runtime::run_fallible`]. +#[derive(Debug, Clone)] +pub enum RunFallibleError { + UndefinedTerminationReason(UndefinedTerminationReason), + FallibleExt(FallibleExtError), +} + +impl From for RunFallibleError +where + E: BackendSyscallError, +{ + fn from(err: E) -> Self { + err.into_run_fallible_error() + } +} + +/// A trait for conversion of the externalities API error +/// to `UndefinedTerminationReason` and `RunFallibleError`. +pub trait BackendSyscallError: Sized { + fn into_termination_reason(self) -> UndefinedTerminationReason; + + fn into_run_fallible_error(self) -> RunFallibleError; +} + +// TODO: consider to remove this trait and use Result, GasError> instead #2571 +/// A trait for conversion of the externalities memory management error to api error. +/// +/// If the conversion fails, then `Self` is returned in the `Err` variant. +pub trait BackendAllocSyscallError: Sized { + type ExtError: BackendSyscallError; + + fn into_backend_error(self) -> Result; +} + #[cfg(test)] mod tests; diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index 6fc3aace0c3..1c4c973a6d6 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -18,13 +18,13 @@ //! sp-sandbox extensions for memory. -use crate::{state::HostState, BackendExternalities}; +use crate::{ + state::HostState, BackendExternalities, BackendSyscallError, RunFallibleError, TrapExplanation, + UndefinedTerminationReason, UnrecoverableMemoryError, +}; use codec::{Decode, DecodeAll, MaxEncodedLen}; use core::{marker::PhantomData, mem, mem::MaybeUninit, slice}; -use gear_backend_common::{ - lazy_pages::ProcessAccessError, runtime::RunFallibleError, BackendSyscallError, - TrapExplanation, UndefinedTerminationReason, UnrecoverableMemoryError, -}; +use gear_backend_common::lazy_pages::ProcessAccessError; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, env::Externalities, @@ -467,8 +467,8 @@ pub struct WasmMemoryWrite { #[cfg(test)] mod tests { use super::*; - use crate::{mock::MockExt, state::State}; - use gear_backend_common::{assert_err, assert_ok, ActorTerminationReason}; + use crate::{mock::MockExt, state::State, ActorTerminationReason}; + use gear_backend_common::{assert_err, assert_ok}; use gear_core::memory::{AllocError, AllocationsContext, NoopGrowHandler}; use gear_sandbox::{AsContextExt, SandboxStore}; diff --git a/core-backend/sandbox/src/mock.rs b/core-backend/sandbox/src/mock.rs index 81c377e53c7..fd301bf1c90 100644 --- a/core-backend/sandbox/src/mock.rs +++ b/core-backend/sandbox/src/mock.rs @@ -16,14 +16,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::BackendExternalities; +use crate::{ + BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, + UndefinedTerminationReason, +}; use alloc::{collections::BTreeSet, vec, vec::Vec}; use codec::{Decode, Encode}; use core::{cell::Cell, fmt, fmt::Debug}; -use gear_backend_common::{ - lazy_pages::ProcessAccessError, runtime::RunFallibleError, BackendAllocSyscallError, - BackendSyscallError, UndefinedTerminationReason, -}; +use gear_backend_common::lazy_pages::ProcessAccessError; use gear_core::{ costs::RuntimeCosts, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/sandbox/src/runtime.rs index f2de4b9206f..446bb4352f4 100644 --- a/core-backend/sandbox/src/runtime.rs +++ b/core-backend/sandbox/src/runtime.rs @@ -24,11 +24,11 @@ use crate::{ WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, state::{HostState, State}, - BackendExternalities, DefaultExecutorMemory, + BackendAllocSyscallError, BackendExternalities, DefaultExecutorMemory, RunFallibleError, + UndefinedTerminationReason, }; use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; -use gear_backend_common::{BackendAllocSyscallError, RunFallibleError, UndefinedTerminationReason}; use gear_core::{costs::RuntimeCosts, pages::WasmPage}; use gear_sandbox::{default_executor::Caller, AsContextExt, HostError, Value}; diff --git a/core-backend/sandbox/src/state.rs b/core-backend/sandbox/src/state.rs index af634974c67..b6fa3d5b8cd 100644 --- a/core-backend/sandbox/src/state.rs +++ b/core-backend/sandbox/src/state.rs @@ -16,11 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::BackendExternalities; -use core::fmt::Debug; -use gear_backend_common::{ - ActorTerminationReason, TerminationReason, TrapExplanation, UndefinedTerminationReason, +use crate::{ + ActorTerminationReason, BackendExternalities, TerminationReason, TrapExplanation, + UndefinedTerminationReason, }; +use core::fmt::Debug; pub type HostState = Option>; diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index 85c3bbfe789..8d8add01d5c 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -28,8 +28,9 @@ use alloc::{ string::String, vec::Vec, }; -use gear_backend_common::{SystemTerminationReason, TrapExplanation}; -use gear_backend_sandbox::env::SandboxSystemEnvironmentError; +use gear_backend_sandbox::{ + env::SandboxSystemEnvironmentError, SystemTerminationReason, TrapExplanation, +}; use gear_core::{ gas::{GasAllowanceCounter, GasAmount, GasCounter}, ids::{CodeId, MessageId, ProgramId, ReservationId}, diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 786c4ec9231..f366c89ca00 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -31,14 +31,11 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}, - runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, TerminationReason, -}; +use gear_backend_common::lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}; use gear_backend_sandbox::{ env::{BackendReport, SandboxEnvironmentError}, - BackendExternalities, MemoryWrap, SandboxEnvironment, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + MemoryWrap, RunFallibleError, SandboxEnvironment, TerminationReason, }; use gear_core::{ code::InstrumentedCode, diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 4bfa90c18eb..25d25969b38 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -24,14 +24,16 @@ use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, }; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}, - runtime::RunFallibleError, - ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, TrapExplanation, - UndefinedTerminationReason, UnrecoverableExecutionError, +use gear_backend_common::lazy_pages::{ + GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status, +}; +use gear_backend_sandbox::{ + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, }; -use gear_backend_sandbox::BackendExternalities; +#[cfg(any(feature = "mock", test))] +use gear_core::message::{ContextSettings, IncomingDispatch}; use gear_core::{ costs::{HostFnWeights, RuntimeCosts}, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index 555e5f355c7..3b655de5f7d 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -28,10 +28,9 @@ use crate::{ precharge::SuccessfulDispatchResultKind, }; use alloc::{string::ToString, vec::Vec}; -use gear_backend_common::{ - runtime::RunFallibleError, BackendAllocSyscallError, BackendSyscallError, +use gear_backend_sandbox::{ + BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, }; -use gear_backend_sandbox::BackendExternalities; use gear_core::{ env::Externalities, ids::{MessageId, ProgramId}, diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index fef0874afa1..bfd987b9135 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -63,7 +63,7 @@ use frame_support::{ traits::{Currency, Randomness}, }; use frame_system::pallet_prelude::BlockNumberFor; -use gear_backend_common::{ +use gear_backend_sandbox::{ TrapExplanation, UnrecoverableExecutionError, UnrecoverableExtError, UnrecoverableWaitError, }; use gear_core::{ @@ -14343,7 +14343,6 @@ mod utils { traits::tokens::{currency::Currency, Balance}, }; use frame_system::pallet_prelude::{BlockNumberFor, OriginFor}; - use gear_backend_common::TrapExplanation; use gear_core::{ ids::{CodeId, MessageId, ProgramId}, message::{Message, Payload, ReplyDetails, UserMessage, UserStoredMessage}, diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index dfddf192075..e1cdb7d46c5 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -18,8 +18,7 @@ use super::*; use arbitrary::Unstructured; -use gear_backend_common::{TerminationReason, TrapExplanation}; -use gear_backend_sandbox::env::BackendReport; +use gear_backend_sandbox::{env::BackendReport, TerminationReason, TrapExplanation}; use gear_core::{ code::Code, ids::{CodeId, ProgramId}, @@ -174,7 +173,7 @@ fn injecting_addresses_works() { #[test] fn error_processing_works_for_fallible_syscalls() { - use gear_backend_common::ActorTerminationReason; + use gear_backend_sandbox::ActorTerminationReason; let fallible_syscalls = SysCallName::instrumentable() .into_iter() diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 70a3f281626..45cddb28287 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -622,8 +622,9 @@ test_gas_counter_injection! { #[test] fn test_sys_calls_table() { use gas_metering::ConstantCostRules; - use gear_backend_common::ActorTerminationReason; - use gear_backend_sandbox::{env::BackendReport, mock::MockExt, SandboxEnvironment}; + use gear_backend_sandbox::{ + env::BackendReport, mock::MockExt, ActorTerminationReason, SandboxEnvironment, + }; use gear_core::message::DispatchKind; use parity_wasm::builder; From d26540078af63c21017d49b3f3017bb5ae941bf3 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 22:51:23 +0300 Subject: [PATCH 16/41] Fix `no_std` --- core-backend/sandbox/src/env.rs | 2 +- core-backend/sandbox/src/memory.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index 84d30b5332c..b96b00ad274 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -27,7 +27,7 @@ use crate::{ ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, TerminationReason, }; -use alloc::{collections::BTreeSet, format}; +use alloc::{collections::BTreeSet, format, string::String}; use core::{ any::Any, convert::Infallible, diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index 1c4c973a6d6..93e3ccf76fb 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -22,6 +22,7 @@ use crate::{ state::HostState, BackendExternalities, BackendSyscallError, RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableMemoryError, }; +use alloc::vec::Vec; use codec::{Decode, DecodeAll, MaxEncodedLen}; use core::{marker::PhantomData, mem, mem::MaybeUninit, slice}; use gear_backend_common::lazy_pages::ProcessAccessError; From 6e9cbbf2f926eda5b5565d582c98e7d3f3ffc221 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 22:53:39 +0300 Subject: [PATCH 17/41] Move `PTR_SPECIAL` into sandbox backend --- core-backend/common/src/lib.rs | 2 -- core-backend/sandbox/src/funcs.rs | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index c3b16a763b9..2bbf4aaea88 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -28,5 +28,3 @@ mod utils; pub use crate::utils::LimitedStr; pub use log; - -pub const PTR_SPECIAL: u32 = u32::MAX; diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/sandbox/src/funcs.rs index cef256a957c..adeb59becbe 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/sandbox/src/funcs.rs @@ -29,7 +29,6 @@ use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; use gear_backend_codegen::host; -use gear_backend_common::PTR_SPECIAL; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, costs::RuntimeCosts, @@ -82,6 +81,8 @@ macro_rules! syscall_trace { } } +const PTR_SPECIAL: u32 = u32::MAX; + pub struct FuncsHandler { _phantom: PhantomData, } From 6121bd6580dbe4bd018cf77e2ef30c6bd8072488 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 23:13:30 +0300 Subject: [PATCH 18/41] Rename `gear-lazy-pages-common` to `gear-lazy-pages-interface` --- Cargo.lock | 12 ++++++------ Cargo.toml | 2 +- core-processor/Cargo.toml | 4 ++-- core-processor/src/ext.rs | 2 +- gcli/Cargo.toml | 2 +- gcli/src/meta/mod.rs | 2 +- gtest/Cargo.toml | 2 +- gtest/src/system.rs | 2 +- .../lazy-pages => lazy-pages/interface}/Cargo.toml | 2 +- .../lazy-pages => lazy-pages/interface}/src/lib.rs | 0 pallets/gear/Cargo.toml | 4 ++-- pallets/gear/src/benchmarking/tests/lazy_pages.rs | 2 +- pallets/gear/src/benchmarking/utils.rs | 2 +- pallets/gear/src/lib.rs | 2 +- utils/wasm-gen/Cargo.toml | 2 +- utils/wasm-gen/src/tests.rs | 2 +- 16 files changed, 22 insertions(+), 22 deletions(-) rename {common/lazy-pages => lazy-pages/interface}/Cargo.toml (93%) rename {common/lazy-pages => lazy-pages/interface}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index ef486c88654..2ae1365f111 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3655,7 +3655,7 @@ dependencies = [ "gear-core", "gear-core-errors", "gear-core-processor", - "gear-lazy-pages-common", + "gear-lazy-pages-interface", "gmeta", "gsdk", "hex", @@ -3960,7 +3960,7 @@ dependencies = [ "gear-backend-sandbox", "gear-core", "gear-core-errors", - "gear-lazy-pages-common", + "gear-lazy-pages-interface", "gear-wasm-instrument", "log", "scale-info", @@ -3993,7 +3993,7 @@ dependencies = [ ] [[package]] -name = "gear-lazy-pages-common" +name = "gear-lazy-pages-interface" version = "0.1.0" dependencies = [ "byteorder", @@ -4430,7 +4430,7 @@ dependencies = [ "gear-backend-sandbox", "gear-core", "gear-core-processor", - "gear-lazy-pages-common", + "gear-lazy-pages-interface", "gear-utils", "gear-wasm-instrument", "gsys", @@ -4764,7 +4764,7 @@ dependencies = [ "gear-core", "gear-core-errors", "gear-core-processor", - "gear-lazy-pages-common", + "gear-lazy-pages-interface", "gear-utils", "gear-wasm-builder", "gear-wasm-instrument", @@ -7369,7 +7369,7 @@ dependencies = [ "gear-core", "gear-core-errors", "gear-core-processor", - "gear-lazy-pages-common", + "gear-lazy-pages-interface", "gear-runtime-interface", "gear-sandbox", "gear-wasm-instrument", diff --git a/Cargo.toml b/Cargo.toml index eecdbf0432f..d585378b284 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -205,7 +205,7 @@ gear-core = { path = "core" } gear-core-errors = { path = "core-errors" } gear-core-processor = { path = "core-processor", default-features = false } gear-lazy-pages = { path = "lazy-pages" } -gear-lazy-pages-common = { path = "common/lazy-pages", default-features = false } +gear-lazy-pages-interface = { path = "lazy-pages/interface", default-features = false } gear-node-testing = { path = "node/testing" } gear-runtime = { path = "runtime/gear" } gear-runtime-common = { path = "runtime/common", default-features = false } diff --git a/core-processor/Cargo.toml b/core-processor/Cargo.toml index d455cc66a6b..a29817889c1 100644 --- a/core-processor/Cargo.toml +++ b/core-processor/Cargo.toml @@ -16,7 +16,7 @@ gear-core-errors = { workspace = true, features = ["codec"] } gear-backend-common.workspace = true gear-backend-sandbox.workspace = true gear-wasm-instrument.workspace = true -gear-lazy-pages-common.workspace = true +gear-lazy-pages-interface.workspace = true scale-info = { workspace = true, features = ["derive"] } log.workspace = true @@ -30,6 +30,6 @@ enum-iterator.workspace = true [features] default = ["std"] -std = ["gear-lazy-pages-common/std", "gear-backend-sandbox/std"] +std = ["gear-lazy-pages-interface/std", "gear-backend-sandbox/std"] strict = [] mock = [] diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 25d25969b38..89d588126dd 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -56,7 +56,7 @@ use gear_core_errors::{ ExecutionError as FallibleExecutionError, ExtError as FallibleExtErrorCore, MessageError, ProgramRentError, ReplyCode, ReservationError, SignalCode, }; -use gear_lazy_pages_common as lazy_pages; +use gear_lazy_pages_interface as lazy_pages; use gear_wasm_instrument::syscalls::SysCallName; /// Processor context. diff --git a/gcli/Cargo.toml b/gcli/Cargo.toml index dc002755932..0880959d94a 100644 --- a/gcli/Cargo.toml +++ b/gcli/Cargo.toml @@ -41,7 +41,7 @@ thiserror.workspace = true tokio = { workspace = true, features = [ "full" ] } whoami.workspace = true core-processor = { workspace = true, features = [ "std" ] } -gear-lazy-pages-common = { workspace = true, features = [ "std" ] } +gear-lazy-pages-interface = { workspace = true, features = [ "std" ] } reqwest = { workspace = true, default-features = false, features = [ "json", "rustls-tls" ] } etc.workspace = true sp-io = { workspace = true, features = [ "std" ] } diff --git a/gcli/src/meta/mod.rs b/gcli/src/meta/mod.rs index 721ab394eb8..4028cccb4a1 100644 --- a/gcli/src/meta/mod.rs +++ b/gcli/src/meta/mod.rs @@ -109,7 +109,7 @@ impl Meta { /// Execute meta method. fn execute(wasm: InstrumentedCode, method: &str) -> Result> { - assert!(gear_lazy_pages_common::try_to_enable_lazy_pages( + assert!(gear_lazy_pages_interface::try_to_enable_lazy_pages( Self::PAGE_STORAGE_PREFIX )); diff --git a/gtest/Cargo.toml b/gtest/Cargo.toml index ee768bce827..a559136423c 100644 --- a/gtest/Cargo.toml +++ b/gtest/Cargo.toml @@ -10,7 +10,7 @@ gear-core.workspace = true gear-core-errors.workspace = true gear-backend-common.workspace = true core-processor = { workspace = true, features = ["std"] } -gear-lazy-pages-common = { workspace = true, features = ["std"] } +gear-lazy-pages-interface = { workspace = true, features = ["std"] } gear-wasm-builder.workspace = true gear-utils.workspace = true diff --git a/gtest/src/system.rs b/gtest/src/system.rs index b4e3c3a70b1..7d1c46e512f 100644 --- a/gtest/src/system.rs +++ b/gtest/src/system.rs @@ -41,7 +41,7 @@ impl System { /// Create a new system. pub fn new() -> Self { - assert!(gear_lazy_pages_common::try_to_enable_lazy_pages( + assert!(gear_lazy_pages_interface::try_to_enable_lazy_pages( Self::PAGE_STORAGE_PREFIX )); Default::default() diff --git a/common/lazy-pages/Cargo.toml b/lazy-pages/interface/Cargo.toml similarity index 93% rename from common/lazy-pages/Cargo.toml rename to lazy-pages/interface/Cargo.toml index 821f15cef8a..862c97df08e 100644 --- a/common/lazy-pages/Cargo.toml +++ b/lazy-pages/interface/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gear-lazy-pages-common" +name = "gear-lazy-pages-interface" version = "0.1.0" authors.workspace = true edition.workspace = true diff --git a/common/lazy-pages/src/lib.rs b/lazy-pages/interface/src/lib.rs similarity index 100% rename from common/lazy-pages/src/lib.rs rename to lazy-pages/interface/src/lib.rs diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index cfb8ed21892..61044982784 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -26,7 +26,7 @@ static_assertions.workspace = true # Internal deps common.workspace = true gear-runtime-interface = { workspace = true } -gear-lazy-pages-common.workspace = true +gear-lazy-pages-interface.workspace = true core-processor.workspace = true gear-core.workspace = true gear-core-errors.workspace = true @@ -138,7 +138,7 @@ std = [ "scopeguard/use_std", "core-processor/std", "gear-backend-sandbox/std", - "gear-lazy-pages-common/std", + "gear-lazy-pages-interface/std", "scale-info/std", "sp-io/std", "sp-std/std", diff --git a/pallets/gear/src/benchmarking/tests/lazy_pages.rs b/pallets/gear/src/benchmarking/tests/lazy_pages.rs index 94a92d7e6df..ec76d450d48 100644 --- a/pallets/gear/src/benchmarking/tests/lazy_pages.rs +++ b/pallets/gear/src/benchmarking/tests/lazy_pages.rs @@ -28,7 +28,7 @@ use gear_core::{ memory::MemoryInterval, pages::{PageNumber, PageU32Size}, }; -use gear_lazy_pages_common as lazy_pages; +use gear_lazy_pages_interface as lazy_pages; use rand::{Rng, SeedableRng}; use super::*; diff --git a/pallets/gear/src/benchmarking/utils.rs b/pallets/gear/src/benchmarking/utils.rs index d04a116c50a..bed4bec7df7 100644 --- a/pallets/gear/src/benchmarking/utils.rs +++ b/pallets/gear/src/benchmarking/utils.rs @@ -114,7 +114,7 @@ where T: Config, T::AccountId: Origin, { - assert!(gear_lazy_pages_common::try_to_enable_lazy_pages( + assert!(gear_lazy_pages_interface::try_to_enable_lazy_pages( ProgramStorageOf::::pages_final_prefix() )); diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index c80340c7996..997b253e4b2 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -72,7 +72,7 @@ use gear_core::{ message::*, pages::{GearPage, WasmPage}, }; -use gear_lazy_pages_common as lazy_pages; +use gear_lazy_pages_interface as lazy_pages; use manager::{CodeInfo, QueuePostProcessingData}; use primitive_types::H256; use sp_runtime::{ diff --git a/utils/wasm-gen/Cargo.toml b/utils/wasm-gen/Cargo.toml index ea4e5c01fbf..78af46a4936 100644 --- a/utils/wasm-gen/Cargo.toml +++ b/utils/wasm-gen/Cargo.toml @@ -27,4 +27,4 @@ proptest.workspace = true gear-backend-sandbox = { workspace = true, features = ["std"] } gear-backend-common = { workspace = true, features = ["mock"] } gear-core-processor = { workspace = true, features = ["std", "mock"] } -gear-lazy-pages-common = { workspace = true, features = ["std"] } +gear-lazy-pages-interface = { workspace = true, features = ["std"] } diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index e1cdb7d46c5..2dd1bf42477 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -244,7 +244,7 @@ fn execute_wasm_with_syscall_injected( const PROGRAM_STORAGE_PREFIX: [u8; 32] = *b"execute_wasm_with_syscall_inject"; - assert!(gear_lazy_pages_common::try_to_enable_lazy_pages( + assert!(gear_lazy_pages_interface::try_to_enable_lazy_pages( PROGRAM_STORAGE_PREFIX )); From 4a9908cc06e6381bdbcb43428898177745d2aaaf Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sat, 9 Sep 2023 23:32:06 +0300 Subject: [PATCH 19/41] Introduce `gear-lazy-pages-common` --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + core-backend/common/src/lib.rs | 2 -- core-backend/sandbox/Cargo.toml | 1 + core-backend/sandbox/src/env.rs | 8 ++++---- core-backend/sandbox/src/lib.rs | 3 ++- core-backend/sandbox/src/memory.rs | 2 +- core-backend/sandbox/src/mock.rs | 2 +- core-processor/Cargo.toml | 1 + core-processor/src/configs.rs | 2 +- core-processor/src/executor.rs | 2 +- core-processor/src/ext.rs | 4 +--- lazy-pages/Cargo.toml | 1 + lazy-pages/common/Cargo.toml | 13 +++++++++++++ .../common/src/lib.rs | 14 ++++++++------ lazy-pages/interface/Cargo.toml | 1 + lazy-pages/interface/src/lib.rs | 6 ++---- lazy-pages/src/common.rs | 7 ++----- lazy-pages/src/globals.rs | 6 ++---- lazy-pages/src/host_func.rs | 2 +- lazy-pages/src/lib.rs | 6 ++---- lazy-pages/src/process.rs | 2 +- lazy-pages/src/signal.rs | 2 +- runtime-interface/Cargo.toml | 2 ++ runtime-interface/src/lib.rs | 6 ++---- runtime/common/Cargo.toml | 1 + runtime/common/src/weights.rs | 2 +- runtime/vara/Cargo.toml | 1 + runtime/vara/src/tests.rs | 2 +- 29 files changed, 73 insertions(+), 46 deletions(-) create mode 100644 lazy-pages/common/Cargo.toml rename core-backend/common/src/lazy_pages.rs => lazy-pages/common/src/lib.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index 2ae1365f111..83444de3ba9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3814,6 +3814,7 @@ dependencies = [ "gear-backend-common", "gear-core", "gear-core-errors", + "gear-lazy-pages-common", "gear-sandbox", "gear-sandbox-env", "gear-wasm-instrument", @@ -3960,6 +3961,7 @@ dependencies = [ "gear-backend-sandbox", "gear-core", "gear-core-errors", + "gear-lazy-pages-common", "gear-lazy-pages-interface", "gear-wasm-instrument", "log", @@ -3977,6 +3979,7 @@ dependencies = [ "errno", "gear-backend-common", "gear-core", + "gear-lazy-pages-common", "gear-sandbox-host", "libc", "log", @@ -3992,6 +3995,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "gear-lazy-pages-common" +version = "0.1.0" +dependencies = [ + "gear-backend-common", + "gear-core", + "num_enum", + "parity-scale-codec", +] + [[package]] name = "gear-lazy-pages-interface" version = "0.1.0" @@ -4001,6 +4014,7 @@ dependencies = [ "gear-backend-common", "gear-common", "gear-core", + "gear-lazy-pages-common", "gear-runtime-interface", "gear-wasm-instrument", "log", @@ -4182,6 +4196,7 @@ dependencies = [ "gear-common", "gear-core", "gear-core-processor", + "gear-lazy-pages-common", "gear-runtime-primitives", "log", "pallet-authorship", @@ -4205,6 +4220,7 @@ dependencies = [ "gear-backend-common", "gear-core", "gear-lazy-pages", + "gear-lazy-pages-common", "gear-sandbox-host", "libc", "log", @@ -13507,6 +13523,7 @@ dependencies = [ "gear-backend-common", "gear-common", "gear-core-processor", + "gear-lazy-pages-common", "gear-runtime-common", "gear-runtime-primitives", "hex-literal", diff --git a/Cargo.toml b/Cargo.toml index d585378b284..8eb7d212cb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -205,6 +205,7 @@ gear-core = { path = "core" } gear-core-errors = { path = "core-errors" } gear-core-processor = { path = "core-processor", default-features = false } gear-lazy-pages = { path = "lazy-pages" } +gear-lazy-pages-common = { path = "lazy-pages/common" } gear-lazy-pages-interface = { path = "lazy-pages/interface", default-features = false } gear-node-testing = { path = "node/testing" } gear-runtime = { path = "runtime/gear" } diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 2bbf4aaea88..879b557d9e6 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -22,8 +22,6 @@ extern crate alloc; -pub mod lazy_pages; - mod utils; pub use crate::utils::LimitedStr; diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/sandbox/Cargo.toml index 7c9e54cdcc7..62d32231516 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/sandbox/Cargo.toml @@ -10,6 +10,7 @@ gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } gear-backend-common.workspace = true gear-backend-codegen.workspace = true +gear-lazy-pages-common.workspace = true gsys = { workspace = true } gear-wasm-instrument.workspace = true diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index b96b00ad274..a1930c1e3d4 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -33,10 +33,7 @@ use core::{ convert::Infallible, fmt::{Debug, Display}, }; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, - LimitedStr, -}; +use gear_backend_common::LimitedStr; use gear_core::{ env::Externalities, gas::GasAmount, @@ -44,6 +41,9 @@ use gear_core::{ message::{DispatchKind, WasmEntryPoint}, pages::{PageNumber, WasmPage}, }; +use gear_lazy_pages_common::{ + GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor, +}; use gear_sandbox::{ default_executor::{ EnvironmentDefinitionBuilder, Instance, Memory as DefaultExecutorMemory, Store, diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index 39a58d23105..ef66d96505b 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -36,7 +36,7 @@ pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; use actor_system_error::actor_system_error; -use gear_backend_common::{lazy_pages::ProcessAccessError, LimitedStr}; +use gear_backend_common::LimitedStr; use gear_core::{ env::Externalities, gas::{ChargeError, CounterType, CountersOwner, GasAmount}, @@ -45,6 +45,7 @@ use gear_core::{ message::MessageWaitedType, }; use gear_core_errors::ExtError as FallibleExtError; +use gear_lazy_pages_common::ProcessAccessError; actor_system_error! { pub type TerminationReason = ActorSystemError; diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index 93e3ccf76fb..e157e691546 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -25,7 +25,6 @@ use crate::{ use alloc::vec::Vec; use codec::{Decode, DecodeAll, MaxEncodedLen}; use core::{marker::PhantomData, mem, mem::MaybeUninit, slice}; -use gear_backend_common::lazy_pages::ProcessAccessError; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, env::Externalities, @@ -33,6 +32,7 @@ use gear_core::{ pages::{PageNumber, PageU32Size, WasmPage}, }; use gear_core_errors::MemoryError as FallibleMemoryError; +use gear_lazy_pages_common::ProcessAccessError; use gear_sandbox::{ default_executor::{Caller, Store}, SandboxMemory, diff --git a/core-backend/sandbox/src/mock.rs b/core-backend/sandbox/src/mock.rs index fd301bf1c90..10f0cc1e34d 100644 --- a/core-backend/sandbox/src/mock.rs +++ b/core-backend/sandbox/src/mock.rs @@ -23,7 +23,6 @@ use crate::{ use alloc::{collections::BTreeSet, vec, vec::Vec}; use codec::{Decode, Encode}; use core::{cell::Cell, fmt, fmt::Debug}; -use gear_backend_common::lazy_pages::ProcessAccessError; use gear_core::{ costs::RuntimeCosts, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, @@ -34,6 +33,7 @@ use gear_core::{ pages::{PageNumber, PageU32Size, WasmPage, WASM_PAGE_SIZE}, }; use gear_core_errors::{ReplyCode, SignalCode}; +use gear_lazy_pages_common::ProcessAccessError; use gear_wasm_instrument::syscalls::SysCallName; /// Mock error diff --git a/core-processor/Cargo.toml b/core-processor/Cargo.toml index a29817889c1..e6d97008384 100644 --- a/core-processor/Cargo.toml +++ b/core-processor/Cargo.toml @@ -16,6 +16,7 @@ gear-core-errors = { workspace = true, features = ["codec"] } gear-backend-common.workspace = true gear-backend-sandbox.workspace = true gear-wasm-instrument.workspace = true +gear-lazy-pages-common.workspace = true gear-lazy-pages-interface.workspace = true scale-info = { workspace = true, features = ["derive"] } diff --git a/core-processor/src/configs.rs b/core-processor/src/configs.rs index e46e856781d..5d04d70271b 100644 --- a/core-processor/src/configs.rs +++ b/core-processor/src/configs.rs @@ -19,11 +19,11 @@ //! Configurations. use alloc::{collections::BTreeSet, vec::Vec}; -use gear_backend_common::lazy_pages::LazyPagesWeights; use gear_core::{ costs::{CostPerPage, HostFnWeights}, pages::{GearPage, WasmPage}, }; +use gear_lazy_pages_common::LazyPagesWeights; use gear_wasm_instrument::syscalls::SysCallName; use scale_info::scale::{self, Decode, Encode}; diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index f366c89ca00..160abf96904 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -31,7 +31,6 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -use gear_backend_common::lazy_pages::{GlobalsAccessConfig, LazyPagesWeights}; use gear_backend_sandbox::{ env::{BackendReport, SandboxEnvironmentError}, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, @@ -51,6 +50,7 @@ use gear_core::{ program::Program, reservation::GasReserver, }; +use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights}; use scale_info::{ scale::{self, Decode, Encode}, TypeInfo, diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 89d588126dd..a62d1057a7a 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -24,9 +24,6 @@ use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, }; -use gear_backend_common::lazy_pages::{ - GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status, -}; use gear_backend_sandbox::{ ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, @@ -56,6 +53,7 @@ use gear_core_errors::{ ExecutionError as FallibleExecutionError, ExtError as FallibleExtErrorCore, MessageError, ProgramRentError, ReplyCode, ReservationError, SignalCode, }; +use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}; use gear_lazy_pages_interface as lazy_pages; use gear_wasm_instrument::syscalls::SysCallName; diff --git a/lazy-pages/Cargo.toml b/lazy-pages/Cargo.toml index 56510ab1ed5..4ed674e8cd4 100644 --- a/lazy-pages/Cargo.toml +++ b/lazy-pages/Cargo.toml @@ -23,6 +23,7 @@ once_cell.workspace = true gear-sandbox-host.workspace = true gear-core.workspace = true gear-backend-common.workspace = true +gear-lazy-pages-common.workspace = true [target."cfg(target_vendor = \"apple\")".dependencies.mach] version = "0.3.2" diff --git a/lazy-pages/common/Cargo.toml b/lazy-pages/common/Cargo.toml new file mode 100644 index 00000000000..0ce6271e973 --- /dev/null +++ b/lazy-pages/common/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "gear-lazy-pages-common" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +gear-core.workspace = true +gear-backend-common.workspace = true + +num_enum.workspace = true +codec.workspace = true diff --git a/core-backend/common/src/lazy_pages.rs b/lazy-pages/common/src/lib.rs similarity index 96% rename from core-backend/common/src/lazy_pages.rs rename to lazy-pages/common/src/lib.rs index 4d892b8f3a4..7953e7bfb1e 100644 --- a/core-backend/common/src/lazy_pages.rs +++ b/lazy-pages/common/src/lib.rs @@ -18,11 +18,13 @@ //! Core logic for usage both in runtime and in lazy-pages native part. -use crate::utils::LimitedStr; +#![no_std] + +use codec::{Decode, Encode}; use core::{any::Any, fmt::Debug}; +use gear_backend_common::LimitedStr; use gear_core::{costs::CostPerPage, memory::HostPointer, pages::GearPage}; use num_enum::{IntoPrimitive, TryFromPrimitive}; -use scale_info::scale::{self, Decode, Encode}; /// Memory access error during sys-call that lazy-pages have caught. /// 0 index is reserved for an ok result. @@ -35,7 +37,7 @@ pub enum ProcessAccessError { /// Informs lazy-pages whether they work with native or WASM runtime. #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] -#[codec(crate = scale)] +#[codec(crate = codec)] pub enum GlobalsAccessMod { /// Is wasm runtime. WasmRuntime, @@ -45,7 +47,7 @@ pub enum GlobalsAccessMod { /// Lazy-pages cases weights. #[derive(Debug, Default, Clone, PartialEq, Eq, Encode, Decode)] -#[codec(crate = scale)] +#[codec(crate = codec)] pub struct LazyPagesWeights { /// First read page access cost. pub signal_read: CostPerPage, @@ -65,7 +67,7 @@ pub struct LazyPagesWeights { /// Globals ctx for lazy-pages initialization for program. #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] -#[codec(crate = scale)] +#[codec(crate = codec)] pub struct GlobalsAccessConfig { /// Raw pointer to the globals access provider. pub access_ptr: HostPointer, @@ -105,7 +107,7 @@ pub trait GlobalsAccessor { /// termination reason sets as `gas limit exceeded` or `gas allowance exceeded`, depending on status. /// NOTE: `repr(i64)` is important to be able add additional fields, without old runtimes separate support logic. #[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)] -#[codec(crate = scale)] +#[codec(crate = codec)] #[repr(i64)] // TODO: consider removal of two exceed options in favor of one global (issue #3018). // Will require bump of many RI func's versions. diff --git a/lazy-pages/interface/Cargo.toml b/lazy-pages/interface/Cargo.toml index 862c97df08e..57fc08f0b97 100644 --- a/lazy-pages/interface/Cargo.toml +++ b/lazy-pages/interface/Cargo.toml @@ -13,6 +13,7 @@ byteorder.workspace = true gear-core.workspace = true gear-backend-common.workspace = true gear-common.workspace = true +gear-lazy-pages-common.workspace = true gear-runtime-interface.workspace = true gear-wasm-instrument.workspace = true diff --git a/lazy-pages/interface/src/lib.rs b/lazy-pages/interface/src/lib.rs index 2abfe100fb8..d975348e97d 100644 --- a/lazy-pages/interface/src/lib.rs +++ b/lazy-pages/interface/src/lib.rs @@ -24,16 +24,14 @@ extern crate alloc; use byteorder::{ByteOrder, LittleEndian}; use core::fmt; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}, - LimitedStr, -}; +use gear_backend_common::LimitedStr; use gear_common::Origin; use gear_core::{ ids::ProgramId, memory::{HostPointer, Memory, MemoryInterval}, pages::{GearPage, PageNumber, PageU32Size, WasmPage}, }; +use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}; use gear_runtime_interface::{gear_ri, LazyPagesProgramContext, LazyPagesRuntimeContext}; use gear_wasm_instrument::GLOBAL_NAME_GAS; use sp_std::{mem, vec, vec::Vec}; diff --git a/lazy-pages/src/common.rs b/lazy-pages/src/common.rs index 07f84200303..a2439e67ad2 100644 --- a/lazy-pages/src/common.rs +++ b/lazy-pages/src/common.rs @@ -20,13 +20,10 @@ use std::{collections::BTreeSet, mem::size_of, num::NonZeroU32}; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessError, Status}, - LimitedStr, -}; - use crate::{globals::GlobalsContext, mprotect::MprotectError}; +use gear_backend_common::LimitedStr; use gear_core::pages::{GearPage, PageDynSize, PageSizeNo, SizeManager, WasmPage}; +use gear_lazy_pages_common::{GlobalsAccessError, Status}; // TODO: investigate error allocations #2441 #[derive(Debug, derive_more::Display, derive_more::From)] diff --git a/lazy-pages/src/globals.rs b/lazy-pages/src/globals.rs index a2ac73f6273..21a53fe68c5 100644 --- a/lazy-pages/src/globals.rs +++ b/lazy-pages/src/globals.rs @@ -20,11 +20,9 @@ use crate::common::{Error, GlobalNames}; use core::any::Any; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}, - LimitedStr, -}; +use gear_backend_common::LimitedStr; use gear_core::memory::HostPointer; +use gear_lazy_pages_common::{GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}; use gear_sandbox_host::sandbox::SandboxInstance; use sp_wasm_interface::Value; diff --git a/lazy-pages/src/host_func.rs b/lazy-pages/src/host_func.rs index 889c3e0223e..b6dc7d793ca 100644 --- a/lazy-pages/src/host_func.rs +++ b/lazy-pages/src/host_func.rs @@ -23,12 +23,12 @@ use crate::{ process::{self, AccessHandler}, LAZY_PAGES_CONTEXT, }; -use gear_backend_common::lazy_pages::{ProcessAccessError, Status}; use gear_core::{ self, memory::MemoryInterval, pages::{GearPage, PageDynSize}, }; +use gear_lazy_pages_common::{ProcessAccessError, Status}; use std::collections::BTreeSet; pub(crate) struct HostFuncAccessHandler<'a> { diff --git a/lazy-pages/src/lib.rs b/lazy-pages/src/lib.rs index 03e1278ac11..5a2d72f1d6f 100644 --- a/lazy-pages/src/lib.rs +++ b/lazy-pages/src/lib.rs @@ -29,10 +29,7 @@ #![allow(clippy::items_after_test_module)] use common::{LazyPagesExecutionContext, LazyPagesRuntimeContext}; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, Status}, - LimitedStr, -}; +use gear_backend_common::LimitedStr; use gear_core::pages::{PageDynSize, PageNumber, PageSizeNo, WasmPage}; use sp_std::vec::Vec; use std::{cell::RefCell, convert::TryInto, num::NonZeroU32}; @@ -57,6 +54,7 @@ use crate::{ mod tests; pub use common::LazyPagesVersion; +use gear_lazy_pages_common::{GlobalsAccessConfig, Status}; pub use host_func::pre_process_memory_accesses; use mprotect::MprotectError; diff --git a/lazy-pages/src/process.rs b/lazy-pages/src/process.rs index 3807841cf8f..4e54e9ad54a 100644 --- a/lazy-pages/src/process.rs +++ b/lazy-pages/src/process.rs @@ -22,8 +22,8 @@ use crate::{ common::{Error, LazyPagesExecutionContext}, mprotect, }; -use gear_backend_common::lazy_pages::Status; use gear_core::pages::{GearPage, PageDynSize}; +use gear_lazy_pages_common::Status; use std::slice; /// `process_lazy_pages` use struct which implements this trait, diff --git a/lazy-pages/src/signal.rs b/lazy-pages/src/signal.rs index 5cae9a7e654..c04e4790200 100644 --- a/lazy-pages/src/signal.rs +++ b/lazy-pages/src/signal.rs @@ -24,8 +24,8 @@ use crate::{ process::{self, AccessHandler}, LAZY_PAGES_CONTEXT, }; -use gear_backend_common::lazy_pages::Status; use gear_core::pages::{GearPage, PageDynSize}; +use gear_lazy_pages_common::Status; use std::convert::TryFrom; pub(crate) trait UserSignalHandler { diff --git a/runtime-interface/Cargo.toml b/runtime-interface/Cargo.toml index f2dd78dbb68..2218a629822 100644 --- a/runtime-interface/Cargo.toml +++ b/runtime-interface/Cargo.toml @@ -11,6 +11,8 @@ repository.workspace = true [dependencies] gear-core.workspace = true gear-backend-common = { workspace = true } +gear-lazy-pages-common.workspace = true + log = { workspace = true, optional = true } libc.workspace = true sp-allocator.workspace = true diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index 4b3a2c31dd6..20751c8a23b 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -23,14 +23,12 @@ use byteorder::{ByteOrder, LittleEndian}; use codec::{Decode, Encode}; -use gear_backend_common::{ - lazy_pages::{GlobalsAccessConfig, ProcessAccessError, Status}, - LimitedStr, -}; +use gear_backend_common::LimitedStr; use gear_core::{ gas::GasLeft, memory::{HostPointer, MemoryInterval}, }; +use gear_lazy_pages_common::{GlobalsAccessConfig, ProcessAccessError, Status}; use sp_runtime_interface::{ pass_by::{Codec, PassBy}, runtime_interface, diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index a35574095b4..2dc106901ca 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -29,6 +29,7 @@ frame-system-benchmarking = { workspace = true, optional = true } runtime-primitives.workspace = true gear-common.workspace = true gear-backend-common.workspace = true +gear-lazy-pages-common.workspace = true gear-core-processor.workspace = true gear-core.workspace = true pallet-gear.workspace = true diff --git a/runtime/common/src/weights.rs b/runtime/common/src/weights.rs index 95317316ac7..d5a7b168135 100644 --- a/runtime/common/src/weights.rs +++ b/runtime/common/src/weights.rs @@ -1,5 +1,5 @@ -use gear_backend_common::lazy_pages::LazyPagesWeights; use gear_core_processor::configs::PageCosts; +use gear_lazy_pages_common::LazyPagesWeights; use pallet_gear::InstructionWeights; const INSTRUCTIONS_SPREAD: u8 = 50; diff --git a/runtime/vara/Cargo.toml b/runtime/vara/Cargo.toml index 1640bc001fb..aba154092d7 100644 --- a/runtime/vara/Cargo.toml +++ b/runtime/vara/Cargo.toml @@ -107,6 +107,7 @@ env_logger.workspace = true wat.workspace = true gear-core-processor.workspace = true gear-backend-common.workspace = true +gear-lazy-pages-common.workspace = true [build-dependencies] substrate-build-script-utils.workspace = true diff --git a/runtime/vara/src/tests.rs b/runtime/vara/src/tests.rs index ab677a238d5..e784daf1847 100644 --- a/runtime/vara/src/tests.rs +++ b/runtime/vara/src/tests.rs @@ -18,8 +18,8 @@ use super::*; use crate::Runtime; -use gear_backend_common::lazy_pages::LazyPagesWeights; use gear_core_processor::configs::PageCosts; +use gear_lazy_pages_common::LazyPagesWeights; use pallet_gear::{InstructionWeights, MemoryWeights}; use runtime_common::weights::{check_instructions_weights, check_pages_weights}; From 4515f9397539763fe4ee5183b63cde9df7584df2 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 10 Sep 2023 03:17:54 +0300 Subject: [PATCH 20/41] Move `LimitedStr` into core --- Cargo.lock | 1 + core-backend/common/src/lib.rs | 1 - core-backend/common/src/utils.rs | 206 ---------------------------- core-backend/sandbox/src/env.rs | 2 +- core-backend/sandbox/src/lib.rs | 2 +- core/Cargo.toml | 1 + core/src/lib.rs | 1 + core/src/str.rs | 226 +++++++++++++++++++++++++++++++ lazy-pages/common/src/lib.rs | 7 +- lazy-pages/interface/src/lib.rs | 2 +- lazy-pages/src/common.rs | 6 +- lazy-pages/src/globals.rs | 3 +- lazy-pages/src/lib.rs | 2 +- lazy-pages/src/tests.rs | 6 +- runtime-interface/src/lib.rs | 2 +- 15 files changed, 248 insertions(+), 220 deletions(-) create mode 100644 core/src/str.rs diff --git a/Cargo.lock b/Cargo.lock index 83444de3ba9..f398e9c1f43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3934,6 +3934,7 @@ dependencies = [ "parity-scale-codec", "paste", "proptest", + "rand 0.8.5", "scale-info", "static_assertions", "wabt", diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs index 879b557d9e6..6c59b626189 100644 --- a/core-backend/common/src/lib.rs +++ b/core-backend/common/src/lib.rs @@ -24,5 +24,4 @@ extern crate alloc; mod utils; -pub use crate::utils::LimitedStr; pub use log; diff --git a/core-backend/common/src/utils.rs b/core-backend/common/src/utils.rs index 923685bd214..6e5a997a5b8 100644 --- a/core-backend/common/src/utils.rs +++ b/core-backend/common/src/utils.rs @@ -16,12 +16,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use alloc::{borrow::Cow, string::String}; -use scale_info::{ - scale::{Decode, Encode}, - TypeInfo, -}; - #[macro_export] macro_rules! assert_ok { ( $x:expr $(,)? ) => { @@ -42,203 +36,3 @@ macro_rules! assert_err { assert_eq!($x, Err($y.into())); }; } - -// Max amount of bytes allowed to be thrown as string explanation of the error. -pub const TRIMMED_MAX_LEN: usize = 1024; - -fn smart_truncate(s: &mut String, max_bytes: usize) { - let mut last_byte = max_bytes; - - if s.len() > last_byte { - while !s.is_char_boundary(last_byte) { - last_byte = last_byte.saturating_sub(1); - } - - s.truncate(last_byte); - } -} - -/// Wrapped string to fit `core_backend::TRIMMED_MAX_LEN` amount of bytes. -/// -/// The `Cow` is used to avoid allocating a new `String` when the `LimitedStr` is -/// created from a `&str`. -/// -/// Plain `str` is not used because it can't be properly encoded/decoded via scale codec. -#[derive( - TypeInfo, Encode, Decode, Debug, Clone, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, -)] -pub struct LimitedStr<'a>(Cow<'a, str>); - -impl<'a> LimitedStr<'a> { - const INIT_ERROR_MSG: &str = concat!( - "String must be less than ", - stringify!(TRIMMED_MAX_LEN), - " bytes." - ); - - #[track_caller] - pub const fn from_small_str(s: &'a str) -> Self { - if s.len() > TRIMMED_MAX_LEN { - panic!("{}", Self::INIT_ERROR_MSG) - } - - Self(Cow::Borrowed(s)) - } - - pub fn as_str(&self) -> &str { - self.0.as_ref() - } -} - -#[derive(Clone, Debug, derive_more::Display)] -#[display(fmt = "String must be less than {} bytes.", TRIMMED_MAX_LEN)] -pub struct LimitedStrTryFromError; - -impl<'a> TryFrom<&'a str> for LimitedStr<'a> { - type Error = LimitedStrTryFromError; - - fn try_from(s: &'a str) -> Result { - if s.len() > TRIMMED_MAX_LEN { - return Err(LimitedStrTryFromError); - } - - Ok(Self(Cow::from(s))) - } -} - -impl<'a> From for LimitedStr<'a> { - fn from(mut s: String) -> Self { - smart_truncate(&mut s, TRIMMED_MAX_LEN); - Self(Cow::from(s)) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use rand::{distributions::Standard, Rng}; - - fn assert_result(string: &'static str, max_bytes: usize, expectation: &'static str) { - let mut string = string.into(); - smart_truncate(&mut string, max_bytes); - assert_eq!(string, expectation); - } - - fn check_panicking(initial_string: &'static str, upper_boundary: usize) { - let initial_size = initial_string.len(); - - for max_bytes in 0..=upper_boundary { - let mut string = initial_string.into(); - smart_truncate(&mut string, max_bytes); - - // Extra check just for confidence. - if max_bytes >= initial_size { - assert_eq!(string, initial_string); - } - } - } - - #[test] - fn truncate_test() { - // String for demonstration with UTF_8 encoding. - let utf_8 = "hello"; - // Length in bytes. - assert_eq!(utf_8.len(), 5); - // Length in chars. - assert_eq!(utf_8.chars().count(), 5); - - // Check that `smart_truncate` never panics. - // - // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. - check_panicking(utf_8, utf_8.len().saturating_mul(2)); - - // Asserting results. - assert_result(utf_8, 0, ""); - assert_result(utf_8, 1, "h"); - assert_result(utf_8, 2, "he"); - assert_result(utf_8, 3, "hel"); - assert_result(utf_8, 4, "hell"); - assert_result(utf_8, 5, "hello"); - assert_result(utf_8, 6, "hello"); - - // String for demonstration with CJK encoding. - let cjk = "你好吗"; - // Length in bytes. - assert_eq!(cjk.len(), 9); - // Length in chars. - assert_eq!(cjk.chars().count(), 3); - - // Check that `smart_truncate` never panics. - // - // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. - check_panicking(cjk, cjk.len().saturating_mul(2)); - - // Asserting results. - assert_result(cjk, 0, ""); - assert_result(cjk, 1, ""); - assert_result(cjk, 2, ""); - assert_result(cjk, 3, "你"); - assert_result(cjk, 4, "你"); - assert_result(cjk, 5, "你"); - assert_result(cjk, 6, "你好"); - assert_result(cjk, 7, "你好"); - assert_result(cjk, 8, "你好"); - assert_result(cjk, 9, "你好吗"); - assert_result(cjk, 10, "你好吗"); - - // String for demonstration with mixed CJK and UTF-8 encoding. - let mix = "你he好l吗lo"; // Chaotic sum of "hello" and "你好吗". - // Length in bytes. - assert_eq!(mix.len(), utf_8.len() + cjk.len()); - assert_eq!(mix.len(), 14); - // Length in chars. - assert_eq!( - mix.chars().count(), - utf_8.chars().count() + cjk.chars().count() - ); - assert_eq!(mix.chars().count(), 8); - - // Check that `smart_truncate` never panics. - // - // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. - check_panicking(mix, mix.len().saturating_mul(2)); - - // Asserting results. - assert_result(mix, 0, ""); - assert_result(mix, 1, ""); - assert_result(mix, 2, ""); - assert_result(mix, 3, "你"); - assert_result(mix, 4, "你h"); - assert_result(mix, 5, "你he"); - assert_result(mix, 6, "你he"); - assert_result(mix, 7, "你he"); - assert_result(mix, 8, "你he好"); - assert_result(mix, 9, "你he好l"); - assert_result(mix, 10, "你he好l"); - assert_result(mix, 11, "你he好l"); - assert_result(mix, 12, "你he好l吗"); - assert_result(mix, 13, "你he好l吗l"); - assert_result(mix, 14, "你he好l吗lo"); - assert_result(mix, 15, "你he好l吗lo"); - } - - #[test] - fn truncate_test_fuzz() { - for _ in 0..50 { - let mut thread_rng = rand::thread_rng(); - - let rand_len = thread_rng.gen_range(0..=100_000); - let max_bytes = thread_rng.gen_range(0..=rand_len); - let mut string = thread_rng - .sample_iter::(Standard) - .take(rand_len) - .collect(); - - smart_truncate(&mut string, max_bytes); - - if string.len() > max_bytes { - panic!("String '{}' input invalidated algorithms property", string); - } - } - } -} diff --git a/core-backend/sandbox/src/env.rs b/core-backend/sandbox/src/env.rs index a1930c1e3d4..797eb9cb05d 100644 --- a/core-backend/sandbox/src/env.rs +++ b/core-backend/sandbox/src/env.rs @@ -33,13 +33,13 @@ use core::{ convert::Infallible, fmt::{Debug, Display}, }; -use gear_backend_common::LimitedStr; use gear_core::{ env::Externalities, gas::GasAmount, memory::HostPointer, message::{DispatchKind, WasmEntryPoint}, pages::{PageNumber, WasmPage}, + str::LimitedStr, }; use gear_lazy_pages_common::{ GlobalsAccessConfig, GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor, diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/sandbox/src/lib.rs index ef66d96505b..a25085bb67e 100644 --- a/core-backend/sandbox/src/lib.rs +++ b/core-backend/sandbox/src/lib.rs @@ -36,13 +36,13 @@ pub use env::SandboxEnvironment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; use actor_system_error::actor_system_error; -use gear_backend_common::LimitedStr; use gear_core::{ env::Externalities, gas::{ChargeError, CounterType, CountersOwner, GasAmount}, ids::ProgramId, memory::MemoryInterval, message::MessageWaitedType, + str::LimitedStr, }; use gear_core_errors::ExtError as FallibleExtError; use gear_lazy_pages_common::ProcessAccessError; diff --git a/core/Cargo.toml b/core/Cargo.toml index 666e400433f..517a43e6bbc 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -30,6 +30,7 @@ byteorder.workspace = true wabt.workspace = true env_logger.workspace = true proptest.workspace = true +rand = { workspace = true, features = ["std", "std_rng"] } [features] strict = [] diff --git a/core/src/lib.rs b/core/src/lib.rs index 5a15add4cfb..1996e7492dd 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -39,6 +39,7 @@ pub mod program; pub mod reservation; pub mod buffer; +pub mod str; use core::mem::size_of; use static_assertions::const_assert; diff --git a/core/src/str.rs b/core/src/str.rs new file mode 100644 index 00000000000..ea5dc35a46d --- /dev/null +++ b/core/src/str.rs @@ -0,0 +1,226 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! String with limited length realization. + +use alloc::{borrow::Cow, string::String}; +use parity_scale_codec::{Decode, Encode}; +use scale_info::TypeInfo; + +/// Max amount of bytes allowed to be thrown as string explanation of the error. +pub const TRIMMED_MAX_LEN: usize = 1024; + +fn smart_truncate(s: &mut String, max_bytes: usize) { + let mut last_byte = max_bytes; + + if s.len() > last_byte { + while !s.is_char_boundary(last_byte) { + last_byte = last_byte.saturating_sub(1); + } + + s.truncate(last_byte); + } +} + +/// Wrapped string to fit [`TRIMMED_MAX_LEN`] amount of bytes. +/// +/// The `Cow` is used to avoid allocating a new `String` when the `LimitedStr` is +/// created from a `&str`. +/// +/// Plain `str` is not used because it can't be properly encoded/decoded via scale codec. +#[derive( + TypeInfo, Encode, Decode, Debug, Clone, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, +)] +pub struct LimitedStr<'a>(Cow<'a, str>); + +impl<'a> LimitedStr<'a> { + const INIT_ERROR_MSG: &str = concat!( + "String must be less than ", + stringify!(TRIMMED_MAX_LEN), + " bytes." + ); + + /// Convert from `&str` in compile-time. + #[track_caller] + pub const fn from_small_str(s: &'a str) -> Self { + if s.len() > TRIMMED_MAX_LEN { + panic!("{}", Self::INIT_ERROR_MSG) + } + + Self(Cow::Borrowed(s)) + } + + /// Return string slice. + pub fn as_str(&self) -> &str { + self.0.as_ref() + } +} + +/// The error type returned when a conversion from `&str` to [`LimitedStr`] fails. +#[derive(Clone, Debug, derive_more::Display)] +#[display(fmt = "String must be less than {} bytes.", TRIMMED_MAX_LEN)] +pub struct LimitedStrTryFromError; + +impl<'a> TryFrom<&'a str> for LimitedStr<'a> { + type Error = LimitedStrTryFromError; + + fn try_from(s: &'a str) -> Result { + if s.len() > TRIMMED_MAX_LEN { + return Err(LimitedStrTryFromError); + } + + Ok(Self(Cow::from(s))) + } +} + +impl<'a> From for LimitedStr<'a> { + fn from(mut s: String) -> Self { + smart_truncate(&mut s, TRIMMED_MAX_LEN); + Self(Cow::from(s)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rand::{distributions::Standard, Rng}; + + fn assert_result(string: &'static str, max_bytes: usize, expectation: &'static str) { + let mut string = string.into(); + smart_truncate(&mut string, max_bytes); + assert_eq!(string, expectation); + } + + fn check_panicking(initial_string: &'static str, upper_boundary: usize) { + let initial_size = initial_string.len(); + + for max_bytes in 0..=upper_boundary { + let mut string = initial_string.into(); + smart_truncate(&mut string, max_bytes); + + // Extra check just for confidence. + if max_bytes >= initial_size { + assert_eq!(string, initial_string); + } + } + } + + #[test] + fn truncate_test() { + // String for demonstration with UTF_8 encoding. + let utf_8 = "hello"; + // Length in bytes. + assert_eq!(utf_8.len(), 5); + // Length in chars. + assert_eq!(utf_8.chars().count(), 5); + + // Check that `smart_truncate` never panics. + // + // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. + check_panicking(utf_8, utf_8.len().saturating_mul(2)); + + // Asserting results. + assert_result(utf_8, 0, ""); + assert_result(utf_8, 1, "h"); + assert_result(utf_8, 2, "he"); + assert_result(utf_8, 3, "hel"); + assert_result(utf_8, 4, "hell"); + assert_result(utf_8, 5, "hello"); + assert_result(utf_8, 6, "hello"); + + // String for demonstration with CJK encoding. + let cjk = "你好吗"; + // Length in bytes. + assert_eq!(cjk.len(), 9); + // Length in chars. + assert_eq!(cjk.chars().count(), 3); + + // Check that `smart_truncate` never panics. + // + // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. + check_panicking(cjk, cjk.len().saturating_mul(2)); + + // Asserting results. + assert_result(cjk, 0, ""); + assert_result(cjk, 1, ""); + assert_result(cjk, 2, ""); + assert_result(cjk, 3, "你"); + assert_result(cjk, 4, "你"); + assert_result(cjk, 5, "你"); + assert_result(cjk, 6, "你好"); + assert_result(cjk, 7, "你好"); + assert_result(cjk, 8, "你好"); + assert_result(cjk, 9, "你好吗"); + assert_result(cjk, 10, "你好吗"); + + // String for demonstration with mixed CJK and UTF-8 encoding. + let mix = "你he好l吗lo"; // Chaotic sum of "hello" and "你好吗". + // Length in bytes. + assert_eq!(mix.len(), utf_8.len() + cjk.len()); + assert_eq!(mix.len(), 14); + // Length in chars. + assert_eq!( + mix.chars().count(), + utf_8.chars().count() + cjk.chars().count() + ); + assert_eq!(mix.chars().count(), 8); + + // Check that `smart_truncate` never panics. + // + // It calls the `smart_truncate` with `max_bytes` arg in 0..= len * 2. + check_panicking(mix, mix.len().saturating_mul(2)); + + // Asserting results. + assert_result(mix, 0, ""); + assert_result(mix, 1, ""); + assert_result(mix, 2, ""); + assert_result(mix, 3, "你"); + assert_result(mix, 4, "你h"); + assert_result(mix, 5, "你he"); + assert_result(mix, 6, "你he"); + assert_result(mix, 7, "你he"); + assert_result(mix, 8, "你he好"); + assert_result(mix, 9, "你he好l"); + assert_result(mix, 10, "你he好l"); + assert_result(mix, 11, "你he好l"); + assert_result(mix, 12, "你he好l吗"); + assert_result(mix, 13, "你he好l吗l"); + assert_result(mix, 14, "你he好l吗lo"); + assert_result(mix, 15, "你he好l吗lo"); + } + + #[test] + fn truncate_test_fuzz() { + for _ in 0..50 { + let mut thread_rng = rand::thread_rng(); + + let rand_len = thread_rng.gen_range(0..=100_000); + let max_bytes = thread_rng.gen_range(0..=rand_len); + let mut string = thread_rng + .sample_iter::(Standard) + .take(rand_len) + .collect(); + + smart_truncate(&mut string, max_bytes); + + if string.len() > max_bytes { + panic!("String '{}' input invalidated algorithms property", string); + } + } + } +} diff --git a/lazy-pages/common/src/lib.rs b/lazy-pages/common/src/lib.rs index 7953e7bfb1e..a925d4daf0f 100644 --- a/lazy-pages/common/src/lib.rs +++ b/lazy-pages/common/src/lib.rs @@ -22,8 +22,7 @@ use codec::{Decode, Encode}; use core::{any::Any, fmt::Debug}; -use gear_backend_common::LimitedStr; -use gear_core::{costs::CostPerPage, memory::HostPointer, pages::GearPage}; +use gear_core::{costs::CostPerPage, memory::HostPointer, pages::GearPage, str::LimitedStr}; use num_enum::{IntoPrimitive, TryFromPrimitive}; /// Memory access error during sys-call that lazy-pages have caught. @@ -83,16 +82,20 @@ pub struct GlobalsAccessError; pub trait GlobalsAccessor { /// Returns global `name` value, if `name` is I64 global export. fn get_i64(&self, name: &LimitedStr) -> Result; + /// Set global `name` == `value`, if `name` is I64 global export. fn set_i64(&mut self, name: &LimitedStr, value: i64) -> Result<(), GlobalsAccessError>; + /// Returns global `name` value, if `name` is I32 global export. fn get_i32(&self, _name: &LimitedStr) -> Result { unimplemented!("Currently has no i32 system globals") } + /// Set global `name` == `value`, if `name` is I32 global export. fn set_i32(&mut self, _name: &LimitedStr, _value: i32) -> Result<(), GlobalsAccessError> { unimplemented!("Currently has no i32 system globals") } + /// Returns as `&mut dyn Any`. fn as_any_mut(&mut self) -> &mut dyn Any; } diff --git a/lazy-pages/interface/src/lib.rs b/lazy-pages/interface/src/lib.rs index d975348e97d..9aab7a86f27 100644 --- a/lazy-pages/interface/src/lib.rs +++ b/lazy-pages/interface/src/lib.rs @@ -24,12 +24,12 @@ extern crate alloc; use byteorder::{ByteOrder, LittleEndian}; use core::fmt; -use gear_backend_common::LimitedStr; use gear_common::Origin; use gear_core::{ ids::ProgramId, memory::{HostPointer, Memory, MemoryInterval}, pages::{GearPage, PageNumber, PageU32Size, WasmPage}, + str::LimitedStr, }; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}; use gear_runtime_interface::{gear_ri, LazyPagesProgramContext, LazyPagesRuntimeContext}; diff --git a/lazy-pages/src/common.rs b/lazy-pages/src/common.rs index a2439e67ad2..a1841e56dcf 100644 --- a/lazy-pages/src/common.rs +++ b/lazy-pages/src/common.rs @@ -21,8 +21,10 @@ use std::{collections::BTreeSet, mem::size_of, num::NonZeroU32}; use crate::{globals::GlobalsContext, mprotect::MprotectError}; -use gear_backend_common::LimitedStr; -use gear_core::pages::{GearPage, PageDynSize, PageSizeNo, SizeManager, WasmPage}; +use gear_core::{ + pages::{GearPage, PageDynSize, PageSizeNo, SizeManager, WasmPage}, + str::LimitedStr, +}; use gear_lazy_pages_common::{GlobalsAccessError, Status}; // TODO: investigate error allocations #2441 diff --git a/lazy-pages/src/globals.rs b/lazy-pages/src/globals.rs index 21a53fe68c5..6418a13bec9 100644 --- a/lazy-pages/src/globals.rs +++ b/lazy-pages/src/globals.rs @@ -20,8 +20,7 @@ use crate::common::{Error, GlobalNames}; use core::any::Any; -use gear_backend_common::LimitedStr; -use gear_core::memory::HostPointer; +use gear_core::{memory::HostPointer, str::LimitedStr}; use gear_lazy_pages_common::{GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor}; use gear_sandbox_host::sandbox::SandboxInstance; use sp_wasm_interface::Value; diff --git a/lazy-pages/src/lib.rs b/lazy-pages/src/lib.rs index 5a2d72f1d6f..7b384255a06 100644 --- a/lazy-pages/src/lib.rs +++ b/lazy-pages/src/lib.rs @@ -29,7 +29,6 @@ #![allow(clippy::items_after_test_module)] use common::{LazyPagesExecutionContext, LazyPagesRuntimeContext}; -use gear_backend_common::LimitedStr; use gear_core::pages::{PageDynSize, PageNumber, PageSizeNo, WasmPage}; use sp_std::vec::Vec; use std::{cell::RefCell, convert::TryInto, num::NonZeroU32}; @@ -54,6 +53,7 @@ use crate::{ mod tests; pub use common::LazyPagesVersion; +use gear_core::str::LimitedStr; use gear_lazy_pages_common::{GlobalsAccessConfig, Status}; pub use host_func::pre_process_memory_accesses; diff --git a/lazy-pages/src/tests.rs b/lazy-pages/src/tests.rs index ae4852df027..a5af3986526 100644 --- a/lazy-pages/src/tests.rs +++ b/lazy-pages/src/tests.rs @@ -20,8 +20,10 @@ use crate::{ common::Error, init_with_handler, mprotect, signal::ExceptionInfo, LazyPagesVersion, UserSignalHandler, }; -use gear_backend_common::LimitedStr; -use gear_core::pages::{GearPage, PageDynSize, PageU32Size, WasmPage}; +use gear_core::{ + pages::{GearPage, PageDynSize, PageU32Size, WasmPage}, + str::LimitedStr, +}; use region::Protection; fn handler_tester(f: F) { diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index 20751c8a23b..0b3c09bff66 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -23,7 +23,6 @@ use byteorder::{ByteOrder, LittleEndian}; use codec::{Decode, Encode}; -use gear_backend_common::LimitedStr; use gear_core::{ gas::GasLeft, memory::{HostPointer, MemoryInterval}, @@ -40,6 +39,7 @@ extern crate alloc; #[cfg(feature = "std")] use gear_lazy_pages as lazy_pages; +use gear_core::str::LimitedStr; pub use sp_std::{convert::TryFrom, result::Result, vec::Vec}; mod gear_sandbox; From 8ae4609d9309cb6060ef9aae8cd6082e61d65e39 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 10 Sep 2023 03:26:42 +0300 Subject: [PATCH 21/41] Remove `gear-backend-common` --- Cargo.lock | 29 ---------------------- Cargo.toml | 1 - core-backend/common/Cargo.toml | 29 ---------------------- core-backend/common/src/lib.rs | 27 -------------------- core-backend/common/src/utils.rs | 38 ---------------------------- core-backend/sandbox/Cargo.toml | 1 - core-backend/sandbox/src/memory.rs | 40 ++++++++++++++++-------------- core-processor/Cargo.toml | 1 - core-processor/src/precharge.rs | 7 +++--- gtest/Cargo.toml | 1 - lazy-pages/Cargo.toml | 1 - lazy-pages/common/Cargo.toml | 1 - lazy-pages/interface/Cargo.toml | 1 - pallets/gear/Cargo.toml | 3 +-- runtime-interface/Cargo.toml | 1 - runtime/common/Cargo.toml | 1 - runtime/gear/Cargo.toml | 1 - runtime/vara/Cargo.toml | 1 - utils/wasm-gen/Cargo.toml | 1 - utils/wasm-instrument/Cargo.toml | 1 - 20 files changed, 26 insertions(+), 160 deletions(-) delete mode 100644 core-backend/common/Cargo.toml delete mode 100644 core-backend/common/src/lib.rs delete mode 100644 core-backend/common/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index f398e9c1f43..67c1a920b16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3787,22 +3787,6 @@ dependencies = [ "syn 2.0.31", ] -[[package]] -name = "gear-backend-common" -version = "1.0.0" -dependencies = [ - "derive_more", - "gear-core", - "gear-core-errors", - "gear-wasm-instrument", - "gsys", - "log", - "num_enum", - "parity-scale-codec", - "rand 0.8.5", - "scale-info", -] - [[package]] name = "gear-backend-sandbox" version = "0.1.0" @@ -3811,7 +3795,6 @@ dependencies = [ "blake2-rfc", "derive_more", "gear-backend-codegen", - "gear-backend-common", "gear-core", "gear-core-errors", "gear-lazy-pages-common", @@ -3958,7 +3941,6 @@ dependencies = [ "derive_more", "enum-iterator 1.4.1", "env_logger", - "gear-backend-common", "gear-backend-sandbox", "gear-core", "gear-core-errors", @@ -3978,7 +3960,6 @@ dependencies = [ "derive_more", "env_logger", "errno", - "gear-backend-common", "gear-core", "gear-lazy-pages-common", "gear-sandbox-host", @@ -4000,7 +3981,6 @@ dependencies = [ name = "gear-lazy-pages-common" version = "0.1.0" dependencies = [ - "gear-backend-common", "gear-core", "num_enum", "parity-scale-codec", @@ -4012,7 +3992,6 @@ version = "0.1.0" dependencies = [ "byteorder", "derive_more", - "gear-backend-common", "gear-common", "gear-core", "gear-lazy-pages-common", @@ -4135,7 +4114,6 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", - "gear-backend-common", "gear-common", "gear-core-processor", "gear-runtime-common", @@ -4193,7 +4171,6 @@ dependencies = [ "frame-support", "frame-system", "frame-system-benchmarking", - "gear-backend-common", "gear-common", "gear-core", "gear-core-processor", @@ -4218,7 +4195,6 @@ version = "1.0.0" dependencies = [ "byteorder", "derive_more", - "gear-backend-common", "gear-core", "gear-lazy-pages", "gear-lazy-pages-common", @@ -4443,7 +4419,6 @@ name = "gear-wasm-gen" version = "0.1.0" dependencies = [ "arbitrary", - "gear-backend-common", "gear-backend-sandbox", "gear-core", "gear-core-processor", @@ -4468,7 +4443,6 @@ name = "gear-wasm-instrument" version = "1.0.0" dependencies = [ "enum-iterator 1.4.1", - "gear-backend-common", "gear-backend-sandbox", "gear-core", "gwasm-instrument", @@ -4777,7 +4751,6 @@ dependencies = [ "demo-ping", "derive_more", "env_logger", - "gear-backend-common", "gear-core", "gear-core-errors", "gear-core-processor", @@ -7380,7 +7353,6 @@ dependencies = [ "frame-support", "frame-support-test", "frame-system", - "gear-backend-common", "gear-backend-sandbox", "gear-common", "gear-core", @@ -13521,7 +13493,6 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", - "gear-backend-common", "gear-common", "gear-core-processor", "gear-lazy-pages-common", diff --git a/Cargo.toml b/Cargo.toml index 8eb7d212cb4..b6be1f63a07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -196,7 +196,6 @@ gtest = { path = "gtest" } gmeta = { path = "gmeta" } gear-authorship = { path = "node/authorship" } gear-backend-codegen = { path = "core-backend/codegen" } -gear-backend-common = { path = "core-backend/common" } gear-backend-sandbox = { path = "core-backend/sandbox", default-features = false } gear-call-gen = { path = "utils/call-gen" } gear-common = { path = "common", default-features = false } diff --git a/core-backend/common/Cargo.toml b/core-backend/common/Cargo.toml deleted file mode 100644 index 231001371aa..00000000000 --- a/core-backend/common/Cargo.toml +++ /dev/null @@ -1,29 +0,0 @@ -[package] -name = "gear-backend-common" -description = "Common library for gear-core-backend" -keywords = ["gear", "wasm", "codegen"] -categories = ["wasm"] -version.workspace = true -authors.workspace = true -edition.workspace = true -license.workspace = true -homepage.workspace = true -repository.workspace = true - -[dependencies] -gear-core.workspace = true -gear-core-errors = { workspace = true, features = ["codec"] } -gear-wasm-instrument.workspace = true - -derive_more.workspace = true -gsys.workspace = true -log.workspace = true -num_enum.workspace = true -parity-scale-codec.workspace = true -scale-info = { workspace = true, features = ["derive"] } - -[dev-dependencies] -rand = { workspace = true, features = ["std", "std_rng"] } - -[features] -mock = [] diff --git a/core-backend/common/src/lib.rs b/core-backend/common/src/lib.rs deleted file mode 100644 index 6c59b626189..00000000000 --- a/core-backend/common/src/lib.rs +++ /dev/null @@ -1,27 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2023 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Crate provides support for wasm runtime. - -#![no_std] - -extern crate alloc; - -mod utils; - -pub use log; diff --git a/core-backend/common/src/utils.rs b/core-backend/common/src/utils.rs deleted file mode 100644 index 6e5a997a5b8..00000000000 --- a/core-backend/common/src/utils.rs +++ /dev/null @@ -1,38 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2022-2023 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -#[macro_export] -macro_rules! assert_ok { - ( $x:expr $(,)? ) => { - let is = $x; - match is { - Ok(_) => (), - _ => assert!(false, "Expected Ok(_). Got {:#?}", is), - } - }; - ( $x:expr, $y:expr $(,)? ) => { - assert_eq!($x, Ok($y)); - }; -} - -#[macro_export] -macro_rules! assert_err { - ( $x:expr , $y:expr $(,)? ) => { - assert_eq!($x, Err($y.into())); - }; -} diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/sandbox/Cargo.toml index 62d32231516..4fa0abc0095 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/sandbox/Cargo.toml @@ -8,7 +8,6 @@ license.workspace = true [dependencies] gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } -gear-backend-common.workspace = true gear-backend-codegen.workspace = true gear-lazy-pages-common.workspace = true gsys = { workspace = true } diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/sandbox/src/memory.rs index e157e691546..a88cd5251d4 100644 --- a/core-backend/sandbox/src/memory.rs +++ b/core-backend/sandbox/src/memory.rs @@ -469,7 +469,6 @@ pub struct WasmMemoryWrite { mod tests { use super::*; use crate::{mock::MockExt, state::State, ActorTerminationReason}; - use gear_backend_common::{assert_err, assert_ok}; use gear_core::memory::{AllocError, AllocationsContext, NoopGrowHandler}; use gear_sandbox::{AsContextExt, SandboxStore}; @@ -501,52 +500,55 @@ mod tests { fn smoky() { let (mut ctx, mut mem_wrap) = new_test_memory(16, 256); - assert_ok!( - ctx.alloc::(16.into(), &mut mem_wrap, |_| Ok(())), + assert_eq!( + ctx.alloc::(16.into(), &mut mem_wrap, |_| Ok(())) + .unwrap(), 16.into() ); - assert_ok!( - ctx.alloc::(0.into(), &mut mem_wrap, |_| Ok(())), + assert_eq!( + ctx.alloc::(0.into(), &mut mem_wrap, |_| Ok(())) + .unwrap(), 16.into() ); // there is a space for 14 more for _ in 0..14 { - assert_ok!(ctx.alloc::(16.into(), &mut mem_wrap, |_| Ok(()))); + ctx.alloc::(16.into(), &mut mem_wrap, |_| Ok(())) + .unwrap(); } // no more mem! - assert_err!( + assert_eq!( ctx.alloc::(1.into(), &mut mem_wrap, |_| Ok(())), - AllocError::ProgramAllocOutOfBounds + Err(AllocError::ProgramAllocOutOfBounds) ); // but we free some - assert_ok!(ctx.free(137.into())); + ctx.free(137.into()).unwrap(); // and now can allocate page that was freed - assert_ok!( + assert_eq!( ctx.alloc::(1.into(), &mut mem_wrap, |_| Ok(())), - 137.into() + Ok(137.into()) ); // if we have 2 in a row we can allocate even 2 - assert_ok!(ctx.free(117.into())); - assert_ok!(ctx.free(118.into())); + ctx.free(117.into()).unwrap(); + ctx.free(118.into()).unwrap(); - assert_ok!( + assert_eq!( ctx.alloc::(2.into(), &mut mem_wrap, |_| Ok(())), - 117.into() + Ok(117.into()) ); // but if 2 are not in a row, bad luck - assert_ok!(ctx.free(117.into())); - assert_ok!(ctx.free(158.into())); + ctx.free(117.into()).unwrap(); + ctx.free(158.into()).unwrap(); - assert_err!( + assert_eq!( ctx.alloc::(2.into(), &mut mem_wrap, |_| Ok(())), - AllocError::ProgramAllocOutOfBounds + Err(AllocError::ProgramAllocOutOfBounds) ); } } diff --git a/core-processor/Cargo.toml b/core-processor/Cargo.toml index e6d97008384..6400be6a35e 100644 --- a/core-processor/Cargo.toml +++ b/core-processor/Cargo.toml @@ -13,7 +13,6 @@ repository.workspace = true [dependencies] gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } -gear-backend-common.workspace = true gear-backend-sandbox.workspace = true gear-wasm-instrument.workspace = true gear-lazy-pages-common.workspace = true diff --git a/core-processor/src/precharge.rs b/core-processor/src/precharge.rs index ccb9b0eb8bb..26b0b3191d8 100644 --- a/core-processor/src/precharge.rs +++ b/core-processor/src/precharge.rs @@ -457,7 +457,6 @@ pub fn precharge_for_memory( #[cfg(test)] mod tests { use super::*; - use gear_backend_common::assert_ok; fn prepare_gas_counters() -> (GasCounter, GasAllowanceCounter) { ( @@ -472,9 +471,11 @@ mod tests { let (mut gas_counter, mut gas_allowance_counter) = prepare_gas_counters(); let mut charger = GasPrecharger::new(&mut gas_counter, &mut gas_allowance_counter); let static_pages = 4.into(); - let res = charger.charge_gas_for_pages(&costs, &Default::default(), static_pages); + let res = charger + .charge_gas_for_pages(&costs, &Default::default(), static_pages) + .unwrap(); // Result is static pages count - assert_ok!(res, static_pages); + assert_eq!(res, static_pages); // Charging for static pages initialization let charge = costs.static_page.calc(static_pages); assert_eq!(charger.counter.left(), 1_000_000 - charge); diff --git a/gtest/Cargo.toml b/gtest/Cargo.toml index a559136423c..169c292db3a 100644 --- a/gtest/Cargo.toml +++ b/gtest/Cargo.toml @@ -8,7 +8,6 @@ license.workspace = true [dependencies] gear-core.workspace = true gear-core-errors.workspace = true -gear-backend-common.workspace = true core-processor = { workspace = true, features = ["std"] } gear-lazy-pages-interface = { workspace = true, features = ["std"] } gear-wasm-builder.workspace = true diff --git a/lazy-pages/Cargo.toml b/lazy-pages/Cargo.toml index 4ed674e8cd4..bd7f129ec56 100644 --- a/lazy-pages/Cargo.toml +++ b/lazy-pages/Cargo.toml @@ -22,7 +22,6 @@ once_cell.workspace = true gear-sandbox-host.workspace = true gear-core.workspace = true -gear-backend-common.workspace = true gear-lazy-pages-common.workspace = true [target."cfg(target_vendor = \"apple\")".dependencies.mach] diff --git a/lazy-pages/common/Cargo.toml b/lazy-pages/common/Cargo.toml index 0ce6271e973..3434d3291fb 100644 --- a/lazy-pages/common/Cargo.toml +++ b/lazy-pages/common/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] gear-core.workspace = true -gear-backend-common.workspace = true num_enum.workspace = true codec.workspace = true diff --git a/lazy-pages/interface/Cargo.toml b/lazy-pages/interface/Cargo.toml index 57fc08f0b97..f04cccf51e7 100644 --- a/lazy-pages/interface/Cargo.toml +++ b/lazy-pages/interface/Cargo.toml @@ -11,7 +11,6 @@ log.workspace = true byteorder.workspace = true gear-core.workspace = true -gear-backend-common.workspace = true gear-common.workspace = true gear-lazy-pages-common.workspace = true gear-runtime-interface.workspace = true diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 61044982784..dd555e0b60c 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -30,7 +30,6 @@ gear-lazy-pages-interface.workspace = true core-processor.workspace = true gear-core.workspace = true gear-core-errors.workspace = true -gear-backend-common.workspace = true gear-backend-sandbox.workspace = true pallet-gear-proc-macro = { path = "proc-macro" } gsys = { workspace = true, optional = true } @@ -172,7 +171,7 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "frame-support/runtime-benchmarks", "common/runtime-benchmarks", - "gear-backend-common/mock", + "gear-backend-sandbox/mock", "gear-core-errors/codec", "gear-sandbox", "sp-consensus-slots", diff --git a/runtime-interface/Cargo.toml b/runtime-interface/Cargo.toml index 2218a629822..f992014ca26 100644 --- a/runtime-interface/Cargo.toml +++ b/runtime-interface/Cargo.toml @@ -10,7 +10,6 @@ repository.workspace = true [dependencies] gear-core.workspace = true -gear-backend-common = { workspace = true } gear-lazy-pages-common.workspace = true log = { workspace = true, optional = true } diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 2dc106901ca..e52e9865188 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -28,7 +28,6 @@ frame-system-benchmarking = { workspace = true, optional = true } # Internal deps runtime-primitives.workspace = true gear-common.workspace = true -gear-backend-common.workspace = true gear-lazy-pages-common.workspace = true gear-core-processor.workspace = true gear-core.workspace = true diff --git a/runtime/gear/Cargo.toml b/runtime/gear/Cargo.toml index 77751e6991e..41ccbe0c5aa 100644 --- a/runtime/gear/Cargo.toml +++ b/runtime/gear/Cargo.toml @@ -79,7 +79,6 @@ substrate-wasm-builder = { workspace = true, optional = true } [dev-dependencies] gear-core-processor.workspace = true -gear-backend-common.workspace = true [features] default = ["std"] diff --git a/runtime/vara/Cargo.toml b/runtime/vara/Cargo.toml index aba154092d7..1b70602582f 100644 --- a/runtime/vara/Cargo.toml +++ b/runtime/vara/Cargo.toml @@ -106,7 +106,6 @@ sp-keyring.workspace = true env_logger.workspace = true wat.workspace = true gear-core-processor.workspace = true -gear-backend-common.workspace = true gear-lazy-pages-common.workspace = true [build-dependencies] diff --git a/utils/wasm-gen/Cargo.toml b/utils/wasm-gen/Cargo.toml index 78af46a4936..210d21655fa 100644 --- a/utils/wasm-gen/Cargo.toml +++ b/utils/wasm-gen/Cargo.toml @@ -25,6 +25,5 @@ indicatif.workspace = true proptest.workspace = true gear-backend-sandbox = { workspace = true, features = ["std"] } -gear-backend-common = { workspace = true, features = ["mock"] } gear-core-processor = { workspace = true, features = ["std", "mock"] } gear-lazy-pages-interface = { workspace = true, features = ["std"] } diff --git a/utils/wasm-instrument/Cargo.toml b/utils/wasm-instrument/Cargo.toml index 3e3a2ebb2e2..a1a6729280b 100644 --- a/utils/wasm-instrument/Cargo.toml +++ b/utils/wasm-instrument/Cargo.toml @@ -18,7 +18,6 @@ enum-iterator.workspace = true wasmparser.workspace = true wat.workspace = true gear-backend-sandbox = { workspace = true, features = ["mock"] } -gear-backend-common = { workspace = true, features = ["mock"] } gear-core.workspace = true [features] From 79bba6200b5f270c06bc5d2ca9710b195df200e0 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 10 Sep 2023 03:52:18 +0300 Subject: [PATCH 22/41] Move sandbox backend into `core-backend` root --- Cargo.lock | 64 +++++++++++------------ Cargo.toml | 7 +-- core-backend/{sandbox => }/Cargo.toml | 4 +- core-backend/codegen/Cargo.toml | 2 +- core-backend/{sandbox => }/src/env.rs | 0 core-backend/{sandbox => }/src/funcs.rs | 2 +- core-backend/{sandbox => }/src/lib.rs | 0 core-backend/{sandbox => }/src/memory.rs | 0 core-backend/{sandbox => }/src/mock.rs | 0 core-backend/{sandbox => }/src/runtime.rs | 0 core-backend/{sandbox => }/src/state.rs | 0 core-backend/{sandbox => }/src/tests.rs | 0 core-processor/Cargo.toml | 4 +- core-processor/src/common.rs | 6 +-- core-processor/src/executor.rs | 10 ++-- core-processor/src/ext.rs | 10 ++-- core-processor/src/processing.rs | 6 +-- pallets/gear/Cargo.toml | 6 +-- pallets/gear/src/benchmarking/mod.rs | 2 +- pallets/gear/src/tests.rs | 6 +-- utils/wasm-gen/Cargo.toml | 2 +- utils/wasm-gen/src/tests.rs | 5 +- utils/wasm-instrument/Cargo.toml | 4 +- utils/wasm-instrument/src/tests.rs | 4 +- 24 files changed, 72 insertions(+), 72 deletions(-) rename core-backend/{sandbox => }/Cargo.toml (91%) rename core-backend/{sandbox => }/src/env.rs (100%) rename core-backend/{sandbox => }/src/funcs.rs (99%) rename core-backend/{sandbox => }/src/lib.rs (100%) rename core-backend/{sandbox => }/src/memory.rs (100%) rename core-backend/{sandbox => }/src/mock.rs (100%) rename core-backend/{sandbox => }/src/runtime.rs (100%) rename core-backend/{sandbox => }/src/state.rs (100%) rename core-backend/{sandbox => }/src/tests.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 67c1a920b16..0107be59a8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3778,34 +3778,6 @@ dependencies = [ "vara-runtime", ] -[[package]] -name = "gear-backend-codegen" -version = "1.0.0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.31", -] - -[[package]] -name = "gear-backend-sandbox" -version = "0.1.0" -dependencies = [ - "actor-system-error", - "blake2-rfc", - "derive_more", - "gear-backend-codegen", - "gear-core", - "gear-core-errors", - "gear-lazy-pages-common", - "gear-sandbox", - "gear-sandbox-env", - "gear-wasm-instrument", - "gsys", - "log", - "parity-scale-codec", -] - [[package]] name = "gear-bags-thresholds" version = "1.0.0" @@ -3924,6 +3896,34 @@ dependencies = [ "wasmparser-nostd 0.100.1", ] +[[package]] +name = "gear-core-backend" +version = "0.1.0" +dependencies = [ + "actor-system-error", + "blake2-rfc", + "derive_more", + "gear-core", + "gear-core-backend-codegen", + "gear-core-errors", + "gear-lazy-pages-common", + "gear-sandbox", + "gear-sandbox-env", + "gear-wasm-instrument", + "gsys", + "log", + "parity-scale-codec", +] + +[[package]] +name = "gear-core-backend-codegen" +version = "0.3.2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.29", +] + [[package]] name = "gear-core-errors" version = "1.0.0" @@ -3941,8 +3941,8 @@ dependencies = [ "derive_more", "enum-iterator 1.4.1", "env_logger", - "gear-backend-sandbox", "gear-core", + "gear-core-backend", "gear-core-errors", "gear-lazy-pages-common", "gear-lazy-pages-interface", @@ -4419,8 +4419,8 @@ name = "gear-wasm-gen" version = "0.1.0" dependencies = [ "arbitrary", - "gear-backend-sandbox", "gear-core", + "gear-core-backend", "gear-core-processor", "gear-lazy-pages-interface", "gear-utils", @@ -4443,8 +4443,8 @@ name = "gear-wasm-instrument" version = "1.0.0" dependencies = [ "enum-iterator 1.4.1", - "gear-backend-sandbox", "gear-core", + "gear-core-backend", "gwasm-instrument", "wasmparser-nostd 0.100.1", "wat", @@ -7353,9 +7353,9 @@ dependencies = [ "frame-support", "frame-support-test", "frame-system", - "gear-backend-sandbox", "gear-common", "gear-core", + "gear-core-backend", "gear-core-errors", "gear-core-processor", "gear-lazy-pages-interface", diff --git a/Cargo.toml b/Cargo.toml index b6be1f63a07..a3e7998aee9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,8 @@ members = [ "common", "common/codegen", "core", - "core-backend/*", + "core-backend", + "core-backend/codegen", "core-processor", "core-errors", "examples/async", @@ -195,8 +196,8 @@ gsys = { path = "gsys" } gtest = { path = "gtest" } gmeta = { path = "gmeta" } gear-authorship = { path = "node/authorship" } -gear-backend-codegen = { path = "core-backend/codegen" } -gear-backend-sandbox = { path = "core-backend/sandbox", default-features = false } +gear-core-backend-codegen = { path = "core-backend/codegen" } +gear-core-backend = { path = "core-backend", default-features = false } gear-call-gen = { path = "utils/call-gen" } gear-common = { path = "common", default-features = false } gear-common-codegen = { path = "common/codegen" } diff --git a/core-backend/sandbox/Cargo.toml b/core-backend/Cargo.toml similarity index 91% rename from core-backend/sandbox/Cargo.toml rename to core-backend/Cargo.toml index 4fa0abc0095..653b6412c6d 100644 --- a/core-backend/sandbox/Cargo.toml +++ b/core-backend/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gear-backend-sandbox" +name = "gear-core-backend" version = "0.1.0" authors.workspace = true edition.workspace = true @@ -8,7 +8,7 @@ license.workspace = true [dependencies] gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } -gear-backend-codegen.workspace = true +gear-core-backend-codegen.workspace = true gear-lazy-pages-common.workspace = true gsys = { workspace = true } diff --git a/core-backend/codegen/Cargo.toml b/core-backend/codegen/Cargo.toml index a6f54390cd4..8f7bb632556 100644 --- a/core-backend/codegen/Cargo.toml +++ b/core-backend/codegen/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gear-backend-codegen" +name = "gear-core-backend-codegen" description = "Code generation library for gear-core-backend" keywords = ["gear", "wasm", "codegen"] categories = ["wasm"] diff --git a/core-backend/sandbox/src/env.rs b/core-backend/src/env.rs similarity index 100% rename from core-backend/sandbox/src/env.rs rename to core-backend/src/env.rs diff --git a/core-backend/sandbox/src/funcs.rs b/core-backend/src/funcs.rs similarity index 99% rename from core-backend/sandbox/src/funcs.rs rename to core-backend/src/funcs.rs index adeb59becbe..d9787cd598a 100644 --- a/core-backend/sandbox/src/funcs.rs +++ b/core-backend/src/funcs.rs @@ -28,7 +28,6 @@ use crate::{ use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; use core::marker::PhantomData; -use gear_backend_codegen::host; use gear_core::{ buffer::{RuntimeBuffer, RuntimeBufferSizeError}, costs::RuntimeCosts, @@ -39,6 +38,7 @@ use gear_core::{ }, pages::{PageNumber, PageU32Size, WasmPage}, }; +use gear_core_backend_codegen::host; use gear_core_errors::{MessageError, ReplyCode, SignalCode}; use gear_sandbox_env::HostError; use gsys::{ diff --git a/core-backend/sandbox/src/lib.rs b/core-backend/src/lib.rs similarity index 100% rename from core-backend/sandbox/src/lib.rs rename to core-backend/src/lib.rs diff --git a/core-backend/sandbox/src/memory.rs b/core-backend/src/memory.rs similarity index 100% rename from core-backend/sandbox/src/memory.rs rename to core-backend/src/memory.rs diff --git a/core-backend/sandbox/src/mock.rs b/core-backend/src/mock.rs similarity index 100% rename from core-backend/sandbox/src/mock.rs rename to core-backend/src/mock.rs diff --git a/core-backend/sandbox/src/runtime.rs b/core-backend/src/runtime.rs similarity index 100% rename from core-backend/sandbox/src/runtime.rs rename to core-backend/src/runtime.rs diff --git a/core-backend/sandbox/src/state.rs b/core-backend/src/state.rs similarity index 100% rename from core-backend/sandbox/src/state.rs rename to core-backend/src/state.rs diff --git a/core-backend/sandbox/src/tests.rs b/core-backend/src/tests.rs similarity index 100% rename from core-backend/sandbox/src/tests.rs rename to core-backend/src/tests.rs diff --git a/core-processor/Cargo.toml b/core-processor/Cargo.toml index 6400be6a35e..aa54468fbbd 100644 --- a/core-processor/Cargo.toml +++ b/core-processor/Cargo.toml @@ -13,7 +13,7 @@ repository.workspace = true [dependencies] gear-core.workspace = true gear-core-errors = { workspace = true, features = ["codec"] } -gear-backend-sandbox.workspace = true +gear-core-backend.workspace = true gear-wasm-instrument.workspace = true gear-lazy-pages-common.workspace = true gear-lazy-pages-interface.workspace = true @@ -30,6 +30,6 @@ enum-iterator.workspace = true [features] default = ["std"] -std = ["gear-lazy-pages-interface/std", "gear-backend-sandbox/std"] +std = ["gear-lazy-pages-interface/std", "gear-core-backend/std"] strict = [] mock = [] diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index 8d8add01d5c..20e4530fbaf 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -28,9 +28,6 @@ use alloc::{ string::String, vec::Vec, }; -use gear_backend_sandbox::{ - env::SandboxSystemEnvironmentError, SystemTerminationReason, TrapExplanation, -}; use gear_core::{ gas::{GasAllowanceCounter, GasAmount, GasCounter}, ids::{CodeId, MessageId, ProgramId, ReservationId}, @@ -42,6 +39,9 @@ use gear_core::{ program::Program, reservation::{GasReservationMap, GasReserver}, }; +use gear_core_backend::{ + env::SandboxSystemEnvironmentError, SystemTerminationReason, TrapExplanation, +}; use gear_core_errors::{SignalCode, SimpleExecutionError}; use scale_info::scale::{self, Decode, Encode}; diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 160abf96904..bd10531769e 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -31,11 +31,6 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -use gear_backend_sandbox::{ - env::{BackendReport, SandboxEnvironmentError}, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - MemoryWrap, RunFallibleError, SandboxEnvironment, TerminationReason, -}; use gear_core::{ code::InstrumentedCode, env::Externalities, @@ -50,6 +45,11 @@ use gear_core::{ program::Program, reservation::GasReserver, }; +use gear_core_backend::{ + env::{BackendReport, SandboxEnvironmentError}, + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + MemoryWrap, RunFallibleError, SandboxEnvironment, TerminationReason, +}; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights}; use scale_info::{ scale::{self, Decode, Encode}, diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index a62d1057a7a..0b7945df7cb 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -24,11 +24,6 @@ use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, }; -use gear_backend_sandbox::{ - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, - UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, -}; #[cfg(any(feature = "mock", test))] use gear_core::message::{ContextSettings, IncomingDispatch}; use gear_core::{ @@ -49,6 +44,11 @@ use gear_core::{ pages::{GearPage, PageU32Size, WasmPage}, reservation::GasReserver, }; +use gear_core_backend::{ + ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, + RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, +}; use gear_core_errors::{ ExecutionError as FallibleExecutionError, ExtError as FallibleExtErrorCore, MessageError, ProgramRentError, ReplyCode, ReservationError, SignalCode, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index 3b655de5f7d..c5d276c446e 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -28,15 +28,15 @@ use crate::{ precharge::SuccessfulDispatchResultKind, }; use alloc::{string::ToString, vec::Vec}; -use gear_backend_sandbox::{ - BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, -}; use gear_core::{ env::Externalities, ids::{MessageId, ProgramId}, message::{ContextSettings, DispatchKind, IncomingDispatch, ReplyMessage, StoredDispatch}, reservation::GasReservationState, }; +use gear_core_backend::{ + BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, +}; use gear_core_errors::{ErrorReplyReason, SignalCode}; /// Process program & dispatch for it and return journal for updates. diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index dd555e0b60c..d93c159b4ef 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -30,7 +30,7 @@ gear-lazy-pages-interface.workspace = true core-processor.workspace = true gear-core.workspace = true gear-core-errors.workspace = true -gear-backend-sandbox.workspace = true +gear-core-backend.workspace = true pallet-gear-proc-macro = { path = "proc-macro" } gsys = { workspace = true, optional = true } pallet-gear-voucher.workspace = true @@ -136,7 +136,7 @@ std = [ "gear-wasm-instrument/std", "scopeguard/use_std", "core-processor/std", - "gear-backend-sandbox/std", + "gear-core-backend/std", "gear-lazy-pages-interface/std", "scale-info/std", "sp-io/std", @@ -171,7 +171,7 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "frame-support/runtime-benchmarks", "common/runtime-benchmarks", - "gear-backend-sandbox/mock", + "gear-core-backend/mock", "gear-core-errors/codec", "gear-sandbox", "sp-consensus-slots", diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 5fa5ab0fcdd..232811287cc 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -85,7 +85,6 @@ use frame_support::{ traits::{Currency, Get, Hooks}, }; use frame_system::{Pallet as SystemPallet, RawOrigin}; -use gear_backend_sandbox::{DefaultExecutorMemory, MemoryWrap}; use gear_core::{ code::{Code, CodeAndId}, gas::{GasAllowanceCounter, GasCounter, ValueCounter}, @@ -95,6 +94,7 @@ use gear_core::{ pages::{GearPage, PageU32Size, WasmPage, GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, reservation::GasReserver, }; +use gear_core_backend::{DefaultExecutorMemory, MemoryWrap}; use gear_core_errors::*; use gear_sandbox::{default_executor::Store, SandboxMemory, SandboxStore}; use gear_wasm_instrument::{ diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index bfd987b9135..47b57d1186b 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -63,15 +63,15 @@ use frame_support::{ traits::{Currency, Randomness}, }; use frame_system::pallet_prelude::BlockNumberFor; -use gear_backend_sandbox::{ - TrapExplanation, UnrecoverableExecutionError, UnrecoverableExtError, UnrecoverableWaitError, -}; use gear_core::{ code::{self, Code}, ids::{CodeId, MessageId, ProgramId}, message::UserStoredMessage, pages::{PageNumber, PageU32Size, WasmPage}, }; +use gear_core_backend::{ + TrapExplanation, UnrecoverableExecutionError, UnrecoverableExtError, UnrecoverableWaitError, +}; use gear_core_errors::*; use gear_wasm_instrument::STACK_END_EXPORT_NAME; use gstd::{collections::BTreeMap, errors::Error as GstdError}; diff --git a/utils/wasm-gen/Cargo.toml b/utils/wasm-gen/Cargo.toml index 210d21655fa..b26cabb8288 100644 --- a/utils/wasm-gen/Cargo.toml +++ b/utils/wasm-gen/Cargo.toml @@ -24,6 +24,6 @@ wat.workspace = true indicatif.workspace = true proptest.workspace = true -gear-backend-sandbox = { workspace = true, features = ["std"] } +gear-core-backend = { workspace = true, features = ["std"] } gear-core-processor = { workspace = true, features = ["std", "mock"] } gear-lazy-pages-interface = { workspace = true, features = ["std"] } diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 2dd1bf42477..df73996947e 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -18,7 +18,6 @@ use super::*; use arbitrary::Unstructured; -use gear_backend_sandbox::{env::BackendReport, TerminationReason, TrapExplanation}; use gear_core::{ code::Code, ids::{CodeId, ProgramId}, @@ -29,7 +28,7 @@ use gear_core::{ }, pages::WASM_PAGE_SIZE, }; -use gear_core_processor::{ProcessorContext, ProcessorExternalities}; +use gear_core_backend::{env::BackendReport, TerminationReason, TrapExplanation}; use gear_utils::NonEmpty; use gear_wasm_instrument::{ parity_wasm::{ @@ -173,7 +172,7 @@ fn injecting_addresses_works() { #[test] fn error_processing_works_for_fallible_syscalls() { - use gear_backend_sandbox::ActorTerminationReason; + use gear_core_backend::ActorTerminationReason; let fallible_syscalls = SysCallName::instrumentable() .into_iter() diff --git a/utils/wasm-instrument/Cargo.toml b/utils/wasm-instrument/Cargo.toml index a1a6729280b..c79f9f48507 100644 --- a/utils/wasm-instrument/Cargo.toml +++ b/utils/wasm-instrument/Cargo.toml @@ -17,12 +17,12 @@ enum-iterator.workspace = true [dev-dependencies] wasmparser.workspace = true wat.workspace = true -gear-backend-sandbox = { workspace = true, features = ["mock"] } +gear-core-backend = { workspace = true, features = ["mock"] } gear-core.workspace = true [features] default = ["std"] std = [ - "gear-backend-sandbox/std", + "gear-core-backend/std", "gwasm-instrument/std", ] diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 45cddb28287..b57f549e811 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -622,10 +622,10 @@ test_gas_counter_injection! { #[test] fn test_sys_calls_table() { use gas_metering::ConstantCostRules; - use gear_backend_sandbox::{ + use gear_core::message::DispatchKind; + use gear_core_backend::{ env::BackendReport, mock::MockExt, ActorTerminationReason, SandboxEnvironment, }; - use gear_core::message::DispatchKind; use parity_wasm::builder; // Make module with one empty function. From 45dd1b5c12f3d45846a2a0f43f3134d0724a15f4 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 10 Sep 2023 03:56:00 +0300 Subject: [PATCH 23/41] Remove `Sandbox` prefixes from types in backend --- core-backend/src/env.rs | 34 +++++++++++++++--------------- core-backend/src/lib.rs | 2 +- core-processor/src/common.rs | 6 ++---- core-processor/src/executor.rs | 20 +++++++++--------- utils/wasm-gen/src/tests.rs | 2 +- utils/wasm-instrument/src/tests.rs | 6 +++--- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/core-backend/src/env.rs b/core-backend/src/env.rs index 797eb9cb05d..194b8af84d7 100644 --- a/core-backend/src/env.rs +++ b/core-backend/src/env.rs @@ -164,30 +164,30 @@ fn store_host_state_mut( } pub type EnvironmentExecutionResult = - Result, SandboxEnvironmentError>; + Result, EnvironmentError>; #[derive(Debug, derive_more::Display)] -pub enum SandboxEnvironmentError { +pub enum EnvironmentError { #[display(fmt = "Actor backend error: {_1}")] Actor(GasAmount, String), #[display(fmt = "System backend error: {_0}")] - System(SandboxSystemEnvironmentError), + System(SystemEnvironmentError), #[display(fmt = "Prepare error: {_1}")] PrepareMemory(GasAmount, PrepareMemoryError), } -impl SandboxEnvironmentError { - pub fn from_infallible(err: SandboxEnvironmentError) -> Self { +impl EnvironmentError { + pub fn from_infallible(err: EnvironmentError) -> Self { match err { - SandboxEnvironmentError::System(err) => Self::System(err), - SandboxEnvironmentError::PrepareMemory(_, err) => match err {}, - SandboxEnvironmentError::Actor(gas_amount, s) => Self::Actor(gas_amount, s), + EnvironmentError::System(err) => Self::System(err), + EnvironmentError::PrepareMemory(_, err) => match err {}, + EnvironmentError::Actor(gas_amount, s) => Self::Actor(gas_amount, s), } } } #[derive(Debug, derive_more::Display)] -pub enum SandboxSystemEnvironmentError { +pub enum SystemEnvironmentError { #[display(fmt = "Failed to create env memory: {_0:?}")] CreateEnvMemory(gear_sandbox::Error), #[display(fmt = "Globals are not supported")] @@ -197,7 +197,7 @@ pub enum SandboxSystemEnvironmentError { } /// Environment to run one module at a time providing Ext. -pub struct SandboxEnvironment +pub struct Environment where Ext: BackendExternalities, EntryPoint: WasmEntryPoint, @@ -264,7 +264,7 @@ impl From> } } -impl SandboxEnvironment +impl Environment where Ext: BackendExternalities + 'static, Ext::UnrecoverableError: BackendSyscallError, @@ -359,7 +359,7 @@ impl GlobalsAccessor for GlobalsAccessProvider SandboxEnvironment +impl Environment where EnvExt: BackendExternalities + 'static, EnvExt::UnrecoverableError: BackendSyscallError, @@ -373,9 +373,9 @@ where entry_point: EntryPoint, entries: BTreeSet, mem_size: WasmPage, - ) -> Result> { - use SandboxEnvironmentError::*; - use SandboxSystemEnvironmentError::*; + ) -> Result> { + use EnvironmentError::*; + use SystemEnvironmentError::*; let entry_forbidden = entry_point .try_into_kind() @@ -451,8 +451,8 @@ where ) -> Result<(), PrepareMemoryError>, PrepareMemoryError: Display, { - use SandboxEnvironmentError::*; - use SandboxSystemEnvironmentError::*; + use EnvironmentError::*; + use SystemEnvironmentError::*; let Self { mut instance, diff --git a/core-backend/src/lib.rs b/core-backend/src/lib.rs index a25085bb67e..ec265f2674b 100644 --- a/core-backend/src/lib.rs +++ b/core-backend/src/lib.rs @@ -32,7 +32,7 @@ pub mod runtime; mod state; use codec::{Decode, Encode}; -pub use env::SandboxEnvironment; +pub use env::Environment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; use actor_system_error::actor_system_error; diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index 20e4530fbaf..1442d66ce6a 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -39,9 +39,7 @@ use gear_core::{ program::Program, reservation::{GasReservationMap, GasReserver}, }; -use gear_core_backend::{ - env::SandboxSystemEnvironmentError, SystemTerminationReason, TrapExplanation, -}; +use gear_core_backend::{env::SystemEnvironmentError, SystemTerminationReason, TrapExplanation}; use gear_core_errors::{SignalCode, SimpleExecutionError}; use scale_info::scale::{self, Decode, Encode}; @@ -495,7 +493,7 @@ pub enum SystemExecutionError { PrepareMemory(SystemPrepareMemoryError), /// Environment error #[display(fmt = "Backend error: {_0}")] - Environment(SandboxSystemEnvironmentError), + Environment(SystemEnvironmentError), /// Termination reason #[from] #[display(fmt = "Syscall function error: {_0}")] diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index bd10531769e..b452a9de137 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -46,9 +46,9 @@ use gear_core::{ reservation::GasReserver, }; use gear_core_backend::{ - env::{BackendReport, SandboxEnvironmentError}, + env::{BackendReport, EnvironmentError}, ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - MemoryWrap, RunFallibleError, SandboxEnvironment, TerminationReason, + Environment, MemoryWrap, RunFallibleError, TerminationReason, }; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights}; use scale_info::{ @@ -225,14 +225,14 @@ where // Execute program in backend env. let execute = || { - let env = SandboxEnvironment::new( + let env = Environment::new( ext, program.code_bytes(), kind, program.code().exports().clone(), memory_size, ) - .map_err(SandboxEnvironmentError::from_infallible)?; + .map_err(EnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { prepare_memory::>( memory, @@ -268,19 +268,19 @@ where (termination, memory, ext) } - Err(SandboxEnvironmentError::System(e)) => { + Err(EnvironmentError::System(e)) => { return Err(ExecutionError::System(SystemExecutionError::Environment(e))) } - Err(SandboxEnvironmentError::PrepareMemory(gas_amount, PrepareMemoryError::Actor(e))) => { + Err(EnvironmentError::PrepareMemory(gas_amount, PrepareMemoryError::Actor(e))) => { return Err(ExecutionError::Actor(ActorExecutionError { gas_amount, reason: ActorExecutionErrorReplyReason::PrepareMemory(e), })) } - Err(SandboxEnvironmentError::PrepareMemory(_gas_amount, PrepareMemoryError::System(e))) => { + Err(EnvironmentError::PrepareMemory(_gas_amount, PrepareMemoryError::System(e))) => { return Err(ExecutionError::System(e.into())); } - Err(SandboxEnvironmentError::Actor(gas_amount, err)) => { + Err(EnvironmentError::Actor(gas_amount, err)) => { log::trace!("ActorExecutionErrorReplyReason::Environment({err}) occurred"); return Err(ExecutionError::Actor(ActorExecutionError { gas_amount, @@ -420,14 +420,14 @@ where // Execute program in backend env. let f = || { - let env = SandboxEnvironment::new( + let env = Environment::new( ext, program.code_bytes(), function, program.code().exports().clone(), memory_size, ) - .map_err(SandboxEnvironmentError::from_infallible)?; + .map_err(EnvironmentError::from_infallible)?; env.execute(|memory, stack_end, globals_config| { prepare_memory::>( memory, diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index df73996947e..0b719e94d54 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -312,7 +312,7 @@ fn execute_wasm_with_syscall_injected( }; let ext = gear_core_processor::Ext::new(processor_context); - let env = SandboxEnvironment::new( + let env = Environment::new( ext, code.code(), DispatchKind::Init, diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index b57f549e811..e5e0eab29d8 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -624,7 +624,7 @@ fn test_sys_calls_table() { use gas_metering::ConstantCostRules; use gear_core::message::DispatchKind; use gear_core_backend::{ - env::BackendReport, mock::MockExt, ActorTerminationReason, SandboxEnvironment, + env::BackendReport, mock::MockExt, ActorTerminationReason, Environment, }; use parity_wasm::builder; @@ -658,8 +658,8 @@ fn test_sys_calls_table() { // Execute wasm and check success. let ext = MockExt::default(); - let env = SandboxEnvironment::new(ext, &code, DispatchKind::Init, Default::default(), 0.into()) - .unwrap(); + let env = + Environment::new(ext, &code, DispatchKind::Init, Default::default(), 0.into()).unwrap(); let report = env .execute(|_, _, _| -> Result<(), u32> { Ok(()) }) .unwrap(); From 11e238acbac54604931eccadde8c481c4dd293a6 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 11 Sep 2023 15:38:12 +0300 Subject: [PATCH 24/41] Fix benches and tests --- Cargo.lock | 2 + pallets/gear/Cargo.toml | 1 + pallets/gear/src/benchmarking/mod.rs | 22 ++++----- .../gear/src/benchmarking/tests/lazy_pages.rs | 48 +++++++------------ runtime/gear/Cargo.toml | 1 + runtime/gear/src/tests.rs | 2 +- 6 files changed, 31 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0107be59a8c..1f3f7309d45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4116,6 +4116,7 @@ dependencies = [ "frame-try-runtime", "gear-common", "gear-core-processor", + "gear-lazy-pages-common", "gear-runtime-common", "gear-runtime-primitives", "hex-literal", @@ -7358,6 +7359,7 @@ dependencies = [ "gear-core-backend", "gear-core-errors", "gear-core-processor", + "gear-lazy-pages-common", "gear-lazy-pages-interface", "gear-runtime-interface", "gear-sandbox", diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index d93c159b4ef..6b412909f43 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -27,6 +27,7 @@ static_assertions.workspace = true common.workspace = true gear-runtime-interface = { workspace = true } gear-lazy-pages-interface.workspace = true +gear-lazy-pages-common.workspace = true core-processor.workspace = true gear-core.workspace = true gear-core-errors.workspace = true diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 232811287cc..9c7a6cb02d6 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -58,9 +58,9 @@ use crate::{ manager::ExtManager, pallet, schedule::{API_BENCHMARK_BATCH_SIZE, INSTR_BENCHMARK_BATCH_SIZE}, - BalanceOf, BenchmarkStorage, Call, Config, CurrencyOf, Event, ExecutionEnvironment, - Ext as Externalities, GasHandlerOf, GearBank, MailboxOf, Pallet as Gear, Pallet, - ProgramStorageOf, QueueOf, RentFreePeriodOf, ResumeMinimalPeriodOf, Schedule, TaskPoolOf, + BalanceOf, BenchmarkStorage, Call, Config, CurrencyOf, Event, Ext as Externalities, + GasHandlerOf, GearBank, MailboxOf, Pallet as Gear, Pallet, ProgramStorageOf, QueueOf, + RentFreePeriodOf, ResumeMinimalPeriodOf, Schedule, TaskPoolOf, }; use ::alloc::{ collections::{BTreeMap, BTreeSet}, @@ -77,7 +77,7 @@ use common::{ use core_processor::{ common::{DispatchOutcome, JournalNote}, configs::{BlockConfig, PageCosts, TESTS_MAX_PAGES_NUMBER}, - ProcessExecutionContext, ProcessorContext, ProcessorExternalities, + Ext, ProcessExecutionContext, ProcessorContext, ProcessorExternalities, }; use frame_benchmarking::{benchmarks, whitelisted_caller}; use frame_support::{ @@ -94,7 +94,7 @@ use gear_core::{ pages::{GearPage, PageU32Size, WasmPage, GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, reservation::GasReserver, }; -use gear_core_backend::{DefaultExecutorMemory, MemoryWrap}; +use gear_core_backend::{DefaultExecutorMemory, Environment, MemoryWrap}; use gear_core_errors::*; use gear_sandbox::{default_executor::Store, SandboxMemory, SandboxStore}; use gear_wasm_instrument::{ @@ -233,12 +233,8 @@ where T: Config, T::AccountId: Origin, { - core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)) + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)) } fn get_last_session_id() -> Option { @@ -459,7 +455,7 @@ benchmarks! { let WasmModule { code, .. } = WasmModule::::sized(c * 1024, Location::Init); }: { let ext = Externalities::new(default_processor_context::()); - ExecutionEnvironment::new(ext, &code, DispatchKind::Init, Default::default(), max_pages::().into()).unwrap(); + Environment::new(ext, &code, DispatchKind::Init, Default::default(), max_pages::().into()).unwrap(); } claim_value { @@ -1644,7 +1640,7 @@ benchmarks! { let r in 0 .. API_BENCHMARK_BATCHES; let mut store = Store::new(None); let mem = DefaultExecutorMemory::new(&mut store, 1, None).unwrap(); - let mut mem = MemoryWrap::::new(mem, store); + let mut mem = MemoryWrap::::new(mem, store); }: { for _ in 0..(r * API_BENCHMARK_BATCH_SIZE) { mem.grow(1.into()).unwrap(); diff --git a/pallets/gear/src/benchmarking/tests/lazy_pages.rs b/pallets/gear/src/benchmarking/tests/lazy_pages.rs index ec76d450d48..ea10a290533 100644 --- a/pallets/gear/src/benchmarking/tests/lazy_pages.rs +++ b/pallets/gear/src/benchmarking/tests/lazy_pages.rs @@ -22,12 +22,13 @@ use core::mem::size_of; use ::alloc::{collections::BTreeSet, format}; use common::ProgramStorage; +use core_processor::Ext; use frame_support::codec::MaxEncodedLen; -use gear_backend_common::lazy_pages::Status; use gear_core::{ memory::MemoryInterval, pages::{PageNumber, PageU32Size}, }; +use gear_lazy_pages_common::Status; use gear_lazy_pages_interface as lazy_pages; use rand::{Rng, SeedableRng}; @@ -303,12 +304,9 @@ where let charged_for_pages = page_sets.charged_for_pages(&exec.block_config.page_costs); - let notes = core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); + let notes = + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); let mut gas_burned = 0; for note in notes.into_iter() { @@ -379,12 +377,9 @@ where .page_costs .lazy_pages_signal_write_after_read = (write_after_read_cost * i).into(); - let notes = core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); + let notes = + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); let mut gas_burned = 0; for note in notes.into_iter() { @@ -538,12 +533,9 @@ where exec.block_config.page_costs = Default::default(); - let notes = core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); + let notes = + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); let mut gas_burned = None; for note in notes.into_iter() { @@ -581,12 +573,9 @@ where ..Default::default() }; - let notes = core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); + let notes = + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); for note in notes.into_iter() { match note { @@ -622,12 +611,9 @@ where ..Default::default() }; - let notes = core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); + let notes = + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)); for note in notes.into_iter() { match note { diff --git a/runtime/gear/Cargo.toml b/runtime/gear/Cargo.toml index 41ccbe0c5aa..21393bb45c1 100644 --- a/runtime/gear/Cargo.toml +++ b/runtime/gear/Cargo.toml @@ -79,6 +79,7 @@ substrate-wasm-builder = { workspace = true, optional = true } [dev-dependencies] gear-core-processor.workspace = true +gear-lazy-pages-common.workspace = true [features] default = ["std"] diff --git a/runtime/gear/src/tests.rs b/runtime/gear/src/tests.rs index 95afd48443d..fdac76f2217 100644 --- a/runtime/gear/src/tests.rs +++ b/runtime/gear/src/tests.rs @@ -18,8 +18,8 @@ use super::*; use crate::Runtime; -use gear_backend_common::lazy_pages::LazyPagesWeights; use gear_core_processor::configs::PageCosts; +use gear_lazy_pages_common::LazyPagesWeights; use pallet_gear::{InstructionWeights, MemoryWeights}; use runtime_common::weights::{check_instructions_weights, check_pages_weights}; From ea0220b670fcb32d619fe2f7304ec41163317c4d Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 11 Sep 2023 17:37:48 +0300 Subject: [PATCH 25/41] Fix Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1f3f7309d45..cec02376821 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3921,7 +3921,7 @@ version = "0.3.2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] From c140b7bcd03c9e6c2527f9cda666d4573d8ec389 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 18 Sep 2023 21:52:43 +0300 Subject: [PATCH 26/41] Fix after-rebase mess --- Cargo.lock | 2 +- core-processor/src/ext.rs | 2 -- pallets/gear/src/benchmarking/tests/mod.rs | 35 ++++++++++------------ utils/wasm-gen/src/tests.rs | 3 +- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cec02376821..d719df3ab56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3917,7 +3917,7 @@ dependencies = [ [[package]] name = "gear-core-backend-codegen" -version = "0.3.2" +version = "1.0.0" dependencies = [ "proc-macro2", "quote", diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 0b7945df7cb..9f496ad84e4 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -24,8 +24,6 @@ use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, }; -#[cfg(any(feature = "mock", test))] -use gear_core::message::{ContextSettings, IncomingDispatch}; use gear_core::{ costs::{HostFnWeights, RuntimeCosts}, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, diff --git a/pallets/gear/src/benchmarking/tests/mod.rs b/pallets/gear/src/benchmarking/tests/mod.rs index e07981baff9..286808fde86 100644 --- a/pallets/gear/src/benchmarking/tests/mod.rs +++ b/pallets/gear/src/benchmarking/tests/mod.rs @@ -36,8 +36,7 @@ use crate::{ HandleKind, }; use common::benchmarking; -use gear_backend_common::TrapExplanation; - +use gear_core_backend::TrapExplanation; use gear_wasm_instrument::parity_wasm::elements::Instruction; pub fn check_stack_overflow() @@ -71,22 +70,18 @@ where ) .unwrap(); - core_processor::process::( - &exec.block_config, - exec.context, - exec.random_data, - ) - .unwrap() - .into_iter() - .find_map(|note| match note { - JournalNote::MessageDispatched { outcome, .. } => Some(outcome), - _ => None, - }) - .map(|outcome| match outcome { - DispatchOutcome::InitFailure { reason, .. } => { - assert_eq!(reason, TrapExplanation::Unknown.to_string()); - } - _ => panic!("Unexpected dispatch outcome: {:?}", outcome), - }) - .unwrap(); + core_processor::process::(&exec.block_config, exec.context, exec.random_data) + .unwrap() + .into_iter() + .find_map(|note| match note { + JournalNote::MessageDispatched { outcome, .. } => Some(outcome), + _ => None, + }) + .map(|outcome| match outcome { + DispatchOutcome::InitFailure { reason, .. } => { + assert_eq!(reason, TrapExplanation::Unknown.to_string()); + } + _ => panic!("Unexpected dispatch outcome: {:?}", outcome), + }) + .unwrap(); } diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 0b719e94d54..d38078ceb56 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -28,7 +28,8 @@ use gear_core::{ }, pages::WASM_PAGE_SIZE, }; -use gear_core_backend::{env::BackendReport, TerminationReason, TrapExplanation}; +use gear_core_backend::{env::BackendReport, Environment, TerminationReason, TrapExplanation}; +use gear_core_processor::{ProcessorContext, ProcessorExternalities}; use gear_utils::NonEmpty; use gear_wasm_instrument::{ parity_wasm::{ From f430afb8057e048cf0aa6f1be10d39cc5c094f48 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 18 Sep 2023 21:57:58 +0300 Subject: [PATCH 27/41] Fix `make doc` --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0e6ee45d5ed..7062514b456 100644 --- a/Makefile +++ b/Makefile @@ -249,7 +249,7 @@ test-syscalls-integrity-release: .PHONY: doc doc: @ RUSTDOCFLAGS="--enable-index-page --generate-link-to-definition -Zunstable-options -D warnings" cargo doc --no-deps \ - -p galloc -p gclient -p gcore -p gear-backend-common -p gear-backend-sandbox \ + -p galloc -p gclient -p gcore -p gear-core-backend \ -p gear-core -p gear-core-processor -p gear-lazy-pages -p gear-core-errors \ -p gmeta -p gstd -p gtest -p gear-wasm-builder -p gear-common \ -p pallet-gear -p pallet-gear-gas -p pallet-gear-messenger -p pallet-gear-payment \ From 04a447b94215b80db29de9d57e5fb702eade173b Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 18 Sep 2023 22:03:34 +0300 Subject: [PATCH 28/41] Fix docs --- core-backend/src/lib.rs | 2 +- core-processor/src/ext.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core-backend/src/lib.rs b/core-backend/src/lib.rs index ec265f2674b..16b65698391 100644 --- a/core-backend/src/lib.rs +++ b/core-backend/src/lib.rs @@ -262,7 +262,7 @@ pub trait BackendExternalities: Externalities + CountersOwner { ) -> Result<(), ProcessAccessError>; } -/// Error returned from closure argument in [`Runtime::run_fallible`]. +/// Error returned from closure argument in [`runtime::CallerWrap::run_fallible`]. #[derive(Debug, Clone)] pub enum RunFallibleError { UndefinedTerminationReason(UndefinedTerminationReason), diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 9f496ad84e4..0e54d2c79c3 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -173,7 +173,7 @@ pub trait ProcessorExternalities { /// Create new fn new(context: ProcessorContext) -> Self; - /// Convert externalities into [`ExtInfo`]. + /// Convert externalities into info. fn into_ext_info(self, memory: &impl Memory) -> Result; /// Protect and save storage keys for pages which has no data From c5df03c93c9d006085488698f65d7fd3795ac802 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Wed, 20 Sep 2023 16:40:48 +0300 Subject: [PATCH 29/41] Update Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 0eb0de000bb..7a34d0f919f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3918,7 +3918,7 @@ dependencies = [ [[package]] name = "gear-core-backend-codegen" -version = "1.0.0" +version = "1.0.1" dependencies = [ "proc-macro2", "quote", From 616e7803be328d763afb99518a3354da4ad24b35 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 22:33:21 +0300 Subject: [PATCH 30/41] Import `gear-lazy-pages-common` without default features --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 11006ad1ead..26970665625 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -205,7 +205,7 @@ gear-core = { path = "core" } gear-core-errors = { path = "core-errors" } gear-core-processor = { path = "core-processor", default-features = false } gear-lazy-pages = { path = "lazy-pages" } -gear-lazy-pages-common = { path = "lazy-pages/common" } +gear-lazy-pages-common = { path = "lazy-pages/common", default-features = false } gear-lazy-pages-interface = { path = "lazy-pages/interface", default-features = false } gear-node-testing = { path = "node/testing" } gear-runtime = { path = "runtime/gear" } From d3d7d4feba3270e6eb1d83e685695064fcfe8c8d Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 22:35:57 +0300 Subject: [PATCH 31/41] Derive crate fields from workspace --- Cargo.lock | 6 +++--- core-backend/Cargo.toml | 5 ++++- lazy-pages/Cargo.toml | 2 +- lazy-pages/common/Cargo.toml | 11 +++++++---- lazy-pages/interface/Cargo.toml | 5 ++++- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4d5deded4c..2b1bde6d3bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3922,7 +3922,7 @@ dependencies = [ [[package]] name = "gear-core-backend" -version = "0.1.0" +version = "1.0.1" dependencies = [ "actor-system-error", "blake2-rfc", @@ -4011,7 +4011,7 @@ dependencies = [ [[package]] name = "gear-lazy-pages-common" -version = "0.1.0" +version = "1.0.1" dependencies = [ "gear-core", "num_enum", @@ -4020,7 +4020,7 @@ dependencies = [ [[package]] name = "gear-lazy-pages-interface" -version = "0.1.0" +version = "1.0.1" dependencies = [ "byteorder", "derive_more", diff --git a/core-backend/Cargo.toml b/core-backend/Cargo.toml index 653b6412c6d..0a1de42cad0 100644 --- a/core-backend/Cargo.toml +++ b/core-backend/Cargo.toml @@ -1,9 +1,12 @@ [package] name = "gear-core-backend" -version = "0.1.0" +description = "Gear WASM backend" +version.workspace = true authors.workspace = true edition.workspace = true license.workspace = true +homepage.workspace = true +repository.workspace = true [dependencies] gear-core.workspace = true diff --git a/lazy-pages/Cargo.toml b/lazy-pages/Cargo.toml index bd7f129ec56..3af1ee4ed69 100644 --- a/lazy-pages/Cargo.toml +++ b/lazy-pages/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gear-lazy-pages" -description = "Gear lazy-pages support" +description = "Gear lazy-pages implementation" version.workspace = true authors.workspace = true edition.workspace = true diff --git a/lazy-pages/common/Cargo.toml b/lazy-pages/common/Cargo.toml index 3434d3291fb..d449c6c6a24 100644 --- a/lazy-pages/common/Cargo.toml +++ b/lazy-pages/common/Cargo.toml @@ -1,9 +1,12 @@ [package] name = "gear-lazy-pages-common" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +description = "Gear lazy-pages commons" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true [dependencies] gear-core.workspace = true diff --git a/lazy-pages/interface/Cargo.toml b/lazy-pages/interface/Cargo.toml index f04cccf51e7..9aca80bf669 100644 --- a/lazy-pages/interface/Cargo.toml +++ b/lazy-pages/interface/Cargo.toml @@ -1,9 +1,12 @@ [package] name = "gear-lazy-pages-interface" -version = "0.1.0" +description = "Gear lazy-pages actual interface" +version.workspace = true authors.workspace = true edition.workspace = true license.workspace = true +homepage.workspace = true +repository.workspace = true [dependencies] derive_more.workspace = true From bf214ca98c092d9b24abc12208533e18dbe8d76d Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 22:44:01 +0300 Subject: [PATCH 32/41] Remove unused `CallerWrap::unreachable_error` --- core-backend/src/runtime.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core-backend/src/runtime.rs b/core-backend/src/runtime.rs index 446bb4352f4..272ece18dbb 100644 --- a/core-backend/src/runtime.rs +++ b/core-backend/src/runtime.rs @@ -70,10 +70,6 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { &mut self.host_state_mut().ext } - pub fn unreachable_error() -> HostError { - HostError - } - #[track_caller] pub fn run_any( &mut self, From c4dcf14d321dbc60ace8cb03fb400d9fcff89e39 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 22:45:49 +0300 Subject: [PATCH 33/41] Make some mods private --- core-backend/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-backend/src/lib.rs b/core-backend/src/lib.rs index 16b65698391..3da5197214c 100644 --- a/core-backend/src/lib.rs +++ b/core-backend/src/lib.rs @@ -25,17 +25,17 @@ extern crate core; pub mod env; mod funcs; -pub mod memory; +mod memory; #[cfg(any(feature = "mock", test))] pub mod mock; -pub mod runtime; +mod runtime; mod state; -use codec::{Decode, Encode}; pub use env::Environment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; use actor_system_error::actor_system_error; +use codec::{Decode, Encode}; use gear_core::{ env::Externalities, gas::{ChargeError, CounterType, CountersOwner, GasAmount}, From 2df2a940a93550f1c4f4c1341179007ca69552d8 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 23:12:32 +0300 Subject: [PATCH 34/41] Move errors into their own module --- core-backend/src/env.rs | 7 +- core-backend/src/error.rs | 264 +++++++++++++++++++++++++++++ core-backend/src/funcs.rs | 9 +- core-backend/src/lib.rs | 247 +-------------------------- core-backend/src/memory.rs | 10 +- core-backend/src/mock.rs | 6 +- core-backend/src/runtime.rs | 4 +- core-backend/src/state.rs | 6 +- core-processor/src/common.rs | 5 +- core-processor/src/executor.rs | 7 +- core-processor/src/ext.rs | 9 +- core-processor/src/processing.rs | 3 +- pallets/gear/src/tests.rs | 2 +- utils/wasm-gen/src/tests.rs | 8 +- utils/wasm-instrument/src/tests.rs | 2 +- 15 files changed, 319 insertions(+), 270 deletions(-) create mode 100644 core-backend/src/error.rs diff --git a/core-backend/src/env.rs b/core-backend/src/env.rs index 194b8af84d7..625eec6e8b0 100644 --- a/core-backend/src/env.rs +++ b/core-backend/src/env.rs @@ -19,13 +19,16 @@ //! sp-sandbox environment for running a module. use crate::{ + error::{ + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, RunFallibleError, + TerminationReason, + }, funcs::FuncsHandler, memory::MemoryWrap, runtime, runtime::CallerWrap, state::{HostState, State}, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - RunFallibleError, TerminationReason, + BackendExternalities, }; use alloc::{collections::BTreeSet, format, string::String}; use core::{ diff --git a/core-backend/src/error.rs b/core-backend/src/error.rs new file mode 100644 index 00000000000..5b5c82a9b63 --- /dev/null +++ b/core-backend/src/error.rs @@ -0,0 +1,264 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use actor_system_error::actor_system_error; +use codec::{Decode, Encode}; +use gear_core::{ + gas::{ChargeError, CounterType}, + ids::ProgramId, + message::MessageWaitedType, + str::LimitedStr, +}; +use gear_core_errors::ExtError as FallibleExtError; + +actor_system_error! { + pub type TerminationReason = ActorSystemError; +} + +#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)] +pub enum UndefinedTerminationReason { + Actor(ActorTerminationReason), + System(SystemTerminationReason), + /// Undefined reason because we need access to counters owner trait for RI. + ProcessAccessErrorResourcesExceed, +} + +impl UndefinedTerminationReason { + pub fn define(self, current_counter: CounterType) -> TerminationReason { + match self { + Self::Actor(r) => r.into(), + Self::System(r) => r.into(), + Self::ProcessAccessErrorResourcesExceed => { + ActorTerminationReason::from(current_counter).into() + } + } + } +} + +impl From for UndefinedTerminationReason { + fn from(err: ChargeError) -> Self { + match err { + ChargeError::GasLimitExceeded => { + ActorTerminationReason::Trap(TrapExplanation::GasLimitExceeded).into() + } + ChargeError::GasAllowanceExceeded => { + ActorTerminationReason::GasAllowanceExceeded.into() + } + } + } +} + +impl From for UndefinedTerminationReason { + fn from(trap: TrapExplanation) -> Self { + ActorTerminationReason::Trap(trap).into() + } +} + +impl From for UndefinedTerminationReason { + fn from(err: E) -> Self { + err.into_termination_reason() + } +} + +#[derive(Decode, Encode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, derive_more::From)] +#[codec(crate = codec)] +pub enum ActorTerminationReason { + Exit(ProgramId), + Leave, + Success, + Wait(Option, MessageWaitedType), + GasAllowanceExceeded, + #[from] + Trap(TrapExplanation), +} + +impl From for ActorTerminationReason { + fn from(counter_type: CounterType) -> Self { + match counter_type { + CounterType::GasLimit => Self::Trap(TrapExplanation::GasLimitExceeded), + CounterType::GasAllowance => Self::GasAllowanceExceeded, + } + } +} + +/// Non-actor related termination reason. +/// +/// ### NOTICE: +/// It's currently unused, but is left as a stub, until +/// further massive errors refactoring is done. +#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)] +pub struct SystemTerminationReason; + +/// Execution error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableExecutionError { + #[display(fmt = "Invalid debug string passed in `gr_debug` sys-call")] + InvalidDebugString, + #[display(fmt = "Not enough gas for operation")] + NotEnoughGas, + #[display(fmt = "Length is overflowed to read payload")] + TooBigReadLen, + #[display(fmt = "Cannot take data in payload range from message with size")] + ReadWrongRange, +} + +/// Memory error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableMemoryError { + /// The error occurs in attempt to access memory outside wasm program memory. + #[display(fmt = "Trying to access memory outside wasm program memory")] + AccessOutOfBounds, + /// The error occurs, when program tries to allocate in block-chain runtime more memory than allowed. + #[display(fmt = "Trying to allocate more memory in block-chain runtime than allowed")] + RuntimeAllocOutOfBounds, +} + +/// Wait error in infallible sys-call. +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableWaitError { + /// An error occurs in attempt to wait for or wait up to zero blocks. + #[display(fmt = "Waiting duration cannot be zero")] + ZeroDuration, + /// An error occurs in attempt to wait after reply sent. + #[display(fmt = "`wait()` is not allowed after reply sent")] + WaitAfterReply, +} + +#[derive( + Decode, + Encode, + Debug, + Clone, + Eq, + PartialEq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum UnrecoverableExtError { + #[display(fmt = "Execution error: {_0}")] + Execution(UnrecoverableExecutionError), + #[display(fmt = "Memory error: {_0}")] + Memory(UnrecoverableMemoryError), + #[display(fmt = "Waiting error: {_0}")] + Wait(UnrecoverableWaitError), +} + +#[derive( + Decode, + Encode, + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + derive_more::Display, + derive_more::From, +)] +#[codec(crate = codec)] +pub enum TrapExplanation { + /// An error occurs in attempt to charge more gas than available during execution. + #[display(fmt = "Not enough gas to continue execution")] + GasLimitExceeded, + /// An error occurs in attempt to call forbidden sys-call. + #[display(fmt = "Unable to call a forbidden function")] + ForbiddenFunction, + /// The error occurs when a program tries to allocate more memory than + /// allowed. + #[display(fmt = "Trying to allocate more wasm program memory than allowed")] + ProgramAllocOutOfBounds, + #[display(fmt = "Sys-call unrecoverable error: {_0}")] + UnrecoverableExt(UnrecoverableExtError), + #[display(fmt = "{_0}")] + Panic(LimitedStr<'static>), + #[display(fmt = "Reason is unknown. Possibly `unreachable` instruction is occurred")] + Unknown, +} + +/// Error returned from closure argument in [`runtime::CallerWrap::run_fallible`]. +#[derive(Debug, Clone)] +pub enum RunFallibleError { + UndefinedTerminationReason(UndefinedTerminationReason), + FallibleExt(FallibleExtError), +} + +impl From for RunFallibleError +where + E: BackendSyscallError, +{ + fn from(err: E) -> Self { + err.into_run_fallible_error() + } +} + +/// A trait for conversion of the externalities API error +/// to `UndefinedTerminationReason` and `RunFallibleError`. +pub trait BackendSyscallError: Sized { + fn into_termination_reason(self) -> UndefinedTerminationReason; + + fn into_run_fallible_error(self) -> RunFallibleError; +} + +// TODO: consider to remove this trait and use Result, GasError> instead #2571 +/// A trait for conversion of the externalities memory management error to api error. +/// +/// If the conversion fails, then `Self` is returned in the `Err` variant. +pub trait BackendAllocSyscallError: Sized { + type ExtError: BackendSyscallError; + + fn into_backend_error(self) -> Result; +} diff --git a/core-backend/src/funcs.rs b/core-backend/src/funcs.rs index 9c5674070c3..22e2ce2f139 100644 --- a/core-backend/src/funcs.rs +++ b/core-backend/src/funcs.rs @@ -19,11 +19,14 @@ //! Syscall implementations generic over wasmi and sandbox backends. use crate::{ + error::{ + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, RunFallibleError, + TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableMemoryError, + }, memory::{MemoryAccessError, WasmMemoryRead}, runtime::CallerWrap, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, - UnrecoverableMemoryError, + BackendExternalities, }; use alloc::string::{String, ToString}; use blake2_rfc::blake2b::blake2b; diff --git a/core-backend/src/lib.rs b/core-backend/src/lib.rs index 3da5197214c..d412eb274de 100644 --- a/core-backend/src/lib.rs +++ b/core-backend/src/lib.rs @@ -21,9 +21,9 @@ #![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; -extern crate core; pub mod env; +pub mod error; mod funcs; mod memory; #[cfg(any(feature = "mock", test))] @@ -34,222 +34,13 @@ mod state; pub use env::Environment; pub use memory::{DefaultExecutorMemory, MemoryWrap}; -use actor_system_error::actor_system_error; -use codec::{Decode, Encode}; use gear_core::{ env::Externalities, - gas::{ChargeError, CounterType, CountersOwner, GasAmount}, - ids::ProgramId, + gas::{CountersOwner, GasAmount}, memory::MemoryInterval, - message::MessageWaitedType, - str::LimitedStr, }; -use gear_core_errors::ExtError as FallibleExtError; use gear_lazy_pages_common::ProcessAccessError; -actor_system_error! { - pub type TerminationReason = ActorSystemError; -} - -#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)] -pub enum UndefinedTerminationReason { - Actor(ActorTerminationReason), - System(SystemTerminationReason), - /// Undefined reason because we need access to counters owner trait for RI. - ProcessAccessErrorResourcesExceed, -} - -impl UndefinedTerminationReason { - pub fn define(self, current_counter: CounterType) -> TerminationReason { - match self { - Self::Actor(r) => r.into(), - Self::System(r) => r.into(), - Self::ProcessAccessErrorResourcesExceed => { - ActorTerminationReason::from(current_counter).into() - } - } - } -} - -impl From for UndefinedTerminationReason { - fn from(err: ChargeError) -> Self { - match err { - ChargeError::GasLimitExceeded => { - ActorTerminationReason::Trap(TrapExplanation::GasLimitExceeded).into() - } - ChargeError::GasAllowanceExceeded => { - ActorTerminationReason::GasAllowanceExceeded.into() - } - } - } -} - -impl From for UndefinedTerminationReason { - fn from(trap: TrapExplanation) -> Self { - ActorTerminationReason::Trap(trap).into() - } -} - -impl From for UndefinedTerminationReason { - fn from(err: E) -> Self { - err.into_termination_reason() - } -} - -#[derive(Decode, Encode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, derive_more::From)] -#[codec(crate = codec)] -pub enum ActorTerminationReason { - Exit(ProgramId), - Leave, - Success, - Wait(Option, MessageWaitedType), - GasAllowanceExceeded, - #[from] - Trap(TrapExplanation), -} - -impl From for ActorTerminationReason { - fn from(counter_type: CounterType) -> Self { - match counter_type { - CounterType::GasLimit => Self::Trap(TrapExplanation::GasLimitExceeded), - CounterType::GasAllowance => Self::GasAllowanceExceeded, - } - } -} - -/// Non-actor related termination reason. -/// -/// ### NOTICE: -/// It's currently unused, but is left as a stub, until -/// further massive errors refactoring is done. -#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)] -pub struct SystemTerminationReason; - -/// Execution error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = codec)] -pub enum UnrecoverableExecutionError { - #[display(fmt = "Invalid debug string passed in `gr_debug` sys-call")] - InvalidDebugString, - #[display(fmt = "Not enough gas for operation")] - NotEnoughGas, - #[display(fmt = "Length is overflowed to read payload")] - TooBigReadLen, - #[display(fmt = "Cannot take data in payload range from message with size")] - ReadWrongRange, -} - -/// Memory error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = codec)] -pub enum UnrecoverableMemoryError { - /// The error occurs in attempt to access memory outside wasm program memory. - #[display(fmt = "Trying to access memory outside wasm program memory")] - AccessOutOfBounds, - /// The error occurs, when program tries to allocate in block-chain runtime more memory than allowed. - #[display(fmt = "Trying to allocate more memory in block-chain runtime than allowed")] - RuntimeAllocOutOfBounds, -} - -/// Wait error in infallible sys-call. -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = codec)] -pub enum UnrecoverableWaitError { - /// An error occurs in attempt to wait for or wait up to zero blocks. - #[display(fmt = "Waiting duration cannot be zero")] - ZeroDuration, - /// An error occurs in attempt to wait after reply sent. - #[display(fmt = "`wait()` is not allowed after reply sent")] - WaitAfterReply, -} - -#[derive( - Decode, - Encode, - Debug, - Clone, - Eq, - PartialEq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = codec)] -pub enum UnrecoverableExtError { - #[display(fmt = "Execution error: {_0}")] - Execution(UnrecoverableExecutionError), - #[display(fmt = "Memory error: {_0}")] - Memory(UnrecoverableMemoryError), - #[display(fmt = "Waiting error: {_0}")] - Wait(UnrecoverableWaitError), -} - -#[derive( - Decode, - Encode, - Debug, - Clone, - PartialEq, - Eq, - PartialOrd, - Ord, - derive_more::Display, - derive_more::From, -)] -#[codec(crate = codec)] -pub enum TrapExplanation { - /// An error occurs in attempt to charge more gas than available during execution. - #[display(fmt = "Not enough gas to continue execution")] - GasLimitExceeded, - /// An error occurs in attempt to call forbidden sys-call. - #[display(fmt = "Unable to call a forbidden function")] - ForbiddenFunction, - /// The error occurs when a program tries to allocate more memory than - /// allowed. - #[display(fmt = "Trying to allocate more wasm program memory than allowed")] - ProgramAllocOutOfBounds, - #[display(fmt = "Sys-call unrecoverable error: {_0}")] - UnrecoverableExt(UnrecoverableExtError), - #[display(fmt = "{_0}")] - Panic(LimitedStr<'static>), - #[display(fmt = "Reason is unknown. Possibly `unreachable` instruction is occurred")] - Unknown, -} - /// Extended externalities that can manage gas counters. pub trait BackendExternalities: Externalities + CountersOwner { fn gas_amount(&self) -> GasAmount; @@ -262,39 +53,5 @@ pub trait BackendExternalities: Externalities + CountersOwner { ) -> Result<(), ProcessAccessError>; } -/// Error returned from closure argument in [`runtime::CallerWrap::run_fallible`]. -#[derive(Debug, Clone)] -pub enum RunFallibleError { - UndefinedTerminationReason(UndefinedTerminationReason), - FallibleExt(FallibleExtError), -} - -impl From for RunFallibleError -where - E: BackendSyscallError, -{ - fn from(err: E) -> Self { - err.into_run_fallible_error() - } -} - -/// A trait for conversion of the externalities API error -/// to `UndefinedTerminationReason` and `RunFallibleError`. -pub trait BackendSyscallError: Sized { - fn into_termination_reason(self) -> UndefinedTerminationReason; - - fn into_run_fallible_error(self) -> RunFallibleError; -} - -// TODO: consider to remove this trait and use Result, GasError> instead #2571 -/// A trait for conversion of the externalities memory management error to api error. -/// -/// If the conversion fails, then `Self` is returned in the `Err` variant. -pub trait BackendAllocSyscallError: Sized { - type ExtError: BackendSyscallError; - - fn into_backend_error(self) -> Result; -} - #[cfg(test)] mod tests; diff --git a/core-backend/src/memory.rs b/core-backend/src/memory.rs index a88cd5251d4..65fadbe026b 100644 --- a/core-backend/src/memory.rs +++ b/core-backend/src/memory.rs @@ -19,8 +19,12 @@ //! sp-sandbox extensions for memory. use crate::{ - state::HostState, BackendExternalities, BackendSyscallError, RunFallibleError, TrapExplanation, - UndefinedTerminationReason, UnrecoverableMemoryError, + error::{ + BackendSyscallError, RunFallibleError, TrapExplanation, UndefinedTerminationReason, + UnrecoverableMemoryError, + }, + state::HostState, + BackendExternalities, }; use alloc::vec::Vec; use codec::{Decode, DecodeAll, MaxEncodedLen}; @@ -468,7 +472,7 @@ pub struct WasmMemoryWrite { #[cfg(test)] mod tests { use super::*; - use crate::{mock::MockExt, state::State, ActorTerminationReason}; + use crate::{error::ActorTerminationReason, mock::MockExt, state::State}; use gear_core::memory::{AllocError, AllocationsContext, NoopGrowHandler}; use gear_sandbox::{AsContextExt, SandboxStore}; diff --git a/core-backend/src/mock.rs b/core-backend/src/mock.rs index 10f0cc1e34d..4264aabedab 100644 --- a/core-backend/src/mock.rs +++ b/core-backend/src/mock.rs @@ -17,8 +17,10 @@ // along with this program. If not, see . use crate::{ - BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, - UndefinedTerminationReason, + error::{ + BackendAllocSyscallError, BackendSyscallError, RunFallibleError, UndefinedTerminationReason, + }, + BackendExternalities, }; use alloc::{collections::BTreeSet, vec, vec::Vec}; use codec::{Decode, Encode}; diff --git a/core-backend/src/runtime.rs b/core-backend/src/runtime.rs index 272ece18dbb..549eb1c472f 100644 --- a/core-backend/src/runtime.rs +++ b/core-backend/src/runtime.rs @@ -19,13 +19,13 @@ //! sp-sandbox runtime (here it's contract execution state) realization. use crate::{ + error::{BackendAllocSyscallError, RunFallibleError, UndefinedTerminationReason}, memory::{ MemoryAccessError, MemoryAccessManager, MemoryWrapRef, WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, state::{HostState, State}, - BackendAllocSyscallError, BackendExternalities, DefaultExecutorMemory, RunFallibleError, - UndefinedTerminationReason, + BackendExternalities, DefaultExecutorMemory, }; use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; diff --git a/core-backend/src/state.rs b/core-backend/src/state.rs index b6fa3d5b8cd..6159391c390 100644 --- a/core-backend/src/state.rs +++ b/core-backend/src/state.rs @@ -17,8 +17,10 @@ // along with this program. If not, see . use crate::{ - ActorTerminationReason, BackendExternalities, TerminationReason, TrapExplanation, - UndefinedTerminationReason, + error::{ + ActorTerminationReason, TerminationReason, TrapExplanation, UndefinedTerminationReason, + }, + BackendExternalities, }; use core::fmt::Debug; diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index 1442d66ce6a..748293b0013 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -39,7 +39,10 @@ use gear_core::{ program::Program, reservation::{GasReservationMap, GasReserver}, }; -use gear_core_backend::{env::SystemEnvironmentError, SystemTerminationReason, TrapExplanation}; +use gear_core_backend::{ + env::SystemEnvironmentError, + error::{SystemTerminationReason, TrapExplanation}, +}; use gear_core_errors::{SignalCode, SimpleExecutionError}; use scale_info::scale::{self, Decode, Encode}; diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index b452a9de137..830790cb35e 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -47,8 +47,11 @@ use gear_core::{ }; use gear_core_backend::{ env::{BackendReport, EnvironmentError}, - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - Environment, MemoryWrap, RunFallibleError, TerminationReason, + error::{ + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, RunFallibleError, + TerminationReason, + }, + BackendExternalities, Environment, MemoryWrap, }; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights}; use scale_info::{ diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 4b210f8bb92..3fcbd5b273f 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -43,9 +43,12 @@ use gear_core::{ reservation::GasReserver, }; use gear_core_backend::{ - ActorTerminationReason, BackendAllocSyscallError, BackendExternalities, BackendSyscallError, - RunFallibleError, TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, - UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, + error::{ + ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, RunFallibleError, + TrapExplanation, UndefinedTerminationReason, UnrecoverableExecutionError, + UnrecoverableExtError as UnrecoverableExtErrorCore, UnrecoverableWaitError, + }, + BackendExternalities, }; use gear_core_errors::{ ExecutionError as FallibleExecutionError, ExtError as FallibleExtErrorCore, MessageError, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index c5d276c446e..8becd057446 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -35,7 +35,8 @@ use gear_core::{ reservation::GasReservationState, }; use gear_core_backend::{ - BackendAllocSyscallError, BackendExternalities, BackendSyscallError, RunFallibleError, + error::{BackendAllocSyscallError, BackendSyscallError, RunFallibleError}, + BackendExternalities, }; use gear_core_errors::{ErrorReplyReason, SignalCode}; diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index 0a361b58c9f..4ddf18a252f 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -69,7 +69,7 @@ use gear_core::{ message::UserStoredMessage, pages::{PageNumber, PageU32Size, WasmPage}, }; -use gear_core_backend::{ +use gear_core_backend::error::{ TrapExplanation, UnrecoverableExecutionError, UnrecoverableExtError, UnrecoverableWaitError, }; use gear_core_errors::*; diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 62cdb17a3af..1c176091d40 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -28,7 +28,11 @@ use gear_core::{ }, pages::WASM_PAGE_SIZE, }; -use gear_core_backend::{env::BackendReport, Environment, TerminationReason, TrapExplanation}; +use gear_core_backend::{ + env::BackendReport, + error::{TerminationReason, TrapExplanation}, + Environment, +}; use gear_core_processor::{ProcessorContext, ProcessorExternalities}; use gear_utils::NonEmpty; use gear_wasm_instrument::{ @@ -173,7 +177,7 @@ fn injecting_addresses_works() { #[test] fn error_processing_works_for_fallible_syscalls() { - use gear_core_backend::ActorTerminationReason; + use gear_core_backend::error::ActorTerminationReason; let fallible_syscalls = SysCallName::instrumentable() .into_iter() diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index e5e0eab29d8..4a5a56a5d72 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -624,7 +624,7 @@ fn test_sys_calls_table() { use gas_metering::ConstantCostRules; use gear_core::message::DispatchKind; use gear_core_backend::{ - env::BackendReport, mock::MockExt, ActorTerminationReason, Environment, + env::BackendReport, error::ActorTerminationReason, mock::MockExt, Environment, }; use parity_wasm::builder; From f24a7e476ba14cf0fc26e31146297b809752bc20 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 23:20:16 +0300 Subject: [PATCH 35/41] Remove `pub use` in lib.rs --- core-backend/src/funcs.rs | 2 +- core-backend/src/lib.rs | 5 +---- core-backend/src/memory.rs | 16 ++++++++-------- core-backend/src/runtime.rs | 9 +++++---- core-processor/src/executor.rs | 5 +++-- utils/wasm-gen/src/tests.rs | 3 +-- utils/wasm-instrument/src/tests.rs | 4 +++- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/core-backend/src/funcs.rs b/core-backend/src/funcs.rs index 22e2ce2f139..68a932d1328 100644 --- a/core-backend/src/funcs.rs +++ b/core-backend/src/funcs.rs @@ -86,7 +86,7 @@ macro_rules! syscall_trace { const PTR_SPECIAL: u32 = u32::MAX; -pub struct FuncsHandler { +pub(crate) struct FuncsHandler { _phantom: PhantomData, } diff --git a/core-backend/src/lib.rs b/core-backend/src/lib.rs index d412eb274de..1a20d9bae1f 100644 --- a/core-backend/src/lib.rs +++ b/core-backend/src/lib.rs @@ -25,15 +25,12 @@ extern crate alloc; pub mod env; pub mod error; mod funcs; -mod memory; +pub mod memory; #[cfg(any(feature = "mock", test))] pub mod mock; mod runtime; mod state; -pub use env::Environment; -pub use memory::{DefaultExecutorMemory, MemoryWrap}; - use gear_core::{ env::Externalities, gas::{CountersOwner, GasAmount}, diff --git a/core-backend/src/memory.rs b/core-backend/src/memory.rs index 65fadbe026b..9d0e2a025ed 100644 --- a/core-backend/src/memory.rs +++ b/core-backend/src/memory.rs @@ -44,7 +44,7 @@ use gear_sandbox::{ pub type DefaultExecutorMemory = gear_sandbox::default_executor::Memory; -pub struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { +pub(crate) struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { pub memory: DefaultExecutorMemory, pub caller: &'a mut Caller<'b, HostState>, } @@ -138,7 +138,7 @@ where } #[derive(Debug, Clone, derive_more::From)] -pub enum MemoryAccessError { +pub(crate) enum MemoryAccessError { Memory(MemoryError), ProcessAccess(ProcessAccessError), RuntimeBuffer(RuntimeBufferSizeError), @@ -204,7 +204,7 @@ impl BackendSyscallError for MemoryAccessError { /// manager.write_as(write1, 111).unwrap(); /// ``` #[derive(Debug)] -pub struct MemoryAccessManager { +pub(crate) struct MemoryAccessManager { // Contains non-zero length intervals only. pub(crate) reads: Vec, pub(crate) writes: Vec, @@ -439,31 +439,31 @@ fn read_memory_as(memory: &impl Memory, ptr: u32) -> Result { +pub(crate) struct WasmMemoryReadAs { pub(crate) ptr: u32, pub(crate) _phantom: PhantomData, } /// Read decoded type access wrapper. -pub struct WasmMemoryReadDecoded { +pub(crate) struct WasmMemoryReadDecoded { pub(crate) ptr: u32, pub(crate) _phantom: PhantomData, } /// Read access wrapper. -pub struct WasmMemoryRead { +pub(crate) struct WasmMemoryRead { pub(crate) ptr: u32, pub(crate) size: u32, } /// Write static size type access wrapper. -pub struct WasmMemoryWriteAs { +pub(crate) struct WasmMemoryWriteAs { pub(crate) ptr: u32, pub(crate) _phantom: PhantomData, } /// Write access wrapper. -pub struct WasmMemoryWrite { +pub(crate) struct WasmMemoryWrite { pub(crate) ptr: u32, pub(crate) size: u32, } diff --git a/core-backend/src/runtime.rs b/core-backend/src/runtime.rs index 549eb1c472f..53d932e8ed5 100644 --- a/core-backend/src/runtime.rs +++ b/core-backend/src/runtime.rs @@ -21,11 +21,12 @@ use crate::{ error::{BackendAllocSyscallError, RunFallibleError, UndefinedTerminationReason}, memory::{ - MemoryAccessError, MemoryAccessManager, MemoryWrapRef, WasmMemoryRead, WasmMemoryReadAs, - WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, + DefaultExecutorMemory, MemoryAccessError, MemoryAccessManager, MemoryWrapRef, + WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, + WasmMemoryWriteAs, }, state::{HostState, State}, - BackendExternalities, DefaultExecutorMemory, + BackendExternalities, }; use alloc::vec::Vec; use codec::{Decode, MaxEncodedLen}; @@ -59,7 +60,7 @@ pub(crate) fn caller_host_state_take( .unwrap_or_else(|| unreachable!("host_state must be set before execution")) } -pub struct CallerWrap<'a, 'b: 'a, Ext> { +pub(crate) struct CallerWrap<'a, 'b: 'a, Ext> { pub caller: &'a mut Caller<'b, HostState>, pub manager: MemoryAccessManager, pub memory: DefaultExecutorMemory, diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 830790cb35e..1f71adaf08f 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -46,12 +46,13 @@ use gear_core::{ reservation::GasReserver, }; use gear_core_backend::{ - env::{BackendReport, EnvironmentError}, + env::{BackendReport, Environment, EnvironmentError}, error::{ ActorTerminationReason, BackendAllocSyscallError, BackendSyscallError, RunFallibleError, TerminationReason, }, - BackendExternalities, Environment, MemoryWrap, + memory::MemoryWrap, + BackendExternalities, }; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights}; use scale_info::{ diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 1c176091d40..73b69f15a53 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -29,9 +29,8 @@ use gear_core::{ pages::WASM_PAGE_SIZE, }; use gear_core_backend::{ - env::BackendReport, + env::{BackendReport, Environment}, error::{TerminationReason, TrapExplanation}, - Environment, }; use gear_core_processor::{ProcessorContext, ProcessorExternalities}; use gear_utils::NonEmpty; diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 4a5a56a5d72..a29fd16c75b 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -624,7 +624,9 @@ fn test_sys_calls_table() { use gas_metering::ConstantCostRules; use gear_core::message::DispatchKind; use gear_core_backend::{ - env::BackendReport, error::ActorTerminationReason, mock::MockExt, Environment, + env::{BackendReport, Environment}, + error::ActorTerminationReason, + mock::MockExt, }; use parity_wasm::builder; From 2d30e7a419fdce1ff9dbe11d8bc3f4c4416227a8 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 23:21:47 +0300 Subject: [PATCH 36/41] Rename `DefaultExecutorMemory` --- core-backend/src/memory.rs | 23 ++++++++++------------- core-backend/src/runtime.rs | 25 ++++++++++++------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/core-backend/src/memory.rs b/core-backend/src/memory.rs index 9d0e2a025ed..efac32db83d 100644 --- a/core-backend/src/memory.rs +++ b/core-backend/src/memory.rs @@ -42,11 +42,11 @@ use gear_sandbox::{ SandboxMemory, }; -pub type DefaultExecutorMemory = gear_sandbox::default_executor::Memory; +pub(crate) type ExecutorMemory = gear_sandbox::default_executor::Memory; pub(crate) struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { - pub memory: DefaultExecutorMemory, - pub caller: &'a mut Caller<'b, HostState>, + pub memory: ExecutorMemory, + pub caller: &'a mut Caller<'b, HostState>, } impl Memory for MemoryWrapRef<'_, '_, Ext> { @@ -78,28 +78,25 @@ impl Memory for MemoryWrapRef<'_, '_, Ext> { } } -/// Wrapper for [`DefaultExecutorMemory`]. +/// Wrapper for [`ExecutorMemory`]. pub struct MemoryWrap where Ext: Externalities + 'static, { - pub(crate) memory: DefaultExecutorMemory, - pub(crate) store: Store>, + pub(crate) memory: ExecutorMemory, + pub(crate) store: Store>, } impl MemoryWrap where Ext: Externalities + 'static, { - /// Wrap [`DefaultExecutorMemory`] for Memory trait. - pub fn new( - memory: DefaultExecutorMemory, - store: Store>, - ) -> Self { + /// Wrap [`ExecutorMemory`] for Memory trait. + pub fn new(memory: ExecutorMemory, store: Store>) -> Self { MemoryWrap { memory, store } } - pub(crate) fn into_store(self) -> Store> { + pub(crate) fn into_store(self) -> Store> { self.store } } @@ -483,7 +480,7 @@ mod tests { use gear_sandbox::SandboxMemory as WasmMemory; let mut store = Store::new(None); - let memory: DefaultExecutorMemory = + let memory: ExecutorMemory = WasmMemory::new(&mut store, static_pages as u32, Some(max_pages as u32)) .expect("Memory creation failed"); *store.data_mut() = Some(State { diff --git a/core-backend/src/runtime.rs b/core-backend/src/runtime.rs index 53d932e8ed5..c5ee4388ab3 100644 --- a/core-backend/src/runtime.rs +++ b/core-backend/src/runtime.rs @@ -21,9 +21,8 @@ use crate::{ error::{BackendAllocSyscallError, RunFallibleError, UndefinedTerminationReason}, memory::{ - DefaultExecutorMemory, MemoryAccessError, MemoryAccessManager, MemoryWrapRef, - WasmMemoryRead, WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, - WasmMemoryWriteAs, + ExecutorMemory, MemoryAccessError, MemoryAccessManager, MemoryWrapRef, WasmMemoryRead, + WasmMemoryReadAs, WasmMemoryReadDecoded, WasmMemoryWrite, WasmMemoryWriteAs, }, state::{HostState, State}, BackendExternalities, @@ -42,8 +41,8 @@ pub(crate) fn as_i64(v: Value) -> Option { #[track_caller] pub(crate) fn caller_host_state_mut<'a, 'b: 'a, Ext>( - caller: &'a mut Caller<'_, HostState>, -) -> &'a mut State { + caller: &'a mut Caller<'_, HostState>, +) -> &'a mut State { caller .data_mut() .as_mut() @@ -52,8 +51,8 @@ pub(crate) fn caller_host_state_mut<'a, 'b: 'a, Ext>( #[track_caller] pub(crate) fn caller_host_state_take( - caller: &mut Caller<'_, HostState>, -) -> State { + caller: &mut Caller<'_, HostState>, +) -> State { caller .data_mut() .take() @@ -61,9 +60,9 @@ pub(crate) fn caller_host_state_take( } pub(crate) struct CallerWrap<'a, 'b: 'a, Ext> { - pub caller: &'a mut Caller<'b, HostState>, + pub caller: &'a mut Caller<'b, HostState>, pub manager: MemoryAccessManager, - pub memory: DefaultExecutorMemory, + pub memory: ExecutorMemory, } impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { @@ -133,7 +132,7 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { #[track_caller] pub fn prepare( - caller: &'a mut Caller<'b, HostState>, + caller: &'a mut Caller<'b, HostState>, ) -> Result { let memory = caller_host_state_mut(caller).memory.clone(); Ok(Self { @@ -144,14 +143,14 @@ impl<'a, 'b, Ext: BackendExternalities + 'static> CallerWrap<'a, 'b, Ext> { } #[track_caller] - pub fn host_state_mut(&mut self) -> &mut State { + pub fn host_state_mut(&mut self) -> &mut State { caller_host_state_mut(self.caller) } #[track_caller] pub fn memory<'c, 'd: 'c>( - caller: &'c mut Caller<'d, HostState>, - memory: DefaultExecutorMemory, + caller: &'c mut Caller<'d, HostState>, + memory: ExecutorMemory, ) -> MemoryWrapRef<'c, 'd, Ext> { MemoryWrapRef::<'c, 'd, _> { memory, caller } } From d53f9d23931d575a8da6adece4f0b85fb3a6fbf1 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 23:35:05 +0300 Subject: [PATCH 37/41] Fix docs --- core-backend/src/error.rs | 2 +- core-backend/src/memory.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core-backend/src/error.rs b/core-backend/src/error.rs index 5b5c82a9b63..baabe2bba54 100644 --- a/core-backend/src/error.rs +++ b/core-backend/src/error.rs @@ -229,7 +229,7 @@ pub enum TrapExplanation { Unknown, } -/// Error returned from closure argument in [`runtime::CallerWrap::run_fallible`]. +/// Error returned by fallible sys-call. #[derive(Debug, Clone)] pub enum RunFallibleError { UndefinedTerminationReason(UndefinedTerminationReason), diff --git a/core-backend/src/memory.rs b/core-backend/src/memory.rs index efac32db83d..e3a8d662f1c 100644 --- a/core-backend/src/memory.rs +++ b/core-backend/src/memory.rs @@ -78,7 +78,7 @@ impl Memory for MemoryWrapRef<'_, '_, Ext> { } } -/// Wrapper for [`ExecutorMemory`]. +/// Wrapper for executor memory. pub struct MemoryWrap where Ext: Externalities + 'static, @@ -92,7 +92,10 @@ where Ext: Externalities + 'static, { /// Wrap [`ExecutorMemory`] for Memory trait. - pub fn new(memory: ExecutorMemory, store: Store>) -> Self { + pub(crate) fn new( + memory: ExecutorMemory, + store: Store>, + ) -> Self { MemoryWrap { memory, store } } From fa6e856929df93f794992a061c90f91d9e304fce Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Sun, 24 Sep 2023 23:41:58 +0300 Subject: [PATCH 38/41] Fix benchmark imports --- core-backend/src/memory.rs | 7 ++----- pallets/gear/src/benchmarking/mod.rs | 7 +++++-- pallets/gear/src/benchmarking/tests/mod.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core-backend/src/memory.rs b/core-backend/src/memory.rs index e3a8d662f1c..626757962a5 100644 --- a/core-backend/src/memory.rs +++ b/core-backend/src/memory.rs @@ -42,7 +42,7 @@ use gear_sandbox::{ SandboxMemory, }; -pub(crate) type ExecutorMemory = gear_sandbox::default_executor::Memory; +pub type ExecutorMemory = gear_sandbox::default_executor::Memory; pub(crate) struct MemoryWrapRef<'a, 'b: 'a, Ext: Externalities + 'static> { pub memory: ExecutorMemory, @@ -92,10 +92,7 @@ where Ext: Externalities + 'static, { /// Wrap [`ExecutorMemory`] for Memory trait. - pub(crate) fn new( - memory: ExecutorMemory, - store: Store>, - ) -> Self { + pub fn new(memory: ExecutorMemory, store: Store>) -> Self { MemoryWrap { memory, store } } diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 23cd7cbe50b..6ee97ec741c 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -94,7 +94,10 @@ use gear_core::{ pages::{GearPage, PageU32Size, WasmPage, GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, reservation::GasReserver, }; -use gear_core_backend::{DefaultExecutorMemory, Environment, MemoryWrap}; +use gear_core_backend::{ + env::Environment, + memory::{ExecutorMemory, MemoryWrap}, +}; use gear_core_errors::*; use gear_sandbox::{default_executor::Store, SandboxMemory, SandboxStore}; use gear_wasm_instrument::{ @@ -1639,7 +1642,7 @@ benchmarks! { mem_grow { let r in 0 .. API_BENCHMARK_BATCHES; let mut store = Store::new(None); - let mem = DefaultExecutorMemory::new(&mut store, 1, None).unwrap(); + let mem = ExecutorMemory::new(&mut store, 1, None).unwrap(); let mut mem = MemoryWrap::::new(mem, store); }: { for _ in 0..(r * API_BENCHMARK_BATCH_SIZE) { diff --git a/pallets/gear/src/benchmarking/tests/mod.rs b/pallets/gear/src/benchmarking/tests/mod.rs index 286808fde86..52ddf0870f0 100644 --- a/pallets/gear/src/benchmarking/tests/mod.rs +++ b/pallets/gear/src/benchmarking/tests/mod.rs @@ -36,7 +36,7 @@ use crate::{ HandleKind, }; use common::benchmarking; -use gear_core_backend::TrapExplanation; +use gear_core_backend::error::TrapExplanation; use gear_wasm_instrument::parity_wasm::elements::Instruction; pub fn check_stack_overflow() From 509ac611f2fad412b332c3f73377584b788bafd7 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 25 Sep 2023 19:34:46 +0300 Subject: [PATCH 39/41] Remove `as lazy_pages` imports --- core-processor/src/ext.rs | 21 +++++++----- .../gear/src/benchmarking/tests/lazy_pages.rs | 5 ++- pallets/gear/src/lib.rs | 3 +- runtime-interface/src/lib.rs | 32 ++++++++----------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/core-processor/src/ext.rs b/core-processor/src/ext.rs index 3fcbd5b273f..55d1f96dec9 100644 --- a/core-processor/src/ext.rs +++ b/core-processor/src/ext.rs @@ -55,7 +55,6 @@ use gear_core_errors::{ ProgramRentError, ReplyCode, ReservationError, SignalCode, }; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesWeights, ProcessAccessError, Status}; -use gear_lazy_pages_interface as lazy_pages; use gear_wasm_instrument::syscalls::SysCallName; /// Processor context. @@ -315,7 +314,7 @@ impl GrowHandler for LazyGrowHandler { // So we remove protections from lazy-pages // and then in `after_grow_action` we set protection back for new wasm memory buffer. let old_mem_addr = mem.get_buffer_host_addr(); - lazy_pages::remove_lazy_pages_prot(mem); + gear_lazy_pages_interface::remove_lazy_pages_prot(mem); Self { old_mem_addr, old_mem_size: mem.size(), @@ -328,7 +327,7 @@ impl GrowHandler for LazyGrowHandler { let new_mem_addr = mem.get_buffer_host_addr().unwrap_or_else(|| { unreachable!("Memory size cannot be zero after grow is applied for memory") }); - lazy_pages::update_lazy_pages_and_protect_again( + gear_lazy_pages_interface::update_lazy_pages_and_protect_again( mem, self.old_mem_addr, self.old_mem_size, @@ -381,7 +380,7 @@ impl ProcessorExternalities for Ext { let (static_pages, initial_allocations, allocations) = allocations_context.into_parts(); // Accessed pages are all pages, that had been released and are in allocations set or static. - let mut accessed_pages = lazy_pages::get_write_accessed_pages(); + let mut accessed_pages = gear_lazy_pages_interface::get_write_accessed_pages(); accessed_pages.retain(|p| { let wasm_page = p.to_page(); wasm_page < static_pages || allocations.contains(&wasm_page) @@ -437,15 +436,21 @@ impl ProcessorExternalities for Ext { globals_config: GlobalsAccessConfig, lazy_pages_weights: LazyPagesWeights, ) { - lazy_pages::init_for_program(mem, prog_id, stack_end, globals_config, lazy_pages_weights); + gear_lazy_pages_interface::init_for_program( + mem, + prog_id, + stack_end, + globals_config, + lazy_pages_weights, + ); } fn lazy_pages_post_execution_actions(mem: &mut impl Memory) { - lazy_pages::remove_lazy_pages_prot(mem); + gear_lazy_pages_interface::remove_lazy_pages_prot(mem); } fn lazy_pages_status() -> Status { - lazy_pages::get_status() + gear_lazy_pages_interface::get_status() } } @@ -459,7 +464,7 @@ impl BackendExternalities for Ext { writes: &[MemoryInterval], gas_counter: &mut u64, ) -> Result<(), ProcessAccessError> { - lazy_pages::pre_process_memory_accesses(reads, writes, gas_counter) + gear_lazy_pages_interface::pre_process_memory_accesses(reads, writes, gas_counter) } } diff --git a/pallets/gear/src/benchmarking/tests/lazy_pages.rs b/pallets/gear/src/benchmarking/tests/lazy_pages.rs index ea10a290533..de869b20aef 100644 --- a/pallets/gear/src/benchmarking/tests/lazy_pages.rs +++ b/pallets/gear/src/benchmarking/tests/lazy_pages.rs @@ -29,7 +29,6 @@ use gear_core::{ pages::{PageNumber, PageU32Size}, }; use gear_lazy_pages_common::Status; -use gear_lazy_pages_interface as lazy_pages; use rand::{Rng, SeedableRng}; use super::*; @@ -590,7 +589,7 @@ where } } - assert_ne!(lazy_pages::get_status(), Status::Normal); + assert_ne!(gear_lazy_pages_interface::get_status(), Status::Normal); }; // Check gas allowance exceeded. @@ -624,6 +623,6 @@ where } } - assert_ne!(lazy_pages::get_status(), Status::Normal); + assert_ne!(gear_lazy_pages_interface::get_status(), Status::Normal); }; } diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 06f7c0b087f..6ac05198171 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -73,7 +73,6 @@ use gear_core::{ message::*, pages::{GearPage, WasmPage}, }; -use gear_lazy_pages_interface as lazy_pages; use manager::{CodeInfo, QueuePostProcessingData}; use primitive_types::H256; use sp_runtime::{ @@ -994,7 +993,7 @@ pub mod pallet { pub(crate) fn enable_lazy_pages() { let prefix = ProgramStorageOf::::pages_final_prefix(); - if !lazy_pages::try_to_enable_lazy_pages(prefix) { + if !gear_lazy_pages_interface::try_to_enable_lazy_pages(prefix) { unreachable!("By some reasons we cannot run lazy-pages on this machine"); } } diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index 0b3c09bff66..fecc627ef0a 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -21,28 +21,24 @@ #![allow(useless_deprecated, deprecated)] #![cfg_attr(not(feature = "std"), no_std)] +extern crate alloc; + use byteorder::{ByteOrder, LittleEndian}; use codec::{Decode, Encode}; use gear_core::{ gas::GasLeft, memory::{HostPointer, MemoryInterval}, + str::LimitedStr, }; use gear_lazy_pages_common::{GlobalsAccessConfig, ProcessAccessError, Status}; use sp_runtime_interface::{ pass_by::{Codec, PassBy}, runtime_interface, }; -use sp_std::mem; - -extern crate alloc; - -#[cfg(feature = "std")] -use gear_lazy_pages as lazy_pages; - -use gear_core::str::LimitedStr; -pub use sp_std::{convert::TryFrom, result::Result, vec::Vec}; +use sp_std::{convert::TryFrom, mem, result::Result, vec::Vec}; mod gear_sandbox; + #[cfg(feature = "std")] pub use gear_sandbox::init as sandbox_init; pub use gear_sandbox::sandbox; @@ -111,7 +107,7 @@ pub trait GearRI { ) -> (GasLeft, Result<(), ProcessAccessErrorVer1>) { let mut gas_left = gas_left.0; let gas_before = gas_left.gas; - let res = lazy_pages::pre_process_memory_accesses(reads, writes, &mut gas_left.gas); + let res = gear_lazy_pages::pre_process_memory_accesses(reads, writes, &mut gas_left.gas); // Support charge for allowance otherwise DB will be corrupted. gas_left.allowance = gas_left @@ -148,7 +144,7 @@ pub trait GearRI { let mut gas_counter = LittleEndian::read_u64(gas_bytes); - let res = match lazy_pages::pre_process_memory_accesses( + let res = match gear_lazy_pages::pre_process_memory_accesses( &reads_intervals, &writes_intervals, &mut gas_counter, @@ -163,16 +159,16 @@ pub trait GearRI { } fn lazy_pages_status() -> (Status,) { - (lazy_pages::status() + (gear_lazy_pages::status() .unwrap_or_else(|err| unreachable!("Cannot get lazy-pages status: {err}")),) } /// Init lazy-pages. /// Returns whether initialization was successful. fn init_lazy_pages(ctx: LazyPagesRuntimeContext) -> bool { - use lazy_pages::LazyPagesVersion; + use gear_lazy_pages::LazyPagesVersion; - lazy_pages::init( + gear_lazy_pages::init( LazyPagesVersion::Version1, ctx.page_sizes, ctx.global_names, @@ -190,7 +186,7 @@ pub trait GearRI { .unwrap_or_else(|err| unreachable!("Cannot cast wasm mem addr to `usize`: {}", err)) }); - lazy_pages::initialize_for_program( + gear_lazy_pages::initialize_for_program( wasm_mem_addr, ctx.wasm_mem_size, ctx.stack_end, @@ -207,9 +203,9 @@ pub trait GearRI { /// else allows read and write accesses. fn mprotect_lazy_pages(protect: bool) { if protect { - lazy_pages::set_lazy_pages_protection() + gear_lazy_pages::set_lazy_pages_protection() } else { - lazy_pages::unset_lazy_pages_protection() + gear_lazy_pages::unset_lazy_pages_protection() } .map_err(|err| err.to_string()) .expect("Cannot set/unset mprotection for lazy pages"); @@ -222,7 +218,7 @@ pub trait GearRI { } fn write_accessed_pages() -> Vec { - lazy_pages::write_accessed_pages() + gear_lazy_pages::write_accessed_pages() .unwrap_or_else(|err| unreachable!("Cannot get write accessed pages: {err}")) } From ab9308007c18aa590ba25bdbfa493dd061c91b69 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Mon, 25 Sep 2023 19:38:21 +0300 Subject: [PATCH 40/41] Fix import --- utils/wasm-gen/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 980e8405a62..fa5887bbe6a 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -243,7 +243,7 @@ fn error_processing_works_for_fallible_syscalls() { #[test] fn precise_syscalls_works() { - use gear_backend_common::ActorTerminationReason; + use gear_core_backend::error::ActorTerminationReason; gear_utils::init_default_logger(); From 15d64e5b6d3cf89585379150117a3fd092b4df0c Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Tue, 26 Sep 2023 15:19:13 +0300 Subject: [PATCH 41/41] Update core/src/str.rs Co-authored-by: Georgy Shepelev --- core/src/str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/str.rs b/core/src/str.rs index ea5dc35a46d..84f09c1e667 100644 --- a/core/src/str.rs +++ b/core/src/str.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! String with limited length realization. +//! String with limited length implementation use alloc::{borrow::Cow, string::String}; use parity_scale_codec::{Decode, Encode};