| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/wasm/module-decoder.h" | 5 #include "src/wasm/module-decoder.h" |
| 6 | 6 |
| 7 #include "src/base/functional.h" | 7 #include "src/base/functional.h" |
| 8 #include "src/base/platform/platform.h" | 8 #include "src/base/platform/platform.h" |
| 9 #include "src/flags.h" | 9 #include "src/flags.h" |
| 10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 0 // source_size | 602 0 // source_size |
| 603 }); | 603 }); |
| 604 WasmDataSegment* segment = &module->data_segments.back(); | 604 WasmDataSegment* segment = &module->data_segments.back(); |
| 605 DecodeDataSegmentInModule(module, segment); | 605 DecodeDataSegmentInModule(module, segment); |
| 606 } | 606 } |
| 607 section_iter.advance(); | 607 section_iter.advance(); |
| 608 } | 608 } |
| 609 | 609 |
| 610 // ===== Name section ==================================================== | 610 // ===== Name section ==================================================== |
| 611 if (section_iter.section_code() == kNameSectionCode) { | 611 if (section_iter.section_code() == kNameSectionCode) { |
| 612 uint32_t functions_count = consume_u32v("functions count"); | 612 // TODO(titzer): find a way to report name errors as warnings. |
| 613 // Use an inner decoder so that errors don't fail the outer decoder. |
| 614 Decoder inner(start_, pc_, end_); |
| 615 uint32_t functions_count = inner.consume_u32v("functions count"); |
| 613 | 616 |
| 614 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | 617 for (uint32_t i = 0; inner.ok() && i < functions_count; ++i) { |
| 615 uint32_t function_name_length = 0; | 618 uint32_t function_name_length = 0; |
| 616 uint32_t name_offset = consume_string(&function_name_length, false); | 619 uint32_t name_offset = |
| 620 consume_string(inner, &function_name_length, false); |
| 617 uint32_t func_index = i; | 621 uint32_t func_index = i; |
| 618 if (func_index < module->functions.size()) { | 622 if (inner.ok() && func_index < module->functions.size()) { |
| 619 module->functions[func_index].name_offset = name_offset; | 623 module->functions[func_index].name_offset = name_offset; |
| 620 module->functions[func_index].name_length = function_name_length; | 624 module->functions[func_index].name_length = function_name_length; |
| 621 } | 625 } |
| 622 | 626 |
| 623 uint32_t local_names_count = consume_u32v("local names count"); | 627 uint32_t local_names_count = inner.consume_u32v("local names count"); |
| 624 for (uint32_t j = 0; ok() && j < local_names_count; j++) { | 628 for (uint32_t j = 0; ok() && j < local_names_count; j++) { |
| 625 skip_string(); | 629 uint32_t length = inner.consume_u32v("string length"); |
| 630 inner.consume_bytes(length, "string"); |
| 626 } | 631 } |
| 627 } | 632 } |
| 633 // Skip the whole names section in the outer decoder. |
| 634 consume_bytes(section_iter.payload_length(), nullptr); |
| 628 section_iter.advance(); | 635 section_iter.advance(); |
| 629 } | 636 } |
| 630 | 637 |
| 631 // ===== Remaining sections ============================================== | 638 // ===== Remaining sections ============================================== |
| 632 if (section_iter.more() && ok()) { | 639 if (section_iter.more() && ok()) { |
| 633 error(pc(), pc(), "unexpected section: %s", | 640 error(pc(), pc(), "unexpected section: %s", |
| 634 SectionName(section_iter.section_code())); | 641 SectionName(section_iter.section_code())); |
| 635 } | 642 } |
| 636 | 643 |
| 637 if (ok()) { | 644 if (ok()) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 char* buffer = new char[len]; | 809 char* buffer = new char[len]; |
| 803 strncpy(buffer, raw, len); | 810 strncpy(buffer, raw, len); |
| 804 buffer[len - 1] = 0; | 811 buffer[len - 1] = 0; |
| 805 | 812 |
| 806 // Copy error code and location. | 813 // Copy error code and location. |
| 807 result_.MoveFrom(result); | 814 result_.MoveFrom(result); |
| 808 result_.error_msg.reset(buffer); | 815 result_.error_msg.reset(buffer); |
| 809 } | 816 } |
| 810 } | 817 } |
| 811 | 818 |
| 819 uint32_t consume_string(uint32_t* length, bool validate_utf8) { |
| 820 return consume_string(*this, length, validate_utf8); |
| 821 } |
| 822 |
| 812 // Reads a length-prefixed string, checking that it is within bounds. Returns | 823 // Reads a length-prefixed string, checking that it is within bounds. Returns |
| 813 // the offset of the string, and the length as an out parameter. | 824 // the offset of the string, and the length as an out parameter. |
| 814 uint32_t consume_string(uint32_t* length, bool validate_utf8) { | 825 uint32_t consume_string(Decoder& decoder, uint32_t* length, |
| 815 *length = consume_u32v("string length"); | 826 bool validate_utf8) { |
| 816 uint32_t offset = pc_offset(); | 827 *length = decoder.consume_u32v("string length"); |
| 817 const byte* string_start = pc_; | 828 uint32_t offset = decoder.pc_offset(); |
| 829 const byte* string_start = decoder.pc(); |
| 818 // Consume bytes before validation to guarantee that the string is not oob. | 830 // Consume bytes before validation to guarantee that the string is not oob. |
| 819 if (*length > 0) consume_bytes(*length, "string"); | 831 if (*length > 0) decoder.consume_bytes(*length, "string"); |
| 820 if (ok() && validate_utf8 && | 832 if (decoder.ok() && validate_utf8 && |
| 821 !unibrow::Utf8::Validate(string_start, *length)) { | 833 !unibrow::Utf8::Validate(string_start, *length)) { |
| 822 error(string_start, "no valid UTF-8 string"); | 834 decoder.error(string_start, "no valid UTF-8 string"); |
| 823 } | 835 } |
| 824 return offset; | 836 return offset; |
| 825 } | 837 } |
| 826 | 838 |
| 827 // Skips over a length-prefixed string, but checks that it is within bounds. | |
| 828 void skip_string() { | |
| 829 uint32_t length = consume_u32v("string length"); | |
| 830 consume_bytes(length, "string"); | |
| 831 } | |
| 832 | |
| 833 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { | 839 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { |
| 834 const byte* pos = pc_; | 840 const byte* pos = pc_; |
| 835 uint32_t sig_index = consume_u32v("signature index"); | 841 uint32_t sig_index = consume_u32v("signature index"); |
| 836 if (sig_index >= module->signatures.size()) { | 842 if (sig_index >= module->signatures.size()) { |
| 837 error(pos, pos, "signature index %u out of bounds (%d signatures)", | 843 error(pos, pos, "signature index %u out of bounds (%d signatures)", |
| 838 sig_index, static_cast<int>(module->signatures.size())); | 844 sig_index, static_cast<int>(module->signatures.size())); |
| 839 *sig = nullptr; | 845 *sig = nullptr; |
| 840 return 0; | 846 return 0; |
| 841 } | 847 } |
| 842 *sig = module->signatures[sig_index]; | 848 *sig = module->signatures[sig_index]; |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1286 result.push_back({section_start, name_offset, name_length, payload_offset, | 1292 result.push_back({section_start, name_offset, name_length, payload_offset, |
| 1287 payload_length, section_length}); | 1293 payload_length, section_length}); |
| 1288 } | 1294 } |
| 1289 | 1295 |
| 1290 return result; | 1296 return result; |
| 1291 } | 1297 } |
| 1292 | 1298 |
| 1293 } // namespace wasm | 1299 } // namespace wasm |
| 1294 } // namespace internal | 1300 } // namespace internal |
| 1295 } // namespace v8 | 1301 } // namespace v8 |
| OLD | NEW |