Skip to content

Commit

Permalink
refactor(wasm-gen): add new approach of processing syscall parameters (
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on authored Oct 23, 2023
1 parent 2b53bfc commit de82fac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
71 changes: 51 additions & 20 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,43 @@ pub(crate) enum ProcessedSysCallParams {
value_type: ValueType,
allowed_values: Option<SysCallParamAllowedValues>,
},
MemoryArray,
MemoryArraySize,
MemoryArrayPtr,
MemoryPtrValue,
}

pub(crate) fn process_syscall_params(
params: &[ParamType],
params_config: &SysCallsParamsConfig,
) -> Vec<ProcessedSysCallParams> {
let length_param_indexes = params
.iter()
.filter_map(|&param| match param {
ParamType::Ptr(PtrInfo {
ty: PtrType::BufferStart { length_param_idx },
..
}) => Some(length_param_idx),
_ => None,
})
.collect::<HashSet<_>>();

let mut res = Vec::with_capacity(params.len());
let mut skip_next_param = false;
for &param in params {
if skip_next_param {
skip_next_param = false;
continue;
}
for (param_idx, &param) in params.iter().enumerate() {
let processed_param = match param {
ParamType::Alloc => ProcessedSysCallParams::Alloc {
allowed_values: params_config.get_rule(&param),
},
ParamType::Size if length_param_indexes.contains(&param_idx) => {
// Due to match guard `ParamType::Size` can be processed in two ways:
// 1. The function will return `ProcessedSysCallParams::MemoryArraySize`
// if this parameter is associated with PtrType::BufferStart { .. }`.
// 2. Otherwise, `ProcessedSysCallParams::Value` will be returned from the function.
ProcessedSysCallParams::MemoryArraySize
}
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
}
}) => ProcessedSysCallParams::MemoryArrayPtr,
ParamType::Ptr(_) => ProcessedSysCallParams::MemoryPtrValue,
_ => ProcessedSysCallParams::Value {
value_type: param.into(),
Expand Down Expand Up @@ -450,6 +457,8 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {
let mem_size = Into::<WasmPageCount>::into(mem_size_pages).memory_size();

let mut setters = Vec::with_capacity(params.len());
let mut memory_array_definition: Option<(i32, Option<i32>)> = None;

for processed_param in process_syscall_params(params, self.config.params_config()) {
match processed_param {
ProcessedSysCallParams::Alloc { allowed_values } => {
Expand Down Expand Up @@ -491,16 +500,38 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {

setters.push(setter);
}
ProcessedSysCallParams::MemoryArray => {
ProcessedSysCallParams::MemoryArraySize => {
let length;
let upper_limit = mem_size.saturating_sub(1) as i32;

let offset = self.unstructured.int_in_range(0..=upper_limit)?;
let length = self.unstructured.int_in_range(0..=(upper_limit - offset))?;
(memory_array_definition, length) = if let Some((offset, _)) =
memory_array_definition
{
let length = self.unstructured.int_in_range(0..=(upper_limit - offset))?;
(None, length)
} else {
let offset = self.unstructured.int_in_range(0..=upper_limit)?;
let length = self.unstructured.int_in_range(0..=(upper_limit - offset))?;
(Some((offset, Some(length))), length)
};

log::trace!(" ---- Memory array length - {length}");
setters.push(ParamSetter::new_i32(length));
}
ProcessedSysCallParams::MemoryArrayPtr => {
let offset;
let upper_limit = mem_size.saturating_sub(1) as i32;

log::trace!(" ---- Memory array {offset}, {length}");
(memory_array_definition, offset) =
if let Some((offset, _)) = memory_array_definition {
(None, offset)
} else {
let offset = self.unstructured.int_in_range(0..=upper_limit)?;
(Some((offset, None)), offset)
};

log::trace!(" ---- Memory array offset - {offset}");
setters.push(ParamSetter::new_i32(offset));
setters.push(ParamSetter::new_i32(length));
}
ProcessedSysCallParams::MemoryPtrValue => {
// Subtract a bit more so entities from `gsys` fit.
Expand Down
9 changes: 3 additions & 6 deletions utils/wasm-instrument/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,9 @@ impl SysCallName {
Self::Read => SysCallSignature::gr([
MessagePosition,
Size,
// TODO #3375, the PtrType::BlockNumber is incorrect here.
// Should be:
// Ptr(PtrInfo::new_mutable(PtrType::BufferStart {
// length_param_idx: 1,
// }))
Ptr(PtrInfo::new_mutable(PtrType::BlockNumber)),
Ptr(PtrInfo::new_mutable(PtrType::BufferStart {
length_param_idx: 1,
})),
Ptr(PtrInfo::new_mutable(PtrType::ErrorCode)),
]),
Self::Reply => SysCallSignature::gr([
Expand Down

0 comments on commit de82fac

Please sign in to comment.