Skip to content

Commit

Permalink
Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Oct 2, 2023
1 parent 1feb3d2 commit d14cf2f
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 111 deletions.
76 changes: 53 additions & 23 deletions utils/wasm-gen/src/generator/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use additional_data::*;
pub use imports::*;
pub use invocator::*;

use gear_wasm_instrument::syscalls::{ParamType, SysCallName, SysCallSignature};
use gear_wasm_instrument::syscalls::{ParamType, PtrInfo, PtrType, SysCallName, SysCallSignature};

/// Type of invocable sys-call.
///
Expand Down Expand Up @@ -79,34 +79,64 @@ impl InvocableSysCall {
InvocableSysCall::Loose(name) => name.signature(),
InvocableSysCall::Precise(name) => match name {
SysCallName::ReservationSend => SysCallSignature::gr([
ParamType::Ptr(None), // Address of recipient and value (HashWithValue struct)
ParamType::Ptr(Some(2)), // Pointer to payload
ParamType::Size, // Size of the payload
ParamType::Delay, // Number of blocks to delay the sending for
ParamType::Gas, // Amount of gas to reserve
ParamType::Duration, // Duration of the reservation
ParamType::Ptr(None), // Address of error returned
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue)),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::BufferStart {
length_param_idx: 2,
})),
// Size of the payload
ParamType::Size,
// Number of blocks to delay the sending for
ParamType::Delay,
// Amount of gas to reserve
ParamType::Gas,
// Duration of the reservation
ParamType::Duration,
// Address of error returned
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorWithHash)),
]),
SysCallName::ReservationReply => SysCallSignature::gr([
ParamType::Ptr(None), // Address of value
ParamType::Ptr(Some(2)), // Pointer to payload
ParamType::Size, // Size of the payload
ParamType::Gas, // Amount of gas to reserve
ParamType::Duration, // Duration of the reservation
ParamType::Ptr(None), // Address of error returned
// Address of value
ParamType::Ptr(PtrInfo::new_immutable(PtrType::Value)),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::BufferStart {
length_param_idx: 2,
})),
// Size of the payload
ParamType::Size,
// Amount of gas to reserve
ParamType::Gas,
// Duration of the reservation
ParamType::Duration,
// Address of error returned
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorWithHash)),
]),
SysCallName::SendCommit => SysCallSignature::gr([
ParamType::Ptr(None), // Address of recipient and value (HashWithValue struct)
ParamType::Ptr(Some(2)), // Pointer to payload
ParamType::Size, // Size of the payload
ParamType::Delay, // Number of blocks to delay the sending for
ParamType::Ptr(None), // Address of error returned
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue)),
// Address of value
ParamType::Ptr(PtrInfo::new_immutable(PtrType::Value)),
// Pointer to payload
ParamType::Ptr(PtrInfo::new_immutable(PtrType::BufferStart {
length_param_idx: 2,
})),
// Size of the payload
ParamType::Size,
// Number of blocks to delay the sending for
ParamType::Delay,
// Address of error returned, `ErrorCode` here because underlying syscalls have different error types
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorCode)),
]),
SysCallName::SendCommitWGas => SysCallSignature::gr([
ParamType::Ptr(None), // Address of recipient and value (HashWithValue struct)
ParamType::Delay, // Number of blocks to delay the sending for
ParamType::Gas, // Amount of gas to reserve
ParamType::Ptr(None), // Address of error returned
// Address of recipient and value (HashWithValue struct)
ParamType::Ptr(PtrInfo::new_immutable(PtrType::HashWithValue)),
// Number of blocks to delay the sending for
ParamType::Delay,
// Amount of gas to reserve
ParamType::Gas,
// Address of error returned, `ErrorCode` here because underlying syscalls have different error types
ParamType::Ptr(PtrInfo::new_mutable(PtrType::ErrorCode)),
]),
_ => unimplemented!(),
},
Expand Down
26 changes: 14 additions & 12 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
use arbitrary::{Result, Unstructured};
use gear_wasm_instrument::{
parity_wasm::elements::{BlockType, Instruction, Internal, ValueType},
syscalls::{ParamType, SysCallName, SysCallSignature},
syscalls::{ParamType, PtrInfo, PtrType, SysCallName, SysCallSignature},
};
use std::{
collections::{btree_map::Entry, BTreeMap, BinaryHeap},
Expand Down Expand Up @@ -64,16 +64,18 @@ pub(crate) fn process_sys_call_params(
ParamType::Alloc => ProcessedSysCallParams::Alloc {
allowed_values: params_config.get_rule(&param),
},
ParamType::Ptr(maybe_idx) => maybe_idx
.map(|_| {
// skipping next as we don't need the following `Size` param,
// because it will be chosen in accordance to the wasm module
// memory pages config.
skip_next_param = true;

ProcessedSysCallParams::MemoryArray
})
.unwrap_or(ProcessedSysCallParams::MemoryPtrValue),
ParamType::Ptr(PtrInfo {
ty: PtrType::BufferStart { .. },
..
}) => {
// skipping next as we don't need the following `Size` param,
// because it will be chosen in accordance to the wasm module
// memory pages config.
skip_next_param = true;

ProcessedSysCallParams::MemoryArray
}
ParamType::Ptr(_) => ProcessedSysCallParams::MemoryPtrValue,
_ => ProcessedSysCallParams::Value {
value_type: param.into(),
allowed_values: params_config.get_rule(&param),
Expand Down Expand Up @@ -565,7 +567,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {
params
.last()
.expect("The last argument of fallible syscall must be pointer to error code"),
ParamType::Ptr(None)
ParamType::Ptr(_)
));
assert_eq!(params.len(), param_setters.len());

Expand Down
Loading

0 comments on commit d14cf2f

Please sign in to comment.