Validates `binary` input data as a WebAssembly binary given the configuration in `engine`. This function will perform a speedy validation of the `binary` input WebAssembly module (which is in [binary form][binary], the text format is not accepted by this function) and return either `Ok` or `Err` depending on the results of validation. The `engine` argument indicates configuration for WebAssembly
(engine: &Engine, binary: &[u8])
| 582 | /// |
| 583 | /// [binary]: https://webassembly.github.io/spec/core/binary/index.html |
| 584 | pub fn validate(engine: &Engine, binary: &[u8]) -> Result<()> { |
| 585 | let mut validator = Validator::new_with_features(engine.features()); |
| 586 | |
| 587 | let mut functions = Vec::new(); |
| 588 | for payload in Parser::new(0).parse_all(binary) { |
| 589 | let payload = payload?; |
| 590 | if let ValidPayload::Func(a, b) = validator.payload(&payload)? { |
| 591 | functions.push((a, b)); |
| 592 | } |
| 593 | if let wasmparser::Payload::Version { encoding, .. } = &payload { |
| 594 | if let wasmparser::Encoding::Component = encoding { |
| 595 | bail!("component passed to module validation"); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | engine.run_maybe_parallel(functions, |(validator, body)| { |
| 601 | // FIXME: it would be best here to use a rayon-specific parallel |
| 602 | // iterator that maintains state-per-thread to share the function |
| 603 | // validator allocations (`Default::default` here) across multiple |
| 604 | // functions. |
| 605 | validator.into_validator(Default::default()).validate(&body) |
| 606 | })?; |
| 607 | Ok(()) |
| 608 | } |
| 609 | |
| 610 | /// Serializes this module to a vector of bytes. |
| 611 | /// |