wasm_smith

Struct Config

Source
pub struct Config {
Show 63 fields pub available_imports: Option<Vec<u8>>, pub exports: Option<Vec<u8>>, pub allow_start_export: bool, pub allowed_instructions: InstructionKinds, pub allow_floats: bool, pub bulk_memory_enabled: bool, pub canonicalize_nans: bool, pub disallow_traps: bool, pub exceptions_enabled: bool, pub export_everything: bool, pub gc_enabled: bool, pub custom_page_sizes_enabled: bool, pub generate_custom_sections: bool, pub max_aliases: usize, pub max_components: usize, pub max_data_segments: usize, pub max_element_segments: usize, pub max_elements: usize, pub max_exports: usize, pub max_funcs: usize, pub max_globals: usize, pub max_imports: usize, pub max_instances: usize, pub max_instructions: usize, pub max_memories: usize, pub max_memory32_bytes: u64, pub max_memory64_bytes: u128, pub max_modules: usize, pub max_nesting_depth: usize, pub max_table_elements: u64, pub max_tables: usize, pub max_tags: usize, pub max_type_size: u32, pub max_types: usize, pub max_values: usize, pub memory64_enabled: bool, pub memory_max_size_required: bool, pub memory_offset_choices: MemoryOffsetChoices, pub min_data_segments: usize, pub min_element_segments: usize, pub min_elements: usize, pub min_exports: usize, pub min_funcs: usize, pub min_globals: usize, pub min_imports: usize, pub min_memories: u32, pub min_tables: u32, pub min_tags: usize, pub min_types: usize, pub min_uleb_size: u8, pub multi_value_enabled: bool, pub reference_types_enabled: bool, pub relaxed_simd_enabled: bool, pub saturating_float_to_int_enabled: bool, pub sign_extension_ops_enabled: bool, pub shared_everything_threads_enabled: bool, pub simd_enabled: bool, pub tail_call_enabled: bool, pub table_max_size_required: bool, pub threads_enabled: bool, pub allow_invalid_funcs: bool, pub wide_arithmetic_enabled: bool, pub extended_const_enabled: bool,
}
Expand description

Configuration for a generated module.

Don’t care to configure your generated modules? Just use Module::arbitrary, which internally uses the default configuration.

Want control over the shape of the module that gets generated? Create a Config and then pass it to Module::new.

§Swarm Testing

You can use the Arbitrary for Config implementation for swarm testing. This will dynamically – but still deterministically – choose configuration options for you.

Note that we pick only maximums, not minimums, here because it is more complex to describe the domain of valid configs when minima are involved (min <= max for each variable) and minima are mostly used to ensure certain elements are present, but do not widen the range of generated Wasm modules.

Fields§

§available_imports: Option<Vec<u8>>

The imports that may be used when generating the module.

Defaults to None which means that any arbitrary import can be generated.

To only allow specific imports, set this field to a WebAssembly module which describes the imports allowed.

Note that Self::min_imports is ignored when available_imports are enabled.

The provided value must be a valid binary encoding of a WebAssembly module. wasm-smith will panic if the module cannot be parsed.

§Example

An implementation of this method could use the wat crate to provide a human-readable and maintainable description:

