1/*
2 * Copyright (C) 2016-2017 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#pragma once
27
28#if ENABLE(WEBASSEMBLY)
29
30#include "B3Compilation.h"
31#include "B3Procedure.h"
32#include "WasmFormat.h"
33#include "WasmLimits.h"
34#include "WasmModuleInformation.h"
35#include "WasmOps.h"
36#include "WasmSections.h"
37#include <type_traits>
38#include <wtf/Expected.h>
39#include <wtf/LEBDecoder.h>
40#include <wtf/StdLibExtras.h>
41#include <wtf/text/WTFString.h>
42#include <wtf/unicode/UTF8Conversion.h>
43
44namespace JSC { namespace Wasm {
45
46namespace FailureHelper {
47// FIXME We should move this to makeString. It's in its own namespace to enable C++ Argument Dependent Lookup à la std::swap: user code can deblare its own "boxFailure" and the fail() helper will find it.
48static inline auto makeString(const char *failure) { return failure; }
49template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type>
50static inline auto makeString(Int failure) { return String::number(failure); }
51}
52
53template<typename SuccessType>
54class Parser {
55public:
56 typedef String ErrorType;
57 typedef Unexpected<ErrorType> UnexpectedResult;
58 typedef Expected<void, ErrorType> PartialResult;
59 typedef Expected<SuccessType, ErrorType> Result;
60
61 const uint8_t* source() const { return m_source; }
62 size_t length() const { return m_sourceLength; }
63 size_t offset() const { return m_offset; }
64
65protected:
66 Parser(const uint8_t*, size_t);
67
68 bool WARN_UNUSED_RETURN consumeCharacter(char);
69 bool WARN_UNUSED_RETURN consumeString(const char*);
70 bool WARN_UNUSED_RETURN consumeUTF8String(Name&, size_t);
71
72 bool WARN_UNUSED_RETURN parseVarUInt1(uint8_t&);
73 bool WARN_UNUSED_RETURN parseInt7(int8_t&);
74 bool WARN_UNUSED_RETURN parseUInt7(uint8_t&);
75 bool WARN_UNUSED_RETURN parseUInt8(uint8_t&);
76 bool WARN_UNUSED_RETURN parseUInt32(uint32_t&);
77 bool WARN_UNUSED_RETURN parseUInt64(uint64_t&);
78 bool WARN_UNUSED_RETURN parseVarUInt32(uint32_t&);
79 bool WARN_UNUSED_RETURN parseVarUInt64(uint64_t&);
80
81 bool WARN_UNUSED_RETURN parseVarInt32(int32_t&);
82 bool WARN_UNUSED_RETURN parseVarInt64(int64_t&);
83
84 bool WARN_UNUSED_RETURN parseResultType(Type&);
85 bool WARN_UNUSED_RETURN parseValueType(Type&);
86 bool WARN_UNUSED_RETURN parseExternalKind(ExternalKind&);
87
88 size_t m_offset = 0;
89
90 template <typename ...Args>
91 NEVER_INLINE UnexpectedResult WARN_UNUSED_RETURN fail(Args... args) const
92 {
93 using namespace FailureHelper; // See ADL comment in namespace above.
94 return UnexpectedResult(makeString("WebAssembly.Module doesn't parse at byte "_s, String::number(m_offset), ": "_s, makeString(args)...));
95 }
96#define WASM_PARSER_FAIL_IF(condition, ...) do { \
97 if (UNLIKELY(condition)) \
98 return fail(__VA_ARGS__); \
99 } while (0)
100
101#define WASM_FAIL_IF_HELPER_FAILS(helper) do { \
102 auto helperResult = helper; \
103 if (UNLIKELY(!helperResult)) \
104 return makeUnexpected(WTFMove(helperResult.error())); \
105 } while (0)
106
107private:
108 const uint8_t* m_source;
109 size_t m_sourceLength;
110};
111
112template<typename SuccessType>
113ALWAYS_INLINE Parser<SuccessType>::Parser(const uint8_t* sourceBuffer, size_t sourceLength)
114 : m_source(sourceBuffer)
115 , m_sourceLength(sourceLength)
116{
117}
118
119template<typename SuccessType>
120ALWAYS_INLINE bool Parser<SuccessType>::consumeCharacter(char c)
121{
122 if (m_offset >= length())
123 return false;
124 if (c == source()[m_offset]) {
125 m_offset++;
126 return true;
127 }
128 return false;
129}
130
131template<typename SuccessType>
132ALWAYS_INLINE bool Parser<SuccessType>::consumeString(const char* str)
133{
134 unsigned start = m_offset;
135 if (m_offset >= length())
136 return false;
137 for (size_t i = 0; str[i]; i++) {
138 if (!consumeCharacter(str[i])) {
139 m_offset = start;
140 return false;
141 }
142 }
143 return true;
144}
145
146template<typename SuccessType>
147ALWAYS_INLINE bool Parser<SuccessType>::consumeUTF8String(Name& result, size_t stringLength)
148{
149 if (length() < stringLength || m_offset > length() - stringLength)
150 return false;
151 if (stringLength > maxStringSize)
152 return false;
153 if (!result.tryReserveCapacity(stringLength))
154 return false;
155
156 const uint8_t* stringStart = source() + m_offset;
157
158 // We don't cache the UTF-16 characters since it seems likely the string is ASCII.
159 if (UNLIKELY(!charactersAreAllASCII(stringStart, stringLength))) {
160 Vector<UChar, 1024> buffer(stringLength);
161 UChar* bufferStart = buffer.data();
162
163 UChar* bufferCurrent = bufferStart;
164 const char* stringCurrent = reinterpret_cast<const char*>(stringStart);
165 if (!WTF::Unicode::convertUTF8ToUTF16(stringCurrent, reinterpret_cast<const char *>(stringStart + stringLength), &bufferCurrent, bufferCurrent + buffer.size()))
166 return false;
167 }
168
169 result.grow(stringLength);
170 memcpy(result.data(), stringStart, stringLength);
171 m_offset += stringLength;
172 return true;
173}
174
175template<typename SuccessType>
176ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt32(uint32_t& result)
177{
178 return WTF::LEBDecoder::decodeUInt32(m_source, m_sourceLength, m_offset, result);
179}
180
181template<typename SuccessType>
182ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt64(uint64_t& result)
183{
184 return WTF::LEBDecoder::decodeUInt64(m_source, m_sourceLength, m_offset, result);
185}
186
187template<typename SuccessType>
188ALWAYS_INLINE bool Parser<SuccessType>::parseVarInt32(int32_t& result)
189{
190 return WTF::LEBDecoder::decodeInt32(m_source, m_sourceLength, m_offset, result);
191}
192
193template<typename SuccessType>
194ALWAYS_INLINE bool Parser<SuccessType>::parseVarInt64(int64_t& result)
195{
196 return WTF::LEBDecoder::decodeInt64(m_source, m_sourceLength, m_offset, result);
197}
198
199template<typename SuccessType>
200ALWAYS_INLINE bool Parser<SuccessType>::parseUInt32(uint32_t& result)
201{
202 if (length() < 4 || m_offset > length() - 4)
203 return false;
204 result = *reinterpret_cast<const uint32_t*>(source() + m_offset);
205 m_offset += 4;
206 return true;
207}
208
209template<typename SuccessType>
210ALWAYS_INLINE bool Parser<SuccessType>::parseUInt64(uint64_t& result)
211{
212 if (length() < 8 || m_offset > length() - 8)
213 return false;
214 result = *reinterpret_cast<const uint64_t*>(source() + m_offset);
215 m_offset += 8;
216 return true;
217}
218
219template<typename SuccessType>
220ALWAYS_INLINE bool Parser<SuccessType>::parseUInt8(uint8_t& result)
221{
222 if (m_offset >= length())
223 return false;
224 result = source()[m_offset++];
225 return true;
226}
227
228template<typename SuccessType>
229ALWAYS_INLINE bool Parser<SuccessType>::parseInt7(int8_t& result)
230{
231 if (m_offset >= length())
232 return false;
233 uint8_t v = source()[m_offset++];
234 result = (v & 0x40) ? WTF::bitwise_cast<int8_t>(uint8_t(v | 0x80)) : v;
235 return (v & 0x80) == 0;
236}
237
238template<typename SuccessType>
239ALWAYS_INLINE bool Parser<SuccessType>::parseUInt7(uint8_t& result)
240{
241 if (m_offset >= length())
242 return false;
243 result = source()[m_offset++];
244 return result < 0x80;
245}
246
247template<typename SuccessType>
248ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt1(uint8_t& result)
249{
250 uint32_t temp;
251 if (!parseVarUInt32(temp))
252 return false;
253 if (temp > 1)
254 return false;
255 result = static_cast<uint8_t>(temp);
256 return true;
257}
258
259template<typename SuccessType>
260ALWAYS_INLINE bool Parser<SuccessType>::parseResultType(Type& result)
261{
262 int8_t value;
263 if (!parseInt7(value))
264 return false;
265 if (!isValidType(value))
266 return false;
267 result = static_cast<Type>(value);
268 return true;
269}
270
271template<typename SuccessType>
272ALWAYS_INLINE bool Parser<SuccessType>::parseValueType(Type& result)
273{
274 return parseResultType(result) && isValueType(result);
275}
276
277template<typename SuccessType>
278ALWAYS_INLINE bool Parser<SuccessType>::parseExternalKind(ExternalKind& result)
279{
280 uint8_t value;
281 if (!parseUInt7(value))
282 return false;
283 if (!isValidExternalKind(value))
284 return false;
285 result = static_cast<ExternalKind>(value);
286 return true;
287}
288
289ALWAYS_INLINE I32InitExpr makeI32InitExpr(uint8_t opcode, uint32_t bits)
290{
291 RELEASE_ASSERT(opcode == I32Const || opcode == GetGlobal);
292 if (opcode == I32Const)
293 return I32InitExpr::constValue(bits);
294 return I32InitExpr::globalImport(bits);
295}
296
297} } // namespace JSC::Wasm
298
299#endif // ENABLE(WEBASSEMBLY)
300