Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Side by Side Diff: src/wasm/module-decoder.cc

Issue 2410953003: [wasm] Fix decoding of shared global index space (Closed)
Patch Set: review comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/wasm/wasm-module.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 consume_resizable_limits("memory", "pages", WasmModule::kMaxLegalPages, 390 consume_resizable_limits("memory", "pages", WasmModule::kMaxLegalPages,
391 &module->min_mem_pages, 391 &module->min_mem_pages,
392 &module->max_mem_pages); 392 &module->max_mem_pages);
393 } 393 }
394 section_iter.advance(); 394 section_iter.advance();
395 } 395 }
396 396
397 // ===== Global section ================================================== 397 // ===== Global section ==================================================
398 if (section_iter.section_code() == kGlobalSectionCode) { 398 if (section_iter.section_code() == kGlobalSectionCode) {
399 uint32_t globals_count = consume_u32v("globals count"); 399 uint32_t globals_count = consume_u32v("globals count");
400 module->globals.reserve(SafeReserve(globals_count)); 400 uint32_t imported_globals = static_cast<uint32_t>(module->globals.size());
401 if (!IsWithinLimit(kMaxReserve, globals_count, imported_globals)) {
Derek Schuff 2016/10/12 05:35:19 OK, so I was afraid this would be a problem; I was
402 error(pos, pos, "too many imported+defined globals: %u + %u",
403 imported_globals, globals_count);
404 }
405 module->globals.reserve(SafeReserve(imported_globals + globals_count));
401 for (uint32_t i = 0; ok() && i < globals_count; ++i) { 406 for (uint32_t i = 0; ok() && i < globals_count; ++i) {
402 TRACE("DecodeGlobal[%d] module+%d\n", i, 407 TRACE("DecodeGlobal[%d] module+%d\n", i,
403 static_cast<int>(pc_ - start_)); 408 static_cast<int>(pc_ - start_));
404 // Add an uninitialized global and pass a pointer to it. 409 // Add an uninitialized global and pass a pointer to it.
405 module->globals.push_back( 410 module->globals.push_back(
406 {kAstStmt, false, WasmInitExpr(), 0, false, false}); 411 {kAstStmt, false, WasmInitExpr(), 0, false, false});
407 WasmGlobal* global = &module->globals.back(); 412 WasmGlobal* global = &module->globals.back();
408 DecodeGlobalInModule(module, i, global); 413 DecodeGlobalInModule(module, i + imported_globals, global);
409 } 414 }
410 section_iter.advance(); 415 section_iter.advance();
411 } 416 }
412 417
413 // ===== Export section ================================================== 418 // ===== Export section ==================================================
414 if (section_iter.section_code() == kExportSectionCode) { 419 if (section_iter.section_code() == kExportSectionCode) {
415 uint32_t export_table_count = consume_u32v("export table count"); 420 uint32_t export_table_count = consume_u32v("export table count");
416 module->export_table.reserve(SafeReserve(export_table_count)); 421 module->export_table.reserve(SafeReserve(export_table_count));
417 for (uint32_t i = 0; ok() && i < export_table_count; ++i) { 422 for (uint32_t i = 0; ok() && i < export_table_count; ++i) {
418 TRACE("DecodeExportTable[%d] module+%d\n", i, 423 TRACE("DecodeExportTable[%d] module+%d\n", i,
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 PreinitializeIndirectFunctionTables(module); 615 PreinitializeIndirectFunctionTables(module);
611 } 616 }
612 const WasmModule* finished_module = module; 617 const WasmModule* finished_module = module;
613 ModuleResult result = toResult(finished_module); 618 ModuleResult result = toResult(finished_module);
614 if (FLAG_dump_wasm_module) DumpModule(module, result); 619 if (FLAG_dump_wasm_module) DumpModule(module, result);
615 return result; 620 return result;
616 } 621 }
617 622
618 uint32_t SafeReserve(uint32_t count) { 623 uint32_t SafeReserve(uint32_t count) {
619 // Avoid OOM by only reserving up to a certain size. 624 // Avoid OOM by only reserving up to a certain size.
620 const uint32_t kMaxReserve = 20000;
621 return count < kMaxReserve ? count : kMaxReserve; 625 return count < kMaxReserve ? count : kMaxReserve;
622 } 626 }
623 627
624 // Decodes a single anonymous function starting at {start_}. 628 // Decodes a single anonymous function starting at {start_}.
625 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, 629 FunctionResult DecodeSingleFunction(ModuleEnv* module_env,
626 WasmFunction* function) { 630 WasmFunction* function) {
627 pc_ = start_; 631 pc_ = start_;
628 function->sig = consume_sig(); // read signature 632 function->sig = consume_sig(); // read signature
629 function->name_offset = 0; // ---- name 633 function->name_offset = 0; // ---- name
630 function->name_length = 0; // ---- name length 634 function->name_length = 0; // ---- name length
(...skipping 18 matching lines...) Expand all
649 WasmInitExpr DecodeInitExpr(const byte* start) { 653 WasmInitExpr DecodeInitExpr(const byte* start) {
650 pc_ = start; 654 pc_ = start;
651 return consume_init_expr(nullptr, kAstStmt); 655 return consume_init_expr(nullptr, kAstStmt);
652 } 656 }
653 657
654 private: 658 private:
655 Zone* module_zone; 659 Zone* module_zone;
656 ModuleResult result_; 660 ModuleResult result_;
657 ModuleOrigin origin_; 661 ModuleOrigin origin_;
658 662
663 static const uint32_t kMaxReserve = 20000;
664
659 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } 665 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); }
660 666
661 // Decodes a single global entry inside a module starting at {pc_}. 667 // Decodes a single global entry inside a module starting at {pc_}.
662 void DecodeGlobalInModule(WasmModule* module, uint32_t index, 668 void DecodeGlobalInModule(WasmModule* module, uint32_t index,
663 WasmGlobal* global) { 669 WasmGlobal* global) {
664 global->type = consume_value_type(); 670 global->type = consume_value_type();
665 global->mutability = consume_u8("mutability") != 0; 671 global->mutability = consume_u8("mutability") != 0;
666 const byte* pos = pc(); 672 const byte* pos = pc();
667 global->init = consume_init_expr(module, kAstStmt); 673 global->init = consume_init_expr(module, kAstStmt);
668 switch (global->init.kind) { 674 switch (global->init.kind) {
669 case WasmInitExpr::kGlobalIndex: { 675 case WasmInitExpr::kGlobalIndex: {
670 uint32_t other_index = global->init.val.global_index; 676 uint32_t other_index = global->init.val.global_index;
671 if (other_index >= index) { 677 if (other_index >= index) {
672 error("invalid global index in init expression"); 678 error(pos, pos,
679 "invalid global index in init expression, "
680 "index %u, other_index %u",
681 index, other_index);
673 } else if (module->globals[other_index].type != global->type) { 682 } else if (module->globals[other_index].type != global->type) {
674 error(pos, pos, 683 error(pos, pos,
675 "type mismatch in global initialization " 684 "type mismatch in global initialization "
676 "(from global #%u), expected %s, got %s", 685 "(from global #%u), expected %s, got %s",
677 other_index, WasmOpcodes::TypeName(global->type), 686 other_index, WasmOpcodes::TypeName(global->type),
678 WasmOpcodes::TypeName(module->globals[other_index].type)); 687 WasmOpcodes::TypeName(module->globals[other_index].type));
679 } 688 }
680 break; 689 break;
681 } 690 }
682 default: 691 default:
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 decoder.consume_bytes(size); 1147 decoder.consume_bytes(size);
1139 } 1148 }
1140 if (decoder.more()) decoder.error("unexpected additional bytes"); 1149 if (decoder.more()) decoder.error("unexpected additional bytes");
1141 1150
1142 return decoder.toResult(std::move(table)); 1151 return decoder.toResult(std::move(table));
1143 } 1152 }
1144 1153
1145 } // namespace wasm 1154 } // namespace wasm
1146 } // namespace internal 1155 } // namespace internal
1147 } // namespace v8 1156 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/wasm/wasm-module.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698