diff options
author | Ben Smith <binji@chromium.org> | 2020-03-11 09:54:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-11 09:54:35 -0700 |
commit | cbe3f1291df14685bf44a61692ebef8934d17c4f (patch) | |
tree | e4e17e8612e29599a12900d92d95f697095dfabe /src/binary-reader.cc | |
parent | f649b7ca9af02ff3213d8c2b135eaef9d87f5e0d (diff) | |
download | wabt-cbe3f1291df14685bf44a61692ebef8934d17c4f.tar.gz wabt-cbe3f1291df14685bf44a61692ebef8934d17c4f.tar.bz2 wabt-cbe3f1291df14685bf44a61692ebef8934d17c4f.zip |
Remove validation from the BinaryReader (#1354)
Validation should only happen in ValidateModule, BinaryReader should
only check whether the binary is malformed.
This change also fixes a few places in BinaryReaderIR where an index is
assumed to be valid.
Diffstat (limited to 'src/binary-reader.cc')
-rw-r--r-- | src/binary-reader.cc | 172 |
1 files changed, 36 insertions, 136 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 2a28a569..3eabc698 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -113,10 +113,6 @@ class BinaryReader { bool IsBlockType(Type); Index NumTotalFuncs(); - Index NumTotalTables(); - Index NumTotalMemories(); - Index NumTotalGlobals(); - Index NumTotalEvents(); Result ReadI32InitExpr(Index index) WABT_WARN_UNUSED; Result ReadInitExpr(Index index, bool require_i32 = false) WABT_WARN_UNUSED; @@ -158,20 +154,13 @@ class BinaryReader { BinarySection last_known_section_ = BinarySection::Invalid; bool did_read_names_section_ = false; bool reading_custom_section_ = false; - Index num_signatures_ = 0; - Index num_imports_ = 0; Index num_func_imports_ = 0; Index num_table_imports_ = 0; Index num_memory_imports_ = 0; Index num_global_imports_ = 0; Index num_event_imports_ = 0; Index num_function_signatures_ = 0; - Index num_tables_ = 0; - Index num_memories_ = 0; - Index num_globals_ = 0; - Index num_exports_ = 0; Index num_function_bodies_ = 0; - Index num_events_ = 0; Index data_count_ = kInvalidIndex; using ReadEndRestoreGuard = @@ -417,29 +406,13 @@ bool BinaryReader::IsBlockType(Type type) { return false; } - return type.GetIndex() < num_signatures_; + return true; } Index BinaryReader::NumTotalFuncs() { return num_func_imports_ + num_function_signatures_; } -Index BinaryReader::NumTotalTables() { - return num_table_imports_ + num_tables_; -} - -Index BinaryReader::NumTotalMemories() { - return num_memory_imports_ + num_memories_; -} - -Index BinaryReader::NumTotalGlobals() { - return num_global_imports_ + num_globals_; -} - -Index BinaryReader::NumTotalEvents() { - return num_event_imports_ + num_events_; -} - Result BinaryReader::ReadI32InitExpr(Index index) { return ReadInitExpr(index, true); } @@ -538,8 +511,6 @@ Result BinaryReader::ReadTable(Type* out_elem_type, Limits* out_elem_limits) { ERROR_IF(is_shared, "tables may not be shared"); if (has_max) { CHECK_RESULT(ReadU32Leb128(&max, "table max elem count")); - ERROR_UNLESS(initial <= max, - "table initial elem count must be <= max elem count"); } out_elem_limits->has_max = has_max; @@ -554,14 +525,10 @@ Result BinaryReader::ReadMemory(Limits* out_page_limits) { uint32_t max = 0; CHECK_RESULT(ReadU32Leb128(&flags, "memory flags")); CHECK_RESULT(ReadU32Leb128(&initial, "memory initial page count")); - ERROR_UNLESS(initial <= WABT_MAX_PAGES, "invalid memory initial size"); bool has_max = flags & WABT_BINARY_LIMITS_HAS_MAX_FLAG; bool is_shared = flags & WABT_BINARY_LIMITS_IS_SHARED_FLAG; - ERROR_IF(is_shared && !has_max, "shared memory must have a max size"); if (has_max) { CHECK_RESULT(ReadU32Leb128(&max, "memory max page count")); - ERROR_UNLESS(max <= WABT_MAX_PAGES, "invalid memory max size"); - ERROR_UNLESS(initial <= max, "memory initial size must be <= max size"); } out_page_limits->has_max = has_max; @@ -792,8 +759,6 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { case Opcode::Call: { Index func_index; CHECK_RESULT(ReadIndex(&func_index, "call function index")); - ERROR_UNLESS(func_index < NumTotalFuncs(), - "invalid call function index: %" PRIindex, func_index); CALLBACK(OnCallExpr, func_index); CALLBACK(OnOpcodeIndex, func_index); break; @@ -802,18 +767,13 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { case Opcode::CallIndirect: { Index sig_index; CHECK_RESULT(ReadIndex(&sig_index, "call_indirect signature index")); - ERROR_UNLESS(sig_index < num_signatures_, - "invalid call_indirect signature index"); Index table_index = 0; if (options_.features.reference_types_enabled()) { CHECK_RESULT(ReadIndex(&table_index, "call_indirect table index")); - ERROR_UNLESS(table_index < NumTotalTables(), - "invalid call_indirect table index"); } else { uint8_t reserved; CHECK_RESULT(ReadU8(&reserved, "call_indirect reserved")); - ERROR_UNLESS(reserved == 0, - "call_indirect reserved value must be 0"); + ERROR_UNLESS(reserved == 0, "call_indirect reserved value must be 0"); } CALLBACK(OnCallIndirectExpr, sig_index, table_index); CALLBACK(OnOpcodeUint32Uint32, sig_index, table_index); @@ -823,9 +783,6 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { case Opcode::ReturnCall: { Index func_index; CHECK_RESULT(ReadIndex(&func_index, "return_call")); - ERROR_UNLESS(func_index < NumTotalFuncs(), - "invalid return_call function index: %" PRIindex, - func_index); CALLBACK(OnReturnCallExpr, func_index); CALLBACK(OnOpcodeIndex, func_index); break; @@ -834,18 +791,15 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { case Opcode::ReturnCallIndirect: { Index sig_index; CHECK_RESULT(ReadIndex(&sig_index, "return_call_indirect")); - ERROR_UNLESS(sig_index < num_signatures_, - "invalid return_call_indirect signature index"); Index table_index = 0; if (options_.features.reference_types_enabled()) { - CHECK_RESULT(ReadIndex(&table_index, "return_call_indirect table index")); - ERROR_UNLESS(table_index < NumTotalTables(), - "invalid return_call_indirect table index"); + CHECK_RESULT( + ReadIndex(&table_index, "return_call_indirect table index")); } else { uint8_t reserved; CHECK_RESULT(ReadU8(&reserved, "return_call_indirect reserved")); ERROR_UNLESS(reserved == 0, - "return_call_indirect reserved value must be 0"); + "return_call_indirect reserved value must be 0"); } CALLBACK(OnReturnCallIndirectExpr, sig_index, table_index); CALLBACK(OnOpcodeUint32Uint32, sig_index, table_index); @@ -1449,9 +1403,6 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { CHECK_RESULT(ReadIndex(&segment, "elem segment index")); Index table_index; CHECK_RESULT(ReadIndex(&table_index, "reserved table index")); - if (!options_.features.reference_types_enabled()) { - ERROR_UNLESS(table_index == 0, "table.index index must be 0"); - } CALLBACK(OnTableInitExpr, segment, table_index); CALLBACK(OnOpcodeUint32Uint32, segment, table_index); break; @@ -1509,13 +1460,7 @@ Result BinaryReader::ReadFunctionBody(Offset end_offset) { Index table_dst; Index table_src; CHECK_RESULT(ReadIndex(&table_dst, "reserved table index")); - if (!options_.features.reference_types_enabled()) { - ERROR_UNLESS(table_dst == 0, "table.copy dst must be 0"); - } CHECK_RESULT(ReadIndex(&table_src, "table src")); - if (!options_.features.reference_types_enabled()) { - ERROR_UNLESS(table_src == 0, "table.copy src must be 0"); - } CALLBACK(OnTableCopyExpr, table_dst, table_src); CALLBACK(OnOpcodeUint32Uint32, table_dst, table_src); break; @@ -1897,17 +1842,16 @@ Result BinaryReader::ReadEventType(Index* out_sig_index) { CHECK_RESULT(ReadU32Leb128(&attribute, "event attribute")); ERROR_UNLESS(attribute == 0, "event attribute must be 0"); CHECK_RESULT(ReadIndex(out_sig_index, "event signature index")); - ERROR_UNLESS(*out_sig_index < num_signatures_, - "invalid event signature index"); return Result::Ok; } Result BinaryReader::ReadEventSection(Offset section_size) { CALLBACK(BeginEventSection, section_size); - CHECK_RESULT(ReadCount(&num_events_, "event count")); - CALLBACK(OnEventCount, num_events_); + Index num_events; + CHECK_RESULT(ReadCount(&num_events, "event count")); + CALLBACK(OnEventCount, num_events); - for (Index i = 0; i < num_events_; ++i) { + for (Index i = 0; i < num_events; ++i) { Index event_index = num_event_imports_ + i; Index sig_index; CHECK_RESULT(ReadEventType(&sig_index)); @@ -1945,10 +1889,11 @@ Result BinaryReader::ReadCustomSection(Offset section_size) { Result BinaryReader::ReadTypeSection(Offset section_size) { CALLBACK(BeginTypeSection, section_size); - CHECK_RESULT(ReadCount(&num_signatures_, "type count")); - CALLBACK(OnTypeCount, num_signatures_); + Index num_signatures; + CHECK_RESULT(ReadCount(&num_signatures, "type count")); + CALLBACK(OnTypeCount, num_signatures); - for (Index i = 0; i < num_signatures_; ++i) { + for (Index i = 0; i < num_signatures; ++i) { Type form; CHECK_RESULT(ReadType(&form, "type form")); @@ -1970,9 +1915,6 @@ Result BinaryReader::ReadTypeSection(Offset section_size) { Index num_results; CHECK_RESULT(ReadCount(&num_results, "function result count")); - ERROR_UNLESS( - num_results <= 1 || options_.features.multi_value_enabled(), - "result count must be 0 or 1"); result_types_.resize(num_results); @@ -2012,9 +1954,10 @@ Result BinaryReader::ReadTypeSection(Offset section_size) { Result BinaryReader::ReadImportSection(Offset section_size) { CALLBACK(BeginImportSection, section_size); - CHECK_RESULT(ReadCount(&num_imports_, "import count")); - CALLBACK(OnImportCount, num_imports_); - for (Index i = 0; i < num_imports_; ++i) { + Index num_imports; + CHECK_RESULT(ReadCount(&num_imports, "import count")); + CALLBACK(OnImportCount, num_imports); + for (Index i = 0; i < num_imports; ++i) { string_view module_name; CHECK_RESULT(ReadStr(&module_name, "import module name")); string_view field_name; @@ -2028,8 +1971,6 @@ Result BinaryReader::ReadImportSection(Offset section_size) { case ExternalKind::Func: { Index sig_index; CHECK_RESULT(ReadIndex(&sig_index, "import signature index")); - ERROR_UNLESS(sig_index < num_signatures_, - "invalid import signature index"); CALLBACK(OnImportFunc, i, module_name, field_name, num_func_imports_, sig_index); num_func_imports_++; @@ -2078,15 +2019,6 @@ Result BinaryReader::ReadImportSection(Offset section_size) { } } - ERROR_UNLESS(num_memory_imports_ <= 1, - "memory count (%" PRIindex ") must be 0 or 1", - num_memory_imports_); - if (!options_.features.reference_types_enabled()) { - ERROR_UNLESS(num_table_imports_ <= 1, - "table count (%" PRIindex ") must be 0 or 1", - num_table_imports_); - } - CALLBACK0(EndImportSection); return Result::Ok; } @@ -2100,8 +2032,6 @@ Result BinaryReader::ReadFunctionSection(Offset section_size) { Index func_index = num_func_imports_ + i; Index sig_index; CHECK_RESULT(ReadIndex(&sig_index, "function signature index")); - ERROR_UNLESS(sig_index < num_signatures_, - "invalid function signature index: %" PRIindex, sig_index); CALLBACK(OnFunction, func_index, sig_index); } CALLBACK0(EndFunctionSection); @@ -2110,14 +2040,10 @@ Result BinaryReader::ReadFunctionSection(Offset section_size) { Result BinaryReader::ReadTableSection(Offset section_size) { CALLBACK(BeginTableSection, section_size); - CHECK_RESULT(ReadCount(&num_tables_, "table count")); - if (!options_.features.reference_types_enabled()) { - ERROR_UNLESS(num_table_imports_ + num_tables_ <= 1, - "table count (%" PRIindex ") must be 0 or 1", - num_table_imports_ + num_tables_); - } - CALLBACK(OnTableCount, num_tables_); - for (Index i = 0; i < num_tables_; ++i) { + Index num_tables; + CHECK_RESULT(ReadCount(&num_tables, "table count")); + CALLBACK(OnTableCount, num_tables); + for (Index i = 0; i < num_tables; ++i) { Index table_index = num_table_imports_ + i; Type elem_type; Limits elem_limits; @@ -2130,12 +2056,10 @@ Result BinaryReader::ReadTableSection(Offset section_size) { Result BinaryReader::ReadMemorySection(Offset section_size) { CALLBACK(BeginMemorySection, section_size); - CHECK_RESULT(ReadCount(&num_memories_, "memory count")); - ERROR_UNLESS(num_memory_imports_ + num_memories_ <= 1, - "memory count (%" PRIindex ") must be 0 or 1", - num_memory_imports_ + num_memories_); - CALLBACK(OnMemoryCount, num_memories_); - for (Index i = 0; i < num_memories_; ++i) { + Index num_memories; + CHECK_RESULT(ReadCount(&num_memories, "memory count")); + CALLBACK(OnMemoryCount, num_memories); + for (Index i = 0; i < num_memories; ++i) { Index memory_index = num_memory_imports_ + i; Limits page_limits; CHECK_RESULT(ReadMemory(&page_limits)); @@ -2147,9 +2071,10 @@ Result BinaryReader::ReadMemorySection(Offset section_size) { Result BinaryReader::ReadGlobalSection(Offset section_size) { CALLBACK(BeginGlobalSection, section_size); - CHECK_RESULT(ReadCount(&num_globals_, "global count")); - CALLBACK(OnGlobalCount, num_globals_); - for (Index i = 0; i < num_globals_; ++i) { + Index num_globals; + CHECK_RESULT(ReadCount(&num_globals, "global count")); + CALLBACK(OnGlobalCount, num_globals); + for (Index i = 0; i < num_globals; ++i) { Index global_index = num_global_imports_ + i; Type global_type; bool mutable_; @@ -2166,9 +2091,10 @@ Result BinaryReader::ReadGlobalSection(Offset section_size) { Result BinaryReader::ReadExportSection(Offset section_size) { CALLBACK(BeginExportSection, section_size); - CHECK_RESULT(ReadCount(&num_exports_, "export count")); - CALLBACK(OnExportCount, num_exports_); - for (Index i = 0; i < num_exports_; ++i) { + Index num_exports; + CHECK_RESULT(ReadCount(&num_exports, "export count")); + CALLBACK(OnExportCount, num_exports); + for (Index i = 0; i < num_exports; ++i) { string_view name; CHECK_RESULT(ReadStr(&name, "export item name")); @@ -2177,29 +2103,9 @@ Result BinaryReader::ReadExportSection(Offset section_size) { Index item_index; CHECK_RESULT(ReadIndex(&item_index, "export item index")); - switch (kind) { - case ExternalKind::Func: - ERROR_UNLESS(item_index < NumTotalFuncs(), - "invalid export func index: %" PRIindex, item_index); - break; - case ExternalKind::Table: - ERROR_UNLESS(item_index < NumTotalTables(), - "invalid export table index: %" PRIindex, item_index); - break; - case ExternalKind::Memory: - ERROR_UNLESS(item_index < NumTotalMemories(), - "invalid export memory index: %" PRIindex, item_index); - break; - case ExternalKind::Global: - ERROR_UNLESS(item_index < NumTotalGlobals(), - "invalid export global index: %" PRIindex, item_index); - break; - case ExternalKind::Event: - ERROR_UNLESS(options_.features.exceptions_enabled(), - "invalid export event kind: exceptions not allowed"); - ERROR_UNLESS(item_index < NumTotalEvents(), - "invalid export event index: %" PRIindex, item_index); - break; + if (kind == ExternalKind::Event) { + ERROR_UNLESS(options_.features.exceptions_enabled(), + "invalid export event kind: exceptions not allowed"); } CALLBACK(OnExport, i, static_cast<ExternalKind>(kind), item_index, name); @@ -2212,8 +2118,6 @@ Result BinaryReader::ReadStartSection(Offset section_size) { CALLBACK(BeginStartSection, section_size); Index func_index; CHECK_RESULT(ReadIndex(&func_index, "start function index")); - ERROR_UNLESS(func_index < NumTotalFuncs(), - "invalid start function index: %" PRIindex, func_index); CALLBACK(OnStartFunction, func_index); CALLBACK0(EndStartSection); return Result::Ok; @@ -2224,8 +2128,6 @@ Result BinaryReader::ReadElemSection(Offset section_size) { Index num_elem_segments; CHECK_RESULT(ReadCount(&num_elem_segments, "elem segment count")); CALLBACK(OnElemSegmentCount, num_elem_segments); - ERROR_UNLESS(num_elem_segments == 0 || NumTotalTables() > 0, - "elem section without table section"); for (Index i = 0; i < num_elem_segments; ++i) { uint32_t flags; CHECK_RESULT(ReadU32Leb128(&flags, "elem segment flags")); @@ -2341,8 +2243,6 @@ Result BinaryReader::ReadDataSection(Offset section_size) { Index num_data_segments; CHECK_RESULT(ReadCount(&num_data_segments, "data segment count")); CALLBACK(OnDataSegmentCount, num_data_segments); - ERROR_UNLESS(num_data_segments == 0 || NumTotalMemories() > 0, - "data section without memory section"); // If the DataCount section is not present, then data_count_ will be invalid. ERROR_UNLESS(data_count_ == kInvalidIndex || data_count_ == num_data_segments, "data segment count does not equal count in DataCount section"); |