Skip to content

Commit

Permalink
feat(wasm-smith): add reserved_memory_size (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on authored Jan 28, 2024
1 parent b5ee21e commit ffe9abe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 11 additions & 0 deletions 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 @@ -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
10 changes: 8 additions & 2 deletions crates/wasm-smith/src/core/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use super::{
Elements, FuncType, GlobalInitExpr, Instruction, InstructionKind::*, InstructionKinds, Module,
ValType,
};
use crate::unique_string;
use arbitrary::{Result, Unstructured};
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryFrom;
use std::rc::Rc;
use wasm_encoder::{BlockType, ConstExpr, ExportKind, GlobalType, MemArg, RefType};
use wasm_encoder::{BlockType, ConstExpr, GlobalType, MemArg, RefType};
mod no_traps;

macro_rules! instructions {
Expand Down Expand Up @@ -4626,6 +4625,13 @@ 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 {
// Here we guarantee that writing to reserved memory will never happen.
// 16 is the number of bytes of the largest load type (V128).
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 ffe9abe

Please sign in to comment.