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.inline.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 #include "oops/oop.inline.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 
193   report_vm_error(file, line, msg.buffer());
194 }
195 
196 void ShenandoahAsserts::assert_in_heap_bounds(void* interior_loc, oop obj, const char *file, int line) {
197   ShenandoahHeap* heap = ShenandoahHeap::heap();
198 
199   if (!heap->is_in_reserved(obj)) {
200     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds failed",
201                   "oop must be in heap bounds",
202                   file, line);
203   }
204 }
205 
206 void ShenandoahAsserts::assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char *file, int line) {
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   // Mirror the allocator's humongous classification. A fresh object whose base
349   // size could grow by one word for an injected identity hash-code is allocated
350   // as humongous up front (see ShenandoahFreeSet::allocate), so verification must
351   // reserve the same headroom while the object is still unexpanded. Once expanded,
352   // obj->size() already includes the hash word and is final, so no headroom is
353   // added -- and requires_humongous() then yields the same answer the allocator
354   // reached on the unexpanded base size.
355   const bool may_expand_for_hash = UseCompactObjectHeaders && !obj->mark().is_expanded();
356   if (ShenandoahHeapRegion::requires_humongous(alloc_size, may_expand_for_hash)) {
357     size_t idx = r->index();
358     size_t end_idx = heap->heap_region_index_containing(obj_end - 1);
359     for (size_t i = idx; i < end_idx; i++) {
360       ShenandoahHeapRegion* chain_reg = heap->get_region(i);
361       if (i == idx && !chain_reg->is_humongous_start()) {
362         print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
363                       "Object must reside in humongous start",
364                       file, line);
365       }
366       if (i != idx && !chain_reg->is_humongous_continuation()) {
367         print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
368                       "Humongous continuation should be of proper size",
369                       file, line);
370       }
371     }
372   } else {
373     if (obj_end > r->top()) {
374       print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
375                     "Object end should be within the active area of the region",
376                     file, line);
377     }
378   }
379 }
380 
381 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) {
382   assert_correct(interior_loc, obj, file, line);
383   oop fwd =   ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
384 
385   if (obj == fwd) {
386     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed",
387                   "Object should be forwarded",
388                   file, line);
389   }
390 }
391 
392 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) {
393   assert_correct(interior_loc, obj, file, line);
394   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
395 
396   if (obj != fwd) {
397     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed",
398                   "Object should not be forwarded",
399                   file, line);
400   }
401 }
402 
403 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) {
404   assert_correct(interior_loc, obj, file, line);
405 
406   ShenandoahHeap* heap = ShenandoahHeap::heap();
407   if (!heap->marking_context()->is_marked(obj)) {
408     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed",
409                   "Object should be marked",
410                   file, line);
411   }
412 }
413 
414 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) {
415   assert_correct(interior_loc, obj, file, line);
416 
417   ShenandoahHeap* heap = ShenandoahHeap::heap();
418   if (!heap->marking_context()->is_marked_weak(obj)) {
419     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed",
420                   "Object should be marked weakly",
421                   file, line);
422   }
423 }
424 
425 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) {
426   assert_correct(interior_loc, obj, file, line);
427 
428   ShenandoahHeap* heap = ShenandoahHeap::heap();
429   if (!heap->marking_context()->is_marked_strong(obj)) {
430     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed",
431                   "Object should be marked strongly",
432                   file, line);
433   }
434 }
435 
436 void ShenandoahAsserts::assert_mark_complete(HeapWord* obj, const char* file, int line) {
437   const ShenandoahHeap* heap = ShenandoahHeap::heap();
438   const ShenandoahHeapRegion* region = heap->heap_region_containing(obj);
439   const ShenandoahGeneration* generation = heap->generation_for(region->affiliation());
440   if (!generation->is_mark_complete()) {
441     ShenandoahMessageBuffer msg("Marking should be complete for object " PTR_FORMAT " in the %s generation", p2i(obj), generation->name());
442     report_vm_error(file, line, msg.buffer());
443   }
444 }
445 
446 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) {
447   assert_correct(interior_loc, obj, file, line);
448 
449   ShenandoahHeap* heap = ShenandoahHeap::heap();
450   if (!heap->in_collection_set(obj)) {
451     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed",
452                   "Object should be in collection set",
453                   file, line);
454   }
455 }
456 
457 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) {
458   assert_correct(interior_loc, obj, file, line);
459 
460   ShenandoahHeap* heap = ShenandoahHeap::heap();
461   if (heap->in_collection_set(obj)) {
462     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed",
463                   "Object should not be in collection set",
464                   file, line);
465   }
466 }
467 
468 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) {
469   ShenandoahHeap* heap = ShenandoahHeap::heap();
470   if (heap->in_collection_set_loc(interior_loc)) {
471     print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed",
472                   "Interior location should not be in collection set",
473                   file, line);
474   }
475 }
476 
477 void ShenandoahAsserts::assert_in_young(void* interior_loc, oop obj, const char* file, int line) {
478   assert_correct(interior_loc, obj, file, line);
479 
480   ShenandoahHeap* heap = ShenandoahHeap::heap();
481   if (!heap->heap_region_containing(obj)->is_young()) {
482     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_young failed",
483                   "Object should be in young region",
484                   file, line);
485   }
486 }
487 
488 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual,
489                                          const char *file, int line) {
490   ShenandoahMessageBuffer msg("%s\n", label);
491   msg.append(" Actual:                  " PTR_FORMAT "\n", p2i(actual));
492   report_vm_error(file, line, msg.buffer());
493 }
494 
495 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) {
496   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
497     return;
498   }
499 
500   if (lock->owned_by_self()) {
501     return;
502   }
503 
504   ShenandoahMessageBuffer msg("Must be at a Shenandoah safepoint or held %s lock", lock->name());
505   report_vm_error(file, line, msg.buffer());
506 }
507 
508 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) {
509   ShenandoahHeap* heap = ShenandoahHeap::heap();
510 
511   if (heap->lock()->owned_by_self()) {
512     return;
513   }
514 
515   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread");
516   report_vm_error(file, line, msg.buffer());
517 }
518 
519 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) {
520   ShenandoahHeap* heap = ShenandoahHeap::heap();
521 
522   if (!heap->lock()->owned_by_self()) {
523     return;
524   }
525 
526   ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread");
527   report_vm_error(file, line, msg.buffer());
528 }
529 
530 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) {
531   ShenandoahHeap* heap = ShenandoahHeap::heap();
532 
533   if (heap->lock()->owned_by_self()) {
534     return;
535   }
536 
537   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
538     return;
539   }
540 
541   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint");
542   report_vm_error(file, line, msg.buffer());
543 }
544 
545 void ShenandoahAsserts::assert_generational(const char* file, int line) {
546   if (ShenandoahHeap::heap()->mode()->is_generational()) {
547     return;
548   }
549 
550   ShenandoahMessageBuffer msg("Must be in generational mode");
551   report_vm_error(file, line, msg.buffer());
552 }
553 
554 void ShenandoahAsserts::assert_control_or_vm_thread_at_safepoint(bool at_safepoint, const char* file, int line) {
555   Thread* thr = Thread::current();
556   if (thr == ShenandoahHeap::heap()->control_thread()) {
557     return;
558   }
559   if (thr->is_VM_thread()) {
560     if (!at_safepoint) {
561       return;
562     } else if (SafepointSynchronize::is_at_safepoint()) {
563       return;
564     }
565   }
566 
567   ShenandoahMessageBuffer msg("Must be either control thread, or vm thread");
568   if (at_safepoint) {
569     msg.append(" at a safepoint");
570   }
571   report_vm_error(file, line, msg.buffer());
572 }
573 
574 bool ShenandoahAsserts::extract_klass_safely(oop obj, narrowKlass& nk, const Klass*& k) {
575   nk = 0;
576   k = nullptr;
577 
578   if (!os::is_readable_pointer(obj)) {
579     return false;
580   }
581 
582   if (UseCompactObjectHeaders) { // look in forwardee
583     markWord mark = obj->mark();
584     if (ShenandoahForwarding::has_forwardee(mark)) {
585       oop fwd = cast_to_oop(mark.clear_lock_bits().to_pointer());
586       if (!os::is_readable_pointer(fwd)) {
587         return false;
588       }
589       mark = fwd->mark();
590     }
591     nk = mark.narrow_klass();
592   } else {
593     nk = obj->narrow_klass();
594   }
595   if (!CompressedKlassPointers::is_valid_narrow_klass_id(nk)) {
596     return false;
597   }
598   k = CompressedKlassPointers::decode_not_null_without_asserts(nk);
599 
600   return k != nullptr;
601 }