Debian Codegraph @

fuzz/src/incremental_parse.rs

raw
1 use Payload::*;
2 use arbitrary::{Result, Unstructured};
3 use wasmparser::*;
4
5 // Simulate receiving chunks of data by fuzzing over a `Vec<Vec<u8>>` where each
6 // element of the outer vec is a chunk of data we received.
7 //
8 // The assertion here is that parsing everything in one go should always produce
9 // the exact same results as an incremental parse.
10 pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
11 let data: Vec<Vec<u8>> = u.arbitrary()?;
12
13 // Concatenate everything together, create our expected iterator of
14 // payloads, and then write out `input.wasm` if debugging is enabled.
15 let everything = data.iter().flat_map(|a| a).copied().collect::<Vec<_>>();
16 let mut expected = Parser::new(0).parse_all(&everything);
17 if log::log_enabled!(log::Level::Debug) {
18 std::fs::write("input.wasm", &everything).unwrap();
19 }
20
21 // Create our parser as well as a stack of nested parsers for parsing nested
22 // modules.
23 let mut stack = Vec::new();
24 let mut parser = Parser::new(0);
25
26 // We'll be parsing data from `buf` starting at `pos`, and we translate
27 // `data` into an iterator of chunks so when requested we'll take another
28 // chunk of data and feed it in.
29 let mut pos = 0;
30 let mut buf = Vec::new();
31 let mut data = data.into_iter().peekable();
32 loop {
33 log::debug!("parsing {}..{}", pos, buf.len());
34 let payload = match parser.parse(&buf[pos..], data.peek().is_none()) {
35 // If more data is requested then we're guaranteed that `data`
36 // should have another element in its iterato, so pull that off and
37 // add it to the end of the `buf`.
38 Ok(Chunk::NeedMoreData(_n)) => {
39 buf.extend(data.next().unwrap());
40 continue;
41 }
42 Ok(Chunk::Parsed { consumed, payload }) => {
43 log::debug!("parsed {consumed} bytes");
44 pos += consumed;
45 payload
46 }
47
48 // On failure we should receive the same failure as if we did a full
49 // parse.
50 Err(actual) => {
51 let expected = expected
52 .next()
53 .expect("full parse stopped early")
54 .err()
55 .expect("full parse didn't return an error");
56 assert_eq!(expected.offset(), actual.offset());
57 assert_eq!(expected.message(), actual.message());
58 break;
59 }
60 };
61 log::debug!("parsed payload {payload:?}");
62 let expected_payload = expected
63 .next()
64 .expect("full parse stopped early")
65 .expect("full parse failed but incremental succeeded");
66 match (payload, expected_payload) {
67 (End(_), End(_)) => match stack.pop() {
68 Some(p) => parser = p,
69 None => {
70 log::debug!("no more parsers");
71 assert!(expected.next().is_none());
72 break;
73 }
74 },
75 (
76 Version {
77 num: a,
78 encoding: ae,
79 range: ar,
80 },
81 Version {
82 num: b,
83 encoding: be,
84 range: br,
85 },
86 ) => {
87 assert_eq!(a, b);
88 assert_eq!(ae, be);
89 assert_eq!(ar, br);
90 }
91
92 (TypeSection(a), TypeSection(b)) => assert_eq!(a.range(), b.range()),
93 (ImportSection(a), ImportSection(b)) => assert_eq!(a.range(), b.range()),
94 (FunctionSection(a), FunctionSection(b)) => assert_eq!(a.range(), b.range()),
95 (TableSection(a), TableSection(b)) => assert_eq!(a.range(), b.range()),
96 (MemorySection(a), MemorySection(b)) => assert_eq!(a.range(), b.range()),
97 (GlobalSection(a), GlobalSection(b)) => assert_eq!(a.range(), b.range()),
98 (ExportSection(a), ExportSection(b)) => assert_eq!(a.range(), b.range()),
99 (TagSection(a), TagSection(b)) => assert_eq!(a.range(), b.range()),
100 (StartSection { func: a, range: ar }, StartSection { func: b, range: br }) => {
101 assert_eq!(a, b);
102 assert_eq!(ar, br);
103 }
104 (ElementSection(a), ElementSection(b)) => assert_eq!(a.range(), b.range()),
105 (
106 DataCountSection {
107 count: a,
108 range: ar,
109 },
110 DataCountSection {
111 count: b,
112 range: br,
113 },
114 ) => {
115 assert_eq!(a, b);
116 assert_eq!(ar, br);
117 }
118 (DataSection(a), DataSection(b)) => assert_eq!(a.range(), b.range()),
119 (CustomSection(ca), CustomSection(cb)) => {
120 assert_eq!(ca.name(), cb.name());
121 assert_eq!(ca.data_offset(), cb.data_offset());
122 assert_eq!(ca.data(), cb.data());
123 assert_eq!(ca.range(), cb.range());
124 }
125 (
126 CodeSectionStart {
127 count: a,
128 range: ar,
129 size: asz,
130 },
131 CodeSectionStart {
132 count: b,
133 range: br,
134 size: bsz,
135 },
136 ) => {
137 assert_eq!(a, b);
138 assert_eq!(ar, br);
139 assert_eq!(asz, bsz);
140 }
141
142 (CodeSectionEntry(a), CodeSectionEntry(b)) => {
143 assert_eq!(a.get_binary_reader().range(), b.get_binary_reader().range());
144 }
145
146 (
147 ModuleSection {
148 parser: p,
149 unchecked_range: a,
150 },
151 ModuleSection {
152 unchecked_range: b, ..
153 },
154 ) => {
155 assert_eq!(a, b);
156 stack.push(parser);
157 parser = p;
158 }
159 (InstanceSection(a), InstanceSection(b)) => assert_eq!(a.range(), b.range()),
160 (
161 ComponentSection {
162 parser: p,
163 unchecked_range: a,
164 },
165 ComponentSection {
166 unchecked_range: b, ..
167 },
168 ) => {
169 assert_eq!(a, b);
170 stack.push(parser);
171 parser = p;
172 }
173 (ComponentInstanceSection(a), ComponentInstanceSection(b)) => {
174 assert_eq!(a.range(), b.range())
175 }
176 (ComponentAliasSection(a), ComponentAliasSection(b)) => {
177 assert_eq!(a.range(), b.range())
178 }
179 (ComponentTypeSection(a), ComponentTypeSection(b)) => assert_eq!(a.range(), b.range()),
180 (ComponentCanonicalSection(a), ComponentCanonicalSection(b)) => {
181 assert_eq!(a.range(), b.range())
182 }
183 (ComponentStartSection { range: a, .. }, ComponentStartSection { range: b, .. }) => {
184 assert_eq!(a, b)
185 }
186 (ComponentImportSection(a), ComponentImportSection(b)) => {
187 assert_eq!(a.range(), b.range())
188 }
189 (ComponentExportSection(a), ComponentExportSection(b)) => {
190 assert_eq!(a.range(), b.range())
191 }
192 (CoreTypeSection(a), CoreTypeSection(b)) => {
193 assert_eq!(a.range(), b.range())
194 }
195
196 (
197 UnknownSection {
198 id: a,
199 contents: ac,
200 range: ar,
201 },
202 UnknownSection {
203 id: b,
204 contents: bc,
205 range: br,
206 },
207 ) => {
208 assert_eq!(a, b);
209 assert_eq!(ar, br);
210 assert_eq!(ac, bc);
211 }
212
213 (a, b) => {
214 panic!("expected {b:?}\ngot {a:?}");
215 }
216 }
217 }
218 Ok(())
219 }
220
221 #[test]
222 fn smoke() {
223 super::test::test_n_times(100, run);
224 }