src/share/vm/classfile/systemDictionary.cpp

Thu, 12 Nov 2009 09:24:21 -0800

author
never
date
Thu, 12 Nov 2009 09:24:21 -0800
changeset 1515
7c57aead6d3e
parent 1494
389049f3f393
child 1511
1920bd911283
permissions
-rw-r--r--

6892658: C2 should optimize some stringbuilder patterns
Reviewed-by: kvn, twisti

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

mercurial