Some(wat::parse_str(r#"
    (module
        (import "env" "ping" (func (param i32)))
        (import "env" "pong" (func (result i32)))
        (import "env" "memory" (memory 1))
        (import "env" "table" (table 1))
        (import "env" "tag" (tag (param i32)))
    )
"#))
§exports: Option<Vec<u8>>

If provided, the generated module will have exports with exactly the same names and types as those in the provided WebAssembly module. The implementation (e.g. function bodies, global initializers) of each export in the generated module will be random and unrelated to the implementation in the provided module. Only globals and functions are supported.

Defaults to None which means arbitrary exports will be generated.

To specify which exports the generated modules should have, set this field to a WebAssembly module which describes the desired exports. To generate modules with varying exports that meet some constraints, consider randomly generating the value for this field.

The provided value must be a valid binary encoding of a WebAssembly module. wasm-smith will panic if the module cannot be parsed.

§Module Limits

All types, functions, globals, and exports that are needed to provide the required exports will be generated, even if it causes the resulting module to exceed the limits defined in Self::max_type_size, Self::max_types, Self::max_funcs, Self::max_globals, or Self::max_exports.

§Example

As for Self::available_imports, the wat crate can be used to provide an human-readable description of the desired exports:

Some(wat::parse_str(r#"
    (module
        (func (export "foo") (param i32) (result i64) unreachable)
        (global (export "bar") f32 f32.const 0)
    )
"#));
§allow_start_export: bool

Determines whether a start export may be included. Defaults to true.

§allowed_instructions: InstructionKinds

The kinds of instructions allowed in the generated wasm programs. Defaults to all.

The categories of instructions match the categories used by the WebAssembly specification; e.g., numeric, vector, control, memory, etc.

Additionally, we include finer-grained categories which exclude floating point instructions, e.g. [InstructionKind::NumericInt] is a subset of [InstructionKind::Numeric] consisting of all numeric instructions which don’t involve floats.

Note that modifying this setting is separate from the proposal flags; that is, if simd_enabled() == true but allowed_instruction() does not include vector instructions, the generated programs will not include these instructions but could contain vector types.

§allow_floats: bool

Determines whether we generate floating point instructions and types.

Defaults to true.

§bulk_memory_enabled: bool

Determines whether the bulk memory proposal is enabled for generating instructions.

Defaults to true.

§canonicalize_nans: bool

Returns whether NaN values are canonicalized after all f32/f64 operation. Defaults to false.

This can be useful when a generated wasm module is executed in multiple runtimes which may produce different NaN values. This ensures that the generated module will always use the same NaN representation for all instructions which have visible side effects, for example writing floats to memory or float-to-int bitcast instructions.

§disallow_traps: bool

Returns whether we should avoid generating code that will possibly trap.

For some trapping instructions, this will emit extra instructions to ensure they don’t trap, while some instructions will simply be excluded. In cases where we would run into a trap, we instead choose some arbitrary non-trapping behavior. For example, if we detect that a Load instruction would attempt to access out-of-bounds memory, we instead pretend the load succeeded and push 0 onto the stack.

One type of trap that we can’t currently avoid is StackOverflow. Even when disallow_traps is set to true, wasm-smith will eventually generate a program that infinitely recurses, causing the call stack to be exhausted.

Defaults to false.

§exceptions_enabled: bool

Determines whether the exception-handling proposal is enabled for generating instructions.

Defaults to true.

§export_everything: bool

Export all WebAssembly objects in the module. Defaults to false.

This overrides Config::min_exports and Config::max_exports.

§gc_enabled: bool

Determines whether the GC proposal is enabled when generating a Wasm module.

Defaults to true.

§custom_page_sizes_enabled: bool

Determines whether the custom-page-sizes proposal is enabled when generating a Wasm module.

Defaults to false.

§generate_custom_sections: bool

Returns whether we should generate custom sections or not. Defaults to false.

§max_aliases: usize

Returns the maximal size of the alias section. Defaults to 1000.

§max_components: usize

The maximum number of components to use. Defaults to 10.

This includes imported components.

Note that this is only relevant for components.

§max_data_segments: usize

The maximum number of data segments to generate. Defaults to 100.

§max_element_segments: usize

The maximum number of element segments to generate. Defaults to 100.

§max_elements: usize

The maximum number of elements within a segment to generate. Defaults to 100.

§max_exports: usize

The maximum number of exports to generate. Defaults to 100.

§max_funcs: usize

The maximum number of functions to generate. Defaults to 100. This includes imported functions.

§max_globals: usize

The maximum number of globals to generate. Defaults to 100. This includes imported globals.

§max_imports: usize

The maximum number of imports to generate. Defaults to 100.

§max_instances: usize

The maximum number of instances to use. Defaults to 10.

This includes imported instances.

Note that this is only relevant for components.

§max_instructions: usize

The maximum number of instructions to generate in a function body. Defaults to 100.

Note that some additional ends, elses, and unreachables may be appended to the function body to finish block scopes.

§max_memories: usize

The maximum number of memories to use. Defaults to 1.

This includes imported memories.

Note that more than one memory is in the realm of the multi-memory wasm proposal.

§max_memory32_bytes: u64

The maximum, in bytes, of any 32-bit memory’s initial or maximum size.

May not be larger than 2**32.

Defaults to 2**32.

§max_memory64_bytes: u128

The maximum, in bytes, of any 64-bit memory’s initial or maximum size.

May not be larger than 2**64.

Defaults to 2**64.

§max_modules: usize

The maximum number of modules to use. Defaults to 10.

This includes imported modules.

Note that this is only relevant for components.

§max_nesting_depth: usize

Returns the maximal nesting depth of modules with the component model proposal. Defaults to 10.

§max_table_elements: u64

The maximum, elements, of any table’s initial or maximum size. Defaults to 1 million.

§max_tables: usize

The maximum number of tables to use. Defaults to 1.

This includes imported tables.

Note that more than one table is in the realm of the reference types proposal.

§max_tags: usize

The maximum number of tags to generate. Defaults to 100.

§max_type_size: u32

Returns the maximal effective size of any type generated by wasm-smith.

Note that this number is roughly in units of “how many types would be needed to represent the recursive type”. A function with 8 parameters and 2 results would take 11 types (one for the type, 10 for params/results). A module type with 2 imports and 3 exports would take 6 (module + imports + exports) plus the size of each import/export type. This is a somewhat rough measurement that is not intended to be very precise.

Defaults to 1000.

§max_types: usize

The maximum number of types to generate. Defaults to 100.

§max_values: usize

The maximum number of values to use. Defaults to 10.

This includes imported values.

Note that this is irrelevant unless value model support is enabled.

§memory64_enabled: bool

Returns whether 64-bit memories are allowed. Defaults to true.

Note that this is the gate for the memory64 proposal to WebAssembly.

§memory_max_size_required: bool

Whether every Wasm memory must have a maximum size specified. Defaults to false.

§memory_offset_choices: MemoryOffsetChoices

Control the probability of generating memory offsets that are in bounds vs. potentially out of bounds.

See the MemoryOffsetChoices struct for details.

§min_data_segments: usize

The minimum number of data segments to generate. Defaults to 0.

§min_element_segments: usize

The minimum number of element segments to generate. Defaults to 0.

§min_elements: usize

The minimum number of elements within a segment to generate. Defaults to 0.

§min_exports: usize

The minimum number of exports to generate. Defaults to 0.

§min_funcs: usize

The minimum number of functions to generate. Defaults to 0.

This includes imported functions.

§min_globals: usize

The minimum number of globals to generate. Defaults to 0.

This includes imported globals.

§min_imports: usize

The minimum number of imports to generate. Defaults to 0.

Note that if the sum of the maximum function1, table, global and memory counts is less than the minimum number of imports, then it will not be possible to satisfy all constraints (because imports count against the limits for those element kinds). In that case, we strictly follow the max-constraints, and can fail to satisfy this minimum number.


  1. the maximum number of functions is also limited by the number of function types arbitrarily chosen; strictly speaking, then, the maximum number of imports that can be created due to max-constraints is sum(min(num_func_types, max_funcs), max_tables, max_globals, max_memories)

§min_memories: u32

The minimum number of memories to use. Defaults to 0.

This includes imported memories.

§min_tables: u32

The minimum number of tables to use. Defaults to 0.

This includes imported tables.

§min_tags: usize

The minimum number of tags to generate. Defaults to 0.

§min_types: usize

The minimum number of types to generate. Defaults to 0.

§min_uleb_size: u8

The minimum size, in bytes, of all leb-encoded integers. Defaults to 1.

This is useful for ensuring that all leb-encoded integers are decoded as such rather than as simply one byte. This will forcibly extend leb integers with an over-long encoding in some locations if the size would otherwise be smaller than number returned here.

§multi_value_enabled: bool

Determines whether the multi-value results are enabled.

Defaults to true.

§reference_types_enabled: bool

Determines whether the reference types proposal is enabled for generating instructions.

Defaults to true.

§relaxed_simd_enabled: bool

Determines whether the Relaxed SIMD proposal is enabled for generating instructions.

Defaults to true.

§saturating_float_to_int_enabled: bool

Determines whether the non-trapping float-to-int conversions proposal is enabled.

Defaults to true.

§sign_extension_ops_enabled: bool

Determines whether the sign-extension-ops proposal is enabled.

Defaults to true.

§shared_everything_threads_enabled: bool

Determines whether the shared-everything-threads proposal is enabled.

The shared-everything-threads proposal, among other things, extends shared attributes to all WebAssembly objects; it builds on the threads proposal.

Defaults to false.

§simd_enabled: bool

Determines whether the SIMD proposal is enabled for generating instructions.

Defaults to true.

§tail_call_enabled: bool

Determines whether the tail calls proposal is enabled for generating instructions.

Defaults to true.

§table_max_size_required: bool

Whether every Wasm table must have a maximum size specified. Defaults to false.

§threads_enabled: bool

Determines whether the threads proposal is enabled.

The threads proposal involves shared linear memory, new atomic instructions, and new wait and notify instructions.

Defaults to true.

§allow_invalid_funcs: bool

Indicates whether wasm-smith is allowed to generate invalid function bodies.

When enabled this option will enable taking raw bytes from the input byte stream and using them as a wasm function body. This means that the output module is not guaranteed to be valid but can help tickle various parts of validation/compilation in some circumstances as well.

Defaults to false.

§wide_arithmetic_enabled: bool

Determines whether the wide-arithmetic proposal is enabled.

Defaults to false.

§extended_const_enabled: bool

Determines whether the extended-const proposal is enabled.

Defaults to true.

Implementations§

Source§

impl Config

Source

pub fn features(&self) -> WasmFeatures

Available on crate feature wasmparser only.

Returns the set of features that are necessary for validating against this Config.

Trait Implementations§

Source§

impl<'a> Arbitrary<'a> for Config

Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.