Skip to content

Commit

Permalink
refactor(wasm-gen): Avoid inserting sys-calls into custom `send_from_…
Browse files Browse the repository at this point in the history
…reservation` function (#3036)
  • Loading branch information
techraed authored Aug 8, 2023
1 parent 8f0cf04 commit b68c27b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
27 changes: 26 additions & 1 deletion utils/wasm-gen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use crate::{utils, GearWasmGeneratorConfig, WasmModule};
use arbitrary::{Result, Unstructured};
use gear_wasm_instrument::parity_wasm::elements::Module;
use std::collections::HashSet;

mod entry_points;
mod memory;
Expand Down Expand Up @@ -208,6 +209,20 @@ type CallIndexesHandle = usize;
/// and internal functions.
struct CallIndexes {
inner: Vec<FunctionIndex>,
/// Indexes of wasm-module functions which were newly generated.
///
/// These are indexes of functions which aren't generated from
/// `wasm-smith` but from the current crate generators. All gear
/// entry points ([`EntryPointsGenerator`]) and custom reservation send
/// function (generated in [`SysCallsImportsGenerator`]) are considered
/// to be "custom" functions.
///
/// Separating "pre-defined" functions from newly generated ones is important
/// when sys-calls invocator inserts calls of generated sys-calls. For example,
/// calls must not be inserted in the custom function, which perofrms `gr_reservation_send`,
/// not to pollute it's internal instructions structure which is defined such that
/// semantically correct `gr_reservation_send` call is performed.
custom_funcs: HashSet<usize>,
}

impl CallIndexes {
Expand All @@ -222,19 +237,29 @@ impl CallIndexes {
inner.push(FunctionIndex::Func(i as u32));
}

Self { inner }
Self {
inner,
custom_funcs: HashSet::new(),
}
}

pub(crate) fn get(&self, handle_idx: CallIndexesHandle) -> Option<FunctionIndex> {
self.inner.get(handle_idx).copied()
}

fn is_custom_func(&self, idx: usize) -> bool {
self.custom_funcs.contains(&idx)
}

fn len(&self) -> usize {
self.inner.len()
}

fn add_func(&mut self, func_idx: usize) {
self.inner.push(FunctionIndex::Func(func_idx as u32));
let is_new = self.custom_funcs.insert(func_idx);

debug_assert!(is_new, "same inner index is used");
}

fn add_import(&mut self, import_idx: usize) {
Expand Down
25 changes: 17 additions & 8 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'a> SysCallsInvocator<'a> {
fn build_sys_call_invoke_instructions(
&mut self,
invocable: InvocableSysCall,
call_indexes_handle: usize,
call_indexes_handle: CallIndexesHandle,
) -> Result<Vec<Instruction>> {
let name = invocable.name();
let signature = invocable.into_signature();
Expand Down Expand Up @@ -233,7 +233,7 @@ impl<'a> SysCallsInvocator<'a> {
&mut self,
params: &[ParamType],
results: &[ValueType],
call_indexes_handle: usize,
call_indexes_handle: CallIndexesHandle,
) -> Result<Vec<Instruction>> {
let results = results.iter().map(|_| Instruction::Drop);

Expand Down Expand Up @@ -309,6 +309,12 @@ impl<'a> SysCallsInvocator<'a> {
let last_funcs_idx = self.module.count_code_funcs() - 1;
let insert_into_func_no = self.unstructured.int_in_range(0..=last_funcs_idx)?;

// Do not insert into custom newly generated function, but only into pre-defined
// internal functions.
if self.call_indexes.is_custom_func(insert_into_func_no) {
return Ok(());
}

self.module.with(|mut module| {
let code = module
.code_section_mut()
Expand All @@ -317,12 +323,15 @@ impl<'a> SysCallsInvocator<'a> {
.code_mut()
.elements_mut();

let res = self
.unstructured
.int_in_range(0..=code.len() - 1)
.map(|pos| {
code.splice(pos..pos, instructions.iter().cloned());
});
// The end of insertion range is second-to-last index, as the last
// index is defined for `Instruction::End` of the function body.
// But if there's only one instruction in the function, then `0`
// index is used as an insertion point.
let last = if code.len() > 1 { code.len() - 2 } else { 0 };

let res = self.unstructured.int_in_range(0..=last).map(|pos| {
code.splice(pos..pos, instructions.iter().cloned());
});

(module, res)
})
Expand Down

0 comments on commit b68c27b

Please sign in to comment.