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