src/share/vm/classfile/systemDictionary.cpp

Tue, 07 Dec 2010 03:15:45 -0800

author
sla
date
Tue, 07 Dec 2010 03:15:45 -0800
changeset 2331
017cd8bce8a8
parent 2314
f95d63e2154a
child 2343
f2da85a9b08e
permissions
-rw-r--r--

6539281: -Xcheck:jni should validate char* argument to ReleaseStringUTFChars
Summary: Tag allocated memory with a magic value and verify when releasing.
Reviewed-by: phh, stefank

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

mercurial