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