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 = 0;
416
417 TempNewSymbol class_name = SymbolTable::new_symbol(name);
418 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
419
420 if (pkg_name != nullptr) {
421 if (!Universe::is_module_initialized()) {
422 location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
423 } else {
424 PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
425 if (package_entry != nullptr) {
426 ResourceMark rm(current);
427 // Get the module name
428 ModuleEntry* module = package_entry->module();
429 assert(module != nullptr, "Boot classLoader package missing module");
430 assert(module->is_named(), "Boot classLoader package is in unnamed module");
431 const char* module_name = module->name()->as_C_string();
432 if (module_name != nullptr) {
433 location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);
434 }
435 }
436 }
437 }
438
439 if (location != 0) {
440 if (UsePerfData) {
441 ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
442 }
443 char* data = NEW_RESOURCE_ARRAY(char, size);
444 (*JImageGetResource)(jimage_non_null(), location, data, size);
445 // Resource allocated
446 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
447 return new ClassFileStream((u1*)data,
448 checked_cast<int>(size),
449 _name,
450 true); // from_boot_loader_modules_image
451 }
452
453 return nullptr;
454 }
455
456 JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,
457 const char* module_name,
458 const char* file_name,
459 jlong &size) {
460 return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));
461 }
462
463 bool ClassPathImageEntry::is_modules_image() const {
464 assert(this == _singleton, "VM supports a single jimage");
465 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");
466 return true;
467 }
468
469 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
470 _module_name = module_name;
471 _module_first_entry = nullptr;
472 _module_last_entry = nullptr;
473 }
474
475 ModuleClassPathList::~ModuleClassPathList() {
476 // Clean out each ClassPathEntry on list
477 ClassPathEntry* e = _module_first_entry;
478 while (e != nullptr) {
479 ClassPathEntry* next_entry = e->next();
480 delete e;
481 e = next_entry;
482 }
483 }
484
485 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
600 #if INCLUDE_CDS
601 if (CDSConfig::is_dumping_archive()) {
602 if (!Arguments::has_jimage()) {
603 vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
604 }
605 }
606 #endif
607
608 while (cp_stream.has_next()) {
609 const char* path = cp_stream.get_next();
610
611 if (set_base_piece) {
612 // The first time through the bootstrap_search setup, it must be determined
613 // what the base or core piece of the boot loader search is. Either a java runtime
614 // image is present or this is an exploded module build situation.
615 assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
616 "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
617 struct stat st;
618 if (os::stat(path, &st) == 0) {
619 // Directory found
620 if (JImage_file != nullptr) {
621 assert(Arguments::has_jimage(), "sanity check");
622 const char* canonical_path = get_canonical_path(path, current);
623 assert(canonical_path != nullptr, "canonical_path issue");
624
625 _jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);
626 assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
627 assert(_jrt_entry->jimage() != nullptr, "No java runtime image");
628 } // else it's an exploded build.
629 } else {
630 // If path does not exist, exit
631 vm_exit_during_initialization("Unable to establish the boot loader search path", path);
632 }
633 set_base_piece = false;
634 } else {
635 // Every entry on the boot class path after the initial base piece,
636 // which is set by os::set_boot_path(), is considered an appended entry.
637 update_class_path_entry_list(current, path);
638 }
639 }
640 }
641
642 // Gets the exploded path for the named module. The memory for the path
643 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
644 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
645 const char *home = Arguments::get_java_home();
646 const char file_sep = os::file_separator()[0];
647 // 10 represents the length of "modules" + 2 file separators + \0
648 size_t len = strlen(home) + strlen(module_name) + 10;
649 char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len);
650 jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
651 return path;
652 }
653
654 // During an exploded modules build, each module defined to the boot loader
655 // will be added to the ClassLoader::_exploded_entries array.
656 void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
657 assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
658 assert(_exploded_entries != nullptr, "_exploded_entries was not initialized");
659
660 // Find the module's symbol
661 ResourceMark rm(current);
662 const char *module_name = module_sym->as_C_string();
663 const char *path = get_exploded_module_path(module_name, false);
664
665 struct stat st;
666 if (os::stat(path, &st) == 0) {
667 // Directory found
668 ClassPathEntry* new_entry = create_class_path_entry(current, path, &st);
669
670 // If the path specification is valid, enter it into this module's list.
671 // There is no need to check for duplicate modules in the exploded entry list,
672 // since no two modules with the same name can be defined to the boot loader.
673 // This is checked at module definition time in Modules::define_module.
674 if (new_entry != nullptr) {
675 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
676 module_cpl->add_to_list(new_entry);
677 {
678 MutexLocker ml(current, Module_lock);
679 _exploded_entries->push(module_cpl);
680 }
681 log_info(class, load)("path: %s", path);
682 }
683 }
684 }
685
686 jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
687 // enable call to C land
688 ThreadToNativeFromVM ttn(thread);
689 HandleMark hm(thread);
690 return ZipLibrary::open(canonical_path, error_msg);
691 }
692
693 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
694 const char *path, const struct stat* st) {
695 ClassPathEntry* new_entry = nullptr;
696 if ((st->st_mode & S_IFMT) == S_IFREG) {
697 ResourceMark rm(current);
698 // Regular file, should be a zip file
699 // Canonicalized filename
700 const char* canonical_path = get_canonical_path(path, current);
701 if (canonical_path == nullptr) {
1026
1027 // Called by the boot classloader to load classes
1028 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1029 assert(name != nullptr, "invariant");
1030
1031 ResourceMark rm(THREAD);
1032 HandleMark hm(THREAD);
1033
1034 const char* const class_name = name->as_C_string();
1035
1036 EventMarkClassLoading m("Loading class %s", class_name);
1037
1038 const char* const file_name = file_name_for_class_name(class_name,
1039 name->utf8_length());
1040 assert(file_name != nullptr, "invariant");
1041
1042 // Lookup stream for parsing .class file
1043 ClassFileStream* stream = nullptr;
1044 s2 classpath_index = 0;
1045 ClassPathEntry* e = nullptr;
1046
1047 // If search_append_only is true, boot loader visibility boundaries are
1048 // set to be _first_append_entry to the end. This includes:
1049 // [-Xbootclasspath/a]; [jvmti appended entries]
1050 //
1051 // If search_append_only is false, boot loader visibility boundaries are
1052 // set to be the --patch-module entries plus the base piece. This includes:
1053 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1054 //
1055
1056 // Load Attempt #1: --patch-module
1057 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1058 // attempt to load the class from those locations specific to the module.
1059 // Specifications to --patch-module can contain a partial number of classes
1060 // that are part of the overall module definition. So if a particular class is not
1061 // found within its module specification, the search should continue to Load Attempt #2.
1062 // Note: The --patch-module entries are never searched if the boot loader's
1063 // visibility boundary is limited to only searching the append entries.
1064 if (_patch_mod_entries != nullptr && !search_append_only) {
1065 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1066 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1067 }
1068
1069 // Load Attempt #2: [jimage | exploded build]
1070 if (!search_append_only && (nullptr == stream)) {
1071 if (has_jrt_entry()) {
1072 e = _jrt_entry;
1073 stream = _jrt_entry->open_stream(THREAD, file_name);
1074 } else {
1075 // Exploded build - attempt to locate class in its defining module's location.
1076 assert(_exploded_entries != nullptr, "No exploded build entries present");
1077 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1078 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1079 }
1080 }
1081
1082 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1083 if (search_append_only && (nullptr == stream)) {
1084 // For the boot loader append path search, the starting classpath_index
1085 // for the appended piece is always 1 to account for either the
1086 // _jrt_entry or the _exploded_entries.
1095 }
1096 e = e->next();
1097 ++classpath_index;
1098 }
1099 }
1100
1101 if (nullptr == stream) {
1102 return nullptr;
1103 }
1104
1105 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1106 Handle protection_domain;
1107 ClassLoadInfo cl_info(protection_domain);
1108
1109 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1110 name,
1111 loader_data,
1112 cl_info,
1113 CHECK_NULL);
1114 result->set_classpath_index(classpath_index);
1115 return result;
1116 }
1117
1118 #if INCLUDE_CDS
1119 static const char* skip_uri_protocol(const char* source) {
1120 if (strncmp(source, "file:", 5) == 0) {
1121 // file: protocol path could start with file:/ or file:///
1122 // locate the char after all the forward slashes
1123 int offset = 5;
1124 while (*(source + offset) == '/') {
1125 offset++;
1126 }
1127 source += offset;
1128 // for non-windows platforms, move back one char as the path begins with a '/'
1129 #ifndef _WINDOWS
1130 source -= 1;
1131 #endif
1132 } else if (strncmp(source, "jrt:/", 5) == 0) {
1133 source += 5;
1134 }
1171 for (size_t i = 0; i < strlen(uri); ++i) {
1172 char decoded = decode_percent_encoded(uri, i);
1173 path[path_index++] = decoded;
1174 }
1175 path[path_index] = '\0';
1176 return path;
1177 }
1178
1179 // Record the shared classpath index and loader type for classes loaded
1180 // by the builtin loaders at dump time.
1181 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1182 const ClassFileStream* stream, bool redefined) {
1183 assert(CDSConfig::is_dumping_archive(), "sanity");
1184 assert(stream != nullptr, "sanity");
1185
1186 if (ik->is_hidden()) {
1187 record_hidden_class(ik);
1188 return;
1189 }
1190
1191 oop loader = ik->class_loader();
1192 char* src = (char*)stream->source();
1193 if (src == nullptr) {
1194 ik->set_shared_classpath_index(-1); // unsupported location
1195 return;
1196 }
1197
1198 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1199 // A class loaded by a user-defined classloader.
1200 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1201 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1202 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1203 return;
1204 }
1205
1206 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1207
1208 ResourceMark rm(current);
1209 int classpath_index = -1;
1210 bool found_invalid = false;
1378 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1379
1380 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1381 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1382 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1383 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1384
1385 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1386 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1387 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1388 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1389 }
1390 }
1391
1392 // lookup java library entry points
1393 load_java_library();
1394 // jimage library entry points are loaded below, in lookup_vm_options
1395 setup_bootstrap_search_path(THREAD);
1396 }
1397
1398 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1399 jlong size;
1400 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1401 if (location == 0)
1402 return nullptr;
1403 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1404 (*JImageGetResource)(jimage, location, val, size);
1405 val[size] = '\0';
1406 return val;
1407 }
1408
1409 // Lookup VM options embedded in the modules jimage file
1410 char* ClassLoader::lookup_vm_options() {
1411 jint error;
1412 char modules_path[JVM_MAXPATHLEN];
1413 const char* fileSep = os::file_separator();
1414
1415 // Initialize jimage library entry points
1416 load_jimage_library();
1417
1418 jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1419 JImage_file =(*JImageOpen)(modules_path, &error);
1420 if (JImage_file == nullptr) {
1421 return nullptr;
1422 }
1423
1424 const char *jimage_version = get_jimage_version_string();
1425 char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");
1426 return options;
1427 }
1428
1429 bool ClassLoader::is_module_observable(const char* module_name) {
1430 assert(JImageOpen != nullptr, "jimage library should have been opened");
1431 if (JImage_file == nullptr) {
1432 struct stat st;
1433 const char *path = get_exploded_module_path(module_name, true);
1434 bool res = os::stat(path, &st) == 0;
1435 FREE_C_HEAP_ARRAY(char, path);
1436 return res;
1437 }
1438 jlong size;
1439 const char *jimage_version = get_jimage_version_string();
1440 return (*JImageFindResource)(JImage_file, module_name, jimage_version, "module-info.class", &size) != 0;
1441 }
1442
1443 jlong ClassLoader::classloader_time_ms() {
1444 return UsePerfData ?
1445 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1446 }
1447
1448 jlong ClassLoader::class_init_count() {
1449 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1450 }
1451
1452 jlong ClassLoader::class_init_time_ms() {
1453 return UsePerfData ?
1454 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1455 }
1456
1457 jlong ClassLoader::class_verify_time_ms() {
1458 return UsePerfData ?
1459 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1460 }
|
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 // PreviewMode status to control preview behaviour. JImage_file is unusable
103 // for normal lookup until (Preview_mode != PREVIEW_MODE_UNINITIALIZED).
104 enum PreviewMode {
105 PREVIEW_MODE_UNINITIALIZED = 0,
106 PREVIEW_MODE_DEFAULT = 1,
107 PREVIEW_MODE_ENABLE_PREVIEW = 2
108 };
109 static PreviewMode Preview_mode = PREVIEW_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 Preview_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 // Returns true if jimage_init() has been called. Once the JImage file is initialized,
270 // jimage_is_preview_enabled() can be called to correctly determine the access mode.
271 static bool jimage_is_initialized() {
272 return jimage_exists() && Preview_mode != PREVIEW_MODE_UNINITIALIZED;
273 }
274
275 // Returns the access mode for an initialized JImage file (reflects --enable-preview).
276 static bool is_preview_enabled() {
277 return Preview_mode == PREVIEW_MODE_ENABLE_PREVIEW;
278 }
279
280 // Looks up the location of a named JImage resource. This "raw" lookup function allows
281 // the preview mode to be manually specified, so must not be accessible outside this
282 // class. ClassPathImageEntry manages all calls for resources after startup is complete.
283 static JImageLocationRef jimage_find_resource(const char* module_name,
284 const char* file_name,
285 bool is_preview,
286 jlong *size) {
287 return ((*JImageFindResource)(jimage_non_null(),
288 module_name,
289 file_name,
290 is_preview,
291 size));
292 }
293 // --------------------------------
294
295 // Given a fully qualified package name, find its defining package in the class loader's
296 // package entry table.
297 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
298 if (pkg_name == nullptr) {
299 return nullptr;
300 }
301 PackageEntryTable* pkgEntryTable = loader_data->packages();
302 return pkgEntryTable->lookup_only(pkg_name);
303 }
304
305 const char* ClassPathEntry::copy_path(const char* path) {
306 char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
307 strcpy(copy, path);
308 return copy;
309 }
310
311 ClassPathDirEntry::~ClassPathDirEntry() {
312 FREE_C_HEAP_ARRAY(char, _dir);
313 }
314
413 return buffer;
414 }
415
416 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
417 jint filesize;
418 u1* buffer = open_entry(current, name, &filesize, false);
419 if (buffer == nullptr) {
420 return nullptr;
421 }
422 if (UsePerfData) {
423 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
424 }
425 // Resource allocated
426 return new ClassFileStream(buffer,
427 filesize,
428 _zip_name);
429 }
430
431 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
432
433 void ClassPathImageEntry::close_jimage() {
434 jimage_close();
435 }
436
437 ClassPathImageEntry::ClassPathImageEntry(const char* name) :
438 ClassPathEntry() {
439 guarantee(jimage_is_initialized(), "jimage is not initialized");
440 guarantee(name != nullptr, "jimage file name is null");
441
442 assert(_singleton == nullptr, "VM supports only one jimage");
443 DEBUG_ONLY(_singleton = this);
444 size_t len = strlen(name) + 1;
445 _name = copy_path(name);
446 }
447
448 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
449 return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
450 }
451
452 // For a class in a named module, look it up in the jimage file using this syntax:
453 // /<module-name>/<package-name>/<base-class>
454 //
455 // Assumptions:
456 // 1. There are no unnamed modules in the jimage file.
457 // 2. A package is in at most one module in the jimage file.
458 //
459 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
460 const bool is_preview = is_preview_enabled();
461
462 jlong size;
463 JImageLocationRef location = 0;
464
465 TempNewSymbol class_name = SymbolTable::new_symbol(name);
466 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
467
468 if (pkg_name != nullptr) {
469 if (!Universe::is_module_initialized()) {
470 location = jimage_find_resource(JAVA_BASE_NAME, name, is_preview, &size);
471 } else {
472 PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
473 if (package_entry != nullptr) {
474 ResourceMark rm(current);
475 // Get the module name
476 ModuleEntry* module = package_entry->module();
477 assert(module != nullptr, "Boot classLoader package missing module");
478 assert(module->is_named(), "Boot classLoader package is in unnamed module");
479 const char* module_name = module->name()->as_C_string();
480 if (module_name != nullptr) {
481 location = jimage_find_resource(module_name, name, is_preview, &size);
482 }
483 }
484 }
485 }
486
487 if (location != 0) {
488 if (UsePerfData) {
489 ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
490 }
491 char* data = NEW_RESOURCE_ARRAY(char, size);
492 (*JImageGetResource)(jimage_non_null(), location, data, size);
493 // Resource allocated
494 assert(this == ClassLoader::get_jrt_entry(), "must be");
495 return new ClassFileStream((u1*)data,
496 checked_cast<int>(size),
497 _name,
498 true); // from_boot_loader_modules_image
499 }
500
501 return nullptr;
502 }
503
504 bool ClassPathImageEntry::is_modules_image() const {
505 assert(this == _singleton, "VM supports a single jimage");
506 assert(this == ClassLoader::get_jrt_entry(), "must be used for jrt entry");
507 return true;
508 }
509
510 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
511 _module_name = module_name;
512 _module_first_entry = nullptr;
513 _module_last_entry = nullptr;
514 }
515
516 ModuleClassPathList::~ModuleClassPathList() {
517 // Clean out each ClassPathEntry on list
518 ClassPathEntry* e = _module_first_entry;
519 while (e != nullptr) {
520 ClassPathEntry* next_entry = e->next();
521 delete e;
522 e = next_entry;
523 }
524 }
525
526 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
641 #if INCLUDE_CDS
642 if (CDSConfig::is_dumping_archive()) {
643 if (!Arguments::has_jimage()) {
644 vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
645 }
646 }
647 #endif
648
649 while (cp_stream.has_next()) {
650 const char* path = cp_stream.get_next();
651
652 if (set_base_piece) {
653 // The first time through the bootstrap_search setup, it must be determined
654 // what the base or core piece of the boot loader search is. Either a java runtime
655 // image is present or this is an exploded module build situation.
656 assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
657 "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
658 struct stat st;
659 if (os::stat(path, &st) == 0) {
660 // Directory found
661 if (jimage_exists()) {
662 assert(Arguments::has_jimage(), "sanity check");
663 const char* canonical_path = get_canonical_path(path, current);
664 assert(canonical_path != nullptr, "canonical_path issue");
665
666 // Hand over lifecycle control of the JImage file to the _jrt_entry singleton
667 // (see ClassPathImageEntry::close_jimage). The image must be initialized by now.
668 _jrt_entry = new ClassPathImageEntry(canonical_path);
669 assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
670 } // else it's an exploded build.
671 } else {
672 // If path does not exist, exit
673 vm_exit_during_initialization("Unable to establish the boot loader search path", path);
674 }
675 set_base_piece = false;
676 } else {
677 // Every entry on the boot class path after the initial base piece,
678 // which is set by os::set_boot_path(), is considered an appended entry.
679 update_class_path_entry_list(current, path);
680 }
681 }
682 }
683
684 // Gets the exploded path for the named module. The memory for the path
685 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
686 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
687 const char *home = Arguments::get_java_home();
688 const char file_sep = os::file_separator()[0];
689 // 10 represents the length of "modules" (7) + 2 file separators + \0
690 size_t len = strlen(home) + strlen(module_name) + 10;
691 char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len);
692 jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
693 return path;
694 }
695
696 // Gets a preview path for a given class path as a resource.
697 static const char* get_preview_path(const char* path) {
698 const char file_sep = os::file_separator()[0];
699 // 18 represents the length of "META-INF" (8) + "preview" (7) + 2 file separators + \0
700 size_t len = strlen(path) + 18;
701 char *preview_path = NEW_RESOURCE_ARRAY(char, len);
702 jio_snprintf(preview_path, len, "%s%cMETA-INF%cpreview", path, file_sep, file_sep);
703 return preview_path;
704 }
705
706 // During an exploded modules build, each module defined to the boot loader
707 // will be added to the ClassLoader::_exploded_entries array.
708 void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
709 assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
710 assert(_exploded_entries != nullptr, "_exploded_entries was not initialized");
711
712 // Find the module's symbol
713 ResourceMark rm(current);
714 const char *module_name = module_sym->as_C_string();
715 const char *path = get_exploded_module_path(module_name, false);
716
717 struct stat st;
718 if (os::stat(path, &st) == 0) {
719 // Directory found
720 ClassPathEntry* new_entry = create_class_path_entry(current, path, &st);
721 // If the path specification is valid, enter it into this module's list.
722 // There is no need to check for duplicate modules in the exploded entry list,
723 // since no two modules with the same name can be defined to the boot loader.
724 // This is checked at module definition time in Modules::define_module.
725 if (new_entry != nullptr) {
726 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
727 log_info(class, load)("path: %s", path);
728
729 // If we are in preview mode, attempt to add a preview entry *before* the
730 // new class path entry if a preview path exists.
731 if (is_preview_enabled()) {
732 const char* preview_path = get_preview_path(path);
733 if (os::stat(preview_path, &st) == 0) {
734 ClassPathEntry* preview_entry = create_class_path_entry(current, preview_path, &st);
735 if (preview_entry != nullptr) {
736 module_cpl->add_to_list(preview_entry);
737 log_info(class, load)("preview path: %s", preview_path);
738 }
739 }
740 }
741
742 module_cpl->add_to_list(new_entry);
743 {
744 MutexLocker ml(current, Module_lock);
745 _exploded_entries->push(module_cpl);
746 }
747 }
748 }
749 }
750
751 jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
752 // enable call to C land
753 ThreadToNativeFromVM ttn(thread);
754 HandleMark hm(thread);
755 return ZipLibrary::open(canonical_path, error_msg);
756 }
757
758 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
759 const char *path, const struct stat* st) {
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) {
1091
1092 // Called by the boot classloader to load classes
1093 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1094 assert(name != nullptr, "invariant");
1095
1096 ResourceMark rm(THREAD);
1097 HandleMark hm(THREAD);
1098
1099 const char* const class_name = name->as_C_string();
1100
1101 EventMarkClassLoading m("Loading class %s", class_name);
1102
1103 const char* const file_name = file_name_for_class_name(class_name,
1104 name->utf8_length());
1105 assert(file_name != nullptr, "invariant");
1106
1107 // Lookup stream for parsing .class file
1108 ClassFileStream* stream = nullptr;
1109 s2 classpath_index = 0;
1110 ClassPathEntry* e = nullptr;
1111 bool is_patched = false;
1112
1113 // If search_append_only is true, boot loader visibility boundaries are
1114 // set to be _first_append_entry to the end. This includes:
1115 // [-Xbootclasspath/a]; [jvmti appended entries]
1116 //
1117 // If search_append_only is false, boot loader visibility boundaries are
1118 // set to be the --patch-module entries plus the base piece. This includes:
1119 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1120 //
1121
1122 // Load Attempt #1: --patch-module
1123 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1124 // attempt to load the class from those locations specific to the module.
1125 // Specifications to --patch-module can contain a partial number of classes
1126 // that are part of the overall module definition. So if a particular class is not
1127 // found within its module specification, the search should continue to Load Attempt #2.
1128 // Note: The --patch-module entries are never searched if the boot loader's
1129 // visibility boundary is limited to only searching the append entries.
1130 if (_patch_mod_entries != nullptr && !search_append_only) {
1131 // At CDS dump time, the --patch-module entries are ignored. That means a
1132 // class is still loaded from the runtime image even if it might
1133 // appear in the _patch_mod_entries. The runtime shared class visibility
1134 // check will determine if a shared class is visible based on the runtime
1135 // environment, including the runtime --patch-module setting.
1136 if (!Arguments::is_valhalla_enabled()) {
1137 // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1138 // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1139 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1140 }
1141 if (Arguments::is_valhalla_enabled() || !CDSConfig::is_dumping_static_archive()) {
1142 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1143 if (stream != nullptr) {
1144 is_patched = true;
1145 }
1146 }
1147 }
1148
1149 // Load Attempt #2: [jimage | exploded build]
1150 if (!search_append_only && (nullptr == stream)) {
1151 if (has_jrt_entry()) {
1152 e = _jrt_entry;
1153 stream = _jrt_entry->open_stream(THREAD, file_name);
1154 } else {
1155 // Exploded build - attempt to locate class in its defining module's location.
1156 assert(_exploded_entries != nullptr, "No exploded build entries present");
1157 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1158 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1159 }
1160 }
1161
1162 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1163 if (search_append_only && (nullptr == stream)) {
1164 // For the boot loader append path search, the starting classpath_index
1165 // for the appended piece is always 1 to account for either the
1166 // _jrt_entry or the _exploded_entries.
1175 }
1176 e = e->next();
1177 ++classpath_index;
1178 }
1179 }
1180
1181 if (nullptr == stream) {
1182 return nullptr;
1183 }
1184
1185 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1186 Handle protection_domain;
1187 ClassLoadInfo cl_info(protection_domain);
1188
1189 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1190 name,
1191 loader_data,
1192 cl_info,
1193 CHECK_NULL);
1194 result->set_classpath_index(classpath_index);
1195 if (is_patched) {
1196 result->set_shared_classpath_index(0);
1197 }
1198 return result;
1199 }
1200
1201 #if INCLUDE_CDS
1202 static const char* skip_uri_protocol(const char* source) {
1203 if (strncmp(source, "file:", 5) == 0) {
1204 // file: protocol path could start with file:/ or file:///
1205 // locate the char after all the forward slashes
1206 int offset = 5;
1207 while (*(source + offset) == '/') {
1208 offset++;
1209 }
1210 source += offset;
1211 // for non-windows platforms, move back one char as the path begins with a '/'
1212 #ifndef _WINDOWS
1213 source -= 1;
1214 #endif
1215 } else if (strncmp(source, "jrt:/", 5) == 0) {
1216 source += 5;
1217 }
1254 for (size_t i = 0; i < strlen(uri); ++i) {
1255 char decoded = decode_percent_encoded(uri, i);
1256 path[path_index++] = decoded;
1257 }
1258 path[path_index] = '\0';
1259 return path;
1260 }
1261
1262 // Record the shared classpath index and loader type for classes loaded
1263 // by the builtin loaders at dump time.
1264 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1265 const ClassFileStream* stream, bool redefined) {
1266 assert(CDSConfig::is_dumping_archive(), "sanity");
1267 assert(stream != nullptr, "sanity");
1268
1269 if (ik->is_hidden()) {
1270 record_hidden_class(ik);
1271 return;
1272 }
1273
1274 if (ik->shared_classpath_index() == 0 && ik->defined_by_boot_loader()) {
1275 return;
1276 }
1277
1278 oop loader = ik->class_loader();
1279 char* src = (char*)stream->source();
1280 if (src == nullptr) {
1281 ik->set_shared_classpath_index(-1); // unsupported location
1282 return;
1283 }
1284
1285 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1286 // A class loaded by a user-defined classloader.
1287 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1288 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1289 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1290 return;
1291 }
1292
1293 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1294
1295 ResourceMark rm(current);
1296 int classpath_index = -1;
1297 bool found_invalid = false;
1465 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1466
1467 NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1468 NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1469 NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1470 NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1471
1472 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1473 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1474 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1475 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1476 }
1477 }
1478
1479 // lookup java library entry points
1480 load_java_library();
1481 // jimage library entry points are loaded below, in lookup_vm_options
1482 setup_bootstrap_search_path(THREAD);
1483 }
1484
1485 // Lookup VM options embedded in the modules jimage file
1486 char* ClassLoader::lookup_vm_options() {
1487 char modules_path[JVM_MAXPATHLEN];
1488 const char* fileSep = os::file_separator();
1489
1490 // Initialize jimage library entry points
1491 load_jimage_library();
1492
1493 jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1494 if (jimage_open(modules_path)) {
1495 // Special case where we lookup the options string *before* set_preview_mode() is called.
1496 // Since VM arguments have not been parsed, and the ClassPathImageEntry singleton
1497 // has not been created yet, we access the JImage file directly in non-preview mode.
1498 jlong size;
1499 JImageLocationRef location =
1500 jimage_find_resource(JAVA_BASE_NAME, "jdk/internal/vm/options", /* is_preview */ false, &size);
1501 if (location != 0) {
1502 char *options = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1503 (*JImageGetResource)(jimage_non_null(), location, options, size);
1504 options[size] = '\0';
1505 return options;
1506 }
1507 }
1508 return nullptr;
1509 }
1510
1511 // Finishes initializing the JImageFile (if present) by setting the access mode.
1512 void ClassLoader::set_preview_mode(bool enable_preview) {
1513 assert(Preview_mode == PREVIEW_MODE_UNINITIALIZED, "set_preview_mode must not be called twice");
1514 Preview_mode = enable_preview ? PREVIEW_MODE_ENABLE_PREVIEW : PREVIEW_MODE_DEFAULT;
1515 }
1516
1517 bool ClassLoader::is_module_observable(const char* module_name) {
1518 assert(JImageOpen != nullptr, "jimage library should have been opened");
1519 if (!jimage_exists()) {
1520 struct stat st;
1521 const char *path = get_exploded_module_path(module_name, true);
1522 bool res = os::stat(path, &st) == 0;
1523 FREE_C_HEAP_ARRAY(char, path);
1524 return res;
1525 }
1526 // We don't expect preview mode (i.e. --enable-preview) to affect module visibility.
1527 jlong size;
1528 return jimage_find_resource(module_name, "module-info.class", /* is_preview */ false, &size) != 0;
1529 }
1530
1531 jlong ClassLoader::classloader_time_ms() {
1532 return UsePerfData ?
1533 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1534 }
1535
1536 jlong ClassLoader::class_init_count() {
1537 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1538 }
1539
1540 jlong ClassLoader::class_init_time_ms() {
1541 return UsePerfData ?
1542 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1543 }
1544
1545 jlong ClassLoader::class_verify_time_ms() {
1546 return UsePerfData ?
1547 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1548 }
|