1 /* 2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. 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 #include "cds/cdsConfig.hpp" 26 #include "classfile/classFileParser.hpp" 27 #include "classfile/javaClasses.hpp" 28 #include "classfile/vmClasses.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "oops/instanceRefKlass.inline.hpp" 31 #include "oops/oop.inline.hpp" 32 33 InstanceRefKlass::InstanceRefKlass() { 34 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS"); 35 } 36 37 static ReferenceType reference_subclass_name_to_type(const Symbol* name) { 38 if ( name == vmSymbols::java_lang_ref_SoftReference()) { 39 return REF_SOFT; 40 } else if (name == vmSymbols::java_lang_ref_WeakReference()) { 41 return REF_WEAK; 42 } else if (name == vmSymbols::java_lang_ref_FinalReference()) { 43 return REF_FINAL; 44 } else if (name == vmSymbols::java_lang_ref_PhantomReference()) { 45 return REF_PHANTOM; 46 } else { 47 ShouldNotReachHere(); 48 return REF_NONE; 49 } 50 } 51 52 static ReferenceType determine_reference_type(const ClassFileParser& parser) { 53 const ReferenceType rt = parser.super_reference_type(); 54 if (rt != REF_NONE) { 55 // Inherit type from super class 56 return rt; 57 } 58 59 // Bootstrapping: this is one of the direct subclasses of java.lang.ref.Reference 60 const Symbol* const name = parser.class_name(); 61 return reference_subclass_name_to_type(name); 62 } 63 64 InstanceRefKlass::InstanceRefKlass(const ClassFileParser& parser) 65 : InstanceKlass(parser, Kind, determine_reference_type(parser)) {} 66 67 void InstanceRefKlass::update_nonstatic_oop_maps(Klass* k) { 68 // Clear the nonstatic oop-map entries corresponding to referent 69 // and discovered fields. They are treated specially by the 70 // garbage collector. 71 InstanceKlass* ik = InstanceKlass::cast(k); 72 73 // Check that we have the right class 74 debug_only(static bool first_time = true); 75 assert(k == vmClasses::Reference_klass() && first_time, 76 "Invalid update of maps"); 77 debug_only(first_time = false); 78 assert(ik->nonstatic_oop_map_count() == 1, "just checking"); 79 80 OopMapBlock* map = ik->start_of_nonstatic_oop_maps(); 81 82 #ifdef ASSERT 83 // Verify fields are in the expected places. 84 int referent_offset = java_lang_ref_Reference::referent_offset(); 85 int queue_offset = java_lang_ref_Reference::queue_offset(); 86 int next_offset = java_lang_ref_Reference::next_offset(); 87 int discovered_offset = java_lang_ref_Reference::discovered_offset(); 88 assert(referent_offset < queue_offset, "just checking"); 89 assert(queue_offset < next_offset, "just checking"); 90 assert(next_offset < discovered_offset, "just checking"); 91 const unsigned int count = 92 1 + ((discovered_offset - referent_offset) / heapOopSize); 93 assert(count == 4, "just checking"); 94 #endif // ASSERT 95 96 // Updated map starts at "queue", covers "queue" and "next". 97 const int new_offset = java_lang_ref_Reference::queue_offset(); 98 const unsigned int new_count = 2; // queue and next 99 100 // Verify existing map is as expected, and update if needed. 101 if (CDSConfig::is_using_archive()) { 102 assert(map->offset() == new_offset, "just checking"); 103 assert(map->count() == new_count, "just checking"); 104 } else { 105 assert(map->offset() == referent_offset, "just checking"); 106 assert(map->count() == count, "just checking"); 107 map->set_offset(new_offset); 108 map->set_count(new_count); 109 } 110 } 111 112 113 // Verification 114 115 void InstanceRefKlass::oop_verify_on(oop obj, outputStream* st) { 116 InstanceKlass::oop_verify_on(obj, st); 117 // Verify referent field 118 oop referent = java_lang_ref_Reference::unknown_referent_no_keepalive(obj); 119 if (referent != nullptr) { 120 guarantee(oopDesc::is_oop(referent), "referent field heap failed"); 121 } 122 // Additional verification for next field, which must be a Reference or null 123 oop next = java_lang_ref_Reference::next(obj); 124 if (next != nullptr) { 125 guarantee(oopDesc::is_oop(next), "next field should be an oop"); 126 guarantee(next->is_instanceRef(), "next field verify failed"); 127 } 128 }