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.
48template<typename T>
49inline String makeString(const T& failure) { return WTF::toString(failure); }
50}
51
52template<typename SuccessType>
53class Parser {
54public:
55 typedef String ErrorType;
56 typedef Unexpected<ErrorType> UnexpectedResult;
57 typedef Expected<void, ErrorType> PartialResult;
58 typedef Expected<SuccessType, ErrorType> Result;
59
60 const uint8_t* source() const { return m_source; }
61 size_t length() const { return m_sourceLength; }
62 size_t offset() const { return m_offset; }
63
64protected:
65 Parser(const uint8_t*, size_t);
66
67 bool WARN_UNUSED_RETURN consumeCharacter(char);
68 bool WARN_UNUSED_RETURN consumeString(const char*);
69 bool WARN_UNUSED_RETURN consumeUTF8String(Name&, size_t);
70
71 bool WARN_UNUSED_RETURN parseVarUInt1(uint8_t&);
72 bool WARN_UNUSED_RETURN parseInt7(int8_t&);
73 bool WARN_UNUSED_RETURN peekInt7(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 PartialResult WARN_UNUSED_RETURN parseBlockSignature(const ModuleInformation&, BlockSignature&);
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 // We keep a local reference to the global table so we don't have to fetch it to find thunk signatures.
111 const SignatureInformation& m_signatureInformation;
112};
113
114template<typename SuccessType>
115ALWAYS_INLINE Parser<SuccessType>::Parser(const uint8_t* sourceBuffer, size_t sourceLength)
116 : m_source(sourceBuffer)
117 , m_sourceLength(sourceLength)
118 , m_signatureInformation(SignatureInformation::singleton())
119{
120}
121
122template<typename SuccessType>
123ALWAYS_INLINE bool Parser<SuccessType>::consumeCharacter(char c)
124{
125 if (m_offset >= length())
126 return false;
127 if (c == source()[m_offset]) {
128 m_offset++;
129 return true;
130 }
131 return false;
132}
133
134template<typename SuccessType>
135ALWAYS_INLINE bool Parser<SuccessType>::consumeString(const char* str)
136{
137 unsigned start = m_offset;
138 if (m_offset >= length())
139 return false;
140 for (size_t i = 0; str[i]; i++) {
141 if (!consumeCharacter(str[i])) {
142 m_offset = start;
143 return false;
144 }
145 }
146 return true;
147}
148
149template<typename SuccessType>
150ALWAYS_INLINE bool Parser<SuccessType>::consumeUTF8String(Name& result, size_t stringLength)
151{
152 if (length() < stringLength || m_offset > length() - stringLength)
153 return false;
154 if (stringLength > maxStringSize)
155 return false;
156 if (!result.tryReserveCapacity(stringLength))
157 return false;
158
159 const uint8_t* stringStart = source() + m_offset;
160
161 // We don't cache the UTF-16 characters since it seems likely the string is ASCII.
162 if (UNLIKELY(!charactersAreAllASCII(stringStart, stringLength))) {
163 Vector<UChar, 1024> buffer(stringLength);
164 UChar* bufferStart = buffer.data();
165
166 UChar* bufferCurrent = bufferStart;
167 const char* stringCurrent = reinterpret_cast<const char*>(stringStart);
168 if (!WTF::Unicode::convertUTF8ToUTF16(stringCurrent, reinterpret_cast<const char *>(stringStart + stringLength), &bufferCurrent, bufferCurrent + buffer.size()))
169 return false;
170 }
171
172 result.grow(stringLength);
173 memcpy(result.data(), stringStart, stringLength);
174 m_offset += stringLength;
175 return true;
176}
177
178template<typename SuccessType>
179ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt32(uint32_t& result)
180{
181 return WTF::LEBDecoder::decodeUInt32(m_source, m_sourceLength, m_offset, result);
182}
183
184template<typename SuccessType>
185ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt64(uint64_t& result)
186{
187 return WTF::LEBDecoder::decodeUInt64(m_source, m_sourceLength, m_offset, result);
188}
189
190template<typename SuccessType>
191ALWAYS_INLINE bool Parser<SuccessType>::parseVarInt32(int32_t& result)
192{
193 return WTF::LEBDecoder::decodeInt32(m_source, m_sourceLength, m_offset, result);
194}
195
196template<typename SuccessType>
197ALWAYS_INLINE bool Parser<SuccessType>::parseVarInt64(int64_t& result)
198{
199 return WTF::LEBDecoder::decodeInt64(m_source, m_sourceLength, m_offset, result);
200}
201
202template<typename SuccessType>
203ALWAYS_INLINE bool Parser<SuccessType>::parseUInt32(uint32_t& result)
204{
205 if (length() < 4 || m_offset > length() - 4)
206 return false;
207 result = *reinterpret_cast<const uint32_t*>(source() + m_offset);
208 m_offset += 4;
209 return true;
210}
211
212template<typename SuccessType>
213ALWAYS_INLINE bool Parser<SuccessType>::parseUInt64(uint64_t& result)
214{
215 if (length() < 8 || m_offset > length() - 8)
216 return false;
217 result = *reinterpret_cast<const uint64_t*>(source() + m_offset);
218 m_offset += 8;
219 return true;
220}
221
222template<typename SuccessType>
223ALWAYS_INLINE bool Parser<SuccessType>::parseUInt8(uint8_t& result)
224{
225 if (m_offset >= length())
226 return false;
227 result = source()[m_offset++];
228 return true;
229}
230
231template<typename SuccessType>
232ALWAYS_INLINE bool Parser<SuccessType>::parseInt7(int8_t& result)
233{
234 if (m_offset >= length())
235 return false;
236 uint8_t v = source()[m_offset++];
237 result = (v & 0x40) ? WTF::bitwise_cast<int8_t>(uint8_t(v | 0x80)) : v;
238 return (v & 0x80) == 0;
239}
240
241template<typename SuccessType>
242ALWAYS_INLINE bool Parser<SuccessType>::peekInt7(int8_t& result)
243{
244 if (m_offset >= length())
245 return false;
246 uint8_t v = source()[m_offset];
247 result = (v & 0x40) ? WTF::bitwise_cast<int8_t>(uint8_t(v | 0x80)) : v;
248 return (v & 0x80) == 0;
249}
250
251template<typename SuccessType>
252ALWAYS_INLINE bool Parser<SuccessType>::parseUInt7(uint8_t& result)
253{
254 if (m_offset >= length())
255 return false;
256 result = source()[m_offset++];
257 return result < 0x80;
258}
259
260template<typename SuccessType>
261ALWAYS_INLINE bool Parser<SuccessType>::parseVarUInt1(uint8_t& result)
262{
263 uint32_t temp;
264 if (!parseVarUInt32(temp))
265 return false;
266 if (temp > 1)
267 return false;
268 result = static_cast<uint8_t>(temp);
269 return true;
270}
271
272template<typename SuccessType>
273ALWAYS_INLINE typename Parser<SuccessType>::PartialResult Parser<SuccessType>::parseBlockSignature(const ModuleInformation& info, BlockSignature& result)
274{
275 int8_t value;
276 if (peekInt7(value) && isValidType(value)) {
277 Type type = static_cast<Type>(value);
278 WASM_PARSER_FAIL_IF(!(isValueType(type) || type == Void), "result type of block: ", makeString(type), " is not a value type or Void");
279 result = m_signatureInformation.thunkFor(type);
280 m_offset++;
281 return { };
282 }
283
284 WASM_PARSER_FAIL_IF(!Options::useWebAssemblyMultiValues(), "Type table indices for block signatures are not supported yet");
285
286 int64_t index;
287 WASM_PARSER_FAIL_IF(!parseVarInt64(index), "Block-like instruction doesn't return value type but can't decode type section index");
288 WASM_PARSER_FAIL_IF(index < 0, "Block-like instruction signature index is negative");
289 WASM_PARSER_FAIL_IF(static_cast<size_t>(index) >= info.usedSignatures.size(), "Block-like instruction signature index is out of bounds. Index: ", index, " type index space: ", info.usedSignatures.size());
290
291 result = &info.usedSignatures[index].get();
292 return { };
293}
294
295template<typename SuccessType>
296ALWAYS_INLINE bool Parser<SuccessType>::parseValueType(Type& result)
297{
298 int8_t value;
299 if (!parseInt7(value))
300 return false;
301 if (!isValidType(value) || !isValueType(static_cast<Type>(value)))
302 return false;
303 result = static_cast<Type>(value);
304 return true;
305}
306
307template<typename SuccessType>
308ALWAYS_INLINE bool Parser<SuccessType>::parseExternalKind(ExternalKind& result)
309{
310 uint8_t value;
311 if (!parseUInt7(value))
312 return false;
313 if (!isValidExternalKind(value))
314 return false;
315 result = static_cast<ExternalKind>(value);
316 return true;
317}
318
319ALWAYS_INLINE I32InitExpr makeI32InitExpr(uint8_t opcode, uint32_t bits)
320{
321 RELEASE_ASSERT(opcode == I32Const || opcode == GetGlobal);
322 if (opcode == I32Const)
323 return I32InitExpr::constValue(bits);
324 return I32InitExpr::globalImport(bits);
325}
326
327} } // namespace JSC::Wasm
328
329#endif // ENABLE(WEBASSEMBLY)
330