Uses this configuration and the supplied source of data to generate a Wasm module. If a `default_fuel` is provided, the resulting module will be configured to ensure termination; as doing so will add an additional global to the module, the pooling allocator, if configured, must also have its globals limit updated.
(
&self,
input: &mut Unstructured<'_>,
default_fuel: Option<u32>,
)
| 104 | /// module, the pooling allocator, if configured, must also have its globals |
| 105 | /// limit updated. |
| 106 | pub fn generate( |
| 107 | &self, |
| 108 | input: &mut Unstructured<'_>, |
| 109 | default_fuel: Option<u32>, |
| 110 | ) -> arbitrary::Result<wasm_smith::Module> { |
| 111 | crate::init_fuzzing(); |
| 112 | |
| 113 | // If requested, save `*.{dna,json}` files for recreating this module |
| 114 | // in wasm-tools alone. |
| 115 | let input_before = if log::log_enabled!(log::Level::Debug) { |
| 116 | let len = input.len(); |
| 117 | Some(input.peek_bytes(len).unwrap().to_vec()) |
| 118 | } else { |
| 119 | None |
| 120 | }; |
| 121 | |
| 122 | let mut module = wasm_smith::Module::new(self.config.clone(), input)?; |
| 123 | |
| 124 | if let Some(before) = input_before { |
| 125 | static GEN_CNT: AtomicUsize = AtomicUsize::new(0); |
| 126 | let used = before.len() - input.len(); |
| 127 | let i = GEN_CNT.fetch_add(1, Relaxed); |
| 128 | let dna = format!("testcase{i}.dna"); |
| 129 | let config = format!("testcase{i}.json"); |
| 130 | log::debug!("writing `{dna}` and `{config}`"); |
| 131 | std::fs::write(&dna, &before[..used]).unwrap(); |
| 132 | std::fs::write(&config, serde_json::to_string_pretty(&self.config).unwrap()).unwrap(); |
| 133 | } |
| 134 | |
| 135 | if let Some(default_fuel) = default_fuel { |
| 136 | module.ensure_termination(default_fuel).unwrap(); |
| 137 | } |
| 138 | |
| 139 | Ok(module) |
| 140 | } |
| 141 | } |