< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

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

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

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



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

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




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




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

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


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

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

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

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