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/SHA1.h>
34#include <wtf/Vector.h>
35#include <wtf/text/WTFString.h>
36
37namespace JSC { namespace Wasm {
38
39class StreamingParserClient {
40};
41
42class StreamingParser {
43 WTF_MAKE_FAST_ALLOCATED;
44public:
45 // The layout of the Wasm module is the following.
46 //
47 // Module: [ Header ][ Section ]*
48 // Section: [ ID ][ SizeOfPayload ][ Payload ]
49 // Code Section: [ ID ][ SizeOfPayload ][ Payload of Code Section ]
50 // [ NumberOfFunctions ]([ SizeOfFunction ][ PayloadOfFunction ])*
51 //
52 // 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).
53 // 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
54 // includes all the functions in the wasm module. Since we would like to compile each function and parse the Code section concurrently, the streaming
55 // parser specially handles the Code section. In the Code section, the streaming parser uses Function as the unit for incremental parsing.
56 enum class State : uint8_t {
57 ModuleHeader,
58 SectionID,
59 SectionSize,
60 SectionPayload,
61 CodeSectionSize,
62 FunctionSize,
63 FunctionPayload,
64 Finished,
65 FatalError,
66 };
67
68 enum class IsEndOfStream { Yes, No };
69
70 StreamingParser(ModuleInformation&);
71
72 State addBytes(const uint8_t* bytes, size_t length) { return addBytes(bytes, length, IsEndOfStream::No); }
73 State finalize();
74
75 const String& errorMessage() const { return m_errorMessage; }
76
77private:
78 static constexpr unsigned moduleHeaderSize = 8;
79 static constexpr unsigned sectionIDSize = 1;
80
81 State addBytes(const uint8_t* bytes, size_t length, IsEndOfStream);
82
83 State parseModuleHeader(Vector<uint8_t>&&);
84 State parseSectionID(Vector<uint8_t>&&);
85 State parseSectionSize(uint32_t);
86 State parseSectionPayload(Vector<uint8_t>&&);
87
88 State parseCodeSectionSize(uint32_t);
89 State parseFunctionSize(uint32_t);
90 State parseFunctionPayload(Vector<uint8_t>&&);
91
92 Optional<Vector<uint8_t>> consume(const uint8_t* bytes, size_t, size_t&, size_t);
93 Expected<uint32_t, State> consumeVarUInt32(const uint8_t* bytes, size_t, size_t&, IsEndOfStream);
94
95 template <typename ...Args> NEVER_INLINE State WARN_UNUSED_RETURN fail(Args...);
96
97 State failOnState(State);
98
99 Ref<ModuleInformation> m_info;
100 Vector<uint8_t> m_remaining;
101 String m_errorMessage;
102
103 CheckedSize m_totalSize { 0 };
104 size_t m_offset { 0 };
105 size_t m_nextOffset { 0 };
106 size_t m_codeOffset { 0 };
107
108 SHA1 m_hasher;
109
110 uint32_t m_sectionLength { 0 };
111
112 uint32_t m_functionCount { 0 };
113 uint32_t m_functionIndex { 0 };
114
115 uint32_t m_functionSize { 0 };
116
117 State m_state { State::ModuleHeader };
118 Section m_section { Section::Begin };
119 Section m_previousKnownSection { Section::Begin };
120};
121
122
123} } // namespace JSC::Wasm
124
125#endif // ENABLE(WEBASSEMBLY)
126