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/flags.h" | 9 #include "src/flags.h" |
10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 false}); // exported | 294 false}); // exported |
295 WasmFunction* function = &module->functions.back(); | 295 WasmFunction* function = &module->functions.back(); |
296 function->sig_index = consume_sig_index(module, &function->sig); | 296 function->sig_index = consume_sig_index(module, &function->sig); |
297 break; | 297 break; |
298 } | 298 } |
299 case kExternalTable: { | 299 case kExternalTable: { |
300 // ===== Imported table ========================================== | 300 // ===== Imported table ========================================== |
301 import->index = | 301 import->index = |
302 static_cast<uint32_t>(module->function_tables.size()); | 302 static_cast<uint32_t>(module->function_tables.size()); |
303 module->function_tables.push_back( | 303 module->function_tables.push_back( |
304 {0, 0, std::vector<int32_t>(), true, false}); | 304 {0, 0, std::vector<int32_t>(), true, false, SignatureMap()}); |
305 expect_u8("element type", 0x20); | 305 expect_u8("element type", 0x20); |
306 WasmIndirectFunctionTable* table = &module->function_tables.back(); | 306 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
307 consume_resizable_limits("element count", "elements", kMaxUInt32, | 307 consume_resizable_limits("element count", "elements", kMaxUInt32, |
308 &table->size, &table->max_size); | 308 &table->size, &table->max_size); |
309 break; | 309 break; |
310 } | 310 } |
311 case kExternalMemory: { | 311 case kExternalMemory: { |
312 // ===== Imported memory ========================================= | 312 // ===== Imported memory ========================================= |
313 consume_resizable_limits( | 313 consume_resizable_limits( |
314 "memory", "pages", WasmModule::kMaxLegalPages, | 314 "memory", "pages", WasmModule::kMaxLegalPages, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 } | 356 } |
357 | 357 |
358 // ===== Table section =================================================== | 358 // ===== Table section =================================================== |
359 if (section_iter.section_code() == kTableSectionCode) { | 359 if (section_iter.section_code() == kTableSectionCode) { |
360 const byte* pos = pc_; | 360 const byte* pos = pc_; |
361 uint32_t table_count = consume_u32v("table count"); | 361 uint32_t table_count = consume_u32v("table count"); |
362 // Require at most one table for now. | 362 // Require at most one table for now. |
363 if (table_count > 1) { | 363 if (table_count > 1) { |
364 error(pos, pos, "invalid table count %d, maximum 1", table_count); | 364 error(pos, pos, "invalid table count %d, maximum 1", table_count); |
365 } | 365 } |
| 366 if (module->function_tables.size() < 1) { |
| 367 module->function_tables.push_back( |
| 368 {0, 0, std::vector<int32_t>(), false, false, SignatureMap()}); |
| 369 } |
366 | 370 |
367 for (uint32_t i = 0; ok() && i < table_count; i++) { | 371 for (uint32_t i = 0; ok() && i < table_count; i++) { |
368 module->function_tables.push_back( | |
369 {0, 0, std::vector<int32_t>(), false, false}); | |
370 WasmIndirectFunctionTable* table = &module->function_tables.back(); | 372 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
371 expect_u8("table type", kWasmAnyFunctionTypeForm); | 373 expect_u8("table type", kWasmAnyFunctionTypeForm); |
372 consume_resizable_limits("table elements", "elements", kMaxUInt32, | 374 consume_resizable_limits("table elements", "elements", kMaxUInt32, |
373 &table->size, &table->max_size); | 375 &table->size, &table->max_size); |
374 } | 376 } |
375 section_iter.advance(); | 377 section_iter.advance(); |
376 } | 378 } |
377 | 379 |
378 // ===== Memory section ================================================== | 380 // ===== Memory section ================================================== |
379 if (section_iter.section_code() == kMemorySectionCode) { | 381 if (section_iter.section_code() == kMemorySectionCode) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 if (func && func->sig->parameter_count() > 0) { | 498 if (func && func->sig->parameter_count() > 0) { |
497 error(pos, "invalid start function: non-zero parameter count"); | 499 error(pos, "invalid start function: non-zero parameter count"); |
498 } | 500 } |
499 section_iter.advance(); | 501 section_iter.advance(); |
500 } | 502 } |
501 | 503 |
502 // ===== Elements section ================================================ | 504 // ===== Elements section ================================================ |
503 if (section_iter.section_code() == kElementSectionCode) { | 505 if (section_iter.section_code() == kElementSectionCode) { |
504 uint32_t element_count = consume_u32v("element count"); | 506 uint32_t element_count = consume_u32v("element count"); |
505 for (uint32_t i = 0; ok() && i < element_count; ++i) { | 507 for (uint32_t i = 0; ok() && i < element_count; ++i) { |
| 508 const byte* pos = pc(); |
506 uint32_t table_index = consume_u32v("table index"); | 509 uint32_t table_index = consume_u32v("table index"); |
507 if (table_index != 0) error("illegal table index != 0"); | 510 if (table_index != 0) { |
| 511 error(pos, pos, "illegal table index %u != 0", table_index); |
| 512 } |
| 513 WasmIndirectFunctionTable* table = nullptr; |
| 514 if (table_index >= module->function_tables.size()) { |
| 515 error(pos, pos, "out of bounds table index %u", table_index); |
| 516 } else { |
| 517 table = &module->function_tables[table_index]; |
| 518 } |
508 WasmInitExpr offset = consume_init_expr(module, kAstI32); | 519 WasmInitExpr offset = consume_init_expr(module, kAstI32); |
509 uint32_t num_elem = consume_u32v("number of elements"); | 520 uint32_t num_elem = consume_u32v("number of elements"); |
510 std::vector<uint32_t> vector; | 521 std::vector<uint32_t> vector; |
511 module->table_inits.push_back({table_index, offset, vector}); | 522 module->table_inits.push_back({table_index, offset, vector}); |
512 WasmTableInit* init = &module->table_inits.back(); | 523 WasmTableInit* init = &module->table_inits.back(); |
513 init->entries.reserve(SafeReserve(num_elem)); | 524 init->entries.reserve(SafeReserve(num_elem)); |
514 for (uint32_t j = 0; ok() && j < num_elem; j++) { | 525 for (uint32_t j = 0; ok() && j < num_elem; j++) { |
515 WasmFunction* func = nullptr; | 526 WasmFunction* func = nullptr; |
516 init->entries.push_back(consume_func_index(module, &func)); | 527 uint32_t index = consume_func_index(module, &func); |
| 528 init->entries.push_back(index); |
| 529 if (table && index < module->functions.size()) { |
| 530 // Canonicalize signature indices during decoding. |
| 531 // TODO(titzer): suboptimal, redundant when verifying only. |
| 532 table->map.FindOrInsert(module->functions[index].sig); |
| 533 } |
517 } | 534 } |
518 } | 535 } |
519 | 536 |
520 section_iter.advance(); | 537 section_iter.advance(); |
521 } | 538 } |
522 | 539 |
523 // ===== Code section ==================================================== | 540 // ===== Code section ==================================================== |
524 if (section_iter.section_code() == kCodeSectionCode) { | 541 if (section_iter.section_code() == kCodeSectionCode) { |
525 const byte* pos = pc_; | 542 const byte* pos = pc_; |
526 uint32_t functions_count = consume_u32v("functions count"); | 543 uint32_t functions_count = consume_u32v("functions count"); |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 decoder.consume_bytes(size); | 1138 decoder.consume_bytes(size); |
1122 } | 1139 } |
1123 if (decoder.more()) decoder.error("unexpected additional bytes"); | 1140 if (decoder.more()) decoder.error("unexpected additional bytes"); |
1124 | 1141 |
1125 return decoder.toResult(std::move(table)); | 1142 return decoder.toResult(std::move(table)); |
1126 } | 1143 } |
1127 | 1144 |
1128 } // namespace wasm | 1145 } // namespace wasm |
1129 } // namespace internal | 1146 } // namespace internal |
1130 } // namespace v8 | 1147 } // namespace v8 |
OLD | NEW |