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

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

Issue 2052623003: [wasm] improve handling of malformed input (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove debugging statement Created 4 years, 6 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
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/signature.h" 5 #include "src/signature.h"
6 6
7 #include "src/bit-vector.h" 7 #include "src/bit-vector.h"
8 #include "src/flags.h" 8 #include "src/flags.h"
9 #include "src/handles.h" 9 #include "src/handles.h"
10 #include "src/zone-containers.h" 10 #include "src/zone-containers.h"
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 for (uint32_t i = 0; i < operand.table_count + 1; i++) { 231 for (uint32_t i = 0; i < operand.table_count + 1; i++) {
232 uint32_t target = operand.read_entry(this, i); 232 uint32_t target = operand.read_entry(this, i);
233 if (target >= block_depth) { 233 if (target >= block_depth) {
234 error(operand.table + i * 2, "improper branch in br_table"); 234 error(operand.table + i * 2, "improper branch in br_table");
235 return false; 235 return false;
236 } 236 }
237 } 237 }
238 return true; 238 return true;
239 } 239 }
240 240
241 int OpcodeArity(const byte* pc) { 241 unsigned int OpcodeArity(const byte* pc) {
242 #define DECLARE_ARITY(name, ...) \ 242 #define DECLARE_ARITY(name, ...) \
243 static const LocalType kTypes_##name[] = {__VA_ARGS__}; \ 243 static const LocalType kTypes_##name[] = {__VA_ARGS__}; \
244 static const int kArity_##name = \ 244 static const int kArity_##name = \
245 static_cast<int>(arraysize(kTypes_##name) - 1); 245 static_cast<int>(arraysize(kTypes_##name) - 1);
246 246
247 FOREACH_SIGNATURE(DECLARE_ARITY); 247 FOREACH_SIGNATURE(DECLARE_ARITY);
248 #undef DECLARE_ARITY 248 #undef DECLARE_ARITY
249 249
250 switch (static_cast<WasmOpcode>(*pc)) { 250 switch (static_cast<WasmOpcode>(*pc)) {
251 case kExprI8Const: 251 case kExprI8Const:
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 FOREACH_SIMPLE_OPCODE(DECLARE_OPCODE_CASE) 312 FOREACH_SIMPLE_OPCODE(DECLARE_OPCODE_CASE)
313 FOREACH_ASMJS_COMPAT_OPCODE(DECLARE_OPCODE_CASE) 313 FOREACH_ASMJS_COMPAT_OPCODE(DECLARE_OPCODE_CASE)
314 FOREACH_SIMD_OPCODE(DECLARE_OPCODE_CASE) 314 FOREACH_SIMD_OPCODE(DECLARE_OPCODE_CASE)
315 #undef DECLARE_OPCODE_CASE 315 #undef DECLARE_OPCODE_CASE
316 default: 316 default:
317 UNREACHABLE(); 317 UNREACHABLE();
318 return 0; 318 return 0;
319 } 319 }
320 } 320 }
321 321
322 int OpcodeLength(const byte* pc) { 322 unsigned int OpcodeLength(const byte* pc) {
323 switch (static_cast<WasmOpcode>(*pc)) { 323 switch (static_cast<WasmOpcode>(*pc)) {
324 #define DECLARE_OPCODE_CASE(name, opcode, sig) case kExpr##name: 324 #define DECLARE_OPCODE_CASE(name, opcode, sig) case kExpr##name:
325 FOREACH_LOAD_MEM_OPCODE(DECLARE_OPCODE_CASE) 325 FOREACH_LOAD_MEM_OPCODE(DECLARE_OPCODE_CASE)
326 FOREACH_STORE_MEM_OPCODE(DECLARE_OPCODE_CASE) 326 FOREACH_STORE_MEM_OPCODE(DECLARE_OPCODE_CASE)
327 #undef DECLARE_OPCODE_CASE 327 #undef DECLARE_OPCODE_CASE
328 { 328 {
329 MemoryAccessOperand operand(this, pc); 329 MemoryAccessOperand operand(this, pc);
330 return 1 + operand.length; 330 return 1 + operand.length;
331 } 331 }
332 case kExprBr: 332 case kExprBr:
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 void DecodeLocalDecls() { 556 void DecodeLocalDecls() {
557 DCHECK_EQ(0, local_type_vec_.size()); 557 DCHECK_EQ(0, local_type_vec_.size());
558 // Initialize {local_type_vec} from signature. 558 // Initialize {local_type_vec} from signature.
559 if (sig_) { 559 if (sig_) {
560 local_type_vec_.reserve(sig_->parameter_count()); 560 local_type_vec_.reserve(sig_->parameter_count());
561 for (size_t i = 0; i < sig_->parameter_count(); i++) { 561 for (size_t i = 0; i < sig_->parameter_count(); i++) {
562 local_type_vec_.push_back(sig_->GetParam(i)); 562 local_type_vec_.push_back(sig_->GetParam(i));
563 } 563 }
564 } 564 }
565 // Decode local declarations, if any. 565 // Decode local declarations, if any.
566 int length; 566 unsigned int length;
567 uint32_t entries = consume_u32v(&length, "local decls count"); 567 uint32_t entries = consume_u32v(&length, "local decls count");
568 while (entries-- > 0 && pc_ < limit_) { 568 while (entries-- > 0 && pc_ < limit_) {
569 uint32_t count = consume_u32v(&length, "local count"); 569 uint32_t count = consume_u32v(&length, "local count");
570 byte code = consume_u8("local type"); 570 byte code = consume_u8("local type");
571 LocalType type; 571 LocalType type;
572 switch (code) { 572 switch (code) {
573 case kLocalI32: 573 case kLocalI32:
574 type = kAstI32; 574 type = kAstI32;
575 break; 575 break;
576 case kLocalI64: 576 case kLocalI64:
(...skipping 17 matching lines...) Expand all
594 // Decodes the body of a function. 594 // Decodes the body of a function.
595 void DecodeFunctionBody() { 595 void DecodeFunctionBody() {
596 TRACE("wasm-decode %p...%p (module+%d, %d bytes) %s\n", 596 TRACE("wasm-decode %p...%p (module+%d, %d bytes) %s\n",
597 reinterpret_cast<const void*>(start_), 597 reinterpret_cast<const void*>(start_),
598 reinterpret_cast<const void*>(limit_), baserel(pc_), 598 reinterpret_cast<const void*>(limit_), baserel(pc_),
599 static_cast<int>(limit_ - start_), builder_ ? "graph building" : ""); 599 static_cast<int>(limit_ - start_), builder_ ? "graph building" : "");
600 600
601 if (pc_ >= limit_) return; // Nothing to do. 601 if (pc_ >= limit_) return; // Nothing to do.
602 602
603 while (true) { // decoding loop. 603 while (true) { // decoding loop.
604 int len = 1; 604 unsigned int len = 1;
605 WasmOpcode opcode = static_cast<WasmOpcode>(*pc_); 605 WasmOpcode opcode = static_cast<WasmOpcode>(*pc_);
606 TRACE(" @%-6d #%02x:%-20s|", startrel(pc_), opcode, 606 TRACE(" @%-6d #%02x:%-20s|", startrel(pc_), opcode,
607 WasmOpcodes::ShortOpcodeName(opcode)); 607 WasmOpcodes::ShortOpcodeName(opcode));
608 608
609 FunctionSig* sig = WasmOpcodes::Signature(opcode); 609 FunctionSig* sig = WasmOpcodes::Signature(opcode);
610 if (sig) { 610 if (sig) {
611 // Fast case of a simple operator. 611 // Fast case of a simple operator.
612 TFNode* node; 612 TFNode* node;
613 switch (sig->parameter_count()) { 613 switch (sig->parameter_count()) {
614 case 1: { 614 case 1: {
(...skipping 815 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 BitVector* AnalyzeLoopAssignment(const byte* pc) { 1430 BitVector* AnalyzeLoopAssignment(const byte* pc) {
1431 if (pc >= limit_) return nullptr; 1431 if (pc >= limit_) return nullptr;
1432 if (*pc != kExprLoop) return nullptr; 1432 if (*pc != kExprLoop) return nullptr;
1433 1433
1434 BitVector* assigned = 1434 BitVector* assigned =
1435 new (zone_) BitVector(static_cast<int>(local_type_vec_.size()), zone_); 1435 new (zone_) BitVector(static_cast<int>(local_type_vec_.size()), zone_);
1436 int depth = 0; 1436 int depth = 0;
1437 // Iteratively process all AST nodes nested inside the loop. 1437 // Iteratively process all AST nodes nested inside the loop.
1438 while (pc < limit_) { 1438 while (pc < limit_) {
1439 WasmOpcode opcode = static_cast<WasmOpcode>(*pc); 1439 WasmOpcode opcode = static_cast<WasmOpcode>(*pc);
1440 int length = 1; 1440 unsigned int length = 1;
1441 switch (opcode) { 1441 switch (opcode) {
1442 case kExprLoop: 1442 case kExprLoop:
1443 case kExprIf: 1443 case kExprIf:
1444 case kExprBlock: 1444 case kExprBlock:
1445 depth++; 1445 depth++;
1446 DCHECK_EQ(1, OpcodeLength(pc)); 1446 DCHECK_EQ(1, OpcodeLength(pc));
1447 break; 1447 break;
1448 case kExprSetLocal: { 1448 case kExprSetLocal: {
1449 LocalIndexOperand operand(this, pc); 1449 LocalIndexOperand operand(this, pc);
1450 if (assigned->length() > 0 && 1450 if (assigned->length() > 0 &&
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1508 PrintF("%s", WasmOpcodes::OpcodeName(tree.opcode())); 1508 PrintF("%s", WasmOpcodes::OpcodeName(tree.opcode()));
1509 if (tree.count > 0) os << "("; 1509 if (tree.count > 0) os << "(";
1510 for (uint32_t i = 0; i < tree.count; i++) { 1510 for (uint32_t i = 0; i < tree.count; i++) {
1511 if (i > 0) os << ", "; 1511 if (i > 0) os << ", ";
1512 os << *tree.children[i]; 1512 os << *tree.children[i];
1513 } 1513 }
1514 if (tree.count > 0) os << ")"; 1514 if (tree.count > 0) os << ")";
1515 return os; 1515 return os;
1516 } 1516 }
1517 1517
1518 int OpcodeLength(const byte* pc, const byte* end) { 1518 unsigned int OpcodeLength(const byte* pc, const byte* end) {
1519 WasmDecoder decoder(nullptr, nullptr, pc, end); 1519 WasmDecoder decoder(nullptr, nullptr, pc, end);
1520 return decoder.OpcodeLength(pc); 1520 return decoder.OpcodeLength(pc);
1521 } 1521 }
1522 1522
1523 int OpcodeArity(const byte* pc, const byte* end) { 1523 unsigned int OpcodeArity(const byte* pc, const byte* end) {
1524 WasmDecoder decoder(nullptr, nullptr, pc, end); 1524 WasmDecoder decoder(nullptr, nullptr, pc, end);
1525 return decoder.OpcodeArity(pc); 1525 return decoder.OpcodeArity(pc);
1526 } 1526 }
1527 1527
1528 void PrintAstForDebugging(const byte* start, const byte* end) { 1528 void PrintAstForDebugging(const byte* start, const byte* end) {
1529 FunctionBody body = {nullptr, nullptr, start, start, end}; 1529 FunctionBody body = {nullptr, nullptr, start, start, end};
1530 base::AccountingAllocator allocator; 1530 base::AccountingAllocator allocator;
1531 PrintAst(&allocator, body); 1531 PrintAst(&allocator, body);
1532 } 1532 }
1533 1533
(...skipping 23 matching lines...) Expand all
1557 1557
1558 for (const byte* locals = body.start; locals < pc; locals++) { 1558 for (const byte* locals = body.start; locals < pc; locals++) {
1559 printf(" 0x%02x,", *locals); 1559 printf(" 0x%02x,", *locals);
1560 } 1560 }
1561 os << std::endl; 1561 os << std::endl;
1562 } 1562 }
1563 1563
1564 os << "// body: \n"; 1564 os << "// body: \n";
1565 int control_depth = 0; 1565 int control_depth = 0;
1566 while (pc < body.end) { 1566 while (pc < body.end) {
1567 size_t length = decoder.OpcodeLength(pc); 1567 unsigned int length = decoder.OpcodeLength(pc);
1568 1568
1569 WasmOpcode opcode = static_cast<WasmOpcode>(*pc); 1569 WasmOpcode opcode = static_cast<WasmOpcode>(*pc);
1570 if (opcode == kExprElse) control_depth--; 1570 if (opcode == kExprElse) control_depth--;
1571 1571
1572 for (int i = 0; i < control_depth && i < 32; i++) printf(" "); 1572 for (int i = 0; i < control_depth && i < 32; i++) printf(" ");
1573 printf("k%s,", WasmOpcodes::OpcodeName(opcode)); 1573 printf("k%s,", WasmOpcodes::OpcodeName(opcode));
1574 1574
1575 for (size_t i = 1; i < length; i++) { 1575 for (size_t i = 1; i < length; i++) {
1576 printf(" 0x%02x,", pc[i]); 1576 printf(" 0x%02x,", pc[i]);
1577 } 1577 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, 1648 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals,
1649 const byte* start, const byte* end) { 1649 const byte* start, const byte* end) {
1650 FunctionBody body = {nullptr, nullptr, nullptr, start, end}; 1650 FunctionBody body = {nullptr, nullptr, nullptr, start, end};
1651 SR_WasmDecoder decoder(zone, nullptr, body); 1651 SR_WasmDecoder decoder(zone, nullptr, body);
1652 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); 1652 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals);
1653 } 1653 }
1654 1654
1655 } // namespace wasm 1655 } // namespace wasm
1656 } // namespace internal 1656 } // namespace internal
1657 } // namespace v8 1657 } // namespace v8
OLDNEW
« src/wasm/ast-decoder.h ('K') | « src/wasm/ast-decoder.h ('k') | src/wasm/decoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698