Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
techraed committed Dec 4, 2023
1 parent da4d20d commit 3c4836c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 102 deletions.
11 changes: 7 additions & 4 deletions utils/node-loader/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}'")),
Expand Down
14 changes: 10 additions & 4 deletions utils/runtime-fuzzer/src/gear_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
88 changes: 51 additions & 37 deletions utils/wasm-gen/src/generator/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -95,7 +98,8 @@ impl InvocableSyscall {
RegularParamType::Gas,
// Duration of the reservation
RegularParamType::DurationBlockNumber,
].into(),
]
.into(),
// Address of error returned
ErrPtr::ErrorWithHash(HashType::MessageId),
)),
Expand All @@ -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),
)),
Expand All @@ -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,
)),
Expand All @@ -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),
)),
Expand Down Expand Up @@ -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,
})
}
Expand Down
10 changes: 5 additions & 5 deletions utils/wasm-gen/src/generator/syscalls/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ impl<'a, 'b> SyscallsImportsGenerator<'a, 'b> {
let precise_syscalls: [(
SyscallName,
fn(&mut Self, SyscallName) -> Result<CallIndexesHandle, PreciseSyscallError>,
); 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 {
Expand Down
92 changes: 52 additions & 40 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub(crate) fn process_syscall_params(
.iter()
.filter_map(|&param| 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,
Expand All @@ -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(&param),
Expand Down Expand Up @@ -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.
Expand All @@ -454,7 +457,7 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> {
} else {
Self::build_error_processing_ignored(signature)
}
},
}
};
instructions.append(&mut result_processing);

Expand Down Expand Up @@ -575,7 +578,10 @@ impl<'a, 'b> SyscallsInvocator<'a, 'b> {
Ok(setters)
}

fn build_error_processing(signature: SyscallSignature, param_setters: Vec<ParamSetter>) -> Vec<Instruction> {
fn build_error_processing(
signature: SyscallSignature,
param_setters: Vec<ParamSetter>,
) -> Vec<Instruction> {
match signature {
SyscallSignature::GrFallible(fallible) => {
// TODO: #3129.
Expand All @@ -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")
Expand All @@ -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<Instruction> {
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"
),
}
}

Expand Down
4 changes: 2 additions & 2 deletions utils/wasm-instrument/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
Loading

0 comments on commit 3c4836c

Please sign in to comment.