< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page




1140     assert(k != NULL, "Must have an instance klass here!");
1141     loader_data->add_to_deallocate_list(k);
1142     return NULL;
1143   }
1144 
1145   // Make sure we have an entry in the SystemDictionary on success
1146   debug_only( {
1147     MutexLocker mu(THREAD, SystemDictionary_lock);
1148 
1149     Klass* check = find_class(h_name, k->class_loader_data());
1150     assert(check == k, "should be present in the dictionary");
1151   } );
1152 
1153   return k;
1154 }
1155 
1156 #if INCLUDE_CDS
1157 // Load a class for boot loader from the shared spaces. This also
1158 // forces the super class and all interfaces to be loaded.
1159 InstanceKlass* SystemDictionary::load_shared_boot_class(Symbol* class_name,

1160                                                         TRAPS) {
1161   InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1162   if (ik != NULL && ik->is_shared_boot_class()) {
1163     return load_shared_class(ik, Handle(), Handle(), NULL, THREAD);
1164   }
1165   return NULL;
1166 }
1167 
1168 // Check if a shared class can be loaded by the specific classloader:
1169 //
1170 // NULL classloader:
1171 //   - Module class from "modules" jimage. ModuleEntry must be defined in the classloader.
1172 //   - Class from -Xbootclasspath/a. The class has no defined PackageEntry, or must
1173 //     be defined in an unnamed module.
1174 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
1175                                                InstanceKlass* ik,

1176                                                Handle class_loader, TRAPS) {
1177   assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
1178          "Cannot use sharing if java.base is patched");
1179   ResourceMark rm(THREAD);
1180   int path_index = ik->shared_classpath_index();
1181   ClassLoaderData* loader_data = class_loader_data(class_loader);
1182   if (path_index < 0) {
1183     // path_index < 0 indicates that the class is intended for a custom loader
1184     // and should not be loaded by boot/platform/app loaders
1185     if (loader_data->is_builtin_class_loader_data()) {
1186       return false;
1187     } else {
1188       return true;
1189     }
1190   }
1191   SharedClassPathEntry* ent =
1192             (SharedClassPathEntry*)FileMapInfo::shared_path(path_index);
1193   if (!Universe::is_module_initialized()) {
1194     assert(ent != NULL && ent->is_modules_image(),
1195            "Loading non-bootstrap classes before the module system is initialized");
1196     assert(class_loader.is_null(), "sanity");
1197     return true;
1198   }
1199   // Get the pkg_entry from the classloader
1200   PackageEntry* pkg_entry = NULL;
1201   ModuleEntry* mod_entry = NULL;
1202   TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);

1203   if (pkg_name != NULL) {
1204     if (loader_data != NULL) {
1205       pkg_entry = loader_data->packages()->lookup_only(pkg_name);
1206       if (pkg_entry != NULL) {
1207         mod_entry = pkg_entry->module();
1208         // If the archived class is from a module that has been patched at runtime,
1209         // the class cannot be loaded from the archive.
1210         if (mod_entry != NULL && mod_entry->is_patched()) {
1211           return false;
1212         }
1213       }
1214     }
1215   }
1216 
1217   if (class_loader.is_null()) {
1218     assert(ent != NULL, "Shared class for NULL classloader must have valid SharedClassPathEntry");
1219     // The NULL classloader can load archived class originated from the
1220     // "modules" jimage and the -Xbootclasspath/a. For class from the
1221     // "modules" jimage, the PackageEntry/ModuleEntry must be defined
1222     // by the NULL classloader.
1223     if (mod_entry != NULL) {
1224       // PackageEntry/ModuleEntry is found in the classloader. Check if the
1225       // ModuleEntry's location agrees with the archived class' origination.


1282       !check_shared_class_super_type(ik, InstanceKlass::cast(ik->super()),
1283                                      class_loader, protection_domain, true, THREAD)) {
1284     return false;
1285   }
1286 
1287   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1288   int num_interfaces = interfaces->length();
1289   for (int index = 0; index < num_interfaces; index++) {
1290     if (!check_shared_class_super_type(ik, interfaces->at(index), class_loader, protection_domain, false, THREAD)) {
1291       return false;
1292     }
1293   }
1294 
1295   return true;
1296 }
1297 
1298 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1299                                                    Handle class_loader,
1300                                                    Handle protection_domain,
1301                                                    const ClassFileStream *cfs,

