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