Skip to content

Commit

Permalink
Apply changes to wasm-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
techraed committed Dec 4, 2023
1 parent 8d46823 commit da4d20d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 153 deletions.
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config/syscalls/injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl SyscallsInjectionTypes {
}
}

/// Imports the given syscall, if necessary.
/// Imports the given syscall, if possible.
pub(crate) fn enable_syscall_import(&mut self, name: InvocableSyscall) {
if let Some(injection_type @ SyscallInjectionType::None) = self.0.get_mut(&name) {
*injection_type = SyscallInjectionType::Import;
Expand Down
42 changes: 24 additions & 18 deletions utils/wasm-gen/src/config/syscalls/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::DEFAULT_INITIAL_SIZE;
use arbitrary::{Result, Unstructured};
use std::{collections::HashMap, ops::RangeInclusive};

pub use gear_wasm_instrument::syscalls::ParamType;
pub use gear_wasm_instrument::syscalls::{ParamType, RegularParamType};

/// Syscalls params config.
///
Expand All @@ -50,17 +50,20 @@ impl SyscallsParamsConfig {

/// New [`SyscallsParamsConfig`] with all rules set to produce one constant value.
pub fn all_constant_value(value: i64) -> Self {
use ParamType::*;
use RegularParamType::*;

let allowed_values: SyscallParamAllowedValues = (value..=value).into();
Self(
[
ParamType::Length,
ParamType::Gas,
ParamType::Offset,
ParamType::DurationBlockNumber,
ParamType::DelayBlockNumber,
ParamType::Handler,
ParamType::Free,
ParamType::Version,
Regular(Length),
Regular(Gas),
Regular(Offset),
Regular(DurationBlockNumber),
Regular(DelayBlockNumber),
Regular(Handler),
Regular(Free),
Regular(Version),
]
.into_iter()
.map(|param_type| (param_type, allowed_values.clone()))
Expand All @@ -75,7 +78,7 @@ impl SyscallsParamsConfig {

/// Set allowed values for the `param`.
pub fn add_rule(&mut self, param: ParamType, allowed_values: SyscallParamAllowedValues) {
matches!(param, ParamType::Ptr(..))
matches!(param, ParamType::Regular(RegularParamType::Pointer(_)))
.then(|| panic!("ParamType::Ptr(..) isn't supported in SyscallsParamsConfig"));

self.0.insert(param, allowed_values);
Expand All @@ -84,20 +87,23 @@ impl SyscallsParamsConfig {

impl Default for SyscallsParamsConfig {
fn default() -> Self {
use ParamType::*;
use RegularParamType::*;

let free_start = DEFAULT_INITIAL_SIZE as i64;
let free_end = free_start + 5;
Self(
[
(ParamType::Length, (0..=0x10000).into()),
(Regular(Length), (0..=0x10000).into()),
// There are no rules for memory arrays and pointers as they are chosen
// in accordance to memory pages config.
(ParamType::Gas, (0..=250_000_000_000).into()),
(ParamType::Offset, (0..=10).into()),
(ParamType::DurationBlockNumber, (1..=8).into()),
(ParamType::DelayBlockNumber, (0..=4).into()),
(ParamType::Handler, (0..=100).into()),
(ParamType::Free, (free_start..=free_end).into()),
(ParamType::Version, (1..=1).into()),
(Regular(Gas), (0..=250_000_000_000).into()),
(Regular(Offset), (0..=10).into()),
(Regular(DurationBlockNumber), (1..=8).into()),
(Regular(DelayBlockNumber), (0..=4).into()),
(Regular(Handler), (0..=100).into()),
(Regular(Free), (free_start..=free_end).into()),
(Regular(Version), (1..=1).into()),
]
.into_iter()
.collect(),
Expand Down
182 changes: 90 additions & 92 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, ParamType, PtrInfo, PtrType, SyscallName, SyscallSignature,
HashType, Ptr, ErrPtr, SyscallName, SyscallSignature, RegularParamType,
};

/// Type of invocable syscall.
Expand Down Expand Up @@ -81,95 +81,85 @@ impl InvocableSyscall {
match self {
InvocableSyscall::Loose(name) => name.signature(),
InvocableSyscall::Precise(name) => match name {
SyscallName::ReservationSend => SyscallSignature::gr_fallible([
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue(
HashType::ActorId,
))),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::SizedBufferStart {
length_param_idx: 2,
})),
// Length of the payload
ParamType::Length,
// Number of blocks to delay the sending for
ParamType::DelayBlockNumber,
// Amount of gas to reserve
ParamType::Gas,
// Duration of the reservation
ParamType::DurationBlockNumber,
SyscallName::ReservationSend => SyscallSignature::gr_fallible((
[
// Address of recipient and value (HashWithValue struct)
Ptr::HashWithValue(HashType::ActorId).into(),
// Pointer to payload
Ptr::SizedBufferStart { length_param_idx: 2 }.into(),
// Length of the payload
RegularParamType::Length,
// Number of blocks to delay the sending for
RegularParamType::DelayBlockNumber,
// Amount of gas to reserve
RegularParamType::Gas,
// Duration of the reservation
RegularParamType::DurationBlockNumber,
].into(),
// Address of error returned
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorWithHash(
HashType::MessageId,
))),
]),
SyscallName::ReservationReply => SyscallSignature::gr_fallible([
// Address of value
ParamType::Ptr(PtrInfo::new_immutable(PtrType::Value)),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::SizedBufferStart {
length_param_idx: 2,
})),
// Length of the payload
ParamType::Length,
// Amount of gas to reserve
ParamType::Gas,
// Duration of the reservation
ParamType::DurationBlockNumber,
ErrPtr::ErrorWithHash(HashType::MessageId),
)),
SyscallName::ReservationReply => SyscallSignature::gr_fallible((
[
// Address of value
Ptr::Value.into(),
// Pointer to payload
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(),
// Address of error returned
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorWithHash(
HashType::MessageId,
))),
]),
SyscallName::SendCommit => SyscallSignature::gr_fallible([
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue(
HashType::ActorId,
))),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::SizedBufferStart {
length_param_idx: 2,
})),
// Length of the payload
ParamType::Length,
// Number of blocks to delay the sending for
ParamType::DelayBlockNumber,
ErrPtr::ErrorWithHash(HashType::MessageId),
)),
SyscallName::SendCommit => SyscallSignature::gr_fallible((
[
// Address of recipient and value (HashWithValue struct)
Ptr::HashWithValue(HashType::ActorId).into(),
// Pointer to payload
Ptr::SizedBufferStart { length_param_idx: 2 }.into(),
// Length of the payload
RegularParamType::Length,
// Number of blocks to delay the sending for
RegularParamType::DelayBlockNumber,
].into(),
// Address of error returned, `ErrorCode` here because underlying syscalls have different error types
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorCode)),
]),
SyscallName::SendCommitWGas => SyscallSignature::gr_fallible([
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue(
HashType::ActorId,
))),
// Number of blocks to delay the sending for
ParamType::DelayBlockNumber,
// Amount of gas to reserve
ParamType::Gas,
ErrPtr::ErrorCode,
)),
SyscallName::SendCommitWGas => SyscallSignature::gr_fallible((
[
// Address of recipient and value (HashWithValue struct)
Ptr::HashWithValue(HashType::ActorId).into(),
// Number of blocks to delay the sending for
RegularParamType::DelayBlockNumber,
// Amount of gas to reserve
RegularParamType::Gas,
].into(),
// Address of error returned, `ErrorCode` here because underlying syscalls have different error types
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorCode)),
]),
SyscallName::ReplyDeposit => SyscallSignature::gr_fallible([
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`.
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue(
HashType::ActorId,
))),
Ptr::HashWithValue(HashType::ActorId).into(),
// An offset defining starting index in the received payload (related to `gr_send_input`).
ParamType::Offset,
RegularParamType::Offset,
// Length of the slice of the received message payload (related to `gr_send_input`).
ParamType::Length,
RegularParamType::Length,
// Delay (related to `gr_send_input`).
ParamType::DelayBlockNumber,
RegularParamType::DelayBlockNumber,
// Amount of gas deposited for a message id got from `gr_send_input`.
// That's an actual input for `gr_reply_deposit`
ParamType::Gas,
RegularParamType::Gas,
].into(),
// Error pointer
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorWithHash(
HashType::MessageId,
))),
]),
ErrPtr::ErrorWithHash(HashType::MessageId),
)),
_ => unimplemented!(),
},
}
Expand Down Expand Up @@ -197,21 +187,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 Expand Up @@ -249,4 +239,12 @@ impl InvocableSyscall {
InvocableSyscall::Precise(syscall) => syscall.returns_error(),
}
}

#[cfg(test)]
pub(crate) fn is_fallible(&self) -> bool {
match self {
InvocableSyscall::Loose(syscall) => syscall.is_fallible(),
InvocableSyscall::Precise(syscall) => syscall.is_fallible(),
}
}
}
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>,
); 5] = [
); 1] = [
(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
Loading

0 comments on commit da4d20d

Please sign in to comment.