Skip to content

Commit

Permalink
refactor(wasm-gen): Clean-up wasm-gen (#3384)
Browse files Browse the repository at this point in the history
  • Loading branch information
techraed authored Oct 10, 2023
1 parent 3ee1edd commit 5b931e2
Show file tree
Hide file tree
Showing 15 changed files with 478 additions and 426 deletions.
2 changes: 1 addition & 1 deletion utils/node-loader/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn get_wasm_gen_config(
(SysCallName::Alloc, 3..=6),
(SysCallName::Free, 3..=6),
]
.map(|(sys_call, range)| (InvocableSysCall::Loose(sys_call), range))
.map(|(syscall, range)| (InvocableSysCall::Loose(syscall), range))
.into_iter(),
);

Expand Down
2 changes: 1 addition & 1 deletion utils/runtime-fuzzer/src/gear_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ fn config(
(SysCallName::Alloc, 3..=6),
(SysCallName::Free, 3..=6),
]
.map(|(sys_call, range)| (InvocableSysCall::Loose(sys_call), range))
.map(|(syscall, range)| (InvocableSysCall::Loose(syscall), range))
.into_iter(),
);

Expand Down
26 changes: 13 additions & 13 deletions utils/wasm-gen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
//!
Expand All @@ -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)?;
Expand All @@ -55,7 +56,7 @@
//! stack_end_page: Some(64),
//! };
//! let entry_points_set = EntryPointsSet::InitHandle;
//! let sys_calls_config = SysCallsConfigBuilder::new(SysCallsInjectionTypes::all_once())
//! let syscalls_config = SysCallsConfigBuilder::new(SysCallsInjectionTypes::all_once())
//! .with_source_msg_dest()
//! .with_log_info("I'm from wasm-gen".into())
//! .build();
Expand All @@ -64,7 +65,7 @@
//! memory_config: memory_pages_config,
//! entry_points_config: entry_points_set,
//! remove_recursions: true,
//! sys_calls_config,
//! syscalls_config,
//! };
//! ```
//!
Expand Down Expand Up @@ -144,15 +145,15 @@ pub struct StandardGearWasmConfigsBundle<T = [u8; 32]> {
/// 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 type for each syscall.
pub injection_types: SysCallsInjectionTypes,
/// Config of gear wasm call entry-points (exports).
pub entry_points_set: EntryPointsSet,
/// Initial wasm memory pages.
pub initial_pages: u32,
/// Optional stack end pages.
pub stack_end_page: Option<u32>,
/// Sys-calls params config
/// Syscalls params config
pub params_config: SysCallsParamsConfig,
/// Flag which signals whether `unreachable` instruction must be used
/// during wasm generation.
Expand Down Expand Up @@ -197,17 +198,16 @@ impl<T: Into<Hash>> ConfigsBundle for StandardGearWasmConfigsBundle<T> {
..SelectableParams::default()
};

let mut sys_calls_config_builder = SysCallsConfigBuilder::new(injection_types);
let mut syscalls_config_builder = SysCallsConfigBuilder::new(injection_types);
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,
Expand All @@ -216,7 +216,7 @@ impl<T: Into<Hash>> ConfigsBundle for StandardGearWasmConfigsBundle<T> {
};
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();
Expand Down
10 changes: 5 additions & 5 deletions utils/wasm-gen/src/config/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -76,8 +76,8 @@ pub struct GearWasmGeneratorConfig {
pub memory_config: MemoryPagesConfig,
/// Entry points config.
pub entry_points_config: EntryPointsSet,
/// Sys-calls generator module config.
pub sys_calls_config: SysCallsConfig,
/// Syscalls generator module config.
pub syscalls_config: SysCallsConfig,
/// Flag, signalizing whether recursions
/// should be removed from resulting module.
pub remove_recursions: bool,
Expand Down
13 changes: 9 additions & 4 deletions utils/wasm-gen/src/config/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}
Expand Down
52 changes: 26 additions & 26 deletions utils/wasm-gen/src/config/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! 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 injection;
mod param;
Expand All @@ -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_types: SysCallsInjectionTypes) -> Self {
Self(SysCallsConfig {
injection_types,
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,
Expand All @@ -68,25 +68,25 @@ 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.0.syscall_destination = SysCallDestination::Source;
self.0
.injection_types
.enable_sys_call_import(InvocableSysCall::Loose(SysCallName::Source));
.enable_syscall_import(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<T: Into<Hash>>(mut self, addresses: NonEmpty<T>) -> 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
}
Expand All @@ -99,7 +99,7 @@ impl SysCallsConfigBuilder {
self.0.log_info = Some(log);
self.0
.injection_types
.enable_sys_call_import(InvocableSysCall::Loose(SysCallName::Debug));
.enable_syscall_import(InvocableSysCall::Loose(SysCallName::Debug));

self
}
Expand Down Expand Up @@ -141,28 +141,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_types: SysCallsInjectionTypes,
params_config: SysCallsParamsConfig,
precise_syscalls_config: PreciseSysCallsConfig,
sys_call_destination: SysCallDestination,
syscall_destination: SysCallDestination,
error_processing_config: ErrorProcessingConfig,
log_info: Option<String>,
}

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_types(&self, name: InvocableSysCall) -> SysCallInjectionType {
self.injection_types.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.
Expand All @@ -172,12 +172,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
}
Expand All @@ -188,9 +188,9 @@ impl SysCallsConfig {
}
}

/// Sys-call destination choice.
/// Syscall 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.
Expand All @@ -203,17 +203,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(_))
}
Expand Down
Loading

0 comments on commit 5b931e2

Please sign in to comment.