diff --git a/utils/wasm-gen/src/config.rs b/utils/wasm-gen/src/config.rs index 1df920dca05..2ec76ebfeb7 100644 --- a/utils/wasm-gen/src/config.rs +++ b/utils/wasm-gen/src/config.rs @@ -153,7 +153,7 @@ pub struct StandardGearWasmConfigsBundle { pub initial_pages: u32, /// Optional stack end pages. pub stack_end_page: Option, - /// Sys-calls params config + /// Syscalls params config pub params_config: SysCallsParamsConfig, /// Flag which signals whether `unreachable` instruction must be used /// during wasm generation. diff --git a/utils/wasm-gen/src/config/generator.rs b/utils/wasm-gen/src/config/generator.rs index 8d068e45837..2c1da46750f 100644 --- a/utils/wasm-gen/src/config/generator.rs +++ b/utils/wasm-gen/src/config/generator.rs @@ -76,7 +76,7 @@ pub struct GearWasmGeneratorConfig { pub memory_config: MemoryPagesConfig, /// Entry points config. pub entry_points_config: EntryPointsSet, - /// Sys-calls generator module config. + /// Syscalls generator module config. pub syscalls_config: SysCallsConfig, /// Flag, signalizing whether recursions /// should be removed from resulting module. diff --git a/utils/wasm-gen/src/config/syscalls.rs b/utils/wasm-gen/src/config/syscalls.rs index c8e3f03d9d9..143f0ec826f 100644 --- a/utils/wasm-gen/src/config/syscalls.rs +++ b/utils/wasm-gen/src/config/syscalls.rs @@ -188,7 +188,7 @@ impl SysCallsConfig { } } -/// Sys-call destination choice. +/// Syscall destination choice. /// /// `gr_send*` and `gr_exit` syscalls generated from this crate can be sent /// to different destination in accordance to the config. diff --git a/utils/wasm-gen/src/config/syscalls/injection.rs b/utils/wasm-gen/src/config/syscalls/injection.rs index d415ff24796..f75d62941fd 100644 --- a/utils/wasm-gen/src/config/syscalls/injection.rs +++ b/utils/wasm-gen/src/config/syscalls/injection.rs @@ -32,7 +32,7 @@ use std::{collections::HashMap, ops::RangeInclusive}; pub enum SysCallInjectionType { /// Don't modify wasm module at all. None, - /// Sys-call import will be injected into import section of wasm module, + /// Syscall import will be injected into import section of wasm module, /// but the `wasm-gen` generators will not call that syscall. /// /// It should be used in cases where you don't need to invoke an actual syscall. diff --git a/utils/wasm-gen/src/config/syscalls/param.rs b/utils/wasm-gen/src/config/syscalls/param.rs index 758931df12c..875c6e7c9fb 100644 --- a/utils/wasm-gen/src/config/syscalls/param.rs +++ b/utils/wasm-gen/src/config/syscalls/param.rs @@ -26,7 +26,7 @@ use std::{collections::HashMap, ops::RangeInclusive}; pub use gear_wasm_instrument::syscalls::ParamType; -/// Sys-calls params config. +/// Syscalls params config. /// /// This is basically a map, which creates a relationship between each kind of /// param, that a syscall can have, and allowed values ("rules") for each of diff --git a/utils/wasm-gen/src/generator/syscalls.rs b/utils/wasm-gen/src/generator/syscalls.rs index 788fb96e994..31f2bb3968e 100644 --- a/utils/wasm-gen/src/generator/syscalls.rs +++ b/utils/wasm-gen/src/generator/syscalls.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Sys-calls generators entities. +//! Syscalls generators entities. //! //! Generators from this module form a state machine: //! ```text diff --git a/utils/wasm-gen/src/generator/syscalls/additional_data.rs b/utils/wasm-gen/src/generator/syscalls/additional_data.rs index d1c14112845..f7a72917f78 100644 --- a/utils/wasm-gen/src/generator/syscalls/additional_data.rs +++ b/utils/wasm-gen/src/generator/syscalls/additional_data.rs @@ -31,7 +31,7 @@ use gear_wasm_instrument::{ parity_wasm::{builder, elements::Instruction}, syscalls::SysCallName, }; -use std::{collections::BTreeMap, iter::Cycle, vec::IntoIter, num::NonZeroU32}; +use std::{collections::BTreeMap, iter::Cycle, num::NonZeroU32, vec::IntoIter}; /// Cycled iterator over wasm module data offsets. /// @@ -271,7 +271,8 @@ pub struct DisabledAdditionalDataInjector<'a, 'b> { pub(super) unstructured: &'b mut Unstructured<'a>, pub(super) module: WasmModule, pub(super) call_indexes: CallIndexes, - pub(super) syscalls_imports: BTreeMap, CallIndexesHandle)>, + pub(super) syscalls_imports: + BTreeMap, CallIndexesHandle)>, pub(super) config: SysCallsConfig, } diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 71f45dbcd83..47ab2e1fce1 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Sys-calls imports generator module. +//! Syscalls imports generator module. use crate::{ generator::{ @@ -46,7 +46,7 @@ pub struct SysCallsImportsGenerator<'a, 'b> { syscalls_imports: BTreeMap, CallIndexesHandle)>, } -/// Sys-calls imports generator instantiator. +/// Syscalls imports generator instantiator. /// /// Serves as a new type in order to create the generator from gear wasm generator and proofs. pub struct SysCallsImportsGeneratorInstantiator<'a, 'b>( @@ -218,7 +218,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { let precise_syscall_amount = NonZeroU32::new(self.unstructured.int_in_range(syscall_amount_range)?); log::trace!( - "Constructing {name} syscall...", + "Constructing `{name}` syscall...", name = InvocableSysCall::Precise(precise_syscall).to_str() ); @@ -278,9 +278,9 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { // if `SysCallInjectionType::Function(syscall_amount_range)` yielded zero. let call_indexes_handle = self.insert_syscall_import(syscall); log::trace!( - " -- Generated {} amount of {} syscall", + " -- Syscall `{}` will be invoked {} times", + syscall.to_str(), syscall_amount, - syscall.to_str() ); Ok(Some((NonZeroU32::new(syscall_amount), call_indexes_handle))) diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index 563fd7c38aa..0740a110776 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Sys-calls invocator module. +//! Syscalls invocator module. use crate::{ generator::{ @@ -32,7 +32,7 @@ use gear_wasm_instrument::{ syscalls::{ParamType, PtrInfo, PtrType, SysCallName, SysCallSignature}, }; use std::{ - collections::{btree_map::Entry, BTreeMap, BinaryHeap}, + collections::{btree_map::Entry, BTreeMap, BinaryHeap, HashSet}, iter, num::NonZeroU32, }; @@ -89,7 +89,7 @@ pub(crate) fn process_syscall_params( res } -/// Sys-calls invocator. +/// Syscalls invocator. /// /// Inserts syscalls invokes randomly into internal functions. /// @@ -197,21 +197,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { } fn insert_syscalls(&mut self) -> Result<()> { - log::trace!( - "Random data before inserting syscalls invoke instructions - {}", - self.unstructured.len() - ); - let insertion_mapping = self.build_syscalls_insertion_mapping()?; for (insert_into_fn, syscalls) in insertion_mapping { self.insert_syscalls_into_fn(insert_into_fn, syscalls)?; } - log::trace!( - "Random data after inserting syscalls invoke instructions - {}", - self.unstructured.len() - ); - Ok(()) } @@ -253,7 +243,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { syscalls: Vec, ) -> Result<()> { log::trace!( - "Random data before inserting syscalls invoke instructions into function {insert_into_fn} - {}", + "Random data before inserting syscalls invoke instructions into function with index {insert_into_fn} - {}", self.unstructured.len() ); @@ -288,7 +278,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { )?; log::trace!( - " -- Inserting syscall {} into function {insert_into_fn} at position {pos}", + " -- Inserting syscall `{}` into function with index {insert_into_fn} at position {pos}", syscall.to_str() ); @@ -319,14 +309,14 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { call_indexes_handle: CallIndexesHandle, ) -> Result { log::trace!( - "Random data before building {} syscall invoke instructions - {}", + "Random data before building `{}` syscall invoke instructions - {}", invocable.to_str(), self.unstructured.len() ); if let Some(argument_index) = invocable.has_destination_param() { log::trace!( - " -- Generating build call for {} syscall with destination", + " -- Building call instructions for a `{}` syscall with destination", invocable.to_str() ); @@ -338,7 +328,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { ) } else { log::trace!( - " -- Generating build call for common syscall {}", + " -- Building call for a common syscall `{}`", invocable.to_str() ); @@ -359,7 +349,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { self.build_call(invocable, signature, call_indexes_handle)?; let destination_instructions = if self.config.syscall_destination().is_source() { - log::trace!(" -- Sys-call destination is result of `gr_source`"); + log::trace!(" --- Syscall destination is result of `gr_source`"); let gr_source_call_indexes_handle = self .syscalls_imports @@ -390,13 +380,13 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { let address_offset = match self.offsets.as_mut() { Some(offsets) => { assert!(self.config.syscall_destination().is_existing_addresses()); - log::trace!(" -- Sys-call destination is an existing program address"); + log::trace!(" ---- Syscall destination is an existing program address"); offsets.next_offset() } None => { assert!(self.config.syscall_destination().is_random()); - log::trace!(" -- Sys-call destination is a random address"); + log::trace!(" ---- Syscall destination is a random address"); self.unstructured.arbitrary()? } @@ -447,7 +437,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { fn build_param_setters(&mut self, params: &[ParamType]) -> Result> { log::trace!( - " ---- Random data before SysCallsInvocator::build_param_setters - {}", + " -- Random data before building param setters - {}", self.unstructured.len() ); @@ -526,7 +516,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { } log::trace!( - " ---- Random data after SysCallsInvocator::build_param_setters - {}", + " -- Random data after building param setters - {}", self.unstructured.len() ); @@ -621,6 +611,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { log::trace!("Resolving calls indexes"); let imports_num = self.module.count_import_funcs() as u32; + let mut logged = HashSet::with_capacity(self.call_indexes.len()); self.module.with(|mut module| { let each_func_instructions = module @@ -637,9 +628,19 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { .expect("getting by handle of existing call"); match index_ty { FunctionIndex::Func(idx) => { - log::trace!(" -- Old function index - {idx}"); + let old_idx = *call_indexes_handle; *call_indexes_handle = idx + imports_num; - log::trace!(" -- New function index - {}", *call_indexes_handle); + + // Log only not changed indexes, because loop can receive repeted + // call indexes. + if !logged.contains(&*call_indexes_handle) { + logged.insert(*call_indexes_handle); + + log::trace!( + " -- Old function index - {old_idx}, new index - {}", + *call_indexes_handle + ); + } } FunctionIndex::Import(idx) => *call_indexes_handle = idx, } @@ -667,10 +668,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { panic!("Export cannot be to the import function"); }; - log::trace!(" -- Old export function index - {idx}"); + let old_idx = *export_call_indexes_handle; *export_call_indexes_handle = idx + imports_num; + log::trace!( - " -- New export function index - {}", + " -- Old export function index - {old_idx}, new index - {}", *export_call_indexes_handle ); }