52 #include "logging/logTag.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/instanceKlass.hpp"
58 #include "oops/instanceRefKlass.hpp"
59 #include "oops/klass.inline.hpp"
60 #include "oops/method.inline.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/symbol.hpp"
64 #include "prims/jvm_misc.hpp"
65 #include "runtime/arguments.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/os.hpp"
72 #include "runtime/perfData.hpp"
73 #include "runtime/threadCritical.hpp"
74 #include "runtime/timer.hpp"
75 #include "runtime/vm_version.hpp"
76 #include "services/management.hpp"
77 #include "services/threadService.hpp"
78 #include "utilities/checkedCast.hpp"
79 #include "utilities/classpathStream.hpp"
80 #include "utilities/events.hpp"
81 #include "utilities/macros.hpp"
82 #include "utilities/ostream.hpp"
83 #include "utilities/utf8.hpp"
84
85 #include <stdlib.h>
86 #include <ctype.h>
87
88 // Entry point in java.dll for path canonicalization
89
90 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
91
92 static canonicalize_fn_t CanonicalizeEntry = nullptr;
93
94 // Entry points for jimage.dll for loading jimage file entries
95
96 static JImageOpen_t JImageOpen = nullptr;
97 static JImageClose_t JImageClose = nullptr;
98 static JImageFindResource_t JImageFindResource = nullptr;
99 static JImageGetResource_t JImageGetResource = nullptr;
100
101 // JimageFile pointer, or null if exploded JDK build.
102 static JImageFile* JImage_file = nullptr;
103
104 // Globals
105
106 PerfCounter* ClassLoader::_perf_accumulated_time = nullptr;
107 PerfCounter* ClassLoader::_perf_classes_inited = nullptr;
108 PerfCounter* ClassLoader::_perf_class_init_time = nullptr;
109 PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr;
110 PerfCounter* ClassLoader::_perf_classes_verified = nullptr;
111 PerfCounter* ClassLoader::_perf_class_verify_time = nullptr;
112 PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr;
113 PerfCounter* ClassLoader::_perf_classes_linked = nullptr;
114 PerfCounter* ClassLoader::_perf_class_link_time = nullptr;
115 PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr;
116 PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr;
117 PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr;
118 PerfCounter* ClassLoader::_perf_app_classload_time = nullptr;
119 PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr;
120 PerfCounter* ClassLoader::_perf_app_classload_count = nullptr;
121 PerfCounter* ClassLoader::_perf_define_appclasses = nullptr;
122 PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr;
123 PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr;
124 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = nullptr;
125 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = nullptr;
126 PerfCounter* ClassLoader::_perf_ik_link_methods_time = nullptr;
127 PerfCounter* ClassLoader::_perf_method_adapters_time = nullptr;
128 PerfCounter* ClassLoader::_perf_ik_link_methods_count = nullptr;
129 PerfCounter* ClassLoader::_perf_method_adapters_count = nullptr;
130 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = nullptr;
131 PerfCounter* ClassLoader::_perf_secondary_hash_time = nullptr;
132
133 PerfCounter* ClassLoader::_perf_resolve_indy_time = nullptr;
134 PerfCounter* ClassLoader::_perf_resolve_invokehandle_time = nullptr;
135 PerfCounter* ClassLoader::_perf_resolve_mh_time = nullptr;
136 PerfCounter* ClassLoader::_perf_resolve_mt_time = nullptr;
137
138 PerfCounter* ClassLoader::_perf_resolve_indy_count = nullptr;
139 PerfCounter* ClassLoader::_perf_resolve_invokehandle_count = nullptr;
140 PerfCounter* ClassLoader::_perf_resolve_mh_count = nullptr;
141 PerfCounter* ClassLoader::_perf_resolve_mt_count = nullptr;
142
143 void ClassLoader::print_counters(outputStream *st) {
144 st->print_cr("ClassLoader:");
145 st->print_cr(" clinit: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
146 st->print_cr(" link methods: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_ik_link_methods_time->get_value()) , _perf_ik_link_methods_count->get_value());
147 st->print_cr(" method adapters: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_method_adapters_time->get_value()) , _perf_method_adapters_count->get_value());
148 st->print_cr(" resolve...");
149 st->print_cr(" invokedynamic: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_resolve_indy_time->get_value()) , _perf_resolve_indy_count->get_value());
150 st->print_cr(" invokehandle: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_resolve_invokehandle_time->get_value()) , _perf_resolve_invokehandle_count->get_value());
151 st->print_cr(" CP_MethodHandle: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_resolve_mh_time->get_value()) , _perf_resolve_mh_count->get_value());
152 st->print_cr(" CP_MethodType: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", Management::ticks_to_ms(_perf_resolve_mt_time->get_value()) , _perf_resolve_mt_count->get_value());
153 st->cr();
154 }
155
156 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
157 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
158 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
159
160 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
161 ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr;
162 #if INCLUDE_CDS
163 ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr;
164 ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr;
165 ClassPathEntry* ClassLoader::_module_path_entries = nullptr;
166 ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr;
167 #endif
168
169 // helper routines
170 #if INCLUDE_CDS
171 static bool string_starts_with(const char* str, const char* str_to_find) {
172 size_t str_len = strlen(str);
173 size_t str_to_find_len = strlen(str_to_find);
286 }
287 #ifdef ASSERT
288 // Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
289 // debug builds so that we guard against use-after-free bugs.
290 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
291 #endif
292 // We don't verify the length of the classfile stream fits in an int, but this is the
293 // bootloader so we have control of this.
294 // Resource allocated
295 return new ClassFileStream(buffer,
296 checked_cast<int>(st.st_size),
297 _dir);
298 }
299 }
300 }
301 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
302 return nullptr;
303 }
304
305 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
306 bool is_boot_append, bool from_class_path_attr) : ClassPathEntry() {
307 _zip = zip;
308 _zip_name = copy_path(zip_name);
309 _from_class_path_attr = from_class_path_attr;
310 }
311
312 ClassPathZipEntry::~ClassPathZipEntry() {
313 ZipLibrary::close(_zip);
314 FREE_C_HEAP_ARRAY(char, _zip_name);
315 }
316
317 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
318 // enable call to C land
319 ThreadToNativeFromVM ttn(current);
320 // check whether zip archive contains name
321 jint name_len;
322 jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
323 if (entry == nullptr) return nullptr;
324 u1* buffer;
325 char name_buf[128];
326 char* filename;
327 if (name_len < 128) {
328 filename = name_buf;
329 } else {
330 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
331 }
332
333 // read contents into resource array
334 size_t size = (uint32_t)(*filesize);
335 if (nul_terminate) {
336 if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
337 return nullptr; // 32-bit integer overflow will occur.
338 }
339 size++;
340 }
341 buffer = NEW_RESOURCE_ARRAY(u1, size);
342 if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
343 return nullptr;
344 }
345
346 // return result
347 if (nul_terminate) {
348 buffer[size - 1] = 0;
349 }
350 return buffer;
351 }
352
353 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
354 jint filesize;
355 u1* buffer = open_entry(current, name, &filesize, false);
356 if (buffer == nullptr) {
357 return nullptr;
358 }
359 if (UsePerfData) {
360 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
523
524 void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
525 const char* bootcp = Arguments::get_boot_class_path();
526 assert(bootcp != nullptr, "Boot class path must not be nullptr");
527 if (PrintSharedArchiveAndExit) {
528 // Don't print bootcp - this is the bootcp of this current VM process, not necessarily
529 // the same as the boot classpath of the shared archive.
530 } else {
531 trace_class_path("bootstrap loader class path=", bootcp);
532 }
533 setup_bootstrap_search_path_impl(current, bootcp);
534 }
535
536 #if INCLUDE_CDS
537 void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
538 assert(CDSConfig::is_dumping_archive(), "sanity");
539
540 ResourceMark rm(current);
541 ClasspathStream cp_stream(class_path);
542
543 while (cp_stream.has_next()) {
544 const char* path = cp_stream.get_next();
545 update_class_path_entry_list(current, path, /* check_for_duplicates */ true,
546 /* is_boot_append */ false, /* from_class_path_attr */ false);
547 }
548 }
549
550 void ClassLoader::add_to_module_path_entries(const char* path,
551 ClassPathEntry* entry) {
552 assert(entry != nullptr, "ClassPathEntry should not be nullptr");
553 assert(CDSConfig::is_dumping_archive(), "sanity");
554
555 // The entry does not exist, add to the list
556 if (_module_path_entries == nullptr) {
557 assert(_last_module_path_entry == nullptr, "Sanity");
558 _module_path_entries = _last_module_path_entry = entry;
559 } else {
560 _last_module_path_entry->set_next(entry);
561 _last_module_path_entry = entry;
562 }
563 }
564
565 // Add a module path to the _module_path_entries list.
566 void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
567 assert(CDSConfig::is_dumping_archive(), "sanity");
746 HandleMark hm(thread);
747 return ZipLibrary::open(canonical_path, error_msg);
748 }
749
750 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
751 const char *path, const struct stat* st,
752 bool is_boot_append,
753 bool from_class_path_attr) {
754 ClassPathEntry* new_entry = nullptr;
755 if ((st->st_mode & S_IFMT) == S_IFREG) {
756 ResourceMark rm(current);
757 // Regular file, should be a zip file
758 // Canonicalized filename
759 const char* canonical_path = get_canonical_path(path, current);
760 if (canonical_path == nullptr) {
761 return nullptr;
762 }
763 char* error_msg = nullptr;
764 jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
765 if (zip != nullptr && error_msg == nullptr) {
766 new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);
767 } else {
768 #if INCLUDE_CDS
769 ClassLoaderExt::set_has_non_jar_in_classpath();
770 #endif
771 return nullptr;
772 }
773 log_info(class, path)("opened: %s", path);
774 log_info(class, load)("opened: %s", path);
775 } else {
776 // Directory
777 new_entry = new ClassPathDirEntry(path);
778 log_info(class, load)("path: %s", path);
779 }
780 return new_entry;
781 }
782
783
784 // Create a class path zip entry for a given path (return null if not found
785 // or zip/JAR file cannot be opened)
786 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
787 // check for a regular file
788 struct stat st;
789 if (os::stat(path, &st) == 0) {
790 if ((st.st_mode & S_IFMT) == S_IFREG) {
791 JavaThread* thread = JavaThread::current();
792 ResourceMark rm(thread);
793 const char* canonical_path = get_canonical_path(path, thread);
794 if (canonical_path != nullptr) {
795 char* error_msg = nullptr;
796 jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
797 if (zip != nullptr && error_msg == nullptr) {
798 // create using canonical path
799 return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);
800 }
801 }
802 }
803 }
804 return nullptr;
805 }
806
807 // The boot append entries are added with a lock, and read lock free.
808 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
809 if (new_entry != nullptr) {
810 MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
811 if (_last_append_entry == nullptr) {
812 _last_append_entry = new_entry;
813 assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
814 Atomic::release_store(&_first_append_entry_list, new_entry);
815 } else {
816 _last_append_entry->set_next(new_entry);
817 _last_append_entry = new_entry;
818 }
819 }
1417 #endif // INCLUDE_CDS
1418
1419 // Initialize the class loader's access to methods in libzip. Parse and
1420 // process the boot classpath into a list ClassPathEntry objects. Once
1421 // this list has been created, it must not change order (see class PackageInfo)
1422 // it can be appended to and is by jvmti.
1423
1424 void ClassLoader::initialize(TRAPS) {
1425 if (UsePerfData) {
1426 // jvmstat performance counters
1427 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1428 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1429 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1430 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1431 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1432 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1433 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1434 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1435 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1436 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1437
1438 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1439 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1440 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1441 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1442 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1443 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1444 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1445 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1446 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1447 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1448 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1449 NEWPERFTICKCOUNTER(_perf_secondary_hash_time, SUN_CLS, "secondarySuperHashTime");
1450
1451 if (log_is_enabled(Info, perf, class, link)) {
1452 NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime");
1453 NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime");
1454 NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount");
1455 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1456
1457 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1458 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1459 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1460 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1461
1462 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1463 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1464 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1465 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1466 }
1467 }
1468
1469 // lookup java library entry points
1470 load_java_library();
1471 // jimage library entry points are loaded below, in lookup_vm_options
1472 setup_bootstrap_search_path(THREAD);
1473 }
1474
1475 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1476 jlong size;
1477 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1478 if (location == 0)
1479 return nullptr;
1480 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1533
1534 // Helper function used by CDS code to get the number of module path
1535 // entries during shared classpath setup time.
1536 int ClassLoader::num_module_path_entries() {
1537 assert(CDSConfig::is_dumping_archive(), "sanity");
1538 int num_entries = 0;
1539 ClassPathEntry* e= ClassLoader::_module_path_entries;
1540 while (e != nullptr) {
1541 num_entries ++;
1542 e = e->next();
1543 }
1544 return num_entries;
1545 }
1546 #endif
1547
1548 jlong ClassLoader::classloader_time_ms() {
1549 return UsePerfData ?
1550 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1551 }
1552
1553 jlong ClassLoader::class_init_count() {
1554 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1555 }
1556
1557 jlong ClassLoader::class_init_time_ms() {
1558 return UsePerfData ?
1559 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1560 }
1561
1562 jlong ClassLoader::class_verify_time_ms() {
1563 return UsePerfData ?
1564 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1565 }
1566
1567 jlong ClassLoader::class_link_count() {
1568 return UsePerfData ? _perf_classes_linked->get_value() : -1;
1569 }
1570
1571 jlong ClassLoader::class_link_time_ms() {
1572 return UsePerfData ?
|
52 #include "logging/logTag.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/instanceKlass.hpp"
58 #include "oops/instanceRefKlass.hpp"
59 #include "oops/klass.inline.hpp"
60 #include "oops/method.inline.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/symbol.hpp"
64 #include "prims/jvm_misc.hpp"
65 #include "runtime/arguments.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/os.hpp"
72 #include "runtime/perfData.inline.hpp"
73 #include "runtime/threadCritical.hpp"
74 #include "runtime/timer.hpp"
75 #include "runtime/vm_version.hpp"
76 #include "services/management.hpp"
77 #include "services/threadService.hpp"
78 #include "utilities/checkedCast.hpp"
79 #include "utilities/classpathStream.hpp"
80 #include "utilities/events.hpp"
81 #include "utilities/macros.hpp"
82 #include "utilities/ostream.hpp"
83 #include "utilities/utf8.hpp"
84
85 #include <stdlib.h>
86 #include <ctype.h>
87
88 // Entry point in java.dll for path canonicalization
89
90 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
91
92 static canonicalize_fn_t CanonicalizeEntry = nullptr;
93
94 // Entry points for jimage.dll for loading jimage file entries
95
96 static JImageOpen_t JImageOpen = nullptr;
97 static JImageClose_t JImageClose = nullptr;
98 static JImageFindResource_t JImageFindResource = nullptr;
99 static JImageGetResource_t JImageGetResource = nullptr;
100
101 // JimageFile pointer, or null if exploded JDK build.
102 static JImageFile* JImage_file = nullptr;
103
104 // Globals
105
106 PerfCounter* ClassLoader::_perf_accumulated_time = nullptr;
107 PerfCounter* ClassLoader::_perf_classes_inited = nullptr;
108 PerfCounter* ClassLoader::_perf_class_init_time = nullptr;
109 PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr;
110 PerfCounter* ClassLoader::_perf_class_init_bytecodes_count = nullptr;
111 PerfCounter* ClassLoader::_perf_classes_verified = nullptr;
112 PerfCounter* ClassLoader::_perf_class_verify_time = nullptr;
113 PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr;
114 PerfCounter* ClassLoader::_perf_classes_linked = nullptr;
115 PerfCounter* ClassLoader::_perf_class_link_time = nullptr;
116 PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr;
117 PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr;
118 PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr;
119 PerfCounter* ClassLoader::_perf_app_classload_time = nullptr;
120 PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr;
121 PerfCounter* ClassLoader::_perf_app_classload_count = nullptr;
122 PerfCounter* ClassLoader::_perf_define_appclasses = nullptr;
123 PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr;
124 PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr;
125 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = nullptr;
126 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = nullptr;
127 PerfCounter* ClassLoader::_perf_preload_total_time = nullptr;
128 PerfCounter* ClassLoader::_perf_preload_time = nullptr;
129 PerfCounter* ClassLoader::_perf_prelink_time = nullptr;
130 PerfCounter* ClassLoader::_perf_preinit_time = nullptr;
131 PerfCounter* ClassLoader::_perf_preresolve_time = nullptr;
132 PerfCounter* ClassLoader::_perf_ik_link_methods_time = nullptr;
133 PerfCounter* ClassLoader::_perf_method_adapters_time = nullptr;
134 PerfCounter* ClassLoader::_perf_ik_link_methods_count = nullptr;
135 PerfCounter* ClassLoader::_perf_method_adapters_count = nullptr;
136 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = nullptr;
137 PerfCounter* ClassLoader::_perf_secondary_hash_time = nullptr;
138
139 PerfTickCounters* ClassLoader::_perf_resolve_indy_time = nullptr;
140 PerfTickCounters* ClassLoader::_perf_resolve_invokehandle_time = nullptr;
141 PerfTickCounters* ClassLoader::_perf_resolve_mh_time = nullptr;
142 PerfTickCounters* ClassLoader::_perf_resolve_mt_time = nullptr;
143
144 PerfCounter* ClassLoader::_perf_resolve_indy_count = nullptr;
145 PerfCounter* ClassLoader::_perf_resolve_invokehandle_count = nullptr;
146 PerfCounter* ClassLoader::_perf_resolve_mh_count = nullptr;
147 PerfCounter* ClassLoader::_perf_resolve_mt_count = nullptr;
148
149 void ClassLoader::print_counters(outputStream *st) {
150 st->print_cr("ClassLoader:");
151 st->print_cr( " clinit: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events",
152 ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
153 st->print_cr(" link methods: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events",
154 Management::ticks_to_us(_perf_ik_link_methods_time->get_value()) , _perf_ik_link_methods_count->get_value());
155 st->print_cr(" method adapters: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events",
156 Management::ticks_to_us(_perf_method_adapters_time->get_value()) , _perf_method_adapters_count->get_value());
157 if (CountBytecodes || CountBytecodesPerThread) {
158 st->print_cr("; executed " JLONG_FORMAT " bytecodes", ClassLoader::class_init_bytecodes_count());
159 }
160 st->print_cr(" resolve...");
161 st->print_cr(" invokedynamic: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events",
162 _perf_resolve_indy_time->elapsed_counter_value_us(),
163 _perf_resolve_indy_time->thread_counter_value_us(),
164 _perf_resolve_indy_count->get_value());
165 st->print_cr(" invokehandle: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events",
166 _perf_resolve_invokehandle_time->elapsed_counter_value_us(),
167 _perf_resolve_invokehandle_time->thread_counter_value_us(),
168 _perf_resolve_invokehandle_count->get_value());
169 st->print_cr(" CP_MethodHandle: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events",
170 _perf_resolve_mh_time->elapsed_counter_value_us(),
171 _perf_resolve_mh_time->thread_counter_value_us(),
172 _perf_resolve_mh_count->get_value());
173 st->print_cr(" CP_MethodType: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events",
174 _perf_resolve_mt_time->elapsed_counter_value_us(),
175 _perf_resolve_mt_time->thread_counter_value_us(),
176 _perf_resolve_mt_count->get_value());
177 }
178
179 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
180 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
181 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
182
183 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
184 ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr;
185 #if INCLUDE_CDS
186 ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr;
187 ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr;
188 ClassPathEntry* ClassLoader::_module_path_entries = nullptr;
189 ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr;
190 #endif
191
192 // helper routines
193 #if INCLUDE_CDS
194 static bool string_starts_with(const char* str, const char* str_to_find) {
195 size_t str_len = strlen(str);
196 size_t str_to_find_len = strlen(str_to_find);
309 }
310 #ifdef ASSERT
311 // Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
312 // debug builds so that we guard against use-after-free bugs.
313 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
314 #endif
315 // We don't verify the length of the classfile stream fits in an int, but this is the
316 // bootloader so we have control of this.
317 // Resource allocated
318 return new ClassFileStream(buffer,
319 checked_cast<int>(st.st_size),
320 _dir);
321 }
322 }
323 }
324 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
325 return nullptr;
326 }
327
328 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
329 bool from_class_path_attr) : ClassPathEntry() {
330 _zip = zip;
331 _zip_name = copy_path(zip_name);
332 _from_class_path_attr = from_class_path_attr;
333 }
334
335 ClassPathZipEntry::~ClassPathZipEntry() {
336 ZipLibrary::close(_zip);
337 FREE_C_HEAP_ARRAY(char, _zip_name);
338 }
339
340 bool ClassPathZipEntry::has_entry(JavaThread* current, const char* name) {
341 ThreadToNativeFromVM ttn(current);
342 // check whether zip archive contains name
343 jint name_len;
344 jint filesize;
345 jzentry* entry = ZipLibrary::find_entry(_zip, name, &filesize, &name_len);
346 if (entry == nullptr) {
347 return false;
348 } else {
349 ZipLibrary::free_entry(_zip, entry);
350 return true;
351 }
352 }
353
354 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
355 // enable call to C land
356 ThreadToNativeFromVM ttn(current);
357 // check whether zip archive contains name
358 jint name_len;
359 jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
360 if (entry == nullptr) return nullptr;
361 u1* buffer;
362 char name_buf[128];
363 char* filename;
364 if (name_len < 128) {
365 filename = name_buf;
366 } else {
367 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
368 }
369
370 // read contents into resource array
371 size_t size = (uint32_t)(*filesize);
372 if (nul_terminate) {
373 if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
374 return nullptr; // 32-bit integer overflow will occur.
375 }
376 size++;
377 }
378
379 // ZIP_ReadEntry also frees zentry
380 buffer = NEW_RESOURCE_ARRAY(u1, size);
381 if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
382 return nullptr;
383 }
384
385 // return result
386 if (nul_terminate) {
387 buffer[size - 1] = 0;
388 }
389 return buffer;
390 }
391
392 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
393 jint filesize;
394 u1* buffer = open_entry(current, name, &filesize, false);
395 if (buffer == nullptr) {
396 return nullptr;
397 }
398 if (UsePerfData) {
399 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
562
563 void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
564 const char* bootcp = Arguments::get_boot_class_path();
565 assert(bootcp != nullptr, "Boot class path must not be nullptr");
566 if (PrintSharedArchiveAndExit) {
567 // Don't print bootcp - this is the bootcp of this current VM process, not necessarily
568 // the same as the boot classpath of the shared archive.
569 } else {
570 trace_class_path("bootstrap loader class path=", bootcp);
571 }
572 setup_bootstrap_search_path_impl(current, bootcp);
573 }
574
575 #if INCLUDE_CDS
576 void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
577 assert(CDSConfig::is_dumping_archive(), "sanity");
578
579 ResourceMark rm(current);
580 ClasspathStream cp_stream(class_path);
581
582 int exclusion_start_index = INT_MAX;
583 size_t limit = SIZE_MAX;
584 if (CacheOnlyClassesIn != nullptr) {
585 char* appclasspath = Arguments::get_appclasspath();
586 bool bad = false;
587 if (strstr(appclasspath, CacheOnlyClassesIn) != appclasspath) {
588 bad = true;
589 } else {
590 limit = strlen(CacheOnlyClassesIn);
591 if (limit > 0 && limit < strlen(appclasspath)
592 && CacheOnlyClassesIn[limit-1] != os::path_separator()[0]
593 && appclasspath[limit] != os::path_separator()[0]) {
594 bad = true;
595 }
596 }
597 if (bad) {
598 vm_exit_during_initialization(err_msg("CacheOnlyClassesIn \"%s\" must be a proper prefix of the CLASSPATH \"%s\"",
599 CacheOnlyClassesIn, appclasspath));
600 }
601 }
602
603 while (cp_stream.has_next()) {
604 if (cp_stream.num_chars_consumed() >= limit && exclusion_start_index == INT_MAX) {
605 exclusion_start_index = num_boot_classpath_entries() + num_app_classpath_entries();
606 }
607 const char* path = cp_stream.get_next();
608 update_class_path_entry_list(current, path, /* check_for_duplicates */ true,
609 /* is_boot_append */ false, /* from_class_path_attr */ false);
610 }
611
612 ClassLoaderExt::set_app_class_exclusion_start_path_index(exclusion_start_index);
613 if (exclusion_start_index != INT_MAX) {
614 log_info(cds)("Exclude all app classes whose shared_classpath_index is greater than %d", exclusion_start_index);
615 }
616 }
617
618 void ClassLoader::add_to_module_path_entries(const char* path,
619 ClassPathEntry* entry) {
620 assert(entry != nullptr, "ClassPathEntry should not be nullptr");
621 assert(CDSConfig::is_dumping_archive(), "sanity");
622
623 // The entry does not exist, add to the list
624 if (_module_path_entries == nullptr) {
625 assert(_last_module_path_entry == nullptr, "Sanity");
626 _module_path_entries = _last_module_path_entry = entry;
627 } else {
628 _last_module_path_entry->set_next(entry);
629 _last_module_path_entry = entry;
630 }
631 }
632
633 // Add a module path to the _module_path_entries list.
634 void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
635 assert(CDSConfig::is_dumping_archive(), "sanity");
814 HandleMark hm(thread);
815 return ZipLibrary::open(canonical_path, error_msg);
816 }
817
818 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
819 const char *path, const struct stat* st,
820 bool is_boot_append,
821 bool from_class_path_attr) {
822 ClassPathEntry* new_entry = nullptr;
823 if ((st->st_mode & S_IFMT) == S_IFREG) {
824 ResourceMark rm(current);
825 // Regular file, should be a zip file
826 // Canonicalized filename
827 const char* canonical_path = get_canonical_path(path, current);
828 if (canonical_path == nullptr) {
829 return nullptr;
830 }
831 char* error_msg = nullptr;
832 jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
833 if (zip != nullptr && error_msg == nullptr) {
834 new_entry = new ClassPathZipEntry(zip, path, from_class_path_attr);
835 } else {
836 #if INCLUDE_CDS
837 ClassLoaderExt::set_has_non_jar_in_classpath();
838 #endif
839 return nullptr;
840 }
841 log_info(class, path)("opened: %s", path);
842 log_info(class, load)("opened: %s", path);
843 } else {
844 // Directory
845 new_entry = new ClassPathDirEntry(path);
846 log_info(class, load)("path: %s", path);
847 }
848 return new_entry;
849 }
850
851
852 // Create a class path zip entry for a given path (return null if not found
853 // or zip/JAR file cannot be opened)
854 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) {
855 // check for a regular file
856 struct stat st;
857 if (os::stat(path, &st) == 0) {
858 if ((st.st_mode & S_IFMT) == S_IFREG) {
859 JavaThread* thread = JavaThread::current();
860 ResourceMark rm(thread);
861 const char* canonical_path = get_canonical_path(path, thread);
862 if (canonical_path != nullptr) {
863 char* error_msg = nullptr;
864 jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
865 if (zip != nullptr && error_msg == nullptr) {
866 // create using canonical path
867 return new ClassPathZipEntry(zip, canonical_path, false);
868 }
869 }
870 }
871 }
872 return nullptr;
873 }
874
875 // The boot append entries are added with a lock, and read lock free.
876 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
877 if (new_entry != nullptr) {
878 MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
879 if (_last_append_entry == nullptr) {
880 _last_append_entry = new_entry;
881 assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
882 Atomic::release_store(&_first_append_entry_list, new_entry);
883 } else {
884 _last_append_entry->set_next(new_entry);
885 _last_append_entry = new_entry;
886 }
887 }
1485 #endif // INCLUDE_CDS
1486
1487 // Initialize the class loader's access to methods in libzip. Parse and
1488 // process the boot classpath into a list ClassPathEntry objects. Once
1489 // this list has been created, it must not change order (see class PackageInfo)
1490 // it can be appended to and is by jvmti.
1491
1492 void ClassLoader::initialize(TRAPS) {
1493 if (UsePerfData) {
1494 // jvmstat performance counters
1495 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1496 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1497 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1498 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1499 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1500 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1501 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1502 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1503 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1504 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1505 NEWPERFEVENTCOUNTER(_perf_class_init_bytecodes_count, SUN_CLS, "clinitBytecodesCount");
1506
1507 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1508 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1509 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1510 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1511 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1512 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1513 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1514 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1515 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1516 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1517 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1518 NEWPERFTICKCOUNTER(_perf_secondary_hash_time, SUN_CLS, "secondarySuperHashTime");
1519
1520 if (log_is_enabled(Info, perf, class, link) || log_is_enabled(Info, init)) {
1521 NEWPERFTICKCOUNTER(_perf_preload_total_time, SUN_CLS, "preloadTotalTime");
1522 NEWPERFTICKCOUNTER(_perf_preload_time, SUN_CLS, "preloadTime");
1523 NEWPERFTICKCOUNTER(_perf_prelink_time, SUN_CLS, "prelinkTime");
1524 NEWPERFTICKCOUNTER(_perf_preinit_time, SUN_CLS, "preinitTime");
1525 NEWPERFTICKCOUNTER(_perf_preresolve_time, SUN_CLS, "preresolveTime");
1526
1527 NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime");
1528 NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime");
1529 NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount");
1530 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1531
1532 NEWPERFTICKCOUNTERS(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1533 NEWPERFTICKCOUNTERS(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1534 NEWPERFTICKCOUNTERS(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1535 NEWPERFTICKCOUNTERS(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1536
1537 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1538 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1539 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1540 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1541 }
1542 }
1543
1544 // lookup java library entry points
1545 load_java_library();
1546 // jimage library entry points are loaded below, in lookup_vm_options
1547 setup_bootstrap_search_path(THREAD);
1548 }
1549
1550 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1551 jlong size;
1552 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1553 if (location == 0)
1554 return nullptr;
1555 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1608
1609 // Helper function used by CDS code to get the number of module path
1610 // entries during shared classpath setup time.
1611 int ClassLoader::num_module_path_entries() {
1612 assert(CDSConfig::is_dumping_archive(), "sanity");
1613 int num_entries = 0;
1614 ClassPathEntry* e= ClassLoader::_module_path_entries;
1615 while (e != nullptr) {
1616 num_entries ++;
1617 e = e->next();
1618 }
1619 return num_entries;
1620 }
1621 #endif
1622
1623 jlong ClassLoader::classloader_time_ms() {
1624 return UsePerfData ?
1625 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1626 }
1627
1628 jlong ClassLoader::class_init_bytecodes_count() {
1629 return UsePerfData ? _perf_class_init_bytecodes_count->get_value() : -1;
1630 }
1631
1632 jlong ClassLoader::class_init_count() {
1633 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1634 }
1635
1636 jlong ClassLoader::class_init_time_ms() {
1637 return UsePerfData ?
1638 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1639 }
1640
1641 jlong ClassLoader::class_verify_time_ms() {
1642 return UsePerfData ?
1643 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1644 }
1645
1646 jlong ClassLoader::class_link_count() {
1647 return UsePerfData ? _perf_classes_linked->get_value() : -1;
1648 }
1649
1650 jlong ClassLoader::class_link_time_ms() {
1651 return UsePerfData ?
|