1 /* 2 * Copyright (c) 2021, 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/archiveBuilder.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/dumpTimeClassInfo.inline.hpp" 28 #include "cds/runTimeClassInfo.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/systemDictionaryShared.hpp" 32 #include "memory/resourceArea.hpp" 33 34 DumpTimeClassInfo::~DumpTimeClassInfo() { 35 if (_verifier_constraints != nullptr) { 36 assert(_verifier_constraint_flags != nullptr, "must be"); 37 delete _verifier_constraints; 38 delete _verifier_constraint_flags; 39 } 40 if (_loader_constraints != nullptr) { 41 delete _loader_constraints; 42 } 43 } 44 45 bool DumpTimeClassInfo::is_excluded() { 46 if (_excluded) { 47 return true; 48 } 49 if (_failed_verification) { 50 if (CDSConfig::preserve_all_dumptime_verification_states(_klass)) { 51 assert(CDSConfig::is_dumping_aot_linked_classes(), "sanity"); 52 // If the verification states are preserved, _klass will be archived in unlinked state. This is 53 // necessary to support the following scenario, where the verification of X requires that 54 // A be a subclass of B: 55 // class X { 56 // B getB() { return new A(); } 57 // } 58 // If X and B can be verified, but A fails verification, we still archive A (in a preloaded 59 // SystemDictionary) so that at runtime we cannot subvert the verification of X by replacing 60 // A with a version that is not a subtype of B. 61 } else { 62 // Don't archive this class. At runtime, load it from classfile and rerun verification. 63 return true; 64 } 65 } 66 return false; 67 } 68 69 size_t DumpTimeClassInfo::runtime_info_bytesize() const { 70 return RunTimeClassInfo::byte_size(_klass, num_verifier_constraints(), 71 num_loader_constraints(), 72 num_enum_klass_static_fields()); 73 } 74 75 void DumpTimeClassInfo::add_verification_constraint(InstanceKlass* k, Symbol* name, 76 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 77 if (_verifier_constraints == nullptr) { 78 _verifier_constraints = new (mtClass) GrowableArray<DTVerifierConstraint>(4, mtClass); 79 } 80 if (_verifier_constraint_flags == nullptr) { 81 _verifier_constraint_flags = new (mtClass) GrowableArray<char>(4, mtClass); 82 } 83 GrowableArray<DTVerifierConstraint>* vc_array = _verifier_constraints; 84 for (int i = 0; i < vc_array->length(); i++) { 85 if (vc_array->at(i).equals(name, from_name)) { 86 return; 87 } 88 } 89 DTVerifierConstraint cons(name, from_name); 90 vc_array->append(cons); 91 92 GrowableArray<char>* vcflags_array = _verifier_constraint_flags; 93 char c = 0; 94 c |= from_field_is_protected ? RunTimeClassInfo::FROM_FIELD_IS_PROTECTED : 0; 95 c |= from_is_array ? RunTimeClassInfo::FROM_IS_ARRAY : 0; 96 c |= from_is_object ? RunTimeClassInfo::FROM_IS_OBJECT : 0; 97 vcflags_array->append(c); 98 99 if (log_is_enabled(Trace, cds, verification)) { 100 ResourceMark rm; 101 log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x] array len %d flags len %d", 102 k->external_name(), from_name->as_klass_external_name(), 103 name->as_klass_external_name(), c, vc_array->length(), vcflags_array->length()); 104 } 105 } 106 107 static char get_loader_type_by(oop loader) { 108 assert(SystemDictionary::is_builtin_class_loader(loader), "Must be built-in loader"); 109 if (SystemDictionary::is_boot_class_loader(loader)) { 110 return (char)ClassLoader::BOOT_LOADER; 111 } else if (SystemDictionary::is_platform_class_loader(loader)) { 112 return (char)ClassLoader::PLATFORM_LOADER; 113 } else { 114 assert(SystemDictionary::is_system_class_loader(loader), "Class loader mismatch"); 115 return (char)ClassLoader::APP_LOADER; 116 } 117 } 118 119 void DumpTimeClassInfo::record_linking_constraint(Symbol* name, Handle loader1, Handle loader2) { 120 assert(loader1 != loader2, "sanity"); 121 LogTarget(Info, class, loader, constraints) log; 122 if (_loader_constraints == nullptr) { 123 _loader_constraints = new (mtClass) GrowableArray<DTLoaderConstraint>(4, mtClass); 124 } 125 char lt1 = get_loader_type_by(loader1()); 126 char lt2 = get_loader_type_by(loader2()); 127 DTLoaderConstraint lc(name, lt1, lt2); 128 for (int i = 0; i < _loader_constraints->length(); i++) { 129 if (lc.equals(_loader_constraints->at(i))) { 130 if (log.is_enabled()) { 131 ResourceMark rm; 132 // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp 133 log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s already added]", 134 _klass->external_name(), name->as_C_string(), 135 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 136 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 137 } 138 return; 139 } 140 } 141 _loader_constraints->append(lc); 142 if (log.is_enabled()) { 143 ResourceMark rm; 144 // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp 145 log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s total %d]", 146 _klass->external_name(), name->as_C_string(), 147 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 148 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id(), 149 _loader_constraints->length()); 150 } 151 } 152 153 void DumpTimeClassInfo::add_enum_klass_static_field(int archived_heap_root_index) { 154 if (_enum_klass_static_fields == nullptr) { 155 _enum_klass_static_fields = new (mtClass) GrowableArray<int>(20, mtClass); 156 } 157 _enum_klass_static_fields->append(archived_heap_root_index); 158 } 159 160 int DumpTimeClassInfo::enum_klass_static_field(int which_field) { 161 assert(_enum_klass_static_fields != nullptr, "must be"); 162 return _enum_klass_static_fields->at(which_field); 163 } 164 165 bool DumpTimeClassInfo::is_builtin() { 166 return SystemDictionaryShared::is_builtin(_klass); 167 } 168 169 DumpTimeClassInfo* DumpTimeSharedClassTable::allocate_info(InstanceKlass* k) { 170 assert(CDSConfig::is_dumping_final_static_archive() || !k->is_shared(), "Do not call with shared classes"); 171 bool created; 172 DumpTimeClassInfo* p = put_if_absent(k, &created); 173 assert(created, "must not exist in table"); 174 p->_klass = k; 175 return p; 176 } 177 178 DumpTimeClassInfo* DumpTimeSharedClassTable::get_info(InstanceKlass* k) { 179 assert(CDSConfig::is_dumping_final_static_archive() || !k->is_shared(), "Do not call with shared classes"); 180 DumpTimeClassInfo* p = get(k); 181 assert(p != nullptr, "we must not see any non-shared InstanceKlass* that's " 182 "not stored with SystemDictionaryShared::init_dumptime_info"); 183 assert(p->_klass == k, "Sanity"); 184 return p; 185 } 186 187 class CountClassByCategory : StackObj { 188 DumpTimeSharedClassTable* _table; 189 public: 190 CountClassByCategory(DumpTimeSharedClassTable* table) : _table(table) {} 191 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 192 if (!info.is_excluded()) { 193 if (info.is_builtin()) { 194 _table->inc_builtin_count(); 195 } else { 196 _table->inc_unregistered_count(); 197 } 198 } 199 } 200 }; 201 202 void DumpTimeSharedClassTable::update_counts() { 203 _builtin_count = 0; 204 _unregistered_count = 0; 205 CountClassByCategory counter(this); 206 iterate_all_live_classes(&counter); 207 }