OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/heap/array-buffer-tracker.h" |
| 6 #include "test/cctest/cctest.h" |
| 7 #include "test/cctest/heap/heap-utils.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 typedef i::LocalArrayBufferTracker LocalTracker; |
| 12 |
| 13 bool IsTracked(i::JSArrayBuffer* buf) { |
| 14 return i::ArrayBufferTracker::IsTracked(buf); |
| 15 } |
| 16 |
| 17 bool IsTrackedInOldSpace(i::JSArrayBuffer* buf) { |
| 18 return !i::Page::FromAddress(buf->address())->InNewSpace() && IsTracked(buf); |
| 19 } |
| 20 |
| 21 bool IsTrackedInNewSpace(i::JSArrayBuffer* buf) { |
| 22 return i::Page::FromAddress(buf->address())->InNewSpace() && IsTracked(buf); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 namespace v8 { |
| 28 namespace internal { |
| 29 |
| 30 // The following tests make sure that JSArrayBuffer tracking works expected when |
| 31 // moving the objects through various spaces during GC phases. |
| 32 |
| 33 TEST(ArrayBuffer_OnlyMC) { |
| 34 CcTest::InitializeVM(); |
| 35 LocalContext env; |
| 36 v8::Isolate* isolate = env->GetIsolate(); |
| 37 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 38 |
| 39 JSArrayBuffer* raw_ab = nullptr; |
| 40 { |
| 41 v8::HandleScope handle_scope(isolate); |
| 42 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 43 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 44 CHECK(IsTrackedInNewSpace(*buf)); |
| 45 heap::GcAndSweep(heap, OLD_SPACE); |
| 46 CHECK(IsTrackedInNewSpace(*buf)); |
| 47 heap::GcAndSweep(heap, OLD_SPACE); |
| 48 CHECK(IsTrackedInOldSpace(*buf)); |
| 49 raw_ab = *buf; |
| 50 // Prohibit page from being released. |
| 51 Page::FromAddress(buf->address())->MarkNeverEvacuate(); |
| 52 } |
| 53 // 2 GCs are needed because we promote to old space as live, meaning that |
| 54 // we will survive one GC. |
| 55 heap::GcAndSweep(heap, OLD_SPACE); |
| 56 heap::GcAndSweep(heap, OLD_SPACE); |
| 57 CHECK(!IsTracked(raw_ab)); |
| 58 } |
| 59 |
| 60 TEST(ArrayBuffer_OnlyScavenge) { |
| 61 CcTest::InitializeVM(); |
| 62 LocalContext env; |
| 63 v8::Isolate* isolate = env->GetIsolate(); |
| 64 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 65 |
| 66 JSArrayBuffer* raw_ab = nullptr; |
| 67 { |
| 68 v8::HandleScope handle_scope(isolate); |
| 69 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 70 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 71 CHECK(IsTrackedInNewSpace(*buf)); |
| 72 heap::GcAndSweep(heap, NEW_SPACE); |
| 73 CHECK(IsTrackedInNewSpace(*buf)); |
| 74 heap::GcAndSweep(heap, NEW_SPACE); |
| 75 CHECK(IsTrackedInOldSpace(*buf)); |
| 76 heap::GcAndSweep(heap, NEW_SPACE); |
| 77 CHECK(IsTrackedInOldSpace(*buf)); |
| 78 raw_ab = *buf; |
| 79 // Prohibit page from being released. |
| 80 Page::FromAddress(buf->address())->MarkNeverEvacuate(); |
| 81 } |
| 82 // 2 GCs are needed because we promote to old space as live, meaning that |
| 83 // we will survive one GC. |
| 84 heap::GcAndSweep(heap, OLD_SPACE); |
| 85 heap::GcAndSweep(heap, OLD_SPACE); |
| 86 CHECK(!IsTracked(raw_ab)); |
| 87 } |
| 88 |
| 89 TEST(ArrayBuffer_ScavengeAndMC) { |
| 90 CcTest::InitializeVM(); |
| 91 LocalContext env; |
| 92 v8::Isolate* isolate = env->GetIsolate(); |
| 93 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 94 |
| 95 JSArrayBuffer* raw_ab = nullptr; |
| 96 { |
| 97 v8::HandleScope handle_scope(isolate); |
| 98 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 99 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 100 CHECK(IsTrackedInNewSpace(*buf)); |
| 101 heap::GcAndSweep(heap, NEW_SPACE); |
| 102 CHECK(IsTrackedInNewSpace(*buf)); |
| 103 heap::GcAndSweep(heap, NEW_SPACE); |
| 104 CHECK(IsTrackedInOldSpace(*buf)); |
| 105 heap::GcAndSweep(heap, OLD_SPACE); |
| 106 CHECK(IsTrackedInOldSpace(*buf)); |
| 107 heap::GcAndSweep(heap, NEW_SPACE); |
| 108 CHECK(IsTrackedInOldSpace(*buf)); |
| 109 raw_ab = *buf; |
| 110 // Prohibit page from being released. |
| 111 Page::FromAddress(buf->address())->MarkNeverEvacuate(); |
| 112 } |
| 113 // 2 GCs are needed because we promote to old space as live, meaning that |
| 114 // we will survive one GC. |
| 115 heap::GcAndSweep(heap, OLD_SPACE); |
| 116 heap::GcAndSweep(heap, OLD_SPACE); |
| 117 CHECK(!IsTracked(raw_ab)); |
| 118 } |
| 119 |
| 120 TEST(ArrayBuffer_Compaction) { |
| 121 FLAG_manual_evacuation_candidates_selection = true; |
| 122 CcTest::InitializeVM(); |
| 123 LocalContext env; |
| 124 v8::Isolate* isolate = env->GetIsolate(); |
| 125 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 126 heap::AbandonCurrentlyFreeMemory(heap->old_space()); |
| 127 |
| 128 v8::HandleScope handle_scope(isolate); |
| 129 Local<v8::ArrayBuffer> ab1 = v8::ArrayBuffer::New(isolate, 100); |
| 130 Handle<JSArrayBuffer> buf1 = v8::Utils::OpenHandle(*ab1); |
| 131 CHECK(IsTrackedInNewSpace(*buf1)); |
| 132 heap::GcAndSweep(heap, NEW_SPACE); |
| 133 heap::GcAndSweep(heap, NEW_SPACE); |
| 134 |
| 135 Page* page_before_gc = Page::FromAddress(buf1->address()); |
| 136 page_before_gc->SetFlag(MemoryChunk::FORCE_EVACUATION_CANDIDATE_FOR_TESTING); |
| 137 CHECK(IsTrackedInOldSpace(*buf1)); |
| 138 |
| 139 heap->CollectAllGarbage(); |
| 140 |
| 141 Page* page_after_gc = Page::FromAddress(buf1->address()); |
| 142 CHECK(IsTrackedInOldSpace(*buf1)); |
| 143 |
| 144 CHECK_NE(page_before_gc, page_after_gc); |
| 145 } |
| 146 |
| 147 TEST(ArrayBuffer_UnregisterDuringSweep) { |
| 148 // Regular pages in old space (without compaction) are processed concurrently |
| 149 // in the sweeper. If we happen to unregister a buffer (either explicitly, or |
| 150 // implicitly through e.g. |Externalize|) we need to sync with the sweeper |
| 151 // task. |
| 152 // |
| 153 // Note: This test will will only fail on TSAN configurations. |
| 154 |
| 155 // Disable verify-heap since it forces sweeping to be completed in the |
| 156 // epilogue of the GC. |
| 157 #ifdef VERIFY_HEAP |
| 158 i::FLAG_verify_heap = false; |
| 159 #endif // VERIFY_HEAP |
| 160 |
| 161 CcTest::InitializeVM(); |
| 162 LocalContext env; |
| 163 v8::Isolate* isolate = env->GetIsolate(); |
| 164 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 165 { |
| 166 v8::HandleScope handle_scope(isolate); |
| 167 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 168 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 169 |
| 170 { |
| 171 v8::HandleScope handle_scope(isolate); |
| 172 // Allocate another buffer on the same page to force processing a |
| 173 // non-empty set of buffers in the last GC. |
| 174 Local<v8::ArrayBuffer> ab2 = v8::ArrayBuffer::New(isolate, 100); |
| 175 Handle<JSArrayBuffer> buf2 = v8::Utils::OpenHandle(*ab2); |
| 176 CHECK(IsTrackedInNewSpace(*buf)); |
| 177 CHECK(IsTrackedInNewSpace(*buf)); |
| 178 heap::GcAndSweep(heap, NEW_SPACE); |
| 179 CHECK(IsTrackedInNewSpace(*buf)); |
| 180 CHECK(IsTrackedInNewSpace(*buf)); |
| 181 heap::GcAndSweep(heap, NEW_SPACE); |
| 182 CHECK(IsTrackedInOldSpace(*buf)); |
| 183 CHECK(IsTrackedInOldSpace(*buf2)); |
| 184 } |
| 185 |
| 186 heap->CollectGarbage(OLD_SPACE); |
| 187 // |Externalize| will cause the buffer to be |Unregister|ed. Without |
| 188 // barriers and proper synchronization this will trigger a data race on |
| 189 // TSAN. |
| 190 v8::ArrayBuffer::Contents contents = ab->Externalize(); |
| 191 heap->isolate()->array_buffer_allocator()->Free(contents.Data(), |
| 192 contents.ByteLength()); |
| 193 } |
| 194 } |
| 195 |
| 196 TEST(ArrayBuffer_NonLivePromotion) { |
| 197 // The test verifies that the marking state is preserved when promoting |
| 198 // a buffer to old space. |
| 199 CcTest::InitializeVM(); |
| 200 LocalContext env; |
| 201 v8::Isolate* isolate = env->GetIsolate(); |
| 202 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 203 |
| 204 JSArrayBuffer* raw_ab = nullptr; |
| 205 { |
| 206 v8::HandleScope handle_scope(isolate); |
| 207 Handle<FixedArray> root = |
| 208 heap->isolate()->factory()->NewFixedArray(1, TENURED); |
| 209 { |
| 210 v8::HandleScope handle_scope(isolate); |
| 211 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 212 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 213 root->set(0, *buf); // Buffer that should not be promoted as live. |
| 214 } |
| 215 heap::SimulateIncrementalMarking(heap, false); |
| 216 CHECK(IsTrackedInNewSpace(JSArrayBuffer::cast(root->get(0)))); |
| 217 heap::GcAndSweep(heap, NEW_SPACE); |
| 218 CHECK(IsTrackedInNewSpace(JSArrayBuffer::cast(root->get(0)))); |
| 219 heap::GcAndSweep(heap, NEW_SPACE); |
| 220 CHECK(IsTrackedInOldSpace(JSArrayBuffer::cast(root->get(0)))); |
| 221 raw_ab = JSArrayBuffer::cast(root->get(0)); |
| 222 root->set(0, heap->undefined_value()); |
| 223 heap::SimulateIncrementalMarking(heap, true); |
| 224 // Prohibit page from being released. |
| 225 Page::FromAddress(raw_ab->address())->MarkNeverEvacuate(); |
| 226 heap::GcAndSweep(heap, OLD_SPACE); |
| 227 CHECK(!IsTracked(raw_ab)); |
| 228 } |
| 229 } |
| 230 |
| 231 TEST(ArrayBuffer_LivePromotion) { |
| 232 // The test verifies that the marking state is preserved when promoting |
| 233 // a buffer to old space. |
| 234 CcTest::InitializeVM(); |
| 235 LocalContext env; |
| 236 v8::Isolate* isolate = env->GetIsolate(); |
| 237 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 238 |
| 239 JSArrayBuffer* raw_ab = nullptr; |
| 240 { |
| 241 v8::HandleScope handle_scope(isolate); |
| 242 Handle<FixedArray> root = |
| 243 heap->isolate()->factory()->NewFixedArray(1, TENURED); |
| 244 { |
| 245 v8::HandleScope handle_scope(isolate); |
| 246 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 247 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 248 root->set(0, *buf); // Buffer that should be promoted as live. |
| 249 } |
| 250 heap::SimulateIncrementalMarking(heap, true); |
| 251 CHECK(IsTrackedInNewSpace(JSArrayBuffer::cast(root->get(0)))); |
| 252 heap::GcAndSweep(heap, NEW_SPACE); |
| 253 CHECK(IsTrackedInNewSpace(JSArrayBuffer::cast(root->get(0)))); |
| 254 heap::GcAndSweep(heap, NEW_SPACE); |
| 255 CHECK(IsTrackedInOldSpace(JSArrayBuffer::cast(root->get(0)))); |
| 256 raw_ab = JSArrayBuffer::cast(root->get(0)); |
| 257 root->set(0, heap->undefined_value()); |
| 258 // Prohibit page from being released. |
| 259 Page::FromAddress(raw_ab->address())->MarkNeverEvacuate(); |
| 260 heap::GcAndSweep(heap, OLD_SPACE); |
| 261 CHECK(IsTracked(raw_ab)); |
| 262 } |
| 263 } |
| 264 |
| 265 TEST(ArrayBuffer_SemiSpaceCopyThenPagePromotion) { |
| 266 // The test verifies that the marking state is preserved across semispace |
| 267 // copy. |
| 268 CcTest::InitializeVM(); |
| 269 LocalContext env; |
| 270 v8::Isolate* isolate = env->GetIsolate(); |
| 271 Heap* heap = reinterpret_cast<Isolate*>(isolate)->heap(); |
| 272 |
| 273 heap::SealCurrentObjects(heap); |
| 274 { |
| 275 v8::HandleScope handle_scope(isolate); |
| 276 Handle<FixedArray> root = |
| 277 heap->isolate()->factory()->NewFixedArray(1, TENURED); |
| 278 { |
| 279 v8::HandleScope handle_scope(isolate); |
| 280 Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 100); |
| 281 Handle<JSArrayBuffer> buf = v8::Utils::OpenHandle(*ab); |
| 282 root->set(0, *buf); // Buffer that should be promoted as live. |
| 283 Page::FromAddress(buf->address())->MarkNeverEvacuate(); |
| 284 } |
| 285 std::vector<Handle<FixedArray>> handles; |
| 286 // Make the whole page transition from new->old, getting the buffers |
| 287 // processed in the sweeper (relying on marking information) instead of |
| 288 // processing during newspace evacuation. |
| 289 heap::FillCurrentPage(heap->new_space(), &handles); |
| 290 CHECK(IsTrackedInNewSpace(JSArrayBuffer::cast(root->get(0)))); |
| 291 heap::GcAndSweep(heap, NEW_SPACE); |
| 292 heap::SimulateIncrementalMarking(heap, true); |
| 293 heap::GcAndSweep(heap, OLD_SPACE); |
| 294 CHECK(IsTracked(JSArrayBuffer::cast(root->get(0)))); |
| 295 } |
| 296 } |
| 297 |
| 298 } // namespace internal |
| 299 } // namespace v8 |
OLD | NEW |