1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/wasm/module-decoder.h"
6 
7 #include "src/base/functional.h"
8 #include "src/base/platform/platform.h"
9 #include "src/flags/flags.h"
10 #include "src/init/v8.h"
11 #include "src/logging/counters.h"
12 #include "src/logging/metrics.h"
13 #include "src/objects/objects-inl.h"
14 #include "src/utils/ostreams.h"
15 #include "src/wasm/decoder.h"
16 #include "src/wasm/function-body-decoder-impl.h"
17 #include "src/wasm/struct-types.h"
18 #include "src/wasm/wasm-constants.h"
19 #include "src/wasm/wasm-engine.h"
20 #include "src/wasm/wasm-limits.h"
21 
22 namespace v8 {
23 namespace internal {
24 namespace wasm {
25 
26 #define TRACE(...)                                    \
27   do {                                                \
28     if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \
29   } while (false)
30 
31 namespace {
32 
33 constexpr char kNameString[] = "name";
34 constexpr char kSourceMappingURLString[] = "sourceMappingURL";
35 constexpr char kCompilationHintsString[] = "compilationHints";
36 constexpr char kDebugInfoString[] = ".debug_info";
37 constexpr char kExternalDebugInfoString[] = "external_debug_info";
38 
ExternalKindName(ImportExportKindCode kind)39 const char* ExternalKindName(ImportExportKindCode kind) {
40   switch (kind) {
41     case kExternalFunction:
42       return "function";
43     case kExternalTable:
44       return "table";
45     case kExternalMemory:
46       return "memory";
47     case kExternalGlobal:
48       return "global";
49     case kExternalException:
50       return "exception";
51   }
52   return "unknown";
53 }
54 
55 }  // namespace
56 
SectionName(SectionCode code)57 const char* SectionName(SectionCode code) {
58   switch (code) {
59     case kUnknownSectionCode:
60       return "Unknown";
61     case kTypeSectionCode:
62       return "Type";
63     case kImportSectionCode:
64       return "Import";
65     case kFunctionSectionCode:
66       return "Function";
67     case kTableSectionCode:
68       return "Table";
69     case kMemorySectionCode:
70       return "Memory";
71     case kGlobalSectionCode:
72       return "Global";
73     case kExportSectionCode:
74       return "Export";
75     case kStartSectionCode:
76       return "Start";
77     case kCodeSectionCode:
78       return "Code";
79     case kElementSectionCode:
80       return "Element";
81     case kDataSectionCode:
82       return "Data";
83     case kExceptionSectionCode:
84       return "Exception";
85     case kDataCountSectionCode:
86       return "DataCount";
87     case kNameSectionCode:
88       return kNameString;
89     case kSourceMappingURLSectionCode:
90       return kSourceMappingURLString;
91     case kDebugInfoSectionCode:
92       return kDebugInfoString;
93     case kExternalDebugInfoSectionCode:
94       return kExternalDebugInfoString;
95     case kCompilationHintsSectionCode:
96       return kCompilationHintsString;
97     default:
98       return "<unknown>";
99   }
100 }
101 
102 namespace {
103 
validate_utf8(Decoder * decoder,WireBytesRef string)104 bool validate_utf8(Decoder* decoder, WireBytesRef string) {
105   return unibrow::Utf8::ValidateEncoding(
106       decoder->start() + decoder->GetBufferRelativeOffset(string.offset()),
107       string.length());
108 }
109 
110 // Reads a length-prefixed string, checking that it is within bounds. Returns
111 // the offset of the string, and the length as an out parameter.
consume_string(Decoder * decoder,bool validate_utf8,const char * name)112 WireBytesRef consume_string(Decoder* decoder, bool validate_utf8,
113                             const char* name) {
114   uint32_t length = decoder->consume_u32v("string length");
115   uint32_t offset = decoder->pc_offset();
116   const byte* string_start = decoder->pc();
117   // Consume bytes before validation to guarantee that the string is not oob.
118   if (length > 0) {
119     decoder->consume_bytes(length, name);
120     if (decoder->ok() && validate_utf8 &&
121         !unibrow::Utf8::ValidateEncoding(string_start, length)) {
122       decoder->errorf(string_start, "%s: no valid UTF-8 string", name);
123     }
124   }
125   return {offset, decoder->failed() ? 0 : length};
126 }
127 
128 namespace {
IdentifyUnknownSectionInternal(Decoder * decoder)129 SectionCode IdentifyUnknownSectionInternal(Decoder* decoder) {
130   WireBytesRef string = consume_string(decoder, true, "section name");
131   if (decoder->failed()) {
132     return kUnknownSectionCode;
133   }
134   const byte* section_name_start =
135       decoder->start() + decoder->GetBufferRelativeOffset(string.offset());
136 
137   TRACE("  +%d  section name        : \"%.*s\"\n",
138         static_cast<int>(section_name_start - decoder->start()),
139         string.length() < 20 ? string.length() : 20, section_name_start);
140 
141   using SpecialSectionPair = std::pair<Vector<const char>, SectionCode>;
142   static constexpr SpecialSectionPair kSpecialSections[]{
143       {StaticCharVector(kNameString), kNameSectionCode},
144       {StaticCharVector(kSourceMappingURLString), kSourceMappingURLSectionCode},
145       {StaticCharVector(kCompilationHintsString), kCompilationHintsSectionCode},
146       {StaticCharVector(kDebugInfoString), kDebugInfoSectionCode},
147       {StaticCharVector(kExternalDebugInfoString),
148        kExternalDebugInfoSectionCode}};
149 
150   auto name_vec =
151       Vector<const char>::cast(VectorOf(section_name_start, string.length()));
152   for (auto& special_section : kSpecialSections) {
153     if (name_vec == special_section.first) return special_section.second;
154   }
155 
156   return kUnknownSectionCode;
157 }
158 }  // namespace
159 
160 // An iterator over the sections in a wasm binary module.
161 // Automatically skips all unknown sections.
162 class WasmSectionIterator {
163  public:
WasmSectionIterator(Decoder * decoder)164   explicit WasmSectionIterator(Decoder* decoder)
165       : decoder_(decoder),
166         section_code_(kUnknownSectionCode),
167         section_start_(decoder->pc()),
168         section_end_(decoder->pc()) {
169     next();
170   }
171 
more() const172   inline bool more() const { return decoder_->ok() && decoder_->more(); }
173 
section_code() const174   inline SectionCode section_code() const { return section_code_; }
175 
section_start() const176   inline const byte* section_start() const { return section_start_; }
177 
section_length() const178   inline uint32_t section_length() const {
179     return static_cast<uint32_t>(section_end_ - section_start_);
180   }
181 
payload() const182   inline Vector<const uint8_t> payload() const {
183     return {payload_start_, payload_length()};
184   }
185 
payload_start() const186   inline const byte* payload_start() const { return payload_start_; }
187 
payload_length() const188   inline uint32_t payload_length() const {
189     return static_cast<uint32_t>(section_end_ - payload_start_);
190   }
191 
section_end() const192   inline const byte* section_end() const { return section_end_; }
193 
194   // Advances to the next section, checking that decoding the current section
195   // stopped at {section_end_}.
advance(bool move_to_section_end=false)196   void advance(bool move_to_section_end = false) {
197     if (move_to_section_end && decoder_->pc() < section_end_) {
198       decoder_->consume_bytes(
199           static_cast<uint32_t>(section_end_ - decoder_->pc()));
200     }
201     if (decoder_->pc() != section_end_) {
202       const char* msg = decoder_->pc() < section_end_ ? "shorter" : "longer";
203       decoder_->errorf(decoder_->pc(),
204                        "section was %s than expected size "
205                        "(%u bytes expected, %zu decoded)",
206                        msg, section_length(),
207                        static_cast<size_t>(decoder_->pc() - section_start_));
208     }
209     next();
210   }
211 
212  private:
213   Decoder* decoder_;
214   SectionCode section_code_;
215   const byte* section_start_;
216   const byte* payload_start_;
217   const byte* section_end_;
218 
219   // Reads the section code/name at the current position and sets up
220   // the embedder fields.
next()221   void next() {
222     if (!decoder_->more()) {
223       section_code_ = kUnknownSectionCode;
224       return;
225     }
226     section_start_ = decoder_->pc();
227     uint8_t section_code = decoder_->consume_u8("section code");
228     // Read and check the section size.
229     uint32_t section_length = decoder_->consume_u32v("section length");
230 
231     payload_start_ = decoder_->pc();
232     if (decoder_->checkAvailable(section_length)) {
233       // Get the limit of the section within the module.
234       section_end_ = payload_start_ + section_length;
235     } else {
236       // The section would extend beyond the end of the module.
237       section_end_ = payload_start_;
238     }
239 
240     if (section_code == kUnknownSectionCode) {
241       // Check for the known "name", "sourceMappingURL", or "compilationHints"
242       // section.
243       // To identify the unknown section we set the end of the decoder bytes to
244       // the end of the custom section, so that we do not read the section name
245       // beyond the end of the section.
246       const byte* module_end = decoder_->end();
247       decoder_->set_end(section_end_);
248       section_code = IdentifyUnknownSectionInternal(decoder_);
249       if (decoder_->ok()) decoder_->set_end(module_end);
250       // As a side effect, the above function will forward the decoder to after
251       // the identifier string.
252       payload_start_ = decoder_->pc();
253     } else if (!IsValidSectionCode(section_code)) {
254       decoder_->errorf(decoder_->pc(), "unknown section code #0x%02x",
255                        section_code);
256       section_code = kUnknownSectionCode;
257     }
258     section_code_ = decoder_->failed() ? kUnknownSectionCode
259                                        : static_cast<SectionCode>(section_code);
260 
261     if (section_code_ == kUnknownSectionCode && section_end_ > decoder_->pc()) {
262       // skip to the end of the unknown section.
263       uint32_t remaining = static_cast<uint32_t>(section_end_ - decoder_->pc());
264       decoder_->consume_bytes(remaining, "section payload");
265     }
266   }
267 };
268 
269 }  // namespace
270 
271 // The main logic for decoding the bytes of a module.
272 class ModuleDecoderImpl : public Decoder {
273  public:
ModuleDecoderImpl(const WasmFeatures & enabled,ModuleOrigin origin)274   explicit ModuleDecoderImpl(const WasmFeatures& enabled, ModuleOrigin origin)
275       : Decoder(nullptr, nullptr),
276         enabled_features_(enabled),
277         origin_(origin) {}
278 
ModuleDecoderImpl(const WasmFeatures & enabled,const byte * module_start,const byte * module_end,ModuleOrigin origin)279   ModuleDecoderImpl(const WasmFeatures& enabled, const byte* module_start,
280                     const byte* module_end, ModuleOrigin origin)
281       : Decoder(module_start, module_end),
282         enabled_features_(enabled),
283         module_start_(module_start),
284         module_end_(module_end),
285         origin_(origin) {
286     if (end_ < start_) {
287       error(start_, "end is less than start");
288       end_ = start_;
289     }
290   }
291 
onFirstError()292   void onFirstError() override {
293     pc_ = end_;  // On error, terminate section decoding loop.
294   }
295 
DumpModule(const Vector<const byte> module_bytes)296   void DumpModule(const Vector<const byte> module_bytes) {
297     std::string path;
298     if (FLAG_dump_wasm_module_path) {
299       path = FLAG_dump_wasm_module_path;
300       if (path.size() &&
301           !base::OS::isDirectorySeparator(path[path.size() - 1])) {
302         path += base::OS::DirectorySeparator();
303       }
304     }
305     // File are named `HASH.{ok,failed}.wasm`.
306     size_t hash = base::hash_range(module_bytes.begin(), module_bytes.end());
307     EmbeddedVector<char, 32> buf;
308     SNPrintF(buf, "%016zx.%s.wasm", hash, ok() ? "ok" : "failed");
309     path += buf.begin();
310     size_t rv = 0;
311     if (FILE* file = base::OS::FOpen(path.c_str(), "wb")) {
312       rv = fwrite(module_bytes.begin(), module_bytes.length(), 1, file);
313       fclose(file);
314     }
315     if (rv != 1) {
316       OFStream os(stderr);
317       os << "Error while dumping wasm file to " << path << std::endl;
318     }
319   }
320 
StartDecoding(Counters * counters,AccountingAllocator * allocator)321   void StartDecoding(Counters* counters, AccountingAllocator* allocator) {
322     CHECK_NULL(module_);
323     SetCounters(counters);
324     module_.reset(
325         new WasmModule(std::make_unique<Zone>(allocator, "signatures")));
326     module_->initial_pages = 0;
327     module_->maximum_pages = 0;
328     module_->mem_export = false;
329     module_->origin = origin_;
330   }
331 
DecodeModuleHeader(Vector<const uint8_t> bytes,uint8_t offset)332   void DecodeModuleHeader(Vector<const uint8_t> bytes, uint8_t offset) {
333     if (failed()) return;
334     Reset(bytes, offset);
335 
336     const byte* pos = pc_;
337     uint32_t magic_word = consume_u32("wasm magic");
338 #define BYTES(x) (x & 0xFF), (x >> 8) & 0xFF, (x >> 16) & 0xFF, (x >> 24) & 0xFF
339     if (magic_word != kWasmMagic) {
340       errorf(pos,
341              "expected magic word %02x %02x %02x %02x, "
342              "found %02x %02x %02x %02x",
343              BYTES(kWasmMagic), BYTES(magic_word));
344     }
345 
346     pos = pc_;
347     {
348       uint32_t magic_version = consume_u32("wasm version");
349       if (magic_version != kWasmVersion) {
350         errorf(pos,
351                "expected version %02x %02x %02x %02x, "
352                "found %02x %02x %02x %02x",
353                BYTES(kWasmVersion), BYTES(magic_version));
354       }
355     }
356 #undef BYTES
357   }
358 
CheckSectionOrder(SectionCode section_code,SectionCode prev_section_code,SectionCode next_section_code)359   bool CheckSectionOrder(SectionCode section_code,
360                          SectionCode prev_section_code,
361                          SectionCode next_section_code) {
362     if (next_ordered_section_ > next_section_code) {
363       errorf(pc(), "The %s section must appear before the %s section",
364              SectionName(section_code), SectionName(next_section_code));
365       return false;
366     }
367     if (next_ordered_section_ <= prev_section_code) {
368       next_ordered_section_ = prev_section_code + 1;
369     }
370     return true;
371   }
372 
CheckUnorderedSection(SectionCode section_code)373   bool CheckUnorderedSection(SectionCode section_code) {
374     if (has_seen_unordered_section(section_code)) {
375       errorf(pc(), "Multiple %s sections not allowed",
376              SectionName(section_code));
377       return false;
378     }
379     set_seen_unordered_section(section_code);
380     return true;
381   }
382 
DecodeSection(SectionCode section_code,Vector<const uint8_t> bytes,uint32_t offset,bool verify_functions=true)383   void DecodeSection(SectionCode section_code, Vector<const uint8_t> bytes,
384                      uint32_t offset, bool verify_functions = true) {
385     if (failed()) return;
386     Reset(bytes, offset);
387     TRACE("Section: %s\n", SectionName(section_code));
388     TRACE("Decode Section %p - %p\n", bytes.begin(), bytes.end());
389 
390     // Check if the section is out-of-order.
391     if (section_code < next_ordered_section_ &&
392         section_code < kFirstUnorderedSection) {
393       errorf(pc(), "unexpected section <%s>", SectionName(section_code));
394       return;
395     }
396 
397     switch (section_code) {
398       case kUnknownSectionCode:
399         break;
400       case kDataCountSectionCode:
401         if (!CheckUnorderedSection(section_code)) return;
402         if (!CheckSectionOrder(section_code, kElementSectionCode,
403                                kCodeSectionCode))
404           return;
405         break;
406       case kExceptionSectionCode:
407         if (!CheckUnorderedSection(section_code)) return;
408         if (!CheckSectionOrder(section_code, kMemorySectionCode,
409                                kGlobalSectionCode))
410           return;
411         break;
412       case kNameSectionCode:
413         // TODO(titzer): report out of place name section as a warning.
414         // Be lenient with placement of name section. All except first
415         // occurrence are ignored.
416       case kSourceMappingURLSectionCode:
417         // sourceMappingURL is a custom section and currently can occur anywhere
418         // in the module. In case of multiple sourceMappingURL sections, all
419         // except the first occurrence are ignored.
420       case kDebugInfoSectionCode:
421         // .debug_info is a custom section containing core DWARF information
422         // if produced by compiler. Its presence likely means that Wasm was
423         // built in a debug mode.
424       case kExternalDebugInfoSectionCode:
425         // external_debug_info is a custom section containing a reference to an
426         // external symbol file.
427       case kCompilationHintsSectionCode:
428         // TODO(frgossen): report out of place compilation hints section as a
429         // warning.
430         // Be lenient with placement of compilation hints section. All except
431         // first occurrence after function section and before code section are
432         // ignored.
433         break;
434       default:
435         next_ordered_section_ = section_code + 1;
436         break;
437     }
438 
439     switch (section_code) {
440       case kUnknownSectionCode:
441         break;
442       case kTypeSectionCode:
443         DecodeTypeSection();
444         break;
445       case kImportSectionCode:
446         DecodeImportSection();
447         break;
448       case kFunctionSectionCode:
449         DecodeFunctionSection();
450         break;
451       case kTableSectionCode:
452         DecodeTableSection();
453         break;
454       case kMemorySectionCode:
455         DecodeMemorySection();
456         break;
457       case kGlobalSectionCode:
458         DecodeGlobalSection();
459         break;
460       case kExportSectionCode:
461         DecodeExportSection();
462         break;
463       case kStartSectionCode:
464         DecodeStartSection();
465         break;
466       case kCodeSectionCode:
467         DecodeCodeSection(verify_functions);
468         break;
469       case kElementSectionCode:
470         DecodeElementSection();
471         break;
472       case kDataSectionCode:
473         DecodeDataSection();
474         break;
475       case kNameSectionCode:
476         DecodeNameSection();
477         break;
478       case kSourceMappingURLSectionCode:
479         DecodeSourceMappingURLSection();
480         break;
481       case kDebugInfoSectionCode:
482         // If there is an explicit source map, prefer it over DWARF info.
483         if (module_->debug_symbols.type == WasmDebugSymbols::Type::None) {
484           module_->debug_symbols = {WasmDebugSymbols::Type::EmbeddedDWARF, {}};
485         }
486         consume_bytes(static_cast<uint32_t>(end_ - start_), ".debug_info");
487         break;
488       case kExternalDebugInfoSectionCode:
489         DecodeExternalDebugInfoSection();
490         break;
491       case kCompilationHintsSectionCode:
492         if (enabled_features_.has_compilation_hints()) {
493           DecodeCompilationHintsSection();
494         } else {
495           // Ignore this section when feature was disabled. It is an optional
496           // custom section anyways.
497           consume_bytes(static_cast<uint32_t>(end_ - start_), nullptr);
498         }
499         break;
500       case kDataCountSectionCode:
501         if (enabled_features_.has_bulk_memory()) {
502           DecodeDataCountSection();
503         } else {
504           errorf(pc(), "unexpected section <%s>", SectionName(section_code));
505         }
506         break;
507       case kExceptionSectionCode:
508         if (enabled_features_.has_eh()) {
509           DecodeExceptionSection();
510         } else {
511           errorf(pc(), "unexpected section <%s>", SectionName(section_code));
512         }
513         break;
514       default:
515         errorf(pc(), "unexpected section <%s>", SectionName(section_code));
516         return;
517     }
518 
519     if (pc() != bytes.end()) {
520       const char* msg = pc() < bytes.end() ? "shorter" : "longer";
521       errorf(pc(),
522              "section was %s than expected size "
523              "(%zu bytes expected, %zu decoded)",
524              msg, bytes.size(), static_cast<size_t>(pc() - bytes.begin()));
525     }
526   }
527 
DecodeTypeSection()528   void DecodeTypeSection() {
529     uint32_t signatures_count = consume_count("types count", kV8MaxWasmTypes);
530     module_->types.reserve(signatures_count);
531     for (uint32_t i = 0; ok() && i < signatures_count; ++i) {
532       TRACE("DecodeSignature[%d] module+%d\n", i,
533             static_cast<int>(pc_ - start_));
534       uint8_t kind = consume_u8("type kind");
535       switch (kind) {
536         case kWasmFunctionTypeCode: {
537           const FunctionSig* s = consume_sig(module_->signature_zone.get());
538           module_->add_signature(s);
539           break;
540         }
541         case kWasmStructTypeCode: {
542           if (!enabled_features_.has_gc()) {
543             errorf(pc(),
544                    "invalid struct type definition, enable with "
545                    "--experimental-wasm-gc");
546             break;
547           }
548           const StructType* s = consume_struct(module_->signature_zone.get());
549           module_->add_struct_type(s);
550           // TODO(7748): Should we canonicalize struct types, like
551           // {signature_map} does for function signatures?
552           break;
553         }
554         case kWasmArrayTypeCode: {
555           if (!enabled_features_.has_gc()) {
556             errorf(pc(),
557                    "invalid array type definition, enable with "
558                    "--experimental-wasm-gc");
559             break;
560           }
561           const ArrayType* type = consume_array(module_->signature_zone.get());
562           module_->add_array_type(type);
563           break;
564         }
565         default:
566           errorf(pc(), "unknown type form: %d", kind);
567           break;
568       }
569     }
570     module_->signature_map.Freeze();
571   }
572 
DecodeImportSection()573   void DecodeImportSection() {
574     uint32_t import_table_count =
575         consume_count("imports count", kV8MaxWasmImports);
576     module_->import_table.reserve(import_table_count);
577     for (uint32_t i = 0; ok() && i < import_table_count; ++i) {
578       TRACE("DecodeImportTable[%d] module+%d\n", i,
579             static_cast<int>(pc_ - start_));
580 
581       module_->import_table.push_back({
582           {0, 0},             // module_name
583           {0, 0},             // field_name
584           kExternalFunction,  // kind
585           0                   // index
586       });
587       WasmImport* import = &module_->import_table.back();
588       const byte* pos = pc_;
589       import->module_name = consume_string(this, true, "module name");
590       import->field_name = consume_string(this, true, "field name");
591       import->kind =
592           static_cast<ImportExportKindCode>(consume_u8("import kind"));
593       switch (import->kind) {
594         case kExternalFunction: {
595           // ===== Imported function ===========================================
596           import->index = static_cast<uint32_t>(module_->functions.size());
597           module_->num_imported_functions++;
598           module_->functions.push_back({nullptr,        // sig
599                                         import->index,  // func_index
600                                         0,              // sig_index
601                                         {0, 0},         // code
602                                         true,           // imported
603                                         false,          // exported
604                                         false});        // declared
605           WasmFunction* function = &module_->functions.back();
606           function->sig_index =
607               consume_sig_index(module_.get(), &function->sig);
608           break;
609         }
610         case kExternalTable: {
611           // ===== Imported table ==============================================
612           if (!AddTable(module_.get())) break;
613           import->index = static_cast<uint32_t>(module_->tables.size());
614           module_->num_imported_tables++;
615           module_->tables.emplace_back();
616           WasmTable* table = &module_->tables.back();
617           table->imported = true;
618           const byte* type_position = pc();
619           ValueType type = consume_reference_type();
620           if (!WasmTable::IsValidTableType(type, module_.get())) {
621             error(type_position,
622                   "Currently, only nullable exnref, externref, and "
623                   "function references are allowed as table types");
624             break;
625           }
626           table->type = type;
627           uint8_t flags = validate_table_flags("element count");
628           consume_resizable_limits(
629               "element count", "elements", std::numeric_limits<uint32_t>::max(),
630               &table->initial_size, &table->has_maximum_size,
631               std::numeric_limits<uint32_t>::max(), &table->maximum_size,
632               flags);
633           break;
634         }
635         case kExternalMemory: {
636           // ===== Imported memory =============================================
637           if (!AddMemory(module_.get())) break;
638           uint8_t flags = validate_memory_flags(&module_->has_shared_memory,
639                                                 &module_->is_memory64);
640           consume_resizable_limits("memory", "pages", max_mem_pages(),
641                                    &module_->initial_pages,
642                                    &module_->has_maximum_pages, max_mem_pages(),
643                                    &module_->maximum_pages, flags);
644           break;
645         }
646         case kExternalGlobal: {
647           // ===== Imported global =============================================
648           import->index = static_cast<uint32_t>(module_->globals.size());
649           module_->globals.push_back(
650               {kWasmStmt, false, WasmInitExpr(), {0}, true, false});
651           WasmGlobal* global = &module_->globals.back();
652           global->type = consume_value_type();
653           global->mutability = consume_mutability();
654           if (global->mutability) {
655             module_->num_imported_mutable_globals++;
656           }
657           break;
658         }
659         case kExternalException: {
660           // ===== Imported exception ==========================================
661           if (!enabled_features_.has_eh()) {
662             errorf(pos, "unknown import kind 0x%02x", import->kind);
663             break;
664           }
665           import->index = static_cast<uint32_t>(module_->exceptions.size());
666           const WasmExceptionSig* exception_sig = nullptr;
667           consume_exception_attribute();  // Attribute ignored for now.
668           consume_exception_sig_index(module_.get(), &exception_sig);
669           module_->exceptions.emplace_back(exception_sig);
670           break;
671         }
672         default:
673           errorf(pos, "unknown import kind 0x%02x", import->kind);
674           break;
675       }
676     }
677   }
678 
DecodeFunctionSection()679   void DecodeFunctionSection() {
680     uint32_t functions_count =
681         consume_count("functions count", kV8MaxWasmFunctions);
682     auto counter =
683         SELECT_WASM_COUNTER(GetCounters(), origin_, wasm_functions_per, module);
684     counter->AddSample(static_cast<int>(functions_count));
685     DCHECK_EQ(module_->functions.size(), module_->num_imported_functions);
686     uint32_t total_function_count =
687         module_->num_imported_functions + functions_count;
688     module_->functions.reserve(total_function_count);
689     module_->num_declared_functions = functions_count;
690     for (uint32_t i = 0; i < functions_count; ++i) {
691       uint32_t func_index = static_cast<uint32_t>(module_->functions.size());
692       module_->functions.push_back({nullptr,     // sig
693                                     func_index,  // func_index
694                                     0,           // sig_index
695                                     {0, 0},      // code
696                                     false,       // imported
697                                     false,       // exported
698                                     false});     // declared
699       WasmFunction* function = &module_->functions.back();
700       function->sig_index = consume_sig_index(module_.get(), &function->sig);
701       if (!ok()) return;
702     }
703     DCHECK_EQ(module_->functions.size(), total_function_count);
704   }
705 
DecodeTableSection()706   void DecodeTableSection() {
707     // TODO(ahaas): Set the correct limit to {kV8MaxWasmTables} once the
708     // implementation of ExternRef landed.
709     uint32_t max_count =
710         enabled_features_.has_reftypes() ? 100000 : kV8MaxWasmTables;
711     uint32_t table_count = consume_count("table count", max_count);
712 
713     for (uint32_t i = 0; ok() && i < table_count; i++) {
714       if (!AddTable(module_.get())) break;
715       module_->tables.emplace_back();
716       WasmTable* table = &module_->tables.back();
717       const byte* type_position = pc();
718       ValueType table_type = consume_reference_type();
719       if (!WasmTable::IsValidTableType(table_type, module_.get())) {
720         error(type_position,
721               "Currently, only nullable exnref, externref, and "
722               "function references are allowed as table types");
723         continue;
724       }
725       table->type = table_type;
726       uint8_t flags = validate_table_flags("table elements");
727       consume_resizable_limits(
728           "table elements", "elements", std::numeric_limits<uint32_t>::max(),
729           &table->initial_size, &table->has_maximum_size,
730           std::numeric_limits<uint32_t>::max(), &table->maximum_size, flags);
731     }
732   }
733 
DecodeMemorySection()734   void DecodeMemorySection() {
735     uint32_t memory_count = consume_count("memory count", kV8MaxWasmMemories);
736 
737     for (uint32_t i = 0; ok() && i < memory_count; i++) {
738       if (!AddMemory(module_.get())) break;
739       uint8_t flags = validate_memory_flags(&module_->has_shared_memory,
740                                             &module_->is_memory64);
741       consume_resizable_limits("memory", "pages", max_mem_pages(),
742                                &module_->initial_pages,
743                                &module_->has_maximum_pages, max_mem_pages(),
744                                &module_->maximum_pages, flags);
745     }
746   }
747 
DecodeGlobalSection()748   void DecodeGlobalSection() {
749     uint32_t globals_count = consume_count("globals count", kV8MaxWasmGlobals);
750     uint32_t imported_globals = static_cast<uint32_t>(module_->globals.size());
751     module_->globals.reserve(imported_globals + globals_count);
752     for (uint32_t i = 0; ok() && i < globals_count; ++i) {
753       TRACE("DecodeGlobal[%d] module+%d\n", i, static_cast<int>(pc_ - start_));
754       // Add an uninitialized global and pass a pointer to it.
755       module_->globals.push_back(
756           {kWasmStmt, false, WasmInitExpr(), {0}, false, false});
757       WasmGlobal* global = &module_->globals.back();
758       global->type = consume_value_type();
759       global->mutability = consume_mutability();
760       global->init =
761           consume_init_expr(module_.get(), global->type, imported_globals + i);
762     }
763     if (ok()) CalculateGlobalOffsets(module_.get());
764   }
765 
DecodeExportSection()766   void DecodeExportSection() {
767     uint32_t export_table_count =
768         consume_count("exports count", kV8MaxWasmExports);
769     module_->export_table.reserve(export_table_count);
770     for (uint32_t i = 0; ok() && i < export_table_count; ++i) {
771       TRACE("DecodeExportTable[%d] module+%d\n", i,
772             static_cast<int>(pc_ - start_));
773 
774       module_->export_table.push_back({
775           {0, 0},             // name
776           kExternalFunction,  // kind
777           0                   // index
778       });
779       WasmExport* exp = &module_->export_table.back();
780 
781       exp->name = consume_string(this, true, "field name");
782 
783       const byte* pos = pc();
784       exp->kind = static_cast<ImportExportKindCode>(consume_u8("export kind"));
785       switch (exp->kind) {
786         case kExternalFunction: {
787           WasmFunction* func = nullptr;
788           exp->index =
789               consume_func_index(module_.get(), &func, "export function index");
790 
791           if (failed()) break;
792           DCHECK_NOT_NULL(func);
793 
794           module_->num_exported_functions++;
795           func->exported = true;
796           // Exported functions are considered "declared".
797           func->declared = true;
798           break;
799         }
800         case kExternalTable: {
801           WasmTable* table = nullptr;
802           exp->index = consume_table_index(module_.get(), &table);
803           if (table) table->exported = true;
804           break;
805         }
806         case kExternalMemory: {
807           uint32_t index = consume_u32v("memory index");
808           // TODO(titzer): This should become more regular
809           // once we support multiple memories.
810           if (!module_->has_memory || index != 0) {
811             error("invalid memory index != 0");
812           }
813           module_->mem_export = true;
814           break;
815         }
816         case kExternalGlobal: {
817           WasmGlobal* global = nullptr;
818           exp->index = consume_global_index(module_.get(), &global);
819           if (global) {
820             global->exported = true;
821           }
822           break;
823         }
824         case kExternalException: {
825           if (!enabled_features_.has_eh()) {
826             errorf(pos, "invalid export kind 0x%02x", exp->kind);
827             break;
828           }
829           WasmException* exception = nullptr;
830           exp->index = consume_exception_index(module_.get(), &exception);
831           break;
832         }
833         default:
834           errorf(pos, "invalid export kind 0x%02x", exp->kind);
835           break;
836       }
837     }
838     // Check for duplicate exports (except for asm.js).
839     if (ok() && origin_ == kWasmOrigin && module_->export_table.size() > 1) {
840       std::vector<WasmExport> sorted_exports(module_->export_table);
841 
842       auto cmp_less = [this](const WasmExport& a, const WasmExport& b) {
843         // Return true if a < b.
844         if (a.name.length() != b.name.length()) {
845           return a.name.length() < b.name.length();
846         }
847         const byte* left = start() + GetBufferRelativeOffset(a.name.offset());
848         const byte* right = start() + GetBufferRelativeOffset(b.name.offset());
849         return memcmp(left, right, a.name.length()) < 0;
850       };
851       std::stable_sort(sorted_exports.begin(), sorted_exports.end(), cmp_less);
852 
853       auto it = sorted_exports.begin();
854       WasmExport* last = &*it++;
855       for (auto end = sorted_exports.end(); it != end; last = &*it++) {
856         DCHECK(!cmp_less(*it, *last));  // Vector must be sorted.
857         if (!cmp_less(*last, *it)) {
858           const byte* pc = start() + GetBufferRelativeOffset(it->name.offset());
859           TruncatedUserString<> name(pc, it->name.length());
860           errorf(pc, "Duplicate export name '%.*s' for %s %d and %s %d",
861                  name.length(), name.start(), ExternalKindName(last->kind),
862                  last->index, ExternalKindName(it->kind), it->index);
863           break;
864         }
865       }
866     }
867   }
868 
DecodeStartSection()869   void DecodeStartSection() {
870     WasmFunction* func;
871     const byte* pos = pc_;
872     module_->start_function_index =
873         consume_func_index(module_.get(), &func, "start function index");
874     if (func &&
875         (func->sig->parameter_count() > 0 || func->sig->return_count() > 0)) {
876       error(pos, "invalid start function: non-zero parameter or return count");
877     }
878   }
879 
DecodeElementSection()880   void DecodeElementSection() {
881     uint32_t element_count =
882         consume_count("element count", FLAG_wasm_max_table_size);
883 
884     for (uint32_t i = 0; ok() && i < element_count; ++i) {
885       const byte* pos = pc();
886 
887       WasmElemSegment::Status status;
888       bool functions_as_elements;
889       uint32_t table_index;
890       WasmInitExpr offset;
891       ValueType type = kWasmBottom;
892       consume_element_segment_header(&status, &functions_as_elements, &type,
893                                      &table_index, &offset);
894       if (failed()) return;
895       DCHECK_NE(type, kWasmBottom);
896 
897       if (status == WasmElemSegment::kStatusActive) {
898         if (table_index >= module_->tables.size()) {
899           errorf(pos, "out of bounds table index %u", table_index);
900           break;
901         }
902         if (!IsSubtypeOf(type, module_->tables[table_index].type,
903                          this->module_.get())) {
904           errorf(pos,
905                  "Invalid element segment. Table %u is not a super-type of %s",
906                  table_index, type.name().c_str());
907           break;
908         }
909       }
910 
911       uint32_t num_elem =
912           consume_count("number of elements", max_table_init_entries());
913       if (status == WasmElemSegment::kStatusActive) {
914         module_->elem_segments.emplace_back(table_index, std::move(offset));
915       } else {
916         module_->elem_segments.emplace_back(
917             status == WasmElemSegment::kStatusDeclarative);
918       }
919 
920       WasmElemSegment* init = &module_->elem_segments.back();
921       init->type = type;
922       for (uint32_t j = 0; j < num_elem; j++) {
923         uint32_t index = functions_as_elements ? consume_element_expr()
924                                                : consume_element_func_index();
925         if (failed()) break;
926         init->entries.push_back(index);
927       }
928     }
929   }
930 
DecodeCodeSection(bool verify_functions)931   void DecodeCodeSection(bool verify_functions) {
932     uint32_t pos = pc_offset();
933     uint32_t functions_count = consume_u32v("functions count");
934     CheckFunctionsCount(functions_count, pos);
935     for (uint32_t i = 0; ok() && i < functions_count; ++i) {
936       const byte* pos = pc();
937       uint32_t size = consume_u32v("body size");
938       if (size > kV8MaxWasmFunctionSize) {
939         errorf(pos, "size %u > maximum function size %zu", size,
940                kV8MaxWasmFunctionSize);
941         return;
942       }
943       uint32_t offset = pc_offset();
944       consume_bytes(size, "function body");
945       if (failed()) break;
946       DecodeFunctionBody(i, size, offset, verify_functions);
947     }
948     DCHECK_GE(pc_offset(), pos);
949     set_code_section(pos, pc_offset() - pos);
950   }
951 
CheckFunctionsCount(uint32_t functions_count,uint32_t offset)952   bool CheckFunctionsCount(uint32_t functions_count, uint32_t offset) {
953     if (functions_count != module_->num_declared_functions) {
954       Reset(nullptr, nullptr, offset);
955       errorf(nullptr, "function body count %u mismatch (%u expected)",
956              functions_count, module_->num_declared_functions);
957       return false;
958     }
959     return true;
960   }
961 
DecodeFunctionBody(uint32_t index,uint32_t length,uint32_t offset,bool verify_functions)962   void DecodeFunctionBody(uint32_t index, uint32_t length, uint32_t offset,
963                           bool verify_functions) {
964     WasmFunction* function =
965         &module_->functions[index + module_->num_imported_functions];
966     function->code = {offset, length};
967     if (verify_functions) {
968       ModuleWireBytes bytes(module_start_, module_end_);
969       VerifyFunctionBody(module_->signature_zone->allocator(),
970                          index + module_->num_imported_functions, bytes,
971                          module_.get(), function);
972     }
973   }
974 
CheckDataSegmentsCount(uint32_t data_segments_count)975   bool CheckDataSegmentsCount(uint32_t data_segments_count) {
976     if (has_seen_unordered_section(kDataCountSectionCode) &&
977         data_segments_count != module_->num_declared_data_segments) {
978       errorf(pc(), "data segments count %u mismatch (%u expected)",
979              data_segments_count, module_->num_declared_data_segments);
980       return false;
981     }
982     return true;
983   }
984 
DecodeDataSection()985   void DecodeDataSection() {
986     uint32_t data_segments_count =
987         consume_count("data segments count", kV8MaxWasmDataSegments);
988     if (!CheckDataSegmentsCount(data_segments_count)) return;
989 
990     module_->data_segments.reserve(data_segments_count);
991     for (uint32_t i = 0; ok() && i < data_segments_count; ++i) {
992       const byte* pos = pc();
993       TRACE("DecodeDataSegment[%d] module+%d\n", i,
994             static_cast<int>(pc_ - start_));
995 
996       bool is_active;
997       uint32_t memory_index;
998       WasmInitExpr dest_addr;
999       consume_data_segment_header(&is_active, &memory_index, &dest_addr);
1000       if (failed()) break;
1001 
1002       if (is_active) {
1003         if (!module_->has_memory) {
1004           error("cannot load data without memory");
1005           break;
1006         }
1007         if (memory_index != 0) {
1008           errorf(pos, "illegal memory index %u != 0", memory_index);
1009           break;
1010         }
1011       }
1012 
1013       uint32_t source_length = consume_u32v("source size");
1014       uint32_t source_offset = pc_offset();
1015 
1016       if (is_active) {
1017         module_->data_segments.emplace_back(std::move(dest_addr));
1018       } else {
1019         module_->data_segments.emplace_back();
1020       }
1021 
1022       WasmDataSegment* segment = &module_->data_segments.back();
1023 
1024       consume_bytes(source_length, "segment data");
1025       if (failed()) break;
1026 
1027       segment->source = {source_offset, source_length};
1028     }
1029   }
1030 
DecodeNameSection()1031   void DecodeNameSection() {
1032     // TODO(titzer): find a way to report name errors as warnings.
1033     // Ignore all but the first occurrence of name section.
1034     if (!has_seen_unordered_section(kNameSectionCode)) {
1035       set_seen_unordered_section(kNameSectionCode);
1036       // Use an inner decoder so that errors don't fail the outer decoder.
1037       Decoder inner(start_, pc_, end_, buffer_offset_);
1038       // Decode all name subsections.
1039       // Be lenient with their order.
1040       while (inner.ok() && inner.more()) {
1041         uint8_t name_type = inner.consume_u8("name type");
1042         if (name_type & 0x80) inner.error("name type if not varuint7");
1043 
1044         uint32_t name_payload_len = inner.consume_u32v("name payload length");
1045         if (!inner.checkAvailable(name_payload_len)) break;
1046 
1047         // Decode module name, ignore the rest.
1048         // Function and local names will be decoded when needed.
1049         if (name_type == NameSectionKindCode::kModule) {
1050           WireBytesRef name = consume_string(&inner, false, "module name");
1051           if (inner.ok() && validate_utf8(&inner, name)) {
1052             module_->name = name;
1053           }
1054         } else {
1055           inner.consume_bytes(name_payload_len, "name subsection payload");
1056         }
1057       }
1058     }
1059     // Skip the whole names section in the outer decoder.
1060     consume_bytes(static_cast<uint32_t>(end_ - start_), nullptr);
1061   }
1062 
DecodeSourceMappingURLSection()1063   void DecodeSourceMappingURLSection() {
1064     Decoder inner(start_, pc_, end_, buffer_offset_);
1065     WireBytesRef url = wasm::consume_string(&inner, true, "module name");
1066     if (inner.ok() &&
1067         module_->debug_symbols.type != WasmDebugSymbols::Type::SourceMap) {
1068       module_->debug_symbols = {WasmDebugSymbols::Type::SourceMap, url};
1069     }
1070     set_seen_unordered_section(kSourceMappingURLSectionCode);
1071     consume_bytes(static_cast<uint32_t>(end_ - start_), nullptr);
1072   }
1073 
DecodeExternalDebugInfoSection()1074   void DecodeExternalDebugInfoSection() {
1075     Decoder inner(start_, pc_, end_, buffer_offset_);
1076     WireBytesRef url =
1077         wasm::consume_string(&inner, true, "external symbol file");
1078     // If there is an explicit source map, prefer it over DWARF info.
1079     if (inner.ok() &&
1080         module_->debug_symbols.type != WasmDebugSymbols::Type::SourceMap) {
1081       module_->debug_symbols = {WasmDebugSymbols::Type::ExternalDWARF, url};
1082       set_seen_unordered_section(kExternalDebugInfoSectionCode);
1083     }
1084     consume_bytes(static_cast<uint32_t>(end_ - start_), nullptr);
1085   }
1086 
DecodeCompilationHintsSection()1087   void DecodeCompilationHintsSection() {
1088     TRACE("DecodeCompilationHints module+%d\n", static_cast<int>(pc_ - start_));
1089 
1090     // TODO(frgossen): Find a way to report compilation hint errors as warnings.
1091     // All except first occurrence after function section and before code
1092     // section are ignored.
1093     const bool before_function_section =
1094         next_ordered_section_ <= kFunctionSectionCode;
1095     const bool after_code_section = next_ordered_section_ > kCodeSectionCode;
1096     if (before_function_section || after_code_section ||
1097         has_seen_unordered_section(kCompilationHintsSectionCode)) {
1098       return;
1099     }
1100     set_seen_unordered_section(kCompilationHintsSectionCode);
1101 
1102     // TODO(frgossen) Propagate errors to outer decoder in experimental phase.
1103     // We should use an inner decoder later and propagate its errors as
1104     // warnings.
1105     Decoder& decoder = *this;
1106     // Decoder decoder(start_, pc_, end_, buffer_offset_);
1107 
1108     // Ensure exactly one compilation hint per function.
1109     uint32_t hint_count = decoder.consume_u32v("compilation hint count");
1110     if (hint_count != module_->num_declared_functions) {
1111       decoder.errorf(decoder.pc(), "Expected %u compilation hints (%u found)",
1112                      module_->num_declared_functions, hint_count);
1113     }
1114 
1115     // Decode sequence of compilation hints.
1116     if (decoder.ok()) {
1117       module_->compilation_hints.reserve(hint_count);
1118     }
1119     for (uint32_t i = 0; decoder.ok() && i < hint_count; i++) {
1120       TRACE("DecodeCompilationHints[%d] module+%d\n", i,
1121             static_cast<int>(pc_ - start_));
1122 
1123       // Compilation hints are encoded in one byte each.
1124       // +-------+----------+---------------+----------+
1125       // | 2 bit | 2 bit    | 2 bit         | 2 bit    |
1126       // | ...   | Top tier | Baseline tier | Strategy |
1127       // +-------+----------+---------------+----------+
1128       uint8_t hint_byte = decoder.consume_u8("compilation hint");
1129       if (!decoder.ok()) break;
1130 
1131       // Decode compilation hint.
1132       WasmCompilationHint hint;
1133       hint.strategy =
1134           static_cast<WasmCompilationHintStrategy>(hint_byte & 0x03);
1135       hint.baseline_tier =
1136           static_cast<WasmCompilationHintTier>(hint_byte >> 2 & 0x3);
1137       hint.top_tier =
1138           static_cast<WasmCompilationHintTier>(hint_byte >> 4 & 0x3);
1139 
1140       // Ensure that the top tier never downgrades a compilation result.
1141       // If baseline and top tier are the same compilation will be invoked only
1142       // once.
1143       if (hint.top_tier < hint.baseline_tier &&
1144           hint.top_tier != WasmCompilationHintTier::kDefault) {
1145         decoder.errorf(decoder.pc(),
1146                        "Invalid compilation hint %#x (forbidden downgrade)",
1147                        hint_byte);
1148       }
1149 
1150       // Happily accept compilation hint.
1151       if (decoder.ok()) {
1152         module_->compilation_hints.push_back(std::move(hint));
1153       }
1154     }
1155 
1156     // If section was invalid reset compilation hints.
1157     if (decoder.failed()) {
1158       module_->compilation_hints.clear();
1159     }
1160 
1161     // @TODO(frgossen) Skip the whole compilation hints section in the outer
1162     // decoder if inner decoder was used.
1163     // consume_bytes(static_cast<uint32_t>(end_ - start_), nullptr);
1164   }
1165 
DecodeDataCountSection()1166   void DecodeDataCountSection() {
1167     module_->num_declared_data_segments =
1168         consume_count("data segments count", kV8MaxWasmDataSegments);
1169   }
1170 
DecodeExceptionSection()1171   void DecodeExceptionSection() {
1172     uint32_t exception_count =
1173         consume_count("exception count", kV8MaxWasmExceptions);
1174     for (uint32_t i = 0; ok() && i < exception_count; ++i) {
1175       TRACE("DecodeException[%d] module+%d\n", i,
1176             static_cast<int>(pc_ - start_));
1177       const WasmExceptionSig* exception_sig = nullptr;
1178       consume_exception_attribute();  // Attribute ignored for now.
1179       consume_exception_sig_index(module_.get(), &exception_sig);
1180       module_->exceptions.emplace_back(exception_sig);
1181     }
1182   }
1183 
CheckMismatchedCounts()1184   bool CheckMismatchedCounts() {
1185     // The declared vs. defined function count is normally checked when
1186     // decoding the code section, but we have to check it here too in case the
1187     // code section is absent.
1188     if (module_->num_declared_functions != 0) {
1189       DCHECK_LT(module_->num_imported_functions, module_->functions.size());
1190       // We know that the code section has been decoded if the first
1191       // non-imported function has its code set.
1192       if (!module_->functions[module_->num_imported_functions].code.is_set()) {
1193         errorf(pc(), "function count is %u, but code section is absent",
1194                module_->num_declared_functions);
1195         return false;
1196       }
1197     }
1198     // Perform a similar check for the DataCount and Data sections, where data
1199     // segments are declared but the Data section is absent.
1200     if (!CheckDataSegmentsCount(
1201             static_cast<uint32_t>(module_->data_segments.size()))) {
1202       return false;
1203     }
1204     return true;
1205   }
1206 
FinishDecoding(bool verify_functions=true)1207   ModuleResult FinishDecoding(bool verify_functions = true) {
1208     if (ok() && CheckMismatchedCounts()) {
1209       CalculateGlobalOffsets(module_.get());
1210     }
1211 
1212     ModuleResult result = toResult(std::move(module_));
1213     if (verify_functions && result.ok() && intermediate_error_.has_error()) {
1214       // Copy error message and location.
1215       return ModuleResult{std::move(intermediate_error_)};
1216     }
1217     return result;
1218   }
1219 
set_code_section(uint32_t offset,uint32_t size)1220   void set_code_section(uint32_t offset, uint32_t size) {
1221     module_->code = {offset, size};
1222   }
1223 
1224   // Decodes an entire module.
DecodeModule(Counters * counters,AccountingAllocator * allocator,bool verify_functions=true)1225   ModuleResult DecodeModule(Counters* counters, AccountingAllocator* allocator,
1226                             bool verify_functions = true) {
1227     StartDecoding(counters, allocator);
1228     uint32_t offset = 0;
1229     Vector<const byte> orig_bytes(start(), end() - start());
1230     DecodeModuleHeader(VectorOf(start(), end() - start()), offset);
1231     if (failed()) {
1232       return FinishDecoding(verify_functions);
1233     }
1234     // Size of the module header.
1235     offset += 8;
1236     Decoder decoder(start_ + offset, end_, offset);
1237 
1238     WasmSectionIterator section_iter(&decoder);
1239 
1240     while (ok() && section_iter.more()) {
1241       // Shift the offset by the section header length
1242       offset += section_iter.payload_start() - section_iter.section_start();
1243       if (section_iter.section_code() != SectionCode::kUnknownSectionCode) {
1244         DecodeSection(section_iter.section_code(), section_iter.payload(),
1245                       offset, verify_functions);
1246       }
1247       // Shift the offset by the remaining section payload
1248       offset += section_iter.payload_length();
1249       section_iter.advance(true);
1250     }
1251 
1252     if (FLAG_dump_wasm_module) DumpModule(orig_bytes);
1253 
1254     if (decoder.failed()) {
1255       return decoder.toResult<std::unique_ptr<WasmModule>>(nullptr);
1256     }
1257 
1258     return FinishDecoding(verify_functions);
1259   }
1260 
1261   // Decodes a single anonymous function starting at {start_}.
DecodeSingleFunction(Zone * zone,const ModuleWireBytes & wire_bytes,const WasmModule * module,std::unique_ptr<WasmFunction> function)1262   FunctionResult DecodeSingleFunction(Zone* zone,
1263                                       const ModuleWireBytes& wire_bytes,
1264                                       const WasmModule* module,
1265                                       std::unique_ptr<WasmFunction> function) {
1266     pc_ = start_;
1267     expect_u8("type form", kWasmFunctionTypeCode);
1268     if (!ok()) return FunctionResult{std::move(intermediate_error_)};
1269     function->sig = consume_sig(zone);
1270     function->code = {off(pc_), static_cast<uint32_t>(end_ - pc_)};
1271 
1272     if (ok())
1273       VerifyFunctionBody(zone->allocator(), 0, wire_bytes, module,
1274                          function.get());
1275 
1276     if (intermediate_error_.has_error()) {
1277       return FunctionResult{std::move(intermediate_error_)};
1278     }
1279 
1280     return FunctionResult(std::move(function));
1281   }
1282 
1283   // Decodes a single function signature at {start}.
DecodeFunctionSignature(Zone * zone,const byte * start)1284   const FunctionSig* DecodeFunctionSignature(Zone* zone, const byte* start) {
1285     pc_ = start;
1286     if (!expect_u8("type form", kWasmFunctionTypeCode)) return nullptr;
1287     const FunctionSig* result = consume_sig(zone);
1288     return ok() ? result : nullptr;
1289   }
1290 
DecodeInitExprForTesting()1291   WasmInitExpr DecodeInitExprForTesting() {
1292     return consume_init_expr(nullptr, kWasmStmt, 0);
1293   }
1294 
shared_module() const1295   const std::shared_ptr<WasmModule>& shared_module() const { return module_; }
1296 
GetCounters() const1297   Counters* GetCounters() const {
1298     DCHECK_NOT_NULL(counters_);
1299     return counters_;
1300   }
1301 
SetCounters(Counters * counters)1302   void SetCounters(Counters* counters) {
1303     DCHECK_NULL(counters_);
1304     counters_ = counters;
1305   }
1306 
1307  private:
1308   const WasmFeatures enabled_features_;
1309   std::shared_ptr<WasmModule> module_;
1310   const byte* module_start_ = nullptr;
1311   const byte* module_end_ = nullptr;
1312   Counters* counters_ = nullptr;
1313   // The type section is the first section in a module.
1314   uint8_t next_ordered_section_ = kFirstSectionInModule;
1315   // We store next_ordered_section_ as uint8_t instead of SectionCode so that
1316   // we can increment it. This static_assert should make sure that SectionCode
1317   // does not get bigger than uint8_t accidentially.
1318   static_assert(sizeof(ModuleDecoderImpl::next_ordered_section_) ==
1319                     sizeof(SectionCode),
1320                 "type mismatch");
1321   uint32_t seen_unordered_sections_ = 0;
1322   static_assert(kBitsPerByte *
1323                         sizeof(ModuleDecoderImpl::seen_unordered_sections_) >
1324                     kLastKnownModuleSection,
1325                 "not enough bits");
1326   WasmError intermediate_error_;
1327   // Set of type offsets discovered in field types during type section decoding.
1328   // Since struct types may be recursive, this is used for checking and error
1329   // reporting once the whole type section is parsed.
1330   std::unordered_map<uint32_t, int> deferred_check_type_index_;
1331   ModuleOrigin origin_;
1332 
TypeOf(const WasmInitExpr & expr)1333   ValueType TypeOf(const WasmInitExpr& expr) {
1334     switch (expr.kind()) {
1335       case WasmInitExpr::kNone:
1336         return kWasmStmt;
1337       case WasmInitExpr::kGlobalGet:
1338         return expr.immediate().index < module_->globals.size()
1339                    ? module_->globals[expr.immediate().index].type
1340                    : kWasmStmt;
1341       case WasmInitExpr::kI32Const:
1342         return kWasmI32;
1343       case WasmInitExpr::kI64Const:
1344         return kWasmI64;
1345       case WasmInitExpr::kF32Const:
1346         return kWasmF32;
1347       case WasmInitExpr::kF64Const:
1348         return kWasmF64;
1349       case WasmInitExpr::kS128Const:
1350         return kWasmS128;
1351       case WasmInitExpr::kRefFuncConst: {
1352         uint32_t heap_type =
1353             enabled_features_.has_typed_funcref()
1354                 ? module_->functions[expr.immediate().index].sig_index
1355                 : HeapType::kFunc;
1356         return ValueType::Ref(heap_type, kNonNullable);
1357       }
1358       case WasmInitExpr::kRefNullConst:
1359         return ValueType::Ref(expr.immediate().heap_type, kNullable);
1360       case WasmInitExpr::kRttCanon:
1361         // TODO(7748): If heaptype is "anyref" (not introduced yet),
1362         // then this should be uint8_t{0}.
1363         return ValueType::Rtt(expr.immediate().heap_type, uint8_t{1});
1364       case WasmInitExpr::kRttSub: {
1365         ValueType operand_type = TypeOf(*expr.operand());
1366         if (operand_type.is_rtt()) {
1367           return ValueType::Rtt(expr.immediate().heap_type,
1368                                 operand_type.depth() + 1);
1369         } else {
1370           return kWasmStmt;
1371         }
1372       }
1373     }
1374   }
1375 
has_seen_unordered_section(SectionCode section_code)1376   bool has_seen_unordered_section(SectionCode section_code) {
1377     return seen_unordered_sections_ & (1 << section_code);
1378   }
1379 
set_seen_unordered_section(SectionCode section_code)1380   void set_seen_unordered_section(SectionCode section_code) {
1381     seen_unordered_sections_ |= 1 << section_code;
1382   }
1383 
off(const byte * ptr)1384   uint32_t off(const byte* ptr) {
1385     return static_cast<uint32_t>(ptr - start_) + buffer_offset_;
1386   }
1387 
AddTable(WasmModule * module)1388   bool AddTable(WasmModule* module) {
1389     if (enabled_features_.has_reftypes()) return true;
1390     if (module->tables.size() > 0) {
1391       error("At most one table is supported");
1392       return false;
1393     } else {
1394       return true;
1395     }
1396   }
1397 
AddMemory(WasmModule * module)1398   bool AddMemory(WasmModule* module) {
1399     if (module->has_memory) {
1400       error("At most one memory is supported");
1401       return false;
1402     } else {
1403       module->has_memory = true;
1404       return true;
1405     }
1406   }
1407 
1408   // Calculate individual global offsets and total size of globals table.
CalculateGlobalOffsets(WasmModule * module)1409   void CalculateGlobalOffsets(WasmModule* module) {
1410     uint32_t untagged_offset = 0;
1411     uint32_t tagged_offset = 0;
1412     uint32_t num_imported_mutable_globals = 0;
1413     for (WasmGlobal& global : module->globals) {
1414       if (global.mutability && global.imported) {
1415         global.index = num_imported_mutable_globals++;
1416       } else if (global.type.is_reference_type()) {
1417         global.offset = tagged_offset;
1418         // All entries in the tagged_globals_buffer have size 1.
1419         tagged_offset++;
1420       } else {
1421         int size = global.type.element_size_bytes();
1422         untagged_offset = (untagged_offset + size - 1) & ~(size - 1);  // align
1423         global.offset = untagged_offset;
1424         untagged_offset += size;
1425       }
1426     }
1427     module->untagged_globals_buffer_size = untagged_offset;
1428     module->tagged_globals_buffer_size = tagged_offset;
1429   }
1430 
1431   // Verifies the body (code) of a given function.
VerifyFunctionBody(AccountingAllocator * allocator,uint32_t func_num,const ModuleWireBytes & wire_bytes,const WasmModule * module,WasmFunction * function)1432   void VerifyFunctionBody(AccountingAllocator* allocator, uint32_t func_num,
1433                           const ModuleWireBytes& wire_bytes,
1434                           const WasmModule* module, WasmFunction* function) {
1435     WasmFunctionName func_name(function,
1436                                wire_bytes.GetNameOrNull(function, module));
1437     if (FLAG_trace_wasm_decoder) {
1438       StdoutStream{} << "Verifying wasm function " << func_name << std::endl;
1439     }
1440     FunctionBody body = {
1441         function->sig, function->code.offset(),
1442         start_ + GetBufferRelativeOffset(function->code.offset()),
1443         start_ + GetBufferRelativeOffset(function->code.end_offset())};
1444 
1445     WasmFeatures unused_detected_features = WasmFeatures::None();
1446     DecodeResult result = VerifyWasmCode(allocator, enabled_features_, module,
1447                                          &unused_detected_features, body);
1448 
1449     // If the decode failed and this is the first error, set error code and
1450     // location.
1451     if (result.failed() && intermediate_error_.empty()) {
1452       // Wrap the error message from the function decoder.
1453       std::ostringstream error_msg;
1454       error_msg << "in function " << func_name << ": "
1455                 << result.error().message();
1456       intermediate_error_ = WasmError{result.error().offset(), error_msg.str()};
1457     }
1458   }
1459 
consume_sig_index(WasmModule * module,const FunctionSig ** sig)1460   uint32_t consume_sig_index(WasmModule* module, const FunctionSig** sig) {
1461     const byte* pos = pc_;
1462     uint32_t sig_index = consume_u32v("signature index");
1463     if (!module->has_signature(sig_index)) {
1464       errorf(pos, "signature index %u out of bounds (%d signatures)", sig_index,
1465              static_cast<int>(module->types.size()));
1466       *sig = nullptr;
1467       return 0;
1468     }
1469     *sig = module->signature(sig_index);
1470     return sig_index;
1471   }
1472 
consume_exception_sig_index(WasmModule * module,const FunctionSig ** sig)1473   uint32_t consume_exception_sig_index(WasmModule* module,
1474                                        const FunctionSig** sig) {
1475     const byte* pos = pc_;
1476     uint32_t sig_index = consume_sig_index(module, sig);
1477     if (*sig && (*sig)->return_count() != 0) {
1478       errorf(pos, "exception signature %u has non-void return", sig_index);
1479       *sig = nullptr;
1480       return 0;
1481     }
1482     return sig_index;
1483   }
1484 
consume_count(const char * name,size_t maximum)1485   uint32_t consume_count(const char* name, size_t maximum) {
1486     const byte* p = pc_;
1487     uint32_t count = consume_u32v(name);
1488     if (count > maximum) {
1489       errorf(p, "%s of %u exceeds internal limit of %zu", name, count, maximum);
1490       return static_cast<uint32_t>(maximum);
1491     }
1492     return count;
1493   }
1494 
consume_func_index(WasmModule * module,WasmFunction ** func,const char * name)1495   uint32_t consume_func_index(WasmModule* module, WasmFunction** func,
1496                               const char* name) {
1497     return consume_index(name, &module->functions, func);
1498   }
1499 
consume_global_index(WasmModule * module,WasmGlobal ** global)1500   uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) {
1501     return consume_index("global index", &module->globals, global);
1502   }
1503 
consume_table_index(WasmModule * module,WasmTable ** table)1504   uint32_t consume_table_index(WasmModule* module, WasmTable** table) {
1505     return consume_index("table index", &module->tables, table);
1506   }
1507 
consume_exception_index(WasmModule * module,WasmException ** except)1508   uint32_t consume_exception_index(WasmModule* module, WasmException** except) {
1509     return consume_index("exception index", &module->exceptions, except);
1510   }
1511 
1512   template <typename T>
consume_index(const char * name,std::vector<T> * vector,T ** ptr)1513   uint32_t consume_index(const char* name, std::vector<T>* vector, T** ptr) {
1514     const byte* pos = pc_;
1515     uint32_t index = consume_u32v(name);
1516     if (index >= vector->size()) {
1517       errorf(pos, "%s %u out of bounds (%d entr%s)", name, index,
1518              static_cast<int>(vector->size()),
1519              vector->size() == 1 ? "y" : "ies");
1520       *ptr = nullptr;
1521       return 0;
1522     }
1523     *ptr = &(*vector)[index];
1524     return index;
1525   }
1526 
validate_table_flags(const char * name)1527   uint8_t validate_table_flags(const char* name) {
1528     uint8_t flags = consume_u8("table limits flags");
1529     STATIC_ASSERT(kNoMaximum < kWithMaximum);
1530     if (V8_UNLIKELY(flags > kWithMaximum)) {
1531       errorf(pc() - 1, "invalid %s limits flags", name);
1532     }
1533     return flags;
1534   }
1535 
validate_memory_flags(bool * has_shared_memory,bool * is_memory64)1536   uint8_t validate_memory_flags(bool* has_shared_memory, bool* is_memory64) {
1537     uint8_t flags = consume_u8("memory limits flags");
1538     *has_shared_memory = false;
1539     switch (flags) {
1540       case kNoMaximum:
1541       case kWithMaximum:
1542         break;
1543       case kSharedNoMaximum:
1544       case kSharedWithMaximum:
1545         if (!enabled_features_.has_threads()) {
1546           errorf(pc() - 1,
1547                  "invalid memory limits flags 0x%x (enable via "
1548                  "--experimental-wasm-threads)",
1549                  flags);
1550         }
1551         *has_shared_memory = true;
1552         // V8 does not support shared memory without a maximum.
1553         if (flags == kSharedNoMaximum) {
1554           errorf(pc() - 1,
1555                  "memory limits flags must have maximum defined if shared is "
1556                  "true");
1557         }
1558         break;
1559       case kMemory64NoMaximum:
1560       case kMemory64WithMaximum:
1561         if (!enabled_features_.has_memory64()) {
1562           errorf(pc() - 1,
1563                  "invalid memory limits flags 0x%x (enable via "
1564                  "--experimental-wasm-memory64)",
1565                  flags);
1566         }
1567         *is_memory64 = true;
1568         break;
1569       default:
1570         errorf(pc() - 1, "invalid memory limits flags 0x%x", flags);
1571         break;
1572     }
1573     return flags;
1574   }
1575 
consume_resizable_limits(const char * name,const char * units,uint32_t max_initial,uint32_t * initial,bool * has_max,uint32_t max_maximum,uint32_t * maximum,uint8_t flags)1576   void consume_resizable_limits(const char* name, const char* units,
1577                                 uint32_t max_initial, uint32_t* initial,
1578                                 bool* has_max, uint32_t max_maximum,
1579                                 uint32_t* maximum, uint8_t flags) {
1580     const byte* pos = pc();
1581     // For memory64 we need to read the numbers as LEB-encoded 64-bit unsigned
1582     // integer. All V8 limits are still within uint32_t range though.
1583     const bool is_memory64 =
1584         flags == kMemory64NoMaximum || flags == kMemory64WithMaximum;
1585     uint64_t initial_64 = is_memory64 ? consume_u64v("initial size")
1586                                       : consume_u32v("initial size");
1587     if (initial_64 > max_initial) {
1588       errorf(pos,
1589              "initial %s size (%" PRIu64
1590              " %s) is larger than implementation limit (%u)",
1591              name, initial_64, units, max_initial);
1592     }
1593     *initial = static_cast<uint32_t>(initial_64);
1594     if (flags & 1) {
1595       *has_max = true;
1596       pos = pc();
1597       uint64_t maximum_64 = is_memory64 ? consume_u64v("maximum size")
1598                                         : consume_u32v("maximum size");
1599       if (maximum_64 > max_maximum) {
1600         errorf(pos,
1601                "maximum %s size (%" PRIu64
1602                " %s) is larger than implementation limit (%u)",
1603                name, maximum_64, units, max_maximum);
1604       }
1605       if (maximum_64 < *initial) {
1606         errorf(pos,
1607                "maximum %s size (%" PRIu64 " %s) is less than initial (%u %s)",
1608                name, maximum_64, units, *initial, units);
1609       }
1610       *maximum = static_cast<uint32_t>(maximum_64);
1611     } else {
1612       *has_max = false;
1613       *maximum = max_initial;
1614     }
1615   }
1616 
expect_u8(const char * name,uint8_t expected)1617   bool expect_u8(const char* name, uint8_t expected) {
1618     const byte* pos = pc();
1619     uint8_t value = consume_u8(name);
1620     if (value != expected) {
1621       errorf(pos, "expected %s 0x%02x, got 0x%02x", name, expected, value);
1622       return false;
1623     }
1624     return true;
1625   }
1626 
1627   // TODO(manoskouk): This is copy-modified from function-body-decoder-impl.h.
1628   // We should find a way to share this code.
Validate(const byte * pc,HeapTypeImmediate<kFullValidation> & imm)1629   V8_INLINE bool Validate(const byte* pc,
1630                           HeapTypeImmediate<kFullValidation>& imm) {
1631     if (V8_UNLIKELY(imm.type.is_bottom())) {
1632       error(pc, "invalid heap type");
1633       return false;
1634     }
1635     if (V8_UNLIKELY(!(imm.type.is_generic() ||
1636                       module_->has_type(imm.type.ref_index())))) {
1637       errorf(pc, "Type index %u is out of bounds", imm.type.ref_index());
1638       return false;
1639     }
1640     return true;
1641   }
1642 
consume_init_expr(WasmModule * module,ValueType expected,size_t current_global_index)1643   WasmInitExpr consume_init_expr(WasmModule* module, ValueType expected,
1644                                  size_t current_global_index) {
1645     constexpr Decoder::ValidateFlag validate = Decoder::kFullValidation;
1646     WasmOpcode opcode = kExprNop;
1647     std::vector<WasmInitExpr> stack;
1648     while (pc() < end() && opcode != kExprEnd) {
1649       uint32_t len = 1;
1650       opcode = static_cast<WasmOpcode>(read_u8<validate>(pc(), "opcode"));
1651       switch (opcode) {
1652         case kExprGlobalGet: {
1653           GlobalIndexImmediate<validate> imm(this, pc() + 1);
1654           len = 1 + imm.length;
1655           // We use 'capacity' over 'size' because we might be
1656           // mid-DecodeGlobalSection().
1657           if (V8_UNLIKELY(imm.index >= module->globals.capacity())) {
1658             error(pc() + 1, "global index is out of bounds");
1659             return {};
1660           }
1661           if (V8_UNLIKELY(imm.index >= current_global_index)) {
1662             errorf(pc() + 1, "global #%u is not defined yet", imm.index);
1663             return {};
1664           }
1665           WasmGlobal* global = &module->globals[imm.index];
1666           if (V8_UNLIKELY(global->mutability)) {
1667             error(pc() + 1,
1668                   "mutable globals cannot be used in initializer "
1669                   "expressions");
1670             return {};
1671           }
1672           if (V8_UNLIKELY(!global->imported && !enabled_features_.has_gc())) {
1673             error(pc() + 1,
1674                   "non-imported globals cannot be used in initializer "
1675                   "expressions");
1676             return {};
1677           }
1678           stack.push_back(WasmInitExpr::GlobalGet(imm.index));
1679           break;
1680         }
1681         case kExprI32Const: {
1682           ImmI32Immediate<Decoder::kFullValidation> imm(this, pc() + 1);
1683           stack.emplace_back(imm.value);
1684           len = 1 + imm.length;
1685           break;
1686         }
1687         case kExprF32Const: {
1688           ImmF32Immediate<Decoder::kFullValidation> imm(this, pc() + 1);
1689           stack.emplace_back(imm.value);
1690           len = 1 + imm.length;
1691           break;
1692         }
1693         case kExprI64Const: {
1694           ImmI64Immediate<Decoder::kFullValidation> imm(this, pc() + 1);
1695           stack.emplace_back(imm.value);
1696           len = 1 + imm.length;
1697           break;
1698         }
1699         case kExprF64Const: {
1700           ImmF64Immediate<Decoder::kFullValidation> imm(this, pc() + 1);
1701           stack.emplace_back(imm.value);
1702           len = 1 + imm.length;
1703           break;
1704         }
1705         case kExprRefNull: {
1706           if (V8_UNLIKELY(!enabled_features_.has_reftypes() &&
1707                           !enabled_features_.has_eh())) {
1708             errorf(pc(),
1709                    "invalid opcode 0x%x in global initializer, enable with "
1710                    "--experimental-wasm-reftypes or --experimental-wasm-eh",
1711                    kExprRefNull);
1712             return {};
1713           }
1714           HeapTypeImmediate<Decoder::kFullValidation> imm(enabled_features_,
1715                                                           this, pc() + 1);
1716           len = 1 + imm.length;
1717           if (!Validate(pc() + 1, imm)) return {};
1718           stack.push_back(
1719               WasmInitExpr::RefNullConst(imm.type.representation()));
1720           break;
1721         }
1722         case kExprRefFunc: {
1723           if (V8_UNLIKELY(!enabled_features_.has_reftypes())) {
1724             errorf(pc(),
1725                    "invalid opcode 0x%x in global initializer, enable with "
1726                    "--experimental-wasm-reftypes",
1727                    kExprRefFunc);
1728             return {};
1729           }
1730 
1731           FunctionIndexImmediate<Decoder::kFullValidation> imm(this, pc() + 1);
1732           len = 1 + imm.length;
1733           if (V8_UNLIKELY(module->functions.size() <= imm.index)) {
1734             errorf(pc(), "invalid function index: %u", imm.index);
1735             return {};
1736           }
1737           stack.push_back(WasmInitExpr::RefFuncConst(imm.index));
1738           // Functions referenced in the globals section count as "declared".
1739           module->functions[imm.index].declared = true;
1740           break;
1741         }
1742         case kSimdPrefix: {
1743           // No need to check for Simd in enabled_features_ here; we either
1744           // failed to validate the global's type earlier, or will fail in
1745           // the type check or stack height check at the end.
1746           opcode = read_prefixed_opcode<validate>(pc(), &len);
1747           if (V8_UNLIKELY(opcode != kExprS128Const)) {
1748             errorf(pc(), "invalid SIMD opcode 0x%x in global initializer",
1749                    opcode);
1750             return {};
1751           }
1752 
1753           Simd128Immediate<validate> imm(this, pc() + len);
1754           len += kSimd128Size;
1755           stack.emplace_back(imm.value);
1756           break;
1757         }
1758         case kGCPrefix: {
1759           // No need to check for GC in enabled_features_ here; we either
1760           // failed to validate the global's type earlier, or will fail in
1761           // the type check or stack height check at the end.
1762           opcode = read_prefixed_opcode<validate>(pc(), &len);
1763           switch (opcode) {
1764             case kExprRttCanon: {
1765               HeapTypeImmediate<validate> imm(enabled_features_, this,
1766                                               pc() + 2);
1767               len += imm.length;
1768               if (!Validate(pc() + len, imm)) return {};
1769               stack.push_back(
1770                   WasmInitExpr::RttCanon(imm.type.representation()));
1771               break;
1772             }
1773             case kExprRttSub: {
1774               HeapTypeImmediate<validate> imm(enabled_features_, this,
1775                                               pc() + 2);
1776               len += imm.length;
1777               if (!Validate(pc() + len, imm)) return {};
1778               if (stack.empty()) {
1779                 error(pc(), "calling rtt.sub without arguments");
1780                 return {};
1781               }
1782               WasmInitExpr parent = std::move(stack.back());
1783               stack.pop_back();
1784               ValueType parent_type = TypeOf(parent);
1785               if (V8_UNLIKELY(
1786                       parent_type.kind() != ValueType::kRtt ||
1787                       !IsSubtypeOf(
1788                           ValueType::Ref(imm.type, kNonNullable),
1789                           ValueType::Ref(parent_type.heap_type(), kNonNullable),
1790                           module_.get()))) {
1791                 error(pc(), "rtt.sub requires a supertype rtt on stack");
1792                 return {};
1793               }
1794               stack.push_back(WasmInitExpr::RttSub(imm.type.representation(),
1795                                                    std::move(parent)));
1796               break;
1797             }
1798             default: {
1799               errorf(pc(), "invalid opcode 0x%x in global initializer", opcode);
1800               return {};
1801             }
1802           }
1803           break;  // case kGCPrefix
1804         }
1805         case kExprEnd:
1806           break;
1807         default: {
1808           errorf(pc(), "invalid opcode 0x%x in global initializer", opcode);
1809           return {};
1810         }
1811       }
1812       pc_ += len;
1813     }
1814 
1815     if (V8_UNLIKELY(pc() > end())) {
1816       error(end(), "Global initializer extending beyond code end");
1817       return {};
1818     }
1819     if (V8_UNLIKELY(opcode != kExprEnd)) {
1820       error(pc(), "Global initializer is missing 'end'");
1821       return {};
1822     }
1823     if (V8_UNLIKELY(stack.size() != 1)) {
1824       errorf(pc(),
1825              "Found 'end' in global initalizer, but %s expressions were "
1826              "found on the stack",
1827              stack.size() > 1 ? "more than one" : "no");
1828       return {};
1829     }
1830 
1831     WasmInitExpr expr = std::move(stack.back());
1832     if (expected != kWasmStmt && !IsSubtypeOf(TypeOf(expr), expected, module)) {
1833       errorf(pc(), "type error in init expression, expected %s, got %s",
1834              expected.name().c_str(), TypeOf(expr).name().c_str());
1835     }
1836     return expr;
1837   }
1838 
1839   // Read a mutability flag
consume_mutability()1840   bool consume_mutability() {
1841     byte val = consume_u8("mutability");
1842     if (val > 1) error(pc_ - 1, "invalid mutability");
1843     return val != 0;
1844   }
1845 
consume_value_type()1846   ValueType consume_value_type() {
1847     uint32_t type_length;
1848     ValueType result = value_type_reader::read_value_type<kFullValidation>(
1849         this, this->pc(), &type_length,
1850         origin_ == kWasmOrigin ? enabled_features_ : WasmFeatures::None());
1851     if (result == kWasmBottom) error(pc_, "invalid value type");
1852     // We use capacity() over size() so this function works
1853     // mid-DecodeTypeSection.
1854     if (result.has_index() && result.ref_index() >= module_->types.capacity()) {
1855       errorf(pc(), "Type index %u is out of bounds", result.ref_index());
1856     }
1857     consume_bytes(type_length, "value type");
1858     return result;
1859   }
1860 
consume_storage_type()1861   ValueType consume_storage_type() {
1862     uint8_t opcode = read_u8<kFullValidation>(this->pc());
1863     switch (opcode) {
1864       case kI8Code:
1865         consume_bytes(1, "i8");
1866         return kWasmI8;
1867       case kI16Code:
1868         consume_bytes(1, "i16");
1869         return kWasmI16;
1870       default:
1871         // It is not a packed type, so it has to be a value type.
1872         return consume_value_type();
1873     }
1874   }
1875 
1876   // Reads a reference type for tables and element segment headers.
1877   // Unless extensions are enabled, only funcref is allowed.
1878   // TODO(manoskouk): Replace this with consume_value_type (and checks against
1879   //                  the returned type at callsites as needed) once the
1880   //                  'reftypes' proposal is standardized.
consume_reference_type()1881   ValueType consume_reference_type() {
1882     if (!enabled_features_.has_reftypes()) {
1883       uint8_t ref_type = consume_u8("reference type");
1884       if (ref_type != kFuncRefCode) {
1885         error(pc_ - 1,
1886               "invalid table type. Consider using experimental flags.");
1887         return kWasmBottom;
1888       }
1889       return kWasmFuncRef;
1890     } else {
1891       const byte* position = pc();
1892       ValueType result = consume_value_type();
1893       if (!result.is_reference_type()) {
1894         error(position, "expected reference type");
1895       }
1896       return result;
1897     }
1898   }
1899 
consume_sig(Zone * zone)1900   const FunctionSig* consume_sig(Zone* zone) {
1901     // Parse parameter types.
1902     uint32_t param_count =
1903         consume_count("param count", kV8MaxWasmFunctionParams);
1904     if (failed()) return nullptr;
1905     std::vector<ValueType> params;
1906     for (uint32_t i = 0; ok() && i < param_count; ++i) {
1907       params.push_back(consume_value_type());
1908     }
1909     std::vector<ValueType> returns;
1910 
1911     // Parse return types.
1912     const size_t max_return_count = enabled_features_.has_mv()
1913                                         ? kV8MaxWasmFunctionMultiReturns
1914                                         : kV8MaxWasmFunctionReturns;
1915     uint32_t return_count = consume_count("return count", max_return_count);
1916     if (failed()) return nullptr;
1917     for (uint32_t i = 0; ok() && i < return_count; ++i) {
1918       returns.push_back(consume_value_type());
1919     }
1920     if (failed()) return nullptr;
1921 
1922     // FunctionSig stores the return types first.
1923     ValueType* buffer = zone->NewArray<ValueType>(param_count + return_count);
1924     uint32_t b = 0;
1925     for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i];
1926     for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i];
1927 
1928     return zone->New<FunctionSig>(return_count, param_count, buffer);
1929   }
1930 
consume_struct(Zone * zone)1931   const StructType* consume_struct(Zone* zone) {
1932     uint32_t field_count = consume_count("field count", kV8MaxWasmStructFields);
1933     if (failed()) return nullptr;
1934     ValueType* fields = zone->NewArray<ValueType>(field_count);
1935     bool* mutabilities = zone->NewArray<bool>(field_count);
1936     for (uint32_t i = 0; ok() && i < field_count; ++i) {
1937       ValueType field = consume_storage_type();
1938       fields[i] = field;
1939       bool mutability = consume_mutability();
1940       mutabilities[i] = mutability;
1941     }
1942     if (failed()) return nullptr;
1943     uint32_t* offsets = zone->NewArray<uint32_t>(field_count);
1944     return zone->New<StructType>(field_count, offsets, fields, mutabilities);
1945   }
1946 
consume_array(Zone * zone)1947   const ArrayType* consume_array(Zone* zone) {
1948     ValueType field = consume_storage_type();
1949     if (failed()) return nullptr;
1950     bool mutability = consume_mutability();
1951     if (!mutability) {
1952       error(this->pc() - 1, "immutable arrays are not supported yet");
1953     }
1954     return zone->New<ArrayType>(field, mutability);
1955   }
1956 
1957   // Consume the attribute field of an exception.
consume_exception_attribute()1958   uint32_t consume_exception_attribute() {
1959     const byte* pos = pc_;
1960     uint32_t attribute = consume_u32v("exception attribute");
1961     if (attribute != kExceptionAttribute) {
1962       errorf(pos, "exception attribute %u not supported", attribute);
1963       return 0;
1964     }
1965     return attribute;
1966   }
1967 
consume_element_segment_header(WasmElemSegment::Status * status,bool * functions_as_elements,ValueType * type,uint32_t * table_index,WasmInitExpr * offset)1968   void consume_element_segment_header(WasmElemSegment::Status* status,
1969                                       bool* functions_as_elements,
1970                                       ValueType* type, uint32_t* table_index,
1971                                       WasmInitExpr* offset) {
1972     const byte* pos = pc();
1973     uint32_t flag;
1974     if (enabled_features_.has_bulk_memory() ||
1975         enabled_features_.has_reftypes()) {
1976       flag = consume_u32v("flag");
1977     } else {
1978       uint32_t table_index = consume_u32v("table index");
1979       // The only valid flag value without bulk_memory or externref is '0'.
1980       if (table_index != 0) {
1981         error(
1982             "Element segments with table indices require "
1983             "--experimental-wasm-bulk-memory or --experimental-wasm-reftypes");
1984         return;
1985       }
1986       flag = 0;
1987     }
1988 
1989     // The mask for the bit in the flag which indicates if the segment is
1990     // active or not.
1991     constexpr uint8_t kIsPassiveMask = 0x01;
1992     // The mask for the bit in the flag which indicates if the segment has an
1993     // explicit table index field.
1994     constexpr uint8_t kHasTableIndexMask = 0x02;
1995     // The mask for the bit in the flag which indicates if the functions of this
1996     // segment are defined as function indices (=0) or elements(=1).
1997     constexpr uint8_t kFunctionsAsElementsMask = 0x04;
1998     constexpr uint8_t kFullMask =
1999         kIsPassiveMask | kHasTableIndexMask | kFunctionsAsElementsMask;
2000 
2001     bool is_passive = flag & kIsPassiveMask;
2002     if (!is_passive) {
2003       *status = WasmElemSegment::kStatusActive;
2004       if (module_->tables.size() == 0) {
2005         error(pc_, "Active element sections require a table");
2006       }
2007     } else if ((flag & kHasTableIndexMask)) {  // Special bit combination for
2008                                                // declarative segments.
2009       *status = WasmElemSegment::kStatusDeclarative;
2010     } else {
2011       *status = WasmElemSegment::kStatusPassive;
2012     }
2013     *functions_as_elements = flag & kFunctionsAsElementsMask;
2014     bool has_table_index = (flag & kHasTableIndexMask) &&
2015                            *status == WasmElemSegment::kStatusActive;
2016 
2017     if (*status == WasmElemSegment::kStatusDeclarative &&
2018         !enabled_features_.has_reftypes()) {
2019       error(
2020           "Declarative element segments require --experimental-wasm-reftypes");
2021       return;
2022     }
2023     if (*status == WasmElemSegment::kStatusPassive &&
2024         !enabled_features_.has_bulk_memory()) {
2025       error("Passive element segments require --experimental-wasm-bulk-memory");
2026       return;
2027     }
2028     if (*functions_as_elements && !enabled_features_.has_bulk_memory()) {
2029       error(
2030           "Illegal segment flag. Did you forget "
2031           "--experimental-wasm-bulk-memory?");
2032       return;
2033     }
2034     if (flag != 0 && !enabled_features_.has_bulk_memory() &&
2035         !enabled_features_.has_reftypes()) {
2036       error(
2037           "Invalid segment flag. Enable with --experimental-wasm-bulk-memory "
2038           "or --experimental-wasm-reftypes");
2039       return;
2040     }
2041     if ((flag & kFullMask) != flag) {
2042       errorf(pos, "illegal flag value %u. Must be between 0 and 7", flag);
2043     }
2044 
2045     if (has_table_index) {
2046       *table_index = consume_u32v("table index");
2047     } else {
2048       *table_index = 0;
2049     }
2050 
2051     if (*status == WasmElemSegment::kStatusActive) {
2052       *offset = consume_init_expr(module_.get(), kWasmI32,
2053                                   module_.get()->globals.size());
2054       if (offset->kind() == WasmInitExpr::kNone) {
2055         // Failed to parse offset initializer, return early.
2056         return;
2057       }
2058     }
2059 
2060     if (*status == WasmElemSegment::kStatusActive && !has_table_index) {
2061       // Active segments without table indices are a special case for backwards
2062       // compatibility. These cases have an implicit element kind or element
2063       // type, so we are done already with the segment header.
2064       *type = kWasmFuncRef;
2065       return;
2066     }
2067 
2068     if (*functions_as_elements) {
2069       *type = consume_reference_type();
2070     } else {
2071       // We have to check that there is an element kind of type Function. All
2072       // other element kinds are not valid yet.
2073       uint8_t val = consume_u8("element kind");
2074       ImportExportKindCode kind = static_cast<ImportExportKindCode>(val);
2075       if (kind != kExternalFunction) {
2076         errorf(pos, "illegal element kind %x. Must be 0x00", val);
2077         return;
2078       }
2079       *type = kWasmFuncRef;
2080     }
2081   }
2082 
consume_data_segment_header(bool * is_active,uint32_t * index,WasmInitExpr * offset)2083   void consume_data_segment_header(bool* is_active, uint32_t* index,
2084                                    WasmInitExpr* offset) {
2085     const byte* pos = pc();
2086     uint32_t flag = consume_u32v("flag");
2087 
2088     // Some flag values are only valid for specific proposals.
2089     if (flag == SegmentFlags::kPassive) {
2090       if (!enabled_features_.has_bulk_memory()) {
2091         error(
2092             "Passive element segments require --experimental-wasm-bulk-memory");
2093         return;
2094       }
2095     } else if (flag == SegmentFlags::kActiveWithIndex) {
2096       if (!(enabled_features_.has_bulk_memory() ||
2097             enabled_features_.has_reftypes())) {
2098         error(
2099             "Element segments with table indices require "
2100             "--experimental-wasm-bulk-memory or --experimental-wasm-reftypes");
2101         return;
2102       }
2103     } else if (flag != SegmentFlags::kActiveNoIndex) {
2104       errorf(pos, "illegal flag value %u. Must be 0, 1, or 2", flag);
2105       return;
2106     }
2107 
2108     // We know now that the flag is valid. Time to read the rest.
2109     size_t num_globals = module_.get()->globals.size();
2110     if (flag == SegmentFlags::kActiveNoIndex) {
2111       *is_active = true;
2112       *index = 0;
2113       *offset = consume_init_expr(module_.get(), kWasmI32, num_globals);
2114       return;
2115     }
2116     if (flag == SegmentFlags::kPassive) {
2117       *is_active = false;
2118       return;
2119     }
2120     if (flag == SegmentFlags::kActiveWithIndex) {
2121       *is_active = true;
2122       *index = consume_u32v("memory index");
2123       *offset = consume_init_expr(module_.get(), kWasmI32, num_globals);
2124     }
2125   }
2126 
consume_element_func_index()2127   uint32_t consume_element_func_index() {
2128     WasmFunction* func = nullptr;
2129     uint32_t index =
2130         consume_func_index(module_.get(), &func, "element function index");
2131     if (failed()) return index;
2132     func->declared = true;
2133     DCHECK_NE(func, nullptr);
2134     DCHECK_EQ(index, func->func_index);
2135     DCHECK_NE(index, WasmElemSegment::kNullIndex);
2136     return index;
2137   }
2138 
consume_element_expr()2139   uint32_t consume_element_expr() {
2140     uint32_t index = WasmElemSegment::kNullIndex;
2141     uint8_t opcode = consume_u8("element opcode");
2142     if (failed()) return index;
2143     switch (opcode) {
2144       case kExprRefNull: {
2145         HeapTypeImmediate<kFullValidation> imm(WasmFeatures::All(), this,
2146                                                this->pc());
2147         consume_bytes(imm.length, "ref.null immediate");
2148         index = WasmElemSegment::kNullIndex;
2149         break;
2150       }
2151       case kExprRefFunc:
2152         index = consume_element_func_index();
2153         if (failed()) return index;
2154         break;
2155       default:
2156         error("invalid opcode in element");
2157         break;
2158     }
2159     expect_u8("end opcode", kExprEnd);
2160     return index;
2161   }
2162 };
2163 
DecodeWasmModule(const WasmFeatures & enabled,const byte * module_start,const byte * module_end,bool verify_functions,ModuleOrigin origin,Counters * counters,std::shared_ptr<metrics::Recorder> metrics_recorder,v8::metrics::Recorder::ContextId context_id,DecodingMethod decoding_method,AccountingAllocator * allocator)2164 ModuleResult DecodeWasmModule(
2165     const WasmFeatures& enabled, const byte* module_start,
2166     const byte* module_end, bool verify_functions, ModuleOrigin origin,
2167     Counters* counters, std::shared_ptr<metrics::Recorder> metrics_recorder,
2168     v8::metrics::Recorder::ContextId context_id, DecodingMethod decoding_method,
2169     AccountingAllocator* allocator) {
2170   size_t size = module_end - module_start;
2171   CHECK_LE(module_start, module_end);
2172   size_t max_size = max_module_size();
2173   if (size > max_size) {
2174     return ModuleResult{
2175         WasmError{0, "size > maximum module size (%zu): %zu", max_size, size}};
2176   }
2177   // TODO(bradnelson): Improve histogram handling of size_t.
2178   auto size_counter =
2179       SELECT_WASM_COUNTER(counters, origin, wasm, module_size_bytes);
2180   size_counter->AddSample(static_cast<int>(size));
2181   // Signatures are stored in zone memory, which have the same lifetime
2182   // as the {module}.
2183   ModuleDecoderImpl decoder(enabled, module_start, module_end, origin);
2184   v8::metrics::WasmModuleDecoded metrics_event;
2185   base::ElapsedTimer timer;
2186   timer.Start();
2187   ModuleResult result =
2188       decoder.DecodeModule(counters, allocator, verify_functions);
2189 
2190   // Record event metrics.
2191   metrics_event.wall_clock_duration_in_us = timer.Elapsed().InMicroseconds();
2192   timer.Stop();
2193   metrics_event.success = decoder.ok() && result.ok();
2194   metrics_event.async = decoding_method == DecodingMethod::kAsync ||
2195                         decoding_method == DecodingMethod::kAsyncStream;
2196   metrics_event.streamed = decoding_method == DecodingMethod::kSyncStream ||
2197                            decoding_method == DecodingMethod::kAsyncStream;
2198   if (result.ok()) {
2199     metrics_event.function_count = result.value()->num_declared_functions;
2200   } else if (auto&& module = decoder.shared_module()) {
2201     metrics_event.function_count = module->num_declared_functions;
2202   }
2203   metrics_event.module_size_in_bytes = size;
2204   metrics_recorder->DelayMainThreadEvent(metrics_event, context_id);
2205 
2206   return result;
2207 }
2208 
ModuleDecoder(const WasmFeatures & enabled)2209 ModuleDecoder::ModuleDecoder(const WasmFeatures& enabled)
2210     : enabled_features_(enabled) {}
2211 
2212 ModuleDecoder::~ModuleDecoder() = default;
2213 
shared_module() const2214 const std::shared_ptr<WasmModule>& ModuleDecoder::shared_module() const {
2215   return impl_->shared_module();
2216 }
2217 
StartDecoding(Counters * counters,std::shared_ptr<metrics::Recorder> metrics_recorder,v8::metrics::Recorder::ContextId context_id,AccountingAllocator * allocator,ModuleOrigin origin)2218 void ModuleDecoder::StartDecoding(
2219     Counters* counters, std::shared_ptr<metrics::Recorder> metrics_recorder,
2220     v8::metrics::Recorder::ContextId context_id, AccountingAllocator* allocator,
2221     ModuleOrigin origin) {
2222   DCHECK_NULL(impl_);
2223   impl_.reset(new ModuleDecoderImpl(enabled_features_, origin));
2224   impl_->StartDecoding(counters, allocator);
2225 }
2226 
DecodeModuleHeader(Vector<const uint8_t> bytes,uint32_t offset)2227 void ModuleDecoder::DecodeModuleHeader(Vector<const uint8_t> bytes,
2228                                        uint32_t offset) {
2229   impl_->DecodeModuleHeader(bytes, offset);
2230 }
2231 
DecodeSection(SectionCode section_code,Vector<const uint8_t> bytes,uint32_t offset,bool verify_functions)2232 void ModuleDecoder::DecodeSection(SectionCode section_code,
2233                                   Vector<const uint8_t> bytes, uint32_t offset,
2234                                   bool verify_functions) {
2235   impl_->DecodeSection(section_code, bytes, offset, verify_functions);
2236 }
2237 
DecodeFunctionBody(uint32_t index,uint32_t length,uint32_t offset,bool verify_functions)2238 void ModuleDecoder::DecodeFunctionBody(uint32_t index, uint32_t length,
2239                                        uint32_t offset, bool verify_functions) {
2240   impl_->DecodeFunctionBody(index, length, offset, verify_functions);
2241 }
2242 
CheckFunctionsCount(uint32_t functions_count,uint32_t offset)2243 bool ModuleDecoder::CheckFunctionsCount(uint32_t functions_count,
2244                                         uint32_t offset) {
2245   return impl_->CheckFunctionsCount(functions_count, offset);
2246 }
2247 
FinishDecoding(bool verify_functions)2248 ModuleResult ModuleDecoder::FinishDecoding(bool verify_functions) {
2249   return impl_->FinishDecoding(verify_functions);
2250 }
2251 
set_code_section(uint32_t offset,uint32_t size)2252 void ModuleDecoder::set_code_section(uint32_t offset, uint32_t size) {
2253   return impl_->set_code_section(offset, size);
2254 }
2255 
IdentifyUnknownSection(ModuleDecoder * decoder,Vector<const uint8_t> bytes,uint32_t offset,SectionCode * result)2256 size_t ModuleDecoder::IdentifyUnknownSection(ModuleDecoder* decoder,
2257                                              Vector<const uint8_t> bytes,
2258                                              uint32_t offset,
2259                                              SectionCode* result) {
2260   if (!decoder->ok()) return 0;
2261   decoder->impl_->Reset(bytes, offset);
2262   *result = IdentifyUnknownSectionInternal(decoder->impl_.get());
2263   return decoder->impl_->pc() - bytes.begin();
2264 }
2265 
ok()2266 bool ModuleDecoder::ok() { return impl_->ok(); }
2267 
DecodeWasmSignatureForTesting(const WasmFeatures & enabled,Zone * zone,const byte * start,const byte * end)2268 const FunctionSig* DecodeWasmSignatureForTesting(const WasmFeatures& enabled,
2269                                                  Zone* zone, const byte* start,
2270                                                  const byte* end) {
2271   ModuleDecoderImpl decoder(enabled, start, end, kWasmOrigin);
2272   return decoder.DecodeFunctionSignature(zone, start);
2273 }
2274 
DecodeWasmInitExprForTesting(const WasmFeatures & enabled,const byte * start,const byte * end)2275 WasmInitExpr DecodeWasmInitExprForTesting(const WasmFeatures& enabled,
2276                                           const byte* start, const byte* end) {
2277   AccountingAllocator allocator;
2278   ModuleDecoderImpl decoder(enabled, start, end, kWasmOrigin);
2279   return decoder.DecodeInitExprForTesting();
2280 }
2281 
DecodeWasmFunctionForTesting(const WasmFeatures & enabled,Zone * zone,const ModuleWireBytes & wire_bytes,const WasmModule * module,const byte * function_start,const byte * function_end,Counters * counters)2282 FunctionResult DecodeWasmFunctionForTesting(
2283     const WasmFeatures& enabled, Zone* zone, const ModuleWireBytes& wire_bytes,
2284     const WasmModule* module, const byte* function_start,
2285     const byte* function_end, Counters* counters) {
2286   size_t size = function_end - function_start;
2287   CHECK_LE(function_start, function_end);
2288   auto size_histogram =
2289       SELECT_WASM_COUNTER(counters, module->origin, wasm, function_size_bytes);
2290   // TODO(bradnelson): Improve histogram handling of ptrdiff_t.
2291   size_histogram->AddSample(static_cast<int>(size));
2292   if (size > kV8MaxWasmFunctionSize) {
2293     return FunctionResult{WasmError{0,
2294                                     "size > maximum function size (%zu): %zu",
2295                                     kV8MaxWasmFunctionSize, size}};
2296   }
2297   ModuleDecoderImpl decoder(enabled, function_start, function_end, kWasmOrigin);
2298   decoder.SetCounters(counters);
2299   return decoder.DecodeSingleFunction(zone, wire_bytes, module,
2300                                       std::make_unique<WasmFunction>());
2301 }
2302 
DecodeAsmJsOffsets(Vector<const uint8_t> encoded_offsets)2303 AsmJsOffsetsResult DecodeAsmJsOffsets(Vector<const uint8_t> encoded_offsets) {
2304   std::vector<AsmJsOffsetFunctionEntries> functions;
2305 
2306   Decoder decoder(encoded_offsets);
2307   uint32_t functions_count = decoder.consume_u32v("functions count");
2308   // Consistency check.
2309   DCHECK_GE(encoded_offsets.size(), functions_count);
2310   functions.reserve(functions_count);
2311 
2312   for (uint32_t i = 0; i < functions_count; ++i) {
2313     uint32_t size = decoder.consume_u32v("table size");
2314     if (size == 0) {
2315       functions.emplace_back();
2316       continue;
2317     }
2318     DCHECK(decoder.checkAvailable(size));
2319     const byte* table_end = decoder.pc() + size;
2320     uint32_t locals_size = decoder.consume_u32v("locals size");
2321     int function_start_position = decoder.consume_u32v("function start pos");
2322     int function_end_position = function_start_position;
2323     int last_byte_offset = locals_size;
2324     int last_asm_position = function_start_position;
2325     std::vector<AsmJsOffsetEntry> func_asm_offsets;
2326     func_asm_offsets.reserve(size / 4);  // conservative estimation
2327     // Add an entry for the stack check, associated with position 0.
2328     func_asm_offsets.push_back(
2329         {0, function_start_position, function_start_position});
2330     while (decoder.pc() < table_end) {
2331       DCHECK(decoder.ok());
2332       last_byte_offset += decoder.consume_u32v("byte offset delta");
2333       int call_position =
2334           last_asm_position + decoder.consume_i32v("call position delta");
2335       int to_number_position =
2336           call_position + decoder.consume_i32v("to_number position delta");
2337       last_asm_position = to_number_position;
2338       if (decoder.pc() == table_end) {
2339         // The last entry is the function end marker.
2340         DCHECK_EQ(call_position, to_number_position);
2341         function_end_position = call_position;
2342       } else {
2343         func_asm_offsets.push_back(
2344             {last_byte_offset, call_position, to_number_position});
2345       }
2346     }
2347     DCHECK_EQ(decoder.pc(), table_end);
2348     functions.emplace_back(AsmJsOffsetFunctionEntries{
2349         function_start_position, function_end_position,
2350         std::move(func_asm_offsets)});
2351   }
2352   DCHECK(decoder.ok());
2353   DCHECK(!decoder.more());
2354 
2355   return decoder.toResult(AsmJsOffsets{std::move(functions)});
2356 }
2357 
DecodeCustomSections(const byte * start,const byte * end)2358 std::vector<CustomSectionOffset> DecodeCustomSections(const byte* start,
2359                                                       const byte* end) {
2360   Decoder decoder(start, end);
2361   decoder.consume_bytes(4, "wasm magic");
2362   decoder.consume_bytes(4, "wasm version");
2363 
2364   std::vector<CustomSectionOffset> result;
2365 
2366   while (decoder.more()) {
2367     byte section_code = decoder.consume_u8("section code");
2368     uint32_t section_length = decoder.consume_u32v("section length");
2369     uint32_t section_start = decoder.pc_offset();
2370     if (section_code != 0) {
2371       // Skip known sections.
2372       decoder.consume_bytes(section_length, "section bytes");
2373       continue;
2374     }
2375     uint32_t name_length = decoder.consume_u32v("name length");
2376     uint32_t name_offset = decoder.pc_offset();
2377     decoder.consume_bytes(name_length, "section name");
2378     uint32_t payload_offset = decoder.pc_offset();
2379     if (section_length < (payload_offset - section_start)) {
2380       decoder.error("invalid section length");
2381       break;
2382     }
2383     uint32_t payload_length = section_length - (payload_offset - section_start);
2384     decoder.consume_bytes(payload_length);
2385     if (decoder.failed()) break;
2386     result.push_back({{section_start, section_length},
2387                       {name_offset, name_length},
2388                       {payload_offset, payload_length}});
2389   }
2390 
2391   return result;
2392 }
2393 
2394 namespace {
2395 
FindNameSection(Decoder * decoder)2396 bool FindNameSection(Decoder* decoder) {
2397   static constexpr int kModuleHeaderSize = 8;
2398   decoder->consume_bytes(kModuleHeaderSize, "module header");
2399 
2400   WasmSectionIterator section_iter(decoder);
2401 
2402   while (decoder->ok() && section_iter.more() &&
2403          section_iter.section_code() != kNameSectionCode) {
2404     section_iter.advance(true);
2405   }
2406   if (!section_iter.more()) return false;
2407 
2408   // Reset the decoder to not read beyond the name section end.
2409   decoder->Reset(section_iter.payload(), decoder->pc_offset());
2410   return true;
2411 }
2412 
2413 }  // namespace
2414 
DecodeFunctionNames(const byte * module_start,const byte * module_end,std::unordered_map<uint32_t,WireBytesRef> * names,const Vector<const WasmExport> export_table)2415 void DecodeFunctionNames(const byte* module_start, const byte* module_end,
2416                          std::unordered_map<uint32_t, WireBytesRef>* names,
2417                          const Vector<const WasmExport> export_table) {
2418   DCHECK_NOT_NULL(names);
2419   DCHECK(names->empty());
2420 
2421   Decoder decoder(module_start, module_end);
2422   if (FindNameSection(&decoder)) {
2423     while (decoder.ok() && decoder.more()) {
2424       uint8_t name_type = decoder.consume_u8("name type");
2425       if (name_type & 0x80) break;  // no varuint7
2426 
2427       uint32_t name_payload_len = decoder.consume_u32v("name payload length");
2428       if (!decoder.checkAvailable(name_payload_len)) break;
2429 
2430       if (name_type != NameSectionKindCode::kFunction) {
2431         decoder.consume_bytes(name_payload_len, "name subsection payload");
2432         continue;
2433       }
2434       uint32_t functions_count = decoder.consume_u32v("functions count");
2435 
2436       for (; decoder.ok() && functions_count > 0; --functions_count) {
2437         uint32_t function_index = decoder.consume_u32v("function index");
2438         WireBytesRef name = consume_string(&decoder, false, "function name");
2439 
2440         // Be lenient with errors in the name section: Ignore non-UTF8 names.
2441         // You can even assign to the same function multiple times (last valid
2442         // one wins).
2443         if (decoder.ok() && validate_utf8(&decoder, name)) {
2444           names->insert(std::make_pair(function_index, name));
2445         }
2446       }
2447     }
2448   }
2449 
2450   // Extract from export table.
2451   for (const WasmExport& exp : export_table) {
2452     if (exp.kind == kExternalFunction && names->count(exp.index) == 0) {
2453       names->insert(std::make_pair(exp.index, exp.name));
2454     }
2455   }
2456 }
2457 
GenerateNamesFromImportsAndExports(ImportExportKindCode kind,const Vector<const WasmImport> import_table,const Vector<const WasmExport> export_table,std::unordered_map<uint32_t,std::pair<WireBytesRef,WireBytesRef>> * names)2458 void GenerateNamesFromImportsAndExports(
2459     ImportExportKindCode kind, const Vector<const WasmImport> import_table,
2460     const Vector<const WasmExport> export_table,
2461     std::unordered_map<uint32_t, std::pair<WireBytesRef, WireBytesRef>>*
2462         names) {
2463   DCHECK_NOT_NULL(names);
2464   DCHECK(names->empty());
2465   DCHECK(kind == kExternalGlobal || kind == kExternalMemory ||
2466          kind == kExternalTable);
2467 
2468   // Extract from import table.
2469   for (const WasmImport& imp : import_table) {
2470     if (imp.kind != kind) continue;
2471     if (!imp.module_name.is_set() || !imp.field_name.is_set()) continue;
2472     if (names->count(imp.index) == 0) {
2473       names->insert(std::make_pair(
2474           imp.index, std::make_pair(imp.module_name, imp.field_name)));
2475     }
2476   }
2477 
2478   // Extract from export table.
2479   for (const WasmExport& exp : export_table) {
2480     if (exp.kind != kind) continue;
2481     if (!exp.name.is_set()) continue;
2482     if (names->count(exp.index) == 0) {
2483       names->insert(
2484           std::make_pair(exp.index, std::make_pair(WireBytesRef(), exp.name)));
2485     }
2486   }
2487 }
2488 
DecodeLocalNames(Vector<const uint8_t> module_bytes)2489 LocalNames DecodeLocalNames(Vector<const uint8_t> module_bytes) {
2490   Decoder decoder(module_bytes);
2491   if (!FindNameSection(&decoder)) return LocalNames{{}};
2492 
2493   std::vector<LocalNamesPerFunction> functions;
2494   while (decoder.ok() && decoder.more()) {
2495     uint8_t name_type = decoder.consume_u8("name type");
2496     if (name_type & 0x80) break;  // no varuint7
2497 
2498     uint32_t name_payload_len = decoder.consume_u32v("name payload length");
2499     if (!decoder.checkAvailable(name_payload_len)) break;
2500 
2501     if (name_type != NameSectionKindCode::kLocal) {
2502       decoder.consume_bytes(name_payload_len, "name subsection payload");
2503       continue;
2504     }
2505 
2506     uint32_t local_names_count = decoder.consume_u32v("local names count");
2507     for (uint32_t i = 0; i < local_names_count; ++i) {
2508       uint32_t func_index = decoder.consume_u32v("function index");
2509       if (func_index > kMaxInt) continue;
2510       std::vector<LocalName> names;
2511       uint32_t num_names = decoder.consume_u32v("namings count");
2512       for (uint32_t k = 0; k < num_names; ++k) {
2513         uint32_t local_index = decoder.consume_u32v("local index");
2514         WireBytesRef name = consume_string(&decoder, false, "local name");
2515         if (!decoder.ok()) break;
2516         if (local_index > kMaxInt) continue;
2517         // Ignore non-utf8 names.
2518         if (!validate_utf8(&decoder, name)) continue;
2519         names.emplace_back(static_cast<int>(local_index), name);
2520       }
2521       // Use stable sort to get deterministic names (the first one declared)
2522       // even in the presence of duplicates.
2523       std::stable_sort(names.begin(), names.end(), LocalName::IndexLess{});
2524       functions.emplace_back(static_cast<int>(func_index), std::move(names));
2525     }
2526   }
2527   std::stable_sort(functions.begin(), functions.end(),
2528                    LocalNamesPerFunction::FunctionIndexLess{});
2529   return LocalNames{std::move(functions)};
2530 }
2531 
2532 #undef TRACE
2533 
2534 }  // namespace wasm
2535 }  // namespace internal
2536 }  // namespace v8
2537