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_path_by_name(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_path_by_name);
125
126 for (int i = 0; i < module_paths->length(); i++) {
127 ClassLoader::setup_module_search_path(current, module_paths->at(i));
323 }
324 result->set_shared_classpath_index(classpath_index);
325 result->set_shared_class_loader_type(classloader_type);
326 #if INCLUDE_CDS_JAVA_HEAP
327 if (CDSConfig::is_dumping_heap() && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER &&
328 classpath_index < 0 && redefined) {
329 // When dumping the heap (which happens only during static dump), classes for the built-in
330 // loaders are always loaded from known locations (jimage, classpath or modulepath),
331 // so classpath_index should always be >= 0.
332 // The only exception is when a java agent is used during dump time (for testing
333 // purposes only). If a class is transformed by the agent, the CodeSource of
334 // this class may point to an unknown location. This may break heap object archiving,
335 // which requires all the boot classes to be from known locations. This is an
336 // uncommon scenario (even in test cases). Let's simply disable heap object archiving.
337 ResourceMark rm;
338 log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.",
339 result->external_name());
340 HeapShared::disable_writing();
341 }
342 #endif // INCLUDE_CDS_JAVA_HEAP
343 }
|
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_path_by_name(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_path_by_name);
138
139 for (int i = 0; i < module_paths->length(); i++) {
140 ClassLoader::setup_module_search_path(current, module_paths->at(i));
336 }
337 result->set_shared_classpath_index(classpath_index);
338 result->set_shared_class_loader_type(classloader_type);
339 #if INCLUDE_CDS_JAVA_HEAP
340 if (CDSConfig::is_dumping_heap() && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER &&
341 classpath_index < 0 && redefined) {
342 // When dumping the heap (which happens only during static dump), classes for the built-in
343 // loaders are always loaded from known locations (jimage, classpath or modulepath),
344 // so classpath_index should always be >= 0.
345 // The only exception is when a java agent is used during dump time (for testing
346 // purposes only). If a class is transformed by the agent, the CodeSource of
347 // this class may point to an unknown location. This may break heap object archiving,
348 // which requires all the boot classes to be from known locations. This is an
349 // uncommon scenario (even in test cases). Let's simply disable heap object archiving.
350 ResourceMark rm;
351 log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.",
352 result->external_name());
353 HeapShared::disable_writing();
354 }
355 #endif // INCLUDE_CDS_JAVA_HEAP
356 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_dynamic_archive()) {
357 check_invalid_classpath_index(classpath_index, result);
358 }
359 }
360
361 ClassPathEntry* ClassLoaderExt::get_class_path_entry(s2 classpath_index) {
362 if (classpath_index < 0) {
363 return nullptr;
364 }
365
366 if (classpath_index == 0) {
367 assert(has_jrt_entry(), "CDS requires modules image");
368 return get_jrt_entry();
369 }
370
371 // Iterate over -Xbootclasspath, if any;
372 int i = 0;
373 for (ClassPathEntry* cpe = first_append_entry(); cpe != nullptr; cpe = cpe->next()) {
374 i++;
375 if (i == classpath_index) {
376 return cpe;
377 }
378 }
379
380 // Iterate over -cp, if any
381 for (ClassPathEntry* cpe = app_classpath_entries(); cpe != nullptr; cpe = cpe->next()) {
382 i++;
383 if (i == classpath_index) {
384 return cpe;
385 }
386 }
387
388 // Iterate over --module-path, if any
389 for (ClassPathEntry* cpe = module_path_entries(); cpe != nullptr; cpe = cpe->next()) {
390 i++;
391 if (i == classpath_index) {
392 return cpe;
393 }
394 }
395
396 return nullptr;
397 }
398
399 // It's possible to use reflection+setAccessible to call into ClassLoader::defineClass() to
400 // pretend that a dynamically generated class comes from a JAR file in the classpath.
401 // Detect such classes and exclude them from the archive.
402 void ClassLoaderExt::check_invalid_classpath_index(s2 classpath_index, InstanceKlass* ik) {
403 ClassPathEntry *cpe = get_class_path_entry(classpath_index);
404 if (cpe != nullptr && cpe->is_jar_file()) {
405 ClassPathZipEntry *zip = (ClassPathZipEntry*)cpe;
406 JavaThread* current = JavaThread::current();
407 ResourceMark rm(current);
408 const char* const class_name = ik->name()->as_C_string();
409 const char* const file_name = file_name_for_class_name(class_name,
410 ik->name()->utf8_length());
411 if (!zip->has_entry(current, file_name)) {
412 log_warning(cds)("class %s cannot be archived because it was not define from %s as claimed",
413 class_name, zip->name());
414 ik->set_shared_classpath_index(-1);
415 }
416 }
417 }
418
419 // -XX:CacheOnlyClassesIn=
420 bool ClassLoaderExt::should_be_excluded(InstanceKlass* k) {
421 int path_index = k->shared_classpath_index();
422 if (path_index >= _app_class_exclusion_start_path_index && path_index < _app_module_paths_start_index) {
423 return true;
424 } else {
425 return false;
426 }
427 }
428
|