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 
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   if (ShenandoahHeapRegion::requires_humongous(alloc_size)) {
349     size_t idx = r->index();
350     size_t end_idx = heap->heap_region_index_containing(obj_end - 1);
351     for (size_t i = idx; i < end_idx; i++) {
352       ShenandoahHeapRegion* chain_reg = heap->get_region(i);
353       if (i == idx && !chain_reg->is_humongous_start()) {
354         print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
355                       "Object must reside in humongous start",
356                       file, line);
357       }
358       if (i != idx && !chain_reg->is_humongous_continuation()) {
359         print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
360                       "Humongous continuation should be of proper size",
361                       file, line);
362       }
363     }
364   } else {
365     if (obj_end > r->top()) {
366       print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
367                     "Object end should be within the active area of the region",
368                     file, line);
369     }
370   }
371 }
372 
373 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) {
374   assert_correct(interior_loc, obj, file, line);
375   oop fwd =   ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
376 
377   if (obj == fwd) {
378     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed",
379                   "Object should be forwarded",
380                   file, line);
381   }
382 }
383 
384 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) {
385   assert_correct(interior_loc, obj, file, line);
386   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
387 
388   if (obj != fwd) {
389     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed",
390                   "Object should not be forwarded",
391                   file, line);
392   }
393 }
394 
395 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) {
396   assert_correct(interior_loc, obj, file, line);
397 
398   ShenandoahHeap* heap = ShenandoahHeap::heap();
399   if (!heap->marking_context()->is_marked(obj)) {
400     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed",
401                   "Object should be marked",
402                   file, line);
403   }
404 }
405 
406 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) {
407   assert_correct(interior_loc, obj, file, line);
408 
409   ShenandoahHeap* heap = ShenandoahHeap::heap();
410   if (!heap->marking_context()->is_marked_weak(obj)) {
411     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed",
412                   "Object should be marked weakly",
413                   file, line);
414   }
415 }
416 
417 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) {
418   assert_correct(interior_loc, obj, file, line);
419 
420   ShenandoahHeap* heap = ShenandoahHeap::heap();
421   if (!heap->marking_context()->is_marked_strong(obj)) {
422     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed",
423                   "Object should be marked strongly",
424                   file, line);
425   }
426 }
427 
428 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) {
429   assert_correct(interior_loc, obj, file, line);
430 
431   ShenandoahHeap* heap = ShenandoahHeap::heap();
432   if (!heap->in_collection_set(obj)) {
433     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed",
434                   "Object should be in collection set",
435                   file, line);
436   }
437 }
438 
439 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) {
440   assert_correct(interior_loc, obj, file, line);
441 
442   ShenandoahHeap* heap = ShenandoahHeap::heap();
443   if (heap->in_collection_set(obj)) {
444     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed",
445                   "Object should not be in collection set",
446                   file, line);
447   }
448 }
449 
450 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) {
451   ShenandoahHeap* heap = ShenandoahHeap::heap();
452   if (heap->in_collection_set_loc(interior_loc)) {
453     print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed",
454                   "Interior location should not be in collection set",
455                   file, line);
456   }
457 }
458 
459 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual,
460                                          const char *file, int line) {
461   ShenandoahMessageBuffer msg("%s\n", label);
462   msg.append(" Actual:                  " PTR_FORMAT "\n", p2i(actual));
463   report_vm_error(file, line, msg.buffer());
464 }
465 
466 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) {
467   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
468     return;
469   }
470 
471   if (lock->owned_by_self()) {
472     return;
473   }
474 
475   ShenandoahMessageBuffer msg("Must be at a Shenandoah safepoint or held %s lock", lock->name());
476   report_vm_error(file, line, msg.buffer());
477 }
478 
479 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) {
480   ShenandoahHeap* heap = ShenandoahHeap::heap();
481 
482   if (heap->lock()->owned_by_self()) {
483     return;
484   }
485 
486   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread");
487   report_vm_error(file, line, msg.buffer());
488 }
489 
490 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) {
491   ShenandoahHeap* heap = ShenandoahHeap::heap();
492 
493   if (!heap->lock()->owned_by_self()) {
494     return;
495   }
496 
497   ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread");
498   report_vm_error(file, line, msg.buffer());
499 }
500 
501 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) {
502   ShenandoahHeap* heap = ShenandoahHeap::heap();
503 
504   if (heap->lock()->owned_by_self()) {
505     return;
506   }
507 
508   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
509     return;
510   }
511 
512   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint");
513   report_vm_error(file, line, msg.buffer());
514 }
515 
516 void ShenandoahAsserts::assert_generational(const char* file, int line) {
517   if (ShenandoahHeap::heap()->mode()->is_generational()) {
518     return;
519   }
520 
521   ShenandoahMessageBuffer msg("Must be in generational mode");
522   report_vm_error(file, line, msg.buffer());
523 }
524 
525 void ShenandoahAsserts::assert_control_or_vm_thread_at_safepoint(bool at_safepoint, const char* file, int line) {
526   Thread* thr = Thread::current();
527   if (thr == ShenandoahHeap::heap()->control_thread()) {
528     return;
529   }
530   if (thr->is_VM_thread()) {
531     if (!at_safepoint) {
532       return;
533     } else if (SafepointSynchronize::is_at_safepoint()) {
534       return;
535     }
536   }
537 
538   ShenandoahMessageBuffer msg("Must be either control thread, or vm thread");
539   if (at_safepoint) {
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 }