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, bool multi_release) : ClassPathEntry() {
307 _zip = zip;
308 _zip_name = copy_path(zip_name);
309 _from_class_path_attr = from_class_path_attr;
310 _multi_release = multi_release;
311 }
312
313 ClassPathZipEntry::~ClassPathZipEntry() {
314 ZipLibrary::close(_zip);
315 FREE_C_HEAP_ARRAY(char, _zip_name);
316 }
317
318 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
319 // enable call to C land
320 ThreadToNativeFromVM ttn(current);
321 // check whether zip archive contains name
322 jint name_len;
323 jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
324 if (entry == nullptr) return nullptr;
325 u1* buffer;
326 char name_buf[128];
327 char* filename;
328 if (name_len < 128) {
329 filename = name_buf;
330 } else {
331 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
332 }
333
334 // read contents into resource array
335 size_t size = (uint32_t)(*filesize);
336 if (nul_terminate) {
337 if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
338 return nullptr; // 32-bit integer overflow will occur.
339 }
340 size++;
341 }
342 buffer = NEW_RESOURCE_ARRAY(u1, size);
343 if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
344 return nullptr;
345 }
346
347 // return result
348 if (nul_terminate) {
349 buffer[size - 1] = 0;
350 }
351 return buffer;
352 }
353
354 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
355 jint filesize;
356 u1* buffer = open_entry(current, name, &filesize, false);
357 if (buffer == nullptr) {
358 return nullptr;
359 }
360 if (UsePerfData) {
361 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
524
525 void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
526 const char* bootcp = Arguments::get_boot_class_path();
527 assert(bootcp != nullptr, "Boot class path must not be nullptr");
528 if (PrintSharedArchiveAndExit) {
529 // Don't print bootcp - this is the bootcp of this current VM process, not necessarily
530 // the same as the boot classpath of the shared archive.
531 } else {
532 trace_class_path("bootstrap loader class path=", bootcp);
533 }
534 setup_bootstrap_search_path_impl(current, bootcp);
535 }
536
537 #if INCLUDE_CDS
538 void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
539 assert(CDSConfig::is_dumping_archive(), "sanity");
540
541 ResourceMark rm(current);
542 ClasspathStream cp_stream(class_path);
543
544 while (cp_stream.has_next()) {
545 const char* path = cp_stream.get_next();
546 update_class_path_entry_list(current, path, /* check_for_duplicates */ true,
547 /* is_boot_append */ false, /* from_class_path_attr */ false);
548 }
549 }
550
551 void ClassLoader::add_to_module_path_entries(const char* path,
552 ClassPathEntry* entry) {
553 assert(entry != nullptr, "ClassPathEntry should not be nullptr");
554 assert(CDSConfig::is_dumping_archive(), "sanity");
555
556 // The entry does not exist, add to the list
557 if (_module_path_entries == nullptr) {
558 assert(_last_module_path_entry == nullptr, "Sanity");
559 _module_path_entries = _last_module_path_entry = entry;
560 } else {
561 _last_module_path_entry->set_next(entry);
562 _last_module_path_entry = entry;
563 }
564 }
565
566 // Add a module path to the _module_path_entries list.
567 void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
568 assert(CDSConfig::is_dumping_archive(), "sanity");
748 return ZipLibrary::open(canonical_path, error_msg);
749 }
750
751 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
752 const char *path, const struct stat* st,
753 bool is_boot_append,
754 bool from_class_path_attr,
755 bool is_multi_release) {
756 ClassPathEntry* new_entry = nullptr;
757 if ((st->st_mode & S_IFMT) == S_IFREG) {
758 ResourceMark rm(current);
759 // Regular file, should be a zip file
760 // Canonicalized filename
761 const char* canonical_path = get_canonical_path(path, current);
762 if (canonical_path == nullptr) {
763 return nullptr;
764 }
765 char* error_msg = nullptr;
766 jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
767 if (zip != nullptr && error_msg == nullptr) {
768 new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr, is_multi_release);
769 } else {
770 #if INCLUDE_CDS
771 ClassLoaderExt::set_has_non_jar_in_classpath();
772 #endif
773 return nullptr;
774 }
775 log_info(class, path)("opened: %s", path);
776 log_info(class, load)("opened: %s", path);
777 } else {
778 // Directory
779 new_entry = new ClassPathDirEntry(path);
780 log_info(class, load)("path: %s", path);
781 }
782 return new_entry;
783 }
784
785
786 // Create a class path zip entry for a given path (return null if not found
787 // or zip/JAR file cannot be opened)
788 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
789 // check for a regular file
790 struct stat st;
791 if (os::stat(path, &st) == 0) {
792 if ((st.st_mode & S_IFMT) == S_IFREG) {
793 JavaThread* thread = JavaThread::current();
794 ResourceMark rm(thread);
795 const char* canonical_path = get_canonical_path(path, thread);
796 if (canonical_path != nullptr) {
797 char* error_msg = nullptr;
798 jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
799 if (zip != nullptr && error_msg == nullptr) {
800 // create using canonical path
801 return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false, false);
802 }
803 }
804 }
805 }
806 return nullptr;
807 }
808
809 // The boot append entries are added with a lock, and read lock free.
810 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
811 if (new_entry != nullptr) {
812 MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
813 if (_last_append_entry == nullptr) {
814 _last_append_entry = new_entry;
815 assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
816 Atomic::release_store(&_first_append_entry_list, new_entry);
817 } else {
818 _last_append_entry->set_next(new_entry);
819 _last_append_entry = new_entry;
820 }
821 }
1441 #endif // INCLUDE_CDS
1442
1443 // Initialize the class loader's access to methods in libzip. Parse and
1444 // process the boot classpath into a list ClassPathEntry objects. Once
1445 // this list has been created, it must not change order (see class PackageInfo)
1446 // it can be appended to and is by jvmti.
1447
1448 void ClassLoader::initialize(TRAPS) {
1449 if (UsePerfData) {
1450 // jvmstat performance counters
1451 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1452 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1453 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1454 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1455 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1456 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1457 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1458 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1459 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1460 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1461
1462 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1463 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1464 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1465 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1466 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1467 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1468 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1469 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1470 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1471 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1472 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1473 NEWPERFTICKCOUNTER(_perf_secondary_hash_time, SUN_CLS, "secondarySuperHashTime");
1474
1475 if (log_is_enabled(Info, perf, class, link)) {
1476 NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime");
1477 NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime");
1478 NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount");
1479 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1480
1481 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1482 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1483 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1484 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1485
1486 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1487 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1488 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1489 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1490 }
1491 }
1492
1493 // lookup java library entry points
1494 load_java_library();
1495 // jimage library entry points are loaded below, in lookup_vm_options
1496 setup_bootstrap_search_path(THREAD);
1497 }
1498
1499 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1500 jlong size;
1501 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1502 if (location == 0)
1503 return nullptr;
1504 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1557
1558 // Helper function used by CDS code to get the number of module path
1559 // entries during shared classpath setup time.
1560 int ClassLoader::num_module_path_entries() {
1561 assert(CDSConfig::is_dumping_archive(), "sanity");
1562 int num_entries = 0;
1563 ClassPathEntry* e= ClassLoader::_module_path_entries;
1564 while (e != nullptr) {
1565 num_entries ++;
1566 e = e->next();
1567 }
1568 return num_entries;
1569 }
1570 #endif
1571
1572 jlong ClassLoader::classloader_time_ms() {
1573 return UsePerfData ?
1574 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1575 }
1576
1577 jlong ClassLoader::class_init_count() {
1578 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1579 }
1580
1581 jlong ClassLoader::class_init_time_ms() {
1582 return UsePerfData ?
1583 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1584 }
1585
1586 jlong ClassLoader::class_verify_time_ms() {
1587 return UsePerfData ?
1588 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1589 }
1590
1591 jlong ClassLoader::class_link_count() {
1592 return UsePerfData ? _perf_classes_linked->get_value() : -1;
1593 }
1594
1595 jlong ClassLoader::class_link_time_ms() {
1596 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, bool multi_release) : ClassPathEntry() {
330 _zip = zip;
331 _zip_name = copy_path(zip_name);
332 _from_class_path_attr = from_class_path_attr;
333 _multi_release = multi_release;
334 }
335
336 ClassPathZipEntry::~ClassPathZipEntry() {
337 ZipLibrary::close(_zip);
338 FREE_C_HEAP_ARRAY(char, _zip_name);
339 }
340
341 bool ClassPathZipEntry::has_entry(JavaThread* current, const char* name) {
342 ThreadToNativeFromVM ttn(current);
343 // check whether zip archive contains name
344 jint name_len;
345 jint filesize;
346 jzentry* entry = ZipLibrary::find_entry(_zip, name, &filesize, &name_len);
347 if (entry == nullptr) {
348 return false;
349 } else {
350 ZipLibrary::free_entry(_zip, entry);
351 return true;
352 }
353 }
354
355 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
356 // enable call to C land
357 ThreadToNativeFromVM ttn(current);
358 // check whether zip archive contains name
359 jint name_len;
360 jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
361 if (entry == nullptr) return nullptr;
362 u1* buffer;
363 char name_buf[128];
364 char* filename;
365 if (name_len < 128) {
366 filename = name_buf;
367 } else {
368 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
369 }
370
371 // read contents into resource array
372 size_t size = (uint32_t)(*filesize);
373 if (nul_terminate) {
374 if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
375 return nullptr; // 32-bit integer overflow will occur.
376 }
377 size++;
378 }
379
380 // ZIP_ReadEntry also frees zentry
381 buffer = NEW_RESOURCE_ARRAY(u1, size);
382 if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
383 return nullptr;
384 }
385
386 // return result
387 if (nul_terminate) {
388 buffer[size - 1] = 0;
389 }
390 return buffer;
391 }
392
393 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
394 jint filesize;
395 u1* buffer = open_entry(current, name, &filesize, false);
396 if (buffer == nullptr) {
397 return nullptr;
398 }
399 if (UsePerfData) {
400 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
563
564 void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
565 const char* bootcp = Arguments::get_boot_class_path();
566 assert(bootcp != nullptr, "Boot class path must not be nullptr");
567 if (PrintSharedArchiveAndExit) {
568 // Don't print bootcp - this is the bootcp of this current VM process, not necessarily
569 // the same as the boot classpath of the shared archive.
570 } else {
571 trace_class_path("bootstrap loader class path=", bootcp);
572 }
573 setup_bootstrap_search_path_impl(current, bootcp);
574 }
575
576 #if INCLUDE_CDS
577 void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
578 assert(CDSConfig::is_dumping_archive(), "sanity");
579
580 ResourceMark rm(current);
581 ClasspathStream cp_stream(class_path);
582
583 int exclusion_start_index = INT_MAX;
584 size_t limit = SIZE_MAX;
585 if (CacheOnlyClassesIn != nullptr) {
586 char* appclasspath = Arguments::get_appclasspath();
587 bool bad = false;
588 if (strstr(appclasspath, CacheOnlyClassesIn) != appclasspath) {
589 bad = true;
590 } else {
591 limit = strlen(CacheOnlyClassesIn);
592 if (limit > 0 && limit < strlen(appclasspath)
593 && CacheOnlyClassesIn[limit-1] != os::path_separator()[0]
594 && appclasspath[limit] != os::path_separator()[0]) {
595 bad = true;
596 }
597 }
598 if (bad) {
599 vm_exit_during_initialization(err_msg("CacheOnlyClassesIn \"%s\" must be a proper prefix of the CLASSPATH \"%s\"",
600 CacheOnlyClassesIn, appclasspath));
601 }
602 }
603
604 while (cp_stream.has_next()) {
605 if (cp_stream.num_chars_consumed() >= limit && exclusion_start_index == INT_MAX) {
606 exclusion_start_index = num_boot_classpath_entries() + num_app_classpath_entries();
607 }
608 const char* path = cp_stream.get_next();
609 update_class_path_entry_list(current, path, /* check_for_duplicates */ true,
610 /* is_boot_append */ false, /* from_class_path_attr */ false);
611 }
612
613 ClassLoaderExt::set_app_class_exclusion_start_path_index(exclusion_start_index);
614 if (exclusion_start_index != INT_MAX) {
615 log_info(cds)("Exclude all app classes whose shared_classpath_index is greater than %d", exclusion_start_index);
616 }
617 }
618
619 void ClassLoader::add_to_module_path_entries(const char* path,
620 ClassPathEntry* entry) {
621 assert(entry != nullptr, "ClassPathEntry should not be nullptr");
622 assert(CDSConfig::is_dumping_archive(), "sanity");
623
624 // The entry does not exist, add to the list
625 if (_module_path_entries == nullptr) {
626 assert(_last_module_path_entry == nullptr, "Sanity");
627 _module_path_entries = _last_module_path_entry = entry;
628 } else {
629 _last_module_path_entry->set_next(entry);
630 _last_module_path_entry = entry;
631 }
632 }
633
634 // Add a module path to the _module_path_entries list.
635 void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
636 assert(CDSConfig::is_dumping_archive(), "sanity");
816 return ZipLibrary::open(canonical_path, error_msg);
817 }
818
819 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
820 const char *path, const struct stat* st,
821 bool is_boot_append,
822 bool from_class_path_attr,
823 bool is_multi_release) {
824 ClassPathEntry* new_entry = nullptr;
825 if ((st->st_mode & S_IFMT) == S_IFREG) {
826 ResourceMark rm(current);
827 // Regular file, should be a zip file
828 // Canonicalized filename
829 const char* canonical_path = get_canonical_path(path, current);
830 if (canonical_path == nullptr) {
831 return nullptr;
832 }
833 char* error_msg = nullptr;
834 jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
835 if (zip != nullptr && error_msg == nullptr) {
836 new_entry = new ClassPathZipEntry(zip, path, from_class_path_attr, is_multi_release);
837 } else {
838 #if INCLUDE_CDS
839 ClassLoaderExt::set_has_non_jar_in_classpath();
840 #endif
841 return nullptr;
842 }
843 log_info(class, path)("opened: %s", path);
844 log_info(class, load)("opened: %s", path);
845 } else {
846 // Directory
847 new_entry = new ClassPathDirEntry(path);
848 log_info(class, load)("path: %s", path);
849 }
850 return new_entry;
851 }
852
853
854 // Create a class path zip entry for a given path (return null if not found
855 // or zip/JAR file cannot be opened)
856 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) {
857 // check for a regular file
858 struct stat st;
859 if (os::stat(path, &st) == 0) {
860 if ((st.st_mode & S_IFMT) == S_IFREG) {
861 JavaThread* thread = JavaThread::current();
862 ResourceMark rm(thread);
863 const char* canonical_path = get_canonical_path(path, thread);
864 if (canonical_path != nullptr) {
865 char* error_msg = nullptr;
866 jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
867 if (zip != nullptr && error_msg == nullptr) {
868 // create using canonical path
869 return new ClassPathZipEntry(zip, canonical_path, false, false);
870 }
871 }
872 }
873 }
874 return nullptr;
875 }
876
877 // The boot append entries are added with a lock, and read lock free.
878 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
879 if (new_entry != nullptr) {
880 MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
881 if (_last_append_entry == nullptr) {
882 _last_append_entry = new_entry;
883 assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
884 Atomic::release_store(&_first_append_entry_list, new_entry);
885 } else {
886 _last_append_entry->set_next(new_entry);
887 _last_append_entry = new_entry;
888 }
889 }
1509 #endif // INCLUDE_CDS
1510
1511 // Initialize the class loader's access to methods in libzip. Parse and
1512 // process the boot classpath into a list ClassPathEntry objects. Once
1513 // this list has been created, it must not change order (see class PackageInfo)
1514 // it can be appended to and is by jvmti.
1515
1516 void ClassLoader::initialize(TRAPS) {
1517 if (UsePerfData) {
1518 // jvmstat performance counters
1519 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1520 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1521 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1522 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1523 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1524 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1525 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1526 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1527 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1528 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1529 NEWPERFEVENTCOUNTER(_perf_class_init_bytecodes_count, SUN_CLS, "clinitBytecodesCount");
1530
1531 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1532 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1533 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1534 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1535 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1536 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1537 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1538 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1539 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1540 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1541 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1542 NEWPERFTICKCOUNTER(_perf_secondary_hash_time, SUN_CLS, "secondarySuperHashTime");
1543
1544 if (log_is_enabled(Info, perf, class, link) || log_is_enabled(Info, init)) {
1545 NEWPERFTICKCOUNTER(_perf_preload_total_time, SUN_CLS, "preloadTotalTime");
1546 NEWPERFTICKCOUNTER(_perf_preload_time, SUN_CLS, "preloadTime");
1547 NEWPERFTICKCOUNTER(_perf_prelink_time, SUN_CLS, "prelinkTime");
1548 NEWPERFTICKCOUNTER(_perf_preinit_time, SUN_CLS, "preinitTime");
1549 NEWPERFTICKCOUNTER(_perf_preresolve_time, SUN_CLS, "preresolveTime");
1550
1551 NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime");
1552 NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime");
1553 NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount");
1554 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1555
1556 NEWPERFTICKCOUNTERS(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1557 NEWPERFTICKCOUNTERS(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1558 NEWPERFTICKCOUNTERS(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1559 NEWPERFTICKCOUNTERS(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1560
1561 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1562 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1563 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1564 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1565 }
1566 }
1567
1568 // lookup java library entry points
1569 load_java_library();
1570 // jimage library entry points are loaded below, in lookup_vm_options
1571 setup_bootstrap_search_path(THREAD);
1572 }
1573
1574 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1575 jlong size;
1576 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1577 if (location == 0)
1578 return nullptr;
1579 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1632
1633 // Helper function used by CDS code to get the number of module path
1634 // entries during shared classpath setup time.
1635 int ClassLoader::num_module_path_entries() {
1636 assert(CDSConfig::is_dumping_archive(), "sanity");
1637 int num_entries = 0;
1638 ClassPathEntry* e= ClassLoader::_module_path_entries;
1639 while (e != nullptr) {
1640 num_entries ++;
1641 e = e->next();
1642 }
1643 return num_entries;
1644 }
1645 #endif
1646
1647 jlong ClassLoader::classloader_time_ms() {
1648 return UsePerfData ?
1649 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1650 }
1651
1652 jlong ClassLoader::class_init_bytecodes_count() {
1653 return UsePerfData ? _perf_class_init_bytecodes_count->get_value() : -1;
1654 }
1655
1656 jlong ClassLoader::class_init_count() {
1657 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1658 }
1659
1660 jlong ClassLoader::class_init_time_ms() {
1661 return UsePerfData ?
1662 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1663 }
1664
1665 jlong ClassLoader::class_verify_time_ms() {
1666 return UsePerfData ?
1667 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1668 }
1669
1670 jlong ClassLoader::class_link_count() {
1671 return UsePerfData ? _perf_classes_linked->get_value() : -1;
1672 }
1673
1674 jlong ClassLoader::class_link_time_ms() {
1675 return UsePerfData ?
|