1 /*
   2  * Copyright (c) 2018, 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/shenandoahBrooksPointer.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.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/shenandoahTraversalGC.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 != NULL && 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.as_string());
  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   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   if (heap->traversal_gc() != NULL) {
  71     msg.append("    %3s in traversal set\n",         heap->traversal_gc()->traversal_set()->is_in((HeapWord*) obj) ? "" : "not");
  72   }
  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) ? "" : "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) ShenandoahBrooksPointer::get_raw_unchecked(obj);
 141     msg.append("Forwardee:\n");
 142     if (!oopDesc::equals_raw(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) ShenandoahBrooksPointer::get_raw_unchecked(obj);
 156     oop fwd2 = (oop) ShenandoahBrooksPointer::get_raw_unchecked(fwd);
 157     if (!oopDesc::equals_raw(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_no_check();
 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_no_check();
 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(ShenandoahBrooksPointer::get_raw_unchecked(obj));
 202 
 203   if (!oopDesc::equals_raw(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(ShenandoahBrooksPointer::get_raw_unchecked(fwd));
 235     if (!oopDesc::equals_raw(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_no_check();
 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() + ShenandoahBrooksPointer::word_size();
 255   if (alloc_size > ShenandoahHeapRegion::humongous_threshold_words()) {
 256     size_t idx = r->region_number();
 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(ShenandoahBrooksPointer::get_raw_unchecked(obj));
 277 
 278   if (oopDesc::equals_raw(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(ShenandoahBrooksPointer::get_raw_unchecked(obj));
 288 
 289   if (!oopDesc::equals_raw(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_no_check();
 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_no_check();
 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_no_check();
 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_no_check();
 331   if (heap->in_collection_set(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_rp_isalive_not_installed(const char *file, int line) {
 346   ShenandoahHeap* heap = ShenandoahHeap::heap();
 347   ReferenceProcessor* rp = heap->ref_processor();
 348   if (rp->is_alive_non_header() != NULL) {
 349     print_rp_failure("Shenandoah assert_rp_isalive_not_installed failed", rp->is_alive_non_header(),
 350                      file, line);
 351   }
 352 }
 353 
 354 void ShenandoahAsserts::assert_rp_isalive_installed(const char *file, int line) {
 355   ShenandoahHeap* heap = ShenandoahHeap::heap();
 356   ReferenceProcessor* rp = heap->ref_processor();
 357   if (rp->is_alive_non_header() == NULL) {
 358     print_rp_failure("Shenandoah assert_rp_isalive_installed failed", rp->is_alive_non_header(),
 359                      file, line);
 360   }
 361 }