42 #include "memory/allocation.inline.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "oops/instanceKlass.hpp"
45 #include "oops/klass.inline.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "oops/symbol.hpp"
48 #include "runtime/arguments.hpp"
49 #include "runtime/handles.inline.hpp"
50 #include "runtime/java.hpp"
51 #include "runtime/os.hpp"
52 #include "utilities/checkedCast.hpp"
53 #include "utilities/stringUtils.hpp"
54
55 jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index;
56 jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index;
57 jshort ClassLoaderExt::_max_used_path_index = 0;
58 int ClassLoaderExt::_num_module_paths = 0;
59 bool ClassLoaderExt::_has_app_classes = false;
60 bool ClassLoaderExt::_has_platform_classes = false;
61 bool ClassLoaderExt::_has_non_jar_in_classpath = false;
62
63 void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {
64 if (CDSConfig::is_using_archive()) {
65 warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");
66 FileMapInfo::current_info()->set_has_platform_or_app_classes(false);
67 if (DynamicArchive::is_mapped()) {
68 FileMapInfo::dynamic_info()->set_has_platform_or_app_classes(false);
69 }
70 }
71 ClassLoader::add_to_boot_append_entries(new_entry);
72 }
73
74 void ClassLoaderExt::setup_app_search_path(JavaThread* current) {
75 assert(CDSConfig::is_dumping_archive(), "sanity");
76 int start_index = ClassLoader::num_boot_classpath_entries();
77 _app_class_paths_start_index = checked_cast<jshort>(start_index);
78 char* app_class_path = os::strdup_check_oom(Arguments::get_appclasspath(), mtClass);
79
80 if (strcmp(app_class_path, ".") == 0) {
81 // This doesn't make any sense, even for AppCDS, so let's skip it. We
88 }
89
90 os::free(app_class_path);
91 }
92
93 int ClassLoaderExt::compare_module_names(const char** p1, const char** p2) {
94 return strcmp(*p1, *p2);
95 }
96
97 void ClassLoaderExt::process_module_table(JavaThread* current, ModuleEntryTable* met) {
98 ResourceMark rm(current);
99 GrowableArray<const char*>* module_paths = new GrowableArray<const char*>(5);
100
101 class ModulePathsGatherer : public ModuleClosure {
102 JavaThread* _current;
103 GrowableArray<const char*>* _module_paths;
104 public:
105 ModulePathsGatherer(JavaThread* current, GrowableArray<const char*>* module_paths) :
106 _current(current), _module_paths(module_paths) {}
107 void do_module(ModuleEntry* m) {
108 char* uri = m->location()->as_C_string();
109 if (strncmp(uri, "file:", 5) == 0) {
110 char* path = ClassLoader::uri_to_path(uri);
111 extract_jar_files_from_path(path, _module_paths);
112 }
113 }
114 };
115
116 ModulePathsGatherer gatherer(current, module_paths);
117 {
118 MutexLocker ml(Module_lock);
119 met->modules_do(&gatherer);
120 }
121
122 // Sort the module paths before storing into CDS archive for simpler
123 // checking at runtime.
124 module_paths->sort(compare_module_names);
125
126 for (int i = 0; i < module_paths->length(); i++) {
127 ClassLoader::setup_module_search_path(current, module_paths->at(i));
328 }
329 result->set_shared_classpath_index(classpath_index);
330 result->set_shared_class_loader_type(classloader_type);
331 #if INCLUDE_CDS_JAVA_HEAP
332 if (CDSConfig::is_dumping_heap() && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER &&
333 classpath_index < 0 && redefined) {
334 // When dumping the heap (which happens only during static dump), classes for the built-in
335 // loaders are always loaded from known locations (jimage, classpath or modulepath),
336 // so classpath_index should always be >= 0.
337 // The only exception is when a java agent is used during dump time (for testing
338 // purposes only). If a class is transformed by the agent, the CodeSource of
339 // this class may point to an unknown location. This may break heap object archiving,
340 // which requires all the boot classes to be from known locations. This is an
341 // uncommon scenario (even in test cases). Let's simply disable heap object archiving.
342 ResourceMark rm;
343 log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.",
344 result->external_name());
345 HeapShared::disable_writing();
346 }
347 #endif // INCLUDE_CDS_JAVA_HEAP
348 }
|
42 #include "memory/allocation.inline.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "oops/instanceKlass.hpp"
45 #include "oops/klass.inline.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "oops/symbol.hpp"
48 #include "runtime/arguments.hpp"
49 #include "runtime/handles.inline.hpp"
50 #include "runtime/java.hpp"
51 #include "runtime/os.hpp"
52 #include "utilities/checkedCast.hpp"
53 #include "utilities/stringUtils.hpp"
54
55 jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index;
56 jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index;
57 jshort ClassLoaderExt::_max_used_path_index = 0;
58 int ClassLoaderExt::_num_module_paths = 0;
59 bool ClassLoaderExt::_has_app_classes = false;
60 bool ClassLoaderExt::_has_platform_classes = false;
61 bool ClassLoaderExt::_has_non_jar_in_classpath = false;
62 int ClassLoaderExt::_app_class_exclusion_start_path_index = INT_MAX;
63
64 void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {
65 if (CDSConfig::is_using_archive()) {
66 warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");
67 FileMapInfo::current_info()->set_has_platform_or_app_classes(false);
68 if (DynamicArchive::is_mapped()) {
69 FileMapInfo::dynamic_info()->set_has_platform_or_app_classes(false);
70 }
71 }
72 ClassLoader::add_to_boot_append_entries(new_entry);
73 }
74
75 void ClassLoaderExt::setup_app_search_path(JavaThread* current) {
76 assert(CDSConfig::is_dumping_archive(), "sanity");
77 int start_index = ClassLoader::num_boot_classpath_entries();
78 _app_class_paths_start_index = checked_cast<jshort>(start_index);
79 char* app_class_path = os::strdup_check_oom(Arguments::get_appclasspath(), mtClass);
80
81 if (strcmp(app_class_path, ".") == 0) {
82 // This doesn't make any sense, even for AppCDS, so let's skip it. We
89 }
90
91 os::free(app_class_path);
92 }
93
94 int ClassLoaderExt::compare_module_names(const char** p1, const char** p2) {
95 return strcmp(*p1, *p2);
96 }
97
98 void ClassLoaderExt::process_module_table(JavaThread* current, ModuleEntryTable* met) {
99 ResourceMark rm(current);
100 GrowableArray<const char*>* module_paths = new GrowableArray<const char*>(5);
101
102 class ModulePathsGatherer : public ModuleClosure {
103 JavaThread* _current;
104 GrowableArray<const char*>* _module_paths;
105 public:
106 ModulePathsGatherer(JavaThread* current, GrowableArray<const char*>* module_paths) :
107 _current(current), _module_paths(module_paths) {}
108 void do_module(ModuleEntry* m) {
109 if (m->location() == nullptr) {
110 // This is a dynamic generated module (which we loaded from the static archive) for supporting
111 // dynamic proxies. We don't archive any other such modules.
112 // No need to include such modules in _module_path.
113 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
114 assert(Modules::is_dynamic_proxy_module(m), "must be");
115 if (log_is_enabled(Info, cds, dynamic, proxy)) {
116 ResourceMark rm;
117 log_info(cds, dynamic, proxy)("Archived dynamic module: %s", m->name()->as_C_string());
118 }
119 return;
120 }
121 char* uri = m->location()->as_C_string();
122 if (strncmp(uri, "file:", 5) == 0) {
123 char* path = ClassLoader::uri_to_path(uri);
124 extract_jar_files_from_path(path, _module_paths);
125 }
126 }
127 };
128
129 ModulePathsGatherer gatherer(current, module_paths);
130 {
131 MutexLocker ml(Module_lock);
132 met->modules_do(&gatherer);
133 }
134
135 // Sort the module paths before storing into CDS archive for simpler
136 // checking at runtime.
137 module_paths->sort(compare_module_names);
138
139 for (int i = 0; i < module_paths->length(); i++) {
140 ClassLoader::setup_module_search_path(current, module_paths->at(i));
341 }
342 result->set_shared_classpath_index(classpath_index);
343 result->set_shared_class_loader_type(classloader_type);
344 #if INCLUDE_CDS_JAVA_HEAP
345 if (CDSConfig::is_dumping_heap() && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER &&
346 classpath_index < 0 && redefined) {
347 // When dumping the heap (which happens only during static dump), classes for the built-in
348 // loaders are always loaded from known locations (jimage, classpath or modulepath),
349 // so classpath_index should always be >= 0.
350 // The only exception is when a java agent is used during dump time (for testing
351 // purposes only). If a class is transformed by the agent, the CodeSource of
352 // this class may point to an unknown location. This may break heap object archiving,
353 // which requires all the boot classes to be from known locations. This is an
354 // uncommon scenario (even in test cases). Let's simply disable heap object archiving.
355 ResourceMark rm;
356 log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.",
357 result->external_name());
358 HeapShared::disable_writing();
359 }
360 #endif // INCLUDE_CDS_JAVA_HEAP
361 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_dynamic_archive()) {
362 check_invalid_classpath_index(classpath_index, result);
363 }
364 }
365
366 ClassPathEntry* ClassLoaderExt::get_class_path_entry(s2 classpath_index) {
367 if (classpath_index < 0) {
368 return nullptr;
369 }
370
371 if (classpath_index == 0) {
372 assert(has_jrt_entry(), "CDS requires modules image");
373 return get_jrt_entry();
374 }
375
376 // Iterate over -Xbootclasspath, if any;
377 int i = 0;
378 for (ClassPathEntry* cpe = first_append_entry(); cpe != nullptr; cpe = cpe->next()) {
379 i++;
380 if (i == classpath_index) {
381 return cpe;
382 }
383 }
384
385 // Iterate over -cp, if any
386 for (ClassPathEntry* cpe = app_classpath_entries(); cpe != nullptr; cpe = cpe->next()) {
387 i++;
388 if (i == classpath_index) {
389 return cpe;
390 }
391 }
392
393 // Iterate over --module-path, if any
394 for (ClassPathEntry* cpe = module_path_entries(); cpe != nullptr; cpe = cpe->next()) {
395 i++;
396 if (i == classpath_index) {
397 return cpe;
398 }
399 }
400
401 return nullptr;
402 }
403
404 // It's possible to use reflection+setAccessible to call into ClassLoader::defineClass() to
405 // pretend that a dynamically generated class comes from a JAR file in the classpath.
406 // Detect such classes and exclude them from the archive.
407 void ClassLoaderExt::check_invalid_classpath_index(s2 classpath_index, InstanceKlass* ik) {
408 ClassPathEntry *cpe = get_class_path_entry(classpath_index);
409 if (cpe != nullptr && cpe->is_jar_file()) {
410 ClassPathZipEntry *zip = (ClassPathZipEntry*)cpe;
411 JavaThread* current = JavaThread::current();
412 ResourceMark rm(current);
413 const char* const class_name = ik->name()->as_C_string();
414 const char* const file_name = file_name_for_class_name(class_name,
415 ik->name()->utf8_length());
416 if (!zip->has_entry(current, file_name)) {
417 log_warning(cds)("class %s cannot be archived because it was not define from %s as claimed",
418 class_name, zip->name());
419 ik->set_shared_classpath_index(-1);
420 }
421 }
422 }
423
424 // -XX:CacheOnlyClassesIn=
425 bool ClassLoaderExt::should_be_excluded(InstanceKlass* k) {
426 int path_index = k->shared_classpath_index();
427 if (path_index >= _app_class_exclusion_start_path_index && path_index < _app_module_paths_start_index) {
428 return true;
429 } else {
430 return false;
431 }
432 }
433
|