/src/serenity/Userland/Libraries/LibWasm/Parser/Parser.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/ConstrainedStream.h> |
8 | | #include <AK/Debug.h> |
9 | | #include <AK/Endian.h> |
10 | | #include <AK/LEB128.h> |
11 | | #include <AK/MemoryStream.h> |
12 | | #include <AK/ScopeGuard.h> |
13 | | #include <AK/ScopeLogger.h> |
14 | | #include <AK/UFixedBigInt.h> |
15 | | #include <LibWasm/Types.h> |
16 | | |
17 | | namespace Wasm { |
18 | | |
19 | | #define TRY_READ(stream, type, error) \ |
20 | 0 | ({ \ |
21 | 0 | /* Ignore -Wshadow to allow nesting the macro. */ \ |
22 | 0 | AK_IGNORE_DIAGNOSTIC("-Wshadow", \ |
23 | 0 | auto&& _temporary_result = stream.read_value<type>()); \ |
24 | 0 | static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \ |
25 | 0 | "Do not return a reference from a fallible expression"); \ |
26 | 0 | if (_temporary_result.is_error()) [[unlikely]] \ |
27 | 0 | return with_eof_check(stream, error); \ |
28 | 0 | _temporary_result.release_value(); \ |
29 | 0 | }) |
30 | | |
31 | | ParseError with_eof_check(Stream const& stream, ParseError error_if_not_eof) |
32 | 0 | { |
33 | 0 | if (stream.is_eof()) |
34 | 0 | return ParseError::UnexpectedEof; |
35 | 0 | return error_if_not_eof; |
36 | 0 | } |
37 | | |
38 | | template<typename T> |
39 | | static auto parse_vector(Stream& stream) |
40 | 0 | { |
41 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger; |
42 | 0 | if constexpr (requires { T::parse(stream); }) { |
43 | 0 | using ResultT = typename decltype(T::parse(stream))::ResultType; |
44 | 0 | auto count_or_error = stream.read_value<LEB128<u32>>(); |
45 | 0 | if (count_or_error.is_error()) |
46 | 0 | return ParseResult<Vector<ResultT>> { with_eof_check(stream, ParseError::ExpectedSize) }; |
47 | 0 | size_t count = count_or_error.release_value(); |
48 | |
|
49 | 0 | Vector<ResultT> entries; |
50 | 0 | entries.ensure_capacity(count); |
51 | 0 | for (size_t i = 0; i < count; ++i) { |
52 | 0 | auto result = T::parse(stream); |
53 | 0 | if (result.is_error()) |
54 | 0 | return ParseResult<Vector<ResultT>> { result.error() }; |
55 | 0 | entries.append(result.release_value()); |
56 | 0 | } |
57 | 0 | return ParseResult<Vector<ResultT>> { move(entries) }; |
58 | 0 | } else { |
59 | 0 | auto count_or_error = stream.read_value<LEB128<u32>>(); |
60 | 0 | if (count_or_error.is_error()) |
61 | 0 | return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) }; |
62 | 0 | size_t count = count_or_error.release_value(); |
63 | |
|
64 | 0 | Vector<T> entries; |
65 | 0 | entries.ensure_capacity(count); |
66 | 0 | for (size_t i = 0; i < count; ++i) { |
67 | 0 | if constexpr (IsSame<T, u32>) { |
68 | 0 | auto value_or_error = stream.read_value<LEB128<u32>>(); |
69 | 0 | if (value_or_error.is_error()) |
70 | 0 | return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) }; |
71 | 0 | size_t value = value_or_error.release_value(); |
72 | 0 | entries.append(value); |
73 | | } else if constexpr (IsSame<T, ssize_t>) { |
74 | | auto value_or_error = stream.read_value<LEB128<ssize_t>>(); |
75 | | if (value_or_error.is_error()) |
76 | | return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) }; |
77 | | ssize_t value = value_or_error.release_value(); |
78 | | entries.append(value); |
79 | 0 | } else if constexpr (IsSame<T, u8>) { |
80 | 0 | if (count > Constants::max_allowed_vector_size) |
81 | 0 | return ParseResult<Vector<T>> { ParseError::HugeAllocationRequested }; |
82 | 0 | entries.resize(count); |
83 | 0 | if (stream.read_until_filled({ entries.data(), entries.size() }).is_error()) |
84 | 0 | return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::InvalidInput) }; |
85 | 0 | break; // Note: We read this all in one go! |
86 | 0 | } |
87 | 0 | } |
88 | 0 | return ParseResult<Vector<T>> { move(entries) }; |
89 | 0 | } |
90 | 0 | } Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::ValueType>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::GenericIndexParser<AK::DistinctNumeric<unsigned long, Wasm::__LabelIndex_tag, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::CastToBool> > >(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::FunctionType>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::ImportSection::Import>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<unsigned int>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::TableSection::Table>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::MemorySection::Memory>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::GlobalSection::Global>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::ExportSection::Export>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::GenericIndexParser<AK::DistinctNumeric<unsigned long, Wasm::__FunctionIndex_tag, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::CastToBool> > >(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::Expression>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::ElementSection::Element>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::Locals>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::CodeSection::Code>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<unsigned char>(AK::Stream&) Unexecuted instantiation: Parser.cpp:auto Wasm::parse_vector<Wasm::DataSection::Data>(AK::Stream&) |
91 | | |
92 | | static ParseResult<ByteString> parse_name(Stream& stream) |
93 | 0 | { |
94 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger; |
95 | 0 | auto data = TRY(parse_vector<u8>(stream)); |
96 | 0 | auto string = ByteString::copy(data); |
97 | 0 | if (!Utf8View(string).validate(Utf8View::AllowSurrogates::No)) |
98 | 0 | return ParseError::InvalidUtf8; |
99 | 0 | return string; |
100 | 0 | } |
101 | | |
102 | | ParseResult<ValueType> ValueType::parse(Stream& stream) |
103 | 0 | { |
104 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("ValueType"sv); |
105 | 0 | auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
106 | |
|
107 | 0 | switch (tag) { |
108 | 0 | case Constants::i32_tag: |
109 | 0 | return ValueType(I32); |
110 | 0 | case Constants::i64_tag: |
111 | 0 | return ValueType(I64); |
112 | 0 | case Constants::f32_tag: |
113 | 0 | return ValueType(F32); |
114 | 0 | case Constants::f64_tag: |
115 | 0 | return ValueType(F64); |
116 | 0 | case Constants::v128_tag: |
117 | 0 | return ValueType(V128); |
118 | 0 | case Constants::function_reference_tag: |
119 | 0 | return ValueType(FunctionReference); |
120 | 0 | case Constants::extern_reference_tag: |
121 | 0 | return ValueType(ExternReference); |
122 | 0 | default: |
123 | 0 | return ParseError::InvalidTag; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | ParseResult<ResultType> ResultType::parse(Stream& stream) |
128 | 0 | { |
129 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("ResultType"sv); |
130 | 0 | auto types = TRY(parse_vector<ValueType>(stream)); |
131 | 0 | return ResultType { types }; |
132 | 0 | } |
133 | | |
134 | | ParseResult<FunctionType> FunctionType::parse(Stream& stream) |
135 | 0 | { |
136 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionType"sv); |
137 | 0 | auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
138 | |
|
139 | 0 | if (tag != Constants::function_signature_tag) { |
140 | 0 | dbgln("Expected 0x60, but found {:#x}", tag); |
141 | 0 | return with_eof_check(stream, ParseError::InvalidTag); |
142 | 0 | } |
143 | | |
144 | 0 | auto parameters_result = TRY(parse_vector<ValueType>(stream)); |
145 | 0 | auto results_result = TRY(parse_vector<ValueType>(stream)); |
146 | |
|
147 | 0 | return FunctionType { parameters_result, results_result }; |
148 | 0 | } |
149 | | |
150 | | ParseResult<Limits> Limits::parse(Stream& stream) |
151 | 0 | { |
152 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Limits"sv); |
153 | 0 | auto flag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
154 | |
|
155 | 0 | if (flag > 1) |
156 | 0 | return with_eof_check(stream, ParseError::InvalidTag); |
157 | | |
158 | 0 | auto min_or_error = stream.read_value<LEB128<u32>>(); |
159 | 0 | if (min_or_error.is_error()) |
160 | 0 | return with_eof_check(stream, ParseError::ExpectedSize); |
161 | 0 | size_t min = min_or_error.release_value(); |
162 | |
|
163 | 0 | Optional<u32> max; |
164 | 0 | if (flag) { |
165 | 0 | auto value_or_error = stream.read_value<LEB128<u32>>(); |
166 | 0 | if (value_or_error.is_error()) |
167 | 0 | return with_eof_check(stream, ParseError::ExpectedSize); |
168 | 0 | max = value_or_error.release_value(); |
169 | 0 | } |
170 | | |
171 | 0 | return Limits { static_cast<u32>(min), move(max) }; |
172 | 0 | } |
173 | | |
174 | | ParseResult<MemoryType> MemoryType::parse(Stream& stream) |
175 | 0 | { |
176 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemoryType"sv); |
177 | 0 | auto limits_result = TRY(Limits::parse(stream)); |
178 | 0 | return MemoryType { limits_result }; |
179 | 0 | } |
180 | | |
181 | | ParseResult<TableType> TableType::parse(Stream& stream) |
182 | 0 | { |
183 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableType"sv); |
184 | 0 | auto type_result = TRY(ValueType::parse(stream)); |
185 | 0 | if (!type_result.is_reference()) |
186 | 0 | return ParseError::InvalidType; |
187 | 0 | auto limits_result = TRY(Limits::parse(stream)); |
188 | 0 | return TableType { type_result, limits_result }; |
189 | 0 | } |
190 | | |
191 | | ParseResult<GlobalType> GlobalType::parse(Stream& stream) |
192 | 0 | { |
193 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalType"sv); |
194 | 0 | auto type_result = TRY(ValueType::parse(stream)); |
195 | 0 | auto mutable_ = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
196 | |
|
197 | 0 | if (mutable_ > 1) |
198 | 0 | return with_eof_check(stream, ParseError::InvalidTag); |
199 | | |
200 | 0 | return GlobalType { type_result, mutable_ == 0x01 }; |
201 | 0 | } |
202 | | |
203 | | ParseResult<BlockType> BlockType::parse(Stream& stream) |
204 | 0 | { |
205 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("BlockType"sv); |
206 | 0 | auto kind = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
207 | |
|
208 | 0 | if (kind == Constants::empty_block_tag) |
209 | 0 | return BlockType {}; |
210 | | |
211 | 0 | { |
212 | 0 | FixedMemoryStream value_stream { ReadonlyBytes { &kind, 1 } }; |
213 | 0 | if (auto value_type = ValueType::parse(value_stream); !value_type.is_error()) |
214 | 0 | return BlockType { value_type.release_value() }; |
215 | 0 | } |
216 | | |
217 | 0 | ReconsumableStream new_stream { stream }; |
218 | 0 | new_stream.unread({ &kind, 1 }); |
219 | | |
220 | | // FIXME: should be an i33. Right now, we're missing a potential last bit at |
221 | | // the end. See https://webassembly.github.io/spec/core/binary/instructions.html#binary-blocktype |
222 | 0 | i32 index_value = TRY_READ(new_stream, LEB128<i32>, ParseError::ExpectedIndex); |
223 | |
|
224 | 0 | if (index_value < 0) { |
225 | 0 | dbgln("Invalid type index {}", index_value); |
226 | 0 | return with_eof_check(stream, ParseError::InvalidIndex); |
227 | 0 | } |
228 | | |
229 | 0 | return BlockType { TypeIndex(index_value) }; |
230 | 0 | } |
231 | | |
232 | | ParseResult<Instruction> Instruction::parse(Stream& stream) |
233 | 0 | { |
234 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Instruction"sv); |
235 | 0 | auto byte = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
236 | |
|
237 | 0 | OpCode opcode { byte }; |
238 | |
|
239 | 0 | switch (opcode.value()) { |
240 | 0 | case Instructions::block.value(): |
241 | 0 | case Instructions::loop.value(): |
242 | 0 | case Instructions::if_.value(): { |
243 | 0 | auto block_type = TRY(BlockType::parse(stream)); |
244 | 0 | return Instruction { |
245 | 0 | opcode, StructuredInstructionArgs { block_type, {}, {} } |
246 | 0 | }; |
247 | 0 | } |
248 | 0 | case Instructions::br.value(): |
249 | 0 | case Instructions::br_if.value(): { |
250 | | // branches with a single label immediate |
251 | 0 | auto index = TRY(GenericIndexParser<LabelIndex>::parse(stream)); |
252 | 0 | return Instruction { opcode, index }; |
253 | 0 | } |
254 | 0 | case Instructions::br_table.value(): { |
255 | | // br_table label* label |
256 | 0 | auto labels = TRY(parse_vector<GenericIndexParser<LabelIndex>>(stream)); |
257 | 0 | auto default_label = TRY(GenericIndexParser<LabelIndex>::parse(stream)); |
258 | 0 | return Instruction { opcode, TableBranchArgs { labels, default_label } }; |
259 | 0 | } |
260 | 0 | case Instructions::call.value(): { |
261 | | // call function |
262 | 0 | auto function_index = TRY(GenericIndexParser<FunctionIndex>::parse(stream)); |
263 | 0 | return Instruction { opcode, function_index }; |
264 | 0 | } |
265 | 0 | case Instructions::call_indirect.value(): { |
266 | | // call_indirect type table |
267 | 0 | auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream)); |
268 | 0 | auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
269 | 0 | return Instruction { opcode, IndirectCallArgs { type_index, table_index } }; |
270 | 0 | } |
271 | 0 | case Instructions::i32_load.value(): |
272 | 0 | case Instructions::i64_load.value(): |
273 | 0 | case Instructions::f32_load.value(): |
274 | 0 | case Instructions::f64_load.value(): |
275 | 0 | case Instructions::i32_load8_s.value(): |
276 | 0 | case Instructions::i32_load8_u.value(): |
277 | 0 | case Instructions::i32_load16_s.value(): |
278 | 0 | case Instructions::i32_load16_u.value(): |
279 | 0 | case Instructions::i64_load8_s.value(): |
280 | 0 | case Instructions::i64_load8_u.value(): |
281 | 0 | case Instructions::i64_load16_s.value(): |
282 | 0 | case Instructions::i64_load16_u.value(): |
283 | 0 | case Instructions::i64_load32_s.value(): |
284 | 0 | case Instructions::i64_load32_u.value(): |
285 | 0 | case Instructions::i32_store.value(): |
286 | 0 | case Instructions::i64_store.value(): |
287 | 0 | case Instructions::f32_store.value(): |
288 | 0 | case Instructions::f64_store.value(): |
289 | 0 | case Instructions::i32_store8.value(): |
290 | 0 | case Instructions::i32_store16.value(): |
291 | 0 | case Instructions::i64_store8.value(): |
292 | 0 | case Instructions::i64_store16.value(): |
293 | 0 | case Instructions::i64_store32.value(): { |
294 | | // op (align [multi-memory: memindex] offset) |
295 | 0 | u32 align = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
296 | | |
297 | | // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment. |
298 | 0 | auto memory_index = 0; |
299 | 0 | if ((align & 0x40) != 0) { |
300 | 0 | align &= ~0x40; |
301 | 0 | memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
302 | 0 | } |
303 | |
|
304 | 0 | auto offset = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
305 | |
|
306 | 0 | return Instruction { opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } }; |
307 | 0 | } |
308 | 0 | case Instructions::local_get.value(): |
309 | 0 | case Instructions::local_set.value(): |
310 | 0 | case Instructions::local_tee.value(): { |
311 | 0 | auto index = TRY(GenericIndexParser<LocalIndex>::parse(stream)); |
312 | 0 | return Instruction { opcode, index }; |
313 | 0 | } |
314 | 0 | case Instructions::global_get.value(): |
315 | 0 | case Instructions::global_set.value(): { |
316 | 0 | auto index = TRY(GenericIndexParser<GlobalIndex>::parse(stream)); |
317 | 0 | return Instruction { opcode, index }; |
318 | 0 | } |
319 | 0 | case Instructions::memory_size.value(): |
320 | 0 | case Instructions::memory_grow.value(): { |
321 | | // op [multi-memory: memindex]|0x00 |
322 | 0 | auto memory_index = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
323 | |
|
324 | 0 | return Instruction { opcode, MemoryIndexArgument { MemoryIndex(memory_index) } }; |
325 | 0 | } |
326 | 0 | case Instructions::i32_const.value(): { |
327 | 0 | auto value = TRY_READ(stream, LEB128<i32>, ParseError::ExpectedSignedImmediate); |
328 | |
|
329 | 0 | return Instruction { opcode, value }; |
330 | 0 | } |
331 | 0 | case Instructions::i64_const.value(): { |
332 | | // op literal |
333 | 0 | auto value = TRY_READ(stream, LEB128<i64>, ParseError::ExpectedSignedImmediate); |
334 | |
|
335 | 0 | return Instruction { opcode, value }; |
336 | 0 | } |
337 | 0 | case Instructions::f32_const.value(): { |
338 | | // op literal |
339 | 0 | auto value = TRY_READ(stream, LittleEndian<u32>, ParseError::ExpectedFloatingImmediate); |
340 | |
|
341 | 0 | auto floating = bit_cast<float>(static_cast<u32>(value)); |
342 | 0 | return Instruction { opcode, floating }; |
343 | 0 | } |
344 | 0 | case Instructions::f64_const.value(): { |
345 | | // op literal |
346 | 0 | auto value = TRY_READ(stream, LittleEndian<u64>, ParseError::ExpectedFloatingImmediate); |
347 | |
|
348 | 0 | auto floating = bit_cast<double>(static_cast<u64>(value)); |
349 | 0 | return Instruction { opcode, floating }; |
350 | 0 | } |
351 | 0 | case Instructions::table_get.value(): |
352 | 0 | case Instructions::table_set.value(): { |
353 | 0 | auto index = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
354 | 0 | return Instruction { opcode, index }; |
355 | 0 | } |
356 | 0 | case Instructions::select_typed.value(): { |
357 | 0 | auto types = TRY(parse_vector<ValueType>(stream)); |
358 | 0 | return Instruction { opcode, types }; |
359 | 0 | } |
360 | 0 | case Instructions::ref_null.value(): { |
361 | 0 | auto type = TRY(ValueType::parse(stream)); |
362 | 0 | if (!type.is_reference()) |
363 | 0 | return ParseError::InvalidType; |
364 | | |
365 | 0 | return Instruction { opcode, type }; |
366 | 0 | } |
367 | 0 | case Instructions::ref_func.value(): { |
368 | 0 | auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream)); |
369 | 0 | return Instruction { opcode, index }; |
370 | 0 | } |
371 | 0 | case Instructions::structured_end.value(): |
372 | 0 | case Instructions::structured_else.value(): |
373 | 0 | case Instructions::ref_is_null.value(): |
374 | 0 | case Instructions::unreachable.value(): |
375 | 0 | case Instructions::nop.value(): |
376 | 0 | case Instructions::return_.value(): |
377 | 0 | case Instructions::drop.value(): |
378 | 0 | case Instructions::select.value(): |
379 | 0 | case Instructions::i32_eqz.value(): |
380 | 0 | case Instructions::i32_eq.value(): |
381 | 0 | case Instructions::i32_ne.value(): |
382 | 0 | case Instructions::i32_lts.value(): |
383 | 0 | case Instructions::i32_ltu.value(): |
384 | 0 | case Instructions::i32_gts.value(): |
385 | 0 | case Instructions::i32_gtu.value(): |
386 | 0 | case Instructions::i32_les.value(): |
387 | 0 | case Instructions::i32_leu.value(): |
388 | 0 | case Instructions::i32_ges.value(): |
389 | 0 | case Instructions::i32_geu.value(): |
390 | 0 | case Instructions::i64_eqz.value(): |
391 | 0 | case Instructions::i64_eq.value(): |
392 | 0 | case Instructions::i64_ne.value(): |
393 | 0 | case Instructions::i64_lts.value(): |
394 | 0 | case Instructions::i64_ltu.value(): |
395 | 0 | case Instructions::i64_gts.value(): |
396 | 0 | case Instructions::i64_gtu.value(): |
397 | 0 | case Instructions::i64_les.value(): |
398 | 0 | case Instructions::i64_leu.value(): |
399 | 0 | case Instructions::i64_ges.value(): |
400 | 0 | case Instructions::i64_geu.value(): |
401 | 0 | case Instructions::f32_eq.value(): |
402 | 0 | case Instructions::f32_ne.value(): |
403 | 0 | case Instructions::f32_lt.value(): |
404 | 0 | case Instructions::f32_gt.value(): |
405 | 0 | case Instructions::f32_le.value(): |
406 | 0 | case Instructions::f32_ge.value(): |
407 | 0 | case Instructions::f64_eq.value(): |
408 | 0 | case Instructions::f64_ne.value(): |
409 | 0 | case Instructions::f64_lt.value(): |
410 | 0 | case Instructions::f64_gt.value(): |
411 | 0 | case Instructions::f64_le.value(): |
412 | 0 | case Instructions::f64_ge.value(): |
413 | 0 | case Instructions::i32_clz.value(): |
414 | 0 | case Instructions::i32_ctz.value(): |
415 | 0 | case Instructions::i32_popcnt.value(): |
416 | 0 | case Instructions::i32_add.value(): |
417 | 0 | case Instructions::i32_sub.value(): |
418 | 0 | case Instructions::i32_mul.value(): |
419 | 0 | case Instructions::i32_divs.value(): |
420 | 0 | case Instructions::i32_divu.value(): |
421 | 0 | case Instructions::i32_rems.value(): |
422 | 0 | case Instructions::i32_remu.value(): |
423 | 0 | case Instructions::i32_and.value(): |
424 | 0 | case Instructions::i32_or.value(): |
425 | 0 | case Instructions::i32_xor.value(): |
426 | 0 | case Instructions::i32_shl.value(): |
427 | 0 | case Instructions::i32_shrs.value(): |
428 | 0 | case Instructions::i32_shru.value(): |
429 | 0 | case Instructions::i32_rotl.value(): |
430 | 0 | case Instructions::i32_rotr.value(): |
431 | 0 | case Instructions::i64_clz.value(): |
432 | 0 | case Instructions::i64_ctz.value(): |
433 | 0 | case Instructions::i64_popcnt.value(): |
434 | 0 | case Instructions::i64_add.value(): |
435 | 0 | case Instructions::i64_sub.value(): |
436 | 0 | case Instructions::i64_mul.value(): |
437 | 0 | case Instructions::i64_divs.value(): |
438 | 0 | case Instructions::i64_divu.value(): |
439 | 0 | case Instructions::i64_rems.value(): |
440 | 0 | case Instructions::i64_remu.value(): |
441 | 0 | case Instructions::i64_and.value(): |
442 | 0 | case Instructions::i64_or.value(): |
443 | 0 | case Instructions::i64_xor.value(): |
444 | 0 | case Instructions::i64_shl.value(): |
445 | 0 | case Instructions::i64_shrs.value(): |
446 | 0 | case Instructions::i64_shru.value(): |
447 | 0 | case Instructions::i64_rotl.value(): |
448 | 0 | case Instructions::i64_rotr.value(): |
449 | 0 | case Instructions::f32_abs.value(): |
450 | 0 | case Instructions::f32_neg.value(): |
451 | 0 | case Instructions::f32_ceil.value(): |
452 | 0 | case Instructions::f32_floor.value(): |
453 | 0 | case Instructions::f32_trunc.value(): |
454 | 0 | case Instructions::f32_nearest.value(): |
455 | 0 | case Instructions::f32_sqrt.value(): |
456 | 0 | case Instructions::f32_add.value(): |
457 | 0 | case Instructions::f32_sub.value(): |
458 | 0 | case Instructions::f32_mul.value(): |
459 | 0 | case Instructions::f32_div.value(): |
460 | 0 | case Instructions::f32_min.value(): |
461 | 0 | case Instructions::f32_max.value(): |
462 | 0 | case Instructions::f32_copysign.value(): |
463 | 0 | case Instructions::f64_abs.value(): |
464 | 0 | case Instructions::f64_neg.value(): |
465 | 0 | case Instructions::f64_ceil.value(): |
466 | 0 | case Instructions::f64_floor.value(): |
467 | 0 | case Instructions::f64_trunc.value(): |
468 | 0 | case Instructions::f64_nearest.value(): |
469 | 0 | case Instructions::f64_sqrt.value(): |
470 | 0 | case Instructions::f64_add.value(): |
471 | 0 | case Instructions::f64_sub.value(): |
472 | 0 | case Instructions::f64_mul.value(): |
473 | 0 | case Instructions::f64_div.value(): |
474 | 0 | case Instructions::f64_min.value(): |
475 | 0 | case Instructions::f64_max.value(): |
476 | 0 | case Instructions::f64_copysign.value(): |
477 | 0 | case Instructions::i32_wrap_i64.value(): |
478 | 0 | case Instructions::i32_trunc_sf32.value(): |
479 | 0 | case Instructions::i32_trunc_uf32.value(): |
480 | 0 | case Instructions::i32_trunc_sf64.value(): |
481 | 0 | case Instructions::i32_trunc_uf64.value(): |
482 | 0 | case Instructions::i64_extend_si32.value(): |
483 | 0 | case Instructions::i64_extend_ui32.value(): |
484 | 0 | case Instructions::i64_trunc_sf32.value(): |
485 | 0 | case Instructions::i64_trunc_uf32.value(): |
486 | 0 | case Instructions::i64_trunc_sf64.value(): |
487 | 0 | case Instructions::i64_trunc_uf64.value(): |
488 | 0 | case Instructions::f32_convert_si32.value(): |
489 | 0 | case Instructions::f32_convert_ui32.value(): |
490 | 0 | case Instructions::f32_convert_si64.value(): |
491 | 0 | case Instructions::f32_convert_ui64.value(): |
492 | 0 | case Instructions::f32_demote_f64.value(): |
493 | 0 | case Instructions::f64_convert_si32.value(): |
494 | 0 | case Instructions::f64_convert_ui32.value(): |
495 | 0 | case Instructions::f64_convert_si64.value(): |
496 | 0 | case Instructions::f64_convert_ui64.value(): |
497 | 0 | case Instructions::f64_promote_f32.value(): |
498 | 0 | case Instructions::i32_reinterpret_f32.value(): |
499 | 0 | case Instructions::i64_reinterpret_f64.value(): |
500 | 0 | case Instructions::f32_reinterpret_i32.value(): |
501 | 0 | case Instructions::f64_reinterpret_i64.value(): |
502 | 0 | case Instructions::i32_extend8_s.value(): |
503 | 0 | case Instructions::i32_extend16_s.value(): |
504 | 0 | case Instructions::i64_extend8_s.value(): |
505 | 0 | case Instructions::i64_extend16_s.value(): |
506 | 0 | case Instructions::i64_extend32_s.value(): |
507 | 0 | return Instruction { opcode }; |
508 | 0 | case 0xfc: |
509 | 0 | case 0xfd: { |
510 | | // These are multibyte instructions. |
511 | 0 | auto selector = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
512 | 0 | OpCode full_opcode = static_cast<u64>(opcode.value()) << 56 | selector; |
513 | |
|
514 | 0 | switch (full_opcode.value()) { |
515 | 0 | case Instructions::i32_trunc_sat_f32_s.value(): |
516 | 0 | case Instructions::i32_trunc_sat_f32_u.value(): |
517 | 0 | case Instructions::i32_trunc_sat_f64_s.value(): |
518 | 0 | case Instructions::i32_trunc_sat_f64_u.value(): |
519 | 0 | case Instructions::i64_trunc_sat_f32_s.value(): |
520 | 0 | case Instructions::i64_trunc_sat_f32_u.value(): |
521 | 0 | case Instructions::i64_trunc_sat_f64_s.value(): |
522 | 0 | case Instructions::i64_trunc_sat_f64_u.value(): |
523 | 0 | return Instruction { full_opcode }; |
524 | 0 | case Instructions::memory_init.value(): { |
525 | 0 | auto index = TRY(GenericIndexParser<DataIndex>::parse(stream)); |
526 | | |
527 | | // Proposal "multi-memory", literal 0x00 is replaced with a memory index. |
528 | 0 | auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput); |
529 | |
|
530 | 0 | return Instruction { full_opcode, MemoryInitArgs { index, MemoryIndex(memory_index) } }; |
531 | 0 | } |
532 | 0 | case Instructions::data_drop.value(): { |
533 | 0 | auto index = TRY(GenericIndexParser<DataIndex>::parse(stream)); |
534 | 0 | return Instruction { full_opcode, index }; |
535 | 0 | } |
536 | 0 | case Instructions::memory_copy.value(): { |
537 | | // Proposal "multi-memory", literal 0x00 is replaced with two memory indices, destination and source, respectively. |
538 | 0 | MemoryIndex indices[] = { 0, 0 }; |
539 | |
|
540 | 0 | for (size_t i = 0; i < 2; ++i) { |
541 | 0 | auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput); |
542 | 0 | indices[i] = memory_index; |
543 | 0 | } |
544 | 0 | return Instruction { full_opcode, MemoryCopyArgs { indices[1], indices[0] } }; |
545 | 0 | } |
546 | 0 | case Instructions::memory_fill.value(): { |
547 | | // Proposal "multi-memory", literal 0x00 is replaced with a memory index. |
548 | 0 | auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput); |
549 | 0 | return Instruction { full_opcode, MemoryIndexArgument { MemoryIndex { memory_index } } }; |
550 | 0 | } |
551 | 0 | case Instructions::table_init.value(): { |
552 | 0 | auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream)); |
553 | 0 | auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
554 | 0 | return Instruction { full_opcode, TableElementArgs { element_index, table_index } }; |
555 | 0 | } |
556 | 0 | case Instructions::elem_drop.value(): { |
557 | 0 | auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream)); |
558 | 0 | return Instruction { full_opcode, element_index }; |
559 | 0 | } |
560 | 0 | case Instructions::table_copy.value(): { |
561 | 0 | auto lhs = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
562 | 0 | auto rhs = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
563 | 0 | return Instruction { full_opcode, TableTableArgs { lhs, rhs } }; |
564 | 0 | } |
565 | 0 | case Instructions::table_grow.value(): |
566 | 0 | case Instructions::table_size.value(): |
567 | 0 | case Instructions::table_fill.value(): { |
568 | 0 | auto index = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
569 | 0 | return Instruction { full_opcode, index }; |
570 | 0 | } |
571 | 0 | case Instructions::v128_load.value(): |
572 | 0 | case Instructions::v128_load8x8_s.value(): |
573 | 0 | case Instructions::v128_load8x8_u.value(): |
574 | 0 | case Instructions::v128_load16x4_s.value(): |
575 | 0 | case Instructions::v128_load16x4_u.value(): |
576 | 0 | case Instructions::v128_load32x2_s.value(): |
577 | 0 | case Instructions::v128_load32x2_u.value(): |
578 | 0 | case Instructions::v128_load8_splat.value(): |
579 | 0 | case Instructions::v128_load16_splat.value(): |
580 | 0 | case Instructions::v128_load32_splat.value(): |
581 | 0 | case Instructions::v128_load64_splat.value(): |
582 | 0 | case Instructions::v128_load32_zero.value(): |
583 | 0 | case Instructions::v128_load64_zero.value(): |
584 | 0 | case Instructions::v128_store.value(): { |
585 | | // op (align [multi-memory memindex] offset) |
586 | 0 | u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex); |
587 | | |
588 | | // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment. |
589 | 0 | auto memory_index = 0; |
590 | 0 | if ((align & 0x20) != 0) { |
591 | 0 | align &= ~0x20; |
592 | 0 | memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
593 | 0 | } |
594 | |
|
595 | 0 | auto offset = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex); |
596 | |
|
597 | 0 | return Instruction { full_opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } }; |
598 | 0 | } |
599 | 0 | case Instructions::v128_load8_lane.value(): |
600 | 0 | case Instructions::v128_load16_lane.value(): |
601 | 0 | case Instructions::v128_load32_lane.value(): |
602 | 0 | case Instructions::v128_load64_lane.value(): |
603 | 0 | case Instructions::v128_store8_lane.value(): |
604 | 0 | case Instructions::v128_store16_lane.value(): |
605 | 0 | case Instructions::v128_store32_lane.value(): |
606 | 0 | case Instructions::v128_store64_lane.value(): { |
607 | | // op (align [multi-memory: memindex] offset) (index) |
608 | 0 | u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex); |
609 | | |
610 | | // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment. |
611 | 0 | auto memory_index = 0; |
612 | 0 | if ((align & 0x20) != 0) { |
613 | 0 | align &= ~0x20; |
614 | 0 | memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput); |
615 | 0 | } |
616 | |
|
617 | 0 | auto offset = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex); |
618 | 0 | auto index = TRY_READ(stream, u8, ParseError::InvalidInput); |
619 | |
|
620 | 0 | return Instruction { full_opcode, MemoryAndLaneArgument { { align, offset, MemoryIndex(memory_index) }, index } }; |
621 | 0 | } |
622 | 0 | case Instructions::v128_const.value(): { |
623 | | // op (literal:16) |
624 | 0 | auto value = TRY_READ(stream, LittleEndian<u128>, ParseError::InvalidImmediate); |
625 | 0 | return Instruction { full_opcode, value }; |
626 | 0 | } |
627 | 0 | case Instructions::i8x16_shuffle.value(): { |
628 | | // op 16x(lane) |
629 | 0 | u8 lanes[16]; |
630 | 0 | for (size_t i = 0; i < 16; ++i) { |
631 | 0 | auto value = TRY_READ(stream, u8, ParseError::InvalidInput); |
632 | 0 | lanes[i] = value; |
633 | 0 | } |
634 | 0 | return Instruction { full_opcode, ShuffleArgument(lanes) }; |
635 | 0 | } |
636 | 0 | case Instructions::i8x16_extract_lane_s.value(): |
637 | 0 | case Instructions::i8x16_extract_lane_u.value(): |
638 | 0 | case Instructions::i8x16_replace_lane.value(): |
639 | 0 | case Instructions::i16x8_extract_lane_s.value(): |
640 | 0 | case Instructions::i16x8_extract_lane_u.value(): |
641 | 0 | case Instructions::i16x8_replace_lane.value(): |
642 | 0 | case Instructions::i32x4_extract_lane.value(): |
643 | 0 | case Instructions::i32x4_replace_lane.value(): |
644 | 0 | case Instructions::i64x2_extract_lane.value(): |
645 | 0 | case Instructions::i64x2_replace_lane.value(): |
646 | 0 | case Instructions::f32x4_extract_lane.value(): |
647 | 0 | case Instructions::f32x4_replace_lane.value(): |
648 | 0 | case Instructions::f64x2_extract_lane.value(): |
649 | 0 | case Instructions::f64x2_replace_lane.value(): { |
650 | | // op (lane) |
651 | 0 | auto lane = TRY_READ(stream, u8, ParseError::InvalidInput); |
652 | 0 | return Instruction { full_opcode, LaneIndex { lane } }; |
653 | 0 | } |
654 | 0 | case Instructions::i8x16_swizzle.value(): |
655 | 0 | case Instructions::i8x16_splat.value(): |
656 | 0 | case Instructions::i16x8_splat.value(): |
657 | 0 | case Instructions::i32x4_splat.value(): |
658 | 0 | case Instructions::i64x2_splat.value(): |
659 | 0 | case Instructions::f32x4_splat.value(): |
660 | 0 | case Instructions::f64x2_splat.value(): |
661 | 0 | case Instructions::i8x16_eq.value(): |
662 | 0 | case Instructions::i8x16_ne.value(): |
663 | 0 | case Instructions::i8x16_lt_s.value(): |
664 | 0 | case Instructions::i8x16_lt_u.value(): |
665 | 0 | case Instructions::i8x16_gt_s.value(): |
666 | 0 | case Instructions::i8x16_gt_u.value(): |
667 | 0 | case Instructions::i8x16_le_s.value(): |
668 | 0 | case Instructions::i8x16_le_u.value(): |
669 | 0 | case Instructions::i8x16_ge_s.value(): |
670 | 0 | case Instructions::i8x16_ge_u.value(): |
671 | 0 | case Instructions::i16x8_eq.value(): |
672 | 0 | case Instructions::i16x8_ne.value(): |
673 | 0 | case Instructions::i16x8_lt_s.value(): |
674 | 0 | case Instructions::i16x8_lt_u.value(): |
675 | 0 | case Instructions::i16x8_gt_s.value(): |
676 | 0 | case Instructions::i16x8_gt_u.value(): |
677 | 0 | case Instructions::i16x8_le_s.value(): |
678 | 0 | case Instructions::i16x8_le_u.value(): |
679 | 0 | case Instructions::i16x8_ge_s.value(): |
680 | 0 | case Instructions::i16x8_ge_u.value(): |
681 | 0 | case Instructions::i32x4_eq.value(): |
682 | 0 | case Instructions::i32x4_ne.value(): |
683 | 0 | case Instructions::i32x4_lt_s.value(): |
684 | 0 | case Instructions::i32x4_lt_u.value(): |
685 | 0 | case Instructions::i32x4_gt_s.value(): |
686 | 0 | case Instructions::i32x4_gt_u.value(): |
687 | 0 | case Instructions::i32x4_le_s.value(): |
688 | 0 | case Instructions::i32x4_le_u.value(): |
689 | 0 | case Instructions::i32x4_ge_s.value(): |
690 | 0 | case Instructions::i32x4_ge_u.value(): |
691 | 0 | case Instructions::f32x4_eq.value(): |
692 | 0 | case Instructions::f32x4_ne.value(): |
693 | 0 | case Instructions::f32x4_lt.value(): |
694 | 0 | case Instructions::f32x4_gt.value(): |
695 | 0 | case Instructions::f32x4_le.value(): |
696 | 0 | case Instructions::f32x4_ge.value(): |
697 | 0 | case Instructions::f64x2_eq.value(): |
698 | 0 | case Instructions::f64x2_ne.value(): |
699 | 0 | case Instructions::f64x2_lt.value(): |
700 | 0 | case Instructions::f64x2_gt.value(): |
701 | 0 | case Instructions::f64x2_le.value(): |
702 | 0 | case Instructions::f64x2_ge.value(): |
703 | 0 | case Instructions::v128_not.value(): |
704 | 0 | case Instructions::v128_and.value(): |
705 | 0 | case Instructions::v128_andnot.value(): |
706 | 0 | case Instructions::v128_or.value(): |
707 | 0 | case Instructions::v128_xor.value(): |
708 | 0 | case Instructions::v128_bitselect.value(): |
709 | 0 | case Instructions::v128_any_true.value(): |
710 | 0 | case Instructions::f32x4_demote_f64x2_zero.value(): |
711 | 0 | case Instructions::f64x2_promote_low_f32x4.value(): |
712 | 0 | case Instructions::i8x16_abs.value(): |
713 | 0 | case Instructions::i8x16_neg.value(): |
714 | 0 | case Instructions::i8x16_popcnt.value(): |
715 | 0 | case Instructions::i8x16_all_true.value(): |
716 | 0 | case Instructions::i8x16_bitmask.value(): |
717 | 0 | case Instructions::i8x16_narrow_i16x8_s.value(): |
718 | 0 | case Instructions::i8x16_narrow_i16x8_u.value(): |
719 | 0 | case Instructions::f32x4_ceil.value(): |
720 | 0 | case Instructions::f32x4_floor.value(): |
721 | 0 | case Instructions::f32x4_trunc.value(): |
722 | 0 | case Instructions::f32x4_nearest.value(): |
723 | 0 | case Instructions::i8x16_shl.value(): |
724 | 0 | case Instructions::i8x16_shr_s.value(): |
725 | 0 | case Instructions::i8x16_shr_u.value(): |
726 | 0 | case Instructions::i8x16_add.value(): |
727 | 0 | case Instructions::i8x16_add_sat_s.value(): |
728 | 0 | case Instructions::i8x16_add_sat_u.value(): |
729 | 0 | case Instructions::i8x16_sub.value(): |
730 | 0 | case Instructions::i8x16_sub_sat_s.value(): |
731 | 0 | case Instructions::i8x16_sub_sat_u.value(): |
732 | 0 | case Instructions::f64x2_ceil.value(): |
733 | 0 | case Instructions::f64x2_floor.value(): |
734 | 0 | case Instructions::i8x16_min_s.value(): |
735 | 0 | case Instructions::i8x16_min_u.value(): |
736 | 0 | case Instructions::i8x16_max_s.value(): |
737 | 0 | case Instructions::i8x16_max_u.value(): |
738 | 0 | case Instructions::f64x2_trunc.value(): |
739 | 0 | case Instructions::i8x16_avgr_u.value(): |
740 | 0 | case Instructions::i16x8_extadd_pairwise_i8x16_s.value(): |
741 | 0 | case Instructions::i16x8_extadd_pairwise_i8x16_u.value(): |
742 | 0 | case Instructions::i32x4_extadd_pairwise_i16x8_s.value(): |
743 | 0 | case Instructions::i32x4_extadd_pairwise_i16x8_u.value(): |
744 | 0 | case Instructions::i16x8_abs.value(): |
745 | 0 | case Instructions::i16x8_neg.value(): |
746 | 0 | case Instructions::i16x8_q15mulr_sat_s.value(): |
747 | 0 | case Instructions::i16x8_all_true.value(): |
748 | 0 | case Instructions::i16x8_bitmask.value(): |
749 | 0 | case Instructions::i16x8_narrow_i32x4_s.value(): |
750 | 0 | case Instructions::i16x8_narrow_i32x4_u.value(): |
751 | 0 | case Instructions::i16x8_extend_low_i8x16_s.value(): |
752 | 0 | case Instructions::i16x8_extend_high_i8x16_s.value(): |
753 | 0 | case Instructions::i16x8_extend_low_i8x16_u.value(): |
754 | 0 | case Instructions::i16x8_extend_high_i8x16_u.value(): |
755 | 0 | case Instructions::i16x8_shl.value(): |
756 | 0 | case Instructions::i16x8_shr_s.value(): |
757 | 0 | case Instructions::i16x8_shr_u.value(): |
758 | 0 | case Instructions::i16x8_add.value(): |
759 | 0 | case Instructions::i16x8_add_sat_s.value(): |
760 | 0 | case Instructions::i16x8_add_sat_u.value(): |
761 | 0 | case Instructions::i16x8_sub.value(): |
762 | 0 | case Instructions::i16x8_sub_sat_s.value(): |
763 | 0 | case Instructions::i16x8_sub_sat_u.value(): |
764 | 0 | case Instructions::f64x2_nearest.value(): |
765 | 0 | case Instructions::i16x8_mul.value(): |
766 | 0 | case Instructions::i16x8_min_s.value(): |
767 | 0 | case Instructions::i16x8_min_u.value(): |
768 | 0 | case Instructions::i16x8_max_s.value(): |
769 | 0 | case Instructions::i16x8_max_u.value(): |
770 | 0 | case Instructions::i16x8_avgr_u.value(): |
771 | 0 | case Instructions::i16x8_extmul_low_i8x16_s.value(): |
772 | 0 | case Instructions::i16x8_extmul_high_i8x16_s.value(): |
773 | 0 | case Instructions::i16x8_extmul_low_i8x16_u.value(): |
774 | 0 | case Instructions::i16x8_extmul_high_i8x16_u.value(): |
775 | 0 | case Instructions::i32x4_abs.value(): |
776 | 0 | case Instructions::i32x4_neg.value(): |
777 | 0 | case Instructions::i32x4_all_true.value(): |
778 | 0 | case Instructions::i32x4_bitmask.value(): |
779 | 0 | case Instructions::i32x4_extend_low_i16x8_s.value(): |
780 | 0 | case Instructions::i32x4_extend_high_i16x8_s.value(): |
781 | 0 | case Instructions::i32x4_extend_low_i16x8_u.value(): |
782 | 0 | case Instructions::i32x4_extend_high_i16x8_u.value(): |
783 | 0 | case Instructions::i32x4_shl.value(): |
784 | 0 | case Instructions::i32x4_shr_s.value(): |
785 | 0 | case Instructions::i32x4_shr_u.value(): |
786 | 0 | case Instructions::i32x4_add.value(): |
787 | 0 | case Instructions::i32x4_sub.value(): |
788 | 0 | case Instructions::i32x4_mul.value(): |
789 | 0 | case Instructions::i32x4_min_s.value(): |
790 | 0 | case Instructions::i32x4_min_u.value(): |
791 | 0 | case Instructions::i32x4_max_s.value(): |
792 | 0 | case Instructions::i32x4_max_u.value(): |
793 | 0 | case Instructions::i32x4_dot_i16x8_s.value(): |
794 | 0 | case Instructions::i32x4_extmul_low_i16x8_s.value(): |
795 | 0 | case Instructions::i32x4_extmul_high_i16x8_s.value(): |
796 | 0 | case Instructions::i32x4_extmul_low_i16x8_u.value(): |
797 | 0 | case Instructions::i32x4_extmul_high_i16x8_u.value(): |
798 | 0 | case Instructions::i64x2_abs.value(): |
799 | 0 | case Instructions::i64x2_neg.value(): |
800 | 0 | case Instructions::i64x2_all_true.value(): |
801 | 0 | case Instructions::i64x2_bitmask.value(): |
802 | 0 | case Instructions::i64x2_extend_low_i32x4_s.value(): |
803 | 0 | case Instructions::i64x2_extend_high_i32x4_s.value(): |
804 | 0 | case Instructions::i64x2_extend_low_i32x4_u.value(): |
805 | 0 | case Instructions::i64x2_extend_high_i32x4_u.value(): |
806 | 0 | case Instructions::i64x2_shl.value(): |
807 | 0 | case Instructions::i64x2_shr_s.value(): |
808 | 0 | case Instructions::i64x2_shr_u.value(): |
809 | 0 | case Instructions::i64x2_add.value(): |
810 | 0 | case Instructions::i64x2_sub.value(): |
811 | 0 | case Instructions::i64x2_mul.value(): |
812 | 0 | case Instructions::i64x2_eq.value(): |
813 | 0 | case Instructions::i64x2_ne.value(): |
814 | 0 | case Instructions::i64x2_lt_s.value(): |
815 | 0 | case Instructions::i64x2_gt_s.value(): |
816 | 0 | case Instructions::i64x2_le_s.value(): |
817 | 0 | case Instructions::i64x2_ge_s.value(): |
818 | 0 | case Instructions::i64x2_extmul_low_i32x4_s.value(): |
819 | 0 | case Instructions::i64x2_extmul_high_i32x4_s.value(): |
820 | 0 | case Instructions::i64x2_extmul_low_i32x4_u.value(): |
821 | 0 | case Instructions::i64x2_extmul_high_i32x4_u.value(): |
822 | 0 | case Instructions::f32x4_abs.value(): |
823 | 0 | case Instructions::f32x4_neg.value(): |
824 | 0 | case Instructions::f32x4_sqrt.value(): |
825 | 0 | case Instructions::f32x4_add.value(): |
826 | 0 | case Instructions::f32x4_sub.value(): |
827 | 0 | case Instructions::f32x4_mul.value(): |
828 | 0 | case Instructions::f32x4_div.value(): |
829 | 0 | case Instructions::f32x4_min.value(): |
830 | 0 | case Instructions::f32x4_max.value(): |
831 | 0 | case Instructions::f32x4_pmin.value(): |
832 | 0 | case Instructions::f32x4_pmax.value(): |
833 | 0 | case Instructions::f64x2_abs.value(): |
834 | 0 | case Instructions::f64x2_neg.value(): |
835 | 0 | case Instructions::f64x2_sqrt.value(): |
836 | 0 | case Instructions::f64x2_add.value(): |
837 | 0 | case Instructions::f64x2_sub.value(): |
838 | 0 | case Instructions::f64x2_mul.value(): |
839 | 0 | case Instructions::f64x2_div.value(): |
840 | 0 | case Instructions::f64x2_min.value(): |
841 | 0 | case Instructions::f64x2_max.value(): |
842 | 0 | case Instructions::f64x2_pmin.value(): |
843 | 0 | case Instructions::f64x2_pmax.value(): |
844 | 0 | case Instructions::i32x4_trunc_sat_f32x4_s.value(): |
845 | 0 | case Instructions::i32x4_trunc_sat_f32x4_u.value(): |
846 | 0 | case Instructions::f32x4_convert_i32x4_s.value(): |
847 | 0 | case Instructions::f32x4_convert_i32x4_u.value(): |
848 | 0 | case Instructions::i32x4_trunc_sat_f64x2_s_zero.value(): |
849 | 0 | case Instructions::i32x4_trunc_sat_f64x2_u_zero.value(): |
850 | 0 | case Instructions::f64x2_convert_low_i32x4_s.value(): |
851 | 0 | case Instructions::f64x2_convert_low_i32x4_u.value(): |
852 | | // op |
853 | 0 | return Instruction { full_opcode }; |
854 | 0 | default: |
855 | 0 | return ParseError::UnknownInstruction; |
856 | 0 | } |
857 | 0 | } |
858 | 0 | } |
859 | 0 | return ParseError::UnknownInstruction; |
860 | 0 | } |
861 | | |
862 | | ParseResult<CustomSection> CustomSection::parse(Stream& stream) |
863 | 0 | { |
864 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("CustomSection"sv); |
865 | 0 | auto name = TRY(parse_name(stream)); |
866 | |
|
867 | 0 | ByteBuffer data_buffer; |
868 | 0 | if (data_buffer.try_resize(64).is_error()) |
869 | 0 | return ParseError::OutOfMemory; |
870 | | |
871 | 0 | while (!stream.is_eof()) { |
872 | 0 | char buf[16]; |
873 | 0 | auto span_or_error = stream.read_some({ buf, 16 }); |
874 | 0 | if (span_or_error.is_error()) |
875 | 0 | break; |
876 | 0 | auto size = span_or_error.release_value().size(); |
877 | 0 | if (size == 0) |
878 | 0 | break; |
879 | 0 | if (data_buffer.try_append(buf, size).is_error()) |
880 | 0 | return ParseError::HugeAllocationRequested; |
881 | 0 | } |
882 | | |
883 | 0 | return CustomSection(name, move(data_buffer)); |
884 | 0 | } |
885 | | |
886 | | ParseResult<TypeSection> TypeSection::parse(Stream& stream) |
887 | 0 | { |
888 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("TypeSection"sv); |
889 | 0 | auto types = TRY(parse_vector<FunctionType>(stream)); |
890 | 0 | return TypeSection { types }; |
891 | 0 | } |
892 | | |
893 | | ParseResult<ImportSection::Import> ImportSection::Import::parse(Stream& stream) |
894 | 0 | { |
895 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Import"sv); |
896 | 0 | auto module = TRY(parse_name(stream)); |
897 | 0 | auto name = TRY(parse_name(stream)); |
898 | 0 | auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
899 | |
|
900 | 0 | switch (tag) { |
901 | 0 | case Constants::extern_function_tag: { |
902 | 0 | auto index = TRY(GenericIndexParser<TypeIndex>::parse(stream)); |
903 | 0 | return Import { module, name, index }; |
904 | 0 | } |
905 | 0 | case Constants::extern_table_tag: |
906 | 0 | return parse_with_type<TableType>(stream, module, name); |
907 | 0 | case Constants::extern_memory_tag: |
908 | 0 | return parse_with_type<MemoryType>(stream, module, name); |
909 | 0 | case Constants::extern_global_tag: |
910 | 0 | return parse_with_type<GlobalType>(stream, module, name); |
911 | 0 | default: |
912 | 0 | return ParseError::InvalidTag; |
913 | 0 | } |
914 | 0 | } |
915 | | |
916 | | ParseResult<ImportSection> ImportSection::parse(Stream& stream) |
917 | 0 | { |
918 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("ImportSection"sv); |
919 | 0 | auto imports = TRY(parse_vector<Import>(stream)); |
920 | 0 | return ImportSection { imports }; |
921 | 0 | } |
922 | | |
923 | | ParseResult<FunctionSection> FunctionSection::parse(Stream& stream) |
924 | 0 | { |
925 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionSection"sv); |
926 | 0 | auto indices = TRY(parse_vector<u32>(stream)); |
927 | |
|
928 | 0 | Vector<TypeIndex> typed_indices; |
929 | 0 | typed_indices.ensure_capacity(indices.size()); |
930 | 0 | for (auto entry : indices) |
931 | 0 | typed_indices.append(entry); |
932 | |
|
933 | 0 | return FunctionSection { move(typed_indices) }; |
934 | 0 | } |
935 | | |
936 | | ParseResult<TableSection::Table> TableSection::Table::parse(Stream& stream) |
937 | 0 | { |
938 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Table"sv); |
939 | 0 | auto type = TRY(TableType::parse(stream)); |
940 | 0 | return Table { type }; |
941 | 0 | } |
942 | | |
943 | | ParseResult<TableSection> TableSection::parse(Stream& stream) |
944 | 0 | { |
945 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableSection"sv); |
946 | 0 | auto tables = TRY(parse_vector<Table>(stream)); |
947 | 0 | return TableSection { tables }; |
948 | 0 | } |
949 | | |
950 | | ParseResult<MemorySection::Memory> MemorySection::Memory::parse(Stream& stream) |
951 | 0 | { |
952 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Memory"sv); |
953 | 0 | auto type = TRY(MemoryType::parse(stream)); |
954 | 0 | return Memory { type }; |
955 | 0 | } |
956 | | |
957 | | ParseResult<MemorySection> MemorySection::parse(Stream& stream) |
958 | 0 | { |
959 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemorySection"sv); |
960 | 0 | auto memories = TRY(parse_vector<Memory>(stream)); |
961 | 0 | return MemorySection { memories }; |
962 | 0 | } |
963 | | |
964 | | ParseResult<Expression> Expression::parse(Stream& stream, Optional<size_t> size_hint) |
965 | 0 | { |
966 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv); |
967 | |
|
968 | 0 | InstructionPointer ip { 0 }; |
969 | 0 | Vector<InstructionPointer> stack; |
970 | 0 | Vector<Instruction> instructions; |
971 | 0 | if (size_hint.has_value()) |
972 | 0 | instructions.ensure_capacity(size_hint.release_value()); |
973 | 0 | while (true) { |
974 | 0 | auto instruction = TRY(Instruction::parse(stream)); |
975 | 0 | switch (instruction.opcode().value()) { |
976 | 0 | case Instructions::block.value(): |
977 | 0 | case Instructions::loop.value(): |
978 | 0 | case Instructions::if_.value(): |
979 | 0 | stack.append(ip); |
980 | 0 | break; |
981 | 0 | case Instructions::structured_end.value(): { |
982 | 0 | if (stack.is_empty()) |
983 | 0 | return Expression { move(instructions) }; |
984 | 0 | auto entry = stack.take_last(); |
985 | 0 | auto& args = instructions[entry.value()].arguments().get<Instruction::StructuredInstructionArgs>(); |
986 | | // Patch the end_ip of the last structured instruction |
987 | 0 | args.end_ip = ip + (args.else_ip.has_value() ? 1 : 0); |
988 | 0 | break; |
989 | 0 | } |
990 | 0 | case Instructions::structured_else.value(): { |
991 | 0 | if (stack.is_empty()) |
992 | 0 | return ParseError::UnknownInstruction; |
993 | 0 | auto entry = stack.last(); |
994 | 0 | auto& args = instructions[entry.value()].arguments().get<Instruction::StructuredInstructionArgs>(); |
995 | 0 | args.else_ip = ip + 1; |
996 | 0 | break; |
997 | 0 | } |
998 | 0 | } |
999 | 0 | instructions.append(move(instruction)); |
1000 | 0 | ++ip; |
1001 | 0 | } |
1002 | | |
1003 | 0 | return Expression { move(instructions) }; |
1004 | 0 | } |
1005 | | |
1006 | | ParseResult<GlobalSection::Global> GlobalSection::Global::parse(Stream& stream) |
1007 | 0 | { |
1008 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Global"sv); |
1009 | 0 | auto type = TRY(GlobalType::parse(stream)); |
1010 | 0 | auto exprs = TRY(Expression::parse(stream)); |
1011 | 0 | return Global { type, exprs }; |
1012 | 0 | } |
1013 | | |
1014 | | ParseResult<GlobalSection> GlobalSection::parse(Stream& stream) |
1015 | 0 | { |
1016 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalSection"sv); |
1017 | 0 | auto result = TRY(parse_vector<Global>(stream)); |
1018 | 0 | return GlobalSection { result }; |
1019 | 0 | } |
1020 | | |
1021 | | ParseResult<ExportSection::Export> ExportSection::Export::parse(Stream& stream) |
1022 | 0 | { |
1023 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Export"sv); |
1024 | 0 | auto name = TRY(parse_name(stream)); |
1025 | 0 | auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag); |
1026 | |
|
1027 | 0 | auto index = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex); |
1028 | |
|
1029 | 0 | switch (tag) { |
1030 | 0 | case Constants::extern_function_tag: |
1031 | 0 | return Export { name, ExportDesc { FunctionIndex { index } } }; |
1032 | 0 | case Constants::extern_table_tag: |
1033 | 0 | return Export { name, ExportDesc { TableIndex { index } } }; |
1034 | 0 | case Constants::extern_memory_tag: |
1035 | 0 | return Export { name, ExportDesc { MemoryIndex { index } } }; |
1036 | 0 | case Constants::extern_global_tag: |
1037 | 0 | return Export { name, ExportDesc { GlobalIndex { index } } }; |
1038 | 0 | default: |
1039 | 0 | return ParseError::InvalidTag; |
1040 | 0 | } |
1041 | 0 | } |
1042 | | |
1043 | | ParseResult<ExportSection> ExportSection::parse(Stream& stream) |
1044 | 0 | { |
1045 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("ExportSection"sv); |
1046 | 0 | auto result = TRY(parse_vector<Export>(stream)); |
1047 | 0 | return ExportSection { result }; |
1048 | 0 | } |
1049 | | |
1050 | | ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(Stream& stream) |
1051 | 0 | { |
1052 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartFunction"sv); |
1053 | 0 | auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream)); |
1054 | 0 | return StartFunction { index }; |
1055 | 0 | } |
1056 | | |
1057 | | ParseResult<StartSection> StartSection::parse(Stream& stream) |
1058 | 0 | { |
1059 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartSection"sv); |
1060 | 0 | auto result = TRY(StartFunction::parse(stream)); |
1061 | 0 | return StartSection { result }; |
1062 | 0 | } |
1063 | | |
1064 | | ParseResult<ElementSection::Element> ElementSection::Element::parse(Stream& stream) |
1065 | 0 | { |
1066 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Element"sv); |
1067 | 0 | auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag); |
1068 | |
|
1069 | 0 | if (tag > 0x07) |
1070 | 0 | return ParseError::InvalidTag; |
1071 | | |
1072 | 0 | auto has_passive = (tag & 0x01) != 0; |
1073 | 0 | auto has_explicit_index = (tag & 0x02) != 0; |
1074 | 0 | auto has_exprs = (tag & 0x04) != 0; |
1075 | |
|
1076 | 0 | Variant<Active, Passive, Declarative> mode = Passive {}; |
1077 | 0 | if (has_passive) { |
1078 | 0 | if (has_explicit_index) { |
1079 | 0 | mode = Declarative {}; |
1080 | 0 | } else { |
1081 | 0 | mode = Passive {}; |
1082 | 0 | } |
1083 | 0 | } else { |
1084 | 0 | TableIndex table_index = 0; |
1085 | 0 | if (has_explicit_index) |
1086 | 0 | table_index = TRY(GenericIndexParser<TableIndex>::parse(stream)); |
1087 | 0 | auto expression = TRY(Expression::parse(stream)); |
1088 | 0 | mode = Active { table_index, expression }; |
1089 | 0 | } |
1090 | |
|
1091 | 0 | auto type = ValueType(ValueType::FunctionReference); |
1092 | 0 | if (has_passive || has_explicit_index) { |
1093 | 0 | if (has_exprs) { |
1094 | 0 | type = TRY(ValueType::parse(stream)); |
1095 | 0 | } else { |
1096 | 0 | auto extern_ = TRY_READ(stream, u8, ParseError::InvalidType); |
1097 | | // Make sure that this is a function, as it's technically only the |
1098 | | // allowed one. |
1099 | 0 | if (extern_ != 0x00) { |
1100 | 0 | return ParseError::InvalidType; |
1101 | 0 | } |
1102 | 0 | type = ValueType(ValueType::FunctionReference); |
1103 | 0 | } |
1104 | 0 | } |
1105 | | |
1106 | 0 | Vector<Expression> items; |
1107 | 0 | if (!has_exprs) { |
1108 | 0 | auto indices = TRY(parse_vector<GenericIndexParser<FunctionIndex>>(stream)); |
1109 | 0 | for (auto& index : indices) { |
1110 | 0 | Vector<Instruction> instructions { Instruction(Instructions::ref_func, index) }; |
1111 | 0 | items.empend(move(instructions)); |
1112 | 0 | } |
1113 | 0 | } else { |
1114 | 0 | items = TRY(parse_vector<Expression>(stream)); |
1115 | 0 | } |
1116 | |
|
1117 | 0 | return Element { type, move(items), move(mode) }; |
1118 | 0 | } |
1119 | | |
1120 | | ParseResult<ElementSection> ElementSection::parse(Stream& stream) |
1121 | 0 | { |
1122 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("ElementSection"sv); |
1123 | 0 | auto result = TRY(parse_vector<Element>(stream)); |
1124 | 0 | return ElementSection { result }; |
1125 | 0 | } |
1126 | | |
1127 | | ParseResult<Locals> Locals::parse(Stream& stream) |
1128 | 0 | { |
1129 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Locals"sv); |
1130 | 0 | auto count = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize); |
1131 | |
|
1132 | 0 | if (count > Constants::max_allowed_function_locals_per_type) |
1133 | 0 | return ParseError::HugeAllocationRequested; |
1134 | | |
1135 | 0 | auto type = TRY(ValueType::parse(stream)); |
1136 | |
|
1137 | 0 | return Locals { count, type }; |
1138 | 0 | } |
1139 | | |
1140 | | ParseResult<CodeSection::Func> CodeSection::Func::parse(Stream& stream, size_t size_hint) |
1141 | 0 | { |
1142 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv); |
1143 | 0 | auto locals = TRY(parse_vector<Locals>(stream)); |
1144 | 0 | auto body = TRY(Expression::parse(stream, size_hint)); |
1145 | 0 | return Func { move(locals), move(body) }; |
1146 | 0 | } |
1147 | | |
1148 | | ParseResult<CodeSection::Code> CodeSection::Code::parse(Stream& stream) |
1149 | 0 | { |
1150 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Code"sv); |
1151 | 0 | auto size = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize); |
1152 | | |
1153 | | // Emprically, if there are `size` bytes to be read, then there's around |
1154 | | // `size / 2` instructions, so we pass that as our size hint. |
1155 | 0 | auto func = TRY(Func::parse(stream, size / 2)); |
1156 | |
|
1157 | 0 | return Code { size, move(func) }; |
1158 | 0 | } |
1159 | | |
1160 | | ParseResult<CodeSection> CodeSection::parse(Stream& stream) |
1161 | 0 | { |
1162 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("CodeSection"sv); |
1163 | 0 | auto result = TRY(parse_vector<Code>(stream)); |
1164 | 0 | return CodeSection { move(result) }; |
1165 | 0 | } |
1166 | | |
1167 | | ParseResult<DataSection::Data> DataSection::Data::parse(Stream& stream) |
1168 | 0 | { |
1169 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Data"sv); |
1170 | 0 | auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag); |
1171 | |
|
1172 | 0 | if (tag > 0x02) |
1173 | 0 | return ParseError::InvalidTag; |
1174 | | |
1175 | 0 | if (tag == 0x00) { |
1176 | 0 | auto expr = TRY(Expression::parse(stream)); |
1177 | 0 | auto init = TRY(parse_vector<u8>(stream)); |
1178 | 0 | return Data { Active { init, { 0 }, expr } }; |
1179 | 0 | } |
1180 | 0 | if (tag == 0x01) { |
1181 | 0 | auto init = TRY(parse_vector<u8>(stream)); |
1182 | 0 | return Data { Passive { init } }; |
1183 | 0 | } |
1184 | 0 | if (tag == 0x02) { |
1185 | 0 | auto index = TRY(GenericIndexParser<MemoryIndex>::parse(stream)); |
1186 | 0 | auto expr = TRY(Expression::parse(stream)); |
1187 | 0 | auto init = TRY(parse_vector<u8>(stream)); |
1188 | 0 | return Data { Active { init, index, expr } }; |
1189 | 0 | } |
1190 | 0 | VERIFY_NOT_REACHED(); |
1191 | 0 | } |
1192 | | |
1193 | | ParseResult<DataSection> DataSection::parse(Stream& stream) |
1194 | 0 | { |
1195 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataSection"sv); |
1196 | 0 | auto data = TRY(parse_vector<Data>(stream)); |
1197 | 0 | return DataSection { data }; |
1198 | 0 | } |
1199 | | |
1200 | | ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] Stream& stream) |
1201 | 0 | { |
1202 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataCountSection"sv); |
1203 | 0 | auto value_or_error = stream.read_value<LEB128<u32>>(); |
1204 | 0 | if (value_or_error.is_error()) { |
1205 | 0 | if (stream.is_eof()) { |
1206 | | // The section simply didn't contain anything. |
1207 | 0 | return DataCountSection { {} }; |
1208 | 0 | } |
1209 | 0 | return ParseError::ExpectedSize; |
1210 | 0 | } |
1211 | 0 | u32 value = value_or_error.release_value(); |
1212 | |
|
1213 | 0 | return DataCountSection { value }; |
1214 | 0 | } |
1215 | | |
1216 | | ParseResult<SectionId> SectionId::parse(Stream& stream) |
1217 | 0 | { |
1218 | 0 | u8 id = TRY_READ(stream, u8, ParseError::ExpectedIndex); |
1219 | 0 | switch (id) { |
1220 | 0 | case 0x00: |
1221 | 0 | return SectionId(SectionIdKind::Custom); |
1222 | 0 | case 0x01: |
1223 | 0 | return SectionId(SectionIdKind::Type); |
1224 | 0 | case 0x02: |
1225 | 0 | return SectionId(SectionIdKind::Import); |
1226 | 0 | case 0x03: |
1227 | 0 | return SectionId(SectionIdKind::Function); |
1228 | 0 | case 0x04: |
1229 | 0 | return SectionId(SectionIdKind::Table); |
1230 | 0 | case 0x05: |
1231 | 0 | return SectionId(SectionIdKind::Memory); |
1232 | 0 | case 0x06: |
1233 | 0 | return SectionId(SectionIdKind::Global); |
1234 | 0 | case 0x07: |
1235 | 0 | return SectionId(SectionIdKind::Export); |
1236 | 0 | case 0x08: |
1237 | 0 | return SectionId(SectionIdKind::Start); |
1238 | 0 | case 0x09: |
1239 | 0 | return SectionId(SectionIdKind::Element); |
1240 | 0 | case 0x0a: |
1241 | 0 | return SectionId(SectionIdKind::Code); |
1242 | 0 | case 0x0b: |
1243 | 0 | return SectionId(SectionIdKind::Data); |
1244 | 0 | case 0x0c: |
1245 | 0 | return SectionId(SectionIdKind::DataCount); |
1246 | 0 | default: |
1247 | 0 | return ParseError::InvalidIndex; |
1248 | 0 | } |
1249 | 0 | } |
1250 | | |
1251 | | ParseResult<NonnullRefPtr<Module>> Module::parse(Stream& stream) |
1252 | 0 | { |
1253 | 0 | ScopeLogger<WASM_BINPARSER_DEBUG> logger("Module"sv); |
1254 | 0 | u8 buf[4]; |
1255 | 0 | if (stream.read_until_filled({ buf, 4 }).is_error()) |
1256 | 0 | return with_eof_check(stream, ParseError::InvalidInput); |
1257 | 0 | if (Bytes { buf, 4 } != wasm_magic.span()) |
1258 | 0 | return with_eof_check(stream, ParseError::InvalidModuleMagic); |
1259 | | |
1260 | 0 | if (stream.read_until_filled({ buf, 4 }).is_error()) |
1261 | 0 | return with_eof_check(stream, ParseError::InvalidInput); |
1262 | 0 | if (Bytes { buf, 4 } != wasm_version.span()) |
1263 | 0 | return with_eof_check(stream, ParseError::InvalidModuleVersion); |
1264 | | |
1265 | 0 | auto last_section_id = SectionId::SectionIdKind::Custom; |
1266 | 0 | auto module_ptr = make_ref_counted<Module>(); |
1267 | 0 | auto& module = *module_ptr; |
1268 | |
|
1269 | 0 | while (!stream.is_eof()) { |
1270 | 0 | auto section_id = TRY(SectionId::parse(stream)); |
1271 | 0 | size_t section_size = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedSize); |
1272 | 0 | auto section_stream = ConstrainedStream { MaybeOwned<Stream>(stream), section_size }; |
1273 | |
|
1274 | 0 | if (section_id.kind() != SectionId::SectionIdKind::Custom && section_id.kind() == last_section_id) |
1275 | 0 | return ParseError::DuplicateSection; |
1276 | | |
1277 | 0 | switch (section_id.kind()) { |
1278 | 0 | case SectionId::SectionIdKind::Custom: |
1279 | 0 | module.custom_sections().append(TRY(CustomSection::parse(section_stream))); |
1280 | 0 | break; |
1281 | 0 | case SectionId::SectionIdKind::Type: |
1282 | 0 | module.type_section() = TRY(TypeSection::parse(section_stream)); |
1283 | 0 | break; |
1284 | 0 | case SectionId::SectionIdKind::Import: |
1285 | 0 | module.import_section() = TRY(ImportSection::parse(section_stream)); |
1286 | 0 | break; |
1287 | 0 | case SectionId::SectionIdKind::Function: |
1288 | 0 | module.function_section() = TRY(FunctionSection::parse(section_stream)); |
1289 | 0 | break; |
1290 | 0 | case SectionId::SectionIdKind::Table: |
1291 | 0 | module.table_section() = TRY(TableSection::parse(section_stream)); |
1292 | 0 | break; |
1293 | 0 | case SectionId::SectionIdKind::Memory: |
1294 | 0 | module.memory_section() = TRY(MemorySection::parse(section_stream)); |
1295 | 0 | break; |
1296 | 0 | case SectionId::SectionIdKind::Global: |
1297 | 0 | module.global_section() = TRY(GlobalSection::parse(section_stream)); |
1298 | 0 | break; |
1299 | 0 | case SectionId::SectionIdKind::Export: |
1300 | 0 | module.export_section() = TRY(ExportSection::parse(section_stream)); |
1301 | 0 | break; |
1302 | 0 | case SectionId::SectionIdKind::Start: |
1303 | 0 | module.start_section() = TRY(StartSection::parse(section_stream)); |
1304 | 0 | break; |
1305 | 0 | case SectionId::SectionIdKind::Element: |
1306 | 0 | module.element_section() = TRY(ElementSection::parse(section_stream)); |
1307 | 0 | break; |
1308 | 0 | case SectionId::SectionIdKind::Code: |
1309 | 0 | module.code_section() = TRY(CodeSection::parse(section_stream)); |
1310 | 0 | break; |
1311 | 0 | case SectionId::SectionIdKind::Data: |
1312 | 0 | module.data_section() = TRY(DataSection::parse(section_stream)); |
1313 | 0 | break; |
1314 | 0 | case SectionId::SectionIdKind::DataCount: |
1315 | 0 | module.data_count_section() = TRY(DataCountSection::parse(section_stream)); |
1316 | 0 | break; |
1317 | 0 | default: |
1318 | 0 | return ParseError::InvalidIndex; |
1319 | 0 | } |
1320 | 0 | if (section_id.kind() != SectionId::SectionIdKind::Custom) { |
1321 | 0 | if (section_id.kind() < last_section_id) |
1322 | 0 | return ParseError::SectionOutOfOrder; |
1323 | 0 | last_section_id = section_id.kind(); |
1324 | 0 | } |
1325 | 0 | if (section_stream.remaining() != 0) |
1326 | 0 | return ParseError::SectionSizeMismatch; |
1327 | 0 | } |
1328 | | |
1329 | 0 | return module_ptr; |
1330 | 0 | } |
1331 | | |
1332 | | ByteString parse_error_to_byte_string(ParseError error) |
1333 | 0 | { |
1334 | 0 | switch (error) { |
1335 | 0 | case ParseError::UnexpectedEof: |
1336 | 0 | return "Unexpected end-of-file"; |
1337 | 0 | case ParseError::ExpectedIndex: |
1338 | 0 | return "Expected a valid index value"; |
1339 | 0 | case ParseError::ExpectedKindTag: |
1340 | 0 | return "Expected a valid kind tag"; |
1341 | 0 | case ParseError::ExpectedSize: |
1342 | 0 | return "Expected a valid LEB128-encoded size"; |
1343 | 0 | case ParseError::ExpectedValueOrTerminator: |
1344 | 0 | return "Expected either a terminator or a value"; |
1345 | 0 | case ParseError::InvalidIndex: |
1346 | 0 | return "An index parsed was semantically invalid"; |
1347 | 0 | case ParseError::InvalidInput: |
1348 | 0 | return "Input data contained invalid bytes"; |
1349 | 0 | case ParseError::InvalidModuleMagic: |
1350 | 0 | return "Incorrect module magic (did not match \\0asm)"; |
1351 | 0 | case ParseError::InvalidModuleVersion: |
1352 | 0 | return "Incorrect module version"; |
1353 | 0 | case ParseError::InvalidSize: |
1354 | 0 | return "A parsed size did not make sense in context"; |
1355 | 0 | case ParseError::InvalidTag: |
1356 | 0 | return "A parsed tag did not make sense in context"; |
1357 | 0 | case ParseError::InvalidType: |
1358 | 0 | return "A parsed type did not make sense in context"; |
1359 | 0 | case ParseError::HugeAllocationRequested: |
1360 | 0 | return "Parsing caused an attempt to allocate a very big chunk of memory, likely malformed data"; |
1361 | 0 | case ParseError::OutOfMemory: |
1362 | 0 | return "The parser hit an OOM condition"; |
1363 | 0 | case ParseError::ExpectedFloatingImmediate: |
1364 | 0 | return "Expected a floating point immediate"; |
1365 | 0 | case ParseError::ExpectedSignedImmediate: |
1366 | 0 | return "Expected a signed integer immediate"; |
1367 | 0 | case ParseError::InvalidImmediate: |
1368 | 0 | return "A parsed instruction immediate was invalid for the instruction it was used for"; |
1369 | 0 | case ParseError::SectionSizeMismatch: |
1370 | 0 | return "A parsed section did not fulfill its expected size"; |
1371 | 0 | case ParseError::InvalidUtf8: |
1372 | 0 | return "A parsed string was not valid UTF-8"; |
1373 | 0 | case ParseError::UnknownInstruction: |
1374 | 0 | return "A parsed instruction was not known to this parser"; |
1375 | 0 | case ParseError::DuplicateSection: |
1376 | 0 | return "Two sections of the same type were encountered"; |
1377 | 0 | case ParseError::SectionOutOfOrder: |
1378 | 0 | return "A section encountered was not in the correct ordering"; |
1379 | 0 | } |
1380 | 0 | return "Unknown error"; |
1381 | 0 | } |
1382 | | } |