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