1 | /* |
2 | * Copyright (C) 2018 Yusuke Suzuki <[email protected]>. |
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 | #pragma once |
27 | |
28 | #if ENABLE(WEBASSEMBLY) |
29 | |
30 | #include "WasmModuleInformation.h" |
31 | #include "WasmParser.h" |
32 | #include "WasmSections.h" |
33 | #include <wtf/CrossThreadCopier.h> |
34 | #include <wtf/SHA1.h> |
35 | #include <wtf/Vector.h> |
36 | #include <wtf/text/WTFString.h> |
37 | |
38 | namespace JSC { namespace Wasm { |
39 | |
40 | class StreamingParserClient { |
41 | public: |
42 | virtual ~StreamingParserClient() = default; |
43 | virtual bool didReceiveSectionData(Section) { return true; }; |
44 | virtual bool didReceiveFunctionData(unsigned, const FunctionData&) = 0; |
45 | virtual void didFinishParsing() { } |
46 | }; |
47 | |
48 | class StreamingParser { |
49 | WTF_MAKE_FAST_ALLOCATED; |
50 | public: |
51 | // The layout of the Wasm module is the following. |
52 | // |
53 | // Module: [ Header ][ Section ]* |
54 | // Section: [ ID ][ SizeOfPayload ][ Payload ] |
55 | // Code Section: [ ID ][ SizeOfPayload ][ Payload of Code Section ] |
56 | // [ NumberOfFunctions ]([ SizeOfFunction ][ PayloadOfFunction ])* |
57 | // |
58 | // Basically we can parse Wasm sections by repeatedly (1) reading the size of the payload, and (2) reading the payload based on the size read in (1). |
59 | // So this streaming parser handles Section as the unit for incremental parsing. The exception is the Code section. The Code section is large since it |
60 | // includes all the functions in the wasm module. Since we would like to compile each function and parse the Code section concurrently, the streaming |
61 | // parser specially handles the Code section. In the Code section, the streaming parser uses Function as the unit for incremental parsing. |
62 | enum class State : uint8_t { |
63 | , |
64 | SectionID, |
65 | SectionSize, |
66 | SectionPayload, |
67 | CodeSectionSize, |
68 | FunctionSize, |
69 | FunctionPayload, |
70 | Finished, |
71 | FatalError, |
72 | }; |
73 | |
74 | enum class IsEndOfStream { Yes, No }; |
75 | |
76 | StreamingParser(ModuleInformation&, StreamingParserClient&); |
77 | |
78 | State addBytes(const uint8_t* bytes, size_t length) { return addBytes(bytes, length, IsEndOfStream::No); } |
79 | State finalize(); |
80 | |
81 | String errorMessage() const { return crossThreadCopy(m_errorMessage); } |
82 | |
83 | void reportError() { moveToStateIfNotFailed(failOnState(State::FatalError)); } |
84 | |
85 | private: |
86 | static constexpr unsigned = 8; |
87 | static constexpr unsigned sectionIDSize = 1; |
88 | |
89 | State addBytes(const uint8_t* bytes, size_t length, IsEndOfStream); |
90 | |
91 | State (Vector<uint8_t>&&); |
92 | State parseSectionID(Vector<uint8_t>&&); |
93 | State parseSectionSize(uint32_t); |
94 | State parseSectionPayload(Vector<uint8_t>&&); |
95 | |
96 | State parseCodeSectionSize(uint32_t); |
97 | State parseFunctionSize(uint32_t); |
98 | State parseFunctionPayload(Vector<uint8_t>&&); |
99 | |
100 | Optional<Vector<uint8_t>> consume(const uint8_t* bytes, size_t, size_t&, size_t); |
101 | Expected<uint32_t, State> consumeVarUInt32(const uint8_t* bytes, size_t, size_t&, IsEndOfStream); |
102 | |
103 | void moveToStateIfNotFailed(State); |
104 | template <typename ...Args> NEVER_INLINE State WARN_UNUSED_RETURN fail(Args...); |
105 | |
106 | State failOnState(State); |
107 | |
108 | Ref<ModuleInformation> m_info; |
109 | StreamingParserClient& m_client; |
110 | Vector<uint8_t> m_remaining; |
111 | String m_errorMessage; |
112 | |
113 | CheckedSize m_totalSize { 0 }; |
114 | size_t m_offset { 0 }; |
115 | size_t m_nextOffset { 0 }; |
116 | size_t m_codeOffset { 0 }; |
117 | |
118 | SHA1 m_hasher; |
119 | |
120 | uint32_t m_sectionLength { 0 }; |
121 | |
122 | uint32_t m_functionCount { 0 }; |
123 | uint32_t m_functionIndex { 0 }; |
124 | |
125 | uint32_t m_functionSize { 0 }; |
126 | |
127 | Lock m_stateLock; |
128 | State m_state { State::ModuleHeader }; |
129 | Section m_section { Section::Begin }; |
130 | Section m_previousKnownSection { Section::Begin }; |
131 | }; |
132 | |
133 | |
134 | } } // namespace JSC::Wasm |
135 | |
136 | #endif // ENABLE(WEBASSEMBLY) |
137 | |