Index: src/wasm/module-decoder.cc |
diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc |
index c7e7fa5c901ec7c5d7dd9377b8fd8f566abe1f74..90065616d93db85ef9dbe817302f6b8a06028f86 100644 |
--- a/src/wasm/module-decoder.cc |
+++ b/src/wasm/module-decoder.cc |
@@ -27,6 +27,141 @@ namespace wasm { |
namespace { |
+const char* kNameString = "name"; |
+const size_t kNameStringLength = 4; |
+ |
+LocalType TypeOf(const WasmModule* module, const WasmInitExpr& expr) { |
+ switch (expr.kind) { |
+ case WasmInitExpr::kNone: |
+ return kAstStmt; |
+ case WasmInitExpr::kGlobalIndex: |
+ return expr.val.global_index < module->globals.size() |
+ ? module->globals[expr.val.global_index].type |
+ : kAstStmt; |
+ case WasmInitExpr::kI32Const: |
+ return kAstI32; |
+ case WasmInitExpr::kI64Const: |
+ return kAstI64; |
+ case WasmInitExpr::kF32Const: |
+ return kAstF32; |
+ case WasmInitExpr::kF64Const: |
+ return kAstF64; |
+ default: |
+ UNREACHABLE(); |
+ return kAstStmt; |
+ } |
+} |
+ |
+// An iterator over the sections in a WASM binary module. |
+// Automatically skips all unknown sections. |
+class WasmSectionIterator { |
+ public: |
+ explicit WasmSectionIterator(Decoder& decoder) |
+ : decoder_(decoder), |
+ section_code_(kUnknownSectionCode), |
+ section_start_(decoder.pc()), |
+ section_end_(decoder.pc()) { |
+ next(); |
+ } |
+ |
+ inline bool more() const { |
+ return section_code_ != kUnknownSectionCode && decoder_.more(); |
+ } |
+ |
+ inline WasmSectionCode section_code() const { return section_code_; } |
+ |
+ inline const byte* section_start() const { return section_start_; } |
+ |
+ inline uint32_t section_length() const { |
+ return static_cast<uint32_t>(section_end_ - section_start_); |
+ } |
+ |
+ inline const byte* section_end() const { return section_end_; } |
+ |
+ // Advances to the next section, checking that decoding the current section |
+ // stopped at {section_end_}. |
+ void advance() { |
+ if (decoder_.pc() != section_end_) { |
+ const char* msg = decoder_.pc() < section_end_ ? "shorter" : "longer"; |
+ decoder_.error(decoder_.pc(), decoder_.pc(), |
+ "section was %s than expected size " |
+ "(%u bytes expected, %zu decoded)", |
+ msg, section_length(), |
+ static_cast<size_t>(decoder_.pc() - section_start_)); |
+ } |
+ next(); |
+ } |
+ |
+ private: |
+ Decoder& decoder_; |
+ WasmSectionCode section_code_; |
+ const byte* section_start_; |
+ const byte* section_end_; |
+ |
+ // Reads the section code/name at the current position and sets up |
+ // the internal fields. |
+ void next() { |
+ while (true) { |
+ if (!decoder_.more()) { |
+ section_code_ = kUnknownSectionCode; |
+ return; |
+ } |
+ uint8_t section_code = decoder_.consume_u8("section code"); |
+ // Read and check the section size. |
+ uint32_t section_length = decoder_.consume_u32v("section length"); |
+ section_start_ = decoder_.pc(); |
+ if (decoder_.checkAvailable(section_length)) { |
+ // Get the limit of the section within the module. |
+ section_end_ = section_start_ + section_length; |
+ } else { |
+ // The section would extend beyond the end of the module. |
+ section_end_ = section_start_; |
+ } |
+ |
+ if (section_code == kUnknownSectionCode) { |
+ // Check for the known "names" section. |
+ uint32_t string_length = decoder_.consume_u32v("section name length"); |
+ const byte* section_name_start = decoder_.pc(); |
+ decoder_.consume_bytes(string_length, "section name"); |
+ if (decoder_.failed() || decoder_.pc() > section_end_) { |
+ TRACE("Section name of length %u couldn't be read\n", string_length); |
+ section_code_ = kUnknownSectionCode; |
+ return; |
+ } |
+ |
+ TRACE(" +%d section name : \"%.*s\"\n", |
+ static_cast<int>(section_name_start - decoder_.start()), |
+ string_length < 20 ? string_length : 20, section_name_start); |
+ |
+ if (string_length == kNameStringLength && |
+ strncmp(reinterpret_cast<const char*>(section_name_start), |
+ kNameString, kNameStringLength) == 0) { |
+ section_code = kNameSectionCode; |
+ } else { |
+ section_code = kUnknownSectionCode; |
+ } |
+ } else if (!IsValidSectionCode(section_code)) { |
+ decoder_.error(decoder_.pc(), decoder_.pc(), |
+ "unknown section code #0x%02x", section_code); |
+ section_code = kUnknownSectionCode; |
+ } |
+ section_code_ = static_cast<WasmSectionCode>(section_code); |
+ |
+ TRACE("Section: %s\n", SectionName(section_code_)); |
+ if (section_code_ == kUnknownSectionCode && |
+ section_end_ > decoder_.pc()) { |
+ // skip to the end of the unknown section. |
+ uint32_t remaining = |
+ static_cast<uint32_t>(section_end_ - decoder_.pc()); |
+ decoder_.consume_bytes(remaining, "section payload"); |
+ // fall through and continue to the next section. |
+ } else { |
+ return; |
+ } |
+ } |
+ } |
+}; |
+ |
// The main logic for decoding the bytes of a module. |
class ModuleDecoder : public Decoder { |
public: |
@@ -77,11 +212,9 @@ class ModuleDecoder : public Decoder { |
module->min_mem_pages = 0; |
module->max_mem_pages = 0; |
module->mem_export = false; |
- module->mem_external = false; |
module->origin = origin_; |
const byte* pos = pc_; |
- int current_order = 0; |
uint32_t magic_word = consume_u32("wasm magic"); |
#define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff |
if (magic_word != kWasmMagic) { |
@@ -89,7 +222,6 @@ class ModuleDecoder : public Decoder { |
"expected magic word %02x %02x %02x %02x, " |
"found %02x %02x %02x %02x", |
BYTES(kWasmMagic), BYTES(magic_word)); |
- goto done; |
} |
pos = pc_; |
@@ -100,296 +232,367 @@ class ModuleDecoder : public Decoder { |
"expected version %02x %02x %02x %02x, " |
"found %02x %02x %02x %02x", |
BYTES(kWasmVersion), BYTES(magic_version)); |
- goto done; |
} |
} |
- // Decode the module sections. |
- while (pc_ < limit_) { |
- TRACE("DecodeSection\n"); |
- pos = pc_; |
- |
- // Read the section name. |
- uint32_t string_length = consume_u32v("section name length"); |
- const byte* section_name_start = pc_; |
- consume_bytes(string_length); |
- if (failed()) { |
- TRACE("Section name of length %u couldn't be read\n", string_length); |
- break; |
+ WasmSectionIterator section_iter(*this); |
+ |
+ // ===== Type section ==================================================== |
+ if (section_iter.section_code() == kTypeSectionCode) { |
+ uint32_t signatures_count = consume_u32v("signatures count"); |
+ module->signatures.reserve(SafeReserve(signatures_count)); |
+ for (uint32_t i = 0; ok() && i < signatures_count; ++i) { |
+ TRACE("DecodeSignature[%d] module+%d\n", i, |
+ static_cast<int>(pc_ - start_)); |
+ FunctionSig* s = consume_sig(); |
+ module->signatures.push_back(s); |
} |
+ section_iter.advance(); |
+ } |
- TRACE(" +%d section name : \"%.*s\"\n", |
- static_cast<int>(section_name_start - start_), |
- string_length < 20 ? string_length : 20, section_name_start); |
- |
- WasmSection::Code section = |
- WasmSection::lookup(section_name_start, string_length); |
- |
- // Read and check the section size. |
- uint32_t section_length = consume_u32v("section length"); |
- if (!checkAvailable(section_length)) { |
- // The section would extend beyond the end of the module. |
- break; |
- } |
- const byte* section_start = pc_; |
- const byte* expected_section_end = pc_ + section_length; |
- |
- current_order = CheckSectionOrder(current_order, section); |
- |
- switch (section) { |
- case WasmSection::Code::End: |
- // Terminate section decoding. |
- limit_ = pc_; |
- break; |
- case WasmSection::Code::Memory: { |
- module->min_mem_pages = consume_u32v("min memory"); |
- module->max_mem_pages = consume_u32v("max memory"); |
- module->mem_export = consume_u8("export memory") != 0; |
- break; |
- } |
- case WasmSection::Code::Signatures: { |
- uint32_t signatures_count = consume_u32v("signatures count"); |
- module->signatures.reserve(SafeReserve(signatures_count)); |
- // Decode signatures. |
- for (uint32_t i = 0; ok() && i < signatures_count; ++i) { |
- TRACE("DecodeSignature[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- FunctionSig* s = consume_sig(); |
- module->signatures.push_back(s); |
- } |
- break; |
+ // ===== Import section ================================================== |
+ if (section_iter.section_code() == kImportSectionCode) { |
+ uint32_t import_table_count = consume_u32v("import table count"); |
+ module->import_table.reserve(SafeReserve(import_table_count)); |
+ for (uint32_t i = 0; ok() && i < import_table_count; ++i) { |
+ TRACE("DecodeImportTable[%d] module+%d\n", i, |
+ static_cast<int>(pc_ - start_)); |
+ |
+ module->import_table.push_back({ |
+ 0, // module_name_length |
+ 0, // module_name_offset |
+ 0, // field_name_offset |
+ 0, // field_name_length |
+ kExternalFunction, // kind |
+ 0 // index |
+ }); |
+ WasmImport* import = &module->import_table.back(); |
+ const byte* pos = pc_; |
+ import->module_name_offset = |
+ consume_string(&import->module_name_length, true); |
+ if (import->module_name_length == 0) { |
+ error(pos, "import module name cannot be NULL"); |
} |
- case WasmSection::Code::FunctionSignatures: { |
- uint32_t functions_count = consume_u32v("functions count"); |
- module->functions.reserve(SafeReserve(functions_count)); |
- for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
- module->functions.push_back({nullptr, // sig |
- i, // func_index |
- 0, // sig_index |
- 0, // name_offset |
- 0, // name_length |
- 0, // code_start_offset |
- 0}); // code_end_offset |
+ import->field_name_offset = |
+ consume_string(&import->field_name_length, true); |
+ |
+ import->kind = static_cast<WasmExternalKind>(consume_u8("import kind")); |
+ switch (import->kind) { |
+ case kExternalFunction: { |
+ // ===== Imported function ======================================= |
+ import->index = static_cast<uint32_t>(module->functions.size()); |
+ module->num_imported_functions++; |
+ module->functions.push_back({nullptr, // sig |
+ import->index, // func_index |
+ 0, // sig_index |
+ 0, // name_offset |
+ 0, // name_length |
+ 0, // code_start_offset |
+ 0, // code_end_offset |
+ true, // imported |
+ false}); // exported |
WasmFunction* function = &module->functions.back(); |
function->sig_index = consume_sig_index(module, &function->sig); |
- } |
- break; |
- } |
- case WasmSection::Code::FunctionBodies: { |
- const byte* pos = pc_; |
- uint32_t functions_count = consume_u32v("functions count"); |
- if (functions_count != module->functions.size()) { |
- error(pos, pos, "function body count %u mismatch (%u expected)", |
- functions_count, |
- static_cast<uint32_t>(module->functions.size())); |
break; |
} |
- for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
- WasmFunction* function = &module->functions[i]; |
- uint32_t size = consume_u32v("body size"); |
- function->code_start_offset = pc_offset(); |
- function->code_end_offset = pc_offset() + size; |
- |
- TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body", |
- size); |
- pc_ += size; |
- if (pc_ > limit_) { |
- error(pc_, "function body extends beyond end of file"); |
- } |
- } |
- break; |
- } |
- case WasmSection::Code::Names: { |
- const byte* pos = pc_; |
- uint32_t functions_count = consume_u32v("functions count"); |
- if (functions_count != module->functions.size()) { |
- error(pos, pos, "function name count %u mismatch (%u expected)", |
- functions_count, |
- static_cast<uint32_t>(module->functions.size())); |
+ case kExternalTable: { |
+ // ===== Imported table ========================================== |
+ import->index = |
+ static_cast<uint32_t>(module->function_tables.size()); |
+ module->function_tables.push_back( |
+ {0, 0, std::vector<int32_t>(), true, false}); |
+ expect_u8("element type", 0x20); |
+ WasmIndirectFunctionTable* table = &module->function_tables.back(); |
+ consume_resizable_limits("element count", "elements", kMaxUInt32, |
+ &table->size, &table->max_size); |
break; |
} |
- |
- for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
- WasmFunction* function = &module->functions[i]; |
- function->name_offset = |
- consume_string(&function->name_length, false); |
- |
- uint32_t local_names_count = consume_u32v("local names count"); |
- for (uint32_t j = 0; ok() && j < local_names_count; j++) { |
- uint32_t unused = 0; |
- uint32_t offset = consume_string(&unused, false); |
- USE(unused); |
- USE(offset); |
- } |
+ case kExternalMemory: { |
+ // ===== Imported memory ========================================= |
+ // import->index = |
+ // static_cast<uint32_t>(module->memories.size()); |
+ // TODO(titzer): imported memories |
+ break; |
} |
- break; |
- } |
- case WasmSection::Code::Globals: { |
- uint32_t globals_count = consume_u32v("globals count"); |
- module->globals.reserve(SafeReserve(globals_count)); |
- // Decode globals. |
- for (uint32_t i = 0; ok() && i < globals_count; ++i) { |
- TRACE("DecodeGlobal[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- // Add an uninitialized global and pass a pointer to it. |
- module->globals.push_back({0, 0, kAstStmt, 0, false}); |
+ case kExternalGlobal: { |
+ // ===== Imported global ========================================= |
+ import->index = static_cast<uint32_t>(module->globals.size()); |
+ module->globals.push_back( |
+ {kAstStmt, false, NO_INIT, 0, true, false}); |
WasmGlobal* global = &module->globals.back(); |
- DecodeGlobalInModule(global); |
- } |
- break; |
- } |
- case WasmSection::Code::DataSegments: { |
- uint32_t data_segments_count = consume_u32v("data segments count"); |
- module->data_segments.reserve(SafeReserve(data_segments_count)); |
- // Decode data segments. |
- for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { |
- TRACE("DecodeDataSegment[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- module->data_segments.push_back({0, // dest_addr |
- 0, // source_offset |
- 0, // source_size |
- false}); // init |
- WasmDataSegment* segment = &module->data_segments.back(); |
- DecodeDataSegmentInModule(module, segment); |
+ global->type = consume_value_type(); |
+ global->mutability = consume_u8("mutability") != 0; |
+ break; |
} |
- break; |
+ default: |
+ error(pos, pos, "unknown import kind 0x%02x", import->kind); |
+ break; |
} |
- case WasmSection::Code::FunctionTable: { |
- // An indirect function table requires functions first. |
- CheckForFunctions(module, section); |
- // Assume only one table for now. |
- static const uint32_t kSupportedTableCount = 1; |
- module->function_tables.reserve(SafeReserve(kSupportedTableCount)); |
- // Decode function table. |
- for (uint32_t i = 0; ok() && i < kSupportedTableCount; ++i) { |
- TRACE("DecodeFunctionTable[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- module->function_tables.push_back({0, 0, std::vector<uint16_t>()}); |
- DecodeFunctionTableInModule(module, &module->function_tables[i]); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Function section ================================================ |
+ if (section_iter.section_code() == kFunctionSectionCode) { |
+ uint32_t functions_count = consume_u32v("functions count"); |
+ module->functions.reserve(SafeReserve(functions_count)); |
+ module->num_declared_functions = functions_count; |
+ for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
+ uint32_t func_index = static_cast<uint32_t>(module->functions.size()); |
+ module->functions.push_back({nullptr, // sig |
+ func_index, // func_index |
+ 0, // sig_index |
+ 0, // name_offset |
+ 0, // name_length |
+ 0, // code_start_offset |
+ 0, // code_end_offset |
+ false, // imported |
+ false}); // exported |
+ WasmFunction* function = &module->functions.back(); |
+ function->sig_index = consume_sig_index(module, &function->sig); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Table section =================================================== |
+ if (section_iter.section_code() == kTableSectionCode) { |
+ const byte* pos = pc_; |
+ uint32_t table_count = consume_u32v("table count"); |
+ // Require at most one table for now. |
+ if (table_count > 1) { |
+ error(pos, pos, "invalid table count %d, maximum 1", table_count); |
+ } |
+ |
+ for (uint32_t i = 0; ok() && i < table_count; i++) { |
+ module->function_tables.push_back( |
+ {0, 0, std::vector<int32_t>(), false, false}); |
+ WasmIndirectFunctionTable* table = &module->function_tables.back(); |
+ expect_u8("table type", kWasmAnyFunctionTypeForm); |
+ consume_resizable_limits("table elements", "elements", kMaxUInt32, |
+ &table->size, &table->max_size); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Memory section ================================================== |
+ if (section_iter.section_code() == kMemorySectionCode) { |
+ const byte* pos = pc_; |
+ uint32_t memory_count = consume_u32v("memory count"); |
+ // Require at most one memory for now. |
+ if (memory_count > 1) { |
+ error(pos, pos, "invalid memory count %d, maximum 1", memory_count); |
+ } |
+ |
+ for (uint32_t i = 0; ok() && i < memory_count; i++) { |
+ consume_resizable_limits("memory", "pages", WasmModule::kMaxLegalPages, |
+ &module->min_mem_pages, |
+ &module->max_mem_pages); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Global section ================================================== |
+ if (section_iter.section_code() == kGlobalSectionCode) { |
+ uint32_t globals_count = consume_u32v("globals count"); |
+ module->globals.reserve(SafeReserve(globals_count)); |
+ for (uint32_t i = 0; ok() && i < globals_count; ++i) { |
+ TRACE("DecodeGlobal[%d] module+%d\n", i, |
+ static_cast<int>(pc_ - start_)); |
+ // Add an uninitialized global and pass a pointer to it. |
+ module->globals.push_back({kAstStmt, false, NO_INIT, 0, false, false}); |
+ WasmGlobal* global = &module->globals.back(); |
+ DecodeGlobalInModule(module, i, global); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Export section ================================================== |
+ if (section_iter.section_code() == kExportSectionCode) { |
+ uint32_t export_table_count = consume_u32v("export table count"); |
+ module->export_table.reserve(SafeReserve(export_table_count)); |
+ for (uint32_t i = 0; ok() && i < export_table_count; ++i) { |
+ TRACE("DecodeExportTable[%d] module+%d\n", i, |
+ static_cast<int>(pc_ - start_)); |
+ |
+ module->export_table.push_back({ |
+ 0, // name_length |
+ 0, // name_offset |
+ kExternalFunction, // kind |
+ 0 // index |
+ }); |
+ WasmExport* exp = &module->export_table.back(); |
+ |
+ exp->name_offset = consume_string(&exp->name_length, true); |
+ const byte* pos = pc(); |
+ exp->kind = static_cast<WasmExternalKind>(consume_u8("export kind")); |
+ switch (exp->kind) { |
+ case kExternalFunction: { |
+ WasmFunction* func = nullptr; |
+ exp->index = consume_func_index(module, &func); |
+ module->num_exported_functions++; |
+ if (func) func->exported = true; |
+ break; |
} |
- break; |
- } |
- case WasmSection::Code::StartFunction: { |
- // Declares a start function for a module. |
- CheckForFunctions(module, section); |
- if (module->start_function_index >= 0) { |
- error("start function already declared"); |
+ case kExternalTable: { |
+ WasmIndirectFunctionTable* table = nullptr; |
+ exp->index = consume_table_index(module, &table); |
+ if (table) table->exported = true; |
break; |
} |
- WasmFunction* func; |
- const byte* pos = pc_; |
- module->start_function_index = consume_func_index(module, &func); |
- if (func && func->sig->parameter_count() > 0) { |
- error(pos, "invalid start function: non-zero parameter count"); |
+ case kExternalMemory: { |
+ uint32_t index = consume_u32v("memory index"); |
+ if (index != 0) error("invalid memory index != 0"); |
+ module->mem_export = true; |
break; |
} |
- break; |
- } |
- case WasmSection::Code::ImportTable: { |
- uint32_t import_table_count = consume_u32v("import table count"); |
- module->import_table.reserve(SafeReserve(import_table_count)); |
- // Decode import table. |
- for (uint32_t i = 0; ok() && i < import_table_count; ++i) { |
- TRACE("DecodeImportTable[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- |
- module->import_table.push_back({nullptr, // sig |
- 0, // sig_index |
- 0, // module_name_offset |
- 0, // module_name_length |
- 0, // function_name_offset |
- 0}); // function_name_length |
- WasmImport* import = &module->import_table.back(); |
- |
- import->sig_index = consume_sig_index(module, &import->sig); |
- const byte* pos = pc_; |
- import->module_name_offset = |
- consume_string(&import->module_name_length, true); |
- if (import->module_name_length == 0) { |
- error(pos, "import module name cannot be NULL"); |
- } |
- import->function_name_offset = |
- consume_string(&import->function_name_length, true); |
+ case kExternalGlobal: { |
+ WasmGlobal* global = nullptr; |
+ exp->index = consume_global_index(module, &global); |
+ if (global) global->exported = true; |
+ break; |
} |
- break; |
+ default: |
+ error(pos, pos, "invalid export kind 0x%02x", exp->kind); |
+ break; |
} |
- case WasmSection::Code::ExportTable: { |
- // Declares an export table. |
- CheckForFunctions(module, section); |
- uint32_t export_table_count = consume_u32v("export table count"); |
- module->export_table.reserve(SafeReserve(export_table_count)); |
- // Decode export table. |
- for (uint32_t i = 0; ok() && i < export_table_count; ++i) { |
- TRACE("DecodeExportTable[%d] module+%d\n", i, |
- static_cast<int>(pc_ - start_)); |
- |
- module->export_table.push_back({0, // func_index |
- 0, // name_offset |
- 0}); // name_length |
- WasmExport* exp = &module->export_table.back(); |
- |
- WasmFunction* func; |
- exp->func_index = consume_func_index(module, &func); |
- exp->name_offset = consume_string(&exp->name_length, true); |
+ } |
+ // Check for duplicate exports. |
+ if (ok() && module->export_table.size() > 1) { |
+ std::vector<WasmExport> sorted_exports(module->export_table); |
+ const byte* base = start_; |
+ auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { |
+ // Return true if a < b. |
+ if (a.name_length != b.name_length) { |
+ return a.name_length < b.name_length; |
} |
- // Check for duplicate exports. |
- if (ok() && module->export_table.size() > 1) { |
- std::vector<WasmExport> sorted_exports(module->export_table); |
- const byte* base = start_; |
- auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { |
- // Return true if a < b. |
- uint32_t len = a.name_length; |
- if (len != b.name_length) return len < b.name_length; |
- return memcmp(base + a.name_offset, base + b.name_offset, len) < |
- 0; |
- }; |
- std::stable_sort(sorted_exports.begin(), sorted_exports.end(), |
- cmp_less); |
- auto it = sorted_exports.begin(); |
- WasmExport* last = &*it++; |
- for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
- DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. |
- if (!cmp_less(*last, *it)) { |
- const byte* pc = start_ + it->name_offset; |
- error(pc, pc, |
- "Duplicate export name '%.*s' for functions %d and %d", |
- it->name_length, pc, last->func_index, it->func_index); |
- break; |
- } |
- } |
+ return memcmp(base + a.name_offset, base + b.name_offset, |
+ a.name_length) < 0; |
+ }; |
+ std::stable_sort(sorted_exports.begin(), sorted_exports.end(), |
+ cmp_less); |
+ auto it = sorted_exports.begin(); |
+ WasmExport* last = &*it++; |
+ for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
+ DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. |
+ if (!cmp_less(*last, *it)) { |
+ const byte* pc = start_ + it->name_offset; |
+ error(pc, pc, |
+ "Duplicate export name '%.*s' for functions %d and %d", |
+ it->name_length, pc, last->index, it->index); |
+ break; |
} |
- break; |
} |
- case WasmSection::Code::Max: |
- // Skip unknown sections. |
- TRACE("Unknown section: '"); |
- for (uint32_t i = 0; i != string_length; ++i) { |
- TRACE("%c", *(section_name_start + i)); |
- } |
- TRACE("'\n"); |
- consume_bytes(section_length); |
- break; |
- } |
- |
- if (pc_ != expected_section_end) { |
- const char* diff = pc_ < expected_section_end ? "shorter" : "longer"; |
- size_t expected_length = static_cast<size_t>(section_length); |
- size_t actual_length = static_cast<size_t>(pc_ - section_start); |
- error(pc_, pc_, |
- "section \"%s\" %s (%zu bytes) than specified (%zu bytes)", |
- WasmSection::getName(section), diff, actual_length, |
- expected_length); |
- break; |
} |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Start section =================================================== |
+ if (section_iter.section_code() == kStartSectionCode) { |
+ WasmFunction* func; |
+ const byte* pos = pc_; |
+ module->start_function_index = consume_func_index(module, &func); |
+ if (func && func->sig->parameter_count() > 0) { |
+ error(pos, "invalid start function: non-zero parameter count"); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Elements section ================================================ |
+ if (section_iter.section_code() == kElementSectionCode) { |
+ uint32_t element_count = consume_u32v("element count"); |
+ for (uint32_t i = 0; ok() && i < element_count; ++i) { |
+ uint32_t table_index = consume_u32v("table index"); |
+ if (table_index != 0) error("illegal table index != 0"); |
+ WasmInitExpr offset = consume_init_expr(module, kAstI32); |
+ uint32_t num_elem = consume_u32v("number of elements"); |
+ std::vector<uint32_t> vector; |
+ module->table_inits.push_back({table_index, offset, vector}); |
+ WasmTableInit* init = &module->table_inits.back(); |
+ init->entries.reserve(SafeReserve(num_elem)); |
+ for (uint32_t j = 0; ok() && j < num_elem; j++) { |
+ WasmFunction* func = nullptr; |
+ init->entries.push_back(consume_func_index(module, &func)); |
+ } |
+ } |
+ |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Code section ==================================================== |
+ if (section_iter.section_code() == kCodeSectionCode) { |
+ const byte* pos = pc_; |
+ uint32_t functions_count = consume_u32v("functions count"); |
+ if (functions_count != module->num_declared_functions) { |
+ error(pos, pos, "function body count %u mismatch (%u expected)", |
+ functions_count, module->num_declared_functions); |
+ } |
+ for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
+ WasmFunction* function = |
+ &module->functions[i + module->num_imported_functions]; |
+ uint32_t size = consume_u32v("body size"); |
+ function->code_start_offset = pc_offset(); |
+ function->code_end_offset = pc_offset() + size; |
+ consume_bytes(size, "function body"); |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Data section ==================================================== |
+ if (section_iter.section_code() == kDataSectionCode) { |
+ uint32_t data_segments_count = consume_u32v("data segments count"); |
+ module->data_segments.reserve(SafeReserve(data_segments_count)); |
+ for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { |
+ TRACE("DecodeDataSegment[%d] module+%d\n", i, |
+ static_cast<int>(pc_ - start_)); |
+ module->data_segments.push_back({ |
+ NO_INIT, // dest_addr |
+ 0, // source_offset |
+ 0 // source_size |
+ }); |
+ WasmDataSegment* segment = &module->data_segments.back(); |
+ DecodeDataSegmentInModule(module, segment); |
+ } |
+ section_iter.advance(); |
} |
- done: |
- if (ok()) CalculateGlobalsOffsets(module); |
+ // ===== Name section ==================================================== |
+ if (section_iter.section_code() == kNameSectionCode) { |
+ const byte* pos = pc_; |
+ uint32_t functions_count = consume_u32v("functions count"); |
+ if (functions_count != module->num_declared_functions) { |
+ error(pos, pos, "function name count %u mismatch (%u expected)", |
+ functions_count, module->num_declared_functions); |
+ } |
+ |
+ for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
+ WasmFunction* function = |
+ &module->functions[i + module->num_imported_functions]; |
+ function->name_offset = consume_string(&function->name_length, false); |
+ |
+ uint32_t local_names_count = consume_u32v("local names count"); |
+ for (uint32_t j = 0; ok() && j < local_names_count; j++) { |
+ uint32_t unused = 0; |
+ uint32_t offset = consume_string(&unused, false); |
+ USE(unused); |
+ USE(offset); |
+ } |
+ } |
+ section_iter.advance(); |
+ } |
+ |
+ // ===== Remaining sections ============================================== |
+ if (section_iter.more() && ok()) { |
+ error(pc(), pc(), "unexpected section: %s", |
+ SectionName(section_iter.section_code())); |
+ } |
+ |
+ if (ok()) { |
+ CalculateGlobalOffsets(module); |
+ PreinitializeIndirectFunctionTables(module); |
+ } |
const WasmModule* finished_module = module; |
ModuleResult result = toResult(finished_module); |
- if (FLAG_dump_wasm_module) { |
- DumpModule(module, result); |
- } |
+ if (FLAG_dump_wasm_module) DumpModule(module, result); |
return result; |
} |
@@ -399,27 +602,6 @@ class ModuleDecoder : public Decoder { |
return count < kMaxReserve ? count : kMaxReserve; |
} |
- void CheckForFunctions(WasmModule* module, WasmSection::Code section) { |
- if (module->functions.size() == 0) { |
- error(pc_ - 1, nullptr, "functions must appear before section %s", |
- WasmSection::getName(section)); |
- } |
- } |
- |
- int CheckSectionOrder(int current_order, WasmSection::Code section) { |
- int next_order = WasmSection::getOrder(section); |
- if (next_order == 0) return current_order; |
- if (next_order == current_order) { |
- error(pc_, pc_, "section \"%s\" already defined", |
- WasmSection::getName(section)); |
- } |
- if (next_order < current_order) { |
- error(pc_, pc_, "section \"%s\" out of order", |
- WasmSection::getName(section)); |
- } |
- return next_order; |
- } |
- |
// Decodes a single anonymous function starting at {start_}. |
FunctionResult DecodeSingleFunction(ModuleEnv* module_env, |
WasmFunction* function) { |
@@ -445,6 +627,11 @@ class ModuleDecoder : public Decoder { |
return ok() ? result : nullptr; |
} |
+ WasmInitExpr DecodeInitExpr(const byte* start) { |
+ pc_ = start; |
+ return consume_init_expr(nullptr, kAstStmt); |
+ } |
+ |
private: |
Zone* module_zone; |
ModuleResult result_; |
@@ -453,19 +640,28 @@ class ModuleDecoder : public Decoder { |
uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
// Decodes a single global entry inside a module starting at {pc_}. |
- void DecodeGlobalInModule(WasmGlobal* global) { |
- global->name_offset = consume_string(&global->name_length, false); |
- if (ok() && |
- !unibrow::Utf8::Validate(start_ + global->name_offset, |
- global->name_length)) { |
- error("global name is not valid utf8"); |
- } |
- global->type = consume_local_type(); |
- if (global->type == kAstStmt) { |
- error("invalid global type"); |
+ void DecodeGlobalInModule(WasmModule* module, uint32_t index, |
+ WasmGlobal* global) { |
+ global->type = consume_value_type(); |
+ global->mutability = consume_u8("mutability") != 0; |
+ const byte* pos = pc(); |
+ global->init = consume_init_expr(module, kAstStmt); |
+ switch (global->init.kind) { |
+ case WasmInitExpr::kGlobalIndex: |
+ if (global->init.val.global_index >= index) { |
+ error("invalid global index in init expression"); |
+ } else if (module->globals[index].type != global->type) { |
+ error("type mismatch in global initialization"); |
+ } |
+ break; |
+ default: |
+ if (global->type != TypeOf(module, global->init)) { |
+ error(pos, pos, |
+ "type error in global initialization, expected %s, got %s", |
+ WasmOpcodes::TypeName(global->type), |
+ WasmOpcodes::TypeName(TypeOf(module, global->init))); |
+ } |
} |
- global->offset = 0; |
- global->exported = consume_u8("exported") != 0; |
} |
bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
@@ -477,10 +673,10 @@ class ModuleDecoder : public Decoder { |
// Decodes a single data segment entry inside a module starting at {pc_}. |
void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { |
const byte* start = pc_; |
- segment->dest_addr = consume_u32v("destination"); |
+ expect_u8("linear memory index", 0); |
+ segment->dest_addr = consume_init_expr(module, kAstI32); |
segment->source_size = consume_u32v("source size"); |
segment->source_offset = static_cast<uint32_t>(pc_ - start_); |
- segment->init = true; |
// Validate the data is in the module. |
uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); |
@@ -489,40 +685,11 @@ class ModuleDecoder : public Decoder { |
error(start, "segment out of bounds of module"); |
} |
- // Validate that the segment will fit into the (minimum) memory. |
- uint32_t memory_limit = |
- WasmModule::kPageSize * (module ? module->min_mem_pages |
- : WasmModule::kMaxMemPages); |
- if (!IsWithinLimit(memory_limit, segment->dest_addr, |
- segment->source_size)) { |
- error(start, "segment out of bounds of memory"); |
- } |
- |
- consume_bytes(segment->source_size); |
- } |
- |
- // Decodes a single function table inside a module starting at {pc_}. |
- void DecodeFunctionTableInModule(WasmModule* module, |
- WasmIndirectFunctionTable* table) { |
- table->size = consume_u32v("function table entry count"); |
- table->max_size = table->size; |
- |
- if (table->max_size != table->size) { |
- error("invalid table maximum size"); |
- } |
- |
- for (uint32_t i = 0; ok() && i < table->size; ++i) { |
- uint16_t index = consume_u32v(); |
- if (index >= module->functions.size()) { |
- error(pc_ - sizeof(index), "invalid function index"); |
- break; |
- } |
- table->values.push_back(index); |
- } |
+ consume_bytes(segment->source_size, "segment data"); |
} |
// Calculate individual global offsets and total size of globals table. |
- void CalculateGlobalsOffsets(WasmModule* module) { |
+ void CalculateGlobalOffsets(WasmModule* module) { |
uint32_t offset = 0; |
if (module->globals.size() == 0) { |
module->globals_size = 0; |
@@ -538,6 +705,30 @@ class ModuleDecoder : public Decoder { |
module->globals_size = offset; |
} |
+ // TODO(titzer): this only works without overlapping initializations from |
+ // global bases for entries |
+ void PreinitializeIndirectFunctionTables(WasmModule* module) { |
+ // Fill all tables with invalid entries first. |
+ for (WasmIndirectFunctionTable& table : module->function_tables) { |
+ table.values.resize(table.size); |
+ for (size_t i = 0; i < table.size; i++) { |
+ table.values[i] = kInvalidFunctionIndex; |
+ } |
+ } |
+ for (WasmTableInit& init : module->table_inits) { |
+ if (init.offset.kind != WasmInitExpr::kI32Const) continue; |
+ if (init.table_index >= module->function_tables.size()) continue; |
+ WasmIndirectFunctionTable& table = |
+ module->function_tables[init.table_index]; |
+ for (size_t i = 0; i < init.entries.size(); i++) { |
+ size_t index = i + init.offset.val.i32_const; |
+ if (index < table.values.size()) { |
+ table.values[index] = init.entries[i]; |
+ } |
+ } |
+ } |
+ } |
+ |
// Verifies the body (code) of a given function. |
void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
WasmFunction* function) { |
@@ -568,25 +759,14 @@ class ModuleDecoder : public Decoder { |
} |
} |
- // Reads a single 32-bit unsigned integer interpreted as an offset, checking |
- // the offset is within bounds and advances. |
- uint32_t consume_offset(const char* name = nullptr) { |
- uint32_t offset = consume_u32(name ? name : "offset"); |
- if (offset > static_cast<uint32_t>(limit_ - start_)) { |
- error(pc_ - sizeof(uint32_t), "offset out of bounds of module"); |
- } |
- return offset; |
- } |
- |
// Reads a length-prefixed string, checking that it is within bounds. Returns |
// the offset of the string, and the length as an out parameter. |
uint32_t consume_string(uint32_t* length, bool validate_utf8) { |
*length = consume_u32v("string length"); |
uint32_t offset = pc_offset(); |
- TRACE(" +%u %-20s: (%u bytes)\n", offset, "string", *length); |
const byte* string_start = pc_; |
// Consume bytes before validation to guarantee that the string is not oob. |
- consume_bytes(*length); |
+ consume_bytes(*length, "string"); |
if (ok() && validate_utf8 && |
!unibrow::Utf8::Validate(string_start, *length)) { |
error(string_start, "no valid UTF-8 string"); |
@@ -608,25 +788,134 @@ class ModuleDecoder : public Decoder { |
} |
uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { |
+ return consume_index("function index", module->functions, func); |
+ } |
+ |
+ uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) { |
+ return consume_index("global index", module->globals, global); |
+ } |
+ |
+ uint32_t consume_table_index(WasmModule* module, |
+ WasmIndirectFunctionTable** table) { |
+ return consume_index("table index", module->function_tables, table); |
+ } |
+ |
+ template <typename T> |
+ uint32_t consume_index(const char* name, std::vector<T>& vector, T** ptr) { |
const byte* pos = pc_; |
- uint32_t func_index = consume_u32v("function index"); |
- if (func_index >= module->functions.size()) { |
- error(pos, pos, "function index %u out of bounds (%d functions)", |
- func_index, static_cast<int>(module->functions.size())); |
- *func = nullptr; |
+ uint32_t index = consume_u32v(name); |
+ if (index >= vector.size()) { |
+ error(pos, pos, "%s %u out of bounds (%d entries)", name, index, |
+ static_cast<int>(vector.size())); |
+ *ptr = nullptr; |
return 0; |
} |
- *func = &module->functions[func_index]; |
- return func_index; |
+ *ptr = &vector[index]; |
+ return index; |
+ } |
+ |
+ void consume_resizable_limits(const char* name, const char* units, |
+ uint32_t max_value, uint32_t* initial, |
+ uint32_t* maximum) { |
+ uint32_t flags = consume_u32v("resizable limits flags"); |
+ const byte* pos = pc(); |
+ *initial = consume_u32v("initial size"); |
+ if (*initial > max_value) { |
+ error(pos, pos, |
+ "initial %s size (%u %s) is larger than maximum allowable (%u)", |
+ name, *initial, units, max_value); |
+ } |
+ if (flags & 1) { |
+ pos = pc(); |
+ *maximum = consume_u32v("maximum size"); |
+ if (*maximum > max_value) { |
+ error(pos, pos, |
+ "maximum %s size (%u %s) is larger than maximum allowable (%u)", |
+ name, *maximum, units, max_value); |
+ } |
+ if (*maximum < *initial) { |
+ error(pos, pos, "maximum %s size (%u %s) is less than initial (%u %s)", |
+ name, *maximum, units, *initial, units); |
+ } |
+ } else { |
+ *maximum = 0; |
+ } |
+ } |
+ |
+ bool expect_u8(const char* name, uint8_t expected) { |
+ const byte* pos = pc(); |
+ uint8_t value = consume_u8(name); |
+ if (value != expected) { |
+ error(pos, pos, "expected %s 0x%02x, got 0x%02x", name, expected, value); |
+ return false; |
+ } |
+ return true; |
+ } |
+ |
+ WasmInitExpr consume_init_expr(WasmModule* module, LocalType expected) { |
+ const byte* pos = pc(); |
+ uint8_t opcode = consume_u8("opcode"); |
+ WasmInitExpr expr; |
+ unsigned len = 0; |
+ switch (opcode) { |
+ case kExprGetGlobal: { |
+ GlobalIndexOperand operand(this, pc() - 1); |
+ expr.kind = WasmInitExpr::kGlobalIndex; |
+ expr.val.global_index = operand.index; |
+ len = operand.length; |
+ break; |
+ } |
+ case kExprI32Const: { |
+ ImmI32Operand operand(this, pc() - 1); |
+ expr.kind = WasmInitExpr::kI32Const; |
+ expr.val.i32_const = operand.value; |
+ len = operand.length; |
+ break; |
+ } |
+ case kExprF32Const: { |
+ ImmF32Operand operand(this, pc() - 1); |
+ expr.kind = WasmInitExpr::kF32Const; |
+ expr.val.f32_const = operand.value; |
+ len = operand.length; |
+ break; |
+ } |
+ case kExprI64Const: { |
+ ImmI64Operand operand(this, pc() - 1); |
+ expr.kind = WasmInitExpr::kI64Const; |
+ expr.val.i64_const = operand.value; |
+ len = operand.length; |
+ break; |
+ } |
+ case kExprF64Const: { |
+ ImmF64Operand operand(this, pc() - 1); |
+ expr.kind = WasmInitExpr::kF64Const; |
+ expr.val.f64_const = operand.value; |
+ len = operand.length; |
+ break; |
+ } |
+ default: { |
+ error("invalid opcode in initialization expression"); |
+ expr.kind = WasmInitExpr::kNone; |
+ expr.val.i32_const = 0; |
+ } |
+ } |
+ consume_bytes(len, "init code"); |
+ if (!expect_u8("end opcode", kExprEnd)) { |
+ expr.kind = WasmInitExpr::kNone; |
+ } |
+ if (expected != kAstStmt && TypeOf(module, expr) != kAstI32) { |
+ error(pos, pos, "type error in init expression, expected %s, got %s", |
+ WasmOpcodes::TypeName(expected), |
+ WasmOpcodes::TypeName(TypeOf(module, expr))); |
+ } |
+ return expr; |
} |
// Reads a single 8-bit integer, interpreting it as a local type. |
- LocalType consume_local_type() { |
- byte val = consume_u8("local type"); |
+ LocalType consume_value_type() { |
+ byte val = consume_u8("value type"); |
LocalTypeCode t = static_cast<LocalTypeCode>(val); |
switch (t) { |
- case kLocalVoid: |
- return kAstStmt; |
case kLocalI32: |
return kAstI32; |
case kLocalI64: |
@@ -645,19 +934,12 @@ class ModuleDecoder : public Decoder { |
// Parses a type entry, which is currently limited to functions only. |
FunctionSig* consume_sig() { |
- const byte* pos = pc_; |
- byte form = consume_u8("type form"); |
- if (form != kWasmFunctionTypeForm) { |
- error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", |
- kWasmFunctionTypeForm, form); |
- return nullptr; |
- } |
+ if (!expect_u8("type form", kWasmFunctionTypeForm)) return nullptr; |
// parse parameter types |
uint32_t param_count = consume_u32v("param count"); |
std::vector<LocalType> params; |
- for (uint32_t i = 0; i < param_count; ++i) { |
- LocalType param = consume_local_type(); |
- if (param == kAstStmt) error(pc_ - 1, "invalid void parameter type"); |
+ for (uint32_t i = 0; ok() && i < param_count; ++i) { |
+ LocalType param = consume_value_type(); |
params.push_back(param); |
} |
@@ -670,12 +952,16 @@ class ModuleDecoder : public Decoder { |
return nullptr; |
} |
std::vector<LocalType> returns; |
- for (uint32_t i = 0; i < return_count; ++i) { |
- LocalType ret = consume_local_type(); |
- if (ret == kAstStmt) error(pc_ - 1, "invalid void return type"); |
+ for (uint32_t i = 0; ok() && i < return_count; ++i) { |
+ LocalType ret = consume_value_type(); |
returns.push_back(ret); |
} |
+ if (failed()) { |
+ // Decoding failed, return void -> void |
+ return new (module_zone) FunctionSig(0, 0, nullptr); |
+ } |
+ |
// FunctionSig stores the return types first. |
LocalType* buffer = |
module_zone->NewArray<LocalType>(param_count + return_count); |
@@ -714,7 +1000,7 @@ class FunctionError : public FunctionResult { |
}; |
Vector<const byte> FindSection(const byte* module_start, const byte* module_end, |
- WasmSection::Code code) { |
+ WasmSectionCode code) { |
Decoder decoder(module_start, module_end); |
uint32_t magic_word = decoder.consume_u32("wasm magic"); |
@@ -723,24 +1009,14 @@ Vector<const byte> FindSection(const byte* module_start, const byte* module_end, |
uint32_t magic_version = decoder.consume_u32("wasm version"); |
if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); |
- while (decoder.more() && decoder.ok()) { |
- // Read the section name. |
- uint32_t string_length = decoder.consume_u32v("section name length"); |
- const byte* section_name_start = decoder.pc(); |
- decoder.consume_bytes(string_length); |
- if (decoder.failed()) break; |
- |
- WasmSection::Code section = |
- WasmSection::lookup(section_name_start, string_length); |
- |
- // Read and check the section size. |
- uint32_t section_length = decoder.consume_u32v("section length"); |
- |
- const byte* section_start = decoder.pc(); |
- decoder.consume_bytes(section_length); |
- if (section == code && decoder.ok()) { |
- return Vector<const uint8_t>(section_start, section_length); |
+ WasmSectionIterator section_iter(decoder); |
+ while (section_iter.more()) { |
+ if (section_iter.section_code() == code) { |
+ return Vector<const uint8_t>(section_iter.section_start(), |
+ section_iter.section_length()); |
} |
+ decoder.consume_bytes(section_iter.section_length(), "section payload"); |
+ section_iter.advance(); |
} |
return Vector<const uint8_t>(); |
@@ -775,6 +1051,13 @@ FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, |
return decoder.DecodeFunctionSignature(start); |
} |
+WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { |
+ AccountingAllocator allocator; |
+ Zone zone(&allocator); |
+ ModuleDecoder decoder(&zone, start, end, kWasmOrigin); |
+ return decoder.DecodeInitExpr(start); |
+} |
+ |
FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
ModuleEnv* module_env, |
const byte* function_start, |
@@ -792,15 +1075,26 @@ FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
return decoder.DecodeSingleFunction(module_env, function); |
} |
-FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start, |
- const byte* module_end) { |
+FunctionOffsetsResult DecodeWasmFunctionOffsets( |
+ const byte* module_start, const byte* module_end, |
+ uint32_t num_imported_functions) { |
+ // Find and decode the code section. |
Vector<const byte> code_section = |
- FindSection(module_start, module_end, WasmSection::Code::FunctionBodies); |
+ FindSection(module_start, module_end, kCodeSectionCode); |
Decoder decoder(code_section.start(), code_section.end()); |
- if (!code_section.start()) decoder.error("no code section"); |
+ FunctionOffsets table; |
+ if (!code_section.start()) { |
+ decoder.error("no code section"); |
+ return decoder.toResult(std::move(table)); |
+ } |
+ |
+ // Reserve entries for the imported functions. |
+ table.reserve(num_imported_functions); |
+ for (uint32_t i = 0; i < num_imported_functions; i++) { |
+ table.push_back(std::make_pair(0, 0)); |
+ } |
uint32_t functions_count = decoder.consume_u32v("functions count"); |
- FunctionOffsets table; |
// Take care of invalid input here. |
if (functions_count < static_cast<unsigned>(code_section.length()) / 2) |
table.reserve(functions_count); |