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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // ===== Imported memory ========================================= | 311 // ===== Imported memory ========================================= |
312 // import->index = | 312 // import->index = |
313 // static_cast<uint32_t>(module->memories.size()); | 313 // static_cast<uint32_t>(module->memories.size()); |
314 // TODO(titzer): imported memories | 314 // TODO(titzer): imported memories |
315 break; | 315 break; |
316 } | 316 } |
317 case kExternalGlobal: { | 317 case kExternalGlobal: { |
318 // ===== Imported global ========================================= | 318 // ===== Imported global ========================================= |
319 import->index = static_cast<uint32_t>(module->globals.size()); | 319 import->index = static_cast<uint32_t>(module->globals.size()); |
320 module->globals.push_back( | 320 module->globals.push_back( |
321 {kAstStmt, false, WasmInitExpr(), 0, true, false}); | 321 {kAstStmt, false, NO_INIT, 0, true, false}); |
322 WasmGlobal* global = &module->globals.back(); | 322 WasmGlobal* global = &module->globals.back(); |
323 global->type = consume_value_type(); | 323 global->type = consume_value_type(); |
324 global->mutability = consume_u8("mutability") != 0; | 324 global->mutability = consume_u8("mutability") != 0; |
325 break; | 325 break; |
326 } | 326 } |
327 default: | 327 default: |
328 error(pos, pos, "unknown import kind 0x%02x", import->kind); | 328 error(pos, pos, "unknown import kind 0x%02x", import->kind); |
329 break; | 329 break; |
330 } | 330 } |
331 } | 331 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 392 } |
393 | 393 |
394 // ===== Global section ================================================== | 394 // ===== Global section ================================================== |
395 if (section_iter.section_code() == kGlobalSectionCode) { | 395 if (section_iter.section_code() == kGlobalSectionCode) { |
396 uint32_t globals_count = consume_u32v("globals count"); | 396 uint32_t globals_count = consume_u32v("globals count"); |
397 module->globals.reserve(SafeReserve(globals_count)); | 397 module->globals.reserve(SafeReserve(globals_count)); |
398 for (uint32_t i = 0; ok() && i < globals_count; ++i) { | 398 for (uint32_t i = 0; ok() && i < globals_count; ++i) { |
399 TRACE("DecodeGlobal[%d] module+%d\n", i, | 399 TRACE("DecodeGlobal[%d] module+%d\n", i, |
400 static_cast<int>(pc_ - start_)); | 400 static_cast<int>(pc_ - start_)); |
401 // Add an uninitialized global and pass a pointer to it. | 401 // Add an uninitialized global and pass a pointer to it. |
402 module->globals.push_back( | 402 module->globals.push_back({kAstStmt, false, NO_INIT, 0, false, false}); |
403 {kAstStmt, false, WasmInitExpr(), 0, false, false}); | |
404 WasmGlobal* global = &module->globals.back(); | 403 WasmGlobal* global = &module->globals.back(); |
405 DecodeGlobalInModule(module, i, global); | 404 DecodeGlobalInModule(module, i, global); |
406 } | 405 } |
407 section_iter.advance(); | 406 section_iter.advance(); |
408 } | 407 } |
409 | 408 |
410 // ===== Export section ================================================== | 409 // ===== Export section ================================================== |
411 if (section_iter.section_code() == kExportSectionCode) { | 410 if (section_iter.section_code() == kExportSectionCode) { |
412 uint32_t export_table_count = consume_u32v("export table count"); | 411 uint32_t export_table_count = consume_u32v("export table count"); |
413 module->export_table.reserve(SafeReserve(export_table_count)); | 412 module->export_table.reserve(SafeReserve(export_table_count)); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 } | 538 } |
540 | 539 |
541 // ===== Data section ==================================================== | 540 // ===== Data section ==================================================== |
542 if (section_iter.section_code() == kDataSectionCode) { | 541 if (section_iter.section_code() == kDataSectionCode) { |
543 uint32_t data_segments_count = consume_u32v("data segments count"); | 542 uint32_t data_segments_count = consume_u32v("data segments count"); |
544 module->data_segments.reserve(SafeReserve(data_segments_count)); | 543 module->data_segments.reserve(SafeReserve(data_segments_count)); |
545 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { | 544 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { |
546 TRACE("DecodeDataSegment[%d] module+%d\n", i, | 545 TRACE("DecodeDataSegment[%d] module+%d\n", i, |
547 static_cast<int>(pc_ - start_)); | 546 static_cast<int>(pc_ - start_)); |
548 module->data_segments.push_back({ | 547 module->data_segments.push_back({ |
549 WasmInitExpr(), // dest_addr | 548 NO_INIT, // dest_addr |
550 0, // source_offset | 549 0, // source_offset |
551 0 // source_size | 550 0 // source_size |
552 }); | 551 }); |
553 WasmDataSegment* segment = &module->data_segments.back(); | 552 WasmDataSegment* segment = &module->data_segments.back(); |
554 DecodeDataSegmentInModule(module, segment); | 553 DecodeDataSegmentInModule(module, segment); |
555 } | 554 } |
556 section_iter.advance(); | 555 section_iter.advance(); |
557 } | 556 } |
558 | 557 |
559 // ===== Name section ==================================================== | 558 // ===== Name section ==================================================== |
560 if (section_iter.section_code() == kNameSectionCode) { | 559 if (section_iter.section_code() == kNameSectionCode) { |
561 const byte* pos = pc_; | 560 const byte* pos = pc_; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 640 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
642 | 641 |
643 // Decodes a single global entry inside a module starting at {pc_}. | 642 // Decodes a single global entry inside a module starting at {pc_}. |
644 void DecodeGlobalInModule(WasmModule* module, uint32_t index, | 643 void DecodeGlobalInModule(WasmModule* module, uint32_t index, |
645 WasmGlobal* global) { | 644 WasmGlobal* global) { |
646 global->type = consume_value_type(); | 645 global->type = consume_value_type(); |
647 global->mutability = consume_u8("mutability") != 0; | 646 global->mutability = consume_u8("mutability") != 0; |
648 const byte* pos = pc(); | 647 const byte* pos = pc(); |
649 global->init = consume_init_expr(module, kAstStmt); | 648 global->init = consume_init_expr(module, kAstStmt); |
650 switch (global->init.kind) { | 649 switch (global->init.kind) { |
651 case WasmInitExpr::kGlobalIndex: { | 650 case WasmInitExpr::kGlobalIndex: |
652 uint32_t other_index = global->init.val.global_index; | 651 if (global->init.val.global_index >= index) { |
653 if (other_index >= index) { | |
654 error("invalid global index in init expression"); | 652 error("invalid global index in init expression"); |
655 } else if (module->globals[other_index].type != global->type) { | 653 } else if (module->globals[index].type != global->type) { |
656 error(pos, pos, | 654 error("type mismatch in global initialization"); |
657 "type mismatch in global initialization " | |
658 "(from global #%u), expected %s, got %s", | |
659 other_index, WasmOpcodes::TypeName(global->type), | |
660 WasmOpcodes::TypeName(module->globals[other_index].type)); | |
661 } | 655 } |
662 break; | 656 break; |
663 } | |
664 default: | 657 default: |
665 if (global->type != TypeOf(module, global->init)) { | 658 if (global->type != TypeOf(module, global->init)) { |
666 error(pos, pos, | 659 error(pos, pos, |
667 "type error in global initialization, expected %s, got %s", | 660 "type error in global initialization, expected %s, got %s", |
668 WasmOpcodes::TypeName(global->type), | 661 WasmOpcodes::TypeName(global->type), |
669 WasmOpcodes::TypeName(TypeOf(module, global->init))); | 662 WasmOpcodes::TypeName(TypeOf(module, global->init))); |
670 } | 663 } |
671 } | 664 } |
672 } | 665 } |
673 | 666 |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1115 decoder.consume_bytes(size); | 1108 decoder.consume_bytes(size); |
1116 } | 1109 } |
1117 if (decoder.more()) decoder.error("unexpected additional bytes"); | 1110 if (decoder.more()) decoder.error("unexpected additional bytes"); |
1118 | 1111 |
1119 return decoder.toResult(std::move(table)); | 1112 return decoder.toResult(std::move(table)); |
1120 } | 1113 } |
1121 | 1114 |
1122 } // namespace wasm | 1115 } // namespace wasm |
1123 } // namespace internal | 1116 } // namespace internal |
1124 } // namespace v8 | 1117 } // namespace v8 |
OLD | NEW |