Debian Codegraph @

fuzz/src/validate.rs

raw
1 use arbitrary::{Result, Unstructured};
2 use wasmparser::{Parser, Validator, WasmFeatures};
3
4 pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
5 // Either use `wasm-smith` to generate a module with possibly invalid
6 // functions or try validating raw bytes from the input itself.
7 if u.arbitrary()? {
8 validate_maybe_invalid_module(u)?;
9 } else {
10 validate_raw_bytes(u)?;
11 }
12 Ok(())
13 }
14
15 pub fn validate_maybe_invalid_module(u: &mut Unstructured<'_>) -> Result<()> {
16 // Generate a "valid" module but specifically allow invalid functions which
17 // means that some functions may be defined from the input bytes raw. This
18 // means that most of the module is valid but only some functions may be
19 // invalid which can help stress various bits and pieces of validation.
20 let (wasm, config) = crate::generate_valid_module(u, |config, _| {
21 config.allow_invalid_funcs = true;
22 Ok(())
23 })?;
24 validate_all(u, crate::validator_for_config(&config), &wasm)
25 }
26
27 pub fn validate_raw_bytes(u: &mut Unstructured<'_>) -> Result<()> {
28 // Enable arbitrary combinations of features to validate the input bytes.
29 let validator = Validator::new_with_features(WasmFeatures::from_bits_truncate(u.arbitrary()?));
30 let wasm = u.bytes(u.len())?;
31 crate::log_wasm(wasm, "");
32 validate_all(u, validator, wasm)
33 }
34
35 fn validate_all(u: &mut Unstructured<'_>, mut validator: Validator, wasm: &[u8]) -> Result<()> {
36 // First try printing this module. Generate a random configuration for
37 // printing and then see what happens. Mostly making sure nothing panics
38 // here.
39 let mut cfg = wasmprinter::Config::new();
40 cfg.fold_instructions(u.arbitrary()?);
41 cfg.print_skeleton(u.arbitrary()?);
42 cfg.print_offsets(u.arbitrary()?);
43 cfg.name_unnamed(u.arbitrary()?);
44 log::debug!("print config {cfg:?}");
45 let mut wat = String::new();
46 let _ = cfg.print(wasm, &mut wasmprinter::PrintFmtWrite(&mut wat));
47
48 // After printing then try to parse and validate the module. See how far we
49 // get as invalid modules are explicitly allowed here. Generally looking for
50 // panics and excessive resource usage here.
51 for payload in Parser::new(0).parse_all(wasm) {
52 let payload = match payload {
53 Ok(p) => p,
54 Err(_) => return Ok(()),
55 };
56
57 if validator.payload(&payload).is_err() {
58 return Ok(());
59 }
60
61 // Check that the payload's range is in bounds, since the payload is
62 // supposedly valid.
63 use wasmparser::Payload::*;
64 match payload {
65 Version { range, .. } => assert!(wasm.get(range).is_some()),
66 TypeSection(s) => assert!(wasm.get(s.range()).is_some()),
67 ImportSection(s) => assert!(wasm.get(s.range()).is_some()),
68 FunctionSection(s) => assert!(wasm.get(s.range()).is_some()),
69 TableSection(s) => assert!(wasm.get(s.range()).is_some()),
70 MemorySection(s) => assert!(wasm.get(s.range()).is_some()),
71 TagSection(s) => assert!(wasm.get(s.range()).is_some()),
72 GlobalSection(s) => assert!(wasm.get(s.range()).is_some()),
73 ExportSection(s) => assert!(wasm.get(s.range()).is_some()),
74 StartSection { range, .. } => assert!(wasm.get(range).is_some()),
75 ElementSection(s) => assert!(wasm.get(s.range()).is_some()),
76 DataCountSection { range, .. } => assert!(wasm.get(range).is_some()),
77 DataSection(s) => assert!(wasm.get(s.range()).is_some()),
78 CodeSectionStart { range, .. } => assert!(wasm.get(range).is_some()),
79 CodeSectionEntry(body) => assert!(wasm.get(body.range()).is_some()),
80 InstanceSection(s) => assert!(wasm.get(s.range()).is_some()),
81 CoreTypeSection(s) => assert!(wasm.get(s.range()).is_some()),
82 ComponentInstanceSection(s) => assert!(wasm.get(s.range()).is_some()),
83 ComponentAliasSection(s) => assert!(wasm.get(s.range()).is_some()),
84 ComponentTypeSection(s) => assert!(wasm.get(s.range()).is_some()),
85 ComponentCanonicalSection(s) => assert!(wasm.get(s.range()).is_some()),
86 ComponentStartSection { range, .. } => assert!(wasm.get(range).is_some()),
87 ComponentImportSection(s) => assert!(wasm.get(s.range()).is_some()),
88 ComponentExportSection(s) => assert!(wasm.get(s.range()).is_some()),
89 CustomSection(s) => assert!(wasm.get(s.range()).is_some()),
90 UnknownSection { range, .. } => assert!(wasm.get(range).is_some()),
91
92 // In order to support streaming parsing and validation, these
93 // sections' ranges are not checked during validation, since they
94 // contain nested sections and we don't want to require all nested
95 // sections are present before we can parse/validate any of them.
96 ComponentSection {
97 unchecked_range: _, ..
98 }
99 | ModuleSection {
100 unchecked_range: _, ..
101 } => {}
102
103 // No associated range.
104 End(_) => {}
105
106 _ => {}
107 }
108 }
109
110 Ok(())
111 }
112
113 #[test]
114 fn smoke() {
115 super::test::test_n_times(100, run);
116 }