1 /* 2 * Copyright (c) 2015, 2018, 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/classListParser.hpp" 29 #include "classfile/classLoader.inline.hpp" 30 #include "classfile/classLoaderExt.hpp" 31 #include "classfile/classLoaderData.inline.hpp" 32 #include "classfile/klassFactory.hpp" 33 #include "classfile/modules.hpp" 34 #include "classfile/sharedClassUtil.hpp" 35 #include "classfile/sharedPathsMiscInfo.hpp" 36 #include "classfile/systemDictionaryShared.hpp" 37 #include "classfile/vmSymbols.hpp" 38 #include "memory/allocation.inline.hpp" 39 #include "memory/filemap.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/instanceKlass.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "oops/symbol.hpp" 44 #include "runtime/arguments.hpp" 45 #include "runtime/handles.inline.hpp" 46 #include "runtime/java.hpp" 47 #include "runtime/javaCalls.hpp" 48 #include "runtime/os.hpp" 49 #include "services/threadService.hpp" 50 #include "utilities/stringUtils.hpp" 51 52 jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index; 53 jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index; 54 bool ClassLoaderExt::_has_app_classes = false; 55 bool ClassLoaderExt::_has_platform_classes = false; 56 57 void ClassLoaderExt::setup_app_search_path() { 58 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump and -XX:+UseAppCDS"); 59 _app_class_paths_start_index = ClassLoader::num_boot_classpath_entries(); 60 char* app_class_path = os::strdup(Arguments::get_appclasspath()); 61 62 if (strcmp(app_class_path, ".") == 0) { 63 // This doesn't make any sense, even for AppCDS, so let's skip it. We 64 // don't want to throw an error here because -cp "." is usually assigned 65 // by the launcher when classpath is not specified. 66 trace_class_path("app loader class path (skipped)=", app_class_path); 67 } else { 68 trace_class_path("app loader class path=", app_class_path); 69 shared_paths_misc_info()->add_app_classpath(app_class_path); 70 ClassLoader::setup_app_search_path(app_class_path); 71 } 72 } 73 74 void ClassLoaderExt::process_module_table(ModuleEntryTable* met) { 75 ResourceMark rm; 76 for (int i = 0; i < met->table_size(); i++) { 77 for (ModuleEntry* m = met->bucket(i); m != NULL;) { 78 char* path = m->location()->as_C_string(); 79 if (strncmp(path, "file:", 5) == 0 && ClassLoader::string_ends_with(path, ".jar")) { 80 m->print(); 81 path = ClassLoader::skip_uri_protocol(path); 82 ClassLoader::setup_module_search_path(path); 83 } 84 m = m->next(); 85 } 86 } 87 } 88 void ClassLoaderExt::setup_module_search_path() { 89 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump and -XX:+UseAppCDS"); 90 _app_module_paths_start_index = ClassLoader::num_boot_classpath_entries() + 91 ClassLoader::num_app_classpath_entries(); 92 Thread* thread = Thread::current(); 93 Handle system_class_loader (thread, SystemDictionary::java_system_loader()); 94 ModuleEntryTable* met = Modules::get_module_entry_table(system_class_loader); 95 process_module_table(met); 96 } 97 98 char* ClassLoaderExt::read_manifest(ClassPathEntry* entry, jint *manifest_size, bool clean_text, TRAPS) { 99 const char* name = "META-INF/MANIFEST.MF"; 100 char* manifest; 101 jint size; 102 103 assert(entry->is_jar_file(), "must be"); 104 manifest = (char*) ((ClassPathZipEntry*)entry )->open_entry(name, &size, true, CHECK_NULL); 105 106 if (manifest == NULL) { // No Manifest 107 *manifest_size = 0; 108 return NULL; 109 } 110 111 112 if (clean_text) { 113 // See http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest 114 // (1): replace all CR/LF and CR with LF 115 StringUtils::replace_no_expand(manifest, "\r\n", "\n"); 116 117 // (2) remove all new-line continuation (remove all "\n " substrings) 118 StringUtils::replace_no_expand(manifest, "\n ", ""); 119 } 120 121 *manifest_size = (jint)strlen(manifest); 122 return manifest; 123 } 124 125 char* ClassLoaderExt::get_class_path_attr(const char* jar_path, char* manifest, jint manifest_size) { 126 const char* tag = "Class-Path: "; 127 const int tag_len = (int)strlen(tag); 128 char* found = NULL; 129 char* line_start = manifest; 130 char* end = manifest + manifest_size; 131 132 assert(*end == 0, "must be nul-terminated"); 133 134 while (line_start < end) { 135 char* line_end = strchr(line_start, '\n'); 136 if (line_end == NULL) { 137 // JAR spec require the manifest file to be terminated by a new line. 138 break; 139 } 140 if (strncmp(tag, line_start, tag_len) == 0) { 141 if (found != NULL) { 142 // Same behavior as jdk/src/share/classes/java/util/jar/Attributes.java 143 // If duplicated entries are found, the last one is used. 144 tty->print_cr("Warning: Duplicate name in Manifest: %s.\n" 145 "Ensure that the manifest does not have duplicate entries, and\n" 146 "that blank lines separate individual sections in both your\n" 147 "manifest and in the META-INF/MANIFEST.MF entry in the jar file:\n%s\n", tag, jar_path); 148 } 149 found = line_start + tag_len; 150 assert(found <= line_end, "sanity"); 151 *line_end = '\0'; 152 } 153 line_start = line_end + 1; 154 } 155 return found; 156 } 157 158 void ClassLoaderExt::process_jar_manifest(ClassPathEntry* entry, 159 bool check_for_duplicates) { 160 Thread* THREAD = Thread::current(); 161 ResourceMark rm(THREAD); 162 jint manifest_size; 163 char* manifest = read_manifest(entry, &manifest_size, CHECK); 164 165 if (manifest == NULL) { 166 return; 167 } 168 169 if (strstr(manifest, "Extension-List:") != NULL) { 170 tty->print_cr("-Xshare:dump does not support Extension-List in JAR manifest: %s", entry->name()); 171 vm_exit(1); 172 } 173 174 char* cp_attr = get_class_path_attr(entry->name(), manifest, manifest_size); 175 176 if (cp_attr != NULL && strlen(cp_attr) > 0) { 177 trace_class_path("found Class-Path: ", cp_attr); 178 179 char sep = os::file_separator()[0]; 180 const char* dir_name = entry->name(); 181 const char* dir_tail = strrchr(dir_name, sep); 182 int dir_len; 183 if (dir_tail == NULL) { 184 dir_len = 0; 185 } else { 186 dir_len = dir_tail - dir_name + 1; 187 } 188 189 // Split the cp_attr by spaces, and add each file 190 char* file_start = cp_attr; 191 char* end = file_start + strlen(file_start); 192 193 while (file_start < end) { 194 char* file_end = strchr(file_start, ' '); 195 if (file_end != NULL) { 196 *file_end = 0; 197 file_end += 1; 198 } else { 199 file_end = end; 200 } 201 202 int name_len = (int)strlen(file_start); 203 if (name_len > 0) { 204 ResourceMark rm(THREAD); 205 char* libname = NEW_RESOURCE_ARRAY(char, dir_len + name_len + 1); 206 *libname = 0; 207 strncat(libname, dir_name, dir_len); 208 strncat(libname, file_start, name_len); 209 trace_class_path("library = ", libname); 210 ClassLoader::update_class_path_entry_list(libname, true, false); 211 } 212 213 file_start = file_end; 214 } 215 } 216 } 217 218 void ClassLoaderExt::setup_search_paths() { 219 if (UseAppCDS) { 220 shared_paths_misc_info()->record_app_offset(); 221 ClassLoaderExt::setup_app_search_path(); 222 } 223 } 224 225 void ClassLoaderExt::setup_module_paths() { 226 if (UseAppCDS) { 227 ClassLoaderExt::setup_module_search_path(); 228 } 229 } 230 231 Thread* ClassLoaderExt::Context::_dump_thread = NULL; 232 233 void ClassLoaderExt::record_result(ClassLoaderExt::Context *context, 234 Symbol* class_name, 235 const s2 classpath_index, 236 InstanceKlass* result, 237 TRAPS) { 238 assert(DumpSharedSpaces, "Sanity"); 239 240 // We need to remember where the class comes from during dumping. 241 oop loader = result->class_loader(); 242 s2 classloader_type = ClassLoader::BOOT_LOADER; 243 if (SystemDictionary::is_system_class_loader(loader)) { 244 classloader_type = ClassLoader::APP_LOADER; 245 ClassLoaderExt::set_has_app_classes(); 246 } else if (SystemDictionary::is_platform_class_loader(loader)) { 247 classloader_type = ClassLoader::PLATFORM_LOADER; 248 ClassLoaderExt::set_has_platform_classes(); 249 } 250 result->set_shared_classpath_index(classpath_index); 251 result->set_class_loader_type(classloader_type); 252 } 253 254 void ClassLoaderExt::finalize_shared_paths_misc_info() { 255 if (UseAppCDS) { 256 if (!_has_app_classes) { 257 shared_paths_misc_info()->pop_app(); 258 } 259 } 260 } 261 262 // Load the class of the given name from the location given by path. The path is specified by 263 // the "source:" in the class list file (see classListParser.cpp), and can be a directory or 264 // a JAR file. 265 InstanceKlass* ClassLoaderExt::load_class(Symbol* name, const char* path, TRAPS) { 266 267 assert(name != NULL, "invariant"); 268 assert(DumpSharedSpaces && UseAppCDS, "this function is only used with -Xshare:dump and -XX:+UseAppCDS"); 269 ResourceMark rm(THREAD); 270 const char* class_name = name->as_C_string(); 271 272 const char* file_name = file_name_for_class_name(class_name, 273 name->utf8_length()); 274 assert(file_name != NULL, "invariant"); 275 276 // Lookup stream for parsing .class file 277 ClassFileStream* stream = NULL; 278 ClassPathEntry* e = find_classpath_entry_from_cache(path, CHECK_NULL); 279 if (e == NULL) { 280 return NULL; 281 } 282 { 283 PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(), 284 ((JavaThread*) THREAD)->get_thread_stat()->perf_timers_addr(), 285 PerfClassTraceTime::CLASS_LOAD); 286 stream = e->open_stream(file_name, CHECK_NULL); 287 } 288 289 if (NULL == stream) { 290 tty->print_cr("Preload Warning: Cannot find %s", class_name); 291 return NULL; 292 } 293 294 assert(stream != NULL, "invariant"); 295 stream->set_verify(true); 296 297 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 298 Handle protection_domain; 299 300 InstanceKlass* result = KlassFactory::create_from_stream(stream, 301 name, 302 loader_data, 303 protection_domain, 304 NULL, // host_klass 305 NULL, // cp_patches 306 THREAD); 307 308 if (HAS_PENDING_EXCEPTION) { 309 tty->print_cr("Preload Error: Failed to load %s", class_name); 310 return NULL; 311 } 312 result->set_shared_classpath_index(UNREGISTERED_INDEX); 313 SystemDictionaryShared::set_shared_class_misc_info(result, stream); 314 return result; 315 } 316 317 struct CachedClassPathEntry { 318 const char* _path; 319 ClassPathEntry* _entry; 320 }; 321 322 static GrowableArray<CachedClassPathEntry>* cached_path_entries = NULL; 323 324 ClassPathEntry* ClassLoaderExt::find_classpath_entry_from_cache(const char* path, TRAPS) { 325 // This is called from dump time so it's single threaded and there's no need for a lock. 326 assert(DumpSharedSpaces && UseAppCDS, "this function is only used with -Xshare:dump and -XX:+UseAppCDS"); 327 if (cached_path_entries == NULL) { 328 cached_path_entries = new (ResourceObj::C_HEAP, mtClass) GrowableArray<CachedClassPathEntry>(20, /*c heap*/ true); 329 } 330 CachedClassPathEntry ccpe; 331 for (int i=0; i<cached_path_entries->length(); i++) { 332 ccpe = cached_path_entries->at(i); 333 if (strcmp(ccpe._path, path) == 0) { 334 if (i != 0) { 335 // Put recent entries at the beginning to speed up searches. 336 cached_path_entries->remove_at(i); 337 cached_path_entries->insert_before(0, ccpe); 338 } 339 return ccpe._entry; 340 } 341 } 342 343 struct stat st; 344 if (os::stat(path, &st) != 0) { 345 // File or directory not found 346 return NULL; 347 } 348 ClassPathEntry* new_entry = NULL; 349 350 new_entry = create_class_path_entry(path, &st, false, false, CHECK_NULL); 351 if (new_entry == NULL) { 352 return NULL; 353 } 354 ccpe._path = strdup(path); 355 ccpe._entry = new_entry; 356 cached_path_entries->insert_before(0, ccpe); 357 return new_entry; 358 } 359 360 Klass* ClassLoaderExt::load_one_class(ClassListParser* parser, TRAPS) { 361 return parser->load_current_class(THREAD); 362 }