Coverage Report

Created: 2026-03-26 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/wasm-smith-0.245.1/src/lib.rs
Line
Count
Source
1
//! A WebAssembly test case generator.
2
//!
3
//! ## Usage
4
//!
5
//! First, use [`cargo fuzz`](https://github.com/rust-fuzz/cargo-fuzz) to define
6
//! a new fuzz target:
7
//!
8
//! ```shell
9
//! $ cargo fuzz add my_wasm_smith_fuzz_target
10
//! ```
11
//!
12
//! Next, add `wasm-smith` to your dependencies:
13
//!
14
//! ```shell
15
//! $ cargo add wasm-smith
16
//! ```
17
//!
18
//! Then, define your fuzz target so that it takes arbitrary
19
//! `wasm_smith::Module`s as an argument, convert the module into serialized
20
//! Wasm bytes via the `to_bytes` method, and then feed it into your system:
21
//!
22
//! ```no_run
23
//! // fuzz/fuzz_targets/my_wasm_smith_fuzz_target.rs
24
//!
25
//! #![no_main]
26
//!
27
//! # #[cfg(not(target_family = "wasm"))] mod x {
28
//! use libfuzzer_sys::fuzz_target;
29
//! use wasm_smith::Module;
30
//!
31
//! fuzz_target!(|module: Module| {
32
//!     let wasm_bytes = module.to_bytes();
33
//!
34
//!     // Your code here...
35
//! });
36
//! # }
37
//! ```
38
//!
39
//! Finally, start fuzzing:
40
//!
41
//! ```shell
42
//! $ cargo fuzz run my_wasm_smith_fuzz_target
43
//! ```
44
//!
45
//! > **Note:** For a real world example, also check out [the `validate` fuzz
46
//! > target](https://github.com/bytecodealliance/wasm-tools/blob/main/fuzz/src/validate.rs)
47
//! > defined in this repository. Using the `wasmparser` crate, it checks that
48
//! > every module generated by `wasm-smith` validates successfully.
49
//!
50
//! ## Design
51
//!
52
//! The design and implementation strategy of wasm-smith is outlined in
53
//! [this article](https://fitzgeraldnick.com/2020/08/24/writing-a-test-case-generator.html).
54
55
#![cfg_attr(docsrs, feature(doc_cfg))]
56
#![deny(missing_docs, missing_debug_implementations)]
57
// Needed for the `instructions!` macro in `src/code_builder.rs`.
58
#![recursion_limit = "1024"]
59
60
#[cfg(feature = "component-model")]
61
mod component;
62
mod config;
63
mod core;
64
65
pub use crate::core::{InstructionKind, InstructionKinds, Module};
66
use arbitrary::{Result, Unstructured};
67
#[cfg(feature = "component-model")]
68
pub use component::Component;
69
pub use config::{Config, MemoryOffsetChoices};
70
use std::{collections::HashSet, fmt::Write, str};
71
use wasm_encoder::MemoryType;
72
73
#[doc(hidden)]
74
pub use config::InternalOptionalConfig;
75
76
1.26M
pub(crate) fn page_size(mem: &MemoryType) -> u32 {
77
    const DEFAULT_WASM_PAGE_SIZE_LOG2: u32 = 16;
78
1.26M
    1 << mem.page_size_log2.unwrap_or(DEFAULT_WASM_PAGE_SIZE_LOG2)
79
1.26M
}
wasm_smith::page_size
Line
Count
Source
76
468k
pub(crate) fn page_size(mem: &MemoryType) -> u32 {
77
    const DEFAULT_WASM_PAGE_SIZE_LOG2: u32 = 16;
78
468k
    1 << mem.page_size_log2.unwrap_or(DEFAULT_WASM_PAGE_SIZE_LOG2)
79
468k
}
wasm_smith::page_size
Line
Count
Source
76
799k
pub(crate) fn page_size(mem: &MemoryType) -> u32 {
77
    const DEFAULT_WASM_PAGE_SIZE_LOG2: u32 = 16;
78
799k
    1 << mem.page_size_log2.unwrap_or(DEFAULT_WASM_PAGE_SIZE_LOG2)
79
799k
}
80
81
/// Do something an arbitrary number of times.
82
///
83
/// The callback can return `false` to exit the loop early.
84
736k
pub(crate) fn arbitrary_loop<'a>(
85
736k
    u: &mut Unstructured<'a>,
