Skip to content

Commit

Permalink
Fix some fuzz memory configuration issues (bytecodealliance#8647)
Browse files Browse the repository at this point in the history
* Fix some fuzz memory configuration issues

Fallout from bytecodealliance#8628 that I forgot to handle.

* More fuzz tweaks

* More tweaks for more bugs
  • Loading branch information
alexcrichton committed May 20, 2024
1 parent eda89e6 commit 98529dc
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions crates/fuzzing/src/generators/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ impl Config {
pooling.max_tables_per_module = config.max_tables as u32;

pooling.core_instance_size = 1_000_000;

if let MemoryConfig::Normal(cfg) = &mut self.wasmtime.memory_config {
match &mut cfg.static_memory_maximum_size {
Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
other @ None => *other = Some(pooling.max_memory_size as u64),
}
}
}
}

Expand Down Expand Up @@ -406,6 +413,12 @@ impl<'a> Arbitrary<'a> for Config {
// If using the pooling allocator, constrain the memory and module configurations
// to the module limits.
if let InstanceAllocationStrategy::Pooling(pooling) = &mut config.wasmtime.strategy {
// Forcibly don't use the `CustomUnaligned` memory configuration
// with the pooling allocator active.
if let MemoryConfig::CustomUnaligned = config.wasmtime.memory_config {
config.wasmtime.memory_config = MemoryConfig::Normal(u.arbitrary()?);
}

let cfg = &mut config.module_config.config;
// If the pooling allocator is used, do not allow shared memory to
// be created. FIXME: see
Expand All @@ -415,7 +428,10 @@ impl<'a> Arbitrary<'a> for Config {
// Ensure the pooling allocator can support the maximal size of
// memory, picking the smaller of the two to win.
let min_pages = cfg.max_memory32_pages.min(cfg.max_memory64_pages);
let min = (min_pages << 16).min(pooling.max_memory_size as u64);
let mut min = (min_pages << 16).min(pooling.max_memory_size as u64);
if let MemoryConfig::Normal(cfg) = &config.wasmtime.memory_config {
min = min.min(cfg.static_memory_maximum_size.unwrap_or(0));
}
pooling.max_memory_size = min as usize;
cfg.max_memory32_pages = min >> 16;
cfg.max_memory64_pages = min >> 16;
Expand All @@ -424,23 +440,23 @@ impl<'a> Arbitrary<'a> for Config {
// of memory so if we still are only allowing 0 pages of memory then
// increase that to one here.
if cfg.disallow_traps {
if pooling.max_memory_size == 0 {
if pooling.max_memory_size < (1 << 16) {
pooling.max_memory_size = 1 << 16;
cfg.max_memory32_pages = 1;
cfg.max_memory64_pages = 1;
if let MemoryConfig::Normal(cfg) = &mut config.wasmtime.memory_config {
match &mut cfg.static_memory_maximum_size {
Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
size @ None => *size = Some(pooling.max_memory_size as u64),
}
}
}
// .. additionally update tables
if pooling.table_elements == 0 {
pooling.table_elements = 1;
}
}

// Forcibly don't use the `CustomUnaligned` memory configuration
// with the pooling allocator active.
if let MemoryConfig::CustomUnaligned = config.wasmtime.memory_config {
config.wasmtime.memory_config = MemoryConfig::Normal(u.arbitrary()?);
}

// Don't allow too many linear memories per instance since massive
// virtual mappings can fail to get allocated.
cfg.min_memories = cfg.min_memories.min(10);
Expand Down

0 comments on commit 98529dc

Please sign in to comment.