OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 | 2 |
3 // Check that we can traverse very deep stacks of ConsStrings using | 3 // Check that we can traverse very deep stacks of ConsStrings using |
4 // StringInputBuffer. Check that Get(int) works on very deep stacks | 4 // StringInputBuffer. Check that Get(int) works on very deep stacks |
5 // of ConsStrings. These operations may not be very fast, but they | 5 // of ConsStrings. These operations may not be very fast, but they |
6 // should be possible without getting errors due to too deep recursion. | 6 // should be possible without getting errors due to too deep recursion. |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include "v8.h" | 10 #include "v8.h" |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 LocalContext context; | 613 LocalContext context; |
614 v8::V8::IgnoreOutOfMemoryException(); | 614 v8::V8::IgnoreOutOfMemoryException(); |
615 v8::Local<v8::Script> script = | 615 v8::Local<v8::Script> script = |
616 v8::Script::Compile(v8::String::New(join_causing_out_of_memory)); | 616 v8::Script::Compile(v8::String::New(join_causing_out_of_memory)); |
617 v8::Local<v8::Value> result = script->Run(); | 617 v8::Local<v8::Value> result = script->Run(); |
618 | 618 |
619 // Check for out of memory state. | 619 // Check for out of memory state. |
620 CHECK(result.IsEmpty()); | 620 CHECK(result.IsEmpty()); |
621 CHECK(context->HasOutOfMemoryException()); | 621 CHECK(context->HasOutOfMemoryException()); |
622 } | 622 } |
| 623 |
| 624 |
| 625 static void CheckException(const char* source) { |
| 626 // An empty handle is returned upon exception. |
| 627 CHECK(CompileRun(source).IsEmpty()); |
| 628 } |
| 629 |
| 630 |
| 631 TEST(RobustSubStringStub) { |
| 632 // This tests whether the SubStringStub can handle unsafe arguments. |
| 633 // If not recognized, those unsafe arguments lead to out-of-bounds reads. |
| 634 FLAG_allow_natives_syntax = true; |
| 635 InitializeVM(); |
| 636 HandleScope scope; |
| 637 v8::Local<v8::Value> result; |
| 638 Handle<String> string; |
| 639 CompileRun("var short = 'abcdef';"); |
| 640 |
| 641 // Invalid indices. |
| 642 CheckException("%_SubString(short, 0, 10000);"); |
| 643 CheckException("%_SubString(short, -1234, 5);"); |
| 644 CheckException("%_SubString(short, 5, 2);"); |
| 645 // Special HeapNumbers. |
| 646 CheckException("%_SubString(short, 1, Infinity);"); |
| 647 CheckException("%_SubString(short, NaN, 5);"); |
| 648 // String arguments. |
| 649 CheckException("%_SubString(short, '2', '5');"); |
| 650 // Ordinary HeapNumbers can be handled (in runtime). |
| 651 result = CompileRun("%_SubString(short, Math.sqrt(4), 5.1);"); |
| 652 string = v8::Utils::OpenHandle(v8::String::Cast(*result)); |
| 653 CHECK_EQ("cde", *(string->ToCString())); |
| 654 |
| 655 CompileRun("var long = 'abcdefghijklmnopqrstuvwxyz';"); |
| 656 // Invalid indices. |
| 657 CheckException("%_SubString(long, 0, 10000);"); |
| 658 CheckException("%_SubString(long, -1234, 17);"); |
| 659 CheckException("%_SubString(long, 17, 2);"); |
| 660 // Special HeapNumbers. |
| 661 CheckException("%_SubString(long, 1, Infinity);"); |
| 662 CheckException("%_SubString(long, NaN, 17);"); |
| 663 // String arguments. |
| 664 CheckException("%_SubString(long, '2', '17');"); |
| 665 // Ordinary HeapNumbers within bounds can be handled (in runtime). |
| 666 result = CompileRun("%_SubString(long, Math.sqrt(4), 17.1);"); |
| 667 string = v8::Utils::OpenHandle(v8::String::Cast(*result)); |
| 668 CHECK_EQ("cdefghijklmnopq", *(string->ToCString())); |
| 669 |
| 670 // Test that out-of-bounds substring of a slice fails when the indices |
| 671 // would have been valid for the underlying string. |
| 672 CompileRun("var slice = long.slice(1, 15);"); |
| 673 CheckException("%_SubString(slice, 0, 17);"); |
| 674 } |
OLD | NEW |