86
736k
    min: usize,
87
736k
    max: usize,
88
736k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
736k
) -> Result<()> {
90
736k
    assert!(max >= min);
91
736k
    for _ in 0..min {
92
7.19k
        if !f(u)? {
93
2
            return Err(arbitrary::Error::IncorrectFormat);
94
7.18k
        }
95
    }
96
736k
    for _ in 0..(max - min) {
97
17.6M
        let keep_going = u.arbitrary().unwrap_or(false);
98
17.6M
        if !keep_going {
99
425k
            break;
100
17.2M
        }
101
102
17.2M
        if !f(u)? {
103
271
            break;
104
17.2M
        }
105
    }
106
107
736k
    Ok(())
108
736k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}::{closure#2}>
Line
Count
Source
84
31.5k
pub(crate) fn arbitrary_loop<'a>(
85
31.5k
    u: &mut Unstructured<'a>,
86
31.5k
    min: usize,
87
31.5k
    max: usize,
88
31.5k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
31.5k
) -> Result<()> {
90
31.5k
    assert!(max >= min);
91
31.5k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
31.5k
    for _ in 0..(max - min) {
97
1.28M
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.28M
        if !keep_going {
99
12.1k
            break;
100
1.27M
        }
101
102
1.27M
        if !f(u)? {
103
0
            break;
104
1.27M
        }
105
    }
106
107
31.5k
    Ok(())
108
31.5k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}::{closure#3}>
Line
Count
Source
84
28.9k
pub(crate) fn arbitrary_loop<'a>(
85
28.9k
    u: &mut Unstructured<'a>,
86
28.9k
    min: usize,
87
28.9k
    max: usize,
88
28.9k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
28.9k
) -> Result<()> {
90
28.9k
    assert!(max >= min);
91
28.9k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
28.9k
    for _ in 0..(max - min) {
97
2.21M
        let keep_going = u.arbitrary().unwrap_or(false);
98
2.21M
        if !keep_going {
99
8.73k
            break;
100
2.20M
        }
101
102
2.20M
        if !f(u)? {
103
0
            break;
104
2.20M
        }
105
    }
106
107
28.9k
    Ok(())
108
28.9k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_data::{closure#4}>
Line
Count
Source
84
1.22k
pub(crate) fn arbitrary_loop<'a>(
85
1.22k
    u: &mut Unstructured<'a>,
86
1.22k
    min: usize,
87
1.22k
    max: usize,
88
1.22k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.22k
) -> Result<()> {
90
1.22k
    assert!(max >= min);
91
1.22k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.22k
    for _ in 0..(max - min) {
97
23.0k
        let keep_going = u.arbitrary().unwrap_or(false);
98
23.0k
        if !keep_going {
99
1.16k
            break;
100
21.8k
        }
101
102
21.8k
        if !f(u)? {
103
0
            break;
104
21.8k
        }
105
    }
106
107
1.22k
    Ok(())
108
1.22k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_tags::{closure#0}>
Line
Count
Source
84
480
pub(crate) fn arbitrary_loop<'a>(
85
480
    u: &mut Unstructured<'a>,
86
480
    min: usize,
87
480
    max: usize,
88
480
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
480
) -> Result<()> {
90
480
    assert!(max >= min);
91
480
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
480
    for _ in 0..(max - min) {
97
29.3k
        let keep_going = u.arbitrary().unwrap_or(false);
98
29.3k
        if !keep_going {
99
431
            break;
100
28.9k
        }
101
102
28.9k
        if !f(u)? {
103
0
            break;
104
28.9k
        }
105
    }
106
107
480
    Ok(())
108
480
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}>
Line
Count
Source
84
1.13k
pub(crate) fn arbitrary_loop<'a>(
85
1.13k
    u: &mut Unstructured<'a>,
86
1.13k
    min: usize,
87
1.13k
    max: usize,
88
1.13k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.13k
) -> Result<()> {
90
1.13k
    assert!(max >= min);
91
1.13k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.13k
    for _ in 0..(max - min) {
97
63.8k
        let keep_going = u.arbitrary().unwrap_or(false);
98
63.8k
        if !keep_going {
99
1.01k
            break;
100
62.8k
        }
101
102
62.8k
        if !f(u)? {
103
0
            break;
104
62.8k
        }
105
    }
106
107
1.13k
    Ok(())
108
1.13k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_funcs::{closure#1}>
Line
Count
Source
84
1.35k
pub(crate) fn arbitrary_loop<'a>(
85
1.35k
    u: &mut Unstructured<'a>,
86
1.35k
    min: usize,
87
1.35k
    max: usize,
88
1.35k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.35k
) -> Result<()> {
90
1.35k
    assert!(max >= min);
91
1.35k
    for _ in 0..min {
92
1.35k
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
1.35k
        }
95
    }
96
1.35k
    for _ in 0..(max - min) {
97
109k
        let keep_going = u.arbitrary().unwrap_or(false);
98
109k
        if !keep_going {
99
1.14k
            break;
100
108k
        }
101
102
108k
        if !f(u)? {
103
0
            break;
104
108k
        }
105
    }
106
107
1.35k
    Ok(())
108
1.35k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_locals::{closure#0}>
Line
Count
Source
84
109k
pub(crate) fn arbitrary_loop<'a>(
85
109k
    u: &mut Unstructured<'a>,
86
109k
    min: usize,
87
109k
    max: usize,
88
109k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
109k
) -> Result<()> {
90
109k
    assert!(max >= min);
91
109k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
109k
    for _ in 0..(max - min) {
97
1.27M
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.27M
        if !keep_going {
99
99.8k
            break;
100
1.17M
        }
101
102
1.17M
        if !f(u)? {
103
0
            break;
104
1.17M
        }
105
    }
106
107
109k
    Ok(())
108
109k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_tables::{closure#0}>
Line
Count
Source
84
1.39k
pub(crate) fn arbitrary_loop<'a>(
85
1.39k
    u: &mut Unstructured<'a>,
86
1.39k
    min: usize,
87
1.39k
    max: usize,
88
1.39k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.39k
) -> Result<()> {
90
1.39k
    assert!(max >= min);
91
1.39k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.39k
    for _ in 0..(max - min) {
97
17.2k
        let keep_going = u.arbitrary().unwrap_or(false);
98
17.2k
        if !keep_going {
99
825
            break;
100
16.4k
        }
101
102
16.4k
        if !f(u)? {
103
0
            break;
104
16.4k
        }
105
    }
106
107
1.39k
    Ok(())
108
1.39k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_exports::{closure#4}>
Line
Count
Source
84
1.39k
pub(crate) fn arbitrary_loop<'a>(
85
1.39k
    u: &mut Unstructured<'a>,
86
1.39k
    min: usize,
87
1.39k
    max: usize,
88
1.39k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.39k
) -> Result<()> {
90
1.39k
    assert!(max >= min);
91
1.39k
    for _ in 0..min {
92
1.39k
        if !f(u)? {
93
1
            return Err(arbitrary::Error::IncorrectFormat);
94
1.39k
        }
95
    }
96
1.39k
    for _ in 0..(max - min) {
97
64.0k
        let keep_going = u.arbitrary().unwrap_or(false);
98
64.0k
        if !keep_going {
99
1.14k
            break;
100
62.9k
        }
101
102
62.9k
        if !f(u)? {
103
122
            break;
104
62.8k
        }
105
    }
106
107
1.39k
    Ok(())
108
1.39k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_globals::{closure#0}>
Line
Count
Source
84
1.39k
pub(crate) fn arbitrary_loop<'a>(
85
1.39k
    u: &mut Unstructured<'a>,
86
1.39k
    min: usize,
87
1.39k
    max: usize,
88
1.39k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.39k
) -> Result<()> {
90
1.39k
    assert!(max >= min);
91
1.39k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.39k
    for _ in 0..(max - min) {
97
83.9k
        let keep_going = u.arbitrary().unwrap_or(false);
98
83.9k
        if !keep_going {
99
1.19k
            break;
100
82.7k
        }
101
102
82.7k
        if !f(u)? {
103
0
            break;
104
82.7k
        }
105
    }
106
107
1.39k
    Ok(())
108
1.39k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_imports::{closure#0}>
Line
Count
Source
84
1.39k
pub(crate) fn arbitrary_loop<'a>(
85
1.39k
    u: &mut Unstructured<'a>,
86
1.39k
    min: usize,
87
1.39k
    max: usize,
88
1.39k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.39k
) -> Result<()> {
90
1.39k
    assert!(max >= min);
91
1.39k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.39k
    for _ in 0..(max - min) {
97
0
        let keep_going = u.arbitrary().unwrap_or(false);
98
0
        if !keep_going {
99
0
            break;
100
0
        }
101
102
0
        if !f(u)? {
103
0
            break;
104
0
        }
105
    }
106
107
1.39k
    Ok(())
108
1.39k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_memories::{closure#0}>
Line
Count
Source
84
1.39k
pub(crate) fn arbitrary_loop<'a>(
85
1.39k
    u: &mut Unstructured<'a>,
86
1.39k
    min: usize,
87
1.39k
    max: usize,
88
1.39k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.39k
) -> Result<()> {
90
1.39k
    assert!(max >= min);
91
1.39k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.39k
    for _ in 0..(max - min) {
97
1.39k
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.39k
        if !keep_going {
99
483
            break;
100
909
        }
101
102
909
        if !f(u)? {
103
0
            break;
104
909
        }
105
    }
106
107
1.39k
    Ok(())
108
1.39k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_func_type::{closure#0}>
Line
Count
Source
84
67.0k
pub(crate) fn arbitrary_loop<'a>(
85
67.0k
    u: &mut Unstructured<'a>,
86
67.0k
    min: usize,
87
67.0k
    max: usize,
88
67.0k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
67.0k
) -> Result<()> {
90
67.0k
    assert!(max >= min);
91
67.0k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
67.0k
    for _ in 0..(max - min) {
97
844k
        let keep_going = u.arbitrary().unwrap_or(false);
98
844k
        if !keep_going {
99
29.3k
            break;
100
815k
        }
101
102
815k
        if !f(u)? {
103
0
            break;
104
815k
        }
105
    }
106
107
67.0k
    Ok(())
108
67.0k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_func_type::{closure#1}>
Line
Count
Source
84
67.0k
pub(crate) fn arbitrary_loop<'a>(
85
67.0k
    u: &mut Unstructured<'a>,
86
67.0k
    min: usize,
87
67.0k
    max: usize,
88
67.0k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
67.0k
) -> Result<()> {
90
67.0k
    assert!(max >= min);
91
67.0k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
67.0k
    for _ in 0..(max - min) {
97
775k
        let keep_going = u.arbitrary().unwrap_or(false);
98
775k
        if !keep_going {
99
21.1k
            break;
100
753k
        }
101
102
753k
        if !f(u)? {
103
0
            break;
104
753k
        }
105
    }
106
107
67.0k
    Ok(())
108
67.0k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}::{closure#2}>
Line
Count
Source
84
18.8k
pub(crate) fn arbitrary_loop<'a>(
85
18.8k
    u: &mut Unstructured<'a>,
86
18.8k
    min: usize,
87
18.8k
    max: usize,
88
18.8k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
18.8k
) -> Result<()> {
90
18.8k
    assert!(max >= min);
91
18.8k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
18.8k
    for _ in 0..(max - min) {
97
528k
        let keep_going = u.arbitrary().unwrap_or(false);
98
528k
        if !keep_going {
99
12.9k
            break;
100
515k
        }
101
102
515k
        if !f(u)? {
103
0
            break;
104
515k
        }
105
    }
106
107
18.8k
    Ok(())
108
18.8k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}::{closure#3}>
Line
Count
Source
84
42.4k
pub(crate) fn arbitrary_loop<'a>(
85
42.4k
    u: &mut Unstructured<'a>,
86
42.4k
    min: usize,
87
42.4k
    max: usize,
88
42.4k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
42.4k
) -> Result<()> {
90
42.4k
    assert!(max >= min);
91
42.4k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
42.4k
    for _ in 0..(max - min) {
97
5.82M
        let keep_going = u.arbitrary().unwrap_or(false);
98
5.82M
        if !keep_going {
99
19.2k
            break;
100
5.80M
        }
101
102
5.80M
        if !f(u)? {
103
0
            break;
104
5.80M
        }
105
    }
106
107
42.4k
    Ok(())
108
42.4k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_data::{closure#4}>
Line
Count
Source
84
1.97k
pub(crate) fn arbitrary_loop<'a>(
85
1.97k
    u: &mut Unstructured<'a>,
86
1.97k
    min: usize,
87
1.97k
    max: usize,
88
1.97k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.97k
) -> Result<()> {
90
1.97k
    assert!(max >= min);
91
1.97k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.97k
    for _ in 0..(max - min) {
97
36.0k
        let keep_going = u.arbitrary().unwrap_or(false);
98
36.0k
        if !keep_going {
99
1.91k
            break;
100
34.1k
        }
101
102
34.1k
        if !f(u)? {
103
0
            break;
104
34.1k
        }
105
    }
106
107
1.97k
    Ok(())
108
1.97k
}
Unexecuted instantiation: wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_tags::{closure#0}>
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_elems::{closure#5}>
Line
Count
Source
84
1.84k
pub(crate) fn arbitrary_loop<'a>(
85
1.84k
    u: &mut Unstructured<'a>,
86
1.84k
    min: usize,
87
1.84k
    max: usize,
88
1.84k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
1.84k
) -> Result<()> {
90
1.84k
    assert!(max >= min);
91
1.84k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
1.84k
    for _ in 0..(max - min) {
97
64.8k
        let keep_going = u.arbitrary().unwrap_or(false);
98
64.8k
        if !keep_going {
99
1.70k
            break;
100
63.1k
        }
101
102
63.1k
        if !f(u)? {
103
0
            break;
104
63.1k
        }
105
    }
106
107
1.84k
    Ok(())
108
1.84k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_funcs::{closure#1}>
Line
Count
Source
84
2.20k
pub(crate) fn arbitrary_loop<'a>(
85
2.20k
    u: &mut Unstructured<'a>,
86
2.20k
    min: usize,
87
2.20k
    max: usize,
88
2.20k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.20k
) -> Result<()> {
90
2.20k
    assert!(max >= min);
91
2.20k
    for _ in 0..min {
92
2.20k
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
2.20k
        }
95
    }
96
2.20k
    for _ in 0..(max - min) {
97
150k
        let keep_going = u.arbitrary().unwrap_or(false);
98
150k
        if !keep_going {
99
1.97k
            break;
100
148k
        }
101
102
148k
        if !f(u)? {
103
0
            break;
104
148k
        }
105
    }
106
107
2.20k
    Ok(())
108
2.20k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_locals::{closure#0}>
Line
Count
Source
84
150k
pub(crate) fn arbitrary_loop<'a>(
85
150k
    u: &mut Unstructured<'a>,
86
150k
    min: usize,
87
150k
    max: usize,
88
150k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
150k
) -> Result<()> {
90
150k
    assert!(max >= min);
91
150k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
150k
    for _ in 0..(max - min) {
97
1.71M
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.71M
        if !keep_going {
99
137k
            break;
100
1.57M
        }
101
102
1.57M
        if !f(u)? {
103
0
            break;
104
1.57M
        }
105
    }
106
107
150k
    Ok(())
108
150k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_tables::{closure#0}>
Line
Count
Source
84
2.23k
pub(crate) fn arbitrary_loop<'a>(
85
2.23k
    u: &mut Unstructured<'a>,
86
2.23k
    min: usize,
87
2.23k
    max: usize,
88
2.23k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.23k
) -> Result<()> {
90
2.23k
    assert!(max >= min);
91
2.23k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
2.23k
    for _ in 0..(max - min) {
97
19.3k
        let keep_going = u.arbitrary().unwrap_or(false);
98
19.3k
        if !keep_going {
99
1.46k
            break;
100
17.8k
        }
101
102
17.8k
        if !f(u)? {
103
0
            break;
104
17.8k
        }
105
    }
106
107
2.23k
    Ok(())
108
2.23k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_exports::{closure#4}>
Line
Count
Source
84
2.23k
pub(crate) fn arbitrary_loop<'a>(
85
2.23k
    u: &mut Unstructured<'a>,
86
2.23k
    min: usize,
87
2.23k
    max: usize,
88
2.23k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.23k
) -> Result<()> {
90
2.23k
    assert!(max >= min);
91
2.23k
    for _ in 0..min {
92
2.23k
        if !f(u)? {
93
1
            return Err(arbitrary::Error::IncorrectFormat);
94
2.23k
        }
95
    }
96
2.23k
    for _ in 0..(max - min) {
97
77.4k
        let keep_going = u.arbitrary().unwrap_or(false);
98
77.4k
        if !keep_going {
99
1.94k
            break;
100
75.5k
        }
101
102
75.5k
        if !f(u)? {
103
149
            break;
104
75.3k
        }
105
    }
106
107
2.23k
    Ok(())
108
2.23k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_globals::{closure#0}>
Line
Count
Source
84
2.23k
pub(crate) fn arbitrary_loop<'a>(
85
2.23k
    u: &mut Unstructured<'a>,
86
2.23k
    min: usize,
87
2.23k
    max: usize,
88
2.23k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.23k
) -> Result<()> {
90
2.23k
    assert!(max >= min);
91
2.23k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
2.23k
    for _ in 0..(max - min) {
97
95.0k
        let keep_going = u.arbitrary().unwrap_or(false);
98
95.0k
        if !keep_going {
99
1.99k
            break;
100
93.0k
        }
101
102
93.0k
        if !f(u)? {
103
0
            break;
104
93.0k
        }
105
    }
106
107
2.23k
    Ok(())
108
2.23k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_imports::{closure#0}>
Line
Count
Source
84
2.23k
pub(crate) fn arbitrary_loop<'a>(
85
2.23k
    u: &mut Unstructured<'a>,
86
2.23k
    min: usize,
87
2.23k
    max: usize,
88
2.23k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.23k
) -> Result<()> {
90
2.23k
    assert!(max >= min);
91
2.23k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
2.23k
    for _ in 0..(max - min) {
97
0
        let keep_going = u.arbitrary().unwrap_or(false);
98
0
        if !keep_going {
99
0
            break;
100
0
        }
101
102
0
        if !f(u)? {
103
0
            break;
104
0
        }
105
    }
106
107
2.23k
    Ok(())
108
2.23k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_memories::{closure#0}>
Line
Count
Source
84
2.23k
pub(crate) fn arbitrary_loop<'a>(
85
2.23k
    u: &mut Unstructured<'a>,
86
2.23k
    min: usize,
87
2.23k
    max: usize,
88
2.23k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
2.23k
) -> Result<()> {
90
2.23k
    assert!(max >= min);
91
2.23k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
2.23k
    for _ in 0..(max - min) {
97
2.23k
        let keep_going = u.arbitrary().unwrap_or(false);
98
2.23k
        if !keep_going {
99
754
            break;
100
1.48k
        }
101
102
1.48k
        if !f(u)? {
103
0
            break;
104
1.48k
        }
105
    }
106
107
2.23k
    Ok(())
108
2.23k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_func_type::{closure#0}>
Line
Count
Source
84
96.2k
pub(crate) fn arbitrary_loop<'a>(
85
96.2k
    u: &mut Unstructured<'a>,
86
96.2k
    min: usize,
87
96.2k
    max: usize,
88
96.2k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
96.2k
) -> Result<()> {
90
96.2k
    assert!(max >= min);
91
96.2k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
96.2k
    for _ in 0..(max - min) {
97
1.25M
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.25M
        if !keep_going {
99
40.0k
            break;
100
1.21M
        }
101
102
1.21M
        if !f(u)? {
103
0
            break;
104
1.21M
        }
105
    }
106
107
96.2k
    Ok(())
108
96.2k
}
wasm_smith::arbitrary_loop::<<wasm_smith::core::Module>::arbitrary_func_type::{closure#1}>
Line
Count
Source
84
96.2k
pub(crate) fn arbitrary_loop<'a>(
85
96.2k
    u: &mut Unstructured<'a>,
86
96.2k
    min: usize,
87
96.2k
    max: usize,
88
96.2k
    mut f: impl FnMut(&mut Unstructured<'a>) -> Result<bool>,
89
96.2k
) -> Result<()> {
90
96.2k
    assert!(max >= min);
91
96.2k
    for _ in 0..min {
92
0
        if !f(u)? {
93
0
            return Err(arbitrary::Error::IncorrectFormat);
94
0
        }
95
    }
96
96.2k
    for _ in 0..(max - min) {
97
1.13M
        let keep_going = u.arbitrary().unwrap_or(false);
98
1.13M
        if !keep_going {
99
25.4k
            break;
100
1.10M
        }
101
102
1.10M
        if !f(u)? {
103
0
            break;
104
1.10M
        }
105
    }
106
107
96.2k
    Ok(())
108
96.2k
}
109
110
// Mirror what happens in `Arbitrary for String`, but do so with a clamped size.
111
146k
pub(crate) fn limited_str<'a>(max_size: usize, u: &mut Unstructured<'a>) -> Result<&'a str> {
112
146k
    let size = u.arbitrary_len::<u8>()?;
113
146k
    let size = std::cmp::min(size, max_size);
114
146k
    match str::from_utf8(u.peek_bytes(size).unwrap()) {
115
26.6k
        Ok(s) => {
116
26.6k
            u.bytes(size).unwrap();
117
26.6k
            Ok(s)
118
        }
119
120k
        Err(e) => {
120
120k
            let i = e.valid_up_to();
121
120k
            let valid = u.bytes(i).unwrap();
122
120k
            let s = str::from_utf8(valid).unwrap();
123
120k
            Ok(s)
124
        }
125
    }
126
146k
}
wasm_smith::limited_str
Line
Count
Source
111
66.2k
pub(crate) fn limited_str<'a>(max_size: usize, u: &mut Unstructured<'a>) -> Result<&'a str> {
112
66.2k
    let size = u.arbitrary_len::<u8>()?;
113
66.2k
    let size = std::cmp::min(size, max_size);
114
66.2k
    match str::from_utf8(u.peek_bytes(size).unwrap()) {
115
9.81k
        Ok(s) => {
116
9.81k
            u.bytes(size).unwrap();
117
9.81k
            Ok(s)
118
        }
119
56.4k
        Err(e) => {
120
56.4k
            let i = e.valid_up_to();
121
56.4k
            let valid = u.bytes(i).unwrap();
122
56.4k
            let s = str::from_utf8(valid).unwrap();
123
56.4k
            Ok(s)
124
        }
125
    }
126
66.2k
}
wasm_smith::limited_str
Line
Count
Source
111
80.5k
pub(crate) fn limited_str<'a>(max_size: usize, u: &mut Unstructured<'a>) -> Result<&'a str> {
112
80.5k
    let size = u.arbitrary_len::<u8>()?;
113
80.5k
    let size = std::cmp::min(size, max_size);
114
80.5k
    match str::from_utf8(u.peek_bytes(size).unwrap()) {
115
16.8k
        Ok(s) => {
116
16.8k
            u.bytes(size).unwrap();
117
16.8k
            Ok(s)
118
        }
119
63.7k
        Err(e) => {
120
63.7k
            let i = e.valid_up_to();
121
63.7k
            let valid = u.bytes(i).unwrap();
122
63.7k
            let s = str::from_utf8(valid).unwrap();
123
63.7k
            Ok(s)
124
        }
125
    }
126
80.5k
}
127
128
146k
pub(crate) fn limited_string(max_size: usize, u: &mut Unstructured) -> Result<String> {
129
146k
    Ok(limited_str(max_size, u)?.into())
130
146k
}
wasm_smith::limited_string
Line
Count
Source
128
66.2k
pub(crate) fn limited_string(max_size: usize, u: &mut Unstructured) -> Result<String> {
129
66.2k
    Ok(limited_str(max_size, u)?.into())
130
66.2k
}
wasm_smith::limited_string
Line
Count
Source
128
80.5k
pub(crate) fn limited_string(max_size: usize, u: &mut Unstructured) -> Result<String> {
129
80.5k
    Ok(limited_str(max_size, u)?.into())
130
80.5k
}
131
132
146k
pub(crate) fn unique_string(
133
146k
    max_size: usize,
134
146k
    names: &mut HashSet<String>,
135
146k
    u: &mut Unstructured,
136
146k
) -> Result<String> {
137
146k
    let mut name = limited_string(max_size, u)?;
138
270k
    while names.contains(&name) {
139
123k
        write!(&mut name, "{}", names.len()).unwrap();
140
123k
    }
141
146k
    names.insert(name.clone());
142
146k
    Ok(name)
143
146k
}
wasm_smith::unique_string
Line
Count
Source
132
66.2k
pub(crate) fn unique_string(
133
66.2k
    max_size: usize,
134
66.2k
    names: &mut HashSet<String>,
135
66.2k
    u: &mut Unstructured,
136
66.2k
) -> Result<String> {
137
66.2k
    let mut name = limited_string(max_size, u)?;
138
122k
    while names.contains(&name) {
139
56.0k
        write!(&mut name, "{}", names.len()).unwrap();
140
56.0k
    }
141
66.2k
    names.insert(name.clone());
142
66.2k
    Ok(name)
143
66.2k
}
wasm_smith::unique_string
Line
Count
Source
132
80.5k
pub(crate) fn unique_string(
133
80.5k
    max_size: usize,
134
80.5k
    names: &mut HashSet<String>,
135
80.5k
    u: &mut Unstructured,
136
80.5k
) -> Result<String> {
137
80.5k
    let mut name = limited_string(max_size, u)?;
138
147k
    while names.contains(&name) {
139
67.4k
        write!(&mut name, "{}", names.len()).unwrap();
140
67.4k
    }
141
80.5k
    names.insert(name.clone());
142
80.5k
    Ok(name)
143
80.5k
}
144
145
#[cfg(feature = "component-model")]
146
pub(crate) fn unique_kebab_string(
147
    max_size: usize,
148
    names: &mut HashSet<String>,
149
    u: &mut Unstructured,
150
) -> Result<String> {
151
    let size = std::cmp::min(u.arbitrary_len::<u8>()?, max_size);
152
    let mut name = String::with_capacity(size);
153
    let mut empty_segment = true;
154
    for i in 0..size {
155
        name.push(match u.int_in_range::<u8>(0..=36)? {
156
            x if (0..26).contains(&x) => {
157
                empty_segment = false;
158
                (b'a' + x) as char
159
            }
160
            x if (26..36).contains(&x) => {
161
                empty_segment = false;
162
                if i == 0 {
163
                    (b'a' + (x - 26)) as char
164
                } else {
165
                    (b'0' + (x - 26)) as char
166
                }
167
            }
168
            x if x == 36 => {
169
                if empty_segment {
170
                    empty_segment = false;
171
                    'a'
172
                } else {
173
                    empty_segment = true;
174
                    '-'
175
                }
176
            }
177
            _ => unreachable!(),
178
        });
179
    }
180
181
    if name.is_empty() || name.ends_with('-') {
182
        name.push('a');
183
    }
184
185
    while names.contains(&name) {
186
        write!(&mut name, "{}", names.len()).unwrap();
187
    }
188
189
    names.insert(name.clone());
190
191
    Ok(name)
192
}
193
194
#[cfg(feature = "component-model")]
195
pub(crate) fn unique_url(
196
    max_size: usize,
197
    names: &mut HashSet<String>,
198
    u: &mut Unstructured,
199
) -> Result<String> {
200
    let path = unique_kebab_string(max_size, names, u)?;
201
    Ok(format!("https://example.com/{path}"))
202
}