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