1 /* 2 * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #include "gc/shenandoah/shenandoahAsserts.hpp" 28 #include "gc/shenandoah/shenandoahForwarding.hpp" 29 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 30 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp" 31 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp" 32 #include "gc/shenandoah/shenandoahUtils.hpp" 33 #include "memory/resourceArea.hpp" 34 35 void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) { 36 // Be extra safe. Only access data that is guaranteed to be safe: 37 // should be in heap, in known committed region, within that region. 38 39 ShenandoahHeap* heap = ShenandoahHeap::heap(); 40 if (!heap->is_in_reserved(loc)) return; 41 42 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 43 if (r != nullptr && r->is_committed()) { 44 address start = MAX2((address) r->bottom(), (address) loc - 32); 45 address end = MIN2((address) r->end(), (address) loc + 128); 46 if (start >= end) return; 47 48 stringStream ss; 49 os::print_hex_dump(&ss, start, end, 4); 50 msg.append("\n"); 51 msg.append("Raw heap memory:\n%s", ss.freeze()); 52 } 53 } 54 55 void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) { 56 ShenandoahHeap* heap = ShenandoahHeap::heap(); 57 ShenandoahHeapRegion *r = heap->heap_region_containing(obj); 58 59 ResourceMark rm; 60 stringStream ss; 61 r->print_on(&ss); 62 63 stringStream mw_ss; 64 obj->mark().print_on(&mw_ss); 65 66 ShenandoahMarkingContext* const ctx = heap->marking_context(); 67 68 msg.append(" " PTR_FORMAT " - klass " PTR_FORMAT " %s\n", p2i(obj), p2i(obj->klass()), obj->klass()->external_name()); 69 msg.append(" %3s allocated after mark start\n", ctx->allocated_after_mark_start(obj) ? "" : "not"); 70 msg.append(" %3s after update watermark\n", cast_from_oop<HeapWord*>(obj) >= r->get_update_watermark() ? "" : "not"); 71 msg.append(" %3s marked strong\n", ctx->is_marked_strong(obj) ? "" : "not"); 72 msg.append(" %3s marked weak\n", ctx->is_marked_weak(obj) ? "" : "not"); 73 msg.append(" %3s in collection set\n", heap->in_collection_set(obj) ? "" : "not"); 74 if (heap->mode()->is_generational() && !obj->is_forwarded()) { 75 msg.append(" age: %d\n", obj->age()); 76 } 77 msg.append(" mark:%s\n", mw_ss.freeze()); 78 msg.append(" region: %s", ss.freeze()); 79 } 80 81 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) { 82 ShenandoahHeap* heap = ShenandoahHeap::heap(); 83 if (heap->is_in_reserved(loc)) { 84 msg.append(" inside Java heap\n"); 85 ShenandoahHeapRegion *r = heap->heap_region_containing(loc); 86 stringStream ss; 87 r->print_on(&ss); 88 89 msg.append(" %3s in collection set\n", heap->in_collection_set_loc(loc) ? "" : "not"); 90 msg.append(" region: %s", ss.freeze()); 91 } else { 92 msg.append(" outside of Java heap\n"); 93 stringStream ss; 94 os::print_location(&ss, (intptr_t) loc, false); 95 msg.append(" %s", ss.freeze()); 96 } 97 } 98 99 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) { 100 ShenandoahHeap* heap = ShenandoahHeap::heap(); 101 msg.append(" " PTR_FORMAT " - safe print, no details\n", p2i(loc)); 102 if (heap->is_in_reserved(loc)) { 103 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 104 if (r != nullptr) { 105 stringStream ss; 106 r->print_on(&ss); 107 msg.append(" region: %s", ss.freeze()); 108 print_raw_memory(msg, loc); 109 } 110 } 111 } 112 113 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc, 114 const char* phase, const char* label, 115 const char* file, int line) { 116 ShenandoahHeap* heap = ShenandoahHeap::heap(); 117 ResourceMark rm; 118 119 bool loc_in_heap = (loc != nullptr && heap->is_in_reserved(loc)); 120 121 ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label); 122 123 msg.append("Referenced from:\n"); 124 if (interior_loc != nullptr) { 125 msg.append(" interior location: " PTR_FORMAT "\n", p2i(interior_loc)); 126 if (loc_in_heap) { 127 print_obj(msg, loc); 128 } else { 129 print_non_obj(msg, interior_loc); 130 } 131 } else { 132 msg.append(" no interior location recorded (probably a plain heap scan, or detached oop)\n"); 133 } 134 msg.append("\n"); 135 136 msg.append("Object:\n"); 137 if (level >= _safe_oop) { 138 print_obj(msg, obj); 139 } else { 140 print_obj_safe(msg, obj); 141 } 142 msg.append("\n"); 143 144 if (level >= _safe_oop) { 145 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 146 msg.append("Forwardee:\n"); 147 if (obj != fwd) { 148 if (level >= _safe_oop_fwd) { 149 print_obj(msg, fwd); 150 } else { 151 print_obj_safe(msg, fwd); 152 } 153 } else { 154 msg.append(" (the object itself)"); 155 } 156 msg.append("\n"); 157 } 158 159 if (level >= _safe_oop_fwd) { 160 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 161 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 162 if (fwd != fwd2) { 163 msg.append("Second forwardee:\n"); 164 print_obj_safe(msg, fwd2); 165 msg.append("\n"); 166 } 167 } 168 169 report_vm_error(file, line, msg.buffer()); 170 } 171 172 void ShenandoahAsserts::assert_in_heap_bounds(void* interior_loc, oop obj, const char *file, int line) { 173 ShenandoahHeap* heap = ShenandoahHeap::heap(); 174 175 if (!heap->is_in_reserved(obj)) { 176 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds failed", 177 "oop must be in heap bounds", 178 file, line); 179 } 180 } 181 182 void ShenandoahAsserts::assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char *file, int line) { 183 ShenandoahHeap* heap = ShenandoahHeap::heap(); 184 185 if (obj != nullptr && !heap->is_in_reserved(obj)) { 186 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds_or_null failed", 187 "oop must be in heap bounds", 188 file, line); 189 } 190 } 191 192 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) { 193 ShenandoahHeap* heap = ShenandoahHeap::heap(); 194 195 // Step 1. Check that obj is correct. 196 // After this step, it is safe to call heap_region_containing(). 197 if (!heap->is_in_reserved(obj)) { 198 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 199 "oop must be in heap bounds", 200 file, line); 201 } 202 203 Klass* obj_klass = ShenandoahForwarding::klass(obj); 204 if (obj_klass == nullptr) { 205 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 206 "Object klass pointer should not be null", 207 file,line); 208 } 209 210 if (!Metaspace::contains(obj_klass)) { 211 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 212 "Object klass pointer must go to metaspace", 213 file,line); 214 } 215 216 if (!heap->is_in(obj)) { 217 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 218 "Object should be in active region area", 219 file, line); 220 } 221 222 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 223 224 if (obj != fwd) { 225 // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something 226 // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr 227 // that still points to the object itself. 228 if (heap->is_full_gc_move_in_progress()) { 229 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 230 "Non-trivial forwarding pointer during Full GC moves, probable bug.", 231 file, line); 232 } 233 234 // Step 2. Check that forwardee is correct 235 if (!heap->is_in_reserved(fwd)) { 236 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 237 "Forwardee must be in heap bounds", 238 file, line); 239 } 240 241 if (obj_klass != ShenandoahForwarding::klass(fwd)) { 242 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 243 "Forwardee klass disagrees with object class", 244 file, line); 245 } 246 247 // Step 3. Check that forwardee points to correct region 248 if (!heap->is_in(fwd)) { 249 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 250 "Forwardee should be in active region area", 251 file, line); 252 } 253 254 if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) { 255 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 256 "Non-trivial forwardee should be in another region", 257 file, line); 258 } 259 260 // Step 4. Check for multiple forwardings 261 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 262 if (fwd != fwd2) { 263 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 264 "Multiple forwardings", 265 file, line); 266 } 267 } 268 269 // Do additional checks for special objects: their fields can hold metadata as well. 270 // We want to check class loading/unloading did not corrupt them. 271 272 if (Universe::is_fully_initialized() && (obj_klass == vmClasses::Class_klass())) { 273 Metadata* klass = obj->metadata_field(java_lang_Class::klass_offset()); 274 if (klass != nullptr && !Metaspace::contains(klass)) { 275 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 276 "Instance class mirror should point to Metaspace", 277 file, line); 278 } 279 280 Metadata* array_klass = obj->metadata_field(java_lang_Class::array_klass_offset()); 281 if (array_klass != nullptr && !Metaspace::contains(array_klass)) { 282 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 283 "Array class mirror should point to Metaspace", 284 file, line); 285 } 286 } 287 } 288 289 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) { 290 assert_correct(interior_loc, obj, file, line); 291 292 ShenandoahHeap* heap = ShenandoahHeap::heap(); 293 ShenandoahHeapRegion* r = heap->heap_region_containing(obj); 294 if (!r->is_active()) { 295 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 296 "Object must reside in active region", 297 file, line); 298 } 299 300 size_t alloc_size = obj->size(); 301 HeapWord* obj_end = cast_from_oop<HeapWord*>(obj) + alloc_size; 302 303 if (ShenandoahHeapRegion::requires_humongous(alloc_size)) { 304 size_t idx = r->index(); 305 size_t end_idx = heap->heap_region_index_containing(obj_end - 1); 306 for (size_t i = idx; i < end_idx; i++) { 307 ShenandoahHeapRegion* chain_reg = heap->get_region(i); 308 if (i == idx && !chain_reg->is_humongous_start()) { 309 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 310 "Object must reside in humongous start", 311 file, line); 312 } 313 if (i != idx && !chain_reg->is_humongous_continuation()) { 314 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 315 "Humongous continuation should be of proper size", 316 file, line); 317 } 318 } 319 } else { 320 if (obj_end > r->top()) { 321 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 322 "Object end should be within the active area of the region", 323 file, line); 324 } 325 } 326 } 327 328 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) { 329 assert_correct(interior_loc, obj, file, line); 330 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 331 332 if (obj == fwd) { 333 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed", 334 "Object should be forwarded", 335 file, line); 336 } 337 } 338 339 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) { 340 assert_correct(interior_loc, obj, file, line); 341 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 342 343 if (obj != fwd) { 344 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed", 345 "Object should not be forwarded", 346 file, line); 347 } 348 } 349 350 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) { 351 assert_correct(interior_loc, obj, file, line); 352 353 ShenandoahHeap* heap = ShenandoahHeap::heap(); 354 if (!heap->marking_context()->is_marked(obj)) { 355 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed", 356 "Object should be marked", 357 file, line); 358 } 359 } 360 361 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) { 362 assert_correct(interior_loc, obj, file, line); 363 364 ShenandoahHeap* heap = ShenandoahHeap::heap(); 365 if (!heap->marking_context()->is_marked_weak(obj)) { 366 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed", 367 "Object should be marked weakly", 368 file, line); 369 } 370 } 371 372 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) { 373 assert_correct(interior_loc, obj, file, line); 374 375 ShenandoahHeap* heap = ShenandoahHeap::heap(); 376 if (!heap->marking_context()->is_marked_strong(obj)) { 377 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed", 378 "Object should be marked strongly", 379 file, line); 380 } 381 } 382 383 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) { 384 assert_correct(interior_loc, obj, file, line); 385 386 ShenandoahHeap* heap = ShenandoahHeap::heap(); 387 if (!heap->in_collection_set(obj)) { 388 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed", 389 "Object should be in collection set", 390 file, line); 391 } 392 } 393 394 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) { 395 assert_correct(interior_loc, obj, file, line); 396 397 ShenandoahHeap* heap = ShenandoahHeap::heap(); 398 if (heap->in_collection_set(obj)) { 399 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed", 400 "Object should not be in collection set", 401 file, line); 402 } 403 } 404 405 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) { 406 ShenandoahHeap* heap = ShenandoahHeap::heap(); 407 if (heap->in_collection_set_loc(interior_loc)) { 408 print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed", 409 "Interior location should not be in collection set", 410 file, line); 411 } 412 } 413 414 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual, 415 const char *file, int line) { 416 ShenandoahMessageBuffer msg("%s\n", label); 417 msg.append(" Actual: " PTR_FORMAT "\n", p2i(actual)); 418 report_vm_error(file, line, msg.buffer()); 419 } 420 421 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) { 422 if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) { 423 return; 424 } 425 426 if (lock->owned_by_self()) { 427 return; 428 } 429 430 ShenandoahMessageBuffer msg("Must be at a Shenandoah safepoint or held %s lock", lock->name()); 431 report_vm_error(file, line, msg.buffer()); 432 } 433 434 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) { 435 ShenandoahHeap* heap = ShenandoahHeap::heap(); 436 437 if (heap->lock()->owned_by_self()) { 438 return; 439 } 440 441 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread"); 442 report_vm_error(file, line, msg.buffer()); 443 } 444 445 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) { 446 ShenandoahHeap* heap = ShenandoahHeap::heap(); 447 448 if (!heap->lock()->owned_by_self()) { 449 return; 450 } 451 452 ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread"); 453 report_vm_error(file, line, msg.buffer()); 454 } 455 456 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) { 457 ShenandoahHeap* heap = ShenandoahHeap::heap(); 458 459 if (heap->lock()->owned_by_self()) { 460 return; 461 } 462 463 if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) { 464 return; 465 } 466 467 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint"); 468 report_vm_error(file, line, msg.buffer()); 469 } 470 471 void ShenandoahAsserts::assert_generational(const char* file, int line) { 472 if (ShenandoahHeap::heap()->mode()->is_generational()) { 473 return; 474 } 475 476 ShenandoahMessageBuffer msg("Must be in generational mode"); 477 report_vm_error(file, line, msg.buffer()); 478 } 479 480 void ShenandoahAsserts::assert_control_or_vm_thread_at_safepoint(bool at_safepoint, const char* file, int line) { 481 Thread* thr = Thread::current(); 482 if (thr == ShenandoahHeap::heap()->control_thread()) { 483 return; 484 } 485 if (thr->is_VM_thread()) { 486 if (!at_safepoint) { 487 return; 488 } else if (SafepointSynchronize::is_at_safepoint()) { 489 return; 490 } 491 } 492 493 ShenandoahMessageBuffer msg("Must be either control thread, or vm thread"); 494 if (at_safepoint) { 495 msg.append(" at a safepoint"); 496 } 497 report_vm_error(file, line, msg.buffer()); 498 } 499 500 void ShenandoahAsserts::assert_generations_reconciled(const char* file, int line) { 501 if (!SafepointSynchronize::is_at_safepoint()) { 502 return; 503 } 504 505 ShenandoahHeap* heap = ShenandoahHeap::heap(); 506 ShenandoahGeneration* ggen = heap->gc_generation(); 507 ShenandoahGeneration* agen = heap->active_generation(); 508 if (agen == ggen) { 509 return; 510 } 511 512 ShenandoahMessageBuffer msg("Active(%d) & GC(%d) Generations aren't reconciled", agen->type(), ggen->type()); 513 report_vm_error(file, line, msg.buffer()); 514 }