hotspot/src/share/vm/classfile/systemDictionary.cpp
changeset 36508 5f9eee6b383b
parent 36381 b9ed6bef9364
child 36822 cdc493d7bc9a
child 37179 4dbcb3a642d2
equal deleted inserted replaced
36507:c80f6ecb0bb3 36508:5f9eee6b383b
    30 #include "classfile/classLoaderExt.hpp"
    30 #include "classfile/classLoaderExt.hpp"
    31 #include "classfile/dictionary.hpp"
    31 #include "classfile/dictionary.hpp"
    32 #include "classfile/javaClasses.inline.hpp"
    32 #include "classfile/javaClasses.inline.hpp"
    33 #include "classfile/klassFactory.hpp"
    33 #include "classfile/klassFactory.hpp"
    34 #include "classfile/loaderConstraints.hpp"
    34 #include "classfile/loaderConstraints.hpp"
       
    35 #include "classfile/packageEntry.hpp"
    35 #include "classfile/placeholders.hpp"
    36 #include "classfile/placeholders.hpp"
    36 #include "classfile/resolutionErrors.hpp"
    37 #include "classfile/resolutionErrors.hpp"
    37 #include "classfile/stringTable.hpp"
    38 #include "classfile/stringTable.hpp"
    38 #include "classfile/systemDictionary.hpp"
    39 #include "classfile/systemDictionary.hpp"
    39 #include "classfile/vmSymbols.hpp"
    40 #include "classfile/vmSymbols.hpp"
    49 #include "oops/klass.inline.hpp"
    50 #include "oops/klass.inline.hpp"
    50 #include "oops/methodData.hpp"
    51 #include "oops/methodData.hpp"
    51 #include "oops/objArrayKlass.hpp"
    52 #include "oops/objArrayKlass.hpp"
    52 #include "oops/objArrayOop.inline.hpp"
    53 #include "oops/objArrayOop.inline.hpp"
    53 #include "oops/oop.inline.hpp"
    54 #include "oops/oop.inline.hpp"
       
    55 #include "oops/symbol.hpp"
    54 #include "oops/typeArrayKlass.hpp"
    56 #include "oops/typeArrayKlass.hpp"
    55 #include "prims/jvmtiEnvBase.hpp"
    57 #include "prims/jvmtiEnvBase.hpp"
    56 #include "prims/methodHandles.hpp"
    58 #include "prims/methodHandles.hpp"
    57 #include "runtime/arguments.hpp"
    59 #include "runtime/arguments.hpp"
    58 #include "runtime/biasedLocking.hpp"
    60 #include "runtime/biasedLocking.hpp"
   169    }
   171    }
   170    return false;
   172    return false;
   171 }
   173 }
   172 
   174 
   173 /**
   175 /**
   174  * Returns true if the passed class loader is the extension class loader.
   176  * Returns true if the passed class loader is the platform class loader.
   175  */
   177  */
   176 bool SystemDictionary::is_ext_class_loader(Handle class_loader) {
   178 bool SystemDictionary::is_platform_class_loader(Handle class_loader) {
   177   if (class_loader.is_null()) {
   179   if (class_loader.is_null()) {
   178     return false;
   180     return false;
   179   }
   181   }
   180   return (class_loader->klass()->name() == vmSymbols::sun_misc_Launcher_ExtClassLoader());
   182   return (class_loader->klass() == SystemDictionary::jdk_internal_loader_ClassLoaders_PlatformClassLoader_klass());
   181 }
   183 }
   182 
   184 
   183 // ----------------------------------------------------------------------------
   185 // ----------------------------------------------------------------------------
   184 // Resolving of classes
   186 // Resolving of classes
   185 
   187 
  1142   }
  1144   }
  1143 
  1145 
  1144   const char* pkg = "java/";
  1146   const char* pkg = "java/";
  1145   if (!HAS_PENDING_EXCEPTION &&
  1147   if (!HAS_PENDING_EXCEPTION &&
  1146       !class_loader.is_null() &&
  1148       !class_loader.is_null() &&
       
  1149       !SystemDictionary::is_platform_class_loader(class_loader) &&
  1147       parsed_name != NULL &&
  1150       parsed_name != NULL &&
  1148       !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
  1151       !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
  1149     // It is illegal to define classes in the "java." package from
  1152     // It is illegal to define classes in the "java." package from
  1150     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
  1153     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
  1151     ResourceMark rm(THREAD);
  1154     ResourceMark rm(THREAD);
  1234 instanceKlassHandle SystemDictionary::load_shared_class(
  1237 instanceKlassHandle SystemDictionary::load_shared_class(
  1235                  Symbol* class_name, Handle class_loader, TRAPS) {
  1238                  Symbol* class_name, Handle class_loader, TRAPS) {
  1236   instanceKlassHandle ik (THREAD, find_shared_class(class_name));
  1239   instanceKlassHandle ik (THREAD, find_shared_class(class_name));
  1237   // Make sure we only return the boot class for the NULL classloader.
  1240   // Make sure we only return the boot class for the NULL classloader.
  1238   if (ik.not_null() &&
  1241   if (ik.not_null() &&
  1239       SharedClassUtil::is_shared_boot_class(ik()) && class_loader.is_null()) {
  1242       ik->is_shared_boot_class() && class_loader.is_null()) {
  1240     Handle protection_domain;
  1243     Handle protection_domain;
  1241     return load_shared_class(ik, class_loader, protection_domain, THREAD);
  1244     return load_shared_class(ik, class_loader, protection_domain, THREAD);
  1242   }
  1245   }
  1243   return instanceKlassHandle();
  1246   return instanceKlassHandle();
       
  1247 }
       
  1248 
       
  1249 // Check if a shared class can be loaded by the specific classloader:
       
  1250 //
       
  1251 // NULL classloader:
       
  1252 //   - Module class from "modules" jimage. ModuleEntry must be defined in the classloader.
       
  1253 //   - Class from -Xbootclasspath/a. The class has no defined PackageEntry, or must
       
  1254 //     be defined in an unnamed module.
       
  1255 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
       
  1256                                                instanceKlassHandle ik,
       
  1257                                                Handle class_loader, TRAPS) {
       
  1258   int path_index = ik->shared_classpath_index();
       
  1259   SharedClassPathEntry* ent =
       
  1260             (SharedClassPathEntry*)FileMapInfo::shared_classpath(path_index);
       
  1261   if (!Universe::is_module_initialized()) {
       
  1262     assert(ent->is_jrt(),
       
  1263            "Loading non-bootstrap classes before the module system is initialized");
       
  1264     assert(class_loader.is_null(), "sanity");
       
  1265     return true;
       
  1266   }
       
  1267   // Get the pkg_entry from the classloader
       
  1268   TempNewSymbol pkg_name = NULL;
       
  1269   PackageEntry* pkg_entry = NULL;
       
  1270   ModuleEntry* mod_entry = NULL;
       
  1271   int length = 0;
       
  1272   ClassLoaderData* loader_data = class_loader_data(class_loader);
       
  1273   const jbyte* pkg_string = InstanceKlass::package_from_name(class_name, length);
       
  1274   if (pkg_string != NULL) {
       
  1275     pkg_name = SymbolTable::new_symbol((const char*)pkg_string,
       
  1276                                        length, CHECK_(false));
       
  1277     if (loader_data != NULL) {
       
  1278       pkg_entry = loader_data->packages()->lookup_only(pkg_name);
       
  1279     }
       
  1280     if (pkg_entry != NULL) {
       
  1281       mod_entry = pkg_entry->module();
       
  1282     }
       
  1283   }
       
  1284 
       
  1285   if (class_loader.is_null()) {
       
  1286     // The NULL classloader can load archived class originated from the
       
  1287     // "modules" jimage and the -Xbootclasspath/a. For class from the
       
  1288     // "modules" jimage, the PackageEntry/ModuleEntry must be defined
       
  1289     // by the NULL classloader.
       
  1290     if (mod_entry != NULL) {
       
  1291       // PackageEntry/ModuleEntry is found in the classloader. Check if the
       
  1292       // ModuleEntry's location agrees with the archived class' origination.
       
  1293       if (ent->is_jrt() && mod_entry->location()->starts_with("jrt:")) {
       
  1294         return true; // Module class from the "module" jimage
       
  1295       }
       
  1296     }
       
  1297 
       
  1298     // If the archived class is not from the "module" jimage, the class can be
       
  1299     // loaded by the NULL classloader if
       
  1300     //
       
  1301     // 1. the class is from the unamed package
       
  1302     // 2. or, the class is not from a module defined in the NULL classloader
       
  1303     // 3. or, the class is from an unamed module
       
  1304     if (!ent->is_jrt() && ik->is_shared_boot_class()) {
       
  1305       // the class is from the -Xbootclasspath/a
       
  1306       if (pkg_string == NULL ||
       
  1307           pkg_entry == NULL ||
       
  1308           pkg_entry->in_unnamed_module()) {
       
  1309         assert(mod_entry == NULL ||
       
  1310                mod_entry == loader_data->modules()->unnamed_module(),
       
  1311                "the unnamed module is not defined in the classloader");
       
  1312         return true;
       
  1313       }
       
  1314     }
       
  1315     return false;
       
  1316   } else {
       
  1317     bool res = SystemDictionaryShared::is_shared_class_visible_for_classloader(
       
  1318               ik, class_loader, pkg_string, pkg_name,
       
  1319               pkg_entry, mod_entry, CHECK_(false));
       
  1320     return res;
       
  1321   }
  1244 }
  1322 }
  1245 
  1323 
  1246 instanceKlassHandle SystemDictionary::load_shared_class(instanceKlassHandle ik,
  1324 instanceKlassHandle SystemDictionary::load_shared_class(instanceKlassHandle ik,
  1247                                                         Handle class_loader,
  1325                                                         Handle class_loader,
  1248                                                         Handle protection_domain, TRAPS) {
  1326                                                         Handle protection_domain, TRAPS) {
  1249   if (ik.not_null()) {
  1327   if (ik.not_null()) {
  1250     instanceKlassHandle nh = instanceKlassHandle(); // null Handle
  1328     instanceKlassHandle nh = instanceKlassHandle(); // null Handle
  1251     Symbol* class_name = ik->name();
  1329     Symbol* class_name = ik->name();
       
  1330 
       
  1331     bool visible = is_shared_class_visible(
       
  1332                             class_name, ik, class_loader, CHECK_(nh));
       
  1333     if (!visible) {
       
  1334       return nh;
       
  1335     }
  1252 
  1336 
  1253     // Found the class, now load the superclass and interfaces.  If they
  1337     // Found the class, now load the superclass and interfaces.  If they
  1254     // are shared, add them to the main system dictionary and reset
  1338     // are shared, add them to the main system dictionary and reset
  1255     // their hierarchy references (supers, subs, and interfaces).
  1339     // their hierarchy references (supers, subs, and interfaces).
  1256 
  1340 
  1301       ObjectLocker ol(lockObject, THREAD, true);
  1385       ObjectLocker ol(lockObject, THREAD, true);
  1302       ik->restore_unshareable_info(loader_data, protection_domain, CHECK_(nh));
  1386       ik->restore_unshareable_info(loader_data, protection_domain, CHECK_(nh));
  1303     }
  1387     }
  1304 
  1388 
  1305     if (log_is_enabled(Info, classload)) {
  1389     if (log_is_enabled(Info, classload)) {
  1306       ik()->print_loading_log(LogLevel::Info, loader_data, NULL);
  1390       ik()->print_loading_log(LogLevel::Info, loader_data, NULL, NULL);
  1307     }
  1391     }
  1308     // No 'else' here as logging levels are not mutually exclusive
  1392     // No 'else' here as logging levels are not mutually exclusive
  1309 
  1393 
  1310     if (log_is_enabled(Debug, classload)) {
  1394     if (log_is_enabled(Debug, classload)) {
  1311       ik()->print_loading_log(LogLevel::Debug, loader_data, NULL);
  1395       ik()->print_loading_log(LogLevel::Debug, loader_data, NULL, NULL);
       
  1396     }
       
  1397 
       
  1398     // For boot loader, ensure that GetSystemPackage knows that a class in this
       
  1399     // package was loaded.
       
  1400     if (class_loader.is_null()) {
       
  1401       int path_index = ik->shared_classpath_index();
       
  1402       ResourceMark rm;
       
  1403       ClassLoader::add_package(ik->name()->as_C_string(), path_index, THREAD);
  1312     }
  1404     }
  1313 
  1405 
  1314     if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
  1406     if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
  1315       // Only dump the classes that can be stored into CDS archive
  1407       // Only dump the classes that can be stored into CDS archive
  1316       if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
  1408       if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
  1327 }
  1419 }
  1328 #endif // INCLUDE_CDS
  1420 #endif // INCLUDE_CDS
  1329 
  1421 
  1330 instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
  1422 instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
  1331   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
  1423   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
       
  1424 
  1332   if (class_loader.is_null()) {
  1425   if (class_loader.is_null()) {
       
  1426     int length = 0;
       
  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     const jbyte* pkg_string = InstanceKlass::package_from_name(class_name, length);
       
  1433     if (pkg_string != NULL) {
       
  1434       TempNewSymbol pkg_name = SymbolTable::new_symbol((const char*)pkg_string, length, CHECK_(nh));
       
  1435       pkg_entry = loader_data->packages()->lookup_only(pkg_name);
       
  1436     }
       
  1437 
       
  1438     // Prior to attempting to load the class, enforce the boot loader's
       
  1439     // visibility boundaries.
       
  1440     if (!Universe::is_module_initialized()) {
       
  1441       // During bootstrapping, prior to module initialization, any
       
  1442       // class attempting to be loaded must be checked against the
       
  1443       // java.base packages in the boot loader's PackageEntryTable.
       
  1444       // No class outside of java.base is allowed to be loaded during
       
  1445       // this bootstrapping window.
       
  1446       if (!DumpSharedSpaces) {
       
  1447         if (pkg_entry == NULL || pkg_entry->in_unnamed_module()) {
       
  1448           // Class is either in the unnamed package or in
       
  1449           // a named package within the unnamed module.  Either
       
  1450           // case is outside of java.base, do not attempt to
       
  1451           // load the class post java.base definition.  If
       
  1452           // java.base has not been defined, let the class load
       
  1453           // and its package will be checked later by
       
  1454           // ModuleEntryTable::verify_javabase_packages.
       
  1455           if (ModuleEntryTable::javabase_defined()) {
       
  1456             return nh;
       
  1457           }
       
  1458         } else {
       
  1459           // Check that the class' package is defined within java.base.
       
  1460           ModuleEntry* mod_entry = pkg_entry->module();
       
  1461           Symbol* mod_entry_name = mod_entry->name();
       
  1462           if (mod_entry_name->fast_compare(vmSymbols::java_base()) != 0) {
       
  1463             return nh;
       
  1464           }
       
  1465         }
       
  1466       }
       
  1467     } else {
       
  1468       assert(!DumpSharedSpaces, "Archive dumped after module system initialization");
       
  1469       // After the module system has been initialized, check if the class'
       
  1470       // package is in a module defined to the boot loader.
       
  1471       if (pkg_string == NULL || pkg_entry == NULL || pkg_entry->in_unnamed_module()) {
       
  1472         // Class is either in the unnamed package, in a named package
       
  1473         // within a module not defined to the boot loader or in a
       
  1474         // a named package within the unnamed module.  In all cases,
       
  1475         // limit visibility to search for the class only in the boot
       
  1476         // loader's append path.
       
  1477         search_only_bootloader_append = true;
       
  1478       }
       
  1479     }
       
  1480 
       
  1481     // Prior to bootstrapping's module initialization, never load a class outside
       
  1482     // of the boot loader's module path
       
  1483     assert(Universe::is_module_initialized() || DumpSharedSpaces ||
       
  1484            !search_only_bootloader_append,
       
  1485            "Attempt to load a class outside of boot loader's module path");
  1333 
  1486 
  1334     // Search the shared system dictionary for classes preloaded into the
  1487     // Search the shared system dictionary for classes preloaded into the
  1335     // shared spaces.
  1488     // shared spaces.
  1336     instanceKlassHandle k;
  1489     instanceKlassHandle k;
  1337     {
  1490     {
  1342     }
  1495     }
  1343 
  1496 
  1344     if (k.is_null()) {
  1497     if (k.is_null()) {
  1345       // Use VM class loader
  1498       // Use VM class loader
  1346       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
  1499       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
  1347       k = ClassLoader::load_class(class_name, CHECK_(nh));
  1500       k = ClassLoader::load_class(class_name, search_only_bootloader_append, CHECK_(nh));
  1348     }
  1501     }
  1349 
  1502 
  1350     // find_or_define_instance_class may return a different InstanceKlass
  1503     // find_or_define_instance_class may return a different InstanceKlass
  1351     if (!k.is_null()) {
  1504     if (!k.is_null()) {
  1352       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
  1505       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
  1667   int d_index = dictionary()->hash_to_index(d_hash);
  1820   int d_index = dictionary()->hash_to_index(d_hash);
  1668   return find_class(d_index, d_hash, class_name, loader_data);
  1821   return find_class(d_index, d_hash, class_name, loader_data);
  1669 }
  1822 }
  1670 
  1823 
  1671 
  1824 
  1672 // Get the next class in the diictionary.
  1825 // Get the next class in the dictionary.
  1673 Klass* SystemDictionary::try_get_next_class() {
  1826 Klass* SystemDictionary::try_get_next_class() {
  1674   return dictionary()->try_get_next_class();
  1827   return dictionary()->try_get_next_class();
  1675 }
  1828 }
  1676 
  1829 
  1677 
  1830 
  1938   start_id = limit_id;
  2091   start_id = limit_id;
  1939 }
  2092 }
  1940 
  2093 
  1941 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
  2094 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
  1942   assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
  2095   assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
       
  2096 
       
  2097   // Create the ModuleEntry for java.base.  This call needs to be done here,
       
  2098   // after vmSymbols::initialize() is called but before any classes are pre-loaded.
       
  2099   ClassLoader::create_javabase();
       
  2100 
  1943   // Preload commonly used klasses
  2101   // Preload commonly used klasses
  1944   WKID scan = FIRST_WKID;
  2102   WKID scan = FIRST_WKID;
  1945   // first do Object, then String, Class
  2103   // first do Object, then String, Class
  1946   if (UseSharedSpaces) {
  2104   if (UseSharedSpaces) {
  1947     initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK);
  2105     initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK);