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 // Add an uninitialized global and pass a pointer to it. |
| 237 module->globals.push_back({0, 0, kAstStmt, 0, false}); |
237 WasmGlobal* global = &module->globals.back(); | 238 WasmGlobal* global = &module->globals.back(); |
238 DecodeGlobalInModule(global); | 239 DecodeGlobalInModule(global); |
239 } | 240 } |
240 break; | 241 break; |
241 } | 242 } |
242 case WasmSection::Code::DataSegments: { | 243 case WasmSection::Code::DataSegments: { |
243 uint32_t data_segments_count = consume_u32v("data segments count"); | 244 uint32_t data_segments_count = consume_u32v("data segments count"); |
244 module->data_segments.reserve(SafeReserve(data_segments_count)); | 245 module->data_segments.reserve(SafeReserve(data_segments_count)); |
245 // Decode data segments. | 246 // Decode data segments. |
246 for (uint32_t i = 0; i < data_segments_count; ++i) { | 247 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_; | 475 ModuleResult result_; |
475 ModuleOrigin origin_; | 476 ModuleOrigin origin_; |
476 | 477 |
477 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 478 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
478 | 479 |
479 // Decodes a single global entry inside a module starting at {pc_}. | 480 // Decodes a single global entry inside a module starting at {pc_}. |
480 void DecodeGlobalInModule(WasmGlobal* global) { | 481 void DecodeGlobalInModule(WasmGlobal* global) { |
481 global->name_offset = consume_string(&global->name_length, false); | 482 global->name_offset = consume_string(&global->name_length, false); |
482 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset, | 483 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset, |
483 global->name_length)); | 484 global->name_length)); |
484 global->type = mem_type(); | 485 global->type = consume_local_type(); |
485 global->offset = 0; | 486 global->offset = 0; |
486 global->exported = consume_u8("exported") != 0; | 487 global->exported = consume_u8("exported") != 0; |
487 } | 488 } |
488 | 489 |
489 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { | 490 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
490 if (offset > limit) return false; | 491 if (offset > limit) return false; |
491 if ((offset + size) < offset) return false; // overflow | 492 if ((offset + size) < offset) return false; // overflow |
492 return (offset + size) <= limit; | 493 return (offset + size) <= limit; |
493 } | 494 } |
494 | 495 |
(...skipping 25 matching lines...) Expand all Loading... |
520 } | 521 } |
521 | 522 |
522 // Calculate individual global offsets and total size of globals table. | 523 // Calculate individual global offsets and total size of globals table. |
523 void CalculateGlobalsOffsets(WasmModule* module) { | 524 void CalculateGlobalsOffsets(WasmModule* module) { |
524 uint32_t offset = 0; | 525 uint32_t offset = 0; |
525 if (module->globals.size() == 0) { | 526 if (module->globals.size() == 0) { |
526 module->globals_size = 0; | 527 module->globals_size = 0; |
527 return; | 528 return; |
528 } | 529 } |
529 for (WasmGlobal& global : module->globals) { | 530 for (WasmGlobal& global : module->globals) { |
530 byte size = WasmOpcodes::MemSize(global.type); | 531 byte size = |
| 532 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); |
531 offset = (offset + size - 1) & ~(size - 1); // align | 533 offset = (offset + size - 1) & ~(size - 1); // align |
532 global.offset = offset; | 534 global.offset = offset; |
533 offset += size; | 535 offset += size; |
534 } | 536 } |
535 module->globals_size = offset; | 537 module->globals_size = offset; |
536 } | 538 } |
537 | 539 |
538 // Verifies the body (code) of a given function. | 540 // Verifies the body (code) of a given function. |
539 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, | 541 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
540 WasmFunction* function) { | 542 WasmFunction* function) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 case kLocalF32: | 630 case kLocalF32: |
629 return kAstF32; | 631 return kAstF32; |
630 case kLocalF64: | 632 case kLocalF64: |
631 return kAstF64; | 633 return kAstF64; |
632 default: | 634 default: |
633 error(pc_ - 1, "invalid local type"); | 635 error(pc_ - 1, "invalid local type"); |
634 return kAstStmt; | 636 return kAstStmt; |
635 } | 637 } |
636 } | 638 } |
637 | 639 |
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. | 640 // Parses a type entry, which is currently limited to functions only. |
672 FunctionSig* consume_sig() { | 641 FunctionSig* consume_sig() { |
673 const byte* pos = pc_; | 642 const byte* pos = pc_; |
674 byte form = consume_u8("type form"); | 643 byte form = consume_u8("type form"); |
675 if (form != kWasmFunctionTypeForm) { | 644 if (form != kWasmFunctionTypeForm) { |
676 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", | 645 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", |
677 kWasmFunctionTypeForm, form); | 646 kWasmFunctionTypeForm, form); |
678 return nullptr; | 647 return nullptr; |
679 } | 648 } |
680 // parse parameter types | 649 // parse parameter types |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 decoder.consume_bytes(size); | 808 decoder.consume_bytes(size); |
840 } | 809 } |
841 if (decoder.more()) decoder.error("unexpected additional bytes"); | 810 if (decoder.more()) decoder.error("unexpected additional bytes"); |
842 | 811 |
843 return decoder.toResult(std::move(table)); | 812 return decoder.toResult(std::move(table)); |
844 } | 813 } |
845 | 814 |
846 } // namespace wasm | 815 } // namespace wasm |
847 } // namespace internal | 816 } // namespace internal |
848 } // namespace v8 | 817 } // namespace v8 |
OLD | NEW |