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