1302                                                    TRAPS) {
1303   assert(ik != NULL, "sanity");
1304   assert(!ik->is_unshareable_info_restored(), "shared class can be loaded only once");
1305   Symbol* class_name = ik->name();
1306 
1307   bool visible = is_shared_class_visible(
1308                           class_name, ik, class_loader, CHECK_NULL);
1309   if (!visible) {
1310     return NULL;
1311   }
1312 
1313   if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1314     return NULL;
1315   }
1316 
1317   InstanceKlass* new_ik = KlassFactory::check_shared_class_file_load_hook(
1318       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1319   if (new_ik != NULL) {
1320     // The class is changed by CFLH. Return the new class. The shared class is
1321     // not used.
1322     return new_ik;
1323   }
1324 
1325   // Adjust methods to recover missing data.  They need addresses for
1326   // interpreter entry points and their default native method address
1327   // must be reset.
1328 
1329   // Updating methods must be done under a lock so multiple
1330   // threads don't update these in parallel
1331   //
1332   // Shared classes are all currently loaded by either the bootstrap or
1333   // internal parallel class loaders, so this will never cause a deadlock
1334   // on a custom class loader lock.
1335 
1336   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
1337   {
1338     HandleMark hm(THREAD);
1339     Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1340     check_loader_lock_contention(lockObject, THREAD);
1341     ObjectLocker ol(lockObject, THREAD, true);
1342     // prohibited package check assumes all classes loaded from archive call
1343     // restore_unshareable_info which calls ik->set_package()
1344     ik->restore_unshareable_info(loader_data, protection_domain, CHECK_NULL);
1345   }
1346 
1347   load_shared_class_misc(ik, loader_data, CHECK_NULL);
1348   return ik;
1349 }
1350 
1351 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS) {
1352   ik->print_class_load_logging(loader_data, NULL, NULL);
1353 
1354   // For boot loader, ensure that GetSystemPackage knows that a class in this
1355   // package was loaded.
1356   if (loader_data->is_the_null_class_loader_data()) {
1357     int path_index = ik->shared_classpath_index();
1358     ClassLoader::add_package(ik, path_index, THREAD);
1359   }
1360 
1361   if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
1362     // Only dump the classes that can be stored into CDS archive
1363     if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
1364       ResourceMark rm(THREAD);


1391   assert(klass->is_shared(), "Must be shared class");
1392   if (klass->class_loader_data() != NULL) {
1393     return;
1394   }
1395 
1396   // add super and interfaces first
1397   Klass* super = klass->super();
1398   if (super != NULL && super->class_loader_data() == NULL) {
1399     assert(super->is_instance_klass(), "Super should be instance klass");
1400     quick_resolve(InstanceKlass::cast(super), loader_data, domain, CHECK);
1401   }
1402 
1403   Array<InstanceKlass*>* ifs = klass->local_interfaces();
1404   for (int i = 0; i < ifs->length(); i++) {
1405     InstanceKlass* ik = ifs->at(i);
1406     if (ik->class_loader_data()  == NULL) {
1407       quick_resolve(ik, loader_data, domain, CHECK);
1408     }
1409   }
1410 
1411   klass->restore_unshareable_info(loader_data, domain, THREAD);
1412   load_shared_class_misc(klass, loader_data, CHECK);
1413   Dictionary* dictionary = loader_data->dictionary();
1414   unsigned int hash = dictionary->compute_hash(klass->name());
1415   dictionary->add_klass(hash, klass->name(), klass);
1416   add_to_hierarchy(klass, CHECK);
1417   assert(klass->is_loaded(), "Must be in at least loaded state");
1418 }
1419 #endif // INCLUDE_CDS
1420 
1421 InstanceKlass* SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
1422 
1423   if (class_loader.is_null()) {
1424     ResourceMark rm(THREAD);
1425     PackageEntry* pkg_entry = NULL;
1426     bool search_only_bootloader_append = false;
1427     ClassLoaderData *loader_data = class_loader_data(class_loader);
1428 
1429     // Find the package in the boot loader's package entry table.
1430     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1431     if (pkg_name != NULL) {


1471         if (!ClassLoader::has_bootclasspath_append()) {
1472            // If there is no bootclasspath append entry, no need to continue
1473            // searching.
1474            return NULL;
1475         }
1476         search_only_bootloader_append = true;
1477       }
1478     }
1479 
1480     // Prior to bootstrapping's module initialization, never load a class outside
1481     // of the boot loader's module path
1482     assert(Universe::is_module_initialized() ||
1483            !search_only_bootloader_append,
1484            "Attempt to load a class outside of boot loader's module path");
1485 
1486     // Search for classes in the CDS archive.
1487     InstanceKlass* k = NULL;
1488     {
1489 #if INCLUDE_CDS
1490       PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1491       k = load_shared_boot_class(class_name, THREAD);
1492 #endif
1493     }
1494 
1495     if (k == NULL) {
1496       // Use VM class loader
1497       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1498       k = ClassLoader::load_class(class_name, search_only_bootloader_append, CHECK_NULL);
1499     }
1500 
1501     // find_or_define_instance_class may return a different InstanceKlass
1502     if (k != NULL) {
1503       InstanceKlass* defined_k =
1504         find_or_define_instance_class(class_name, class_loader, k, THREAD);
1505       if (!HAS_PENDING_EXCEPTION && defined_k != k) {
1506         // If a parallel capable class loader already defined this class, register 'k' for cleanup.
1507         assert(defined_k != NULL, "Should have a klass if there's no exception");
1508         loader_data->add_to_deallocate_list(k);
1509         k = defined_k;
1510       } else if (HAS_PENDING_EXCEPTION) {
1511         loader_data->add_to_deallocate_list(k);




1140     assert(k != NULL, "Must have an instance klass here!");
1141     loader_data->add_to_deallocate_list(k);
1142     return NULL;
1143   }
1144 
1145   // Make sure we have an entry in the SystemDictionary on success
1146   debug_only( {
1147     MutexLocker mu(THREAD, SystemDictionary_lock);
1148 
1149     Klass* check = find_class(h_name, k->class_loader_data());
1150     assert(check == k, "should be present in the dictionary");
1151   } );
1152 
1153   return k;
1154 }
1155 
1156 #if INCLUDE_CDS
1157 // Load a class for boot loader from the shared spaces. This also
1158 // forces the super class and all interfaces to be loaded.
1159 InstanceKlass* SystemDictionary::load_shared_boot_class(Symbol* class_name,
1160                                                         PackageEntry* pkg_entry,
1161                                                         TRAPS) {
1162   InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1163   if (ik != NULL && ik->is_shared_boot_class()) {
1164     return load_shared_class(ik, Handle(), Handle(), NULL, pkg_entry, THREAD);
1165   }
1166   return NULL;
1167 }
1168 
1169 // Check if a shared class can be loaded by the specific classloader:
1170 //
1171 // NULL classloader:
1172 //   - Module class from "modules" jimage. ModuleEntry must be defined in the classloader.
1173 //   - Class from -Xbootclasspath/a. The class has no defined PackageEntry, or must
1174 //     be defined in an unnamed module.
1175 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
1176                                                InstanceKlass* ik,
1177                                                PackageEntry* pkg_entry,
1178                                                Handle class_loader, TRAPS) {
1179   assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
1180          "Cannot use sharing if java.base is patched");
1181   ResourceMark rm(THREAD);
1182   int path_index = ik->shared_classpath_index();
1183   ClassLoaderData* loader_data = class_loader_data(class_loader);
1184   if (path_index < 0) {
1185     // path_index < 0 indicates that the class is intended for a custom loader
1186     // and should not be loaded by boot/platform/app loaders
1187     if (loader_data->is_builtin_class_loader_data()) {
1188       return false;
1189     } else {
1190       return true;
1191     }
1192   }
1193   SharedClassPathEntry* ent =
1194             (SharedClassPathEntry*)FileMapInfo::shared_path(path_index);
1195   if (!Universe::is_module_initialized()) {
1196     assert(ent != NULL && ent->is_modules_image(),
1197            "Loading non-bootstrap classes before the module system is initialized");
1198     assert(class_loader.is_null(), "sanity");
1199     return true;
1200   }
1201   // Get the pkg_entry from the classloader

1202   ModuleEntry* mod_entry = NULL;
1203   TempNewSymbol pkg_name = pkg_entry != NULL ? pkg_entry->name() :
1204                                                ClassLoader::package_from_class_name(class_name);
1205   if (pkg_name != NULL) {
1206     if (loader_data != NULL) {

1207       if (pkg_entry != NULL) {
1208         mod_entry = pkg_entry->module();
1209         // If the archived class is from a module that has been patched at runtime,
1210         // the class cannot be loaded from the archive.
1211         if (mod_entry != NULL && mod_entry->is_patched()) {
1212           return false;
1213         }
1214       }
1215     }
1216   }
1217 
1218   if (class_loader.is_null()) {
1219     assert(ent != NULL, "Shared class for NULL classloader must have valid SharedClassPathEntry");
1220     // The NULL classloader can load archived class originated from the
1221     // "modules" jimage and the -Xbootclasspath/a. For class from the
1222     // "modules" jimage, the PackageEntry/ModuleEntry must be defined
1223     // by the NULL classloader.
1224     if (mod_entry != NULL) {
1225       // PackageEntry/ModuleEntry is found in the classloader. Check if the
1226       // ModuleEntry's location agrees with the archived class' origination.


1283       !check_shared_class_super_type(ik, InstanceKlass::cast(ik->super()),
1284                                      class_loader, protection_domain, true, THREAD)) {
1285     return false;
1286   }
1287 
1288   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1289   int num_interfaces = interfaces->length();
1290   for (int index = 0; index < num_interfaces; index++) {
1291     if (!check_shared_class_super_type(ik, interfaces->at(index), class_loader, protection_domain, false, THREAD)) {
1292       return false;
1293     }
1294   }
1295 
1296   return true;
1297 }
1298 
1299 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1300                                                    Handle class_loader,
1301                                                    Handle protection_domain,
1302                                                    const ClassFileStream *cfs,
1303                                                    PackageEntry* pkg_entry,
1304                                                    TRAPS) {
1305   assert(ik != NULL, "sanity");
1306   assert(!ik->is_unshareable_info_restored(), "shared class can be loaded only once");
1307   Symbol* class_name = ik->name();
1308 
1309   bool visible = is_shared_class_visible(
1310                           class_name, ik, pkg_entry, class_loader, CHECK_NULL);
1311   if (!visible) {
1312     return NULL;
1313   }
1314 
1315   if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1316     return NULL;
1317   }
1318 
1319   InstanceKlass* new_ik = KlassFactory::check_shared_class_file_load_hook(
1320       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1321   if (new_ik != NULL) {
1322     // The class is changed by CFLH. Return the new class. The shared class is
1323     // not used.
1324     return new_ik;
1325   }
1326 
1327   // Adjust methods to recover missing data.  They need addresses for
1328   // interpreter entry points and their default native method address
1329   // must be reset.
1330 
1331   // Updating methods must be done under a lock so multiple
1332   // threads don't update these in parallel
1333   //
1334   // Shared classes are all currently loaded by either the bootstrap or
1335   // internal parallel class loaders, so this will never cause a deadlock
1336   // on a custom class loader lock.
1337 
1338   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
1339   {
1340     HandleMark hm(THREAD);
1341     Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1342     check_loader_lock_contention(lockObject, THREAD);
1343     ObjectLocker ol(lockObject, THREAD, true);
1344     // prohibited package check assumes all classes loaded from archive call
1345     // restore_unshareable_info which calls ik->set_package()
1346     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1347   }
1348 
1349   load_shared_class_misc(ik, loader_data, CHECK_NULL);
1350   return ik;
1351 }
1352 
1353 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS) {
1354   ik->print_class_load_logging(loader_data, NULL, NULL);
1355 
1356   // For boot loader, ensure that GetSystemPackage knows that a class in this
1357   // package was loaded.
1358   if (loader_data->is_the_null_class_loader_data()) {
1359     int path_index = ik->shared_classpath_index();
1360     ClassLoader::add_package(ik, path_index, THREAD);
1361   }
1362 
1363   if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
1364     // Only dump the classes that can be stored into CDS archive
1365     if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
1366       ResourceMark rm(THREAD);


1393   assert(klass->is_shared(), "Must be shared class");
1394   if (klass->class_loader_data() != NULL) {
1395     return;
1396   }
1397 
1398   // add super and interfaces first
1399   Klass* super = klass->super();
1400   if (super != NULL && super->class_loader_data() == NULL) {
1401     assert(super->is_instance_klass(), "Super should be instance klass");
1402     quick_resolve(InstanceKlass::cast(super), loader_data, domain, CHECK);
1403   }
1404 
1405   Array<InstanceKlass*>* ifs = klass->local_interfaces();
1406   for (int i = 0; i < ifs->length(); i++) {
1407     InstanceKlass* ik = ifs->at(i);
1408     if (ik->class_loader_data()  == NULL) {
1409       quick_resolve(ik, loader_data, domain, CHECK);
1410     }
1411   }
1412 
1413   klass->restore_unshareable_info(loader_data, domain, NULL, THREAD);
1414   load_shared_class_misc(klass, loader_data, CHECK);
1415   Dictionary* dictionary = loader_data->dictionary();
1416   unsigned int hash = dictionary->compute_hash(klass->name());
1417   dictionary->add_klass(hash, klass->name(), klass);
1418   add_to_hierarchy(klass, CHECK);
1419   assert(klass->is_loaded(), "Must be in at least loaded state");
1420 }
1421 #endif // INCLUDE_CDS
1422 
1423 InstanceKlass* SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
1424 
1425   if (class_loader.is_null()) {
1426     ResourceMark rm(THREAD);
1427     PackageEntry* pkg_entry = NULL;
1428     bool search_only_bootloader_append = false;
1429     ClassLoaderData *loader_data = class_loader_data(class_loader);
1430 
1431     // Find the package in the boot loader's package entry table.
1432     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1433     if (pkg_name != NULL) {


1473         if (!ClassLoader::has_bootclasspath_append()) {
1474            // If there is no bootclasspath append entry, no need to continue
1475            // searching.
1476            return NULL;
1477         }
1478         search_only_bootloader_append = true;
1479       }
1480     }
1481 
1482     // Prior to bootstrapping's module initialization, never load a class outside
1483     // of the boot loader's module path
1484     assert(Universe::is_module_initialized() ||
1485            !search_only_bootloader_append,
1486            "Attempt to load a class outside of boot loader's module path");
1487 
1488     // Search for classes in the CDS archive.
1489     InstanceKlass* k = NULL;
1490     {
1491 #if INCLUDE_CDS
1492       PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1493       k = load_shared_boot_class(class_name, pkg_entry, THREAD);
1494 #endif
1495     }
1496 
1497     if (k == NULL) {
1498       // Use VM class loader
1499       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1500       k = ClassLoader::load_class(class_name, search_only_bootloader_append, CHECK_NULL);
1501     }
1502 
1503     // find_or_define_instance_class may return a different InstanceKlass
1504     if (k != NULL) {
1505       InstanceKlass* defined_k =
1506         find_or_define_instance_class(class_name, class_loader, k, THREAD);
1507       if (!HAS_PENDING_EXCEPTION && defined_k != k) {
1508         // If a parallel capable class loader already defined this class, register 'k' for cleanup.
1509         assert(defined_k != NULL, "Should have a klass if there's no exception");
1510         loader_data->add_to_deallocate_list(k);
1511         k = defined_k;
1512       } else if (HAS_PENDING_EXCEPTION) {
1513         loader_data->add_to_deallocate_list(k);


< prev index next >