1 | /* |
2 | * Copyright (C) 2016-2018 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "WasmModuleParser.h" |
28 | |
29 | #if ENABLE(WEBASSEMBLY) |
30 | |
31 | #include "IdentifierInlines.h" |
32 | #include "WasmMemoryInformation.h" |
33 | #include "WasmNameSectionParser.h" |
34 | #include "WasmOps.h" |
35 | #include "WasmSectionParser.h" |
36 | #include "WasmSections.h" |
37 | #include <wtf/SHA1.h> |
38 | |
39 | namespace JSC { namespace Wasm { |
40 | |
41 | auto ModuleParser::parse() -> Result |
42 | { |
43 | const size_t minSize = 8; |
44 | uint32_t versionNumber; |
45 | |
46 | WASM_PARSER_FAIL_IF(length() < minSize, "expected a module of at least " , minSize, " bytes" ); |
47 | WASM_PARSER_FAIL_IF(length() > maxModuleSize, "module size " , length(), " is too large, maximum " , maxModuleSize); |
48 | WASM_PARSER_FAIL_IF(!consumeCharacter(0) || !consumeString("asm" ), "modules doesn't start with '\\0asm'" ); |
49 | WASM_PARSER_FAIL_IF(!parseUInt32(versionNumber), "can't parse version number" ); |
50 | WASM_PARSER_FAIL_IF(versionNumber != expectedVersionNumber, "unexpected version number " , versionNumber, " expected " , expectedVersionNumber); |
51 | |
52 | // This is not really a known section. |
53 | Section previousKnownSection = Section::Begin; |
54 | while (m_offset < length()) { |
55 | uint8_t sectionByte; |
56 | |
57 | WASM_PARSER_FAIL_IF(!parseUInt7(sectionByte), "can't get section byte" ); |
58 | |
59 | Section section = Section::Custom; |
60 | WASM_PARSER_FAIL_IF(!decodeSection(sectionByte, section)); |
61 | ASSERT(section != Section::Begin); |
62 | |
63 | uint32_t sectionLength; |
64 | WASM_PARSER_FAIL_IF(!validateOrder(previousKnownSection, section), "invalid section order, " , previousKnownSection, " followed by " , section); |
65 | WASM_PARSER_FAIL_IF(!parseVarUInt32(sectionLength), "can't get " , section, " section's length" ); |
66 | WASM_PARSER_FAIL_IF(sectionLength > length() - m_offset, section, " section of size " , sectionLength, " would overflow Module's size" ); |
67 | |
68 | SectionParser parser(source() + m_offset, sectionLength, m_offset, m_info.get()); |
69 | |
70 | switch (section) { |
71 | #define WASM_SECTION_PARSE(NAME, ID, DESCRIPTION) \ |
72 | case Section::NAME: { \ |
73 | WASM_FAIL_IF_HELPER_FAILS(parser.parse ## NAME()); \ |
74 | break; \ |
75 | } |
76 | FOR_EACH_KNOWN_WASM_SECTION(WASM_SECTION_PARSE) |
77 | #undef WASM_SECTION_PARSE |
78 | |
79 | case Section::Custom: { |
80 | WASM_FAIL_IF_HELPER_FAILS(parser.parseCustom()); |
81 | break; |
82 | } |
83 | |
84 | case Section::Begin: { |
85 | RELEASE_ASSERT_NOT_REACHED(); |
86 | break; |
87 | } |
88 | } |
89 | |
90 | WASM_PARSER_FAIL_IF(parser.length() != parser.offset(), "parsing ended before the end of " , section, " section" ); |
91 | |
92 | m_offset += sectionLength; |
93 | |
94 | |
95 | if (isKnownSection(section)) |
96 | previousKnownSection = section; |
97 | } |
98 | |
99 | if (UNLIKELY(Options::useEagerWebAssemblyModuleHashing())) { |
100 | SHA1 hasher; |
101 | hasher.addBytes(source(), length()); |
102 | m_info->nameSection->setHash(hasher.computeHexDigest()); |
103 | } |
104 | |
105 | return { }; |
106 | } |
107 | |
108 | } } // namespace JSC::Wasm |
109 | |
110 | #endif // ENABLE(WEBASSEMBLY) |
111 | |