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/shenandoahObjectUtils.inline.hpp" 33 #include "gc/shenandoah/shenandoahUtils.hpp" 34 #include "memory/resourceArea.hpp" 35 36 void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) { 37 // Be extra safe. Only access data that is guaranteed to be safe: 38 // should be in heap, in known committed region, within that region. 39 40 ShenandoahHeap* heap = ShenandoahHeap::heap(); 41 if (!heap->is_in(loc)) return; 42 43 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 44 if (r != nullptr && r->is_committed()) { 45 address start = MAX2((address) r->bottom(), (address) loc - 32); 46 address end = MIN2((address) r->end(), (address) loc + 128); 47 if (start >= end) return; 48 49 stringStream ss; 50 os::print_hex_dump(&ss, start, end, 4); 51 msg.append("\n"); 52 msg.append("Raw heap memory:\n%s", ss.freeze()); 53 } 54 } 55 56 void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) { 57 ShenandoahHeap* heap = ShenandoahHeap::heap(); 58 ShenandoahHeapRegion *r = heap->heap_region_containing(obj); 59 60 ResourceMark rm; 61 stringStream ss; 62 r->print_on(&ss); 63 64 stringStream mw_ss; 65 obj->mark().print_on(&mw_ss); 66 67 ShenandoahMarkingContext* const ctx = heap->marking_context(); 68 69 msg.append(" " PTR_FORMAT " - klass " PTR_FORMAT " %s\n", p2i(obj), p2i(obj->klass()), obj->klass()->external_name()); 70 msg.append(" %3s allocated after mark start\n", ctx->allocated_after_mark_start(obj) ? "" : "not"); 71 msg.append(" %3s after update watermark\n", cast_from_oop<HeapWord*>(obj) >= r->get_update_watermark() ? "" : "not"); 72 msg.append(" %3s marked strong\n", ctx->is_marked_strong(obj) ? "" : "not"); 73 msg.append(" %3s marked weak\n", ctx->is_marked_weak(obj) ? "" : "not"); 74 msg.append(" %3s in collection set\n", heap->in_collection_set(obj) ? "" : "not"); 75 msg.append(" mark:%s\n", mw_ss.freeze()); 76 msg.append(" region: %s", ss.freeze()); 77 } 78 79 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) { 80 ShenandoahHeap* heap = ShenandoahHeap::heap(); 81 if (heap->is_in(loc)) { 82 msg.append(" inside Java heap\n"); 83 ShenandoahHeapRegion *r = heap->heap_region_containing(loc); 84 stringStream ss; 85 r->print_on(&ss); 86 87 msg.append(" %3s in collection set\n", heap->in_collection_set_loc(loc) ? "" : "not"); 88 msg.append(" region: %s", ss.freeze()); 89 } else { 90 msg.append(" outside of Java heap\n"); 91 stringStream ss; 92 os::print_location(&ss, (intptr_t) loc, false); 93 msg.append(" %s", ss.freeze()); 94 } 95 } 96 97 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) { 98 ShenandoahHeap* heap = ShenandoahHeap::heap(); 99 msg.append(" " PTR_FORMAT " - safe print, no details\n", p2i(loc)); 100 if (heap->is_in(loc)) { 101 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 102 if (r != nullptr) { 103 stringStream ss; 104 r->print_on(&ss); 105 msg.append(" region: %s", ss.freeze()); 106 print_raw_memory(msg, loc); 107 } 108 } 109 } 110 111 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc, 112 const char* phase, const char* label, 113 const char* file, int line) { 114 ShenandoahHeap* heap = ShenandoahHeap::heap(); 115 ResourceMark rm; 116 117 bool loc_in_heap = (loc != nullptr && heap->is_in(loc)); 118 119 ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label); 120 121 msg.append("Referenced from:\n"); 122 if (interior_loc != nullptr) { 123 msg.append(" interior location: " PTR_FORMAT "\n", p2i(interior_loc)); 124 if (loc_in_heap) { 125 print_obj(msg, loc); 126 } else { 127 print_non_obj(msg, interior_loc); 128 } 129 } else { 130 msg.append(" no interior location recorded (probably a plain heap scan, or detached oop)\n"); 131 } 132 msg.append("\n"); 133 134 msg.append("Object:\n"); 135 if (level >= _safe_oop) { 136 print_obj(msg, obj); 137 } else { 138 print_obj_safe(msg, obj); 139 } 140 msg.append("\n"); 141 142 if (level >= _safe_oop) { 143 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 144 msg.append("Forwardee:\n"); 145 if (obj != fwd) { 146 if (level >= _safe_oop_fwd) { 147 print_obj(msg, fwd); 148 } else { 149 print_obj_safe(msg, fwd); 150 } 151 } else { 152 msg.append(" (the object itself)"); 153 } 154 msg.append("\n"); 155 } 156 157 if (level >= _safe_oop_fwd) { 158 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 159 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 160 if (fwd != fwd2) { 161 msg.append("Second forwardee:\n"); 162 print_obj_safe(msg, fwd2); 163 msg.append("\n"); 164 } 165 } 166 167 report_vm_error(file, line, msg.buffer()); 168 } 169 170 void ShenandoahAsserts::assert_in_heap(void* interior_loc, oop obj, const char *file, int line) { 171 ShenandoahHeap* heap = ShenandoahHeap::heap(); 172 173 if (!heap->is_in(obj)) { 174 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap failed", 175 "oop must point to a heap address", 176 file, line); 177 } 178 } 179 180 void ShenandoahAsserts::assert_in_heap_or_null(void* interior_loc, oop obj, const char *file, int line) { 181 ShenandoahHeap* heap = ShenandoahHeap::heap(); 182 183 if (obj != nullptr && !heap->is_in(obj)) { 184 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_or_null failed", 185 "oop must point to a heap address", 186 file, line); 187 } 188 } 189 190 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) { 191 ShenandoahHeap* heap = ShenandoahHeap::heap(); 192 193 // Step 1. Check that obj is correct. 194 // After this step, it is safe to call heap_region_containing(). 195 if (!heap->is_in(obj)) { 196 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 197 "oop must point to a heap address", 198 file, line); 199 } 200 201 Klass* obj_klass = ShenandoahObjectUtils::klass(obj); 202 if (obj_klass == nullptr) { 203 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 204 "Object klass pointer should not be null", 205 file,line); 206 } 207 208 if (!Metaspace::contains(obj_klass)) { 209 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 210 "Object klass pointer must go to metaspace", 211 file,line); 212 } 213 214 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 215 216 if (obj != fwd) { 217 // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something 218 // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr 219 // that still points to the object itself. 220 if (heap->is_full_gc_move_in_progress()) { 221 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 222 "Non-trivial forwarding pointer during Full GC moves, probable bug.", 223 file, line); 224 } 225 226 // Step 2. Check that forwardee is correct 227 if (!heap->is_in(fwd)) { 228 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 229 "Forwardee must point to a heap address", 230 file, line); 231 } 232 233 if (obj_klass != ShenandoahObjectUtils::klass(fwd)) { 234 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 235 "Forwardee klass disagrees with object class", 236 file, line); 237 } 238 239 // Step 3. Check that forwardee points to correct region 240 if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) { 241 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 242 "Non-trivial forwardee should in another region", 243 file, line); 244 } 245 246 // Step 4. Check for multiple forwardings 247 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 248 if (fwd != fwd2) { 249 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed", 250 "Multiple forwardings", 251 file, line); 252 } 253 } 254 } 255 256 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) { 257 assert_correct(interior_loc, obj, file, line); 258 259 ShenandoahHeap* heap = ShenandoahHeap::heap(); 260 ShenandoahHeapRegion* r = heap->heap_region_containing(obj); 261 if (!r->is_active()) { 262 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 263 "Object must reside in active region", 264 file, line); 265 } 266 267 size_t alloc_size = obj->size(); 268 if (alloc_size > ShenandoahHeapRegion::humongous_threshold_words()) { 269 size_t idx = r->index(); 270 size_t num_regions = ShenandoahHeapRegion::required_regions(alloc_size * HeapWordSize); 271 for (size_t i = idx; i < idx + num_regions; i++) { 272 ShenandoahHeapRegion* chain_reg = heap->get_region(i); 273 if (i == idx && !chain_reg->is_humongous_start()) { 274 print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 275 "Object must reside in humongous start", 276 file, line); 277 } 278 if (i != idx && !chain_reg->is_humongous_continuation()) { 279 print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed", 280 "Humongous continuation should be of proper size", 281 file, line); 282 } 283 } 284 } 285 } 286 287 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) { 288 assert_correct(interior_loc, obj, file, line); 289 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 290 291 if (obj == fwd) { 292 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed", 293 "Object should be forwarded", 294 file, line); 295 } 296 } 297 298 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) { 299 assert_correct(interior_loc, obj, file, line); 300 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 301 302 if (obj != fwd) { 303 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed", 304 "Object should not be forwarded", 305 file, line); 306 } 307 } 308 309 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) { 310 assert_correct(interior_loc, obj, file, line); 311 312 ShenandoahHeap* heap = ShenandoahHeap::heap(); 313 if (!heap->marking_context()->is_marked(obj)) { 314 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed", 315 "Object should be marked", 316 file, line); 317 } 318 } 319 320 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) { 321 assert_correct(interior_loc, obj, file, line); 322 323 ShenandoahHeap* heap = ShenandoahHeap::heap(); 324 if (!heap->marking_context()->is_marked_weak(obj)) { 325 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed", 326 "Object should be marked weakly", 327 file, line); 328 } 329 } 330 331 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) { 332 assert_correct(interior_loc, obj, file, line); 333 334 ShenandoahHeap* heap = ShenandoahHeap::heap(); 335 if (!heap->marking_context()->is_marked_strong(obj)) { 336 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed", 337 "Object should be marked strongly", 338 file, line); 339 } 340 } 341 342 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) { 343 assert_correct(interior_loc, obj, file, line); 344 345 ShenandoahHeap* heap = ShenandoahHeap::heap(); 346 if (!heap->in_collection_set(obj)) { 347 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed", 348 "Object should be in collection set", 349 file, line); 350 } 351 } 352 353 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) { 354 assert_correct(interior_loc, obj, file, line); 355 356 ShenandoahHeap* heap = ShenandoahHeap::heap(); 357 if (heap->in_collection_set(obj)) { 358 print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed", 359 "Object should not be in collection set", 360 file, line); 361 } 362 } 363 364 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) { 365 ShenandoahHeap* heap = ShenandoahHeap::heap(); 366 if (heap->in_collection_set_loc(interior_loc)) { 367 print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed", 368 "Interior location should not be in collection set", 369 file, line); 370 } 371 } 372 373 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual, 374 const char *file, int line) { 375 ShenandoahMessageBuffer msg("%s\n", label); 376 msg.append(" Actual: " PTR_FORMAT "\n", p2i(actual)); 377 report_vm_error(file, line, msg.buffer()); 378 } 379 380 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) { 381 if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) { 382 return; 383 } 384 385 if (lock->owned_by_self()) { 386 return; 387 } 388 389 ShenandoahMessageBuffer msg("Must ba at a Shenandoah safepoint or held %s lock", lock->name()); 390 report_vm_error(file, line, msg.buffer()); 391 } 392 393 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) { 394 ShenandoahHeap* heap = ShenandoahHeap::heap(); 395 396 if (heap->lock()->owned_by_self()) { 397 return; 398 } 399 400 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread"); 401 report_vm_error(file, line, msg.buffer()); 402 } 403 404 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) { 405 ShenandoahHeap* heap = ShenandoahHeap::heap(); 406 407 if (!heap->lock()->owned_by_self()) { 408 return; 409 } 410 411 ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread"); 412 report_vm_error(file, line, msg.buffer()); 413 } 414 415 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) { 416 ShenandoahHeap* heap = ShenandoahHeap::heap(); 417 418 if (heap->lock()->owned_by_self()) { 419 return; 420 } 421 422 if (ShenandoahSafepoint::is_at_shenandoah_safepoint() && Thread::current()->is_VM_thread()) { 423 return; 424 } 425 426 ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint"); 427 report_vm_error(file, line, msg.buffer()); 428 }