hotspot/src/share/vm/classfile/classLoader.cpp
changeset 41281 e1dc38ba642f
parent 41183 207b92e69457
child 41667 4a349512fde1
equal deleted inserted replaced
41279:5a7c83da4a2d 41281:e1dc38ba642f
    83 typedef jboolean (JNICALL *ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);
    83 typedef jboolean (JNICALL *ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);
    84 typedef jboolean (JNICALL *ReadMappedEntry_t)(jzfile *zip, jzentry *entry, unsigned char **buf, char *namebuf);
    84 typedef jboolean (JNICALL *ReadMappedEntry_t)(jzfile *zip, jzentry *entry, unsigned char **buf, char *namebuf);
    85 typedef jzentry* (JNICALL *GetNextEntry_t)(jzfile *zip, jint n);
    85 typedef jzentry* (JNICALL *GetNextEntry_t)(jzfile *zip, jint n);
    86 typedef jboolean (JNICALL *ZipInflateFully_t)(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg);
    86 typedef jboolean (JNICALL *ZipInflateFully_t)(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg);
    87 typedef jint     (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len);
    87 typedef jint     (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len);
       
    88 typedef void     (JNICALL *FreeEntry_t)(jzfile *zip, jzentry *entry);
    88 
    89 
    89 static ZipOpen_t         ZipOpen            = NULL;
    90 static ZipOpen_t         ZipOpen            = NULL;
    90 static ZipClose_t        ZipClose           = NULL;
    91 static ZipClose_t        ZipClose           = NULL;
    91 static FindEntry_t       FindEntry          = NULL;
    92 static FindEntry_t       FindEntry          = NULL;
    92 static ReadEntry_t       ReadEntry          = NULL;
    93 static ReadEntry_t       ReadEntry          = NULL;
    93 static ReadMappedEntry_t ReadMappedEntry    = NULL;
    94 static ReadMappedEntry_t ReadMappedEntry    = NULL;
    94 static GetNextEntry_t    GetNextEntry       = NULL;
    95 static GetNextEntry_t    GetNextEntry       = NULL;
    95 static canonicalize_fn_t CanonicalizeEntry  = NULL;
    96 static canonicalize_fn_t CanonicalizeEntry  = NULL;
    96 static ZipInflateFully_t ZipInflateFully    = NULL;
    97 static ZipInflateFully_t ZipInflateFully    = NULL;
    97 static Crc32_t           Crc32              = NULL;
    98 static Crc32_t           Crc32              = NULL;
       
    99 static FreeEntry_t       FreeEntry          = NULL;
    98 
   100 
    99 // Entry points for jimage.dll for loading jimage file entries
   101 // Entry points for jimage.dll for loading jimage file entries
   100 
   102 
   101 static JImageOpen_t                    JImageOpen             = NULL;
   103 static JImageOpen_t                    JImageOpen             = NULL;
   102 static JImageClose_t                   JImageClose            = NULL;
   104 static JImageClose_t                   JImageClose            = NULL;
   148 int             ClassLoader::_num_entries        = 0;
   150 int             ClassLoader::_num_entries        = 0;
   149 #if INCLUDE_CDS
   151 #if INCLUDE_CDS
   150 GrowableArray<char*>* ClassLoader::_boot_modules_array = NULL;
   152 GrowableArray<char*>* ClassLoader::_boot_modules_array = NULL;
   151 GrowableArray<char*>* ClassLoader::_platform_modules_array = NULL;
   153 GrowableArray<char*>* ClassLoader::_platform_modules_array = NULL;
   152 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
   154 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
       
   155 int  ClassLoader::_num_patch_mod_prefixes = 0;
   153 #endif
   156 #endif
   154 
   157 
   155 // helper routines
   158 // helper routines
   156 bool string_starts_with(const char* str, const char* str_to_find) {
   159 bool string_starts_with(const char* str, const char* str_to_find) {
   157   size_t str_len = strlen(str);
   160   size_t str_len = strlen(str);
   315 ClassPathZipEntry::~ClassPathZipEntry() {
   318 ClassPathZipEntry::~ClassPathZipEntry() {
   316   if (ZipClose != NULL) {
   319   if (ZipClose != NULL) {
   317     (*ZipClose)(_zip);
   320     (*ZipClose)(_zip);
   318   }
   321   }
   319   FREE_C_HEAP_ARRAY(char, _zip_name);
   322   FREE_C_HEAP_ARRAY(char, _zip_name);
       
   323 }
       
   324 
       
   325 bool ClassPathZipEntry::stream_exists(const char* name) {
       
   326   // enable call to C land
       
   327   JavaThread* thread = JavaThread::current();
       
   328   ThreadToNativeFromVM ttn(thread);
       
   329   // check whether zip archive contains name
       
   330   jint name_len, filesize;
       
   331   jzentry* entry = (*FindEntry)(_zip, name, &filesize, &name_len);
       
   332   if (entry != NULL) {
       
   333     (*FreeEntry)(_zip, entry);
       
   334     return true;
       
   335   }
       
   336   return false;
   320 }
   337 }
   321 
   338 
   322 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
   339 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
   323     // enable call to C land
   340     // enable call to C land
   324   JavaThread* thread = JavaThread::current();
   341   JavaThread* thread = JavaThread::current();
   638     exit_with_path_failure("Cannot have empty path in archived classpaths", NULL);
   655     exit_with_path_failure("Cannot have empty path in archived classpaths", NULL);
   639   }
   656   }
   640 
   657 
   641   struct stat st;
   658   struct stat st;
   642   if (os::stat(path, &st) == 0) {
   659   if (os::stat(path, &st) == 0) {
   643     if ((st.st_mode & S_IFREG) != S_IFREG) { // is directory
   660     if ((st.st_mode & S_IFMT) != S_IFREG) { // is not a regular file
   644       if (!os::dir_is_empty(path)) {
   661       if (!os::dir_is_empty(path)) {
   645         tty->print_cr("Error: non-empty directory '%s'", path);
   662         tty->print_cr("Error: non-empty directory '%s'", path);
   646         exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL);
   663         exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL);
   647       }
   664       }
   648     }
   665     }
   691 void ClassLoader::setup_patch_mod_entries() {
   708 void ClassLoader::setup_patch_mod_entries() {
   692   Thread* THREAD = Thread::current();
   709   Thread* THREAD = Thread::current();
   693   GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
   710   GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
   694   int num_of_entries = patch_mod_args->length();
   711   int num_of_entries = patch_mod_args->length();
   695 
   712 
   696   assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with --patch-module");
       
   697   assert(!UseSharedSpaces, "UseSharedSpaces not supported with --patch-module");
       
   698 
   713 
   699   // Set up the boot loader's _patch_mod_entries list
   714   // Set up the boot loader's _patch_mod_entries list
   700   _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);
   715   _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);
   701 
   716 
   702   for (int i = 0; i < num_of_entries; i++) {
   717   for (int i = 0; i < num_of_entries; i++) {
   849 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
   864 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
   850                                                      bool throw_exception,
   865                                                      bool throw_exception,
   851                                                      bool is_boot_append, TRAPS) {
   866                                                      bool is_boot_append, TRAPS) {
   852   JavaThread* thread = JavaThread::current();
   867   JavaThread* thread = JavaThread::current();
   853   ClassPathEntry* new_entry = NULL;
   868   ClassPathEntry* new_entry = NULL;
   854   if ((st->st_mode & S_IFREG) == S_IFREG) {
   869   if ((st->st_mode & S_IFMT) == S_IFREG) {
   855     ResourceMark rm(thread);
   870     ResourceMark rm(thread);
   856     // Regular file, should be a zip or jimage file
   871     // Regular file, should be a zip or jimage file
   857     // Canonicalized filename
   872     // Canonicalized filename
   858     char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
   873     char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
   859     if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
   874     if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
   912 // or zip/JAR file cannot be opened)
   927 // or zip/JAR file cannot be opened)
   913 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
   928 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
   914   // check for a regular file
   929   // check for a regular file
   915   struct stat st;
   930   struct stat st;
   916   if (os::stat(path, &st) == 0) {
   931   if (os::stat(path, &st) == 0) {
   917     if ((st.st_mode & S_IFREG) == S_IFREG) {
   932     if ((st.st_mode & S_IFMT) == S_IFREG) {
   918       char canonical_path[JVM_MAXPATHLEN];
   933       char canonical_path[JVM_MAXPATHLEN];
   919       if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
   934       if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
   920         char* error_msg = NULL;
   935         char* error_msg = NULL;
   921         jzfile* zip;
   936         jzfile* zip;
   922         {
   937         {
  1066   ReadEntry    = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry"));
  1081   ReadEntry    = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry"));
  1067   ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry"));
  1082   ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry"));
  1068   GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry"));
  1083   GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry"));
  1069   ZipInflateFully = CAST_TO_FN_PTR(ZipInflateFully_t, os::dll_lookup(handle, "ZIP_InflateFully"));
  1084   ZipInflateFully = CAST_TO_FN_PTR(ZipInflateFully_t, os::dll_lookup(handle, "ZIP_InflateFully"));
  1070   Crc32        = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32"));
  1085   Crc32        = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32"));
       
  1086   FreeEntry    = CAST_TO_FN_PTR(FreeEntry_t, os::dll_lookup(handle, "ZIP_FreeEntry"));
  1071 
  1087 
  1072   // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL
  1088   // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL
  1073   if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL ||
  1089   if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL ||
  1074       GetNextEntry == NULL || Crc32 == NULL) {
  1090       GetNextEntry == NULL || Crc32 == NULL) {
  1075     vm_exit_during_initialization("Corrupted ZIP library", path);
  1091     vm_exit_during_initialization("Corrupted ZIP library", path);
  1393   }
  1409   }
  1394 
  1410 
  1395   return NULL;
  1411   return NULL;
  1396 }
  1412 }
  1397 
  1413 
       
  1414 #if INCLUDE_CDS
       
  1415 // The following function is only used during CDS dump time.
       
  1416 // It checks if a class can be found in the jar entries of the _patch_mod_entries.
       
  1417 // It does not support non-jar entries.
       
  1418 bool ClassLoader::is_in_patch_module(const char* const file_name) {
       
  1419   assert(DumpSharedSpaces, "dump time only");
       
  1420   if (_patch_mod_entries == NULL) {
       
  1421     return false;
       
  1422   }
       
  1423 
       
  1424   int num_of_entries = _patch_mod_entries->length();
       
  1425   char* class_module_name = NULL;
       
  1426   ResourceMark rm;
       
  1427   const char *pkg_name = package_from_name(file_name);
       
  1428   // Using the jimage to obtain the class' module name.
       
  1429   // The ModuleEntryTable cannot be used at this point during dump time
       
  1430   // because the module system hasn't been initialized yet.
       
  1431   if (pkg_name != NULL) {
       
  1432     JImageFile *jimage = _jrt_entry->jimage();
       
  1433     class_module_name = (char*)(*JImagePackageToModule)(jimage, pkg_name);
       
  1434   }
       
  1435 
       
  1436   if (class_module_name == NULL) {
       
  1437     return false;
       
  1438   }
       
  1439 
       
  1440   // Loop through all the patch module entries looking for module
       
  1441   for (int i = 0; i < num_of_entries; i++) {
       
  1442     ModuleClassPathList* module_cpl = _patch_mod_entries->at(i);
       
  1443     Symbol* module_cpl_name = module_cpl->module_name();
       
  1444 
       
  1445     if (strcmp(module_cpl_name->as_C_string(), class_module_name) == 0) {
       
  1446       // Class' module has been located, attempt to locate
       
  1447       // the class from the module's ClassPathEntry list.
       
  1448       ClassPathEntry* e = module_cpl->module_first_entry();
       
  1449       while (e != NULL) {
       
  1450         if (e->is_jar_file()) {
       
  1451           if (e->stream_exists(file_name)) {
       
  1452             return true;
       
  1453           } else {
       
  1454             e = e->next();
       
  1455           }
       
  1456         }
       
  1457       }
       
  1458     }
       
  1459   }
       
  1460 
       
  1461   return false;
       
  1462 }
       
  1463 #endif // INCLUDE_CDS
       
  1464 
  1398 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
  1465 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
  1399   assert(name != NULL, "invariant");
  1466   assert(name != NULL, "invariant");
  1400   assert(THREAD->is_Java_thread(), "must be a JavaThread");
  1467   assert(THREAD->is_Java_thread(), "must be a JavaThread");
  1401 
  1468 
  1402   ResourceMark rm(THREAD);
  1469   ResourceMark rm(THREAD);
  1418   s2 classpath_index = 0;
  1485   s2 classpath_index = 0;
  1419   ClassPathEntry* e = NULL;
  1486   ClassPathEntry* e = NULL;
  1420 
  1487 
  1421   // If DumpSharedSpaces is true boot loader visibility boundaries are set to:
  1488   // If DumpSharedSpaces is true boot loader visibility boundaries are set to:
  1422   //   - [jimage] + [_first_append_entry to _last_append_entry] (all path entries).
  1489   //   - [jimage] + [_first_append_entry to _last_append_entry] (all path entries).
  1423   // No --patch-module entries or exploded module builds are included since CDS
  1490   // If a class is found in the --patch-module entries, the class will not be included in the
  1424   // is not supported if --patch-module or exploded module builds are used.
  1491   // CDS archive. Also, CDS is not supported if exploded module builds are used.
  1425   //
  1492   //
  1426   // If search_append_only is true, boot loader visibility boundaries are
  1493   // If search_append_only is true, boot loader visibility boundaries are
  1427   // set to be _first_append_entry to the end. This includes:
  1494   // set to be _first_append_entry to the end. This includes:
  1428   //   [-Xbootclasspath/a]; [jvmti appended entries]
  1495   //   [-Xbootclasspath/a]; [jvmti appended entries]
  1429   //
  1496   //
  1442   // Specifications to --patch-module can contain a partial number of classes
  1509   // Specifications to --patch-module can contain a partial number of classes
  1443   // that are part of the overall module definition.  So if a particular class is not
  1510   // that are part of the overall module definition.  So if a particular class is not
  1444   // found within its module specification, the search should continue to Load Attempt #2.
  1511   // found within its module specification, the search should continue to Load Attempt #2.
  1445   // Note: The --patch-module entries are never searched if the boot loader's
  1512   // Note: The --patch-module entries are never searched if the boot loader's
  1446   //       visibility boundary is limited to only searching the append entries.
  1513   //       visibility boundary is limited to only searching the append entries.
  1447   if (_patch_mod_entries != NULL && !search_append_only && !DumpSharedSpaces) {
  1514   if (_patch_mod_entries != NULL && !search_append_only) {
  1448     stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL);
  1515     if (!DumpSharedSpaces) {
       
  1516       stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL);
       
  1517     } else {
       
  1518 #if INCLUDE_CDS
       
  1519       if (is_in_patch_module(file_name)) {
       
  1520         tty->print_cr("Preload Warning: Skip archiving class %s found in --patch-module entry", class_name);
       
  1521         return NULL;
       
  1522       }
       
  1523 #endif
       
  1524     }
  1449   }
  1525   }
  1450 
  1526 
  1451   // Load Attempt #2: [jimage | exploded build]
  1527   // Load Attempt #2: [jimage | exploded build]
  1452   if (!search_append_only && (NULL == stream)) {
  1528   if (!search_append_only && (NULL == stream)) {
  1453     if (has_jrt_entry()) {
  1529     if (has_jrt_entry()) {
  1594 #endif
  1670 #endif
  1595   setup_bootstrap_search_path();
  1671   setup_bootstrap_search_path();
  1596 }
  1672 }
  1597 
  1673 
  1598 #if INCLUDE_CDS
  1674 #if INCLUDE_CDS
       
  1675 // Capture all the --patch-module entries specified during CDS dump time.
       
  1676 // It also captures the non-existing path(s) and the required file(s) during inspecting
       
  1677 // the entries.
       
  1678 void ClassLoader::setup_patch_mod_path() {
       
  1679   assert(DumpSharedSpaces, "only used with -Xshare:dump");
       
  1680   ResourceMark rm;
       
  1681   GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
       
  1682   if (patch_mod_args != NULL) {
       
  1683     int num_of_entries = patch_mod_args->length();
       
  1684     for (int i = 0; i < num_of_entries; i++) {
       
  1685       const char* module_name = (patch_mod_args->at(i))->module_name();
       
  1686       const char* module_path = (patch_mod_args->at(i))->path_string();
       
  1687       int path_len = (int)strlen(module_path);
       
  1688       int name_len = (int)strlen(module_name);
       
  1689       int buf_len = name_len + path_len + 2; // add 2 for the '=' and NULL terminator
       
  1690       int end = 0;
       
  1691       char* buf = NEW_C_HEAP_ARRAY(char, buf_len, mtInternal);
       
  1692       // Iterate over the module's class path entries
       
  1693       for (int start = 0; start < path_len; start = end) {
       
  1694         while (module_path[end] && module_path[end] != os::path_separator()[0]) {
       
  1695           end++;
       
  1696         }
       
  1697         strncpy(buf, &module_path[start], end - start);
       
  1698         buf[end - start] = '\0';
       
  1699         struct stat st;
       
  1700         if (os::stat(buf, &st) != 0) {
       
  1701           // File not found
       
  1702           _shared_paths_misc_info->add_nonexist_path(buf);
       
  1703         } else {
       
  1704           if ((st.st_mode & S_IFMT) != S_IFREG) { // is not a regular file
       
  1705             vm_exit_during_initialization(
       
  1706               "--patch-module requires a regular file during dumping", buf);
       
  1707           } else {
       
  1708             _shared_paths_misc_info->add_required_file(buf);
       
  1709           }
       
  1710         }
       
  1711         while (module_path[end] == os::path_separator()[0]) {
       
  1712           end++;
       
  1713         }
       
  1714       };
       
  1715       jio_snprintf(buf, buf_len, "%s=%s", module_name, module_path);
       
  1716       _shared_paths_misc_info->add_patch_mod_classpath((const char*)buf);
       
  1717       _num_patch_mod_prefixes++;
       
  1718       FREE_C_HEAP_ARRAY(char, buf);
       
  1719     }
       
  1720   }
       
  1721 }
       
  1722 
  1599 void ClassLoader::initialize_shared_path() {
  1723 void ClassLoader::initialize_shared_path() {
  1600   if (DumpSharedSpaces) {
  1724   if (DumpSharedSpaces) {
       
  1725     setup_patch_mod_path();
  1601     ClassLoaderExt::setup_search_paths();
  1726     ClassLoaderExt::setup_search_paths();
  1602     _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
  1727     _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
  1603   }
  1728   }
  1604 }
  1729 }
  1605 #endif
  1730 #endif