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(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(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(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(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(void* interior_loc, oop obj, const char *file, int line) { 173 ShenandoahHeap* heap = ShenandoahHeap::heap(); 174 175 if (!heap->is_in(obj)) { 176 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap failed", 177 "oop must point to a heap address", 178 file, line); 179 } 180 } 181 182 void ShenandoahAsserts::assert_in_heap_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(obj)) { 186 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_or_null failed", 187 "oop must point to a heap address", 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(obj)) { 198 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 199 "oop must point to a heap address", 200 file, line); 201 } 202 203 Klass* obj_klass = obj->klass_or_null(); 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 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 217 218 if (obj != fwd) { 219 // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something 220 // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr 221 // that still points to the object itself. 222 if (heap->is_full_gc_move_in_progress()) { 223 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 224 "Non-trivial forwarding pointer during Full GC moves, probable bug.", 225 file, line); 226 } 227 228 // Step 2. Check that forwardee is correct 229 if (!heap->is_in(fwd)) { 230 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 231 "Forwardee must point to a heap address", 232 file, line); 233 } 234 235 if (obj_klass != fwd->klass()) { 236 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 237 "Forwardee klass disagrees with object class", 238 file, line); 239 } 240 241 // Step 3. Check that forwardee points to correct region 242 if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) { 243 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 244 "Non-trivial forwardee should in another region", 245 file, line); 246 } 247 248 // Step 4. Check for multiple forwardings 249 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 250 if (fwd != fwd2) { 251 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 252 "Multiple forwardings", 253 file, line); 254 } 255 } 256 } 257 258 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) { 259 assert_correct(interior_loc, obj, file, line); 260 261 ShenandoahHeap* heap = ShenandoahHeap::heap(); 262 ShenandoahHeapRegion* r = heap->heap_region_containing(obj); 263 if (!r->is_active()) { 264 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 265 "Object must reside in active region", 266 file, line); 267 } 268 269 size_t alloc_size = obj->size(); 270 if (alloc_size > ShenandoahHeapRegion::humongous_threshold_words()) { 271 size_t idx = r->index(); 272 size_t num_regions = ShenandoahHeapRegion::required_regions(alloc_size * HeapWordSize); 273 for (size_t i = idx; i < idx + num_regions; i++) { 274 ShenandoahHeapRegion* chain_reg = heap->get_region(i); 275 if (i == idx && !chain_reg->is_humongous_start()) { 276 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 277 "Object must reside in humongous start", 278 file, line); 279 } 280 if (i != idx && !chain_reg->is_humongous_continuation()) { 281 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 282 "Humongous continuation should be of proper size", 283 file, line); 284 } 285 } 286 } 287 } 288 289 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) { 290 assert_correct(interior_loc, obj, file, line); 291 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 292 293 if (obj == fwd) { 294 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed", 295 "Object should be forwarded", 296 file, line); 297 } 298 } 299 300 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) { 301 assert_correct(interior_loc, obj, file, line); 302 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 303 304 if (obj != fwd) { 305 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed", 306 "Object should not be forwarded", 307 file, line); 308 } 309 } 310 311 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) { 312 assert_correct(interior_loc, obj, file, line); 313 314 ShenandoahHeap* heap = ShenandoahHeap::heap(); 315 if (!heap->marking_context()->is_marked(obj)) { 316 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed", 317 "Object should be marked", 318 file, line); 319 } 320 } 321 322 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) { 323 assert_correct(interior_loc, obj, file, line); 324 325 ShenandoahHeap* heap = ShenandoahHeap::heap(); 326 if (!heap->marking_context()->is_marked_weak(obj)) { 327 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed", 328 "Object should be marked weakly", 329 file, line); 330 } 331 } 332 333 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) { 334 assert_correct(interior_loc, obj, file, line); 335 336 ShenandoahHeap* heap = ShenandoahHeap::heap(); 337 if (!heap->marking_context()->is_marked_strong(obj)) { 338 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed", 339 "Object should be marked strongly", 340 file, line); 341 } 342 } 343 344 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) { 345 assert_correct(interior_loc, obj, file, line); 346 347 ShenandoahHeap* heap = ShenandoahHeap::heap(); 348 if (!heap->in_collection_set(obj)) { 349 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed", 350 "Object should be in collection set", 351 file, line); 352 } 353 } 354 355 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) { 356 assert_correct(interior_loc, obj, file, line); 357 358 ShenandoahHeap* heap = ShenandoahHeap::heap(); 359 if (heap->in_collection_set(obj)) { 360 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed", 361 "Object should not be in collection set", 362 file, line); 363 } 364 } 365 366 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) { 367 ShenandoahHeap* heap = ShenandoahHeap::heap(); 368 if (heap->in_collection_set_loc(interior_loc)) { 369 print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed", 370 "Interior location should not be in collection set", 371 file, line); 372 } 373 } 374 375 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual, 376 const char *file, int line) { 377 ShenandoahMessageBuffer msg("%s\n", label); 378 msg.append(" Actual: " PTR_FORMAT "\n", p2i(actual)); 379 report_vm_error(file, line, msg.buffer()); 380 } 381 382 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) { 383 if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) { 384 return; 385 } 386 387 if (lock->owned_by_self()) { 388 return; 389 } 390 391 ShenandoahMessageBuffer msg("Must be at a Shenandoah safepoint or held %s lock", lock->name()); 392 report_vm_error(file, line, msg.buffer()); 393 } 394 395 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) { 396 ShenandoahHeap* heap = ShenandoahHeap::heap(); 397 398 if (heap->lock()->owned_by_self()) { 399 return; 400 } 401 402 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread"); 403 report_vm_error(file, line, msg.buffer()); 404 } 405 406 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) { 407 ShenandoahHeap* heap = ShenandoahHeap::heap(); 408 409 if (!heap->lock()->owned_by_self()) { 410 return; 411 } 412 413 ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread"); 414 report_vm_error(file, line, msg.buffer()); 415 } 416 417 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) { 418 ShenandoahHeap* heap = ShenandoahHeap::heap(); 419 420 if (heap->lock()->owned_by_self()) { 421 return; 422 } 423 424 if (ShenandoahSafepoint::is_at_shenandoah_safepoint() && Thread::current()->is_VM_thread()) { 425 return; 426 } 427 428 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint"); 429 report_vm_error(file, line, msg.buffer()); 430 } 431 432 // Unlike assert_heaplocked_or_safepoint(), this does not require current thread in safepoint to be a VM thread 433 // TODO: This should be more aptly named. Nothing in this method checks we are actually in Full GC. 434 void ShenandoahAsserts::assert_heaplocked_or_fullgc_safepoint(const char* file, int line) { 435 ShenandoahHeap* heap = ShenandoahHeap::heap(); 436 437 if (heap->lock()->owned_by_self()) { 438 return; 439 } 440 441 if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) { 442 return; 443 } 444 445 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint"); 446 report_vm_error(file, line, msg.buffer()); 447 } --- EOF ---