1 /* 2 * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "gc_implementation/shenandoah/shenandoahAsserts.hpp" 27 #include "gc_implementation/shenandoah/shenandoahForwarding.hpp" 28 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 29 #include "gc_implementation/shenandoah/shenandoahMarkingContext.inline.hpp" 30 #include "gc_implementation/shenandoah/shenandoahUtils.hpp" 31 #include "memory/resourceArea.hpp" 32 33 void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) { 34 // Be extra safe. Only access data that is guaranteed to be safe: 35 // should be in heap, in known committed region, within that region. 36 37 ShenandoahHeap* heap = ShenandoahHeap::heap(); 38 if (!heap->is_in(loc)) return; 39 40 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 41 if (r != NULL && r->is_committed()) { 42 address start = MAX2((address) r->bottom(), (address) loc - 32); 43 address end = MIN2((address) r->end(), (address) loc + 128); 44 if (start >= end) return; 45 46 stringStream ss; 47 os::print_hex_dump(&ss, start, end, 4); 48 msg.append("\n"); 49 msg.append("Raw heap memory:\n%s", ss.as_string()); 50 } 51 } 52 53 void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) { 54 ShenandoahHeap* heap = ShenandoahHeap::heap(); 55 ShenandoahHeapRegion *r = heap->heap_region_containing(obj); 56 57 ResourceMark rm; 58 stringStream ss; 59 r->print_on(&ss); 60 61 stringStream mw_ss; 62 obj->mark()->print_on(&mw_ss); 63 64 ShenandoahMarkingContext* const ctx = heap->marking_context(); 65 66 msg.append(" " PTR_FORMAT " - klass " PTR_FORMAT " %s\n", p2i(obj), p2i(obj->klass()), obj->klass()->external_name()); 67 msg.append(" %3s allocated after mark start\n", ctx->allocated_after_mark_start((HeapWord *) obj) ? "" : "not"); 68 msg.append(" %3s marked \n", ctx->is_marked(obj) ? "" : "not"); 69 msg.append(" %3s in collection set\n", heap->in_collection_set(obj) ? "" : "not"); 70 msg.append(" mark:%s\n", mw_ss.as_string()); 71 msg.append(" region: %s", ss.as_string()); 72 } 73 74 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) { 75 ShenandoahHeap* heap = ShenandoahHeap::heap(); 76 if (heap->is_in(loc)) { 77 msg.append(" inside Java heap\n"); 78 ShenandoahHeapRegion *r = heap->heap_region_containing(loc); 79 stringStream ss; 80 r->print_on(&ss); 81 82 msg.append(" %3s in collection set\n", heap->in_collection_set(loc) ? "" : "not"); 83 msg.append(" region: %s", ss.as_string()); 84 } else { 85 msg.append(" outside of Java heap\n"); 86 stringStream ss; 87 os::print_location(&ss, (intptr_t) loc, false); 88 msg.append(" %s", ss.as_string()); 89 } 90 } 91 92 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) { 93 ShenandoahHeap* heap = ShenandoahHeap::heap(); 94 msg.append(" " PTR_FORMAT " - safe print, no details\n", p2i(loc)); 95 if (heap->is_in(loc)) { 96 ShenandoahHeapRegion* r = heap->heap_region_containing(loc); 97 if (r != NULL) { 98 stringStream ss; 99 r->print_on(&ss); 100 msg.append(" region: %s", ss.as_string()); 101 print_raw_memory(msg, loc); 102 } 103 } 104 } 105 106 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc, 107 const char* phase, const char* label, 108 const char* file, int line) { 109 ShenandoahHeap* heap = ShenandoahHeap::heap(); 110 ResourceMark rm; 111 112 bool loc_in_heap = (loc != NULL && heap->is_in(loc)); 113 114 ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label); 115 116 msg.append("Referenced from:\n"); 117 if (interior_loc != NULL) { 118 msg.append(" interior location: " PTR_FORMAT "\n", p2i(interior_loc)); 119 if (loc_in_heap) { 120 print_obj(msg, loc); 121 } else { 122 print_non_obj(msg, interior_loc); 123 } 124 } else { 125 msg.append(" no interior location recorded (probably a plain heap scan, or detached oop)\n"); 126 } 127 msg.append("\n"); 128 129 msg.append("Object:\n"); 130 if (level >= _safe_oop) { 131 print_obj(msg, obj); 132 } else { 133 print_obj_safe(msg, obj); 134 } 135 msg.append("\n"); 136 137 if (level >= _safe_oop) { 138 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 139 msg.append("Forwardee:\n"); 140 if (obj != fwd) { 141 if (level >= _safe_oop_fwd) { 142 print_obj(msg, fwd); 143 } else { 144 print_obj_safe(msg, fwd); 145 } 146 } else { 147 msg.append(" (the object itself)"); 148 } 149 msg.append("\n"); 150 } 151 152 if (level >= _safe_oop_fwd) { 153 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 154 oop fwd2 = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 155 if (fwd != fwd2) { 156 msg.append("Second forwardee:\n"); 157 print_obj_safe(msg, fwd2); 158 msg.append("\n"); 159 } 160 } 161 162 report_vm_error(file, line, msg.buffer()); 163 } 164 165 void ShenandoahAsserts::assert_in_heap(void* interior_loc, oop obj, const char *file, int line) { 166 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 167 168 if (!heap->is_in(obj)) { 169 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_heap failed", 170 "oop must point to a heap address", 171 file, line); 172 } 173 } 174 175 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) { 176 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 177 178 // Step 1. Check that obj is correct. 179 // After this step, it is safe to call heap_region_containing(). 180 if (!heap->is_in(obj)) { 181 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 182 "oop must point to a heap address", 183 file, line); 184 } 185 186 Klass* obj_klass = obj->klass_or_null(); 187 if (obj_klass == NULL) { 188 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 189 "Object klass pointer should not be NULL", 190 file,line); 191 } 192 193 if (!Metaspace::contains(obj_klass)) { 194 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 195 "Object klass pointer must go to metaspace", 196 file,line); 197 } 198 199 oop fwd = oop(ShenandoahForwarding::get_forwardee_raw_unchecked(obj)); 200 201 if (obj != fwd) { 202 // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something 203 // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr 204 // that still points to the object itself. 205 if (heap->is_full_gc_move_in_progress()) { 206 print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 207 "Non-trivial forwarding pointer during Full GC moves, probable bug.", 208 file, line); 209 } 210 211 // Step 2. Check that forwardee is correct 212 if (!heap->is_in(fwd)) { 213 print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 214 "Forwardee must point to a heap address", 215 file, line); 216 } 217 218 if (obj_klass != fwd->klass()) { 219 print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 220 "Forwardee klass disagrees with object class", 221 file, line); 222 } 223 224 // Step 3. Check that forwardee points to correct region 225 if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) { 226 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 227 "Non-trivial forwardee should in another region", 228 file, line); 229 } 230 231 // Step 4. Check for multiple forwardings 232 oop fwd2 = oop(ShenandoahForwarding::get_forwardee_raw_unchecked(fwd)); 233 if (fwd != fwd2) { 234 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_correct failed", 235 "Multiple forwardings", 236 file, line); 237 } 238 } 239 } 240 241 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) { 242 assert_correct(interior_loc, obj, file, line); 243 244 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 245 ShenandoahHeapRegion* r = heap->heap_region_containing(obj); 246 if (!r->is_active()) { 247 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed", 248 "Object must reside in active region", 249 file, line); 250 } 251 252 size_t alloc_size = obj->size(); 253 if (alloc_size > ShenandoahHeapRegion::humongous_threshold_words()) { 254 size_t idx = r->region_number(); 255 size_t num_regions = ShenandoahHeapRegion::required_regions(alloc_size * HeapWordSize); 256 for (size_t i = idx; i < idx + num_regions; i++) { 257 ShenandoahHeapRegion* chain_reg = heap->get_region(i); 258 if (i == idx && !chain_reg->is_humongous_start()) { 259 print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed", 260 "Object must reside in humongous start", 261 file, line); 262 } 263 if (i != idx && !chain_reg->is_humongous_continuation()) { 264 print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed", 265 "Humongous continuation should be of proper size", 266 file, line); 267 } 268 } 269 } 270 } 271 272 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) { 273 assert_correct(interior_loc, obj, file, line); 274 oop fwd = oop(ShenandoahForwarding::get_forwardee_raw_unchecked(obj)); 275 276 if (obj == fwd) { 277 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_forwarded failed", 278 "Object should be forwarded", 279 file, line); 280 } 281 } 282 283 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) { 284 assert_correct(interior_loc, obj, file, line); 285 oop fwd = oop(ShenandoahForwarding::get_forwardee_raw_unchecked(obj)); 286 287 if (obj != fwd) { 288 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_not_forwarded failed", 289 "Object should not be forwarded", 290 file, line); 291 } 292 } 293 294 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) { 295 assert_correct(interior_loc, obj, file, line); 296 297 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 298 if (!heap->marking_context()->is_marked(obj)) { 299 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_marked failed", 300 "Object should be marked", 301 file, line); 302 } 303 } 304 305 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) { 306 assert_correct(interior_loc, obj, file, line); 307 308 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 309 if (!heap->in_collection_set(obj)) { 310 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_in_cset failed", 311 "Object should be in collection set", 312 file, line); 313 } 314 } 315 316 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) { 317 assert_correct(interior_loc, obj, file, line); 318 319 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 320 if (heap->in_collection_set(obj)) { 321 print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_not_in_cset failed", 322 "Object should not be in collection set", 323 file, line); 324 } 325 } 326 327 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) { 328 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 329 if (heap->in_collection_set(interior_loc)) { 330 print_failure(_safe_unknown, NULL, interior_loc, NULL, "Shenandoah assert_not_in_cset_loc failed", 331 "Interior location should not be in collection set", 332 file, line); 333 } 334 } 335 336 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual, 337 const char *file, int line) { 338 ShenandoahMessageBuffer msg("%s\n", label); 339 msg.append(" Actual: " PTR_FORMAT "\n", p2i(actual)); 340 report_vm_error(file, line, msg.buffer()); 341 } 342 343 void ShenandoahAsserts::assert_rp_isalive_not_installed(const char *file, int line) { 344 ShenandoahHeap* heap = ShenandoahHeap::heap(); 345 ReferenceProcessor* rp = heap->ref_processor(); 346 if (rp->is_alive_non_header() != NULL) { 347 print_rp_failure("Shenandoah assert_rp_isalive_not_installed failed", rp->is_alive_non_header(), 348 file, line); 349 } 350 } 351 352 void ShenandoahAsserts::assert_rp_isalive_installed(const char *file, int line) { 353 ShenandoahHeap* heap = ShenandoahHeap::heap(); 354 ReferenceProcessor* rp = heap->ref_processor(); 355 if (rp->is_alive_non_header() == NULL) { 356 print_rp_failure("Shenandoah assert_rp_isalive_installed failed", rp->is_alive_non_header(), 357 file, line); 358 } 359 }