src/share/vm/classfile/systemDictionary.cpp

Wed, 11 Jan 2012 17:34:02 -0500

author
phh
date
Wed, 11 Jan 2012 17:34:02 -0500
changeset 3427
94ec88ca68e2
parent 3137
e6b1331a51d2
child 3491
b2cd0ee8f778
permissions
-rw-r--r--

7115199: Add event tracing hooks and Java Flight Recorder infrastructure
Summary: Added a nop tracing infrastructure, JFR makefile changes and other infrastructure used only by JFR.
Reviewed-by: acorn, sspitsyn
Contributed-by: markus.gronlund@oracle.com

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;
never@3137 128 return java_lang_ClassLoader::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;
never@3137 134 if (AllowParallelDefineClass && java_lang_ClassLoader::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 }
coleenp@2777 1258 if (JvmtiExport::has_redefined_a_class()) {
coleenp@2777 1259 // Reinitialize vtable because RedefineClasses may have changed some
coleenp@2777 1260 // entries in this vtable for super classes so the CDS vtable might
coleenp@2777 1261 // point to old or obsolete entries. RedefineClasses doesn't fix up
coleenp@2777 1262 // vtables in the shared system dictionary, only the main one.
coleenp@2777 1263 // It also redefines the itable too so fix that too.
coleenp@2777 1264 ResourceMark rm(THREAD);
coleenp@2777 1265 ik->vtable()->initialize_vtable(false, CHECK_(nh));
coleenp@2777 1266 ik->itable()->initialize_itable(false, CHECK_(nh));
coleenp@2777 1267 }
duke@435 1268 }
duke@435 1269
duke@435 1270 if (TraceClassLoading) {
duke@435 1271 ResourceMark rm;
duke@435 1272 tty->print("[Loaded %s", ik->external_name());
duke@435 1273 tty->print(" from shared objects file");
duke@435 1274 tty->print_cr("]");
duke@435 1275 }
duke@435 1276 // notify a class loaded from shared object
duke@435 1277 ClassLoadingService::notify_class_loaded(instanceKlass::cast(ik()),
duke@435 1278 true /* shared class */);
duke@435 1279 }
duke@435 1280 return ik;
duke@435 1281 }
duke@435 1282
duke@435 1283 #ifdef KERNEL
duke@435 1284 // Some classes on the bootstrap class path haven't been installed on the
duke@435 1285 // system yet. Call the DownloadManager method to make them appear in the
duke@435 1286 // bootstrap class path and try again to load the named class.
duke@435 1287 // Note that with delegation class loaders all classes in another loader will
duke@435 1288 // first try to call this so it'd better be fast!!
duke@435 1289 static instanceKlassHandle download_and_retry_class_load(
coleenp@2497 1290 Symbol* class_name,
duke@435 1291 TRAPS) {
duke@435 1292
never@3137 1293 klassOop dlm = SystemDictionary::DownloadManager_klass();
duke@435 1294 instanceKlassHandle nk;
duke@435 1295
duke@435 1296 // If download manager class isn't loaded just return.
duke@435 1297 if (dlm == NULL) return nk;
duke@435 1298
duke@435 1299 { HandleMark hm(THREAD);
duke@435 1300 ResourceMark rm(THREAD);
duke@435 1301 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nk));
duke@435 1302 Handle class_string = java_lang_String::externalize_classname(s, CHECK_(nk));
duke@435 1303
duke@435 1304 // return value
duke@435 1305 JavaValue result(T_OBJECT);
duke@435 1306
duke@435 1307 // Call the DownloadManager. We assume that it has a lock because
duke@435 1308 // multiple classes could be not found and downloaded at the same time.
duke@435 1309 // class sun.misc.DownloadManager;
duke@435 1310 // public static String getBootClassPathEntryForClass(String className);
duke@435 1311 JavaCalls::call_static(&result,
duke@435 1312 KlassHandle(THREAD, dlm),
coleenp@2497 1313 vmSymbols::getBootClassPathEntryForClass_name(),
coleenp@2497 1314 vmSymbols::string_string_signature(),
duke@435 1315 class_string,
duke@435 1316 CHECK_(nk));
duke@435 1317
duke@435 1318 // Get result.string and add to bootclasspath
duke@435 1319 assert(result.get_type() == T_OBJECT, "just checking");
duke@435 1320 oop obj = (oop) result.get_jobject();
duke@435 1321 if (obj == NULL) { return nk; }
duke@435 1322
coleenp@457 1323 Handle h_obj(THREAD, obj);
coleenp@457 1324 char* new_class_name = java_lang_String::as_platform_dependent_str(h_obj,
coleenp@457 1325 CHECK_(nk));
duke@435 1326
duke@435 1327 // lock the loader
duke@435 1328 // we use this lock because JVMTI does.
duke@435 1329 Handle loader_lock(THREAD, SystemDictionary::system_loader_lock());
duke@435 1330
duke@435 1331 ObjectLocker ol(loader_lock, THREAD);
duke@435 1332 // add the file to the bootclasspath
duke@435 1333 ClassLoader::update_class_path_entry_list(new_class_name, true);
duke@435 1334 } // end HandleMark
duke@435 1335
duke@435 1336 if (TraceClassLoading) {
duke@435 1337 ClassLoader::print_bootclasspath();
duke@435 1338 }
duke@435 1339 return ClassLoader::load_classfile(class_name, CHECK_(nk));
duke@435 1340 }
duke@435 1341 #endif // KERNEL
duke@435 1342
duke@435 1343
coleenp@2497 1344 instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
duke@435 1345 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
duke@435 1346 if (class_loader.is_null()) {
mchung@1310 1347
duke@435 1348 // Search the shared system dictionary for classes preloaded into the
duke@435 1349 // shared spaces.
duke@435 1350 instanceKlassHandle k;
mchung@1310 1351 {
mchung@1310 1352 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
mchung@1310 1353 k = load_shared_class(class_name, class_loader, THREAD);
mchung@1310 1354 }
duke@435 1355
duke@435 1356 if (k.is_null()) {
duke@435 1357 // Use VM class loader
mchung@1310 1358 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
duke@435 1359 k = ClassLoader::load_classfile(class_name, CHECK_(nh));
duke@435 1360 }
duke@435 1361
duke@435 1362 #ifdef KERNEL
duke@435 1363 // If the VM class loader has failed to load the class, call the
duke@435 1364 // DownloadManager class to make it magically appear on the classpath
duke@435 1365 // and try again. This is only configured with the Kernel VM.
duke@435 1366 if (k.is_null()) {
duke@435 1367 k = download_and_retry_class_load(class_name, CHECK_(nh));
duke@435 1368 }
duke@435 1369 #endif // KERNEL
duke@435 1370
acorn@949 1371 // find_or_define_instance_class may return a different instanceKlass
duke@435 1372 if (!k.is_null()) {
duke@435 1373 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
duke@435 1374 }
duke@435 1375 return k;
duke@435 1376 } else {
duke@435 1377 // Use user specified class loader to load class. Call loadClass operation on class_loader.
duke@435 1378 ResourceMark rm(THREAD);
duke@435 1379
mchung@1310 1380 assert(THREAD->is_Java_thread(), "must be a JavaThread");
mchung@1310 1381 JavaThread* jt = (JavaThread*) THREAD;
mchung@1310 1382
mchung@1310 1383 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
mchung@1310 1384 ClassLoader::perf_app_classload_selftime(),
mchung@1310 1385 ClassLoader::perf_app_classload_count(),
mchung@1310 1386 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@1310 1387 jt->get_thread_stat()->perf_timers_addr(),
mchung@1310 1388 PerfClassTraceTime::CLASS_LOAD);
mchung@1310 1389
duke@435 1390 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
duke@435 1391 // Translate to external class name format, i.e., convert '/' chars to '.'
duke@435 1392 Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
duke@435 1393
duke@435 1394 JavaValue result(T_OBJECT);
duke@435 1395
never@1577 1396 KlassHandle spec_klass (THREAD, SystemDictionary::ClassLoader_klass());
duke@435 1397
acorn@949 1398 // Call public unsynchronized loadClass(String) directly for all class loaders
acorn@949 1399 // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
acorn@949 1400 // acquire a class-name based lock rather than the class loader object lock.
acorn@949 1401 // JDK < 7 already acquire the class loader lock in loadClass(String, boolean),
acorn@949 1402 // so the call to loadClassInternal() was not required.
acorn@949 1403 //
acorn@949 1404 // UnsyncloadClass flag means both call loadClass(String) and do
acorn@949 1405 // not acquire the class loader lock even for class loaders that are
acorn@949 1406 // not parallelCapable. This was a risky transitional
acorn@949 1407 // flag for diagnostic purposes only. It is risky to call
duke@435 1408 // custom class loaders without synchronization.
duke@435 1409 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of
acorn@949 1410 // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field.
duke@435 1411 // Do NOT assume this will be supported in future releases.
acorn@949 1412 //
acorn@949 1413 // Added MustCallLoadClassInternal in case we discover in the field
acorn@949 1414 // a customer that counts on this call
acorn@949 1415 if (MustCallLoadClassInternal && has_loadClassInternal()) {
duke@435 1416 JavaCalls::call_special(&result,
duke@435 1417 class_loader,
duke@435 1418 spec_klass,
coleenp@2497 1419 vmSymbols::loadClassInternal_name(),
coleenp@2497 1420 vmSymbols::string_class_signature(),
duke@435 1421 string,
duke@435 1422 CHECK_(nh));
duke@435 1423 } else {
duke@435 1424 JavaCalls::call_virtual(&result,
duke@435 1425 class_loader,
duke@435 1426 spec_klass,
coleenp@2497 1427 vmSymbols::loadClass_name(),
coleenp@2497 1428 vmSymbols::string_class_signature(),
duke@435 1429 string,
duke@435 1430 CHECK_(nh));
duke@435 1431 }
duke@435 1432
duke@435 1433 assert(result.get_type() == T_OBJECT, "just checking");
duke@435 1434 oop obj = (oop) result.get_jobject();
duke@435 1435
duke@435 1436 // Primitive classes return null since forName() can not be
duke@435 1437 // used to obtain any of the Class objects representing primitives or void
duke@435 1438 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
duke@435 1439 instanceKlassHandle k =
duke@435 1440 instanceKlassHandle(THREAD, java_lang_Class::as_klassOop(obj));
duke@435 1441 // For user defined Java class loaders, check that the name returned is
duke@435 1442 // the same as that requested. This check is done for the bootstrap
duke@435 1443 // loader when parsing the class file.
coleenp@2497 1444 if (class_name == k->name()) {
duke@435 1445 return k;
duke@435 1446 }
duke@435 1447 }
duke@435 1448 // Class is not found or has the wrong name, return NULL
duke@435 1449 return nh;
duke@435 1450 }
duke@435 1451 }
duke@435 1452
duke@435 1453 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
duke@435 1454
duke@435 1455 Handle class_loader_h(THREAD, k->class_loader());
duke@435 1456
acorn@949 1457 // for bootstrap and other parallel classloaders don't acquire lock,
acorn@949 1458 // use placeholder token
acorn@949 1459 // If a parallelCapable class loader calls define_instance_class instead of
acorn@949 1460 // find_or_define_instance_class to get here, we have a timing
acorn@949 1461 // hole with systemDictionary updates and check_constraints
acorn@949 1462 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
duke@435 1463 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
duke@435 1464 compute_loader_lock_object(class_loader_h, THREAD)),
duke@435 1465 "define called without lock");
duke@435 1466 }
duke@435 1467
duke@435 1468 // Check class-loading constraints. Throw exception if violation is detected.
duke@435 1469 // Grabs and releases SystemDictionary_lock
duke@435 1470 // The check_constraints/find_class call and update_dictionary sequence
duke@435 1471 // must be "atomic" for a specific class/classloader pair so we never
duke@435 1472 // define two different instanceKlasses for that class/classloader pair.
duke@435 1473 // Existing classloaders will call define_instance_class with the
duke@435 1474 // classloader lock held
duke@435 1475 // Parallel classloaders will call find_or_define_instance_class
duke@435 1476 // which will require a token to perform the define class
coleenp@2497 1477 Symbol* name_h = k->name();
duke@435 1478 unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader_h);
duke@435 1479 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1480 check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
duke@435 1481
duke@435 1482 // Register class just loaded with class loader (placed in Vector)
duke@435 1483 // Note we do this before updating the dictionary, as this can
duke@435 1484 // fail with an OutOfMemoryError (if it does, we will *not* put this
duke@435 1485 // class in the dictionary and will not update the class hierarchy).
duke@435 1486 if (k->class_loader() != NULL) {
duke@435 1487 methodHandle m(THREAD, Universe::loader_addClass_method());
duke@435 1488 JavaValue result(T_VOID);
duke@435 1489 JavaCallArguments args(class_loader_h);
duke@435 1490 args.push_oop(Handle(THREAD, k->java_mirror()));
duke@435 1491 JavaCalls::call(&result, m, &args, CHECK);
duke@435 1492 }
duke@435 1493
duke@435 1494 // Add the new class. We need recompile lock during update of CHA.
duke@435 1495 {
duke@435 1496 unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader_h);
duke@435 1497 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1498
duke@435 1499 MutexLocker mu_r(Compile_lock, THREAD);
duke@435 1500
duke@435 1501 // Add to class hierarchy, initialize vtables, and do possible
duke@435 1502 // deoptimizations.
duke@435 1503 add_to_hierarchy(k, CHECK); // No exception, but can block
duke@435 1504
duke@435 1505 // Add to systemDictionary - so other classes can see it.
duke@435 1506 // Grabs and releases SystemDictionary_lock
duke@435 1507 update_dictionary(d_index, d_hash, p_index, p_hash,
duke@435 1508 k, class_loader_h, THREAD);
duke@435 1509 }
duke@435 1510 k->eager_initialize(THREAD);
duke@435 1511
duke@435 1512 // notify jvmti
duke@435 1513 if (JvmtiExport::should_post_class_load()) {
duke@435 1514 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
duke@435 1515 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
duke@435 1516
duke@435 1517 }
duke@435 1518 }
duke@435 1519
duke@435 1520 // Support parallel classloading
acorn@1510 1521 // All parallel class loaders, including bootstrap classloader
acorn@1510 1522 // lock a placeholder entry for this class/class_loader pair
acorn@1510 1523 // to allow parallel defines of different classes for this class loader
acorn@949 1524 // With AllowParallelDefine flag==true, in case they do not synchronize around
acorn@949 1525 // FindLoadedClass/DefineClass, calls, we check for parallel
duke@435 1526 // loading for them, wait if a defineClass is in progress
duke@435 1527 // and return the initial requestor's results
acorn@1510 1528 // This flag does not apply to the bootstrap classloader.
acorn@949 1529 // With AllowParallelDefine flag==false, call through to define_instance_class
acorn@949 1530 // which will throw LinkageError: duplicate class definition.
acorn@1510 1531 // False is the requested default.
duke@435 1532 // For better performance, the class loaders should synchronize
acorn@949 1533 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
duke@435 1534 // potentially waste time reading and parsing the bytestream.
duke@435 1535 // Note: VM callers should ensure consistency of k/class_name,class_loader
coleenp@2497 1536 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
duke@435 1537
duke@435 1538 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
coleenp@2497 1539 Symbol* name_h = k->name(); // passed in class_name may be null
duke@435 1540
acorn@950 1541 unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader);
duke@435 1542 int d_index = dictionary()->hash_to_index(d_hash);
duke@435 1543
duke@435 1544 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
acorn@950 1545 unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader);
duke@435 1546 int p_index = placeholders()->hash_to_index(p_hash);
duke@435 1547 PlaceholderEntry* probe;
duke@435 1548
duke@435 1549 {
duke@435 1550 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 1551 // First check if class already defined
acorn@1510 1552 if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
acorn@1510 1553 klassOop check = find_class(d_index, d_hash, name_h, class_loader);
acorn@1510 1554 if (check != NULL) {
acorn@1510 1555 return(instanceKlassHandle(THREAD, check));
acorn@1510 1556 }
duke@435 1557 }
duke@435 1558
duke@435 1559 // Acquire define token for this class/classloader
coleenp@2497 1560 probe = placeholders()->find_and_add(p_index, p_hash, name_h, class_loader, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
acorn@949 1561 // Wait if another thread defining in parallel
acorn@949 1562 // All threads wait - even those that will throw duplicate class: otherwise
acorn@949 1563 // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
acorn@949 1564 // if other thread has not finished updating dictionary
acorn@949 1565 while (probe->definer() != NULL) {
acorn@949 1566 SystemDictionary_lock->wait();
acorn@949 1567 }
acorn@949 1568 // Only special cases allow parallel defines and can use other thread's results
acorn@949 1569 // Other cases fall through, and may run into duplicate defines
acorn@949 1570 // caught by finding an entry in the SystemDictionary
acorn@1510 1571 if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instanceKlass() != NULL)) {
jrose@866 1572 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
acorn@950 1573 placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
acorn@949 1574 SystemDictionary_lock->notify_all();
jrose@866 1575 #ifdef ASSERT
acorn@950 1576 klassOop check = find_class(d_index, d_hash, name_h, class_loader);
acorn@949 1577 assert(check != NULL, "definer missed recording success");
jrose@866 1578 #endif
acorn@949 1579 return(instanceKlassHandle(THREAD, probe->instanceKlass()));
acorn@949 1580 } else {
acorn@949 1581 // This thread will define the class (even if earlier thread tried and had an error)
duke@435 1582 probe->set_definer(THREAD);
duke@435 1583 }
duke@435 1584 }
duke@435 1585
duke@435 1586 define_instance_class(k, THREAD);
duke@435 1587
duke@435 1588 Handle linkage_exception = Handle(); // null handle
duke@435 1589
duke@435 1590 // definer must notify any waiting threads
duke@435 1591 {
duke@435 1592 MutexLocker mu(SystemDictionary_lock, THREAD);
acorn@950 1593 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, class_loader);
duke@435 1594 assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
duke@435 1595 if (probe != NULL) {
duke@435 1596 if (HAS_PENDING_EXCEPTION) {
duke@435 1597 linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
duke@435 1598 CLEAR_PENDING_EXCEPTION;
duke@435 1599 } else {
duke@435 1600 probe->set_instanceKlass(k());
duke@435 1601 }
duke@435 1602 probe->set_definer(NULL);
duke@435 1603 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
acorn@950 1604 placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
duke@435 1605 SystemDictionary_lock->notify_all();
duke@435 1606 }
duke@435 1607 }
duke@435 1608
duke@435 1609 // Can't throw exception while holding lock due to rank ordering
duke@435 1610 if (linkage_exception() != NULL) {
duke@435 1611 THROW_OOP_(linkage_exception(), nh); // throws exception and returns
duke@435 1612 }
duke@435 1613
duke@435 1614 return k;
duke@435 1615 }
duke@435 1616 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
duke@435 1617 // If class_loader is NULL we synchronize on _system_loader_lock_obj
duke@435 1618 if (class_loader.is_null()) {
duke@435 1619 return Handle(THREAD, _system_loader_lock_obj);
duke@435 1620 } else {
duke@435 1621 return class_loader;
duke@435 1622 }
duke@435 1623 }
duke@435 1624
duke@435 1625 // This method is added to check how often we have to wait to grab loader
duke@435 1626 // lock. The results are being recorded in the performance counters defined in
duke@435 1627 // ClassLoader::_sync_systemLoaderLockContentionRate and
duke@435 1628 // ClassLoader::_sync_nonSystemLoaderLockConteionRate.
duke@435 1629 void SystemDictionary::check_loader_lock_contention(Handle loader_lock, TRAPS) {
duke@435 1630 if (!UsePerfData) {
duke@435 1631 return;
duke@435 1632 }
duke@435 1633
duke@435 1634 assert(!loader_lock.is_null(), "NULL lock object");
duke@435 1635
duke@435 1636 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
duke@435 1637 == ObjectSynchronizer::owner_other) {
duke@435 1638 // contention will likely happen, so increment the corresponding
duke@435 1639 // contention counter.
duke@435 1640 if (loader_lock() == _system_loader_lock_obj) {
duke@435 1641 ClassLoader::sync_systemLoaderLockContentionRate()->inc();
duke@435 1642 } else {
duke@435 1643 ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
duke@435 1644 }
duke@435 1645 }
duke@435 1646 }
duke@435 1647
duke@435 1648 // ----------------------------------------------------------------------------
duke@435 1649 // Lookup
duke@435 1650
duke@435 1651 klassOop SystemDictionary::find_class(int index, unsigned int hash,
coleenp@2497 1652 Symbol* class_name,
duke@435 1653 Handle class_loader) {
duke@435 1654 assert_locked_or_safepoint(SystemDictionary_lock);
duke@435 1655 assert (index == dictionary()->index_for(class_name, class_loader),
duke@435 1656 "incorrect index?");
duke@435 1657
duke@435 1658 klassOop k = dictionary()->find_class(index, hash, class_name, class_loader);
duke@435 1659 return k;
duke@435 1660 }
duke@435 1661
duke@435 1662
duke@435 1663 // Basic find on classes in the midst of being loaded
coleenp@2497 1664 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
coleenp@2497 1665 Handle class_loader) {
duke@435 1666 assert_locked_or_safepoint(SystemDictionary_lock);
coleenp@2497 1667 unsigned int p_hash = placeholders()->compute_hash(class_name, class_loader);
coleenp@2497 1668 int p_index = placeholders()->hash_to_index(p_hash);
coleenp@2497 1669 return placeholders()->find_entry(p_index, p_hash, class_name, class_loader);
duke@435 1670 }
duke@435 1671
duke@435 1672
duke@435 1673 // Used for assertions and verification only
coleenp@2497 1674 klassOop SystemDictionary::find_class(Symbol* class_name, Handle class_loader) {
duke@435 1675 #ifndef ASSERT
duke@435 1676 guarantee(VerifyBeforeGC ||
duke@435 1677 VerifyDuringGC ||
duke@435 1678 VerifyBeforeExit ||
duke@435 1679 VerifyAfterGC, "too expensive");
duke@435 1680 #endif
duke@435 1681 assert_locked_or_safepoint(SystemDictionary_lock);
duke@435 1682
duke@435 1683 // First look in the loaded class array
duke@435 1684 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
duke@435 1685 int d_index = dictionary()->hash_to_index(d_hash);
coleenp@2497 1686 return find_class(d_index, d_hash, class_name, class_loader);
duke@435 1687 }
duke@435 1688
duke@435 1689
duke@435 1690 // Get the next class in the diictionary.
duke@435 1691 klassOop SystemDictionary::try_get_next_class() {
duke@435 1692 return dictionary()->try_get_next_class();
duke@435 1693 }
duke@435 1694
duke@435 1695
duke@435 1696 // ----------------------------------------------------------------------------
duke@435 1697 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
duke@435 1698 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
duke@435 1699 // before a new class is used.
duke@435 1700
duke@435 1701 void SystemDictionary::add_to_hierarchy(instanceKlassHandle k, TRAPS) {
duke@435 1702 assert(k.not_null(), "just checking");
never@2551 1703 assert_locked_or_safepoint(Compile_lock);
never@2551 1704
duke@435 1705 // Link into hierachy. Make sure the vtables are initialized before linking into
duke@435 1706 k->append_to_sibling_list(); // add to superklass/sibling list
duke@435 1707 k->process_interfaces(THREAD); // handle all "implements" declarations
duke@435 1708 k->set_init_state(instanceKlass::loaded);
duke@435 1709 // Now flush all code that depended on old class hierarchy.
duke@435 1710 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
duke@435 1711 // Also, first reinitialize vtable because it may have gotten out of synch
duke@435 1712 // while the new class wasn't connected to the class hierarchy.
duke@435 1713 Universe::flush_dependents_on(k);
duke@435 1714 }
duke@435 1715
duke@435 1716
duke@435 1717 // ----------------------------------------------------------------------------
duke@435 1718 // GC support
duke@435 1719
duke@435 1720 // Following roots during mark-sweep is separated in two phases.
duke@435 1721 //
duke@435 1722 // The first phase follows preloaded classes and all other system
duke@435 1723 // classes, since these will never get unloaded anyway.
duke@435 1724 //
duke@435 1725 // The second phase removes (unloads) unreachable classes from the
duke@435 1726 // system dictionary and follows the remaining classes' contents.
duke@435 1727
duke@435 1728 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
duke@435 1729 // Follow preloaded classes/mirrors and system loader object
duke@435 1730 blk->do_oop(&_java_system_loader);
duke@435 1731 preloaded_oops_do(blk);
duke@435 1732 always_strong_classes_do(blk);
duke@435 1733 }
duke@435 1734
duke@435 1735
duke@435 1736 void SystemDictionary::always_strong_classes_do(OopClosure* blk) {
duke@435 1737 // Follow all system classes and temporary placeholders in dictionary
duke@435 1738 dictionary()->always_strong_classes_do(blk);
duke@435 1739
duke@435 1740 // Placeholders. These are *always* strong roots, as they
duke@435 1741 // represent classes we're actively loading.
duke@435 1742 placeholders_do(blk);
duke@435 1743
jrose@1145 1744 // Visit extra methods
twisti@2258 1745 invoke_method_table()->oops_do(blk);
duke@435 1746 }
duke@435 1747
duke@435 1748
duke@435 1749 void SystemDictionary::placeholders_do(OopClosure* blk) {
duke@435 1750 placeholders()->oops_do(blk);
duke@435 1751 }
duke@435 1752
duke@435 1753
duke@435 1754 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
duke@435 1755 bool result = dictionary()->do_unloading(is_alive);
duke@435 1756 constraints()->purge_loader_constraints(is_alive);
duke@435 1757 resolution_errors()->purge_resolution_errors(is_alive);
duke@435 1758 return result;
duke@435 1759 }
duke@435 1760
duke@435 1761
duke@435 1762 // The mirrors are scanned by shared_oops_do() which is
duke@435 1763 // not called by oops_do(). In order to process oops in
duke@435 1764 // a necessary order, shared_oops_do() is call by
duke@435 1765 // Universe::oops_do().
duke@435 1766 void SystemDictionary::oops_do(OopClosure* f) {
duke@435 1767 // Adjust preloaded classes and system loader object
duke@435 1768 f->do_oop(&_java_system_loader);
duke@435 1769 preloaded_oops_do(f);
duke@435 1770
duke@435 1771 lazily_loaded_oops_do(f);
duke@435 1772
duke@435 1773 // Adjust dictionary
duke@435 1774 dictionary()->oops_do(f);
duke@435 1775
jrose@1145 1776 // Visit extra methods
twisti@2258 1777 invoke_method_table()->oops_do(f);
jrose@1145 1778
duke@435 1779 // Partially loaded classes
duke@435 1780 placeholders()->oops_do(f);
duke@435 1781
duke@435 1782 // Adjust constraint table
duke@435 1783 constraints()->oops_do(f);
duke@435 1784
duke@435 1785 // Adjust resolution error table
duke@435 1786 resolution_errors()->oops_do(f);
duke@435 1787 }
duke@435 1788
duke@435 1789
duke@435 1790 void SystemDictionary::preloaded_oops_do(OopClosure* f) {
jrose@567 1791 for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
jrose@567 1792 f->do_oop((oop*) &_well_known_klasses[k]);
jrose@567 1793 }
duke@435 1794
duke@435 1795 {
duke@435 1796 for (int i = 0; i < T_VOID+1; i++) {
duke@435 1797 if (_box_klasses[i] != NULL) {
duke@435 1798 assert(i >= T_BOOLEAN, "checking");
duke@435 1799 f->do_oop((oop*) &_box_klasses[i]);
duke@435 1800 }
duke@435 1801 }
duke@435 1802 }
duke@435 1803
duke@435 1804 // The basic type mirrors would have already been processed in
duke@435 1805 // Universe::oops_do(), via a call to shared_oops_do(), so should
duke@435 1806 // not be processed again.
duke@435 1807
duke@435 1808 f->do_oop((oop*) &_system_loader_lock_obj);
duke@435 1809 FilteredFieldsMap::klasses_oops_do(f);
duke@435 1810 }
duke@435 1811
duke@435 1812 void SystemDictionary::lazily_loaded_oops_do(OopClosure* f) {
duke@435 1813 f->do_oop((oop*) &_abstract_ownable_synchronizer_klass);
duke@435 1814 }
duke@435 1815
duke@435 1816 // Just the classes from defining class loaders
duke@435 1817 // Don't iterate over placeholders
duke@435 1818 void SystemDictionary::classes_do(void f(klassOop)) {
duke@435 1819 dictionary()->classes_do(f);
duke@435 1820 }
duke@435 1821
duke@435 1822 // Added for initialize_itable_for_klass
duke@435 1823 // Just the classes from defining class loaders
duke@435 1824 // Don't iterate over placeholders
duke@435 1825 void SystemDictionary::classes_do(void f(klassOop, TRAPS), TRAPS) {
duke@435 1826 dictionary()->classes_do(f, CHECK);
duke@435 1827 }
duke@435 1828
duke@435 1829 // All classes, and their class loaders
duke@435 1830 // Don't iterate over placeholders
duke@435 1831 void SystemDictionary::classes_do(void f(klassOop, oop)) {
duke@435 1832 dictionary()->classes_do(f);
duke@435 1833 }
duke@435 1834
duke@435 1835 // All classes, and their class loaders
duke@435 1836 // (added for helpers that use HandleMarks and ResourceMarks)
duke@435 1837 // Don't iterate over placeholders
duke@435 1838 void SystemDictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) {
duke@435 1839 dictionary()->classes_do(f, CHECK);
duke@435 1840 }
duke@435 1841
coleenp@2497 1842 void SystemDictionary::placeholders_do(void f(Symbol*, oop)) {
duke@435 1843 placeholders()->entries_do(f);
duke@435 1844 }
duke@435 1845
duke@435 1846 void SystemDictionary::methods_do(void f(methodOop)) {
duke@435 1847 dictionary()->methods_do(f);
twisti@2258 1848 invoke_method_table()->methods_do(f);
duke@435 1849 }
duke@435 1850
duke@435 1851 // ----------------------------------------------------------------------------
duke@435 1852 // Lazily load klasses
duke@435 1853
duke@435 1854 void SystemDictionary::load_abstract_ownable_synchronizer_klass(TRAPS) {
duke@435 1855 assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
duke@435 1856
duke@435 1857 // if multiple threads calling this function, only one thread will load
duke@435 1858 // the class. The other threads will find the loaded version once the
duke@435 1859 // class is loaded.
duke@435 1860 klassOop aos = _abstract_ownable_synchronizer_klass;
duke@435 1861 if (aos == NULL) {
coleenp@2497 1862 klassOop k = resolve_or_fail(vmSymbols::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
duke@435 1863 // Force a fence to prevent any read before the write completes
duke@435 1864 OrderAccess::fence();
duke@435 1865 _abstract_ownable_synchronizer_klass = k;
duke@435 1866 }
duke@435 1867 }
duke@435 1868
duke@435 1869 // ----------------------------------------------------------------------------
duke@435 1870 // Initialization
duke@435 1871
duke@435 1872 void SystemDictionary::initialize(TRAPS) {
duke@435 1873 // Allocate arrays
duke@435 1874 assert(dictionary() == NULL,
duke@435 1875 "SystemDictionary should only be initialized once");
twisti@2258 1876 _dictionary = new Dictionary(_nof_buckets);
twisti@2258 1877 _placeholders = new PlaceholderTable(_nof_buckets);
duke@435 1878 _number_of_modifications = 0;
twisti@2258 1879 _loader_constraints = new LoaderConstraintTable(_loader_constraint_size);
twisti@2258 1880 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
twisti@2258 1881 _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
duke@435 1882
duke@435 1883 // Allocate private object used as system class loader lock
duke@435 1884 _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
duke@435 1885 // Initialize basic classes
duke@435 1886 initialize_preloaded_classes(CHECK);
duke@435 1887 }
duke@435 1888
jrose@567 1889 // Compact table of directions on the initialization of klasses:
jrose@567 1890 static const short wk_init_info[] = {
jrose@567 1891 #define WK_KLASS_INIT_INFO(name, symbol, option) \
jrose@567 1892 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
jrose@567 1893 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
jrose@567 1894 | (int)SystemDictionary::option ),
jrose@567 1895 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
jrose@567 1896 #undef WK_KLASS_INIT_INFO
jrose@567 1897 0
jrose@567 1898 };
jrose@567 1899
jrose@567 1900 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
jrose@567 1901 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
jrose@567 1902 int info = wk_init_info[id - FIRST_WKID];
jrose@567 1903 int sid = (info >> CEIL_LG_OPTION_LIMIT);
coleenp@2497 1904 Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
jrose@567 1905 klassOop* klassp = &_well_known_klasses[id];
jrose@2742 1906 bool must_load = (init_opt < SystemDictionary::Opt);
jrose@2742 1907 bool try_load = true;
jrose@567 1908 if (init_opt == SystemDictionary::Opt_Kernel) {
jrose@567 1909 #ifndef KERNEL
jrose@567 1910 try_load = false;
jrose@567 1911 #endif //KERNEL
jrose@567 1912 }
jrose@2742 1913 if ((*klassp) == NULL && try_load) {
jrose@567 1914 if (must_load) {
jrose@567 1915 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
jrose@567 1916 } else {
jrose@567 1917 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
jrose@567 1918 }
jrose@567 1919 }
jrose@2742 1920 return ((*klassp) != NULL);
jrose@567 1921 }
jrose@567 1922
jrose@567 1923 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
jrose@567 1924 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
jrose@567 1925 for (int id = (int)start_id; id < (int)limit_id; id++) {
jrose@567 1926 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
jrose@567 1927 int info = wk_init_info[id - FIRST_WKID];
jrose@567 1928 int sid = (info >> CEIL_LG_OPTION_LIMIT);
jrose@567 1929 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
jrose@567 1930
jrose@567 1931 initialize_wk_klass((WKID)id, opt, CHECK);
jrose@567 1932
jrose@567 1933 // Update limits, so find_well_known_klass can be very fast:
coleenp@2497 1934 Symbol* s = vmSymbols::symbol_at((vmSymbols::SID)sid);
jrose@567 1935 if (wk_klass_name_limits[1] == NULL) {
jrose@567 1936 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s;
jrose@567 1937 } else if (wk_klass_name_limits[1] < s) {
jrose@567 1938 wk_klass_name_limits[1] = s;
jrose@567 1939 } else if (wk_klass_name_limits[0] > s) {
jrose@567 1940 wk_klass_name_limits[0] = s;
jrose@567 1941 }
jrose@567 1942 }
jrose@1145 1943
jrose@1145 1944 // move the starting value forward to the limit:
jrose@1145 1945 start_id = limit_id;
jrose@567 1946 }
jrose@567 1947
duke@435 1948
duke@435 1949 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
never@1577 1950 assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
duke@435 1951 // Preload commonly used klasses
jrose@567 1952 WKID scan = FIRST_WKID;
jrose@567 1953 // first do Object, String, Class
never@1577 1954 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
jrose@567 1955
never@3137 1956 java_lang_Class::compute_offsets();
jrose@567 1957
duke@435 1958 // Fixup mirrors for classes loaded before java.lang.Class.
duke@435 1959 // These calls iterate over the objects currently in the perm gen
duke@435 1960 // so calling them at this point is matters (not before when there
duke@435 1961 // are fewer objects and not later after there are more objects
duke@435 1962 // in the perm gen.
duke@435 1963 Universe::initialize_basic_type_mirrors(CHECK);
duke@435 1964 Universe::fixup_mirrors(CHECK);
duke@435 1965
jrose@567 1966 // do a bunch more:
never@1577 1967 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK);
duke@435 1968
duke@435 1969 // Preload ref klasses and set reference types
never@1577 1970 instanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER);
never@1577 1971 instanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass));
duke@435 1972
never@1577 1973 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(PhantomReference_klass), scan, CHECK);
never@1577 1974 instanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT);
never@1577 1975 instanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK);
never@1577 1976 instanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL);
never@1577 1977 instanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM);
duke@435 1978
twisti@2698 1979 // JSR 292 classes
twisti@2698 1980 WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);
twisti@3050 1981 WKID jsr292_group_end = WK_KLASS_ENUM_NAME(VolatileCallSite_klass);
twisti@2698 1982 initialize_wk_klasses_until(jsr292_group_start, scan, CHECK);
jrose@1161 1983 if (EnableInvokeDynamic) {
twisti@2698 1984 initialize_wk_klasses_through(jsr292_group_end, scan, CHECK);
twisti@2698 1985 } else {
twisti@2698 1986 // Skip the JSR 292 classes, if not enabled.
twisti@2698 1987 scan = WKID(jsr292_group_end + 1);
jrose@1161 1988 }
jrose@1145 1989
jrose@567 1990 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK);
duke@435 1991
never@1577 1992 _box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass);
never@1577 1993 _box_klasses[T_CHAR] = WK_KLASS(Character_klass);
never@1577 1994 _box_klasses[T_FLOAT] = WK_KLASS(Float_klass);
never@1577 1995 _box_klasses[T_DOUBLE] = WK_KLASS(Double_klass);
never@1577 1996 _box_klasses[T_BYTE] = WK_KLASS(Byte_klass);
never@1577 1997 _box_klasses[T_SHORT] = WK_KLASS(Short_klass);
never@1577 1998 _box_klasses[T_INT] = WK_KLASS(Integer_klass);
never@1577 1999 _box_klasses[T_LONG] = WK_KLASS(Long_klass);
jrose@567 2000 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass);
jrose@567 2001 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass);
duke@435 2002
duke@435 2003 #ifdef KERNEL
never@3137 2004 if (DownloadManager_klass() == NULL) {
duke@435 2005 warning("Cannot find sun/jkernel/DownloadManager");
duke@435 2006 }
duke@435 2007 #endif // KERNEL
acorn@949 2008
duke@435 2009 { // Compute whether we should use loadClass or loadClassInternal when loading classes.
never@1577 2010 methodOop method = instanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
duke@435 2011 _has_loadClassInternal = (method != NULL);
duke@435 2012 }
duke@435 2013 { // Compute whether we should use checkPackageAccess or NOT
never@1577 2014 methodOop method = instanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
duke@435 2015 _has_checkPackageAccess = (method != NULL);
duke@435 2016 }
duke@435 2017 }
duke@435 2018
duke@435 2019 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
duke@435 2020 // If so, returns the basic type it holds. If not, returns T_OBJECT.
duke@435 2021 BasicType SystemDictionary::box_klass_type(klassOop k) {
duke@435 2022 assert(k != NULL, "");
duke@435 2023 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
duke@435 2024 if (_box_klasses[i] == k)
duke@435 2025 return (BasicType)i;
duke@435 2026 }
duke@435 2027 return T_OBJECT;
duke@435 2028 }
duke@435 2029
jrose@1100 2030 KlassHandle SystemDictionaryHandles::box_klass(BasicType t) {
jrose@1100 2031 if (t >= T_BOOLEAN && t <= T_VOID)
jrose@1100 2032 return KlassHandle(&SystemDictionary::_box_klasses[t], true);
jrose@1100 2033 else
jrose@1100 2034 return KlassHandle();
jrose@1100 2035 }
jrose@1100 2036
duke@435 2037 // Constraints on class loaders. The details of the algorithm can be
duke@435 2038 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
duke@435 2039 // Virtual Machine" by Sheng Liang and Gilad Bracha. The basic idea is
duke@435 2040 // that the system dictionary needs to maintain a set of contraints that
duke@435 2041 // must be satisfied by all classes in the dictionary.
duke@435 2042 // if defining is true, then LinkageError if already in systemDictionary
duke@435 2043 // if initiating loader, then ok if instanceKlass matches existing entry
duke@435 2044
duke@435 2045 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
duke@435 2046 instanceKlassHandle k,
duke@435 2047 Handle class_loader, bool defining,
duke@435 2048 TRAPS) {
duke@435 2049 const char *linkage_error = NULL;
duke@435 2050 {
coleenp@2497 2051 Symbol* name = k->name();
duke@435 2052 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 2053
duke@435 2054 klassOop check = find_class(d_index, d_hash, name, class_loader);
duke@435 2055 if (check != (klassOop)NULL) {
duke@435 2056 // if different instanceKlass - duplicate class definition,
duke@435 2057 // else - ok, class loaded by a different thread in parallel,
duke@435 2058 // we should only have found it if it was done loading and ok to use
duke@435 2059 // system dictionary only holds instance classes, placeholders
duke@435 2060 // also holds array classes
duke@435 2061
duke@435 2062 assert(check->klass_part()->oop_is_instance(), "noninstance in systemdictionary");
duke@435 2063 if ((defining == true) || (k() != check)) {
duke@435 2064 linkage_error = "loader (instance of %s): attempted duplicate class "
duke@435 2065 "definition for name: \"%s\"";
duke@435 2066 } else {
duke@435 2067 return;
duke@435 2068 }
duke@435 2069 }
duke@435 2070
duke@435 2071 #ifdef ASSERT
coleenp@2497 2072 Symbol* ph_check = find_placeholder(name, class_loader);
coleenp@2497 2073 assert(ph_check == NULL || ph_check == name, "invalid symbol");
duke@435 2074 #endif
duke@435 2075
duke@435 2076 if (linkage_error == NULL) {
duke@435 2077 if (constraints()->check_or_update(k, class_loader, name) == false) {
duke@435 2078 linkage_error = "loader constraint violation: loader (instance of %s)"
duke@435 2079 " previously initiated loading for a different type with name \"%s\"";
duke@435 2080 }
duke@435 2081 }
duke@435 2082 }
duke@435 2083
duke@435 2084 // Throw error now if needed (cannot throw while holding
duke@435 2085 // SystemDictionary_lock because of rank ordering)
duke@435 2086
duke@435 2087 if (linkage_error) {
duke@435 2088 ResourceMark rm(THREAD);
duke@435 2089 const char* class_loader_name = loader_name(class_loader());
duke@435 2090 char* type_name = k->name()->as_C_string();
duke@435 2091 size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
duke@435 2092 strlen(type_name);
duke@435 2093 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
duke@435 2094 jio_snprintf(buf, buflen, linkage_error, class_loader_name, type_name);
duke@435 2095 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
duke@435 2096 }
duke@435 2097 }
duke@435 2098
duke@435 2099
duke@435 2100 // Update system dictionary - done after check_constraint and add_to_hierachy
duke@435 2101 // have been called.
duke@435 2102 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
duke@435 2103 int p_index, unsigned int p_hash,
duke@435 2104 instanceKlassHandle k,
duke@435 2105 Handle class_loader,
duke@435 2106 TRAPS) {
duke@435 2107 // Compile_lock prevents systemDictionary updates during compilations
duke@435 2108 assert_locked_or_safepoint(Compile_lock);
coleenp@2497 2109 Symbol* name = k->name();
duke@435 2110
duke@435 2111 {
duke@435 2112 MutexLocker mu1(SystemDictionary_lock, THREAD);
duke@435 2113
duke@435 2114 // See whether biased locking is enabled and if so set it for this
duke@435 2115 // klass.
duke@435 2116 // Note that this must be done past the last potential blocking
duke@435 2117 // point / safepoint. We enable biased locking lazily using a
duke@435 2118 // VM_Operation to iterate the SystemDictionary and installing the
duke@435 2119 // biasable mark word into each instanceKlass's prototype header.
duke@435 2120 // To avoid race conditions where we accidentally miss enabling the
duke@435 2121 // optimization for one class in the process of being added to the
duke@435 2122 // dictionary, we must not safepoint after the test of
duke@435 2123 // BiasedLocking::enabled().
duke@435 2124 if (UseBiasedLocking && BiasedLocking::enabled()) {
duke@435 2125 // Set biased locking bit for all loaded classes; it will be
duke@435 2126 // cleared if revocation occurs too often for this type
duke@435 2127 // NOTE that we must only do this when the class is initally
duke@435 2128 // defined, not each time it is referenced from a new class loader
duke@435 2129 if (k->class_loader() == class_loader()) {
duke@435 2130 k->set_prototype_header(markOopDesc::biased_locking_prototype());
duke@435 2131 }
duke@435 2132 }
duke@435 2133
phh@3427 2134 // Assign a classid if one has not already been assigned. The
phh@3427 2135 // counter does not need to be atomically incremented since this
phh@3427 2136 // is only done while holding the SystemDictionary_lock.
phh@3427 2137 // All loaded classes get a unique ID.
phh@3427 2138 TRACE_INIT_ID(k);
phh@3427 2139
duke@435 2140 // Check for a placeholder. If there, remove it and make a
duke@435 2141 // new system dictionary entry.
duke@435 2142 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
duke@435 2143 klassOop sd_check = find_class(d_index, d_hash, name, class_loader);
duke@435 2144 if (sd_check == NULL) {
duke@435 2145 dictionary()->add_klass(name, class_loader, k);
duke@435 2146 notice_modification();
duke@435 2147 }
duke@435 2148 #ifdef ASSERT
duke@435 2149 sd_check = find_class(d_index, d_hash, name, class_loader);
duke@435 2150 assert (sd_check != NULL, "should have entry in system dictionary");
duke@435 2151 // Changed to allow PH to remain to complete class circularity checking
duke@435 2152 // while only one thread can define a class at one time, multiple
duke@435 2153 // classes can resolve the superclass for a class at one time,
duke@435 2154 // and the placeholder is used to track that
coleenp@2497 2155 // Symbol* ph_check = find_placeholder(name, class_loader);
duke@435 2156 // assert (ph_check == NULL, "should not have a placeholder entry");
duke@435 2157 #endif
duke@435 2158 SystemDictionary_lock->notify_all();
duke@435 2159 }
duke@435 2160 }
duke@435 2161
duke@435 2162
never@2551 2163 // Try to find a class name using the loader constraints. The
never@2551 2164 // loader constraints might know about a class that isn't fully loaded
never@2551 2165 // yet and these will be ignored.
duke@435 2166 klassOop SystemDictionary::find_constrained_instance_or_array_klass(
coleenp@2497 2167 Symbol* class_name, Handle class_loader, TRAPS) {
duke@435 2168
duke@435 2169 // First see if it has been loaded directly.
duke@435 2170 // Force the protection domain to be null. (This removes protection checks.)
duke@435 2171 Handle no_protection_domain;
duke@435 2172 klassOop klass = find_instance_or_array_klass(class_name, class_loader,
duke@435 2173 no_protection_domain, CHECK_NULL);
duke@435 2174 if (klass != NULL)
duke@435 2175 return klass;
duke@435 2176
duke@435 2177 // Now look to see if it has been loaded elsewhere, and is subject to
duke@435 2178 // a loader constraint that would require this loader to return the
duke@435 2179 // klass that is already loaded.
coleenp@2497 2180 if (FieldType::is_array(class_name)) {
acorn@1771 2181 // For array classes, their klassOops are not kept in the
acorn@1771 2182 // constraint table. The element klassOops are.
coleenp@2497 2183 FieldArrayInfo fd;
coleenp@2497 2184 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
duke@435 2185 if (t != T_OBJECT) {
duke@435 2186 klass = Universe::typeArrayKlassObj(t);
duke@435 2187 } else {
duke@435 2188 MutexLocker mu(SystemDictionary_lock, THREAD);
coleenp@2497 2189 klass = constraints()->find_constrained_klass(fd.object_key(), class_loader);
duke@435 2190 }
acorn@1771 2191 // If element class already loaded, allocate array klass
duke@435 2192 if (klass != NULL) {
coleenp@2497 2193 klass = Klass::cast(klass)->array_klass_or_null(fd.dimension());
duke@435 2194 }
duke@435 2195 } else {
duke@435 2196 MutexLocker mu(SystemDictionary_lock, THREAD);
duke@435 2197 // Non-array classes are easy: simply check the constraint table.
duke@435 2198 klass = constraints()->find_constrained_klass(class_name, class_loader);
duke@435 2199 }
duke@435 2200
duke@435 2201 return klass;
duke@435 2202 }
duke@435 2203
duke@435 2204
coleenp@2497 2205 bool SystemDictionary::add_loader_constraint(Symbol* class_name,
duke@435 2206 Handle class_loader1,
duke@435 2207 Handle class_loader2,
duke@435 2208 Thread* THREAD) {
coleenp@2497 2209 Symbol* constraint_name = NULL;
coleenp@2497 2210 if (!FieldType::is_array(class_name)) {
acorn@1771 2211 constraint_name = class_name;
acorn@1771 2212 } else {
acorn@1771 2213 // For array classes, their klassOops are not kept in the
acorn@1771 2214 // constraint table. The element classes are.
coleenp@2497 2215 FieldArrayInfo fd;
coleenp@2497 2216 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
acorn@1771 2217 // primitive types always pass
acorn@1771 2218 if (t != T_OBJECT) {
acorn@1771 2219 return true;
acorn@1771 2220 } else {
coleenp@2497 2221 constraint_name = fd.object_key();
acorn@1771 2222 }
acorn@1771 2223 }
acorn@1771 2224 unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, class_loader1);
duke@435 2225 int d_index1 = dictionary()->hash_to_index(d_hash1);
duke@435 2226
acorn@1771 2227 unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, class_loader2);
duke@435 2228 int d_index2 = dictionary()->hash_to_index(d_hash2);
acorn@1771 2229 {
acorn@1771 2230 MutexLocker mu_s(SystemDictionary_lock, THREAD);
duke@435 2231
acorn@1771 2232 // Better never do a GC while we're holding these oops
acorn@1771 2233 No_Safepoint_Verifier nosafepoint;
duke@435 2234
acorn@1771 2235 klassOop klass1 = find_class(d_index1, d_hash1, constraint_name, class_loader1);
acorn@1771 2236 klassOop klass2 = find_class(d_index2, d_hash2, constraint_name, class_loader2);
acorn@1771 2237 return constraints()->add_entry(constraint_name, klass1, class_loader1,
acorn@1771 2238 klass2, class_loader2);
duke@435 2239 }
duke@435 2240 }
duke@435 2241
duke@435 2242 // Add entry to resolution error table to record the error when the first
duke@435 2243 // attempt to resolve a reference to a class has failed.
coleenp@2497 2244 void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which, Symbol* error) {
duke@435 2245 unsigned int hash = resolution_errors()->compute_hash(pool, which);
duke@435 2246 int index = resolution_errors()->hash_to_index(hash);
duke@435 2247 {
duke@435 2248 MutexLocker ml(SystemDictionary_lock, Thread::current());
duke@435 2249 resolution_errors()->add_entry(index, hash, pool, which, error);
duke@435 2250 }
duke@435 2251 }
duke@435 2252
duke@435 2253 // Lookup resolution error table. Returns error if found, otherwise NULL.
coleenp@2497 2254 Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
duke@435 2255 unsigned int hash = resolution_errors()->compute_hash(pool, which);
duke@435 2256 int index = resolution_errors()->hash_to_index(hash);
duke@435 2257 {
duke@435 2258 MutexLocker ml(SystemDictionary_lock, Thread::current());
duke@435 2259 ResolutionErrorEntry* entry = resolution_errors()->find_entry(index, hash, pool, which);
coleenp@2497 2260 return (entry != NULL) ? entry->error() : (Symbol*)NULL;
duke@435 2261 }
duke@435 2262 }
duke@435 2263
duke@435 2264
jrose@1100 2265 // Signature constraints ensure that callers and callees agree about
jrose@1100 2266 // the meaning of type names in their signatures. This routine is the
jrose@1100 2267 // intake for constraints. It collects them from several places:
jrose@1100 2268 //
jrose@1100 2269 // * LinkResolver::resolve_method (if check_access is true) requires
jrose@1100 2270 // that the resolving class (the caller) and the defining class of
jrose@1100 2271 // the resolved method (the callee) agree on each type in the
jrose@1100 2272 // method's signature.
jrose@1100 2273 //
jrose@1100 2274 // * LinkResolver::resolve_interface_method performs exactly the same
jrose@1100 2275 // checks.
jrose@1100 2276 //
jrose@1100 2277 // * LinkResolver::resolve_field requires that the constant pool
jrose@1100 2278 // attempting to link to a field agree with the field's defining
jrose@1100 2279 // class about the type of the field signature.
jrose@1100 2280 //
jrose@1100 2281 // * klassVtable::initialize_vtable requires that, when a class
jrose@1100 2282 // overrides a vtable entry allocated by a superclass, that the
jrose@1100 2283 // overriding method (i.e., the callee) agree with the superclass
jrose@1100 2284 // on each type in the method's signature.
jrose@1100 2285 //
jrose@1100 2286 // * klassItable::initialize_itable requires that, when a class fills
jrose@1100 2287 // in its itables, for each non-abstract method installed in an
jrose@1100 2288 // itable, the method (i.e., the callee) agree with the interface
jrose@1100 2289 // on each type in the method's signature.
jrose@1100 2290 //
jrose@1100 2291 // All those methods have a boolean (check_access, checkconstraints)
jrose@1100 2292 // which turns off the checks. This is used from specialized contexts
jrose@1100 2293 // such as bootstrapping, dumping, and debugging.
jrose@1100 2294 //
jrose@1100 2295 // No direct constraint is placed between the class and its
jrose@1100 2296 // supertypes. Constraints are only placed along linked relations
jrose@1100 2297 // between callers and callees. When a method overrides or implements
jrose@1100 2298 // an abstract method in a supertype (superclass or interface), the
jrose@1100 2299 // constraints are placed as if the supertype were the caller to the
jrose@1100 2300 // overriding method. (This works well, since callers to the
jrose@1100 2301 // supertype have already established agreement between themselves and
jrose@1100 2302 // the supertype.) As a result of all this, a class can disagree with
jrose@1100 2303 // its supertype about the meaning of a type name, as long as that
jrose@1100 2304 // class neither calls a relevant method of the supertype, nor is
jrose@1100 2305 // called (perhaps via an override) from the supertype.
jrose@1100 2306 //
jrose@1100 2307 //
jrose@1100 2308 // SystemDictionary::check_signature_loaders(sig, l1, l2)
jrose@1100 2309 //
duke@435 2310 // Make sure all class components (including arrays) in the given
duke@435 2311 // signature will be resolved to the same class in both loaders.
duke@435 2312 // Returns the name of the type that failed a loader constraint check, or
duke@435 2313 // NULL if no constraint failed. The returned C string needs cleaning up
jrose@1100 2314 // with a ResourceMark in the caller. No exception except OOME is thrown.
acorn@1771 2315 // Arrays are not added to the loader constraint table, their elements are.
coleenp@2497 2316 char* SystemDictionary::check_signature_loaders(Symbol* signature,
duke@435 2317 Handle loader1, Handle loader2,
duke@435 2318 bool is_method, TRAPS) {
duke@435 2319 // Nothing to do if loaders are the same.
duke@435 2320 if (loader1() == loader2()) {
duke@435 2321 return NULL;
duke@435 2322 }
duke@435 2323
coleenp@2497 2324 ResourceMark rm(THREAD);
duke@435 2325 SignatureStream sig_strm(signature, is_method);
duke@435 2326 while (!sig_strm.is_done()) {
duke@435 2327 if (sig_strm.is_object()) {
coleenp@2497 2328 Symbol* s = sig_strm.as_symbol(CHECK_NULL);
coleenp@2497 2329 Symbol* sig = s;
duke@435 2330 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) {
coleenp@2497 2331 return sig->as_C_string();
duke@435 2332 }
duke@435 2333 }
duke@435 2334 sig_strm.next();
duke@435 2335 }
duke@435 2336 return NULL;
duke@435 2337 }
duke@435 2338
duke@435 2339
coleenp@2497 2340 methodOop SystemDictionary::find_method_handle_invoke(Symbol* name,
coleenp@2497 2341 Symbol* signature,
jrose@1863 2342 KlassHandle accessing_klass,
jrose@1145 2343 TRAPS) {
twisti@2698 2344 if (!EnableInvokeDynamic) return NULL;
coleenp@2497 2345 vmSymbols::SID name_id = vmSymbols::find_sid(name);
jrose@1862 2346 assert(name_id != vmSymbols::NO_SID, "must be a known name");
jrose@1862 2347 unsigned int hash = invoke_method_table()->compute_hash(signature, name_id);
jrose@1145 2348 int index = invoke_method_table()->hash_to_index(hash);
jrose@1862 2349 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
jrose@1863 2350 methodHandle non_cached_result;
jrose@1145 2351 if (spe == NULL || spe->property_oop() == NULL) {
jrose@1863 2352 spe = NULL;
jrose@1145 2353 // Must create lots of stuff here, but outside of the SystemDictionary lock.
twisti@1570 2354 if (THREAD->is_Compiler_thread())
twisti@1570 2355 return NULL; // do not attempt from within compiler
jrose@2742 2356 bool for_invokeGeneric = (name_id != vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name));
jrose@1863 2357 bool found_on_bcp = false;
coleenp@2497 2358 Handle mt = find_method_handle_type(signature, accessing_klass,
jrose@2148 2359 for_invokeGeneric,
jrose@2148 2360 found_on_bcp, CHECK_NULL);
jrose@1145 2361 KlassHandle mh_klass = SystemDictionaryHandles::MethodHandle_klass();
jrose@1862 2362 methodHandle m = methodOopDesc::make_invoke_method(mh_klass, name, signature,
jrose@1145 2363 mt, CHECK_NULL);
jrose@1145 2364 // Now grab the lock. We might have to throw away the new method,
jrose@1145 2365 // if a racing thread has managed to install one at the same time.
jrose@1863 2366 if (found_on_bcp) {
jrose@1145 2367 MutexLocker ml(SystemDictionary_lock, Thread::current());
jrose@1862 2368 spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
jrose@1145 2369 if (spe == NULL)
jrose@1862 2370 spe = invoke_method_table()->add_entry(index, hash, signature, name_id);
never@2895 2371 if (spe->property_oop() == NULL) {
jrose@1145 2372 spe->set_property_oop(m());
never@2895 2373 // Link m to his method type, if it is suitably generic.
never@2895 2374 oop mtform = java_lang_invoke_MethodType::form(mt());
never@2895 2375 if (mtform != NULL && mt() == java_lang_invoke_MethodTypeForm::erasedType(mtform)
jrose@2982 2376 // vmlayout must be an invokeExact:
jrose@2982 2377 && name_id == vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name)
never@2895 2378 && java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() > 0) {
never@2895 2379 java_lang_invoke_MethodTypeForm::init_vmlayout(mtform, m());
never@2895 2380 }
never@2895 2381 }
jrose@1863 2382 } else {
jrose@1863 2383 non_cached_result = m;
jrose@1145 2384 }
jrose@1145 2385 }
jrose@1863 2386 if (spe != NULL && spe->property_oop() != NULL) {
jrose@1863 2387 assert(spe->property_oop()->is_method(), "");
jrose@1863 2388 return (methodOop) spe->property_oop();
jrose@1863 2389 } else {
jrose@1863 2390 return non_cached_result();
jrose@1863 2391 }
jrose@1145 2392 }
jrose@1145 2393
jrose@2639 2394 // Ask Java code to find or construct a java.lang.invoke.MethodType for the given
jrose@1145 2395 // signature, as interpreted relative to the given class loader.
jrose@1145 2396 // Because of class loader constraints, all method handle usage must be
jrose@1145 2397 // consistent with this loader.
coleenp@2497 2398 Handle SystemDictionary::find_method_handle_type(Symbol* signature,
jrose@1863 2399 KlassHandle accessing_klass,
jrose@2148 2400 bool for_invokeGeneric,
jrose@1863 2401 bool& return_bcp_flag,
jrose@1862 2402 TRAPS) {
jrose@1863 2403 Handle class_loader, protection_domain;
jrose@1863 2404 bool is_on_bcp = true; // keep this true as long as we can materialize from the boot classloader
jrose@1145 2405 Handle empty;
coleenp@2497 2406 int npts = ArgumentCount(signature).size();
never@1577 2407 objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty));
jrose@1145 2408 int arg = 0;
jrose@1145 2409 Handle rt; // the return type from the signature
coleenp@2497 2410 ResourceMark rm(THREAD);
coleenp@2497 2411 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) {
jrose@1863 2412 oop mirror = NULL;
jrose@1863 2413 if (is_on_bcp) {
jrose@1863 2414 mirror = ss.as_java_mirror(class_loader, protection_domain,
jrose@1863 2415 SignatureStream::ReturnNull, CHECK_(empty));
jrose@1863 2416 if (mirror == NULL) {
jrose@1863 2417 // fall back from BCP to accessing_klass
jrose@1863 2418 if (accessing_klass.not_null()) {
jrose@1863 2419 class_loader = Handle(THREAD, instanceKlass::cast(accessing_klass())->class_loader());
jrose@1863 2420 protection_domain = Handle(THREAD, instanceKlass::cast(accessing_klass())->protection_domain());
jrose@1863 2421 }
jrose@1863 2422 is_on_bcp = false;
jrose@1863 2423 }
jrose@1863 2424 }
jrose@1863 2425 if (!is_on_bcp) {
jrose@1863 2426 // Resolve, throwing a real error if it doesn't work.
jrose@1863 2427 mirror = ss.as_java_mirror(class_loader, protection_domain,
jrose@1863 2428 SignatureStream::NCDFError, CHECK_(empty));
jrose@1145 2429 }
jrose@1145 2430 if (ss.at_return_type())
jrose@1145 2431 rt = Handle(THREAD, mirror);
jrose@1145 2432 else
jrose@1145 2433 pts->obj_at_put(arg++, mirror);
jrose@1863 2434 // Check accessibility.
jrose@1863 2435 if (ss.is_object() && accessing_klass.not_null()) {
jrose@1863 2436 klassOop sel_klass = java_lang_Class::as_klassOop(mirror);
jrose@1863 2437 // Emulate constantPoolOopDesc::verify_constant_pool_resolve.
jrose@1863 2438 if (Klass::cast(sel_klass)->oop_is_objArray())
jrose@1863 2439 sel_klass = objArrayKlass::cast(sel_klass)->bottom_klass();
jrose@1863 2440 if (Klass::cast(sel_klass)->oop_is_instance()) {
jrose@1863 2441 KlassHandle sel_kh(THREAD, sel_klass);
jrose@1863 2442 LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty));
jrose@1863 2443 }
jrose@1863 2444 }
jrose@1145 2445 }
jrose@1145 2446 assert(arg == npts, "");
jrose@1145 2447
jrose@2639 2448 // call java.lang.invoke.MethodHandleNatives::findMethodType(Class rt, Class[] pts) -> MethodType
jrose@1145 2449 JavaCallArguments args(Handle(THREAD, rt()));
jrose@1145 2450 args.push_oop(pts());
jrose@1145 2451 JavaValue result(T_OBJECT);
jrose@1145 2452 JavaCalls::call_static(&result,
jrose@1862 2453 SystemDictionary::MethodHandleNatives_klass(),
jrose@1862 2454 vmSymbols::findMethodHandleType_name(),
jrose@2742 2455 vmSymbols::findMethodHandleType_signature(),
jrose@1145 2456 &args, CHECK_(empty));
jrose@2148 2457 Handle method_type(THREAD, (oop) result.get_jobject());
jrose@2148 2458
jrose@2148 2459 if (for_invokeGeneric) {
jrose@2639 2460 // call java.lang.invoke.MethodHandleNatives::notifyGenericMethodType(MethodType) -> void
jrose@2148 2461 JavaCallArguments args(Handle(THREAD, method_type()));
jrose@2148 2462 JavaValue no_result(T_VOID);
jrose@2148 2463 JavaCalls::call_static(&no_result,
jrose@2148 2464 SystemDictionary::MethodHandleNatives_klass(),
jrose@2148 2465 vmSymbols::notifyGenericMethodType_name(),
jrose@2742 2466 vmSymbols::notifyGenericMethodType_signature(),
jrose@2148 2467 &args, THREAD);
jrose@2148 2468 if (HAS_PENDING_EXCEPTION) {
jrose@2148 2469 // If the notification fails, just kill it.
jrose@2148 2470 CLEAR_PENDING_EXCEPTION;
jrose@2148 2471 }
jrose@2148 2472 }
jrose@1863 2473
jrose@1863 2474 // report back to the caller with the MethodType and the "on_bcp" flag
jrose@1863 2475 return_bcp_flag = is_on_bcp;
jrose@2148 2476 return method_type;
jrose@1145 2477 }
jrose@1145 2478
jrose@1957 2479 // Ask Java code to find or construct a method handle constant.
jrose@1957 2480 Handle SystemDictionary::link_method_handle_constant(KlassHandle caller,
jrose@1957 2481 int ref_kind, //e.g., JVM_REF_invokeVirtual
jrose@1957 2482 KlassHandle callee,
coleenp@2497 2483 Symbol* name_sym,
coleenp@2497 2484 Symbol* signature,
jrose@1957 2485 TRAPS) {
jrose@1957 2486 Handle empty;
coleenp@2497 2487 Handle name = java_lang_String::create_from_symbol(name_sym, CHECK_(empty));
jrose@1957 2488 Handle type;
jrose@1957 2489 if (signature->utf8_length() > 0 && signature->byte_at(0) == '(') {
jrose@1957 2490 bool ignore_is_on_bcp = false;
jrose@2148 2491 type = find_method_handle_type(signature, caller, false, ignore_is_on_bcp, CHECK_(empty));
jrose@1957 2492 } else {
coleenp@2497 2493 ResourceMark rm(THREAD);
coleenp@2497 2494 SignatureStream ss(signature, false);
jrose@1957 2495 if (!ss.is_done()) {
jrose@1957 2496 oop mirror = ss.as_java_mirror(caller->class_loader(), caller->protection_domain(),
jrose@1957 2497 SignatureStream::NCDFError, CHECK_(empty));
jrose@1957 2498 type = Handle(THREAD, mirror);
jrose@1957 2499 ss.next();
jrose@1957 2500 if (!ss.is_done()) type = Handle(); // error!
jrose@1957 2501 }
jrose@1957 2502 }
jrose@1957 2503 if (type.is_null()) {
jrose@1957 2504 THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad signature", empty);
jrose@1957 2505 }
jrose@1957 2506
jrose@2639 2507 // call java.lang.invoke.MethodHandleNatives::linkMethodHandleConstant(Class caller, int refKind, Class callee, String name, Object type) -> MethodHandle
jrose@1957 2508 JavaCallArguments args;
jrose@1957 2509 args.push_oop(caller->java_mirror()); // the referring class
jrose@1957 2510 args.push_int(ref_kind);
jrose@1957 2511 args.push_oop(callee->java_mirror()); // the target class
jrose@1957 2512 args.push_oop(name());
jrose@1957 2513 args.push_oop(type());
jrose@1957 2514 JavaValue result(T_OBJECT);
jrose@1957 2515 JavaCalls::call_static(&result,
jrose@1957 2516 SystemDictionary::MethodHandleNatives_klass(),
jrose@1957 2517 vmSymbols::linkMethodHandleConstant_name(),
jrose@2742 2518 vmSymbols::linkMethodHandleConstant_signature(),
jrose@1957 2519 &args, CHECK_(empty));
jrose@1957 2520 return Handle(THREAD, (oop) result.get_jobject());
jrose@1957 2521 }
jrose@1145 2522
jrose@2639 2523 // Ask Java code to find or construct a java.lang.invoke.CallSite for the given
jrose@1161 2524 // name and signature, as interpreted relative to the given class loader.
jrose@1862 2525 Handle SystemDictionary::make_dynamic_call_site(Handle bootstrap_method,
coleenp@2497 2526 Symbol* name,
jrose@1862 2527 methodHandle signature_invoker,
jrose@1862 2528 Handle info,
jrose@1862 2529 methodHandle caller_method,
jrose@1161 2530 int caller_bci,
jrose@1161 2531 TRAPS) {
jrose@1161 2532 Handle empty;
jrose@2015 2533 guarantee(bootstrap_method.not_null() &&
jrose@2639 2534 java_lang_invoke_MethodHandle::is_instance(bootstrap_method()),
jrose@2015 2535 "caller must supply a valid BSM");
jrose@2015 2536
jrose@1862 2537 Handle caller_mname = MethodHandles::new_MemberName(CHECK_(empty));
jrose@1862 2538 MethodHandles::init_MemberName(caller_mname(), caller_method());
jrose@1862 2539
jrose@2639 2540 // call java.lang.invoke.MethodHandleNatives::makeDynamicCallSite(bootm, name, mtype, info, caller_mname, caller_pos)
coleenp@2497 2541 oop name_str_oop = StringTable::intern(name, CHECK_(empty)); // not a handle!
jrose@1862 2542 JavaCallArguments args(Handle(THREAD, bootstrap_method()));
jrose@1161 2543 args.push_oop(name_str_oop);
jrose@1862 2544 args.push_oop(signature_invoker->method_handle_type());
jrose@1862 2545 args.push_oop(info());
jrose@1862 2546 args.push_oop(caller_mname());
jrose@1161 2547 args.push_int(caller_bci);
jrose@1161 2548 JavaValue result(T_OBJECT);
jrose@1161 2549 JavaCalls::call_static(&result,
jrose@1862 2550 SystemDictionary::MethodHandleNatives_klass(),
jrose@1862 2551 vmSymbols::makeDynamicCallSite_name(),
jrose@2742 2552 vmSymbols::makeDynamicCallSite_signature(),
jrose@1161 2553 &args, CHECK_(empty));
jrose@1161 2554 oop call_site_oop = (oop) result.get_jobject();
jrose@1424 2555 assert(call_site_oop->is_oop()
jrose@2639 2556 /*&& java_lang_invoke_CallSite::is_instance(call_site_oop)*/, "must be sane");
jrose@1161 2557 if (TraceMethodHandles) {
jrose@1494 2558 #ifndef PRODUCT
jrose@1161 2559 tty->print_cr("Linked invokedynamic bci=%d site="INTPTR_FORMAT":", caller_bci, call_site_oop);
jrose@1161 2560 call_site_oop->print();
jrose@1161 2561 tty->cr();
jrose@1494 2562 #endif //PRODUCT
jrose@1161 2563 }
jrose@1161 2564 return call_site_oop;
jrose@1161 2565 }
jrose@1161 2566
jrose@2015 2567 Handle SystemDictionary::find_bootstrap_method(methodHandle caller_method, int caller_bci,
jrose@2268 2568 int cache_index,
jrose@2268 2569 Handle& argument_info_result,
jrose@2268 2570 TRAPS) {
jrose@1161 2571 Handle empty;
jrose@2015 2572
jrose@2015 2573 constantPoolHandle pool;
jrose@2015 2574 {
jrose@2015 2575 klassOop caller = caller_method->method_holder();
jrose@2015 2576 if (!Klass::cast(caller)->oop_is_instance()) return empty;
jrose@2015 2577 pool = constantPoolHandle(THREAD, instanceKlass::cast(caller)->constants());
jrose@2015 2578 }
jrose@2015 2579
jrose@2015 2580 int constant_pool_index = pool->cache()->entry_at(cache_index)->constant_pool_index();
jrose@2015 2581 constantTag tag = pool->tag_at(constant_pool_index);
jrose@2015 2582
jrose@2015 2583 if (tag.is_invoke_dynamic()) {
jrose@2268 2584 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
jrose@2015 2585 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
jrose@2015 2586 int bsm_index = pool->invoke_dynamic_bootstrap_method_ref_index_at(constant_pool_index);
jrose@2015 2587 if (bsm_index != 0) {
jrose@2015 2588 int bsm_index_in_cache = pool->cache()->entry_at(cache_index)->bootstrap_method_index_in_cache();
jrose@2015 2589 DEBUG_ONLY(int bsm_index_2 = pool->cache()->entry_at(bsm_index_in_cache)->constant_pool_index());
jrose@2015 2590 assert(bsm_index == bsm_index_2, "BSM constant lifted to cache");
jrose@2015 2591 if (TraceMethodHandles) {
jrose@2015 2592 tty->print_cr("resolving bootstrap method for "PTR_FORMAT" at %d at cache[%d]CP[%d]...",
jrose@2015 2593 (intptr_t) caller_method(), caller_bci, cache_index, constant_pool_index);
jrose@2015 2594 }
jrose@2015 2595 oop bsm_oop = pool->resolve_cached_constant_at(bsm_index_in_cache, CHECK_(empty));
jrose@2015 2596 if (TraceMethodHandles) {
jrose@2015 2597 tty->print_cr("bootstrap method for "PTR_FORMAT" at %d retrieved as "PTR_FORMAT":",
jrose@2015 2598 (intptr_t) caller_method(), caller_bci, (intptr_t) bsm_oop);
jrose@2015 2599 }
jrose@2268 2600 assert(bsm_oop->is_oop(), "must be sane");
jrose@2268 2601 // caller must verify that it is of type MethodHandle
jrose@2268 2602 Handle bsm(THREAD, bsm_oop);
jrose@2268 2603 bsm_oop = NULL; // safety
jrose@2268 2604
jrose@2268 2605 // Extract the optional static arguments.
jrose@2268 2606 Handle argument_info; // either null, or one arg, or Object[]{arg...}
jrose@2268 2607 int argc = pool->invoke_dynamic_argument_count_at(constant_pool_index);
jrose@2268 2608 if (TraceInvokeDynamic) {
jrose@2268 2609 tty->print_cr("find_bootstrap_method: [%d/%d] CONSTANT_InvokeDynamic: %d[%d]",
jrose@2268 2610 constant_pool_index, cache_index, bsm_index, argc);
jrose@2268 2611 }
jrose@2268 2612 if (argc > 0) {
jrose@2268 2613 objArrayHandle arg_array;
jrose@2268 2614 if (argc > 1) {
jrose@2268 2615 objArrayOop arg_array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), argc, CHECK_(empty));
jrose@2268 2616 arg_array = objArrayHandle(THREAD, arg_array_oop);
jrose@2268 2617 argument_info = arg_array;
jrose@2268 2618 }
jrose@2268 2619 for (int arg_i = 0; arg_i < argc; arg_i++) {
jrose@2268 2620 int arg_index = pool->invoke_dynamic_argument_index_at(constant_pool_index, arg_i);
jrose@2268 2621 oop arg_oop = pool->resolve_possibly_cached_constant_at(arg_index, CHECK_(empty));
jrose@2268 2622 if (arg_array.is_null()) {
jrose@2268 2623 argument_info = Handle(THREAD, arg_oop);
jrose@2268 2624 } else {
jrose@2268 2625 arg_array->obj_at_put(arg_i, arg_oop);
jrose@2268 2626 }
jrose@2268 2627 }
jrose@2268 2628 }
jrose@2268 2629
jrose@2268 2630 argument_info_result = argument_info; // return argument_info to caller
jrose@2268 2631 return bsm;
jrose@2015 2632 }
jrose@2015 2633 } else {
jrose@2015 2634 ShouldNotReachHere(); // verifier does not allow this
jrose@2015 2635 }
jrose@2015 2636
jrose@1862 2637 return empty;
jrose@1161 2638 }
jrose@1161 2639
duke@435 2640 // Since the identity hash code for symbols changes when the symbols are
duke@435 2641 // moved from the regular perm gen (hash in the mark word) to the shared
duke@435 2642 // spaces (hash is the address), the classes loaded into the dictionary
duke@435 2643 // may be in the wrong buckets.
duke@435 2644
duke@435 2645 void SystemDictionary::reorder_dictionary() {
duke@435 2646 dictionary()->reorder_dictionary();
duke@435 2647 }
duke@435 2648
duke@435 2649
duke@435 2650 void SystemDictionary::copy_buckets(char** top, char* end) {
duke@435 2651 dictionary()->copy_buckets(top, end);
duke@435 2652 }
duke@435 2653
duke@435 2654
duke@435 2655 void SystemDictionary::copy_table(char** top, char* end) {
duke@435 2656 dictionary()->copy_table(top, end);
duke@435 2657 }
duke@435 2658
duke@435 2659
duke@435 2660 void SystemDictionary::reverse() {
duke@435 2661 dictionary()->reverse();
duke@435 2662 }
duke@435 2663
duke@435 2664 int SystemDictionary::number_of_classes() {
duke@435 2665 return dictionary()->number_of_entries();
duke@435 2666 }
duke@435 2667
duke@435 2668
duke@435 2669 // ----------------------------------------------------------------------------
duke@435 2670 #ifndef PRODUCT
duke@435 2671
duke@435 2672 void SystemDictionary::print() {
duke@435 2673 dictionary()->print();
duke@435 2674
duke@435 2675 // Placeholders
duke@435 2676 GCMutexLocker mu(SystemDictionary_lock);
duke@435 2677 placeholders()->print();
duke@435 2678
duke@435 2679 // loader constraints - print under SD_lock
duke@435 2680 constraints()->print();
duke@435 2681 }
duke@435 2682
duke@435 2683 #endif
duke@435 2684
duke@435 2685 void SystemDictionary::verify() {
duke@435 2686 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
duke@435 2687 guarantee(constraints() != NULL,
duke@435 2688 "Verify of loader constraints failed");
duke@435 2689 guarantee(dictionary()->number_of_entries() >= 0 &&
duke@435 2690 placeholders()->number_of_entries() >= 0,
duke@435 2691 "Verify of system dictionary failed");
duke@435 2692
duke@435 2693 // Verify dictionary
duke@435 2694 dictionary()->verify();
duke@435 2695
duke@435 2696 GCMutexLocker mu(SystemDictionary_lock);
duke@435 2697 placeholders()->verify();
duke@435 2698
duke@435 2699 // Verify constraint table
duke@435 2700 guarantee(constraints() != NULL, "Verify of loader constraints failed");
tonyp@1693 2701 constraints()->verify(dictionary(), placeholders());
duke@435 2702 }
duke@435 2703
duke@435 2704
duke@435 2705 void SystemDictionary::verify_obj_klass_present(Handle obj,
coleenp@2497 2706 Symbol* class_name,
duke@435 2707 Handle class_loader) {
duke@435 2708 GCMutexLocker mu(SystemDictionary_lock);
coleenp@2497 2709 Symbol* name;
coleenp@2497 2710
coleenp@2497 2711 klassOop probe = find_class(class_name, class_loader);
duke@435 2712 if (probe == NULL) {
duke@435 2713 probe = SystemDictionary::find_shared_class(class_name);
coleenp@2497 2714 if (probe == NULL) {
coleenp@2497 2715 name = find_placeholder(class_name, class_loader);
coleenp@2497 2716 }
duke@435 2717 }
coleenp@2497 2718 guarantee(probe != NULL || name != NULL,
coleenp@2497 2719 "Loaded klasses should be in SystemDictionary");
duke@435 2720 }
duke@435 2721
duke@435 2722 #ifndef PRODUCT
duke@435 2723
duke@435 2724 // statistics code
duke@435 2725 class ClassStatistics: AllStatic {
duke@435 2726 private:
duke@435 2727 static int nclasses; // number of classes
duke@435 2728 static int nmethods; // number of methods
duke@435 2729 static int nmethoddata; // number of methodData
duke@435 2730 static int class_size; // size of class objects in words
duke@435 2731 static int method_size; // size of method objects in words
duke@435 2732 static int debug_size; // size of debug info in methods
duke@435 2733 static int methoddata_size; // size of methodData objects in words
duke@435 2734
duke@435 2735 static void do_class(klassOop k) {
duke@435 2736 nclasses++;
duke@435 2737 class_size += k->size();
duke@435 2738 if (k->klass_part()->oop_is_instance()) {
duke@435 2739 instanceKlass* ik = (instanceKlass*)k->klass_part();
duke@435 2740 class_size += ik->methods()->size();
duke@435 2741 class_size += ik->constants()->size();
duke@435 2742 class_size += ik->local_interfaces()->size();
duke@435 2743 class_size += ik->transitive_interfaces()->size();
duke@435 2744 // We do not have to count implementors, since we only store one!
never@3137 2745 class_size += ik->all_fields_count() * FieldInfo::field_slots;
duke@435 2746 }
duke@435 2747 }
duke@435 2748
duke@435 2749 static void do_method(methodOop m) {
duke@435 2750 nmethods++;
duke@435 2751 method_size += m->size();
duke@435 2752 // class loader uses same objArray for empty vectors, so don't count these
duke@435 2753 if (m->exception_table()->length() != 0) method_size += m->exception_table()->size();
duke@435 2754 if (m->has_stackmap_table()) {
duke@435 2755 method_size += m->stackmap_data()->size();
duke@435 2756 }
duke@435 2757
duke@435 2758 methodDataOop mdo = m->method_data();
duke@435 2759 if (mdo != NULL) {
duke@435 2760 nmethoddata++;
duke@435 2761 methoddata_size += mdo->size();
duke@435 2762 }
duke@435 2763 }
duke@435 2764
duke@435 2765 public:
duke@435 2766 static void print() {
duke@435 2767 SystemDictionary::classes_do(do_class);
duke@435 2768 SystemDictionary::methods_do(do_method);
duke@435 2769 tty->print_cr("Class statistics:");
duke@435 2770 tty->print_cr("%d classes (%d bytes)", nclasses, class_size * oopSize);
duke@435 2771 tty->print_cr("%d methods (%d bytes = %d base + %d debug info)", nmethods,
duke@435 2772 (method_size + debug_size) * oopSize, method_size * oopSize, debug_size * oopSize);
duke@435 2773 tty->print_cr("%d methoddata (%d bytes)", nmethoddata, methoddata_size * oopSize);
duke@435 2774 }
duke@435 2775 };
duke@435 2776
duke@435 2777
duke@435 2778 int ClassStatistics::nclasses = 0;
duke@435 2779 int ClassStatistics::nmethods = 0;
duke@435 2780 int ClassStatistics::nmethoddata = 0;
duke@435 2781 int ClassStatistics::class_size = 0;
duke@435 2782 int ClassStatistics::method_size = 0;
duke@435 2783 int ClassStatistics::debug_size = 0;
duke@435 2784 int ClassStatistics::methoddata_size = 0;
duke@435 2785
duke@435 2786 void SystemDictionary::print_class_statistics() {
duke@435 2787 ResourceMark rm;
duke@435 2788 ClassStatistics::print();
duke@435 2789 }
duke@435 2790
duke@435 2791
duke@435 2792 class MethodStatistics: AllStatic {
duke@435 2793 public:
duke@435 2794 enum {
duke@435 2795 max_parameter_size = 10
duke@435 2796 };
duke@435 2797 private:
duke@435 2798
duke@435 2799 static int _number_of_methods;
duke@435 2800 static int _number_of_final_methods;
duke@435 2801 static int _number_of_static_methods;
duke@435 2802 static int _number_of_native_methods;
duke@435 2803 static int _number_of_synchronized_methods;
duke@435 2804 static int _number_of_profiled_methods;
duke@435 2805 static int _number_of_bytecodes;
duke@435 2806 static int _parameter_size_profile[max_parameter_size];
duke@435 2807 static int _bytecodes_profile[Bytecodes::number_of_java_codes];
duke@435 2808
duke@435 2809 static void initialize() {
duke@435 2810 _number_of_methods = 0;
duke@435 2811 _number_of_final_methods = 0;
duke@435 2812 _number_of_static_methods = 0;
duke@435 2813 _number_of_native_methods = 0;
duke@435 2814 _number_of_synchronized_methods = 0;
duke@435 2815 _number_of_profiled_methods = 0;
duke@435 2816 _number_of_bytecodes = 0;
duke@435 2817 for (int i = 0; i < max_parameter_size ; i++) _parameter_size_profile[i] = 0;
duke@435 2818 for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile [j] = 0;
duke@435 2819 };
duke@435 2820
duke@435 2821 static void do_method(methodOop m) {
duke@435 2822 _number_of_methods++;
duke@435 2823 // collect flag info
duke@435 2824 if (m->is_final() ) _number_of_final_methods++;
duke@435 2825 if (m->is_static() ) _number_of_static_methods++;
duke@435 2826 if (m->is_native() ) _number_of_native_methods++;
duke@435 2827 if (m->is_synchronized()) _number_of_synchronized_methods++;
duke@435 2828 if (m->method_data() != NULL) _number_of_profiled_methods++;
duke@435 2829 // collect parameter size info (add one for receiver, if any)
duke@435 2830 _parameter_size_profile[MIN2(m->size_of_parameters() + (m->is_static() ? 0 : 1), max_parameter_size - 1)]++;
duke@435 2831 // collect bytecodes info
duke@435 2832 {
duke@435 2833 Thread *thread = Thread::current();
duke@435 2834 HandleMark hm(thread);
duke@435 2835 BytecodeStream s(methodHandle(thread, m));
duke@435 2836 Bytecodes::Code c;
duke@435 2837 while ((c = s.next()) >= 0) {
duke@435 2838 _number_of_bytecodes++;
duke@435 2839 _bytecodes_profile[c]++;
duke@435 2840 }
duke@435 2841 }
duke@435 2842 }
duke@435 2843
duke@435 2844 public:
duke@435 2845 static void print() {
duke@435 2846 initialize();
duke@435 2847 SystemDictionary::methods_do(do_method);
duke@435 2848 // generate output
duke@435 2849 tty->cr();
duke@435 2850 tty->print_cr("Method statistics (static):");
duke@435 2851 // flag distribution
duke@435 2852 tty->cr();
duke@435 2853 tty->print_cr("%6d final methods %6.1f%%", _number_of_final_methods , _number_of_final_methods * 100.0F / _number_of_methods);
duke@435 2854 tty->print_cr("%6d static methods %6.1f%%", _number_of_static_methods , _number_of_static_methods * 100.0F / _number_of_methods);
duke@435 2855 tty->print_cr("%6d native methods %6.1f%%", _number_of_native_methods , _number_of_native_methods * 100.0F / _number_of_methods);
duke@435 2856 tty->print_cr("%6d synchronized methods %6.1f%%", _number_of_synchronized_methods, _number_of_synchronized_methods * 100.0F / _number_of_methods);
duke@435 2857 tty->print_cr("%6d profiled methods %6.1f%%", _number_of_profiled_methods, _number_of_profiled_methods * 100.0F / _number_of_methods);
duke@435 2858 // parameter size profile
duke@435 2859 tty->cr();
duke@435 2860 { int tot = 0;
duke@435 2861 int avg = 0;
duke@435 2862 for (int i = 0; i < max_parameter_size; i++) {
duke@435 2863 int n = _parameter_size_profile[i];
duke@435 2864 tot += n;
duke@435 2865 avg += n*i;
duke@435 2866 tty->print_cr("parameter size = %1d: %6d methods %5.1f%%", i, n, n * 100.0F / _number_of_methods);
duke@435 2867 }
duke@435 2868 assert(tot == _number_of_methods, "should be the same");
duke@435 2869 tty->print_cr(" %6d methods 100.0%%", _number_of_methods);
duke@435 2870 tty->print_cr("(average parameter size = %3.1f including receiver, if any)", (float)avg / _number_of_methods);
duke@435 2871 }
duke@435 2872 // bytecodes profile
duke@435 2873 tty->cr();
duke@435 2874 { int tot = 0;
duke@435 2875 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
duke@435 2876 if (Bytecodes::is_defined(i)) {
duke@435 2877 Bytecodes::Code c = Bytecodes::cast(i);
duke@435 2878 int n = _bytecodes_profile[c];
duke@435 2879 tot += n;
duke@435 2880 tty->print_cr("%9d %7.3f%% %s", n, n * 100.0F / _number_of_bytecodes, Bytecodes::name(c));
duke@435 2881 }
duke@435 2882 }
duke@435 2883 assert(tot == _number_of_bytecodes, "should be the same");
duke@435 2884 tty->print_cr("%9d 100.000%%", _number_of_bytecodes);
duke@435 2885 }
duke@435 2886 tty->cr();
duke@435 2887 }
duke@435 2888 };
duke@435 2889
duke@435 2890 int MethodStatistics::_number_of_methods;
duke@435 2891 int MethodStatistics::_number_of_final_methods;
duke@435 2892 int MethodStatistics::_number_of_static_methods;
duke@435 2893 int MethodStatistics::_number_of_native_methods;
duke@435 2894 int MethodStatistics::_number_of_synchronized_methods;
duke@435 2895 int MethodStatistics::_number_of_profiled_methods;
duke@435 2896 int MethodStatistics::_number_of_bytecodes;
duke@435 2897 int MethodStatistics::_parameter_size_profile[MethodStatistics::max_parameter_size];
duke@435 2898 int MethodStatistics::_bytecodes_profile[Bytecodes::number_of_java_codes];
duke@435 2899
duke@435 2900
duke@435 2901 void SystemDictionary::print_method_statistics() {
duke@435 2902 MethodStatistics::print();
duke@435 2903 }
duke@435 2904
duke@435 2905 #endif // PRODUCT

mercurial