From aa060f22d302789c4f80dd1ebaa233a97b6b0073 Mon Sep 17 00:00:00 2001 From: Aleksey Shipilev Date: Mon, 21 Oct 2024 08:04:12 +0000 Subject: [PATCH] 8342334: CDS: Scratch mirrors should not point to dead klasses Reviewed-by: adinn, coleenp, iklam --- src/hotspot/share/cds/heapShared.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/hotspot/share/cds/heapShared.cpp b/src/hotspot/share/cds/heapShared.cpp index 81aa7ac94dc21..22040448770d5 100644 --- a/src/hotspot/share/cds/heapShared.cpp +++ b/src/hotspot/share/cds/heapShared.cpp @@ -386,6 +386,13 @@ void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) { } void HeapShared::remove_scratch_objects(Klass* k) { + // Klass is being deallocated. Java mirror can still be alive, and it should not + // point to dead klass. We need to break the link from mirror to the Klass. + // See how InstanceKlass::deallocate_contents does it for normal mirrors. + oop mirror = _scratch_java_mirror_table->get_oop(k); + if (mirror != nullptr) { + java_lang_Class::set_klass(mirror, nullptr); + } _scratch_java_mirror_table->remove_oop(k); if (k->is_instance_klass()) { _scratch_references_table->remove(InstanceKlass::cast(k)->constants());