Skip to content

Commit

Permalink
Introduce unreachable_instruction_enabled option (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
techraed authored Sep 19, 2023
1 parent 2db6004 commit 9942645
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions crates/wasm-smith/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ pub trait Config: 'static + std::fmt::Debug {
fn call_indirect_enabled(&self) -> bool {
true
}

/// Determines whether `unreachable` instruction is enabled
/// in the generating module.
///
/// Defaults to `true`.
fn unreachable_instruction_enabled(&self) -> bool {
true
}
}

/// The default configuration.
Expand Down Expand Up @@ -551,6 +559,7 @@ pub struct SwarmConfig {
pub table_max_size_required: bool,
pub memory_grow_enabled: bool,
pub call_indirect_enabled: bool,
pub unreachable_instruction_enabled: bool,
}

impl<'a> Arbitrary<'a> for SwarmConfig {
Expand Down Expand Up @@ -598,6 +607,7 @@ impl<'a> Arbitrary<'a> for SwarmConfig {
float_enabled: u.arbitrary()?,
memory_grow_enabled: u.arbitrary()?,
call_indirect_enabled: u.arbitrary()?,
unreachable_instruction_enabled: u.arbitrary()?,

// These fields, unlike the ones above, are less useful to set.
// They either make weird inputs or are for features not widely
Expand Down Expand Up @@ -838,4 +848,8 @@ impl Config for SwarmConfig {
fn call_indirect_enabled(&self) -> bool {
self.call_indirect_enabled
}

fn unreachable_instruction_enabled(&self) -> bool {
self.unreachable_instruction_enabled
}
}
7 changes: 6 additions & 1 deletion crates/wasm-smith/src/core/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro_rules! instructions {
// less than 1000.
instructions! {
// Control instructions.
(None, unreachable, Control, 990),
(Some(unreachable_valid), unreachable, Control, 990),
(None, nop, Control, 800),
(None, block, Control),
(None, r#loop, Control),
Expand Down Expand Up @@ -1099,6 +1099,11 @@ fn arbitrary_val(ty: ValType, u: &mut Unstructured<'_>) -> Instruction {
}
}

#[inline]
fn unreachable_valid(module: &Module, _: &mut CodeBuilder) -> bool {
module.config.unreachable_instruction_enabled()
}

fn unreachable(_: &mut Unstructured, _: &Module, _: &mut CodeBuilder) -> Result<Instruction> {
Ok(Instruction::Unreachable)
}
Expand Down

0 comments on commit 9942645

Please sign in to comment.