1 /* 2 * Copyright (c) 2021, 2024, 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 "precompiled.hpp" 26 #include "cds/cds_globals.hpp" 27 #include "cds/classListWriter.hpp" 28 #include "cds/lambdaFormInvokers.inline.hpp" 29 #include "classfile/classFileStream.hpp" 30 #include "classfile/classLoader.hpp" 31 #include "classfile/classLoaderData.hpp" 32 #include "classfile/classLoaderDataGraph.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "oops/constantPool.inline.hpp" 37 #include "oops/instanceKlass.hpp" 38 #include "runtime/mutexLocker.hpp" 39 40 fileStream* ClassListWriter::_classlist_file = nullptr; 41 42 void ClassListWriter::init() { 43 // For -XX:DumpLoadedClassList=<file> option 44 if (DumpLoadedClassList != nullptr) { 45 const char* list_name = make_log_name(DumpLoadedClassList, nullptr); 46 _classlist_file = new(mtInternal) 47 fileStream(list_name); 48 _classlist_file->print_cr("# NOTE: Do not modify this file."); 49 _classlist_file->print_cr("#"); 50 _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option"); 51 _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump)."); 52 _classlist_file->print_cr("#"); 53 FREE_C_HEAP_ARRAY(char, list_name); 54 } 55 } 56 57 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) { 58 assert(is_enabled(), "must be"); 59 60 if (!ClassLoader::has_jrt_entry()) { 61 log_warning(cds)("DumpLoadedClassList and CDS are not supported in exploded build"); 62 DumpLoadedClassList = nullptr; 63 return; 64 } 65 66 ClassListWriter w; 67 write_to_stream(k, w.stream(), cfs); 68 } 69 70 class ClassListWriter::IDTable : public ResourceHashtable< 71 const InstanceKlass*, int, 72 15889, // prime number 73 AnyObj::C_HEAP> {}; 74 75 ClassListWriter::IDTable* ClassListWriter::_id_table = nullptr; 76 int ClassListWriter::_total_ids = 0; 77 78 int ClassListWriter::get_id(const InstanceKlass* k) { 79 assert_locked(); 80 if (_id_table == nullptr) { 81 _id_table = new (mtClass)IDTable(); 82 } 83 bool created; 84 int* v = _id_table->put_if_absent(k, &created); 85 if (created) { 86 *v = _total_ids++; 87 } 88 return *v; 89 } 90 91 bool ClassListWriter::has_id(const InstanceKlass* k) { 92 assert_locked(); 93 if (_id_table != nullptr) { 94 return _id_table->get(k) != nullptr; 95 } else { 96 return false; 97 } 98 } 99 100 void ClassListWriter::handle_class_unloading(const InstanceKlass* klass) { 101 assert_locked(); 102 if (_id_table != nullptr) { 103 _id_table->remove(klass); 104 } 105 } 106 107 void ClassListWriter::write_to_stream(const InstanceKlass* k, outputStream* stream, const ClassFileStream* cfs) { 108 assert_locked(); 109 110 ClassLoaderData* loader_data = k->class_loader_data(); 111 bool is_builtin_loader = SystemDictionaryShared::is_builtin_loader(loader_data); 112 if (!is_builtin_loader) { 113 // class may be loaded from shared archive 114 if (!k->is_shared()) { 115 if (cfs == nullptr || cfs->source() == nullptr) { 116 // CDS static dump only handles unregistered class with known source. 117 return; 118 } 119 if (strncmp(cfs->source(), "file:", 5) != 0) { 120 return; 121 } 122 } else { 123 // Shared unregistered classes are skipped since their real source are not recorded in shared space. 124 return; 125 } 126 if (!SystemDictionaryShared::add_unregistered_class(Thread::current(), (InstanceKlass*)k)) { 127 return; 128 } 129 } 130 131 if (cfs != nullptr && cfs->source() != nullptr) { 132 if (strcmp(cfs->source(), "_ClassSpecializer_generateConcreteSpeciesCode") == 0) { 133 return; 134 } 135 136 if (strncmp(cfs->source(), "__", 2) == 0) { 137 // generated class: __dynamic_proxy__, __JVM_LookupDefineClass__, etc 138 return; 139 } 140 } 141 142 { 143 InstanceKlass* super = k->java_super(); 144 if (super != nullptr && !has_id(super)) { 145 return; 146 } 147 148 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 149 int len = interfaces->length(); 150 for (int i = 0; i < len; i++) { 151 InstanceKlass* intf = interfaces->at(i); 152 if (!has_id(intf)) { 153 return; 154 } 155 } 156 } 157 158 if (k->is_hidden()) { 159 return; 160 } 161 162 if (k->module()->is_patched()) { 163 return; 164 } 165 166 ResourceMark rm; 167 stream->print("%s id: %d", k->name()->as_C_string(), get_id(k)); 168 if (!is_builtin_loader) { 169 InstanceKlass* super = k->java_super(); 170 assert(super != nullptr, "must be"); 171 stream->print(" super: %d", get_id(super)); 172 173 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 174 int len = interfaces->length(); 175 if (len > 0) { 176 stream->print(" interfaces:"); 177 for (int i = 0; i < len; i++) { 178 InstanceKlass* intf = interfaces->at(i); 179 stream->print(" %d", get_id(intf)); 180 } 181 } 182 183 // NB: the string following "source: " is not really a proper file name, but rather 184 // a truncated URI referring to a file. It must be decoded after reading. 185 #ifdef _WINDOWS 186 // "file:/C:/dir/foo.jar" -> "C:/dir/foo.jar" 187 stream->print(" source: %s", cfs->source() + 6); 188 #else 189 // "file:/dir/foo.jar" -> "/dir/foo.jar" 190 stream->print(" source: %s", cfs->source() + 5); 191 #endif 192 } 193 194 stream->cr(); 195 stream->flush(); 196 } 197 198 void ClassListWriter::delete_classlist() { 199 if (_classlist_file != nullptr) { 200 delete _classlist_file; 201 } 202 } 203 204 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure { 205 public: 206 void do_cld(ClassLoaderData* cld) { 207 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) { 208 if (klass->is_instance_klass()) { 209 InstanceKlass* ik = InstanceKlass::cast(klass); 210 write_resolved_constants_for(ik); 211 } 212 } 213 } 214 }; 215 216 void ClassListWriter::write_resolved_constants() { 217 if (!is_enabled()) { 218 return; 219 } 220 MutexLocker lock(ClassLoaderDataGraph_lock); 221 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag); 222 223 WriteResolveConstantsCLDClosure closure; 224 ClassLoaderDataGraph::loaded_cld_do(&closure); 225 } 226 227 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) { 228 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) || 229 ik->is_hidden()) { 230 return; 231 } 232 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) { 233 return; 234 } 235 if (ik->name()->equals("jdk/internal/module/SystemModules$all")) { 236 // This class is regenerated during JDK build process, so the classlist 237 // may not match the version that's in the real jdk image. 238 return; 239 } 240 241 if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders. 242 return; 243 } 244 245 ResourceMark rm; 246 ConstantPool* cp = ik->constants(); 247 GrowableArray<bool> list(cp->length(), cp->length(), false); 248 bool print = false; 249 250 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused 251 switch (cp->tag_at(cp_index).value()) { 252 case JVM_CONSTANT_Class: 253 { 254 Klass* k = cp->resolved_klass_at(cp_index); 255 if (k->is_instance_klass()) { 256 list.at_put(cp_index, true); 257 print = true; 258 } 259 } 260 break; 261 } 262 } 263 264 if (cp->cache() != nullptr) { 265 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries(); 266 if (indy_entries != nullptr) { 267 for (int i = 0; i < indy_entries->length(); i++) { 268 ResolvedIndyEntry* rie = indy_entries->adr_at(i); 269 int cp_index = rie->constant_pool_index(); 270 if (rie->is_resolved()) { 271 list.at_put(cp_index, true); 272 print = true; 273 } 274 } 275 } 276 277 Array<ResolvedFieldEntry>* field_entries = cp->cache()->resolved_field_entries(); 278 if (field_entries != nullptr) { 279 for (int i = 0; i < field_entries->length(); i++) { 280 ResolvedFieldEntry* rfe = field_entries->adr_at(i); 281 if (rfe->is_resolved(Bytecodes::_getfield) || 282 rfe->is_resolved(Bytecodes::_putfield)) { 283 list.at_put(rfe->constant_pool_index(), true); 284 print = true; 285 } 286 } 287 } 288 289 Array<ResolvedMethodEntry>* method_entries = cp->cache()->resolved_method_entries(); 290 if (method_entries != nullptr) { 291 for (int i = 0; i < method_entries->length(); i++) { 292 ResolvedMethodEntry* rme = method_entries->adr_at(i); 293 if (rme->is_resolved(Bytecodes::_invokevirtual) || 294 rme->is_resolved(Bytecodes::_invokespecial) || 295 rme->is_resolved(Bytecodes::_invokeinterface) || 296 rme->is_resolved(Bytecodes::_invokehandle)) { 297 list.at_put(rme->constant_pool_index(), true); 298 print = true; 299 } 300 } 301 } 302 } 303 304 if (print) { 305 outputStream* stream = _classlist_file; 306 stream->print("@cp %s", ik->name()->as_C_string()); 307 for (int i = 0; i < list.length(); i++) { 308 if (list.at(i)) { 309 constantTag cp_tag = cp->tag_at(i).value(); 310 assert(cp_tag.value() == JVM_CONSTANT_Class || 311 cp_tag.value() == JVM_CONSTANT_Fieldref || 312 cp_tag.value() == JVM_CONSTANT_Methodref|| 313 cp_tag.value() == JVM_CONSTANT_InterfaceMethodref || 314 cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity"); 315 stream->print(" %d", i); 316 } 317 } 318 stream->cr(); 319 } 320 }