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