1 /*
  2  * Copyright (c) 2015, 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/aotClassLocation.hpp"
 26 #include "cds/cds_globals.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "cds/dynamicArchive.hpp"
 29 #include "cds/filemap.hpp"
 30 #include "cds/heapShared.hpp"
 31 #include "classfile/classFileParser.hpp"
 32 #include "classfile/classLoader.inline.hpp"
 33 #include "classfile/classLoaderExt.hpp"
 34 #include "classfile/classLoaderData.inline.hpp"
 35 #include "classfile/classLoadInfo.hpp"
 36 #include "classfile/klassFactory.hpp"
 37 #include "classfile/modules.hpp"
 38 #include "classfile/systemDictionary.hpp"
 39 #include "classfile/vmSymbols.hpp"
 40 #include "gc/shared/collectedHeap.hpp"
 41 #include "logging/log.hpp"
 42 #include "memory/allocation.inline.hpp"
 43 #include "memory/resourceArea.hpp"
 44 #include "oops/instanceKlass.hpp"
 45 #include "oops/klass.inline.hpp"
 46 #include "oops/oop.inline.hpp"
 47 #include "oops/symbol.hpp"
 48 #include "runtime/arguments.hpp"
 49 #include "runtime/handles.inline.hpp"
 50 #include "runtime/java.hpp"
 51 #include "runtime/os.hpp"
 52 #include "utilities/checkedCast.hpp"
 53 #include "utilities/stringUtils.hpp"
 54 
 55 void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {
 56   if (CDSConfig::is_using_archive()) {
 57     warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");
 58     FileMapInfo::current_info()->set_has_platform_or_app_classes(false);
 59     if (DynamicArchive::is_mapped()) {
 60       FileMapInfo::dynamic_info()->set_has_platform_or_app_classes(false);
 61     }
 62   }
 63   ClassLoader::add_to_boot_append_entries(new_entry);
 64 }
 65 
 66 int ClassLoaderExt::compare_module_names(const char** p1, const char** p2) {
 67   return strcmp(*p1, *p2);
 68 }
 69 
 70 void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* result, bool redefined) {
 71   assert(CDSConfig::is_dumping_archive(), "sanity");
 72 
 73   // We need to remember where the class comes from during dumping.
 74   oop loader = result->class_loader();
 75   s2 classloader_type = ClassLoader::BOOT_LOADER;
 76   if (SystemDictionary::is_system_class_loader(loader)) {
 77     classloader_type = ClassLoader::APP_LOADER;
 78     AOTClassLocationConfig::dumptime_set_has_app_classes();
 79   } else if (SystemDictionary::is_platform_class_loader(loader)) {
 80     classloader_type = ClassLoader::PLATFORM_LOADER;
 81     AOTClassLocationConfig::dumptime_set_has_platform_classes();
 82   }
 83   AOTClassLocationConfig::dumptime_update_max_used_index(classpath_index);
 84   result->set_shared_classpath_index(classpath_index);
 85   result->set_shared_class_loader_type(classloader_type);
 86 #if INCLUDE_CDS_JAVA_HEAP
 87   if (CDSConfig::is_dumping_heap() && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER &&
 88       classpath_index < 0 && redefined) {
 89     // When dumping the heap (which happens only during static dump), classes for the built-in
 90     // loaders are always loaded from known locations (jimage, classpath or modulepath),
 91     // so classpath_index should always be >= 0.
 92     // The only exception is when a java agent is used during dump time (for testing
 93     // purposes only). If a class is transformed by the agent, the AOTClassLocation of
 94     // this class may point to an unknown location. This may break heap object archiving,
 95     // which requires all the boot classes to be from known locations. This is an
 96     // uncommon scenario (even in test cases). Let's simply disable heap object archiving.
 97     ResourceMark rm;
 98     log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.",
 99                      result->external_name());
100     CDSConfig::disable_heap_dumping();
101   }
102 #endif // INCLUDE_CDS_JAVA_HEAP
103 
104   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_dynamic_archive()) {
105     AOTClassLocationConfig::dumptime()->check_invalid_classpath_index(classpath_index, result);
106   }
107 }