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