< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

1116 
1117 // Called by the boot classloader to load classes
1118 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1119   assert(name != nullptr, "invariant");
1120 
1121   ResourceMark rm(THREAD);
1122   HandleMark hm(THREAD);
1123 
1124   const char* const class_name = name->as_C_string();
1125 
1126   EventMarkClassLoading m("Loading class %s", class_name);
1127 
1128   const char* const file_name = file_name_for_class_name(class_name,
1129                                                          name->utf8_length());
1130   assert(file_name != nullptr, "invariant");
1131 
1132   // Lookup stream for parsing .class file
1133   ClassFileStream* stream = nullptr;
1134   s2 classpath_index = 0;
1135   ClassPathEntry* e = nullptr;

1136 
1137   // If search_append_only is true, boot loader visibility boundaries are
1138   // set to be _first_append_entry to the end. This includes:
1139   //   [-Xbootclasspath/a]; [jvmti appended entries]
1140   //
1141   // If search_append_only is false, boot loader visibility boundaries are
1142   // set to be the --patch-module entries plus the base piece. This includes:
1143   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1144   //
1145 
1146   // Load Attempt #1: --patch-module
1147   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1148   // attempt to load the class from those locations specific to the module.
1149   // Specifications to --patch-module can contain a partial number of classes
1150   // that are part of the overall module definition.  So if a particular class is not
1151   // found within its module specification, the search should continue to Load Attempt #2.
1152   // Note: The --patch-module entries are never searched if the boot loader's
1153   //       visibility boundary is limited to only searching the append entries.
1154   if (_patch_mod_entries != nullptr && !search_append_only) {
1155     assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1156     stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);














1157   }
1158 
1159   // Load Attempt #2: [jimage | exploded build]
1160   if (!search_append_only && (nullptr == stream)) {
1161     if (has_jrt_entry()) {
1162       e = _jrt_entry;
1163       stream = _jrt_entry->open_stream(THREAD, file_name);
1164     } else {
1165       // Exploded build - attempt to locate class in its defining module's location.
1166       assert(_exploded_entries != nullptr, "No exploded build entries present");
1167       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1168       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1169     }
1170   }
1171 
1172   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1173   if (search_append_only && (nullptr == stream)) {
1174     // For the boot loader append path search, the starting classpath_index
1175     // for the appended piece is always 1 to account for either the
1176     // _jrt_entry or the _exploded_entries.

1187       ++classpath_index;
1188     }
1189   }
1190 
1191   if (nullptr == stream) {
1192     return nullptr;
1193   }
1194 
1195   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1196 
1197   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1198   Handle protection_domain;
1199   ClassLoadInfo cl_info(protection_domain);
1200 
1201   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1202                                                            name,
1203                                                            loader_data,
1204                                                            cl_info,
1205                                                            CHECK_NULL);
1206   result->set_classpath_index(classpath_index);




