Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(gear-core): check data section for uploading codes #3733

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/src/paused_program_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use super::{program_storage::MemoryMap, *};
use crate::storage::{MapStorage, ValueStorage};
use gear_core::{
code::MAX_WASM_PAGE_COUNT,
code::MAX_WASM_PAGE_AMOUNT,
pages::{GEAR_PAGE_SIZE, WASM_PAGE_SIZE},
};
use sp_core::MAX_POSSIBLE_ALLOCATION;
use sp_io::hashing;

const SPLIT_COUNT: u16 = (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 * MAX_WASM_PAGE_COUNT / 2;
const SPLIT_COUNT: u16 = (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 * MAX_WASM_PAGE_AMOUNT / 2;

pub type SessionId = u32;

Expand Down
5 changes: 3 additions & 2 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ actor_system_error! {
pub type PrepareMemoryError = ActorSystemError<ActorPrepareMemoryError, SystemPrepareMemoryError>;
}

// TODO: add this cases checks to Code::try_new in gear-core #3735
/// Prepare memory error
#[derive(Encode, Decode, TypeInfo, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
#[codec(crate = scale)]
Expand All @@ -79,8 +80,8 @@ pub enum ActorPrepareMemoryError {

#[derive(Debug, Eq, PartialEq, derive_more::Display)]
pub enum SystemPrepareMemoryError {
/// Mem size less then static pages num
#[display(fmt = "Mem size less then static pages num")]
/// Mem size less than static pages num
#[display(fmt = "Mem size less than static pages num")]
InsufficientMemorySize,
}

Expand Down
4 changes: 2 additions & 2 deletions core-processor/src/precharge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ impl<'a> GasPrecharger<'a> {

if let Some(page) = allocations.iter().next_back() {
// It means we somehow violated some constraints:
// 1. one of allocated pages > MAX_WASM_PAGE_COUNT
// 2. static pages > MAX_WASM_PAGE_COUNT
// 1. one of allocated pages > MAX_WASM_PAGE_AMOUNT
// 2. static pages > MAX_WASM_PAGE_AMOUNT
Ok(page
.inc()
.unwrap_or_else(|_| unreachable!("WASM memory size is too big")))
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ rand = { workspace = true, features = ["std", "std_rng"] }
[features]
default = []
strict = []
std = ["serde/std"]
std = ["serde/std", "wasmparser/std"]
77 changes: 46 additions & 31 deletions core/src/code/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

//! Module that describes various code errors.

use gear_wasm_instrument::InstrumentationError;
pub use gear_wasm_instrument::{parity_wasm::SerializationError, InstrumentationError};
pub use wasmparser::BinaryReaderError;

/// Section name in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum SectionName {
/// Type section.
#[display(fmt = "Type section")]
Expand All @@ -41,7 +42,7 @@ pub enum SectionName {
}

/// Section error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum SectionError {
/// Section not found.
#[display(fmt = "{_0} not found")]
Expand All @@ -52,7 +53,7 @@ pub enum SectionError {
}

/// Memory error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum MemoryError {
/// Memory entry not found in import section.
#[display(fmt = "Memory entry not found")]
Expand All @@ -63,29 +64,35 @@ pub enum MemoryError {
}

/// Stack end error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum StackEndError {
/// Can't insert new global due to index overflow in global section.
#[display(fmt = "Can't insert new global due to index overflow")]
/// Unsupported initialization of gear stack end global variable.
#[display(fmt = "Unsupported initialization of gear stack end global")]
Initialization,
/// Too many globals to create new const global for stack end.
#[display(fmt = "Too many globals, so cannot create new global for stack end")]
GlobalIndexOverflow,
/// Pointer to the stack end overlaps data segment.
#[display(fmt = "Pointer to the stack end overlaps data segment")]
StackEndOverlaps,
}

/// Initialization error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
pub enum InitializationError {
/// Unsupported initialization of gear stack end global variable.
#[display(fmt = "Unsupported initialization of gear stack end global variable")]
StackEnd,
/// Stack end error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum DataSectionError {
/// Unsupported initialization of data segment.
#[display(fmt = "Unsupported initialization of data segment")]
DataSegment,
Initialization,
/// Data section overlaps gear stack.
#[display(fmt = "Data segment {_0:#x} overlaps gear stack [0x0, {_1:#x})")]
GearStackOverlaps(u32, u32),
/// Data segment end address is out of possible 32 bits address space.
#[display(fmt = "Data segment {_0:#x} ends out of possible 32 bits address space")]
EndAddressOverflow(u32),
/// Data segment end address is out of static memory.
#[display(fmt = "Data segment [{_0:#x}, {_1:#x}] is out of static memory [0x0, {_2:#x})")]
EndAddressOutOfStaticMemory(u32, u32, u32),
}

/// Export error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum ExportError {
/// Incorrect global export index. Can occur when export refers to not existing global index.
#[display(fmt = "Global index `{_0}` in export index `{_1}` is incorrect")]
Expand All @@ -108,7 +115,7 @@ pub enum ExportError {
}

/// Import error in WASM module.
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, derive_more::Display)]
pub enum ImportError {
/// The imported function is not supported by the Gear protocol.
#[display(fmt = "Unknown imported function with index `{_0}`")]
Expand All @@ -121,18 +128,26 @@ pub enum ImportError {
InvalidImportFnSignature(u32),
}

/// Module encode/decode error.
#[derive(Debug, derive_more::Display)]
pub enum CodecError {
/// The wasm bytecode is failed to be decoded
#[display(fmt = "The wasm bytecode is failed to be decoded: {_0}")]
Decode(SerializationError),
/// Failed to encode instrumented program: {_0}
#[display(fmt = "Failed to encode instrumented program: {_0}")]
Encode(SerializationError),
}

/// Describes why the code is not valid Gear program.
#[derive(Debug, PartialEq, Eq, derive_more::Display, derive_more::From)]
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum CodeError {
/// Validation by wasmparser failed.
#[display(fmt = "Wasm validation failed")]
Validation,
/// Error occurred during decoding original program code.
#[display(fmt = "The wasm bytecode is failed to be decoded")]
Decode,
/// Error occurred during encoding instrumented program.
#[display(fmt = "Failed to encode instrumented program")]
Encode,
#[display(fmt = "Wasmer validation error: {_0}")]
Validation(BinaryReaderError),
/// Module encode/decode error.
#[display(fmt = "Codec error: {_0}")]
Codec(CodecError),
/// The provided code contains section error.
#[display(fmt = "Section error: {_0}")]
Section(SectionError),
Expand All @@ -142,9 +157,9 @@ pub enum CodeError {
/// The provided code contains stack end error.
#[display(fmt = "Stack end error: {_0}")]
StackEnd(StackEndError),
/// The provided code contains initialization error.
#[display(fmt = "Initialization error: {_0}")]
Initialization(InitializationError),
/// The provided code contains data section error.
#[display(fmt = "Data section error: {_0}")]
DataSection(DataSectionError),
/// The provided code contains export error.
#[display(fmt = "Export error: {_0}")]
Export(ExportError),
Expand Down
Loading
Loading