From 2e74006ae282a56898d274a7f05fa86503e2efd2 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Wed, 20 Sep 2023 21:33:17 +0300 Subject: [PATCH 01/16] State agenda --- utils/wasm-gen/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index 64bd9e4bfc3..e76d3c23378 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -73,3 +73,10 @@ pub fn generate_gear_program_module( GearWasmGenerator::new_with_config(wasm_module, u, gear_wasm_generator_config).generate() } + + +// TODO +// 1. Rename all sys-calls and sys_calls to syscalls +// 2. clean-up in SysCallsImportsGenerator +// 3. Check all other code +// 4. Check logs after merging @mertwole's changes. Make it more clear. From e3598b94f4fd4d145e31405af5d994e572eaf88d Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Sat, 23 Sep 2023 03:41:08 +0300 Subject: [PATCH 02/16] Adjust agenda --- utils/wasm-gen/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index e76d3c23378..39048712ebd 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -78,5 +78,6 @@ pub fn generate_gear_program_module( // TODO // 1. Rename all sys-calls and sys_calls to syscalls // 2. clean-up in SysCallsImportsGenerator -// 3. Check all other code -// 4. Check logs after merging @mertwole's changes. Make it more clear. +// 3. clean-up after 3242, 3196 +// 4. Check all other code +// 5. Check logs after merging @mertwole's changes. Make it more clear. From b1e046440693dd5379fea872fc44561e6b2d97dc Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Mon, 2 Oct 2023 09:17:38 +0300 Subject: [PATCH 03/16] Rename all `sys-calls` and `sys_calls` to `syscalls` --- utils/node-loader/src/utils.rs | 2 +- utils/runtime-fuzzer/src/gear_calls.rs | 2 +- utils/wasm-gen/src/config.rs | 20 +- utils/wasm-gen/src/config/generator.rs | 8 +- utils/wasm-gen/src/config/syscalls.rs | 52 ++--- utils/wasm-gen/src/config/syscalls/amount.rs | 28 +-- utils/wasm-gen/src/config/syscalls/param.rs | 6 +- utils/wasm-gen/src/config/syscalls/precise.rs | 8 +- utils/wasm-gen/src/generator.rs | 24 +-- utils/wasm-gen/src/generator/syscalls.rs | 38 ++-- .../src/generator/syscalls/additional_data.rs | 18 +- .../src/generator/syscalls/imports.rs | 196 +++++++++--------- .../src/generator/syscalls/invocator.rs | 72 +++---- utils/wasm-gen/src/lib.rs | 2 +- utils/wasm-gen/src/tests.rs | 16 +- 15 files changed, 246 insertions(+), 246 deletions(-) diff --git a/utils/node-loader/src/utils.rs b/utils/node-loader/src/utils.rs index e7640fa21c1..eba8b33d677 100644 --- a/utils/node-loader/src/utils.rs +++ b/utils/node-loader/src/utils.rs @@ -224,7 +224,7 @@ pub fn get_wasm_gen_config( (SysCallName::Alloc, 5..=10), (SysCallName::Free, 5..=10), ] - .map(|(sys_call, range)| (InvocableSysCall::Loose(sys_call), range)) + .map(|(syscall, range)| (InvocableSysCall::Loose(syscall), range)) .into_iter(), ); diff --git a/utils/runtime-fuzzer/src/gear_calls.rs b/utils/runtime-fuzzer/src/gear_calls.rs index 8490247b13d..36058b9a30e 100644 --- a/utils/runtime-fuzzer/src/gear_calls.rs +++ b/utils/runtime-fuzzer/src/gear_calls.rs @@ -405,7 +405,7 @@ fn config( (SysCallName::Alloc, 20..=30), (SysCallName::Free, 20..=30), ] - .map(|(sys_call, range)| (InvocableSysCall::Loose(sys_call), range)) + .map(|(syscall, range)| (InvocableSysCall::Loose(syscall), range)) .into_iter(), ); diff --git a/utils/wasm-gen/src/config.rs b/utils/wasm-gen/src/config.rs index 05ff15c2a1e..ff6031a4c5a 100644 --- a/utils/wasm-gen/src/config.rs +++ b/utils/wasm-gen/src/config.rs @@ -55,7 +55,7 @@ //! stack_end_page: Some(64), //! }; //! let entry_points_set = EntryPointsSet::InitHandle; -//! let sys_calls_config = SysCallsConfigBuilder::new(SysCallsInjectionAmounts::all_once()) +//! let syscalls_config = SysCallsConfigBuilder::new(SysCallsInjectionAmounts::all_once()) //! .with_source_msg_dest() //! .with_log_info("I'm from wasm-gen".into()) //! .build(); @@ -64,7 +64,7 @@ //! memory_config: memory_pages_config, //! entry_points_config: entry_points_set, //! remove_recursions: true, -//! sys_calls_config, +//! syscalls_config, //! }; //! ``` //! @@ -144,7 +144,7 @@ pub struct StandardGearWasmConfigsBundle { /// Flag which signals whether `call_indirect` instruction must be used /// during wasm generation. pub call_indirect_enabled: bool, - /// Injection amount ranges for each sys-call. + /// Injection amount ranges for each syscall. pub injection_amounts: SysCallsInjectionAmounts, /// Config of gear wasm call entry-points (exports). pub entry_points_set: EntryPointsSet, @@ -197,17 +197,17 @@ impl> ConfigsBundle for StandardGearWasmConfigsBundle { ..SelectableParams::default() }; - let mut sys_calls_config_builder = SysCallsConfigBuilder::new(injection_amounts); + let mut syscalls_config_builder = SysCallsConfigBuilder::new(injection_amounts); if let Some(log_info) = log_info { - sys_calls_config_builder = sys_calls_config_builder.with_log_info(log_info); + syscalls_config_builder = syscalls_config_builder.with_log_info(log_info); } if let Some(addresses) = existing_addresses { - sys_calls_config_builder = - sys_calls_config_builder.with_data_offset_msg_dest(addresses); + syscalls_config_builder = + syscalls_config_builder.with_data_offset_msg_dest(addresses); } else { - sys_calls_config_builder = sys_calls_config_builder.with_source_msg_dest(); + syscalls_config_builder = syscalls_config_builder.with_source_msg_dest(); } - sys_calls_config_builder = sys_calls_config_builder.with_params_config(params_config); + syscalls_config_builder = syscalls_config_builder.with_params_config(params_config); let memory_pages_config = MemoryPagesConfig { initial_size: initial_pages, @@ -216,7 +216,7 @@ impl> ConfigsBundle for StandardGearWasmConfigsBundle { }; let gear_wasm_generator_config = GearWasmGeneratorConfigBuilder::new() .with_recursions_removed(remove_recursion) - .with_sys_calls_config(sys_calls_config_builder.build()) + .with_syscalls_config(syscalls_config_builder.build()) .with_entry_points_config(entry_points_set) .with_memory_config(memory_pages_config) .build(); diff --git a/utils/wasm-gen/src/config/generator.rs b/utils/wasm-gen/src/config/generator.rs index 7d831396143..8d068e45837 100644 --- a/utils/wasm-gen/src/config/generator.rs +++ b/utils/wasm-gen/src/config/generator.rs @@ -46,9 +46,9 @@ impl GearWasmGeneratorConfigBuilder { self } - /// Defines sys-calls config for the gear wasm generator. - pub fn with_sys_calls_config(mut self, sys_calls_config: SysCallsConfig) -> Self { - self.0.sys_calls_config = sys_calls_config; + /// Defines syscalls config for the gear wasm generator. + pub fn with_syscalls_config(mut self, syscalls_config: SysCallsConfig) -> Self { + self.0.syscalls_config = syscalls_config; self } @@ -77,7 +77,7 @@ pub struct GearWasmGeneratorConfig { /// Entry points config. pub entry_points_config: EntryPointsSet, /// Sys-calls generator module config. - pub sys_calls_config: SysCallsConfig, + pub syscalls_config: SysCallsConfig, /// Flag, signalizing whether recursions /// should be removed from resulting module. pub remove_recursions: bool, diff --git a/utils/wasm-gen/src/config/syscalls.rs b/utils/wasm-gen/src/config/syscalls.rs index 829cc230e94..bde832e40bc 100644 --- a/utils/wasm-gen/src/config/syscalls.rs +++ b/utils/wasm-gen/src/config/syscalls.rs @@ -16,8 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Configuration for the sys-calls imports generator, additional data injector -//! and sys-calls invocations generator. +//! Configuration for the syscalls imports generator, additional data injector +//! and syscalls invocations generator. mod amount; mod param; @@ -39,26 +39,26 @@ use crate::InvocableSysCall; pub struct SysCallsConfigBuilder(SysCallsConfig); impl SysCallsConfigBuilder { - /// Create a new builder with defined injection amounts for all sys-calls. + /// Create a new builder with defined injection amounts for all syscalls. pub fn new(injection_amounts: SysCallsInjectionAmounts) -> Self { Self(SysCallsConfig { injection_amounts, params_config: SysCallsParamsConfig::default(), precise_syscalls_config: PreciseSysCallsConfig::default(), - sys_call_destination: SysCallDestination::default(), + syscall_destination: SysCallDestination::default(), error_processing_config: ErrorProcessingConfig::None, log_info: None, }) } - /// Set config for sys-calls params. + /// Set config for syscalls params. pub fn with_params_config(mut self, params_config: SysCallsParamsConfig) -> Self { self.0.params_config = params_config; self } - /// Set config for precise sys-calls. + /// Set config for precise syscalls. pub fn with_precise_syscalls_config( mut self, precise_syscalls_config: PreciseSysCallsConfig, @@ -68,23 +68,23 @@ impl SysCallsConfigBuilder { self } - /// Set whether `gr_send*` and `gr_exit` sys-calls must use `gr_source` result for sys-call destination. + /// Set whether `gr_send*` and `gr_exit` syscalls must use `gr_source` result for syscall destination. pub fn with_source_msg_dest(mut self) -> Self { - self.0.sys_call_destination = SysCallDestination::Source; - self.enable_sys_call(InvocableSysCall::Loose(SysCallName::Source)); + self.0.syscall_destination = SysCallDestination::Source; + self.enable_syscall(InvocableSysCall::Loose(SysCallName::Source)); self } - /// Set whether `gr_send*` and `gr_exit` sys-calls must use some address from `addresses` collection - /// as a sys-call destination. + /// Set whether `gr_send*` and `gr_exit` syscalls must use some address from `addresses` collection + /// as a syscall destination. pub fn with_data_offset_msg_dest>(mut self, addresses: NonEmpty) -> Self { let addresses = NonEmpty::collect(addresses.into_iter().map(|pid| HashWithValue { hash: pid.into(), value: 0, })) .expect("collected from non empty"); - self.0.sys_call_destination = SysCallDestination::ExistingAddresses(addresses); + self.0.syscall_destination = SysCallDestination::ExistingAddresses(addresses); self } @@ -95,7 +95,7 @@ impl SysCallsConfigBuilder { /// Choosing gear export to log data is done from best `init` to worse `handle`. pub fn with_log_info(mut self, log: String) -> Self { self.0.log_info = Some(log); - self.enable_sys_call(InvocableSysCall::Loose(SysCallName::Debug)); + self.enable_syscall(InvocableSysCall::Loose(SysCallName::Debug)); self } @@ -107,7 +107,7 @@ impl SysCallsConfigBuilder { self } - fn enable_sys_call(&mut self, name: InvocableSysCall) { + fn enable_syscall(&mut self, name: InvocableSysCall) { let range = self.0.injection_amounts.get(name); let range_start = *range.start(); @@ -147,28 +147,28 @@ impl ErrorProcessingConfig { } } -/// United config for all entities in sys-calls generator module. +/// United config for all entities in syscalls generator module. #[derive(Debug, Clone, Default)] pub struct SysCallsConfig { injection_amounts: SysCallsInjectionAmounts, params_config: SysCallsParamsConfig, precise_syscalls_config: PreciseSysCallsConfig, - sys_call_destination: SysCallDestination, + syscall_destination: SysCallDestination, error_processing_config: ErrorProcessingConfig, log_info: Option, } impl SysCallsConfig { - /// Get possible number of times (range) the sys-call can be injected in the wasm. + /// Get possible number of times (range) the syscall can be injected in the wasm. pub fn injection_amounts(&self, name: InvocableSysCall) -> RangeInclusive { self.injection_amounts.get(name) } - /// Get defined sys-call destination for `gr_send*` and `gr_exit` sys-calls. + /// Get defined syscall destination for `gr_send*` and `gr_exit` syscalls. /// /// For more info, read [`SysCallDestination`]. - pub fn sys_call_destination(&self) -> &SysCallDestination { - &self.sys_call_destination + pub fn syscall_destination(&self) -> &SysCallDestination { + &self.syscall_destination } /// Get defined log info. @@ -178,12 +178,12 @@ impl SysCallsConfig { self.log_info.as_ref() } - /// Get sys-calls params config. + /// Get syscalls params config. pub fn params_config(&self) -> &SysCallsParamsConfig { &self.params_config } - /// Get precise sys-calls config. + /// Get precise syscalls config. pub fn precise_syscalls_config(&self) -> &PreciseSysCallsConfig { &self.precise_syscalls_config } @@ -196,7 +196,7 @@ impl SysCallsConfig { /// Sys-call destination choice. /// -/// `gr_send*` and `gr_exit` sys-calls generated from this crate can be sent +/// `gr_send*` and `gr_exit` syscalls generated from this crate can be sent /// to different destination in accordance to the config. /// It's either to the message source, to some existing known address, /// or to some random, most probably non-existing, address. @@ -209,17 +209,17 @@ pub enum SysCallDestination { } impl SysCallDestination { - /// Check whether sys-call destination is a result of `gr_source`. + /// Check whether syscall destination is a result of `gr_source`. pub fn is_source(&self) -> bool { matches!(&self, SysCallDestination::Source) } - /// Check whether sys-call destination is defined randomly. + /// Check whether syscall destination is defined randomly. pub fn is_random(&self) -> bool { matches!(&self, SysCallDestination::Random) } - /// Check whether sys-call destination is defined from a collection of existing addresses. + /// Check whether syscall destination is defined from a collection of existing addresses. pub fn is_existing_addresses(&self) -> bool { matches!(&self, SysCallDestination::ExistingAddresses(_)) } diff --git a/utils/wasm-gen/src/config/syscalls/amount.rs b/utils/wasm-gen/src/config/syscalls/amount.rs index 58350dea518..437c0e3809c 100644 --- a/utils/wasm-gen/src/config/syscalls/amount.rs +++ b/utils/wasm-gen/src/config/syscalls/amount.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Entities describing possible amount for each sys-call to be injected into wasm module. +//! Entities describing possible amount for each syscall to be injected into wasm module. //! //! Types here are used to create [`crate::SysCallsConfig`]. @@ -25,30 +25,30 @@ use crate::InvocableSysCall; use gear_wasm_instrument::syscalls::SysCallName; use std::{collections::HashMap, ops::RangeInclusive}; -/// Possible injection amount ranges for each sys-call. +/// Possible injection amount ranges for each syscall. #[derive(Debug, Clone)] pub struct SysCallsInjectionAmounts(HashMap>); impl SysCallsInjectionAmounts { - /// Instantiate a sys-calls amounts ranges map, where each gear sys-call is injected into wasm-module only once. + /// Instantiate a syscalls amounts ranges map, where each gear syscall is injected into wasm-module only once. pub fn all_once() -> Self { Self::new_with_range(1..=1) } - /// Instantiate a sys-calls amounts ranges map, where no gear sys-call is ever injected into wasm-module. + /// Instantiate a syscalls amounts ranges map, where no gear syscall is ever injected into wasm-module. pub fn all_never() -> Self { Self::new_with_range(0..=0) } - /// Instantiate a sys-calls amounts ranges map with given range. + /// Instantiate a syscalls amounts ranges map with given range. fn new_with_range(range: RangeInclusive) -> Self { - let sys_calls = SysCallName::instrumentable(); + let syscalls = SysCallName::instrumentable(); Self( - sys_calls + syscalls .iter() .cloned() .map(|name| (InvocableSysCall::Loose(name), range.clone())) - .chain(sys_calls.iter().cloned().filter_map(|name| { + .chain(syscalls.iter().cloned().filter_map(|name| { InvocableSysCall::has_precise_variant(name) .then_some((InvocableSysCall::Precise(name), range.clone())) })) @@ -56,25 +56,25 @@ impl SysCallsInjectionAmounts { ) } - /// Get amount possible sys-call amount range. + /// Get amount possible syscall amount range. pub fn get(&self, name: InvocableSysCall) -> RangeInclusive { self.0 .get(&name) .cloned() - .expect("instantiated with all sys-calls set") + .expect("instantiated with all syscalls set") } - /// Sets possible amount range for the the sys-call. + /// Sets possible amount range for the the syscall. pub fn set(&mut self, name: InvocableSysCall, min: u32, max: u32) { self.0.insert(name, min..=max); } - /// Same as [`SysCallsAmountRanges::set`], but sets amount ranges for multiple sys-calls. + /// Same as [`SysCallsAmountRanges::set`], but sets amount ranges for multiple syscalls. pub fn set_multiple( &mut self, - sys_calls_freqs: impl Iterator)>, + syscalls_freqs: impl Iterator)>, ) { - self.0.extend(sys_calls_freqs) + self.0.extend(syscalls_freqs) } } diff --git a/utils/wasm-gen/src/config/syscalls/param.rs b/utils/wasm-gen/src/config/syscalls/param.rs index 705bdd580ed..758931df12c 100644 --- a/utils/wasm-gen/src/config/syscalls/param.rs +++ b/utils/wasm-gen/src/config/syscalls/param.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Entities describing sys-call param, more precisely, it's allowed values. +//! Entities describing syscall param, more precisely, it's allowed values. //! //! Types here are used to create [`crate::SysCallsConfig`]. @@ -29,7 +29,7 @@ pub use gear_wasm_instrument::syscalls::ParamType; /// Sys-calls params config. /// /// This is basically a map, which creates a relationship between each kind of -/// param, that a sys-call can have, and allowed values ("rules") for each of +/// param, that a syscall can have, and allowed values ("rules") for each of /// the params. /// /// # Note: @@ -103,7 +103,7 @@ impl Default for SysCallsParamsConfig { } } -/// Range of allowed values for the sys-call param. +/// Range of allowed values for the syscall param. #[derive(Debug, Clone)] pub struct SysCallParamAllowedValues(RangeInclusive); diff --git a/utils/wasm-gen/src/config/syscalls/precise.rs b/utils/wasm-gen/src/config/syscalls/precise.rs index 32c14483ea2..a207af80f03 100644 --- a/utils/wasm-gen/src/config/syscalls/precise.rs +++ b/utils/wasm-gen/src/config/syscalls/precise.rs @@ -16,11 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Entities describing configuration for precise sys-calls. +//! Entities describing configuration for precise syscalls. use std::ops::RangeInclusive; -/// Represents the configuration for building some parts of precise sys-calls. +/// Represents the configuration for building some parts of precise syscalls. /// Can be used to write unit tests so you don't have to rely on randomness. #[derive(Debug, Clone)] pub struct PreciseSysCallsConfig { @@ -28,14 +28,14 @@ pub struct PreciseSysCallsConfig { } impl PreciseSysCallsConfig { - /// Creates a new configuration for precise sys-calls, filled with the given values. + /// Creates a new configuration for precise syscalls, filled with the given values. pub fn new(range_of_send_push_calls: RangeInclusive) -> Self { Self { range_of_send_push_calls, } } - /// Get the range of `send_push*` sys-calls. + /// Get the range of `send_push*` syscalls. pub fn range_of_send_push_calls(&self) -> RangeInclusive { self.range_of_send_push_calls.clone() } diff --git a/utils/wasm-gen/src/generator.rs b/utils/wasm-gen/src/generator.rs index ffa44d21a90..e629e2e6a04 100644 --- a/utils/wasm-gen/src/generator.rs +++ b/utils/wasm-gen/src/generator.rs @@ -132,7 +132,7 @@ impl<'a, 'b> GearWasmGenerator<'a, 'b> { let (disabled_syscalls_invocator, frozen_gear_wasm_gen) = Self::from((disabled_ep_gen, frozen_gear_wasm_gen)) - .generate_sys_calls(mem_imports_gen_proof, ep_gen_proof)?; + .generate_syscalls(mem_imports_gen_proof, ep_gen_proof)?; let config = frozen_gear_wasm_gen.melt(); let module = ModuleWithCallIndexes::from(disabled_syscalls_invocator) @@ -178,25 +178,25 @@ impl<'a, 'b> GearWasmGenerator<'a, 'b> { Ok((disabled_ep_gen, frozen_gear_wasm_gen, ep_gen_proof)) } - /// Generate sys-calls using sys-calls module generators. - pub fn generate_sys_calls( + /// Generate syscalls using syscalls module generators. + pub fn generate_syscalls( self, mem_import_gen_proof: MemoryImportGenerationProof, ep_gen_proof: GearEntryPointGenerationProof, ) -> Result<(DisabledSysCallsInvocator, FrozenGearWasmGenerator<'a, 'b>)> { - let sys_calls_imports_gen_instantiator = + let syscalls_imports_gen_instantiator = SysCallsImportsGeneratorInstantiator::from((self, mem_import_gen_proof, ep_gen_proof)); - let (sys_calls_imports_gen, frozen_gear_wasm_gen) = - sys_calls_imports_gen_instantiator.into(); - let sys_calls_imports_gen_res = sys_calls_imports_gen.generate()?; + let (syscalls_imports_gen, frozen_gear_wasm_gen) = + syscalls_imports_gen_instantiator.into(); + let syscalls_imports_gen_res = syscalls_imports_gen.generate()?; - let ad_injector = AdditionalDataInjector::from(sys_calls_imports_gen_res); + let ad_injector = AdditionalDataInjector::from(syscalls_imports_gen_res); let data_injection_res = ad_injector.inject(); - let sys_calls_invocator = SysCallsInvocator::from(data_injection_res); - let disabled_sys_calls_invocator = sys_calls_invocator.insert_invokes()?; + let syscalls_invocator = SysCallsInvocator::from(data_injection_res); + let disabled_syscalls_invocator = syscalls_invocator.insert_invokes()?; - Ok((disabled_sys_calls_invocator, frozen_gear_wasm_gen)) + Ok((disabled_syscalls_invocator, frozen_gear_wasm_gen)) } /// Disable current generator. @@ -222,7 +222,7 @@ struct CallIndexes { /// 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, + /// when syscalls invocator inserts calls of generated syscalls. 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. diff --git a/utils/wasm-gen/src/generator/syscalls.rs b/utils/wasm-gen/src/generator/syscalls.rs index 9fe6f122b74..2b7436f219a 100644 --- a/utils/wasm-gen/src/generator/syscalls.rs +++ b/utils/wasm-gen/src/generator/syscalls.rs @@ -20,14 +20,14 @@ //! //! Generators from this module form a state machine: //! ```text -//! # Zero sys-calls generators nesting level. +//! # Zero syscalls generators nesting level. //! SysCallsImport--->DisabledSysCallsImport--->ModuleWithCallIndexes--->WasmModule //! -//! # First sys-calls generators nesting level. +//! # First syscalls generators nesting level. //! SysCallsImport--->DisabledSysCallsImport--(SysCallsImportsGenerationProof)-->AdditionalDataInjector---\ //! |--->DisabledAdditionalDataInjector--->ModuleWithCallIndexes--->WasmModule //! -//! # Third sys-calls generators nesting level +//! # Third syscalls generators nesting level //! SysCallsImport--->DisabledSysCallsImport--(SysCallsImportsGenerationProof)-->AdditionalDataInjector---\ //! |--->DisabledAdditionalDataInjector--(AddressesInjectionOutcome)-->SysCallsInvocator--->DisabledSysCallsInvocator--->ModuleWithCallIndexes--->WasmModule //! ``` @@ -44,16 +44,16 @@ pub use invocator::*; use gear_wasm_instrument::syscalls::{ParamType, SysCallName, SysCallSignature}; -/// Type of invocable sys-call. +/// Type of invocable syscall. /// -/// Basically, there are 2 types of generated sys-calls: +/// Basically, there are 2 types of generated syscalls: /// 1. Those invocation of which is done regardless of validity of call context (`Loose`). /// 2. Those which are invoked correctly with implementing all call context (`Precise`). /// /// Clarifying that, `gr_reservation_send` requires an existing reservation id, /// which is pretty hard to predict beforehand with a generator. So this call context /// is created from scratch - first `gr_reserve_gas` is called and then it's result -/// is used for the further `gr_reservation_send` call. Those are `Precise` sys-calls. +/// is used for the further `gr_reservation_send` call. Those are `Precise` syscalls. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum InvocableSysCall { Loose(SysCallName), @@ -63,8 +63,8 @@ pub enum InvocableSysCall { impl InvocableSysCall { pub(crate) fn to_str(self) -> &'static str { match self { - InvocableSysCall::Loose(sys_call) => sys_call.to_str(), - InvocableSysCall::Precise(sys_call) => match sys_call { + InvocableSysCall::Loose(syscall) => syscall.to_str(), + InvocableSysCall::Precise(syscall) => match syscall { SysCallName::ReservationSend => "precise_gr_reservation_send", SysCallName::ReservationReply => "precise_gr_reservation_reply", SysCallName::SendCommit => "precise_gr_send_commit", @@ -113,15 +113,15 @@ impl InvocableSysCall { } } - /// Checks whether given sys-call has the precise variant. - pub(crate) fn has_precise_variant(sys_call: SysCallName) -> bool { - Self::required_imports_for_sys_call(sys_call).is_some() + /// Checks whether given syscall has the precise variant. + pub(crate) fn has_precise_variant(syscall: SysCallName) -> bool { + Self::required_imports_for_syscall(syscall).is_some() } - /// Returns the required imports to build precise sys-call. - fn required_imports_for_sys_call(sys_call: SysCallName) -> Option<&'static [SysCallName]> { - // NOTE: the last sys-call must be pattern itself - Some(match sys_call { + /// Returns the required imports to build precise syscall. + fn required_imports_for_syscall(syscall: SysCallName) -> Option<&'static [SysCallName]> { + // NOTE: the last syscall must be pattern itself + Some(match syscall { SysCallName::ReservationSend => { &[SysCallName::ReserveGas, SysCallName::ReservationSend] } @@ -143,10 +143,10 @@ impl InvocableSysCall { }) } - /// Returns the required imports to build precise sys-call, but of a fixed size. - fn required_imports(sys_call: SysCallName) -> &'static [SysCallName; N] { - Self::required_imports_for_sys_call(sys_call) - .expect("failed to find required imports for sys-call") + /// Returns the required imports to build precise syscall, but of a fixed size. + fn required_imports(syscall: SysCallName) -> &'static [SysCallName; N] { + Self::required_imports_for_syscall(syscall) + .expect("failed to find required imports for syscall") .try_into() .expect("failed to convert slice") } diff --git a/utils/wasm-gen/src/generator/syscalls/additional_data.rs b/utils/wasm-gen/src/generator/syscalls/additional_data.rs index ed31137c446..736ca7e74d7 100644 --- a/utils/wasm-gen/src/generator/syscalls/additional_data.rs +++ b/utils/wasm-gen/src/generator/syscalls/additional_data.rs @@ -58,7 +58,7 @@ impl AddressesOffsets { /// The config, which contains additional data types and values is received from [`DisabledSysCallsImportsGenerator`]. /// /// The generator is instantiated only with having [`SysCallsImportsGenerationProof`], which gives a guarantee. that -/// if log info should be injected, than `gr_debug` sys-call import is generated. +/// if log info should be injected, than `gr_debug` syscall import is generated. pub struct AdditionalDataInjector<'a, 'b> { unstructured: &'b mut Unstructured<'a>, call_indexes: CallIndexes, @@ -66,7 +66,7 @@ pub struct AdditionalDataInjector<'a, 'b> { last_offset: u32, module: WasmModule, addresses_offsets: Vec, - sys_calls_imports: BTreeMap, + syscalls_imports: BTreeMap, } impl<'a, 'b> @@ -76,7 +76,7 @@ impl<'a, 'b> )> for AdditionalDataInjector<'a, 'b> { fn from( - (disabled_gen, _sys_calls_gen_proof): ( + (disabled_gen, _syscalls_gen_proof): ( DisabledSysCallsImportsGenerator<'a, 'b>, SysCallsImportsGenerationProof, ), @@ -91,7 +91,7 @@ impl<'a, 'b> last_offset: data_offset as u32, module: disabled_gen.module, addresses_offsets: Vec::new(), - sys_calls_imports: disabled_gen.sys_calls_imports, + syscalls_imports: disabled_gen.syscalls_imports, call_indexes: disabled_gen.call_indexes, } } @@ -122,7 +122,7 @@ impl<'a, 'b> AdditionalDataInjector<'a, 'b> { DisabledAdditionalDataInjector { module: self.module, call_indexes: self.call_indexes, - sys_calls_imports: self.sys_calls_imports, + syscalls_imports: self.syscalls_imports, config: self.config, unstructured: self.unstructured, } @@ -140,7 +140,7 @@ impl<'a, 'b> AdditionalDataInjector<'a, 'b> { )); } - let SysCallDestination::ExistingAddresses(existing_addresses) = self.config.sys_call_destination() else { + let SysCallDestination::ExistingAddresses(existing_addresses) = self.config.syscall_destination() else { return None; }; @@ -213,10 +213,10 @@ impl<'a, 'b> AdditionalDataInjector<'a, 'b> { .expect("impossible to have no gear export"); let debug_call_indexes_handle = self - .sys_calls_imports + .syscalls_imports .get(&InvocableSysCall::Loose(SysCallName::Debug)) .map(|&(_, handle)| handle as u32) - .expect("impossible by configs generation to have log info printing without debug sys-call generated"); + .expect("impossible by configs generation to have log info printing without debug syscall generated"); self.module.with(|module| { let log_bytes_len = log_bytes.len() as u32; @@ -271,7 +271,7 @@ pub struct DisabledAdditionalDataInjector<'a, 'b> { pub(super) unstructured: &'b mut Unstructured<'a>, pub(super) module: WasmModule, pub(super) call_indexes: CallIndexes, - pub(super) sys_calls_imports: BTreeMap, + pub(super) syscalls_imports: BTreeMap, 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 797bbc6e685..5368cba711b 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -37,13 +37,13 @@ use gear_wasm_instrument::{ use gsys::{Handle, Hash, Length}; use std::{collections::BTreeMap, mem}; -/// Gear sys-calls imports generator. +/// Gear syscalls imports generator. pub struct SysCallsImportsGenerator<'a, 'b> { unstructured: &'b mut Unstructured<'a>, call_indexes: CallIndexes, module: WasmModule, config: SysCallsConfig, - sys_calls_imports: BTreeMap, + syscalls_imports: BTreeMap, } /// Sys-calls imports generator instantiator. @@ -57,7 +57,7 @@ pub struct SysCallsImportsGeneratorInstantiator<'a, 'b>( ), ); -/// An error that occurs when generating precise sys-call. +/// An error that occurs when generating precise syscall. #[derive(thiserror::Error, Debug)] pub enum PreciseSysCallError { #[error("{0}")] @@ -94,12 +94,12 @@ impl<'a, 'b> From> _mem_import_gen_proof, _gen_ep_gen_proof, )) = instantiator; - let sys_call_gen = SysCallsImportsGenerator { + let syscall_gen = SysCallsImportsGenerator { unstructured: generator.unstructured, call_indexes: generator.call_indexes, module: generator.module, - config: generator.config.sys_calls_config.clone(), - sys_calls_imports: Default::default(), + config: generator.config.syscalls_config.clone(), + syscalls_imports: Default::default(), }; let frozen = FrozenGearWasmGenerator { config: generator.config, @@ -107,12 +107,12 @@ impl<'a, 'b> From> unstructured: None, }; - (sys_call_gen, frozen) + (syscall_gen, frozen) } } impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { - /// Instantiate a new gear sys-calls imports generator. + /// Instantiate a new gear syscalls imports generator. /// /// The generator instantiations requires having type-level proof that the wasm module has memory import in it. /// This proof could be gotten from memory generator. @@ -133,14 +133,14 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { call_indexes, module, config, - sys_calls_imports: Default::default(), + syscalls_imports: Default::default(), } } /// Disable current generator. pub fn disable(self) -> DisabledSysCallsImportsGenerator<'a, 'b> { log::trace!( - "Random data when disabling sys-calls imports generator - {}", + "Random data when disabling syscalls imports generator - {}", self.unstructured.len() ); DisabledSysCallsImportsGenerator { @@ -148,52 +148,52 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { call_indexes: self.call_indexes, module: self.module, config: self.config, - sys_calls_imports: self.sys_calls_imports, + syscalls_imports: self.syscalls_imports, } } - /// Generates sys-calls imports and a function, that calls `gr_reservation_send` from config, + /// Generates syscalls imports and a function, that calls `gr_reservation_send` from config, /// used to instantiate the generator. /// - /// Returns disabled sys-calls imports generator and a proof that imports from config were generated. + /// Returns disabled syscalls imports generator and a proof that imports from config were generated. pub fn generate( mut self, ) -> Result<( DisabledSysCallsImportsGenerator<'a, 'b>, SysCallsImportsGenerationProof, )> { - log::trace!("Generating sys-calls imports"); + log::trace!("Generating syscalls imports"); - let sys_calls_proof = self.generate_sys_calls_imports()?; - self.generate_precise_sys_calls()?; + let syscalls_proof = self.generate_syscalls_imports()?; + self.generate_precise_syscalls()?; - Ok((self.disable(), sys_calls_proof)) + Ok((self.disable(), syscalls_proof)) } - /// Generates sys-calls imports from config, used to instantiate the generator. - pub fn generate_sys_calls_imports(&mut self) -> Result { + /// Generates syscalls imports from config, used to instantiate the generator. + pub fn generate_syscalls_imports(&mut self) -> Result { log::trace!( - "Random data before sys-calls imports - {}", + "Random data before syscalls imports - {}", self.unstructured.len() ); - for sys_call in SysCallName::instrumentable() { - let sys_call_generation_data = self.generate_sys_call_import(sys_call)?; - if let Some(sys_call_generation_data) = sys_call_generation_data { - self.sys_calls_imports - .insert(InvocableSysCall::Loose(sys_call), sys_call_generation_data); + for syscall in SysCallName::instrumentable() { + let syscall_generation_data = self.generate_syscall_import(syscall)?; + if let Some(syscall_generation_data) = syscall_generation_data { + self.syscalls_imports + .insert(InvocableSysCall::Loose(syscall), syscall_generation_data); } } Ok(SysCallsImportsGenerationProof(())) } - /// Generates precise sys-calls and handles errors if any occurred during generation. - fn generate_precise_sys_calls(&mut self) -> Result<()> { + /// Generates precise syscalls and handles errors if any occurred during generation. + fn generate_precise_syscalls(&mut self) -> Result<()> { use SysCallName::*; #[allow(clippy::type_complexity)] - let sys_calls: [( + let syscalls: [( SysCallName, fn(&mut Self, SysCallName) -> Result<(), PreciseSysCallError>, ); 4] = [ @@ -203,18 +203,18 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { (SendCommitWGas, Self::generate_send_commit_with_gas), ]; - for (sys_call, generate_method) in sys_calls { - let sys_call_amount_range = self + for (syscall, generate_method) in syscalls { + let syscall_amount_range = self .config - .injection_amounts(InvocableSysCall::Precise(sys_call)); - let sys_call_amount = self.unstructured.int_in_range(sys_call_amount_range)?; - for _ in 0..sys_call_amount { + .injection_amounts(InvocableSysCall::Precise(syscall)); + let syscall_amount = self.unstructured.int_in_range(syscall_amount_range)?; + for _ in 0..syscall_amount { log::trace!( - "Constructing {name} sys-call...", - name = InvocableSysCall::Precise(sys_call).to_str() + "Constructing {name} syscall...", + name = InvocableSysCall::Precise(syscall).to_str() ); - if let Err(PreciseSysCallError::Arbitrary(err)) = generate_method(self, sys_call) { + if let Err(PreciseSysCallError::Arbitrary(err)) = generate_method(self, syscall) { return Err(err); } } @@ -223,45 +223,45 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Ok(()) } - /// Generate import of the gear sys-call defined by `sys_call` param. + /// Generate import of the gear syscall defined by `syscall` param. /// - /// Returns [`Option`] which wraps the tuple of amount of sys-call further injections + /// Returns [`Option`] which wraps the tuple of amount of syscall further injections /// and handle in the call indexes collection, if amount is not zero. Otherwise returns /// None. - fn generate_sys_call_import( + fn generate_syscall_import( &mut self, - sys_call: SysCallName, + syscall: SysCallName, ) -> Result> { - let sys_call_amount_range = self + let syscall_amount_range = self .config - .injection_amounts(InvocableSysCall::Loose(sys_call)); - let sys_call_amount = self.unstructured.int_in_range(sys_call_amount_range)?; - Ok((sys_call_amount != 0).then(|| { - let call_indexes_handle = self.insert_sys_call_import(sys_call); + .injection_amounts(InvocableSysCall::Loose(syscall)); + let syscall_amount = self.unstructured.int_in_range(syscall_amount_range)?; + Ok((syscall_amount != 0).then(|| { + let call_indexes_handle = self.insert_syscall_import(syscall); log::trace!( - " -- Generated {} amount of {} sys-call", - sys_call_amount, - sys_call.to_str() + " -- Generated {} amount of {} syscall", + syscall_amount, + syscall.to_str() ); - (sys_call_amount, call_indexes_handle) + (syscall_amount, call_indexes_handle) })) } - /// Inserts gear sys-call defined by the `sys_call` param. - fn insert_sys_call_import(&mut self, sys_call: SysCallName) -> CallIndexesHandle { - let sys_call_import_idx = self.module.count_import_funcs(); + /// Inserts gear syscall defined by the `syscall` param. + fn insert_syscall_import(&mut self, syscall: SysCallName) -> CallIndexesHandle { + let syscall_import_idx = self.module.count_import_funcs(); - // Insert sys-call import to the module + // Insert syscall import to the module self.module.with(|module| { let mut module_builder = builder::from_module(module); // Build signature applicable for the parity-wasm for the sys call - let sys_call_signature = sys_call.signature().func_type(); + let syscall_signature = syscall.signature().func_type(); let signature_idx = module_builder.push_signature( builder::signature() - .with_params(sys_call_signature.params().iter().copied()) - .with_results(sys_call_signature.results().iter().copied()) + .with_params(syscall_signature.params().iter().copied()) + .with_results(syscall_signature.results().iter().copied()) .build_sig(), ); @@ -271,7 +271,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { .module("env") .external() .func(signature_idx) - .field(sys_call.to_str()) + .field(syscall.to_str()) .build(), ); @@ -279,33 +279,33 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { }); let call_indexes_handle = self.call_indexes.len(); - self.call_indexes.add_import(sys_call_import_idx); + self.call_indexes.add_import(syscall_import_idx); call_indexes_handle } } impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { - /// The amount of memory used to create a precise sys-call. + /// The amount of memory used to create a precise syscall. const PRECISE_SYS_CALL_MEMORY_SIZE: u32 = 100; - /// Returns the indexes of invocable sys-calls. - fn invocable_sys_calls_indexes( + /// Returns the indexes of invocable syscalls. + fn invocable_syscalls_indexes( &mut self, - sys_calls: &'static [SysCallName; N], + syscalls: &'static [SysCallName; N], ) -> [usize; N] { let mut indexes = [0; N]; - for (index, &sys_call) in indexes.iter_mut().zip(sys_calls.iter()) { + for (index, &syscall) in indexes.iter_mut().zip(syscalls.iter()) { *index = self - .sys_calls_imports - .get(&InvocableSysCall::Loose(sys_call)) + .syscalls_imports + .get(&InvocableSysCall::Loose(syscall)) .map(|&(_, call_indexes_handle)| call_indexes_handle) .unwrap_or_else(|| { // insert required import when we can't find it - let call_indexes_handle = self.insert_sys_call_import(sys_call); - self.sys_calls_imports - .insert(InvocableSysCall::Loose(sys_call), (0, call_indexes_handle)); + let call_indexes_handle = self.insert_syscall_import(syscall); + self.syscalls_imports + .insert(InvocableSysCall::Loose(syscall), (0, call_indexes_handle)); call_indexes_handle }) } @@ -313,14 +313,14 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { indexes } - /// Generates a function which calls "properly" the given sys-call. - fn generate_proper_sys_call_invocation( + /// Generates a function which calls "properly" the given syscall. + fn generate_proper_syscall_invocation( &mut self, - sys_call: SysCallName, + syscall: SysCallName, func_instructions: Instructions, ) { - let invocable_sys_call = InvocableSysCall::Precise(sys_call); - let signature = invocable_sys_call.into_signature(); + let invocable_syscall = InvocableSysCall::Precise(syscall); + let signature = invocable_syscall.into_signature(); let func_ty = signature.func_type(); let func_signature = builder::signature() @@ -343,18 +343,18 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { }); log::trace!( - "Built proper call to {precise_sys_call_name}", - precise_sys_call_name = invocable_sys_call.to_str() + "Built proper call to {precise_syscall_name}", + precise_syscall_name = invocable_syscall.to_str() ); let call_indexes_handle = self.call_indexes.len(); self.call_indexes.add_func(func_idx.signature as usize); - self.sys_calls_imports - .insert(invocable_sys_call, (1, call_indexes_handle)); + self.syscalls_imports + .insert(invocable_syscall, (1, call_indexes_handle)); } - /// Returns the size of the memory in bytes that can be used to build precise sys-call. + /// Returns the size of the memory in bytes that can be used to build precise syscall. fn memory_size_in_bytes(&self) -> u32 { let initial_mem_size: WasmPageCount = self .module @@ -364,7 +364,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { initial_mem_size.memory_size() } - /// Reserves enough memory build precise sys-call. + /// Reserves enough memory build precise syscall. fn reserve_memory(&self) -> i32 { self.memory_size_in_bytes() .saturating_sub(Self::PRECISE_SYS_CALL_MEMORY_SIZE) as i32 @@ -373,10 +373,10 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { /// Generates a function which calls "properly" the `gr_reservation_send`. fn generate_send_from_reservation( &mut self, - sys_call: SysCallName, + syscall: SysCallName, ) -> Result<(), PreciseSysCallError> { let [reserve_gas_idx, reservation_send_idx] = - self.invocable_sys_calls_indexes(InvocableSysCall::required_imports(sys_call)); + self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); // subtract to be sure we are in memory boundaries. let rid_pid_value_ptr = self.reserve_memory(); @@ -456,7 +456,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, ]); - self.generate_proper_sys_call_invocation(sys_call, func_instructions); + self.generate_proper_syscall_invocation(syscall, func_instructions); Ok(()) } @@ -464,10 +464,10 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { /// Generates a function which calls "properly" the `gr_reservation_reply`. fn generate_reply_from_reservation( &mut self, - sys_call: SysCallName, + syscall: SysCallName, ) -> Result<(), PreciseSysCallError> { let [reserve_gas_idx, reservation_reply_idx] = - self.invocable_sys_calls_indexes(InvocableSysCall::required_imports(sys_call)); + self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); // subtract to be sure we are in memory boundaries. let rid_value_ptr = self.reserve_memory(); @@ -528,15 +528,15 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, ]); - self.generate_proper_sys_call_invocation(sys_call, func_instructions); + self.generate_proper_syscall_invocation(syscall, func_instructions); Ok(()) } /// Generates a function which calls "properly" the `gr_send_commit`. - fn generate_send_commit(&mut self, sys_call: SysCallName) -> Result<(), PreciseSysCallError> { + fn generate_send_commit(&mut self, syscall: SysCallName) -> Result<(), PreciseSysCallError> { let [send_init_idx, send_push_idx, send_commit_idx] = - self.invocable_sys_calls_indexes(InvocableSysCall::required_imports(sys_call)); + self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); // subtract to be sure we are in memory boundaries. let handle_ptr = self.reserve_memory(); @@ -625,7 +625,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { let func_instructions = Instructions::new(elements); - self.generate_proper_sys_call_invocation(sys_call, func_instructions); + self.generate_proper_syscall_invocation(syscall, func_instructions); Ok(()) } @@ -633,10 +633,10 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { /// Generates a function which calls "properly" the `gr_send_commit_wgas`. fn generate_send_commit_with_gas( &mut self, - sys_call: SysCallName, + syscall: SysCallName, ) -> Result<(), PreciseSysCallError> { let [size_idx, send_init_idx, send_push_input_idx, send_commit_wgas_idx] = - self.invocable_sys_calls_indexes(InvocableSysCall::required_imports(sys_call)); + self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); // subtract to be sure we are in memory boundaries. let handle_ptr = self.reserve_memory(); @@ -732,32 +732,32 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { let func_instructions = Instructions::new(elements); - self.generate_proper_sys_call_invocation(sys_call, func_instructions); + self.generate_proper_syscall_invocation(syscall, func_instructions); Ok(()) } } -/// Proof that there was an instance of sys-calls imports generator and `SysCallsImportsGenerator::generate_sys_calls_imports` was called. +/// Proof that there was an instance of syscalls imports generator and `SysCallsImportsGenerator::generate_syscalls_imports` was called. pub struct SysCallsImportsGenerationProof(()); -/// Disabled gear wasm sys-calls generator. +/// Disabled gear wasm syscalls generator. /// -/// Instance of this types signals that there was once active sys-calls generator, +/// Instance of this types signals that there was once active syscalls generator, /// but it ended up it's work. pub struct DisabledSysCallsImportsGenerator<'a, 'b> { pub(super) unstructured: &'b mut Unstructured<'a>, pub(super) call_indexes: CallIndexes, pub(super) module: WasmModule, pub(super) config: SysCallsConfig, - pub(super) sys_calls_imports: BTreeMap, + pub(super) syscalls_imports: BTreeMap, } impl<'a, 'b> From> for ModuleWithCallIndexes { - fn from(disabled_sys_call_gen: DisabledSysCallsImportsGenerator<'a, 'b>) -> Self { + fn from(disabled_syscall_gen: DisabledSysCallsImportsGenerator<'a, 'b>) -> Self { ModuleWithCallIndexes { - module: disabled_sys_call_gen.module, - call_indexes: disabled_sys_call_gen.call_indexes, + module: disabled_syscall_gen.module, + call_indexes: disabled_syscall_gen.call_indexes, } } } diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index d479812b2f3..f75edb67ccd 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -49,7 +49,7 @@ pub(crate) enum ProcessedSysCallParams { MemoryPtrValue, } -pub(crate) fn process_sys_call_params( +pub(crate) fn process_syscall_params( params: &[ParamType], params_config: &SysCallsParamsConfig, ) -> Vec { @@ -88,13 +88,13 @@ pub(crate) fn process_sys_call_params( /// Sys-calls invocator. /// -/// Inserts sys-calls invokes randomly into internal functions. +/// Inserts syscalls invokes randomly into internal functions. /// /// This type is instantiated from disable additional data injector and /// data injection outcome ([`AddressesInjectionOutcome`]). The latter was introduced /// to give additional guarantees for config and generators consistency. Otherwise, /// if there wasn't any addresses injection outcome, which signals that there was a try to -/// inject addresses, sys-calls invocator could falsely set `gr_send*` and `gr_exit` call's destination param +/// inject addresses, syscalls invocator could falsely set `gr_send*` and `gr_exit` call's destination param /// to random value. For example, existing addresses could have been defined in the config, but /// additional data injector was disabled, before injecting addresses from the config. As a result, /// invocator would set un-intended by config values as messages destination. To avoid such @@ -105,7 +105,7 @@ pub struct SysCallsInvocator<'a, 'b> { module: WasmModule, config: SysCallsConfig, offsets: Option, - sys_call_imports: BTreeMap, + syscall_imports: BTreeMap, } impl<'a, 'b> @@ -126,7 +126,7 @@ impl<'a, 'b> module: disabled_gen.module, config: disabled_gen.config, offsets: outcome.offsets, - sys_call_imports: disabled_gen.sys_calls_imports, + syscall_imports: disabled_gen.syscalls_imports, } } } @@ -168,20 +168,20 @@ impl ParamSetter { pub type SysCallInvokeInstructions = Vec; impl<'a, 'b> SysCallsInvocator<'a, 'b> { - /// Insert sys-calls invokes. + /// Insert syscalls invokes. /// - /// The method builds instructions, which describe how each sys-call is called, and then + /// The method builds instructions, which describe how each syscall is called, and then /// insert these instructions into any random function. In the end, all call indexes are resolved. pub fn insert_invokes(mut self) -> Result { log::trace!( - "Random data before inserting all sys-calls invocations - {}", + "Random data before inserting all syscalls invocations - {}", self.unstructured.len() ); - self.insert_sys_calls()?; + self.insert_syscalls()?; log::trace!( - "Random data after inserting all sys-calls invocations - {}", + "Random data after inserting all syscalls invocations - {}", self.unstructured.len() ); @@ -193,9 +193,9 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { }) } - fn insert_sys_calls(&mut self) -> Result<()> { + fn insert_syscalls(&mut self) -> Result<()> { log::trace!( - "Random data before inserting sys-calls invoke instructions - {}", + "Random data before inserting syscalls invoke instructions - {}", self.unstructured.len() ); @@ -205,7 +205,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { .collect(); let syscalls_to_insert = - self.sys_call_imports + self.syscall_imports .clone() .into_iter() .flat_map(|(syscall, (amount, _))| { @@ -218,11 +218,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { self.build_syscalls_insertion_mapping(syscalls_to_insert, &insert_into_funcs)?; for (insert_into_fn, syscalls) in insertion_mapping { - self.insert_sys_calls_into_fn(insert_into_fn, syscalls)?; + self.insert_syscalls_into_fn(insert_into_fn, syscalls)?; } log::trace!( - "Random data after inserting sys-calls invoke instructions - {}", + "Random data after inserting syscalls invoke instructions - {}", self.unstructured.len() ); @@ -257,13 +257,13 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { Ok(insertion_mapping) } - fn insert_sys_calls_into_fn( + fn insert_syscalls_into_fn( &mut self, insert_into_fn: usize, syscalls: Vec, ) -> Result<()> { log::trace!( - "Random data before inserting sys-calls invoke instructions into function {insert_into_fn} - {}", + "Random data before inserting syscalls invoke instructions into function {insert_into_fn} - {}", self.unstructured.len() ); @@ -287,11 +287,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { for (pos, syscall) in insertion_positions.zip(syscalls) { let call_indexes_handle = self - .sys_call_imports + .syscall_imports .get(&syscall) .map(|(_, call_indexes_handle)| *call_indexes_handle) - .expect("Syscall presented in sys_call_imports"); - let instructions = self.build_sys_call_invoke_instructions( + .expect("Syscall presented in syscall_imports"); + let instructions = self.build_syscall_invoke_instructions( syscall, syscall.into_signature(), call_indexes_handle, @@ -315,28 +315,28 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { } log::trace!( - "Random data after inserting sys-calls invoke instructions into function {insert_into_fn} - {}", + "Random data after inserting syscalls invoke instructions into function {insert_into_fn} - {}", self.unstructured.len() ); Ok(()) } - fn build_sys_call_invoke_instructions( + fn build_syscall_invoke_instructions( &mut self, invocable: InvocableSysCall, signature: SysCallSignature, call_indexes_handle: CallIndexesHandle, ) -> Result { log::trace!( - "Random data before building {} sys-call 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 {} sys-call with destination", + " -- Generating build call for {} syscall with destination", invocable.to_str() ); @@ -348,7 +348,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { ) } else { log::trace!( - " -- Generating build call for common sys-call {}", + " -- Generating build call for common syscall {}", invocable.to_str() ); @@ -368,11 +368,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { let mut original_instructions = self.build_call(invocable, signature, call_indexes_handle)?; - let destination_instructions = if self.config.sys_call_destination().is_source() { + let destination_instructions = if self.config.syscall_destination().is_source() { log::trace!(" -- Sys-call destination is result of `gr_source`"); let gr_source_call_indexes_handle = self - .sys_call_imports + .syscall_imports .get(&InvocableSysCall::Loose(SysCallName::Source)) .map(|&(_, call_indexes_handle)| call_indexes_handle as u32) .expect("by config if destination is source, then `gr_source` is generated"); @@ -399,13 +399,13 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { } else { let address_offset = match self.offsets.as_mut() { Some(offsets) => { - assert!(self.config.sys_call_destination().is_existing_addresses()); + assert!(self.config.syscall_destination().is_existing_addresses()); log::trace!(" -- Sys-call destination is an existing program address"); offsets.next_offset() } None => { - assert!(self.config.sys_call_destination().is_random()); + assert!(self.config.syscall_destination().is_random()); log::trace!(" -- Sys-call destination is a random address"); self.unstructured.arbitrary()? @@ -467,7 +467,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { let mem_size = Into::::into(mem_size_pages).memory_size(); let mut setters = Vec::with_capacity(params.len()); - for processed_param in process_sys_call_params(params, self.config.params_config()) { + for processed_param in process_syscall_params(params, self.config.params_config()) { match processed_param { ProcessedSysCallParams::Alloc { allowed_values } => { let pages_to_alloc = if let Some(allowed_values) = allowed_values { @@ -687,20 +687,20 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { } } -/// Disabled sys-calls invocator. +/// Disabled syscalls invocator. /// -/// This type signals that sys-calls imports generation, additional data injection and -/// sys-calls invocation (with further call indexes resolution) is done. +/// This type signals that syscalls imports generation, additional data injection and +/// syscalls invocation (with further call indexes resolution) is done. pub struct DisabledSysCallsInvocator { module: WasmModule, call_indexes: CallIndexes, } impl From for ModuleWithCallIndexes { - fn from(disabled_sys_calls_invocator: DisabledSysCallsInvocator) -> Self { + fn from(disabled_syscalls_invocator: DisabledSysCallsInvocator) -> Self { ModuleWithCallIndexes { - module: disabled_sys_calls_invocator.module, - call_indexes: disabled_sys_calls_invocator.call_indexes, + module: disabled_syscalls_invocator.module, + call_indexes: disabled_syscalls_invocator.call_indexes, } } } diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index 39048712ebd..4b95719079f 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -76,7 +76,7 @@ pub fn generate_gear_program_module( // TODO -// 1. Rename all sys-calls and sys_calls to syscalls +// 1. Rename all syscalls and syscalls to syscalls // 2. clean-up in SysCallsImportsGenerator // 3. clean-up after 3242, 3196 // 4. Check all other code diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index fa5887bbe6a..0835dc4473d 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -121,7 +121,7 @@ fn injecting_addresses_works() { upper_limit: None, stack_end_page: Some(stack_end_page), }) - .with_sys_calls_config( + .with_syscalls_config( SysCallsConfigBuilder::new(Default::default()) .with_data_offset_msg_dest(addresses) .build(), @@ -193,7 +193,7 @@ fn error_processing_works_for_fallible_syscalls() { }); for syscall in fallible_syscalls { - // Prepare sys-calls config & context settings for test case. + // Prepare syscalls config & context settings for test case. let (params_config, initial_memory_write) = get_params_for_syscall_to_fail(syscall); const INJECTED_SYSCALLS: u32 = 8; @@ -201,13 +201,13 @@ fn error_processing_works_for_fallible_syscalls() { let mut injection_amounts = SysCallsInjectionAmounts::all_never(); injection_amounts.set(syscall, INJECTED_SYSCALLS, INJECTED_SYSCALLS); - let sys_calls_config_builder = + let syscalls_config_builder = SysCallsConfigBuilder::new(injection_amounts).with_params_config(params_config); // Assert that syscalls results will be processed. let termination_reason = execute_wasm_with_custom_configs( &mut unstructured, - sys_calls_config_builder + syscalls_config_builder .clone() .set_error_processing_config(ErrorProcessingConfig::All) .build(), @@ -226,7 +226,7 @@ fn error_processing_works_for_fallible_syscalls() { // Assert that syscall results will be ignored. let termination_reason = execute_wasm_with_custom_configs( &mut unstructured2, - sys_calls_config_builder.build(), + syscalls_config_builder.build(), initial_memory_write.clone(), 0, true, @@ -261,7 +261,7 @@ fn precise_syscalls_works() { }); for syscall in precise_syscalls { - // Prepare sys-calls config & context settings for test case. + // Prepare syscalls config & context settings for test case. const INJECTED_SYSCALLS: u32 = 1; let mut injection_amounts = SysCallsInjectionAmounts::all_never(); @@ -322,7 +322,7 @@ fn get_params_for_syscall_to_fail( fn execute_wasm_with_custom_configs( unstructured: &mut Unstructured, - sys_calls_config: SysCallsConfig, + syscalls_config: SysCallsConfig, initial_memory_write: Option, outgoing_limit: u32, imitate_reply: bool, @@ -340,7 +340,7 @@ fn execute_wasm_with_custom_configs( initial_size: INITIAL_PAGES as u32, ..MemoryPagesConfig::default() }) - .with_sys_calls_config(sys_calls_config) + .with_syscalls_config(syscalls_config) .with_entry_points_config(EntryPointsSet::Init) .build(), SelectableParams { From 4b2abca674720deff67081302b222f093fd66a35 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Mon, 2 Oct 2023 09:21:56 +0300 Subject: [PATCH 04/16] Fmt, remove comment --- utils/wasm-gen/src/config.rs | 3 +-- utils/wasm-gen/src/generator.rs | 3 +-- utils/wasm-gen/src/lib.rs | 2 -- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/utils/wasm-gen/src/config.rs b/utils/wasm-gen/src/config.rs index ff6031a4c5a..60c96cd2d71 100644 --- a/utils/wasm-gen/src/config.rs +++ b/utils/wasm-gen/src/config.rs @@ -202,8 +202,7 @@ impl> ConfigsBundle for StandardGearWasmConfigsBundle { syscalls_config_builder = syscalls_config_builder.with_log_info(log_info); } if let Some(addresses) = existing_addresses { - syscalls_config_builder = - syscalls_config_builder.with_data_offset_msg_dest(addresses); + syscalls_config_builder = syscalls_config_builder.with_data_offset_msg_dest(addresses); } else { syscalls_config_builder = syscalls_config_builder.with_source_msg_dest(); } diff --git a/utils/wasm-gen/src/generator.rs b/utils/wasm-gen/src/generator.rs index e629e2e6a04..5d9e662d68a 100644 --- a/utils/wasm-gen/src/generator.rs +++ b/utils/wasm-gen/src/generator.rs @@ -186,8 +186,7 @@ impl<'a, 'b> GearWasmGenerator<'a, 'b> { ) -> Result<(DisabledSysCallsInvocator, FrozenGearWasmGenerator<'a, 'b>)> { let syscalls_imports_gen_instantiator = SysCallsImportsGeneratorInstantiator::from((self, mem_import_gen_proof, ep_gen_proof)); - let (syscalls_imports_gen, frozen_gear_wasm_gen) = - syscalls_imports_gen_instantiator.into(); + let (syscalls_imports_gen, frozen_gear_wasm_gen) = syscalls_imports_gen_instantiator.into(); let syscalls_imports_gen_res = syscalls_imports_gen.generate()?; let ad_injector = AdditionalDataInjector::from(syscalls_imports_gen_res); diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index 4b95719079f..8f51d0f8f3b 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -74,9 +74,7 @@ pub fn generate_gear_program_module( GearWasmGenerator::new_with_config(wasm_module, u, gear_wasm_generator_config).generate() } - // TODO -// 1. Rename all syscalls and syscalls to syscalls // 2. clean-up in SysCallsImportsGenerator // 3. clean-up after 3242, 3196 // 4. Check all other code From 3421d4bf5738bffe25ebf2113b77a68b1110776c Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Mon, 2 Oct 2023 10:39:10 +0300 Subject: [PATCH 05/16] Clean-up imports generator: rename methods for precise syscalls generation, restructure functions --- utils/wasm-gen/src/generator/syscalls.rs | 16 +- .../src/generator/syscalls/imports.rs | 198 ++++++++++-------- 2 files changed, 115 insertions(+), 99 deletions(-) diff --git a/utils/wasm-gen/src/generator/syscalls.rs b/utils/wasm-gen/src/generator/syscalls.rs index 2b7436f219a..14c25ce2c85 100644 --- a/utils/wasm-gen/src/generator/syscalls.rs +++ b/utils/wasm-gen/src/generator/syscalls.rs @@ -118,6 +118,14 @@ impl InvocableSysCall { Self::required_imports_for_syscall(syscall).is_some() } + /// Returns the required imports to build precise syscall, but of a fixed size. + fn required_imports(syscall: SysCallName) -> &'static [SysCallName; N] { + Self::required_imports_for_syscall(syscall) + .expect("failed to find required imports for syscall") + .try_into() + .expect("failed to convert slice") + } + /// Returns the required imports to build precise syscall. fn required_imports_for_syscall(syscall: SysCallName) -> Option<&'static [SysCallName]> { // NOTE: the last syscall must be pattern itself @@ -143,14 +151,6 @@ impl InvocableSysCall { }) } - /// Returns the required imports to build precise syscall, but of a fixed size. - fn required_imports(syscall: SysCallName) -> &'static [SysCallName; N] { - Self::required_imports_for_syscall(syscall) - .expect("failed to find required imports for syscall") - .try_into() - .expect("failed to convert slice") - } - /// Returns the index of the destination param. fn has_destination_param(&self) -> Option { use InvocableSysCall::*; diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 5368cba711b..5262a88c25a 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -193,7 +193,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { use SysCallName::*; #[allow(clippy::type_complexity)] - let syscalls: [( + let precise_syscalls: [( SysCallName, fn(&mut Self, SysCallName) -> Result<(), PreciseSysCallError>, ); 4] = [ @@ -203,18 +203,17 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { (SendCommitWGas, Self::generate_send_commit_with_gas), ]; - for (syscall, generate_method) in syscalls { - let syscall_amount_range = self - .config - .injection_amounts(InvocableSysCall::Precise(syscall)); - let syscall_amount = self.unstructured.int_in_range(syscall_amount_range)?; + for (precise_syscall, generate_precise_syscall) in precise_syscalls { + let syscall_amount = self.produce_syscall_injection_amount(precise_syscall, false)?; for _ in 0..syscall_amount { log::trace!( "Constructing {name} syscall...", - name = InvocableSysCall::Precise(syscall).to_str() + name = InvocableSysCall::Precise(precise_syscall).to_str() ); - if let Err(PreciseSysCallError::Arbitrary(err)) = generate_method(self, syscall) { + if let Err(PreciseSysCallError::Arbitrary(err)) = + generate_precise_syscall(self, precise_syscall) + { return Err(err); } } @@ -232,10 +231,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { &mut self, syscall: SysCallName, ) -> Result> { - let syscall_amount_range = self - .config - .injection_amounts(InvocableSysCall::Loose(syscall)); - let syscall_amount = self.unstructured.int_in_range(syscall_amount_range)?; + let syscall_amount = self.produce_syscall_injection_amount(syscall, true)?; Ok((syscall_amount != 0).then(|| { let call_indexes_handle = self.insert_syscall_import(syscall); log::trace!( @@ -283,92 +279,31 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { call_indexes_handle } -} - -impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { - /// The amount of memory used to create a precise syscall. - const PRECISE_SYS_CALL_MEMORY_SIZE: u32 = 100; - - /// Returns the indexes of invocable syscalls. - fn invocable_syscalls_indexes( - &mut self, - syscalls: &'static [SysCallName; N], - ) -> [usize; N] { - let mut indexes = [0; N]; - - for (index, &syscall) in indexes.iter_mut().zip(syscalls.iter()) { - *index = self - .syscalls_imports - .get(&InvocableSysCall::Loose(syscall)) - .map(|&(_, call_indexes_handle)| call_indexes_handle) - .unwrap_or_else(|| { - // insert required import when we can't find it - let call_indexes_handle = self.insert_syscall_import(syscall); - self.syscalls_imports - .insert(InvocableSysCall::Loose(syscall), (0, call_indexes_handle)); - call_indexes_handle - }) - } - indexes - } - - /// Generates a function which calls "properly" the given syscall. - fn generate_proper_syscall_invocation( + /// Produces amount of time that syscall invocations will be injected for underlying "random" + /// `unstructured` bytes. + /// + /// # Note + /// Calling that > 2 times gives different results depending on bytes of the unerlyinh `unstrucutred`. + fn produce_syscall_injection_amount( &mut self, syscall: SysCallName, - func_instructions: Instructions, - ) { - let invocable_syscall = InvocableSysCall::Precise(syscall); - let signature = invocable_syscall.into_signature(); + is_loose: bool, + ) -> Result { + use InvocableSysCall::*; - let func_ty = signature.func_type(); - let func_signature = builder::signature() - .with_params(func_ty.params().iter().copied()) - .with_results(func_ty.results().iter().copied()) - .build_sig(); + let invocable_syscall = is_loose + .then_some(Loose(syscall)) + .unwrap_or(Precise(syscall)); + let syscall_amount_range = self.config.injection_amounts(invocable_syscall); - let func_idx = self.module.with(|module| { - let mut module_builder = builder::from_module(module); - let idx = module_builder.push_function( - builder::function() - .with_signature(func_signature) - .body() - .with_instructions(func_instructions) - .build() - .build(), - ); - - (module_builder.build(), idx) - }); - - log::trace!( - "Built proper call to {precise_syscall_name}", - precise_syscall_name = invocable_syscall.to_str() - ); - - let call_indexes_handle = self.call_indexes.len(); - self.call_indexes.add_func(func_idx.signature as usize); - - self.syscalls_imports - .insert(invocable_syscall, (1, call_indexes_handle)); - } - - /// Returns the size of the memory in bytes that can be used to build precise syscall. - fn memory_size_in_bytes(&self) -> u32 { - let initial_mem_size: WasmPageCount = self - .module - .initial_mem_size() - .expect("generator is instantiated with a mem import generation proof") - .into(); - initial_mem_size.memory_size() + self.unstructured.int_in_range(syscall_amount_range) } +} - /// Reserves enough memory build precise syscall. - fn reserve_memory(&self) -> i32 { - self.memory_size_in_bytes() - .saturating_sub(Self::PRECISE_SYS_CALL_MEMORY_SIZE) as i32 - } +impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { + /// The amount of memory used to create a precise syscall. + const PRECISE_SYS_CALL_MEMORY_SIZE: u32 = 100; /// Generates a function which calls "properly" the `gr_reservation_send`. fn generate_send_from_reservation( @@ -736,6 +671,87 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Ok(()) } + + /// Returns the indexes of invocable syscalls. + fn invocable_syscalls_indexes( + &mut self, + syscalls: &'static [SysCallName; N], + ) -> [usize; N] { + let mut indexes = [0; N]; + + for (index, &syscall) in indexes.iter_mut().zip(syscalls.iter()) { + *index = self + .syscalls_imports + .get(&InvocableSysCall::Loose(syscall)) + .map(|&(_, call_indexes_handle)| call_indexes_handle) + .unwrap_or_else(|| { + // insert required import when we can't find it + let call_indexes_handle = self.insert_syscall_import(syscall); + self.syscalls_imports + .insert(InvocableSysCall::Loose(syscall), (0, call_indexes_handle)); + call_indexes_handle + }) + } + + indexes + } + + /// Reserves enough memory build precise syscall. + fn reserve_memory(&self) -> i32 { + self.memory_size_in_bytes() + .saturating_sub(Self::PRECISE_SYS_CALL_MEMORY_SIZE) as i32 + } + + /// Returns the size of the memory in bytes that can be used to build precise syscall. + fn memory_size_in_bytes(&self) -> u32 { + let initial_mem_size: WasmPageCount = self + .module + .initial_mem_size() + .expect("generator is instantiated with a mem import generation proof") + .into(); + initial_mem_size.memory_size() + } + + /// Generates a function which calls "properly" the given syscall. + fn generate_proper_syscall_invocation( + &mut self, + syscall: SysCallName, + func_instructions: Instructions, + ) { + let invocable_syscall = InvocableSysCall::Precise(syscall); + let signature = invocable_syscall.into_signature(); + + let func_ty = signature.func_type(); + let func_signature = builder::signature() + .with_params(func_ty.params().iter().copied()) + .with_results(func_ty.results().iter().copied()) + .build_sig(); + + let func_idx = self.module.with(|module| { + let mut module_builder = builder::from_module(module); + let idx = module_builder.push_function( + builder::function() + .with_signature(func_signature) + .body() + .with_instructions(func_instructions) + .build() + .build(), + ); + + (module_builder.build(), idx) + }); + + log::trace!( + "Built proper call to {precise_syscall_name}", + precise_syscall_name = invocable_syscall.to_str() + ); + + let call_indexes_handle = self.call_indexes.len(); + self.call_indexes.add_func(func_idx.signature as usize); + + self.syscalls_imports + .insert(invocable_syscall, (1, call_indexes_handle)); + } } /// Proof that there was an instance of syscalls imports generator and `SysCallsImportsGenerator::generate_syscalls_imports` was called. From 2b3a3c71c268dc025958a79c145c0097d7b686ae Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Wed, 4 Oct 2023 17:33:50 +0300 Subject: [PATCH 06/16] Fix precise syscalls insertion bug --- .../src/generator/syscalls/imports.rs | 68 ++++++++++--------- .../src/generator/syscalls/invocator.rs | 10 +-- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 5262a88c25a..36a2fe1faf5 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -195,7 +195,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { #[allow(clippy::type_complexity)] let precise_syscalls: [( SysCallName, - fn(&mut Self, SysCallName) -> Result<(), PreciseSysCallError>, + fn(&mut Self, SysCallName) -> Result, ); 4] = [ (ReservationSend, Self::generate_send_from_reservation), (ReservationReply, Self::generate_reply_from_reservation), @@ -203,19 +203,21 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { (SendCommitWGas, Self::generate_send_commit_with_gas), ]; - for (precise_syscall, generate_precise_syscall) in precise_syscalls { + for (precise_syscall, generate_method) in precise_syscalls { let syscall_amount = self.produce_syscall_injection_amount(precise_syscall, false)?; - for _ in 0..syscall_amount { - log::trace!( - "Constructing {name} syscall...", - name = InvocableSysCall::Precise(precise_syscall).to_str() - ); - - if let Err(PreciseSysCallError::Arbitrary(err)) = - generate_precise_syscall(self, precise_syscall) - { - return Err(err); + log::trace!( + "Constructing {name} syscall...", + name = InvocableSysCall::Precise(precise_syscall).to_str() + ); + + match generate_method(self, precise_syscall) { + Ok(call_indexes_handle) => { + self.syscalls_imports.insert( + InvocableSysCall::Precise(precise_syscall), + (syscall_amount, call_indexes_handle), + ); } + Err(PreciseSysCallError::Arbitrary(err)) => return Err(err), } } @@ -309,7 +311,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { fn generate_send_from_reservation( &mut self, syscall: SysCallName, - ) -> Result<(), PreciseSysCallError> { + ) -> Result { let [reserve_gas_idx, reservation_send_idx] = self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); @@ -390,17 +392,17 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, Instruction::End, ]); + let call_indexes_handle = + self.generate_proper_syscall_invocation(syscall, func_instructions); - self.generate_proper_syscall_invocation(syscall, func_instructions); - - Ok(()) + Ok(call_indexes_handle) } /// Generates a function which calls "properly" the `gr_reservation_reply`. fn generate_reply_from_reservation( &mut self, syscall: SysCallName, - ) -> Result<(), PreciseSysCallError> { + ) -> Result { let [reserve_gas_idx, reservation_reply_idx] = self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); @@ -462,14 +464,17 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, Instruction::End, ]); + let call_indexes_handle = + self.generate_proper_syscall_invocation(syscall, func_instructions); - self.generate_proper_syscall_invocation(syscall, func_instructions); - - Ok(()) + Ok(call_indexes_handle) } /// Generates a function which calls "properly" the `gr_send_commit`. - fn generate_send_commit(&mut self, syscall: SysCallName) -> Result<(), PreciseSysCallError> { + fn generate_send_commit( + &mut self, + syscall: SysCallName, + ) -> Result { let [send_init_idx, send_push_idx, send_commit_idx] = self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); @@ -557,19 +562,18 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, Instruction::End, ]); - let func_instructions = Instructions::new(elements); + let call_indexes_handle = + self.generate_proper_syscall_invocation(syscall, func_instructions); - self.generate_proper_syscall_invocation(syscall, func_instructions); - - Ok(()) + Ok(call_indexes_handle) } /// Generates a function which calls "properly" the `gr_send_commit_wgas`. fn generate_send_commit_with_gas( &mut self, syscall: SysCallName, - ) -> Result<(), PreciseSysCallError> { + ) -> Result { let [size_idx, send_init_idx, send_push_input_idx, send_commit_wgas_idx] = self.invocable_syscalls_indexes(InvocableSysCall::required_imports(syscall)); @@ -664,12 +668,11 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { Instruction::End, Instruction::End, ]); - let func_instructions = Instructions::new(elements); + let call_indexes_handle = + self.generate_proper_syscall_invocation(syscall, func_instructions); - self.generate_proper_syscall_invocation(syscall, func_instructions); - - Ok(()) + Ok(call_indexes_handle) } /// Returns the indexes of invocable syscalls. @@ -717,7 +720,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { &mut self, syscall: SysCallName, func_instructions: Instructions, - ) { + ) -> CallIndexesHandle { let invocable_syscall = InvocableSysCall::Precise(syscall); let signature = invocable_syscall.into_signature(); @@ -749,8 +752,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { let call_indexes_handle = self.call_indexes.len(); self.call_indexes.add_func(func_idx.signature as usize); - self.syscalls_imports - .insert(invocable_syscall, (1, call_indexes_handle)); + call_indexes_handle } } diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index f75edb67ccd..84fc3ea896d 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -105,7 +105,7 @@ pub struct SysCallsInvocator<'a, 'b> { module: WasmModule, config: SysCallsConfig, offsets: Option, - syscall_imports: BTreeMap, + syscalls_imports: BTreeMap, } impl<'a, 'b> @@ -126,7 +126,7 @@ impl<'a, 'b> module: disabled_gen.module, config: disabled_gen.config, offsets: outcome.offsets, - syscall_imports: disabled_gen.syscalls_imports, + syscalls_imports: disabled_gen.syscalls_imports, } } } @@ -205,7 +205,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { .collect(); let syscalls_to_insert = - self.syscall_imports + self.syscalls_imports .clone() .into_iter() .flat_map(|(syscall, (amount, _))| { @@ -287,7 +287,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { for (pos, syscall) in insertion_positions.zip(syscalls) { let call_indexes_handle = self - .syscall_imports + .syscalls_imports .get(&syscall) .map(|(_, call_indexes_handle)| *call_indexes_handle) .expect("Syscall presented in syscall_imports"); @@ -372,7 +372,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { log::trace!(" -- Sys-call destination is result of `gr_source`"); let gr_source_call_indexes_handle = self - .syscall_imports + .syscalls_imports .get(&InvocableSysCall::Loose(SysCallName::Source)) .map(|&(_, call_indexes_handle)| call_indexes_handle as u32) .expect("by config if destination is source, then `gr_source` is generated"); From 2ba529e918991088cfa2d31e98e64427a540c42b Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Wed, 4 Oct 2023 21:04:10 +0300 Subject: [PATCH 07/16] Make min/max funcs - non zero, reduce heap allocations in invocator --- utils/wasm-gen/src/config.rs | 5 +- utils/wasm-gen/src/config/module.rs | 13 +++-- utils/wasm-gen/src/generator.rs | 39 +++++++++++--- .../src/generator/syscalls/imports.rs | 10 ++-- .../src/generator/syscalls/invocator.rs | 53 +++++++------------ utils/wasm-gen/src/lib.rs | 6 +-- utils/wasm-gen/src/tests.rs | 6 +-- 7 files changed, 75 insertions(+), 57 deletions(-) diff --git a/utils/wasm-gen/src/config.rs b/utils/wasm-gen/src/config.rs index 60c96cd2d71..9cfdd5dbdc6 100644 --- a/utils/wasm-gen/src/config.rs +++ b/utils/wasm-gen/src/config.rs @@ -22,6 +22,7 @@ //! 1. From scratch by settings fields to corresponding values sometimes using //! related to these fields builders. For example, wasm module configs: //! ```rust +//! # use std::num::NonZeroUsize; //! use gear_wasm_gen::*; //! use arbitrary::{Arbitrary, Result, Unstructured}; //! @@ -38,8 +39,8 @@ //! InstructionKind::Control, //! ], //! max_instructions: 100_000, -//! min_funcs: 15, -//! max_funcs: 30, +//! min_funcs: NonZeroUsize::new(15).unwrap(), +//! max_funcs: NonZeroUsize::new(30).unwrap(), //! unreachable_enabled: true, //! }; //! let arbitrary = ArbitraryParams::arbitrary(u)?; diff --git a/utils/wasm-gen/src/config/module.rs b/utils/wasm-gen/src/config/module.rs index 4690382a667..709c35a2678 100644 --- a/utils/wasm-gen/src/config/module.rs +++ b/utils/wasm-gen/src/config/module.rs @@ -23,6 +23,8 @@ //! can be arbitrary, but some must be constantly set. That's implemented with [`ArbitraryParams`] //! and [`ConstantParams`]. +use std::num::NonZeroUsize; + use arbitrary::{Arbitrary, Result, Unstructured}; pub use wasm_smith::InstructionKind; use wasm_smith::{InstructionKind::*, InstructionKinds, SwarmConfig}; @@ -88,6 +90,9 @@ impl From<(SelectableParams, ArbitraryParams)> for WasmModuleConfig { unreachable_enabled, } = selectable_params; + let min_funcs = min_funcs.get(); + let max_funcs = max_funcs.get(); + let ArbitraryParams { available_imports, canonicalize_nans, @@ -352,10 +357,10 @@ pub struct SelectableParams { pub max_instructions: usize, /// Minimum amount of functions `wasm-gen` will insert /// into generated wasm. - pub min_funcs: usize, + pub min_funcs: NonZeroUsize, /// Maximum amount of functions `wasm-gen` will insert /// into generated wasm. - pub max_funcs: usize, + pub max_funcs: NonZeroUsize, /// Flag signalizing whether `unreachable` instruction /// must be used or not. pub unreachable_enabled: bool, @@ -369,8 +374,8 @@ impl Default for SelectableParams { Numeric, Reference, Parametric, Variable, Table, Memory, Control, ], max_instructions: 500, - min_funcs: 3, - max_funcs: 5, + min_funcs: NonZeroUsize::new(3).expect("from non zero value; qed."), + max_funcs: NonZeroUsize::new(5).expect("from non zero value; qed."), unreachable_enabled: true, } } diff --git a/utils/wasm-gen/src/generator.rs b/utils/wasm-gen/src/generator.rs index 5d9e662d68a..f188c0da787 100644 --- a/utils/wasm-gen/src/generator.rs +++ b/utils/wasm-gen/src/generator.rs @@ -56,7 +56,7 @@ use crate::{utils, GearWasmGeneratorConfig, WasmModule}; use arbitrary::{Result, Unstructured}; use gear_wasm_instrument::parity_wasm::elements::Module; -use std::collections::HashSet; +use std::{collections::HashSet, ops::RangeInclusive}; mod entry_points; mod memory; @@ -216,15 +216,17 @@ struct CallIndexes { /// /// 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. + /// entry points ([`EntryPointsGenerator`]) and custom precuse syscalls + /// (generated in [`SysCallsImportsGenerator`]) are considered to be + /// "custom" functions. /// /// Separating "pre-defined" functions from newly generated ones is important /// when syscalls invocator inserts calls of generated syscalls. 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. + /// + /// Same immutability is actual for gear exports to keep them as simple as possible. custom_funcs: HashSet, } @@ -250,8 +252,24 @@ impl CallIndexes { self.inner.get(handle_idx).copied() } - fn is_custom_func(&self, idx: usize) -> bool { - self.custom_funcs.contains(&idx) + fn predefined_funcs_indexes(&self) -> RangeInclusive { + let last = if let Some(first_custom_func_idx) = self.custom_funcs.iter().min() { + // Take last predefined func idx + // + // Subtraction is safe, because by config it's guaranteed + // that there's at least one internal function from `wasm-smith`. + // So, if there's only one predefined function, then first idx + // of a custom function is 1. + first_custom_func_idx - 1 + } else { + self.inner + .iter() + .filter_map(FunctionIndex::internal_func_idx) + .max() + .expect("at least 1 func is generated by config definition") as usize + }; + + 0..=last } fn len(&self) -> usize { @@ -282,6 +300,15 @@ enum FunctionIndex { Func(u32), } +impl FunctionIndex { + fn internal_func_idx(&self) -> Option { + match self { + FunctionIndex::Func(idx) => Some(*idx), + _ => None, + } + } +} + /// Frozen gear wasm generator. /// /// Instantce of this generator signals, that some gear wasm generator diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 36a2fe1faf5..d941e617865 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -292,11 +292,11 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { syscall: SysCallName, is_loose: bool, ) -> Result { - use InvocableSysCall::*; - - let invocable_syscall = is_loose - .then_some(Loose(syscall)) - .unwrap_or(Precise(syscall)); + let invocable_syscall = if is_loose { + InvocableSysCall::Loose(syscall) + } else { + InvocableSysCall::Precise(syscall) + }; let syscall_amount_range = self.config.injection_amounts(invocable_syscall); self.unstructured.int_in_range(syscall_amount_range) diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index 18cd7bdf43b..a71cfec2553 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -201,24 +201,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { self.unstructured.len() ); - let code_funcs = self.module.count_code_funcs(); - let insert_into_funcs: Vec<_> = (0..code_funcs) - .filter(|idx| !self.call_indexes.is_custom_func(*idx)) - .collect(); - - let syscalls_to_insert = - self.syscalls_imports - .clone() - .into_iter() - .flat_map(|(syscall, (amount, _))| { - iter::repeat(syscall) - .take(amount as usize) - .collect::>() - }); - - let insertion_mapping = - self.build_syscalls_insertion_mapping(syscalls_to_insert, &insert_into_funcs)?; - + 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)?; } @@ -234,24 +217,28 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { /// Distributes provided syscalls among provided function ids. /// /// Returns mapping `func_id` <-> `syscalls which should be inserted into func_id`. - fn build_syscalls_insertion_mapping( + fn build_syscalls_insertion_mapping( &mut self, - syscalls: I, - insert_into_funcs: &[usize], - ) -> Result>> - where - I: Iterator, - { + ) -> Result>> { + let insert_into_funcs = self.call_indexes.predefined_funcs_indexes(); + let syscalls = self + .syscalls_imports + .clone() + .into_iter() + .map(|(syscall, (amount, _))| (syscall, amount)); + let mut insertion_mapping: BTreeMap<_, Vec<_>> = BTreeMap::new(); - for syscall in syscalls { - let insert_into = *self.unstructured.choose(insert_into_funcs)?; + for (syscall, amount) in syscalls { + for _ in 0..amount { + let insert_into = self.unstructured.int_in_range(insert_into_funcs.clone())?; - match insertion_mapping.entry(insert_into) { - Entry::Occupied(mut entry) => { - entry.get_mut().push(syscall); - } - Entry::Vacant(entry) => { - entry.insert(vec![syscall]); + match insertion_mapping.entry(insert_into) { + Entry::Occupied(mut entry) => { + entry.get_mut().push(syscall); + } + Entry::Vacant(entry) => { + entry.insert(vec![syscall]); + } } } } diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index 8f51d0f8f3b..3252549bf62 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -75,7 +75,5 @@ pub fn generate_gear_program_module( } // TODO -// 2. clean-up in SysCallsImportsGenerator -// 3. clean-up after 3242, 3196 -// 4. Check all other code -// 5. Check logs after merging @mertwole's changes. Make it more clear. +// 3. clean-up invocator +// 4. Check logs. diff --git a/utils/wasm-gen/src/tests.rs b/utils/wasm-gen/src/tests.rs index 0835dc4473d..137b3ff6e63 100644 --- a/utils/wasm-gen/src/tests.rs +++ b/utils/wasm-gen/src/tests.rs @@ -43,7 +43,7 @@ use gear_wasm_instrument::{ }; use proptest::prelude::*; use rand::{rngs::SmallRng, RngCore, SeedableRng}; -use std::mem; +use std::{mem, num::NonZeroUsize}; const UNSTRUCTURED_SIZE: usize = 1_000_000; @@ -347,8 +347,8 @@ fn execute_wasm_with_custom_configs( call_indirect_enabled: false, allowed_instructions: vec![], max_instructions: 0, - min_funcs: 1, - max_funcs: 1, + min_funcs: NonZeroUsize::new(1).unwrap(), + max_funcs: NonZeroUsize::new(1).unwrap(), unreachable_enabled: true, }, ); From d8bda6c12b3c4ffe4a26e51c193b1ab03ca841da Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Thu, 5 Oct 2023 20:50:39 +0300 Subject: [PATCH 08/16] Rename param --- utils/wasm-gen/src/generator/syscalls/invocator.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index a71cfec2553..0f1f074737d 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -350,7 +350,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { invocable: InvocableSysCall, signature: SysCallSignature, call_indexes_handle: CallIndexesHandle, - argument_index: usize, + destination_arg_idx: usize, ) -> Result> { // The value for the destination param is chosen from config. // It's either the result of `gr_source`, some existing address (set in the data section) or a completely random value. @@ -404,7 +404,10 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { vec![Instruction::I32Const(address_offset as i32)] }; - original_instructions.splice(argument_index..argument_index + 1, destination_instructions); + original_instructions.splice( + destination_arg_idx..destination_arg_idx + 1, + destination_instructions, + ); Ok(original_instructions) } From 4e04a7663dc46a51a041c6bfd32697cd6c9dc593 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Thu, 5 Oct 2023 21:51:56 +0300 Subject: [PATCH 09/16] State todos --- utils/wasm-gen/src/config/syscalls/injection.rs | 3 +++ utils/wasm-gen/src/generator/syscalls/imports.rs | 1 + 2 files changed, 4 insertions(+) diff --git a/utils/wasm-gen/src/config/syscalls/injection.rs b/utils/wasm-gen/src/config/syscalls/injection.rs index 43f40cda554..5521d2a87a2 100644 --- a/utils/wasm-gen/src/config/syscalls/injection.rs +++ b/utils/wasm-gen/src/config/syscalls/injection.rs @@ -97,6 +97,7 @@ impl SysCallsInjectionTypes { }; for &syscall_import in required_imports { + println!("IMPORT REQUIRED {}", syscall_import.to_str()); self.enable_syscall_import(InvocableSysCall::Loose(syscall_import)); } } @@ -106,6 +107,8 @@ impl SysCallsInjectionTypes { 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; + } else { + todo!("!!!"); } } diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 4dc731752c2..8a97e7491a2 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -265,6 +265,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { }; let call_indexes_handle = self.insert_syscall_import(syscall); + todo!("ZERO amount must be inserted"); Ok((syscall_amount != 0).then(|| { log::trace!( " -- Generated {} amount of {} syscall", From f296d0ecd50327d1acd0112385c0ea4c236916a0 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Thu, 5 Oct 2023 22:26:10 +0300 Subject: [PATCH 10/16] Fix test --- utils/wasm-gen/src/config/syscalls/injection.rs | 3 --- .../wasm-gen/src/generator/syscalls/imports.rs | 17 ++++++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/utils/wasm-gen/src/config/syscalls/injection.rs b/utils/wasm-gen/src/config/syscalls/injection.rs index 5521d2a87a2..43f40cda554 100644 --- a/utils/wasm-gen/src/config/syscalls/injection.rs +++ b/utils/wasm-gen/src/config/syscalls/injection.rs @@ -97,7 +97,6 @@ impl SysCallsInjectionTypes { }; for &syscall_import in required_imports { - println!("IMPORT REQUIRED {}", syscall_import.to_str()); self.enable_syscall_import(InvocableSysCall::Loose(syscall_import)); } } @@ -107,8 +106,6 @@ impl SysCallsInjectionTypes { 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; - } else { - todo!("!!!"); } } diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 8a97e7491a2..67d812fc334 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -265,16 +265,15 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { }; let call_indexes_handle = self.insert_syscall_import(syscall); - todo!("ZERO amount must be inserted"); - Ok((syscall_amount != 0).then(|| { - log::trace!( - " -- Generated {} amount of {} syscall", - syscall_amount, - syscall.to_str() - ); + log::trace!( + " -- Generated {} amount of {} sys-call", + syscall_amount, + syscall.to_str() + ); + + todo!("zero amount must not be set"); - (syscall_amount, call_indexes_handle) - })) + Ok(Some((syscall_amount, call_indexes_handle))) } /// Inserts gear syscall defined by the `syscall` param. From 6efbf56b339fd2ad00478c429503b3dc57bc10cc Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Thu, 5 Oct 2023 22:27:47 +0300 Subject: [PATCH 11/16] Remove redundant module --- utils/wasm-gen/src/config.rs | 2 +- utils/wasm-gen/src/config/syscalls/amount.rs | 85 -------------------- 2 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 utils/wasm-gen/src/config/syscalls/amount.rs diff --git a/utils/wasm-gen/src/config.rs b/utils/wasm-gen/src/config.rs index 220cf5d1284..1df920dca05 100644 --- a/utils/wasm-gen/src/config.rs +++ b/utils/wasm-gen/src/config.rs @@ -145,7 +145,7 @@ pub struct StandardGearWasmConfigsBundle { /// Flag which signals whether `call_indirect` instruction must be used /// during wasm generation. pub call_indirect_enabled: bool, - /// Injection amount ranges for each syscall. + /// Injection type for each syscall. pub injection_types: SysCallsInjectionTypes, /// Config of gear wasm call entry-points (exports). pub entry_points_set: EntryPointsSet, diff --git a/utils/wasm-gen/src/config/syscalls/amount.rs b/utils/wasm-gen/src/config/syscalls/amount.rs deleted file mode 100644 index 437c0e3809c..00000000000 --- a/utils/wasm-gen/src/config/syscalls/amount.rs +++ /dev/null @@ -1,85 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2023 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Entities describing possible amount for each syscall to be injected into wasm module. -//! -//! Types here are used to create [`crate::SysCallsConfig`]. - -use crate::InvocableSysCall; - -use gear_wasm_instrument::syscalls::SysCallName; -use std::{collections::HashMap, ops::RangeInclusive}; - -/// Possible injection amount ranges for each syscall. -#[derive(Debug, Clone)] -pub struct SysCallsInjectionAmounts(HashMap>); - -impl SysCallsInjectionAmounts { - /// Instantiate a syscalls amounts ranges map, where each gear syscall is injected into wasm-module only once. - pub fn all_once() -> Self { - Self::new_with_range(1..=1) - } - - /// Instantiate a syscalls amounts ranges map, where no gear syscall is ever injected into wasm-module. - pub fn all_never() -> Self { - Self::new_with_range(0..=0) - } - - /// Instantiate a syscalls amounts ranges map with given range. - fn new_with_range(range: RangeInclusive) -> Self { - let syscalls = SysCallName::instrumentable(); - Self( - syscalls - .iter() - .cloned() - .map(|name| (InvocableSysCall::Loose(name), range.clone())) - .chain(syscalls.iter().cloned().filter_map(|name| { - InvocableSysCall::has_precise_variant(name) - .then_some((InvocableSysCall::Precise(name), range.clone())) - })) - .collect(), - ) - } - - /// Get amount possible syscall amount range. - pub fn get(&self, name: InvocableSysCall) -> RangeInclusive { - self.0 - .get(&name) - .cloned() - .expect("instantiated with all syscalls set") - } - - /// Sets possible amount range for the the syscall. - pub fn set(&mut self, name: InvocableSysCall, min: u32, max: u32) { - self.0.insert(name, min..=max); - } - - /// Same as [`SysCallsAmountRanges::set`], but sets amount ranges for multiple syscalls. - pub fn set_multiple( - &mut self, - syscalls_freqs: impl Iterator)>, - ) { - self.0.extend(syscalls_freqs) - } -} - -impl Default for SysCallsInjectionAmounts { - fn default() -> Self { - Self::all_once() - } -} From a1fb3c3c80a32b9fa4948479f858573b85af5029 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Sat, 7 Oct 2023 15:55:56 +0300 Subject: [PATCH 12/16] Refactor syscalls imports --- .../wasm-gen/src/config/syscalls/injection.rs | 16 +++++--- .../src/generator/syscalls/additional_data.rs | 6 +-- .../src/generator/syscalls/imports.rs | 39 ++++++++++++------- .../src/generator/syscalls/invocator.rs | 7 ++-- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/utils/wasm-gen/src/config/syscalls/injection.rs b/utils/wasm-gen/src/config/syscalls/injection.rs index 43f40cda554..d415ff24796 100644 --- a/utils/wasm-gen/src/config/syscalls/injection.rs +++ b/utils/wasm-gen/src/config/syscalls/injection.rs @@ -39,11 +39,15 @@ pub enum SysCallInjectionType { /// For example, `precision_gr_reservation_send` syscall uses `gr_reserve_gas` under /// the hood. In this case, `gr_reserve_gas` will be imported but will not be called. Import, - /// Sys-call import will be injected into import section of wasm module, - /// and the `wasm-gen` generators will insert invoke instructions for that syscall. + /// Syscall import will be injected into import section of wasm module, + /// and the `wasm-gen` generators can insert invoke instructions for that syscall. + /// + /// It wraps syscall amount range `RangeInclusive` - the range from which + /// amount of the syscall invocations will be generated. /// - /// It also has `syscall_amount_range: RangeInclusive` - the range from which - /// amount of syscalls will be generated for injection into code section of wasm module. + /// Setting range to `(0..=n)`, where `n >= 0` can imitate `SysCallInjectionType::Import`, + /// as in case if syscall amount range is zero, then syscall import will be injected, but + /// no invocations will be generated, which is pretty simillar to the other variant. Function(RangeInclusive), } @@ -87,6 +91,8 @@ impl SysCallsInjectionTypes { } /// Sets possible amount range for the the syscall. + /// + /// Sets injection type for `name` syscall to `SysCallInjectionType::Function`. pub fn set(&mut self, name: InvocableSysCall, min: u32, max: u32) { self.0 .insert(name, SysCallInjectionType::Function(min..=max)); @@ -102,7 +108,7 @@ impl SysCallsInjectionTypes { } } - /// Imports the given syscall if necessary. + /// Imports the given syscall, if necessary. 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; diff --git a/utils/wasm-gen/src/generator/syscalls/additional_data.rs b/utils/wasm-gen/src/generator/syscalls/additional_data.rs index 736ca7e74d7..d1c14112845 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}; +use std::{collections::BTreeMap, iter::Cycle, vec::IntoIter, num::NonZeroU32}; /// Cycled iterator over wasm module data offsets. /// @@ -66,7 +66,7 @@ pub struct AdditionalDataInjector<'a, 'b> { last_offset: u32, module: WasmModule, addresses_offsets: Vec, - syscalls_imports: BTreeMap, + syscalls_imports: BTreeMap, CallIndexesHandle)>, } impl<'a, 'b> @@ -271,7 +271,7 @@ 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, + 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 67d812fc334..71f45dbcd83 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -35,7 +35,7 @@ use gear_wasm_instrument::{ syscalls::SysCallName, }; use gsys::{Handle, Hash, Length}; -use std::{collections::BTreeMap, mem}; +use std::{collections::BTreeMap, mem, num::NonZeroU32}; /// Gear syscalls imports generator. pub struct SysCallsImportsGenerator<'a, 'b> { @@ -43,7 +43,7 @@ pub struct SysCallsImportsGenerator<'a, 'b> { call_indexes: CallIndexes, module: WasmModule, config: SysCallsConfig, - syscalls_imports: BTreeMap, + syscalls_imports: BTreeMap, CallIndexesHandle)>, } /// Sys-calls imports generator instantiator. @@ -215,24 +215,32 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { .config .injection_types(InvocableSysCall::Precise(precise_syscall)); if let SysCallInjectionType::Function(syscall_amount_range) = syscall_injection_type { - let syscall_amount = self.unstructured.int_in_range(syscall_amount_range)?; + let precise_syscall_amount = + NonZeroU32::new(self.unstructured.int_in_range(syscall_amount_range)?); log::trace!( "Constructing {name} syscall...", name = InvocableSysCall::Precise(precise_syscall).to_str() ); + if precise_syscall_amount.is_none() { + // Amount is zero. + continue; + } + match generate_method(self, precise_syscall) { Ok(call_indexes_handle) => { self.syscalls_imports.insert( InvocableSysCall::Precise(precise_syscall), - (syscall_amount, call_indexes_handle), + (precise_syscall_amount, call_indexes_handle), ); } Err(PreciseSysCallError::RequiredImports(err)) => { // By syscalls injection types config all required syscalls for // precise syscalls are set. + // By generator's implementation, precise calls are generated after + // generating syscalls imports. panic!( - "Invalid generators configuration: required syscalls aren't set: {err}" + "Invalid generators configuration or implementation: required syscalls aren't set: {err}" ) } Err(PreciseSysCallError::Arbitrary(err)) => return Err(err), @@ -245,13 +253,15 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { /// Generate import of the gear syscall defined by `syscall` param. /// - /// Returns [`Option`] which wraps the tuple of amount of syscall further injections - /// and handle in the call indexes collection, if amount is not zero. Otherwise returns - /// None. + /// Returns [`Option`] which wraps the tuple of maybe non-zero amount of syscall further injections + /// and handle in the call indexes collection. The amount type is `NonZeroU32` in order to distiguish + /// between syscalls imports that must be generated without further invocation and ones, + /// that must be invoked along with the import generation. + /// If no import is required, `None` is returned. fn generate_syscall_import( &mut self, syscall: SysCallName, - ) -> Result> { + ) -> Result, CallIndexesHandle)>> { let syscall_injection_type = self .config .injection_types(InvocableSysCall::Loose(syscall)); @@ -264,16 +274,16 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { _ => return Ok(None), }; + // Insert import either for case of `SysCallInjectionType::Import`, or + // if `SysCallInjectionType::Function(syscall_amount_range)` yielded zero. let call_indexes_handle = self.insert_syscall_import(syscall); log::trace!( - " -- Generated {} amount of {} sys-call", + " -- Generated {} amount of {} syscall", syscall_amount, syscall.to_str() ); - todo!("zero amount must not be set"); - - Ok(Some((syscall_amount, call_indexes_handle))) + Ok(Some((NonZeroU32::new(syscall_amount), call_indexes_handle))) } /// Inserts gear syscall defined by the `syscall` param. @@ -772,7 +782,8 @@ pub struct DisabledSysCallsImportsGenerator<'a, 'b> { pub(super) call_indexes: CallIndexes, pub(super) module: WasmModule, pub(super) config: SysCallsConfig, - pub(super) syscalls_imports: BTreeMap, + pub(super) syscalls_imports: + BTreeMap, CallIndexesHandle)>, } impl<'a, 'b> From> for ModuleWithCallIndexes { diff --git a/utils/wasm-gen/src/generator/syscalls/invocator.rs b/utils/wasm-gen/src/generator/syscalls/invocator.rs index 0f1f074737d..563fd7c38aa 100644 --- a/utils/wasm-gen/src/generator/syscalls/invocator.rs +++ b/utils/wasm-gen/src/generator/syscalls/invocator.rs @@ -34,6 +34,7 @@ use gear_wasm_instrument::{ use std::{ collections::{btree_map::Entry, BTreeMap, BinaryHeap}, iter, + num::NonZeroU32, }; #[derive(Debug)] @@ -107,7 +108,7 @@ pub struct SysCallsInvocator<'a, 'b> { module: WasmModule, config: SysCallsConfig, offsets: Option, - syscalls_imports: BTreeMap, + syscalls_imports: BTreeMap, CallIndexesHandle)>, } impl<'a, 'b> @@ -225,11 +226,11 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> { .syscalls_imports .clone() .into_iter() - .map(|(syscall, (amount, _))| (syscall, amount)); + .filter_map(|(syscall, (amount, _))| amount.map(|a| (syscall, a))); let mut insertion_mapping: BTreeMap<_, Vec<_>> = BTreeMap::new(); for (syscall, amount) in syscalls { - for _ in 0..amount { + for _ in 0..amount.get() { let insert_into = self.unstructured.int_in_range(insert_into_funcs.clone())?; match insertion_mapping.entry(insert_into) { From e60083358fd9697061b03f4b2ac288afc3a19adc Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Sat, 7 Oct 2023 17:11:16 +0300 Subject: [PATCH 13/16] Adjust logs, rename rest 'sys-call' stuff to 'syscalls' --- utils/wasm-gen/src/config.rs | 2 +- utils/wasm-gen/src/config/generator.rs | 2 +- utils/wasm-gen/src/config/syscalls.rs | 2 +- .../wasm-gen/src/config/syscalls/injection.rs | 2 +- utils/wasm-gen/src/config/syscalls/param.rs | 2 +- utils/wasm-gen/src/generator/syscalls.rs | 2 +- .../src/generator/syscalls/additional_data.rs | 5 +- .../src/generator/syscalls/imports.rs | 10 ++-- .../src/generator/syscalls/invocator.rs | 56 ++++++++++--------- 9 files changed, 43 insertions(+), 40 deletions(-) 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 ); } From 43321e475d771da98a70245ff5ab5acbc8716620 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Sat, 7 Oct 2023 17:14:33 +0300 Subject: [PATCH 14/16] Fix typos --- utils/wasm-gen/src/config/syscalls/injection.rs | 2 +- utils/wasm-gen/src/generator/syscalls/imports.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/wasm-gen/src/config/syscalls/injection.rs b/utils/wasm-gen/src/config/syscalls/injection.rs index f75d62941fd..5b6694a0c68 100644 --- a/utils/wasm-gen/src/config/syscalls/injection.rs +++ b/utils/wasm-gen/src/config/syscalls/injection.rs @@ -47,7 +47,7 @@ pub enum SysCallInjectionType { /// /// Setting range to `(0..=n)`, where `n >= 0` can imitate `SysCallInjectionType::Import`, /// as in case if syscall amount range is zero, then syscall import will be injected, but - /// no invocations will be generated, which is pretty simillar to the other variant. + /// no invocations will be generated, which is pretty similar to the other variant. Function(RangeInclusive), } diff --git a/utils/wasm-gen/src/generator/syscalls/imports.rs b/utils/wasm-gen/src/generator/syscalls/imports.rs index 47ab2e1fce1..2f0cf38b8b3 100644 --- a/utils/wasm-gen/src/generator/syscalls/imports.rs +++ b/utils/wasm-gen/src/generator/syscalls/imports.rs @@ -254,7 +254,7 @@ impl<'a, 'b> SysCallsImportsGenerator<'a, 'b> { /// Generate import of the gear syscall defined by `syscall` param. /// /// Returns [`Option`] which wraps the tuple of maybe non-zero amount of syscall further injections - /// and handle in the call indexes collection. The amount type is `NonZeroU32` in order to distiguish + /// and handle in the call indexes collection. The amount type is `NonZeroU32` in order to distinguish /// between syscalls imports that must be generated without further invocation and ones, /// that must be invoked along with the import generation. /// If no import is required, `None` is returned. From 4fd0b01a19414481d8f145959a2cbd8b4df9d355 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Sat, 7 Oct 2023 17:15:51 +0300 Subject: [PATCH 15/16] Remove todos --- utils/wasm-gen/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/utils/wasm-gen/src/lib.rs b/utils/wasm-gen/src/lib.rs index 3252549bf62..64bd9e4bfc3 100644 --- a/utils/wasm-gen/src/lib.rs +++ b/utils/wasm-gen/src/lib.rs @@ -73,7 +73,3 @@ pub fn generate_gear_program_module( GearWasmGenerator::new_with_config(wasm_module, u, gear_wasm_generator_config).generate() } - -// TODO -// 3. clean-up invocator -// 4. Check logs. From eaf444f649e28f15bf09ad2b8b19144ea86ac6e1 Mon Sep 17 00:00:00 2001 From: Sabaun Taraki Date: Mon, 9 Oct 2023 21:12:51 +0300 Subject: [PATCH 16/16] Fix typo --- utils/wasm-gen/src/generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/wasm-gen/src/generator.rs b/utils/wasm-gen/src/generator.rs index f188c0da787..5277a5fb22e 100644 --- a/utils/wasm-gen/src/generator.rs +++ b/utils/wasm-gen/src/generator.rs @@ -216,7 +216,7 @@ struct CallIndexes { /// /// 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 precuse syscalls + /// entry points ([`EntryPointsGenerator`]) and custom precise syscalls /// (generated in [`SysCallsImportsGenerator`]) are considered to be /// "custom" functions. ///