1 /* 2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classFileParser.hpp" 27 #include "classfile/classFileStream.hpp" 28 #include "classfile/classLoader.inline.hpp" 29 #include "classfile/classLoaderExt.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/klassFactory.hpp" 32 #include "classfile/modules.hpp" 33 #include "classfile/systemDictionaryShared.hpp" 34 #include "classfile/vmSymbols.hpp" 35 #include "logging/log.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/filemap.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/symbol.hpp" 42 #include "runtime/arguments.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/java.hpp" 45 #include "runtime/javaCalls.hpp" 46 #include "runtime/os.hpp" 47 #include "services/threadService.hpp" 48 #include "utilities/stringUtils.hpp" 49 50 jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index; 51 jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index; 52 jshort ClassLoaderExt::_max_used_path_index = 0; 53 bool ClassLoaderExt::_has_app_classes = false; 54 bool ClassLoaderExt::_has_platform_classes = false; 55 56 void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) { 57 if (UseSharedSpaces) { 58 warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended"); 59 FileMapInfo::current_info()->set_has_platform_or_app_classes(false); 60 } 61 ClassLoader::add_to_boot_append_entries(new_entry); 62 } 63 64 void ClassLoaderExt::setup_app_search_path() { 65 Arguments::assert_is_dumping_archive(); 66 _app_class_paths_start_index = ClassLoader::num_boot_classpath_entries(); 67 char* app_class_path = os::strdup(Arguments::get_appclasspath()); 68 69 if (strcmp(app_class_path, ".") == 0) { 70 // This doesn't make any sense, even for AppCDS, so let's skip it. We 71 // don't want to throw an error here because -cp "." is usually assigned 72 // by the launcher when classpath is not specified. 73 trace_class_path("app loader class path (skipped)=", app_class_path); 74 } else { 75 trace_class_path("app loader class path=", app_class_path); 76 ClassLoader::setup_app_search_path(app_class_path); 77 } 78 } 79 80 void ClassLoaderExt::process_module_table(ModuleEntryTable* met, TRAPS) { 81 ResourceMark rm(THREAD); 82 for (int i = 0; i < met->table_size(); i++) { 83 for (ModuleEntry* m = met->bucket(i); m != NULL;) { 84 char* path = m->location()->as_C_string(); 85 if (strncmp(path, "file:", 5) == 0) { 86 path = ClassLoader::skip_uri_protocol(path); 87 ClassLoader::setup_module_search_path(path, THREAD); 88 } 89 m = m->next(); 90 } 91 } 92 } 93 void ClassLoaderExt::setup_module_paths(TRAPS) { 94 Arguments::assert_is_dumping_archive(); 95 _app_module_paths_start_index = ClassLoader::num_boot_classpath_entries() + 96 ClassLoader::num_app_classpath_entries(); 97 Handle system_class_loader (THREAD, SystemDictionary::java_system_loader()); 98 ModuleEntryTable* met = Modules::get_module_entry_table(system_class_loader); 99 process_module_table(met, THREAD); 100 } 101 102 char* ClassLoaderExt::read_manifest(ClassPathEntry* entry, jint *manifest_size, bool clean_text, TRAPS) { 103 const char* name = "META-INF/MANIFEST.MF"; 104 char* manifest; 105 jint size; 106 107 assert(entry->is_jar_file(), "must be"); 108 manifest = (char*) ((ClassPathZipEntry*)entry )->open_entry(name, &size, true, CHECK_NULL); 109 110 if (manifest == NULL) { // No Manifest 111 *manifest_size = 0; 112 return NULL; 113 } 114 115 116 if (clean_text) { 117 // See http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest 118 // (1): replace all CR/LF and CR with LF 119 StringUtils::replace_no_expand(manifest, "\r\n", "\n"); 120 121 // (2) remove all new-line continuation (remove all "\n " substrings) 122 StringUtils::replace_no_expand(manifest, "\n ", ""); 123 } 124 125 *manifest_size = (jint)strlen(manifest); 126 return manifest; 127 } 128 129 char* ClassLoaderExt::get_class_path_attr(const char* jar_path, char* manifest, jint manifest_size) { 130 const char* tag = "Class-Path: "; 131 const int tag_len = (int)strlen(tag); 132 char* found = NULL; 133 char* line_start = manifest; 134 char* end = manifest + manifest_size; 135 136 assert(*end == 0, "must be nul-terminated"); 137 138 while (line_start < end) { 139 char* line_end = strchr(line_start, '\n'); 140 if (line_end == NULL) { 141 // JAR spec require the manifest file to be terminated by a new line. 142 break; 143 } 144 if (strncmp(tag, line_start, tag_len) == 0) { 145 if (found != NULL) { 146 // Same behavior as jdk/src/share/classes/java/util/jar/Attributes.java 147 // If duplicated entries are found, the last one is used. 148 log_warning(cds)("Warning: Duplicate name in Manifest: %s.\n" 149 "Ensure that the manifest does not have duplicate entries, and\n" 150 "that blank lines separate individual sections in both your\n" 151 "manifest and in the META-INF/MANIFEST.MF entry in the jar file:\n%s\n", tag, jar_path); 152 } 153 found = line_start + tag_len; 154 assert(found <= line_end, "sanity"); 155 *line_end = '\0'; 156 } 157 line_start = line_end + 1; 158 } 159 return found; 160 } 161 162 void ClassLoaderExt::process_jar_manifest(ClassPathEntry* entry, 163 bool check_for_duplicates) { 164 Thread* THREAD = Thread::current(); 165 ResourceMark rm(THREAD); 166 jint manifest_size; 167 char* manifest = read_manifest(entry, &manifest_size, CHECK); 168 169 if (manifest == NULL) { 170 return; 171 } 172 173 if (strstr(manifest, "Extension-List:") != NULL) { 174 vm_exit_during_cds_dumping(err_msg("-Xshare:dump does not support Extension-List in JAR manifest: %s", entry->name())); 175 } 176 177 char* cp_attr = get_class_path_attr(entry->name(), manifest, manifest_size); 178 179 if (cp_attr != NULL && strlen(cp_attr) > 0) { 180 trace_class_path("found Class-Path: ", cp_attr); 181 182 char sep = os::file_separator()[0]; 183 const char* dir_name = entry->name(); 184 const char* dir_tail = strrchr(dir_name, sep); 185 int dir_len; 186 if (dir_tail == NULL) { 187 dir_len = 0; 188 } else { 189 dir_len = dir_tail - dir_name + 1; 190 } 191 192 // Split the cp_attr by spaces, and add each file 193 char* file_start = cp_attr; 194 char* end = file_start + strlen(file_start); 195 196 while (file_start < end) { 197 char* file_end = strchr(file_start, ' '); 198 if (file_end != NULL) { 199 *file_end = 0; 200 file_end += 1; 201 } else { 202 file_end = end; 203 } 204 205 size_t name_len = strlen(file_start); 206 if (name_len > 0) { 207 ResourceMark rm(THREAD); 208 size_t libname_len = dir_len + name_len; 209 char* libname = NEW_RESOURCE_ARRAY(char, libname_len + 1); 210 int n = os::snprintf(libname, libname_len + 1, "%.*s%s", dir_len, dir_name, file_start); 211 assert((size_t)n == libname_len, "Unexpected number of characters in string"); 212 if (ClassLoader::update_class_path_entry_list(libname, true, false, true /* from_class_path_attr */)) { 213 trace_class_path("library = ", libname); 214 } else { 215 trace_class_path("library (non-existent) = ", libname); 216 FileMapInfo::record_non_existent_class_path_entry(libname); 217 } 218 } 219 220 file_start = file_end; 221 } 222 } 223 } 224 225 void ClassLoaderExt::setup_search_paths() { 226 ClassLoaderExt::setup_app_search_path(); 227 } 228 229 void ClassLoaderExt::record_result(const s2 classpath_index, 230 InstanceKlass* result, 231 TRAPS) { 232 Arguments::assert_is_dumping_archive(); 233 234 // We need to remember where the class comes from during dumping. 235 oop loader = result->class_loader(); 236 s2 classloader_type = ClassLoader::BOOT_LOADER; 237 if (SystemDictionary::is_system_class_loader(loader)) { 238 classloader_type = ClassLoader::APP_LOADER; 239 ClassLoaderExt::set_has_app_classes(); 240 } else if (SystemDictionary::is_platform_class_loader(loader)) { 241 classloader_type = ClassLoader::PLATFORM_LOADER; 242 ClassLoaderExt::set_has_platform_classes(); 243 } 244 if (classpath_index > ClassLoaderExt::max_used_path_index()) { 245 ClassLoaderExt::set_max_used_path_index(classpath_index); 246 } 247 result->set_shared_classpath_index(classpath_index); 248 result->set_shared_class_loader_type(classloader_type); 249 } 250 251 // Load the class of the given name from the location given by path. The path is specified by 252 // the "source:" in the class list file (see classListParser.cpp), and can be a directory or 253 // a JAR file. 254 InstanceKlass* ClassLoaderExt::load_class(Symbol* name, const char* path, TRAPS) { 255 assert(name != NULL, "invariant"); 256 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump"); 257 ResourceMark rm(THREAD); 258 const char* class_name = name->as_C_string(); 259 260 const char* file_name = file_name_for_class_name(class_name, 261 name->utf8_length()); 262 assert(file_name != NULL, "invariant"); 263 264 // Lookup stream for parsing .class file 265 ClassFileStream* stream = NULL; 266 ClassPathEntry* e = find_classpath_entry_from_cache(path, CHECK_NULL); 267 if (e == NULL) { 268 return NULL; 269 } 270 { 271 PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(), 272 ((JavaThread*) THREAD)->get_thread_stat()->perf_timers_addr(), 273 PerfClassTraceTime::CLASS_LOAD); 274 stream = e->open_stream(file_name, CHECK_NULL); 275 } 276 277 if (NULL == stream) { 278 log_warning(cds)("Preload Warning: Cannot find %s", class_name); 279 return NULL; 280 } 281 282 assert(stream != NULL, "invariant"); 283 stream->set_verify(true); 284 285 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 286 Handle protection_domain; 287 ClassLoadInfo cl_info(protection_domain); 288 289 InstanceKlass* result = KlassFactory::create_from_stream(stream, 290 name, 291 loader_data, 292 cl_info, 293 THREAD); 294 295 if (HAS_PENDING_EXCEPTION) { 296 log_error(cds)("Preload Error: Failed to load %s", class_name); 297 return NULL; 298 } 299 return result; 300 } 301 302 struct CachedClassPathEntry { 303 const char* _path; 304 ClassPathEntry* _entry; 305 }; 306 307 static GrowableArray<CachedClassPathEntry>* cached_path_entries = NULL; 308 309 ClassPathEntry* ClassLoaderExt::find_classpath_entry_from_cache(const char* path, TRAPS) { 310 // This is called from dump time so it's single threaded and there's no need for a lock. 311 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump"); 312 if (cached_path_entries == NULL) { 313 cached_path_entries = new (ResourceObj::C_HEAP, mtClass) GrowableArray<CachedClassPathEntry>(20, /*c heap*/ true); 314 } 315 CachedClassPathEntry ccpe; 316 for (int i=0; i<cached_path_entries->length(); i++) { 317 ccpe = cached_path_entries->at(i); 318 if (strcmp(ccpe._path, path) == 0) { 319 if (i != 0) { 320 // Put recent entries at the beginning to speed up searches. 321 cached_path_entries->remove_at(i); 322 cached_path_entries->insert_before(0, ccpe); 323 } 324 return ccpe._entry; 325 } 326 } 327 328 struct stat st; 329 if (os::stat(path, &st) != 0) { 330 // File or directory not found 331 return NULL; 332 } 333 ClassPathEntry* new_entry = NULL; 334 335 new_entry = create_class_path_entry(path, &st, false, false, false, CHECK_NULL); 336 if (new_entry == NULL) { 337 return NULL; 338 } 339 ccpe._path = strdup(path); 340 ccpe._entry = new_entry; 341 cached_path_entries->insert_before(0, ccpe); 342 return new_entry; 343 }