1 /* 2 * Copyright (c) 2014, 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/classFileStream.hpp" 27 #include "classfile/classListParser.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/classLoaderExt.hpp" 31 #include "classfile/compactHashtable.inline.hpp" 32 #include "classfile/dictionary.hpp" 33 #include "classfile/javaClasses.hpp" 34 #include "classfile/sharedClassUtil.hpp" 35 #include "classfile/symbolTable.hpp" 36 #include "classfile/systemDictionary.hpp" 37 #include "classfile/systemDictionaryShared.hpp" 38 #include "classfile/verificationType.hpp" 39 #include "classfile/vmSymbols.hpp" 40 #include "logging/log.hpp" 41 #include "memory/allocation.hpp" 42 #include "memory/filemap.hpp" 43 #include "memory/metadataFactory.hpp" 44 #include "memory/metaspaceClosure.hpp" 45 #include "memory/oopFactory.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "oops/instanceKlass.hpp" 48 #include "oops/klass.inline.hpp" 49 #include "oops/objArrayOop.inline.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/typeArrayOop.inline.hpp" 52 #include "runtime/handles.inline.hpp" 53 #include "runtime/java.hpp" 54 #include "runtime/javaCalls.hpp" 55 #include "runtime/mutexLocker.hpp" 56 #include "utilities/hashtable.inline.hpp" 57 #include "utilities/stringUtils.hpp" 58 59 60 objArrayOop SystemDictionaryShared::_shared_protection_domains = NULL; 61 objArrayOop SystemDictionaryShared::_shared_jar_urls = NULL; 62 objArrayOop SystemDictionaryShared::_shared_jar_manifests = NULL; 63 64 static Mutex* SharedDictionary_lock = NULL; 65 66 void SystemDictionaryShared::initialize(TRAPS) { 67 if (_java_system_loader != NULL) { 68 SharedDictionary_lock = new Mutex(Mutex::leaf, "SharedDictionary_lock", true); 69 70 // These classes need to be initialized before calling get_shared_jar_manifest(), etc. 71 SystemDictionary::ByteArrayInputStream_klass()->initialize(CHECK); 72 SystemDictionary::File_klass()->initialize(CHECK); 73 SystemDictionary::Jar_Manifest_klass()->initialize(CHECK); 74 SystemDictionary::CodeSource_klass()->initialize(CHECK); 75 } 76 } 77 78 oop SystemDictionaryShared::shared_protection_domain(int index) { 79 return _shared_protection_domains->obj_at(index); 80 } 81 82 oop SystemDictionaryShared::shared_jar_url(int index) { 83 return _shared_jar_urls->obj_at(index); 84 } 85 86 oop SystemDictionaryShared::shared_jar_manifest(int index) { 87 return _shared_jar_manifests->obj_at(index); 88 } 89 90 91 Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) { 92 Handle empty; 93 Handle manifest ; 94 if (shared_jar_manifest(shared_path_index) == NULL) { 95 SharedClassPathEntryExt* ent = (SharedClassPathEntryExt*)FileMapInfo::shared_path(shared_path_index); 96 long size = ent->manifest_size(); 97 if (size <= 0) { 98 return empty; // No manifest - return NULL handle 99 } 100 101 // ByteArrayInputStream bais = new ByteArrayInputStream(buf); 102 InstanceKlass* bais_klass = SystemDictionary::ByteArrayInputStream_klass(); 103 Handle bais = bais_klass->allocate_instance_handle(CHECK_(empty)); 104 { 105 const char* src = ent->manifest(); 106 assert(src != NULL, "No Manifest data"); 107 typeArrayOop buf = oopFactory::new_byteArray(size, CHECK_(empty)); 108 typeArrayHandle bufhandle(THREAD, buf); 109 char* dst = (char*)(buf->byte_at_addr(0)); 110 memcpy(dst, src, (size_t)size); 111 112 JavaValue result(T_VOID); 113 JavaCalls::call_special(&result, bais, bais_klass, 114 vmSymbols::object_initializer_name(), 115 vmSymbols::byte_array_void_signature(), 116 bufhandle, CHECK_(empty)); 117 } 118 119 // manifest = new Manifest(bais) 120 InstanceKlass* manifest_klass = SystemDictionary::Jar_Manifest_klass(); 121 manifest = manifest_klass->allocate_instance_handle(CHECK_(empty)); 122 { 123 JavaValue result(T_VOID); 124 JavaCalls::call_special(&result, manifest, manifest_klass, 125 vmSymbols::object_initializer_name(), 126 vmSymbols::input_stream_void_signature(), 127 bais, CHECK_(empty)); 128 } 129 atomic_set_shared_jar_manifest(shared_path_index, manifest()); 130 } 131 132 manifest = Handle(THREAD, shared_jar_manifest(shared_path_index)); 133 assert(manifest.not_null(), "sanity"); 134 return manifest; 135 } 136 137 Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) { 138 Handle url_h; 139 if (shared_jar_url(shared_path_index) == NULL) { 140 JavaValue result(T_OBJECT); 141 const char* path = FileMapInfo::shared_path_name(shared_path_index); 142 Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h)); 143 Klass* classLoaders_klass = 144 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 145 JavaCalls::call_static(&result, classLoaders_klass, 146 vmSymbols::toFileURL_name(), 147 vmSymbols::toFileURL_signature(), 148 path_string, CHECK_(url_h)); 149 150 atomic_set_shared_jar_url(shared_path_index, (oop)result.get_jobject()); 151 } 152 153 url_h = Handle(THREAD, shared_jar_url(shared_path_index)); 154 assert(url_h.not_null(), "sanity"); 155 return url_h; 156 } 157 158 Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) { 159 ResourceMark rm(THREAD); 160 Handle pkgname_string; 161 char* pkgname = (char*) ClassLoader::package_from_name((const char*) class_name->as_C_string()); 162 if (pkgname != NULL) { // Package prefix found 163 StringUtils::replace_no_expand(pkgname, "/", "."); 164 pkgname_string = java_lang_String::create_from_str(pkgname, 165 CHECK_(pkgname_string)); 166 } 167 return pkgname_string; 168 } 169 170 // Define Package for shared app classes from JAR file and also checks for 171 // package sealing (all done in Java code) 172 // See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html 173 void SystemDictionaryShared::define_shared_package(Symbol* class_name, 174 Handle class_loader, 175 Handle manifest, 176 Handle url, 177 TRAPS) { 178 assert(class_loader == _java_system_loader, "unexpected class loader"); 179 // get_package_name() returns a NULL handle if the class is in unnamed package 180 Handle pkgname_string = get_package_name(class_name, CHECK); 181 if (pkgname_string.not_null()) { 182 Klass* app_classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(); 183 JavaValue result(T_OBJECT); 184 JavaCallArguments args(3); 185 args.set_receiver(class_loader); 186 args.push_oop(pkgname_string); 187 args.push_oop(manifest); 188 args.push_oop(url); 189 JavaCalls::call_virtual(&result, app_classLoader_klass, 190 vmSymbols::defineOrCheckPackage_name(), 191 vmSymbols::defineOrCheckPackage_signature(), 192 &args, 193 CHECK); 194 } 195 } 196 197 // Define Package for shared app/platform classes from named module 198 void SystemDictionaryShared::define_shared_package(Symbol* class_name, 199 Handle class_loader, 200 ModuleEntry* mod_entry, 201 TRAPS) { 202 assert(mod_entry != NULL, "module_entry should not be NULL"); 203 Handle module_handle(THREAD, mod_entry->module()); 204 205 Handle pkg_name = get_package_name(class_name, CHECK); 206 assert(pkg_name.not_null(), "Package should not be null for class in named module"); 207 208 Klass* classLoader_klass; 209 if (SystemDictionary::is_system_class_loader(class_loader())) { 210 classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(); 211 } else { 212 assert(SystemDictionary::is_platform_class_loader(class_loader()), "unexpected classloader"); 213 classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_PlatformClassLoader_klass(); 214 } 215 216 JavaValue result(T_OBJECT); 217 JavaCallArguments args(2); 218 args.set_receiver(class_loader); 219 args.push_oop(pkg_name); 220 args.push_oop(module_handle); 221 JavaCalls::call_virtual(&result, classLoader_klass, 222 vmSymbols::definePackage_name(), 223 vmSymbols::definePackage_signature(), 224 &args, 225 CHECK); 226 } 227 228 // Get the ProtectionDomain associated with the CodeSource from the classloader. 229 Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader, 230 Handle url, TRAPS) { 231 // CodeSource cs = new CodeSource(url, null); 232 InstanceKlass* cs_klass = SystemDictionary::CodeSource_klass(); 233 Handle cs = cs_klass->allocate_instance_handle(CHECK_NH); 234 JavaValue void_result(T_VOID); 235 JavaCalls::call_special(&void_result, cs, cs_klass, 236 vmSymbols::object_initializer_name(), 237 vmSymbols::url_code_signer_array_void_signature(), 238 url, Handle(), CHECK_NH); 239 240 // protection_domain = SecureClassLoader.getProtectionDomain(cs); 241 Klass* secureClassLoader_klass = SystemDictionary::SecureClassLoader_klass(); 242 JavaValue obj_result(T_OBJECT); 243 JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass, 244 vmSymbols::getProtectionDomain_name(), 245 vmSymbols::getProtectionDomain_signature(), 246 cs, CHECK_NH); 247 return Handle(THREAD, (oop)obj_result.get_jobject()); 248 } 249 250 // Returns the ProtectionDomain associated with the JAR file identified by the url. 251 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 252 int shared_path_index, 253 Handle url, 254 TRAPS) { 255 Handle protection_domain; 256 if (shared_protection_domain(shared_path_index) == NULL) { 257 Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD); 258 atomic_set_shared_protection_domain(shared_path_index, pd()); 259 } 260 261 // Acquire from the cache because if another thread beats the current one to 262 // set the shared protection_domain and the atomic_set fails, the current thread 263 // needs to get the updated protection_domain from the cache. 264 protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index)); 265 assert(protection_domain.not_null(), "sanity"); 266 return protection_domain; 267 } 268 269 // Returns the ProtectionDomain associated with the moduleEntry. 270 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 271 ModuleEntry* mod, TRAPS) { 272 ClassLoaderData *loader_data = mod->loader_data(); 273 Handle protection_domain; 274 if (mod->shared_protection_domain() == NULL) { 275 Symbol* location = mod->location(); 276 if (location != NULL) { 277 Handle url_string = java_lang_String::create_from_symbol( 278 location, CHECK_(protection_domain)); 279 JavaValue result(T_OBJECT); 280 Klass* classLoaders_klass = 281 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 282 JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(), 283 vmSymbols::toFileURL_signature(), 284 url_string, CHECK_(protection_domain)); 285 Handle url = Handle(THREAD, (oop)result.get_jobject()); 286 287 Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD); 288 mod->set_shared_protection_domain(loader_data, pd); 289 } 290 } 291 292 protection_domain = Handle(THREAD, mod->shared_protection_domain()); 293 assert(protection_domain.not_null(), "sanity"); 294 return protection_domain; 295 } 296 297 // Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with 298 // the given InstanceKlass. 299 // Returns the ProtectionDomain for the InstanceKlass. 300 Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, TRAPS) { 301 Handle pd; 302 303 if (ik != NULL) { 304 int index = ik->shared_classpath_index(); 305 assert(index >= 0, "Sanity"); 306 SharedClassPathEntryExt* ent = 307 (SharedClassPathEntryExt*)FileMapInfo::shared_path(index); 308 Symbol* class_name = ik->name(); 309 310 if (ent->is_modules_image()) { 311 // For shared app/platform classes originated from the run-time image: 312 // The ProtectionDomains are cached in the corresponding ModuleEntries 313 // for fast access by the VM. 314 ResourceMark rm; 315 ClassLoaderData *loader_data = 316 ClassLoaderData::class_loader_data(class_loader()); 317 PackageEntryTable* pkgEntryTable = loader_data->packages(); 318 TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK_(pd)); 319 if (pkg_name != NULL) { 320 PackageEntry* pkg_entry = pkgEntryTable->lookup_only(pkg_name); 321 if (pkg_entry != NULL) { 322 ModuleEntry* mod_entry = pkg_entry->module(); 323 pd = get_shared_protection_domain(class_loader, mod_entry, THREAD); 324 define_shared_package(class_name, class_loader, mod_entry, CHECK_(pd)); 325 } 326 } 327 } else { 328 // For shared app/platform classes originated from JAR files on the class path: 329 // Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length 330 // as the shared classpath table in the shared archive (see 331 // FileMap::_shared_path_table in filemap.hpp for details). 332 // 333 // If a shared InstanceKlass k is loaded from the class path, let 334 // 335 // index = k->shared_classpath_index(): 336 // 337 // FileMap::_shared_path_table[index] identifies the JAR file that contains k. 338 // 339 // k's protection domain is: 340 // 341 // ProtectionDomain pd = _shared_protection_domains[index]; 342 // 343 // and k's Package is initialized using 344 // 345 // manifest = _shared_jar_manifests[index]; 346 // url = _shared_jar_urls[index]; 347 // define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 348 // 349 // Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by 350 // the corresponding SystemDictionaryShared::get_shared_xxx() function. 351 Handle manifest = get_shared_jar_manifest(index, CHECK_(pd)); 352 Handle url = get_shared_jar_url(index, CHECK_(pd)); 353 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 354 pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd)); 355 } 356 } 357 return pd; 358 } 359 360 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) { 361 oop class_loader = loader_data->class_loader(); 362 return (class_loader == NULL || 363 SystemDictionary::is_system_class_loader(class_loader) || 364 SystemDictionary::is_platform_class_loader(class_loader)); 365 } 366 367 // Currently AppCDS only archives classes from the run-time image, the 368 // -Xbootclasspath/a path, the class path, and the module path. 369 // 370 // Check if a shared class can be loaded by the specific classloader. Following 371 // are the "visible" archived classes for different classloaders. 372 // 373 // NULL classloader: 374 // - see SystemDictionary::is_shared_class_visible() 375 // Platform classloader: 376 // - Module class from runtime image. ModuleEntry must be defined in the 377 // classloader. 378 // App classloader: 379 // - Module Class from runtime image and module path. ModuleEntry must be defined in the 380 // classloader. 381 // - Class from -cp. The class must have no PackageEntry defined in any of the 382 // boot/platform/app classloader, or must be in the unnamed module defined in the 383 // AppClassLoader. 384 bool SystemDictionaryShared::is_shared_class_visible_for_classloader( 385 InstanceKlass* ik, 386 Handle class_loader, 387 const char* pkg_string, 388 Symbol* pkg_name, 389 PackageEntry* pkg_entry, 390 ModuleEntry* mod_entry, 391 TRAPS) { 392 assert(class_loader.not_null(), "Class loader should not be NULL"); 393 assert(Universe::is_module_initialized(), "Module system is not initialized"); 394 ResourceMark rm(THREAD); 395 396 int path_index = ik->shared_classpath_index(); 397 SharedClassPathEntry* ent = 398 (SharedClassPathEntry*)FileMapInfo::shared_path(path_index); 399 400 if (SystemDictionary::is_platform_class_loader(class_loader())) { 401 assert(ent != NULL, "shared class for PlatformClassLoader should have valid SharedClassPathEntry"); 402 // The PlatformClassLoader can only load archived class originated from the 403 // run-time image. The class' PackageEntry/ModuleEntry must be 404 // defined by the PlatformClassLoader. 405 if (mod_entry != NULL) { 406 // PackageEntry/ModuleEntry is found in the classloader. Check if the 407 // ModuleEntry's location agrees with the archived class' origination. 408 if (ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) { 409 return true; // Module class from the runtime image 410 } 411 } 412 } else if (SystemDictionary::is_system_class_loader(class_loader())) { 413 assert(ent != NULL, "shared class for system loader should have valid SharedClassPathEntry"); 414 if (pkg_string == NULL) { 415 // The archived class is in the unnamed package. Currently, the boot image 416 // does not contain any class in the unnamed package. 417 assert(!ent->is_modules_image(), "Class in the unnamed package must be from the classpath"); 418 if (path_index >= ClassLoaderExt::app_class_paths_start_index()) { 419 assert(path_index < ClassLoaderExt::app_module_paths_start_index(), "invalid path_index"); 420 return true; 421 } 422 } else { 423 // Check if this is from a PackageEntry/ModuleEntry defined in the AppClassloader. 424 if (pkg_entry == NULL) { 425 // It's not guaranteed that the class is from the classpath if the 426 // PackageEntry cannot be found from the AppClassloader. Need to check 427 // the boot and platform classloader as well. 428 if (get_package_entry(pkg_name, ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader())) == NULL && 429 get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data()) == NULL) { 430 // The PackageEntry is not defined in any of the boot/platform/app classloaders. 431 // The archived class must from -cp path and not from the runtime image. 432 if (!ent->is_modules_image() && path_index >= ClassLoaderExt::app_class_paths_start_index() && 433 path_index < ClassLoaderExt::app_module_paths_start_index()) { 434 return true; 435 } 436 } 437 } else if (mod_entry != NULL) { 438 // The package/module is defined in the AppClassLoader. We support 439 // archiving application module class from the runtime image or from 440 // a named module from a module path. 441 // Packages from the -cp path are in the unnamed_module. 442 if (ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) { 443 // shared module class from runtime image 444 return true; 445 } else if (pkg_entry->in_unnamed_module() && path_index >= ClassLoaderExt::app_class_paths_start_index() && 446 path_index < ClassLoaderExt::app_module_paths_start_index()) { 447 // shared class from -cp 448 DEBUG_ONLY( \ 449 ClassLoaderData* loader_data = class_loader_data(class_loader); \ 450 assert(mod_entry == loader_data->unnamed_module(), "the unnamed module is not defined in the classloader");) 451 return true; 452 } else { 453 if(!pkg_entry->in_unnamed_module() && 454 (path_index >= ClassLoaderExt::app_module_paths_start_index())&& 455 (path_index < FileMapInfo::get_number_of_shared_paths()) && 456 (strcmp(ent->name(), ClassLoader::skip_uri_protocol(mod_entry->location()->as_C_string())) == 0)) { 457 // shared module class from module path 458 return true; 459 } else { 460 assert(path_index < FileMapInfo::get_number_of_shared_paths(), "invalid path_index"); 461 } 462 } 463 } 464 } 465 } else { 466 // TEMP: if a shared class can be found by a custom loader, consider it visible now. 467 // FIXME: is this actually correct? 468 return true; 469 } 470 return false; 471 } 472 473 // The following stack shows how this code is reached: 474 // 475 // [0] SystemDictionaryShared::find_or_load_shared_class() 476 // [1] JVM_FindLoadedClass 477 // [2] java.lang.ClassLoader.findLoadedClass0() 478 // [3] java.lang.ClassLoader.findLoadedClass() 479 // [4] java.lang.ClassLoader.loadClass() 480 // [5] jdk.internal.loader.ClassLoaders$AppClassLoader_klass.loadClass() 481 // 482 // Because AppCDS supports only the PlatformClassLoader and AppClassLoader, we make the following 483 // assumptions (based on the JDK 8.0 source code): 484 // 485 // [a] these two loaders use the default implementation of 486 // ClassLoader.loadClass(String name, boolean resolve), which 487 // [b] calls findLoadedClass(name), immediately followed by parent.loadClass(), 488 // immediately followed by findClass(name). 489 // [c] If the requested class is a shared class of the current class loader, parent.loadClass() 490 // always returns null, and 491 // [d] if AppCDS is not enabled, the class would be loaded by findClass() by decoding it from a 492 // JAR file and then parsed. 493 // 494 // Given these assumptions, we intercept the findLoadedClass() call to invoke 495 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 496 // the archive. The reasons are: 497 // 498 // + Because AppCDS is a commercial feature, we want to hide the implementation. There 499 // is currently no easy way to hide Java code, so we did it with native code. 500 // + Start-up is improved because we avoid decoding the JAR file, and avoid delegating 501 // to the parent (since we know the parent will not find this class). 502 // 503 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 504 // needs to be redesigned. 505 // 506 // An alternative is to modify the Java code of AppClassLoader.loadClass(). 507 // 508 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 509 Symbol* name, Handle class_loader, TRAPS) { 510 InstanceKlass* k = NULL; 511 if (UseSharedSpaces) { 512 FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header(); 513 if (!header->has_platform_or_app_classes()) { 514 return NULL; 515 } 516 517 if (shared_dictionary() != NULL && 518 (SystemDictionary::is_system_class_loader(class_loader()) || 519 SystemDictionary::is_platform_class_loader(class_loader()))) { 520 // Fix for 4474172; see evaluation for more details 521 class_loader = Handle( 522 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); 523 ClassLoaderData *loader_data = register_loader(class_loader); 524 Dictionary* dictionary = loader_data->dictionary(); 525 526 unsigned int d_hash = dictionary->compute_hash(name); 527 528 bool DoObjectLock = true; 529 if (is_parallelCapable(class_loader)) { 530 DoObjectLock = false; 531 } 532 533 // Make sure we are synchronized on the class loader before we proceed 534 // 535 // Note: currently, find_or_load_shared_class is called only from 536 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 537 // which are parallel-capable loaders, so this lock is NOT taken. 538 Handle lockObject = compute_loader_lock_object(class_loader, THREAD); 539 check_loader_lock_contention(lockObject, THREAD); 540 ObjectLocker ol(lockObject, THREAD, DoObjectLock); 541 542 { 543 MutexLocker mu(SystemDictionary_lock, THREAD); 544 Klass* check = find_class(d_hash, name, dictionary); 545 if (check != NULL) { 546 return InstanceKlass::cast(check); 547 } 548 } 549 550 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 551 if (k != NULL) { 552 define_instance_class(k, CHECK_NULL); 553 } 554 } 555 } 556 return k; 557 } 558 559 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 560 Symbol* class_name, Handle class_loader, TRAPS) { 561 assert(UseSharedSpaces, "must be"); 562 assert(shared_dictionary() != NULL, "already checked"); 563 Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name); 564 565 if (k != NULL) { 566 InstanceKlass* ik = InstanceKlass::cast(k); 567 if ((ik->is_shared_app_class() && 568 SystemDictionary::is_system_class_loader(class_loader())) || 569 (ik->is_shared_platform_class() && 570 SystemDictionary::is_platform_class_loader(class_loader()))) { 571 Handle protection_domain = 572 SystemDictionaryShared::init_security_info(class_loader, ik, CHECK_NULL); 573 return load_shared_class(ik, class_loader, protection_domain, THREAD); 574 } 575 } 576 577 return NULL; 578 } 579 580 void SystemDictionaryShared::oops_do(OopClosure* f) { 581 f->do_oop((oop*)&_shared_protection_domains); 582 f->do_oop((oop*)&_shared_jar_urls); 583 f->do_oop((oop*)&_shared_jar_manifests); 584 } 585 586 void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) { 587 if (_shared_protection_domains == NULL) { 588 _shared_protection_domains = oopFactory::new_objArray( 589 SystemDictionary::ProtectionDomain_klass(), size, CHECK); 590 } 591 } 592 593 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) { 594 if (_shared_jar_urls == NULL) { 595 _shared_jar_urls = oopFactory::new_objArray( 596 SystemDictionary::URL_klass(), size, CHECK); 597 } 598 } 599 600 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) { 601 if (_shared_jar_manifests == NULL) { 602 _shared_jar_manifests = oopFactory::new_objArray( 603 SystemDictionary::Jar_Manifest_klass(), size, CHECK); 604 } 605 } 606 607 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) { 608 allocate_shared_protection_domain_array(size, CHECK); 609 allocate_shared_jar_url_array(size, CHECK); 610 allocate_shared_jar_manifest_array(size, CHECK); 611 } 612 613 // This function is called for loading only UNREGISTERED classes 614 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name, 615 Handle class_loader, 616 Handle protection_domain, 617 const ClassFileStream* cfs, 618 TRAPS) { 619 if (shared_dictionary() == NULL) { 620 return NULL; 621 } 622 if (class_name == NULL) { // don't do this for anonymous classes 623 return NULL; 624 } 625 if (class_loader.is_null() || 626 SystemDictionary::is_system_class_loader(class_loader()) || 627 SystemDictionary::is_platform_class_loader(class_loader())) { 628 // Do nothing for the BUILTIN loaders. 629 return NULL; 630 } 631 632 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 633 Klass* k; 634 635 { // UNREGISTERED loader 636 if (!shared_dictionary()->class_exists_for_unregistered_loader(class_name)) { 637 // No classes of this name for unregistered loaders. 638 return NULL; 639 } 640 641 int clsfile_size = cfs->length(); 642 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 643 644 k = shared_dictionary()->find_class_for_unregistered_loader(class_name, 645 clsfile_size, clsfile_crc32); 646 } 647 648 if (k == NULL) { // not archived 649 return NULL; 650 } 651 652 return acquire_class_for_current_thread(InstanceKlass::cast(k), class_loader, 653 protection_domain, THREAD); 654 } 655 656 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 657 InstanceKlass *ik, 658 Handle class_loader, 659 Handle protection_domain, 660 TRAPS) { 661 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 662 663 { 664 MutexLocker mu(SharedDictionary_lock, THREAD); 665 if (ik->class_loader_data() != NULL) { 666 // ik is already loaded (by this loader or by a different loader) 667 // or ik is being loaded by a different thread (by this loader or by a different loader) 668 return NULL; 669 } 670 671 // No other thread has acquired this yet, so give it to *this thread* 672 ik->set_class_loader_data(loader_data); 673 } 674 675 // No longer holding SharedDictionary_lock 676 // No need to lock, as <ik> can be held only by a single thread. 677 loader_data->add_class(ik); 678 679 // Load and check super/interfaces, restore unsharable info 680 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, THREAD); 681 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) { 682 // TODO: clean up <ik> so it can be used again 683 return NULL; 684 } 685 686 return shared_klass; 687 } 688 689 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name, 690 ClassLoaderData* loader_data, 691 InstanceKlass* k, 692 TRAPS) { 693 assert(DumpSharedSpaces, "only when dumping"); 694 assert(boot_loader_dictionary() != NULL, "must be"); 695 696 if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) { 697 MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this. 698 add_to_hierarchy(k, CHECK_0); 699 return true; 700 } 701 return false; 702 } 703 704 // This function is called to resolve the super/interfaces of shared classes for 705 // non-built-in loaders. E.g., ChildClass in the below example 706 // where "super:" (and optionally "interface:") have been specified. 707 // 708 // java/lang/Object id: 0 709 // Interface id: 2 super: 0 source: cust.jar 710 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar 711 Klass* SystemDictionaryShared::dump_time_resolve_super_or_fail( 712 Symbol* child_name, Symbol* class_name, Handle class_loader, 713 Handle protection_domain, bool is_superclass, TRAPS) { 714 715 assert(DumpSharedSpaces, "only when dumping"); 716 717 ClassListParser* parser = ClassListParser::instance(); 718 if (parser == NULL) { 719 // We're still loading the well-known classes, before the ClassListParser is created. 720 return NULL; 721 } 722 if (child_name->equals(parser->current_class_name())) { 723 // When this function is called, all the numbered super and interface types 724 // must have already been loaded. Hence this function is never recursively called. 725 if (is_superclass) { 726 return parser->lookup_super_for_current_class(class_name); 727 } else { 728 return parser->lookup_interface_for_current_class(class_name); 729 } 730 } else { 731 // The VM is not trying to resolve a super type of parser->current_class_name(). 732 // Instead, it's resolving an error class (because parser->current_class_name() has 733 // failed parsing or verification). Don't do anything here. 734 return NULL; 735 } 736 } 737 738 struct SharedMiscInfo { 739 Klass* _klass; 740 int _clsfile_size; 741 int _clsfile_crc32; 742 }; 743 744 static GrowableArray<SharedMiscInfo>* misc_info_array = NULL; 745 746 void SystemDictionaryShared::set_shared_class_misc_info(Klass* k, ClassFileStream* cfs) { 747 assert(DumpSharedSpaces, "only when dumping"); 748 int clsfile_size = cfs->length(); 749 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 750 751 if (misc_info_array == NULL) { 752 misc_info_array = new (ResourceObj::C_HEAP, mtClass) GrowableArray<SharedMiscInfo>(20, /*c heap*/ true); 753 } 754 755 SharedMiscInfo misc_info; 756 DEBUG_ONLY({ 757 for (int i=0; i<misc_info_array->length(); i++) { 758 misc_info = misc_info_array->at(i); 759 assert(misc_info._klass != k, "cannot call set_shared_class_misc_info twice for the same class"); 760 } 761 }); 762 763 misc_info._klass = k; 764 misc_info._clsfile_size = clsfile_size; 765 misc_info._clsfile_crc32 = clsfile_crc32; 766 767 misc_info_array->append(misc_info); 768 } 769 770 void SystemDictionaryShared::init_shared_dictionary_entry(Klass* k, DictionaryEntry* ent) { 771 SharedDictionaryEntry* entry = (SharedDictionaryEntry*)ent; 772 entry->_id = -1; 773 entry->_clsfile_size = -1; 774 entry->_clsfile_crc32 = -1; 775 entry->_verifier_constraints = NULL; 776 entry->_verifier_constraint_flags = NULL; 777 778 if (misc_info_array != NULL) { 779 for (int i=0; i<misc_info_array->length(); i++) { 780 SharedMiscInfo misc_info = misc_info_array->at(i); 781 if (misc_info._klass == k) { 782 entry->_clsfile_size = misc_info._clsfile_size; 783 entry->_clsfile_crc32 = misc_info._clsfile_crc32; 784 misc_info_array->remove_at(i); 785 return; 786 } 787 } 788 } 789 } 790 791 bool SystemDictionaryShared::add_verification_constraint(Klass* k, Symbol* name, 792 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 793 assert(DumpSharedSpaces, "called at dump time only"); 794 795 // Skip anonymous classes, which are not archived as they are not in 796 // dictionary (see assert_no_anonymoys_classes_in_dictionaries() in 797 // VM_PopulateDumpSharedSpace::doit()). 798 if (k->class_loader_data()->is_anonymous()) { 799 return true; // anonymous classes are not archived, skip 800 } 801 802 SharedDictionaryEntry* entry = ((SharedDictionary*)(k->class_loader_data()->dictionary()))->find_entry_for(k); 803 ResourceMark rm; 804 // Lambda classes are not archived and will be regenerated at runtime. 805 if (entry == NULL && strstr(k->name()->as_C_string(), "Lambda$") != NULL) { 806 return true; 807 } 808 assert(entry != NULL, "class should be in dictionary before being verified"); 809 entry->add_verification_constraint(name, from_name, from_field_is_protected, 810 from_is_array, from_is_object); 811 if (entry->is_builtin()) { 812 // For builtin class loaders, we can try to complete the verification check at dump time, 813 // because we can resolve all the constraint classes. 814 return false; 815 } else { 816 // For non-builtin class loaders, we cannot complete the verification check at dump time, 817 // because at dump time we don't know how to resolve classes for such loaders. 818 return true; 819 } 820 } 821 822 void SystemDictionaryShared::finalize_verification_constraints() { 823 boot_loader_dictionary()->finalize_verification_constraints(); 824 } 825 826 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 827 TRAPS) { 828 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 829 SharedDictionaryEntry* entry = shared_dictionary()->find_entry_for(klass); 830 assert(entry != NULL, "call this only for shared classes"); 831 entry->check_verification_constraints(klass, THREAD); 832 } 833 834 SharedDictionaryEntry* SharedDictionary::find_entry_for(Klass* klass) { 835 Symbol* class_name = klass->name(); 836 unsigned int hash = compute_hash(class_name); 837 int index = hash_to_index(hash); 838 839 for (SharedDictionaryEntry* entry = bucket(index); 840 entry != NULL; 841 entry = entry->next()) { 842 if (entry->hash() == hash && entry->literal() == klass) { 843 return entry; 844 } 845 } 846 847 return NULL; 848 } 849 850 void SharedDictionary::finalize_verification_constraints() { 851 int bytes = 0, count = 0; 852 for (int index = 0; index < table_size(); index++) { 853 for (SharedDictionaryEntry *probe = bucket(index); 854 probe != NULL; 855 probe = probe->next()) { 856 int n = probe->finalize_verification_constraints(); 857 if (n > 0) { 858 bytes += n; 859 count ++; 860 } 861 } 862 } 863 if (log_is_enabled(Info, cds, verification)) { 864 double avg = 0; 865 if (count > 0) { 866 avg = double(bytes) / double(count); 867 } 868 log_info(cds, verification)("Recorded verification constraints for %d classes = %d bytes (avg = %.2f bytes) ", count, bytes, avg); 869 } 870 } 871 872 void SharedDictionaryEntry::add_verification_constraint(Symbol* name, 873 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 874 if (_verifier_constraints == NULL) { 875 _verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<Symbol*>(8, true, mtClass); 876 } 877 if (_verifier_constraint_flags == NULL) { 878 _verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, true, mtClass); 879 } 880 GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints; 881 for (int i=0; i<vc_array->length(); i+= 2) { 882 if (name == vc_array->at(i) && 883 from_name == vc_array->at(i+1)) { 884 return; 885 } 886 } 887 vc_array->append(name); 888 vc_array->append(from_name); 889 890 GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags; 891 char c = 0; 892 c |= from_field_is_protected ? FROM_FIELD_IS_PROTECTED : 0; 893 c |= from_is_array ? FROM_IS_ARRAY : 0; 894 c |= from_is_object ? FROM_IS_OBJECT : 0; 895 vcflags_array->append(c); 896 897 if (log_is_enabled(Trace, cds, verification)) { 898 ResourceMark rm; 899 log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s", 900 instance_klass()->external_name(), from_name->as_klass_external_name(), 901 name->as_klass_external_name()); 902 } 903 } 904 905 int SharedDictionaryEntry::finalize_verification_constraints() { 906 assert(DumpSharedSpaces, "called at dump time only"); 907 Thread* THREAD = Thread::current(); 908 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 909 GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints; 910 GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags; 911 912 if (vc_array != NULL) { 913 if (log_is_enabled(Trace, cds, verification)) { 914 ResourceMark rm; 915 log_trace(cds, verification)("finalize_verification_constraint: %s", 916 literal()->external_name()); 917 } 918 919 // Copy the constraints from C_HEAP-alloced GrowableArrays to Metaspace-alloced 920 // Arrays 921 int size = 0; 922 { 923 // FIXME: change this to be done after relocation, so we can use symbol offset?? 924 int length = vc_array->length(); 925 Array<Symbol*>* out = MetadataFactory::new_array<Symbol*>(loader_data, length, 0, THREAD); 926 assert(out != NULL, "Dump time allocation failure would have aborted VM"); 927 for (int i=0; i<length; i++) { 928 out->at_put(i, vc_array->at(i)); 929 } 930 _verifier_constraints = out; 931 size += out->size() * BytesPerWord; 932 delete vc_array; 933 } 934 { 935 int length = vcflags_array->length(); 936 Array<char>* out = MetadataFactory::new_array<char>(loader_data, length, 0, THREAD); 937 assert(out != NULL, "Dump time allocation failure would have aborted VM"); 938 for (int i=0; i<length; i++) { 939 out->at_put(i, vcflags_array->at(i)); 940 } 941 _verifier_constraint_flags = out; 942 size += out->size() * BytesPerWord; 943 delete vcflags_array; 944 } 945 946 return size; 947 } 948 return 0; 949 } 950 951 void SharedDictionaryEntry::check_verification_constraints(InstanceKlass* klass, TRAPS) { 952 Array<Symbol*>* vc_array = (Array<Symbol*>*)_verifier_constraints; 953 Array<char>* vcflags_array = (Array<char>*)_verifier_constraint_flags; 954 955 if (vc_array != NULL) { 956 int length = vc_array->length(); 957 for (int i=0; i<length; i+=2) { 958 Symbol* name = vc_array->at(i); 959 Symbol* from_name = vc_array->at(i+1); 960 char c = vcflags_array->at(i/2); 961 962 bool from_field_is_protected = (c & FROM_FIELD_IS_PROTECTED) ? true : false; 963 bool from_is_array = (c & FROM_IS_ARRAY) ? true : false; 964 bool from_is_object = (c & FROM_IS_OBJECT) ? true : false; 965 966 bool ok = VerificationType::resolve_and_check_assignability(klass, name, 967 from_name, from_field_is_protected, from_is_array, from_is_object, CHECK); 968 if (!ok) { 969 ResourceMark rm(THREAD); 970 stringStream ss; 971 972 ss.print_cr("Bad type on operand stack"); 973 ss.print_cr("Exception Details:"); 974 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 975 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 976 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 977 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 978 } 979 } 980 } 981 } 982 983 void SharedDictionaryEntry::metaspace_pointers_do(MetaspaceClosure* it) { 984 it->push((Array<Symbol*>**)&_verifier_constraints); 985 it->push((Array<char>**)&_verifier_constraint_flags); 986 } 987 988 bool SharedDictionary::add_non_builtin_klass(const Symbol* class_name, 989 ClassLoaderData* loader_data, 990 InstanceKlass* klass) { 991 992 assert(DumpSharedSpaces, "supported only when dumping"); 993 assert(klass != NULL, "adding NULL klass"); 994 assert(klass->name() == class_name, "sanity check on name"); 995 assert(klass->shared_classpath_index() < 0, 996 "the shared classpath index should not be set for shared class loaded by the custom loaders"); 997 998 // Add an entry for a non-builtin class. 999 // For a shared class for custom class loaders, SystemDictionary::resolve_or_null will 1000 // not find this class, because is_builtin() is false. 1001 unsigned int hash = compute_hash(class_name); 1002 int index = hash_to_index(hash); 1003 1004 for (SharedDictionaryEntry* entry = bucket(index); 1005 entry != NULL; 1006 entry = entry->next()) { 1007 if (entry->hash() == hash) { 1008 Klass* klass = (Klass*)entry->literal(); 1009 if (klass->name() == class_name && klass->class_loader_data() == loader_data) { 1010 // There is already a class defined with the same name 1011 return false; 1012 } 1013 } 1014 } 1015 1016 assert(Dictionary::entry_size() >= sizeof(SharedDictionaryEntry), "must be big enough"); 1017 SharedDictionaryEntry* entry = (SharedDictionaryEntry*)new_entry(hash, klass); 1018 add_entry(index, entry); 1019 1020 assert(entry->is_unregistered(), "sanity"); 1021 assert(!entry->is_builtin(), "sanity"); 1022 return true; 1023 } 1024 1025 1026 //----------------- 1027 // SharedDictionary 1028 //----------------- 1029 1030 1031 Klass* SharedDictionary::find_class_for_builtin_loader(const Symbol* name) const { 1032 SharedDictionaryEntry* entry = get_entry_for_builtin_loader(name); 1033 return entry != NULL ? entry->instance_klass() : (Klass*)NULL; 1034 } 1035 1036 Klass* SharedDictionary::find_class_for_unregistered_loader(const Symbol* name, 1037 int clsfile_size, 1038 int clsfile_crc32) const { 1039 1040 const SharedDictionaryEntry* entry = get_entry_for_unregistered_loader(name, 1041 clsfile_size, 1042 clsfile_crc32); 1043 return entry != NULL ? entry->instance_klass() : (Klass*)NULL; 1044 } 1045 1046 void SharedDictionary::update_entry(Klass* klass, int id) { 1047 assert(DumpSharedSpaces, "supported only when dumping"); 1048 Symbol* class_name = klass->name(); 1049 unsigned int hash = compute_hash(class_name); 1050 int index = hash_to_index(hash); 1051 1052 for (SharedDictionaryEntry* entry = bucket(index); 1053 entry != NULL; 1054 entry = entry->next()) { 1055 if (entry->hash() == hash && entry->literal() == klass) { 1056 entry->_id = id; 1057 return; 1058 } 1059 } 1060 1061 ShouldNotReachHere(); 1062 } 1063 1064 SharedDictionaryEntry* SharedDictionary::get_entry_for_builtin_loader(const Symbol* class_name) const { 1065 assert(!DumpSharedSpaces, "supported only when at runtime"); 1066 unsigned int hash = compute_hash(class_name); 1067 const int index = hash_to_index(hash); 1068 1069 for (SharedDictionaryEntry* entry = bucket(index); 1070 entry != NULL; 1071 entry = entry->next()) { 1072 if (entry->hash() == hash && entry->equals(class_name)) { 1073 if (entry->is_builtin()) { 1074 return entry; 1075 } 1076 } 1077 } 1078 return NULL; 1079 } 1080 1081 SharedDictionaryEntry* SharedDictionary::get_entry_for_unregistered_loader(const Symbol* class_name, 1082 int clsfile_size, 1083 int clsfile_crc32) const { 1084 assert(!DumpSharedSpaces, "supported only when at runtime"); 1085 unsigned int hash = compute_hash(class_name); 1086 int index = hash_to_index(hash); 1087 1088 for (SharedDictionaryEntry* entry = bucket(index); 1089 entry != NULL; 1090 entry = entry->next()) { 1091 if (entry->hash() == hash && entry->equals(class_name)) { 1092 if (entry->is_unregistered()) { 1093 if (clsfile_size == -1) { 1094 // We're called from class_exists_for_unregistered_loader. At run time, we want to 1095 // compute the CRC of a ClassFileStream only if there is an UNREGISTERED class 1096 // with the matching name. 1097 return entry; 1098 } else { 1099 // We're called from find_class_for_unregistered_loader 1100 if (entry->_clsfile_size && clsfile_crc32 == entry->_clsfile_crc32) { 1101 return entry; 1102 } 1103 } 1104 1105 // There can be only 1 class with this name for unregistered loaders. 1106 return NULL; 1107 } 1108 } 1109 } 1110 return NULL; 1111 }