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/macro-assembler.h" | 9 #include "src/macro-assembler.h" |
10 #include "src/objects.h" | 10 #include "src/objects.h" |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 break; | 226 break; |
227 } | 227 } |
228 case WasmSection::Code::Globals: { | 228 case WasmSection::Code::Globals: { |
229 uint32_t globals_count = consume_u32v("globals count"); | 229 uint32_t globals_count = consume_u32v("globals count"); |
230 module->globals.reserve(SafeReserve(globals_count)); | 230 module->globals.reserve(SafeReserve(globals_count)); |
231 // Decode globals. | 231 // Decode globals. |
232 for (uint32_t i = 0; i < globals_count; ++i) { | 232 for (uint32_t i = 0; i < globals_count; ++i) { |
233 if (failed()) break; | 233 if (failed()) break; |
234 TRACE("DecodeGlobal[%d] module+%d\n", i, | 234 TRACE("DecodeGlobal[%d] module+%d\n", i, |
235 static_cast<int>(pc_ - start_)); | 235 static_cast<int>(pc_ - start_)); |
236 module->globals.push_back({0, 0, MachineType::Int32(), 0, false}); | 236 module->globals.push_back({0, 0, kAstStmt, 0, false}); |
ahaas
2016/07/21 12:06:46
Could you add a comment which says what this line
titzer
2016/07/21 12:15:10
Done.
| |
237 WasmGlobal* global = &module->globals.back(); | 237 WasmGlobal* global = &module->globals.back(); |
238 DecodeGlobalInModule(global); | 238 DecodeGlobalInModule(global); |
239 } | 239 } |
240 break; | 240 break; |
241 } | 241 } |
242 case WasmSection::Code::DataSegments: { | 242 case WasmSection::Code::DataSegments: { |
243 uint32_t data_segments_count = consume_u32v("data segments count"); | 243 uint32_t data_segments_count = consume_u32v("data segments count"); |
244 module->data_segments.reserve(SafeReserve(data_segments_count)); | 244 module->data_segments.reserve(SafeReserve(data_segments_count)); |
245 // Decode data segments. | 245 // Decode data segments. |
246 for (uint32_t i = 0; i < data_segments_count; ++i) { | 246 for (uint32_t i = 0; i < data_segments_count; ++i) { |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
474 ModuleResult result_; | 474 ModuleResult result_; |
475 ModuleOrigin origin_; | 475 ModuleOrigin origin_; |
476 | 476 |
477 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 477 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
478 | 478 |
479 // Decodes a single global entry inside a module starting at {pc_}. | 479 // Decodes a single global entry inside a module starting at {pc_}. |
480 void DecodeGlobalInModule(WasmGlobal* global) { | 480 void DecodeGlobalInModule(WasmGlobal* global) { |
481 global->name_offset = consume_string(&global->name_length, false); | 481 global->name_offset = consume_string(&global->name_length, false); |
482 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset, | 482 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset, |
483 global->name_length)); | 483 global->name_length)); |
484 global->type = mem_type(); | 484 global->type = consume_local_type(); |
485 global->offset = 0; | 485 global->offset = 0; |
486 global->exported = consume_u8("exported") != 0; | 486 global->exported = consume_u8("exported") != 0; |
487 } | 487 } |
488 | 488 |
489 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { | 489 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
490 if (offset > limit) return false; | 490 if (offset > limit) return false; |
491 if ((offset + size) < offset) return false; // overflow | 491 if ((offset + size) < offset) return false; // overflow |
492 return (offset + size) <= limit; | 492 return (offset + size) <= limit; |
493 } | 493 } |
494 | 494 |
(...skipping 25 matching lines...) Expand all Loading... | |
520 } | 520 } |
521 | 521 |
522 // Calculate individual global offsets and total size of globals table. | 522 // Calculate individual global offsets and total size of globals table. |
523 void CalculateGlobalsOffsets(WasmModule* module) { | 523 void CalculateGlobalsOffsets(WasmModule* module) { |
524 uint32_t offset = 0; | 524 uint32_t offset = 0; |
525 if (module->globals.size() == 0) { | 525 if (module->globals.size() == 0) { |
526 module->globals_size = 0; | 526 module->globals_size = 0; |
527 return; | 527 return; |
528 } | 528 } |
529 for (WasmGlobal& global : module->globals) { | 529 for (WasmGlobal& global : module->globals) { |
530 byte size = WasmOpcodes::MemSize(global.type); | 530 byte size = |
531 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); | |
531 offset = (offset + size - 1) & ~(size - 1); // align | 532 offset = (offset + size - 1) & ~(size - 1); // align |
532 global.offset = offset; | 533 global.offset = offset; |
533 offset += size; | 534 offset += size; |
534 } | 535 } |
535 module->globals_size = offset; | 536 module->globals_size = offset; |
536 } | 537 } |
537 | 538 |
538 // Verifies the body (code) of a given function. | 539 // Verifies the body (code) of a given function. |
539 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, | 540 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
540 WasmFunction* function) { | 541 WasmFunction* function) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
628 case kLocalF32: | 629 case kLocalF32: |
629 return kAstF32; | 630 return kAstF32; |
630 case kLocalF64: | 631 case kLocalF64: |
631 return kAstF64; | 632 return kAstF64; |
632 default: | 633 default: |
633 error(pc_ - 1, "invalid local type"); | 634 error(pc_ - 1, "invalid local type"); |
634 return kAstStmt; | 635 return kAstStmt; |
635 } | 636 } |
636 } | 637 } |
637 | 638 |
638 // Reads a single 8-bit integer, interpreting it as a memory type. | |
639 MachineType mem_type() { | |
640 byte val = consume_u8("memory type"); | |
641 MemTypeCode t = static_cast<MemTypeCode>(val); | |
642 switch (t) { | |
643 case kMemI8: | |
644 return MachineType::Int8(); | |
645 case kMemU8: | |
646 return MachineType::Uint8(); | |
647 case kMemI16: | |
648 return MachineType::Int16(); | |
649 case kMemU16: | |
650 return MachineType::Uint16(); | |
651 case kMemI32: | |
652 return MachineType::Int32(); | |
653 case kMemU32: | |
654 return MachineType::Uint32(); | |
655 case kMemI64: | |
656 return MachineType::Int64(); | |
657 case kMemU64: | |
658 return MachineType::Uint64(); | |
659 case kMemF32: | |
660 return MachineType::Float32(); | |
661 case kMemF64: | |
662 return MachineType::Float64(); | |
663 case kMemS128: | |
664 return MachineType::Simd128(); | |
665 default: | |
666 error(pc_ - 1, "invalid memory type"); | |
667 return MachineType::None(); | |
668 } | |
669 } | |
670 | |
671 // Parses a type entry, which is currently limited to functions only. | 639 // Parses a type entry, which is currently limited to functions only. |
672 FunctionSig* consume_sig() { | 640 FunctionSig* consume_sig() { |
673 const byte* pos = pc_; | 641 const byte* pos = pc_; |
674 byte form = consume_u8("type form"); | 642 byte form = consume_u8("type form"); |
675 if (form != kWasmFunctionTypeForm) { | 643 if (form != kWasmFunctionTypeForm) { |
676 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", | 644 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", |
677 kWasmFunctionTypeForm, form); | 645 kWasmFunctionTypeForm, form); |
678 return nullptr; | 646 return nullptr; |
679 } | 647 } |
680 // parse parameter types | 648 // parse parameter types |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
839 decoder.consume_bytes(size); | 807 decoder.consume_bytes(size); |
840 } | 808 } |
841 if (decoder.more()) decoder.error("unexpected additional bytes"); | 809 if (decoder.more()) decoder.error("unexpected additional bytes"); |
842 | 810 |
843 return decoder.toResult(std::move(table)); | 811 return decoder.toResult(std::move(table)); |
844 } | 812 } |
845 | 813 |
846 } // namespace wasm | 814 } // namespace wasm |
847 } // namespace internal | 815 } // namespace internal |
848 } // namespace v8 | 816 } // namespace v8 |
OLD | NEW |