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

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

Issue 2643783002: [wasm] check that there is at most 1 table (Closed)
Patch Set: Tweak module builder Created 3 years, 11 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 | test/mjsunit/wasm/indirect-tables.js » ('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 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 0, // code_start_offset 297 0, // code_start_offset
298 0, // code_end_offset 298 0, // code_end_offset
299 true, // imported 299 true, // imported
300 false}); // exported 300 false}); // exported
301 WasmFunction* function = &module->functions.back(); 301 WasmFunction* function = &module->functions.back();
302 function->sig_index = consume_sig_index(module, &function->sig); 302 function->sig_index = consume_sig_index(module, &function->sig);
303 break; 303 break;
304 } 304 }
305 case kExternalTable: { 305 case kExternalTable: {
306 // ===== Imported table ========================================== 306 // ===== Imported table ==========================================
307 if (!AddTable(module)) break;
307 import->index = 308 import->index =
308 static_cast<uint32_t>(module->function_tables.size()); 309 static_cast<uint32_t>(module->function_tables.size());
309 module->function_tables.push_back({0, 0, false, 310 module->function_tables.push_back({0, 0, false,
310 std::vector<int32_t>(), true, 311 std::vector<int32_t>(), true,
311 false, SignatureMap()}); 312 false, SignatureMap()});
312 expect_u8("element type", kWasmAnyFunctionTypeForm); 313 expect_u8("element type", kWasmAnyFunctionTypeForm);
313 WasmIndirectFunctionTable* table = &module->function_tables.back(); 314 WasmIndirectFunctionTable* table = &module->function_tables.back();
314 consume_resizable_limits("element count", "elements", 315 consume_resizable_limits("element count", "elements",
315 kV8MaxWasmTableSize, &table->min_size, 316 kV8MaxWasmTableSize, &table->min_size,
316 &table->has_max, kV8MaxWasmTableSize, 317 &table->has_max, kV8MaxWasmTableSize,
317 &table->max_size); 318 &table->max_size);
318 break; 319 break;
319 } 320 }
320 case kExternalMemory: { 321 case kExternalMemory: {
321 // ===== Imported memory ========================================= 322 // ===== Imported memory =========================================
323 if (!AddMemory(module)) break;
322 consume_resizable_limits( 324 consume_resizable_limits(
323 "memory", "pages", kV8MaxWasmMemoryPages, 325 "memory", "pages", kV8MaxWasmMemoryPages,
324 &module->min_mem_pages, &module->has_max_mem, 326 &module->min_mem_pages, &module->has_max_mem,
325 kSpecMaxWasmMemoryPages, &module->max_mem_pages); 327 kSpecMaxWasmMemoryPages, &module->max_mem_pages);
326 SetHasMemory(module);
327 break; 328 break;
328 } 329 }
329 case kExternalGlobal: { 330 case kExternalGlobal: {
330 // ===== Imported global ========================================= 331 // ===== Imported global =========================================
331 import->index = static_cast<uint32_t>(module->globals.size()); 332 import->index = static_cast<uint32_t>(module->globals.size());
332 module->globals.push_back( 333 module->globals.push_back(
333 {kWasmStmt, false, WasmInitExpr(), 0, true, false}); 334 {kWasmStmt, false, WasmInitExpr(), 0, true, false});
334 WasmGlobal* global = &module->globals.back(); 335 WasmGlobal* global = &module->globals.back();
335 global->type = consume_value_type(); 336 global->type = consume_value_type();
336 global->mutability = consume_mutability(); 337 global->mutability = consume_mutability();
(...skipping 29 matching lines...) Expand all
366 false}); // exported 367 false}); // exported
367 WasmFunction* function = &module->functions.back(); 368 WasmFunction* function = &module->functions.back();
368 function->sig_index = consume_sig_index(module, &function->sig); 369 function->sig_index = consume_sig_index(module, &function->sig);
369 } 370 }
370 section_iter.advance(); 371 section_iter.advance();
371 } 372 }
372 373
373 // ===== Table section =================================================== 374 // ===== Table section ===================================================
374 if (section_iter.section_code() == kTableSectionCode) { 375 if (section_iter.section_code() == kTableSectionCode) {
375 uint32_t table_count = consume_count("table count", kV8MaxWasmTables); 376 uint32_t table_count = consume_count("table count", kV8MaxWasmTables);
376 if (module->function_tables.size() < 1) { 377
378 for (uint32_t i = 0; ok() && i < table_count; i++) {
379 if (!AddTable(module)) break;
377 module->function_tables.push_back({0, 0, false, std::vector<int32_t>(), 380 module->function_tables.push_back({0, 0, false, std::vector<int32_t>(),
378 false, false, SignatureMap()}); 381 false, false, SignatureMap()});
379 }
380
381 for (uint32_t i = 0; ok() && i < table_count; i++) {
382 WasmIndirectFunctionTable* table = &module->function_tables.back(); 382 WasmIndirectFunctionTable* table = &module->function_tables.back();
383 expect_u8("table type", kWasmAnyFunctionTypeForm); 383 expect_u8("table type", kWasmAnyFunctionTypeForm);
384 consume_resizable_limits( 384 consume_resizable_limits(
385 "table elements", "elements", kV8MaxWasmTableSize, &table->min_size, 385 "table elements", "elements", kV8MaxWasmTableSize, &table->min_size,
386 &table->has_max, kV8MaxWasmTableSize, &table->max_size); 386 &table->has_max, kV8MaxWasmTableSize, &table->max_size);
387 } 387 }
388 section_iter.advance(); 388 section_iter.advance();
389 } 389 }
390 390
391 // ===== Memory section ================================================== 391 // ===== Memory section ==================================================
392 if (section_iter.section_code() == kMemorySectionCode) { 392 if (section_iter.section_code() == kMemorySectionCode) {
393 uint32_t memory_count = consume_count("memory count", kV8MaxWasmMemories); 393 uint32_t memory_count = consume_count("memory count", kV8MaxWasmMemories);
394 394
395 for (uint32_t i = 0; ok() && i < memory_count; i++) { 395 for (uint32_t i = 0; ok() && i < memory_count; i++) {
396 if (!AddMemory(module)) break;
396 consume_resizable_limits("memory", "pages", kV8MaxWasmMemoryPages, 397 consume_resizable_limits("memory", "pages", kV8MaxWasmMemoryPages,
397 &module->min_mem_pages, &module->has_max_mem, 398 &module->min_mem_pages, &module->has_max_mem,
398 kSpecMaxWasmMemoryPages, 399 kSpecMaxWasmMemoryPages,
399 &module->max_mem_pages); 400 &module->max_mem_pages);
400 } 401 }
401 SetHasMemory(module);
402 section_iter.advance(); 402 section_iter.advance();
403 } 403 }
404 404
405 // ===== Global section ================================================== 405 // ===== Global section ==================================================
406 if (section_iter.section_code() == kGlobalSectionCode) { 406 if (section_iter.section_code() == kGlobalSectionCode) {
407 uint32_t globals_count = 407 uint32_t globals_count =
408 consume_count("globals count", kV8MaxWasmGlobals); 408 consume_count("globals count", kV8MaxWasmGlobals);
409 uint32_t imported_globals = static_cast<uint32_t>(module->globals.size()); 409 uint32_t imported_globals = static_cast<uint32_t>(module->globals.size());
410 module->globals.reserve(imported_globals + globals_count); 410 module->globals.reserve(imported_globals + globals_count);
411 for (uint32_t i = 0; ok() && i < globals_count; ++i) { 411 for (uint32_t i = 0; ok() && i < globals_count; ++i) {
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 return consume_init_expr(nullptr, kWasmStmt); 676 return consume_init_expr(nullptr, kWasmStmt);
677 } 677 }
678 678
679 private: 679 private:
680 Zone* module_zone; 680 Zone* module_zone;
681 ModuleResult result_; 681 ModuleResult result_;
682 ModuleOrigin origin_; 682 ModuleOrigin origin_;
683 683
684 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } 684 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); }
685 685
686 void SetHasMemory(WasmModule* module) { 686 bool AddTable(WasmModule* module) {
687 if (module->has_memory) { 687 if (module->function_tables.size() > 0) {
688 error("At most one memory object is supported"); 688 error("At most one table is supported");
689 return false;
689 } else { 690 } else {
690 module->has_memory = true; 691 return true;
691 } 692 }
692 } 693 }
693 694
695 bool AddMemory(WasmModule* module) {
696 if (module->has_memory) {
697 error("At most one memory is supported");
698 return false;
699 } else {
700 module->has_memory = true;
701 return true;
702 }
703 }
704
694 // Decodes a single global entry inside a module starting at {pc_}. 705 // Decodes a single global entry inside a module starting at {pc_}.
695 void DecodeGlobalInModule(WasmModule* module, uint32_t index, 706 void DecodeGlobalInModule(WasmModule* module, uint32_t index,
696 WasmGlobal* global) { 707 WasmGlobal* global) {
697 global->type = consume_value_type(); 708 global->type = consume_value_type();
698 global->mutability = consume_mutability(); 709 global->mutability = consume_mutability();
699 const byte* pos = pc(); 710 const byte* pos = pc();
700 global->init = consume_init_expr(module, kWasmStmt); 711 global->init = consume_init_expr(module, kWasmStmt);
701 switch (global->init.kind) { 712 switch (global->init.kind) {
702 case WasmInitExpr::kGlobalIndex: { 713 case WasmInitExpr::kGlobalIndex: {
703 uint32_t other_index = global->init.val.global_index; 714 uint32_t other_index = global->init.val.global_index;
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 table.push_back(std::move(func_asm_offsets)); 1256 table.push_back(std::move(func_asm_offsets));
1246 } 1257 }
1247 if (decoder.more()) decoder.error("unexpected additional bytes"); 1258 if (decoder.more()) decoder.error("unexpected additional bytes");
1248 1259
1249 return decoder.toResult(std::move(table)); 1260 return decoder.toResult(std::move(table));
1250 } 1261 }
1251 1262
1252 } // namespace wasm 1263 } // namespace wasm
1253 } // namespace internal 1264 } // namespace internal
1254 } // namespace v8 1265 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/wasm/indirect-tables.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698