src/share/vm/classfile/systemDictionary.cpp

Wed, 09 Jun 2010 18:50:45 -0700

author
jrose
date
Wed, 09 Jun 2010 18:50:45 -0700
changeset 1957
136b78722a08
parent 1907
c18cbe5936b8
child 2015
083fde3b838e
permissions
-rw-r--r--

6939203: JSR 292 needs method handle constants
Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode.
Reviewed-by: twisti, never

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

mercurial