< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

   1 /*
   2  * Copyright (c) 1997, 2023, 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  *

1084 
1085 // Called by the boot classloader to load classes
1086 InstanceKlass* ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
1087   assert(name != nullptr, "invariant");
1088 
1089   ResourceMark rm(THREAD);
1090   HandleMark hm(THREAD);
1091 
1092   const char* const class_name = name->as_C_string();
1093 
1094   EventMarkClassLoading m("Loading class %s", class_name);
1095 
1096   const char* const file_name = file_name_for_class_name(class_name,
1097                                                          name->utf8_length());
1098   assert(file_name != nullptr, "invariant");
1099 
1100   // Lookup stream for parsing .class file
1101   ClassFileStream* stream = nullptr;
1102   s2 classpath_index = 0;
1103   ClassPathEntry* e = nullptr;

1104 
1105   // If search_append_only is true, boot loader visibility boundaries are
1106   // set to be _first_append_entry to the end. This includes:
1107   //   [-Xbootclasspath/a]; [jvmti appended entries]
1108   //
1109   // If search_append_only is false, boot loader visibility boundaries are
1110   // set to be the --patch-module entries plus the base piece. This includes:
1111   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1112   //
1113 
1114   // Load Attempt #1: --patch-module
1115   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1116   // attempt to load the class from those locations specific to the module.
1117   // Specifications to --patch-module can contain a partial number of classes
1118   // that are part of the overall module definition.  So if a particular class is not
1119   // found within its module specification, the search should continue to Load Attempt #2.
1120   // Note: The --patch-module entries are never searched if the boot loader's
1121   //       visibility boundary is limited to only searching the append entries.
1122   if (_patch_mod_entries != nullptr && !search_append_only) {
1123     // At CDS dump time, the --patch-module entries are ignored. That means a
1124     // class is still loaded from the runtime image even if it might
1125     // appear in the _patch_mod_entries. The runtime shared class visibility
1126     // check will determine if a shared class is visible based on the runtime
1127     // environment, including the runtime --patch-module setting.
1128     //
1129     // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1130     // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1131     assert(!CDSConfig::is_dumping_dynamic_archive(), "sanity");
1132     if (!CDSConfig::is_dumping_static_archive()) {

1133       stream = search_module_entries(THREAD, _patch_mod_entries, class_name, file_name);



1134     }
1135   }
1136 
1137   // Load Attempt #2: [jimage | exploded build]
1138   if (!search_append_only && (nullptr == stream)) {
1139     if (has_jrt_entry()) {
1140       e = _jrt_entry;
1141       stream = _jrt_entry->open_stream(THREAD, file_name);
1142     } else {
1143       // Exploded build - attempt to locate class in its defining module's location.
1144       assert(_exploded_entries != nullptr, "No exploded build entries present");
1145       stream = search_module_entries(THREAD, _exploded_entries, class_name, file_name);
1146     }
1147   }
1148 
1149   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1150   if (search_append_only && (nullptr == stream)) {
1151     // For the boot loader append path search, the starting classpath_index
1152     // for the appended piece is always 1 to account for either the
1153     // _jrt_entry or the _exploded_entries.

1164       ++classpath_index;
1165     }
1166   }
1167 
1168   if (nullptr == stream) {
1169     return nullptr;
1170   }
1171 
1172   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1173 
1174   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1175   Handle protection_domain;
1176   ClassLoadInfo cl_info(protection_domain);
1177 
1178   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1179                                                            name,
1180                                                            loader_data,
1181                                                            cl_info,
1182                                                            CHECK_NULL);
1183   result->set_classpath_index(classpath_index);




1184   return result;
1185 }
1186 
1187 #if INCLUDE_CDS
1188 char* ClassLoader::skip_uri_protocol(char* source) {
1189   if (strncmp(source, "file:", 5) == 0) {
1190     // file: protocol path could start with file:/ or file:///
1191     // locate the char after all the forward slashes
1192     int offset = 5;
1193     while (*(source + offset) == '/') {
1194         offset++;
1195     }
1196     source += offset;
1197   // for non-windows platforms, move back one char as the path begins with a '/'
1198 #ifndef _WINDOWS
1199     source -= 1;
1200 #endif
1201   } else if (strncmp(source, "jrt:/", 5) == 0) {
1202     source += 5;
1203   }
1204   return source;
1205 }
1206 
1207 // Record the shared classpath index and loader type for classes loaded
1208 // by the builtin loaders at dump time.
1209 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1210                                 const ClassFileStream* stream, bool redefined) {
1211   assert(CDSConfig::is_dumping_archive(), "sanity");
1212   assert(stream != nullptr, "sanity");
1213 
1214   if (ik->is_hidden()) {
1215     // We do not archive hidden classes.
1216     return;
1217   }
1218 




1219   oop loader = ik->class_loader();
1220   char* src = (char*)stream->source();
1221   if (src == nullptr) {
1222     if (loader == nullptr) {
1223       // JFR classes
1224       ik->set_shared_classpath_index(0);
1225       ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1226     }
1227     return;
1228   }
1229 
1230   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1231 
1232   ResourceMark rm(current);
1233   int classpath_index = -1;
1234   PackageEntry* pkg_entry = ik->package();
1235 
1236   if (FileMapInfo::get_number_of_shared_paths() > 0) {
1237     // Save the path from the file: protocol or the module name from the jrt: protocol
1238     // if no protocol prefix is found, path is the same as stream->source(). This path

1286       // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1287       // The class must be from the runtime modules image.
1288       if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1289         classpath_index = i;
1290         break;
1291       }
1292     }
1293 
1294     // No path entry found for this class: most likely a shared class loaded by the
1295     // user defined classloader.
1296     if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1297       assert(ik->shared_classpath_index() < 0, "not assigned yet");
1298       ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1299       SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1300       return;
1301     }
1302   } else {
1303     // The shared path table is set up after module system initialization.
1304     // The path table contains no entry before that. Any classes loaded prior
1305     // to the setup of the shared path table must be from the modules image.
1306     assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");


1307     assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1308     classpath_index = 0;
1309   }
1310 
1311   const char* const class_name = ik->name()->as_C_string();
1312   const char* const file_name = file_name_for_class_name(class_name,
1313                                                          ik->name()->utf8_length());
1314   assert(file_name != nullptr, "invariant");
1315   ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1316 }
1317 #endif // INCLUDE_CDS
1318 
1319 // Initialize the class loader's access to methods in libzip.  Parse and
1320 // process the boot classpath into a list ClassPathEntry objects.  Once
1321 // this list has been created, it must not change order (see class PackageInfo)
1322 // it can be appended to and is by jvmti.
1323 
1324 void ClassLoader::initialize(TRAPS) {
1325   if (UsePerfData) {
1326     // jvmstat performance counters

   1 /*
   2  * Copyright (c) 1997, 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  *

1084 
1085 // Called by the boot classloader to load classes
1086 InstanceKlass* ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
1087   assert(name != nullptr, "invariant");
1088 
1089   ResourceMark rm(THREAD);
1090   HandleMark hm(THREAD);
1091 
1092   const char* const class_name = name->as_C_string();
1093 
1094   EventMarkClassLoading m("Loading class %s", class_name);
1095 
1096   const char* const file_name = file_name_for_class_name(class_name,
1097                                                          name->utf8_length());
1098   assert(file_name != nullptr, "invariant");
1099 
1100   // Lookup stream for parsing .class file
1101   ClassFileStream* stream = nullptr;
1102   s2 classpath_index = 0;
1103   ClassPathEntry* e = nullptr;
1104   bool is_patched = false;
1105 
1106   // If search_append_only is true, boot loader visibility boundaries are
1107   // set to be _first_append_entry to the end. This includes:
1108   //   [-Xbootclasspath/a]; [jvmti appended entries]
1109   //
1110   // If search_append_only is false, boot loader visibility boundaries are
1111   // set to be the --patch-module entries plus the base piece. This includes:
1112   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1113   //
1114 
1115   // Load Attempt #1: --patch-module
1116   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1117   // attempt to load the class from those locations specific to the module.
1118   // Specifications to --patch-module can contain a partial number of classes
1119   // that are part of the overall module definition.  So if a particular class is not
1120   // found within its module specification, the search should continue to Load Attempt #2.
1121   // Note: The --patch-module entries are never searched if the boot loader's
1122   //       visibility boundary is limited to only searching the append entries.
1123   if (_patch_mod_entries != nullptr && !search_append_only) {
1124     // At CDS dump time, the --patch-module entries are ignored. That means a
1125     // class is still loaded from the runtime image even if it might
1126     // appear in the _patch_mod_entries. The runtime shared class visibility
1127     // check will determine if a shared class is visible based on the runtime
1128     // environment, including the runtime --patch-module setting.
1129     if (!CDSConfig::is_valhalla_preview()) {
1130       // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1131       // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1132       assert(!CDSConfig::is_dumping_dynamic_archive(), "sanity");
1133     }
1134     if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1135       stream = search_module_entries(THREAD, _patch_mod_entries, class_name, file_name);
1136       if (stream != nullptr) {
1137         is_patched = true;
1138       }
1139     }
1140   }
1141 
1142   // Load Attempt #2: [jimage | exploded build]
1143   if (!search_append_only && (nullptr == stream)) {
1144     if (has_jrt_entry()) {
1145       e = _jrt_entry;
1146       stream = _jrt_entry->open_stream(THREAD, file_name);
1147     } else {
1148       // Exploded build - attempt to locate class in its defining module's location.
1149       assert(_exploded_entries != nullptr, "No exploded build entries present");
1150       stream = search_module_entries(THREAD, _exploded_entries, class_name, file_name);
1151     }
1152   }
1153 
1154   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1155   if (search_append_only && (nullptr == stream)) {
1156     // For the boot loader append path search, the starting classpath_index
1157     // for the appended piece is always 1 to account for either the
1158     // _jrt_entry or the _exploded_entries.

1169       ++classpath_index;
1170     }
1171   }
1172 
1173   if (nullptr == stream) {
1174     return nullptr;
1175   }
1176 
1177   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1178 
1179   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1180   Handle protection_domain;
1181   ClassLoadInfo cl_info(protection_domain);
1182 
1183   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1184                                                            name,
1185                                                            loader_data,
1186                                                            cl_info,
1187                                                            CHECK_NULL);
1188   result->set_classpath_index(classpath_index);
1189   if (is_patched) {
1190     result->set_shared_classpath_index(0);
1191     result->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1192   }
1193   return result;
1194 }
1195 
1196 #if INCLUDE_CDS
1197 char* ClassLoader::skip_uri_protocol(char* source) {
1198   if (strncmp(source, "file:", 5) == 0) {
1199     // file: protocol path could start with file:/ or file:///
1200     // locate the char after all the forward slashes
1201     int offset = 5;
1202     while (*(source + offset) == '/') {
1203         offset++;
1204     }
1205     source += offset;
1206   // for non-windows platforms, move back one char as the path begins with a '/'
1207 #ifndef _WINDOWS
1208     source -= 1;
1209 #endif
1210   } else if (strncmp(source, "jrt:/", 5) == 0) {
1211     source += 5;
1212   }
1213   return source;
1214 }
1215 
1216 // Record the shared classpath index and loader type for classes loaded
1217 // by the builtin loaders at dump time.
1218 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1219                                 const ClassFileStream* stream, bool redefined) {
1220   assert(CDSConfig::is_dumping_archive(), "sanity");
1221   assert(stream != nullptr, "sanity");
1222 
1223   if (ik->is_hidden()) {
1224     // We do not archive hidden classes.
1225     return;
1226   }
1227 
1228   if (ik->shared_classpath_index() == 0 && ik->is_shared_boot_class()) {
1229     return;
1230   }
1231 
1232   oop loader = ik->class_loader();
1233   char* src = (char*)stream->source();
1234   if (src == nullptr) {
1235     if (loader == nullptr) {
1236       // JFR classes
1237       ik->set_shared_classpath_index(0);
1238       ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1239     }
1240     return;
1241   }
1242 
1243   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1244 
1245   ResourceMark rm(current);
1246   int classpath_index = -1;
1247   PackageEntry* pkg_entry = ik->package();
1248 
1249   if (FileMapInfo::get_number_of_shared_paths() > 0) {
1250     // Save the path from the file: protocol or the module name from the jrt: protocol
1251     // if no protocol prefix is found, path is the same as stream->source(). This path

1299       // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1300       // The class must be from the runtime modules image.
1301       if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1302         classpath_index = i;
1303         break;
1304       }
1305     }
1306 
1307     // No path entry found for this class: most likely a shared class loaded by the
1308     // user defined classloader.
1309     if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1310       assert(ik->shared_classpath_index() < 0, "not assigned yet");
1311       ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1312       SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1313       return;
1314     }
1315   } else {
1316     // The shared path table is set up after module system initialization.
1317     // The path table contains no entry before that. Any classes loaded prior
1318     // to the setup of the shared path table must be from the modules image.
1319     if (!CDSConfig::is_valhalla_preview()) {
1320       assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");
1321     }
1322     assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1323     classpath_index = 0;
1324   }
1325 
1326   const char* const class_name = ik->name()->as_C_string();
1327   const char* const file_name = file_name_for_class_name(class_name,
1328                                                          ik->name()->utf8_length());
1329   assert(file_name != nullptr, "invariant");
1330   ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1331 }
1332 #endif // INCLUDE_CDS
1333 
1334 // Initialize the class loader's access to methods in libzip.  Parse and
1335 // process the boot classpath into a list ClassPathEntry objects.  Once
1336 // this list has been created, it must not change order (see class PackageInfo)
1337 // it can be appended to and is by jvmti.
1338 
1339 void ClassLoader::initialize(TRAPS) {
1340   if (UsePerfData) {
1341     // jvmstat performance counters
< prev index next >