Skip to content

Commit da45b53

Browse files
Generate psuedorandom gc_zeal_alloc_counter values for fuzzing (#13084)
* Generate psuedorandom `gc_zeal_alloc_counter` values for fuzzing * Fix plain wasmtime-fuzzing tests * Actually plumb stack-switching in wast tests The fix in #13087 was incomplete. This completes it (in theory). --------- Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent 1ac6e1b commit da45b53

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

crates/cli-flags/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clap::Parser;
44
use serde::Deserialize;
55
use std::{
66
fmt, fs,
7+
num::NonZeroU32,
78
path::{Path, PathBuf},
89
time::Duration,
910
};
@@ -211,6 +212,10 @@ wasmtime_option_group! {
211212
#[serde(default)]
212213
#[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
213214
pub pooling_pagemap_scan: Option<wasmtime::Enabled>,
215+
216+
/// XXX: For internal fuzzing and debugging use only!
217+
#[doc(hidden)]
218+
pub gc_zeal_alloc_counter: Option<NonZeroU32>,
214219
}
215220

216221
enum Optimize {
@@ -921,6 +926,12 @@ impl CommonOptions {
921926
config.table_lazy_init(enable);
922927
}
923928

929+
if let Some(n) = self.opts.gc_zeal_alloc_counter
930+
&& (cfg!(gc_zeal) || cfg!(fuzzing))
931+
{
932+
config.gc_zeal_alloc_counter(Some(n))?;
933+
}
934+
924935
// If fuel has been configured, set the `consume fuel` flag on the config.
925936
if self.wasm.fuel.is_some() {
926937
config.consume_fuel(true);

crates/cli-flags/src/opt.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use crate::{KeyValuePair, WasiNnGraph};
99
use clap::builder::{StringValueParser, TypedValueParser, ValueParserFactory};
1010
use clap::error::{Error, ErrorKind};
1111
use serde::de::{self, Visitor};
12+
use std::num::NonZeroU32;
1213
use std::path::PathBuf;
1314
use std::str::FromStr;
1415
use std::time::Duration;
1516
use std::{fmt, marker};
16-
use wasmtime::{Result, bail};
17+
use wasmtime::{Result, bail, format_err};
1718

1819
/// Characters which can be safely ignored while parsing numeric options to wasmtime
1920
const IGNORED_NUMBER_CHARS: [char; 1] = ['_'];
@@ -375,6 +376,19 @@ impl WasmtimeOptionValue for u32 {
375376
}
376377
}
377378

379+
impl WasmtimeOptionValue for NonZeroU32 {
380+
const VAL_HELP: &'static str = "=N";
381+
382+
fn parse(val: Option<&str>) -> Result<Self> {
383+
let n = <u32 as WasmtimeOptionValue>::parse(val)?;
384+
NonZeroU32::new(n).ok_or_else(|| format_err!("value must be non-zero"))
385+
}
386+
387+
fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
388+
write!(f, "{self}")
389+
}
390+
}
391+
378392
impl WasmtimeOptionValue for u64 {
379393
const VAL_HELP: &'static str = "=N";
380394
fn parse(val: Option<&str>) -> Result<Self> {

crates/fuzzing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ features = [
5959
'pulley',
6060
'threads',
6161
'parallel-compilation',
62+
'stack-switching',
6263
]
6364

6465
# We rely on precompiled v8 binaries, but rusty-v8 doesn't have a precompiled

crates/fuzzing/src/generators/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::{AsyncConfig, CodegenSettings, InstanceAllocationStrategy, MemoryConfig, ModuleConfig};
44
use crate::oracles::{StoreLimits, Timeout};
55
use arbitrary::{Arbitrary, Unstructured};
6+
use std::num::NonZeroU32;
67
use std::time::Duration;
78
use wasmtime::Result;
89
use wasmtime::{Enabled, Engine, Module, Store};
@@ -151,7 +152,7 @@ impl Config {
151152
hogs_memory: _,
152153
nan_canonicalization: _,
153154
gc_types: _,
154-
stack_switching: _,
155+
stack_switching,
155156
spec_test: _,
156157
} = test.config;
157158

@@ -171,6 +172,7 @@ impl Config {
171172
self.module_config.component_model_map = component_model_map.unwrap_or(false);
172173
self.module_config.component_model_fixed_length_lists =
173174
component_model_fixed_length_lists.unwrap_or(false);
175+
self.module_config.stack_switching = stack_switching.unwrap_or(false);
174176

175177
// Enable/disable proposals that wasm-smith has knobs for which will be
176178
// read when creating `wasmtime::Config`.
@@ -290,6 +292,10 @@ impl Config {
290292
16 << 20,
291293
self.wasmtime.memory_guaranteed_dense_image_size,
292294
));
295+
cfg.opts.gc_zeal_alloc_counter = self
296+
.wasmtime
297+
.gc_zeal_alloc_counter
298+
.map(|c| c.clamp(NonZeroU32::new(1).unwrap(), NonZeroU32::new(1024).unwrap()));
293299
cfg.wasm.async_stack_zeroing = Some(self.wasmtime.async_stack_zeroing);
294300
cfg.wasm.bulk_memory = Some(self.module_config.config.bulk_memory_enabled);
295301
cfg.wasm.component_model_async = Some(self.module_config.component_model_async);
@@ -615,6 +621,7 @@ pub struct WasmtimeConfig {
615621
/// Configuration for the compiler to use.
616622
pub compiler_strategy: CompilerStrategy,
617623
collector: Collector,
624+
gc_zeal_alloc_counter: Option<NonZeroU32>,
618625
table_lazy_init: bool,
619626

620627
/// Configuration for whether wasm is invoked in an async fashion and how

0 commit comments

Comments
 (0)