v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-mementos.cc
Go to the documentation of this file.
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "cctest.h"
29 
30 using namespace v8::internal;
31 
32 
33 static void SetUpNewSpaceWithPoisonedMementoAtTop() {
34  Isolate* isolate = CcTest::i_isolate();
35  Heap* heap = isolate->heap();
36  NewSpace* new_space = heap->new_space();
37 
38  // Make sure we can allocate some objects without causing a GC later.
40 
41  // Allocate a string, the GC may suspect a memento behind the string.
42  Handle<SeqOneByteString> string = isolate->factory()->NewRawOneByteString(12);
43  CHECK(*string);
44 
45  // Create an allocation memento behind the string with a garbage allocation
46  // site pointer.
47  AllocationMemento* memento =
48  reinterpret_cast<AllocationMemento*>(new_space->top() + kHeapObjectTag);
49  memento->set_map_no_write_barrier(heap->allocation_memento_map());
50  memento->set_allocation_site(
51  reinterpret_cast<AllocationSite*>(kHeapObjectTag), SKIP_WRITE_BARRIER);
52 }
53 
54 
55 TEST(Regress340063) {
57  if (!i::FLAG_allocation_site_pretenuring) return;
59 
60 
61  SetUpNewSpaceWithPoisonedMementoAtTop();
62 
63  // Call GC to see if we can handle a poisonous memento right after the
64  // current new space top pointer.
67 }
68 
69 
70 TEST(BadMementoAfterTopForceScavenge) {
72  if (!i::FLAG_allocation_site_pretenuring) return;
74 
75  SetUpNewSpaceWithPoisonedMementoAtTop();
76 
77  // Force GC to test the poisoned memento handling
79 }
80 
81 
82 TEST(PretenuringCallNew) {
84  if (!i::FLAG_allocation_site_pretenuring) return;
85  if (!i::FLAG_pretenuring_call_new) return;
86 
88  Isolate* isolate = CcTest::i_isolate();
89  Heap* heap = isolate->heap();
90 
91  // We need to create several instances to get past the slack-tracking
92  // phase, where mementos aren't emitted.
93  int call_count = 10;
95  i::ScopedVector<char> test_buf(1024);
96  const char* program =
97  "function f() {"
98  " this.a = 3;"
99  " this.b = {};"
100  " return this;"
101  "};"
102  "var a;"
103  "for(var i = 0; i < %d; i++) {"
104  " a = new f();"
105  "}"
106  "a;";
107  i::OS::SNPrintF(test_buf, program, call_count);
108  v8::Local<v8::Value> res = CompileRun(test_buf.start());
109  Handle<JSObject> o =
111 
112  // The object of class f should have a memento secreted behind it.
113  Address memento_address = o->address() + o->map()->instance_size();
114  AllocationMemento* memento =
115  reinterpret_cast<AllocationMemento*>(memento_address + kHeapObjectTag);
116  CHECK_EQ(memento->map(), heap->allocation_memento_map());
117 
118  // Furthermore, how many mementos did we create? The count should match
119  // call_count - SharedFunctionInfo::kGenerousAllocationCount.
120  AllocationSite* site = memento->GetAllocationSite();
122  site->pretenure_create_count()->value());
123 }
byte * Address
Definition: globals.h:186
static const int kGenerousAllocationCount
Definition: objects.h:6827
#define CHECK_EQ(expected, value)
Definition: checks.h:252
AllocationSite * GetAllocationSite()
Definition: objects.h:8452
void CollectAllGarbage(int flags, const char *gc_reason=NULL, const GCCallbackFlags gc_callback_flags=kNoGCCallbackFlags)
Definition: heap.cc:731
#define CHECK(condition)
Definition: checks.h:75
Factory * factory()
Definition: isolate.h:995
#define CHECK_GE(a, b)
Definition: checks.h:261
T * start() const
Definition: utils.h:426
const int kHeapObjectTag
Definition: v8.h:5473
static i::Isolate * i_isolate()
Definition: cctest.h:102
Definition: v8.h:123
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:308
void set_map_no_write_barrier(Map *value)
Definition: objects-inl.h:1352
static const int kAbortIncrementalMarkingMask
Definition: heap.h:1260
bool CollectGarbage(AllocationSpace space, const char *gc_reason=NULL, const GCCallbackFlags gc_callback_flags=kNoGCCallbackFlags)
Definition: heap-inl.h:554
static int SNPrintF(Vector< char > str, const char *format,...)
static void InitializeVM()
Definition: cctest.h:116
Handle< SeqOneByteString > NewRawOneByteString(int length, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:291
NewSpace * new_space()
Definition: heap.h:637
static v8::Isolate * isolate()
Definition: cctest.h:96