68 #include "services/management.hpp"
69 #include "services/threadService.hpp"
70 #include "utilities/events.hpp"
71 #include "utilities/hashtable.inline.hpp"
72 #include "utilities/macros.hpp"
73 #if INCLUDE_CDS
74 #include "classfile/sharedClassUtil.hpp"
75 #include "classfile/sharedPathsMiscInfo.hpp"
76 #endif
77
78 // Entry points in zip.dll for loading zip/jar file entries
79
80 typedef void * * (JNICALL *ZipOpen_t)(const char *name, char **pmsg);
81 typedef void (JNICALL *ZipClose_t)(jzfile *zip);
82 typedef jzentry* (JNICALL *FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);
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);
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);
87 typedef jint (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len);
88
89 static ZipOpen_t ZipOpen = NULL;
90 static ZipClose_t ZipClose = NULL;
91 static FindEntry_t FindEntry = NULL;
92 static ReadEntry_t ReadEntry = NULL;
93 static ReadMappedEntry_t ReadMappedEntry = NULL;
94 static GetNextEntry_t GetNextEntry = NULL;
95 static canonicalize_fn_t CanonicalizeEntry = NULL;
96 static ZipInflateFully_t ZipInflateFully = NULL;
97 static Crc32_t Crc32 = NULL;
98
99 // Entry points for jimage.dll for loading jimage file entries
100
101 static JImageOpen_t JImageOpen = NULL;
102 static JImageClose_t JImageClose = NULL;
103 static JImagePackageToModule_t JImagePackageToModule = NULL;
104 static JImageFindResource_t JImageFindResource = NULL;
105 static JImageGetResource_t JImageGetResource = NULL;
106 static JImageResourceIterator_t JImageResourceIterator = NULL;
107 static JImage_ResourcePath_t JImageResourcePath = NULL;
108
109 // Globals
110
111 PerfCounter* ClassLoader::_perf_accumulated_time = NULL;
112 PerfCounter* ClassLoader::_perf_classes_inited = NULL;
113 PerfCounter* ClassLoader::_perf_class_init_time = NULL;
114 PerfCounter* ClassLoader::_perf_class_init_selftime = NULL;
115 PerfCounter* ClassLoader::_perf_classes_verified = NULL;
116 PerfCounter* ClassLoader::_perf_class_verify_time = NULL;
117 PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL;
133 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
134 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL;
135 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL;
136 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL;
137 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL;
138 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL;
139 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
140 PerfCounter* ClassLoader::_isUnsyncloadClass = NULL;
141 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL;
142
143 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;
144 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;
145 ClassPathEntry* ClassLoader::_jrt_entry = NULL;
146 ClassPathEntry* ClassLoader::_first_append_entry = NULL;
147 ClassPathEntry* ClassLoader::_last_append_entry = NULL;
148 int ClassLoader::_num_entries = 0;
149 #if INCLUDE_CDS
150 GrowableArray<char*>* ClassLoader::_boot_modules_array = NULL;
151 GrowableArray<char*>* ClassLoader::_platform_modules_array = NULL;
152 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
153 #endif
154
155 // helper routines
156 bool string_starts_with(const char* str, const char* str_to_find) {
157 size_t str_len = strlen(str);
158 size_t str_to_find_len = strlen(str_to_find);
159 if (str_to_find_len > str_len) {
160 return false;
161 }
162 return (strncmp(str, str_to_find, str_to_find_len) == 0);
163 }
164
165 static const char* get_jimage_version_string() {
166 static char version_string[10] = "";
167 if (version_string[0] == '\0') {
168 jio_snprintf(version_string, sizeof(version_string), "%d.%d",
169 Abstract_VM_Version::vm_major_version(), Abstract_VM_Version::vm_minor_version());
170 }
171 return (const char*)version_string;
172 }
302 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN);
303 return NULL;
304 }
305
306 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append) : ClassPathEntry() {
307 _zip = zip;
308 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass);
309 strcpy(copy, zip_name);
310 _zip_name = copy;
311 _is_boot_append = is_boot_append;
312 _multi_versioned = _unknown;
313 }
314
315 ClassPathZipEntry::~ClassPathZipEntry() {
316 if (ZipClose != NULL) {
317 (*ZipClose)(_zip);
318 }
319 FREE_C_HEAP_ARRAY(char, _zip_name);
320 }
321
322 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
323 // enable call to C land
324 JavaThread* thread = JavaThread::current();
325 ThreadToNativeFromVM ttn(thread);
326 // check whether zip archive contains name
327 jint name_len;
328 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);
329 if (entry == NULL) return NULL;
330 u1* buffer;
331 char name_buf[128];
332 char* filename;
333 if (name_len < 128) {
334 filename = name_buf;
335 } else {
336 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
337 }
338
339 // file found, get pointer to the entry in mmapped jar file.
340 if (ReadMappedEntry == NULL ||
341 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) {
623 // For very long paths, we need to print each character separately,
624 // as print_cr() has a length limit
625 while (name[0] != '\0') {
626 out->print("%c", name[0]);
627 name++;
628 }
629 }
630 }
631 out->cr();
632 }
633 }
634
635 #if INCLUDE_CDS
636 void ClassLoader::check_shared_classpath(const char *path) {
637 if (strcmp(path, "") == 0) {
638 exit_with_path_failure("Cannot have empty path in archived classpaths", NULL);
639 }
640
641 struct stat st;
642 if (os::stat(path, &st) == 0) {
643 if ((st.st_mode & S_IFREG) != S_IFREG) { // is directory
644 if (!os::dir_is_empty(path)) {
645 tty->print_cr("Error: non-empty directory '%s'", path);
646 exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL);
647 }
648 }
649 }
650 }
651 #endif
652
653 void ClassLoader::setup_bootstrap_search_path() {
654 const char* sys_class_path = Arguments::get_sysclasspath();
655 const char* java_class_path = Arguments::get_appclasspath();
656 if (PrintSharedArchiveAndExit) {
657 // Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily
658 // the same as the bootcp of the shared archive.
659 } else {
660 trace_class_path("bootstrap loader class path=", sys_class_path);
661 trace_class_path("classpath: ", java_class_path);
662 }
663 #if INCLUDE_CDS
676 void* ClassLoader::get_shared_paths_misc_info() {
677 return _shared_paths_misc_info->buffer();
678 }
679
680 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) {
681 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size);
682 bool result = checker->check();
683 delete checker;
684 return result;
685 }
686 #endif
687
688 // Construct the array of module/path pairs as specified to --patch-module
689 // for the boot loader to search ahead of the jimage, if the class being
690 // loaded is defined to a module that has been specified to --patch-module.
691 void ClassLoader::setup_patch_mod_entries() {
692 Thread* THREAD = Thread::current();
693 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
694 int num_of_entries = patch_mod_args->length();
695
696 assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with --patch-module");
697 assert(!UseSharedSpaces, "UseSharedSpaces not supported with --patch-module");
698
699 // 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);
701
702 for (int i = 0; i < num_of_entries; i++) {
703 const char* module_name = (patch_mod_args->at(i))->module_name();
704 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK);
705 assert(module_sym != NULL, "Failed to obtain Symbol for module name");
706 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
707
708 char* class_path = (patch_mod_args->at(i))->path_string();
709 int len = (int)strlen(class_path);
710 int end = 0;
711 // Iterate over the module's class path entries
712 for (int start = 0; start < len; start = end) {
713 while (class_path[end] && class_path[end] != os::path_separator()[0]) {
714 end++;
715 }
716 EXCEPTION_MARK;
717 ResourceMark rm(THREAD);
834
835 // If the path specification is valid, enter it into this module's list.
836 // There is no need to check for duplicate modules in the exploded entry list,
837 // since no two modules with the same name can be defined to the boot loader.
838 // This is checked at module definition time in Modules::define_module.
839 if (new_entry != NULL) {
840 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
841 module_cpl->add_to_list(new_entry);
842 _exploded_entries->push(module_cpl);
843 log_info(class, load)("path: %s", path);
844 }
845 }
846 FREE_C_HEAP_ARRAY(char, path);
847 }
848
849 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
850 bool throw_exception,
851 bool is_boot_append, TRAPS) {
852 JavaThread* thread = JavaThread::current();
853 ClassPathEntry* new_entry = NULL;
854 if ((st->st_mode & S_IFREG) == S_IFREG) {
855 ResourceMark rm(thread);
856 // Regular file, should be a zip or jimage file
857 // Canonicalized filename
858 char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
859 if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
860 // This matches the classic VM
861 if (throw_exception) {
862 THROW_MSG_(vmSymbols::java_io_IOException(), "Bad pathname", NULL);
863 } else {
864 return NULL;
865 }
866 }
867 jint error;
868 JImageFile* jimage =(*JImageOpen)(canonical_path, &error);
869 if (jimage != NULL) {
870 new_entry = new ClassPathImageEntry(jimage, canonical_path);
871 } else {
872 char* error_msg = NULL;
873 jzfile* zip;
874 {
897 }
898 }
899 }
900 log_info(class, path)("opened: %s", path);
901 log_info(class, load)("opened: %s", path);
902 } else {
903 // Directory
904 new_entry = new ClassPathDirEntry(path);
905 log_info(class, load)("path: %s", path);
906 }
907 return new_entry;
908 }
909
910
911 // Create a class path zip entry for a given path (return NULL if not found
912 // or zip/JAR file cannot be opened)
913 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
914 // check for a regular file
915 struct stat st;
916 if (os::stat(path, &st) == 0) {
917 if ((st.st_mode & S_IFREG) == S_IFREG) {
918 char canonical_path[JVM_MAXPATHLEN];
919 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
920 char* error_msg = NULL;
921 jzfile* zip;
922 {
923 // enable call to C land
924 JavaThread* thread = JavaThread::current();
925 ThreadToNativeFromVM ttn(thread);
926 HandleMark hm(thread);
927 zip = (*ZipOpen)(canonical_path, &error_msg);
928 }
929 if (zip != NULL && error_msg == NULL) {
930 // create using canonical path
931 return new ClassPathZipEntry(zip, canonical_path, is_boot_append);
932 }
933 }
934 }
935 }
936 return NULL;
937 }
1051 os::native_java_library();
1052 // Load zip library
1053 char path[JVM_MAXPATHLEN];
1054 char ebuf[1024];
1055 void* handle = NULL;
1056 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
1057 handle = os::dll_load(path, ebuf, sizeof ebuf);
1058 }
1059 if (handle == NULL) {
1060 vm_exit_during_initialization("Unable to load ZIP library", path);
1061 }
1062 // Lookup zip entry points
1063 ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, os::dll_lookup(handle, "ZIP_Open"));
1064 ZipClose = CAST_TO_FN_PTR(ZipClose_t, os::dll_lookup(handle, "ZIP_Close"));
1065 FindEntry = CAST_TO_FN_PTR(FindEntry_t, os::dll_lookup(handle, "ZIP_FindEntry"));
1066 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"));
1068 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"));
1070 Crc32 = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32"));
1071
1072 // 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 ||
1074 GetNextEntry == NULL || Crc32 == NULL) {
1075 vm_exit_during_initialization("Corrupted ZIP library", path);
1076 }
1077
1078 if (ZipInflateFully == NULL) {
1079 vm_exit_during_initialization("Corrupted ZIP library ZIP_InflateFully missing", path);
1080 }
1081
1082 // Lookup canonicalize entry in libjava.dll
1083 void *javalib_handle = os::native_java_library();
1084 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::dll_lookup(javalib_handle, "Canonicalize"));
1085 // This lookup only works on 1.3. Do not check for non-null here
1086 }
1087
1088 void ClassLoader::load_jimage_library() {
1089 // First make sure native library is loaded
1090 os::native_java_library();
1378 while (e != NULL) {
1379 stream = e->open_stream(file_name, CHECK_NULL);
1380 // No context.check is required since CDS is not supported
1381 // for an exploded modules build or if --patch-module is specified.
1382 if (NULL != stream) {
1383 return stream;
1384 }
1385 e = e->next();
1386 }
1387 // If the module was located, break out even if the class was not
1388 // located successfully from that module's ClassPathEntry list.
1389 // There will not be another valid entry for that module.
1390 return NULL;
1391 }
1392 }
1393 }
1394
1395 return NULL;
1396 }
1397
1398 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
1399 assert(name != NULL, "invariant");
1400 assert(THREAD->is_Java_thread(), "must be a JavaThread");
1401
1402 ResourceMark rm(THREAD);
1403 HandleMark hm(THREAD);
1404
1405 const char* const class_name = name->as_C_string();
1406
1407 EventMark m("loading class %s", class_name);
1408 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion);
1409
1410 const char* const file_name = file_name_for_class_name(class_name,
1411 name->utf8_length());
1412 assert(file_name != NULL, "invariant");
1413
1414 ClassLoaderExt::Context context(class_name, file_name, THREAD);
1415
1416 // Lookup stream for parsing .class file
1417 ClassFileStream* stream = NULL;
1418 s2 classpath_index = 0;
1419 ClassPathEntry* e = NULL;
1420
1421 // If DumpSharedSpaces is true boot loader visibility boundaries are set to:
1422 // - [jimage] + [_first_append_entry to _last_append_entry] (all path entries).
1423 // No --patch-module entries or exploded module builds are included since CDS
1424 // is not supported if --patch-module or exploded module builds are used.
1425 //
1426 // If search_append_only is true, boot loader visibility boundaries are
1427 // set to be _first_append_entry to the end. This includes:
1428 // [-Xbootclasspath/a]; [jvmti appended entries]
1429 //
1430 // If both DumpSharedSpaces and search_append_only are false, boot loader
1431 // visibility boundaries are set to be the --patch-module entries plus the base piece.
1432 // This would include:
1433 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1434 //
1435 // DumpSharedSpaces and search_append_only are mutually exclusive and cannot
1436 // be true at the same time.
1437 assert(!(DumpSharedSpaces && search_append_only), "DumpSharedSpaces and search_append_only are both true");
1438
1439 // Load Attempt #1: --patch-module
1440 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1441 // attempt to load the class from those locations specific to the module.
1442 // 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
1444 // 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
1446 // visibility boundary is limited to only searching the append entries.
1447 if (_patch_mod_entries != NULL && !search_append_only && !DumpSharedSpaces) {
1448 stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL);
1449 }
1450
1451 // Load Attempt #2: [jimage | exploded build]
1452 if (!search_append_only && (NULL == stream)) {
1453 if (has_jrt_entry()) {
1454 e = _jrt_entry;
1455 stream = _jrt_entry->open_stream(file_name, CHECK_NULL);
1456 if (!context.check(stream, classpath_index)) {
1457 return NULL;
1458 }
1459 } else {
1460 // Exploded build - attempt to locate class in its defining module's location.
1461 assert(_exploded_entries != NULL, "No exploded build entries present");
1462 stream = search_module_entries(_exploded_entries, class_name, file_name, CHECK_NULL);
1463 }
1464 }
1465
1466 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1467 if ((search_append_only || DumpSharedSpaces) && (NULL == stream)) {
1468 // For the boot loader append path search, the starting classpath_index
1579 // increment the isUnsyncloadClass counter if UnsyncloadClass is set.
1580 if (UnsyncloadClass) {
1581 _isUnsyncloadClass->inc();
1582 }
1583 }
1584
1585 // lookup zip library entry points
1586 load_zip_library();
1587 // lookup jimage library entry points
1588 load_jimage_library();
1589 #if INCLUDE_CDS
1590 // initialize search path
1591 if (DumpSharedSpaces) {
1592 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
1593 }
1594 #endif
1595 setup_bootstrap_search_path();
1596 }
1597
1598 #if INCLUDE_CDS
1599 void ClassLoader::initialize_shared_path() {
1600 if (DumpSharedSpaces) {
1601 ClassLoaderExt::setup_search_paths();
1602 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
1603 }
1604 }
1605 #endif
1606
1607 jlong ClassLoader::classloader_time_ms() {
1608 return UsePerfData ?
1609 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1610 }
1611
1612 jlong ClassLoader::class_init_count() {
1613 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1614 }
1615
1616 jlong ClassLoader::class_init_time_ms() {
1617 return UsePerfData ?
1618 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1619 }
1620
|
68 #include "services/management.hpp"
69 #include "services/threadService.hpp"
70 #include "utilities/events.hpp"
71 #include "utilities/hashtable.inline.hpp"
72 #include "utilities/macros.hpp"
73 #if INCLUDE_CDS
74 #include "classfile/sharedClassUtil.hpp"
75 #include "classfile/sharedPathsMiscInfo.hpp"
76 #endif
77
78 // Entry points in zip.dll for loading zip/jar file entries
79
80 typedef void * * (JNICALL *ZipOpen_t)(const char *name, char **pmsg);
81 typedef void (JNICALL *ZipClose_t)(jzfile *zip);
82 typedef jzentry* (JNICALL *FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);
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);
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);
87 typedef jint (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len);
88 typedef void (JNICALL *FreeEntry_t)(jzfile *zip, jzentry *entry);
89
90 static ZipOpen_t ZipOpen = NULL;
91 static ZipClose_t ZipClose = NULL;
92 static FindEntry_t FindEntry = NULL;
93 static ReadEntry_t ReadEntry = NULL;
94 static ReadMappedEntry_t ReadMappedEntry = NULL;
95 static GetNextEntry_t GetNextEntry = NULL;
96 static canonicalize_fn_t CanonicalizeEntry = NULL;
97 static ZipInflateFully_t ZipInflateFully = NULL;
98 static Crc32_t Crc32 = NULL;
99 static FreeEntry_t FreeEntry = NULL;
100
101 // Entry points for jimage.dll for loading jimage file entries
102
103 static JImageOpen_t JImageOpen = NULL;
104 static JImageClose_t JImageClose = NULL;
105 static JImagePackageToModule_t JImagePackageToModule = NULL;
106 static JImageFindResource_t JImageFindResource = NULL;
107 static JImageGetResource_t JImageGetResource = NULL;
108 static JImageResourceIterator_t JImageResourceIterator = NULL;
109 static JImage_ResourcePath_t JImageResourcePath = NULL;
110
111 // Globals
112
113 PerfCounter* ClassLoader::_perf_accumulated_time = NULL;
114 PerfCounter* ClassLoader::_perf_classes_inited = NULL;
115 PerfCounter* ClassLoader::_perf_class_init_time = NULL;
116 PerfCounter* ClassLoader::_perf_class_init_selftime = NULL;
117 PerfCounter* ClassLoader::_perf_classes_verified = NULL;
118 PerfCounter* ClassLoader::_perf_class_verify_time = NULL;
119 PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL;
135 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
136 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL;
137 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL;
138 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL;
139 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL;
140 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL;
141 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
142 PerfCounter* ClassLoader::_isUnsyncloadClass = NULL;
143 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL;
144
145 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;
146 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;
147 ClassPathEntry* ClassLoader::_jrt_entry = NULL;
148 ClassPathEntry* ClassLoader::_first_append_entry = NULL;
149 ClassPathEntry* ClassLoader::_last_append_entry = NULL;
150 int ClassLoader::_num_entries = 0;
151 #if INCLUDE_CDS
152 GrowableArray<char*>* ClassLoader::_boot_modules_array = NULL;
153 GrowableArray<char*>* ClassLoader::_platform_modules_array = NULL;
154 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
155 int ClassLoader::_num_patch_mod_prefixes = 0;
156 #endif
157
158 // helper routines
159 bool string_starts_with(const char* str, const char* str_to_find) {
160 size_t str_len = strlen(str);
161 size_t str_to_find_len = strlen(str_to_find);
162 if (str_to_find_len > str_len) {
163 return false;
164 }
165 return (strncmp(str, str_to_find, str_to_find_len) == 0);
166 }
167
168 static const char* get_jimage_version_string() {
169 static char version_string[10] = "";
170 if (version_string[0] == '\0') {
171 jio_snprintf(version_string, sizeof(version_string), "%d.%d",
172 Abstract_VM_Version::vm_major_version(), Abstract_VM_Version::vm_minor_version());
173 }
174 return (const char*)version_string;
175 }
305 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN);
306 return NULL;
307 }
308
309 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append) : ClassPathEntry() {
310 _zip = zip;
311 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass);
312 strcpy(copy, zip_name);
313 _zip_name = copy;
314 _is_boot_append = is_boot_append;
315 _multi_versioned = _unknown;
316 }
317
318 ClassPathZipEntry::~ClassPathZipEntry() {
319 if (ZipClose != NULL) {
320 (*ZipClose)(_zip);
321 }
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;
337 }
338
339 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
340 // enable call to C land
341 JavaThread* thread = JavaThread::current();
342 ThreadToNativeFromVM ttn(thread);
343 // check whether zip archive contains name
344 jint name_len;
345 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);
346 if (entry == NULL) return NULL;
347 u1* buffer;
348 char name_buf[128];
349 char* filename;
350 if (name_len < 128) {
351 filename = name_buf;
352 } else {
353 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
354 }
355
356 // file found, get pointer to the entry in mmapped jar file.
357 if (ReadMappedEntry == NULL ||
358 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) {
640 // For very long paths, we need to print each character separately,
641 // as print_cr() has a length limit
642 while (name[0] != '\0') {
643 out->print("%c", name[0]);
644 name++;
645 }
646 }
647 }
648 out->cr();
649 }
650 }
651
652 #if INCLUDE_CDS
653 void ClassLoader::check_shared_classpath(const char *path) {
654 if (strcmp(path, "") == 0) {
655 exit_with_path_failure("Cannot have empty path in archived classpaths", NULL);
656 }
657
658 struct stat st;
659 if (os::stat(path, &st) == 0) {
660 if ((st.st_mode & S_IFMT) != S_IFREG) { // is directory
661 if (!os::dir_is_empty(path)) {
662 tty->print_cr("Error: non-empty directory '%s'", path);
663 exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL);
664 }
665 }
666 }
667 }
668 #endif
669
670 void ClassLoader::setup_bootstrap_search_path() {
671 const char* sys_class_path = Arguments::get_sysclasspath();
672 const char* java_class_path = Arguments::get_appclasspath();
673 if (PrintSharedArchiveAndExit) {
674 // Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily
675 // the same as the bootcp of the shared archive.
676 } else {
677 trace_class_path("bootstrap loader class path=", sys_class_path);
678 trace_class_path("classpath: ", java_class_path);
679 }
680 #if INCLUDE_CDS
693 void* ClassLoader::get_shared_paths_misc_info() {
694 return _shared_paths_misc_info->buffer();
695 }
696
697 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) {
698 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size);
699 bool result = checker->check();
700 delete checker;
701 return result;
702 }
703 #endif
704
705 // Construct the array of module/path pairs as specified to --patch-module
706 // for the boot loader to search ahead of the jimage, if the class being
707 // loaded is defined to a module that has been specified to --patch-module.
708 void ClassLoader::setup_patch_mod_entries() {
709 Thread* THREAD = Thread::current();
710 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
711 int num_of_entries = patch_mod_args->length();
712
713
714 // Set up the boot loader's _patch_mod_entries list
715 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);
716
717 for (int i = 0; i < num_of_entries; i++) {
718 const char* module_name = (patch_mod_args->at(i))->module_name();
719 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK);
720 assert(module_sym != NULL, "Failed to obtain Symbol for module name");
721 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
722
723 char* class_path = (patch_mod_args->at(i))->path_string();
724 int len = (int)strlen(class_path);
725 int end = 0;
726 // Iterate over the module's class path entries
727 for (int start = 0; start < len; start = end) {
728 while (class_path[end] && class_path[end] != os::path_separator()[0]) {
729 end++;
730 }
731 EXCEPTION_MARK;
732 ResourceMark rm(THREAD);
849
850 // If the path specification is valid, enter it into this module's list.
851 // There is no need to check for duplicate modules in the exploded entry list,
852 // since no two modules with the same name can be defined to the boot loader.
853 // This is checked at module definition time in Modules::define_module.
854 if (new_entry != NULL) {
855 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
856 module_cpl->add_to_list(new_entry);
857 _exploded_entries->push(module_cpl);
858 log_info(class, load)("path: %s", path);
859 }
860 }
861 FREE_C_HEAP_ARRAY(char, path);
862 }
863
864 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
865 bool throw_exception,
866 bool is_boot_append, TRAPS) {
867 JavaThread* thread = JavaThread::current();
868 ClassPathEntry* new_entry = NULL;
869 if ((st->st_mode & S_IFMT) == S_IFREG) {
870 ResourceMark rm(thread);
871 // Regular file, should be a zip or jimage file
872 // Canonicalized filename
873 char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
874 if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
875 // This matches the classic VM
876 if (throw_exception) {
877 THROW_MSG_(vmSymbols::java_io_IOException(), "Bad pathname", NULL);
878 } else {
879 return NULL;
880 }
881 }
882 jint error;
883 JImageFile* jimage =(*JImageOpen)(canonical_path, &error);
884 if (jimage != NULL) {
885 new_entry = new ClassPathImageEntry(jimage, canonical_path);
886 } else {
887 char* error_msg = NULL;
888 jzfile* zip;
889 {
912 }
913 }
914 }
915 log_info(class, path)("opened: %s", path);
916 log_info(class, load)("opened: %s", path);
917 } else {
918 // Directory
919 new_entry = new ClassPathDirEntry(path);
920 log_info(class, load)("path: %s", path);
921 }
922 return new_entry;
923 }
924
925
926 // Create a class path zip entry for a given path (return NULL if not found
927 // or zip/JAR file cannot be opened)
928 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
929 // check for a regular file
930 struct stat st;
931 if (os::stat(path, &st) == 0) {
932 if ((st.st_mode & S_IFMT) == S_IFREG) {
933 char canonical_path[JVM_MAXPATHLEN];
934 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
935 char* error_msg = NULL;
936 jzfile* zip;
937 {
938 // enable call to C land
939 JavaThread* thread = JavaThread::current();
940 ThreadToNativeFromVM ttn(thread);
941 HandleMark hm(thread);
942 zip = (*ZipOpen)(canonical_path, &error_msg);
943 }
944 if (zip != NULL && error_msg == NULL) {
945 // create using canonical path
946 return new ClassPathZipEntry(zip, canonical_path, is_boot_append);
947 }
948 }
949 }
950 }
951 return NULL;
952 }
1066 os::native_java_library();
1067 // Load zip library
1068 char path[JVM_MAXPATHLEN];
1069 char ebuf[1024];
1070 void* handle = NULL;
1071 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
1072 handle = os::dll_load(path, ebuf, sizeof ebuf);
1073 }
1074 if (handle == NULL) {
1075 vm_exit_during_initialization("Unable to load ZIP library", path);
1076 }
1077 // Lookup zip entry points
1078 ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, os::dll_lookup(handle, "ZIP_Open"));
1079 ZipClose = CAST_TO_FN_PTR(ZipClose_t, os::dll_lookup(handle, "ZIP_Close"));
1080 FindEntry = CAST_TO_FN_PTR(FindEntry_t, os::dll_lookup(handle, "ZIP_FindEntry"));
1081 ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry"));
1082 ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry"));
1083 GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry"));
1084 ZipInflateFully = CAST_TO_FN_PTR(ZipInflateFully_t, os::dll_lookup(handle, "ZIP_InflateFully"));
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"));
1087
1088 // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL
1089 if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL ||
1090 GetNextEntry == NULL || Crc32 == NULL) {
1091 vm_exit_during_initialization("Corrupted ZIP library", path);
1092 }
1093
1094 if (ZipInflateFully == NULL) {
1095 vm_exit_during_initialization("Corrupted ZIP library ZIP_InflateFully missing", path);
1096 }
1097
1098 // Lookup canonicalize entry in libjava.dll
1099 void *javalib_handle = os::native_java_library();
1100 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::dll_lookup(javalib_handle, "Canonicalize"));
1101 // This lookup only works on 1.3. Do not check for non-null here
1102 }
1103
1104 void ClassLoader::load_jimage_library() {
1105 // First make sure native library is loaded
1106 os::native_java_library();
1394 while (e != NULL) {
1395 stream = e->open_stream(file_name, CHECK_NULL);
1396 // No context.check is required since CDS is not supported
1397 // for an exploded modules build or if --patch-module is specified.
1398 if (NULL != stream) {
1399 return stream;
1400 }
1401 e = e->next();
1402 }
1403 // If the module was located, break out even if the class was not
1404 // located successfully from that module's ClassPathEntry list.
1405 // There will not be another valid entry for that module.
1406 return NULL;
1407 }
1408 }
1409 }
1410
1411 return NULL;
1412 }
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 } else {
1457 if (DumpSharedSpaces) {
1458 vm_exit_during_initialization("Only jar file in --patch-module is supported for CDS dumping ", e->name());
1459 }
1460 }
1461 }
1462 }
1463 }
1464
1465 return false;
1466 }
1467 #endif // INCLUDE_CDS
1468
1469 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
1470 assert(name != NULL, "invariant");
1471 assert(THREAD->is_Java_thread(), "must be a JavaThread");
1472
1473 ResourceMark rm(THREAD);
1474 HandleMark hm(THREAD);
1475
1476 const char* const class_name = name->as_C_string();
1477
1478 EventMark m("loading class %s", class_name);
1479 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion);
1480
1481 const char* const file_name = file_name_for_class_name(class_name,
1482 name->utf8_length());
1483 assert(file_name != NULL, "invariant");
1484
1485 ClassLoaderExt::Context context(class_name, file_name, THREAD);
1486
1487 // Lookup stream for parsing .class file
1488 ClassFileStream* stream = NULL;
1489 s2 classpath_index = 0;
1490 ClassPathEntry* e = NULL;
1491
1492 // If DumpSharedSpaces is true boot loader visibility boundaries are set to:
1493 // - [jimage] + [_first_append_entry to _last_append_entry] (all path entries).
1494 // If a class is found in the --patch-module entries, the class will not be included in the
1495 // CDS archive. Also, CDS is not supported if exploded module builds are used.
1496 //
1497 // If search_append_only is true, boot loader visibility boundaries are
1498 // set to be _first_append_entry to the end. This includes:
1499 // [-Xbootclasspath/a]; [jvmti appended entries]
1500 //
1501 // If both DumpSharedSpaces and search_append_only are false, boot loader
1502 // visibility boundaries are set to be the --patch-module entries plus the base piece.
1503 // This would include:
1504 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1505 //
1506 // DumpSharedSpaces and search_append_only are mutually exclusive and cannot
1507 // be true at the same time.
1508 assert(!(DumpSharedSpaces && search_append_only), "DumpSharedSpaces and search_append_only are both true");
1509
1510 // Load Attempt #1: --patch-module
1511 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1512 // attempt to load the class from those locations specific to the module.
1513 // Specifications to --patch-module can contain a partial number of classes
1514 // that are part of the overall module definition. So if a particular class is not
1515 // found within its module specification, the search should continue to Load Attempt #2.
1516 // Note: The --patch-module entries are never searched if the boot loader's
1517 // visibility boundary is limited to only searching the append entries.
1518 if (_patch_mod_entries != NULL && !search_append_only) {
1519 if (!DumpSharedSpaces) {
1520 stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL);
1521 } else {
1522 #if INCLUDE_CDS
1523 if (is_in_patch_module(file_name)) {
1524 tty->print_cr("Preload Warning: Skip archiving class %s found in --patch-module entry", class_name);
1525 return NULL;
1526 }
1527 #endif
1528 }
1529 }
1530
1531 // Load Attempt #2: [jimage | exploded build]
1532 if (!search_append_only && (NULL == stream)) {
1533 if (has_jrt_entry()) {
1534 e = _jrt_entry;
1535 stream = _jrt_entry->open_stream(file_name, CHECK_NULL);
1536 if (!context.check(stream, classpath_index)) {
1537 return NULL;
1538 }
1539 } else {
1540 // Exploded build - attempt to locate class in its defining module's location.
1541 assert(_exploded_entries != NULL, "No exploded build entries present");
1542 stream = search_module_entries(_exploded_entries, class_name, file_name, CHECK_NULL);
1543 }
1544 }
1545
1546 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1547 if ((search_append_only || DumpSharedSpaces) && (NULL == stream)) {
1548 // For the boot loader append path search, the starting classpath_index
1659 // increment the isUnsyncloadClass counter if UnsyncloadClass is set.
1660 if (UnsyncloadClass) {
1661 _isUnsyncloadClass->inc();
1662 }
1663 }
1664
1665 // lookup zip library entry points
1666 load_zip_library();
1667 // lookup jimage library entry points
1668 load_jimage_library();
1669 #if INCLUDE_CDS
1670 // initialize search path
1671 if (DumpSharedSpaces) {
1672 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
1673 }
1674 #endif
1675 setup_bootstrap_search_path();
1676 }
1677
1678 #if INCLUDE_CDS
1679 // Capture all the --patch-module entries specified during CDS dump time.
1680 // It also captures the non-existing path(s) and the required file(s) during inspecting
1681 // the entries.
1682 void ClassLoader::setup_patch_mod_path() {
1683 assert(DumpSharedSpaces, "only used with -Xshare:dump");
1684 ResourceMark rm;
1685 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
1686 if (patch_mod_args != NULL) {
1687 int num_of_entries = patch_mod_args->length();
1688 for (int i = 0; i < num_of_entries; i++) {
1689 const char* module_name = (patch_mod_args->at(i))->module_name();
1690 const char* module_path = (patch_mod_args->at(i))->path_string();
1691 int path_len = (int)strlen(module_path);
1692 int name_len = (int)strlen(module_name);
1693 int buf_len = name_len + path_len + 2; // add 2 for the '=' and NULL terminator
1694 int end = 0;
1695 char* buf = NEW_C_HEAP_ARRAY(char, buf_len, mtInternal);
1696 // Iterate over the module's class path entries
1697 for (int start = 0; start < path_len; start = end) {
1698 while (module_path[end] && module_path[end] != os::path_separator()[0]) {
1699 end++;
1700 }
1701 strncpy(buf, &module_path[start], end - start);
1702 buf[end - start] = '\0';
1703 struct stat st;
1704 if (os::stat(buf, &st) != 0) {
1705 // File not found
1706 _shared_paths_misc_info->add_nonexist_path(buf);
1707 } else {
1708 if ((st.st_mode & S_IFMT) != S_IFREG) { // is not a regular file
1709 vm_exit_during_initialization(
1710 "--patch-module requires a regular file during dumping", buf);
1711 } else {
1712 _shared_paths_misc_info->add_required_file(buf);
1713 }
1714 }
1715 while (module_path[end] == os::path_separator()[0]) {
1716 end++;
1717 }
1718 };
1719 jio_snprintf(buf, buf_len, "%s=%s", module_name, module_path);
1720 _shared_paths_misc_info->add_patch_mod_classpath((const char*)buf);
1721 _num_patch_mod_prefixes++;
1722 FREE_C_HEAP_ARRAY(char, buf);
1723 }
1724 }
1725 }
1726
1727 void ClassLoader::initialize_shared_path() {
1728 if (DumpSharedSpaces) {
1729 setup_patch_mod_path();
1730 ClassLoaderExt::setup_search_paths();
1731 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
1732 }
1733 }
1734 #endif
1735
1736 jlong ClassLoader::classloader_time_ms() {
1737 return UsePerfData ?
1738 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1739 }
1740
1741 jlong ClassLoader::class_init_count() {
1742 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1743 }
1744
1745 jlong ClassLoader::class_init_time_ms() {
1746 return UsePerfData ?
1747 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1748 }
1749
|