src/share/vm/classfile/systemDictionary.cpp

Thu, 07 Apr 2011 17:02:30 -0700

author
jrose
date
Thu, 07 Apr 2011 17:02:30 -0700
changeset 2742
ed69575596ac
parent 2698
38fea01eb669
child 2777
8ce625481709
permissions
-rw-r--r--

6981791: remove experimental code for JSR 292
Reviewed-by: twisti

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

mercurial