src/share/vm/classfile/systemDictionary.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9550
270570f695e0
parent 9122
024be04bb151
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kbarrett@9099 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/classLoaderData.inline.hpp"
aoqi@0 27 #include "classfile/dictionary.hpp"
aoqi@0 28 #include "classfile/javaClasses.hpp"
aoqi@0 29 #include "classfile/loaderConstraints.hpp"
aoqi@0 30 #include "classfile/placeholders.hpp"
aoqi@0 31 #include "classfile/resolutionErrors.hpp"
aoqi@0 32 #include "classfile/systemDictionary.hpp"
iklam@7089 33 #if INCLUDE_CDS
iklam@7089 34 #include "classfile/sharedClassUtil.hpp"
iklam@7089 35 #include "classfile/systemDictionaryShared.hpp"
iklam@7089 36 #endif
aoqi@0 37 #include "classfile/vmSymbols.hpp"
aoqi@0 38 #include "compiler/compileBroker.hpp"
aoqi@0 39 #include "interpreter/bytecodeStream.hpp"
aoqi@0 40 #include "interpreter/interpreter.hpp"
iklam@7089 41 #include "memory/filemap.hpp"
aoqi@0 42 #include "memory/gcLocker.hpp"
aoqi@0 43 #include "memory/oopFactory.hpp"
aoqi@0 44 #include "oops/instanceKlass.hpp"
aoqi@0 45 #include "oops/instanceRefKlass.hpp"
aoqi@0 46 #include "oops/klass.inline.hpp"
aoqi@0 47 #include "oops/methodData.hpp"
aoqi@0 48 #include "oops/objArrayKlass.hpp"
aoqi@0 49 #include "oops/oop.inline.hpp"
aoqi@0 50 #include "oops/oop.inline2.hpp"
aoqi@0 51 #include "oops/typeArrayKlass.hpp"
aoqi@0 52 #include "prims/jvmtiEnvBase.hpp"
aoqi@0 53 #include "prims/methodHandles.hpp"
iveresov@7172 54 #include "runtime/arguments.hpp"
aoqi@0 55 #include "runtime/biasedLocking.hpp"
aoqi@0 56 #include "runtime/fieldType.hpp"
aoqi@0 57 #include "runtime/handles.inline.hpp"
aoqi@0 58 #include "runtime/java.hpp"
aoqi@0 59 #include "runtime/javaCalls.hpp"
aoqi@0 60 #include "runtime/mutexLocker.hpp"
goetz@6911 61 #include "runtime/orderAccess.inline.hpp"
aoqi@0 62 #include "runtime/signature.hpp"
aoqi@0 63 #include "services/classLoadingService.hpp"
aoqi@0 64 #include "services/threadService.hpp"
aoqi@0 65 #include "utilities/macros.hpp"
aoqi@0 66 #include "utilities/ticks.hpp"
aoqi@0 67 #if INCLUDE_TRACE
mgronlun@7367 68 #include "trace/tracing.hpp"
aoqi@0 69 #endif
aoqi@0 70
aoqi@0 71 Dictionary* SystemDictionary::_dictionary = NULL;
aoqi@0 72 PlaceholderTable* SystemDictionary::_placeholders = NULL;
aoqi@0 73 Dictionary* SystemDictionary::_shared_dictionary = NULL;
aoqi@0 74 LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL;
aoqi@0 75 ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL;
aoqi@0 76 SymbolPropertyTable* SystemDictionary::_invoke_method_table = NULL;
aoqi@0 77
aoqi@0 78
aoqi@0 79 int SystemDictionary::_number_of_modifications = 0;
aoqi@0 80 int SystemDictionary::_sdgeneration = 0;
aoqi@0 81 const int SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
aoqi@0 82 20201,40423,99991};
aoqi@0 83
aoqi@0 84 oop SystemDictionary::_system_loader_lock_obj = NULL;
aoqi@0 85
aoqi@0 86 Klass* SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
aoqi@0 87 = { NULL /*, NULL...*/ };
aoqi@0 88
aoqi@0 89 Klass* SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ };
aoqi@0 90
aoqi@0 91 oop SystemDictionary::_java_system_loader = NULL;
aoqi@0 92
aoqi@0 93 bool SystemDictionary::_has_loadClassInternal = false;
aoqi@0 94 bool SystemDictionary::_has_checkPackageAccess = false;
aoqi@0 95
aoqi@0 96 // lazily initialized klass variables
aoqi@0 97 Klass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
aoqi@0 98
aoqi@0 99
aoqi@0 100 // ----------------------------------------------------------------------------
aoqi@0 101 // Java-level SystemLoader
aoqi@0 102
aoqi@0 103 oop SystemDictionary::java_system_loader() {
aoqi@0 104 return _java_system_loader;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 void SystemDictionary::compute_java_system_loader(TRAPS) {
aoqi@0 108 KlassHandle system_klass(THREAD, WK_KLASS(ClassLoader_klass));
aoqi@0 109 JavaValue result(T_OBJECT);
aoqi@0 110 JavaCalls::call_static(&result,
aoqi@0 111 KlassHandle(THREAD, WK_KLASS(ClassLoader_klass)),
aoqi@0 112 vmSymbols::getSystemClassLoader_name(),
aoqi@0 113 vmSymbols::void_classloader_signature(),
aoqi@0 114 CHECK);
aoqi@0 115
aoqi@0 116 _java_system_loader = (oop)result.get_jobject();
iklam@7089 117
iklam@7089 118 CDS_ONLY(SystemDictionaryShared::initialize(CHECK);)
aoqi@0 119 }
aoqi@0 120
aoqi@0 121
aoqi@0 122 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, TRAPS) {
aoqi@0 123 if (class_loader() == NULL) return ClassLoaderData::the_null_class_loader_data();
phh@9507 124 return ClassLoaderDataGraph::find_or_create(class_loader, THREAD);
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 // ----------------------------------------------------------------------------
aoqi@0 128 // debugging
aoqi@0 129
aoqi@0 130 #ifdef ASSERT
aoqi@0 131
aoqi@0 132 // return true if class_name contains no '.' (internal format is '/')
aoqi@0 133 bool SystemDictionary::is_internal_format(Symbol* class_name) {
aoqi@0 134 if (class_name != NULL) {
aoqi@0 135 ResourceMark rm;
aoqi@0 136 char* name = class_name->as_C_string();
aoqi@0 137 return strchr(name, '.') == NULL;
aoqi@0 138 } else {
aoqi@0 139 return true;
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 #endif
aoqi@0 144
aoqi@0 145 // ----------------------------------------------------------------------------
aoqi@0 146 // Parallel class loading check
aoqi@0 147
aoqi@0 148 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
aoqi@0 149 if (UnsyncloadClass || class_loader.is_null()) return true;
aoqi@0 150 if (AlwaysLockClassLoader) return false;
aoqi@0 151 return java_lang_ClassLoader::parallelCapable(class_loader());
aoqi@0 152 }
aoqi@0 153 // ----------------------------------------------------------------------------
aoqi@0 154 // ParallelDefineClass flag does not apply to bootclass loader
aoqi@0 155 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
aoqi@0 156 if (class_loader.is_null()) return false;
aoqi@0 157 if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
aoqi@0 158 return true;
aoqi@0 159 }
aoqi@0 160 return false;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 /**
aoqi@0 164 * Returns true if the passed class loader is the extension class loader.
aoqi@0 165 */
aoqi@0 166 bool SystemDictionary::is_ext_class_loader(Handle class_loader) {
aoqi@0 167 if (class_loader.is_null()) {
aoqi@0 168 return false;
aoqi@0 169 }
aoqi@0 170 return (class_loader->klass()->name() == vmSymbols::sun_misc_Launcher_ExtClassLoader());
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 // ----------------------------------------------------------------------------
aoqi@0 174 // Resolving of classes
aoqi@0 175
aoqi@0 176 // Forwards to resolve_or_null
aoqi@0 177
aoqi@0 178 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
aoqi@0 179 Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
aoqi@0 180 if (HAS_PENDING_EXCEPTION || klass == NULL) {
aoqi@0 181 KlassHandle k_h(THREAD, klass);
aoqi@0 182 // can return a null klass
aoqi@0 183 klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
aoqi@0 184 }
aoqi@0 185 return klass;
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 Klass* SystemDictionary::handle_resolution_exception(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) {
aoqi@0 189 if (HAS_PENDING_EXCEPTION) {
aoqi@0 190 // If we have a pending exception we forward it to the caller, unless throw_error is true,
aoqi@0 191 // in which case we have to check whether the pending exception is a ClassNotFoundException,
aoqi@0 192 // and if so convert it to a NoClassDefFoundError
aoqi@0 193 // And chain the original ClassNotFoundException
aoqi@0 194 if (throw_error && PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass())) {
aoqi@0 195 ResourceMark rm(THREAD);
aoqi@0 196 assert(klass_h() == NULL, "Should not have result with exception pending");
aoqi@0 197 Handle e(THREAD, PENDING_EXCEPTION);
aoqi@0 198 CLEAR_PENDING_EXCEPTION;
aoqi@0 199 THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string(), e);
aoqi@0 200 } else {
aoqi@0 201 return NULL;
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204 // Class not found, throw appropriate error or exception depending on value of throw_error
aoqi@0 205 if (klass_h() == NULL) {
aoqi@0 206 ResourceMark rm(THREAD);
aoqi@0 207 if (throw_error) {
aoqi@0 208 THROW_MSG_NULL(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string());
aoqi@0 209 } else {
aoqi@0 210 THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
aoqi@0 211 }
aoqi@0 212 }
aoqi@0 213 return (Klass*)klass_h();
aoqi@0 214 }
aoqi@0 215
aoqi@0 216
aoqi@0 217 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name,
aoqi@0 218 bool throw_error, TRAPS)
aoqi@0 219 {
aoqi@0 220 return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD);
aoqi@0 221 }
aoqi@0 222
aoqi@0 223
aoqi@0 224 // Forwards to resolve_instance_class_or_null
aoqi@0 225
aoqi@0 226 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
aoqi@0 227 assert(!THREAD->is_Compiler_thread(),
aoqi@0 228 err_msg("can not load classes with compiler thread: class=%s, classloader=%s",
aoqi@0 229 class_name->as_C_string(),
aoqi@0 230 class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string()));
aoqi@0 231 if (FieldType::is_array(class_name)) {
aoqi@0 232 return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
aoqi@0 233 } else if (FieldType::is_obj(class_name)) {
aoqi@0 234 ResourceMark rm(THREAD);
aoqi@0 235 // Ignore wrapping L and ;.
aoqi@0 236 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
aoqi@0 237 class_name->utf8_length() - 2, CHECK_NULL);
aoqi@0 238 return resolve_instance_class_or_null(name, class_loader, protection_domain, CHECK_NULL);
aoqi@0 239 } else {
aoqi@0 240 return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
aoqi@0 241 }
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, TRAPS) {
aoqi@0 245 return resolve_or_null(class_name, Handle(), Handle(), THREAD);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 // Forwards to resolve_instance_class_or_null
aoqi@0 249
aoqi@0 250 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
aoqi@0 251 Handle class_loader,
aoqi@0 252 Handle protection_domain,
aoqi@0 253 TRAPS) {
aoqi@0 254 assert(FieldType::is_array(class_name), "must be array");
aoqi@0 255 Klass* k = NULL;
aoqi@0 256 FieldArrayInfo fd;
aoqi@0 257 // dimension and object_key in FieldArrayInfo are assigned as a side-effect
aoqi@0 258 // of this call
aoqi@0 259 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_NULL);
aoqi@0 260 if (t == T_OBJECT) {
aoqi@0 261 // naked oop "k" is OK here -- we assign back into it
aoqi@0 262 k = SystemDictionary::resolve_instance_class_or_null(fd.object_key(),
aoqi@0 263 class_loader,
aoqi@0 264 protection_domain,
aoqi@0 265 CHECK_NULL);
aoqi@0 266 if (k != NULL) {
aoqi@0 267 k = k->array_klass(fd.dimension(), CHECK_NULL);
aoqi@0 268 }
aoqi@0 269 } else {
aoqi@0 270 k = Universe::typeArrayKlassObj(t);
aoqi@0 271 k = TypeArrayKlass::cast(k)->array_klass(fd.dimension(), CHECK_NULL);
aoqi@0 272 }
aoqi@0 273 return k;
aoqi@0 274 }
aoqi@0 275
aoqi@0 276
aoqi@0 277 // Must be called for any super-class or super-interface resolution
aoqi@0 278 // during class definition to allow class circularity checking
aoqi@0 279 // super-interface callers:
aoqi@0 280 // parse_interfaces - for defineClass & jvmtiRedefineClasses
aoqi@0 281 // super-class callers:
aoqi@0 282 // ClassFileParser - for defineClass & jvmtiRedefineClasses
aoqi@0 283 // load_shared_class - while loading a class from shared archive
aoqi@0 284 // resolve_instance_class_or_null:
aoqi@0 285 // via: handle_parallel_super_load
aoqi@0 286 // when resolving a class that has an existing placeholder with
aoqi@0 287 // a saved superclass [i.e. a defineClass is currently in progress]
aoqi@0 288 // if another thread is trying to resolve the class, it must do
aoqi@0 289 // super-class checks on its own thread to catch class circularity
aoqi@0 290 // This last call is critical in class circularity checking for cases
aoqi@0 291 // where classloading is delegated to different threads and the
aoqi@0 292 // classloader lock is released.
aoqi@0 293 // Take the case: Base->Super->Base
aoqi@0 294 // 1. If thread T1 tries to do a defineClass of class Base
aoqi@0 295 // resolve_super_or_fail creates placeholder: T1, Base (super Super)
aoqi@0 296 // 2. resolve_instance_class_or_null does not find SD or placeholder for Super
aoqi@0 297 // so it tries to load Super
aoqi@0 298 // 3. If we load the class internally, or user classloader uses same thread
aoqi@0 299 // loadClassFromxxx or defineClass via parseClassFile Super ...
aoqi@0 300 // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base)
aoqi@0 301 // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base
aoqi@0 302 // 3.4 calls resolve_super_or_fail Base
aoqi@0 303 // 3.5 finds T1,Base -> throws class circularity
aoqi@0 304 //OR 4. If T2 tries to resolve Super via defineClass Super ...
aoqi@0 305 // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base)
aoqi@0 306 // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super)
aoqi@0 307 // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2
aoqi@0 308 // 4.4 finds T2, Super -> throws class circularity
aoqi@0 309 // Must be called, even if superclass is null, since this is
aoqi@0 310 // where the placeholder entry is created which claims this
aoqi@0 311 // thread is loading this class/classloader.
aoqi@0 312 Klass* SystemDictionary::resolve_super_or_fail(Symbol* child_name,
aoqi@0 313 Symbol* class_name,
aoqi@0 314 Handle class_loader,
aoqi@0 315 Handle protection_domain,
aoqi@0 316 bool is_superclass,
aoqi@0 317 TRAPS) {
aoqi@0 318 // Double-check, if child class is already loaded, just return super-class,interface
aoqi@0 319 // Don't add a placedholder if already loaded, i.e. already in system dictionary
aoqi@0 320 // Make sure there's a placeholder for the *child* before resolving.
aoqi@0 321 // Used as a claim that this thread is currently loading superclass/classloader
aoqi@0 322 // Used here for ClassCircularity checks and also for heap verification
aoqi@0 323 // (every InstanceKlass in the heap needs to be in the system dictionary
aoqi@0 324 // or have a placeholder).
aoqi@0 325 // Must check ClassCircularity before checking if super class is already loaded
aoqi@0 326 //
aoqi@0 327 // We might not already have a placeholder if this child_name was
aoqi@0 328 // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
aoqi@0 329 // the name of the class might not be known until the stream is actually
aoqi@0 330 // parsed.
aoqi@0 331 // Bugs 4643874, 4715493
aoqi@0 332 // compute_hash can have a safepoint
aoqi@0 333
aoqi@0 334 ClassLoaderData* loader_data = class_loader_data(class_loader);
aoqi@0 335 unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data);
aoqi@0 336 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 337 unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data);
aoqi@0 338 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 339 // can't throw error holding a lock
aoqi@0 340 bool child_already_loaded = false;
aoqi@0 341 bool throw_circularity_error = false;
aoqi@0 342 {
aoqi@0 343 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 344 Klass* childk = find_class(d_index, d_hash, child_name, loader_data);
aoqi@0 345 Klass* quicksuperk;
aoqi@0 346 // to support // loading: if child done loading, just return superclass
aoqi@0 347 // if class_name, & class_loader don't match:
aoqi@0 348 // if initial define, SD update will give LinkageError
aoqi@0 349 // if redefine: compare_class_versions will give HIERARCHY_CHANGED
aoqi@0 350 // so we don't throw an exception here.
aoqi@0 351 // see: nsk redefclass014 & java.lang.instrument Instrument032
aoqi@0 352 if ((childk != NULL ) && (is_superclass) &&
aoqi@0 353 ((quicksuperk = InstanceKlass::cast(childk)->super()) != NULL) &&
aoqi@0 354
aoqi@0 355 ((quicksuperk->name() == class_name) &&
aoqi@0 356 (quicksuperk->class_loader() == class_loader()))) {
aoqi@0 357 return quicksuperk;
aoqi@0 358 } else {
aoqi@0 359 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
aoqi@0 360 if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
aoqi@0 361 throw_circularity_error = true;
aoqi@0 362 }
aoqi@0 363 }
aoqi@0 364 if (!throw_circularity_error) {
aoqi@0 365 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
aoqi@0 366 }
aoqi@0 367 }
aoqi@0 368 if (throw_circularity_error) {
aoqi@0 369 ResourceMark rm(THREAD);
aoqi@0 370 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
aoqi@0 371 }
aoqi@0 372
aoqi@0 373 // java.lang.Object should have been found above
aoqi@0 374 assert(class_name != NULL, "null super class for resolving");
aoqi@0 375 // Resolve the super class or interface, check results on return
aoqi@0 376 Klass* superk = SystemDictionary::resolve_or_null(class_name,
aoqi@0 377 class_loader,
aoqi@0 378 protection_domain,
aoqi@0 379 THREAD);
aoqi@0 380
aoqi@0 381 KlassHandle superk_h(THREAD, superk);
aoqi@0 382
aoqi@0 383 // Clean up of placeholders moved so that each classloadAction registrar self-cleans up
aoqi@0 384 // It is no longer necessary to keep the placeholder table alive until update_dictionary
aoqi@0 385 // or error. GC used to walk the placeholder table as strong roots.
aoqi@0 386 // The instanceKlass is kept alive because the class loader is on the stack,
aoqi@0 387 // which keeps the loader_data alive, as well as all instanceKlasses in
aoqi@0 388 // the loader_data. parseClassFile adds the instanceKlass to loader_data.
aoqi@0 389 {
aoqi@0 390 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 391 placeholders()->find_and_remove(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, THREAD);
aoqi@0 392 SystemDictionary_lock->notify_all();
aoqi@0 393 }
aoqi@0 394 if (HAS_PENDING_EXCEPTION || superk_h() == NULL) {
aoqi@0 395 // can null superk
aoqi@0 396 superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD));
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 return superk_h();
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 void SystemDictionary::validate_protection_domain(instanceKlassHandle klass,
aoqi@0 403 Handle class_loader,
aoqi@0 404 Handle protection_domain,
aoqi@0 405 TRAPS) {
aoqi@0 406 if(!has_checkPackageAccess()) return;
aoqi@0 407
aoqi@0 408 // Now we have to call back to java to check if the initating class has access
aoqi@0 409 JavaValue result(T_VOID);
aoqi@0 410 if (TraceProtectionDomainVerification) {
aoqi@0 411 // Print out trace information
aoqi@0 412 tty->print_cr("Checking package access");
aoqi@0 413 tty->print(" - class loader: "); class_loader()->print_value_on(tty); tty->cr();
aoqi@0 414 tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr();
aoqi@0 415 tty->print(" - loading: "); klass()->print_value_on(tty); tty->cr();
aoqi@0 416 }
aoqi@0 417
aoqi@0 418 KlassHandle system_loader(THREAD, SystemDictionary::ClassLoader_klass());
aoqi@0 419 JavaCalls::call_special(&result,
aoqi@0 420 class_loader,
aoqi@0 421 system_loader,
aoqi@0 422 vmSymbols::checkPackageAccess_name(),
aoqi@0 423 vmSymbols::class_protectiondomain_signature(),
aoqi@0 424 Handle(THREAD, klass->java_mirror()),
aoqi@0 425 protection_domain,
aoqi@0 426 THREAD);
aoqi@0 427
aoqi@0 428 if (TraceProtectionDomainVerification) {
aoqi@0 429 if (HAS_PENDING_EXCEPTION) {
aoqi@0 430 tty->print_cr(" -> DENIED !!!!!!!!!!!!!!!!!!!!!");
aoqi@0 431 } else {
aoqi@0 432 tty->print_cr(" -> granted");
aoqi@0 433 }
aoqi@0 434 tty->cr();
aoqi@0 435 }
aoqi@0 436
aoqi@0 437 if (HAS_PENDING_EXCEPTION) return;
aoqi@0 438
aoqi@0 439 // If no exception has been thrown, we have validated the protection domain
aoqi@0 440 // Insert the protection domain of the initiating class into the set.
aoqi@0 441 {
aoqi@0 442 // We recalculate the entry here -- we've called out to java since
aoqi@0 443 // the last time it was calculated.
aoqi@0 444 ClassLoaderData* loader_data = class_loader_data(class_loader);
aoqi@0 445
aoqi@0 446 Symbol* kn = klass->name();
aoqi@0 447 unsigned int d_hash = dictionary()->compute_hash(kn, loader_data);
aoqi@0 448 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 449
aoqi@0 450 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 451 {
aoqi@0 452 // Note that we have an entry, and entries can be deleted only during GC,
aoqi@0 453 // so we cannot allow GC to occur while we're holding this entry.
aoqi@0 454
aoqi@0 455 // We're using a No_Safepoint_Verifier to catch any place where we
aoqi@0 456 // might potentially do a GC at all.
aoqi@0 457 // Dictionary::do_unloading() asserts that classes in SD are only
aoqi@0 458 // unloaded at a safepoint. Anonymous classes are not in SD.
aoqi@0 459 No_Safepoint_Verifier nosafepoint;
aoqi@0 460 dictionary()->add_protection_domain(d_index, d_hash, klass, loader_data,
aoqi@0 461 protection_domain, THREAD);
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464 }
aoqi@0 465
aoqi@0 466 // We only get here if this thread finds that another thread
aoqi@0 467 // has already claimed the placeholder token for the current operation,
aoqi@0 468 // but that other thread either never owned or gave up the
aoqi@0 469 // object lock
aoqi@0 470 // Waits on SystemDictionary_lock to indicate placeholder table updated
aoqi@0 471 // On return, caller must recheck placeholder table state
aoqi@0 472 //
aoqi@0 473 // We only get here if
aoqi@0 474 // 1) custom classLoader, i.e. not bootstrap classloader
aoqi@0 475 // 2) UnsyncloadClass not set
aoqi@0 476 // 3) custom classLoader has broken the class loader objectLock
aoqi@0 477 // so another thread got here in parallel
aoqi@0 478 //
aoqi@0 479 // lockObject must be held.
aoqi@0 480 // Complicated dance due to lock ordering:
aoqi@0 481 // Must first release the classloader object lock to
aoqi@0 482 // allow initial definer to complete the class definition
aoqi@0 483 // and to avoid deadlock
aoqi@0 484 // Reclaim classloader lock object with same original recursion count
aoqi@0 485 // Must release SystemDictionary_lock after notify, since
aoqi@0 486 // class loader lock must be claimed before SystemDictionary_lock
aoqi@0 487 // to prevent deadlocks
aoqi@0 488 //
aoqi@0 489 // The notify allows applications that did an untimed wait() on
aoqi@0 490 // the classloader object lock to not hang.
aoqi@0 491 void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) {
aoqi@0 492 assert_lock_strong(SystemDictionary_lock);
aoqi@0 493
aoqi@0 494 bool calledholdinglock
aoqi@0 495 = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject);
aoqi@0 496 assert(calledholdinglock,"must hold lock for notify");
aoqi@0 497 assert((!(lockObject() == _system_loader_lock_obj) && !is_parallelCapable(lockObject)), "unexpected double_lock_wait");
aoqi@0 498 ObjectSynchronizer::notifyall(lockObject, THREAD);
aoqi@0 499 intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD);
aoqi@0 500 SystemDictionary_lock->wait();
aoqi@0 501 SystemDictionary_lock->unlock();
aoqi@0 502 ObjectSynchronizer::reenter(lockObject, recursions, THREAD);
aoqi@0 503 SystemDictionary_lock->lock();
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 // If the class in is in the placeholder table, class loading is in progress
aoqi@0 507 // For cases where the application changes threads to load classes, it
aoqi@0 508 // is critical to ClassCircularity detection that we try loading
aoqi@0 509 // the superclass on the same thread internally, so we do parallel
aoqi@0 510 // super class loading here.
aoqi@0 511 // This also is critical in cases where the original thread gets stalled
aoqi@0 512 // even in non-circularity situations.
aoqi@0 513 // Note: must call resolve_super_or_fail even if null super -
aoqi@0 514 // to force placeholder entry creation for this class for circularity detection
aoqi@0 515 // Caller must check for pending exception
aoqi@0 516 // Returns non-null Klass* if other thread has completed load
aoqi@0 517 // and we are done,
aoqi@0 518 // If return null Klass* and no pending exception, the caller must load the class
aoqi@0 519 instanceKlassHandle SystemDictionary::handle_parallel_super_load(
aoqi@0 520 Symbol* name, Symbol* superclassname, Handle class_loader,
aoqi@0 521 Handle protection_domain, Handle lockObject, TRAPS) {
aoqi@0 522
aoqi@0 523 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
aoqi@0 524 ClassLoaderData* loader_data = class_loader_data(class_loader);
aoqi@0 525 unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
aoqi@0 526 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 527 unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
aoqi@0 528 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 529
aoqi@0 530 // superk is not used, resolve_super called for circularity check only
aoqi@0 531 // This code is reached in two situations. One if this thread
aoqi@0 532 // is loading the same class twice (e.g. ClassCircularity, or
aoqi@0 533 // java.lang.instrument).
aoqi@0 534 // The second is if another thread started the resolve_super first
aoqi@0 535 // and has not yet finished.
aoqi@0 536 // In both cases the original caller will clean up the placeholder
aoqi@0 537 // entry on error.
aoqi@0 538 Klass* superk = SystemDictionary::resolve_super_or_fail(name,
aoqi@0 539 superclassname,
aoqi@0 540 class_loader,
aoqi@0 541 protection_domain,
aoqi@0 542 true,
aoqi@0 543 CHECK_(nh));
aoqi@0 544
aoqi@0 545 // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
aoqi@0 546 // Serial class loaders and bootstrap classloader do wait for superclass loads
aoqi@0 547 if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
aoqi@0 548 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 549 // Check if classloading completed while we were loading superclass or waiting
aoqi@0 550 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 551 if (check != NULL) {
aoqi@0 552 // Klass is already loaded, so just return it
aoqi@0 553 return(instanceKlassHandle(THREAD, check));
aoqi@0 554 } else {
aoqi@0 555 return nh;
aoqi@0 556 }
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 // must loop to both handle other placeholder updates
aoqi@0 560 // and spurious notifications
aoqi@0 561 bool super_load_in_progress = true;
aoqi@0 562 PlaceholderEntry* placeholder;
aoqi@0 563 while (super_load_in_progress) {
aoqi@0 564 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 565 // Check if classloading completed while we were loading superclass or waiting
aoqi@0 566 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 567 if (check != NULL) {
aoqi@0 568 // Klass is already loaded, so just return it
aoqi@0 569 return(instanceKlassHandle(THREAD, check));
aoqi@0 570 } else {
aoqi@0 571 placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
aoqi@0 572 if (placeholder && placeholder->super_load_in_progress() ){
aoqi@0 573 // Before UnsyncloadClass:
aoqi@0 574 // We only get here if the application has released the
aoqi@0 575 // classloader lock when another thread was in the middle of loading a
aoqi@0 576 // superclass/superinterface for this class, and now
aoqi@0 577 // this thread is also trying to load this class.
aoqi@0 578 // To minimize surprises, the first thread that started to
aoqi@0 579 // load a class should be the one to complete the loading
aoqi@0 580 // with the classfile it initially expected.
aoqi@0 581 // This logic has the current thread wait once it has done
aoqi@0 582 // all the superclass/superinterface loading it can, until
aoqi@0 583 // the original thread completes the class loading or fails
aoqi@0 584 // If it completes we will use the resulting InstanceKlass
aoqi@0 585 // which we will find below in the systemDictionary.
aoqi@0 586 // We also get here for parallel bootstrap classloader
aoqi@0 587 if (class_loader.is_null()) {
aoqi@0 588 SystemDictionary_lock->wait();
aoqi@0 589 } else {
aoqi@0 590 double_lock_wait(lockObject, THREAD);
aoqi@0 591 }
aoqi@0 592 } else {
aoqi@0 593 // If not in SD and not in PH, other thread's load must have failed
aoqi@0 594 super_load_in_progress = false;
aoqi@0 595 }
aoqi@0 596 }
aoqi@0 597 }
aoqi@0 598 return (nh);
aoqi@0 599 }
aoqi@0 600
aoqi@0 601
aoqi@0 602 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
aoqi@0 603 Handle class_loader,
aoqi@0 604 Handle protection_domain,
aoqi@0 605 TRAPS) {
aoqi@0 606 assert(name != NULL && !FieldType::is_array(name) &&
aoqi@0 607 !FieldType::is_obj(name), "invalid class name");
aoqi@0 608
aoqi@0 609 Ticks class_load_start_time = Ticks::now();
aoqi@0 610
aoqi@0 611 // UseNewReflection
aoqi@0 612 // Fix for 4474172; see evaluation for more details
aoqi@0 613 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
aoqi@0 614 ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
aoqi@0 615
aoqi@0 616 // Do lookup to see if class already exist and the protection domain
aoqi@0 617 // has the right access
aoqi@0 618 // This call uses find which checks protection domain already matches
aoqi@0 619 // All subsequent calls use find_class, and set has_loaded_class so that
aoqi@0 620 // before we return a result we call out to java to check for valid protection domain
aoqi@0 621 // to allow returning the Klass* and add it to the pd_set if it is valid
aoqi@0 622 unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
aoqi@0 623 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 624 Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
aoqi@0 625 protection_domain, THREAD);
aoqi@0 626 if (probe != NULL) return probe;
aoqi@0 627
aoqi@0 628
aoqi@0 629 // Non-bootstrap class loaders will call out to class loader and
aoqi@0 630 // define via jvm/jni_DefineClass which will acquire the
aoqi@0 631 // class loader object lock to protect against multiple threads
aoqi@0 632 // defining the class in parallel by accident.
aoqi@0 633 // This lock must be acquired here so the waiter will find
aoqi@0 634 // any successful result in the SystemDictionary and not attempt
aoqi@0 635 // the define
aoqi@0 636 // ParallelCapable Classloaders and the bootstrap classloader,
aoqi@0 637 // or all classloaders with UnsyncloadClass do not acquire lock here
aoqi@0 638 bool DoObjectLock = true;
aoqi@0 639 if (is_parallelCapable(class_loader)) {
aoqi@0 640 DoObjectLock = false;
aoqi@0 641 }
aoqi@0 642
aoqi@0 643 unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
aoqi@0 644 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 645
aoqi@0 646 // Class is not in SystemDictionary so we have to do loading.
aoqi@0 647 // Make sure we are synchronized on the class loader before we proceed
aoqi@0 648 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
aoqi@0 649 check_loader_lock_contention(lockObject, THREAD);
aoqi@0 650 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
aoqi@0 651
aoqi@0 652 // Check again (after locking) if class already exist in SystemDictionary
aoqi@0 653 bool class_has_been_loaded = false;
aoqi@0 654 bool super_load_in_progress = false;
aoqi@0 655 bool havesupername = false;
aoqi@0 656 instanceKlassHandle k;
aoqi@0 657 PlaceholderEntry* placeholder;
aoqi@0 658 Symbol* superclassname = NULL;
aoqi@0 659
aoqi@0 660 {
aoqi@0 661 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 662 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 663 if (check != NULL) {
aoqi@0 664 // Klass is already loaded, so just return it
aoqi@0 665 class_has_been_loaded = true;
aoqi@0 666 k = instanceKlassHandle(THREAD, check);
aoqi@0 667 } else {
aoqi@0 668 placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
aoqi@0 669 if (placeholder && placeholder->super_load_in_progress()) {
aoqi@0 670 super_load_in_progress = true;
aoqi@0 671 if (placeholder->havesupername() == true) {
aoqi@0 672 superclassname = placeholder->supername();
aoqi@0 673 havesupername = true;
aoqi@0 674 }
aoqi@0 675 }
aoqi@0 676 }
aoqi@0 677 }
aoqi@0 678
aoqi@0 679 // If the class is in the placeholder table, class loading is in progress
aoqi@0 680 if (super_load_in_progress && havesupername==true) {
aoqi@0 681 k = SystemDictionary::handle_parallel_super_load(name, superclassname,
aoqi@0 682 class_loader, protection_domain, lockObject, THREAD);
aoqi@0 683 if (HAS_PENDING_EXCEPTION) {
aoqi@0 684 return NULL;
aoqi@0 685 }
aoqi@0 686 if (!k.is_null()) {
aoqi@0 687 class_has_been_loaded = true;
aoqi@0 688 }
aoqi@0 689 }
aoqi@0 690
aoqi@0 691 bool throw_circularity_error = false;
aoqi@0 692 if (!class_has_been_loaded) {
aoqi@0 693 bool load_instance_added = false;
aoqi@0 694
aoqi@0 695 // add placeholder entry to record loading instance class
aoqi@0 696 // Five cases:
aoqi@0 697 // All cases need to prevent modifying bootclasssearchpath
aoqi@0 698 // in parallel with a classload of same classname
aoqi@0 699 // Redefineclasses uses existence of the placeholder for the duration
aoqi@0 700 // of the class load to prevent concurrent redefinition of not completely
aoqi@0 701 // defined classes.
aoqi@0 702 // case 1. traditional classloaders that rely on the classloader object lock
aoqi@0 703 // - no other need for LOAD_INSTANCE
aoqi@0 704 // case 2. traditional classloaders that break the classloader object lock
aoqi@0 705 // as a deadlock workaround. Detection of this case requires that
aoqi@0 706 // this check is done while holding the classloader object lock,
aoqi@0 707 // and that lock is still held when calling classloader's loadClass.
aoqi@0 708 // For these classloaders, we ensure that the first requestor
aoqi@0 709 // completes the load and other requestors wait for completion.
aoqi@0 710 // case 3. UnsyncloadClass - don't use objectLocker
aoqi@0 711 // With this flag, we allow parallel classloading of a
aoqi@0 712 // class/classloader pair
aoqi@0 713 // case4. Bootstrap classloader - don't own objectLocker
aoqi@0 714 // This classloader supports parallelism at the classloader level,
aoqi@0 715 // but only allows a single load of a class/classloader pair.
aoqi@0 716 // No performance benefit and no deadlock issues.
aoqi@0 717 // case 5. parallelCapable user level classloaders - without objectLocker
aoqi@0 718 // Allow parallel classloading of a class/classloader pair
aoqi@0 719
aoqi@0 720 {
aoqi@0 721 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 722 if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
aoqi@0 723 PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
aoqi@0 724 if (oldprobe) {
aoqi@0 725 // only need check_seen_thread once, not on each loop
aoqi@0 726 // 6341374 java/lang/Instrument with -Xcomp
aoqi@0 727 if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
aoqi@0 728 throw_circularity_error = true;
aoqi@0 729 } else {
aoqi@0 730 // case 1: traditional: should never see load_in_progress.
aoqi@0 731 while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
aoqi@0 732
aoqi@0 733 // case 4: bootstrap classloader: prevent futile classloading,
aoqi@0 734 // wait on first requestor
aoqi@0 735 if (class_loader.is_null()) {
aoqi@0 736 SystemDictionary_lock->wait();
aoqi@0 737 } else {
aoqi@0 738 // case 2: traditional with broken classloader lock. wait on first
aoqi@0 739 // requestor.
aoqi@0 740 double_lock_wait(lockObject, THREAD);
aoqi@0 741 }
aoqi@0 742 // Check if classloading completed while we were waiting
aoqi@0 743 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 744 if (check != NULL) {
aoqi@0 745 // Klass is already loaded, so just return it
aoqi@0 746 k = instanceKlassHandle(THREAD, check);
aoqi@0 747 class_has_been_loaded = true;
aoqi@0 748 }
aoqi@0 749 // check if other thread failed to load and cleaned up
aoqi@0 750 oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
aoqi@0 751 }
aoqi@0 752 }
aoqi@0 753 }
aoqi@0 754 }
aoqi@0 755 // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
aoqi@0 756 // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
aoqi@0 757 // LOAD_INSTANCE in parallel
aoqi@0 758
aoqi@0 759 if (!throw_circularity_error && !class_has_been_loaded) {
aoqi@0 760 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
aoqi@0 761 load_instance_added = true;
aoqi@0 762 // For class loaders that do not acquire the classloader object lock,
aoqi@0 763 // if they did not catch another thread holding LOAD_INSTANCE,
aoqi@0 764 // need a check analogous to the acquire ObjectLocker/find_class
aoqi@0 765 // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
aoqi@0 766 // one final check if the load has already completed
aoqi@0 767 // class loaders holding the ObjectLock shouldn't find the class here
aoqi@0 768 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 769 if (check != NULL) {
aoqi@0 770 // Klass is already loaded, so return it after checking/adding protection domain
aoqi@0 771 k = instanceKlassHandle(THREAD, check);
aoqi@0 772 class_has_been_loaded = true;
aoqi@0 773 }
aoqi@0 774 }
aoqi@0 775 }
aoqi@0 776
aoqi@0 777 // must throw error outside of owning lock
aoqi@0 778 if (throw_circularity_error) {
aoqi@0 779 assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup");
aoqi@0 780 ResourceMark rm(THREAD);
aoqi@0 781 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
aoqi@0 782 }
aoqi@0 783
aoqi@0 784 if (!class_has_been_loaded) {
aoqi@0 785
aoqi@0 786 // Do actual loading
aoqi@0 787 k = load_instance_class(name, class_loader, THREAD);
aoqi@0 788
aoqi@0 789 // For UnsyncloadClass only
aoqi@0 790 // If they got a linkageError, check if a parallel class load succeeded.
aoqi@0 791 // If it did, then for bytecode resolution the specification requires
aoqi@0 792 // that we return the same result we did for the other thread, i.e. the
aoqi@0 793 // successfully loaded InstanceKlass
aoqi@0 794 // Should not get here for classloaders that support parallelism
aoqi@0 795 // with the new cleaner mechanism, even with AllowParallelDefineClass
aoqi@0 796 // Bootstrap goes through here to allow for an extra guarantee check
aoqi@0 797 if (UnsyncloadClass || (class_loader.is_null())) {
aoqi@0 798 if (k.is_null() && HAS_PENDING_EXCEPTION
aoqi@0 799 && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
aoqi@0 800 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 801 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 802 if (check != NULL) {
aoqi@0 803 // Klass is already loaded, so just use it
aoqi@0 804 k = instanceKlassHandle(THREAD, check);
aoqi@0 805 CLEAR_PENDING_EXCEPTION;
aoqi@0 806 guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
aoqi@0 807 }
aoqi@0 808 }
aoqi@0 809 }
aoqi@0 810
aoqi@0 811 // If everything was OK (no exceptions, no null return value), and
aoqi@0 812 // class_loader is NOT the defining loader, do a little more bookkeeping.
aoqi@0 813 if (!HAS_PENDING_EXCEPTION && !k.is_null() &&
aoqi@0 814 k->class_loader() != class_loader()) {
aoqi@0 815
aoqi@0 816 check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
aoqi@0 817
aoqi@0 818 // Need to check for a PENDING_EXCEPTION again; check_constraints
coleenp@9530 819 // can throw but we may have to remove entry from the placeholder table below.
coleenp@9530 820 if (!HAS_PENDING_EXCEPTION) {
coleenp@9530 821 // Record dependency for non-parent delegation.
coleenp@9530 822 // This recording keeps the defining class loader of the klass (k) found
coleenp@9530 823 // from being unloaded while the initiating class loader is loaded
coleenp@9530 824 // even if the reference to the defining class loader is dropped
coleenp@9530 825 // before references to the initiating class loader.
coleenp@9530 826 loader_data->record_dependency(k(), THREAD);
coleenp@9530 827 }
coleenp@9530 828
aoqi@0 829 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 830 { // Grabbing the Compile_lock prevents systemDictionary updates
aoqi@0 831 // during compilations.
aoqi@0 832 MutexLocker mu(Compile_lock, THREAD);
aoqi@0 833 update_dictionary(d_index, d_hash, p_index, p_hash,
aoqi@0 834 k, class_loader, THREAD);
aoqi@0 835 }
aoqi@0 836
aoqi@0 837 if (JvmtiExport::should_post_class_load()) {
aoqi@0 838 Thread *thread = THREAD;
aoqi@0 839 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
aoqi@0 840 JvmtiExport::post_class_load((JavaThread *) thread, k());
aoqi@0 841 }
aoqi@0 842 }
aoqi@0 843 }
aoqi@0 844 } // load_instance_class loop
aoqi@0 845
aoqi@0 846 if (load_instance_added == true) {
aoqi@0 847 // clean up placeholder entries for LOAD_INSTANCE success or error
aoqi@0 848 // This brackets the SystemDictionary updates for both defining
aoqi@0 849 // and initiating loaders
aoqi@0 850 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 851 placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
aoqi@0 852 SystemDictionary_lock->notify_all();
aoqi@0 853 }
aoqi@0 854 }
aoqi@0 855
aoqi@0 856 if (HAS_PENDING_EXCEPTION || k.is_null()) {
aoqi@0 857 return NULL;
aoqi@0 858 }
aoqi@0 859
aoqi@0 860 post_class_load_event(class_load_start_time, k, class_loader);
aoqi@0 861
aoqi@0 862 #ifdef ASSERT
aoqi@0 863 {
aoqi@0 864 ClassLoaderData* loader_data = k->class_loader_data();
aoqi@0 865 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 866 Klass* kk = find_class(name, loader_data);
aoqi@0 867 assert(kk == k(), "should be present in dictionary");
aoqi@0 868 }
aoqi@0 869 #endif
aoqi@0 870
aoqi@0 871 // return if the protection domain in NULL
aoqi@0 872 if (protection_domain() == NULL) return k();
aoqi@0 873
aoqi@0 874 // Check the protection domain has the right access
aoqi@0 875 {
aoqi@0 876 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 877 // Note that we have an entry, and entries can be deleted only during GC,
aoqi@0 878 // so we cannot allow GC to occur while we're holding this entry.
aoqi@0 879 // We're using a No_Safepoint_Verifier to catch any place where we
aoqi@0 880 // might potentially do a GC at all.
aoqi@0 881 // Dictionary::do_unloading() asserts that classes in SD are only
aoqi@0 882 // unloaded at a safepoint. Anonymous classes are not in SD.
aoqi@0 883 No_Safepoint_Verifier nosafepoint;
aoqi@0 884 if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
aoqi@0 885 loader_data,
aoqi@0 886 protection_domain)) {
aoqi@0 887 return k();
aoqi@0 888 }
aoqi@0 889 }
aoqi@0 890
aoqi@0 891 // Verify protection domain. If it fails an exception is thrown
aoqi@0 892 validate_protection_domain(k, class_loader, protection_domain, CHECK_NULL);
aoqi@0 893
aoqi@0 894 return k();
aoqi@0 895 }
aoqi@0 896
aoqi@0 897
aoqi@0 898 // This routine does not lock the system dictionary.
aoqi@0 899 //
aoqi@0 900 // Since readers don't hold a lock, we must make sure that system
aoqi@0 901 // dictionary entries are only removed at a safepoint (when only one
aoqi@0 902 // thread is running), and are added to in a safe way (all links must
aoqi@0 903 // be updated in an MT-safe manner).
aoqi@0 904 //
aoqi@0 905 // Callers should be aware that an entry could be added just after
aoqi@0 906 // _dictionary->bucket(index) is read here, so the caller will not see
aoqi@0 907 // the new entry.
aoqi@0 908
aoqi@0 909 Klass* SystemDictionary::find(Symbol* class_name,
aoqi@0 910 Handle class_loader,
aoqi@0 911 Handle protection_domain,
aoqi@0 912 TRAPS) {
aoqi@0 913
aoqi@0 914 // UseNewReflection
aoqi@0 915 // The result of this call should be consistent with the result
aoqi@0 916 // of the call to resolve_instance_class_or_null().
aoqi@0 917 // See evaluation 6790209 and 4474172 for more details.
aoqi@0 918 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
aoqi@0 919 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(class_loader());
aoqi@0 920
aoqi@0 921 if (loader_data == NULL) {
aoqi@0 922 // If the ClassLoaderData has not been setup,
aoqi@0 923 // then the class loader has no entries in the dictionary.
aoqi@0 924 return NULL;
aoqi@0 925 }
aoqi@0 926
aoqi@0 927 unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
aoqi@0 928 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 929
aoqi@0 930 {
aoqi@0 931 // Note that we have an entry, and entries can be deleted only during GC,
aoqi@0 932 // so we cannot allow GC to occur while we're holding this entry.
aoqi@0 933 // We're using a No_Safepoint_Verifier to catch any place where we
aoqi@0 934 // might potentially do a GC at all.
aoqi@0 935 // Dictionary::do_unloading() asserts that classes in SD are only
aoqi@0 936 // unloaded at a safepoint. Anonymous classes are not in SD.
aoqi@0 937 No_Safepoint_Verifier nosafepoint;
aoqi@0 938 return dictionary()->find(d_index, d_hash, class_name, loader_data,
aoqi@0 939 protection_domain, THREAD);
aoqi@0 940 }
aoqi@0 941 }
aoqi@0 942
aoqi@0 943
aoqi@0 944 // Look for a loaded instance or array klass by name. Do not do any loading.
aoqi@0 945 // return NULL in case of error.
aoqi@0 946 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name,
aoqi@0 947 Handle class_loader,
aoqi@0 948 Handle protection_domain,
aoqi@0 949 TRAPS) {
aoqi@0 950 Klass* k = NULL;
aoqi@0 951 assert(class_name != NULL, "class name must be non NULL");
aoqi@0 952
aoqi@0 953 if (FieldType::is_array(class_name)) {
aoqi@0 954 // The name refers to an array. Parse the name.
aoqi@0 955 // dimension and object_key in FieldArrayInfo are assigned as a
aoqi@0 956 // side-effect of this call
aoqi@0 957 FieldArrayInfo fd;
aoqi@0 958 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
aoqi@0 959 if (t != T_OBJECT) {
aoqi@0 960 k = Universe::typeArrayKlassObj(t);
aoqi@0 961 } else {
aoqi@0 962 k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD);
aoqi@0 963 }
aoqi@0 964 if (k != NULL) {
aoqi@0 965 k = k->array_klass_or_null(fd.dimension());
aoqi@0 966 }
aoqi@0 967 } else {
aoqi@0 968 k = find(class_name, class_loader, protection_domain, THREAD);
aoqi@0 969 }
aoqi@0 970 return k;
aoqi@0 971 }
aoqi@0 972
aoqi@0 973 // Note: this method is much like resolve_from_stream, but
aoqi@0 974 // updates no supplemental data structures.
aoqi@0 975 // TODO consolidate the two methods with a helper routine?
aoqi@0 976 Klass* SystemDictionary::parse_stream(Symbol* class_name,
aoqi@0 977 Handle class_loader,
aoqi@0 978 Handle protection_domain,
aoqi@0 979 ClassFileStream* st,
aoqi@0 980 KlassHandle host_klass,
aoqi@0 981 GrowableArray<Handle>* cp_patches,
aoqi@0 982 TRAPS) {
aoqi@0 983 TempNewSymbol parsed_name = NULL;
aoqi@0 984
aoqi@0 985 Ticks class_load_start_time = Ticks::now();
aoqi@0 986
aoqi@0 987 ClassLoaderData* loader_data;
aoqi@0 988 if (host_klass.not_null()) {
aoqi@0 989 // Create a new CLD for anonymous class, that uses the same class loader
aoqi@0 990 // as the host_klass
aoqi@0 991 assert(EnableInvokeDynamic, "");
aoqi@0 992 guarantee(host_klass->class_loader() == class_loader(), "should be the same");
iklam@7089 993 guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
aoqi@0 994 loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
aoqi@0 995 loader_data->record_dependency(host_klass(), CHECK_NULL);
aoqi@0 996 } else {
aoqi@0 997 loader_data = ClassLoaderData::class_loader_data(class_loader());
aoqi@0 998 }
aoqi@0 999
aoqi@0 1000 // Parse the stream. Note that we do this even though this klass might
aoqi@0 1001 // already be present in the SystemDictionary, otherwise we would not
aoqi@0 1002 // throw potential ClassFormatErrors.
aoqi@0 1003 //
aoqi@0 1004 // Note: "name" is updated.
aoqi@0 1005
aoqi@0 1006 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
aoqi@0 1007 loader_data,
aoqi@0 1008 protection_domain,
aoqi@0 1009 host_klass,
aoqi@0 1010 cp_patches,
aoqi@0 1011 parsed_name,
aoqi@0 1012 true,
aoqi@0 1013 THREAD);
aoqi@0 1014
aoqi@0 1015
aoqi@0 1016 if (host_klass.not_null() && k.not_null()) {
aoqi@0 1017 assert(EnableInvokeDynamic, "");
aoqi@0 1018 // If it's anonymous, initialize it now, since nobody else will.
aoqi@0 1019
aoqi@0 1020 {
aoqi@0 1021 MutexLocker mu_r(Compile_lock, THREAD);
aoqi@0 1022
aoqi@0 1023 // Add to class hierarchy, initialize vtables, and do possible
aoqi@0 1024 // deoptimizations.
aoqi@0 1025 add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
aoqi@0 1026
aoqi@0 1027 // But, do not add to system dictionary.
aoqi@0 1028
aoqi@0 1029 // compiled code dependencies need to be validated anyway
aoqi@0 1030 notice_modification();
aoqi@0 1031 }
aoqi@0 1032
aoqi@0 1033 // Rewrite and patch constant pool here.
aoqi@0 1034 k->link_class(CHECK_NULL);
aoqi@0 1035 if (cp_patches != NULL) {
aoqi@0 1036 k->constants()->patch_resolved_references(cp_patches);
aoqi@0 1037 }
aoqi@0 1038 k->eager_initialize(CHECK_NULL);
aoqi@0 1039
aoqi@0 1040 // notify jvmti
aoqi@0 1041 if (JvmtiExport::should_post_class_load()) {
aoqi@0 1042 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
aoqi@0 1043 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
aoqi@0 1044 }
aoqi@0 1045
aoqi@0 1046 post_class_load_event(class_load_start_time, k, class_loader);
aoqi@0 1047 }
aoqi@0 1048 assert(host_klass.not_null() || cp_patches == NULL,
aoqi@0 1049 "cp_patches only found with host_klass");
aoqi@0 1050
aoqi@0 1051 return k();
aoqi@0 1052 }
aoqi@0 1053
aoqi@0 1054 // Add a klass to the system from a stream (called by jni_DefineClass and
aoqi@0 1055 // JVM_DefineClass).
aoqi@0 1056 // Note: class_name can be NULL. In that case we do not know the name of
aoqi@0 1057 // the class until we have parsed the stream.
aoqi@0 1058
aoqi@0 1059 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
aoqi@0 1060 Handle class_loader,
aoqi@0 1061 Handle protection_domain,
aoqi@0 1062 ClassFileStream* st,
aoqi@0 1063 bool verify,
aoqi@0 1064 TRAPS) {
aoqi@0 1065
aoqi@0 1066 // Classloaders that support parallelism, e.g. bootstrap classloader,
aoqi@0 1067 // or all classloaders with UnsyncloadClass do not acquire lock here
aoqi@0 1068 bool DoObjectLock = true;
aoqi@0 1069 if (is_parallelCapable(class_loader)) {
aoqi@0 1070 DoObjectLock = false;
aoqi@0 1071 }
aoqi@0 1072
aoqi@0 1073 ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
aoqi@0 1074
aoqi@0 1075 // Make sure we are synchronized on the class loader before we proceed
aoqi@0 1076 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
aoqi@0 1077 check_loader_lock_contention(lockObject, THREAD);
aoqi@0 1078 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
aoqi@0 1079
aoqi@0 1080 TempNewSymbol parsed_name = NULL;
aoqi@0 1081
aoqi@0 1082 // Parse the stream. Note that we do this even though this klass might
aoqi@0 1083 // already be present in the SystemDictionary, otherwise we would not
aoqi@0 1084 // throw potential ClassFormatErrors.
aoqi@0 1085 //
aoqi@0 1086 // Note: "name" is updated.
aoqi@0 1087
aoqi@0 1088 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
aoqi@0 1089 loader_data,
aoqi@0 1090 protection_domain,
aoqi@0 1091 parsed_name,
aoqi@0 1092 verify,
aoqi@0 1093 THREAD);
aoqi@0 1094
aoqi@0 1095 const char* pkg = "java/";
shshahma@8621 1096 size_t pkglen = strlen(pkg);
aoqi@0 1097 if (!HAS_PENDING_EXCEPTION &&
aoqi@0 1098 !class_loader.is_null() &&
aoqi@0 1099 parsed_name != NULL &&
shshahma@8621 1100 parsed_name->utf8_length() >= (int)pkglen &&
shshahma@8621 1101 !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
aoqi@0 1102 // It is illegal to define classes in the "java." package from
aoqi@0 1103 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
aoqi@0 1104 ResourceMark rm(THREAD);
aoqi@0 1105 char* name = parsed_name->as_C_string();
aoqi@0 1106 char* index = strrchr(name, '/');
shshahma@8621 1107 assert(index != NULL, "must be");
aoqi@0 1108 *index = '\0'; // chop to just the package name
aoqi@0 1109 while ((index = strchr(name, '/')) != NULL) {
aoqi@0 1110 *index = '.'; // replace '/' with '.' in package name
aoqi@0 1111 }
aoqi@0 1112 const char* fmt = "Prohibited package name: %s";
aoqi@0 1113 size_t len = strlen(fmt) + strlen(name);
aoqi@0 1114 char* message = NEW_RESOURCE_ARRAY(char, len);
aoqi@0 1115 jio_snprintf(message, len, fmt, name);
aoqi@0 1116 Exceptions::_throw_msg(THREAD_AND_LOCATION,
aoqi@0 1117 vmSymbols::java_lang_SecurityException(), message);
aoqi@0 1118 }
aoqi@0 1119
aoqi@0 1120 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 1121 assert(parsed_name != NULL, "Sanity");
aoqi@0 1122 assert(class_name == NULL || class_name == parsed_name, "name mismatch");
aoqi@0 1123 // Verification prevents us from creating names with dots in them, this
aoqi@0 1124 // asserts that that's the case.
aoqi@0 1125 assert(is_internal_format(parsed_name),
aoqi@0 1126 "external class name format used internally");
aoqi@0 1127
aoqi@0 1128 // Add class just loaded
aoqi@0 1129 // If a class loader supports parallel classloading handle parallel define requests
aoqi@0 1130 // find_or_define_instance_class may return a different InstanceKlass
aoqi@0 1131 if (is_parallelCapable(class_loader)) {
aoqi@0 1132 k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
aoqi@0 1133 } else {
aoqi@0 1134 define_instance_class(k, THREAD);
aoqi@0 1135 }
aoqi@0 1136 }
aoqi@0 1137
aoqi@0 1138 // Make sure we have an entry in the SystemDictionary on success
aoqi@0 1139 debug_only( {
aoqi@0 1140 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 1141 assert(parsed_name != NULL, "parsed_name is still null?");
aoqi@0 1142 Symbol* h_name = k->name();
aoqi@0 1143 ClassLoaderData *defining_loader_data = k->class_loader_data();
aoqi@0 1144
aoqi@0 1145 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 1146
aoqi@0 1147 Klass* check = find_class(parsed_name, loader_data);
aoqi@0 1148 assert(check == k(), "should be present in the dictionary");
aoqi@0 1149
aoqi@0 1150 Klass* check2 = find_class(h_name, defining_loader_data);
aoqi@0 1151 assert(check == check2, "name inconsistancy in SystemDictionary");
aoqi@0 1152 }
aoqi@0 1153 } );
aoqi@0 1154
aoqi@0 1155 return k();
aoqi@0 1156 }
aoqi@0 1157
iklam@7089 1158 #if INCLUDE_CDS
aoqi@0 1159 void SystemDictionary::set_shared_dictionary(HashtableBucket<mtClass>* t, int length,
aoqi@0 1160 int number_of_entries) {
aoqi@0 1161 assert(length == _nof_buckets * sizeof(HashtableBucket<mtClass>),
aoqi@0 1162 "bad shared dictionary size.");
aoqi@0 1163 _shared_dictionary = new Dictionary(_nof_buckets, t, number_of_entries);
aoqi@0 1164 }
aoqi@0 1165
aoqi@0 1166
aoqi@0 1167 // If there is a shared dictionary, then find the entry for the
aoqi@0 1168 // given shared system class, if any.
aoqi@0 1169
aoqi@0 1170 Klass* SystemDictionary::find_shared_class(Symbol* class_name) {
aoqi@0 1171 if (shared_dictionary() != NULL) {
aoqi@0 1172 unsigned int d_hash = shared_dictionary()->compute_hash(class_name, NULL);
aoqi@0 1173 int d_index = shared_dictionary()->hash_to_index(d_hash);
aoqi@0 1174
aoqi@0 1175 return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
aoqi@0 1176 } else {
aoqi@0 1177 return NULL;
aoqi@0 1178 }
aoqi@0 1179 }
aoqi@0 1180
aoqi@0 1181
aoqi@0 1182 // Load a class from the shared spaces (found through the shared system
aoqi@0 1183 // dictionary). Force the superclass and all interfaces to be loaded.
aoqi@0 1184 // Update the class definition to include sibling classes and no
aoqi@0 1185 // subclasses (yet). [Classes in the shared space are not part of the
aoqi@0 1186 // object hierarchy until loaded.]
aoqi@0 1187
aoqi@0 1188 instanceKlassHandle SystemDictionary::load_shared_class(
aoqi@0 1189 Symbol* class_name, Handle class_loader, TRAPS) {
aoqi@0 1190 instanceKlassHandle ik (THREAD, find_shared_class(class_name));
iklam@7089 1191 // Make sure we only return the boot class for the NULL classloader.
iklam@7089 1192 if (ik.not_null() &&
iklam@7089 1193 SharedClassUtil::is_shared_boot_class(ik()) && class_loader.is_null()) {
iklam@7089 1194 Handle protection_domain;
iklam@7089 1195 return load_shared_class(ik, class_loader, protection_domain, THREAD);
iklam@7089 1196 }
iklam@7089 1197 return instanceKlassHandle();
aoqi@0 1198 }
aoqi@0 1199
iklam@7089 1200 instanceKlassHandle SystemDictionary::load_shared_class(instanceKlassHandle ik,
iklam@7089 1201 Handle class_loader,
iklam@7089 1202 Handle protection_domain, TRAPS) {
aoqi@0 1203 if (ik.not_null()) {
aoqi@0 1204 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
iklam@7089 1205 Symbol* class_name = ik->name();
aoqi@0 1206
aoqi@0 1207 // Found the class, now load the superclass and interfaces. If they
aoqi@0 1208 // are shared, add them to the main system dictionary and reset
aoqi@0 1209 // their hierarchy references (supers, subs, and interfaces).
aoqi@0 1210
aoqi@0 1211 if (ik->super() != NULL) {
aoqi@0 1212 Symbol* cn = ik->super()->name();
iklam@8497 1213 Klass *s = resolve_super_or_fail(class_name, cn,
iklam@8497 1214 class_loader, protection_domain, true, CHECK_(nh));
iklam@8497 1215 if (s != ik->super()) {
iklam@8497 1216 // The dynamically resolved super class is not the same as the one we used during dump time,
iklam@8497 1217 // so we cannot use ik.
iklam@8497 1218 return nh;
iklam@8497 1219 }
aoqi@0 1220 }
aoqi@0 1221
aoqi@0 1222 Array<Klass*>* interfaces = ik->local_interfaces();
aoqi@0 1223 int num_interfaces = interfaces->length();
aoqi@0 1224 for (int index = 0; index < num_interfaces; index++) {
aoqi@0 1225 Klass* k = interfaces->at(index);
aoqi@0 1226
aoqi@0 1227 // Note: can not use InstanceKlass::cast here because
aoqi@0 1228 // interfaces' InstanceKlass's C++ vtbls haven't been
aoqi@0 1229 // reinitialized yet (they will be once the interface classes
aoqi@0 1230 // are loaded)
aoqi@0 1231 Symbol* name = k->name();
iklam@8497 1232 Klass* i = resolve_super_or_fail(class_name, name, class_loader, protection_domain, false, CHECK_(nh));
iklam@8497 1233 if (k != i) {
iklam@8497 1234 // The dynamically resolved interface class is not the same as the one we used during dump time,
iklam@8497 1235 // so we cannot use ik.
iklam@8497 1236 return nh;
iklam@8497 1237 }
aoqi@0 1238 }
aoqi@0 1239
aoqi@0 1240 // Adjust methods to recover missing data. They need addresses for
aoqi@0 1241 // interpreter entry points and their default native method address
aoqi@0 1242 // must be reset.
aoqi@0 1243
aoqi@0 1244 // Updating methods must be done under a lock so multiple
aoqi@0 1245 // threads don't update these in parallel
iklam@7089 1246 //
iklam@7089 1247 // Shared classes are all currently loaded by either the bootstrap or
iklam@7089 1248 // internal parallel class loaders, so this will never cause a deadlock
iklam@7089 1249 // on a custom class loader lock.
aoqi@0 1250
iklam@7089 1251 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
aoqi@0 1252 {
aoqi@0 1253 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
aoqi@0 1254 check_loader_lock_contention(lockObject, THREAD);
aoqi@0 1255 ObjectLocker ol(lockObject, THREAD, true);
iklam@7089 1256 ik->restore_unshareable_info(loader_data, protection_domain, CHECK_(nh));
aoqi@0 1257 }
aoqi@0 1258
aoqi@0 1259 if (TraceClassLoading) {
aoqi@0 1260 ResourceMark rm;
aoqi@0 1261 tty->print("[Loaded %s", ik->external_name());
aoqi@0 1262 tty->print(" from shared objects file");
iklam@7089 1263 if (class_loader.not_null()) {
iklam@7089 1264 tty->print(" by %s", loader_data->loader_name());
iklam@7089 1265 }
aoqi@0 1266 tty->print_cr("]");
aoqi@0 1267 }
iklam@7089 1268
iklam@7089 1269 if (DumpLoadedClassList != NULL && classlist_file->is_open()) {
iklam@7089 1270 // Only dump the classes that can be stored into CDS archive
iklam@7089 1271 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
iklam@7089 1272 ResourceMark rm(THREAD);
iklam@7089 1273 classlist_file->print_cr("%s", ik->name()->as_C_string());
iklam@7089 1274 classlist_file->flush();
iklam@7089 1275 }
iklam@7089 1276 }
iklam@7089 1277
aoqi@0 1278 // notify a class loaded from shared object
aoqi@0 1279 ClassLoadingService::notify_class_loaded(InstanceKlass::cast(ik()),
aoqi@0 1280 true /* shared class */);
aoqi@0 1281 }
aoqi@0 1282 return ik;
aoqi@0 1283 }
iklam@7090 1284 #endif // INCLUDE_CDS
aoqi@0 1285
aoqi@0 1286 instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
aoqi@0 1287 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
aoqi@0 1288 if (class_loader.is_null()) {
aoqi@0 1289
aoqi@0 1290 // Search the shared system dictionary for classes preloaded into the
aoqi@0 1291 // shared spaces.
aoqi@0 1292 instanceKlassHandle k;
aoqi@0 1293 {
iklam@7089 1294 #if INCLUDE_CDS
aoqi@0 1295 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
aoqi@0 1296 k = load_shared_class(class_name, class_loader, THREAD);
iklam@7089 1297 #endif
aoqi@0 1298 }
aoqi@0 1299
aoqi@0 1300 if (k.is_null()) {
aoqi@0 1301 // Use VM class loader
aoqi@0 1302 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
aoqi@0 1303 k = ClassLoader::load_classfile(class_name, CHECK_(nh));
aoqi@0 1304 }
aoqi@0 1305
aoqi@0 1306 // find_or_define_instance_class may return a different InstanceKlass
aoqi@0 1307 if (!k.is_null()) {
aoqi@0 1308 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
aoqi@0 1309 }
aoqi@0 1310 return k;
aoqi@0 1311 } else {
aoqi@0 1312 // Use user specified class loader to load class. Call loadClass operation on class_loader.
aoqi@0 1313 ResourceMark rm(THREAD);
aoqi@0 1314
aoqi@0 1315 assert(THREAD->is_Java_thread(), "must be a JavaThread");
aoqi@0 1316 JavaThread* jt = (JavaThread*) THREAD;
aoqi@0 1317
aoqi@0 1318 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
aoqi@0 1319 ClassLoader::perf_app_classload_selftime(),
aoqi@0 1320 ClassLoader::perf_app_classload_count(),
aoqi@0 1321 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 1322 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 1323 PerfClassTraceTime::CLASS_LOAD);
aoqi@0 1324
aoqi@0 1325 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
aoqi@0 1326 // Translate to external class name format, i.e., convert '/' chars to '.'
aoqi@0 1327 Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
aoqi@0 1328
aoqi@0 1329 JavaValue result(T_OBJECT);
aoqi@0 1330
aoqi@0 1331 KlassHandle spec_klass (THREAD, SystemDictionary::ClassLoader_klass());
aoqi@0 1332
aoqi@0 1333 // Call public unsynchronized loadClass(String) directly for all class loaders
aoqi@0 1334 // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
aoqi@0 1335 // acquire a class-name based lock rather than the class loader object lock.
aoqi@0 1336 // JDK < 7 already acquire the class loader lock in loadClass(String, boolean),
aoqi@0 1337 // so the call to loadClassInternal() was not required.
aoqi@0 1338 //
aoqi@0 1339 // UnsyncloadClass flag means both call loadClass(String) and do
aoqi@0 1340 // not acquire the class loader lock even for class loaders that are
aoqi@0 1341 // not parallelCapable. This was a risky transitional
aoqi@0 1342 // flag for diagnostic purposes only. It is risky to call
aoqi@0 1343 // custom class loaders without synchronization.
aoqi@0 1344 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of
aoqi@0 1345 // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field.
aoqi@0 1346 // Do NOT assume this will be supported in future releases.
aoqi@0 1347 //
aoqi@0 1348 // Added MustCallLoadClassInternal in case we discover in the field
aoqi@0 1349 // a customer that counts on this call
aoqi@0 1350 if (MustCallLoadClassInternal && has_loadClassInternal()) {
aoqi@0 1351 JavaCalls::call_special(&result,
aoqi@0 1352 class_loader,
aoqi@0 1353 spec_klass,
aoqi@0 1354 vmSymbols::loadClassInternal_name(),
aoqi@0 1355 vmSymbols::string_class_signature(),
aoqi@0 1356 string,
aoqi@0 1357 CHECK_(nh));
aoqi@0 1358 } else {
aoqi@0 1359 JavaCalls::call_virtual(&result,
aoqi@0 1360 class_loader,
aoqi@0 1361 spec_klass,
aoqi@0 1362 vmSymbols::loadClass_name(),
aoqi@0 1363 vmSymbols::string_class_signature(),
aoqi@0 1364 string,
aoqi@0 1365 CHECK_(nh));
aoqi@0 1366 }
aoqi@0 1367
aoqi@0 1368 assert(result.get_type() == T_OBJECT, "just checking");
aoqi@0 1369 oop obj = (oop) result.get_jobject();
aoqi@0 1370
aoqi@0 1371 // Primitive classes return null since forName() can not be
aoqi@0 1372 // used to obtain any of the Class objects representing primitives or void
aoqi@0 1373 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
aoqi@0 1374 instanceKlassHandle k =
aoqi@0 1375 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
aoqi@0 1376 // For user defined Java class loaders, check that the name returned is
aoqi@0 1377 // the same as that requested. This check is done for the bootstrap
aoqi@0 1378 // loader when parsing the class file.
aoqi@0 1379 if (class_name == k->name()) {
aoqi@0 1380 return k;
aoqi@0 1381 }
aoqi@0 1382 }
aoqi@0 1383 // Class is not found or has the wrong name, return NULL
aoqi@0 1384 return nh;
aoqi@0 1385 }
aoqi@0 1386 }
aoqi@0 1387
aoqi@0 1388 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
aoqi@0 1389
aoqi@0 1390 ClassLoaderData* loader_data = k->class_loader_data();
aoqi@0 1391 Handle class_loader_h(THREAD, loader_data->class_loader());
aoqi@0 1392
aoqi@0 1393 for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
aoqi@0 1394
aoqi@0 1395 // for bootstrap and other parallel classloaders don't acquire lock,
aoqi@0 1396 // use placeholder token
aoqi@0 1397 // If a parallelCapable class loader calls define_instance_class instead of
aoqi@0 1398 // find_or_define_instance_class to get here, we have a timing
aoqi@0 1399 // hole with systemDictionary updates and check_constraints
aoqi@0 1400 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
aoqi@0 1401 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
aoqi@0 1402 compute_loader_lock_object(class_loader_h, THREAD)),
aoqi@0 1403 "define called without lock");
aoqi@0 1404 }
aoqi@0 1405
aoqi@0 1406 // Check class-loading constraints. Throw exception if violation is detected.
aoqi@0 1407 // Grabs and releases SystemDictionary_lock
aoqi@0 1408 // The check_constraints/find_class call and update_dictionary sequence
aoqi@0 1409 // must be "atomic" for a specific class/classloader pair so we never
aoqi@0 1410 // define two different instanceKlasses for that class/classloader pair.
aoqi@0 1411 // Existing classloaders will call define_instance_class with the
aoqi@0 1412 // classloader lock held
aoqi@0 1413 // Parallel classloaders will call find_or_define_instance_class
aoqi@0 1414 // which will require a token to perform the define class
aoqi@0 1415 Symbol* name_h = k->name();
aoqi@0 1416 unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
aoqi@0 1417 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 1418 check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
aoqi@0 1419
aoqi@0 1420 // Register class just loaded with class loader (placed in Vector)
aoqi@0 1421 // Note we do this before updating the dictionary, as this can
aoqi@0 1422 // fail with an OutOfMemoryError (if it does, we will *not* put this
aoqi@0 1423 // class in the dictionary and will not update the class hierarchy).
aoqi@0 1424 // JVMTI FollowReferences needs to find the classes this way.
aoqi@0 1425 if (k->class_loader() != NULL) {
aoqi@0 1426 methodHandle m(THREAD, Universe::loader_addClass_method());
aoqi@0 1427 JavaValue result(T_VOID);
aoqi@0 1428 JavaCallArguments args(class_loader_h);
aoqi@0 1429 args.push_oop(Handle(THREAD, k->java_mirror()));
aoqi@0 1430 JavaCalls::call(&result, m, &args, CHECK);
aoqi@0 1431 }
aoqi@0 1432
aoqi@0 1433 // Add the new class. We need recompile lock during update of CHA.
aoqi@0 1434 {
aoqi@0 1435 unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
aoqi@0 1436 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 1437
aoqi@0 1438 MutexLocker mu_r(Compile_lock, THREAD);
aoqi@0 1439
aoqi@0 1440 // Add to class hierarchy, initialize vtables, and do possible
aoqi@0 1441 // deoptimizations.
aoqi@0 1442 add_to_hierarchy(k, CHECK); // No exception, but can block
aoqi@0 1443
aoqi@0 1444 // Add to systemDictionary - so other classes can see it.
aoqi@0 1445 // Grabs and releases SystemDictionary_lock
aoqi@0 1446 update_dictionary(d_index, d_hash, p_index, p_hash,
aoqi@0 1447 k, class_loader_h, THREAD);
aoqi@0 1448 }
aoqi@0 1449 k->eager_initialize(THREAD);
aoqi@0 1450
aoqi@0 1451 // notify jvmti
aoqi@0 1452 if (JvmtiExport::should_post_class_load()) {
aoqi@0 1453 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
aoqi@0 1454 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
aoqi@0 1455
aoqi@0 1456 }
aoqi@0 1457
aoqi@0 1458 }
aoqi@0 1459
aoqi@0 1460 // Support parallel classloading
aoqi@0 1461 // All parallel class loaders, including bootstrap classloader
aoqi@0 1462 // lock a placeholder entry for this class/class_loader pair
aoqi@0 1463 // to allow parallel defines of different classes for this class loader
aoqi@0 1464 // With AllowParallelDefine flag==true, in case they do not synchronize around
aoqi@0 1465 // FindLoadedClass/DefineClass, calls, we check for parallel
aoqi@0 1466 // loading for them, wait if a defineClass is in progress
aoqi@0 1467 // and return the initial requestor's results
aoqi@0 1468 // This flag does not apply to the bootstrap classloader.
aoqi@0 1469 // With AllowParallelDefine flag==false, call through to define_instance_class
aoqi@0 1470 // which will throw LinkageError: duplicate class definition.
aoqi@0 1471 // False is the requested default.
aoqi@0 1472 // For better performance, the class loaders should synchronize
aoqi@0 1473 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
aoqi@0 1474 // potentially waste time reading and parsing the bytestream.
aoqi@0 1475 // Note: VM callers should ensure consistency of k/class_name,class_loader
aoqi@0 1476 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
aoqi@0 1477
aoqi@0 1478 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
aoqi@0 1479 Symbol* name_h = k->name(); // passed in class_name may be null
aoqi@0 1480 ClassLoaderData* loader_data = class_loader_data(class_loader);
aoqi@0 1481
aoqi@0 1482 unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
aoqi@0 1483 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 1484
aoqi@0 1485 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
aoqi@0 1486 unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
aoqi@0 1487 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 1488 PlaceholderEntry* probe;
aoqi@0 1489
aoqi@0 1490 {
aoqi@0 1491 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 1492 // First check if class already defined
aoqi@0 1493 if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
aoqi@0 1494 Klass* check = find_class(d_index, d_hash, name_h, loader_data);
aoqi@0 1495 if (check != NULL) {
aoqi@0 1496 return(instanceKlassHandle(THREAD, check));
aoqi@0 1497 }
aoqi@0 1498 }
aoqi@0 1499
aoqi@0 1500 // Acquire define token for this class/classloader
aoqi@0 1501 probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
aoqi@0 1502 // Wait if another thread defining in parallel
aoqi@0 1503 // All threads wait - even those that will throw duplicate class: otherwise
aoqi@0 1504 // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
aoqi@0 1505 // if other thread has not finished updating dictionary
aoqi@0 1506 while (probe->definer() != NULL) {
aoqi@0 1507 SystemDictionary_lock->wait();
aoqi@0 1508 }
aoqi@0 1509 // Only special cases allow parallel defines and can use other thread's results
aoqi@0 1510 // Other cases fall through, and may run into duplicate defines
aoqi@0 1511 // caught by finding an entry in the SystemDictionary
aoqi@0 1512 if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
aoqi@0 1513 placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
aoqi@0 1514 SystemDictionary_lock->notify_all();
aoqi@0 1515 #ifdef ASSERT
aoqi@0 1516 Klass* check = find_class(d_index, d_hash, name_h, loader_data);
aoqi@0 1517 assert(check != NULL, "definer missed recording success");
aoqi@0 1518 #endif
aoqi@0 1519 return(instanceKlassHandle(THREAD, probe->instance_klass()));
aoqi@0 1520 } else {
aoqi@0 1521 // This thread will define the class (even if earlier thread tried and had an error)
aoqi@0 1522 probe->set_definer(THREAD);
aoqi@0 1523 }
aoqi@0 1524 }
aoqi@0 1525
aoqi@0 1526 define_instance_class(k, THREAD);
aoqi@0 1527
aoqi@0 1528 Handle linkage_exception = Handle(); // null handle
aoqi@0 1529
aoqi@0 1530 // definer must notify any waiting threads
aoqi@0 1531 {
aoqi@0 1532 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 1533 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, loader_data);
aoqi@0 1534 assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
aoqi@0 1535 if (probe != NULL) {
aoqi@0 1536 if (HAS_PENDING_EXCEPTION) {
aoqi@0 1537 linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
aoqi@0 1538 CLEAR_PENDING_EXCEPTION;
aoqi@0 1539 } else {
aoqi@0 1540 probe->set_instance_klass(k());
aoqi@0 1541 }
aoqi@0 1542 probe->set_definer(NULL);
aoqi@0 1543 placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
aoqi@0 1544 SystemDictionary_lock->notify_all();
aoqi@0 1545 }
aoqi@0 1546 }
aoqi@0 1547
aoqi@0 1548 // Can't throw exception while holding lock due to rank ordering
aoqi@0 1549 if (linkage_exception() != NULL) {
aoqi@0 1550 THROW_OOP_(linkage_exception(), nh); // throws exception and returns
aoqi@0 1551 }
aoqi@0 1552
aoqi@0 1553 return k;
aoqi@0 1554 }
aoqi@0 1555 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
aoqi@0 1556 // If class_loader is NULL we synchronize on _system_loader_lock_obj
aoqi@0 1557 if (class_loader.is_null()) {
aoqi@0 1558 return Handle(THREAD, _system_loader_lock_obj);
aoqi@0 1559 } else {
aoqi@0 1560 return class_loader;
aoqi@0 1561 }
aoqi@0 1562 }
aoqi@0 1563
aoqi@0 1564 // This method is added to check how often we have to wait to grab loader
aoqi@0 1565 // lock. The results are being recorded in the performance counters defined in
aoqi@0 1566 // ClassLoader::_sync_systemLoaderLockContentionRate and
aoqi@0 1567 // ClassLoader::_sync_nonSystemLoaderLockConteionRate.
aoqi@0 1568 void SystemDictionary::check_loader_lock_contention(Handle loader_lock, TRAPS) {
aoqi@0 1569 if (!UsePerfData) {
aoqi@0 1570 return;
aoqi@0 1571 }
aoqi@0 1572
aoqi@0 1573 assert(!loader_lock.is_null(), "NULL lock object");
aoqi@0 1574
aoqi@0 1575 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
aoqi@0 1576 == ObjectSynchronizer::owner_other) {
aoqi@0 1577 // contention will likely happen, so increment the corresponding
aoqi@0 1578 // contention counter.
aoqi@0 1579 if (loader_lock() == _system_loader_lock_obj) {
aoqi@0 1580 ClassLoader::sync_systemLoaderLockContentionRate()->inc();
aoqi@0 1581 } else {
aoqi@0 1582 ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
aoqi@0 1583 }
aoqi@0 1584 }
aoqi@0 1585 }
aoqi@0 1586
aoqi@0 1587 // ----------------------------------------------------------------------------
aoqi@0 1588 // Lookup
aoqi@0 1589
aoqi@0 1590 Klass* SystemDictionary::find_class(int index, unsigned int hash,
aoqi@0 1591 Symbol* class_name,
aoqi@0 1592 ClassLoaderData* loader_data) {
aoqi@0 1593 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 1594 assert (index == dictionary()->index_for(class_name, loader_data),
aoqi@0 1595 "incorrect index?");
aoqi@0 1596
aoqi@0 1597 Klass* k = dictionary()->find_class(index, hash, class_name, loader_data);
aoqi@0 1598 return k;
aoqi@0 1599 }
aoqi@0 1600
aoqi@0 1601
aoqi@0 1602 // Basic find on classes in the midst of being loaded
aoqi@0 1603 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
aoqi@0 1604 ClassLoaderData* loader_data) {
aoqi@0 1605 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 1606 unsigned int p_hash = placeholders()->compute_hash(class_name, loader_data);
aoqi@0 1607 int p_index = placeholders()->hash_to_index(p_hash);
aoqi@0 1608 return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
aoqi@0 1609 }
aoqi@0 1610
aoqi@0 1611
aoqi@0 1612 // Used for assertions and verification only
aoqi@0 1613 Klass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
aoqi@0 1614 #ifndef ASSERT
aoqi@0 1615 guarantee(VerifyBeforeGC ||
aoqi@0 1616 VerifyDuringGC ||
aoqi@0 1617 VerifyBeforeExit ||
aoqi@0 1618 VerifyDuringStartup ||
aoqi@0 1619 VerifyAfterGC, "too expensive");
aoqi@0 1620 #endif
aoqi@0 1621 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 1622
aoqi@0 1623 // First look in the loaded class array
aoqi@0 1624 unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
aoqi@0 1625 int d_index = dictionary()->hash_to_index(d_hash);
aoqi@0 1626 return find_class(d_index, d_hash, class_name, loader_data);
aoqi@0 1627 }
aoqi@0 1628
aoqi@0 1629
aoqi@0 1630 // Get the next class in the diictionary.
aoqi@0 1631 Klass* SystemDictionary::try_get_next_class() {
aoqi@0 1632 return dictionary()->try_get_next_class();
aoqi@0 1633 }
aoqi@0 1634
aoqi@0 1635
aoqi@0 1636 // ----------------------------------------------------------------------------
aoqi@0 1637 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
aoqi@0 1638 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
aoqi@0 1639 // before a new class is used.
aoqi@0 1640
aoqi@0 1641 void SystemDictionary::add_to_hierarchy(instanceKlassHandle k, TRAPS) {
aoqi@0 1642 assert(k.not_null(), "just checking");
aoqi@0 1643 assert_locked_or_safepoint(Compile_lock);
aoqi@0 1644
aoqi@0 1645 // Link into hierachy. Make sure the vtables are initialized before linking into
aoqi@0 1646 k->append_to_sibling_list(); // add to superklass/sibling list
aoqi@0 1647 k->process_interfaces(THREAD); // handle all "implements" declarations
aoqi@0 1648 k->set_init_state(InstanceKlass::loaded);
aoqi@0 1649 // Now flush all code that depended on old class hierarchy.
aoqi@0 1650 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
aoqi@0 1651 // Also, first reinitialize vtable because it may have gotten out of synch
aoqi@0 1652 // while the new class wasn't connected to the class hierarchy.
aoqi@0 1653 Universe::flush_dependents_on(k);
aoqi@0 1654 }
aoqi@0 1655
aoqi@0 1656 // ----------------------------------------------------------------------------
aoqi@0 1657 // GC support
aoqi@0 1658
aoqi@0 1659 // Following roots during mark-sweep is separated in two phases.
aoqi@0 1660 //
aoqi@0 1661 // The first phase follows preloaded classes and all other system
aoqi@0 1662 // classes, since these will never get unloaded anyway.
aoqi@0 1663 //
aoqi@0 1664 // The second phase removes (unloads) unreachable classes from the
aoqi@0 1665 // system dictionary and follows the remaining classes' contents.
aoqi@0 1666
aoqi@0 1667 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
stefank@6992 1668 roots_oops_do(blk, NULL);
aoqi@0 1669 }
aoqi@0 1670
aoqi@0 1671 void SystemDictionary::always_strong_classes_do(KlassClosure* closure) {
aoqi@0 1672 // Follow all system classes and temporary placeholders in dictionary
aoqi@0 1673 dictionary()->always_strong_classes_do(closure);
aoqi@0 1674
aoqi@0 1675 // Placeholders. These represent classes we're actively loading.
aoqi@0 1676 placeholders()->classes_do(closure);
aoqi@0 1677 }
aoqi@0 1678
aoqi@0 1679 // Calculate a "good" systemdictionary size based
aoqi@0 1680 // on predicted or current loaded classes count
aoqi@0 1681 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
aoqi@0 1682 int newsize = _old_default_sdsize;
aoqi@0 1683 if ((classcount > 0) && !DumpSharedSpaces) {
aoqi@0 1684 int desiredsize = classcount/_average_depth_goal;
aoqi@0 1685 for (newsize = _primelist[_sdgeneration]; _sdgeneration < _prime_array_size -1;
aoqi@0 1686 newsize = _primelist[++_sdgeneration]) {
aoqi@0 1687 if (desiredsize <= newsize) {
aoqi@0 1688 break;
aoqi@0 1689 }
aoqi@0 1690 }
aoqi@0 1691 }
aoqi@0 1692 return newsize;
aoqi@0 1693 }
aoqi@0 1694
aoqi@0 1695 #ifdef ASSERT
aoqi@0 1696 class VerifySDReachableAndLiveClosure : public OopClosure {
aoqi@0 1697 private:
aoqi@0 1698 BoolObjectClosure* _is_alive;
aoqi@0 1699
aoqi@0 1700 template <class T> void do_oop_work(T* p) {
aoqi@0 1701 oop obj = oopDesc::load_decode_heap_oop(p);
aoqi@0 1702 guarantee(_is_alive->do_object_b(obj), "Oop in system dictionary must be live");
aoqi@0 1703 }
aoqi@0 1704
aoqi@0 1705 public:
aoqi@0 1706 VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { }
aoqi@0 1707
aoqi@0 1708 virtual void do_oop(oop* p) { do_oop_work(p); }
aoqi@0 1709 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
aoqi@0 1710 };
aoqi@0 1711 #endif
aoqi@0 1712
aoqi@0 1713 // Assumes classes in the SystemDictionary are only unloaded at a safepoint
aoqi@0 1714 // Note: anonymous classes are not in the SD.
stefank@7333 1715 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive, bool clean_alive) {
aoqi@0 1716 // First, mark for unload all ClassLoaderData referencing a dead class loader.
stefank@7333 1717 bool unloading_occurred = ClassLoaderDataGraph::do_unloading(is_alive, clean_alive);
thartmann@7064 1718 if (unloading_occurred) {
thartmann@7064 1719 dictionary()->do_unloading();
aoqi@0 1720 constraints()->purge_loader_constraints();
aoqi@0 1721 resolution_errors()->purge_resolution_errors();
aoqi@0 1722 }
aoqi@0 1723 // Oops referenced by the system dictionary may get unreachable independently
aoqi@0 1724 // of the class loader (eg. cached protection domain oops). So we need to
aoqi@0 1725 // explicitly unlink them here instead of in Dictionary::do_unloading.
aoqi@0 1726 dictionary()->unlink(is_alive);
aoqi@0 1727 #ifdef ASSERT
aoqi@0 1728 VerifySDReachableAndLiveClosure cl(is_alive);
aoqi@0 1729 dictionary()->oops_do(&cl);
aoqi@0 1730 #endif
aoqi@0 1731 return unloading_occurred;
aoqi@0 1732 }
aoqi@0 1733
stefank@6992 1734 void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
stefank@6992 1735 strong->do_oop(&_java_system_loader);
stefank@6992 1736 strong->do_oop(&_system_loader_lock_obj);
iklam@7089 1737 CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);)
stefank@6992 1738
stefank@6992 1739 // Adjust dictionary
stefank@6992 1740 dictionary()->roots_oops_do(strong, weak);
stefank@6992 1741
stefank@6992 1742 // Visit extra methods
stefank@6992 1743 invoke_method_table()->oops_do(strong);
stefank@6992 1744 }
stefank@6992 1745
aoqi@0 1746 void SystemDictionary::oops_do(OopClosure* f) {
aoqi@0 1747 f->do_oop(&_java_system_loader);
aoqi@0 1748 f->do_oop(&_system_loader_lock_obj);
iklam@7089 1749 CDS_ONLY(SystemDictionaryShared::oops_do(f);)
aoqi@0 1750
aoqi@0 1751 // Adjust dictionary
aoqi@0 1752 dictionary()->oops_do(f);
aoqi@0 1753
aoqi@0 1754 // Visit extra methods
aoqi@0 1755 invoke_method_table()->oops_do(f);
aoqi@0 1756 }
aoqi@0 1757
aoqi@0 1758 // Extended Class redefinition support.
aoqi@0 1759 // If one of these classes is replaced, we need to replace it in these places.
aoqi@0 1760 // KlassClosure::do_klass should take the address of a class but we can
aoqi@0 1761 // change that later.
aoqi@0 1762 void SystemDictionary::preloaded_classes_do(KlassClosure* f) {
aoqi@0 1763 for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
aoqi@0 1764 f->do_klass(_well_known_klasses[k]);
aoqi@0 1765 }
aoqi@0 1766
aoqi@0 1767 {
aoqi@0 1768 for (int i = 0; i < T_VOID+1; i++) {
aoqi@0 1769 if (_box_klasses[i] != NULL) {
aoqi@0 1770 assert(i >= T_BOOLEAN, "checking");
aoqi@0 1771 f->do_klass(_box_klasses[i]);
aoqi@0 1772 }
aoqi@0 1773 }
aoqi@0 1774 }
aoqi@0 1775
aoqi@0 1776 FilteredFieldsMap::classes_do(f);
aoqi@0 1777 }
aoqi@0 1778
aoqi@0 1779 void SystemDictionary::lazily_loaded_classes_do(KlassClosure* f) {
aoqi@0 1780 f->do_klass(_abstract_ownable_synchronizer_klass);
aoqi@0 1781 }
aoqi@0 1782
aoqi@0 1783 // Just the classes from defining class loaders
aoqi@0 1784 // Don't iterate over placeholders
aoqi@0 1785 void SystemDictionary::classes_do(void f(Klass*)) {
aoqi@0 1786 dictionary()->classes_do(f);
aoqi@0 1787 }
aoqi@0 1788
aoqi@0 1789 // Added for initialize_itable_for_klass
aoqi@0 1790 // Just the classes from defining class loaders
aoqi@0 1791 // Don't iterate over placeholders
aoqi@0 1792 void SystemDictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
aoqi@0 1793 dictionary()->classes_do(f, CHECK);
aoqi@0 1794 }
aoqi@0 1795
aoqi@0 1796 // All classes, and their class loaders
aoqi@0 1797 // Don't iterate over placeholders
aoqi@0 1798 void SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
aoqi@0 1799 dictionary()->classes_do(f);
aoqi@0 1800 }
aoqi@0 1801
aoqi@0 1802 void SystemDictionary::placeholders_do(void f(Symbol*)) {
aoqi@0 1803 placeholders()->entries_do(f);
aoqi@0 1804 }
aoqi@0 1805
aoqi@0 1806 void SystemDictionary::methods_do(void f(Method*)) {
aoqi@0 1807 dictionary()->methods_do(f);
aoqi@0 1808 invoke_method_table()->methods_do(f);
aoqi@0 1809 }
aoqi@0 1810
iklam@7089 1811 void SystemDictionary::remove_classes_in_error_state() {
iklam@7089 1812 dictionary()->remove_classes_in_error_state();
iklam@7089 1813 }
iklam@7089 1814
aoqi@0 1815 // ----------------------------------------------------------------------------
aoqi@0 1816 // Lazily load klasses
aoqi@0 1817
aoqi@0 1818 void SystemDictionary::load_abstract_ownable_synchronizer_klass(TRAPS) {
aoqi@0 1819 assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
aoqi@0 1820
aoqi@0 1821 // if multiple threads calling this function, only one thread will load
aoqi@0 1822 // the class. The other threads will find the loaded version once the
aoqi@0 1823 // class is loaded.
aoqi@0 1824 Klass* aos = _abstract_ownable_synchronizer_klass;
aoqi@0 1825 if (aos == NULL) {
aoqi@0 1826 Klass* k = resolve_or_fail(vmSymbols::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
aoqi@0 1827 // Force a fence to prevent any read before the write completes
aoqi@0 1828 OrderAccess::fence();
aoqi@0 1829 _abstract_ownable_synchronizer_klass = k;
aoqi@0 1830 }
aoqi@0 1831 }
aoqi@0 1832
aoqi@0 1833 // ----------------------------------------------------------------------------
aoqi@0 1834 // Initialization
aoqi@0 1835
aoqi@0 1836 void SystemDictionary::initialize(TRAPS) {
aoqi@0 1837 // Allocate arrays
aoqi@0 1838 assert(dictionary() == NULL,
aoqi@0 1839 "SystemDictionary should only be initialized once");
aoqi@0 1840 _sdgeneration = 0;
aoqi@0 1841 _dictionary = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount));
aoqi@0 1842 _placeholders = new PlaceholderTable(_nof_buckets);
aoqi@0 1843 _number_of_modifications = 0;
aoqi@0 1844 _loader_constraints = new LoaderConstraintTable(_loader_constraint_size);
aoqi@0 1845 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
aoqi@0 1846 _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
aoqi@0 1847
aoqi@0 1848 // Allocate private object used as system class loader lock
aoqi@0 1849 _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
aoqi@0 1850 // Initialize basic classes
aoqi@0 1851 initialize_preloaded_classes(CHECK);
aoqi@0 1852 }
aoqi@0 1853
aoqi@0 1854 // Compact table of directions on the initialization of klasses:
aoqi@0 1855 static const short wk_init_info[] = {
aoqi@0 1856 #define WK_KLASS_INIT_INFO(name, symbol, option) \
aoqi@0 1857 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
aoqi@0 1858 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
aoqi@0 1859 | (int)SystemDictionary::option ),
aoqi@0 1860 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
aoqi@0 1861 #undef WK_KLASS_INIT_INFO
aoqi@0 1862 0
aoqi@0 1863 };
aoqi@0 1864
aoqi@0 1865 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
aoqi@0 1866 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
aoqi@0 1867 int info = wk_init_info[id - FIRST_WKID];
aoqi@0 1868 int sid = (info >> CEIL_LG_OPTION_LIMIT);
aoqi@0 1869 Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
aoqi@0 1870 Klass** klassp = &_well_known_klasses[id];
aoqi@0 1871 bool must_load = (init_opt < SystemDictionary::Opt);
aoqi@0 1872 if ((*klassp) == NULL) {
aoqi@0 1873 if (must_load) {
aoqi@0 1874 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
aoqi@0 1875 } else {
aoqi@0 1876 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
aoqi@0 1877 }
aoqi@0 1878 }
aoqi@0 1879 return ((*klassp) != NULL);
aoqi@0 1880 }
aoqi@0 1881
aoqi@0 1882 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
aoqi@0 1883 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
aoqi@0 1884 for (int id = (int)start_id; id < (int)limit_id; id++) {
aoqi@0 1885 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
aoqi@0 1886 int info = wk_init_info[id - FIRST_WKID];
aoqi@0 1887 int sid = (info >> CEIL_LG_OPTION_LIMIT);
aoqi@0 1888 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
aoqi@0 1889
aoqi@0 1890 initialize_wk_klass((WKID)id, opt, CHECK);
aoqi@0 1891 }
aoqi@0 1892
aoqi@0 1893 // move the starting value forward to the limit:
aoqi@0 1894 start_id = limit_id;
aoqi@0 1895 }
aoqi@0 1896
aoqi@0 1897 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
aoqi@0 1898 assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
aoqi@0 1899 // Preload commonly used klasses
aoqi@0 1900 WKID scan = FIRST_WKID;
aoqi@0 1901 // first do Object, then String, Class
aoqi@0 1902 if (UseSharedSpaces) {
aoqi@0 1903 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK);
aoqi@0 1904 // Initialize the constant pool for the Object_class
aoqi@0 1905 InstanceKlass* ik = InstanceKlass::cast(Object_klass());
aoqi@0 1906 ik->constants()->restore_unshareable_info(CHECK);
aoqi@0 1907 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
aoqi@0 1908 } else {
aoqi@0 1909 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
aoqi@0 1910 }
aoqi@0 1911
aoqi@0 1912 // Calculate offsets for String and Class classes since they are loaded and
aoqi@0 1913 // can be used after this point.
aoqi@0 1914 java_lang_String::compute_offsets();
aoqi@0 1915 java_lang_Class::compute_offsets();
aoqi@0 1916
aoqi@0 1917 // Fixup mirrors for classes loaded before java.lang.Class.
aoqi@0 1918 // These calls iterate over the objects currently in the perm gen
aoqi@0 1919 // so calling them at this point is matters (not before when there
aoqi@0 1920 // are fewer objects and not later after there are more objects
aoqi@0 1921 // in the perm gen.
aoqi@0 1922 Universe::initialize_basic_type_mirrors(CHECK);
aoqi@0 1923 Universe::fixup_mirrors(CHECK);
aoqi@0 1924
aoqi@0 1925 // do a bunch more:
aoqi@0 1926 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK);
aoqi@0 1927
aoqi@0 1928 // Preload ref klasses and set reference types
aoqi@0 1929 InstanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER);
aoqi@0 1930 InstanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass));
aoqi@0 1931
jmasa@7469 1932 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Cleaner_klass), scan, CHECK);
aoqi@0 1933 InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT);
aoqi@0 1934 InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK);
aoqi@0 1935 InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL);
aoqi@0 1936 InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM);
jmasa@7469 1937 InstanceKlass::cast(WK_KLASS(Cleaner_klass))->set_reference_type(REF_CLEANER);
aoqi@0 1938
kbarrett@9099 1939 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(ReferenceQueue_klass), scan, CHECK);
kbarrett@9099 1940
aoqi@0 1941 // JSR 292 classes
aoqi@0 1942 WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);
aoqi@0 1943 WKID jsr292_group_end = WK_KLASS_ENUM_NAME(VolatileCallSite_klass);
aoqi@0 1944 initialize_wk_klasses_until(jsr292_group_start, scan, CHECK);
aoqi@0 1945 if (EnableInvokeDynamic) {
aoqi@0 1946 initialize_wk_klasses_through(jsr292_group_end, scan, CHECK);
aoqi@0 1947 } else {
aoqi@0 1948 // Skip the JSR 292 classes, if not enabled.
aoqi@0 1949 scan = WKID(jsr292_group_end + 1);
aoqi@0 1950 }
aoqi@0 1951
aoqi@0 1952 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK);
aoqi@0 1953
aoqi@0 1954 _box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass);
aoqi@0 1955 _box_klasses[T_CHAR] = WK_KLASS(Character_klass);
aoqi@0 1956 _box_klasses[T_FLOAT] = WK_KLASS(Float_klass);
aoqi@0 1957 _box_klasses[T_DOUBLE] = WK_KLASS(Double_klass);
aoqi@0 1958 _box_klasses[T_BYTE] = WK_KLASS(Byte_klass);
aoqi@0 1959 _box_klasses[T_SHORT] = WK_KLASS(Short_klass);
aoqi@0 1960 _box_klasses[T_INT] = WK_KLASS(Integer_klass);
aoqi@0 1961 _box_klasses[T_LONG] = WK_KLASS(Long_klass);
aoqi@0 1962 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass);
aoqi@0 1963 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass);
aoqi@0 1964
aoqi@0 1965 { // Compute whether we should use loadClass or loadClassInternal when loading classes.
aoqi@0 1966 Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
aoqi@0 1967 _has_loadClassInternal = (method != NULL);
aoqi@0 1968 }
aoqi@0 1969 { // Compute whether we should use checkPackageAccess or NOT
aoqi@0 1970 Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
aoqi@0 1971 _has_checkPackageAccess = (method != NULL);
aoqi@0 1972 }
aoqi@0 1973 }
aoqi@0 1974
aoqi@0 1975 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
aoqi@0 1976 // If so, returns the basic type it holds. If not, returns T_OBJECT.
aoqi@0 1977 BasicType SystemDictionary::box_klass_type(Klass* k) {
aoqi@0 1978 assert(k != NULL, "");
aoqi@0 1979 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
aoqi@0 1980 if (_box_klasses[i] == k)
aoqi@0 1981 return (BasicType)i;
aoqi@0 1982 }
aoqi@0 1983 return T_OBJECT;
aoqi@0 1984 }
aoqi@0 1985
aoqi@0 1986 // Constraints on class loaders. The details of the algorithm can be
aoqi@0 1987 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
aoqi@0 1988 // Virtual Machine" by Sheng Liang and Gilad Bracha. The basic idea is
aoqi@0 1989 // that the system dictionary needs to maintain a set of contraints that
aoqi@0 1990 // must be satisfied by all classes in the dictionary.
aoqi@0 1991 // if defining is true, then LinkageError if already in systemDictionary
aoqi@0 1992 // if initiating loader, then ok if InstanceKlass matches existing entry
aoqi@0 1993
aoqi@0 1994 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
aoqi@0 1995 instanceKlassHandle k,
aoqi@0 1996 Handle class_loader, bool defining,
aoqi@0 1997 TRAPS) {
aoqi@0 1998 const char *linkage_error = NULL;
aoqi@0 1999 {
aoqi@0 2000 Symbol* name = k->name();
aoqi@0 2001 ClassLoaderData *loader_data = class_loader_data(class_loader);
aoqi@0 2002
aoqi@0 2003 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 2004
aoqi@0 2005 Klass* check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 2006 if (check != (Klass*)NULL) {
aoqi@0 2007 // if different InstanceKlass - duplicate class definition,
aoqi@0 2008 // else - ok, class loaded by a different thread in parallel,
aoqi@0 2009 // we should only have found it if it was done loading and ok to use
aoqi@0 2010 // system dictionary only holds instance classes, placeholders
aoqi@0 2011 // also holds array classes
aoqi@0 2012
aoqi@0 2013 assert(check->oop_is_instance(), "noninstance in systemdictionary");
aoqi@0 2014 if ((defining == true) || (k() != check)) {
aoqi@0 2015 linkage_error = "loader (instance of %s): attempted duplicate class "
aoqi@0 2016 "definition for name: \"%s\"";
aoqi@0 2017 } else {
aoqi@0 2018 return;
aoqi@0 2019 }
aoqi@0 2020 }
aoqi@0 2021
aoqi@0 2022 #ifdef ASSERT
aoqi@0 2023 Symbol* ph_check = find_placeholder(name, loader_data);
aoqi@0 2024 assert(ph_check == NULL || ph_check == name, "invalid symbol");
aoqi@0 2025 #endif
aoqi@0 2026
aoqi@0 2027 if (linkage_error == NULL) {
aoqi@0 2028 if (constraints()->check_or_update(k, class_loader, name) == false) {
aoqi@0 2029 linkage_error = "loader constraint violation: loader (instance of %s)"
aoqi@0 2030 " previously initiated loading for a different type with name \"%s\"";
aoqi@0 2031 }
aoqi@0 2032 }
aoqi@0 2033 }
aoqi@0 2034
aoqi@0 2035 // Throw error now if needed (cannot throw while holding
aoqi@0 2036 // SystemDictionary_lock because of rank ordering)
aoqi@0 2037
aoqi@0 2038 if (linkage_error) {
aoqi@0 2039 ResourceMark rm(THREAD);
aoqi@0 2040 const char* class_loader_name = loader_name(class_loader());
aoqi@0 2041 char* type_name = k->name()->as_C_string();
aoqi@0 2042 size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
aoqi@0 2043 strlen(type_name);
aoqi@0 2044 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
aoqi@0 2045 jio_snprintf(buf, buflen, linkage_error, class_loader_name, type_name);
aoqi@0 2046 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
aoqi@0 2047 }
aoqi@0 2048 }
aoqi@0 2049
aoqi@0 2050
aoqi@0 2051 // Update system dictionary - done after check_constraint and add_to_hierachy
aoqi@0 2052 // have been called.
aoqi@0 2053 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
aoqi@0 2054 int p_index, unsigned int p_hash,
aoqi@0 2055 instanceKlassHandle k,
aoqi@0 2056 Handle class_loader,
aoqi@0 2057 TRAPS) {
aoqi@0 2058 // Compile_lock prevents systemDictionary updates during compilations
aoqi@0 2059 assert_locked_or_safepoint(Compile_lock);
aoqi@0 2060 Symbol* name = k->name();
aoqi@0 2061 ClassLoaderData *loader_data = class_loader_data(class_loader);
aoqi@0 2062
aoqi@0 2063 {
aoqi@0 2064 MutexLocker mu1(SystemDictionary_lock, THREAD);
aoqi@0 2065
aoqi@0 2066 // See whether biased locking is enabled and if so set it for this
aoqi@0 2067 // klass.
aoqi@0 2068 // Note that this must be done past the last potential blocking
aoqi@0 2069 // point / safepoint. We enable biased locking lazily using a
aoqi@0 2070 // VM_Operation to iterate the SystemDictionary and installing the
aoqi@0 2071 // biasable mark word into each InstanceKlass's prototype header.
aoqi@0 2072 // To avoid race conditions where we accidentally miss enabling the
aoqi@0 2073 // optimization for one class in the process of being added to the
aoqi@0 2074 // dictionary, we must not safepoint after the test of
aoqi@0 2075 // BiasedLocking::enabled().
aoqi@0 2076 if (UseBiasedLocking && BiasedLocking::enabled()) {
aoqi@0 2077 // Set biased locking bit for all loaded classes; it will be
aoqi@0 2078 // cleared if revocation occurs too often for this type
aoqi@0 2079 // NOTE that we must only do this when the class is initally
aoqi@0 2080 // defined, not each time it is referenced from a new class loader
aoqi@0 2081 if (k->class_loader() == class_loader()) {
aoqi@0 2082 k->set_prototype_header(markOopDesc::biased_locking_prototype());
aoqi@0 2083 }
aoqi@0 2084 }
aoqi@0 2085
aoqi@0 2086 // Make a new system dictionary entry.
aoqi@0 2087 Klass* sd_check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 2088 if (sd_check == NULL) {
aoqi@0 2089 dictionary()->add_klass(name, loader_data, k);
aoqi@0 2090 notice_modification();
aoqi@0 2091 }
aoqi@0 2092 #ifdef ASSERT
aoqi@0 2093 sd_check = find_class(d_index, d_hash, name, loader_data);
aoqi@0 2094 assert (sd_check != NULL, "should have entry in system dictionary");
aoqi@0 2095 // Note: there may be a placeholder entry: for circularity testing
aoqi@0 2096 // or for parallel defines
aoqi@0 2097 #endif
aoqi@0 2098 SystemDictionary_lock->notify_all();
aoqi@0 2099 }
aoqi@0 2100 }
aoqi@0 2101
aoqi@0 2102
aoqi@0 2103 // Try to find a class name using the loader constraints. The
aoqi@0 2104 // loader constraints might know about a class that isn't fully loaded
aoqi@0 2105 // yet and these will be ignored.
aoqi@0 2106 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
aoqi@0 2107 Symbol* class_name, Handle class_loader, TRAPS) {
aoqi@0 2108
aoqi@0 2109 // First see if it has been loaded directly.
aoqi@0 2110 // Force the protection domain to be null. (This removes protection checks.)
aoqi@0 2111 Handle no_protection_domain;
aoqi@0 2112 Klass* klass = find_instance_or_array_klass(class_name, class_loader,
aoqi@0 2113 no_protection_domain, CHECK_NULL);
aoqi@0 2114 if (klass != NULL)
aoqi@0 2115 return klass;
aoqi@0 2116
aoqi@0 2117 // Now look to see if it has been loaded elsewhere, and is subject to
aoqi@0 2118 // a loader constraint that would require this loader to return the
aoqi@0 2119 // klass that is already loaded.
aoqi@0 2120 if (FieldType::is_array(class_name)) {
aoqi@0 2121 // For array classes, their Klass*s are not kept in the
aoqi@0 2122 // constraint table. The element Klass*s are.
aoqi@0 2123 FieldArrayInfo fd;
aoqi@0 2124 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
aoqi@0 2125 if (t != T_OBJECT) {
aoqi@0 2126 klass = Universe::typeArrayKlassObj(t);
aoqi@0 2127 } else {
aoqi@0 2128 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 2129 klass = constraints()->find_constrained_klass(fd.object_key(), class_loader);
aoqi@0 2130 }
aoqi@0 2131 // If element class already loaded, allocate array klass
aoqi@0 2132 if (klass != NULL) {
aoqi@0 2133 klass = klass->array_klass_or_null(fd.dimension());
aoqi@0 2134 }
aoqi@0 2135 } else {
aoqi@0 2136 MutexLocker mu(SystemDictionary_lock, THREAD);
aoqi@0 2137 // Non-array classes are easy: simply check the constraint table.
aoqi@0 2138 klass = constraints()->find_constrained_klass(class_name, class_loader);
aoqi@0 2139 }
aoqi@0 2140
aoqi@0 2141 return klass;
aoqi@0 2142 }
aoqi@0 2143
aoqi@0 2144
aoqi@0 2145 bool SystemDictionary::add_loader_constraint(Symbol* class_name,
aoqi@0 2146 Handle class_loader1,
aoqi@0 2147 Handle class_loader2,
aoqi@0 2148 Thread* THREAD) {
aoqi@0 2149 ClassLoaderData* loader_data1 = class_loader_data(class_loader1);
aoqi@0 2150 ClassLoaderData* loader_data2 = class_loader_data(class_loader2);
aoqi@0 2151
aoqi@0 2152 Symbol* constraint_name = NULL;
aoqi@0 2153 if (!FieldType::is_array(class_name)) {
aoqi@0 2154 constraint_name = class_name;
aoqi@0 2155 } else {
aoqi@0 2156 // For array classes, their Klass*s are not kept in the
aoqi@0 2157 // constraint table. The element classes are.
aoqi@0 2158 FieldArrayInfo fd;
aoqi@0 2159 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
aoqi@0 2160 // primitive types always pass
aoqi@0 2161 if (t != T_OBJECT) {
aoqi@0 2162 return true;
aoqi@0 2163 } else {
aoqi@0 2164 constraint_name = fd.object_key();
aoqi@0 2165 }
aoqi@0 2166 }
aoqi@0 2167 unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, loader_data1);
aoqi@0 2168 int d_index1 = dictionary()->hash_to_index(d_hash1);
aoqi@0 2169
aoqi@0 2170 unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, loader_data2);
aoqi@0 2171 int d_index2 = dictionary()->hash_to_index(d_hash2);
aoqi@0 2172 {
aoqi@0 2173 MutexLocker mu_s(SystemDictionary_lock, THREAD);
aoqi@0 2174
aoqi@0 2175 // Better never do a GC while we're holding these oops
aoqi@0 2176 No_Safepoint_Verifier nosafepoint;
aoqi@0 2177
aoqi@0 2178 Klass* klass1 = find_class(d_index1, d_hash1, constraint_name, loader_data1);
aoqi@0 2179 Klass* klass2 = find_class(d_index2, d_hash2, constraint_name, loader_data2);
aoqi@0 2180 return constraints()->add_entry(constraint_name, klass1, class_loader1,
aoqi@0 2181 klass2, class_loader2);
aoqi@0 2182 }
aoqi@0 2183 }
aoqi@0 2184
aoqi@0 2185 // Add entry to resolution error table to record the error when the first
aoqi@0 2186 // attempt to resolve a reference to a class has failed.
aoqi@0 2187 void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which, Symbol* error) {
aoqi@0 2188 unsigned int hash = resolution_errors()->compute_hash(pool, which);
aoqi@0 2189 int index = resolution_errors()->hash_to_index(hash);
aoqi@0 2190 {
aoqi@0 2191 MutexLocker ml(SystemDictionary_lock, Thread::current());
aoqi@0 2192 resolution_errors()->add_entry(index, hash, pool, which, error);
aoqi@0 2193 }
aoqi@0 2194 }
aoqi@0 2195
aoqi@0 2196 // Delete a resolution error for RedefineClasses for a constant pool is going away
aoqi@0 2197 void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
aoqi@0 2198 resolution_errors()->delete_entry(pool);
aoqi@0 2199 }
aoqi@0 2200
aoqi@0 2201 // Lookup resolution error table. Returns error if found, otherwise NULL.
aoqi@0 2202 Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
aoqi@0 2203 unsigned int hash = resolution_errors()->compute_hash(pool, which);
aoqi@0 2204 int index = resolution_errors()->hash_to_index(hash);
aoqi@0 2205 {
aoqi@0 2206 MutexLocker ml(SystemDictionary_lock, Thread::current());
aoqi@0 2207 ResolutionErrorEntry* entry = resolution_errors()->find_entry(index, hash, pool, which);
aoqi@0 2208 return (entry != NULL) ? entry->error() : (Symbol*)NULL;
aoqi@0 2209 }
aoqi@0 2210 }
aoqi@0 2211
aoqi@0 2212
aoqi@0 2213 // Signature constraints ensure that callers and callees agree about
aoqi@0 2214 // the meaning of type names in their signatures. This routine is the
aoqi@0 2215 // intake for constraints. It collects them from several places:
aoqi@0 2216 //
aoqi@0 2217 // * LinkResolver::resolve_method (if check_access is true) requires
aoqi@0 2218 // that the resolving class (the caller) and the defining class of
aoqi@0 2219 // the resolved method (the callee) agree on each type in the
aoqi@0 2220 // method's signature.
aoqi@0 2221 //
aoqi@0 2222 // * LinkResolver::resolve_interface_method performs exactly the same
aoqi@0 2223 // checks.
aoqi@0 2224 //
aoqi@0 2225 // * LinkResolver::resolve_field requires that the constant pool
aoqi@0 2226 // attempting to link to a field agree with the field's defining
aoqi@0 2227 // class about the type of the field signature.
aoqi@0 2228 //
aoqi@0 2229 // * klassVtable::initialize_vtable requires that, when a class
aoqi@0 2230 // overrides a vtable entry allocated by a superclass, that the
aoqi@0 2231 // overriding method (i.e., the callee) agree with the superclass
aoqi@0 2232 // on each type in the method's signature.
aoqi@0 2233 //
aoqi@0 2234 // * klassItable::initialize_itable requires that, when a class fills
aoqi@0 2235 // in its itables, for each non-abstract method installed in an
aoqi@0 2236 // itable, the method (i.e., the callee) agree with the interface
aoqi@0 2237 // on each type in the method's signature.
aoqi@0 2238 //
aoqi@0 2239 // All those methods have a boolean (check_access, checkconstraints)
aoqi@0 2240 // which turns off the checks. This is used from specialized contexts
aoqi@0 2241 // such as bootstrapping, dumping, and debugging.
aoqi@0 2242 //
aoqi@0 2243 // No direct constraint is placed between the class and its
aoqi@0 2244 // supertypes. Constraints are only placed along linked relations
aoqi@0 2245 // between callers and callees. When a method overrides or implements
aoqi@0 2246 // an abstract method in a supertype (superclass or interface), the
aoqi@0 2247 // constraints are placed as if the supertype were the caller to the
aoqi@0 2248 // overriding method. (This works well, since callers to the
aoqi@0 2249 // supertype have already established agreement between themselves and
aoqi@0 2250 // the supertype.) As a result of all this, a class can disagree with
aoqi@0 2251 // its supertype about the meaning of a type name, as long as that
aoqi@0 2252 // class neither calls a relevant method of the supertype, nor is
aoqi@0 2253 // called (perhaps via an override) from the supertype.
aoqi@0 2254 //
aoqi@0 2255 //
aoqi@0 2256 // SystemDictionary::check_signature_loaders(sig, l1, l2)
aoqi@0 2257 //
aoqi@0 2258 // Make sure all class components (including arrays) in the given
aoqi@0 2259 // signature will be resolved to the same class in both loaders.
aoqi@0 2260 // Returns the name of the type that failed a loader constraint check, or
aoqi@0 2261 // NULL if no constraint failed. No exception except OOME is thrown.
aoqi@0 2262 // Arrays are not added to the loader constraint table, their elements are.
aoqi@0 2263 Symbol* SystemDictionary::check_signature_loaders(Symbol* signature,
aoqi@0 2264 Handle loader1, Handle loader2,
aoqi@0 2265 bool is_method, TRAPS) {
aoqi@0 2266 // Nothing to do if loaders are the same.
aoqi@0 2267 if (loader1() == loader2()) {
aoqi@0 2268 return NULL;
aoqi@0 2269 }
aoqi@0 2270
aoqi@0 2271 SignatureStream sig_strm(signature, is_method);
aoqi@0 2272 while (!sig_strm.is_done()) {
aoqi@0 2273 if (sig_strm.is_object()) {
aoqi@0 2274 Symbol* sig = sig_strm.as_symbol(CHECK_NULL);
aoqi@0 2275 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) {
aoqi@0 2276 return sig;
aoqi@0 2277 }
aoqi@0 2278 }
aoqi@0 2279 sig_strm.next();
aoqi@0 2280 }
aoqi@0 2281 return NULL;
aoqi@0 2282 }
aoqi@0 2283
aoqi@0 2284
aoqi@0 2285 methodHandle SystemDictionary::find_method_handle_intrinsic(vmIntrinsics::ID iid,
aoqi@0 2286 Symbol* signature,
aoqi@0 2287 TRAPS) {
aoqi@0 2288 methodHandle empty;
aoqi@0 2289 assert(EnableInvokeDynamic, "");
aoqi@0 2290 assert(MethodHandles::is_signature_polymorphic(iid) &&
aoqi@0 2291 MethodHandles::is_signature_polymorphic_intrinsic(iid) &&
aoqi@0 2292 iid != vmIntrinsics::_invokeGeneric,
aoqi@0 2293 err_msg("must be a known MH intrinsic iid=%d: %s", iid, vmIntrinsics::name_at(iid)));
aoqi@0 2294
aoqi@0 2295 unsigned int hash = invoke_method_table()->compute_hash(signature, iid);
aoqi@0 2296 int index = invoke_method_table()->hash_to_index(hash);
aoqi@0 2297 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid);
aoqi@0 2298 methodHandle m;
aoqi@0 2299 if (spe == NULL || spe->method() == NULL) {
aoqi@0 2300 spe = NULL;
aoqi@0 2301 // Must create lots of stuff here, but outside of the SystemDictionary lock.
aoqi@0 2302 m = Method::make_method_handle_intrinsic(iid, signature, CHECK_(empty));
iveresov@7216 2303 if (!Arguments::is_interpreter_only()) {
iveresov@7216 2304 // Generate a compiled form of the MH intrinsic.
iveresov@7216 2305 AdapterHandlerLibrary::create_native_wrapper(m);
iveresov@7216 2306 // Check if have the compiled code.
iveresov@7216 2307 if (!m->has_compiled_code()) {
iveresov@7216 2308 THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(),
iveresov@7216 2309 "out of space in CodeCache for method handle intrinsic", empty);
iveresov@7216 2310 }
iveresov@7172 2311 }
aoqi@0 2312 // Now grab the lock. We might have to throw away the new method,
aoqi@0 2313 // if a racing thread has managed to install one at the same time.
aoqi@0 2314 {
aoqi@0 2315 MutexLocker ml(SystemDictionary_lock, THREAD);
aoqi@0 2316 spe = invoke_method_table()->find_entry(index, hash, signature, iid);
aoqi@0 2317 if (spe == NULL)
aoqi@0 2318 spe = invoke_method_table()->add_entry(index, hash, signature, iid);
aoqi@0 2319 if (spe->method() == NULL)
aoqi@0 2320 spe->set_method(m());
aoqi@0 2321 }
aoqi@0 2322 }
aoqi@0 2323
aoqi@0 2324 assert(spe != NULL && spe->method() != NULL, "");
iveresov@7172 2325 assert(Arguments::is_interpreter_only() || (spe->method()->has_compiled_code() &&
iveresov@7146 2326 spe->method()->code()->entry_point() == spe->method()->from_compiled_entry()),
iveresov@7146 2327 "MH intrinsic invariant");
aoqi@0 2328 return spe->method();
aoqi@0 2329 }
aoqi@0 2330
aoqi@0 2331 // Helper for unpacking the return value from linkMethod and linkCallSite.
aoqi@0 2332 static methodHandle unpack_method_and_appendix(Handle mname,
aoqi@0 2333 KlassHandle accessing_klass,
aoqi@0 2334 objArrayHandle appendix_box,
aoqi@0 2335 Handle* appendix_result,
aoqi@0 2336 TRAPS) {
aoqi@0 2337 methodHandle empty;
aoqi@0 2338 if (mname.not_null()) {
aoqi@0 2339 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
aoqi@0 2340 if (vmtarget != NULL && vmtarget->is_method()) {
aoqi@0 2341 Method* m = (Method*)vmtarget;
aoqi@0 2342 oop appendix = appendix_box->obj_at(0);
aoqi@0 2343 if (TraceMethodHandles) {
aoqi@0 2344 #ifndef PRODUCT
aoqi@0 2345 tty->print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
aoqi@0 2346 m->print();
aoqi@0 2347 if (appendix != NULL) { tty->print("appendix = "); appendix->print(); }
aoqi@0 2348 tty->cr();
aoqi@0 2349 #endif //PRODUCT
aoqi@0 2350 }
aoqi@0 2351 (*appendix_result) = Handle(THREAD, appendix);
aoqi@0 2352 // the target is stored in the cpCache and if a reference to this
aoqi@0 2353 // MethodName is dropped we need a way to make sure the
aoqi@0 2354 // class_loader containing this method is kept alive.
aoqi@0 2355 // FIXME: the appendix might also preserve this dependency.
aoqi@0 2356 ClassLoaderData* this_key = InstanceKlass::cast(accessing_klass())->class_loader_data();
aoqi@0 2357 this_key->record_dependency(m->method_holder(), CHECK_NULL); // Can throw OOM
aoqi@0 2358 return methodHandle(THREAD, m);
aoqi@0 2359 }
aoqi@0 2360 }
aoqi@0 2361 THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives", empty);
aoqi@0 2362 return empty;
aoqi@0 2363 }
aoqi@0 2364
aoqi@0 2365 methodHandle SystemDictionary::find_method_handle_invoker(Symbol* name,
aoqi@0 2366 Symbol* signature,
aoqi@0 2367 KlassHandle accessing_klass,
aoqi@0 2368 Handle *appendix_result,
aoqi@0 2369 Handle *method_type_result,
aoqi@0 2370 TRAPS) {
aoqi@0 2371 methodHandle empty;
aoqi@0 2372 assert(EnableInvokeDynamic, "");
aoqi@0 2373 assert(!THREAD->is_Compiler_thread(), "");
aoqi@0 2374 Handle method_type =
aoqi@0 2375 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty));
aoqi@0 2376
aoqi@0 2377 KlassHandle mh_klass = SystemDictionary::MethodHandle_klass();
aoqi@0 2378 int ref_kind = JVM_REF_invokeVirtual;
aoqi@0 2379 Handle name_str = StringTable::intern(name, CHECK_(empty));
aoqi@0 2380 objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty));
aoqi@0 2381 assert(appendix_box->obj_at(0) == NULL, "");
aoqi@0 2382
aoqi@0 2383 // This should not happen. JDK code should take care of that.
aoqi@0 2384 if (accessing_klass.is_null() || method_type.is_null()) {
aoqi@0 2385 THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad invokehandle", empty);
aoqi@0 2386 }
aoqi@0 2387
aoqi@0 2388 // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName
aoqi@0 2389 JavaCallArguments args;
aoqi@0 2390 args.push_oop(accessing_klass()->java_mirror());
aoqi@0 2391 args.push_int(ref_kind);
aoqi@0 2392 args.push_oop(mh_klass()->java_mirror());
aoqi@0 2393 args.push_oop(name_str());
aoqi@0 2394 args.push_oop(method_type());
aoqi@0 2395 args.push_oop(appendix_box());
aoqi@0 2396 JavaValue result(T_OBJECT);
aoqi@0 2397 JavaCalls::call_static(&result,
aoqi@0 2398 SystemDictionary::MethodHandleNatives_klass(),
aoqi@0 2399 vmSymbols::linkMethod_name(),
aoqi@0 2400 vmSymbols::linkMethod_signature(),
aoqi@0 2401 &args, CHECK_(empty));
aoqi@0 2402 Handle mname(THREAD, (oop) result.get_jobject());
aoqi@0 2403 (*method_type_result) = method_type;
aoqi@0 2404 return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD);
aoqi@0 2405 }
aoqi@0 2406
vlivanov@7936 2407 // Decide if we can globally cache a lookup of this class, to be returned to any client that asks.
vlivanov@7936 2408 // We must ensure that all class loaders everywhere will reach this class, for any client.
vlivanov@7936 2409 // This is a safe bet for public classes in java.lang, such as Object and String.
vlivanov@7936 2410 // We also include public classes in java.lang.invoke, because they appear frequently in system-level method types.
vlivanov@7936 2411 // Out of an abundance of caution, we do not include any other classes, not even for packages like java.util.
vlivanov@7936 2412 static bool is_always_visible_class(oop mirror) {
vlivanov@7936 2413 Klass* klass = java_lang_Class::as_Klass(mirror);
vlivanov@7936 2414 if (klass->oop_is_objArray()) {
vlivanov@7936 2415 klass = ObjArrayKlass::cast(klass)->bottom_klass(); // check element type
vlivanov@7936 2416 }
vlivanov@7936 2417 if (klass->oop_is_typeArray()) {
vlivanov@7936 2418 return true; // primitive array
vlivanov@7936 2419 }
vlivanov@7936 2420 assert(klass->oop_is_instance(), klass->external_name());
vlivanov@7936 2421 return klass->is_public() &&
vlivanov@7936 2422 (InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::Object_klass()) || // java.lang
vlivanov@7936 2423 InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::MethodHandle_klass())); // java.lang.invoke
vlivanov@7936 2424 }
aoqi@0 2425
aoqi@0 2426 // Ask Java code to find or construct a java.lang.invoke.MethodType for the given
aoqi@0 2427 // signature, as interpreted relative to the given class loader.
aoqi@0 2428 // Because of class loader constraints, all method handle usage must be
aoqi@0 2429 // consistent with this loader.
aoqi@0 2430 Handle SystemDictionary::find_method_handle_type(Symbol* signature,
aoqi@0 2431 KlassHandle accessing_klass,
aoqi@0 2432 TRAPS) {
aoqi@0 2433 Handle empty;
aoqi@0 2434 vmIntrinsics::ID null_iid = vmIntrinsics::_none; // distinct from all method handle invoker intrinsics
aoqi@0 2435 unsigned int hash = invoke_method_table()->compute_hash(signature, null_iid);
aoqi@0 2436 int index = invoke_method_table()->hash_to_index(hash);
aoqi@0 2437 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
aoqi@0 2438 if (spe != NULL && spe->method_type() != NULL) {
aoqi@0 2439 assert(java_lang_invoke_MethodType::is_instance(spe->method_type()), "");
aoqi@0 2440 return Handle(THREAD, spe->method_type());
aoqi@0 2441 } else if (THREAD->is_Compiler_thread()) {
aoqi@0 2442 warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME
aoqi@0 2443 return Handle(); // do not attempt from within compiler, unless it was cached
aoqi@0 2444 }
aoqi@0 2445
aoqi@0 2446 Handle class_loader, protection_domain;
vlivanov@7936 2447 if (accessing_klass.not_null()) {
vlivanov@7936 2448 class_loader = Handle(THREAD, InstanceKlass::cast(accessing_klass())->class_loader());
vlivanov@7936 2449 protection_domain = Handle(THREAD, InstanceKlass::cast(accessing_klass())->protection_domain());
vlivanov@7936 2450 }
vlivanov@7936 2451 bool can_be_cached = true;
aoqi@0 2452 int npts = ArgumentCount(signature).size();
aoqi@0 2453 objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty));
aoqi@0 2454 int arg = 0;
vlivanov@7936 2455 Handle rt; // the return type from the signature
aoqi@0 2456 ResourceMark rm(THREAD);
aoqi@0 2457 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) {
aoqi@0 2458 oop mirror = NULL;
vlivanov@7936 2459 if (can_be_cached) {
vlivanov@7936 2460 // Use neutral class loader to lookup candidate classes to be placed in the cache.
vlivanov@7936 2461 mirror = ss.as_java_mirror(Handle(), Handle(),
aoqi@0 2462 SignatureStream::ReturnNull, CHECK_(empty));
vlivanov@7936 2463 if (mirror == NULL || (ss.is_object() && !is_always_visible_class(mirror))) {
vlivanov@7936 2464 // Fall back to accessing_klass context.
vlivanov@7936 2465 can_be_cached = false;
aoqi@0 2466 }
aoqi@0 2467 }
vlivanov@7936 2468 if (!can_be_cached) {
aoqi@0 2469 // Resolve, throwing a real error if it doesn't work.
aoqi@0 2470 mirror = ss.as_java_mirror(class_loader, protection_domain,
aoqi@0 2471 SignatureStream::NCDFError, CHECK_(empty));
aoqi@0 2472 }
vlivanov@7936 2473 assert(!oopDesc::is_null(mirror), ss.as_symbol(THREAD)->as_C_string());
aoqi@0 2474 if (ss.at_return_type())
aoqi@0 2475 rt = Handle(THREAD, mirror);
aoqi@0 2476 else
aoqi@0 2477 pts->obj_at_put(arg++, mirror);
aoqi@0 2478
aoqi@0 2479 // Check accessibility.
aoqi@0 2480 if (ss.is_object() && accessing_klass.not_null()) {
aoqi@0 2481 Klass* sel_klass = java_lang_Class::as_Klass(mirror);
aoqi@0 2482 mirror = NULL; // safety
aoqi@0 2483 // Emulate ConstantPool::verify_constant_pool_resolve.
aoqi@0 2484 if (sel_klass->oop_is_objArray())
aoqi@0 2485 sel_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
aoqi@0 2486 if (sel_klass->oop_is_instance()) {
aoqi@0 2487 KlassHandle sel_kh(THREAD, sel_klass);
aoqi@0 2488 LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty));
aoqi@0 2489 }
aoqi@0 2490 }
aoqi@0 2491 }
aoqi@0 2492 assert(arg == npts, "");
aoqi@0 2493
aoqi@0 2494 // call java.lang.invoke.MethodHandleNatives::findMethodType(Class rt, Class[] pts) -> MethodType
aoqi@0 2495 JavaCallArguments args(Handle(THREAD, rt()));
aoqi@0 2496 args.push_oop(pts());
aoqi@0 2497 JavaValue result(T_OBJECT);
aoqi@0 2498 JavaCalls::call_static(&result,
aoqi@0 2499 SystemDictionary::MethodHandleNatives_klass(),
aoqi@0 2500 vmSymbols::findMethodHandleType_name(),
aoqi@0 2501 vmSymbols::findMethodHandleType_signature(),
aoqi@0 2502 &args, CHECK_(empty));
aoqi@0 2503 Handle method_type(THREAD, (oop) result.get_jobject());
aoqi@0 2504
vlivanov@7936 2505 if (can_be_cached) {
aoqi@0 2506 // We can cache this MethodType inside the JVM.
aoqi@0 2507 MutexLocker ml(SystemDictionary_lock, THREAD);
aoqi@0 2508 spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
aoqi@0 2509 if (spe == NULL)
aoqi@0 2510 spe = invoke_method_table()->add_entry(index, hash, signature, null_iid);
aoqi@0 2511 if (spe->method_type() == NULL) {
aoqi@0 2512 spe->set_method_type(method_type());
aoqi@0 2513 }
aoqi@0 2514 }
aoqi@0 2515
aoqi@0 2516 // report back to the caller with the MethodType
aoqi@0 2517 return method_type;
aoqi@0 2518 }
aoqi@0 2519
aoqi@0 2520 // Ask Java code to find or construct a method handle constant.
aoqi@0 2521 Handle SystemDictionary::link_method_handle_constant(KlassHandle caller,
aoqi@0 2522 int ref_kind, //e.g., JVM_REF_invokeVirtual
aoqi@0 2523 KlassHandle callee,
aoqi@0 2524 Symbol* name_sym,
aoqi@0 2525 Symbol* signature,
aoqi@0 2526 TRAPS) {
aoqi@0 2527 Handle empty;
aoqi@0 2528 Handle name = java_lang_String::create_from_symbol(name_sym, CHECK_(empty));
aoqi@0 2529 Handle type;
aoqi@0 2530 if (signature->utf8_length() > 0 && signature->byte_at(0) == '(') {
aoqi@0 2531 type = find_method_handle_type(signature, caller, CHECK_(empty));
aoqi@0 2532 } else if (caller.is_null()) {
aoqi@0 2533 // This should not happen. JDK code should take care of that.
aoqi@0 2534 THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MH constant", empty);
aoqi@0 2535 } else {
aoqi@0 2536 ResourceMark rm(THREAD);
aoqi@0 2537 SignatureStream ss(signature, false);
aoqi@0 2538 if (!ss.is_done()) {
aoqi@0 2539 oop mirror = ss.as_java_mirror(caller->class_loader(), caller->protection_domain(),
aoqi@0 2540 SignatureStream::NCDFError, CHECK_(empty));
aoqi@0 2541 type = Handle(THREAD, mirror);
aoqi@0 2542 ss.next();
aoqi@0 2543 if (!ss.is_done()) type = Handle(); // error!
aoqi@0 2544 }
aoqi@0 2545 }
aoqi@0 2546 if (type.is_null()) {
aoqi@0 2547 THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad signature", empty);
aoqi@0 2548 }
aoqi@0 2549
aoqi@0 2550 // call java.lang.invoke.MethodHandleNatives::linkMethodHandleConstant(Class caller, int refKind, Class callee, String name, Object type) -> MethodHandle
aoqi@0 2551 JavaCallArguments args;
aoqi@0 2552 args.push_oop(caller->java_mirror()); // the referring class
aoqi@0 2553 args.push_int(ref_kind);
aoqi@0 2554 args.push_oop(callee->java_mirror()); // the target class
aoqi@0 2555 args.push_oop(name());
aoqi@0 2556 args.push_oop(type());
aoqi@0 2557 JavaValue result(T_OBJECT);
aoqi@0 2558 JavaCalls::call_static(&result,
aoqi@0 2559 SystemDictionary::MethodHandleNatives_klass(),
aoqi@0 2560 vmSymbols::linkMethodHandleConstant_name(),
aoqi@0 2561 vmSymbols::linkMethodHandleConstant_signature(),
aoqi@0 2562 &args, CHECK_(empty));
aoqi@0 2563 return Handle(THREAD, (oop) result.get_jobject());
aoqi@0 2564 }
aoqi@0 2565
aoqi@0 2566 // Ask Java code to find or construct a java.lang.invoke.CallSite for the given
aoqi@0 2567 // name and signature, as interpreted relative to the given class loader.
aoqi@0 2568 methodHandle SystemDictionary::find_dynamic_call_site_invoker(KlassHandle caller,
aoqi@0 2569 Handle bootstrap_specifier,
aoqi@0 2570 Symbol* name,
aoqi@0 2571 Symbol* type,
aoqi@0 2572 Handle *appendix_result,
aoqi@0 2573 Handle *method_type_result,
aoqi@0 2574 TRAPS) {
aoqi@0 2575 methodHandle empty;
aoqi@0 2576 Handle bsm, info;
aoqi@0 2577 if (java_lang_invoke_MethodHandle::is_instance(bootstrap_specifier())) {
aoqi@0 2578 bsm = bootstrap_specifier;
aoqi@0 2579 } else {
aoqi@0 2580 assert(bootstrap_specifier->is_objArray(), "");
aoqi@0 2581 objArrayHandle args(THREAD, (objArrayOop) bootstrap_specifier());
aoqi@0 2582 int len = args->length();
aoqi@0 2583 assert(len >= 1, "");
aoqi@0 2584 bsm = Handle(THREAD, args->obj_at(0));
aoqi@0 2585 if (len > 1) {
aoqi@0 2586 objArrayOop args1 = oopFactory::new_objArray(SystemDictionary::Object_klass(), len-1, CHECK_(empty));
aoqi@0 2587 for (int i = 1; i < len; i++)
aoqi@0 2588 args1->obj_at_put(i-1, args->obj_at(i));
aoqi@0 2589 info = Handle(THREAD, args1);
aoqi@0 2590 }
aoqi@0 2591 }
aoqi@0 2592 guarantee(java_lang_invoke_MethodHandle::is_instance(bsm()),
aoqi@0 2593 "caller must supply a valid BSM");
aoqi@0 2594
aoqi@0 2595 Handle method_name = java_lang_String::create_from_symbol(name, CHECK_(empty));
aoqi@0 2596 Handle method_type = find_method_handle_type(type, caller, CHECK_(empty));
aoqi@0 2597
aoqi@0 2598 // This should not happen. JDK code should take care of that.
aoqi@0 2599 if (caller.is_null() || method_type.is_null()) {
aoqi@0 2600 THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad invokedynamic", empty);
aoqi@0 2601 }
aoqi@0 2602
aoqi@0 2603 objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty));
aoqi@0 2604 assert(appendix_box->obj_at(0) == NULL, "");
aoqi@0 2605
aoqi@0 2606 // call java.lang.invoke.MethodHandleNatives::linkCallSite(caller, bsm, name, mtype, info, &appendix)
aoqi@0 2607 JavaCallArguments args;
aoqi@0 2608 args.push_oop(caller->java_mirror());
aoqi@0 2609 args.push_oop(bsm());
aoqi@0 2610 args.push_oop(method_name());
aoqi@0 2611 args.push_oop(method_type());
aoqi@0 2612 args.push_oop(info());
aoqi@0 2613 args.push_oop(appendix_box);
aoqi@0 2614 JavaValue result(T_OBJECT);
aoqi@0 2615 JavaCalls::call_static(&result,
aoqi@0 2616 SystemDictionary::MethodHandleNatives_klass(),
aoqi@0 2617 vmSymbols::linkCallSite_name(),
aoqi@0 2618 vmSymbols::linkCallSite_signature(),
aoqi@0 2619 &args, CHECK_(empty));
aoqi@0 2620 Handle mname(THREAD, (oop) result.get_jobject());
aoqi@0 2621 (*method_type_result) = method_type;
aoqi@0 2622 return unpack_method_and_appendix(mname, caller, appendix_box, appendix_result, THREAD);
aoqi@0 2623 }
aoqi@0 2624
aoqi@0 2625 // Since the identity hash code for symbols changes when the symbols are
aoqi@0 2626 // moved from the regular perm gen (hash in the mark word) to the shared
aoqi@0 2627 // spaces (hash is the address), the classes loaded into the dictionary
aoqi@0 2628 // may be in the wrong buckets.
aoqi@0 2629
aoqi@0 2630 void SystemDictionary::reorder_dictionary() {
aoqi@0 2631 dictionary()->reorder_dictionary();
aoqi@0 2632 }
aoqi@0 2633
aoqi@0 2634
aoqi@0 2635 void SystemDictionary::copy_buckets(char** top, char* end) {
aoqi@0 2636 dictionary()->copy_buckets(top, end);
aoqi@0 2637 }
aoqi@0 2638
aoqi@0 2639
aoqi@0 2640 void SystemDictionary::copy_table(char** top, char* end) {
aoqi@0 2641 dictionary()->copy_table(top, end);
aoqi@0 2642 }
aoqi@0 2643
aoqi@0 2644
aoqi@0 2645 void SystemDictionary::reverse() {
aoqi@0 2646 dictionary()->reverse();
aoqi@0 2647 }
aoqi@0 2648
aoqi@0 2649 int SystemDictionary::number_of_classes() {
aoqi@0 2650 return dictionary()->number_of_entries();
aoqi@0 2651 }
aoqi@0 2652
aoqi@0 2653
aoqi@0 2654 // ----------------------------------------------------------------------------
iklam@7089 2655 void SystemDictionary::print_shared(bool details) {
iklam@7089 2656 shared_dictionary()->print(details);
iklam@7089 2657 }
aoqi@0 2658
iklam@7089 2659 void SystemDictionary::print(bool details) {
iklam@7089 2660 dictionary()->print(details);
aoqi@0 2661
aoqi@0 2662 // Placeholders
aoqi@0 2663 GCMutexLocker mu(SystemDictionary_lock);
aoqi@0 2664 placeholders()->print();
aoqi@0 2665
aoqi@0 2666 // loader constraints - print under SD_lock
aoqi@0 2667 constraints()->print();
aoqi@0 2668 }
aoqi@0 2669
aoqi@0 2670
aoqi@0 2671 void SystemDictionary::verify() {
aoqi@0 2672 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
aoqi@0 2673 guarantee(constraints() != NULL,
aoqi@0 2674 "Verify of loader constraints failed");
aoqi@0 2675 guarantee(dictionary()->number_of_entries() >= 0 &&
aoqi@0 2676 placeholders()->number_of_entries() >= 0,
aoqi@0 2677 "Verify of system dictionary failed");
aoqi@0 2678
aoqi@0 2679 // Verify dictionary
aoqi@0 2680 dictionary()->verify();
aoqi@0 2681
aoqi@0 2682 GCMutexLocker mu(SystemDictionary_lock);
aoqi@0 2683 placeholders()->verify();
aoqi@0 2684
aoqi@0 2685 // Verify constraint table
aoqi@0 2686 guarantee(constraints() != NULL, "Verify of loader constraints failed");
aoqi@0 2687 constraints()->verify(dictionary(), placeholders());
aoqi@0 2688 }
aoqi@0 2689
aoqi@0 2690 // utility function for class load event
aoqi@0 2691 void SystemDictionary::post_class_load_event(const Ticks& start_time,
aoqi@0 2692 instanceKlassHandle k,
aoqi@0 2693 Handle initiating_loader) {
aoqi@0 2694 #if INCLUDE_TRACE
aoqi@0 2695 EventClassLoad event(UNTIMED);
aoqi@0 2696 if (event.should_commit()) {
aoqi@0 2697 event.set_starttime(start_time);
aoqi@0 2698 event.set_loadedClass(k());
aoqi@0 2699 oop defining_class_loader = k->class_loader();
aoqi@0 2700 event.set_definingClassLoader(defining_class_loader != NULL ?
aoqi@0 2701 defining_class_loader->klass() : (Klass*)NULL);
aoqi@0 2702 oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
aoqi@0 2703 event.set_initiatingClassLoader(class_loader != NULL ?
aoqi@0 2704 class_loader->klass() : (Klass*)NULL);
aoqi@0 2705 event.commit();
aoqi@0 2706 }
mgronlun@7367 2707 #endif // INCLUDE_TRACE
aoqi@0 2708 }
aoqi@0 2709
aoqi@0 2710 #ifndef PRODUCT
aoqi@0 2711
aoqi@0 2712 // statistics code
aoqi@0 2713 class ClassStatistics: AllStatic {
aoqi@0 2714 private:
aoqi@0 2715 static int nclasses; // number of classes
aoqi@0 2716 static int nmethods; // number of methods
aoqi@0 2717 static int nmethoddata; // number of methodData
aoqi@0 2718 static int class_size; // size of class objects in words
aoqi@0 2719 static int method_size; // size of method objects in words
aoqi@0 2720 static int debug_size; // size of debug info in methods
aoqi@0 2721 static int methoddata_size; // size of methodData objects in words
aoqi@0 2722
aoqi@0 2723 static void do_class(Klass* k) {
aoqi@0 2724 nclasses++;
aoqi@0 2725 class_size += k->size();
aoqi@0 2726 if (k->oop_is_instance()) {
aoqi@0 2727 InstanceKlass* ik = (InstanceKlass*)k;
aoqi@0 2728 class_size += ik->methods()->size();
aoqi@0 2729 class_size += ik->constants()->size();
aoqi@0 2730 class_size += ik->local_interfaces()->size();
aoqi@0 2731 class_size += ik->transitive_interfaces()->size();
aoqi@0 2732 // We do not have to count implementors, since we only store one!
aoqi@0 2733 // SSS: How should these be accounted now that they have moved?
aoqi@0 2734 // class_size += ik->fields()->length();
aoqi@0 2735 }
aoqi@0 2736 }
aoqi@0 2737
aoqi@0 2738 static void do_method(Method* m) {
aoqi@0 2739 nmethods++;
aoqi@0 2740 method_size += m->size();
aoqi@0 2741 // class loader uses same objArray for empty vectors, so don't count these
aoqi@0 2742 if (m->has_stackmap_table()) {
aoqi@0 2743 method_size += m->stackmap_data()->size();
aoqi@0 2744 }
aoqi@0 2745
aoqi@0 2746 MethodData* mdo = m->method_data();
aoqi@0 2747 if (mdo != NULL) {
aoqi@0 2748 nmethoddata++;
aoqi@0 2749 methoddata_size += mdo->size();
aoqi@0 2750 }
aoqi@0 2751 }
aoqi@0 2752
aoqi@0 2753 public:
aoqi@0 2754 static void print() {
aoqi@0 2755 SystemDictionary::classes_do(do_class);
aoqi@0 2756 SystemDictionary::methods_do(do_method);
aoqi@0 2757 tty->print_cr("Class statistics:");
aoqi@0 2758 tty->print_cr("%d classes (%d bytes)", nclasses, class_size * oopSize);
aoqi@0 2759 tty->print_cr("%d methods (%d bytes = %d base + %d debug info)", nmethods,
aoqi@0 2760 (method_size + debug_size) * oopSize, method_size * oopSize, debug_size * oopSize);
aoqi@0 2761 tty->print_cr("%d methoddata (%d bytes)", nmethoddata, methoddata_size * oopSize);
aoqi@0 2762 }
aoqi@0 2763 };
aoqi@0 2764
aoqi@0 2765
aoqi@0 2766 int ClassStatistics::nclasses = 0;
aoqi@0 2767 int ClassStatistics::nmethods = 0;
aoqi@0 2768 int ClassStatistics::nmethoddata = 0;
aoqi@0 2769 int ClassStatistics::class_size = 0;
aoqi@0 2770 int ClassStatistics::method_size = 0;
aoqi@0 2771 int ClassStatistics::debug_size = 0;
aoqi@0 2772 int ClassStatistics::methoddata_size = 0;
aoqi@0 2773
aoqi@0 2774 void SystemDictionary::print_class_statistics() {
aoqi@0 2775 ResourceMark rm;
aoqi@0 2776 ClassStatistics::print();
aoqi@0 2777 }
aoqi@0 2778
aoqi@0 2779
aoqi@0 2780 class MethodStatistics: AllStatic {
aoqi@0 2781 public:
aoqi@0 2782 enum {
aoqi@0 2783 max_parameter_size = 10
aoqi@0 2784 };
aoqi@0 2785 private:
aoqi@0 2786
aoqi@0 2787 static int _number_of_methods;
aoqi@0 2788 static int _number_of_final_methods;
aoqi@0 2789 static int _number_of_static_methods;
aoqi@0 2790 static int _number_of_native_methods;
aoqi@0 2791 static int _number_of_synchronized_methods;
aoqi@0 2792 static int _number_of_profiled_methods;
aoqi@0 2793 static int _number_of_bytecodes;
aoqi@0 2794 static int _parameter_size_profile[max_parameter_size];
aoqi@0 2795 static int _bytecodes_profile[Bytecodes::number_of_java_codes];
aoqi@0 2796
aoqi@0 2797 static void initialize() {
aoqi@0 2798 _number_of_methods = 0;
aoqi@0 2799 _number_of_final_methods = 0;
aoqi@0 2800 _number_of_static_methods = 0;
aoqi@0 2801 _number_of_native_methods = 0;
aoqi@0 2802 _number_of_synchronized_methods = 0;
aoqi@0 2803 _number_of_profiled_methods = 0;
aoqi@0 2804 _number_of_bytecodes = 0;
aoqi@0 2805 for (int i = 0; i < max_parameter_size ; i++) _parameter_size_profile[i] = 0;
aoqi@0 2806 for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile [j] = 0;
aoqi@0 2807 };
aoqi@0 2808
aoqi@0 2809 static void do_method(Method* m) {
aoqi@0 2810 _number_of_methods++;
aoqi@0 2811 // collect flag info
aoqi@0 2812 if (m->is_final() ) _number_of_final_methods++;
aoqi@0 2813 if (m->is_static() ) _number_of_static_methods++;
aoqi@0 2814 if (m->is_native() ) _number_of_native_methods++;
aoqi@0 2815 if (m->is_synchronized()) _number_of_synchronized_methods++;
aoqi@0 2816 if (m->method_data() != NULL) _number_of_profiled_methods++;
aoqi@0 2817 // collect parameter size info (add one for receiver, if any)
aoqi@0 2818 _parameter_size_profile[MIN2(m->size_of_parameters() + (m->is_static() ? 0 : 1), max_parameter_size - 1)]++;
aoqi@0 2819 // collect bytecodes info
aoqi@0 2820 {
aoqi@0 2821 Thread *thread = Thread::current();
aoqi@0 2822 HandleMark hm(thread);
aoqi@0 2823 BytecodeStream s(methodHandle(thread, m));
aoqi@0 2824 Bytecodes::Code c;
aoqi@0 2825 while ((c = s.next()) >= 0) {
aoqi@0 2826 _number_of_bytecodes++;
aoqi@0 2827 _bytecodes_profile[c]++;
aoqi@0 2828 }
aoqi@0 2829 }
aoqi@0 2830 }
aoqi@0 2831
aoqi@0 2832 public:
aoqi@0 2833 static void print() {
aoqi@0 2834 initialize();
aoqi@0 2835 SystemDictionary::methods_do(do_method);
aoqi@0 2836 // generate output
aoqi@0 2837 tty->cr();
aoqi@0 2838 tty->print_cr("Method statistics (static):");
aoqi@0 2839 // flag distribution
aoqi@0 2840 tty->cr();
aoqi@0 2841 tty->print_cr("%6d final methods %6.1f%%", _number_of_final_methods , _number_of_final_methods * 100.0F / _number_of_methods);
aoqi@0 2842 tty->print_cr("%6d static methods %6.1f%%", _number_of_static_methods , _number_of_static_methods * 100.0F / _number_of_methods);
aoqi@0 2843 tty->print_cr("%6d native methods %6.1f%%", _number_of_native_methods , _number_of_native_methods * 100.0F / _number_of_methods);
aoqi@0 2844 tty->print_cr("%6d synchronized methods %6.1f%%", _number_of_synchronized_methods, _number_of_synchronized_methods * 100.0F / _number_of_methods);
aoqi@0 2845 tty->print_cr("%6d profiled methods %6.1f%%", _number_of_profiled_methods, _number_of_profiled_methods * 100.0F / _number_of_methods);
aoqi@0 2846 // parameter size profile
aoqi@0 2847 tty->cr();
aoqi@0 2848 { int tot = 0;
aoqi@0 2849 int avg = 0;
aoqi@0 2850 for (int i = 0; i < max_parameter_size; i++) {
aoqi@0 2851 int n = _parameter_size_profile[i];
aoqi@0 2852 tot += n;
aoqi@0 2853 avg += n*i;
aoqi@0 2854 tty->print_cr("parameter size = %1d: %6d methods %5.1f%%", i, n, n * 100.0F / _number_of_methods);
aoqi@0 2855 }
aoqi@0 2856 assert(tot == _number_of_methods, "should be the same");
aoqi@0 2857 tty->print_cr(" %6d methods 100.0%%", _number_of_methods);
aoqi@0 2858 tty->print_cr("(average parameter size = %3.1f including receiver, if any)", (float)avg / _number_of_methods);
aoqi@0 2859 }
aoqi@0 2860 // bytecodes profile
aoqi@0 2861 tty->cr();
aoqi@0 2862 { int tot = 0;
aoqi@0 2863 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
aoqi@0 2864 if (Bytecodes::is_defined(i)) {
aoqi@0 2865 Bytecodes::Code c = Bytecodes::cast(i);
aoqi@0 2866 int n = _bytecodes_profile[c];
aoqi@0 2867 tot += n;
aoqi@0 2868 tty->print_cr("%9d %7.3f%% %s", n, n * 100.0F / _number_of_bytecodes, Bytecodes::name(c));
aoqi@0 2869 }
aoqi@0 2870 }
aoqi@0 2871 assert(tot == _number_of_bytecodes, "should be the same");
aoqi@0 2872 tty->print_cr("%9d 100.000%%", _number_of_bytecodes);
aoqi@0 2873 }
aoqi@0 2874 tty->cr();
aoqi@0 2875 }
aoqi@0 2876 };
aoqi@0 2877
aoqi@0 2878 int MethodStatistics::_number_of_methods;
aoqi@0 2879 int MethodStatistics::_number_of_final_methods;
aoqi@0 2880 int MethodStatistics::_number_of_static_methods;
aoqi@0 2881 int MethodStatistics::_number_of_native_methods;
aoqi@0 2882 int MethodStatistics::_number_of_synchronized_methods;
aoqi@0 2883 int MethodStatistics::_number_of_profiled_methods;
aoqi@0 2884 int MethodStatistics::_number_of_bytecodes;
aoqi@0 2885 int MethodStatistics::_parameter_size_profile[MethodStatistics::max_parameter_size];
aoqi@0 2886 int MethodStatistics::_bytecodes_profile[Bytecodes::number_of_java_codes];
aoqi@0 2887
aoqi@0 2888
aoqi@0 2889 void SystemDictionary::print_method_statistics() {
aoqi@0 2890 MethodStatistics::print();
aoqi@0 2891 }
aoqi@0 2892
aoqi@0 2893 #endif // PRODUCT

mercurial