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 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 char* dst = (char*)(buf->byte_at_addr(0)); 91 memcpy(dst, src, (size_t)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 Klass* check = find_class(d_hash, name, dictionary); 509 if (check != NULL) { 510 return InstanceKlass::cast(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 Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name); 528 529 if (k != NULL) { 530 InstanceKlass* ik = InstanceKlass::cast(k); 531 if ((ik->is_shared_app_class() && 532 SystemDictionary::is_system_class_loader(class_loader())) || 533 (ik->is_shared_platform_class() && 534 SystemDictionary::is_platform_class_loader(class_loader()))) { 535 Handle protection_domain = 536 SystemDictionaryShared::init_security_info(class_loader, ik, CHECK_NULL); 537 return load_shared_class(ik, class_loader, protection_domain, THREAD); 538 } 539 } 540 541 return NULL; 542 } 543 544 void SystemDictionaryShared::oops_do(OopClosure* f) { 545 f->do_oop((oop*)&_shared_protection_domains); 546 f->do_oop((oop*)&_shared_jar_urls); 547 f->do_oop((oop*)&_shared_jar_manifests); 548 } 549 550 void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) { 551 if (_shared_protection_domains == NULL) { 552 _shared_protection_domains = oopFactory::new_objArray( 553 SystemDictionary::ProtectionDomain_klass(), size, CHECK); 554 } 555 } 556 557 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) { 558 if (_shared_jar_urls == NULL) { 559 _shared_jar_urls = oopFactory::new_objArray( 560 SystemDictionary::URL_klass(), size, CHECK); 561 } 562 } 563 564 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) { 565 if (_shared_jar_manifests == NULL) { 566 _shared_jar_manifests = oopFactory::new_objArray( 567 SystemDictionary::Jar_Manifest_klass(), size, CHECK); 568 } 569 } 570 571 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) { 572 allocate_shared_protection_domain_array(size, CHECK); 573 allocate_shared_jar_url_array(size, CHECK); 574 allocate_shared_jar_manifest_array(size, CHECK); 575 } 576 577 // This function is called for loading only UNREGISTERED classes 578 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name, 579 Handle class_loader, 580 Handle protection_domain, 581 const ClassFileStream* cfs, 582 TRAPS) { 583 if (shared_dictionary() == NULL) { 584 return NULL; 585 } 586 if (class_name == NULL) { // don't do this for anonymous classes 587 return NULL; 588 } 589 if (class_loader.is_null() || 590 SystemDictionary::is_system_class_loader(class_loader()) || 591 SystemDictionary::is_platform_class_loader(class_loader())) { 592 // Do nothing for the BUILTIN loaders. 593 return NULL; 594 } 595 596 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 597 Klass* k; 598 599 { // UNREGISTERED loader 600 if (!shared_dictionary()->class_exists_for_unregistered_loader(class_name)) { 601 // No classes of this name for unregistered loaders. 602 return NULL; 603 } 604 605 int clsfile_size = cfs->length(); 606 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 607 608 k = shared_dictionary()->find_class_for_unregistered_loader(class_name, 609 clsfile_size, clsfile_crc32); 610 } 611 612 if (k == NULL) { // not archived 613 return NULL; 614 } 615 616 return acquire_class_for_current_thread(InstanceKlass::cast(k), class_loader, 617 protection_domain, THREAD); 618 } 619 620 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 621 InstanceKlass *ik, 622 Handle class_loader, 623 Handle protection_domain, 624 TRAPS) { 625 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 626 627 { 628 MutexLocker mu(SharedDictionary_lock, THREAD); 629 if (ik->class_loader_data() != NULL) { 630 // ik is already loaded (by this loader or by a different loader) 631 // or ik is being loaded by a different thread (by this loader or by a different loader) 632 return NULL; 633 } 634 635 // No other thread has acquired this yet, so give it to *this thread* 636 ik->set_class_loader_data(loader_data); 637 } 638 639 // No longer holding SharedDictionary_lock 640 // No need to lock, as <ik> can be held only by a single thread. 641 loader_data->add_class(ik); 642 643 // Load and check super/interfaces, restore unsharable info 644 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, THREAD); 645 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) { 646 // TODO: clean up <ik> so it can be used again 647 return NULL; 648 } 649 650 return shared_klass; 651 } 652 653 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name, 654 ClassLoaderData* loader_data, 655 InstanceKlass* k, 656 TRAPS) { 657 assert(DumpSharedSpaces, "only when dumping"); 658 assert(boot_loader_dictionary() != NULL, "must be"); 659 660 if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) { 661 MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this. 662 add_to_hierarchy(k, CHECK_0); 663 return true; 664 } 665 return false; 666 } 667 668 // This function is called to resolve the super/interfaces of shared classes for 669 // non-built-in loaders. E.g., ChildClass in the below example 670 // where "super:" (and optionally "interface:") have been specified. 671 // 672 // java/lang/Object id: 0 673 // Interface id: 2 super: 0 source: cust.jar 674 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar 675 Klass* SystemDictionaryShared::dump_time_resolve_super_or_fail( 676 Symbol* child_name, Symbol* class_name, Handle class_loader, 677 Handle protection_domain, bool is_superclass, TRAPS) { 678 679 assert(DumpSharedSpaces, "only when dumping"); 680 681 ClassListParser* parser = ClassListParser::instance(); 682 if (parser == NULL) { 683 // We're still loading the well-known classes, before the ClassListParser is created. 684 return NULL; 685 } 686 if (child_name->equals(parser->current_class_name())) { 687 // When this function is called, all the numbered super and interface types 688 // must have already been loaded. Hence this function is never recursively called. 689 if (is_superclass) { 690 return parser->lookup_super_for_current_class(class_name); 691 } else { 692 return parser->lookup_interface_for_current_class(class_name); 693 } 694 } else { 695 // The VM is not trying to resolve a super type of parser->current_class_name(). 696 // Instead, it's resolving an error class (because parser->current_class_name() has 697 // failed parsing or verification). Don't do anything here. 698 return NULL; 699 } 700 } 701 702 struct SharedMiscInfo { 703 Klass* _klass; 704 int _clsfile_size; 705 int _clsfile_crc32; 706 }; 707 708 static GrowableArray<SharedMiscInfo>* misc_info_array = NULL; 709 710 void SystemDictionaryShared::set_shared_class_misc_info(Klass* k, ClassFileStream* cfs) { 711 assert(DumpSharedSpaces, "only when dumping"); 712 int clsfile_size = cfs->length(); 713 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 714 715 if (misc_info_array == NULL) { 716 misc_info_array = new (ResourceObj::C_HEAP, mtClass) GrowableArray<SharedMiscInfo>(20, /*c heap*/ true); 717 } 718 719 SharedMiscInfo misc_info; 720 DEBUG_ONLY({ 721 for (int i=0; i<misc_info_array->length(); i++) { 722 misc_info = misc_info_array->at(i); 723 assert(misc_info._klass != k, "cannot call set_shared_class_misc_info twice for the same class"); 724 } 725 }); 726 727 misc_info._klass = k; 728 misc_info._clsfile_size = clsfile_size; 729 misc_info._clsfile_crc32 = clsfile_crc32; 730 731 misc_info_array->append(misc_info); 732 } 733 734 void SystemDictionaryShared::init_shared_dictionary_entry(Klass* k, DictionaryEntry* ent) { 735 SharedDictionaryEntry* entry = (SharedDictionaryEntry*)ent; 736 entry->_id = -1; 737 entry->_clsfile_size = -1; 738 entry->_clsfile_crc32 = -1; 739 entry->_verifier_constraints = NULL; 740 entry->_verifier_constraint_flags = NULL; 741 742 if (misc_info_array != NULL) { 743 for (int i=0; i<misc_info_array->length(); i++) { 744 SharedMiscInfo misc_info = misc_info_array->at(i); 745 if (misc_info._klass == k) { 746 entry->_clsfile_size = misc_info._clsfile_size; 747 entry->_clsfile_crc32 = misc_info._clsfile_crc32; 748 misc_info_array->remove_at(i); 749 return; 750 } 751 } 752 } 753 } 754 755 bool SystemDictionaryShared::add_verification_constraint(Klass* k, Symbol* name, 756 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 757 assert(DumpSharedSpaces, "called at dump time only"); 758 759 // Skip anonymous classes, which are not archived as they are not in 760 // dictionary (see assert_no_anonymoys_classes_in_dictionaries() in 761 // VM_PopulateDumpSharedSpace::doit()). 762 if (k->class_loader_data()->is_anonymous()) { 763 return true; // anonymous classes are not archived, skip 764 } 765 766 SharedDictionaryEntry* entry = ((SharedDictionary*)(k->class_loader_data()->dictionary()))->find_entry_for(k); 767 ResourceMark rm; 768 // Lambda classes are not archived and will be regenerated at runtime. 769 if (entry == NULL && strstr(k->name()->as_C_string(), "Lambda$") != NULL) { 770 return true; 771 } 772 assert(entry != NULL, "class should be in dictionary before being verified"); 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() { 787 boot_loader_dictionary()->finalize_verification_constraints(); 788 } 789 790 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 791 TRAPS) { 792 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 793 SharedDictionaryEntry* entry = shared_dictionary()->find_entry_for(klass); 794 assert(entry != NULL, "call this only for shared classes"); 795 entry->check_verification_constraints(klass, THREAD); 796 } 797 798 SharedDictionaryEntry* SharedDictionary::find_entry_for(Klass* klass) { 799 Symbol* class_name = klass->name(); 800 unsigned int hash = compute_hash(class_name); 801 int index = hash_to_index(hash); 802 803 for (SharedDictionaryEntry* entry = bucket(index); 804 entry != NULL; 805 entry = entry->next()) { 806 if (entry->hash() == hash && entry->literal() == klass) { 807 return entry; 808 } 809 } 810 811 return NULL; 812 } 813 814 void SharedDictionary::finalize_verification_constraints() { 815 int bytes = 0, count = 0; 816 for (int index = 0; index < table_size(); index++) { 817 for (SharedDictionaryEntry *probe = bucket(index); 818 probe != NULL; 819 probe = probe->next()) { 820 int n = probe->finalize_verification_constraints(); 821 if (n > 0) { 822 bytes += n; 823 count ++; 824 } 825 } 826 } 827 if (log_is_enabled(Info, cds, verification)) { 828 double avg = 0; 829 if (count > 0) { 830 avg = double(bytes) / double(count); 831 } 832 log_info(cds, verification)("Recorded verification constraints for %d classes = %d bytes (avg = %.2f bytes) ", count, bytes, avg); 833 } 834 } 835 836 void SharedDictionaryEntry::add_verification_constraint(Symbol* name, 837 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 838 if (_verifier_constraints == NULL) { 839 _verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<Symbol*>(8, true, mtClass); 840 } 841 if (_verifier_constraint_flags == NULL) { 842 _verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, true, mtClass); 843 } 844 GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints; 845 for (int i=0; i<vc_array->length(); i+= 2) { 846 if (name == vc_array->at(i) && 847 from_name == vc_array->at(i+1)) { 848 return; 849 } 850 } 851 vc_array->append(name); 852 vc_array->append(from_name); 853 854 GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags; 855 char c = 0; 856 c |= from_field_is_protected ? FROM_FIELD_IS_PROTECTED : 0; 857 c |= from_is_array ? FROM_IS_ARRAY : 0; 858 c |= from_is_object ? FROM_IS_OBJECT : 0; 859 vcflags_array->append(c); 860 861 if (log_is_enabled(Trace, cds, verification)) { 862 ResourceMark rm; 863 log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s", 864 instance_klass()->external_name(), from_name->as_klass_external_name(), 865 name->as_klass_external_name()); 866 } 867 } 868 869 int SharedDictionaryEntry::finalize_verification_constraints() { 870 assert(DumpSharedSpaces, "called at dump time only"); 871 Thread* THREAD = Thread::current(); 872 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 873 GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints; 874 GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags; 875 876 if (vc_array != NULL) { 877 if (log_is_enabled(Trace, cds, verification)) { 878 ResourceMark rm; 879 log_trace(cds, verification)("finalize_verification_constraint: %s", 880 literal()->external_name()); 881 } 882 883 // Copy the constraints from C_HEAP-alloced GrowableArrays to Metaspace-alloced 884 // Arrays 885 int size = 0; 886 { 887 // FIXME: change this to be done after relocation, so we can use symbol offset?? 888 int length = vc_array->length(); 889 Array<Symbol*>* out = MetadataFactory::new_array<Symbol*>(loader_data, length, 0, THREAD); 890 assert(out != NULL, "Dump time allocation failure would have aborted VM"); 891 for (int i=0; i<length; i++) { 892 out->at_put(i, vc_array->at(i)); 893 } 894 _verifier_constraints = out; 895 size += out->size() * BytesPerWord; 896 delete vc_array; 897 } 898 { 899 int length = vcflags_array->length(); 900 Array<char>* out = MetadataFactory::new_array<char>(loader_data, length, 0, THREAD); 901 assert(out != NULL, "Dump time allocation failure would have aborted VM"); 902 for (int i=0; i<length; i++) { 903 out->at_put(i, vcflags_array->at(i)); 904 } 905 _verifier_constraint_flags = out; 906 size += out->size() * BytesPerWord; 907 delete vcflags_array; 908 } 909 910 return size; 911 } 912 return 0; 913 } 914 915 void SharedDictionaryEntry::check_verification_constraints(InstanceKlass* klass, TRAPS) { 916 Array<Symbol*>* vc_array = (Array<Symbol*>*)_verifier_constraints; 917 Array<char>* vcflags_array = (Array<char>*)_verifier_constraint_flags; 918 919 if (vc_array != NULL) { 920 int length = vc_array->length(); 921 for (int i=0; i<length; i+=2) { 922 Symbol* name = vc_array->at(i); 923 Symbol* from_name = vc_array->at(i+1); 924 char c = vcflags_array->at(i/2); 925 926 bool from_field_is_protected = (c & FROM_FIELD_IS_PROTECTED) ? true : false; 927 bool from_is_array = (c & FROM_IS_ARRAY) ? true : false; 928 bool from_is_object = (c & FROM_IS_OBJECT) ? true : false; 929 930 bool ok = VerificationType::resolve_and_check_assignability(klass, name, 931 from_name, from_field_is_protected, from_is_array, from_is_object, CHECK); 932 if (!ok) { 933 ResourceMark rm(THREAD); 934 stringStream ss; 935 936 ss.print_cr("Bad type on operand stack"); 937 ss.print_cr("Exception Details:"); 938 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 939 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 940 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 941 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 942 } 943 } 944 } 945 } 946 947 void SharedDictionaryEntry::metaspace_pointers_do(MetaspaceClosure* it) { 948 it->push((Array<Symbol*>**)&_verifier_constraints); 949 it->push((Array<char>**)&_verifier_constraint_flags); 950 } 951 952 bool SharedDictionary::add_non_builtin_klass(const Symbol* class_name, 953 ClassLoaderData* loader_data, 954 InstanceKlass* klass) { 955 956 assert(DumpSharedSpaces, "supported only when dumping"); 957 assert(klass != NULL, "adding NULL klass"); 958 assert(klass->name() == class_name, "sanity check on name"); 959 assert(klass->shared_classpath_index() < 0, 960 "the shared classpath index should not be set for shared class loaded by the custom loaders"); 961 962 // Add an entry for a non-builtin class. 963 // For a shared class for custom class loaders, SystemDictionary::resolve_or_null will 964 // not find this class, because is_builtin() is false. 965 unsigned int hash = compute_hash(class_name); 966 int index = hash_to_index(hash); 967 968 for (SharedDictionaryEntry* entry = bucket(index); 969 entry != NULL; 970 entry = entry->next()) { 971 if (entry->hash() == hash) { 972 Klass* klass = (Klass*)entry->literal(); 973 if (klass->name() == class_name && klass->class_loader_data() == loader_data) { 974 // There is already a class defined with the same name 975 return false; 976 } 977 } 978 } 979 980 assert(Dictionary::entry_size() >= sizeof(SharedDictionaryEntry), "must be big enough"); 981 SharedDictionaryEntry* entry = (SharedDictionaryEntry*)new_entry(hash, klass); 982 add_entry(index, entry); 983 984 assert(entry->is_unregistered(), "sanity"); 985 assert(!entry->is_builtin(), "sanity"); 986 return true; 987 } 988 989 990 //----------------- 991 // SharedDictionary 992 //----------------- 993 994 995 Klass* SharedDictionary::find_class_for_builtin_loader(const Symbol* name) const { 996 SharedDictionaryEntry* entry = get_entry_for_builtin_loader(name); 997 return entry != NULL ? entry->instance_klass() : (Klass*)NULL; 998 } 999 1000 Klass* SharedDictionary::find_class_for_unregistered_loader(const Symbol* name, 1001 int clsfile_size, 1002 int clsfile_crc32) const { 1003 1004 const SharedDictionaryEntry* entry = get_entry_for_unregistered_loader(name, 1005 clsfile_size, 1006 clsfile_crc32); 1007 return entry != NULL ? entry->instance_klass() : (Klass*)NULL; 1008 } 1009 1010 void SharedDictionary::update_entry(Klass* klass, int id) { 1011 assert(DumpSharedSpaces, "supported only when dumping"); 1012 Symbol* class_name = klass->name(); 1013 unsigned int hash = compute_hash(class_name); 1014 int index = hash_to_index(hash); 1015 1016 for (SharedDictionaryEntry* entry = bucket(index); 1017 entry != NULL; 1018 entry = entry->next()) { 1019 if (entry->hash() == hash && entry->literal() == klass) { 1020 entry->_id = id; 1021 return; 1022 } 1023 } 1024 1025 ShouldNotReachHere(); 1026 } 1027 1028 SharedDictionaryEntry* SharedDictionary::get_entry_for_builtin_loader(const Symbol* class_name) const { 1029 assert(!DumpSharedSpaces, "supported only when at runtime"); 1030 unsigned int hash = compute_hash(class_name); 1031 const int index = hash_to_index(hash); 1032 1033 for (SharedDictionaryEntry* entry = bucket(index); 1034 entry != NULL; 1035 entry = entry->next()) { 1036 if (entry->hash() == hash && entry->equals(class_name)) { 1037 if (entry->is_builtin()) { 1038 return entry; 1039 } 1040 } 1041 } 1042 return NULL; 1043 } 1044 1045 SharedDictionaryEntry* SharedDictionary::get_entry_for_unregistered_loader(const Symbol* class_name, 1046 int clsfile_size, 1047 int clsfile_crc32) const { 1048 assert(!DumpSharedSpaces, "supported only when at runtime"); 1049 unsigned int hash = compute_hash(class_name); 1050 int index = hash_to_index(hash); 1051 1052 for (SharedDictionaryEntry* entry = bucket(index); 1053 entry != NULL; 1054 entry = entry->next()) { 1055 if (entry->hash() == hash && entry->equals(class_name)) { 1056 if (entry->is_unregistered()) { 1057 if (clsfile_size == -1) { 1058 // We're called from class_exists_for_unregistered_loader. At run time, we want to 1059 // compute the CRC of a ClassFileStream only if there is an UNREGISTERED class 1060 // with the matching name. 1061 return entry; 1062 } else { 1063 // We're called from find_class_for_unregistered_loader 1064 if (entry->_clsfile_size && clsfile_crc32 == entry->_clsfile_crc32) { 1065 return entry; 1066 } 1067 } 1068 1069 // There can be only 1 class with this name for unregistered loaders. 1070 return NULL; 1071 } 1072 } 1073 } 1074 return NULL; 1075 }