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