Skip to content

Commit

Permalink
feat(wasm-smith): add reserved_memory_size and tweak `memory_offset…
Browse files Browse the repository at this point in the history
…_choices`
  • Loading branch information
StackOverflowExcept1on committed Jan 20, 2024
1 parent 7100fe0 commit 265ee58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/wasm-smith/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ pub trait Config: 'static + std::fmt::Debug {
false
}

/// The size of reserved memory on the last memory page. Defaults to None.
fn reserved_memory_size(&self) -> Option<u64> {
None
}

/// Determines whether the tail calls proposal is enabled for generating
/// instructions.
///
Expand Down Expand Up @@ -560,6 +565,7 @@ pub struct SwarmConfig {
pub min_uleb_size: u8,
pub multi_value_enabled: bool,
pub reference_types_enabled: bool,
pub reserved_memory_size: Option<u64>,
pub tail_call_enabled: bool,
pub relaxed_simd_enabled: bool,
pub saturating_float_to_int_enabled: bool,
Expand Down Expand Up @@ -595,6 +601,7 @@ impl<'a> Arbitrary<'a> for SwarmConfig {
max_tables,
max_memory_pages: u.arbitrary()?,
min_uleb_size: u.int_in_range(0..=5)?,
reserved_memory_size: None,
bulk_memory_enabled: reference_types_enabled || u.arbitrary()?,
reference_types_enabled,
simd_enabled: u.arbitrary()?,
Expand Down Expand Up @@ -637,7 +644,7 @@ impl<'a> Arbitrary<'a> for SwarmConfig {
max_modules: 0,
max_components: 0,
max_values: 0,
memory_offset_choices: (75, 24, 1),
memory_offset_choices: (75, 25, 0),
allow_start_export: true,
relaxed_simd_enabled: false,
exceptions_enabled: false,
Expand Down Expand Up @@ -784,6 +791,10 @@ impl Config for SwarmConfig {
self.reference_types_enabled
}

fn reserved_memory_size(&self) -> Option<u64> {
self.reserved_memory_size
}

fn tail_call_enabled(&self) -> bool {
self.tail_call_enabled
}
Expand Down
5 changes: 5 additions & 0 deletions crates/wasm-smith/src/core/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,11 @@ fn memory_offset(u: &mut Unstructured, module: &Module, memory_index: u32) -> Re

let choice = u.int_in_range(0..=a + b + c - 1)?;
if choice < a {
let min = module
.config
.reserved_memory_size()
.map(|reserved| min.saturating_sub(reserved).saturating_sub(16))
.unwrap_or(min);
u.int_in_range(0..=min)
} else if choice < a + b {
u.int_in_range(min..=max)
Expand Down

0 comments on commit 265ee58

Please sign in to comment.