| 1 |
//! Configuring the shape of generated Wasm modules. |
| 2 |
|
| 3 |
use crate::InstructionKinds; |
| 4 |
use anyhow::bail; |
| 5 |
use arbitrary::{Arbitrary, Result, Unstructured}; |
| 6 |
|
| 7 |
macro_rules! define_config { |
| 8 |
( |
| 9 |
$(#[$attr:meta])* |
| 10 |
pub struct Config { |
| 11 |
$( |
| 12 |
$(#[$field_attr:meta])* |
| 13 |
pub $field:ident : $field_ty:ty = $default:expr, |
| 14 |
)* |
| 15 |
} |
| 16 |
) => { |
| 17 |
$(#[$attr])* |
| 18 |
pub struct Config { |
| 19 |
/// The imports that may be used when generating the module. |
| 20 |
/// |
| 21 |
/// Defaults to `None` which means that any arbitrary import can be |
| 22 |
/// generated. |
| 23 |
/// |
| 24 |
/// To only allow specific imports, set this field to a WebAssembly |
| 25 |
/// module which describes the imports allowed. |
| 26 |
/// |
| 27 |
/// Note that [`Self::min_imports`] is ignored when |
| 28 |
/// `available_imports` are enabled. |
| 29 |
/// |
| 30 |
/// The provided value must be a valid binary encoding of a |
| 31 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 32 |
/// be parsed. |
| 33 |
/// |
| 34 |
/// # Example |
| 35 |
/// |
| 36 |
/// An implementation of this method could use the `wat` crate to |
| 37 |
/// provide a human-readable and maintainable description: |
| 38 |
/// |
| 39 |
/// ```rust |
| 40 |
/// Some(wat::parse_str(r#" |
| 41 |
/// (module |
| 42 |
/// (import "env" "ping" (func (param i32))) |
| 43 |
/// (import "env" "pong" (func (result i32))) |
| 44 |
/// (import "env" "memory" (memory 1)) |
| 45 |
/// (import "env" "table" (table 1)) |
| 46 |
/// (import "env" "tag" (tag (param i32))) |
| 47 |
/// ) |
| 48 |
/// "#)) |
| 49 |
/// # ; |
| 50 |
/// ``` |
| 51 |
pub available_imports: Option<Vec<u8>>, |
| 52 |
|
| 53 |
/// If provided, the generated module will have exports with exactly |
| 54 |
/// the same names and types as those in the provided WebAssembly |
| 55 |
/// module. The implementation (e.g. function bodies, global |
| 56 |
/// initializers) of each export in the generated module will be |
| 57 |
/// random and unrelated to the implementation in the provided |
| 58 |
/// module. |
| 59 |
/// |
| 60 |
/// |
| 61 |
/// Defaults to `None` which means arbitrary exports will be |
| 62 |
/// generated. |
| 63 |
/// |
| 64 |
/// To specify which exports the generated modules should have, set |
| 65 |
/// this field to a WebAssembly module which describes the desired |
| 66 |
/// exports. To generate modules with varying exports that meet some |
| 67 |
/// constraints, consider randomly generating the value for this |
| 68 |
/// field. |
| 69 |
/// |
| 70 |
/// The provided value must be a valid binary encoding of a |
| 71 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 72 |
/// be parsed. |
| 73 |
/// |
| 74 |
/// # Module Limits |
| 75 |
/// |
| 76 |
/// All types, functions, globals, memories, tables, tags, and exports |
| 77 |
/// that are needed to provide the required exports will be generated, |
| 78 |
/// even if it causes the resulting module to exceed the limits defined |
| 79 |
/// in [`Self::max_type_size`], [`Self::max_types`], |
| 80 |
/// [`Self::max_funcs`], [`Self::max_globals`], |
| 81 |
/// [`Self::max_memories`], [`Self::max_tables`], |
| 82 |
/// [`Self::max_tags`], or [`Self::max_exports`]. |
| 83 |
/// |
| 84 |
/// # Example |
| 85 |
/// |
| 86 |
/// As for [`Self::available_imports`], the `wat` crate can be used |
| 87 |
/// to provide an human-readable description of the desired exports: |
| 88 |
/// |
| 89 |
/// ```rust |
| 90 |
/// Some(wat::parse_str(r#" |
| 91 |
/// (module |
| 92 |
/// (func (export "foo") (param i32) (result i64) unreachable) |
| 93 |
/// (global (export "bar") f32 f32.const 0) |
| 94 |
/// (memory (export "baz") 1 10) |
| 95 |
/// (table (export "qux") 5 10 (ref null extern)) |
| 96 |
/// (tag (export "quux") (param f32)) |
| 97 |
/// ) |
| 98 |
/// "#)); |
| 99 |
/// ``` |
| 100 |
pub exports: Option<Vec<u8>>, |
| 101 |
|
| 102 |
/// If provided, the generated module will have imports and exports |
| 103 |
/// with exactly the same names and types as those in the provided |
| 104 |
/// WebAssembly module. |
| 105 |
/// |
| 106 |
/// Defaults to `None` which means arbitrary imports and exports will be |
| 107 |
/// generated. |
| 108 |
/// |
| 109 |
/// Note that [`Self::available_imports`] and [`Self::exports`] are |
| 110 |
/// ignored when `module_shape` is enabled. |
| 111 |
/// |
| 112 |
/// The provided value must be a valid binary encoding of a |
| 113 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 114 |
/// be parsed. |
| 115 |
/// |
| 116 |
/// # Module Limits |
| 117 |
/// |
| 118 |
/// All types, functions, globals, memories, tables, tags, imports, and exports |
| 119 |
/// that are needed to provide the required imports and exports will be generated, |
| 120 |
/// even if it causes the resulting module to exceed the limits defined in |
| 121 |
/// [`Self::max_type_size`], [`Self::max_types`], [`Self::max_funcs`], |
| 122 |
/// [`Self::max_globals`], [`Self::max_memories`], [`Self::max_tables`], |
| 123 |
/// [`Self::max_tags`], [`Self::max_imports`], or [`Self::max_exports`]. |
| 124 |
/// |
| 125 |
/// # Example |
| 126 |
/// |
| 127 |
/// As for [`Self::available_imports`] and [`Self::exports`], the |
| 128 |
/// `wat` crate can be used to provide a human-readable description of the |
| 129 |
/// module shape: |
| 130 |
/// |
| 131 |
/// ```rust |
| 132 |
/// Some(wat::parse_str(r#" |
| 133 |
/// (module |
| 134 |
/// (import "env" "ping" (func (param i32))) |
| 135 |
/// (import "env" "memory" (memory 1)) |
| 136 |
/// (func (export "foo") (param anyref) (result structref) unreachable) |
| 137 |
/// (global (export "bar") arrayref (ref.null array)) |
| 138 |
/// ) |
| 139 |
/// "#)); |
| 140 |
/// ``` |
| 141 |
pub module_shape: Option<Vec<u8>>, |
| 142 |
|
| 143 |
$( |
| 144 |
$(#[$field_attr])* |
| 145 |
pub $field: $field_ty, |
| 146 |
)* |
| 147 |
} |
| 148 |
|
| 149 |
impl Default for Config { |
| 150 |
fn default() -> Config { |
| 151 |
Config { |
| 152 |
available_imports: None, |
| 153 |
exports: None, |
| 154 |
module_shape: None, |
| 155 |
|
| 156 |
$( |
| 157 |
$field: $default, |
| 158 |
)* |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
#[doc(hidden)] |
| 164 |
#[derive(Clone, Debug, Default)] |
| 165 |
#[cfg_attr(feature = "clap", derive(clap::Parser))] |
| 166 |
#[cfg_attr(feature = "serde", derive(serde_derive::Deserialize, serde_derive::Serialize))] |
| 167 |
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case", deny_unknown_fields))] |
| 168 |
pub struct InternalOptionalConfig { |
| 169 |
/// The imports that may be used when generating the module. |
| 170 |
/// |
| 171 |
/// When unspecified, any arbitrary import can be generated. |
| 172 |
/// |
| 173 |
/// To only allow specific imports, provide a file path of a |
| 174 |
/// WebAssembly module which describes the imports allowed. |
| 175 |
/// |
| 176 |
/// Note that [`Self::min_imports`] is ignored when |
| 177 |
/// `available_imports` are enabled. |
| 178 |
/// |
| 179 |
/// The provided value must be a valid binary encoding of a |
| 180 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 181 |
/// be parsed. |
| 182 |
#[cfg_attr(feature = "clap", clap(long))] |
| 183 |
available_imports: Option<std::path::PathBuf>, |
| 184 |
|
| 185 |
/// If provided, the generated module will have exports with exactly |
| 186 |
/// the same names and types as those in the provided WebAssembly |
| 187 |
/// module. The implementation (e.g. function bodies, global |
| 188 |
/// initializers) of each export in the generated module will be |
| 189 |
/// random and unrelated to the implementation in the provided |
| 190 |
/// module. |
| 191 |
/// |
| 192 |
/// Defaults to `None` which means arbitrary exports will be |
| 193 |
/// generated. |
| 194 |
/// |
| 195 |
/// To specify which exports the generated modules should have, set |
| 196 |
/// this field to a WebAssembly module which describes the desired |
| 197 |
/// exports. To generate modules with varying exports that meet some |
| 198 |
/// constraints, consider randomly generating the value for this |
| 199 |
/// field. |
| 200 |
/// |
| 201 |
/// The provided value must be a valid binary encoding of a |
| 202 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 203 |
/// be parsed. |
| 204 |
/// |
| 205 |
/// # Module Limits |
| 206 |
/// |
| 207 |
/// All types, functions, globals, memories, tables, tags, and exports |
| 208 |
/// that are needed to provide the required exports will be generated, |
| 209 |
/// even if it causes the resulting module to exceed the limits defined |
| 210 |
/// in [`Self::max_type_size`], [`Self::max_types`], |
| 211 |
/// [`Self::max_funcs`], [`Self::max_globals`], |
| 212 |
/// [`Self::max_memories`], [`Self::max_tables`], |
| 213 |
/// [`Self::max_tags`], or [`Self::max_exports`]. |
| 214 |
/// |
| 215 |
#[cfg_attr(feature = "clap", clap(long))] |
| 216 |
exports: Option<std::path::PathBuf>, |
| 217 |
|
| 218 |
/// If provided, the generated module will have imports and exports |
| 219 |
/// with exactly the same names and types as those in the provided |
| 220 |
/// WebAssembly module. |
| 221 |
/// |
| 222 |
/// Defaults to `None` which means arbitrary imports and exports will be |
| 223 |
/// generated. |
| 224 |
/// |
| 225 |
/// Note that [`Self::available_imports`] and [`Self::exports`] are |
| 226 |
/// ignored when `module_shape` is enabled. |
| 227 |
/// |
| 228 |
/// The provided value must be a valid binary encoding of a |
| 229 |
/// WebAssembly module. `wasm-smith` will panic if the module cannot |
| 230 |
/// be parsed. |
| 231 |
/// |
| 232 |
/// # Module Limits |
| 233 |
/// |
| 234 |
/// All types, functions, globals, memories, tables, tags, imports, and exports |
| 235 |
/// that are needed to provide the required imports and exports will be generated, |
| 236 |
/// even if it causes the resulting module to exceed the limits defined in |
| 237 |
/// [`Self::max_type_size`], [`Self::max_types`], [`Self::max_funcs`], |
| 238 |
/// [`Self::max_globals`], [`Self::max_memories`], [`Self::max_tables`], |
| 239 |
/// [`Self::max_tags`], [`Self::max_imports`], or [`Self::max_exports`]. |
| 240 |
#[cfg_attr(feature = "clap", clap(long))] |
| 241 |
module_shape: Option<std::path::PathBuf>, |
| 242 |
|
| 243 |
$( |
| 244 |
$(#[$field_attr])* |
| 245 |
#[cfg_attr(feature = "clap", clap(long))] |
| 246 |
pub $field: Option<$field_ty>, |
| 247 |
)* |
| 248 |
} |
| 249 |
|
| 250 |
impl InternalOptionalConfig { |
| 251 |
pub fn or(self, other: Self) -> Self { |
| 252 |
Self { |
| 253 |
available_imports: self.available_imports.or(other.available_imports), |
| 254 |
exports: self.exports.or(other.exports), |
| 255 |
module_shape: self.module_shape.or(other.module_shape), |
| 256 |
|
| 257 |
$( |
| 258 |
$field: self.$field.or(other.$field), |
| 259 |
)* |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
#[cfg(feature = "serde")] |
| 265 |
impl TryFrom<InternalOptionalConfig> for Config { |
| 266 |
type Error = anyhow::Error; |
| 267 |
fn try_from(config: InternalOptionalConfig) -> anyhow::Result<Config> { |
| 268 |
let default = Config::default(); |
| 269 |
Ok(Config { |
| 270 |
available_imports: if let Some(file) = config |
| 271 |
.available_imports |
| 272 |
.as_ref() { |
| 273 |
Some(wat::parse_file(file)?) |
| 274 |
} else { |
| 275 |
None |
| 276 |
}, |
| 277 |
exports: if let Some(file) = config |
| 278 |
.exports |
| 279 |
.as_ref() { |
| 280 |
Some(wat::parse_file(file)?) |
| 281 |
} else { |
| 282 |
None |
| 283 |
}, |
| 284 |
module_shape: if let Some(file) = config |
| 285 |
.module_shape |
| 286 |
.as_ref() { |
| 287 |
Some(wat::parse_file(file)?) |
| 288 |
} else { |
| 289 |
None |
| 290 |
}, |
| 291 |
|
| 292 |
$( |
| 293 |
$field: config.$field.unwrap_or(default.$field), |
| 294 |
)* |
| 295 |
}) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
impl TryFrom<&Config> for InternalOptionalConfig { |
| 300 |
type Error = anyhow::Error; |
| 301 |
fn try_from(config: &Config) -> anyhow::Result<InternalOptionalConfig> { |
| 302 |
if config.available_imports.is_some() { |
| 303 |
bail!("cannot serialize configuration with `available_imports`"); |
| 304 |
} |
| 305 |
if config.exports.is_some() { |
| 306 |
bail!("cannot serialize configuration with `exports`"); |
| 307 |
} |
| 308 |
if config.module_shape.is_some() { |
| 309 |
bail!("cannot serialize configuration with `module_shape`"); |
| 310 |
} |
| 311 |
Ok(InternalOptionalConfig { |
| 312 |
available_imports: None, |
| 313 |
exports: None, |
| 314 |
module_shape: None, |
| 315 |
$( $field: Some(config.$field.clone()), )* |
| 316 |
}) |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
define_config! { |
| 323 |
/// Configuration for a generated module. |
| 324 |
/// |
| 325 |
/// Don't care to configure your generated modules? Just use |
| 326 |
/// [`Module::arbitrary`][crate::Module], which internally uses the default |
| 327 |
/// configuration. |
| 328 |
/// |
| 329 |
/// Want control over the shape of the module that gets generated? Create a |
| 330 |
/// `Config` and then pass it to [`Module::new`][crate::Module::new]. |
| 331 |
/// |
| 332 |
/// # Swarm Testing |
| 333 |
/// |
| 334 |
/// You can use the `Arbitrary for Config` implementation for [swarm |
| 335 |
/// testing]. This will dynamically -- but still deterministically -- choose |
| 336 |
/// configuration options for you. |
| 337 |
/// |
| 338 |
/// [swarm testing]: https://www.cs.utah.edu/~regehr/papers/swarm12.pdf |
| 339 |
/// |
| 340 |
/// Note that we pick only *maximums*, not minimums, here because it is more |
| 341 |
/// complex to describe the domain of valid configs when minima are involved |
| 342 |
/// (`min <= max` for each variable) and minima are mostly used to ensure |
| 343 |
/// certain elements are present, but do not widen the range of generated |
| 344 |
/// Wasm modules. |
| 345 |
#[derive(Clone, Debug)] |
| 346 |
pub struct Config { |
| 347 |
/// Determines whether a `start` export may be included. Defaults to `true`. |
| 348 |
pub allow_start_export: bool = true, |
| 349 |
|
| 350 |
/// The kinds of instructions allowed in the generated wasm |
| 351 |
/// programs. Defaults to all. |
| 352 |
/// |
| 353 |
/// The categories of instructions match the categories used by the |
| 354 |
/// [WebAssembly |
| 355 |
/// specification](https://webassembly.github.io/spec/core/syntax/instructions.html); |
| 356 |
/// e.g., numeric, vector, control, memory, etc. |
| 357 |
/// |
| 358 |
/// Additionally, we include finer-grained categories which exclude floating point |
| 359 |
/// instructions, e.g. [`InstructionKind::NumericInt`] is a subset of |
| 360 |
/// [`InstructionKind::Numeric`] consisting of all numeric instructions which |
| 361 |
/// don't involve floats. |
| 362 |
/// |
| 363 |
/// Note that modifying this setting is separate from the proposal |
| 364 |
/// flags; that is, if `simd_enabled() == true` but |
| 365 |
/// `allowed_instruction()` does not include vector instructions, the |
| 366 |
/// generated programs will not include these instructions but could |
| 367 |
/// contain vector types. |
| 368 |
/// |
| 369 |
/// [`InstructionKind::Numeric`]: crate::InstructionKind::Numeric |
| 370 |
/// [`InstructionKind::NumericInt`]: crate::InstructionKind::NumericInt |
| 371 |
pub allowed_instructions: InstructionKinds = InstructionKinds::all(), |
| 372 |
|
| 373 |
/// Determines whether we generate floating point instructions and types. |
| 374 |
/// |
| 375 |
/// Defaults to `true`. |
| 376 |
pub allow_floats: bool = true, |
| 377 |
|
| 378 |
/// Determines whether the bulk memory proposal is enabled for |
| 379 |
/// generating instructions. |
| 380 |
/// |
| 381 |
/// Defaults to `true`. |
| 382 |
pub bulk_memory_enabled: bool = true, |
| 383 |
|
| 384 |
/// Returns whether NaN values are canonicalized after all f32/f64 |
| 385 |
/// operation. Defaults to false. |
| 386 |
/// |
| 387 |
/// This can be useful when a generated wasm module is executed in |
| 388 |
/// multiple runtimes which may produce different NaN values. This |
| 389 |
/// ensures that the generated module will always use the same NaN |
| 390 |
/// representation for all instructions which have visible side effects, |
| 391 |
/// for example writing floats to memory or float-to-int bitcast |
| 392 |
/// instructions. |
| 393 |
pub canonicalize_nans: bool = false, |
| 394 |
|
| 395 |
/// Returns whether we should avoid generating code that will possibly |
| 396 |
/// trap. |
| 397 |
/// |
| 398 |
/// For some trapping instructions, this will emit extra instructions to |
| 399 |
/// ensure they don't trap, while some instructions will simply be |
| 400 |
/// excluded. In cases where we would run into a trap, we instead |
| 401 |
/// choose some arbitrary non-trapping behavior. For example, if we |
| 402 |
/// detect that a Load instruction would attempt to access out-of-bounds |
| 403 |
/// memory, we instead pretend the load succeeded and push 0 onto the |
| 404 |
/// stack. |
| 405 |
/// |
| 406 |
/// One type of trap that we can't currently avoid is |
| 407 |
/// StackOverflow. Even when `disallow_traps` is set to true, wasm-smith |
| 408 |
/// will eventually generate a program that infinitely recurses, causing |
| 409 |
/// the call stack to be exhausted. |
| 410 |
/// |
| 411 |
/// Defaults to `false`. |
| 412 |
pub disallow_traps: bool = false, |
| 413 |
|
| 414 |
/// Determines whether the exception-handling proposal is enabled for |
| 415 |
/// generating instructions. |
| 416 |
/// |
| 417 |
/// Defaults to `true`. |
| 418 |
pub exceptions_enabled: bool = true, |
| 419 |
|
| 420 |
/// Export all WebAssembly objects in the module. Defaults to false. |
| 421 |
/// |
| 422 |
/// This overrides [`Config::min_exports`] and [`Config::max_exports`]. |
| 423 |
pub export_everything: bool = false, |
| 424 |
|
| 425 |
/// Determines whether the GC proposal is enabled when generating a Wasm |
| 426 |
/// module. |
| 427 |
/// |
| 428 |
/// Defaults to `true`. |
| 429 |
pub gc_enabled: bool = true, |
| 430 |
|
| 431 |
/// Determines whether the custom-page-sizes proposal is enabled when |
| 432 |
/// generating a Wasm module. |
| 433 |
/// |
| 434 |
/// Defaults to `false`. |
| 435 |
pub custom_page_sizes_enabled: bool = false, |
| 436 |
|
| 437 |
/// Returns whether we should generate custom sections or not. Defaults |
| 438 |
/// to false. |
| 439 |
pub generate_custom_sections: bool = false, |
| 440 |
|
| 441 |
/// Returns the maximal size of the `alias` section. Defaults to 1000. |
| 442 |
pub max_aliases: usize = 1000, |
| 443 |
|
| 444 |
/// The maximum number of components to use. Defaults to 10. |
| 445 |
/// |
| 446 |
/// This includes imported components. |
| 447 |
/// |
| 448 |
/// Note that this is only relevant for components. |
| 449 |
pub max_components: usize = 10, |
| 450 |
|
| 451 |
/// The maximum number of data segments to generate. Defaults to 100. |
| 452 |
pub max_data_segments: usize = 100, |
| 453 |
|
| 454 |
/// The maximum number of element segments to generate. Defaults to 100. |
| 455 |
pub max_element_segments: usize = 100, |
| 456 |
|
| 457 |
/// The maximum number of elements within a segment to |
| 458 |
/// generate. Defaults to 100. |
| 459 |
pub max_elements: usize = 100, |
| 460 |
|
| 461 |
/// The maximum number of exports to generate. Defaults to 100. |
| 462 |
pub max_exports: usize = 100, |
| 463 |
|
| 464 |
/// The maximum number of functions to generate. Defaults to 100. This |
| 465 |
/// includes imported functions. |
| 466 |
pub max_funcs: usize = 100, |
| 467 |
|
| 468 |
/// The maximum number of globals to generate. Defaults to 100. This |
| 469 |
/// includes imported globals. |
| 470 |
pub max_globals: usize = 100, |
| 471 |
|
| 472 |
/// The maximum number of imports to generate. Defaults to 100. |
| 473 |
pub max_imports: usize = 100, |
| 474 |
|
| 475 |
/// The maximum number of instances to use. Defaults to 10. |
| 476 |
/// |
| 477 |
/// This includes imported instances. |
| 478 |
/// |
| 479 |
/// Note that this is only relevant for components. |
| 480 |
pub max_instances: usize = 10, |
| 481 |
|
| 482 |
/// The maximum number of instructions to generate in a function |
| 483 |
/// body. Defaults to 100. |
| 484 |
/// |
| 485 |
/// Note that some additional `end`s, `else`s, and `unreachable`s may be |
| 486 |
/// appended to the function body to finish block scopes. |
| 487 |
pub max_instructions: usize = 100, |
| 488 |
|
| 489 |
/// The maximum number of memories to use. Defaults to 1. |
| 490 |
/// |
| 491 |
/// This includes imported memories. |
| 492 |
/// |
| 493 |
/// Note that more than one memory is in the realm of the multi-memory |
| 494 |
/// wasm proposal. |
| 495 |
pub max_memories: usize = 1, |
| 496 |
|
| 497 |
/// The maximum, in bytes, of any 32-bit memory's initial or maximum |
| 498 |
/// size. |
| 499 |
/// |
| 500 |
/// May not be larger than `2**32`. |
| 501 |
/// |
| 502 |
/// Defaults to `2**32`. |
| 503 |
pub max_memory32_bytes: u64 = u32::MAX as u64 + 1, |
| 504 |
|
| 505 |
/// The maximum, in bytes, of any 64-bit memory's initial or maximum |
| 506 |
/// size. |
| 507 |
/// |
| 508 |
/// May not be larger than `2**64`. |
| 509 |
/// |
| 510 |
/// Defaults to `2**64`. |
| 511 |
pub max_memory64_bytes: u128 = u64::MAX as u128 + 1, |
| 512 |
|
| 513 |
/// The maximum number of modules to use. Defaults to 10. |
| 514 |
/// |
| 515 |
/// This includes imported modules. |
| 516 |
/// |
| 517 |
/// Note that this is only relevant for components. |
| 518 |
pub max_modules: usize = 10, |
| 519 |
|
| 520 |
/// Returns the maximal nesting depth of modules with the component |
| 521 |
/// model proposal. Defaults to 10. |
| 522 |
pub max_nesting_depth: usize = 10, |
| 523 |
|
| 524 |
/// The maximum, elements, of any table's initial or maximum |
| 525 |
/// size. Defaults to 1 million. |
| 526 |
pub max_table_elements: u64 = 1_000_000, |
| 527 |
|
| 528 |
/// The maximum number of tables to use. Defaults to 1. |
| 529 |
/// |
| 530 |
/// This includes imported tables. |
| 531 |
/// |
| 532 |
/// Note that more than one table is in the realm of the reference types |
| 533 |
/// proposal. |
| 534 |
pub max_tables: usize = 1, |
| 535 |
|
| 536 |
/// The maximum number of tags to generate. Defaults to 100. |
| 537 |
pub max_tags: usize = 100, |
| 538 |
|
| 539 |
/// Returns the maximal effective size of any type generated by |
| 540 |
/// wasm-smith. |
| 541 |
/// |
| 542 |
/// Note that this number is roughly in units of "how many types would |
| 543 |
/// be needed to represent the recursive type". A function with 8 |
| 544 |
/// parameters and 2 results would take 11 types (one for the type, 10 |
| 545 |
/// for params/results). A module type with 2 imports and 3 exports |
| 546 |
/// would take 6 (module + imports + exports) plus the size of each |
| 547 |
/// import/export type. This is a somewhat rough measurement that is not |
| 548 |
/// intended to be very precise. |
| 549 |
/// |
| 550 |
/// Defaults to 1000. |
| 551 |
pub max_type_size: u32 = 1000, |
| 552 |
|
| 553 |
/// The maximum number of types to generate. Defaults to 100. |
| 554 |
pub max_types: usize = 100, |
| 555 |
|
| 556 |
/// The maximum number of values to use. Defaults to 10. |
| 557 |
/// |
| 558 |
/// This includes imported values. |
| 559 |
/// |
| 560 |
/// Note that this is irrelevant unless value model support is enabled. |
| 561 |
pub max_values: usize = 10, |
| 562 |
|
| 563 |
/// Returns whether 64-bit memories are allowed. Defaults to true. |
| 564 |
/// |
| 565 |
/// Note that this is the gate for the memory64 proposal to WebAssembly. |
| 566 |
pub memory64_enabled: bool = true, |
| 567 |
|
| 568 |
/// Whether every Wasm memory must have a maximum size |
| 569 |
/// specified. Defaults to `false`. |
| 570 |
pub memory_max_size_required: bool = false, |
| 571 |
|
| 572 |
/// Control the probability of generating memory offsets that are in |
| 573 |
/// bounds vs. potentially out of bounds. |
| 574 |
/// |
| 575 |
/// See the `MemoryOffsetChoices` struct for details. |
| 576 |
pub memory_offset_choices: MemoryOffsetChoices = MemoryOffsetChoices::default(), |
| 577 |
|
| 578 |
/// The minimum number of data segments to generate. Defaults to 0. |
| 579 |
pub min_data_segments: usize = 0, |
| 580 |
|
| 581 |
/// The minimum number of element segments to generate. Defaults to 0. |
| 582 |
pub min_element_segments: usize = 0, |
| 583 |
|
| 584 |
/// The minimum number of elements within a segment to |
| 585 |
/// generate. Defaults to 0. |
| 586 |
pub min_elements: usize = 0, |
| 587 |
|
| 588 |
/// The minimum number of exports to generate. Defaults to 0. |
| 589 |
pub min_exports: usize = 0, |
| 590 |
|
| 591 |
/// The minimum number of functions to generate. Defaults to 0. |
| 592 |
/// |
| 593 |
/// This includes imported functions. |
| 594 |
pub min_funcs: usize = 0, |
| 595 |
|
| 596 |
/// The minimum number of globals to generate. Defaults to 0. |
| 597 |
/// |
| 598 |
/// This includes imported globals. |
| 599 |
pub min_globals: usize = 0, |
| 600 |
|
| 601 |
/// The minimum number of imports to generate. Defaults to 0. |
| 602 |
/// |
| 603 |
/// Note that if the sum of the maximum function[^1], table, global and |
| 604 |
/// memory counts is less than the minimum number of imports, then it |
| 605 |
/// will not be possible to satisfy all constraints (because imports |
| 606 |
/// count against the limits for those element kinds). In that case, we |
| 607 |
/// strictly follow the max-constraints, and can fail to satisfy this |
| 608 |
/// minimum number. |
| 609 |
/// |
| 610 |
/// [^1]: the maximum number of functions is also limited by the number |
| 611 |
/// of function types arbitrarily chosen; strictly speaking, then, the |
| 612 |
/// maximum number of imports that can be created due to max-constraints |
| 613 |
/// is `sum(min(num_func_types, max_funcs), max_tables, max_globals, |
| 614 |
/// max_memories)`. |
| 615 |
pub min_imports: usize = 0, |
| 616 |
|
| 617 |
/// The minimum number of memories to use. Defaults to 0. |
| 618 |
/// |
| 619 |
/// This includes imported memories. |
| 620 |
pub min_memories: u32 = 0, |
| 621 |
|
| 622 |
/// The minimum number of tables to use. Defaults to 0. |
| 623 |
/// |
| 624 |
/// This includes imported tables. |
| 625 |
pub min_tables: u32 = 0, |
| 626 |
|
| 627 |
/// The minimum number of tags to generate. Defaults to 0. |
| 628 |
pub min_tags: usize = 0, |
| 629 |
|
| 630 |
/// The minimum number of types to generate. Defaults to 0. |
| 631 |
pub min_types: usize = 0, |
| 632 |
|
| 633 |
/// The minimum size, in bytes, of all leb-encoded integers. Defaults to |
| 634 |
/// 1. |
| 635 |
/// |
| 636 |
/// This is useful for ensuring that all leb-encoded integers are |
| 637 |
/// decoded as such rather than as simply one byte. This will forcibly |
| 638 |
/// extend leb integers with an over-long encoding in some locations if |
| 639 |
/// the size would otherwise be smaller than number returned here. |
| 640 |
pub min_uleb_size: u8 = 1, |
| 641 |
|
| 642 |
/// Determines whether the multi-value results are enabled. |
| 643 |
/// |
| 644 |
/// Defaults to `true`. |
| 645 |
pub multi_value_enabled: bool = true, |
| 646 |
|
| 647 |
/// Determines whether the reference types proposal is enabled for |
| 648 |
/// generating instructions. |
| 649 |
/// |
| 650 |
/// Defaults to `true`. |
| 651 |
pub reference_types_enabled: bool = true, |
| 652 |
|
| 653 |
/// Determines whether the Relaxed SIMD proposal is enabled for |
| 654 |
/// generating instructions. |
| 655 |
/// |
| 656 |
/// Defaults to `true`. |
| 657 |
pub relaxed_simd_enabled: bool = true, |
| 658 |
|
| 659 |
/// Determines whether the non-trapping float-to-int conversions |
| 660 |
/// proposal is enabled. |
| 661 |
/// |
| 662 |
/// Defaults to `true`. |
| 663 |
pub saturating_float_to_int_enabled: bool = true, |
| 664 |
|
| 665 |
/// Determines whether the sign-extension-ops proposal is enabled. |
| 666 |
/// |
| 667 |
/// Defaults to `true`. |
| 668 |
pub sign_extension_ops_enabled: bool = true, |
| 669 |
|
| 670 |
/// Determines whether the shared-everything-threads proposal is |
| 671 |
/// enabled. |
| 672 |
/// |
| 673 |
/// The [shared-everything-threads] proposal, among other things, |
| 674 |
/// extends `shared` attributes to all WebAssembly objects; it builds on |
| 675 |
/// the [threads] proposal. |
| 676 |
/// |
| 677 |
/// [shared-everything-threads]: https://github.com/WebAssembly/shared-everything-threads |
| 678 |
/// [threads]: https://github.com/WebAssembly/threads |
| 679 |
/// |
| 680 |
/// Defaults to `false`. |
| 681 |
pub shared_everything_threads_enabled: bool = false, |
| 682 |
|
| 683 |
/// Determines whether the SIMD proposal is enabled for generating |
| 684 |
/// instructions. |
| 685 |
/// |
| 686 |
/// Defaults to `true`. |
| 687 |
pub simd_enabled: bool = true, |
| 688 |
|
| 689 |
/// Determines whether the tail calls proposal is enabled for generating |
| 690 |
/// instructions. |
| 691 |
/// |
| 692 |
/// Defaults to `true`. |
| 693 |
pub tail_call_enabled: bool = true, |
| 694 |
|
| 695 |
/// Whether every Wasm table must have a maximum size |
| 696 |
/// specified. Defaults to `false`. |
| 697 |
pub table_max_size_required: bool = false, |
| 698 |
|
| 699 |
/// Determines whether the threads proposal is enabled. |
| 700 |
/// |
| 701 |
/// The [threads proposal] involves shared linear memory, new atomic |
| 702 |
/// instructions, and new `wait` and `notify` instructions. |
| 703 |
/// |
| 704 |
/// [threads proposal]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md |
| 705 |
/// |
| 706 |
/// Defaults to `true`. |
| 707 |
pub threads_enabled: bool = true, |
| 708 |
|
| 709 |
/// Indicates whether wasm-smith is allowed to generate invalid function |
| 710 |
/// bodies. |
| 711 |
/// |
| 712 |
/// When enabled this option will enable taking raw bytes from the input |
| 713 |
/// byte stream and using them as a wasm function body. This means that |
| 714 |
/// the output module is not guaranteed to be valid but can help tickle |
| 715 |
/// various parts of validation/compilation in some circumstances as |
| 716 |
/// well. |
| 717 |
/// |
| 718 |
/// Defaults to `false`. |
| 719 |
pub allow_invalid_funcs: bool = false, |
| 720 |
|
| 721 |
/// Determines whether the [wide-arithmetic proposal] is enabled. |
| 722 |
/// |
| 723 |
/// [wide-arithmetic proposal]: https://github.com/WebAssembly/wide-arithmetic |
| 724 |
/// |
| 725 |
/// Defaults to `false`. |
| 726 |
pub wide_arithmetic_enabled: bool = false, |
| 727 |
|
| 728 |
/// Determines whether the [extended-const proposal] is enabled. |
| 729 |
/// |
| 730 |
/// [extended-const proposal]: https://github.com/WebAssembly/extended-const |
| 731 |
/// |
| 732 |
/// Defaults to `true`. |
| 733 |
pub extended_const_enabled: bool = true, |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
/// This is a tuple `(a, b, c)` where |
| 738 |
/// |
| 739 |
/// * `a / (a+b+c)` is the probability of generating a memory offset within |
| 740 |
/// `0..memory.min_size`, i.e. an offset that is definitely in bounds of a |
| 741 |
/// non-empty memory. (Note that if a memory is zero-sized, however, no offset |
| 742 |
/// will ever be in bounds.) |
| 743 |
/// |
| 744 |
/// * `b / (a+b+c)` is the probability of generating a memory offset within |
| 745 |
/// `memory.min_size..memory.max_size`, i.e. an offset that is possibly in |
| 746 |
/// bounds if the memory has been grown. |
| 747 |
/// |
| 748 |
/// * `c / (a+b+c)` is the probability of generating a memory offset within the |
| 749 |
/// range `memory.max_size..`, i.e. an offset that is definitely out of |
| 750 |
/// bounds. |
| 751 |
/// |
| 752 |
/// At least one of `a`, `b`, and `c` must be non-zero. |
| 753 |
/// |
| 754 |
/// If you want to always generate memory offsets that are definitely in bounds |
| 755 |
/// of a non-zero-sized memory, for example, you could return `(1, 0, 0)`. |
| 756 |
/// |
| 757 |
/// The default is `(90, 9, 1)`. |
| 758 |
#[derive(Clone, Debug)] |
| 759 |
#[cfg_attr( |
| 760 |
feature = "serde", |
| 761 |
derive(serde_derive::Deserialize, serde_derive::Serialize) |
| 762 |
)] |
| 763 |
pub struct MemoryOffsetChoices(pub u32, pub u32, pub u32); |
| 764 |
|
| 765 |
impl Default for MemoryOffsetChoices { |
| 766 |
fn default() -> Self { |
| 767 |
MemoryOffsetChoices(90, 9, 1) |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
impl std::str::FromStr for MemoryOffsetChoices { |
| 772 |
type Err = String; |
| 773 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 774 |
use std::str::FromStr; |
| 775 |
let mut parts = s.split(","); |
| 776 |
let a = parts |
| 777 |
.next() |
| 778 |
.ok_or_else(|| "need 3 comma separated values".to_string())?; |
| 779 |
let a = <u32 as FromStr>::from_str(a).map_err(|e| e.to_string())?; |
| 780 |
let b = parts |
| 781 |
.next() |
| 782 |
.ok_or_else(|| "need 3 comma separated values".to_string())?; |
| 783 |
let b = <u32 as FromStr>::from_str(b).map_err(|e| e.to_string())?; |
| 784 |
let c = parts |
| 785 |
.next() |
| 786 |
.ok_or_else(|| "need 3 comma separated values".to_string())?; |
| 787 |
let c = <u32 as FromStr>::from_str(c).map_err(|e| e.to_string())?; |
| 788 |
if parts.next().is_some() { |
| 789 |
return Err("found more than 3 comma separated values".to_string()); |
| 790 |
} |
| 791 |
Ok(MemoryOffsetChoices(a, b, c)) |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
impl<'a> Arbitrary<'a> for Config { |
| 796 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 797 |
const MAX_MAXIMUM: usize = 1000; |
| 798 |
|
| 799 |
let mut config = Config { |
| 800 |
max_types: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 801 |
max_imports: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 802 |
max_tags: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 803 |
max_funcs: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 804 |
max_globals: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 805 |
max_exports: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 806 |
max_element_segments: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 807 |
max_elements: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 808 |
max_data_segments: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 809 |
max_instructions: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 810 |
max_memories: u.int_in_range(0..=100)?, |
| 811 |
max_tables: u.int_in_range(0..=100)?, |
| 812 |
max_memory32_bytes: u.int_in_range(0..=u32::MAX as u64 + 1)?, |
| 813 |
max_memory64_bytes: u.int_in_range(0..=u64::MAX as u128 + 1)?, |
| 814 |
min_uleb_size: u.int_in_range(0..=5)?, |
| 815 |
bulk_memory_enabled: u.arbitrary()?, |
| 816 |
reference_types_enabled: u.arbitrary()?, |
| 817 |
simd_enabled: u.arbitrary()?, |
| 818 |
multi_value_enabled: u.arbitrary()?, |
| 819 |
max_aliases: u.int_in_range(0..=MAX_MAXIMUM)?, |
| 820 |
max_nesting_depth: u.int_in_range(0..=10)?, |
| 821 |
saturating_float_to_int_enabled: u.arbitrary()?, |
| 822 |
sign_extension_ops_enabled: u.arbitrary()?, |
| 823 |
relaxed_simd_enabled: u.arbitrary()?, |
| 824 |
exceptions_enabled: u.arbitrary()?, |
| 825 |
threads_enabled: u.arbitrary()?, |
| 826 |
tail_call_enabled: u.arbitrary()?, |
| 827 |
gc_enabled: u.arbitrary()?, |
| 828 |
memory64_enabled: u.arbitrary()?, |
| 829 |
allowed_instructions: { |
| 830 |
use flagset::Flags; |
| 831 |
let mut allowed = Vec::new(); |
| 832 |
for kind in crate::core::InstructionKind::LIST { |
| 833 |
if u.arbitrary()? { |
| 834 |
allowed.push(*kind); |
| 835 |
} |
| 836 |
} |
| 837 |
InstructionKinds::new(&allowed) |
| 838 |
}, |
| 839 |
table_max_size_required: u.arbitrary()?, |
| 840 |
max_table_elements: u.int_in_range(0..=1_000_000)?, |
| 841 |
disallow_traps: u.arbitrary()?, |
| 842 |
allow_floats: u.arbitrary()?, |
| 843 |
extended_const_enabled: u.arbitrary()?, |
| 844 |
|
| 845 |
// These fields, unlike the ones above, are less useful to set. |
| 846 |
// They either make weird inputs or are for features not widely |
| 847 |
// implemented yet so they're turned off by default. |
| 848 |
min_types: 0, |
| 849 |
min_imports: 0, |
| 850 |
min_tags: 0, |
| 851 |
min_funcs: 0, |
| 852 |
min_globals: 0, |
| 853 |
min_exports: 0, |
| 854 |
min_element_segments: 0, |
| 855 |
min_elements: 0, |
| 856 |
min_data_segments: 0, |
| 857 |
min_memories: 0, |
| 858 |
min_tables: 0, |
| 859 |
memory_max_size_required: false, |
| 860 |
max_instances: 0, |
| 861 |
max_modules: 0, |
| 862 |
max_components: 0, |
| 863 |
max_values: 0, |
| 864 |
memory_offset_choices: MemoryOffsetChoices::default(), |
| 865 |
allow_start_export: true, |
| 866 |
max_type_size: 1000, |
| 867 |
canonicalize_nans: false, |
| 868 |
available_imports: None, |
| 869 |
exports: None, |
| 870 |
module_shape: None, |
| 871 |
export_everything: false, |
| 872 |
generate_custom_sections: false, |
| 873 |
allow_invalid_funcs: false, |
| 874 |
|
| 875 |
// Proposals that are not stage4+ are disabled by default. |
| 876 |
custom_page_sizes_enabled: false, |
| 877 |
wide_arithmetic_enabled: false, |
| 878 |
shared_everything_threads_enabled: false, |
| 879 |
}; |
| 880 |
config.sanitize(); |
| 881 |
Ok(config) |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
impl Config { |
| 886 |
/// "Shrink" this `Config` where appropriate to ensure its configuration is |
| 887 |
/// valid for wasm-smith. |
| 888 |
/// |
| 889 |
/// This method will take the arbitrary state that this `Config` is in and |
| 890 |
/// will possibly mutate dependent options as needed by `wasm-smith`. For |
| 891 |
/// example if the `reference_types_enabled` field is turned off then |
| 892 |
/// `wasm-smith`, as of the time of this writing, additionally requires that |
| 893 |
/// the `gc_enabled` is not turned on. |
| 894 |
/// |
| 895 |
/// This method will not enable anything that isn't already enabled or |
| 896 |
/// increase any limit of an item, but it may turn features off or shrink |
| 897 |
/// limits from what they're previously specified as. |
| 898 |
pub(crate) fn sanitize(&mut self) { |
| 899 |
// If reference types are disabled then automatically flag tables as |
| 900 |
// capped at 1 and disable gc as well. |
| 901 |
if !self.reference_types_enabled { |
| 902 |
self.max_tables = self.max_tables.min(1); |
| 903 |
self.gc_enabled = false; |
| 904 |
self.shared_everything_threads_enabled = false; |
| 905 |
} |
| 906 |
|
| 907 |
// shared-everything-threads depends on GC, so if gc is disabled then |
| 908 |
// also disable shared-everything-threads. |
| 909 |
if !self.gc_enabled { |
| 910 |
self.shared_everything_threads_enabled = false; |
| 911 |
} |
| 912 |
|
| 913 |
// If simd is disabled then disable all relaxed simd instructions as |
| 914 |
// well. |
| 915 |
if !self.simd_enabled { |
| 916 |
self.relaxed_simd_enabled = false; |
| 917 |
} |
| 918 |
|
| 919 |
// It is impossible to use the shared-everything-threads proposal |
| 920 |
// without threads, which it is built on. |
| 921 |
if !self.threads_enabled { |
| 922 |
self.shared_everything_threads_enabled = false; |
| 923 |
} |
| 924 |
|
| 925 |
// If module_shape is present then disable available_imports and exports. |
| 926 |
if self.module_shape.is_some() { |
| 927 |
self.available_imports = None; |
| 928 |
self.exports = None; |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
/// Returns the set of features that are necessary for validating against |
| 933 |
/// this `Config`. |
| 934 |
#[cfg(feature = "wasmparser")] |
| 935 |
pub fn features(&self) -> wasmparser::WasmFeatures { |
| 936 |
use wasmparser::WasmFeatures; |
| 937 |
|
| 938 |
// Currently wasm-smith doesn't have knobs for the MVP (floats) or |
| 939 |
// `mutable-global`. These are unconditionally enabled. |
| 940 |
let mut features = WasmFeatures::MUTABLE_GLOBAL | WasmFeatures::WASM1; |
| 941 |
|
| 942 |
// All other features that can be generated by wasm-smith are gated by |
| 943 |
// configuration fields. Conditionally set each feature based on the |
| 944 |
// status of the fields in `self`. |
| 945 |
features.set( |
| 946 |
WasmFeatures::SATURATING_FLOAT_TO_INT, |
| 947 |
self.saturating_float_to_int_enabled, |
| 948 |
); |
| 949 |
features.set( |
| 950 |
WasmFeatures::SIGN_EXTENSION, |
| 951 |
self.sign_extension_ops_enabled, |
| 952 |
); |
| 953 |
features.set(WasmFeatures::REFERENCE_TYPES, self.reference_types_enabled); |
| 954 |
features.set(WasmFeatures::MULTI_VALUE, self.multi_value_enabled); |
| 955 |
features.set(WasmFeatures::BULK_MEMORY, self.bulk_memory_enabled); |
| 956 |
features.set(WasmFeatures::SIMD, self.simd_enabled); |
| 957 |
features.set(WasmFeatures::RELAXED_SIMD, self.relaxed_simd_enabled); |
| 958 |
features.set(WasmFeatures::MULTI_MEMORY, self.max_memories > 1); |
| 959 |
features.set(WasmFeatures::EXCEPTIONS, self.exceptions_enabled); |
| 960 |
features.set(WasmFeatures::MEMORY64, self.memory64_enabled); |
| 961 |
features.set(WasmFeatures::TAIL_CALL, self.tail_call_enabled); |
| 962 |
features.set(WasmFeatures::FUNCTION_REFERENCES, self.gc_enabled); |
| 963 |
features.set(WasmFeatures::GC, self.gc_enabled); |
| 964 |
features.set(WasmFeatures::THREADS, self.threads_enabled); |
| 965 |
features.set( |
| 966 |
WasmFeatures::SHARED_EVERYTHING_THREADS, |
| 967 |
self.shared_everything_threads_enabled, |
| 968 |
); |
| 969 |
features.set( |
| 970 |
WasmFeatures::CUSTOM_PAGE_SIZES, |
| 971 |
self.custom_page_sizes_enabled, |
| 972 |
); |
| 973 |
features.set(WasmFeatures::EXTENDED_CONST, self.extended_const_enabled); |
| 974 |
features.set(WasmFeatures::WIDE_ARITHMETIC, self.wide_arithmetic_enabled); |
| 975 |
|
| 976 |
features |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
#[cfg(feature = "serde")] |
| 981 |
impl<'de> serde::Deserialize<'de> for Config { |
| 982 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 983 |
where |
| 984 |
D: serde::de::Deserializer<'de>, |
| 985 |
{ |
| 986 |
use serde::de::Error; |
| 987 |
|
| 988 |
match Config::try_from(InternalOptionalConfig::deserialize(deserializer)?) { |
| 989 |
Ok(config) => Ok(config), |
| 990 |
Err(e) => Err(D::Error::custom(e)), |
| 991 |
} |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
#[cfg(feature = "serde")] |
| 996 |
impl serde::Serialize for Config { |
| 997 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 998 |
where |
| 999 |
S: serde::Serializer, |
| 1000 |
{ |
| 1001 |
use serde::ser::Error; |
| 1002 |
|
| 1003 |
match InternalOptionalConfig::try_from(self) { |
| 1004 |
Ok(result) => result.serialize(serializer), |
| 1005 |
Err(e) => Err(S::Error::custom(e)), |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |