686 // This logic has the current thread wait once it has done
687 // all the superclass/superinterface loading it can, until
688 // the original thread completes the class loading or fails
689 // If it completes we will use the resulting InstanceKlass
690 // which we will find below in the systemDictionary.
691 // We also get here for parallel bootstrap classloader
692 if (class_loader.is_null()) {
693 SystemDictionary_lock->wait();
694 } else {
695 double_lock_wait(lockObject, THREAD);
696 }
697 } else {
698 // If not in SD and not in PH, other thread's load must have failed
699 super_load_in_progress = false;
700 }
701 }
702 }
703 return NULL;
704 }
705
706 static void post_class_load_event(EventClassLoad* event, const InstanceKlass* k, const ClassLoaderData* init_cld) {
707 assert(event != NULL, "invariant");
708 assert(k != NULL, "invariant");
709 assert(event->should_commit(), "invariant");
710 event->set_loadedClass(k);
711 event->set_definingClassLoader(k->class_loader_data());
712 event->set_initiatingClassLoader(init_cld);
713 event->commit();
714 }
715
716
717 // Be careful when modifying this code: once you have run
718 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
719 // you need to find_and_remove it before returning.
720 // So be careful to not exit with a CHECK_ macro betweeen these calls.
721 //
722 // name must be in the form of "java/lang/Object" -- cannot be "Ljava/lang/Object;"
723 InstanceKlass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
724 Handle class_loader,
725 Handle protection_domain,
726 TRAPS) {
1238 }
1239 return NULL;
1240 }
1241
1242 // Check if a shared class can be loaded by the specific classloader:
1243 //
1244 // NULL classloader:
1245 // - Module class from "modules" jimage. ModuleEntry must be defined in the classloader.
1246 // - Class from -Xbootclasspath/a. The class has no defined PackageEntry, or must
1247 // be defined in an unnamed module.
1248 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
1249 InstanceKlass* ik,
1250 PackageEntry* pkg_entry,
1251 Handle class_loader, TRAPS) {
1252 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
1253 "Cannot use sharing if java.base is patched");
1254 ResourceMark rm(THREAD);
1255 int path_index = ik->shared_classpath_index();
1256 ClassLoaderData* loader_data = class_loader_data(class_loader);
1257 if (path_index < 0) {
1258 // path_index < 0 indicates that the class is intended for a custom loader
1259 // and should not be loaded by boot/platform/app loaders
1260 if (loader_data->is_builtin_class_loader_data()) {
1261 return false;
1262 } else {
1263 return true;
1264 }
1265 }
1266 SharedClassPathEntry* ent =
1267 (SharedClassPathEntry*)FileMapInfo::shared_path(path_index);
1268 if (!Universe::is_module_initialized()) {
1269 assert(ent != NULL && ent->is_modules_image(),
1270 "Loading non-bootstrap classes before the module system is initialized");
1271 assert(class_loader.is_null(), "sanity");
1272 return true;
1273 }
1274 // Get the pkg_entry from the classloader
1275 ModuleEntry* mod_entry = NULL;
1276 TempNewSymbol pkg_name = pkg_entry != NULL ? pkg_entry->name() :
1277 ClassLoader::package_from_class_name(class_name);
1278 if (pkg_name != NULL) {
1279 if (loader_data != NULL) {
1280 if (pkg_entry != NULL) {
1281 mod_entry = pkg_entry->module();
1282 // If the archived class is from a module that has been patched at runtime,
1283 // the class cannot be loaded from the archive.
1284 if (mod_entry != NULL && mod_entry->is_patched()) {
1352 // If unexpected superclass or interfaces are found, we cannot
1353 // load <ik> from the shared archive.
1354
1355 if (ik->super() != NULL &&
1356 !check_shared_class_super_type(ik, InstanceKlass::cast(ik->super()),
1357 class_loader, protection_domain, true, THREAD)) {
1358 return false;
1359 }
1360
1361 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1362 int num_interfaces = interfaces->length();
1363 for (int index = 0; index < num_interfaces; index++) {
1364 if (!check_shared_class_super_type(ik, interfaces->at(index), class_loader, protection_domain, false, THREAD)) {
1365 return false;
1366 }
1367 }
1368
1369 return true;
1370 }
1371
1372 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1373 Handle class_loader,
1374 Handle protection_domain,
1375 const ClassFileStream *cfs,
1376 PackageEntry* pkg_entry,
1377 TRAPS) {
1378 assert(ik != NULL, "sanity");
1379 assert(!ik->is_unshareable_info_restored(), "shared class can be loaded only once");
1380 Symbol* class_name = ik->name();
1381
1382 bool visible = is_shared_class_visible(
1383 class_name, ik, pkg_entry, class_loader, CHECK_NULL);
1384 if (!visible) {
1385 return NULL;
1386 }
1387
1388 if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1389 return NULL;
1390 }
1391
1392 InstanceKlass* new_ik = KlassFactory::check_shared_class_file_load_hook(
1393 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1394 if (new_ik != NULL) {
1395 // The class is changed by CFLH. Return the new class. The shared class is
1396 // not used.
1397 return new_ik;
1398 }
1399
1400 // Adjust methods to recover missing data. They need addresses for
1401 // interpreter entry points and their default native method address
1402 // must be reset.
1403
1404 // Updating methods must be done under a lock so multiple
1405 // threads don't update these in parallel
1406 //
1407 // Shared classes are all currently loaded by either the bootstrap or
1408 // internal parallel class loaders, so this will never cause a deadlock
1409 // on a custom class loader lock.
1410
1411 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
1412 {
1413 HandleMark hm(THREAD);
1414 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1415 check_loader_lock_contention(lockObject, THREAD);
1416 ObjectLocker ol(lockObject, THREAD, true);
1417 // prohibited package check assumes all classes loaded from archive call
1418 // restore_unshareable_info which calls ik->set_package()
1419 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1420 }
1421
1422 load_shared_class_misc(ik, loader_data, CHECK_NULL);
1423 return ik;
1424 }
1425
1426 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS) {
1427 ik->print_class_load_logging(loader_data, NULL, NULL);
1428
1429 // For boot loader, ensure that GetSystemPackage knows that a class in this
1430 // package was loaded.
1431 if (loader_data->is_the_null_class_loader_data()) {
1432 int path_index = ik->shared_classpath_index();
1433 ik->set_classpath_index(path_index, THREAD);
1434 }
1435
1436 if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
1437 // Only dump the classes that can be stored into CDS archive
1438 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
1439 ResourceMark rm(THREAD);
1440 classlist_file->print_cr("%s", ik->name()->as_C_string());
1441 classlist_file->flush();
1442 }
1443 }
1444
1445 // notify a class loaded from shared object
1446 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1447
1448 ik->set_has_passed_fingerprint_check(false);
1449 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
1450 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
1451 uint64_t cds_fp = ik->get_stored_fingerprint();
1452 if (aot_fp != 0 && aot_fp == cds_fp) {
|
686 // This logic has the current thread wait once it has done
687 // all the superclass/superinterface loading it can, until
688 // the original thread completes the class loading or fails
689 // If it completes we will use the resulting InstanceKlass
690 // which we will find below in the systemDictionary.
691 // We also get here for parallel bootstrap classloader
692 if (class_loader.is_null()) {
693 SystemDictionary_lock->wait();
694 } else {
695 double_lock_wait(lockObject, THREAD);
696 }
697 } else {
698 // If not in SD and not in PH, other thread's load must have failed
699 super_load_in_progress = false;
700 }
701 }
702 }
703 return NULL;
704 }
705
706 void SystemDictionary::post_class_load_event(EventClassLoad* event, const InstanceKlass* k, const ClassLoaderData* init_cld) {
707 assert(event != NULL, "invariant");
708 assert(k != NULL, "invariant");
709 assert(event->should_commit(), "invariant");
710 event->set_loadedClass(k);
711 event->set_definingClassLoader(k->class_loader_data());
712 event->set_initiatingClassLoader(init_cld);
713 event->commit();
714 }
715
716
717 // Be careful when modifying this code: once you have run
718 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
719 // you need to find_and_remove it before returning.
720 // So be careful to not exit with a CHECK_ macro betweeen these calls.
721 //
722 // name must be in the form of "java/lang/Object" -- cannot be "Ljava/lang/Object;"
723 InstanceKlass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
724 Handle class_loader,
725 Handle protection_domain,
726 TRAPS) {
1238 }
1239 return NULL;
1240 }
1241
1242 // Check if a shared class can be loaded by the specific classloader:
1243 //
1244 // NULL classloader:
1245 // - Module class from "modules" jimage. ModuleEntry must be defined in the classloader.
1246 // - Class from -Xbootclasspath/a. The class has no defined PackageEntry, or must
1247 // be defined in an unnamed module.
1248 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
1249 InstanceKlass* ik,
1250 PackageEntry* pkg_entry,
1251 Handle class_loader, TRAPS) {
1252 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
1253 "Cannot use sharing if java.base is patched");
1254 ResourceMark rm(THREAD);
1255 int path_index = ik->shared_classpath_index();
1256 ClassLoaderData* loader_data = class_loader_data(class_loader);
1257 if (path_index < 0) {
1258 // path_index < 0 indicates that
1259 // 1) the class is intended for a custom loader and should not be loaded by boot/platform/app loaders
1260 // or
1261 // 2) the class is a hidden lambda proxy class which would be loaded by boot/platform/app loaders
1262 if (loader_data->is_builtin_class_loader_data()) {
1263 if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1264 return false;
1265 } else {
1266 return true;
1267 }
1268 } else {
1269 return true;
1270 }
1271 }
1272 SharedClassPathEntry* ent =
1273 (SharedClassPathEntry*)FileMapInfo::shared_path(path_index);
1274 if (!Universe::is_module_initialized()) {
1275 assert(ent != NULL && ent->is_modules_image(),
1276 "Loading non-bootstrap classes before the module system is initialized");
1277 assert(class_loader.is_null(), "sanity");
1278 return true;
1279 }
1280 // Get the pkg_entry from the classloader
1281 ModuleEntry* mod_entry = NULL;
1282 TempNewSymbol pkg_name = pkg_entry != NULL ? pkg_entry->name() :
1283 ClassLoader::package_from_class_name(class_name);
1284 if (pkg_name != NULL) {
1285 if (loader_data != NULL) {
1286 if (pkg_entry != NULL) {
1287 mod_entry = pkg_entry->module();
1288 // If the archived class is from a module that has been patched at runtime,
1289 // the class cannot be loaded from the archive.
1290 if (mod_entry != NULL && mod_entry->is_patched()) {
1358 // If unexpected superclass or interfaces are found, we cannot
1359 // load <ik> from the shared archive.
1360
1361 if (ik->super() != NULL &&
1362 !check_shared_class_super_type(ik, InstanceKlass::cast(ik->super()),
1363 class_loader, protection_domain, true, THREAD)) {
1364 return false;
1365 }
1366
1367 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1368 int num_interfaces = interfaces->length();
1369 for (int index = 0; index < num_interfaces; index++) {
1370 if (!check_shared_class_super_type(ik, interfaces->at(index), class_loader, protection_domain, false, THREAD)) {
1371 return false;
1372 }
1373 }
1374
1375 return true;
1376 }
1377
1378 InstanceKlass* SystemDictionary::load_shared_lambda_proxy_class(InstanceKlass* ik,
1379 Handle class_loader,
1380 Handle protection_domain,
1381 PackageEntry* pkg_entry,
1382 TRAPS) {
1383 InstanceKlass* shared_nest_host = SystemDictionaryShared::get_shared_nest_host(ik);
1384 assert(shared_nest_host->is_shared(), "nest host must be in CDS archive");
1385 Symbol* cn = shared_nest_host->name();
1386 Klass *s = resolve_or_fail(cn, class_loader, protection_domain, true, CHECK_NULL);
1387 if (s != shared_nest_host) {
1388 // The dynamically resolved nest_host is not the same as the one we used during dump time,
1389 // so we cannot use ik.
1390 return NULL;
1391 } else {
1392 assert(s->is_shared(), "must be");
1393 }
1394
1395 // The lambda proxy class and its nest host have the same class loader and class loader data,
1396 // as verified in SystemDictionaryShared::add_lambda_proxy_class()
1397 assert(shared_nest_host->class_loader() == class_loader(), "mismatched class loader");
1398 assert(shared_nest_host->class_loader_data() == ClassLoaderData::class_loader_data(class_loader()), "mismatched class loader data");
1399 ik->set_nest_host(shared_nest_host, THREAD);
1400
1401 InstanceKlass* loaded_ik = load_shared_class(ik, class_loader, protection_domain, NULL, pkg_entry, CHECK_NULL);
1402
1403 assert(shared_nest_host->is_same_class_package(ik),
1404 "lambda proxy class and its nest host must be in the same package");
1405
1406 return loaded_ik;
1407 }
1408
1409 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1410 Handle class_loader,
1411 Handle protection_domain,
1412 const ClassFileStream *cfs,
1413 PackageEntry* pkg_entry,
1414 TRAPS) {
1415 assert(ik != NULL, "sanity");
1416 assert(!ik->is_unshareable_info_restored(), "shared class can be loaded only once");
1417 Symbol* class_name = ik->name();
1418
1419 bool visible = is_shared_class_visible(
1420 class_name, ik, pkg_entry, class_loader, CHECK_NULL);
1421 if (!visible) {
1422 return NULL;
1423 }
1424
1425 if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1426 return NULL;
1427 }
1428
1429 InstanceKlass* new_ik = NULL;
1430 // CFLH check is skipped for VM hidden or anonymous classes (see KlassFactory::create_from_stream).
1431 // It will be skipped for shared VM hidden lambda proxy classes.
1432 if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1433 new_ik = KlassFactory::check_shared_class_file_load_hook(
1434 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1435 }
1436 if (new_ik != NULL) {
1437 // The class is changed by CFLH. Return the new class. The shared class is
1438 // not used.
1439 return new_ik;
1440 }
1441
1442 // Adjust methods to recover missing data. They need addresses for
1443 // interpreter entry points and their default native method address
1444 // must be reset.
1445
1446 // Updating methods must be done under a lock so multiple
1447 // threads don't update these in parallel
1448 //
1449 // Shared classes are all currently loaded by either the bootstrap or
1450 // internal parallel class loaders, so this will never cause a deadlock
1451 // on a custom class loader lock.
1452
1453 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
1454 {
1455 HandleMark hm(THREAD);
1456 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1457 check_loader_lock_contention(lockObject, THREAD);
1458 ObjectLocker ol(lockObject, THREAD, true);
1459 // prohibited package check assumes all classes loaded from archive call
1460 // restore_unshareable_info which calls ik->set_package()
1461 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1462 }
1463
1464 load_shared_class_misc(ik, loader_data, CHECK_NULL);
1465 return ik;
1466 }
1467
1468 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS) {
1469 ik->print_class_load_logging(loader_data, NULL, NULL);
1470
1471 // For boot loader, ensure that GetSystemPackage knows that a class in this
1472 // package was loaded.
1473 if (loader_data->is_the_null_class_loader_data()) {
1474 int path_index = ik->shared_classpath_index();
1475 if (path_index < 0) {
1476 if (SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1477 InstanceKlass* nest_host = SystemDictionaryShared::get_shared_nest_host(ik);
1478 path_index = nest_host->shared_classpath_index();
1479 }
1480 }
1481 ik->set_classpath_index(path_index, THREAD);
1482 }
1483
1484 if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
1485 // Only dump the classes that can be stored into CDS archive
1486 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
1487 ResourceMark rm(THREAD);
1488 classlist_file->print_cr("%s", ik->name()->as_C_string());
1489 classlist_file->flush();
1490 }
1491 }
1492
1493 // notify a class loaded from shared object
1494 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1495
1496 ik->set_has_passed_fingerprint_check(false);
1497 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
1498 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
1499 uint64_t cds_fp = ik->get_stored_fingerprint();
1500 if (aot_fp != 0 && aot_fp == cds_fp) {
|