diff --git a/utils/node-loader/src/utils.rs b/utils/node-loader/src/utils.rs index c155c61a1b4..3cf66ccdeaa 100644 --- a/utils/node-loader/src/utils.rs +++ b/utils/node-loader/src/utils.rs @@ -7,8 +7,8 @@ use gear_core::ids::{MessageId, ProgramId}; use gear_core_errors::ReplyCode; use gear_utils::NonEmpty; use gear_wasm_gen::{ - EntryPointsSet, InvocableSyscall, ParamType, StandardGearWasmConfigsBundle, SyscallName, - SyscallsInjectionTypes, SyscallsParamsConfig, + EntryPointsSet, InvocableSyscall, ParamType, RegularParamType, StandardGearWasmConfigsBundle, + SyscallName, SyscallsInjectionTypes, SyscallsParamsConfig, }; use gsdk::metadata::runtime_types::{ gear_common::event::DispatchStatus as GenDispatchStatus, @@ -230,8 +230,11 @@ pub fn get_wasm_gen_config( ); let mut params_config = SyscallsParamsConfig::default(); - params_config.add_rule(ParamType::Alloc, (1..=10).into()); - params_config.add_rule(ParamType::Free, (initial_pages..=initial_pages + 50).into()); + params_config.add_rule(ParamType::Regular(RegularParamType::Alloc), (1..=10).into()); + params_config.add_rule( + ParamType::Regular(RegularParamType::Free), + (initial_pages..=initial_pages + 50).into(), + ); StandardGearWasmConfigsBundle { log_info: Some(format!("Gear program seed = '{seed}'")), diff --git a/utils/runtime-fuzzer/src/gear_calls.rs b/utils/runtime-fuzzer/src/gear_calls.rs index cf241af7040..1e57f8d81a4 100644 --- a/utils/runtime-fuzzer/src/gear_calls.rs +++ b/utils/runtime-fuzzer/src/gear_calls.rs @@ -35,8 +35,8 @@ use gear_call_gen::{ClaimValueArgs, SendReplyArgs}; use gear_core::ids::{CodeId, MessageId, ProgramId}; use gear_utils::NonEmpty; use gear_wasm_gen::{ - EntryPointsSet, InvocableSyscall, ParamType, StandardGearWasmConfigsBundle, SyscallName, - SyscallsInjectionTypes, SyscallsParamsConfig, + EntryPointsSet, InvocableSyscall, ParamType, RegularParamType, StandardGearWasmConfigsBundle, + SyscallName, SyscallsInjectionTypes, SyscallsParamsConfig, }; use std::mem; @@ -425,8 +425,14 @@ fn config( ); let mut params_config = SyscallsParamsConfig::default(); - params_config.add_rule(ParamType::Alloc, (10..=20).into()); - params_config.add_rule(ParamType::Free, (initial_pages..=initial_pages + 35).into()); + params_config.add_rule( + ParamType::Regular(RegularParamType::Alloc), + (10..=20).into(), + ); + params_config.add_rule( + ParamType::Regular(RegularParamType::Free), + (initial_pages..=initial_pages + 35).into(), + ); let existing_addresses = NonEmpty::collect( programs diff --git a/utils/wasm-gen/src/generator/syscalls.rs b/utils/wasm-gen/src/generator/syscalls.rs index f1eb0ae9df6..441a4286dc6 100644 --- a/utils/wasm-gen/src/generator/syscalls.rs +++ b/utils/wasm-gen/src/generator/syscalls.rs @@ -43,7 +43,7 @@ pub use imports::*; pub use invocator::*; use gear_wasm_instrument::syscalls::{ - HashType, Ptr, ErrPtr, SyscallName, SyscallSignature, RegularParamType, + ErrPtr, HashType, Ptr, RegularParamType, SyscallName, SyscallSignature, }; /// Type of invocable syscall. @@ -86,7 +86,10 @@ impl InvocableSyscall { // Address of recipient and value (HashWithValue struct) Ptr::HashWithValue(HashType::ActorId).into(), // Pointer to payload - Ptr::SizedBufferStart { length_param_idx: 2 }.into(), + Ptr::SizedBufferStart { + length_param_idx: 2, + } + .into(), // Length of the payload RegularParamType::Length, // Number of blocks to delay the sending for @@ -95,7 +98,8 @@ impl InvocableSyscall { RegularParamType::Gas, // Duration of the reservation RegularParamType::DurationBlockNumber, - ].into(), + ] + .into(), // Address of error returned ErrPtr::ErrorWithHash(HashType::MessageId), )), @@ -104,14 +108,18 @@ impl InvocableSyscall { // Address of value Ptr::Value.into(), // Pointer to payload - Ptr::SizedBufferStart { length_param_idx: 2 }.into(), + Ptr::SizedBufferStart { + length_param_idx: 2, + } + .into(), // Length of the payload RegularParamType::Length, // Amount of gas to reserve RegularParamType::Gas, // Duration of the reservation RegularParamType::DurationBlockNumber, - ].into(), + ] + .into(), // Address of error returned ErrPtr::ErrorWithHash(HashType::MessageId), )), @@ -120,12 +128,16 @@ impl InvocableSyscall { // Address of recipient and value (HashWithValue struct) Ptr::HashWithValue(HashType::ActorId).into(), // Pointer to payload - Ptr::SizedBufferStart { length_param_idx: 2 }.into(), + Ptr::SizedBufferStart { + length_param_idx: 2, + } + .into(), // Length of the payload RegularParamType::Length, // Number of blocks to delay the sending for RegularParamType::DelayBlockNumber, - ].into(), + ] + .into(), // Address of error returned, `ErrorCode` here because underlying syscalls have different error types ErrPtr::ErrorCode, )), @@ -137,26 +149,28 @@ impl InvocableSyscall { RegularParamType::DelayBlockNumber, // Amount of gas to reserve RegularParamType::Gas, - ].into(), + ] + .into(), // Address of error returned, `ErrorCode` here because underlying syscalls have different error types ErrPtr::ErrorCode, )), SyscallName::ReplyDeposit => SyscallSignature::gr_fallible(( [ - // Address of recipient and value (HashWithValue struct). That's needed - // because first `gr_send_input` is invoked and resulting message id is - // used as an input to `gr_reply_deposit`. - Ptr::HashWithValue(HashType::ActorId).into(), - // An offset defining starting index in the received payload (related to `gr_send_input`). - RegularParamType::Offset, - // Length of the slice of the received message payload (related to `gr_send_input`). - RegularParamType::Length, - // Delay (related to `gr_send_input`). - RegularParamType::DelayBlockNumber, - // Amount of gas deposited for a message id got from `gr_send_input`. - // That's an actual input for `gr_reply_deposit` - RegularParamType::Gas, - ].into(), + // Address of recipient and value (HashWithValue struct). That's needed + // because first `gr_send_input` is invoked and resulting message id is + // used as an input to `gr_reply_deposit`. + Ptr::HashWithValue(HashType::ActorId).into(), + // An offset defining starting index in the received payload (related to `gr_send_input`). + RegularParamType::Offset, + // Length of the slice of the received message payload (related to `gr_send_input`). + RegularParamType::Length, + // Delay (related to `gr_send_input`). + RegularParamType::DelayBlockNumber, + // Amount of gas deposited for a message id got from `gr_send_input`. + // That's an actual input for `gr_reply_deposit` + RegularParamType::Gas, + ] + .into(), // Error pointer ErrPtr::ErrorWithHash(HashType::MessageId), )), @@ -187,21 +201,21 @@ impl InvocableSyscall { SyscallName::ReservationSend => { &[SyscallName::ReserveGas, SyscallName::ReservationSend] } - // SyscallName::ReservationReply => { - // &[SyscallName::ReserveGas, SyscallName::ReservationReply] - // } - // SyscallName::SendCommit => &[ - // SyscallName::SendInit, - // SyscallName::SendPush, - // SyscallName::SendCommit, - // ], - // SyscallName::SendCommitWGas => &[ - // SyscallName::Size, - // SyscallName::SendInit, - // SyscallName::SendPushInput, - // SyscallName::SendCommitWGas, - // ], - // SyscallName::ReplyDeposit => &[SyscallName::SendInput, SyscallName::ReplyDeposit], + SyscallName::ReservationReply => { + &[SyscallName::ReserveGas, SyscallName::ReservationReply] + } + SyscallName::SendCommit => &[ + SyscallName::SendInit, + SyscallName::SendPush, + SyscallName::SendCommit, + ], + SyscallName::SendCommitWGas => &[ + SyscallName::Size, + SyscallName::SendInit, + SyscallName::SendPushInput, + SyscallName::SendCommitWGas, + ], + SyscallName::ReplyDeposit => &[SyscallName::SendInput, SyscallName::ReplyDeposit], _ => return None, }) } diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 91f0769294f..db2afe973b8 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -203,12 +203,12 @@ impl<'a, 'b> SyscallsImportsGenerator<'a, 'b> { let precise_syscalls: [( SyscallName, fn(&mut Self, SyscallName) -> Result, - ); 1] = [ + ); 5] = [ (ReservationSend, Self::generate_send_from_reservation), - // (ReservationReply, Self::generate_reply_from_reservation), - // (SendCommit, Self::generate_send_commit), - // (SendCommitWGas, Self::generate_send_commit_with_gas), - // (ReplyDeposit, Self::generate_reply_deposit), + (ReservationReply, Self::generate_reply_from_reservation), + (SendCommit, Self::generate_send_commit), + (SendCommitWGas, Self::generate_send_commit_with_gas), + (ReplyDeposit, Self::generate_reply_deposit), ]; for (precise_syscall, generate_method) in precise_syscalls { diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index 6387b01e191..e6dbda1356e 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -63,7 +63,9 @@ pub(crate) fn process_syscall_params( .iter() .filter_map(|¶m| match param { Regular(Pointer(PtrInfo { - ty: Ptr::SizedBufferStart { length_param_idx } | Ptr::MutSizedBufferStart { length_param_idx }, + ty: + Ptr::SizedBufferStart { length_param_idx } + | Ptr::MutSizedBufferStart { length_param_idx }, .. })) => Some(length_param_idx), _ => None, @@ -87,7 +89,8 @@ pub(crate) fn process_syscall_params( ty: Ptr::SizedBufferStart { .. }, .. })) => ProcessedSyscallParams::MemoryArrayPtr, - Regular(Pointer(_)) => ProcessedSyscallParams::MemoryPtrValue, + // It's guaranteed that fallible syscall has error pointer as a last param. + Regular(Pointer(_)) | Error(_) => ProcessedSyscallParams::MemoryPtrValue, _ => ProcessedSyscallParams::Value { value_type: param.into(), allowed_values: params_config.get_rule(¶m), @@ -445,7 +448,7 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> { // It's guaranteed here that infallible has no errors to process // as it has not mut err pointers or error indicating values returned. Vec::new() - }, + } signature @ (SyscallSignature::GrFallible(_) | SyscallSignature::System(_)) => { // It's guaranteed by definition that these variants return an error either by returning // error indicating value or by having err mut pointer in params. @@ -454,7 +457,7 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> { } else { Self::build_error_processing_ignored(signature) } - }, + } }; instructions.append(&mut result_processing); @@ -575,7 +578,10 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> { Ok(setters) } - fn build_error_processing(signature: SyscallSignature, param_setters: Vec) -> Vec { + fn build_error_processing( + signature: SyscallSignature, + param_setters: Vec, + ) -> Vec { match signature { SyscallSignature::GrFallible(fallible) => { // TODO: #3129. @@ -586,7 +592,11 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> { static_assertions::assert_eq_size!(gsys::ErrorCode, u32); let no_error_val = gsys::ErrorCode::default() as i32; - assert_eq!(fallible.params().len(), param_setters.len(), "ParamsSetter is inconsistent with syscall params."); + assert_eq!( + fallible.params().len(), + param_setters.len(), + "ParamsSetter is inconsistent with syscall params." + ); if let Some(res_ptr) = param_setters .last() .expect("At least one argument in fallible syscall") @@ -604,47 +614,49 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> { } else { panic!("Incorrect last parameter type: expected pointer"); } - }, - SyscallSignature::System(system) => { - // That's basically those syscalls, that doesn't have an error pointer, - // but return value indicating error. These are currently `Alloc` and `Free`. - assert_eq!(system.results().len(), 1); - - let error_code = match system.params()[0] { - ParamType::Regular(RegularParamType::Alloc) => { - // Alloc syscall: returns u32::MAX (= -1i32) in case of error. - -1 - } - ParamType::Regular(RegularParamType::Free) => { - // Free syscall: returns 1 in case of error. - 1 - } - _ => { - unimplemented!("Only alloc and free are supported for now") - } - }; + } + SyscallSignature::System(system) => { + // That's basically those syscalls, that doesn't have an error pointer, + // but return value indicating error. These are currently `Alloc` and `Free`. + assert_eq!(system.results().len(), 1); + + let error_code = match system.params()[0] { + ParamType::Regular(RegularParamType::Alloc) => { + // Alloc syscall: returns u32::MAX (= -1i32) in case of error. + -1 + } + ParamType::Regular(RegularParamType::Free) => { + // Free syscall: returns 1 in case of error. + 1 + } + _ => { + unimplemented!("Only alloc and free are supported for now") + } + }; - vec![ - Instruction::I32Const(error_code), - Instruction::I32Eq, - Instruction::If(BlockType::NoResult), - Instruction::Unreachable, - Instruction::End, - ] - }, - SyscallSignature::GrInfallible(_) => unreachable!("Invalid implementation. This function is called only for returning errors syscall"), + vec![ + Instruction::I32Const(error_code), + Instruction::I32Eq, + Instruction::If(BlockType::NoResult), + Instruction::Unreachable, + Instruction::End, + ] + } + SyscallSignature::GrInfallible(_) => unreachable!( + "Invalid implementation. This function is called only for returning errors syscall" + ), } } fn build_error_processing_ignored(signature: SyscallSignature) -> Vec { match signature { - SyscallSignature::System(system) => { - iter::repeat(Instruction::Drop) - .take(system.results().len()) - .collect() - }, + SyscallSignature::System(system) => iter::repeat(Instruction::Drop) + .take(system.results().len()) + .collect(), SyscallSignature::GrFallible(_) => Vec::new(), - SyscallSignature::GrInfallible(_) => unreachable!("Invalid implementation. This function is called only for returning errors syscall"), + SyscallSignature::GrInfallible(_) => unreachable!( + "Invalid implementation. This function is called only for returning errors syscall" + ), } } diff --git a/utils/wasm-instrument/src/syscalls.rs b/utils/wasm-instrument/src/syscalls.rs index 6c94be1e297..dfe7a354683 100644 --- a/utils/wasm-instrument/src/syscalls.rs +++ b/utils/wasm-instrument/src/syscalls.rs @@ -19,10 +19,10 @@ //! Gear syscalls for smart contracts execution signatures. use crate::parity_wasm::elements::{FunctionType, ValueType}; -use alloc::{collections::BTreeSet, vec::Vec, borrow::ToOwned}; +use alloc::{borrow::ToOwned, collections::BTreeSet, vec::Vec}; +use core::iter; use enum_iterator::{self, Sequence}; pub use pointers::*; -use core::iter; /// All available syscalls. /// diff --git a/utils/wasm-instrument/src/tests.rs b/utils/wasm-instrument/src/tests.rs index 1c0a504981f..575114d223f 100644 --- a/utils/wasm-instrument/src/tests.rs +++ b/utils/wasm-instrument/src/tests.rs @@ -19,7 +19,7 @@ use super::*; use crate::{ rules::CustomConstantCostRules, - syscalls::{ParamType, PtrInfo, PtrType, SyscallName}, + syscalls::{ParamType::*, Ptr, PtrInfo, RegularParamType::*, SyscallName}, }; use alloc::format; use elements::Instruction::*; @@ -624,18 +624,18 @@ fn check_memory_array_pointers_definition_correctness() { for syscall in syscalls { let signature = syscall.signature(); let size_param_indexes = signature - .params + .params() .iter() .filter_map(|param_ty| match param_ty { - ParamType::Ptr(PtrInfo { - ty: PtrType::SizedBufferStart { length_param_idx }, + Regular(Pointer(PtrInfo { + ty: Ptr::SizedBufferStart { length_param_idx }, .. - }) => Some(*length_param_idx), + })) => Some(*length_param_idx), _ => None, }); for idx in size_param_indexes { - assert_eq!(signature.params.get(idx), Some(&ParamType::Length)); + assert_eq!(signature.params().get(idx), Some(&Regular(Length))); } } } @@ -648,12 +648,10 @@ fn check_syscall_err_ptr_position() { if syscall.is_fallible() { let signature = syscall.signature(); let err_ptr = signature - .params + .params() .last() .expect("fallible syscall has at least err ptr"); - assert!( - matches!(err_ptr, ParamType::Ptr(PtrInfo { mutable: true, ty }) if ty.is_error()) - ); + assert!(matches!(err_ptr, Error(PtrInfo { mutable: true, .. }))); } } }