danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "v8.h" |
| 6 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7 | #include "compilation-cache.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8 | #include "execution.h" |
| 9 | #include "factory.h" |
| 10 | #include "macro-assembler.h" |
| 11 | #include "global-handles.h" |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12 | #include "stub-cache.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13 | #include "cctest.h" |
| 14 | |
| 15 | using namespace v8::internal; |
| 16 | |
| 17 | static v8::Persistent<v8::Context> env; |
| 18 | |
| 19 | static void InitializeVM() { |
| 20 | if (env.IsEmpty()) env = v8::Context::New(); |
| 21 | v8::HandleScope scope; |
| 22 | env->Enter(); |
| 23 | } |
| 24 | |
| 25 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 26 | // Go through all incremental marking steps in one swoop. |
| 27 | static void SimulateIncrementalMarking() { |
| 28 | IncrementalMarking* marking = HEAP->incremental_marking(); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 29 | CHECK(marking->IsMarking() || marking->IsStopped()); |
| 30 | if (marking->IsStopped()) { |
| 31 | marking->Start(); |
| 32 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 33 | CHECK(marking->IsMarking()); |
| 34 | while (!marking->IsComplete()) { |
| 35 | marking->Step(MB, IncrementalMarking::NO_GC_VIA_STACK_GUARD); |
| 36 | } |
| 37 | CHECK(marking->IsComplete()); |
| 38 | } |
| 39 | |
| 40 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 41 | static void CheckMap(Map* map, int type, int instance_size) { |
| 42 | CHECK(map->IsHeapObject()); |
| 43 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | CHECK(HEAP->Contains(map)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 45 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 46 | CHECK_EQ(HEAP->meta_map(), map->map()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 47 | CHECK_EQ(type, map->instance_type()); |
| 48 | CHECK_EQ(instance_size, map->instance_size()); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | TEST(HeapMaps) { |
| 53 | InitializeVM(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 54 | CheckMap(HEAP->meta_map(), MAP_TYPE, Map::kSize); |
| 55 | CheckMap(HEAP->heap_number_map(), HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 56 | CheckMap(HEAP->fixed_array_map(), FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 57 | CheckMap(HEAP->string_map(), STRING_TYPE, kVariableSizeSentinel); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 61 | static void CheckOddball(Isolate* isolate, Object* obj, const char* string) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 62 | CHECK(obj->IsOddball()); |
| 63 | bool exc; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 64 | Object* print_string = |
| 65 | *Execution::ToString(Handle<Object>(obj, isolate), &exc); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 66 | CHECK(String::cast(print_string)->IsUtf8EqualTo(CStrVector(string))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 70 | static void CheckSmi(Isolate* isolate, int value, const char* string) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 71 | bool exc; |
| 72 | Object* print_string = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 73 | *Execution::ToString(Handle<Object>(Smi::FromInt(value), isolate), &exc); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 74 | CHECK(String::cast(print_string)->IsUtf8EqualTo(CStrVector(string))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 78 | static void CheckNumber(Isolate* isolate, double value, const char* string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 79 | Object* obj = HEAP->NumberFromDouble(value)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 80 | CHECK(obj->IsNumber()); |
| 81 | bool exc; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 82 | Object* print_string = |
| 83 | *Execution::ToString(Handle<Object>(obj, isolate), &exc); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 84 | CHECK(String::cast(print_string)->IsUtf8EqualTo(CStrVector(string))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 88 | static void CheckFindCodeObject(Isolate* isolate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | // Test FindCodeObject |
| 90 | #define __ assm. |
| 91 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 92 | Assembler assm(isolate, NULL, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 93 | |
| 94 | __ nop(); // supported on all architectures |
| 95 | |
| 96 | CodeDesc desc; |
| 97 | assm.GetCode(&desc); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 98 | Heap* heap = isolate->heap(); |
| 99 | Object* code = heap->CreateCode( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 100 | desc, |
| 101 | Code::ComputeFlags(Code::STUB), |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 102 | Handle<Code>())->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 103 | CHECK(code->IsCode()); |
| 104 | |
| 105 | HeapObject* obj = HeapObject::cast(code); |
| 106 | Address obj_addr = obj->address(); |
| 107 | |
| 108 | for (int i = 0; i < obj->Size(); i += kPointerSize) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 109 | Object* found = heap->FindCodeObject(obj_addr + i); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 110 | CHECK_EQ(code, found); |
| 111 | } |
| 112 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 113 | Object* copy = heap->CreateCode( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 114 | desc, |
| 115 | Code::ComputeFlags(Code::STUB), |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 116 | Handle<Code>())->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 117 | CHECK(copy->IsCode()); |
| 118 | HeapObject* obj_copy = HeapObject::cast(copy); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 119 | Object* not_right = heap->FindCodeObject(obj_copy->address() + |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 120 | obj_copy->Size() / 2); |
| 121 | CHECK(not_right != code); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | TEST(HeapObjects) { |
| 126 | InitializeVM(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 127 | Isolate* isolate = Isolate::Current(); |
| 128 | Heap* heap = isolate->heap(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 129 | |
| 130 | v8::HandleScope sc; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 131 | Object* value = heap->NumberFromDouble(1.000123)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 132 | CHECK(value->IsHeapNumber()); |
| 133 | CHECK(value->IsNumber()); |
| 134 | CHECK_EQ(1.000123, value->Number()); |
| 135 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 136 | value = heap->NumberFromDouble(1.0)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 137 | CHECK(value->IsSmi()); |
| 138 | CHECK(value->IsNumber()); |
| 139 | CHECK_EQ(1.0, value->Number()); |
| 140 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 141 | value = heap->NumberFromInt32(1024)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 142 | CHECK(value->IsSmi()); |
| 143 | CHECK(value->IsNumber()); |
| 144 | CHECK_EQ(1024.0, value->Number()); |
| 145 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 146 | value = heap->NumberFromInt32(Smi::kMinValue)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 147 | CHECK(value->IsSmi()); |
| 148 | CHECK(value->IsNumber()); |
| 149 | CHECK_EQ(Smi::kMinValue, Smi::cast(value)->value()); |
| 150 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 151 | value = heap->NumberFromInt32(Smi::kMaxValue)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 152 | CHECK(value->IsSmi()); |
| 153 | CHECK(value->IsNumber()); |
| 154 | CHECK_EQ(Smi::kMaxValue, Smi::cast(value)->value()); |
| 155 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 156 | #ifndef V8_TARGET_ARCH_X64 |
| 157 | // TODO(lrn): We need a NumberFromIntptr function in order to test this. |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 158 | value = heap->NumberFromInt32(Smi::kMinValue - 1)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 159 | CHECK(value->IsHeapNumber()); |
| 160 | CHECK(value->IsNumber()); |
| 161 | CHECK_EQ(static_cast<double>(Smi::kMinValue - 1), value->Number()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 162 | #endif |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 163 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 164 | MaybeObject* maybe_value = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 165 | heap->NumberFromUint32(static_cast<uint32_t>(Smi::kMaxValue) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 166 | value = maybe_value->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 167 | CHECK(value->IsHeapNumber()); |
| 168 | CHECK(value->IsNumber()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 169 | CHECK_EQ(static_cast<double>(static_cast<uint32_t>(Smi::kMaxValue) + 1), |
| 170 | value->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 171 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 172 | maybe_value = heap->NumberFromUint32(static_cast<uint32_t>(1) << 31); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 173 | value = maybe_value->ToObjectChecked(); |
| 174 | CHECK(value->IsHeapNumber()); |
| 175 | CHECK(value->IsNumber()); |
| 176 | CHECK_EQ(static_cast<double>(static_cast<uint32_t>(1) << 31), |
| 177 | value->Number()); |
| 178 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 179 | // nan oddball checks |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 180 | CHECK(heap->nan_value()->IsNumber()); |
| 181 | CHECK(isnan(heap->nan_value()->Number())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 182 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 183 | Handle<String> s = FACTORY->NewStringFromAscii(CStrVector("fisk hest ")); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 184 | CHECK(s->IsString()); |
| 185 | CHECK_EQ(10, s->length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 186 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 187 | String* object_string = String::cast(heap->Object_string()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 188 | CHECK( |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 189 | Isolate::Current()->context()->global_object()->HasLocalProperty( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 190 | object_string)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 191 | |
| 192 | // Check ToString for oddballs |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 193 | CheckOddball(isolate, heap->true_value(), "true"); |
| 194 | CheckOddball(isolate, heap->false_value(), "false"); |
| 195 | CheckOddball(isolate, heap->null_value(), "null"); |
| 196 | CheckOddball(isolate, heap->undefined_value(), "undefined"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 197 | |
| 198 | // Check ToString for Smis |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 199 | CheckSmi(isolate, 0, "0"); |
| 200 | CheckSmi(isolate, 42, "42"); |
| 201 | CheckSmi(isolate, -42, "-42"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 202 | |
| 203 | // Check ToString for Numbers |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 204 | CheckNumber(isolate, 1.1, "1.1"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 205 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 206 | CheckFindCodeObject(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
| 210 | TEST(Tagging) { |
| 211 | InitializeVM(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 212 | int request = 24; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 213 | CHECK_EQ(request, static_cast<int>(OBJECT_POINTER_ALIGN(request))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 214 | CHECK(Smi::FromInt(42)->IsSmi()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 215 | CHECK(Failure::RetryAfterGC(NEW_SPACE)->IsFailure()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 216 | CHECK_EQ(NEW_SPACE, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 217 | Failure::RetryAfterGC(NEW_SPACE)->allocation_space()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 218 | CHECK_EQ(OLD_POINTER_SPACE, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 219 | Failure::RetryAfterGC(OLD_POINTER_SPACE)->allocation_space()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 220 | CHECK(Failure::Exception()->IsFailure()); |
| 221 | CHECK(Smi::FromInt(Smi::kMinValue)->IsSmi()); |
| 222 | CHECK(Smi::FromInt(Smi::kMaxValue)->IsSmi()); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | TEST(GarbageCollection) { |
| 227 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 228 | Isolate* isolate = Isolate::Current(); |
| 229 | Heap* heap = isolate->heap(); |
| 230 | Factory* factory = isolate->factory(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 231 | |
| 232 | v8::HandleScope sc; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 233 | // Check GC. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 234 | heap->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 235 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 236 | Handle<String> name = factory->InternalizeUtf8String("theFunction"); |
| 237 | Handle<String> prop_name = factory->InternalizeUtf8String("theSlot"); |
| 238 | Handle<String> prop_namex = factory->InternalizeUtf8String("theSlotx"); |
| 239 | Handle<String> obj_name = factory->InternalizeUtf8String("theObject"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 240 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 241 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 242 | HandleScope inner_scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 243 | // Allocate a function and keep it in global object's property. |
| 244 | Handle<JSFunction> function = |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 245 | factory->NewFunction(name, factory->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 246 | Handle<Map> initial_map = |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 247 | factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 248 | function->set_initial_map(*initial_map); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 249 | Isolate::Current()->context()->global_object()->SetProperty( |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 250 | *name, *function, NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 251 | // Allocate an object. Unrooted after leaving the scope. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 252 | Handle<JSObject> obj = factory->NewJSObject(function); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 253 | obj->SetProperty( |
| 254 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
| 255 | obj->SetProperty( |
| 256 | *prop_namex, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 257 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 258 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
| 259 | CHECK_EQ(Smi::FromInt(24), obj->GetProperty(*prop_namex)); |
| 260 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 261 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 262 | heap->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 263 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 264 | // Function should be alive. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 265 | CHECK(Isolate::Current()->context()->global_object()-> |
| 266 | HasLocalProperty(*name)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 267 | // Check function is retained. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 268 | Object* func_value = Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 269 | GetProperty(*name)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 270 | CHECK(func_value->IsJSFunction()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 271 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 272 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 273 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 274 | HandleScope inner_scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 275 | // Allocate another object, make it reachable from global. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 276 | Handle<JSObject> obj = factory->NewJSObject(function); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 277 | Isolate::Current()->context()->global_object()->SetProperty( |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 278 | *obj_name, *obj, NONE, kNonStrictMode)->ToObjectChecked(); |
| 279 | obj->SetProperty( |
| 280 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 281 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 282 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 283 | // After gc, it should survive. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 284 | heap->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 285 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 286 | CHECK(Isolate::Current()->context()->global_object()-> |
| 287 | HasLocalProperty(*obj_name)); |
| 288 | CHECK(Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 289 | GetProperty(*obj_name)->ToObjectChecked()->IsJSObject()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 290 | Object* obj = Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 291 | GetProperty(*obj_name)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 292 | JSObject* js_obj = JSObject::cast(obj); |
| 293 | CHECK_EQ(Smi::FromInt(23), js_obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | static void VerifyStringAllocation(const char* string) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 298 | v8::HandleScope scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 299 | Handle<String> s = FACTORY->NewStringFromUtf8(CStrVector(string)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 300 | CHECK_EQ(StrLength(string), s->length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 301 | for (int index = 0; index < s->length(); index++) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 302 | CHECK_EQ(static_cast<uint16_t>(string[index]), s->Get(index)); |
| 303 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | TEST(String) { |
| 308 | InitializeVM(); |
| 309 | |
| 310 | VerifyStringAllocation("a"); |
| 311 | VerifyStringAllocation("ab"); |
| 312 | VerifyStringAllocation("abc"); |
| 313 | VerifyStringAllocation("abcd"); |
| 314 | VerifyStringAllocation("fiskerdrengen er paa havet"); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | TEST(LocalHandles) { |
| 319 | InitializeVM(); |
| 320 | |
| 321 | v8::HandleScope scope; |
| 322 | const char* name = "Kasper the spunky"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 323 | Handle<String> string = FACTORY->NewStringFromAscii(CStrVector(name)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 324 | CHECK_EQ(StrLength(name), string->length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | TEST(GlobalHandles) { |
| 329 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 330 | Isolate* isolate = Isolate::Current(); |
| 331 | Heap* heap = isolate->heap(); |
| 332 | Factory* factory = isolate->factory(); |
| 333 | GlobalHandles* global_handles = isolate->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 334 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 335 | Handle<Object> h1; |
| 336 | Handle<Object> h2; |
| 337 | Handle<Object> h3; |
| 338 | Handle<Object> h4; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 339 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 340 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 341 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 342 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 343 | Handle<Object> i = factory->NewStringFromAscii(CStrVector("fisk")); |
| 344 | Handle<Object> u = factory->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 345 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 346 | h1 = global_handles->Create(*i); |
| 347 | h2 = global_handles->Create(*u); |
| 348 | h3 = global_handles->Create(*i); |
| 349 | h4 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 350 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 351 | |
| 352 | // after gc, it should survive |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 353 | heap->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 354 | |
| 355 | CHECK((*h1)->IsString()); |
| 356 | CHECK((*h2)->IsHeapNumber()); |
| 357 | CHECK((*h3)->IsString()); |
| 358 | CHECK((*h4)->IsHeapNumber()); |
| 359 | |
| 360 | CHECK_EQ(*h3, *h1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 361 | global_handles->Destroy(h1.location()); |
| 362 | global_handles->Destroy(h3.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 363 | |
| 364 | CHECK_EQ(*h4, *h2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 365 | global_handles->Destroy(h2.location()); |
| 366 | global_handles->Destroy(h4.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | static bool WeakPointerCleared = false; |
| 371 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 372 | static void TestWeakGlobalHandleCallback(v8::Isolate* isolate, |
| 373 | v8::Persistent<v8::Value> handle, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 374 | void* id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 375 | if (1234 == reinterpret_cast<intptr_t>(id)) WeakPointerCleared = true; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 376 | handle.Dispose(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
| 380 | TEST(WeakGlobalHandlesScavenge) { |
| 381 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 382 | Isolate* isolate = Isolate::Current(); |
| 383 | Heap* heap = isolate->heap(); |
| 384 | Factory* factory = isolate->factory(); |
| 385 | GlobalHandles* global_handles = isolate->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 386 | |
| 387 | WeakPointerCleared = false; |
| 388 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 389 | Handle<Object> h1; |
| 390 | Handle<Object> h2; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 391 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 392 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 393 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 394 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 395 | Handle<Object> i = factory->NewStringFromAscii(CStrVector("fisk")); |
| 396 | Handle<Object> u = factory->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 397 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 398 | h1 = global_handles->Create(*i); |
| 399 | h2 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 400 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 401 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 402 | global_handles->MakeWeak(h2.location(), |
| 403 | reinterpret_cast<void*>(1234), |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 404 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 405 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 406 | |
| 407 | // Scavenge treats weak pointers as normal roots. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 408 | heap->PerformScavenge(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 409 | |
| 410 | CHECK((*h1)->IsString()); |
| 411 | CHECK((*h2)->IsHeapNumber()); |
| 412 | |
| 413 | CHECK(!WeakPointerCleared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 414 | CHECK(!global_handles->IsNearDeath(h2.location())); |
| 415 | CHECK(!global_handles->IsNearDeath(h1.location())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 416 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | global_handles->Destroy(h1.location()); |
| 418 | global_handles->Destroy(h2.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
| 422 | TEST(WeakGlobalHandlesMark) { |
| 423 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 424 | Isolate* isolate = Isolate::Current(); |
| 425 | Heap* heap = isolate->heap(); |
| 426 | Factory* factory = isolate->factory(); |
| 427 | GlobalHandles* global_handles = isolate->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 428 | |
| 429 | WeakPointerCleared = false; |
| 430 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 431 | Handle<Object> h1; |
| 432 | Handle<Object> h2; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 433 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 434 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 435 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 436 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 437 | Handle<Object> i = factory->NewStringFromAscii(CStrVector("fisk")); |
| 438 | Handle<Object> u = factory->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 439 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 440 | h1 = global_handles->Create(*i); |
| 441 | h2 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 442 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 443 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 444 | // Make sure the objects are promoted. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 445 | heap->CollectGarbage(OLD_POINTER_SPACE); |
| 446 | heap->CollectGarbage(NEW_SPACE); |
| 447 | CHECK(!heap->InNewSpace(*h1) && !heap->InNewSpace(*h2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 448 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 449 | global_handles->MakeWeak(h2.location(), |
| 450 | reinterpret_cast<void*>(1234), |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 451 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 452 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 453 | CHECK(!GlobalHandles::IsNearDeath(h1.location())); |
| 454 | CHECK(!GlobalHandles::IsNearDeath(h2.location())); |
| 455 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 456 | // Incremental marking potentially marked handles before they turned weak. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 457 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 458 | |
| 459 | CHECK((*h1)->IsString()); |
| 460 | |
| 461 | CHECK(WeakPointerCleared); |
| 462 | CHECK(!GlobalHandles::IsNearDeath(h1.location())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 463 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 464 | global_handles->Destroy(h1.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 465 | } |
| 466 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 467 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 468 | TEST(DeleteWeakGlobalHandle) { |
| 469 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 470 | Isolate* isolate = Isolate::Current(); |
| 471 | Heap* heap = isolate->heap(); |
| 472 | Factory* factory = isolate->factory(); |
| 473 | GlobalHandles* global_handles = isolate->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 474 | |
| 475 | WeakPointerCleared = false; |
| 476 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 477 | Handle<Object> h; |
| 478 | |
| 479 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 480 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 481 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 482 | Handle<Object> i = factory->NewStringFromAscii(CStrVector("fisk")); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 483 | h = global_handles->Create(*i); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 484 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 485 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 486 | global_handles->MakeWeak(h.location(), |
| 487 | reinterpret_cast<void*>(1234), |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 488 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 489 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 490 | |
| 491 | // Scanvenge does not recognize weak reference. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 492 | heap->PerformScavenge(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 493 | |
| 494 | CHECK(!WeakPointerCleared); |
| 495 | |
| 496 | // Mark-compact treats weak reference properly. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 497 | heap->CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 498 | |
| 499 | CHECK(WeakPointerCleared); |
| 500 | } |
| 501 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 502 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 503 | static const char* not_so_random_string_table[] = { |
| 504 | "abstract", |
| 505 | "boolean", |
| 506 | "break", |
| 507 | "byte", |
| 508 | "case", |
| 509 | "catch", |
| 510 | "char", |
| 511 | "class", |
| 512 | "const", |
| 513 | "continue", |
| 514 | "debugger", |
| 515 | "default", |
| 516 | "delete", |
| 517 | "do", |
| 518 | "double", |
| 519 | "else", |
| 520 | "enum", |
| 521 | "export", |
| 522 | "extends", |
| 523 | "false", |
| 524 | "final", |
| 525 | "finally", |
| 526 | "float", |
| 527 | "for", |
| 528 | "function", |
| 529 | "goto", |
| 530 | "if", |
| 531 | "implements", |
| 532 | "import", |
| 533 | "in", |
| 534 | "instanceof", |
| 535 | "int", |
| 536 | "interface", |
| 537 | "long", |
| 538 | "native", |
| 539 | "new", |
| 540 | "null", |
| 541 | "package", |
| 542 | "private", |
| 543 | "protected", |
| 544 | "public", |
| 545 | "return", |
| 546 | "short", |
| 547 | "static", |
| 548 | "super", |
| 549 | "switch", |
| 550 | "synchronized", |
| 551 | "this", |
| 552 | "throw", |
| 553 | "throws", |
| 554 | "transient", |
| 555 | "true", |
| 556 | "try", |
| 557 | "typeof", |
| 558 | "var", |
| 559 | "void", |
| 560 | "volatile", |
| 561 | "while", |
| 562 | "with", |
| 563 | 0 |
| 564 | }; |
| 565 | |
| 566 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 567 | static void CheckInternalizedStrings(const char** strings) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 568 | for (const char* string = *strings; *strings != 0; string = *strings++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 569 | Object* a; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 570 | MaybeObject* maybe_a = HEAP->InternalizeUtf8String(string); |
| 571 | // InternalizeUtf8String may return a failure if a GC is needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 572 | if (!maybe_a->ToObject(&a)) continue; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 573 | CHECK(a->IsInternalizedString()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 574 | Object* b; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 575 | MaybeObject* maybe_b = HEAP->InternalizeUtf8String(string); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 576 | if (!maybe_b->ToObject(&b)) continue; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 577 | CHECK_EQ(b, a); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 578 | CHECK(String::cast(b)->IsUtf8EqualTo(CStrVector(string))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 583 | TEST(StringTable) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 584 | InitializeVM(); |
| 585 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 586 | CheckInternalizedStrings(not_so_random_string_table); |
| 587 | CheckInternalizedStrings(not_so_random_string_table); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | TEST(FunctionAllocation) { |
| 592 | InitializeVM(); |
| 593 | |
| 594 | v8::HandleScope sc; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 595 | Handle<String> name = FACTORY->InternalizeUtf8String("theFunction"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 596 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 597 | FACTORY->NewFunction(name, FACTORY->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 598 | Handle<Map> initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 599 | FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 600 | function->set_initial_map(*initial_map); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 601 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 602 | Handle<String> prop_name = FACTORY->InternalizeUtf8String("theSlot"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 603 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 604 | obj->SetProperty( |
| 605 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 606 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 607 | // Check that we can add properties to function objects. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 608 | function->SetProperty( |
| 609 | *prop_name, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 610 | CHECK_EQ(Smi::FromInt(24), function->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | |
| 614 | TEST(ObjectProperties) { |
| 615 | InitializeVM(); |
| 616 | |
| 617 | v8::HandleScope sc; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 618 | String* object_string = String::cast(HEAP->Object_string()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 619 | Object* raw_object = Isolate::Current()->context()->global_object()-> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 620 | GetProperty(object_string)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 621 | JSFunction* object_function = JSFunction::cast(raw_object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 622 | Handle<JSFunction> constructor(object_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 623 | Handle<JSObject> obj = FACTORY->NewJSObject(constructor); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 624 | Handle<String> first = FACTORY->InternalizeUtf8String("first"); |
| 625 | Handle<String> second = FACTORY->InternalizeUtf8String("second"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 626 | |
| 627 | // check for empty |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 628 | CHECK(!obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 629 | |
| 630 | // add first |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 631 | obj->SetProperty( |
| 632 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 633 | CHECK(obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 634 | |
| 635 | // delete first |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 636 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 637 | CHECK(!obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 638 | |
| 639 | // add first and then second |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 640 | obj->SetProperty( |
| 641 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 642 | obj->SetProperty( |
| 643 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 644 | CHECK(obj->HasLocalProperty(*first)); |
| 645 | CHECK(obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 646 | |
| 647 | // delete first and then second |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 648 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 649 | CHECK(obj->HasLocalProperty(*second)); |
| 650 | CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION)); |
| 651 | CHECK(!obj->HasLocalProperty(*first)); |
| 652 | CHECK(!obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 653 | |
| 654 | // add first and then second |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 655 | obj->SetProperty( |
| 656 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 657 | obj->SetProperty( |
| 658 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 659 | CHECK(obj->HasLocalProperty(*first)); |
| 660 | CHECK(obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 661 | |
| 662 | // delete second and then first |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 663 | CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION)); |
| 664 | CHECK(obj->HasLocalProperty(*first)); |
| 665 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 666 | CHECK(!obj->HasLocalProperty(*first)); |
| 667 | CHECK(!obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 668 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 669 | // check string and internalized string match |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 670 | const char* string1 = "fisk"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 671 | Handle<String> s1 = FACTORY->NewStringFromAscii(CStrVector(string1)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 672 | obj->SetProperty( |
| 673 | *s1, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 674 | Handle<String> s1_string = FACTORY->InternalizeUtf8String(string1); |
| 675 | CHECK(obj->HasLocalProperty(*s1_string)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 676 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 677 | // check internalized string and string match |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 678 | const char* string2 = "fugl"; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 679 | Handle<String> s2_string = FACTORY->InternalizeUtf8String(string2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 680 | obj->SetProperty( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 681 | *s2_string, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 682 | Handle<String> s2 = FACTORY->NewStringFromAscii(CStrVector(string2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 683 | CHECK(obj->HasLocalProperty(*s2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | |
| 687 | TEST(JSObjectMaps) { |
| 688 | InitializeVM(); |
| 689 | |
| 690 | v8::HandleScope sc; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 691 | Handle<String> name = FACTORY->InternalizeUtf8String("theFunction"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 692 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 693 | FACTORY->NewFunction(name, FACTORY->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 694 | Handle<Map> initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 695 | FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 696 | function->set_initial_map(*initial_map); |
| 697 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 698 | Handle<String> prop_name = FACTORY->InternalizeUtf8String("theSlot"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 699 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 700 | |
| 701 | // Set a propery |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 702 | obj->SetProperty( |
| 703 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 704 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 705 | |
| 706 | // Check the map has changed |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 707 | CHECK(*initial_map != obj->map()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | |
| 711 | TEST(JSArray) { |
| 712 | InitializeVM(); |
| 713 | |
| 714 | v8::HandleScope sc; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 715 | Handle<String> name = FACTORY->InternalizeUtf8String("Array"); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 716 | Object* raw_object = Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 717 | GetProperty(*name)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 718 | Handle<JSFunction> function = Handle<JSFunction>( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 719 | JSFunction::cast(raw_object)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 720 | |
| 721 | // Allocate the object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 722 | Handle<JSObject> object = FACTORY->NewJSObject(function); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 723 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 724 | // We just initialized the VM, no heap allocation failure yet. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 725 | array->Initialize(0)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 726 | |
| 727 | // Set array length to 0. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 728 | array->SetElementsLength(Smi::FromInt(0))->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 729 | CHECK_EQ(Smi::FromInt(0), array->length()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 730 | // Must be in fast mode. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 731 | CHECK(array->HasFastSmiOrObjectElements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 732 | |
| 733 | // array[length] = name. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 734 | array->SetElement(0, *name, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 735 | CHECK_EQ(Smi::FromInt(1), array->length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 736 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 737 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 738 | // Set array length with larger than smi value. |
| 739 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 740 | FACTORY->NewNumberFromUint(static_cast<uint32_t>(Smi::kMaxValue) + 1); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 741 | array->SetElementsLength(*length)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 742 | |
| 743 | uint32_t int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 744 | CHECK(length->ToArrayIndex(&int_length)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 745 | CHECK_EQ(*length, array->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 746 | CHECK(array->HasDictionaryElements()); // Must be in slow mode. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 747 | |
| 748 | // array[length] = name. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 749 | array->SetElement(int_length, *name, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 750 | uint32_t new_int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 751 | CHECK(array->length()->ToArrayIndex(&new_int_length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 752 | CHECK_EQ(static_cast<double>(int_length), new_int_length - 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 753 | CHECK_EQ(array->GetElement(int_length), *name); |
| 754 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | TEST(JSObjectCopy) { |
| 759 | InitializeVM(); |
| 760 | |
| 761 | v8::HandleScope sc; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 762 | String* object_string = String::cast(HEAP->Object_string()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 763 | Object* raw_object = Isolate::Current()->context()->global_object()-> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 764 | GetProperty(object_string)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 765 | JSFunction* object_function = JSFunction::cast(raw_object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 766 | Handle<JSFunction> constructor(object_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 767 | Handle<JSObject> obj = FACTORY->NewJSObject(constructor); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 768 | Handle<String> first = FACTORY->InternalizeUtf8String("first"); |
| 769 | Handle<String> second = FACTORY->InternalizeUtf8String("second"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 770 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 771 | obj->SetProperty( |
| 772 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 773 | obj->SetProperty( |
| 774 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 775 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 776 | obj->SetElement(0, *first, NONE, kNonStrictMode)->ToObjectChecked(); |
| 777 | obj->SetElement(1, *second, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 778 | |
| 779 | // Make the clone. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 780 | Handle<JSObject> clone = Copy(obj); |
| 781 | CHECK(!clone.is_identical_to(obj)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 782 | |
| 783 | CHECK_EQ(obj->GetElement(0), clone->GetElement(0)); |
| 784 | CHECK_EQ(obj->GetElement(1), clone->GetElement(1)); |
| 785 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 786 | CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*first)); |
| 787 | CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 788 | |
| 789 | // Flip the values. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 790 | clone->SetProperty( |
| 791 | *first, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
| 792 | clone->SetProperty( |
| 793 | *second, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 794 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 795 | clone->SetElement(0, *second, NONE, kNonStrictMode)->ToObjectChecked(); |
| 796 | clone->SetElement(1, *first, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 797 | |
| 798 | CHECK_EQ(obj->GetElement(1), clone->GetElement(0)); |
| 799 | CHECK_EQ(obj->GetElement(0), clone->GetElement(1)); |
| 800 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 801 | CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*first)); |
| 802 | CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
| 806 | TEST(StringAllocation) { |
| 807 | InitializeVM(); |
| 808 | |
| 809 | |
| 810 | const unsigned char chars[] = { 0xe5, 0xa4, 0xa7 }; |
| 811 | for (int length = 0; length < 100; length++) { |
| 812 | v8::HandleScope scope; |
| 813 | char* non_ascii = NewArray<char>(3 * length + 1); |
| 814 | char* ascii = NewArray<char>(length + 1); |
| 815 | non_ascii[3 * length] = 0; |
| 816 | ascii[length] = 0; |
| 817 | for (int i = 0; i < length; i++) { |
| 818 | ascii[i] = 'a'; |
| 819 | non_ascii[3 * i] = chars[0]; |
| 820 | non_ascii[3 * i + 1] = chars[1]; |
| 821 | non_ascii[3 * i + 2] = chars[2]; |
| 822 | } |
| 823 | Handle<String> non_ascii_sym = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 824 | FACTORY->InternalizeUtf8String( |
| 825 | Vector<const char>(non_ascii, 3 * length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 826 | CHECK_EQ(length, non_ascii_sym->length()); |
| 827 | Handle<String> ascii_sym = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 828 | FACTORY->InternalizeOneByteString(OneByteVector(ascii, length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 829 | CHECK_EQ(length, ascii_sym->length()); |
| 830 | Handle<String> non_ascii_str = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 831 | FACTORY->NewStringFromUtf8(Vector<const char>(non_ascii, 3 * length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 832 | non_ascii_str->Hash(); |
| 833 | CHECK_EQ(length, non_ascii_str->length()); |
| 834 | Handle<String> ascii_str = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 835 | FACTORY->NewStringFromUtf8(Vector<const char>(ascii, length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 836 | ascii_str->Hash(); |
| 837 | CHECK_EQ(length, ascii_str->length()); |
| 838 | DeleteArray(non_ascii); |
| 839 | DeleteArray(ascii); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 844 | static int ObjectsFoundInHeap(Heap* heap, Handle<Object> objs[], int size) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 845 | // Count the number of objects found in the heap. |
| 846 | int found_count = 0; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 847 | HeapIterator iterator(heap); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 848 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 849 | for (int i = 0; i < size; i++) { |
| 850 | if (*objs[i] == obj) { |
| 851 | found_count++; |
| 852 | } |
| 853 | } |
| 854 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 855 | return found_count; |
| 856 | } |
| 857 | |
| 858 | |
| 859 | TEST(Iteration) { |
| 860 | InitializeVM(); |
| 861 | v8::HandleScope scope; |
| 862 | |
| 863 | // Array of objects to scan haep for. |
| 864 | const int objs_count = 6; |
| 865 | Handle<Object> objs[objs_count]; |
| 866 | int next_objs_index = 0; |
| 867 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 868 | // Allocate a JS array to OLD_POINTER_SPACE and NEW_SPACE |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 869 | objs[next_objs_index++] = FACTORY->NewJSArray(10); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 870 | objs[next_objs_index++] = FACTORY->NewJSArray(10, |
| 871 | FAST_HOLEY_ELEMENTS, |
| 872 | TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 873 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 874 | // Allocate a small string to OLD_DATA_SPACE and NEW_SPACE |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 875 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 876 | FACTORY->NewStringFromAscii(CStrVector("abcdefghij")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 877 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 878 | FACTORY->NewStringFromAscii(CStrVector("abcdefghij"), TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 879 | |
| 880 | // Allocate a large string (for large object space). |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 881 | int large_size = Page::kMaxNonCodeHeapObjectSize + 1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 882 | char* str = new char[large_size]; |
| 883 | for (int i = 0; i < large_size - 1; ++i) str[i] = 'a'; |
| 884 | str[large_size - 1] = '\0'; |
| 885 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 886 | FACTORY->NewStringFromAscii(CStrVector(str), TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 887 | delete[] str; |
| 888 | |
| 889 | // Add a Map object to look for. |
| 890 | objs[next_objs_index++] = Handle<Map>(HeapObject::cast(*objs[0])->map()); |
| 891 | |
| 892 | CHECK_EQ(objs_count, next_objs_index); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 893 | CHECK_EQ(objs_count, ObjectsFoundInHeap(HEAP, objs, objs_count)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 895 | |
| 896 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 897 | TEST(EmptyHandleEscapeFrom) { |
| 898 | InitializeVM(); |
| 899 | |
| 900 | v8::HandleScope scope; |
| 901 | Handle<JSObject> runaway; |
| 902 | |
| 903 | { |
| 904 | v8::HandleScope nested; |
| 905 | Handle<JSObject> empty; |
| 906 | runaway = empty.EscapeFrom(&nested); |
| 907 | } |
| 908 | |
| 909 | CHECK(runaway.is_null()); |
| 910 | } |
| 911 | |
| 912 | |
| 913 | static int LenFromSize(int size) { |
| 914 | return (size - FixedArray::kHeaderSize) / kPointerSize; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | TEST(Regression39128) { |
| 919 | // Test case for crbug.com/39128. |
| 920 | InitializeVM(); |
| 921 | |
| 922 | // Increase the chance of 'bump-the-pointer' allocation in old space. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 923 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 924 | |
| 925 | v8::HandleScope scope; |
| 926 | |
| 927 | // The plan: create JSObject which references objects in new space. |
| 928 | // Then clone this object (forcing it to go into old space) and check |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 929 | // that region dirty marks are updated correctly. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 930 | |
| 931 | // Step 1: prepare a map for the object. We add 1 inobject property to it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 932 | Handle<JSFunction> object_ctor( |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 933 | Isolate::Current()->native_context()->object_function()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 934 | CHECK(object_ctor->has_initial_map()); |
| 935 | Handle<Map> object_map(object_ctor->initial_map()); |
| 936 | // Create a map with single inobject property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 937 | Handle<Map> my_map = FACTORY->CopyMap(object_map, 1); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 938 | int n_properties = my_map->inobject_properties(); |
| 939 | CHECK_GT(n_properties, 0); |
| 940 | |
| 941 | int object_size = my_map->instance_size(); |
| 942 | |
| 943 | // Step 2: allocate a lot of objects so to almost fill new space: we need |
| 944 | // just enough room to allocate JSObject and thus fill the newspace. |
| 945 | |
| 946 | int allocation_amount = Min(FixedArray::kMaxSize, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 947 | HEAP->MaxObjectSizeInNewSpace()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 948 | int allocation_len = LenFromSize(allocation_amount); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 949 | NewSpace* new_space = HEAP->new_space(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 950 | Address* top_addr = new_space->allocation_top_address(); |
| 951 | Address* limit_addr = new_space->allocation_limit_address(); |
| 952 | while ((*limit_addr - *top_addr) > allocation_amount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 953 | CHECK(!HEAP->always_allocate()); |
| 954 | Object* array = HEAP->AllocateFixedArray(allocation_len)->ToObjectChecked(); |
| 955 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 956 | CHECK(new_space->Contains(array)); |
| 957 | } |
| 958 | |
| 959 | // Step 3: now allocate fixed array and JSObject to fill the whole new space. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 960 | int to_fill = static_cast<int>(*limit_addr - *top_addr - object_size); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 961 | int fixed_array_len = LenFromSize(to_fill); |
| 962 | CHECK(fixed_array_len < FixedArray::kMaxLength); |
| 963 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 964 | CHECK(!HEAP->always_allocate()); |
| 965 | Object* array = HEAP->AllocateFixedArray(fixed_array_len)->ToObjectChecked(); |
| 966 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 967 | CHECK(new_space->Contains(array)); |
| 968 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 969 | Object* object = HEAP->AllocateJSObjectFromMap(*my_map)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 970 | CHECK(new_space->Contains(object)); |
| 971 | JSObject* jsobject = JSObject::cast(object); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 972 | CHECK_EQ(0, FixedArray::cast(jsobject->elements())->length()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 973 | CHECK_EQ(0, jsobject->properties()->length()); |
| 974 | // Create a reference to object in new space in jsobject. |
| 975 | jsobject->FastPropertyAtPut(-1, array); |
| 976 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 977 | CHECK_EQ(0, static_cast<int>(*limit_addr - *top_addr)); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 978 | |
| 979 | // Step 4: clone jsobject, but force always allocate first to create a clone |
| 980 | // in old pointer space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 981 | Address old_pointer_space_top = HEAP->old_pointer_space()->top(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 982 | AlwaysAllocateScope aa_scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 983 | Object* clone_obj = HEAP->CopyJSObject(jsobject)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 984 | JSObject* clone = JSObject::cast(clone_obj); |
| 985 | if (clone->address() != old_pointer_space_top) { |
| 986 | // Alas, got allocated from free list, we cannot do checks. |
| 987 | return; |
| 988 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 989 | CHECK(HEAP->old_pointer_space()->Contains(clone->address())); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 990 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 991 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 992 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 993 | TEST(TestCodeFlushing) { |
ricow@chromium.org | fd0930e | 2010-06-11 10:37:34 +0000 | [diff] [blame] | 994 | // If we do not flush code this test is invalid. |
| 995 | if (!FLAG_flush_code) return; |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 996 | i::FLAG_allow_natives_syntax = true; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 997 | InitializeVM(); |
| 998 | v8::HandleScope scope; |
| 999 | const char* source = "function foo() {" |
| 1000 | " var x = 42;" |
| 1001 | " var y = 42;" |
| 1002 | " var z = x + y;" |
| 1003 | "};" |
| 1004 | "foo()"; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1005 | Handle<String> foo_name = FACTORY->InternalizeUtf8String("foo"); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1006 | |
| 1007 | // This compile will add the code to the compilation cache. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1008 | { v8::HandleScope scope; |
| 1009 | CompileRun(source); |
| 1010 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1011 | |
| 1012 | // Check function is compiled. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1013 | Object* func_value = Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1014 | GetProperty(*foo_name)->ToObjectChecked(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1015 | CHECK(func_value->IsJSFunction()); |
| 1016 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 1017 | CHECK(function->shared()->is_compiled()); |
| 1018 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1019 | // The code will survive at least two GCs. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 1020 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 1021 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1022 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1023 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1024 | // Simulate several GCs that use full marking. |
| 1025 | const int kAgingThreshold = 6; |
| 1026 | for (int i = 0; i < kAgingThreshold; i++) { |
| 1027 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 1028 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1029 | |
| 1030 | // foo should no longer be in the compilation cache |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1031 | CHECK(!function->shared()->is_compiled() || function->IsOptimized()); |
| 1032 | CHECK(!function->is_compiled() || function->IsOptimized()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1033 | // Call foo to get it recompiled. |
| 1034 | CompileRun("foo()"); |
| 1035 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1036 | CHECK(function->is_compiled()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1037 | } |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1038 | |
| 1039 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1040 | TEST(TestCodeFlushingIncremental) { |
| 1041 | // If we do not flush code this test is invalid. |
| 1042 | if (!FLAG_flush_code || !FLAG_flush_code_incrementally) return; |
| 1043 | i::FLAG_allow_natives_syntax = true; |
| 1044 | InitializeVM(); |
| 1045 | v8::HandleScope scope; |
| 1046 | const char* source = "function foo() {" |
| 1047 | " var x = 42;" |
| 1048 | " var y = 42;" |
| 1049 | " var z = x + y;" |
| 1050 | "};" |
| 1051 | "foo()"; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1052 | Handle<String> foo_name = FACTORY->InternalizeUtf8String("foo"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1053 | |
| 1054 | // This compile will add the code to the compilation cache. |
| 1055 | { v8::HandleScope scope; |
| 1056 | CompileRun(source); |
| 1057 | } |
| 1058 | |
| 1059 | // Check function is compiled. |
| 1060 | Object* func_value = Isolate::Current()->context()->global_object()-> |
| 1061 | GetProperty(*foo_name)->ToObjectChecked(); |
| 1062 | CHECK(func_value->IsJSFunction()); |
| 1063 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 1064 | CHECK(function->shared()->is_compiled()); |
| 1065 | |
| 1066 | // The code will survive at least two GCs. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 1067 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 1068 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1069 | CHECK(function->shared()->is_compiled()); |
| 1070 | |
| 1071 | // Simulate several GCs that use incremental marking. |
| 1072 | const int kAgingThreshold = 6; |
| 1073 | for (int i = 0; i < kAgingThreshold; i++) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1074 | SimulateIncrementalMarking(); |
| 1075 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1076 | } |
| 1077 | CHECK(!function->shared()->is_compiled() || function->IsOptimized()); |
| 1078 | CHECK(!function->is_compiled() || function->IsOptimized()); |
| 1079 | |
| 1080 | // This compile will compile the function again. |
| 1081 | { v8::HandleScope scope; |
| 1082 | CompileRun("foo();"); |
| 1083 | } |
| 1084 | |
| 1085 | // Simulate several GCs that use incremental marking but make sure |
| 1086 | // the loop breaks once the function is enqueued as a candidate. |
| 1087 | for (int i = 0; i < kAgingThreshold; i++) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1088 | SimulateIncrementalMarking(); |
| 1089 | if (!function->next_function_link()->IsUndefined()) break; |
| 1090 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1091 | } |
| 1092 | |
| 1093 | // Force optimization while incremental marking is active and while |
| 1094 | // the function is enqueued as a candidate. |
| 1095 | { v8::HandleScope scope; |
| 1096 | CompileRun("%OptimizeFunctionOnNextCall(foo); foo();"); |
| 1097 | } |
| 1098 | |
| 1099 | // Simulate one final GC to make sure the candidate queue is sane. |
| 1100 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1101 | CHECK(function->shared()->is_compiled() || !function->IsOptimized()); |
| 1102 | CHECK(function->is_compiled() || !function->IsOptimized()); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | TEST(TestCodeFlushingIncrementalScavenge) { |
| 1107 | // If we do not flush code this test is invalid. |
| 1108 | if (!FLAG_flush_code || !FLAG_flush_code_incrementally) return; |
| 1109 | i::FLAG_allow_natives_syntax = true; |
| 1110 | InitializeVM(); |
| 1111 | v8::HandleScope scope; |
| 1112 | const char* source = "var foo = function() {" |
| 1113 | " var x = 42;" |
| 1114 | " var y = 42;" |
| 1115 | " var z = x + y;" |
| 1116 | "};" |
| 1117 | "foo();" |
| 1118 | "var bar = function() {" |
| 1119 | " var x = 23;" |
| 1120 | "};" |
| 1121 | "bar();"; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1122 | Handle<String> foo_name = FACTORY->InternalizeUtf8String("foo"); |
| 1123 | Handle<String> bar_name = FACTORY->InternalizeUtf8String("bar"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Perfrom one initial GC to enable code flushing. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 1126 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1127 | |
| 1128 | // This compile will add the code to the compilation cache. |
| 1129 | { v8::HandleScope scope; |
| 1130 | CompileRun(source); |
| 1131 | } |
| 1132 | |
| 1133 | // Check functions are compiled. |
| 1134 | Object* func_value = Isolate::Current()->context()->global_object()-> |
| 1135 | GetProperty(*foo_name)->ToObjectChecked(); |
| 1136 | CHECK(func_value->IsJSFunction()); |
| 1137 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 1138 | CHECK(function->shared()->is_compiled()); |
| 1139 | Object* func_value2 = Isolate::Current()->context()->global_object()-> |
| 1140 | GetProperty(*bar_name)->ToObjectChecked(); |
| 1141 | CHECK(func_value2->IsJSFunction()); |
| 1142 | Handle<JSFunction> function2(JSFunction::cast(func_value2)); |
| 1143 | CHECK(function2->shared()->is_compiled()); |
| 1144 | |
| 1145 | // Clear references to functions so that one of them can die. |
| 1146 | { v8::HandleScope scope; |
| 1147 | CompileRun("foo = 0; bar = 0;"); |
| 1148 | } |
| 1149 | |
| 1150 | // Bump the code age so that flushing is triggered while the function |
| 1151 | // object is still located in new-space. |
| 1152 | const int kAgingThreshold = 6; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 1153 | for (int i = 0; i < kAgingThreshold; i++) { |
| 1154 | function->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 1155 | function2->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 1156 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Simulate incremental marking so that the functions are enqueued as |
| 1159 | // code flushing candidates. Then kill one of the functions. Finally |
| 1160 | // perform a scavenge while incremental marking is still running. |
| 1161 | SimulateIncrementalMarking(); |
| 1162 | *function2.location() = NULL; |
| 1163 | HEAP->CollectGarbage(NEW_SPACE, "test scavenge while marking"); |
| 1164 | |
| 1165 | // Simulate one final GC to make sure the candidate queue is sane. |
| 1166 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1167 | CHECK(!function->shared()->is_compiled() || function->IsOptimized()); |
| 1168 | CHECK(!function->is_compiled() || function->IsOptimized()); |
| 1169 | } |
| 1170 | |
| 1171 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1172 | TEST(TestCodeFlushingIncrementalAbort) { |
| 1173 | // If we do not flush code this test is invalid. |
| 1174 | if (!FLAG_flush_code || !FLAG_flush_code_incrementally) return; |
| 1175 | i::FLAG_allow_natives_syntax = true; |
| 1176 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1177 | Isolate* isolate = Isolate::Current(); |
| 1178 | Heap* heap = isolate->heap(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1179 | v8::HandleScope scope; |
| 1180 | const char* source = "function foo() {" |
| 1181 | " var x = 42;" |
| 1182 | " var y = 42;" |
| 1183 | " var z = x + y;" |
| 1184 | "};" |
| 1185 | "foo()"; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1186 | Handle<String> foo_name = FACTORY->InternalizeUtf8String("foo"); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1187 | |
| 1188 | // This compile will add the code to the compilation cache. |
| 1189 | { v8::HandleScope scope; |
| 1190 | CompileRun(source); |
| 1191 | } |
| 1192 | |
| 1193 | // Check function is compiled. |
| 1194 | Object* func_value = Isolate::Current()->context()->global_object()-> |
| 1195 | GetProperty(*foo_name)->ToObjectChecked(); |
| 1196 | CHECK(func_value->IsJSFunction()); |
| 1197 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 1198 | CHECK(function->shared()->is_compiled()); |
| 1199 | |
| 1200 | // The code will survive at least two GCs. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1201 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 1202 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1203 | CHECK(function->shared()->is_compiled()); |
| 1204 | |
| 1205 | // Bump the code age so that flushing is triggered. |
| 1206 | const int kAgingThreshold = 6; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 1207 | for (int i = 0; i < kAgingThreshold; i++) { |
| 1208 | function->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 1209 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Simulate incremental marking so that the function is enqueued as |
| 1212 | // code flushing candidate. |
| 1213 | SimulateIncrementalMarking(); |
| 1214 | |
| 1215 | // Enable the debugger and add a breakpoint while incremental marking |
| 1216 | // is running so that incremental marking aborts and code flushing is |
| 1217 | // disabled. |
| 1218 | int position = 0; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1219 | Handle<Object> breakpoint_object(Smi::FromInt(0), isolate); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1220 | isolate->debug()->SetBreakPoint(function, breakpoint_object, &position); |
| 1221 | isolate->debug()->ClearAllBreakPoints(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Force optimization now that code flushing is disabled. |
| 1224 | { v8::HandleScope scope; |
| 1225 | CompileRun("%OptimizeFunctionOnNextCall(foo); foo();"); |
| 1226 | } |
| 1227 | |
| 1228 | // Simulate one final GC to make sure the candidate queue is sane. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1229 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1230 | CHECK(function->shared()->is_compiled() || !function->IsOptimized()); |
| 1231 | CHECK(function->is_compiled() || !function->IsOptimized()); |
| 1232 | } |
| 1233 | |
| 1234 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1235 | // Count the number of native contexts in the weak list of native contexts. |
| 1236 | int CountNativeContexts() { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1237 | int count = 0; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1238 | Object* object = HEAP->native_contexts_list(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1239 | while (!object->IsUndefined()) { |
| 1240 | count++; |
| 1241 | object = Context::cast(object)->get(Context::NEXT_CONTEXT_LINK); |
| 1242 | } |
| 1243 | return count; |
| 1244 | } |
| 1245 | |
| 1246 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1247 | // Count the number of user functions in the weak list of optimized |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1248 | // functions attached to a native context. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1249 | static int CountOptimizedUserFunctions(v8::Handle<v8::Context> context) { |
| 1250 | int count = 0; |
| 1251 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
| 1252 | Object* object = icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST); |
| 1253 | while (object->IsJSFunction() && !JSFunction::cast(object)->IsBuiltin()) { |
| 1254 | count++; |
| 1255 | object = JSFunction::cast(object)->next_function_link(); |
| 1256 | } |
| 1257 | return count; |
| 1258 | } |
| 1259 | |
| 1260 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1261 | TEST(TestInternalWeakLists) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1262 | v8::V8::Initialize(); |
| 1263 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 1264 | // Some flags turn Scavenge collections into Mark-sweep collections |
| 1265 | // and hence are incompatible with this test case. |
| 1266 | if (FLAG_gc_global || FLAG_stress_compaction) return; |
| 1267 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1268 | static const int kNumTestContexts = 10; |
| 1269 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1270 | Isolate* isolate = Isolate::Current(); |
| 1271 | Heap* heap = isolate->heap(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1272 | v8::HandleScope scope; |
| 1273 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1274 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1275 | CHECK_EQ(0, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Create a number of global contests which gets linked together. |
| 1278 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1279 | ctx[i] = v8::Context::New(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1280 | |
| 1281 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1282 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1283 | CHECK_EQ(i + 1, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1284 | |
| 1285 | ctx[i]->Enter(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Create a handle scope so no function objects get stuch in the outer |
| 1288 | // handle scope |
| 1289 | v8::HandleScope scope; |
| 1290 | const char* source = "function f1() { };" |
| 1291 | "function f2() { };" |
| 1292 | "function f3() { };" |
| 1293 | "function f4() { };" |
| 1294 | "function f5() { };"; |
| 1295 | CompileRun(source); |
| 1296 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[i])); |
| 1297 | CompileRun("f1()"); |
| 1298 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1299 | CompileRun("f2()"); |
| 1300 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1301 | CompileRun("f3()"); |
| 1302 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1303 | CompileRun("f4()"); |
| 1304 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1305 | CompileRun("f5()"); |
| 1306 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1307 | |
| 1308 | // Remove function f1, and |
| 1309 | CompileRun("f1=null"); |
| 1310 | |
| 1311 | // Scavenge treats these references as strong. |
| 1312 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1313 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1314 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1315 | } |
| 1316 | |
| 1317 | // Mark compact handles the weak references. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1318 | isolate->compilation_cache()->Clear(); |
| 1319 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1320 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1321 | |
| 1322 | // Get rid of f3 and f5 in the same way. |
| 1323 | CompileRun("f3=null"); |
| 1324 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1325 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1326 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1327 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1328 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1329 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1330 | CompileRun("f5=null"); |
| 1331 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1332 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1333 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1334 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1335 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1336 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1337 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1338 | ctx[i]->Exit(); |
| 1339 | } |
| 1340 | |
| 1341 | // Force compilation cache cleanup. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1342 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1343 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1344 | // Dispose the native contexts one by one. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1345 | for (int i = 0; i < kNumTestContexts; i++) { |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1346 | ctx[i].Dispose(ctx[i]->GetIsolate()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1347 | ctx[i].Clear(); |
| 1348 | |
| 1349 | // Scavenge treats these references as strong. |
| 1350 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1351 | HEAP->PerformScavenge(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1352 | CHECK_EQ(kNumTestContexts - i, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // Mark compact handles the weak references. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1356 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1357 | CHECK_EQ(kNumTestContexts - i - 1, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1360 | CHECK_EQ(0, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1364 | // Count the number of native contexts in the weak list of native contexts |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1365 | // causing a GC after the specified number of elements. |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1366 | static int CountNativeContextsWithGC(Isolate* isolate, int n) { |
| 1367 | Heap* heap = isolate->heap(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1368 | int count = 0; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1369 | Handle<Object> object(heap->native_contexts_list(), isolate); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1370 | while (!object->IsUndefined()) { |
| 1371 | count++; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1372 | if (count == n) heap->CollectAllGarbage(Heap::kNoGCFlags); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1373 | object = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1374 | Handle<Object>(Context::cast(*object)->get(Context::NEXT_CONTEXT_LINK), |
| 1375 | isolate); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1376 | } |
| 1377 | return count; |
| 1378 | } |
| 1379 | |
| 1380 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1381 | // Count the number of user functions in the weak list of optimized |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1382 | // functions attached to a native context causing a GC after the |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1383 | // specified number of elements. |
| 1384 | static int CountOptimizedUserFunctionsWithGC(v8::Handle<v8::Context> context, |
| 1385 | int n) { |
| 1386 | int count = 0; |
| 1387 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1388 | Isolate* isolate = icontext->GetIsolate(); |
| 1389 | Handle<Object> object(icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST), |
| 1390 | isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1391 | while (object->IsJSFunction() && |
| 1392 | !Handle<JSFunction>::cast(object)->IsBuiltin()) { |
| 1393 | count++; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1394 | if (count == n) isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1395 | object = Handle<Object>( |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1396 | Object::cast(JSFunction::cast(*object)->next_function_link()), |
| 1397 | isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1398 | } |
| 1399 | return count; |
| 1400 | } |
| 1401 | |
| 1402 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1403 | TEST(TestInternalWeakListsTraverseWithGC) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1404 | v8::V8::Initialize(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1405 | Isolate* isolate = Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1406 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1407 | static const int kNumTestContexts = 10; |
| 1408 | |
| 1409 | v8::HandleScope scope; |
| 1410 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1411 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1412 | CHECK_EQ(0, CountNativeContexts()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Create an number of contexts and check the length of the weak list both |
| 1415 | // with and without GCs while iterating the list. |
| 1416 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1417 | ctx[i] = v8::Context::New(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1418 | CHECK_EQ(i + 1, CountNativeContexts()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1419 | CHECK_EQ(i + 1, CountNativeContextsWithGC(isolate, i / 2 + 1)); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1420 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1421 | |
| 1422 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1423 | |
| 1424 | // Compile a number of functions the length of the weak list of optimized |
| 1425 | // functions both with and without GCs while iterating the list. |
| 1426 | ctx[0]->Enter(); |
| 1427 | const char* source = "function f1() { };" |
| 1428 | "function f2() { };" |
| 1429 | "function f3() { };" |
| 1430 | "function f4() { };" |
| 1431 | "function f5() { };"; |
| 1432 | CompileRun(source); |
| 1433 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[0])); |
| 1434 | CompileRun("f1()"); |
| 1435 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1436 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1437 | CompileRun("f2()"); |
| 1438 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1439 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1440 | CompileRun("f3()"); |
| 1441 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1442 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1443 | CompileRun("f4()"); |
| 1444 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1445 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 2)); |
| 1446 | CompileRun("f5()"); |
| 1447 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1448 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 4)); |
| 1449 | |
| 1450 | ctx[0]->Exit(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1451 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1452 | |
| 1453 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1454 | TEST(TestSizeOfObjects) { |
| 1455 | v8::V8::Initialize(); |
| 1456 | |
| 1457 | // Get initial heap size after several full GCs, which will stabilize |
| 1458 | // the heap size and return with sweeping finished completely. |
| 1459 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1460 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1461 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1462 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1463 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 1464 | CHECK(HEAP->old_pointer_space()->IsLazySweepingComplete()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1465 | int initial_size = static_cast<int>(HEAP->SizeOfObjects()); |
| 1466 | |
| 1467 | { |
| 1468 | // Allocate objects on several different old-space pages so that |
| 1469 | // lazy sweeping kicks in for subsequent GC runs. |
| 1470 | AlwaysAllocateScope always_allocate; |
| 1471 | int filler_size = static_cast<int>(FixedArray::SizeFor(8192)); |
| 1472 | for (int i = 1; i <= 100; i++) { |
| 1473 | HEAP->AllocateFixedArray(8192, TENURED)->ToObjectChecked(); |
| 1474 | CHECK_EQ(initial_size + i * filler_size, |
| 1475 | static_cast<int>(HEAP->SizeOfObjects())); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | // The heap size should go back to initial size after a full GC, even |
| 1480 | // though sweeping didn't finish yet. |
| 1481 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Normally sweeping would not be complete here, but no guarantees. |
| 1484 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1485 | CHECK_EQ(initial_size, static_cast<int>(HEAP->SizeOfObjects())); |
| 1486 | |
| 1487 | // Advancing the sweeper step-wise should not change the heap size. |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 1488 | while (!HEAP->old_pointer_space()->IsLazySweepingComplete()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1489 | HEAP->old_pointer_space()->AdvanceSweeper(KB); |
| 1490 | CHECK_EQ(initial_size, static_cast<int>(HEAP->SizeOfObjects())); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1495 | TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { |
| 1496 | InitializeVM(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1497 | HEAP->EnsureHeapIsIterable(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1498 | intptr_t size_of_objects_1 = HEAP->SizeOfObjects(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1499 | HeapIterator iterator(HEAP); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1500 | intptr_t size_of_objects_2 = 0; |
| 1501 | for (HeapObject* obj = iterator.next(); |
| 1502 | obj != NULL; |
| 1503 | obj = iterator.next()) { |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 1504 | if (!obj->IsFreeSpace()) { |
| 1505 | size_of_objects_2 += obj->Size(); |
| 1506 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1507 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1508 | // Delta must be within 5% of the larger result. |
| 1509 | // TODO(gc): Tighten this up by distinguishing between byte |
| 1510 | // arrays that are real and those that merely mark free space |
| 1511 | // on the heap. |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1512 | if (size_of_objects_1 > size_of_objects_2) { |
| 1513 | intptr_t delta = size_of_objects_1 - size_of_objects_2; |
| 1514 | PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, " |
| 1515 | "Iterator: %" V8_PTR_PREFIX "d, " |
| 1516 | "delta: %" V8_PTR_PREFIX "d\n", |
| 1517 | size_of_objects_1, size_of_objects_2, delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1518 | CHECK_GT(size_of_objects_1 / 20, delta); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1519 | } else { |
| 1520 | intptr_t delta = size_of_objects_2 - size_of_objects_1; |
| 1521 | PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, " |
| 1522 | "Iterator: %" V8_PTR_PREFIX "d, " |
| 1523 | "delta: %" V8_PTR_PREFIX "d\n", |
| 1524 | size_of_objects_1, size_of_objects_2, delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1525 | CHECK_GT(size_of_objects_2 / 20, delta); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1526 | } |
| 1527 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1528 | |
| 1529 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1530 | static void FillUpNewSpace(NewSpace* new_space) { |
| 1531 | // Fill up new space to the point that it is completely full. Make sure |
| 1532 | // that the scavenger does not undo the filling. |
| 1533 | v8::HandleScope scope; |
| 1534 | AlwaysAllocateScope always_allocate; |
| 1535 | intptr_t available = new_space->EffectiveCapacity() - new_space->Size(); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1536 | intptr_t number_of_fillers = (available / FixedArray::SizeFor(32)) - 1; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1537 | for (intptr_t i = 0; i < number_of_fillers; i++) { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1538 | CHECK(HEAP->InNewSpace(*FACTORY->NewFixedArray(32, NOT_TENURED))); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1543 | TEST(GrowAndShrinkNewSpace) { |
| 1544 | InitializeVM(); |
| 1545 | NewSpace* new_space = HEAP->new_space(); |
| 1546 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 1547 | if (HEAP->ReservedSemiSpaceSize() == HEAP->InitialSemiSpaceSize() || |
| 1548 | HEAP->MaxSemiSpaceSize() == HEAP->InitialSemiSpaceSize()) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1549 | // The max size cannot exceed the reserved size, since semispaces must be |
| 1550 | // always within the reserved space. We can't test new space growing and |
| 1551 | // shrinking if the reserved size is the same as the minimum (initial) size. |
| 1552 | return; |
| 1553 | } |
| 1554 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1555 | // Explicitly growing should double the space capacity. |
| 1556 | intptr_t old_capacity, new_capacity; |
| 1557 | old_capacity = new_space->Capacity(); |
| 1558 | new_space->Grow(); |
| 1559 | new_capacity = new_space->Capacity(); |
| 1560 | CHECK(2 * old_capacity == new_capacity); |
| 1561 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1562 | old_capacity = new_space->Capacity(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1563 | FillUpNewSpace(new_space); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1564 | new_capacity = new_space->Capacity(); |
| 1565 | CHECK(old_capacity == new_capacity); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1566 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1567 | // Explicitly shrinking should not affect space capacity. |
| 1568 | old_capacity = new_space->Capacity(); |
| 1569 | new_space->Shrink(); |
| 1570 | new_capacity = new_space->Capacity(); |
| 1571 | CHECK(old_capacity == new_capacity); |
| 1572 | |
| 1573 | // Let the scavenger empty the new space. |
| 1574 | HEAP->CollectGarbage(NEW_SPACE); |
| 1575 | CHECK_LE(new_space->Size(), old_capacity); |
| 1576 | |
| 1577 | // Explicitly shrinking should halve the space capacity. |
| 1578 | old_capacity = new_space->Capacity(); |
| 1579 | new_space->Shrink(); |
| 1580 | new_capacity = new_space->Capacity(); |
| 1581 | CHECK(old_capacity == 2 * new_capacity); |
| 1582 | |
| 1583 | // Consecutive shrinking should not affect space capacity. |
| 1584 | old_capacity = new_space->Capacity(); |
| 1585 | new_space->Shrink(); |
| 1586 | new_space->Shrink(); |
| 1587 | new_space->Shrink(); |
| 1588 | new_capacity = new_space->Capacity(); |
| 1589 | CHECK(old_capacity == new_capacity); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1590 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1591 | |
| 1592 | |
| 1593 | TEST(CollectingAllAvailableGarbageShrinksNewSpace) { |
| 1594 | InitializeVM(); |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1595 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 1596 | if (HEAP->ReservedSemiSpaceSize() == HEAP->InitialSemiSpaceSize() || |
| 1597 | HEAP->MaxSemiSpaceSize() == HEAP->InitialSemiSpaceSize()) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1598 | // The max size cannot exceed the reserved size, since semispaces must be |
| 1599 | // always within the reserved space. We can't test new space growing and |
| 1600 | // shrinking if the reserved size is the same as the minimum (initial) size. |
| 1601 | return; |
| 1602 | } |
| 1603 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1604 | v8::HandleScope scope; |
| 1605 | NewSpace* new_space = HEAP->new_space(); |
| 1606 | intptr_t old_capacity, new_capacity; |
| 1607 | old_capacity = new_space->Capacity(); |
| 1608 | new_space->Grow(); |
| 1609 | new_capacity = new_space->Capacity(); |
| 1610 | CHECK(2 * old_capacity == new_capacity); |
| 1611 | FillUpNewSpace(new_space); |
| 1612 | HEAP->CollectAllAvailableGarbage(); |
| 1613 | new_capacity = new_space->Capacity(); |
| 1614 | CHECK(old_capacity == new_capacity); |
| 1615 | } |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 1616 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1617 | |
| 1618 | static int NumberOfGlobalObjects() { |
| 1619 | int count = 0; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 1620 | HeapIterator iterator(HEAP); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1621 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 1622 | if (obj->IsGlobalObject()) count++; |
| 1623 | } |
| 1624 | return count; |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | // Test that we don't embed maps from foreign contexts into |
| 1629 | // optimized code. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1630 | TEST(LeakNativeContextViaMap) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1631 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1632 | v8::HandleScope outer_scope; |
| 1633 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1634 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1635 | ctx1->Enter(); |
| 1636 | |
| 1637 | HEAP->CollectAllAvailableGarbage(); |
| 1638 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1639 | |
| 1640 | { |
| 1641 | v8::HandleScope inner_scope; |
| 1642 | CompileRun("var v = {x: 42}"); |
| 1643 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1644 | ctx2->Enter(); |
| 1645 | ctx2->Global()->Set(v8_str("o"), v); |
| 1646 | v8::Local<v8::Value> res = CompileRun( |
| 1647 | "function f() { return o.x; }" |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1648 | "for (var i = 0; i < 10; ++i) f();" |
| 1649 | "%OptimizeFunctionOnNextCall(f);" |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1650 | "f();"); |
| 1651 | CHECK_EQ(42, res->Int32Value()); |
| 1652 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1653 | ctx2->Exit(); |
| 1654 | ctx1->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1655 | ctx1.Dispose(ctx1->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1656 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1657 | } |
| 1658 | HEAP->CollectAllAvailableGarbage(); |
| 1659 | CHECK_EQ(2, NumberOfGlobalObjects()); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1660 | ctx2.Dispose(ctx2->GetIsolate()); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1661 | HEAP->CollectAllAvailableGarbage(); |
| 1662 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | // Test that we don't embed functions from foreign contexts into |
| 1667 | // optimized code. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1668 | TEST(LeakNativeContextViaFunction) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1669 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1670 | v8::HandleScope outer_scope; |
| 1671 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1672 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1673 | ctx1->Enter(); |
| 1674 | |
| 1675 | HEAP->CollectAllAvailableGarbage(); |
| 1676 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1677 | |
| 1678 | { |
| 1679 | v8::HandleScope inner_scope; |
| 1680 | CompileRun("var v = function() { return 42; }"); |
| 1681 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1682 | ctx2->Enter(); |
| 1683 | ctx2->Global()->Set(v8_str("o"), v); |
| 1684 | v8::Local<v8::Value> res = CompileRun( |
| 1685 | "function f(x) { return x(); }" |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1686 | "for (var i = 0; i < 10; ++i) f(o);" |
| 1687 | "%OptimizeFunctionOnNextCall(f);" |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1688 | "f(o);"); |
| 1689 | CHECK_EQ(42, res->Int32Value()); |
| 1690 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1691 | ctx2->Exit(); |
| 1692 | ctx1->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1693 | ctx1.Dispose(ctx1->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1694 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1695 | } |
| 1696 | HEAP->CollectAllAvailableGarbage(); |
| 1697 | CHECK_EQ(2, NumberOfGlobalObjects()); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1698 | ctx2.Dispose(ctx2->GetIsolate()); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1699 | HEAP->CollectAllAvailableGarbage(); |
| 1700 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1701 | } |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1702 | |
| 1703 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1704 | TEST(LeakNativeContextViaMapKeyed) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1705 | i::FLAG_allow_natives_syntax = true; |
| 1706 | v8::HandleScope outer_scope; |
| 1707 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1708 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1709 | ctx1->Enter(); |
| 1710 | |
| 1711 | HEAP->CollectAllAvailableGarbage(); |
| 1712 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1713 | |
| 1714 | { |
| 1715 | v8::HandleScope inner_scope; |
| 1716 | CompileRun("var v = [42, 43]"); |
| 1717 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1718 | ctx2->Enter(); |
| 1719 | ctx2->Global()->Set(v8_str("o"), v); |
| 1720 | v8::Local<v8::Value> res = CompileRun( |
| 1721 | "function f() { return o[0]; }" |
| 1722 | "for (var i = 0; i < 10; ++i) f();" |
| 1723 | "%OptimizeFunctionOnNextCall(f);" |
| 1724 | "f();"); |
| 1725 | CHECK_EQ(42, res->Int32Value()); |
| 1726 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1727 | ctx2->Exit(); |
| 1728 | ctx1->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1729 | ctx1.Dispose(ctx1->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1730 | v8::V8::ContextDisposedNotification(); |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1731 | } |
| 1732 | HEAP->CollectAllAvailableGarbage(); |
| 1733 | CHECK_EQ(2, NumberOfGlobalObjects()); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1734 | ctx2.Dispose(ctx2->GetIsolate()); |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1735 | HEAP->CollectAllAvailableGarbage(); |
| 1736 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1737 | } |
| 1738 | |
| 1739 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1740 | TEST(LeakNativeContextViaMapProto) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1741 | i::FLAG_allow_natives_syntax = true; |
| 1742 | v8::HandleScope outer_scope; |
| 1743 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1744 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1745 | ctx1->Enter(); |
| 1746 | |
| 1747 | HEAP->CollectAllAvailableGarbage(); |
| 1748 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1749 | |
| 1750 | { |
| 1751 | v8::HandleScope inner_scope; |
| 1752 | CompileRun("var v = { y: 42}"); |
| 1753 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1754 | ctx2->Enter(); |
| 1755 | ctx2->Global()->Set(v8_str("o"), v); |
| 1756 | v8::Local<v8::Value> res = CompileRun( |
| 1757 | "function f() {" |
| 1758 | " var p = {x: 42};" |
| 1759 | " p.__proto__ = o;" |
| 1760 | " return p.x;" |
| 1761 | "}" |
| 1762 | "for (var i = 0; i < 10; ++i) f();" |
| 1763 | "%OptimizeFunctionOnNextCall(f);" |
| 1764 | "f();"); |
| 1765 | CHECK_EQ(42, res->Int32Value()); |
| 1766 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1767 | ctx2->Exit(); |
| 1768 | ctx1->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1769 | ctx1.Dispose(ctx1->GetIsolate()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1770 | v8::V8::ContextDisposedNotification(); |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1771 | } |
| 1772 | HEAP->CollectAllAvailableGarbage(); |
| 1773 | CHECK_EQ(2, NumberOfGlobalObjects()); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 1774 | ctx2.Dispose(ctx2->GetIsolate()); |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1775 | HEAP->CollectAllAvailableGarbage(); |
| 1776 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1777 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1778 | |
| 1779 | |
| 1780 | TEST(InstanceOfStubWriteBarrier) { |
| 1781 | i::FLAG_allow_natives_syntax = true; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1782 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1783 | i::FLAG_verify_heap = true; |
| 1784 | #endif |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1785 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1786 | InitializeVM(); |
| 1787 | if (!i::V8::UseCrankshaft()) return; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1788 | if (i::FLAG_force_marking_deque_overflows) return; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1789 | v8::HandleScope outer_scope; |
| 1790 | |
| 1791 | { |
| 1792 | v8::HandleScope scope; |
| 1793 | CompileRun( |
| 1794 | "function foo () { }" |
| 1795 | "function mkbar () { return new (new Function(\"\")) (); }" |
| 1796 | "function f (x) { return (x instanceof foo); }" |
| 1797 | "function g () { f(mkbar()); }" |
| 1798 | "f(new foo()); f(new foo());" |
| 1799 | "%OptimizeFunctionOnNextCall(f);" |
| 1800 | "f(new foo()); g();"); |
| 1801 | } |
| 1802 | |
| 1803 | IncrementalMarking* marking = HEAP->incremental_marking(); |
| 1804 | marking->Abort(); |
| 1805 | marking->Start(); |
| 1806 | |
| 1807 | Handle<JSFunction> f = |
| 1808 | v8::Utils::OpenHandle( |
| 1809 | *v8::Handle<v8::Function>::Cast( |
| 1810 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 1811 | |
| 1812 | CHECK(f->IsOptimized()); |
| 1813 | |
| 1814 | while (!Marking::IsBlack(Marking::MarkBitFrom(f->code())) && |
| 1815 | !marking->IsStopped()) { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1816 | // Discard any pending GC requests otherwise we will get GC when we enter |
| 1817 | // code below. |
| 1818 | marking->Step(MB, IncrementalMarking::NO_GC_VIA_STACK_GUARD); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | CHECK(marking->IsMarking()); |
| 1822 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1823 | { |
| 1824 | v8::HandleScope scope; |
| 1825 | v8::Handle<v8::Object> global = v8::Context::GetCurrent()->Global(); |
| 1826 | v8::Handle<v8::Function> g = |
| 1827 | v8::Handle<v8::Function>::Cast(global->Get(v8_str("g"))); |
| 1828 | g->Call(global, 0, NULL); |
| 1829 | } |
| 1830 | |
| 1831 | HEAP->incremental_marking()->set_should_hurry(true); |
| 1832 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 1833 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1834 | |
| 1835 | |
| 1836 | TEST(PrototypeTransitionClearing) { |
| 1837 | InitializeVM(); |
| 1838 | v8::HandleScope scope; |
| 1839 | |
| 1840 | CompileRun( |
| 1841 | "var base = {};" |
| 1842 | "var live = [];" |
| 1843 | "for (var i = 0; i < 10; i++) {" |
| 1844 | " var object = {};" |
| 1845 | " var prototype = {};" |
| 1846 | " object.__proto__ = prototype;" |
| 1847 | " if (i >= 3) live.push(object, prototype);" |
| 1848 | "}"); |
| 1849 | |
| 1850 | Handle<JSObject> baseObject = |
| 1851 | v8::Utils::OpenHandle( |
| 1852 | *v8::Handle<v8::Object>::Cast( |
| 1853 | v8::Context::GetCurrent()->Global()->Get(v8_str("base")))); |
| 1854 | |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 1855 | // Verify that only dead prototype transitions are cleared. |
| 1856 | CHECK_EQ(10, baseObject->map()->NumberOfProtoTransitions()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1857 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 1858 | const int transitions = 10 - 3; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1859 | CHECK_EQ(transitions, baseObject->map()->NumberOfProtoTransitions()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1860 | |
| 1861 | // Verify that prototype transitions array was compacted. |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1862 | FixedArray* trans = baseObject->map()->GetPrototypeTransitions(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1863 | for (int i = 0; i < transitions; i++) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1864 | int j = Map::kProtoTransitionHeaderSize + |
| 1865 | i * Map::kProtoTransitionElementsPerEntry; |
| 1866 | CHECK(trans->get(j + Map::kProtoTransitionMapOffset)->IsMap()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1867 | Object* proto = trans->get(j + Map::kProtoTransitionPrototypeOffset); |
| 1868 | CHECK(proto->IsTheHole() || proto->IsJSObject()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1869 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1870 | |
| 1871 | // Make sure next prototype is placed on an old-space evacuation candidate. |
| 1872 | Handle<JSObject> prototype; |
| 1873 | PagedSpace* space = HEAP->old_pointer_space(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1874 | { |
| 1875 | AlwaysAllocateScope always_allocate; |
| 1876 | SimulateFullSpace(space); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1877 | prototype = FACTORY->NewJSArray(32 * KB, FAST_HOLEY_ELEMENTS, TENURED); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1878 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Add a prototype on an evacuation candidate and verify that transition |
| 1881 | // clearing correctly records slots in prototype transition array. |
| 1882 | i::FLAG_always_compact = true; |
| 1883 | Handle<Map> map(baseObject->map()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1884 | CHECK(!space->LastPage()->Contains( |
| 1885 | map->GetPrototypeTransitions()->address())); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1886 | CHECK(space->LastPage()->Contains(prototype->address())); |
| 1887 | baseObject->SetPrototype(*prototype, false)->ToObjectChecked(); |
| 1888 | CHECK(map->GetPrototypeTransition(*prototype)->IsMap()); |
| 1889 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1890 | CHECK(map->GetPrototypeTransition(*prototype)->IsMap()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1891 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1892 | |
| 1893 | |
| 1894 | TEST(ResetSharedFunctionInfoCountersDuringIncrementalMarking) { |
| 1895 | i::FLAG_allow_natives_syntax = true; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1896 | #ifdef VERIFY_HEAP |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1897 | i::FLAG_verify_heap = true; |
| 1898 | #endif |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1899 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1900 | InitializeVM(); |
| 1901 | if (!i::V8::UseCrankshaft()) return; |
| 1902 | v8::HandleScope outer_scope; |
| 1903 | |
| 1904 | { |
| 1905 | v8::HandleScope scope; |
| 1906 | CompileRun( |
| 1907 | "function f () {" |
| 1908 | " var s = 0;" |
| 1909 | " for (var i = 0; i < 100; i++) s += i;" |
| 1910 | " return s;" |
| 1911 | "}" |
| 1912 | "f(); f();" |
| 1913 | "%OptimizeFunctionOnNextCall(f);" |
| 1914 | "f();"); |
| 1915 | } |
| 1916 | Handle<JSFunction> f = |
| 1917 | v8::Utils::OpenHandle( |
| 1918 | *v8::Handle<v8::Function>::Cast( |
| 1919 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 1920 | CHECK(f->IsOptimized()); |
| 1921 | |
| 1922 | IncrementalMarking* marking = HEAP->incremental_marking(); |
| 1923 | marking->Abort(); |
| 1924 | marking->Start(); |
| 1925 | |
| 1926 | // The following two calls will increment HEAP->global_ic_age(). |
| 1927 | const int kLongIdlePauseInMs = 1000; |
| 1928 | v8::V8::ContextDisposedNotification(); |
| 1929 | v8::V8::IdleNotification(kLongIdlePauseInMs); |
| 1930 | |
| 1931 | while (!marking->IsStopped() && !marking->IsComplete()) { |
| 1932 | marking->Step(1 * MB, IncrementalMarking::NO_GC_VIA_STACK_GUARD); |
| 1933 | } |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 1934 | if (!marking->IsStopped() || marking->should_hurry()) { |
| 1935 | // We don't normally finish a GC via Step(), we normally finish by |
| 1936 | // setting the stack guard and then do the final steps in the stack |
| 1937 | // guard interrupt. But here we didn't ask for that, and there is no |
| 1938 | // JS code running to trigger the interrupt, so we explicitly finalize |
| 1939 | // here. |
| 1940 | HEAP->CollectAllGarbage(Heap::kNoGCFlags, |
| 1941 | "Test finalizing incremental mark-sweep"); |
| 1942 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1943 | |
| 1944 | CHECK_EQ(HEAP->global_ic_age(), f->shared()->ic_age()); |
| 1945 | CHECK_EQ(0, f->shared()->opt_count()); |
| 1946 | CHECK_EQ(0, f->shared()->code()->profiler_ticks()); |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | TEST(ResetSharedFunctionInfoCountersDuringMarkSweep) { |
| 1951 | i::FLAG_allow_natives_syntax = true; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1952 | #ifdef VERIFY_HEAP |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1953 | i::FLAG_verify_heap = true; |
| 1954 | #endif |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1955 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1956 | InitializeVM(); |
| 1957 | if (!i::V8::UseCrankshaft()) return; |
| 1958 | v8::HandleScope outer_scope; |
| 1959 | |
| 1960 | { |
| 1961 | v8::HandleScope scope; |
| 1962 | CompileRun( |
| 1963 | "function f () {" |
| 1964 | " var s = 0;" |
| 1965 | " for (var i = 0; i < 100; i++) s += i;" |
| 1966 | " return s;" |
| 1967 | "}" |
| 1968 | "f(); f();" |
| 1969 | "%OptimizeFunctionOnNextCall(f);" |
| 1970 | "f();"); |
| 1971 | } |
| 1972 | Handle<JSFunction> f = |
| 1973 | v8::Utils::OpenHandle( |
| 1974 | *v8::Handle<v8::Function>::Cast( |
| 1975 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 1976 | CHECK(f->IsOptimized()); |
| 1977 | |
| 1978 | HEAP->incremental_marking()->Abort(); |
| 1979 | |
| 1980 | // The following two calls will increment HEAP->global_ic_age(). |
| 1981 | // Since incremental marking is off, IdleNotification will do full GC. |
| 1982 | const int kLongIdlePauseInMs = 1000; |
| 1983 | v8::V8::ContextDisposedNotification(); |
| 1984 | v8::V8::IdleNotification(kLongIdlePauseInMs); |
| 1985 | |
| 1986 | CHECK_EQ(HEAP->global_ic_age(), f->shared()->ic_age()); |
| 1987 | CHECK_EQ(0, f->shared()->opt_count()); |
| 1988 | CHECK_EQ(0, f->shared()->code()->profiler_ticks()); |
| 1989 | } |
| 1990 | |
| 1991 | |
| 1992 | // Test that HAllocateObject will always return an object in new-space. |
| 1993 | TEST(OptimizedAllocationAlwaysInNewSpace) { |
| 1994 | i::FLAG_allow_natives_syntax = true; |
| 1995 | InitializeVM(); |
| 1996 | if (!i::V8::UseCrankshaft() || i::FLAG_always_opt) return; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 1997 | if (i::FLAG_gc_global || i::FLAG_stress_compaction) return; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1998 | v8::HandleScope scope; |
| 1999 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 2000 | SimulateFullSpace(HEAP->new_space()); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 2001 | AlwaysAllocateScope always_allocate; |
| 2002 | v8::Local<v8::Value> res = CompileRun( |
| 2003 | "function c(x) {" |
| 2004 | " this.x = x;" |
| 2005 | " for (var i = 0; i < 32; i++) {" |
| 2006 | " this['x' + i] = x;" |
| 2007 | " }" |
| 2008 | "}" |
| 2009 | "function f(x) { return new c(x); };" |
| 2010 | "f(1); f(2); f(3);" |
| 2011 | "%OptimizeFunctionOnNextCall(f);" |
| 2012 | "f(4);"); |
| 2013 | CHECK_EQ(4, res->ToObject()->GetRealNamedProperty(v8_str("x"))->Int32Value()); |
| 2014 | |
| 2015 | Handle<JSObject> o = |
| 2016 | v8::Utils::OpenHandle(*v8::Handle<v8::Object>::Cast(res)); |
| 2017 | |
| 2018 | CHECK(HEAP->InNewSpace(*o)); |
| 2019 | } |
yangguo@chromium.org | 5f0b8ea | 2012-05-16 12:37:04 +0000 | [diff] [blame] | 2020 | |
| 2021 | |
| 2022 | static int CountMapTransitions(Map* map) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2023 | return map->transitions()->number_of_transitions(); |
yangguo@chromium.org | 5f0b8ea | 2012-05-16 12:37:04 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2027 | // Test that map transitions are cleared and maps are collected with |
| 2028 | // incremental marking as well. |
| 2029 | TEST(Regress1465) { |
| 2030 | i::FLAG_allow_natives_syntax = true; |
| 2031 | i::FLAG_trace_incremental_marking = true; |
| 2032 | InitializeVM(); |
| 2033 | v8::HandleScope scope; |
| 2034 | static const int transitions_count = 256; |
| 2035 | |
| 2036 | { |
| 2037 | AlwaysAllocateScope always_allocate; |
| 2038 | for (int i = 0; i < transitions_count; i++) { |
| 2039 | EmbeddedVector<char, 64> buffer; |
| 2040 | OS::SNPrintF(buffer, "var o = new Object; o.prop%d = %d;", i, i); |
| 2041 | CompileRun(buffer.start()); |
| 2042 | } |
| 2043 | CompileRun("var root = new Object;"); |
| 2044 | } |
| 2045 | |
| 2046 | Handle<JSObject> root = |
| 2047 | v8::Utils::OpenHandle( |
| 2048 | *v8::Handle<v8::Object>::Cast( |
| 2049 | v8::Context::GetCurrent()->Global()->Get(v8_str("root")))); |
| 2050 | |
| 2051 | // Count number of live transitions before marking. |
| 2052 | int transitions_before = CountMapTransitions(root->map()); |
| 2053 | CompileRun("%DebugPrint(root);"); |
| 2054 | CHECK_EQ(transitions_count, transitions_before); |
| 2055 | |
| 2056 | SimulateIncrementalMarking(); |
yangguo@chromium.org | 5f0b8ea | 2012-05-16 12:37:04 +0000 | [diff] [blame] | 2057 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | 5f0b8ea | 2012-05-16 12:37:04 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Count number of live transitions after marking. Note that one transition |
| 2060 | // is left, because 'o' still holds an instance of one transition target. |
| 2061 | int transitions_after = CountMapTransitions(root->map()); |
| 2062 | CompileRun("%DebugPrint(root);"); |
| 2063 | CHECK_EQ(1, transitions_after); |
| 2064 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2065 | |
| 2066 | |
| 2067 | TEST(Regress2143a) { |
| 2068 | i::FLAG_collect_maps = true; |
| 2069 | i::FLAG_incremental_marking = true; |
| 2070 | InitializeVM(); |
| 2071 | v8::HandleScope scope; |
| 2072 | |
| 2073 | // Prepare a map transition from the root object together with a yet |
| 2074 | // untransitioned root object. |
| 2075 | CompileRun("var root = new Object;" |
| 2076 | "root.foo = 0;" |
| 2077 | "root = new Object;"); |
| 2078 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2079 | SimulateIncrementalMarking(); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Compile a StoreIC that performs the prepared map transition. This |
| 2082 | // will restart incremental marking and should make sure the root is |
| 2083 | // marked grey again. |
| 2084 | CompileRun("function f(o) {" |
| 2085 | " o.foo = 0;" |
| 2086 | "}" |
| 2087 | "f(new Object);" |
| 2088 | "f(root);"); |
| 2089 | |
| 2090 | // This bug only triggers with aggressive IC clearing. |
| 2091 | HEAP->AgeInlineCaches(); |
| 2092 | |
| 2093 | // Explicitly request GC to perform final marking step and sweeping. |
| 2094 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2095 | |
| 2096 | Handle<JSObject> root = |
| 2097 | v8::Utils::OpenHandle( |
| 2098 | *v8::Handle<v8::Object>::Cast( |
| 2099 | v8::Context::GetCurrent()->Global()->Get(v8_str("root")))); |
| 2100 | |
| 2101 | // The root object should be in a sane state. |
| 2102 | CHECK(root->IsJSObject()); |
| 2103 | CHECK(root->map()->IsMap()); |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | TEST(Regress2143b) { |
| 2108 | i::FLAG_collect_maps = true; |
| 2109 | i::FLAG_incremental_marking = true; |
| 2110 | i::FLAG_allow_natives_syntax = true; |
| 2111 | InitializeVM(); |
| 2112 | v8::HandleScope scope; |
| 2113 | |
| 2114 | // Prepare a map transition from the root object together with a yet |
| 2115 | // untransitioned root object. |
| 2116 | CompileRun("var root = new Object;" |
| 2117 | "root.foo = 0;" |
| 2118 | "root = new Object;"); |
| 2119 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2120 | SimulateIncrementalMarking(); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2121 | |
| 2122 | // Compile an optimized LStoreNamedField that performs the prepared |
| 2123 | // map transition. This will restart incremental marking and should |
| 2124 | // make sure the root is marked grey again. |
| 2125 | CompileRun("function f(o) {" |
| 2126 | " o.foo = 0;" |
| 2127 | "}" |
| 2128 | "f(new Object);" |
| 2129 | "f(new Object);" |
| 2130 | "%OptimizeFunctionOnNextCall(f);" |
| 2131 | "f(root);" |
| 2132 | "%DeoptimizeFunction(f);"); |
| 2133 | |
| 2134 | // This bug only triggers with aggressive IC clearing. |
| 2135 | HEAP->AgeInlineCaches(); |
| 2136 | |
| 2137 | // Explicitly request GC to perform final marking step and sweeping. |
| 2138 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2139 | |
| 2140 | Handle<JSObject> root = |
| 2141 | v8::Utils::OpenHandle( |
| 2142 | *v8::Handle<v8::Object>::Cast( |
| 2143 | v8::Context::GetCurrent()->Global()->Get(v8_str("root")))); |
| 2144 | |
| 2145 | // The root object should be in a sane state. |
| 2146 | CHECK(root->IsJSObject()); |
| 2147 | CHECK(root->map()->IsMap()); |
| 2148 | } |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 2149 | |
| 2150 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 2151 | TEST(ReleaseOverReservedPages) { |
| 2152 | i::FLAG_trace_gc = true; |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 2153 | // The optimizer can allocate stuff, messing up the test. |
| 2154 | i::FLAG_crankshaft = false; |
| 2155 | i::FLAG_always_opt = false; |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 2156 | InitializeVM(); |
| 2157 | v8::HandleScope scope; |
| 2158 | static const int number_of_test_pages = 20; |
| 2159 | |
| 2160 | // Prepare many pages with low live-bytes count. |
| 2161 | PagedSpace* old_pointer_space = HEAP->old_pointer_space(); |
| 2162 | CHECK_EQ(1, old_pointer_space->CountTotalPages()); |
| 2163 | for (int i = 0; i < number_of_test_pages; i++) { |
| 2164 | AlwaysAllocateScope always_allocate; |
| 2165 | SimulateFullSpace(old_pointer_space); |
| 2166 | FACTORY->NewFixedArray(1, TENURED); |
| 2167 | } |
| 2168 | CHECK_EQ(number_of_test_pages + 1, old_pointer_space->CountTotalPages()); |
| 2169 | |
| 2170 | // Triggering one GC will cause a lot of garbage to be discovered but |
| 2171 | // even spread across all allocated pages. |
| 2172 | HEAP->CollectAllGarbage(Heap::kNoGCFlags, "triggered for preparation"); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 2173 | CHECK_GE(number_of_test_pages + 1, old_pointer_space->CountTotalPages()); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 2174 | |
| 2175 | // Triggering subsequent GCs should cause at least half of the pages |
| 2176 | // to be released to the OS after at most two cycles. |
| 2177 | HEAP->CollectAllGarbage(Heap::kNoGCFlags, "triggered by test 1"); |
| 2178 | CHECK_GE(number_of_test_pages + 1, old_pointer_space->CountTotalPages()); |
| 2179 | HEAP->CollectAllGarbage(Heap::kNoGCFlags, "triggered by test 2"); |
| 2180 | CHECK_GE(number_of_test_pages + 1, old_pointer_space->CountTotalPages() * 2); |
| 2181 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 2182 | // Triggering a last-resort GC should cause all pages to be released to the |
| 2183 | // OS so that other processes can seize the memory. If we get a failure here |
| 2184 | // where there are 2 pages left instead of 1, then we should increase the |
| 2185 | // size of the first page a little in SizeOfFirstPage in spaces.cc. The |
| 2186 | // first page should be small in order to reduce memory used when the VM |
| 2187 | // boots, but if the 20 small arrays don't fit on the first page then that's |
| 2188 | // an indication that it is too small. |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 2189 | HEAP->CollectAllAvailableGarbage("triggered really hard"); |
| 2190 | CHECK_EQ(1, old_pointer_space->CountTotalPages()); |
| 2191 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2192 | |
| 2193 | |
| 2194 | TEST(Regress2237) { |
| 2195 | InitializeVM(); |
| 2196 | v8::HandleScope scope; |
| 2197 | Handle<String> slice(HEAP->empty_string()); |
| 2198 | |
| 2199 | { |
| 2200 | // Generate a parent that lives in new-space. |
| 2201 | v8::HandleScope inner_scope; |
| 2202 | const char* c = "This text is long enough to trigger sliced strings."; |
| 2203 | Handle<String> s = FACTORY->NewStringFromAscii(CStrVector(c)); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 2204 | CHECK(s->IsSeqOneByteString()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2205 | CHECK(HEAP->InNewSpace(*s)); |
| 2206 | |
| 2207 | // Generate a sliced string that is based on the above parent and |
| 2208 | // lives in old-space. |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 2209 | SimulateFullSpace(HEAP->new_space()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2210 | AlwaysAllocateScope always_allocate; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 2211 | Handle<String> t = FACTORY->NewProperSubString(s, 5, 35); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2212 | CHECK(t->IsSlicedString()); |
| 2213 | CHECK(!HEAP->InNewSpace(*t)); |
| 2214 | *slice.location() = *t.location(); |
| 2215 | } |
| 2216 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 2217 | CHECK(SlicedString::cast(*slice)->parent()->IsSeqOneByteString()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2218 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 2219 | CHECK(SlicedString::cast(*slice)->parent()->IsSeqOneByteString()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 2220 | } |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 2221 | |
| 2222 | |
| 2223 | #ifdef OBJECT_PRINT |
| 2224 | TEST(PrintSharedFunctionInfo) { |
| 2225 | InitializeVM(); |
| 2226 | v8::HandleScope scope; |
| 2227 | const char* source = "f = function() { return 987654321; }\n" |
| 2228 | "g = function() { return 123456789; }\n"; |
| 2229 | CompileRun(source); |
| 2230 | Handle<JSFunction> g = |
| 2231 | v8::Utils::OpenHandle( |
| 2232 | *v8::Handle<v8::Function>::Cast( |
| 2233 | v8::Context::GetCurrent()->Global()->Get(v8_str("g")))); |
| 2234 | |
| 2235 | AssertNoAllocation no_alloc; |
| 2236 | g->shared()->PrintLn(); |
| 2237 | } |
| 2238 | #endif // OBJECT_PRINT |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 2239 | |
| 2240 | |
| 2241 | TEST(Regress2211) { |
| 2242 | InitializeVM(); |
| 2243 | v8::HandleScope scope; |
| 2244 | |
| 2245 | v8::Handle<v8::String> value = v8_str("val string"); |
| 2246 | Smi* hash = Smi::FromInt(321); |
| 2247 | Heap* heap = Isolate::Current()->heap(); |
| 2248 | |
| 2249 | for (int i = 0; i < 2; i++) { |
| 2250 | // Store identity hash first and common hidden property second. |
| 2251 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 2252 | Handle<JSObject> internal_obj = v8::Utils::OpenHandle(*obj); |
| 2253 | CHECK(internal_obj->HasFastProperties()); |
| 2254 | |
| 2255 | // In the first iteration, set hidden value first and identity hash second. |
| 2256 | // In the second iteration, reverse the order. |
| 2257 | if (i == 0) obj->SetHiddenValue(v8_str("key string"), value); |
| 2258 | MaybeObject* maybe_obj = internal_obj->SetIdentityHash(hash, |
| 2259 | ALLOW_CREATION); |
| 2260 | CHECK(!maybe_obj->IsFailure()); |
| 2261 | if (i == 1) obj->SetHiddenValue(v8_str("key string"), value); |
| 2262 | |
| 2263 | // Check values. |
| 2264 | CHECK_EQ(hash, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2265 | internal_obj->GetHiddenProperty(heap->identity_hash_string())); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 2266 | CHECK(value->Equals(obj->GetHiddenValue(v8_str("key string")))); |
| 2267 | |
| 2268 | // Check size. |
| 2269 | DescriptorArray* descriptors = internal_obj->map()->instance_descriptors(); |
| 2270 | ObjectHashTable* hashtable = ObjectHashTable::cast( |
| 2271 | internal_obj->FastPropertyAt(descriptors->GetFieldIndex(0))); |
| 2272 | // HashTable header (5) and 4 initial entries (8). |
| 2273 | CHECK_LE(hashtable->SizeFor(hashtable->length()), 13 * kPointerSize); |
| 2274 | } |
| 2275 | } |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2276 | |
| 2277 | |
| 2278 | TEST(IncrementalMarkingClearsTypeFeedbackCells) { |
| 2279 | if (i::FLAG_always_opt) return; |
| 2280 | InitializeVM(); |
| 2281 | v8::HandleScope scope; |
| 2282 | v8::Local<v8::Value> fun1, fun2; |
| 2283 | |
| 2284 | { |
| 2285 | LocalContext env; |
| 2286 | CompileRun("function fun() {};"); |
| 2287 | fun1 = env->Global()->Get(v8_str("fun")); |
| 2288 | } |
| 2289 | |
| 2290 | { |
| 2291 | LocalContext env; |
| 2292 | CompileRun("function fun() {};"); |
| 2293 | fun2 = env->Global()->Get(v8_str("fun")); |
| 2294 | } |
| 2295 | |
| 2296 | // Prepare function f that contains type feedback for closures |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2297 | // originating from two different native contexts. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2298 | v8::Context::GetCurrent()->Global()->Set(v8_str("fun1"), fun1); |
| 2299 | v8::Context::GetCurrent()->Global()->Set(v8_str("fun2"), fun2); |
| 2300 | CompileRun("function f(a, b) { a(); b(); } f(fun1, fun2);"); |
| 2301 | Handle<JSFunction> f = |
| 2302 | v8::Utils::OpenHandle( |
| 2303 | *v8::Handle<v8::Function>::Cast( |
| 2304 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2305 | Handle<TypeFeedbackCells> cells(TypeFeedbackInfo::cast( |
| 2306 | f->shared()->code()->type_feedback_info())->type_feedback_cells()); |
| 2307 | |
| 2308 | CHECK_EQ(2, cells->CellCount()); |
| 2309 | CHECK(cells->Cell(0)->value()->IsJSFunction()); |
| 2310 | CHECK(cells->Cell(1)->value()->IsJSFunction()); |
| 2311 | |
| 2312 | SimulateIncrementalMarking(); |
| 2313 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 2314 | |
| 2315 | CHECK_EQ(2, cells->CellCount()); |
| 2316 | CHECK(cells->Cell(0)->value()->IsTheHole()); |
| 2317 | CHECK(cells->Cell(1)->value()->IsTheHole()); |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | static Code* FindFirstIC(Code* code, Code::Kind kind) { |
| 2322 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 2323 | RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) | |
| 2324 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID) | |
| 2325 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_CONTEXT); |
| 2326 | for (RelocIterator it(code, mask); !it.done(); it.next()) { |
| 2327 | RelocInfo* info = it.rinfo(); |
| 2328 | Code* target = Code::GetCodeFromTargetAddress(info->target_address()); |
| 2329 | if (target->is_inline_cache_stub() && target->kind() == kind) { |
| 2330 | return target; |
| 2331 | } |
| 2332 | } |
| 2333 | return NULL; |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | TEST(IncrementalMarkingPreservesMonomorhpicIC) { |
| 2338 | if (i::FLAG_always_opt) return; |
| 2339 | InitializeVM(); |
| 2340 | v8::HandleScope scope; |
| 2341 | |
| 2342 | // Prepare function f that contains a monomorphic IC for object |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2343 | // originating from the same native context. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2344 | CompileRun("function fun() { this.x = 1; }; var obj = new fun();" |
| 2345 | "function f(o) { return o.x; } f(obj); f(obj);"); |
| 2346 | Handle<JSFunction> f = |
| 2347 | v8::Utils::OpenHandle( |
| 2348 | *v8::Handle<v8::Function>::Cast( |
| 2349 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2350 | |
| 2351 | Code* ic_before = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
| 2352 | CHECK(ic_before->ic_state() == MONOMORPHIC); |
| 2353 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2354 | SimulateIncrementalMarking(); |
| 2355 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 2356 | |
| 2357 | Code* ic_after = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
| 2358 | CHECK(ic_after->ic_state() == MONOMORPHIC); |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | TEST(IncrementalMarkingClearsMonomorhpicIC) { |
| 2363 | if (i::FLAG_always_opt) return; |
| 2364 | InitializeVM(); |
| 2365 | v8::HandleScope scope; |
| 2366 | v8::Local<v8::Value> obj1; |
| 2367 | |
| 2368 | { |
| 2369 | LocalContext env; |
| 2370 | CompileRun("function fun() { this.x = 1; }; var obj = new fun();"); |
| 2371 | obj1 = env->Global()->Get(v8_str("obj")); |
| 2372 | } |
| 2373 | |
| 2374 | // Prepare function f that contains a monomorphic IC for object |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2375 | // originating from a different native context. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2376 | v8::Context::GetCurrent()->Global()->Set(v8_str("obj1"), obj1); |
| 2377 | CompileRun("function f(o) { return o.x; } f(obj1); f(obj1);"); |
| 2378 | Handle<JSFunction> f = |
| 2379 | v8::Utils::OpenHandle( |
| 2380 | *v8::Handle<v8::Function>::Cast( |
| 2381 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2382 | |
| 2383 | Code* ic_before = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
| 2384 | CHECK(ic_before->ic_state() == MONOMORPHIC); |
| 2385 | |
| 2386 | // Fire context dispose notification. |
| 2387 | v8::V8::ContextDisposedNotification(); |
| 2388 | SimulateIncrementalMarking(); |
| 2389 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 2390 | |
| 2391 | Code* ic_after = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
| 2392 | CHECK(ic_after->ic_state() == UNINITIALIZED); |
| 2393 | } |
| 2394 | |
| 2395 | |
| 2396 | TEST(IncrementalMarkingClearsPolymorhpicIC) { |
| 2397 | if (i::FLAG_always_opt) return; |
| 2398 | InitializeVM(); |
| 2399 | v8::HandleScope scope; |
| 2400 | v8::Local<v8::Value> obj1, obj2; |
| 2401 | |
| 2402 | { |
| 2403 | LocalContext env; |
| 2404 | CompileRun("function fun() { this.x = 1; }; var obj = new fun();"); |
| 2405 | obj1 = env->Global()->Get(v8_str("obj")); |
| 2406 | } |
| 2407 | |
| 2408 | { |
| 2409 | LocalContext env; |
| 2410 | CompileRun("function fun() { this.x = 2; }; var obj = new fun();"); |
| 2411 | obj2 = env->Global()->Get(v8_str("obj")); |
| 2412 | } |
| 2413 | |
| 2414 | // Prepare function f that contains a polymorphic IC for objects |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2415 | // originating from two different native contexts. |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2416 | v8::Context::GetCurrent()->Global()->Set(v8_str("obj1"), obj1); |
| 2417 | v8::Context::GetCurrent()->Global()->Set(v8_str("obj2"), obj2); |
| 2418 | CompileRun("function f(o) { return o.x; } f(obj1); f(obj1); f(obj2);"); |
| 2419 | Handle<JSFunction> f = |
| 2420 | v8::Utils::OpenHandle( |
| 2421 | *v8::Handle<v8::Function>::Cast( |
| 2422 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2423 | |
| 2424 | Code* ic_before = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2425 | CHECK(ic_before->ic_state() == POLYMORPHIC); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Fire context dispose notification. |
| 2428 | v8::V8::ContextDisposedNotification(); |
| 2429 | SimulateIncrementalMarking(); |
| 2430 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 2431 | |
| 2432 | Code* ic_after = FindFirstIC(f->shared()->code(), Code::LOAD_IC); |
| 2433 | CHECK(ic_after->ic_state() == UNINITIALIZED); |
| 2434 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2435 | |
| 2436 | |
| 2437 | class SourceResource: public v8::String::ExternalAsciiStringResource { |
| 2438 | public: |
| 2439 | explicit SourceResource(const char* data) |
| 2440 | : data_(data), length_(strlen(data)) { } |
| 2441 | |
| 2442 | virtual void Dispose() { |
| 2443 | i::DeleteArray(data_); |
| 2444 | data_ = NULL; |
| 2445 | } |
| 2446 | |
| 2447 | const char* data() const { return data_; } |
| 2448 | |
| 2449 | size_t length() const { return length_; } |
| 2450 | |
| 2451 | bool IsDisposed() { return data_ == NULL; } |
| 2452 | |
| 2453 | private: |
| 2454 | const char* data_; |
| 2455 | size_t length_; |
| 2456 | }; |
| 2457 | |
| 2458 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2459 | void ReleaseStackTraceDataTest(const char* source) { |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2460 | // Test that the data retained by the Error.stack accessor is released |
| 2461 | // after the first time the accessor is fired. We use external string |
| 2462 | // to check whether the data is being released since the external string |
| 2463 | // resource's callback is fired when the external string is GC'ed. |
| 2464 | InitializeVM(); |
| 2465 | v8::HandleScope scope; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2466 | SourceResource* resource = new SourceResource(i::StrDup(source)); |
| 2467 | { |
| 2468 | v8::HandleScope scope; |
| 2469 | v8::Handle<v8::String> source_string = v8::String::NewExternal(resource); |
| 2470 | v8::Script::Compile(source_string)->Run(); |
| 2471 | CHECK(!resource->IsDisposed()); |
| 2472 | } |
| 2473 | HEAP->CollectAllAvailableGarbage(); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2474 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2475 | // External source has been released. |
| 2476 | CHECK(resource->IsDisposed()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2477 | delete resource; |
| 2478 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2479 | |
| 2480 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2481 | TEST(ReleaseStackTraceData) { |
| 2482 | static const char* source1 = "var error = null; " |
| 2483 | /* Normal Error */ "try { " |
| 2484 | " throw new Error(); " |
| 2485 | "} catch (e) { " |
| 2486 | " error = e; " |
| 2487 | "} "; |
| 2488 | static const char* source2 = "var error = null; " |
| 2489 | /* Stack overflow */ "try { " |
| 2490 | " (function f() { f(); })(); " |
| 2491 | "} catch (e) { " |
| 2492 | " error = e; " |
| 2493 | "} "; |
| 2494 | ReleaseStackTraceDataTest(source1); |
| 2495 | ReleaseStackTraceDataTest(source2); |
| 2496 | } |
| 2497 | |
| 2498 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2499 | TEST(Regression144230) { |
| 2500 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2501 | Isolate* isolate = Isolate::Current(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2502 | Heap* heap = isolate->heap(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2503 | v8::HandleScope scope; |
| 2504 | |
| 2505 | // First make sure that the uninitialized CallIC stub is on a single page |
| 2506 | // that will later be selected as an evacuation candidate. |
| 2507 | { |
| 2508 | v8::HandleScope inner_scope; |
| 2509 | AlwaysAllocateScope always_allocate; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2510 | SimulateFullSpace(heap->code_space()); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2511 | isolate->stub_cache()->ComputeCallInitialize(9, RelocInfo::CODE_TARGET); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | // Second compile a CallIC and execute it once so that it gets patched to |
| 2515 | // the pre-monomorphic stub. These code objects are on yet another page. |
| 2516 | { |
| 2517 | v8::HandleScope inner_scope; |
| 2518 | AlwaysAllocateScope always_allocate; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2519 | SimulateFullSpace(heap->code_space()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2520 | CompileRun("var o = { f:function(a,b,c,d,e,f,g,h,i) {}};" |
| 2521 | "function call() { o.f(1,2,3,4,5,6,7,8,9); };" |
| 2522 | "call();"); |
| 2523 | } |
| 2524 | |
| 2525 | // Third we fill up the last page of the code space so that it does not get |
| 2526 | // chosen as an evacuation candidate. |
| 2527 | { |
| 2528 | v8::HandleScope inner_scope; |
| 2529 | AlwaysAllocateScope always_allocate; |
| 2530 | CompileRun("for (var i = 0; i < 2000; i++) {" |
| 2531 | " eval('function f' + i + '() { return ' + i +'; };' +" |
| 2532 | " 'f' + i + '();');" |
| 2533 | "}"); |
| 2534 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2535 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2536 | |
| 2537 | // Fourth is the tricky part. Make sure the code containing the CallIC is |
| 2538 | // visited first without clearing the IC. The shared function info is then |
| 2539 | // visited later, causing the CallIC to be cleared. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2540 | Handle<String> name = isolate->factory()->InternalizeUtf8String("call"); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2541 | Handle<GlobalObject> global(isolate->context()->global_object()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2542 | MaybeObject* maybe_call = global->GetProperty(*name); |
| 2543 | JSFunction* call = JSFunction::cast(maybe_call->ToObjectChecked()); |
| 2544 | USE(global->SetProperty(*name, Smi::FromInt(0), NONE, kNonStrictMode)); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2545 | isolate->compilation_cache()->Clear(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2546 | call->shared()->set_ic_age(heap->global_ic_age() + 1); |
| 2547 | Handle<Object> call_code(call->code(), isolate); |
| 2548 | Handle<Object> call_function(call, isolate); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Now we are ready to mess up the heap. |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2551 | heap->CollectAllGarbage(Heap::kReduceMemoryFootprintMask); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Either heap verification caught the problem already or we go kaboom once |
| 2554 | // the CallIC is executed the next time. |
| 2555 | USE(global->SetProperty(*name, *call_function, NONE, kNonStrictMode)); |
| 2556 | CompileRun("call();"); |
| 2557 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2558 | |
| 2559 | |
| 2560 | TEST(Regress159140) { |
| 2561 | i::FLAG_allow_natives_syntax = true; |
| 2562 | i::FLAG_flush_code_incrementally = true; |
| 2563 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2564 | Isolate* isolate = Isolate::Current(); |
| 2565 | Heap* heap = isolate->heap(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2566 | v8::HandleScope scope; |
| 2567 | |
| 2568 | // Perform one initial GC to enable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2569 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Prepare several closures that are all eligible for code flushing |
| 2572 | // because all reachable ones are not optimized. Make sure that the |
| 2573 | // optimized code object is directly reachable through a handle so |
| 2574 | // that it is marked black during incremental marking. |
| 2575 | Handle<Code> code; |
| 2576 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2577 | HandleScope inner_scope(isolate); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2578 | CompileRun("function h(x) {}" |
| 2579 | "function mkClosure() {" |
| 2580 | " return function(x) { return x + 1; };" |
| 2581 | "}" |
| 2582 | "var f = mkClosure();" |
| 2583 | "var g = mkClosure();" |
| 2584 | "f(1); f(2);" |
| 2585 | "g(1); g(2);" |
| 2586 | "h(1); h(2);" |
| 2587 | "%OptimizeFunctionOnNextCall(f); f(3);" |
| 2588 | "%OptimizeFunctionOnNextCall(h); h(3);"); |
| 2589 | |
| 2590 | Handle<JSFunction> f = |
| 2591 | v8::Utils::OpenHandle( |
| 2592 | *v8::Handle<v8::Function>::Cast( |
| 2593 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2594 | CHECK(f->is_compiled()); |
| 2595 | CompileRun("f = null;"); |
| 2596 | |
| 2597 | Handle<JSFunction> g = |
| 2598 | v8::Utils::OpenHandle( |
| 2599 | *v8::Handle<v8::Function>::Cast( |
| 2600 | v8::Context::GetCurrent()->Global()->Get(v8_str("g")))); |
| 2601 | CHECK(g->is_compiled()); |
| 2602 | const int kAgingThreshold = 6; |
| 2603 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2604 | g->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2605 | } |
| 2606 | |
| 2607 | code = inner_scope.CloseAndEscape(Handle<Code>(f->code())); |
| 2608 | } |
| 2609 | |
| 2610 | // Simulate incremental marking so that the functions are enqueued as |
| 2611 | // code flushing candidates. Then optimize one function. Finally |
| 2612 | // finish the GC to complete code flushing. |
| 2613 | SimulateIncrementalMarking(); |
| 2614 | CompileRun("%OptimizeFunctionOnNextCall(g); g(3);"); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2615 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2616 | |
| 2617 | // Unoptimized code is missing and the deoptimizer will go ballistic. |
| 2618 | CompileRun("g('bozo');"); |
| 2619 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2620 | |
| 2621 | |
| 2622 | TEST(Regress165495) { |
| 2623 | i::FLAG_allow_natives_syntax = true; |
| 2624 | i::FLAG_flush_code_incrementally = true; |
| 2625 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2626 | Isolate* isolate = Isolate::Current(); |
| 2627 | Heap* heap = isolate->heap(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2628 | v8::HandleScope scope; |
| 2629 | |
| 2630 | // Perform one initial GC to enable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2631 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2632 | |
| 2633 | // Prepare an optimized closure that the optimized code map will get |
| 2634 | // populated. Then age the unoptimized code to trigger code flushing |
| 2635 | // but make sure the optimized code is unreachable. |
| 2636 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2637 | HandleScope inner_scope(isolate); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2638 | CompileRun("function mkClosure() {" |
| 2639 | " return function(x) { return x + 1; };" |
| 2640 | "}" |
| 2641 | "var f = mkClosure();" |
| 2642 | "f(1); f(2);" |
| 2643 | "%OptimizeFunctionOnNextCall(f); f(3);"); |
| 2644 | |
| 2645 | Handle<JSFunction> f = |
| 2646 | v8::Utils::OpenHandle( |
| 2647 | *v8::Handle<v8::Function>::Cast( |
| 2648 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2649 | CHECK(f->is_compiled()); |
| 2650 | const int kAgingThreshold = 6; |
| 2651 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2652 | f->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2653 | } |
| 2654 | |
| 2655 | CompileRun("f = null;"); |
| 2656 | } |
| 2657 | |
| 2658 | // Simulate incremental marking so that unoptimized code is flushed |
| 2659 | // even though it still is cached in the optimized code map. |
| 2660 | SimulateIncrementalMarking(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2661 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2662 | |
| 2663 | // Make a new closure that will get code installed from the code map. |
| 2664 | // Unoptimized code is missing and the deoptimizer will go ballistic. |
| 2665 | CompileRun("var g = mkClosure(); g('bozo');"); |
| 2666 | } |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2667 | |
| 2668 | |
| 2669 | TEST(Regress169209) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2670 | i::FLAG_stress_compaction = false; |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2671 | i::FLAG_allow_natives_syntax = true; |
| 2672 | i::FLAG_flush_code_incrementally = true; |
| 2673 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2674 | Isolate* isolate = Isolate::Current(); |
| 2675 | Heap* heap = isolate->heap(); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2676 | v8::HandleScope scope; |
| 2677 | |
| 2678 | // Perform one initial GC to enable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2679 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2680 | |
| 2681 | // Prepare a shared function info eligible for code flushing for which |
| 2682 | // the unoptimized code will be replaced during optimization. |
| 2683 | Handle<SharedFunctionInfo> shared1; |
| 2684 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2685 | HandleScope inner_scope(isolate); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2686 | CompileRun("function f() { return 'foobar'; }" |
| 2687 | "function g(x) { if (x) f(); }" |
| 2688 | "f();" |
| 2689 | "g(false);" |
| 2690 | "g(false);"); |
| 2691 | |
| 2692 | Handle<JSFunction> f = |
| 2693 | v8::Utils::OpenHandle( |
| 2694 | *v8::Handle<v8::Function>::Cast( |
| 2695 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2696 | CHECK(f->is_compiled()); |
| 2697 | const int kAgingThreshold = 6; |
| 2698 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2699 | f->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2700 | } |
| 2701 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2702 | shared1 = inner_scope.CloseAndEscape(handle(f->shared(), isolate)); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | // Prepare a shared function info eligible for code flushing that will |
| 2706 | // represent the dangling tail of the candidate list. |
| 2707 | Handle<SharedFunctionInfo> shared2; |
| 2708 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2709 | HandleScope inner_scope(isolate); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2710 | CompileRun("function flushMe() { return 0; }" |
| 2711 | "flushMe(1);"); |
| 2712 | |
| 2713 | Handle<JSFunction> f = |
| 2714 | v8::Utils::OpenHandle( |
| 2715 | *v8::Handle<v8::Function>::Cast( |
| 2716 | v8::Context::GetCurrent()->Global()->Get(v8_str("flushMe")))); |
| 2717 | CHECK(f->is_compiled()); |
| 2718 | const int kAgingThreshold = 6; |
| 2719 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2720 | f->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2721 | } |
| 2722 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2723 | shared2 = inner_scope.CloseAndEscape(handle(f->shared(), isolate)); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | // Simulate incremental marking and collect code flushing candidates. |
| 2727 | SimulateIncrementalMarking(); |
| 2728 | CHECK(shared1->code()->gc_metadata() != NULL); |
| 2729 | |
| 2730 | // Optimize function and make sure the unoptimized code is replaced. |
| 2731 | #ifdef DEBUG |
| 2732 | FLAG_stop_at = "f"; |
| 2733 | #endif |
| 2734 | CompileRun("%OptimizeFunctionOnNextCall(g);" |
| 2735 | "g(false);"); |
| 2736 | |
| 2737 | // Finish garbage collection cycle. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2738 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 2739 | CHECK(shared1->code()->gc_metadata() == NULL); |
| 2740 | } |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 2741 | |
| 2742 | |
| 2743 | // Helper function that simulates a fill new-space in the heap. |
| 2744 | static inline void AllocateAllButNBytes(v8::internal::NewSpace* space, |
| 2745 | int extra_bytes) { |
| 2746 | int space_remaining = static_cast<int>( |
| 2747 | *space->allocation_limit_address() - *space->allocation_top_address()); |
| 2748 | CHECK(space_remaining >= extra_bytes); |
| 2749 | int new_linear_size = space_remaining - extra_bytes; |
| 2750 | v8::internal::MaybeObject* maybe = space->AllocateRaw(new_linear_size); |
| 2751 | v8::internal::FreeListNode* node = v8::internal::FreeListNode::cast(maybe); |
| 2752 | node->set_size(space->heap(), new_linear_size); |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | TEST(Regress169928) { |
| 2757 | i::FLAG_allow_natives_syntax = true; |
| 2758 | i::FLAG_crankshaft = false; |
| 2759 | InitializeVM(); |
| 2760 | v8::HandleScope scope; |
| 2761 | |
| 2762 | // Some flags turn Scavenge collections into Mark-sweep collections |
| 2763 | // and hence are incompatible with this test case. |
| 2764 | if (FLAG_gc_global || FLAG_stress_compaction) return; |
| 2765 | |
| 2766 | // Prepare the environment |
| 2767 | CompileRun("function fastliteralcase(literal, value) {" |
| 2768 | " literal[0] = value;" |
| 2769 | " return literal;" |
| 2770 | "}" |
| 2771 | "function get_standard_literal() {" |
| 2772 | " var literal = [1, 2, 3];" |
| 2773 | " return literal;" |
| 2774 | "}" |
| 2775 | "obj = fastliteralcase(get_standard_literal(), 1);" |
| 2776 | "obj = fastliteralcase(get_standard_literal(), 1.5);" |
| 2777 | "obj = fastliteralcase(get_standard_literal(), 2);"); |
| 2778 | |
| 2779 | // prepare the heap |
| 2780 | v8::Local<v8::String> mote_code_string = |
| 2781 | v8_str("fastliteralcase(mote, 2.5);"); |
| 2782 | |
| 2783 | v8::Local<v8::String> array_name = v8_str("mote"); |
| 2784 | v8::Context::GetCurrent()->Global()->Set(array_name, v8::Int32::New(0)); |
| 2785 | |
| 2786 | // First make sure we flip spaces |
| 2787 | HEAP->CollectGarbage(NEW_SPACE); |
| 2788 | |
| 2789 | // Allocate the object. |
| 2790 | Handle<FixedArray> array_data = FACTORY->NewFixedArray(2, NOT_TENURED); |
| 2791 | array_data->set(0, Smi::FromInt(1)); |
| 2792 | array_data->set(1, Smi::FromInt(2)); |
| 2793 | |
| 2794 | AllocateAllButNBytes(HEAP->new_space(), |
| 2795 | JSArray::kSize + AllocationSiteInfo::kSize + |
| 2796 | kPointerSize); |
| 2797 | |
| 2798 | Handle<JSArray> array = FACTORY->NewJSArrayWithElements(array_data, |
| 2799 | FAST_SMI_ELEMENTS, |
| 2800 | NOT_TENURED); |
| 2801 | |
| 2802 | CHECK_EQ(Smi::FromInt(2), array->length()); |
| 2803 | CHECK(array->HasFastSmiOrObjectElements()); |
| 2804 | |
| 2805 | // We need filler the size of AllocationSiteInfo object, plus an extra |
| 2806 | // fill pointer value. |
| 2807 | MaybeObject* maybe_object = HEAP->AllocateRaw( |
| 2808 | AllocationSiteInfo::kSize + kPointerSize, NEW_SPACE, OLD_POINTER_SPACE); |
| 2809 | Object* obj = NULL; |
| 2810 | CHECK(maybe_object->ToObject(&obj)); |
| 2811 | Address addr_obj = reinterpret_cast<Address>( |
| 2812 | reinterpret_cast<byte*>(obj - kHeapObjectTag)); |
| 2813 | HEAP->CreateFillerObjectAt(addr_obj, |
| 2814 | AllocationSiteInfo::kSize + kPointerSize); |
| 2815 | |
| 2816 | // Give the array a name, making sure not to allocate strings. |
| 2817 | v8::Handle<v8::Object> array_obj = v8::Utils::ToLocal(array); |
| 2818 | v8::Context::GetCurrent()->Global()->Set(array_name, array_obj); |
| 2819 | |
| 2820 | // This should crash with a protection violation if we are running a build |
| 2821 | // with the bug. |
| 2822 | AlwaysAllocateScope aa_scope; |
| 2823 | v8::Script::Compile(mote_code_string)->Run(); |
| 2824 | } |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2825 | |
| 2826 | |
| 2827 | TEST(Regress168801) { |
| 2828 | i::FLAG_always_compact = true; |
| 2829 | i::FLAG_cache_optimized_code = false; |
| 2830 | i::FLAG_allow_natives_syntax = true; |
| 2831 | i::FLAG_flush_code_incrementally = true; |
| 2832 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2833 | Isolate* isolate = Isolate::Current(); |
| 2834 | Heap* heap = isolate->heap(); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2835 | v8::HandleScope scope; |
| 2836 | |
| 2837 | // Perform one initial GC to enable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2838 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2839 | |
| 2840 | // Ensure the code ends up on an evacuation candidate. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2841 | SimulateFullSpace(heap->code_space()); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2842 | |
| 2843 | // Prepare an unoptimized function that is eligible for code flushing. |
| 2844 | Handle<JSFunction> function; |
| 2845 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2846 | HandleScope inner_scope(isolate); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2847 | CompileRun("function mkClosure() {" |
| 2848 | " return function(x) { return x + 1; };" |
| 2849 | "}" |
| 2850 | "var f = mkClosure();" |
| 2851 | "f(1); f(2);"); |
| 2852 | |
| 2853 | Handle<JSFunction> f = |
| 2854 | v8::Utils::OpenHandle( |
| 2855 | *v8::Handle<v8::Function>::Cast( |
| 2856 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2857 | CHECK(f->is_compiled()); |
| 2858 | const int kAgingThreshold = 6; |
| 2859 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2860 | f->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2861 | } |
| 2862 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2863 | function = inner_scope.CloseAndEscape(handle(*f, isolate)); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | // Simulate incremental marking so that unoptimized function is enqueued as a |
| 2867 | // candidate for code flushing. The shared function info however will not be |
| 2868 | // explicitly enqueued. |
| 2869 | SimulateIncrementalMarking(); |
| 2870 | |
| 2871 | // Now optimize the function so that it is taken off the candidate list. |
| 2872 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2873 | HandleScope inner_scope(isolate); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2874 | CompileRun("%OptimizeFunctionOnNextCall(f); f(3);"); |
| 2875 | } |
| 2876 | |
| 2877 | // This cycle will bust the heap and subsequent cycles will go ballistic. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2878 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 2879 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
mvstanton@chromium.org | c47dff5 | 2013-01-23 16:28:41 +0000 | [diff] [blame] | 2880 | } |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2881 | |
| 2882 | |
| 2883 | TEST(Regress173458) { |
| 2884 | i::FLAG_always_compact = true; |
| 2885 | i::FLAG_cache_optimized_code = false; |
| 2886 | i::FLAG_allow_natives_syntax = true; |
| 2887 | i::FLAG_flush_code_incrementally = true; |
| 2888 | InitializeVM(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2889 | Isolate* isolate = Isolate::Current(); |
| 2890 | Heap* heap = isolate->heap(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2891 | v8::HandleScope scope; |
| 2892 | |
| 2893 | // Perform one initial GC to enable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2894 | heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2895 | |
| 2896 | // Ensure the code ends up on an evacuation candidate. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2897 | SimulateFullSpace(heap->code_space()); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2898 | |
| 2899 | // Prepare an unoptimized function that is eligible for code flushing. |
| 2900 | Handle<JSFunction> function; |
| 2901 | { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2902 | HandleScope inner_scope(isolate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2903 | CompileRun("function mkClosure() {" |
| 2904 | " return function(x) { return x + 1; };" |
| 2905 | "}" |
| 2906 | "var f = mkClosure();" |
| 2907 | "f(1); f(2);"); |
| 2908 | |
| 2909 | Handle<JSFunction> f = |
| 2910 | v8::Utils::OpenHandle( |
| 2911 | *v8::Handle<v8::Function>::Cast( |
| 2912 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 2913 | CHECK(f->is_compiled()); |
| 2914 | const int kAgingThreshold = 6; |
| 2915 | for (int i = 0; i < kAgingThreshold; i++) { |
| 2916 | f->shared()->code()->MakeOlder(static_cast<MarkingParity>(i % 2)); |
| 2917 | } |
| 2918 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2919 | function = inner_scope.CloseAndEscape(handle(*f, isolate)); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | // Simulate incremental marking so that unoptimized function is enqueued as a |
| 2923 | // candidate for code flushing. The shared function info however will not be |
| 2924 | // explicitly enqueued. |
| 2925 | SimulateIncrementalMarking(); |
| 2926 | |
| 2927 | // Now enable the debugger which in turn will disable code flushing. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2928 | CHECK(isolate->debug()->Load()); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2929 | |
| 2930 | // This cycle will bust the heap and subsequent cycles will go ballistic. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2931 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 2932 | heap->CollectAllGarbage(Heap::kNoGCFlags); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2933 | } |