1207   return result;
1208 }
1209 
1210 #if INCLUDE_CDS
1211 char* ClassLoader::skip_uri_protocol(char* source) {
1212   if (strncmp(source, "file:", 5) == 0) {
1213     // file: protocol path could start with file:/ or file:///
1214     // locate the char after all the forward slashes
1215     int offset = 5;
1216     while (*(source + offset) == '/') {
1217         offset++;
1218     }
1219     source += offset;
1220   // for non-windows platforms, move back one char as the path begins with a '/'
1221 #ifndef _WINDOWS
1222     source -= 1;
1223 #endif
1224   } else if (strncmp(source, "jrt:/", 5) == 0) {
1225     source += 5;
1226   }
1227   return source;
1228 }
1229 
1230 // Record the shared classpath index and loader type for classes loaded
1231 // by the builtin loaders at dump time.
1232 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1233                                 const ClassFileStream* stream, bool redefined) {
1234   assert(CDSConfig::is_dumping_archive(), "sanity");
1235   assert(stream != nullptr, "sanity");
1236 
1237   if (ik->is_hidden()) {
1238     // We do not archive hidden classes.
1239     return;
1240   }
1241 




1242   oop loader = ik->class_loader();
1243   char* src = (char*)stream->source();
1244   if (src == nullptr) {
1245     if (loader == nullptr) {
1246       // JFR classes
1247       ik->set_shared_classpath_index(0);
1248       ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1249     }
1250     return;
1251   }
1252 
1253   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1254 
1255   ResourceMark rm(current);
1256   int classpath_index = -1;
1257   PackageEntry* pkg_entry = ik->package();
1258 
1259   if (FileMapInfo::get_number_of_shared_paths() > 0) {
1260     // Save the path from the file: protocol or the module name from the jrt: protocol
1261     // if no protocol prefix is found, path is the same as stream->source(). This path

1309       // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1310       // The class must be from the runtime modules image.
1311       if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1312         classpath_index = i;
1313         break;
1314       }
1315     }
1316 
1317     // No path entry found for this class: most likely a shared class loaded by the
1318     // user defined classloader.
1319     if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1320       assert(ik->shared_classpath_index() < 0, "not assigned yet");
1321       ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1322       SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1323       return;
1324     }
1325   } else {
1326     // The shared path table is set up after module system initialization.
1327     // The path table contains no entry before that. Any classes loaded prior
1328     // to the setup of the shared path table must be from the modules image.
1329     assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");


1330     assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1331     classpath_index = 0;
1332   }
1333 
1334   const char* const class_name = ik->name()->as_C_string();
1335   const char* const file_name = file_name_for_class_name(class_name,
1336                                                          ik->name()->utf8_length());
1337   assert(file_name != nullptr, "invariant");
1338   ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1339 }
1340 #endif // INCLUDE_CDS
1341 
1342 // Initialize the class loader's access to methods in libzip.  Parse and
1343 // process the boot classpath into a list ClassPathEntry objects.  Once
1344 // this list has been created, it must not change order (see class PackageInfo)
1345 // it can be appended to and is by jvmti.
1346 
1347 void ClassLoader::initialize(TRAPS) {
1348   if (UsePerfData) {
1349     // jvmstat performance counters

1116 
1117 // Called by the boot classloader to load classes
1118 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1119   assert(name != nullptr, "invariant");
1120 
1121   ResourceMark rm(THREAD);
1122   HandleMark hm(THREAD);
1123 
1124   const char* const class_name = name->as_C_string();
1125 
1126   EventMarkClassLoading m("Loading class %s", class_name);
1127 
1128   const char* const file_name = file_name_for_class_name(class_name,
1129                                                          name->utf8_length());
1130   assert(file_name != nullptr, "invariant");
1131 
1132   // Lookup stream for parsing .class file
1133   ClassFileStream* stream = nullptr;
1134   s2 classpath_index = 0;
1135   ClassPathEntry* e = nullptr;
1136   bool is_patched = false;
1137 
1138   // If search_append_only is true, boot loader visibility boundaries are
1139   // set to be _first_append_entry to the end. This includes:
1140   //   [-Xbootclasspath/a]; [jvmti appended entries]
1141   //
1142   // If search_append_only is false, boot loader visibility boundaries are
1143   // set to be the --patch-module entries plus the base piece. This includes:
1144   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1145   //
1146 
1147   // Load Attempt #1: --patch-module
1148   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1149   // attempt to load the class from those locations specific to the module.
1150   // Specifications to --patch-module can contain a partial number of classes
1151   // that are part of the overall module definition.  So if a particular class is not
1152   // found within its module specification, the search should continue to Load Attempt #2.
1153   // Note: The --patch-module entries are never searched if the boot loader's
1154   //       visibility boundary is limited to only searching the append entries.
1155   if (_patch_mod_entries != nullptr && !search_append_only) {
1156     // At CDS dump time, the --patch-module entries are ignored. That means a
1157     // class is still loaded from the runtime image even if it might
1158     // appear in the _patch_mod_entries. The runtime shared class visibility
1159     // check will determine if a shared class is visible based on the runtime
1160     // environment, including the runtime --patch-module setting.
1161     if (!CDSConfig::is_valhalla_preview()) {
1162       // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1163       // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1164       assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1165     }
1166     if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1167       stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1168       if (stream != nullptr) {
1169         is_patched = true;
1170       }
1171     }
1172   }
1173 
1174   // Load Attempt #2: [jimage | exploded build]
1175   if (!search_append_only && (nullptr == stream)) {
1176     if (has_jrt_entry()) {
1177       e = _jrt_entry;
1178       stream = _jrt_entry->open_stream(THREAD, file_name);
1179     } else {
1180       // Exploded build - attempt to locate class in its defining module's location.
1181       assert(_exploded_entries != nullptr, "No exploded build entries present");
1182       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1183       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1184     }
1185   }
1186 
1187   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1188   if (search_append_only && (nullptr == stream)) {
1189     // For the boot loader append path search, the starting classpath_index
1190     // for the appended piece is always 1 to account for either the
1191     // _jrt_entry or the _exploded_entries.

1202       ++classpath_index;
1203     }
1204   }
1205 
1206   if (nullptr == stream) {
1207     return nullptr;
1208   }
1209 
1210   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1211 
1212   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1213   Handle protection_domain;
1214   ClassLoadInfo cl_info(protection_domain);
1215 
1216   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1217                                                            name,
1218                                                            loader_data,
1219                                                            cl_info,
1220                                                            CHECK_NULL);
1221   result->set_classpath_index(classpath_index);
1222   if (is_patched) {
1223     result->set_shared_classpath_index(0);
1224     result->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1225   }
1226   return result;
1227 }
1228 
1229 #if INCLUDE_CDS
1230 char* ClassLoader::skip_uri_protocol(char* source) {
1231   if (strncmp(source, "file:", 5) == 0) {
1232     // file: protocol path could start with file:/ or file:///
1233     // locate the char after all the forward slashes
1234     int offset = 5;
1235     while (*(source + offset) == '/') {
1236         offset++;
1237     }
1238     source += offset;
1239   // for non-windows platforms, move back one char as the path begins with a '/'
1240 #ifndef _WINDOWS
1241     source -= 1;
1242 #endif
1243   } else if (strncmp(source, "jrt:/", 5) == 0) {
1244     source += 5;
1245   }
1246   return source;
1247 }
1248 
1249 // Record the shared classpath index and loader type for classes loaded
1250 // by the builtin loaders at dump time.
1251 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1252                                 const ClassFileStream* stream, bool redefined) {
1253   assert(CDSConfig::is_dumping_archive(), "sanity");
1254   assert(stream != nullptr, "sanity");
1255 
1256   if (ik->is_hidden()) {
1257     // We do not archive hidden classes.
1258     return;
1259   }
1260 
1261   if (ik->shared_classpath_index() == 0 && ik->is_shared_boot_class()) {
1262     return;
1263   }
1264 
1265   oop loader = ik->class_loader();
1266   char* src = (char*)stream->source();
1267   if (src == nullptr) {
1268     if (loader == nullptr) {
1269       // JFR classes
1270       ik->set_shared_classpath_index(0);
1271       ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1272     }
1273     return;
1274   }
1275 
1276   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1277 
1278   ResourceMark rm(current);
1279   int classpath_index = -1;
1280   PackageEntry* pkg_entry = ik->package();
1281 
1282   if (FileMapInfo::get_number_of_shared_paths() > 0) {
1283     // Save the path from the file: protocol or the module name from the jrt: protocol
1284     // if no protocol prefix is found, path is the same as stream->source(). This path

1332       // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1333       // The class must be from the runtime modules image.
1334       if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1335         classpath_index = i;
1336         break;
1337       }
1338     }
1339 
1340     // No path entry found for this class: most likely a shared class loaded by the
1341     // user defined classloader.
1342     if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1343       assert(ik->shared_classpath_index() < 0, "not assigned yet");
1344       ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1345       SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1346       return;
1347     }
1348   } else {
1349     // The shared path table is set up after module system initialization.
1350     // The path table contains no entry before that. Any classes loaded prior
1351     // to the setup of the shared path table must be from the modules image.
1352     if (!CDSConfig::is_valhalla_preview()) {
1353       assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");
1354     }
1355     assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1356     classpath_index = 0;
1357   }
1358 
1359   const char* const class_name = ik->name()->as_C_string();
1360   const char* const file_name = file_name_for_class_name(class_name,
1361                                                          ik->name()->utf8_length());
1362   assert(file_name != nullptr, "invariant");
1363   ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1364 }
1365 #endif // INCLUDE_CDS
1366 
1367 // Initialize the class loader's access to methods in libzip.  Parse and
1368 // process the boot classpath into a list ClassPathEntry objects.  Once
1369 // this list has been created, it must not change order (see class PackageInfo)
1370 // it can be appended to and is by jvmti.
1371 
1372 void ClassLoader::initialize(TRAPS) {
1373   if (UsePerfData) {
1374     // jvmstat performance counters
< prev index next >