79 #include "utilities/macros.hpp"
80 #include "utilities/ostream.hpp"
81 #include "utilities/utf8.hpp"
82
83 #include <ctype.h>
84 #include <stdlib.h>
85
86 // Entry point in java.dll for path canonicalization
87
88 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
89
90 static canonicalize_fn_t CanonicalizeEntry = nullptr;
91
92 // Entry points for jimage.dll for loading jimage file entries
93
94 static JImageOpen_t JImageOpen = nullptr;
95 static JImageClose_t JImageClose = nullptr;
96 static JImageFindResource_t JImageFindResource = nullptr;
97 static JImageGetResource_t JImageGetResource = nullptr;
98
99 // JimageFile pointer, or null if exploded JDK build.
100 static JImageFile* JImage_file = nullptr;
101
102 // Globals
103
104 PerfCounter* ClassLoader::_perf_accumulated_time = nullptr;
105 PerfCounter* ClassLoader::_perf_classes_inited = nullptr;
106 PerfCounter* ClassLoader::_perf_class_init_time = nullptr;
107 PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr;
108 PerfCounter* ClassLoader::_perf_classes_verified = nullptr;
109 PerfCounter* ClassLoader::_perf_class_verify_time = nullptr;
110 PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr;
111 PerfCounter* ClassLoader::_perf_classes_linked = nullptr;
112 PerfCounter* ClassLoader::_perf_class_link_time = nullptr;
113 PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr;
114 PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr;
115 PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr;
116 PerfCounter* ClassLoader::_perf_app_classload_time = nullptr;
117 PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr;
118 PerfCounter* ClassLoader::_perf_app_classload_count = nullptr;
119 PerfCounter* ClassLoader::_perf_define_appclasses = nullptr;
120 PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr;
121 PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr;
136 PerfCounter* ClassLoader::_perf_resolve_indy_count = nullptr;
137 PerfCounter* ClassLoader::_perf_resolve_invokehandle_count = nullptr;
138 PerfCounter* ClassLoader::_perf_resolve_mh_count = nullptr;
139 PerfCounter* ClassLoader::_perf_resolve_mt_count = nullptr;
140
141 void ClassLoader::print_counters(outputStream *st) {
142 st->print_cr("ClassLoader:");
143 st->print_cr(" clinit: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
144 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());
145 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());
146 st->print_cr(" resolve...");
147 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());
148 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());
149 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());
150 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());
151 st->cr();
152 }
153
154 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
155 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
156 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
157
158 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
159 ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr;
160
161 // helper routines
162 #if INCLUDE_CDS
163 static bool string_starts_with(const char* str, const char* str_to_find) {
164 size_t str_len = strlen(str);
165 size_t str_to_find_len = strlen(str_to_find);
166 if (str_to_find_len > str_len) {
167 return false;
168 }
169 return (strncmp(str, str_to_find, str_to_find_len) == 0);
170 }
171 #endif
172
173 static const char* get_jimage_version_string() {
174 static char version_string[10] = "";
175 if (version_string[0] == '\0') {
176 jio_snprintf(version_string, sizeof(version_string), "%d.%d",
177 VM_Version::vm_major_version(), VM_Version::vm_minor_version());
178 }
179 return (const char*)version_string;
180 }
181
182 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
183 size_t str_len = strlen(str);
184 size_t str_to_find_len = strlen(str_to_find);
185 if (str_to_find_len > str_len) {
186 return false;
187 }
188 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
189 }
190
191 // Used to obtain the package name from a fully qualified class name.
192 Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
193 if (name == nullptr) {
194 if (bad_class_name != nullptr) {
195 *bad_class_name = true;
196 }
197 return nullptr;
198 }
199
200 int utf_len = name->utf8_length();
201 const jbyte* base = (const jbyte*)name->base();
216 // In this situation, is_same_class_package returns false.
217 if (*start == JVM_SIGNATURE_CLASS) {
218 if (bad_class_name != nullptr) {
219 *bad_class_name = true;
220 }
221 return nullptr;
222 }
223 }
224 // A class name could have just the slash character in the name,
225 // in which case start > end
226 if (start >= end) {
227 // No package name
228 if (bad_class_name != nullptr) {
229 *bad_class_name = true;
230 }
231 return nullptr;
232 }
233 return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base));
234 }
235
236 // Given a fully qualified package name, find its defining package in the class loader's
237 // package entry table.
238 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
239 if (pkg_name == nullptr) {
240 return nullptr;
241 }
242 PackageEntryTable* pkgEntryTable = loader_data->packages();
243 return pkgEntryTable->lookup_only(pkg_name);
244 }
245
246 const char* ClassPathEntry::copy_path(const char* path) {
247 char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
248 strcpy(copy, path);
249 return copy;
250 }
251
252 ClassPathDirEntry::~ClassPathDirEntry() {
253 FREE_C_HEAP_ARRAY(char, _dir);
254 }
255
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);
365 }
366 // Resource allocated
367 return new ClassFileStream(buffer,
368 filesize,
369 _zip_name);
370 }
371
372 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
373
374 JImageFile* ClassPathImageEntry::jimage() const {
375 return JImage_file;
376 }
377
378 JImageFile* ClassPathImageEntry::jimage_non_null() const {
379 assert(ClassLoader::has_jrt_entry(), "must be");
380 assert(jimage() != nullptr, "should have been opened by ClassLoader::lookup_vm_options "
381 "and remained throughout normal JVM lifetime");
382 return jimage();
383 }
384
385 void ClassPathImageEntry::close_jimage() {
386 if (jimage() != nullptr) {
387 (*JImageClose)(jimage());
388 JImage_file = nullptr;
389 }
390 }
391
392 ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :
393 ClassPathEntry() {
394 guarantee(jimage != nullptr, "jimage file is null");
395 guarantee(name != nullptr, "jimage file name is null");
396 assert(_singleton == nullptr, "VM supports only one jimage");
397 DEBUG_ONLY(_singleton = this);
398 size_t len = strlen(name) + 1;
399 _name = copy_path(name);
400 }
401
402 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
403 return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
404 }
405
406 // For a class in a named module, look it up in the jimage file using this syntax:
407 // /<module-name>/<package-name>/<base-class>
408 //
409 // Assumptions:
410 // 1. There are no unnamed modules in the jimage file.
411 // 2. A package is in at most one module in the jimage file.
412 //
413 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
414 jlong size;
415 JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size);
416
417 if (location == 0) {
418 TempNewSymbol class_name = SymbolTable::new_symbol(name);
419 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
420
421 if (pkg_name != nullptr) {
422 if (!Universe::is_module_initialized()) {
423 location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
424 } else {
425 PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
426 if (package_entry != nullptr) {
427 ResourceMark rm(current);
428 // Get the module name
429 ModuleEntry* module = package_entry->module();
430 assert(module != nullptr, "Boot classLoader package missing module");
431 assert(module->is_named(), "Boot classLoader package is in unnamed module");
432 const char* module_name = module->name()->as_C_string();
433 if (module_name != nullptr) {
434 location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);
435 }
436 }
437 }
438 }
439 }
440 if (location != 0) {
441 if (UsePerfData) {
442 ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
443 }
444 char* data = NEW_RESOURCE_ARRAY(char, size);
445 (*JImageGetResource)(jimage_non_null(), location, data, size);
446 // Resource allocated
447 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
448 return new ClassFileStream((u1*)data,
449 checked_cast<int>(size),
450 _name,
451 true); // from_boot_loader_modules_image
452 }
453
454 return nullptr;
455 }
456
457 JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,
458 const char* module_name,
459 const char* file_name,
460 jlong &size) {
461 return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));
462 }
463
464 bool ClassPathImageEntry::is_modules_image() const {
465 assert(this == _singleton, "VM supports a single jimage");
466 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");
467 return true;
468 }
469
470 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
471 _module_name = module_name;
472 _module_first_entry = nullptr;
473 _module_last_entry = nullptr;
474 }
475
476 ModuleClassPathList::~ModuleClassPathList() {
477 // Clean out each ClassPathEntry on list
478 ClassPathEntry* e = _module_first_entry;
479 while (e != nullptr) {
480 ClassPathEntry* next_entry = e->next();
481 delete e;
482 e = next_entry;
483 }
484 }
485
486 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
601 #if INCLUDE_CDS
602 if (CDSConfig::is_dumping_archive()) {
603 if (!Arguments::has_jimage()) {
604 vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
605 }
606 }
607 #endif
608
609 while (cp_stream.has_next()) {
610 const char* path = cp_stream.get_next();
611
612 if (set_base_piece) {
613 // The first time through the bootstrap_search setup, it must be determined
614 // what the base or core piece of the boot loader search is. Either a java runtime
615 // image is present or this is an exploded module build situation.
616 assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
617 "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
618 struct stat st;
619 if (os::stat(path, &st) == 0) {
620 // Directory found
621 if (JImage_file != nullptr) {
622 assert(Arguments::has_jimage(), "sanity check");
623 const char* canonical_path = get_canonical_path(path, current);
624 assert(canonical_path != nullptr, "canonical_path issue");
625
626 _jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);
627 assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
628 assert(_jrt_entry->jimage() != nullptr, "No java runtime image");
629 } // else it's an exploded build.
630 } else {
631 // If path does not exist, exit
632 vm_exit_during_initialization("Unable to establish the boot loader search path", path);
633 }
634 set_base_piece = false;
635 } else {
636 // Every entry on the boot class path after the initial base piece,
637 // which is set by os::set_boot_path(), is considered an appended entry.
638 update_class_path_entry_list(current, path);
639 }
640 }
641 }
642
643 // Gets the exploded path for the named module. The memory for the path
644 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
645 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
646 const char *home = Arguments::get_java_home();
647 const char file_sep = os::file_separator()[0];
648 // 10 represents the length of "modules" + 2 file separators + \0
1027
1028 // Called by the boot classloader to load classes
1029 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1030 assert(name != nullptr, "invariant");
1031
1032 ResourceMark rm(THREAD);
1033 HandleMark hm(THREAD);
1034
1035 const char* const class_name = name->as_C_string();
1036
1037 EventMarkClassLoading m("Loading class %s", class_name);
1038
1039 const char* const file_name = file_name_for_class_name(class_name,
1040 name->utf8_length());
1041 assert(file_name != nullptr, "invariant");
1042
1043 // Lookup stream for parsing .class file
1044 ClassFileStream* stream = nullptr;
1045 s2 classpath_index = 0;
1046 ClassPathEntry* e = nullptr;
1047
1048 // If search_append_only is true, boot loader visibility boundaries are
1049 // set to be _first_append_entry to the end. This includes:
1050 // [-Xbootclasspath/a]; [jvmti appended entries]
1051 //
1052 // If search_append_only is false, boot loader visibility boundaries are
1053 // set to be the --patch-module entries plus the base piece. This includes:
1054 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1055 //
1056
1057 // Load Attempt #1: --patch-module
1058 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1059 // attempt to load the class from those locations specific to the module.
1060 // Specifications to --patch-module can contain a partial number of classes
1061 // that are part of the overall module definition. So if a particular class is not
1062 // found within its module specification, the search should continue to Load Attempt #2.
1063 // Note: The --patch-module entries are never searched if the boot loader's
1064 // visibility boundary is limited to only searching the append entries.
1065 if (_patch_mod_entries != nullptr && !search_append_only) {
1066 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1067 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1068 }
1069
1070 // Load Attempt #2: [jimage | exploded build]
1071 if (!search_append_only && (nullptr == stream)) {
1072 if (has_jrt_entry()) {
1073 e = _jrt_entry;
1074 stream = _jrt_entry->open_stream(THREAD, file_name);
1075 } else {
1076 // Exploded build - attempt to locate class in its defining module's location.
1077 assert(_exploded_entries != nullptr, "No exploded build entries present");
1078 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1079 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1080 }
1081 }
1082
1083 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1084 if (search_append_only && (nullptr == stream)) {
1085 // For the boot loader append path search, the starting classpath_index
1086 // for the appended piece is always 1 to account for either the
1087 // _jrt_entry or the _exploded_entries.
1096 }
1097 e = e->next();
1098 ++classpath_index;
1099 }
1100 }
1101
1102 if (nullptr == stream) {
1103 return nullptr;
1104 }
1105
1106 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1107 Handle protection_domain;
1108 ClassLoadInfo cl_info(protection_domain);
1109
1110 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1111 name,
1112 loader_data,
1113 cl_info,
1114 CHECK_NULL);
1115 result->set_classpath_index(classpath_index);
1116 return result;
1117 }
1118
1119 #if INCLUDE_CDS
1120 static const char* skip_uri_protocol(const char* source) {
1121 if (strncmp(source, "file:", 5) == 0) {
1122 // file: protocol path could start with file:/ or file:///
1123 // locate the char after all the forward slashes
1124 int offset = 5;
1125 while (*(source + offset) == '/') {
1126 offset++;
1127 }
1128 source += offset;
1129 // for non-windows platforms, move back one char as the path begins with a '/'
1130 #ifndef _WINDOWS
1131 source -= 1;
1132 #endif
1133 } else if (strncmp(source, "jrt:/", 5) == 0) {
1134 source += 5;
1135 }
1172 for (size_t i = 0; i < strlen(uri); ++i) {
1173 char decoded = decode_percent_encoded(uri, i);
1174 path[path_index++] = decoded;
1175 }
1176 path[path_index] = '\0';
1177 return path;
1178 }
1179
1180 // Record the shared classpath index and loader type for classes loaded
1181 // by the builtin loaders at dump time.
1182 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1183 const ClassFileStream* stream, bool redefined) {
1184 assert(CDSConfig::is_dumping_archive(), "sanity");
1185 assert(stream != nullptr, "sanity");
1186
1187 if (ik->is_hidden()) {
1188 record_hidden_class(ik);
1189 return;
1190 }
1191
1192 oop loader = ik->class_loader();
1193 char* src = (char*)stream->source();
1194 if (src == nullptr) {
1195 ik->set_shared_classpath_index(-1); // unsupported location
1196 return;
1197 }
1198
1199 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1200 // A class loaded by a user-defined classloader.
1201 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1202 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1203 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1204 return;
1205 }
1206
1207 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1208
1209 ResourceMark rm(current);
1210 int classpath_index = -1;
1211 bool found_invalid = false;
1379 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1380
1381 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1382 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1383 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1384 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1385
1386 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1387 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1388 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1389 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1390 }
1391 }
1392
1393 // lookup java library entry points
1394 load_java_library();
1395 // jimage library entry points are loaded below, in lookup_vm_options
1396 setup_bootstrap_search_path(THREAD);
1397 }
1398
1399 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1400 jlong size;
1401 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1402 if (location == 0)
1403 return nullptr;
1404 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1405 (*JImageGetResource)(jimage, location, val, size);
1406 val[size] = '\0';
1407 return val;
1408 }
1409
1410 // Lookup VM options embedded in the modules jimage file
1411 char* ClassLoader::lookup_vm_options() {
1412 jint error;
1413 char modules_path[JVM_MAXPATHLEN];
1414 const char* fileSep = os::file_separator();
1415
1416 // Initialize jimage library entry points
1417 load_jimage_library();
1418
1419 jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1420 JImage_file =(*JImageOpen)(modules_path, &error);
1421 if (JImage_file == nullptr) {
1422 return nullptr;
1423 }
1424
1425 const char *jimage_version = get_jimage_version_string();
1426 char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");
1427 return options;
1428 }
1429
1430 bool ClassLoader::is_module_observable(const char* module_name) {
1431 assert(JImageOpen != nullptr, "jimage library should have been opened");
1432 if (JImage_file == nullptr) {
1433 struct stat st;
1434 const char *path = get_exploded_module_path(module_name, true);
1435 bool res = os::stat(path, &st) == 0;
1436 FREE_C_HEAP_ARRAY(char, path);
1437 return res;
1438 }
1439 jlong size;
1440 const char *jimage_version = get_jimage_version_string();
1441 return (*JImageFindResource)(JImage_file, module_name, jimage_version, "module-info.class", &size) != 0;
1442 }
1443
1444 jlong ClassLoader::classloader_time_ms() {
1445 return UsePerfData ?
1446 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1447 }
1448
1449 jlong ClassLoader::class_init_count() {
1450 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1451 }
1452
1453 jlong ClassLoader::class_init_time_ms() {
1454 return UsePerfData ?
1455 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1456 }
1457
1458 jlong ClassLoader::class_verify_time_ms() {
1459 return UsePerfData ?
1460 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1461 }
|
79 #include "utilities/macros.hpp"
80 #include "utilities/ostream.hpp"
81 #include "utilities/utf8.hpp"
82
83 #include <ctype.h>
84 #include <stdlib.h>
85
86 // Entry point in java.dll for path canonicalization
87
88 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
89
90 static canonicalize_fn_t CanonicalizeEntry = nullptr;
91
92 // Entry points for jimage.dll for loading jimage file entries
93
94 static JImageOpen_t JImageOpen = nullptr;
95 static JImageClose_t JImageClose = nullptr;
96 static JImageFindResource_t JImageFindResource = nullptr;
97 static JImageGetResource_t JImageGetResource = nullptr;
98
99 // JImageFile pointer, or null if exploded JDK build.
100 static JImageFile* JImage_file = nullptr;
101
102 // JImageMode status to control preview behaviour. JImage_file is unusable
103 // for normal lookup until (JImage_mode != JIMAGE_MODE_UNINITIALIZED).
104 enum JImageMode {
105 JIMAGE_MODE_UNINITIALIZED = 0,
106 JIMAGE_MODE_DEFAULT = 1,
107 JIMAGE_MODE_ENABLE_PREVIEW = 2
108 };
109 static JImageMode JImage_mode = JIMAGE_MODE_UNINITIALIZED;
110
111 // Globals
112
113 PerfCounter* ClassLoader::_perf_accumulated_time = nullptr;
114 PerfCounter* ClassLoader::_perf_classes_inited = nullptr;
115 PerfCounter* ClassLoader::_perf_class_init_time = nullptr;
116 PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr;
117 PerfCounter* ClassLoader::_perf_classes_verified = nullptr;
118 PerfCounter* ClassLoader::_perf_class_verify_time = nullptr;
119 PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr;
120 PerfCounter* ClassLoader::_perf_classes_linked = nullptr;
121 PerfCounter* ClassLoader::_perf_class_link_time = nullptr;
122 PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr;
123 PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr;
124 PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr;
125 PerfCounter* ClassLoader::_perf_app_classload_time = nullptr;
126 PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr;
127 PerfCounter* ClassLoader::_perf_app_classload_count = nullptr;
128 PerfCounter* ClassLoader::_perf_define_appclasses = nullptr;
129 PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr;
130 PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr;
145 PerfCounter* ClassLoader::_perf_resolve_indy_count = nullptr;
146 PerfCounter* ClassLoader::_perf_resolve_invokehandle_count = nullptr;
147 PerfCounter* ClassLoader::_perf_resolve_mh_count = nullptr;
148 PerfCounter* ClassLoader::_perf_resolve_mt_count = nullptr;
149
150 void ClassLoader::print_counters(outputStream *st) {
151 st->print_cr("ClassLoader:");
152 st->print_cr(" clinit: " JLONG_FORMAT "ms / " JLONG_FORMAT " events", ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
153 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());
154 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());
155 st->print_cr(" resolve...");
156 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());
157 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());
158 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());
159 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());
160 st->cr();
161 }
162
163 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
164 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
165 ClassPathImageEntry* ClassLoader::_jrt_entry = nullptr;
166
167 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
168 ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr;
169
170 // helper routines
171 #if INCLUDE_CDS
172 static bool string_starts_with(const char* str, const char* str_to_find) {
173 size_t str_len = strlen(str);
174 size_t str_to_find_len = strlen(str_to_find);
175 if (str_to_find_len > str_len) {
176 return false;
177 }
178 return (strncmp(str, str_to_find, str_to_find_len) == 0);
179 }
180 #endif
181
182 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
183 size_t str_len = strlen(str);
184 size_t str_to_find_len = strlen(str_to_find);
185 if (str_to_find_len > str_len) {
186 return false;
187 }
188 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
189 }
190
191 // Used to obtain the package name from a fully qualified class name.
192 Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
193 if (name == nullptr) {
194 if (bad_class_name != nullptr) {
195 *bad_class_name = true;
196 }
197 return nullptr;
198 }
199
200 int utf_len = name->utf8_length();
201 const jbyte* base = (const jbyte*)name->base();
216 // In this situation, is_same_class_package returns false.
217 if (*start == JVM_SIGNATURE_CLASS) {
218 if (bad_class_name != nullptr) {
219 *bad_class_name = true;
220 }
221 return nullptr;
222 }
223 }
224 // A class name could have just the slash character in the name,
225 // in which case start > end
226 if (start >= end) {
227 // No package name
228 if (bad_class_name != nullptr) {
229 *bad_class_name = true;
230 }
231 return nullptr;
232 }
233 return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base));
234 }
235
236 // --------------------------------
237 // The following jimage_xxx static functions encapsulate all JImage_file and JImage_mode access.
238 // This is done to make it easy to reason about the JImage file state (exists vs initialized etc.).
239
240 // Opens the named JImage file and sets the JImage file reference.
241 // Returns true if opening the JImage file was successful (see also jimage_exists()).
242 static bool jimage_open(const char* modules_path) {
243 // Currently 'error' is not set to anything useful, so ignore it here.
244 jint error;
245 JImage_file = (*JImageOpen)(modules_path, &error);
246 return JImage_file != nullptr;
247 }
248
249 // Closes and clears the JImage file reference (this will only be called during shutdown).
250 static void jimage_close() {
251 if (JImage_file != nullptr) {
252 (*JImageClose)(JImage_file);
253 JImage_file = nullptr;
254 }
255 }
256
257 // Returns whether a JImage file was opened (but NOT whether it was initialized yet).
258 static bool jimage_exists() {
259 return JImage_file != nullptr;
260 }
261
262 // Returns the JImage file reference (which may or may not be initialized).
263 static JImageFile* jimage_non_null() {
264 assert(jimage_exists(), "should have been opened by ClassLoader::lookup_vm_options "
265 "and remained throughout normal JVM lifetime");
266 return JImage_file;
267 }
268
269 // Called once to set the access mode for resource (i.e. preview or non-preview) before
270 // general resource lookup can occur.
271 static void jimage_init(bool enable_preview) {
272 assert(JImage_mode == JIMAGE_MODE_UNINITIALIZED, "jimage_init must not be called twice");
273 JImage_mode = enable_preview ? JIMAGE_MODE_ENABLE_PREVIEW : JIMAGE_MODE_DEFAULT;
274 }
275
276 // Returns true if jimage_init() has been called. Once the JImage file is initialized,
277 // jimage_is_preview_enabled() can be called to correctly determine the access mode.
278 static bool jimage_is_initialized() {
279 return jimage_exists() && JImage_mode != JIMAGE_MODE_UNINITIALIZED;
280 }
281
282 // Returns the access mode for an initialized JImage file (reflects --enable-preview).
283 static bool jimage_is_preview_enabled() {
284 assert(jimage_is_initialized(), "jimage is not initialized");
285 return JImage_mode == JIMAGE_MODE_ENABLE_PREVIEW;
286 }
287
288 // Looks up the location of a named JImage resource. This "raw" lookup function allows
289 // the preview mode to be manually specified, so must not be accessible outside this
290 // class. ClassPathImageEntry manages all calls for resources after startup is complete.
291 static JImageLocationRef jimage_find_resource(const char* module_name,
292 const char* file_name,
293 bool is_preview,
294 jlong *size) {
295 return ((*JImageFindResource)(jimage_non_null(),
296 module_name,
297 file_name,
298 is_preview,
299 size));
300 }
301 // --------------------------------
302
303 // Given a fully qualified package name, find its defining package in the class loader's
304 // package entry table.
305 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
306 if (pkg_name == nullptr) {
307 return nullptr;
308 }
309 PackageEntryTable* pkgEntryTable = loader_data->packages();
310 return pkgEntryTable->lookup_only(pkg_name);
311 }
312
313 const char* ClassPathEntry::copy_path(const char* path) {
314 char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
315 strcpy(copy, path);
316 return copy;
317 }
318
319 ClassPathDirEntry::~ClassPathDirEntry() {
320 FREE_C_HEAP_ARRAY(char, _dir);
321 }
322
421 return buffer;
422 }
423
424 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
425 jint filesize;
426 u1* buffer = open_entry(current, name, &filesize, false);
427 if (buffer == nullptr) {
428 return nullptr;
429 }
430 if (UsePerfData) {
431 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
432 }
433 // Resource allocated
434 return new ClassFileStream(buffer,
435 filesize,
436 _zip_name);
437 }
438
439 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
440
441 void ClassPathImageEntry::close_jimage() {
442 jimage_close();
443 }
444
445 ClassPathImageEntry::ClassPathImageEntry(const char* name) :
446 ClassPathEntry() {
447 guarantee(jimage_is_initialized(), "jimage is not initialized");
448 guarantee(name != nullptr, "jimage file name is null");
449
450 assert(_singleton == nullptr, "VM supports only one jimage");
451 DEBUG_ONLY(_singleton = this);
452 size_t len = strlen(name) + 1;
453 _name = copy_path(name);
454 }
455
456 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
457 return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
458 }
459
460 // For a class in a named module, look it up in the jimage file using this syntax:
461 // /<module-name>/<package-name>/<base-class>
462 //
463 // Assumptions:
464 // 1. There are no unnamed modules in the jimage file.
465 // 2. A package is in at most one module in the jimage file.
466 //
467 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
468 bool is_preview = jimage_is_preview_enabled();
469
470 jlong size;
471 JImageLocationRef location = jimage_find_resource("", name, is_preview, &size);
472
473 if (location == 0) {
474 TempNewSymbol class_name = SymbolTable::new_symbol(name);
475 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
476
477 if (pkg_name != nullptr) {
478 if (!Universe::is_module_initialized()) {
479 location = jimage_find_resource(JAVA_BASE_NAME, name, is_preview, &size);
480 } else {
481 PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
482 if (package_entry != nullptr) {
483 ResourceMark rm(current);
484 // Get the module name
485 ModuleEntry* module = package_entry->module();
486 assert(module != nullptr, "Boot classLoader package missing module");
487 assert(module->is_named(), "Boot classLoader package is in unnamed module");
488 const char* module_name = module->name()->as_C_string();
489 if (module_name != nullptr) {
490 location = jimage_find_resource(module_name, name, is_preview, &size);
491 }
492 }
493 }
494 }
495 }
496 if (location != 0) {
497 if (UsePerfData) {
498 ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
499 }
500 char* data = NEW_RESOURCE_ARRAY(char, size);
501 (*JImageGetResource)(jimage_non_null(), location, data, size);
502 // Resource allocated
503 assert(this == ClassLoader::get_jrt_entry(), "must be");
504 return new ClassFileStream((u1*)data,
505 checked_cast<int>(size),
506 _name,
507 true); // from_boot_loader_modules_image
508 }
509
510 return nullptr;
511 }
512
513 bool ClassPathImageEntry::is_modules_image() const {
514 assert(this == _singleton, "VM supports a single jimage");
515 assert(this == ClassLoader::get_jrt_entry(), "must be used for jrt entry");
516 return true;
517 }
518
519 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
520 _module_name = module_name;
521 _module_first_entry = nullptr;
522 _module_last_entry = nullptr;
523 }
524
525 ModuleClassPathList::~ModuleClassPathList() {
526 // Clean out each ClassPathEntry on list
527 ClassPathEntry* e = _module_first_entry;
528 while (e != nullptr) {
529 ClassPathEntry* next_entry = e->next();
530 delete e;
531 e = next_entry;
532 }
533 }
534
535 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
650 #if INCLUDE_CDS
651 if (CDSConfig::is_dumping_archive()) {
652 if (!Arguments::has_jimage()) {
653 vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
654 }
655 }
656 #endif
657
658 while (cp_stream.has_next()) {
659 const char* path = cp_stream.get_next();
660
661 if (set_base_piece) {
662 // The first time through the bootstrap_search setup, it must be determined
663 // what the base or core piece of the boot loader search is. Either a java runtime
664 // image is present or this is an exploded module build situation.
665 assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
666 "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
667 struct stat st;
668 if (os::stat(path, &st) == 0) {
669 // Directory found
670 if (jimage_exists()) {
671 assert(Arguments::has_jimage(), "sanity check");
672 const char* canonical_path = get_canonical_path(path, current);
673 assert(canonical_path != nullptr, "canonical_path issue");
674
675 // Hand over lifecycle control of the JImage file to the _jrt_entry singleton
676 // (see ClassPathImageEntry::close_jimage). The image must be initialized by now.
677 _jrt_entry = new ClassPathImageEntry(canonical_path);
678 assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
679 } // else it's an exploded build.
680 } else {
681 // If path does not exist, exit
682 vm_exit_during_initialization("Unable to establish the boot loader search path", path);
683 }
684 set_base_piece = false;
685 } else {
686 // Every entry on the boot class path after the initial base piece,
687 // which is set by os::set_boot_path(), is considered an appended entry.
688 update_class_path_entry_list(current, path);
689 }
690 }
691 }
692
693 // Gets the exploded path for the named module. The memory for the path
694 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
695 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
696 const char *home = Arguments::get_java_home();
697 const char file_sep = os::file_separator()[0];
698 // 10 represents the length of "modules" + 2 file separators + \0
1077
1078 // Called by the boot classloader to load classes
1079 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1080 assert(name != nullptr, "invariant");
1081
1082 ResourceMark rm(THREAD);
1083 HandleMark hm(THREAD);
1084
1085 const char* const class_name = name->as_C_string();
1086
1087 EventMarkClassLoading m("Loading class %s", class_name);
1088
1089 const char* const file_name = file_name_for_class_name(class_name,
1090 name->utf8_length());
1091 assert(file_name != nullptr, "invariant");
1092
1093 // Lookup stream for parsing .class file
1094 ClassFileStream* stream = nullptr;
1095 s2 classpath_index = 0;
1096 ClassPathEntry* e = nullptr;
1097 bool is_patched = false;
1098
1099 // If search_append_only is true, boot loader visibility boundaries are
1100 // set to be _first_append_entry to the end. This includes:
1101 // [-Xbootclasspath/a]; [jvmti appended entries]
1102 //
1103 // If search_append_only is false, boot loader visibility boundaries are
1104 // set to be the --patch-module entries plus the base piece. This includes:
1105 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1106 //
1107
1108 // Load Attempt #1: --patch-module
1109 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1110 // attempt to load the class from those locations specific to the module.
1111 // Specifications to --patch-module can contain a partial number of classes
1112 // that are part of the overall module definition. So if a particular class is not
1113 // found within its module specification, the search should continue to Load Attempt #2.
1114 // Note: The --patch-module entries are never searched if the boot loader's
1115 // visibility boundary is limited to only searching the append entries.
1116 if (_patch_mod_entries != nullptr && !search_append_only) {
1117 // At CDS dump time, the --patch-module entries are ignored. That means a
1118 // class is still loaded from the runtime image even if it might
1119 // appear in the _patch_mod_entries. The runtime shared class visibility
1120 // check will determine if a shared class is visible based on the runtime
1121 // environment, including the runtime --patch-module setting.
1122 if (!CDSConfig::is_valhalla_preview()) {
1123 // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1124 // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1125 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1126 }
1127 if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1128 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1129 if (stream != nullptr) {
1130 is_patched = true;
1131 }
1132 }
1133 }
1134
1135 // Load Attempt #2: [jimage | exploded build]
1136 if (!search_append_only && (nullptr == stream)) {
1137 if (has_jrt_entry()) {
1138 e = _jrt_entry;
1139 stream = _jrt_entry->open_stream(THREAD, file_name);
1140 } else {
1141 // Exploded build - attempt to locate class in its defining module's location.
1142 assert(_exploded_entries != nullptr, "No exploded build entries present");
1143 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1144 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1145 }
1146 }
1147
1148 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1149 if (search_append_only && (nullptr == stream)) {
1150 // For the boot loader append path search, the starting classpath_index
1151 // for the appended piece is always 1 to account for either the
1152 // _jrt_entry or the _exploded_entries.
1161 }
1162 e = e->next();
1163 ++classpath_index;
1164 }
1165 }
1166
1167 if (nullptr == stream) {
1168 return nullptr;
1169 }
1170
1171 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1172 Handle protection_domain;
1173 ClassLoadInfo cl_info(protection_domain);
1174
1175 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1176 name,
1177 loader_data,
1178 cl_info,
1179 CHECK_NULL);
1180 result->set_classpath_index(classpath_index);
1181 if (is_patched) {
1182 result->set_shared_classpath_index(0);
1183 }
1184 return result;
1185 }
1186
1187 #if INCLUDE_CDS
1188 static const char* skip_uri_protocol(const char* source) {
1189 if (strncmp(source, "file:", 5) == 0) {
1190 // file: protocol path could start with file:/ or file:///
1191 // locate the char after all the forward slashes
1192 int offset = 5;
1193 while (*(source + offset) == '/') {
1194 offset++;
1195 }
1196 source += offset;
1197 // for non-windows platforms, move back one char as the path begins with a '/'
1198 #ifndef _WINDOWS
1199 source -= 1;
1200 #endif
1201 } else if (strncmp(source, "jrt:/", 5) == 0) {
1202 source += 5;
1203 }
1240 for (size_t i = 0; i < strlen(uri); ++i) {
1241 char decoded = decode_percent_encoded(uri, i);
1242 path[path_index++] = decoded;
1243 }
1244 path[path_index] = '\0';
1245 return path;
1246 }
1247
1248 // Record the shared classpath index and loader type for classes loaded
1249 // by the builtin loaders at dump time.
1250 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1251 const ClassFileStream* stream, bool redefined) {
1252 assert(CDSConfig::is_dumping_archive(), "sanity");
1253 assert(stream != nullptr, "sanity");
1254
1255 if (ik->is_hidden()) {
1256 record_hidden_class(ik);
1257 return;
1258 }
1259
1260 if (ik->shared_classpath_index() == 0 && ik->defined_by_boot_loader()) {
1261 return;
1262 }
1263
1264 oop loader = ik->class_loader();
1265 char* src = (char*)stream->source();
1266 if (src == nullptr) {
1267 ik->set_shared_classpath_index(-1); // unsupported location
1268 return;
1269 }
1270
1271 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1272 // A class loaded by a user-defined classloader.
1273 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1274 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1275 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1276 return;
1277 }
1278
1279 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1280
1281 ResourceMark rm(current);
1282 int classpath_index = -1;
1283 bool found_invalid = false;
1451 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1452
1453 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1454 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1455 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1456 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1457
1458 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1459 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1460 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1461 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1462 }
1463 }
1464
1465 // lookup java library entry points
1466 load_java_library();
1467 // jimage library entry points are loaded below, in lookup_vm_options
1468 setup_bootstrap_search_path(THREAD);
1469 }
1470
1471 // Lookup VM options embedded in the modules jimage file
1472 char* ClassLoader::lookup_vm_options() {
1473 char modules_path[JVM_MAXPATHLEN];
1474 const char* fileSep = os::file_separator();
1475
1476 // Initialize jimage library entry points
1477 load_jimage_library();
1478
1479 jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1480 if (jimage_open(modules_path)) {
1481 // Special case where we lookup the options string *before* calling jimage_init().
1482 // Since VM arguments have not been parsed, and the ClassPathImageEntry singleton
1483 // has not been created yet, we access the JImage file directly in non-preview mode.
1484 jlong size;
1485 JImageLocationRef location =
1486 jimage_find_resource(JAVA_BASE_NAME, "jdk/internal/vm/options", /* is_preview */ false, &size);
1487 if (location != 0) {
1488 char *options = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1489 (*JImageGetResource)(jimage_non_null(), location, options, size);
1490 options[size] = '\0';
1491 return options;
1492 }
1493 }
1494 return nullptr;
1495 }
1496
1497 // Finishes initializing the JImageFile (if present) by setting the access mode.
1498 void ClassLoader::init_jimage(bool enable_preview) {
1499 if (jimage_exists()) {
1500 jimage_init(enable_preview);
1501 }
1502 }
1503
1504 bool ClassLoader::is_module_observable(const char* module_name) {
1505 assert(JImageOpen != nullptr, "jimage library should have been opened");
1506 if (!jimage_exists()) {
1507 struct stat st;
1508 const char *path = get_exploded_module_path(module_name, true);
1509 bool res = os::stat(path, &st) == 0;
1510 FREE_C_HEAP_ARRAY(char, path);
1511 return res;
1512 }
1513 // We don't expect preview mode (i.e. --enable-preview) to affect module visibility.
1514 jlong size;
1515 return jimage_find_resource(module_name, "module-info.class", /* is_preview */ false, &size) != 0;
1516 }
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 }
|