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