Skip to content

Commit

Permalink
Adjust logs, rename rest 'sys-call' stuff to 'syscalls'
Browse files Browse the repository at this point in the history
  • Loading branch information
techraed committed Oct 7, 2023
1 parent a1fb3c3 commit e600833
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub struct StandardGearWasmConfigsBundle<T = [u8; 32]> {
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
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config/syscalls/injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/config/syscalls/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm-gen/src/generator/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Sys-calls generators entities.
//! Syscalls generators entities.
//!
//! Generators from this module form a state machine:
//! ```text
Expand Down
5 changes: 3 additions & 2 deletions utils/wasm-gen/src/generator/syscalls/additional_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<InvocableSysCall, (Option<NonZeroU32>, CallIndexesHandle)>,
pub(super) syscalls_imports:
BTreeMap<InvocableSysCall, (Option<NonZeroU32>, CallIndexesHandle)>,
pub(super) config: SysCallsConfig,
}

Expand Down
10 changes: 5 additions & 5 deletions utils/wasm-gen/src/generator/syscalls/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Sys-calls imports generator module.
//! Syscalls imports generator module.
use crate::{
generator::{
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct SysCallsImportsGenerator<'a, 'b> {
syscalls_imports: BTreeMap<InvocableSysCall, (Option<NonZeroU32>, 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>(
Expand Down Expand Up @@ -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()
);

Expand Down Expand Up @@ -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)))
Expand Down
56 changes: 29 additions & 27 deletions utils/wasm-gen/src/generator/syscalls/invocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Sys-calls invocator module.
//! Syscalls invocator module.
use crate::{
generator::{
Expand All @@ -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,
};
Expand Down Expand Up @@ -89,7 +89,7 @@ pub(crate) fn process_syscall_params(
res
}

/// Sys-calls invocator.
/// Syscalls invocator.
///
/// Inserts syscalls invokes randomly into internal functions.
///
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -253,7 +243,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {
syscalls: Vec<InvocableSysCall>,
) -> 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()
);

Expand Down Expand Up @@ -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()
);

Expand Down Expand Up @@ -319,14 +309,14 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {
call_indexes_handle: CallIndexesHandle,
) -> Result<SysCallInvokeInstructions> {
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()
);

Expand All @@ -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()
);

Expand All @@ -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
Expand Down Expand Up @@ -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()?
}
Expand Down Expand Up @@ -447,7 +437,7 @@ impl<'a, 'b> SysCallsInvocator<'a, 'b> {

fn build_param_setters(&mut self, params: &[ParamType]) -> Result<Vec<ParamSetter>> {
log::trace!(
" ---- Random data before SysCallsInvocator::build_param_setters - {}",
" -- Random data before building param setters - {}",
self.unstructured.len()
);

Expand Down Expand Up @@ -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()
);

Expand Down Expand Up @@ -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
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
);
}
Expand Down

0 comments on commit e600833

Please sign in to comment.