1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WasmModuleParser.h"
#if ENABLE(WEBASSEMBLY)
#include "IdentifierInlines.h"
#include "JSWebAssemblyTable.h"
#include "WasmFormat.h"
#include "WasmMemoryInformation.h"
#include "WasmOps.h"
#include "WasmSections.h"
#include <sys/mman.h>
namespace JSC { namespace Wasm {
ALWAYS_INLINE I32InitExpr makeI32InitExpr(uint8_t opcode, uint32_t bits)
{
RELEASE_ASSERT(opcode == I32Const || opcode == GetGlobal);
if (opcode == I32Const)
return I32InitExpr::constValue(bits);
return I32InitExpr::globalImport(bits);
}
auto ModuleParser::parse() -> Result
{
m_result.module = std::make_unique<ModuleInformation>();
const size_t minSize = 8;
uint32_t versionNumber;
WASM_PARSER_FAIL_IF(length() < minSize, "expected a module of at least ", minSize, " bytes");
WASM_PARSER_FAIL_IF(!consumeCharacter(0) || !consumeString("asm"), "modules doesn't start with '\\0asm'");
WASM_PARSER_FAIL_IF(!parseUInt32(versionNumber), "can't parse version number");
WASM_PARSER_FAIL_IF(versionNumber != expectedVersionNumber, "unexpected version number ", versionNumber, " expected ", expectedVersionNumber);
Section previousSection = Section::Unknown;
while (m_offset < length()) {
uint8_t sectionByte;
WASM_PARSER_FAIL_IF(!parseUInt7(sectionByte), "can't get section byte");
Section section = Section::Unknown;
if (sectionByte) {
if (isValidSection(sectionByte))
section = static_cast<Section>(sectionByte);
}
uint32_t sectionLength;
WASM_PARSER_FAIL_IF(!validateOrder(previousSection, section), "invalid section order, ", previousSection, " followed by ", section);
WASM_PARSER_FAIL_IF(!parseVarUInt32(sectionLength), "can't get ", section, " section's length");
WASM_PARSER_FAIL_IF(sectionLength > length() - m_offset, section, "section of size ", sectionLength, " would overflow Module's size");
auto end = m_offset + sectionLength;
switch (section) {
#define WASM_SECTION_PARSE(NAME, ID, DESCRIPTION) \
case Section::NAME: { \
WASM_FAIL_IF_HELPER_FAILS(parse ## NAME()); \
break; \
}
FOR_EACH_WASM_SECTION(WASM_SECTION_PARSE)
#undef WASM_SECTION_PARSE
case Section::Unknown: {
WASM_FAIL_IF_HELPER_FAILS(parseCustom(sectionLength));
break;
}
}
WASM_PARSER_FAIL_IF(end != m_offset, "parsing ended before the end of ", section, " section");
previousSection = section;
}
return WTFMove(m_result);
}
auto ModuleParser::parseType() -> PartialResult
{
uint32_t count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Type section's count");
WASM_PARSER_FAIL_IF(count == std::numeric_limits<uint32_t>::max(), "Type section's count is too big ", count);
WASM_PARSER_FAIL_IF(!m_result.moduleSignatureIndicesToUniquedSignatureIndices.tryReserveCapacity(count), "can't allocate enough memory for Type section's ", count, " entries");
for (uint32_t i = 0; i < count; ++i) {
int8_t type;
uint32_t argumentCount;
Vector<Type> argumentTypes;
WASM_PARSER_FAIL_IF(!parseInt7(type), "can't get ", i, "th Type's type");
WASM_PARSER_FAIL_IF(type != Func, i, "th Type is non-Func ", type);
WASM_PARSER_FAIL_IF(!parseVarUInt32(argumentCount), "can't get ", i, "th Type's argument count");
WASM_PARSER_FAIL_IF(argumentCount == std::numeric_limits<uint32_t>::max(), i, "th argument count is too big ", argumentCount);
std::unique_ptr<Signature, void (*)(Signature*)> signature(Signature::create(argumentCount), &Signature::destroy);
WASM_PARSER_FAIL_IF(!signature, "can't allocate enough memory for Type section's ", i, "th signature");
for (unsigned i = 0; i < argumentCount; ++i) {
Type argumentType;
WASM_PARSER_FAIL_IF(!parseResultType(argumentType), "can't get ", i, "th argument Type");
signature->argument(i) = argumentType;
}
uint8_t returnCount;
WASM_PARSER_FAIL_IF(!parseVarUInt1(returnCount), "can't get ", i, "th Type's return count");
Type returnType;
if (returnCount) {
Type value;
WASM_PARSER_FAIL_IF(!parseValueType(value), "can't get ", i, "th Type's return value");
returnType = static_cast<Type>(value);
} else
returnType = Type::Void;
signature->returnType() = returnType;
SignatureIndex signatureIndex = SignatureInformation::adopt(m_vm, signature.release());
m_result.moduleSignatureIndicesToUniquedSignatureIndices.uncheckedAppend(signatureIndex);
}
return { };
}
auto ModuleParser::parseImport() -> PartialResult
{
uint32_t importCount;
WASM_PARSER_FAIL_IF(!parseVarUInt32(importCount), "can't get Import section's count");
WASM_PARSER_FAIL_IF(importCount == std::numeric_limits<uint32_t>::max(), "Import section's count is too big ", importCount);
WASM_PARSER_FAIL_IF(!m_result.module->globals.tryReserveCapacity(importCount), "can't allocate enough memory for ", importCount, " globals"); // FIXME this over-allocates when we fix the FIXMEs below.
WASM_PARSER_FAIL_IF(!m_result.module->imports.tryReserveCapacity(importCount), "can't allocate enough memory for ", importCount, " imports"); // FIXME this over-allocates when we fix the FIXMEs below.
WASM_PARSER_FAIL_IF(!m_result.module->importFunctionSignatureIndices.tryReserveCapacity(importCount), "can't allocate enough memory for ", importCount, " import function signatures"); // FIXME this over-allocates when we fix the FIXMEs below.
for (uint32_t importNumber = 0; importNumber < importCount; ++importNumber) {
Import imp;
uint32_t moduleLen;
uint32_t fieldLen;
String moduleString;
String fieldString;
WASM_PARSER_FAIL_IF(!parseVarUInt32(moduleLen), "can't get ", importNumber, "th Import's module name length");
WASM_PARSER_FAIL_IF(!consumeUTF8String(moduleString, moduleLen), "can't get ", importNumber, "th Import's module name of length ", moduleLen);
imp.module = Identifier::fromString(m_vm, moduleString);
WASM_PARSER_FAIL_IF(!parseVarUInt32(fieldLen), "can't get ", importNumber, "th Import's field name length in module '", moduleString, "'");
WASM_PARSER_FAIL_IF(!consumeUTF8String(fieldString, fieldLen), "can't get ", importNumber, "th Import's field name of length ", moduleLen, " in module '", moduleString, "'");
imp.field = Identifier::fromString(m_vm, fieldString);
WASM_PARSER_FAIL_IF(!parseExternalKind(imp.kind), "can't get ", importNumber, "th Import's kind in module '", moduleString, "' field '", fieldString, "'");
switch (imp.kind) {
case ExternalKind::Function: {
uint32_t functionSignatureIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(functionSignatureIndex), "can't get ", importNumber, "th Import's function signature in module '", moduleString, "' field '", fieldString, "'");
WASM_PARSER_FAIL_IF(functionSignatureIndex >= m_result.moduleSignatureIndicesToUniquedSignatureIndices.size(), "invalid function signature for ", importNumber, "th Import, ", functionSignatureIndex, " is out of range of ", m_result.moduleSignatureIndicesToUniquedSignatureIndices.size(), " in module '", moduleString, "' field '", fieldString, "'");
imp.kindIndex = m_result.module->importFunctionSignatureIndices.size();
SignatureIndex signatureIndex = m_result.moduleSignatureIndicesToUniquedSignatureIndices[functionSignatureIndex];
m_result.module->importFunctionSignatureIndices.uncheckedAppend(signatureIndex);
break;
}
case ExternalKind::Table: {
bool isImport = true;
PartialResult result = parseTableHelper(isImport);
if (UNLIKELY(!result))
return result.getUnexpected();
break;
}
case ExternalKind::Memory: {
bool isImport = true;
PartialResult result = parseMemoryHelper(isImport);
if (UNLIKELY(!result))
return result.getUnexpected();
break;
}
case ExternalKind::Global: {
Global global;
WASM_FAIL_IF_HELPER_FAILS(parseGlobalType(global));
WASM_PARSER_FAIL_IF(global.mutability == Global::Mutable, "Mutable Globals aren't supported");
imp.kindIndex = m_result.module->globals.size();
m_result.module->globals.uncheckedAppend(WTFMove(global));
break;
}
}
m_result.module->imports.uncheckedAppend(imp);
}
m_result.module->firstInternalGlobal = m_result.module->globals.size();
return { };
}
auto ModuleParser::parseFunction() -> PartialResult
{
uint32_t count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Function section's count");
WASM_PARSER_FAIL_IF(count == std::numeric_limits<uint32_t>::max(), "Function section's count is too big ", count);
WASM_PARSER_FAIL_IF(!m_result.module->internalFunctionSignatureIndices.tryReserveCapacity(count), "can't allocate enough memory for ", count, " Function signatures");
WASM_PARSER_FAIL_IF(!m_result.functionLocationInBinary.tryReserveCapacity(count), "can't allocate enough memory for ", count, "Function locations");
for (uint32_t i = 0; i < count; ++i) {
uint32_t typeNumber;
WASM_PARSER_FAIL_IF(!parseVarUInt32(typeNumber), "can't get ", i, "th Function's type number");
WASM_PARSER_FAIL_IF(typeNumber >= m_result.moduleSignatureIndicesToUniquedSignatureIndices.size(), i, "th Function type number is invalid ", typeNumber);
SignatureIndex signatureIndex = m_result.moduleSignatureIndicesToUniquedSignatureIndices[typeNumber];
// The Code section fixes up start and end.
size_t start = 0;
size_t end = 0;
m_result.module->internalFunctionSignatureIndices.uncheckedAppend(signatureIndex);
m_result.functionLocationInBinary.uncheckedAppend({ start, end });
}
return { };
}
auto ModuleParser::parseResizableLimits(uint32_t& initial, std::optional<uint32_t>& maximum) -> PartialResult
{
ASSERT(!maximum);
uint8_t flags;
WASM_PARSER_FAIL_IF(!parseVarUInt1(flags), "can't parse resizable limits flags");
WASM_PARSER_FAIL_IF(!parseVarUInt32(initial), "can't parse resizable limits initial page count");
if (flags) {
uint32_t maximumInt;
WASM_PARSER_FAIL_IF(!parseVarUInt32(maximumInt), "can't parse resizable limits maximum page count");
WASM_PARSER_FAIL_IF(initial > maximumInt, "resizable limits has a initial page count of ", initial, " which is greater than its maximum ", maximumInt);
maximum = maximumInt;
}
return { };
}
auto ModuleParser::parseTableHelper(bool isImport) -> PartialResult
{
WASM_PARSER_FAIL_IF(m_hasTable, "Table section cannot exist if an Import has a table");
m_hasTable = true;
int8_t type;
WASM_PARSER_FAIL_IF(!parseInt7(type), "can't parse Table type");
WASM_PARSER_FAIL_IF(type != Wasm::Anyfunc, "Table type should be anyfunc, got ", type);
uint32_t initial;
std::optional<uint32_t> maximum;
PartialResult limits = parseResizableLimits(initial, maximum);
if (UNLIKELY(!limits))
return limits.getUnexpected();
WASM_PARSER_FAIL_IF(!JSWebAssemblyTable::isValidSize(initial), "Table's initial page count of ", initial, " is invalid");
ASSERT(!maximum || *maximum >= initial);
m_result.module->tableInformation = TableInformation(initial, maximum, isImport);
return { };
}
auto ModuleParser::parseTable() -> PartialResult
{
uint32_t count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Table's count");
WASM_PARSER_FAIL_IF(count != 1, "Table count of ", count, " is invalid, only 1 is allowed for now");
bool isImport = false;
PartialResult result = parseTableHelper(isImport);
if (UNLIKELY(!result))
return result.getUnexpected();
return { };
}
auto ModuleParser::parseMemoryHelper(bool isImport) -> PartialResult
{
WASM_PARSER_FAIL_IF(!!m_result.module->memory, "Memory section cannot exist if an Import has a memory");
PageCount initialPageCount;
PageCount maximumPageCount;
{
uint32_t initial;
std::optional<uint32_t> maximum;
PartialResult limits = parseResizableLimits(initial, maximum);
if (UNLIKELY(!limits))
return limits.getUnexpected();
ASSERT(!maximum || *maximum >= initial);
WASM_PARSER_FAIL_IF(!PageCount::isValid(initial), "Memory's initial page count of ", initial, " is invalid");
initialPageCount = PageCount(initial);
if (maximum) {
WASM_PARSER_FAIL_IF(!PageCount::isValid(*maximum), "Memory's maximum page count of ", *maximum, " is invalid");
maximumPageCount = PageCount(*maximum);
}
}
ASSERT(initialPageCount);
ASSERT(!maximumPageCount || maximumPageCount >= initialPageCount);
m_result.module->memory = MemoryInformation(initialPageCount, maximumPageCount, isImport);
return { };
}
auto ModuleParser::parseMemory() -> PartialResult
{
uint8_t count;
WASM_PARSER_FAIL_IF(!parseVarUInt1(count), "can't parse Memory section's count");
if (!count)
return { };
WASM_PARSER_FAIL_IF(count != 1, "Memory section has more than one memory, WebAssembly currently only allows zero or one");
bool isImport = false;
return parseMemoryHelper(isImport);
}
auto ModuleParser::parseGlobal() -> PartialResult
{
uint32_t globalCount;
WASM_PARSER_FAIL_IF(!parseVarUInt32(globalCount), "can't get Global section's count");
WASM_PARSER_FAIL_IF(!m_result.module->globals.tryReserveCapacity(globalCount + m_result.module->firstInternalGlobal), "can't allocate memory for ", globalCount + m_result.module->firstInternalGlobal, " globals");
for (uint32_t globalIndex = 0; globalIndex < globalCount; ++globalIndex) {
Global global;
uint8_t initOpcode;
WASM_FAIL_IF_HELPER_FAILS(parseGlobalType(global));
Type typeForInitOpcode;
WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, global.initialBitsOrImportNumber, typeForInitOpcode));
if (initOpcode == GetGlobal)
global.initializationType = Global::FromGlobalImport;
else
global.initializationType = Global::FromExpression;
WASM_PARSER_FAIL_IF(typeForInitOpcode != global.type, "Global init_expr opcode of type ", typeForInitOpcode, " doesn't match global's type ", global.type);
m_result.module->globals.uncheckedAppend(WTFMove(global));
}
return { };
}
auto ModuleParser::parseExport() -> PartialResult
{
uint32_t exportCount;
WASM_PARSER_FAIL_IF(!parseVarUInt32(exportCount), "can't get Export section's count");
WASM_PARSER_FAIL_IF(exportCount == std::numeric_limits<uint32_t>::max(), "Export section's count is too big ", exportCount);
WASM_PARSER_FAIL_IF(!m_result.module->exports.tryReserveCapacity(exportCount), "can't allocate enough memory for ", exportCount, " exports");
HashSet<String> exportNames;
for (uint32_t exportNumber = 0; exportNumber < exportCount; ++exportNumber) {
Export exp;
uint32_t fieldLen;
String fieldString;
WASM_PARSER_FAIL_IF(!parseVarUInt32(fieldLen), "can't get ", exportNumber, "th Export's field name length");
WASM_PARSER_FAIL_IF(!consumeUTF8String(fieldString, fieldLen), "can't get ", exportNumber, "th Export's field name of length ", fieldLen);
WASM_PARSER_FAIL_IF(exportNames.contains(fieldString), "duplicate export: '", fieldString, "'");
exportNames.add(fieldString);
exp.field = Identifier::fromString(m_vm, fieldString);
WASM_PARSER_FAIL_IF(!parseExternalKind(exp.kind), "can't get ", exportNumber, "th Export's kind, named '", fieldString, "'");
WASM_PARSER_FAIL_IF(!parseVarUInt32(exp.kindIndex), "can't get ", exportNumber, "th Export's kind index, named '", fieldString, "'");
switch (exp.kind) {
case ExternalKind::Function: {
WASM_PARSER_FAIL_IF(exp.kindIndex >= m_result.module->functionIndexSpaceSize(), exportNumber, "th Export has invalid function number ", exp.kindIndex, " it exceeds the function index space ", m_result.module->functionIndexSpaceSize(), ", named '", fieldString, "'");
break;
}
case ExternalKind::Table: {
WASM_PARSER_FAIL_IF(!m_hasTable, "can't export a non-existent Table");
WASM_PARSER_FAIL_IF(exp.kindIndex, "can't export Table ", exp.kindIndex, " only zero-index Table is currently supported");
break;
}
case ExternalKind::Memory: {
WASM_PARSER_FAIL_IF(!m_result.module->memory, "can't export a non-existent Memory");
WASM_PARSER_FAIL_IF(exp.kindIndex, "can't export Memory ", exp.kindIndex, " only one Table is currently supported");
break;
}
case ExternalKind::Global: {
WASM_PARSER_FAIL_IF(exp.kindIndex >= m_result.module->globals.size(), exportNumber, "th Export has invalid global number ", exp.kindIndex, " it exceeds the globals count ", m_result.module->globals.size(), ", named '", fieldString, "'");
WASM_PARSER_FAIL_IF(m_result.module->globals[exp.kindIndex].mutability != Global::Immutable, exportNumber, "th Export isn't immutable, named '", fieldString, "'");
break;
}
}
m_result.module->exports.uncheckedAppend(exp);
}
return { };
}
auto ModuleParser::parseStart() -> PartialResult
{
uint32_t startFunctionIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(startFunctionIndex), "can't get Start index");
WASM_PARSER_FAIL_IF(startFunctionIndex >= m_result.module->functionIndexSpaceSize(), "Start index ", startFunctionIndex, " exceeds function index space ", m_result.module->functionIndexSpaceSize());
SignatureIndex signatureIndex = m_result.module->signatureIndexFromFunctionIndexSpace(startFunctionIndex);
const Signature* signature = SignatureInformation::get(m_vm, signatureIndex);
WASM_PARSER_FAIL_IF(signature->argumentCount(), "Start function can't have arguments");
WASM_PARSER_FAIL_IF(signature->returnType() != Void, "Start function can't return a value");
m_result.module->startFunctionIndexSpace = startFunctionIndex;
return { };
}
auto ModuleParser::parseElement() -> PartialResult
{
WASM_PARSER_FAIL_IF(!m_hasTable, "Element section expects a Table to be present");
uint32_t elementCount;
WASM_PARSER_FAIL_IF(!parseVarUInt32(elementCount), "can't get Element section's count");
WASM_PARSER_FAIL_IF(elementCount == std::numeric_limits<uint32_t>::max(), "Element section's count is too big ", elementCount);
WASM_PARSER_FAIL_IF(!m_result.module->elements.tryReserveCapacity(elementCount), "can't allocate memory for ", elementCount, " Elements");
for (unsigned elementNum = 0; elementNum < elementCount; ++elementNum) {
uint32_t tableIndex;
uint64_t offset;
uint8_t initOpcode;
uint32_t indexCount;
WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), "can't get ", elementNum, "th Element table index");
WASM_PARSER_FAIL_IF(tableIndex, "Element section can only have one Table for now");
Type initExprType;
WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, offset, initExprType));
WASM_PARSER_FAIL_IF(initOpcode != OpType::I32Const, "Element section doesn't support non-i32 init_expr opcode for now, got ", initOpcode);
WASM_PARSER_FAIL_IF(!parseVarUInt32(indexCount), "can't get ", elementNum, "th index count for Element section");
WASM_PARSER_FAIL_IF(indexCount == std::numeric_limits<uint32_t>::max(), "Element section's ", elementNum, "th index count is too big ", indexCount);
ASSERT(!!m_result.module->tableInformation);
if (std::optional<uint32_t> maximum = m_result.module->tableInformation.maximum()) {
// FIXME: should indexCount being zero be a validation error?
// https://bugs.webkit.org/show_bug.cgi?id=165826
if (indexCount) {
// FIXME: right now, provably out of bounds writes are validation errors.
// Should they be though?
// https://bugs.webkit.org/show_bug.cgi?id=165827
uint64_t lastWrittenIndex = static_cast<uint64_t>(indexCount) + static_cast<uint64_t>(offset) - 1;
WASM_PARSER_FAIL_IF(lastWrittenIndex >= static_cast<uint64_t>(*maximum), "Element section's ", elementNum, "th element writes to index ", lastWrittenIndex, " which exceeds the maximum ", *maximum);
}
}
Element element;
WASM_PARSER_FAIL_IF(!element.functionIndices.tryReserveCapacity(indexCount), "can't allocate memory for ", indexCount, " Element indices");
element.offset = offset;
for (unsigned index = 0; index < indexCount; ++index) {
uint32_t functionIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(functionIndex), "can't get Element section's ", elementNum, "th element's ", index, "th index");
WASM_PARSER_FAIL_IF(functionIndex >= m_result.module->functionIndexSpaceSize(), "Element section's ", elementNum, "th element's ", index, "th index is ", functionIndex, " which exceeds the function index space size of ", m_result.module->functionIndexSpaceSize());
element.functionIndices.uncheckedAppend(functionIndex);
}
m_result.module->elements.uncheckedAppend(WTFMove(element));
}
return { };
}
auto ModuleParser::parseCode() -> PartialResult
{
uint32_t count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Code section's count");
WASM_PARSER_FAIL_IF(count == std::numeric_limits<uint32_t>::max(), "Code section's count is too big ", count);
WASM_PARSER_FAIL_IF(count != m_result.functionLocationInBinary.size(), "Code section count ", count, " exceeds the declared number of functions ", m_result.functionLocationInBinary.size());
for (uint32_t i = 0; i < count; ++i) {
uint32_t functionSize;
WASM_PARSER_FAIL_IF(!parseVarUInt32(functionSize), "can't get ", i, "th Code function's size");
WASM_PARSER_FAIL_IF(functionSize > length(), "Code function's size ", functionSize, " exceeds the module's size ", length());
WASM_PARSER_FAIL_IF(functionSize > length() - m_offset, "Code function's size ", functionSize, " exceeds the module's remaining size", length() - m_offset);
m_result.functionLocationInBinary[i].start = m_offset;
m_result.functionLocationInBinary[i].end = m_offset + functionSize;
m_offset = m_result.functionLocationInBinary[i].end;
}
return { };
}
auto ModuleParser::parseInitExpr(uint8_t& opcode, uint64_t& bitsOrImportNumber, Type& resultType) -> PartialResult
{
WASM_PARSER_FAIL_IF(!parseUInt8(opcode), "can't get init_expr's opcode");
switch (opcode) {
case I32Const: {
int32_t constant;
WASM_PARSER_FAIL_IF(!parseVarInt32(constant), "can't get constant value for init_expr's i32.const");
bitsOrImportNumber = static_cast<uint64_t>(constant);
resultType = I32;
break;
}
case I64Const: {
int64_t constant;
WASM_PARSER_FAIL_IF(!parseVarInt64(constant), "can't get constant value for init_expr's i64.const");
bitsOrImportNumber = constant;
resultType = I64;
break;
}
case F32Const: {
uint32_t constant;
WASM_PARSER_FAIL_IF(!parseUInt32(constant), "can't get constant value for init_expr's f32.const");
bitsOrImportNumber = constant;
resultType = F32;
break;
}
case F64Const: {
uint64_t constant;
WASM_PARSER_FAIL_IF(!parseUInt64(constant), "can't get constant value for init_expr's f64.const");
bitsOrImportNumber = constant;
resultType = F64;
break;
}
case GetGlobal: {
uint32_t index;
WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get get_global's index");
WASM_PARSER_FAIL_IF(index >= m_result.module->imports.size(), "get_global's index ", index, " exceeds the number of imports ", m_result.module->imports.size());
const Import& import = m_result.module->imports[index];
WASM_PARSER_FAIL_IF(m_result.module->imports[index].kind != ExternalKind::Global, "get_global's import kind is ", m_result.module->imports[index].kind, " should be global");
WASM_PARSER_FAIL_IF(import.kindIndex >= m_result.module->firstInternalGlobal, "get_global import kind index ", import.kindIndex, " exceeds the first internal global ", m_result.module->firstInternalGlobal);
ASSERT(m_result.module->globals[import.kindIndex].mutability == Global::Immutable);
resultType = m_result.module->globals[index].type;
bitsOrImportNumber = index;
break;
}
default:
WASM_PARSER_FAIL_IF(false, "unknown init_expr opcode ", opcode);
}
uint8_t endOpcode;
WASM_PARSER_FAIL_IF(!parseUInt8(endOpcode), "can't get init_expr's end opcode");
WASM_PARSER_FAIL_IF(endOpcode != OpType::End, "init_expr should end with end, ended with ", endOpcode);
return { };
}
auto ModuleParser::parseGlobalType(Global& global) -> PartialResult
{
uint8_t mutability;
WASM_PARSER_FAIL_IF(!parseValueType(global.type), "can't get Global's value type");
WASM_PARSER_FAIL_IF(!parseVarUInt1(mutability), "can't get Global type's mutability");
global.mutability = static_cast<Global::Mutability>(mutability);
return { };
}
auto ModuleParser::parseData() -> PartialResult
{
uint32_t segmentCount;
WASM_PARSER_FAIL_IF(!m_result.module->memory, "Data section cannot exist without a Memory section or Import");
WASM_PARSER_FAIL_IF(!parseVarUInt32(segmentCount), "can't get Data section's count");
WASM_PARSER_FAIL_IF(segmentCount == std::numeric_limits<uint32_t>::max(), "Data section's count is too big ", segmentCount);
WASM_PARSER_FAIL_IF(!m_result.module->data.tryReserveCapacity(segmentCount), "can't allocate enough memory for Data section's ", segmentCount, " segments");
for (uint32_t segmentNumber = 0; segmentNumber < segmentCount; ++segmentNumber) {
uint32_t index;
uint64_t initExprBits;
uint8_t initOpcode;
uint32_t dataByteLength;
WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get ", segmentNumber, "th Data segment's index");
WASM_PARSER_FAIL_IF(index, segmentNumber, "th Data segment has non-zero index ", index);
Type initExprType;
WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, initExprBits, initExprType));
WASM_PARSER_FAIL_IF(initExprType != I32, segmentNumber, "th Data segment's init_expr must produce an i32");
WASM_PARSER_FAIL_IF(!parseVarUInt32(dataByteLength), "can't get ", segmentNumber, "th Data segment's data byte length");
WASM_PARSER_FAIL_IF(dataByteLength == std::numeric_limits<uint32_t>::max(), segmentNumber, "th Data segment's data byte length is too big ", dataByteLength);
Segment* segment = Segment::create(makeI32InitExpr(initOpcode, initExprBits), dataByteLength);
WASM_PARSER_FAIL_IF(!segment, "can't allocate enough memory for ", segmentNumber, "th Data segment of size ", dataByteLength);
m_result.module->data.uncheckedAppend(Segment::adoptPtr(segment));
for (uint32_t dataByte = 0; dataByte < dataByteLength; ++dataByte) {
uint8_t byte;
WASM_PARSER_FAIL_IF(!parseUInt8(byte), "can't get ", dataByte, "th data byte from ", segmentNumber, "th Data segment");
segment->byte(dataByte) = byte;
}
}
return { };
}
auto ModuleParser::parseCustom(uint32_t sectionLength) -> PartialResult
{
const uint32_t customSectionStartOffset = m_offset;
CustomSection section;
uint32_t customSectionNumber = m_result.module->customSections.size() + 1;
uint32_t nameLen;
WASM_PARSER_FAIL_IF(!m_result.module->customSections.tryReserveCapacity(customSectionNumber), "can't allocate enough memory for ", customSectionNumber, "th custom section");
WASM_PARSER_FAIL_IF(!parseVarUInt32(nameLen), "can't get ", customSectionNumber, "th custom section's name length");
WASM_PARSER_FAIL_IF(!consumeUTF8String(section.name, nameLen), "nameLen get ", customSectionNumber, "th custom section's name of length ", nameLen);
uint32_t payloadBytes = sectionLength - (m_offset - customSectionStartOffset);
WASM_PARSER_FAIL_IF(!section.payload.tryReserveCapacity(payloadBytes), "can't allocate enough memory for ", customSectionNumber, "th custom section's ", payloadBytes, " bytes");
for (uint32_t byteNumber = 0; byteNumber < payloadBytes; ++byteNumber) {
uint8_t byte;
WASM_PARSER_FAIL_IF(!parseUInt8(byte), "can't get ", byteNumber, "th data byte from ", customSectionNumber, "th custom section");
section.payload.uncheckedAppend(byte);
}
m_result.module->customSections.uncheckedAppend(WTFMove(section));
return { };
}
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
|