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