< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahAsserts.cpp

Print this page

  1 /*
  2  * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 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_reserved(loc)) return;
 40 
 41   ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
 42   if (r != nullptr && 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.freeze());
 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(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 strong\n",              ctx->is_marked_strong(obj) ? "" : "not");
 71   msg.append("    %3s marked weak\n",                ctx->is_marked_weak(obj) ? "" : "not");
 72   msg.append("    %3s in collection set\n",          heap->in_collection_set(obj) ? "" : "not");
 73   if (heap->mode()->is_generational() && !obj->is_forwarded()) {
 74     msg.append("  age: %d\n", obj->age());
 75   }
 76   msg.append("  mark:%s\n", mw_ss.freeze());
 77   msg.append("  region: %s", ss.freeze());





















 78 }
 79 
 80 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {
 81   ShenandoahHeap* heap = ShenandoahHeap::heap();
 82   if (heap->is_in_reserved(loc)) {
 83     msg.append("  inside Java heap\n");
 84     ShenandoahHeapRegion *r = heap->heap_region_containing(loc);
 85     stringStream ss;
 86     r->print_on(&ss);
 87 
 88     msg.append("    %3s in collection set\n",    heap->in_collection_set_loc(loc) ? "" : "not");
 89     msg.append("  region: %s", ss.freeze());
 90   } else {
 91     msg.append("  outside of Java heap\n");
 92     stringStream ss;
 93     os::print_location(&ss, (intptr_t) loc, false);
 94     msg.append("  %s", ss.freeze());
 95   }
 96 }
 97 
 98 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) {
 99   ShenandoahHeap* heap = ShenandoahHeap::heap();
100   msg.append("  " PTR_FORMAT " - safe print, no details\n", p2i(loc));
101   if (heap->is_in_reserved(loc)) {
102     ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
103     if (r != nullptr) {
104       stringStream ss;
105       r->print_on(&ss);
106       msg.append("  region: %s", ss.freeze());
107       print_raw_memory(msg, loc);
108     }
109   }
110 }
111 
112 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc,
113                                        const char* phase, const char* label,
114                                        const char* file, int line) {
115   ShenandoahHeap* heap = ShenandoahHeap::heap();
116   ResourceMark rm;
117 




118   bool loc_in_heap = (loc != nullptr && heap->is_in_reserved(loc));
119 
120   ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label);
121 
122   msg.append("Referenced from:\n");
123   if (interior_loc != nullptr) {
124     msg.append("  interior location: " PTR_FORMAT "\n", p2i(interior_loc));
125     if (loc_in_heap) {
126       print_obj(msg, loc);
127     } else {
128       print_non_obj(msg, interior_loc);
129     }
130   } else {
131     msg.append("  no interior location recorded (probably a plain heap scan, or detached oop)\n");
132   }
133   msg.append("\n");
134 
135   msg.append("Object:\n");
136   if (level >= _safe_oop) {
137     print_obj(msg, obj);
138   } else {
139     print_obj_safe(msg, obj);
140   }
141   msg.append("\n");
142 
143   if (level >= _safe_oop) {
144     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
145     msg.append("Forwardee:\n");
146     if (obj != fwd) {
147       if (level >= _safe_oop_fwd) {
148         print_obj(msg, fwd);
149       } else {
150         print_obj_safe(msg, fwd);
151       }
152     } else {
153       msg.append("  (the object itself)");
154     }
155     msg.append("\n");
156   }
157 
158   if (level >= _safe_oop_fwd) {
159     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
160     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
161     if (fwd != fwd2) {
162       msg.append("Second forwardee:\n");
163       print_obj_safe(msg, fwd2);
164       msg.append("\n");
165     }
166   }
167 

182   ShenandoahHeap* heap = ShenandoahHeap::heap();
183 
184   if (obj != nullptr && !heap->is_in_reserved(obj)) {
185     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds_or_null failed",
186                   "oop must be in heap bounds",
187                   file, line);
188   }
189 }
190 
191 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) {
192   ShenandoahHeap* heap = ShenandoahHeap::heap();
193 
194   // Step 1. Check that obj is correct.
195   // After this step, it is safe to call heap_region_containing().
196   if (!heap->is_in_reserved(obj)) {
197     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
198                   "oop must be in heap bounds",
199                   file, line);
200   }
201 
202   Klass* obj_klass = ShenandoahForwarding::klass(obj);
203   if (obj_klass == nullptr) {
204     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
205                   "Object klass pointer should not be null",
206                   file,line);
207   }
208 
209   if (!Metaspace::contains(obj_klass)) {
210     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
211                   "Object klass pointer must go to metaspace",
212                   file,line);
213   }
214 
215   if (!heap->is_in(obj)) {
216     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
217                   "Object should be in active region area",
218                   file, line);
219   }
220 
221   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
222 
223   if (obj != fwd) {
224     // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something
225     // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr
226     // that still points to the object itself.
227     if (heap->is_full_gc_move_in_progress()) {
228       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
229                     "Non-trivial forwarding pointer during Full GC moves, probable bug.",
230                     file, line);
231     }
232 
233     // Step 2. Check that forwardee is correct
234     if (!heap->is_in_reserved(fwd)) {
235       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
236                     "Forwardee must be in heap bounds",
237                     file, line);
238     }
239 
240     if (obj_klass != ShenandoahForwarding::klass(fwd)) {
241       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
242                     "Forwardee klass disagrees with object class",
243                     file, line);
244     }
245 
246     // Step 3. Check that forwardee points to correct region
247     if (!heap->is_in(fwd)) {
248       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
249                     "Forwardee should be in active region area",
250                     file, line);
251     }
252 
253     if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) {
254       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
255                     "Non-trivial forwardee should be in another region",
256                     file, line);
257     }
258 
259     // Step 4. Check for multiple forwardings
260     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
261     if (fwd != fwd2) {
262       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
263                     "Multiple forwardings",
264                     file, line);
265     }
266   }
267 


























268   // Do additional checks for special objects: their fields can hold metadata as well.
269   // We want to check class loading/unloading did not corrupt them.


270 
271   if (Universe::is_fully_initialized() && (obj_klass == vmClasses::Class_klass())) {
272     Metadata* klass = obj->metadata_field(java_lang_Class::klass_offset());
273     if (klass != nullptr && !Metaspace::contains(klass)) {
274       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
275                     "Instance class mirror should point to Metaspace",
276                     file, line);
277     }
278 
279     Metadata* array_klass = obj->metadata_field(java_lang_Class::array_klass_offset());
280     if (array_klass != nullptr && !Metaspace::contains(array_klass)) {
281       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
282                     "Array class mirror should point to Metaspace",
283                     file, line);
284     }
285   }
286 }
287 
288 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) {
289   assert_correct(interior_loc, obj, file, line);
290 
291   ShenandoahHeap* heap = ShenandoahHeap::heap();
292   ShenandoahHeapRegion* r = heap->heap_region_containing(obj);
293   if (!r->is_active()) {
294     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
295                   "Object must reside in active region",
296                   file, line);
297   }
298 
299   size_t alloc_size = obj->size();
300   HeapWord* obj_end = cast_from_oop<HeapWord*>(obj) + alloc_size;
301 
302   if (ShenandoahHeapRegion::requires_humongous(alloc_size)) {

494     msg.append(" at a safepoint");
495   }
496   report_vm_error(file, line, msg.buffer());
497 }
498 
499 void ShenandoahAsserts::assert_generations_reconciled(const char* file, int line) {
500   if (!SafepointSynchronize::is_at_safepoint()) {
501     return;
502   }
503 
504   ShenandoahHeap* heap = ShenandoahHeap::heap();
505   ShenandoahGeneration* ggen = heap->gc_generation();
506   ShenandoahGeneration* agen = heap->active_generation();
507   if (agen == ggen) {
508     return;
509   }
510 
511   ShenandoahMessageBuffer msg("Active(%d) & GC(%d) Generations aren't reconciled", agen->type(), ggen->type());
512   report_vm_error(file, line, msg.buffer());
513 }
































  1 /*
  2  * Copyright (c) 2018, 2025, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 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 "oops/oop.inline.hpp"
 33 #include "memory/resourceArea.hpp"
 34 #include "runtime/os.hpp"
 35 #include "utilities/vmError.hpp"
 36 
 37 void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) {
 38   // Be extra safe. Only access data that is guaranteed to be safe:
 39   // should be in heap, in known committed region, within that region.
 40 
 41   ShenandoahHeap* heap = ShenandoahHeap::heap();
 42   if (!heap->is_in_reserved(loc)) return;
 43 
 44   ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
 45   if (r != nullptr && r->is_committed()) {
 46     address start = MAX2((address) r->bottom(), (address) loc - 32);
 47     address end   = MIN2((address) r->end(),    (address) loc + 128);
 48     if (start >= end) return;
 49 
 50     stringStream ss;
 51     os::print_hex_dump(&ss, start, end, 4);
 52     msg.append("\n");
 53     msg.append("Raw heap memory:\n%s", ss.freeze());
 54   }
 55 }
 56 
 57 void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) {
 58   ShenandoahHeap* heap = ShenandoahHeap::heap();
 59   ShenandoahHeapRegion *r = heap->heap_region_containing(obj);
 60 
 61   ResourceMark rm;
 62   stringStream ss;
 63   StreamIndentor si(&ss);



 64 
 65   ShenandoahMarkingContext* const ctx = heap->marking_context();
 66 
 67   narrowKlass nk = 0;
 68   const Klass* obj_klass = nullptr;
 69   const bool klass_valid = extract_klass_safely(obj, nk, obj_klass);
 70   const char* klass_text = "(invalid)";
 71   if (klass_valid && os::is_readable_pointer(obj_klass) && Metaspace::contains(obj_klass)) {
 72     klass_text = obj_klass->external_name();
 73   }
 74   ss.print_cr(PTR_FORMAT " - nk %u klass " PTR_FORMAT " %s\n", p2i(obj), nk, p2i(obj_klass), klass_text);
 75   {
 76     StreamIndentor si(&ss);
 77     ss.print_cr("%3s allocated after mark start", ctx->allocated_after_mark_start(obj) ? "" : "not");
 78     ss.print_cr("%3s after update watermark",     cast_from_oop<HeapWord*>(obj) >= r->get_update_watermark() ? "" : "not");
 79     ss.print_cr("%3s marked strong",              ctx->is_marked_strong(obj) ? "" : "not");
 80     ss.print_cr("%3s marked weak",                ctx->is_marked_weak(obj) ? "" : "not");
 81     ss.print_cr("%3s in collection set",          heap->in_collection_set(obj) ? "" : "not");
 82     if (heap->mode()->is_generational() && !obj->is_forwarded()) {
 83       ss.print_cr("age: %d", obj->age());
 84     }
 85     ss.print_raw("mark: ");
 86     obj->mark().print_on(&ss);
 87     ss.cr();
 88     ss.print_raw("region: ");
 89     r->print_on(&ss);
 90     ss.cr();
 91     if (obj_klass == vmClasses::Class_klass()) {
 92       msg.append("  mirrored klass:       " PTR_FORMAT "\n", p2i(obj->metadata_field(java_lang_Class::klass_offset())));
 93       msg.append("  mirrored array klass: " PTR_FORMAT "\n", p2i(obj->metadata_field(java_lang_Class::array_klass_offset())));
 94     }
 95   }
 96   const_address loc = cast_from_oop<const_address>(obj);
 97   os::print_hex_dump(&ss, loc, loc + 64, 4, true, 32, loc);
 98   msg.append("%s", ss.base());
 99 }
100 
101 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {
102   ShenandoahHeap* heap = ShenandoahHeap::heap();
103   if (heap->is_in_reserved(loc)) {
104     msg.append("  inside Java heap\n");
105     ShenandoahHeapRegion *r = heap->heap_region_containing(loc);
106     stringStream ss;
107     r->print_on(&ss);
108 
109     msg.append("    %3s in collection set\n",    heap->in_collection_set_loc(loc) ? "" : "not");
110     msg.append("  region: %s", ss.freeze());
111   } else {
112     msg.append("  outside of Java heap\n");
113     stringStream ss;
114     os::print_location(&ss, (intptr_t) loc, false);
115     msg.append("  %s", ss.freeze());
116   }
117 }
118 
119 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) {
120   ShenandoahHeap* heap = ShenandoahHeap::heap();
121   msg.append("  " PTR_FORMAT " - safe print, no details\n", p2i(loc));
122   if (heap->is_in_reserved(loc)) {
123     ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
124     if (r != nullptr) {
125       stringStream ss;
126       r->print_on(&ss);
127       msg.append("  region: %s", ss.freeze());
128       print_raw_memory(msg, loc);
129     }
130   }
131 }
132 
133 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc,
134                                        const char* phase, const char* label,
135                                        const char* file, int line) {
136   ShenandoahHeap* heap = ShenandoahHeap::heap();
137   ResourceMark rm;
138 
139   if (!os::is_readable_pointer(obj)) {
140     level = _safe_unknown;
141   }
142 
143   bool loc_in_heap = (loc != nullptr && heap->is_in_reserved(loc));
144 
145   ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label);
146 
147   msg.append("Referenced from:\n");
148   if (interior_loc != nullptr) {
149     msg.append("  interior location: " PTR_FORMAT "\n", p2i(interior_loc));
150     if (loc_in_heap && os::is_readable_pointer(loc)) {
151       print_obj(msg, loc);
152     } else {
153       print_non_obj(msg, interior_loc);
154     }
155   } else {
156     msg.append("  no interior location recorded (probably a plain heap scan, or detached oop)\n");
157   }
158   msg.append("\n");
159 
160   msg.append("Object:\n");
161   if (level >= _safe_oop) {
162     print_obj(msg, obj);
163   } else {
164     print_obj_safe(msg, obj);
165   }
166   msg.append("\n");
167 
168   if (level >= _safe_oop) {
169     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
170     msg.append("Forwardee:\n");
171     if (obj != fwd) {
172       if (level >= _safe_oop_fwd && os::is_readable_pointer(fwd)) {
173         print_obj(msg, fwd);
174       } else {
175         print_obj_safe(msg, fwd);
176       }
177     } else {
178       msg.append("  (the object itself)");
179     }
180     msg.append("\n");
181   }
182 
183   if (level >= _safe_oop_fwd) {
184     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
185     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
186     if (fwd != fwd2) {
187       msg.append("Second forwardee:\n");
188       print_obj_safe(msg, fwd2);
189       msg.append("\n");
190     }
191   }
192 

207   ShenandoahHeap* heap = ShenandoahHeap::heap();
208 
209   if (obj != nullptr && !heap->is_in_reserved(obj)) {
210     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds_or_null failed",
211                   "oop must be in heap bounds",
212                   file, line);
213   }
214 }
215 
216 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) {
217   ShenandoahHeap* heap = ShenandoahHeap::heap();
218 
219   // Step 1. Check that obj is correct.
220   // After this step, it is safe to call heap_region_containing().
221   if (!heap->is_in_reserved(obj)) {
222     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
223                   "oop must be in heap bounds",
224                   file, line);
225   }
226 
227   if (!os::is_readable_pointer(obj)) {

228     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
229                   "oop within heap bounds but at unreadable location",
230                   file, line);






231   }
232 
233   if (!heap->is_in(obj)) {
234     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
235                   "Object should be in active region area",
236                   file, line);
237   }
238 
239   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
240 
241   if (obj != fwd) {
242     // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something
243     // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr
244     // that still points to the object itself.
245     if (heap->is_full_gc_move_in_progress()) {
246       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
247                     "Non-trivial forwarding pointer during Full GC moves, probable bug.",
248                     file, line);
249     }
250 
251     // Step 2. Check that forwardee is correct
252     if (!heap->is_in_reserved(fwd)) {
253       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
254                     "Forwardee must be in heap bounds",
255                     file, line);
256     }
257 
258     if (!os::is_readable_pointer(fwd)) {
259       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
260                     "Forwardee within heap bounds but at unreadable location",
261                     file, line);
262     }
263 
264     // Step 3. Check that forwardee points to correct region
265     if (!heap->is_in(fwd)) {
266       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
267                     "Forwardee should be in active region area",
268                     file, line);
269     }
270 
271     if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) {
272       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
273                     "Non-trivial forwardee should be in another region",
274                     file, line);
275     }
276 
277     // Step 4. Check for multiple forwardings
278     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
279     if (fwd != fwd2) {
280       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
281                     "Multiple forwardings",
282                     file, line);
283     }
284   }
285 
286   const Klass* obj_klass = nullptr;
287   narrowKlass nk = 0;
288   if (!extract_klass_safely(obj, nk, obj_klass)) {
289     print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
290                   "Object klass pointer invalid",
291                   file,line);
292   }
293 
294   if (obj_klass == nullptr) {
295     print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
296                   "Object klass pointer should not be null",
297                   file,line);
298   }
299 
300   if (!Metaspace::contains(obj_klass)) {
301     print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
302                   "Object klass pointer must go to metaspace",
303                   file,line);
304   }
305 
306   if (!UseCompactObjectHeaders && obj_klass != fwd->klass_or_null()) {
307     print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
308                   "Forwardee klass disagrees with object class",
309                   file, line);
310   }
311 
312   // Do additional checks for special objects: their fields can hold metadata as well.
313   // We want to check class loading/unloading did not corrupt them. We can only reasonably
314   // trust the forwarded objects, as the from-space object can have the klasses effectively
315   // dead.
316 
317   if (Universe::is_fully_initialized() && (obj_klass == vmClasses::Class_klass())) {
318     const Metadata* klass = fwd->metadata_field(java_lang_Class::klass_offset());
319     if (klass != nullptr && !Metaspace::contains(klass)) {
320       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
321                     "Mirrored instance class should point to Metaspace",
322                     file, line);
323     }
324 
325     const Metadata* array_klass = fwd->metadata_field(java_lang_Class::array_klass_offset());
326     if (array_klass != nullptr && !Metaspace::contains(array_klass)) {
327       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
328                     "Mirrored array class should point to Metaspace",
329                     file, line);
330     }
331   }
332 }
333 
334 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) {
335   assert_correct(interior_loc, obj, file, line);
336 
337   ShenandoahHeap* heap = ShenandoahHeap::heap();
338   ShenandoahHeapRegion* r = heap->heap_region_containing(obj);
339   if (!r->is_active()) {
340     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
341                   "Object must reside in active region",
342                   file, line);
343   }
344 
345   size_t alloc_size = obj->size();
346   HeapWord* obj_end = cast_from_oop<HeapWord*>(obj) + alloc_size;
347 
348   if (ShenandoahHeapRegion::requires_humongous(alloc_size)) {

540     msg.append(" at a safepoint");
541   }
542   report_vm_error(file, line, msg.buffer());
543 }
544 
545 void ShenandoahAsserts::assert_generations_reconciled(const char* file, int line) {
546   if (!SafepointSynchronize::is_at_safepoint()) {
547     return;
548   }
549 
550   ShenandoahHeap* heap = ShenandoahHeap::heap();
551   ShenandoahGeneration* ggen = heap->gc_generation();
552   ShenandoahGeneration* agen = heap->active_generation();
553   if (agen == ggen) {
554     return;
555   }
556 
557   ShenandoahMessageBuffer msg("Active(%d) & GC(%d) Generations aren't reconciled", agen->type(), ggen->type());
558   report_vm_error(file, line, msg.buffer());
559 }
560 
561 bool ShenandoahAsserts::extract_klass_safely(oop obj, narrowKlass& nk, const Klass*& k) {
562   nk = 0;
563   k = nullptr;
564 
565   if (!os::is_readable_pointer(obj)) {
566     return false;
567   }
568   if (UseCompressedClassPointers) {
569     if (UseCompactObjectHeaders) { // look in forwardee
570       markWord mark = obj->mark();
571       if (mark.is_marked()) {
572         oop fwd = cast_to_oop(mark.clear_lock_bits().to_pointer());
573         if (!os::is_readable_pointer(fwd)) {
574           return false;
575         }
576         mark = fwd->mark();
577       }
578       nk = mark.narrow_klass();
579     } else {
580       nk = obj->narrow_klass();
581     }
582     if (!CompressedKlassPointers::is_valid_narrow_klass_id(nk)) {
583       return false;
584     }
585     k = CompressedKlassPointers::decode_not_null_without_asserts(nk);
586   } else {
587     k = obj->klass();
588   }
589   return k != nullptr;
590 }
< prev index next >