src/share/vm/classfile/systemDictionary.cpp

Mon, 14 May 2012 09:36:00 -0700

author
kvn
date
Mon, 14 May 2012 09:36:00 -0700
changeset 3760
8f972594effc
parent 3491
b2cd0ee8f778
child 3803
71afdabfd05b
permissions
-rw-r--r--

6924259: Remove String.count/String.offset
Summary: Allow a version of String class that doesn't have count and offset fields.
Reviewed-by: never, coleenp

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

mercurial