aoqi@0: /* kbarrett@9099: * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/classLoaderData.inline.hpp" aoqi@0: #include "classfile/dictionary.hpp" aoqi@0: #include "classfile/javaClasses.hpp" aoqi@0: #include "classfile/loaderConstraints.hpp" aoqi@0: #include "classfile/placeholders.hpp" aoqi@0: #include "classfile/resolutionErrors.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" iklam@7089: #if INCLUDE_CDS iklam@7089: #include "classfile/sharedClassUtil.hpp" iklam@7089: #include "classfile/systemDictionaryShared.hpp" iklam@7089: #endif aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "interpreter/bytecodeStream.hpp" aoqi@0: #include "interpreter/interpreter.hpp" iklam@7089: #include "memory/filemap.hpp" aoqi@0: #include "memory/gcLocker.hpp" aoqi@0: #include "memory/oopFactory.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/instanceRefKlass.hpp" aoqi@0: #include "oops/klass.inline.hpp" aoqi@0: #include "oops/methodData.hpp" aoqi@0: #include "oops/objArrayKlass.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "oops/oop.inline2.hpp" aoqi@0: #include "oops/typeArrayKlass.hpp" aoqi@0: #include "prims/jvmtiEnvBase.hpp" aoqi@0: #include "prims/methodHandles.hpp" iveresov@7172: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/biasedLocking.hpp" aoqi@0: #include "runtime/fieldType.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "services/classLoadingService.hpp" aoqi@0: #include "services/threadService.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: #include "utilities/ticks.hpp" aoqi@0: #if INCLUDE_TRACE mgronlun@7367: #include "trace/tracing.hpp" aoqi@0: #endif aoqi@0: aoqi@0: Dictionary* SystemDictionary::_dictionary = NULL; aoqi@0: PlaceholderTable* SystemDictionary::_placeholders = NULL; aoqi@0: Dictionary* SystemDictionary::_shared_dictionary = NULL; aoqi@0: LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL; aoqi@0: ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL; aoqi@0: SymbolPropertyTable* SystemDictionary::_invoke_method_table = NULL; aoqi@0: aoqi@0: aoqi@0: int SystemDictionary::_number_of_modifications = 0; aoqi@0: int SystemDictionary::_sdgeneration = 0; aoqi@0: const int SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103, aoqi@0: 20201,40423,99991}; aoqi@0: aoqi@0: oop SystemDictionary::_system_loader_lock_obj = NULL; aoqi@0: aoqi@0: Klass* SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT] aoqi@0: = { NULL /*, NULL...*/ }; aoqi@0: aoqi@0: Klass* SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ }; aoqi@0: aoqi@0: oop SystemDictionary::_java_system_loader = NULL; aoqi@0: aoqi@0: bool SystemDictionary::_has_loadClassInternal = false; aoqi@0: bool SystemDictionary::_has_checkPackageAccess = false; aoqi@0: aoqi@0: // lazily initialized klass variables aoqi@0: Klass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL; aoqi@0: aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Java-level SystemLoader aoqi@0: aoqi@0: oop SystemDictionary::java_system_loader() { aoqi@0: return _java_system_loader; aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::compute_java_system_loader(TRAPS) { aoqi@0: KlassHandle system_klass(THREAD, WK_KLASS(ClassLoader_klass)); aoqi@0: JavaValue result(T_OBJECT); aoqi@0: JavaCalls::call_static(&result, aoqi@0: KlassHandle(THREAD, WK_KLASS(ClassLoader_klass)), aoqi@0: vmSymbols::getSystemClassLoader_name(), aoqi@0: vmSymbols::void_classloader_signature(), aoqi@0: CHECK); aoqi@0: aoqi@0: _java_system_loader = (oop)result.get_jobject(); iklam@7089: iklam@7089: CDS_ONLY(SystemDictionaryShared::initialize(CHECK);) aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, TRAPS) { aoqi@0: if (class_loader() == NULL) return ClassLoaderData::the_null_class_loader_data(); aoqi@0: return ClassLoaderDataGraph::find_or_create(class_loader, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // debugging aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: // return true if class_name contains no '.' (internal format is '/') aoqi@0: bool SystemDictionary::is_internal_format(Symbol* class_name) { aoqi@0: if (class_name != NULL) { aoqi@0: ResourceMark rm; aoqi@0: char* name = class_name->as_C_string(); aoqi@0: return strchr(name, '.') == NULL; aoqi@0: } else { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Parallel class loading check aoqi@0: aoqi@0: bool SystemDictionary::is_parallelCapable(Handle class_loader) { aoqi@0: if (UnsyncloadClass || class_loader.is_null()) return true; aoqi@0: if (AlwaysLockClassLoader) return false; aoqi@0: return java_lang_ClassLoader::parallelCapable(class_loader()); aoqi@0: } aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // ParallelDefineClass flag does not apply to bootclass loader aoqi@0: bool SystemDictionary::is_parallelDefine(Handle class_loader) { aoqi@0: if (class_loader.is_null()) return false; aoqi@0: if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) { aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the passed class loader is the extension class loader. aoqi@0: */ aoqi@0: bool SystemDictionary::is_ext_class_loader(Handle class_loader) { aoqi@0: if (class_loader.is_null()) { aoqi@0: return false; aoqi@0: } aoqi@0: return (class_loader->klass()->name() == vmSymbols::sun_misc_Launcher_ExtClassLoader()); aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Resolving of classes aoqi@0: aoqi@0: // Forwards to resolve_or_null aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) { aoqi@0: Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION || klass == NULL) { aoqi@0: KlassHandle k_h(THREAD, klass); aoqi@0: // can return a null klass aoqi@0: klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD); aoqi@0: } aoqi@0: return klass; aoqi@0: } aoqi@0: aoqi@0: Klass* SystemDictionary::handle_resolution_exception(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) { aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: // If we have a pending exception we forward it to the caller, unless throw_error is true, aoqi@0: // in which case we have to check whether the pending exception is a ClassNotFoundException, aoqi@0: // and if so convert it to a NoClassDefFoundError aoqi@0: // And chain the original ClassNotFoundException aoqi@0: if (throw_error && PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass())) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: assert(klass_h() == NULL, "Should not have result with exception pending"); aoqi@0: Handle e(THREAD, PENDING_EXCEPTION); aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string(), e); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: // Class not found, throw appropriate error or exception depending on value of throw_error aoqi@0: if (klass_h() == NULL) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: if (throw_error) { aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string()); aoqi@0: } else { aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string()); aoqi@0: } aoqi@0: } aoqi@0: return (Klass*)klass_h(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, aoqi@0: bool throw_error, TRAPS) aoqi@0: { aoqi@0: return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Forwards to resolve_instance_class_or_null aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) { aoqi@0: assert(!THREAD->is_Compiler_thread(), aoqi@0: err_msg("can not load classes with compiler thread: class=%s, classloader=%s", aoqi@0: class_name->as_C_string(), aoqi@0: class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string())); aoqi@0: if (FieldType::is_array(class_name)) { aoqi@0: return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL); aoqi@0: } else if (FieldType::is_obj(class_name)) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: // Ignore wrapping L and ;. aoqi@0: TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1, aoqi@0: class_name->utf8_length() - 2, CHECK_NULL); aoqi@0: return resolve_instance_class_or_null(name, class_loader, protection_domain, CHECK_NULL); aoqi@0: } else { aoqi@0: return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_or_null(Symbol* class_name, TRAPS) { aoqi@0: return resolve_or_null(class_name, Handle(), Handle(), THREAD); aoqi@0: } aoqi@0: aoqi@0: // Forwards to resolve_instance_class_or_null aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: TRAPS) { aoqi@0: assert(FieldType::is_array(class_name), "must be array"); aoqi@0: Klass* k = NULL; aoqi@0: FieldArrayInfo fd; aoqi@0: // dimension and object_key in FieldArrayInfo are assigned as a side-effect aoqi@0: // of this call aoqi@0: BasicType t = FieldType::get_array_info(class_name, fd, CHECK_NULL); aoqi@0: if (t == T_OBJECT) { aoqi@0: // naked oop "k" is OK here -- we assign back into it aoqi@0: k = SystemDictionary::resolve_instance_class_or_null(fd.object_key(), aoqi@0: class_loader, aoqi@0: protection_domain, aoqi@0: CHECK_NULL); aoqi@0: if (k != NULL) { aoqi@0: k = k->array_klass(fd.dimension(), CHECK_NULL); aoqi@0: } aoqi@0: } else { aoqi@0: k = Universe::typeArrayKlassObj(t); aoqi@0: k = TypeArrayKlass::cast(k)->array_klass(fd.dimension(), CHECK_NULL); aoqi@0: } aoqi@0: return k; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Must be called for any super-class or super-interface resolution aoqi@0: // during class definition to allow class circularity checking aoqi@0: // super-interface callers: aoqi@0: // parse_interfaces - for defineClass & jvmtiRedefineClasses aoqi@0: // super-class callers: aoqi@0: // ClassFileParser - for defineClass & jvmtiRedefineClasses aoqi@0: // load_shared_class - while loading a class from shared archive aoqi@0: // resolve_instance_class_or_null: aoqi@0: // via: handle_parallel_super_load aoqi@0: // when resolving a class that has an existing placeholder with aoqi@0: // a saved superclass [i.e. a defineClass is currently in progress] aoqi@0: // if another thread is trying to resolve the class, it must do aoqi@0: // super-class checks on its own thread to catch class circularity aoqi@0: // This last call is critical in class circularity checking for cases aoqi@0: // where classloading is delegated to different threads and the aoqi@0: // classloader lock is released. aoqi@0: // Take the case: Base->Super->Base aoqi@0: // 1. If thread T1 tries to do a defineClass of class Base aoqi@0: // resolve_super_or_fail creates placeholder: T1, Base (super Super) aoqi@0: // 2. resolve_instance_class_or_null does not find SD or placeholder for Super aoqi@0: // so it tries to load Super aoqi@0: // 3. If we load the class internally, or user classloader uses same thread aoqi@0: // loadClassFromxxx or defineClass via parseClassFile Super ... aoqi@0: // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base) aoqi@0: // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base aoqi@0: // 3.4 calls resolve_super_or_fail Base aoqi@0: // 3.5 finds T1,Base -> throws class circularity aoqi@0: //OR 4. If T2 tries to resolve Super via defineClass Super ... aoqi@0: // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base) aoqi@0: // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super) aoqi@0: // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2 aoqi@0: // 4.4 finds T2, Super -> throws class circularity aoqi@0: // Must be called, even if superclass is null, since this is aoqi@0: // where the placeholder entry is created which claims this aoqi@0: // thread is loading this class/classloader. aoqi@0: Klass* SystemDictionary::resolve_super_or_fail(Symbol* child_name, aoqi@0: Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: bool is_superclass, aoqi@0: TRAPS) { aoqi@0: // Double-check, if child class is already loaded, just return super-class,interface aoqi@0: // Don't add a placedholder if already loaded, i.e. already in system dictionary aoqi@0: // Make sure there's a placeholder for the *child* before resolving. aoqi@0: // Used as a claim that this thread is currently loading superclass/classloader aoqi@0: // Used here for ClassCircularity checks and also for heap verification aoqi@0: // (every InstanceKlass in the heap needs to be in the system dictionary aoqi@0: // or have a placeholder). aoqi@0: // Must check ClassCircularity before checking if super class is already loaded aoqi@0: // aoqi@0: // We might not already have a placeholder if this child_name was aoqi@0: // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass); aoqi@0: // the name of the class might not be known until the stream is actually aoqi@0: // parsed. aoqi@0: // Bugs 4643874, 4715493 aoqi@0: // compute_hash can have a safepoint aoqi@0: aoqi@0: ClassLoaderData* loader_data = class_loader_data(class_loader); aoqi@0: unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: // can't throw error holding a lock aoqi@0: bool child_already_loaded = false; aoqi@0: bool throw_circularity_error = false; aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: Klass* childk = find_class(d_index, d_hash, child_name, loader_data); aoqi@0: Klass* quicksuperk; aoqi@0: // to support // loading: if child done loading, just return superclass aoqi@0: // if class_name, & class_loader don't match: aoqi@0: // if initial define, SD update will give LinkageError aoqi@0: // if redefine: compare_class_versions will give HIERARCHY_CHANGED aoqi@0: // so we don't throw an exception here. aoqi@0: // see: nsk redefclass014 & java.lang.instrument Instrument032 aoqi@0: if ((childk != NULL ) && (is_superclass) && aoqi@0: ((quicksuperk = InstanceKlass::cast(childk)->super()) != NULL) && aoqi@0: aoqi@0: ((quicksuperk->name() == class_name) && aoqi@0: (quicksuperk->class_loader() == class_loader()))) { aoqi@0: return quicksuperk; aoqi@0: } else { aoqi@0: PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data); aoqi@0: if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) { aoqi@0: throw_circularity_error = true; aoqi@0: } aoqi@0: } aoqi@0: if (!throw_circularity_error) { aoqi@0: PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, class_name, THREAD); aoqi@0: } aoqi@0: } aoqi@0: if (throw_circularity_error) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string()); aoqi@0: } aoqi@0: aoqi@0: // java.lang.Object should have been found above aoqi@0: assert(class_name != NULL, "null super class for resolving"); aoqi@0: // Resolve the super class or interface, check results on return aoqi@0: Klass* superk = SystemDictionary::resolve_or_null(class_name, aoqi@0: class_loader, aoqi@0: protection_domain, aoqi@0: THREAD); aoqi@0: aoqi@0: KlassHandle superk_h(THREAD, superk); aoqi@0: aoqi@0: // Clean up of placeholders moved so that each classloadAction registrar self-cleans up aoqi@0: // It is no longer necessary to keep the placeholder table alive until update_dictionary aoqi@0: // or error. GC used to walk the placeholder table as strong roots. aoqi@0: // The instanceKlass is kept alive because the class loader is on the stack, aoqi@0: // which keeps the loader_data alive, as well as all instanceKlasses in aoqi@0: // the loader_data. parseClassFile adds the instanceKlass to loader_data. aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: placeholders()->find_and_remove(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, THREAD); aoqi@0: SystemDictionary_lock->notify_all(); aoqi@0: } aoqi@0: if (HAS_PENDING_EXCEPTION || superk_h() == NULL) { aoqi@0: // can null superk aoqi@0: superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD)); aoqi@0: } aoqi@0: aoqi@0: return superk_h(); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::validate_protection_domain(instanceKlassHandle klass, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: TRAPS) { aoqi@0: if(!has_checkPackageAccess()) return; aoqi@0: aoqi@0: // Now we have to call back to java to check if the initating class has access aoqi@0: JavaValue result(T_VOID); aoqi@0: if (TraceProtectionDomainVerification) { aoqi@0: // Print out trace information aoqi@0: tty->print_cr("Checking package access"); aoqi@0: tty->print(" - class loader: "); class_loader()->print_value_on(tty); tty->cr(); aoqi@0: tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr(); aoqi@0: tty->print(" - loading: "); klass()->print_value_on(tty); tty->cr(); aoqi@0: } aoqi@0: aoqi@0: KlassHandle system_loader(THREAD, SystemDictionary::ClassLoader_klass()); aoqi@0: JavaCalls::call_special(&result, aoqi@0: class_loader, aoqi@0: system_loader, aoqi@0: vmSymbols::checkPackageAccess_name(), aoqi@0: vmSymbols::class_protectiondomain_signature(), aoqi@0: Handle(THREAD, klass->java_mirror()), aoqi@0: protection_domain, aoqi@0: THREAD); aoqi@0: aoqi@0: if (TraceProtectionDomainVerification) { aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: tty->print_cr(" -> DENIED !!!!!!!!!!!!!!!!!!!!!"); aoqi@0: } else { aoqi@0: tty->print_cr(" -> granted"); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: if (HAS_PENDING_EXCEPTION) return; aoqi@0: aoqi@0: // If no exception has been thrown, we have validated the protection domain aoqi@0: // Insert the protection domain of the initiating class into the set. aoqi@0: { aoqi@0: // We recalculate the entry here -- we've called out to java since aoqi@0: // the last time it was calculated. aoqi@0: ClassLoaderData* loader_data = class_loader_data(class_loader); aoqi@0: aoqi@0: Symbol* kn = klass->name(); aoqi@0: unsigned int d_hash = dictionary()->compute_hash(kn, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: { aoqi@0: // Note that we have an entry, and entries can be deleted only during GC, aoqi@0: // so we cannot allow GC to occur while we're holding this entry. aoqi@0: aoqi@0: // We're using a No_Safepoint_Verifier to catch any place where we aoqi@0: // might potentially do a GC at all. aoqi@0: // Dictionary::do_unloading() asserts that classes in SD are only aoqi@0: // unloaded at a safepoint. Anonymous classes are not in SD. aoqi@0: No_Safepoint_Verifier nosafepoint; aoqi@0: dictionary()->add_protection_domain(d_index, d_hash, klass, loader_data, aoqi@0: protection_domain, THREAD); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We only get here if this thread finds that another thread aoqi@0: // has already claimed the placeholder token for the current operation, aoqi@0: // but that other thread either never owned or gave up the aoqi@0: // object lock aoqi@0: // Waits on SystemDictionary_lock to indicate placeholder table updated aoqi@0: // On return, caller must recheck placeholder table state aoqi@0: // aoqi@0: // We only get here if aoqi@0: // 1) custom classLoader, i.e. not bootstrap classloader aoqi@0: // 2) UnsyncloadClass not set aoqi@0: // 3) custom classLoader has broken the class loader objectLock aoqi@0: // so another thread got here in parallel aoqi@0: // aoqi@0: // lockObject must be held. aoqi@0: // Complicated dance due to lock ordering: aoqi@0: // Must first release the classloader object lock to aoqi@0: // allow initial definer to complete the class definition aoqi@0: // and to avoid deadlock aoqi@0: // Reclaim classloader lock object with same original recursion count aoqi@0: // Must release SystemDictionary_lock after notify, since aoqi@0: // class loader lock must be claimed before SystemDictionary_lock aoqi@0: // to prevent deadlocks aoqi@0: // aoqi@0: // The notify allows applications that did an untimed wait() on aoqi@0: // the classloader object lock to not hang. aoqi@0: void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) { aoqi@0: assert_lock_strong(SystemDictionary_lock); aoqi@0: aoqi@0: bool calledholdinglock aoqi@0: = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject); aoqi@0: assert(calledholdinglock,"must hold lock for notify"); aoqi@0: assert((!(lockObject() == _system_loader_lock_obj) && !is_parallelCapable(lockObject)), "unexpected double_lock_wait"); aoqi@0: ObjectSynchronizer::notifyall(lockObject, THREAD); aoqi@0: intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD); aoqi@0: SystemDictionary_lock->wait(); aoqi@0: SystemDictionary_lock->unlock(); aoqi@0: ObjectSynchronizer::reenter(lockObject, recursions, THREAD); aoqi@0: SystemDictionary_lock->lock(); aoqi@0: } aoqi@0: aoqi@0: // If the class in is in the placeholder table, class loading is in progress aoqi@0: // For cases where the application changes threads to load classes, it aoqi@0: // is critical to ClassCircularity detection that we try loading aoqi@0: // the superclass on the same thread internally, so we do parallel aoqi@0: // super class loading here. aoqi@0: // This also is critical in cases where the original thread gets stalled aoqi@0: // even in non-circularity situations. aoqi@0: // Note: must call resolve_super_or_fail even if null super - aoqi@0: // to force placeholder entry creation for this class for circularity detection aoqi@0: // Caller must check for pending exception aoqi@0: // Returns non-null Klass* if other thread has completed load aoqi@0: // and we are done, aoqi@0: // If return null Klass* and no pending exception, the caller must load the class aoqi@0: instanceKlassHandle SystemDictionary::handle_parallel_super_load( aoqi@0: Symbol* name, Symbol* superclassname, Handle class_loader, aoqi@0: Handle protection_domain, Handle lockObject, TRAPS) { aoqi@0: aoqi@0: instanceKlassHandle nh = instanceKlassHandle(); // null Handle aoqi@0: ClassLoaderData* loader_data = class_loader_data(class_loader); aoqi@0: unsigned int d_hash = dictionary()->compute_hash(name, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: unsigned int p_hash = placeholders()->compute_hash(name, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: aoqi@0: // superk is not used, resolve_super called for circularity check only aoqi@0: // This code is reached in two situations. One if this thread aoqi@0: // is loading the same class twice (e.g. ClassCircularity, or aoqi@0: // java.lang.instrument). aoqi@0: // The second is if another thread started the resolve_super first aoqi@0: // and has not yet finished. aoqi@0: // In both cases the original caller will clean up the placeholder aoqi@0: // entry on error. aoqi@0: Klass* superk = SystemDictionary::resolve_super_or_fail(name, aoqi@0: superclassname, aoqi@0: class_loader, aoqi@0: protection_domain, aoqi@0: true, aoqi@0: CHECK_(nh)); aoqi@0: aoqi@0: // parallelCapable class loaders do NOT wait for parallel superclass loads to complete aoqi@0: // Serial class loaders and bootstrap classloader do wait for superclass loads aoqi@0: if (!class_loader.is_null() && is_parallelCapable(class_loader)) { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: // Check if classloading completed while we were loading superclass or waiting aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so just return it aoqi@0: return(instanceKlassHandle(THREAD, check)); aoqi@0: } else { aoqi@0: return nh; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // must loop to both handle other placeholder updates aoqi@0: // and spurious notifications aoqi@0: bool super_load_in_progress = true; aoqi@0: PlaceholderEntry* placeholder; aoqi@0: while (super_load_in_progress) { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: // Check if classloading completed while we were loading superclass or waiting aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so just return it aoqi@0: return(instanceKlassHandle(THREAD, check)); aoqi@0: } else { aoqi@0: placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data); aoqi@0: if (placeholder && placeholder->super_load_in_progress() ){ aoqi@0: // Before UnsyncloadClass: aoqi@0: // We only get here if the application has released the aoqi@0: // classloader lock when another thread was in the middle of loading a aoqi@0: // superclass/superinterface for this class, and now aoqi@0: // this thread is also trying to load this class. aoqi@0: // To minimize surprises, the first thread that started to aoqi@0: // load a class should be the one to complete the loading aoqi@0: // with the classfile it initially expected. aoqi@0: // This logic has the current thread wait once it has done aoqi@0: // all the superclass/superinterface loading it can, until aoqi@0: // the original thread completes the class loading or fails aoqi@0: // If it completes we will use the resulting InstanceKlass aoqi@0: // which we will find below in the systemDictionary. aoqi@0: // We also get here for parallel bootstrap classloader aoqi@0: if (class_loader.is_null()) { aoqi@0: SystemDictionary_lock->wait(); aoqi@0: } else { aoqi@0: double_lock_wait(lockObject, THREAD); aoqi@0: } aoqi@0: } else { aoqi@0: // If not in SD and not in PH, other thread's load must have failed aoqi@0: super_load_in_progress = false; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return (nh); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: TRAPS) { aoqi@0: assert(name != NULL && !FieldType::is_array(name) && aoqi@0: !FieldType::is_obj(name), "invalid class name"); aoqi@0: aoqi@0: Ticks class_load_start_time = Ticks::now(); aoqi@0: aoqi@0: // UseNewReflection aoqi@0: // Fix for 4474172; see evaluation for more details aoqi@0: class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); aoqi@0: ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL); aoqi@0: aoqi@0: // Do lookup to see if class already exist and the protection domain aoqi@0: // has the right access aoqi@0: // This call uses find which checks protection domain already matches aoqi@0: // All subsequent calls use find_class, and set has_loaded_class so that aoqi@0: // before we return a result we call out to java to check for valid protection domain aoqi@0: // to allow returning the Klass* and add it to the pd_set if it is valid aoqi@0: unsigned int d_hash = dictionary()->compute_hash(name, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data, aoqi@0: protection_domain, THREAD); aoqi@0: if (probe != NULL) return probe; aoqi@0: aoqi@0: aoqi@0: // Non-bootstrap class loaders will call out to class loader and aoqi@0: // define via jvm/jni_DefineClass which will acquire the aoqi@0: // class loader object lock to protect against multiple threads aoqi@0: // defining the class in parallel by accident. aoqi@0: // This lock must be acquired here so the waiter will find aoqi@0: // any successful result in the SystemDictionary and not attempt aoqi@0: // the define aoqi@0: // ParallelCapable Classloaders and the bootstrap classloader, aoqi@0: // or all classloaders with UnsyncloadClass do not acquire lock here aoqi@0: bool DoObjectLock = true; aoqi@0: if (is_parallelCapable(class_loader)) { aoqi@0: DoObjectLock = false; aoqi@0: } aoqi@0: aoqi@0: unsigned int p_hash = placeholders()->compute_hash(name, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: aoqi@0: // Class is not in SystemDictionary so we have to do loading. aoqi@0: // Make sure we are synchronized on the class loader before we proceed aoqi@0: Handle lockObject = compute_loader_lock_object(class_loader, THREAD); aoqi@0: check_loader_lock_contention(lockObject, THREAD); aoqi@0: ObjectLocker ol(lockObject, THREAD, DoObjectLock); aoqi@0: aoqi@0: // Check again (after locking) if class already exist in SystemDictionary aoqi@0: bool class_has_been_loaded = false; aoqi@0: bool super_load_in_progress = false; aoqi@0: bool havesupername = false; aoqi@0: instanceKlassHandle k; aoqi@0: PlaceholderEntry* placeholder; aoqi@0: Symbol* superclassname = NULL; aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so just return it aoqi@0: class_has_been_loaded = true; aoqi@0: k = instanceKlassHandle(THREAD, check); aoqi@0: } else { aoqi@0: placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data); aoqi@0: if (placeholder && placeholder->super_load_in_progress()) { aoqi@0: super_load_in_progress = true; aoqi@0: if (placeholder->havesupername() == true) { aoqi@0: superclassname = placeholder->supername(); aoqi@0: havesupername = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // If the class is in the placeholder table, class loading is in progress aoqi@0: if (super_load_in_progress && havesupername==true) { aoqi@0: k = SystemDictionary::handle_parallel_super_load(name, superclassname, aoqi@0: class_loader, protection_domain, lockObject, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: return NULL; aoqi@0: } aoqi@0: if (!k.is_null()) { aoqi@0: class_has_been_loaded = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool throw_circularity_error = false; aoqi@0: if (!class_has_been_loaded) { aoqi@0: bool load_instance_added = false; aoqi@0: aoqi@0: // add placeholder entry to record loading instance class aoqi@0: // Five cases: aoqi@0: // All cases need to prevent modifying bootclasssearchpath aoqi@0: // in parallel with a classload of same classname aoqi@0: // Redefineclasses uses existence of the placeholder for the duration aoqi@0: // of the class load to prevent concurrent redefinition of not completely aoqi@0: // defined classes. aoqi@0: // case 1. traditional classloaders that rely on the classloader object lock aoqi@0: // - no other need for LOAD_INSTANCE aoqi@0: // case 2. traditional classloaders that break the classloader object lock aoqi@0: // as a deadlock workaround. Detection of this case requires that aoqi@0: // this check is done while holding the classloader object lock, aoqi@0: // and that lock is still held when calling classloader's loadClass. aoqi@0: // For these classloaders, we ensure that the first requestor aoqi@0: // completes the load and other requestors wait for completion. aoqi@0: // case 3. UnsyncloadClass - don't use objectLocker aoqi@0: // With this flag, we allow parallel classloading of a aoqi@0: // class/classloader pair aoqi@0: // case4. Bootstrap classloader - don't own objectLocker aoqi@0: // This classloader supports parallelism at the classloader level, aoqi@0: // but only allows a single load of a class/classloader pair. aoqi@0: // No performance benefit and no deadlock issues. aoqi@0: // case 5. parallelCapable user level classloaders - without objectLocker aoqi@0: // Allow parallel classloading of a class/classloader pair aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: if (class_loader.is_null() || !is_parallelCapable(class_loader)) { aoqi@0: PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data); aoqi@0: if (oldprobe) { aoqi@0: // only need check_seen_thread once, not on each loop aoqi@0: // 6341374 java/lang/Instrument with -Xcomp aoqi@0: if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) { aoqi@0: throw_circularity_error = true; aoqi@0: } else { aoqi@0: // case 1: traditional: should never see load_in_progress. aoqi@0: while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) { aoqi@0: aoqi@0: // case 4: bootstrap classloader: prevent futile classloading, aoqi@0: // wait on first requestor aoqi@0: if (class_loader.is_null()) { aoqi@0: SystemDictionary_lock->wait(); aoqi@0: } else { aoqi@0: // case 2: traditional with broken classloader lock. wait on first aoqi@0: // requestor. aoqi@0: double_lock_wait(lockObject, THREAD); aoqi@0: } aoqi@0: // Check if classloading completed while we were waiting aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so just return it aoqi@0: k = instanceKlassHandle(THREAD, check); aoqi@0: class_has_been_loaded = true; aoqi@0: } aoqi@0: // check if other thread failed to load and cleaned up aoqi@0: oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // All cases: add LOAD_INSTANCE holding SystemDictionary_lock aoqi@0: // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try aoqi@0: // LOAD_INSTANCE in parallel aoqi@0: aoqi@0: if (!throw_circularity_error && !class_has_been_loaded) { aoqi@0: PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD); aoqi@0: load_instance_added = true; aoqi@0: // For class loaders that do not acquire the classloader object lock, aoqi@0: // if they did not catch another thread holding LOAD_INSTANCE, aoqi@0: // need a check analogous to the acquire ObjectLocker/find_class aoqi@0: // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL aoqi@0: // one final check if the load has already completed aoqi@0: // class loaders holding the ObjectLock shouldn't find the class here aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so return it after checking/adding protection domain aoqi@0: k = instanceKlassHandle(THREAD, check); aoqi@0: class_has_been_loaded = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // must throw error outside of owning lock aoqi@0: if (throw_circularity_error) { aoqi@0: assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup"); aoqi@0: ResourceMark rm(THREAD); aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string()); aoqi@0: } aoqi@0: aoqi@0: if (!class_has_been_loaded) { aoqi@0: aoqi@0: // Do actual loading aoqi@0: k = load_instance_class(name, class_loader, THREAD); aoqi@0: aoqi@0: // For UnsyncloadClass only aoqi@0: // If they got a linkageError, check if a parallel class load succeeded. aoqi@0: // If it did, then for bytecode resolution the specification requires aoqi@0: // that we return the same result we did for the other thread, i.e. the aoqi@0: // successfully loaded InstanceKlass aoqi@0: // Should not get here for classloaders that support parallelism aoqi@0: // with the new cleaner mechanism, even with AllowParallelDefineClass aoqi@0: // Bootstrap goes through here to allow for an extra guarantee check aoqi@0: if (UnsyncloadClass || (class_loader.is_null())) { aoqi@0: if (k.is_null() && HAS_PENDING_EXCEPTION aoqi@0: && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != NULL) { aoqi@0: // Klass is already loaded, so just use it aoqi@0: k = instanceKlassHandle(THREAD, check); aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // If everything was OK (no exceptions, no null return value), and aoqi@0: // class_loader is NOT the defining loader, do a little more bookkeeping. aoqi@0: if (!HAS_PENDING_EXCEPTION && !k.is_null() && aoqi@0: k->class_loader() != class_loader()) { aoqi@0: aoqi@0: check_constraints(d_index, d_hash, k, class_loader, false, THREAD); aoqi@0: aoqi@0: // Need to check for a PENDING_EXCEPTION again; check_constraints aoqi@0: // can throw and doesn't use the CHECK macro. aoqi@0: if (!HAS_PENDING_EXCEPTION) { aoqi@0: { // Grabbing the Compile_lock prevents systemDictionary updates aoqi@0: // during compilations. aoqi@0: MutexLocker mu(Compile_lock, THREAD); aoqi@0: update_dictionary(d_index, d_hash, p_index, p_hash, aoqi@0: k, class_loader, THREAD); aoqi@0: } aoqi@0: aoqi@0: if (JvmtiExport::should_post_class_load()) { aoqi@0: Thread *thread = THREAD; aoqi@0: assert(thread->is_Java_thread(), "thread->is_Java_thread()"); aoqi@0: JvmtiExport::post_class_load((JavaThread *) thread, k()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } // load_instance_class loop aoqi@0: aoqi@0: if (load_instance_added == true) { aoqi@0: // clean up placeholder entries for LOAD_INSTANCE success or error aoqi@0: // This brackets the SystemDictionary updates for both defining aoqi@0: // and initiating loaders aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD); aoqi@0: SystemDictionary_lock->notify_all(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (HAS_PENDING_EXCEPTION || k.is_null()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: post_class_load_event(class_load_start_time, k, class_loader); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: { aoqi@0: ClassLoaderData* loader_data = k->class_loader_data(); aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: Klass* kk = find_class(name, loader_data); aoqi@0: assert(kk == k(), "should be present in dictionary"); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // return if the protection domain in NULL aoqi@0: if (protection_domain() == NULL) return k(); aoqi@0: aoqi@0: // Check the protection domain has the right access aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: // Note that we have an entry, and entries can be deleted only during GC, aoqi@0: // so we cannot allow GC to occur while we're holding this entry. aoqi@0: // We're using a No_Safepoint_Verifier to catch any place where we aoqi@0: // might potentially do a GC at all. aoqi@0: // Dictionary::do_unloading() asserts that classes in SD are only aoqi@0: // unloaded at a safepoint. Anonymous classes are not in SD. aoqi@0: No_Safepoint_Verifier nosafepoint; aoqi@0: if (dictionary()->is_valid_protection_domain(d_index, d_hash, name, aoqi@0: loader_data, aoqi@0: protection_domain)) { aoqi@0: return k(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Verify protection domain. If it fails an exception is thrown aoqi@0: validate_protection_domain(k, class_loader, protection_domain, CHECK_NULL); aoqi@0: aoqi@0: return k(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // This routine does not lock the system dictionary. aoqi@0: // aoqi@0: // Since readers don't hold a lock, we must make sure that system aoqi@0: // dictionary entries are only removed at a safepoint (when only one aoqi@0: // thread is running), and are added to in a safe way (all links must aoqi@0: // be updated in an MT-safe manner). aoqi@0: // aoqi@0: // Callers should be aware that an entry could be added just after aoqi@0: // _dictionary->bucket(index) is read here, so the caller will not see aoqi@0: // the new entry. aoqi@0: aoqi@0: Klass* SystemDictionary::find(Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: TRAPS) { aoqi@0: aoqi@0: // UseNewReflection aoqi@0: // The result of this call should be consistent with the result aoqi@0: // of the call to resolve_instance_class_or_null(). aoqi@0: // See evaluation 6790209 and 4474172 for more details. aoqi@0: class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); aoqi@0: ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(class_loader()); aoqi@0: aoqi@0: if (loader_data == NULL) { aoqi@0: // If the ClassLoaderData has not been setup, aoqi@0: // then the class loader has no entries in the dictionary. aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: aoqi@0: { aoqi@0: // Note that we have an entry, and entries can be deleted only during GC, aoqi@0: // so we cannot allow GC to occur while we're holding this entry. aoqi@0: // We're using a No_Safepoint_Verifier to catch any place where we aoqi@0: // might potentially do a GC at all. aoqi@0: // Dictionary::do_unloading() asserts that classes in SD are only aoqi@0: // unloaded at a safepoint. Anonymous classes are not in SD. aoqi@0: No_Safepoint_Verifier nosafepoint; aoqi@0: return dictionary()->find(d_index, d_hash, class_name, loader_data, aoqi@0: protection_domain, THREAD); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Look for a loaded instance or array klass by name. Do not do any loading. aoqi@0: // return NULL in case of error. aoqi@0: Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: TRAPS) { aoqi@0: Klass* k = NULL; aoqi@0: assert(class_name != NULL, "class name must be non NULL"); aoqi@0: aoqi@0: if (FieldType::is_array(class_name)) { aoqi@0: // The name refers to an array. Parse the name. aoqi@0: // dimension and object_key in FieldArrayInfo are assigned as a aoqi@0: // side-effect of this call aoqi@0: FieldArrayInfo fd; aoqi@0: BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL)); aoqi@0: if (t != T_OBJECT) { aoqi@0: k = Universe::typeArrayKlassObj(t); aoqi@0: } else { aoqi@0: k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD); aoqi@0: } aoqi@0: if (k != NULL) { aoqi@0: k = k->array_klass_or_null(fd.dimension()); aoqi@0: } aoqi@0: } else { aoqi@0: k = find(class_name, class_loader, protection_domain, THREAD); aoqi@0: } aoqi@0: return k; aoqi@0: } aoqi@0: aoqi@0: // Note: this method is much like resolve_from_stream, but aoqi@0: // updates no supplemental data structures. aoqi@0: // TODO consolidate the two methods with a helper routine? aoqi@0: Klass* SystemDictionary::parse_stream(Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: ClassFileStream* st, aoqi@0: KlassHandle host_klass, aoqi@0: GrowableArray* cp_patches, aoqi@0: TRAPS) { aoqi@0: TempNewSymbol parsed_name = NULL; aoqi@0: aoqi@0: Ticks class_load_start_time = Ticks::now(); aoqi@0: aoqi@0: ClassLoaderData* loader_data; aoqi@0: if (host_klass.not_null()) { aoqi@0: // Create a new CLD for anonymous class, that uses the same class loader aoqi@0: // as the host_klass aoqi@0: assert(EnableInvokeDynamic, ""); aoqi@0: guarantee(host_klass->class_loader() == class_loader(), "should be the same"); iklam@7089: guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping"); aoqi@0: loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL); aoqi@0: loader_data->record_dependency(host_klass(), CHECK_NULL); aoqi@0: } else { aoqi@0: loader_data = ClassLoaderData::class_loader_data(class_loader()); aoqi@0: } aoqi@0: aoqi@0: // Parse the stream. Note that we do this even though this klass might aoqi@0: // already be present in the SystemDictionary, otherwise we would not aoqi@0: // throw potential ClassFormatErrors. aoqi@0: // aoqi@0: // Note: "name" is updated. aoqi@0: aoqi@0: instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name, aoqi@0: loader_data, aoqi@0: protection_domain, aoqi@0: host_klass, aoqi@0: cp_patches, aoqi@0: parsed_name, aoqi@0: true, aoqi@0: THREAD); aoqi@0: aoqi@0: aoqi@0: if (host_klass.not_null() && k.not_null()) { aoqi@0: assert(EnableInvokeDynamic, ""); aoqi@0: // If it's anonymous, initialize it now, since nobody else will. aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu_r(Compile_lock, THREAD); aoqi@0: aoqi@0: // Add to class hierarchy, initialize vtables, and do possible aoqi@0: // deoptimizations. aoqi@0: add_to_hierarchy(k, CHECK_NULL); // No exception, but can block aoqi@0: aoqi@0: // But, do not add to system dictionary. aoqi@0: aoqi@0: // compiled code dependencies need to be validated anyway aoqi@0: notice_modification(); aoqi@0: } aoqi@0: aoqi@0: // Rewrite and patch constant pool here. aoqi@0: k->link_class(CHECK_NULL); aoqi@0: if (cp_patches != NULL) { aoqi@0: k->constants()->patch_resolved_references(cp_patches); aoqi@0: } aoqi@0: k->eager_initialize(CHECK_NULL); aoqi@0: aoqi@0: // notify jvmti aoqi@0: if (JvmtiExport::should_post_class_load()) { aoqi@0: assert(THREAD->is_Java_thread(), "thread->is_Java_thread()"); aoqi@0: JvmtiExport::post_class_load((JavaThread *) THREAD, k()); aoqi@0: } aoqi@0: aoqi@0: post_class_load_event(class_load_start_time, k, class_loader); aoqi@0: } aoqi@0: assert(host_klass.not_null() || cp_patches == NULL, aoqi@0: "cp_patches only found with host_klass"); aoqi@0: aoqi@0: return k(); aoqi@0: } aoqi@0: aoqi@0: // Add a klass to the system from a stream (called by jni_DefineClass and aoqi@0: // JVM_DefineClass). aoqi@0: // Note: class_name can be NULL. In that case we do not know the name of aoqi@0: // the class until we have parsed the stream. aoqi@0: aoqi@0: Klass* SystemDictionary::resolve_from_stream(Symbol* class_name, aoqi@0: Handle class_loader, aoqi@0: Handle protection_domain, aoqi@0: ClassFileStream* st, aoqi@0: bool verify, aoqi@0: TRAPS) { aoqi@0: aoqi@0: // Classloaders that support parallelism, e.g. bootstrap classloader, aoqi@0: // or all classloaders with UnsyncloadClass do not acquire lock here aoqi@0: bool DoObjectLock = true; aoqi@0: if (is_parallelCapable(class_loader)) { aoqi@0: DoObjectLock = false; aoqi@0: } aoqi@0: aoqi@0: ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL); aoqi@0: aoqi@0: // Make sure we are synchronized on the class loader before we proceed aoqi@0: Handle lockObject = compute_loader_lock_object(class_loader, THREAD); aoqi@0: check_loader_lock_contention(lockObject, THREAD); aoqi@0: ObjectLocker ol(lockObject, THREAD, DoObjectLock); aoqi@0: aoqi@0: TempNewSymbol parsed_name = NULL; aoqi@0: aoqi@0: // Parse the stream. Note that we do this even though this klass might aoqi@0: // already be present in the SystemDictionary, otherwise we would not aoqi@0: // throw potential ClassFormatErrors. aoqi@0: // aoqi@0: // Note: "name" is updated. aoqi@0: aoqi@0: instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name, aoqi@0: loader_data, aoqi@0: protection_domain, aoqi@0: parsed_name, aoqi@0: verify, aoqi@0: THREAD); aoqi@0: aoqi@0: const char* pkg = "java/"; shshahma@8621: size_t pkglen = strlen(pkg); aoqi@0: if (!HAS_PENDING_EXCEPTION && aoqi@0: !class_loader.is_null() && aoqi@0: parsed_name != NULL && shshahma@8621: parsed_name->utf8_length() >= (int)pkglen && shshahma@8621: !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) { aoqi@0: // It is illegal to define classes in the "java." package from aoqi@0: // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader aoqi@0: ResourceMark rm(THREAD); aoqi@0: char* name = parsed_name->as_C_string(); aoqi@0: char* index = strrchr(name, '/'); shshahma@8621: assert(index != NULL, "must be"); aoqi@0: *index = '\0'; // chop to just the package name aoqi@0: while ((index = strchr(name, '/')) != NULL) { aoqi@0: *index = '.'; // replace '/' with '.' in package name aoqi@0: } aoqi@0: const char* fmt = "Prohibited package name: %s"; aoqi@0: size_t len = strlen(fmt) + strlen(name); aoqi@0: char* message = NEW_RESOURCE_ARRAY(char, len); aoqi@0: jio_snprintf(message, len, fmt, name); aoqi@0: Exceptions::_throw_msg(THREAD_AND_LOCATION, aoqi@0: vmSymbols::java_lang_SecurityException(), message); aoqi@0: } aoqi@0: aoqi@0: if (!HAS_PENDING_EXCEPTION) { aoqi@0: assert(parsed_name != NULL, "Sanity"); aoqi@0: assert(class_name == NULL || class_name == parsed_name, "name mismatch"); aoqi@0: // Verification prevents us from creating names with dots in them, this aoqi@0: // asserts that that's the case. aoqi@0: assert(is_internal_format(parsed_name), aoqi@0: "external class name format used internally"); aoqi@0: aoqi@0: // Add class just loaded aoqi@0: // If a class loader supports parallel classloading handle parallel define requests aoqi@0: // find_or_define_instance_class may return a different InstanceKlass aoqi@0: if (is_parallelCapable(class_loader)) { aoqi@0: k = find_or_define_instance_class(class_name, class_loader, k, THREAD); aoqi@0: } else { aoqi@0: define_instance_class(k, THREAD); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Make sure we have an entry in the SystemDictionary on success aoqi@0: debug_only( { aoqi@0: if (!HAS_PENDING_EXCEPTION) { aoqi@0: assert(parsed_name != NULL, "parsed_name is still null?"); aoqi@0: Symbol* h_name = k->name(); aoqi@0: ClassLoaderData *defining_loader_data = k->class_loader_data(); aoqi@0: aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: aoqi@0: Klass* check = find_class(parsed_name, loader_data); aoqi@0: assert(check == k(), "should be present in the dictionary"); aoqi@0: aoqi@0: Klass* check2 = find_class(h_name, defining_loader_data); aoqi@0: assert(check == check2, "name inconsistancy in SystemDictionary"); aoqi@0: } aoqi@0: } ); aoqi@0: aoqi@0: return k(); aoqi@0: } aoqi@0: iklam@7089: #if INCLUDE_CDS aoqi@0: void SystemDictionary::set_shared_dictionary(HashtableBucket* t, int length, aoqi@0: int number_of_entries) { aoqi@0: assert(length == _nof_buckets * sizeof(HashtableBucket), aoqi@0: "bad shared dictionary size."); aoqi@0: _shared_dictionary = new Dictionary(_nof_buckets, t, number_of_entries); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // If there is a shared dictionary, then find the entry for the aoqi@0: // given shared system class, if any. aoqi@0: aoqi@0: Klass* SystemDictionary::find_shared_class(Symbol* class_name) { aoqi@0: if (shared_dictionary() != NULL) { aoqi@0: unsigned int d_hash = shared_dictionary()->compute_hash(class_name, NULL); aoqi@0: int d_index = shared_dictionary()->hash_to_index(d_hash); aoqi@0: aoqi@0: return shared_dictionary()->find_shared_class(d_index, d_hash, class_name); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Load a class from the shared spaces (found through the shared system aoqi@0: // dictionary). Force the superclass and all interfaces to be loaded. aoqi@0: // Update the class definition to include sibling classes and no aoqi@0: // subclasses (yet). [Classes in the shared space are not part of the aoqi@0: // object hierarchy until loaded.] aoqi@0: aoqi@0: instanceKlassHandle SystemDictionary::load_shared_class( aoqi@0: Symbol* class_name, Handle class_loader, TRAPS) { aoqi@0: instanceKlassHandle ik (THREAD, find_shared_class(class_name)); iklam@7089: // Make sure we only return the boot class for the NULL classloader. iklam@7089: if (ik.not_null() && iklam@7089: SharedClassUtil::is_shared_boot_class(ik()) && class_loader.is_null()) { iklam@7089: Handle protection_domain; iklam@7089: return load_shared_class(ik, class_loader, protection_domain, THREAD); iklam@7089: } iklam@7089: return instanceKlassHandle(); aoqi@0: } aoqi@0: iklam@7089: instanceKlassHandle SystemDictionary::load_shared_class(instanceKlassHandle ik, iklam@7089: Handle class_loader, iklam@7089: Handle protection_domain, TRAPS) { aoqi@0: if (ik.not_null()) { aoqi@0: instanceKlassHandle nh = instanceKlassHandle(); // null Handle iklam@7089: Symbol* class_name = ik->name(); aoqi@0: aoqi@0: // Found the class, now load the superclass and interfaces. If they aoqi@0: // are shared, add them to the main system dictionary and reset aoqi@0: // their hierarchy references (supers, subs, and interfaces). aoqi@0: aoqi@0: if (ik->super() != NULL) { aoqi@0: Symbol* cn = ik->super()->name(); iklam@8497: Klass *s = resolve_super_or_fail(class_name, cn, iklam@8497: class_loader, protection_domain, true, CHECK_(nh)); iklam@8497: if (s != ik->super()) { iklam@8497: // The dynamically resolved super class is not the same as the one we used during dump time, iklam@8497: // so we cannot use ik. iklam@8497: return nh; iklam@8497: } aoqi@0: } aoqi@0: aoqi@0: Array* interfaces = ik->local_interfaces(); aoqi@0: int num_interfaces = interfaces->length(); aoqi@0: for (int index = 0; index < num_interfaces; index++) { aoqi@0: Klass* k = interfaces->at(index); aoqi@0: aoqi@0: // Note: can not use InstanceKlass::cast here because aoqi@0: // interfaces' InstanceKlass's C++ vtbls haven't been aoqi@0: // reinitialized yet (they will be once the interface classes aoqi@0: // are loaded) aoqi@0: Symbol* name = k->name(); iklam@8497: Klass* i = resolve_super_or_fail(class_name, name, class_loader, protection_domain, false, CHECK_(nh)); iklam@8497: if (k != i) { iklam@8497: // The dynamically resolved interface class is not the same as the one we used during dump time, iklam@8497: // so we cannot use ik. iklam@8497: return nh; iklam@8497: } aoqi@0: } aoqi@0: aoqi@0: // Adjust methods to recover missing data. They need addresses for aoqi@0: // interpreter entry points and their default native method address aoqi@0: // must be reset. aoqi@0: aoqi@0: // Updating methods must be done under a lock so multiple aoqi@0: // threads don't update these in parallel iklam@7089: // iklam@7089: // Shared classes are all currently loaded by either the bootstrap or iklam@7089: // internal parallel class loaders, so this will never cause a deadlock iklam@7089: // on a custom class loader lock. aoqi@0: iklam@7089: ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); aoqi@0: { aoqi@0: Handle lockObject = compute_loader_lock_object(class_loader, THREAD); aoqi@0: check_loader_lock_contention(lockObject, THREAD); aoqi@0: ObjectLocker ol(lockObject, THREAD, true); iklam@7089: ik->restore_unshareable_info(loader_data, protection_domain, CHECK_(nh)); aoqi@0: } aoqi@0: aoqi@0: if (TraceClassLoading) { aoqi@0: ResourceMark rm; aoqi@0: tty->print("[Loaded %s", ik->external_name()); aoqi@0: tty->print(" from shared objects file"); iklam@7089: if (class_loader.not_null()) { iklam@7089: tty->print(" by %s", loader_data->loader_name()); iklam@7089: } aoqi@0: tty->print_cr("]"); aoqi@0: } iklam@7089: iklam@7089: if (DumpLoadedClassList != NULL && classlist_file->is_open()) { iklam@7089: // Only dump the classes that can be stored into CDS archive iklam@7089: if (SystemDictionaryShared::is_sharing_possible(loader_data)) { iklam@7089: ResourceMark rm(THREAD); iklam@7089: classlist_file->print_cr("%s", ik->name()->as_C_string()); iklam@7089: classlist_file->flush(); iklam@7089: } iklam@7089: } iklam@7089: aoqi@0: // notify a class loaded from shared object aoqi@0: ClassLoadingService::notify_class_loaded(InstanceKlass::cast(ik()), aoqi@0: true /* shared class */); aoqi@0: } aoqi@0: return ik; aoqi@0: } iklam@7090: #endif // INCLUDE_CDS aoqi@0: aoqi@0: instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) { aoqi@0: instanceKlassHandle nh = instanceKlassHandle(); // null Handle aoqi@0: if (class_loader.is_null()) { aoqi@0: aoqi@0: // Search the shared system dictionary for classes preloaded into the aoqi@0: // shared spaces. aoqi@0: instanceKlassHandle k; aoqi@0: { iklam@7089: #if INCLUDE_CDS aoqi@0: PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time()); aoqi@0: k = load_shared_class(class_name, class_loader, THREAD); iklam@7089: #endif aoqi@0: } aoqi@0: aoqi@0: if (k.is_null()) { aoqi@0: // Use VM class loader aoqi@0: PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time()); aoqi@0: k = ClassLoader::load_classfile(class_name, CHECK_(nh)); aoqi@0: } aoqi@0: aoqi@0: // find_or_define_instance_class may return a different InstanceKlass aoqi@0: if (!k.is_null()) { aoqi@0: k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh)); aoqi@0: } aoqi@0: return k; aoqi@0: } else { aoqi@0: // Use user specified class loader to load class. Call loadClass operation on class_loader. aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: assert(THREAD->is_Java_thread(), "must be a JavaThread"); aoqi@0: JavaThread* jt = (JavaThread*) THREAD; aoqi@0: aoqi@0: PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(), aoqi@0: ClassLoader::perf_app_classload_selftime(), aoqi@0: ClassLoader::perf_app_classload_count(), aoqi@0: jt->get_thread_stat()->perf_recursion_counts_addr(), aoqi@0: jt->get_thread_stat()->perf_timers_addr(), aoqi@0: PerfClassTraceTime::CLASS_LOAD); aoqi@0: aoqi@0: Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh)); aoqi@0: // Translate to external class name format, i.e., convert '/' chars to '.' aoqi@0: Handle string = java_lang_String::externalize_classname(s, CHECK_(nh)); aoqi@0: aoqi@0: JavaValue result(T_OBJECT); aoqi@0: aoqi@0: KlassHandle spec_klass (THREAD, SystemDictionary::ClassLoader_klass()); aoqi@0: aoqi@0: // Call public unsynchronized loadClass(String) directly for all class loaders aoqi@0: // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will aoqi@0: // acquire a class-name based lock rather than the class loader object lock. aoqi@0: // JDK < 7 already acquire the class loader lock in loadClass(String, boolean), aoqi@0: // so the call to loadClassInternal() was not required. aoqi@0: // aoqi@0: // UnsyncloadClass flag means both call loadClass(String) and do aoqi@0: // not acquire the class loader lock even for class loaders that are aoqi@0: // not parallelCapable. This was a risky transitional aoqi@0: // flag for diagnostic purposes only. It is risky to call aoqi@0: // custom class loaders without synchronization. aoqi@0: // WARNING If a custom class loader does NOT synchronizer findClass, or callers of aoqi@0: // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field. aoqi@0: // Do NOT assume this will be supported in future releases. aoqi@0: // aoqi@0: // Added MustCallLoadClassInternal in case we discover in the field aoqi@0: // a customer that counts on this call aoqi@0: if (MustCallLoadClassInternal && has_loadClassInternal()) { aoqi@0: JavaCalls::call_special(&result, aoqi@0: class_loader, aoqi@0: spec_klass, aoqi@0: vmSymbols::loadClassInternal_name(), aoqi@0: vmSymbols::string_class_signature(), aoqi@0: string, aoqi@0: CHECK_(nh)); aoqi@0: } else { aoqi@0: JavaCalls::call_virtual(&result, aoqi@0: class_loader, aoqi@0: spec_klass, aoqi@0: vmSymbols::loadClass_name(), aoqi@0: vmSymbols::string_class_signature(), aoqi@0: string, aoqi@0: CHECK_(nh)); aoqi@0: } aoqi@0: aoqi@0: assert(result.get_type() == T_OBJECT, "just checking"); aoqi@0: oop obj = (oop) result.get_jobject(); aoqi@0: aoqi@0: // Primitive classes return null since forName() can not be aoqi@0: // used to obtain any of the Class objects representing primitives or void aoqi@0: if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) { aoqi@0: instanceKlassHandle k = aoqi@0: instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj)); aoqi@0: // For user defined Java class loaders, check that the name returned is aoqi@0: // the same as that requested. This check is done for the bootstrap aoqi@0: // loader when parsing the class file. aoqi@0: if (class_name == k->name()) { aoqi@0: return k; aoqi@0: } aoqi@0: } aoqi@0: // Class is not found or has the wrong name, return NULL aoqi@0: return nh; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) { aoqi@0: aoqi@0: ClassLoaderData* loader_data = k->class_loader_data(); aoqi@0: Handle class_loader_h(THREAD, loader_data->class_loader()); aoqi@0: aoqi@0: for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){} aoqi@0: aoqi@0: // for bootstrap and other parallel classloaders don't acquire lock, aoqi@0: // use placeholder token aoqi@0: // If a parallelCapable class loader calls define_instance_class instead of aoqi@0: // find_or_define_instance_class to get here, we have a timing aoqi@0: // hole with systemDictionary updates and check_constraints aoqi@0: if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) { aoqi@0: assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, aoqi@0: compute_loader_lock_object(class_loader_h, THREAD)), aoqi@0: "define called without lock"); aoqi@0: } aoqi@0: aoqi@0: // Check class-loading constraints. Throw exception if violation is detected. aoqi@0: // Grabs and releases SystemDictionary_lock aoqi@0: // The check_constraints/find_class call and update_dictionary sequence aoqi@0: // must be "atomic" for a specific class/classloader pair so we never aoqi@0: // define two different instanceKlasses for that class/classloader pair. aoqi@0: // Existing classloaders will call define_instance_class with the aoqi@0: // classloader lock held aoqi@0: // Parallel classloaders will call find_or_define_instance_class aoqi@0: // which will require a token to perform the define class aoqi@0: Symbol* name_h = k->name(); aoqi@0: unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK); aoqi@0: aoqi@0: // Register class just loaded with class loader (placed in Vector) aoqi@0: // Note we do this before updating the dictionary, as this can aoqi@0: // fail with an OutOfMemoryError (if it does, we will *not* put this aoqi@0: // class in the dictionary and will not update the class hierarchy). aoqi@0: // JVMTI FollowReferences needs to find the classes this way. aoqi@0: if (k->class_loader() != NULL) { aoqi@0: methodHandle m(THREAD, Universe::loader_addClass_method()); aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCallArguments args(class_loader_h); aoqi@0: args.push_oop(Handle(THREAD, k->java_mirror())); aoqi@0: JavaCalls::call(&result, m, &args, CHECK); aoqi@0: } aoqi@0: aoqi@0: // Add the new class. We need recompile lock during update of CHA. aoqi@0: { aoqi@0: unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: aoqi@0: MutexLocker mu_r(Compile_lock, THREAD); aoqi@0: aoqi@0: // Add to class hierarchy, initialize vtables, and do possible aoqi@0: // deoptimizations. aoqi@0: add_to_hierarchy(k, CHECK); // No exception, but can block aoqi@0: aoqi@0: // Add to systemDictionary - so other classes can see it. aoqi@0: // Grabs and releases SystemDictionary_lock aoqi@0: update_dictionary(d_index, d_hash, p_index, p_hash, aoqi@0: k, class_loader_h, THREAD); aoqi@0: } aoqi@0: k->eager_initialize(THREAD); aoqi@0: aoqi@0: // notify jvmti aoqi@0: if (JvmtiExport::should_post_class_load()) { aoqi@0: assert(THREAD->is_Java_thread(), "thread->is_Java_thread()"); aoqi@0: JvmtiExport::post_class_load((JavaThread *) THREAD, k()); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // Support parallel classloading aoqi@0: // All parallel class loaders, including bootstrap classloader aoqi@0: // lock a placeholder entry for this class/class_loader pair aoqi@0: // to allow parallel defines of different classes for this class loader aoqi@0: // With AllowParallelDefine flag==true, in case they do not synchronize around aoqi@0: // FindLoadedClass/DefineClass, calls, we check for parallel aoqi@0: // loading for them, wait if a defineClass is in progress aoqi@0: // and return the initial requestor's results aoqi@0: // This flag does not apply to the bootstrap classloader. aoqi@0: // With AllowParallelDefine flag==false, call through to define_instance_class aoqi@0: // which will throw LinkageError: duplicate class definition. aoqi@0: // False is the requested default. aoqi@0: // For better performance, the class loaders should synchronize aoqi@0: // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they aoqi@0: // potentially waste time reading and parsing the bytestream. aoqi@0: // Note: VM callers should ensure consistency of k/class_name,class_loader aoqi@0: instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) { aoqi@0: aoqi@0: instanceKlassHandle nh = instanceKlassHandle(); // null Handle aoqi@0: Symbol* name_h = k->name(); // passed in class_name may be null aoqi@0: ClassLoaderData* loader_data = class_loader_data(class_loader); aoqi@0: aoqi@0: unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: aoqi@0: // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS aoqi@0: unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: PlaceholderEntry* probe; aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: // First check if class already defined aoqi@0: if (UnsyncloadClass || (is_parallelDefine(class_loader))) { aoqi@0: Klass* check = find_class(d_index, d_hash, name_h, loader_data); aoqi@0: if (check != NULL) { aoqi@0: return(instanceKlassHandle(THREAD, check)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Acquire define token for this class/classloader aoqi@0: probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD); aoqi@0: // Wait if another thread defining in parallel aoqi@0: // All threads wait - even those that will throw duplicate class: otherwise aoqi@0: // caller is surprised by LinkageError: duplicate, but findLoadedClass fails aoqi@0: // if other thread has not finished updating dictionary aoqi@0: while (probe->definer() != NULL) { aoqi@0: SystemDictionary_lock->wait(); aoqi@0: } aoqi@0: // Only special cases allow parallel defines and can use other thread's results aoqi@0: // Other cases fall through, and may run into duplicate defines aoqi@0: // caught by finding an entry in the SystemDictionary aoqi@0: if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) { aoqi@0: placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD); aoqi@0: SystemDictionary_lock->notify_all(); aoqi@0: #ifdef ASSERT aoqi@0: Klass* check = find_class(d_index, d_hash, name_h, loader_data); aoqi@0: assert(check != NULL, "definer missed recording success"); aoqi@0: #endif aoqi@0: return(instanceKlassHandle(THREAD, probe->instance_klass())); aoqi@0: } else { aoqi@0: // This thread will define the class (even if earlier thread tried and had an error) aoqi@0: probe->set_definer(THREAD); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: define_instance_class(k, THREAD); aoqi@0: aoqi@0: Handle linkage_exception = Handle(); // null handle aoqi@0: aoqi@0: // definer must notify any waiting threads aoqi@0: { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, loader_data); aoqi@0: assert(probe != NULL, "DEFINE_CLASS placeholder lost?"); aoqi@0: if (probe != NULL) { aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: linkage_exception = Handle(THREAD,PENDING_EXCEPTION); aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: } else { aoqi@0: probe->set_instance_klass(k()); aoqi@0: } aoqi@0: probe->set_definer(NULL); aoqi@0: placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD); aoqi@0: SystemDictionary_lock->notify_all(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Can't throw exception while holding lock due to rank ordering aoqi@0: if (linkage_exception() != NULL) { aoqi@0: THROW_OOP_(linkage_exception(), nh); // throws exception and returns aoqi@0: } aoqi@0: aoqi@0: return k; aoqi@0: } aoqi@0: Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) { aoqi@0: // If class_loader is NULL we synchronize on _system_loader_lock_obj aoqi@0: if (class_loader.is_null()) { aoqi@0: return Handle(THREAD, _system_loader_lock_obj); aoqi@0: } else { aoqi@0: return class_loader; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This method is added to check how often we have to wait to grab loader aoqi@0: // lock. The results are being recorded in the performance counters defined in aoqi@0: // ClassLoader::_sync_systemLoaderLockContentionRate and aoqi@0: // ClassLoader::_sync_nonSystemLoaderLockConteionRate. aoqi@0: void SystemDictionary::check_loader_lock_contention(Handle loader_lock, TRAPS) { aoqi@0: if (!UsePerfData) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: assert(!loader_lock.is_null(), "NULL lock object"); aoqi@0: aoqi@0: if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock) aoqi@0: == ObjectSynchronizer::owner_other) { aoqi@0: // contention will likely happen, so increment the corresponding aoqi@0: // contention counter. aoqi@0: if (loader_lock() == _system_loader_lock_obj) { aoqi@0: ClassLoader::sync_systemLoaderLockContentionRate()->inc(); aoqi@0: } else { aoqi@0: ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Lookup aoqi@0: aoqi@0: Klass* SystemDictionary::find_class(int index, unsigned int hash, aoqi@0: Symbol* class_name, aoqi@0: ClassLoaderData* loader_data) { aoqi@0: assert_locked_or_safepoint(SystemDictionary_lock); aoqi@0: assert (index == dictionary()->index_for(class_name, loader_data), aoqi@0: "incorrect index?"); aoqi@0: aoqi@0: Klass* k = dictionary()->find_class(index, hash, class_name, loader_data); aoqi@0: return k; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Basic find on classes in the midst of being loaded aoqi@0: Symbol* SystemDictionary::find_placeholder(Symbol* class_name, aoqi@0: ClassLoaderData* loader_data) { aoqi@0: assert_locked_or_safepoint(SystemDictionary_lock); aoqi@0: unsigned int p_hash = placeholders()->compute_hash(class_name, loader_data); aoqi@0: int p_index = placeholders()->hash_to_index(p_hash); aoqi@0: return placeholders()->find_entry(p_index, p_hash, class_name, loader_data); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Used for assertions and verification only aoqi@0: Klass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) { aoqi@0: #ifndef ASSERT aoqi@0: guarantee(VerifyBeforeGC || aoqi@0: VerifyDuringGC || aoqi@0: VerifyBeforeExit || aoqi@0: VerifyDuringStartup || aoqi@0: VerifyAfterGC, "too expensive"); aoqi@0: #endif aoqi@0: assert_locked_or_safepoint(SystemDictionary_lock); aoqi@0: aoqi@0: // First look in the loaded class array aoqi@0: unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data); aoqi@0: int d_index = dictionary()->hash_to_index(d_hash); aoqi@0: return find_class(d_index, d_hash, class_name, loader_data); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Get the next class in the diictionary. aoqi@0: Klass* SystemDictionary::try_get_next_class() { aoqi@0: return dictionary()->try_get_next_class(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock aoqi@0: // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in aoqi@0: // before a new class is used. aoqi@0: aoqi@0: void SystemDictionary::add_to_hierarchy(instanceKlassHandle k, TRAPS) { aoqi@0: assert(k.not_null(), "just checking"); aoqi@0: assert_locked_or_safepoint(Compile_lock); aoqi@0: aoqi@0: // Link into hierachy. Make sure the vtables are initialized before linking into aoqi@0: k->append_to_sibling_list(); // add to superklass/sibling list aoqi@0: k->process_interfaces(THREAD); // handle all "implements" declarations aoqi@0: k->set_init_state(InstanceKlass::loaded); aoqi@0: // Now flush all code that depended on old class hierarchy. aoqi@0: // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97) aoqi@0: // Also, first reinitialize vtable because it may have gotten out of synch aoqi@0: // while the new class wasn't connected to the class hierarchy. aoqi@0: Universe::flush_dependents_on(k); aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // GC support aoqi@0: aoqi@0: // Following roots during mark-sweep is separated in two phases. aoqi@0: // aoqi@0: // The first phase follows preloaded classes and all other system aoqi@0: // classes, since these will never get unloaded anyway. aoqi@0: // aoqi@0: // The second phase removes (unloads) unreachable classes from the aoqi@0: // system dictionary and follows the remaining classes' contents. aoqi@0: aoqi@0: void SystemDictionary::always_strong_oops_do(OopClosure* blk) { stefank@6992: roots_oops_do(blk, NULL); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::always_strong_classes_do(KlassClosure* closure) { aoqi@0: // Follow all system classes and temporary placeholders in dictionary aoqi@0: dictionary()->always_strong_classes_do(closure); aoqi@0: aoqi@0: // Placeholders. These represent classes we're actively loading. aoqi@0: placeholders()->classes_do(closure); aoqi@0: } aoqi@0: aoqi@0: // Calculate a "good" systemdictionary size based aoqi@0: // on predicted or current loaded classes count aoqi@0: int SystemDictionary::calculate_systemdictionary_size(int classcount) { aoqi@0: int newsize = _old_default_sdsize; aoqi@0: if ((classcount > 0) && !DumpSharedSpaces) { aoqi@0: int desiredsize = classcount/_average_depth_goal; aoqi@0: for (newsize = _primelist[_sdgeneration]; _sdgeneration < _prime_array_size -1; aoqi@0: newsize = _primelist[++_sdgeneration]) { aoqi@0: if (desiredsize <= newsize) { aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return newsize; aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: class VerifySDReachableAndLiveClosure : public OopClosure { aoqi@0: private: aoqi@0: BoolObjectClosure* _is_alive; aoqi@0: aoqi@0: template void do_oop_work(T* p) { aoqi@0: oop obj = oopDesc::load_decode_heap_oop(p); aoqi@0: guarantee(_is_alive->do_object_b(obj), "Oop in system dictionary must be live"); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { } aoqi@0: aoqi@0: virtual void do_oop(oop* p) { do_oop_work(p); } aoqi@0: virtual void do_oop(narrowOop* p) { do_oop_work(p); } aoqi@0: }; aoqi@0: #endif aoqi@0: aoqi@0: // Assumes classes in the SystemDictionary are only unloaded at a safepoint aoqi@0: // Note: anonymous classes are not in the SD. stefank@7333: bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive, bool clean_alive) { aoqi@0: // First, mark for unload all ClassLoaderData referencing a dead class loader. stefank@7333: bool unloading_occurred = ClassLoaderDataGraph::do_unloading(is_alive, clean_alive); thartmann@7064: if (unloading_occurred) { thartmann@7064: dictionary()->do_unloading(); aoqi@0: constraints()->purge_loader_constraints(); aoqi@0: resolution_errors()->purge_resolution_errors(); aoqi@0: } aoqi@0: // Oops referenced by the system dictionary may get unreachable independently aoqi@0: // of the class loader (eg. cached protection domain oops). So we need to aoqi@0: // explicitly unlink them here instead of in Dictionary::do_unloading. aoqi@0: dictionary()->unlink(is_alive); aoqi@0: #ifdef ASSERT aoqi@0: VerifySDReachableAndLiveClosure cl(is_alive); aoqi@0: dictionary()->oops_do(&cl); aoqi@0: #endif aoqi@0: return unloading_occurred; aoqi@0: } aoqi@0: stefank@6992: void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) { stefank@6992: strong->do_oop(&_java_system_loader); stefank@6992: strong->do_oop(&_system_loader_lock_obj); iklam@7089: CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);) stefank@6992: stefank@6992: // Adjust dictionary stefank@6992: dictionary()->roots_oops_do(strong, weak); stefank@6992: stefank@6992: // Visit extra methods stefank@6992: invoke_method_table()->oops_do(strong); stefank@6992: } stefank@6992: aoqi@0: void SystemDictionary::oops_do(OopClosure* f) { aoqi@0: f->do_oop(&_java_system_loader); aoqi@0: f->do_oop(&_system_loader_lock_obj); iklam@7089: CDS_ONLY(SystemDictionaryShared::oops_do(f);) aoqi@0: aoqi@0: // Adjust dictionary aoqi@0: dictionary()->oops_do(f); aoqi@0: aoqi@0: // Visit extra methods aoqi@0: invoke_method_table()->oops_do(f); aoqi@0: } aoqi@0: aoqi@0: // Extended Class redefinition support. aoqi@0: // If one of these classes is replaced, we need to replace it in these places. aoqi@0: // KlassClosure::do_klass should take the address of a class but we can aoqi@0: // change that later. aoqi@0: void SystemDictionary::preloaded_classes_do(KlassClosure* f) { aoqi@0: for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) { aoqi@0: f->do_klass(_well_known_klasses[k]); aoqi@0: } aoqi@0: aoqi@0: { aoqi@0: for (int i = 0; i < T_VOID+1; i++) { aoqi@0: if (_box_klasses[i] != NULL) { aoqi@0: assert(i >= T_BOOLEAN, "checking"); aoqi@0: f->do_klass(_box_klasses[i]); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: FilteredFieldsMap::classes_do(f); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::lazily_loaded_classes_do(KlassClosure* f) { aoqi@0: f->do_klass(_abstract_ownable_synchronizer_klass); aoqi@0: } aoqi@0: aoqi@0: // Just the classes from defining class loaders aoqi@0: // Don't iterate over placeholders aoqi@0: void SystemDictionary::classes_do(void f(Klass*)) { aoqi@0: dictionary()->classes_do(f); aoqi@0: } aoqi@0: aoqi@0: // Added for initialize_itable_for_klass aoqi@0: // Just the classes from defining class loaders aoqi@0: // Don't iterate over placeholders aoqi@0: void SystemDictionary::classes_do(void f(Klass*, TRAPS), TRAPS) { aoqi@0: dictionary()->classes_do(f, CHECK); aoqi@0: } aoqi@0: aoqi@0: // All classes, and their class loaders aoqi@0: // Don't iterate over placeholders aoqi@0: void SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*)) { aoqi@0: dictionary()->classes_do(f); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::placeholders_do(void f(Symbol*)) { aoqi@0: placeholders()->entries_do(f); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::methods_do(void f(Method*)) { aoqi@0: dictionary()->methods_do(f); aoqi@0: invoke_method_table()->methods_do(f); aoqi@0: } aoqi@0: iklam@7089: void SystemDictionary::remove_classes_in_error_state() { iklam@7089: dictionary()->remove_classes_in_error_state(); iklam@7089: } iklam@7089: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Lazily load klasses aoqi@0: aoqi@0: void SystemDictionary::load_abstract_ownable_synchronizer_klass(TRAPS) { aoqi@0: assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later"); aoqi@0: aoqi@0: // if multiple threads calling this function, only one thread will load aoqi@0: // the class. The other threads will find the loaded version once the aoqi@0: // class is loaded. aoqi@0: Klass* aos = _abstract_ownable_synchronizer_klass; aoqi@0: if (aos == NULL) { aoqi@0: Klass* k = resolve_or_fail(vmSymbols::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK); aoqi@0: // Force a fence to prevent any read before the write completes aoqi@0: OrderAccess::fence(); aoqi@0: _abstract_ownable_synchronizer_klass = k; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- aoqi@0: // Initialization aoqi@0: aoqi@0: void SystemDictionary::initialize(TRAPS) { aoqi@0: // Allocate arrays aoqi@0: assert(dictionary() == NULL, aoqi@0: "SystemDictionary should only be initialized once"); aoqi@0: _sdgeneration = 0; aoqi@0: _dictionary = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount)); aoqi@0: _placeholders = new PlaceholderTable(_nof_buckets); aoqi@0: _number_of_modifications = 0; aoqi@0: _loader_constraints = new LoaderConstraintTable(_loader_constraint_size); aoqi@0: _resolution_errors = new ResolutionErrorTable(_resolution_error_size); aoqi@0: _invoke_method_table = new SymbolPropertyTable(_invoke_method_size); aoqi@0: aoqi@0: // Allocate private object used as system class loader lock aoqi@0: _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK); aoqi@0: // Initialize basic classes aoqi@0: initialize_preloaded_classes(CHECK); aoqi@0: } aoqi@0: aoqi@0: // Compact table of directions on the initialization of klasses: aoqi@0: static const short wk_init_info[] = { aoqi@0: #define WK_KLASS_INIT_INFO(name, symbol, option) \ aoqi@0: ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \ aoqi@0: << SystemDictionary::CEIL_LG_OPTION_LIMIT) \ aoqi@0: | (int)SystemDictionary::option ), aoqi@0: WK_KLASSES_DO(WK_KLASS_INIT_INFO) aoqi@0: #undef WK_KLASS_INIT_INFO aoqi@0: 0 aoqi@0: }; aoqi@0: aoqi@0: bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) { aoqi@0: assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob"); aoqi@0: int info = wk_init_info[id - FIRST_WKID]; aoqi@0: int sid = (info >> CEIL_LG_OPTION_LIMIT); aoqi@0: Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid); aoqi@0: Klass** klassp = &_well_known_klasses[id]; aoqi@0: bool must_load = (init_opt < SystemDictionary::Opt); aoqi@0: if ((*klassp) == NULL) { aoqi@0: if (must_load) { aoqi@0: (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class aoqi@0: } else { aoqi@0: (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass aoqi@0: } aoqi@0: } aoqi@0: return ((*klassp) != NULL); aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) { aoqi@0: assert((int)start_id <= (int)limit_id, "IDs are out of order!"); aoqi@0: for (int id = (int)start_id; id < (int)limit_id; id++) { aoqi@0: assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob"); aoqi@0: int info = wk_init_info[id - FIRST_WKID]; aoqi@0: int sid = (info >> CEIL_LG_OPTION_LIMIT); aoqi@0: int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT)); aoqi@0: aoqi@0: initialize_wk_klass((WKID)id, opt, CHECK); aoqi@0: } aoqi@0: aoqi@0: // move the starting value forward to the limit: aoqi@0: start_id = limit_id; aoqi@0: } aoqi@0: aoqi@0: void SystemDictionary::initialize_preloaded_classes(TRAPS) { aoqi@0: assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once"); aoqi@0: // Preload commonly used klasses aoqi@0: WKID scan = FIRST_WKID; aoqi@0: // first do Object, then String, Class aoqi@0: if (UseSharedSpaces) { aoqi@0: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK); aoqi@0: // Initialize the constant pool for the Object_class aoqi@0: InstanceKlass* ik = InstanceKlass::cast(Object_klass()); aoqi@0: ik->constants()->restore_unshareable_info(CHECK); aoqi@0: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); aoqi@0: } else { aoqi@0: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); aoqi@0: } aoqi@0: aoqi@0: // Calculate offsets for String and Class classes since they are loaded and aoqi@0: // can be used after this point. aoqi@0: java_lang_String::compute_offsets(); aoqi@0: java_lang_Class::compute_offsets(); aoqi@0: aoqi@0: // Fixup mirrors for classes loaded before java.lang.Class. aoqi@0: // These calls iterate over the objects currently in the perm gen aoqi@0: // so calling them at this point is matters (not before when there aoqi@0: // are fewer objects and not later after there are more objects aoqi@0: // in the perm gen. aoqi@0: Universe::initialize_basic_type_mirrors(CHECK); aoqi@0: Universe::fixup_mirrors(CHECK); aoqi@0: aoqi@0: // do a bunch more: aoqi@0: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK); aoqi@0: aoqi@0: // Preload ref klasses and set reference types aoqi@0: InstanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER); aoqi@0: InstanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass)); aoqi@0: jmasa@7469: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Cleaner_klass), scan, CHECK); aoqi@0: InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT); aoqi@0: InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK); aoqi@0: InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL); aoqi@0: InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM); jmasa@7469: InstanceKlass::cast(WK_KLASS(Cleaner_klass))->set_reference_type(REF_CLEANER); aoqi@0: kbarrett@9099: initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(ReferenceQueue_klass), scan, CHECK); kbarrett@9099: aoqi@0: // JSR 292 classes aoqi@0: WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass); aoqi@0: WKID jsr292_group_end = WK_KLASS_ENUM_NAME(VolatileCallSite_klass); aoqi@0: initialize_wk_klasses_until(jsr292_group_start, scan, CHECK); aoqi@0: if (EnableInvokeDynamic) { aoqi@0: initialize_wk_klasses_through(jsr292_group_end, scan, CHECK); aoqi@0: } else { aoqi@0: // Skip the JSR 292 classes, if not enabled. aoqi@0: scan = WKID(jsr292_group_end + 1); aoqi@0: } aoqi@0: aoqi@0: initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK); aoqi@0: aoqi@0: _box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass); aoqi@0: _box_klasses[T_CHAR] = WK_KLASS(Character_klass); aoqi@0: _box_klasses[T_FLOAT] = WK_KLASS(Float_klass); aoqi@0: _box_klasses[T_DOUBLE] = WK_KLASS(Double_klass); aoqi@0: _box_klasses[T_BYTE] = WK_KLASS(Byte_klass); aoqi@0: _box_klasses[T_SHORT] = WK_KLASS(Short_klass); aoqi@0: _box_klasses[T_INT] = WK_KLASS(Integer_klass); aoqi@0: _box_klasses[T_LONG] = WK_KLASS(Long_klass); aoqi@0: //_box_klasses[T_OBJECT] = WK_KLASS(object_klass); aoqi@0: //_box_klasses[T_ARRAY] = WK_KLASS(object_klass); aoqi@0: aoqi@0: { // Compute whether we should use loadClass or loadClassInternal when loading classes. aoqi@0: Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature()); aoqi@0: _has_loadClassInternal = (method != NULL); aoqi@0: } aoqi@0: { // Compute whether we should use checkPackageAccess or NOT aoqi@0: Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature()); aoqi@0: _has_checkPackageAccess = (method != NULL); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Tells if a given klass is a box (wrapper class, such as java.lang.Integer). aoqi@0: // If so, returns the basic type it holds. If not, returns T_OBJECT. aoqi@0: BasicType SystemDictionary::box_klass_type(Klass* k) { aoqi@0: assert(k != NULL, ""); aoqi@0: for (int i = T_BOOLEAN; i < T_VOID+1; i++) { aoqi@0: if (_box_klasses[i] == k) aoqi@0: return (BasicType)i; aoqi@0: } aoqi@0: return T_OBJECT; aoqi@0: } aoqi@0: aoqi@0: // Constraints on class loaders. The details of the algorithm can be aoqi@0: // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java aoqi@0: // Virtual Machine" by Sheng Liang and Gilad Bracha. The basic idea is aoqi@0: // that the system dictionary needs to maintain a set of contraints that aoqi@0: // must be satisfied by all classes in the dictionary. aoqi@0: // if defining is true, then LinkageError if already in systemDictionary aoqi@0: // if initiating loader, then ok if InstanceKlass matches existing entry aoqi@0: aoqi@0: void SystemDictionary::check_constraints(int d_index, unsigned int d_hash, aoqi@0: instanceKlassHandle k, aoqi@0: Handle class_loader, bool defining, aoqi@0: TRAPS) { aoqi@0: const char *linkage_error = NULL; aoqi@0: { aoqi@0: Symbol* name = k->name(); aoqi@0: ClassLoaderData *loader_data = class_loader_data(class_loader); aoqi@0: aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: aoqi@0: Klass* check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (check != (Klass*)NULL) { aoqi@0: // if different InstanceKlass - duplicate class definition, aoqi@0: // else - ok, class loaded by a different thread in parallel, aoqi@0: // we should only have found it if it was done loading and ok to use aoqi@0: // system dictionary only holds instance classes, placeholders aoqi@0: // also holds array classes aoqi@0: aoqi@0: assert(check->oop_is_instance(), "noninstance in systemdictionary"); aoqi@0: if ((defining == true) || (k() != check)) { aoqi@0: linkage_error = "loader (instance of %s): attempted duplicate class " aoqi@0: "definition for name: \"%s\""; aoqi@0: } else { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: Symbol* ph_check = find_placeholder(name, loader_data); aoqi@0: assert(ph_check == NULL || ph_check == name, "invalid symbol"); aoqi@0: #endif aoqi@0: aoqi@0: if (linkage_error == NULL) { aoqi@0: if (constraints()->check_or_update(k, class_loader, name) == false) { aoqi@0: linkage_error = "loader constraint violation: loader (instance of %s)" aoqi@0: " previously initiated loading for a different type with name \"%s\""; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Throw error now if needed (cannot throw while holding aoqi@0: // SystemDictionary_lock because of rank ordering) aoqi@0: aoqi@0: if (linkage_error) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: const char* class_loader_name = loader_name(class_loader()); aoqi@0: char* type_name = k->name()->as_C_string(); aoqi@0: size_t buflen = strlen(linkage_error) + strlen(class_loader_name) + aoqi@0: strlen(type_name); aoqi@0: char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); aoqi@0: jio_snprintf(buf, buflen, linkage_error, class_loader_name, type_name); aoqi@0: THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Update system dictionary - done after check_constraint and add_to_hierachy aoqi@0: // have been called. aoqi@0: void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash, aoqi@0: int p_index, unsigned int p_hash, aoqi@0: instanceKlassHandle k, aoqi@0: Handle class_loader, aoqi@0: TRAPS) { aoqi@0: // Compile_lock prevents systemDictionary updates during compilations aoqi@0: assert_locked_or_safepoint(Compile_lock); aoqi@0: Symbol* name = k->name(); aoqi@0: ClassLoaderData *loader_data = class_loader_data(class_loader); aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu1(SystemDictionary_lock, THREAD); aoqi@0: aoqi@0: // See whether biased locking is enabled and if so set it for this aoqi@0: // klass. aoqi@0: // Note that this must be done past the last potential blocking aoqi@0: // point / safepoint. We enable biased locking lazily using a aoqi@0: // VM_Operation to iterate the SystemDictionary and installing the aoqi@0: // biasable mark word into each InstanceKlass's prototype header. aoqi@0: // To avoid race conditions where we accidentally miss enabling the aoqi@0: // optimization for one class in the process of being added to the aoqi@0: // dictionary, we must not safepoint after the test of aoqi@0: // BiasedLocking::enabled(). aoqi@0: if (UseBiasedLocking && BiasedLocking::enabled()) { aoqi@0: // Set biased locking bit for all loaded classes; it will be aoqi@0: // cleared if revocation occurs too often for this type aoqi@0: // NOTE that we must only do this when the class is initally aoqi@0: // defined, not each time it is referenced from a new class loader aoqi@0: if (k->class_loader() == class_loader()) { aoqi@0: k->set_prototype_header(markOopDesc::biased_locking_prototype()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Make a new system dictionary entry. aoqi@0: Klass* sd_check = find_class(d_index, d_hash, name, loader_data); aoqi@0: if (sd_check == NULL) { aoqi@0: dictionary()->add_klass(name, loader_data, k); aoqi@0: notice_modification(); aoqi@0: } aoqi@0: #ifdef ASSERT aoqi@0: sd_check = find_class(d_index, d_hash, name, loader_data); aoqi@0: assert (sd_check != NULL, "should have entry in system dictionary"); aoqi@0: // Note: there may be a placeholder entry: for circularity testing aoqi@0: // or for parallel defines aoqi@0: #endif aoqi@0: SystemDictionary_lock->notify_all(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Try to find a class name using the loader constraints. The aoqi@0: // loader constraints might know about a class that isn't fully loaded aoqi@0: // yet and these will be ignored. aoqi@0: Klass* SystemDictionary::find_constrained_instance_or_array_klass( aoqi@0: Symbol* class_name, Handle class_loader, TRAPS) { aoqi@0: aoqi@0: // First see if it has been loaded directly. aoqi@0: // Force the protection domain to be null. (This removes protection checks.) aoqi@0: Handle no_protection_domain; aoqi@0: Klass* klass = find_instance_or_array_klass(class_name, class_loader, aoqi@0: no_protection_domain, CHECK_NULL); aoqi@0: if (klass != NULL) aoqi@0: return klass; aoqi@0: aoqi@0: // Now look to see if it has been loaded elsewhere, and is subject to aoqi@0: // a loader constraint that would require this loader to return the aoqi@0: // klass that is already loaded. aoqi@0: if (FieldType::is_array(class_name)) { aoqi@0: // For array classes, their Klass*s are not kept in the aoqi@0: // constraint table. The element Klass*s are. aoqi@0: FieldArrayInfo fd; aoqi@0: BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL)); aoqi@0: if (t != T_OBJECT) { aoqi@0: klass = Universe::typeArrayKlassObj(t); aoqi@0: } else { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: klass = constraints()->find_constrained_klass(fd.object_key(), class_loader); aoqi@0: } aoqi@0: // If element class already loaded, allocate array klass aoqi@0: if (klass != NULL) { aoqi@0: klass = klass->array_klass_or_null(fd.dimension()); aoqi@0: } aoqi@0: } else { aoqi@0: MutexLocker mu(SystemDictionary_lock, THREAD); aoqi@0: // Non-array classes are easy: simply check the constraint table. aoqi@0: klass = constraints()->find_constrained_klass(class_name, class_loader); aoqi@0: } aoqi@0: aoqi@0: return klass; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool SystemDictionary::add_loader_constraint(Symbol* class_name, aoqi@0: Handle class_loader1, aoqi@0: Handle class_loader2, aoqi@0: Thread* THREAD) { aoqi@0: ClassLoaderData* loader_data1 = class_loader_data(class_loader1); aoqi@0: ClassLoaderData* loader_data2 = class_loader_data(class_loader2); aoqi@0: aoqi@0: Symbol* constraint_name = NULL; aoqi@0: if (!FieldType::is_array(class_name)) { aoqi@0: constraint_name = class_name; aoqi@0: } else { aoqi@0: // For array classes, their Klass*s are not kept in the aoqi@0: // constraint table. The element classes are. aoqi@0: FieldArrayInfo fd; aoqi@0: BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false)); aoqi@0: // primitive types always pass aoqi@0: if (t != T_OBJECT) { aoqi@0: return true; aoqi@0: } else { aoqi@0: constraint_name = fd.object_key(); aoqi@0: } aoqi@0: } aoqi@0: unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, loader_data1); aoqi@0: int d_index1 = dictionary()->hash_to_index(d_hash1); aoqi@0: aoqi@0: unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, loader_data2); aoqi@0: int d_index2 = dictionary()->hash_to_index(d_hash2); aoqi@0: { aoqi@0: MutexLocker mu_s(SystemDictionary_lock, THREAD); aoqi@0: aoqi@0: // Better never do a GC while we're holding these oops aoqi@0: No_Safepoint_Verifier nosafepoint; aoqi@0: aoqi@0: Klass* klass1 = find_class(d_index1, d_hash1, constraint_name, loader_data1); aoqi@0: Klass* klass2 = find_class(d_index2, d_hash2, constraint_name, loader_data2); aoqi@0: return constraints()->add_entry(constraint_name, klass1, class_loader1, aoqi@0: klass2, class_loader2); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Add entry to resolution error table to record the error when the first aoqi@0: // attempt to resolve a reference to a class has failed. aoqi@0: void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which, Symbol* error) { aoqi@0: unsigned int hash = resolution_errors()->compute_hash(pool, which); aoqi@0: int index = resolution_errors()->hash_to_index(hash); aoqi@0: { aoqi@0: MutexLocker ml(SystemDictionary_lock, Thread::current()); aoqi@0: resolution_errors()->add_entry(index, hash, pool, which, error); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Delete a resolution error for RedefineClasses for a constant pool is going away aoqi@0: void SystemDictionary::delete_resolution_error(ConstantPool* pool) { aoqi@0: resolution_errors()->delete_entry(pool); aoqi@0: } aoqi@0: aoqi@0: // Lookup resolution error table. Returns error if found, otherwise NULL. aoqi@0: Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) { aoqi@0: unsigned int hash = resolution_errors()->compute_hash(pool, which); aoqi@0: int index = resolution_errors()->hash_to_index(hash); aoqi@0: { aoqi@0: MutexLocker ml(SystemDictionary_lock, Thread::current()); aoqi@0: ResolutionErrorEntry* entry = resolution_errors()->find_entry(index, hash, pool, which); aoqi@0: return (entry != NULL) ? entry->error() : (Symbol*)NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Signature constraints ensure that callers and callees agree about aoqi@0: // the meaning of type names in their signatures. This routine is the aoqi@0: // intake for constraints. It collects them from several places: aoqi@0: // aoqi@0: // * LinkResolver::resolve_method (if check_access is true) requires aoqi@0: // that the resolving class (the caller) and the defining class of aoqi@0: // the resolved method (the callee) agree on each type in the aoqi@0: // method's signature. aoqi@0: // aoqi@0: // * LinkResolver::resolve_interface_method performs exactly the same aoqi@0: // checks. aoqi@0: // aoqi@0: // * LinkResolver::resolve_field requires that the constant pool aoqi@0: // attempting to link to a field agree with the field's defining aoqi@0: // class about the type of the field signature. aoqi@0: // aoqi@0: // * klassVtable::initialize_vtable requires that, when a class aoqi@0: // overrides a vtable entry allocated by a superclass, that the aoqi@0: // overriding method (i.e., the callee) agree with the superclass aoqi@0: // on each type in the method's signature. aoqi@0: // aoqi@0: // * klassItable::initialize_itable requires that, when a class fills aoqi@0: // in its itables, for each non-abstract method installed in an aoqi@0: // itable, the method (i.e., the callee) agree with the interface aoqi@0: // on each type in the method's signature. aoqi@0: // aoqi@0: // All those methods have a boolean (check_access, checkconstraints) aoqi@0: // which turns off the checks. This is used from specialized contexts aoqi@0: // such as bootstrapping, dumping, and debugging. aoqi@0: // aoqi@0: // No direct constraint is placed between the class and its aoqi@0: // supertypes. Constraints are only placed along linked relations aoqi@0: // between callers and callees. When a method overrides or implements aoqi@0: // an abstract method in a supertype (superclass or interface), the aoqi@0: // constraints are placed as if the supertype were the caller to the aoqi@0: // overriding method. (This works well, since callers to the aoqi@0: // supertype have already established agreement between themselves and aoqi@0: // the supertype.) As a result of all this, a class can disagree with aoqi@0: // its supertype about the meaning of a type name, as long as that aoqi@0: // class neither calls a relevant method of the supertype, nor is aoqi@0: // called (perhaps via an override) from the supertype. aoqi@0: // aoqi@0: // aoqi@0: // SystemDictionary::check_signature_loaders(sig, l1, l2) aoqi@0: // aoqi@0: // Make sure all class components (including arrays) in the given aoqi@0: // signature will be resolved to the same class in both loaders. aoqi@0: // Returns the name of the type that failed a loader constraint check, or aoqi@0: // NULL if no constraint failed. No exception except OOME is thrown. aoqi@0: // Arrays are not added to the loader constraint table, their elements are. aoqi@0: Symbol* SystemDictionary::check_signature_loaders(Symbol* signature, aoqi@0: Handle loader1, Handle loader2, aoqi@0: bool is_method, TRAPS) { aoqi@0: // Nothing to do if loaders are the same. aoqi@0: if (loader1() == loader2()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: SignatureStream sig_strm(signature, is_method); aoqi@0: while (!sig_strm.is_done()) { aoqi@0: if (sig_strm.is_object()) { aoqi@0: Symbol* sig = sig_strm.as_symbol(CHECK_NULL); aoqi@0: if (!add_loader_constraint(sig, loader1, loader2, THREAD)) { aoqi@0: return sig; aoqi@0: } aoqi@0: } aoqi@0: sig_strm.next(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: methodHandle SystemDictionary::find_method_handle_intrinsic(vmIntrinsics::ID iid, aoqi@0: Symbol* signature, aoqi@0: TRAPS) { aoqi@0: methodHandle empty; aoqi@0: assert(EnableInvokeDynamic, ""); aoqi@0: assert(MethodHandles::is_signature_polymorphic(iid) && aoqi@0: MethodHandles::is_signature_polymorphic_intrinsic(iid) && aoqi@0: iid != vmIntrinsics::_invokeGeneric, aoqi@0: err_msg("must be a known MH intrinsic iid=%d: %s", iid, vmIntrinsics::name_at(iid))); aoqi@0: aoqi@0: unsigned int hash = invoke_method_table()->compute_hash(signature, iid); aoqi@0: int index = invoke_method_table()->hash_to_index(hash); aoqi@0: SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid); aoqi@0: methodHandle m; aoqi@0: if (spe == NULL || spe->method() == NULL) { aoqi@0: spe = NULL; aoqi@0: // Must create lots of stuff here, but outside of the SystemDictionary lock. aoqi@0: m = Method::make_method_handle_intrinsic(iid, signature, CHECK_(empty)); iveresov@7216: if (!Arguments::is_interpreter_only()) { iveresov@7216: // Generate a compiled form of the MH intrinsic. iveresov@7216: AdapterHandlerLibrary::create_native_wrapper(m); iveresov@7216: // Check if have the compiled code. iveresov@7216: if (!m->has_compiled_code()) { iveresov@7216: THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(), iveresov@7216: "out of space in CodeCache for method handle intrinsic", empty); iveresov@7216: } iveresov@7172: } aoqi@0: // Now grab the lock. We might have to throw away the new method, aoqi@0: // if a racing thread has managed to install one at the same time. aoqi@0: { aoqi@0: MutexLocker ml(SystemDictionary_lock, THREAD); aoqi@0: spe = invoke_method_table()->find_entry(index, hash, signature, iid); aoqi@0: if (spe == NULL) aoqi@0: spe = invoke_method_table()->add_entry(index, hash, signature, iid); aoqi@0: if (spe->method() == NULL) aoqi@0: spe->set_method(m()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(spe != NULL && spe->method() != NULL, ""); iveresov@7172: assert(Arguments::is_interpreter_only() || (spe->method()->has_compiled_code() && iveresov@7146: spe->method()->code()->entry_point() == spe->method()->from_compiled_entry()), iveresov@7146: "MH intrinsic invariant"); aoqi@0: return spe->method(); aoqi@0: } aoqi@0: aoqi@0: // Helper for unpacking the return value from linkMethod and linkCallSite. aoqi@0: static methodHandle unpack_method_and_appendix(Handle mname, aoqi@0: KlassHandle accessing_klass, aoqi@0: objArrayHandle appendix_box, aoqi@0: Handle* appendix_result, aoqi@0: TRAPS) { aoqi@0: methodHandle empty; aoqi@0: if (mname.not_null()) { aoqi@0: Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); aoqi@0: if (vmtarget != NULL && vmtarget->is_method()) { aoqi@0: Method* m = (Method*)vmtarget; aoqi@0: oop appendix = appendix_box->obj_at(0); aoqi@0: if (TraceMethodHandles) { aoqi@0: #ifndef PRODUCT aoqi@0: tty->print("Linked method=" INTPTR_FORMAT ": ", p2i(m)); aoqi@0: m->print(); aoqi@0: if (appendix != NULL) { tty->print("appendix = "); appendix->print(); } aoqi@0: tty->cr(); aoqi@0: #endif //PRODUCT aoqi@0: } aoqi@0: (*appendix_result) = Handle(THREAD, appendix); aoqi@0: // the target is stored in the cpCache and if a reference to this aoqi@0: // MethodName is dropped we need a way to make sure the aoqi@0: // class_loader containing this method is kept alive. aoqi@0: // FIXME: the appendix might also preserve this dependency. aoqi@0: ClassLoaderData* this_key = InstanceKlass::cast(accessing_klass())->class_loader_data(); aoqi@0: this_key->record_dependency(m->method_holder(), CHECK_NULL); // Can throw OOM aoqi@0: return methodHandle(THREAD, m); aoqi@0: } aoqi@0: } aoqi@0: THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives", empty); aoqi@0: return empty; aoqi@0: } aoqi@0: aoqi@0: methodHandle SystemDictionary::find_method_handle_invoker(Symbol* name, aoqi@0: Symbol* signature, aoqi@0: KlassHandle accessing_klass, aoqi@0: Handle *appendix_result, aoqi@0: Handle *method_type_result, aoqi@0: TRAPS) { aoqi@0: methodHandle empty; aoqi@0: assert(EnableInvokeDynamic, ""); aoqi@0: assert(!THREAD->is_Compiler_thread(), ""); aoqi@0: Handle method_type = aoqi@0: SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty)); aoqi@0: aoqi@0: KlassHandle mh_klass = SystemDictionary::MethodHandle_klass(); aoqi@0: int ref_kind = JVM_REF_invokeVirtual; aoqi@0: Handle name_str = StringTable::intern(name, CHECK_(empty)); aoqi@0: objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty)); aoqi@0: assert(appendix_box->obj_at(0) == NULL, ""); aoqi@0: aoqi@0: // This should not happen. JDK code should take care of that. aoqi@0: if (accessing_klass.is_null() || method_type.is_null()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad invokehandle", empty); aoqi@0: } aoqi@0: aoqi@0: // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName aoqi@0: JavaCallArguments args; aoqi@0: args.push_oop(accessing_klass()->java_mirror()); aoqi@0: args.push_int(ref_kind); aoqi@0: args.push_oop(mh_klass()->java_mirror()); aoqi@0: args.push_oop(name_str()); aoqi@0: args.push_oop(method_type()); aoqi@0: args.push_oop(appendix_box()); aoqi@0: JavaValue result(T_OBJECT); aoqi@0: JavaCalls::call_static(&result, aoqi@0: SystemDictionary::MethodHandleNatives_klass(), aoqi@0: vmSymbols::linkMethod_name(), aoqi@0: vmSymbols::linkMethod_signature(), aoqi@0: &args, CHECK_(empty)); aoqi@0: Handle mname(THREAD, (oop) result.get_jobject()); aoqi@0: (*method_type_result) = method_type; aoqi@0: return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD); aoqi@0: } aoqi@0: vlivanov@7936: // Decide if we can globally cache a lookup of this class, to be returned to any client that asks. vlivanov@7936: // We must ensure that all class loaders everywhere will reach this class, for any client. vlivanov@7936: // This is a safe bet for public classes in java.lang, such as Object and String. vlivanov@7936: // We also include public classes in java.lang.invoke, because they appear frequently in system-level method types. vlivanov@7936: // Out of an abundance of caution, we do not include any other classes, not even for packages like java.util. vlivanov@7936: static bool is_always_visible_class(oop mirror) { vlivanov@7936: Klass* klass = java_lang_Class::as_Klass(mirror); vlivanov@7936: if (klass->oop_is_objArray()) { vlivanov@7936: klass = ObjArrayKlass::cast(klass)->bottom_klass(); // check element type vlivanov@7936: } vlivanov@7936: if (klass->oop_is_typeArray()) { vlivanov@7936: return true; // primitive array vlivanov@7936: } vlivanov@7936: assert(klass->oop_is_instance(), klass->external_name()); vlivanov@7936: return klass->is_public() && vlivanov@7936: (InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::Object_klass()) || // java.lang vlivanov@7936: InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::MethodHandle_klass())); // java.lang.invoke vlivanov@7936: } aoqi@0: aoqi@0: // Ask Java code to find or construct a java.lang.invoke.MethodType for the given aoqi@0: // signature, as interpreted relative to the given class loader. aoqi@0: // Because of class loader constraints, all method handle usage must be aoqi@0: // consistent with this loader. aoqi@0: Handle SystemDictionary::find_method_handle_type(Symbol* signature, aoqi@0: KlassHandle accessing_klass, aoqi@0: TRAPS) { aoqi@0: Handle empty; aoqi@0: vmIntrinsics::ID null_iid = vmIntrinsics::_none; // distinct from all method handle invoker intrinsics aoqi@0: unsigned int hash = invoke_method_table()->compute_hash(signature, null_iid); aoqi@0: int index = invoke_method_table()->hash_to_index(hash); aoqi@0: SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, null_iid); aoqi@0: if (spe != NULL && spe->method_type() != NULL) { aoqi@0: assert(java_lang_invoke_MethodType::is_instance(spe->method_type()), ""); aoqi@0: return Handle(THREAD, spe->method_type()); aoqi@0: } else if (THREAD->is_Compiler_thread()) { aoqi@0: warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME aoqi@0: return Handle(); // do not attempt from within compiler, unless it was cached aoqi@0: } aoqi@0: aoqi@0: Handle class_loader, protection_domain; vlivanov@7936: if (accessing_klass.not_null()) { vlivanov@7936: class_loader = Handle(THREAD, InstanceKlass::cast(accessing_klass())->class_loader()); vlivanov@7936: protection_domain = Handle(THREAD, InstanceKlass::cast(accessing_klass())->protection_domain()); vlivanov@7936: } vlivanov@7936: bool can_be_cached = true; aoqi@0: int npts = ArgumentCount(signature).size(); aoqi@0: objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty)); aoqi@0: int arg = 0; vlivanov@7936: Handle rt; // the return type from the signature aoqi@0: ResourceMark rm(THREAD); aoqi@0: for (SignatureStream ss(signature); !ss.is_done(); ss.next()) { aoqi@0: oop mirror = NULL; vlivanov@7936: if (can_be_cached) { vlivanov@7936: // Use neutral class loader to lookup candidate classes to be placed in the cache. vlivanov@7936: mirror = ss.as_java_mirror(Handle(), Handle(), aoqi@0: SignatureStream::ReturnNull, CHECK_(empty)); vlivanov@7936: if (mirror == NULL || (ss.is_object() && !is_always_visible_class(mirror))) { vlivanov@7936: // Fall back to accessing_klass context. vlivanov@7936: can_be_cached = false; aoqi@0: } aoqi@0: } vlivanov@7936: if (!can_be_cached) { aoqi@0: // Resolve, throwing a real error if it doesn't work. aoqi@0: mirror = ss.as_java_mirror(class_loader, protection_domain, aoqi@0: SignatureStream::NCDFError, CHECK_(empty)); aoqi@0: } vlivanov@7936: assert(!oopDesc::is_null(mirror), ss.as_symbol(THREAD)->as_C_string()); aoqi@0: if (ss.at_return_type()) aoqi@0: rt = Handle(THREAD, mirror); aoqi@0: else aoqi@0: pts->obj_at_put(arg++, mirror); aoqi@0: aoqi@0: // Check accessibility. aoqi@0: if (ss.is_object() && accessing_klass.not_null()) { aoqi@0: Klass* sel_klass = java_lang_Class::as_Klass(mirror); aoqi@0: mirror = NULL; // safety aoqi@0: // Emulate ConstantPool::verify_constant_pool_resolve. aoqi@0: if (sel_klass->oop_is_objArray()) aoqi@0: sel_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass(); aoqi@0: if (sel_klass->oop_is_instance()) { aoqi@0: KlassHandle sel_kh(THREAD, sel_klass); aoqi@0: LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: assert(arg == npts, ""); aoqi@0: aoqi@0: // call java.lang.invoke.MethodHandleNatives::findMethodType(Class rt, Class[] pts) -> MethodType aoqi@0: JavaCallArguments args(Handle(THREAD, rt())); aoqi@0: args.push_oop(pts()); aoqi@0: JavaValue result(T_OBJECT); aoqi@0: JavaCalls::call_static(&result, aoqi@0: SystemDictionary::MethodHandleNatives_klass(), aoqi@0: vmSymbols::findMethodHandleType_name(), aoqi@0: vmSymbols::findMethodHandleType_signature(), aoqi@0: &args, CHECK_(empty)); aoqi@0: Handle method_type(THREAD, (oop) result.get_jobject()); aoqi@0: vlivanov@7936: if (can_be_cached) { aoqi@0: // We can cache this MethodType inside the JVM. aoqi@0: MutexLocker ml(SystemDictionary_lock, THREAD); aoqi@0: spe = invoke_method_table()->find_entry(index, hash, signature, null_iid); aoqi@0: if (spe == NULL) aoqi@0: spe = invoke_method_table()->add_entry(index, hash, signature, null_iid); aoqi@0: if (spe->method_type() == NULL) { aoqi@0: spe->set_method_type(method_type()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // report back to the caller with the MethodType aoqi@0: return method_type; aoqi@0: } aoqi@0: aoqi@0: // Ask Java code to find or construct a method handle constant. aoqi@0: Handle SystemDictionary::link_method_handle_constant(KlassHandle caller, aoqi@0: int ref_kind, //e.g., JVM_REF_invokeVirtual aoqi@0: KlassHandle callee, aoqi@0: Symbol* name_sym, aoqi@0: Symbol* signature, aoqi@0: TRAPS) { aoqi@0: Handle empty; aoqi@0: Handle name = java_lang_String::create_from_symbol(name_sym, CHECK_(empty)); aoqi@0: Handle type; aoqi@0: if (signature->utf8_length() > 0 && signature->byte_at(0) == '(') { aoqi@0: type = find_method_handle_type(signature, caller, CHECK_(empty)); aoqi@0: } else if (caller.is_null()) { aoqi@0: // This should not happen. JDK code should take care of that. aoqi@0: THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MH constant", empty); aoqi@0: } else { aoqi@0: ResourceMark rm(THREAD); aoqi@0: SignatureStream ss(signature, false); aoqi@0: if (!ss.is_done()) { aoqi@0: oop mirror = ss.as_java_mirror(caller->class_loader(), caller->protection_domain(), aoqi@0: SignatureStream::NCDFError, CHECK_(empty)); aoqi@0: type = Handle(THREAD, mirror); aoqi@0: ss.next(); aoqi@0: if (!ss.is_done()) type = Handle(); // error! aoqi@0: } aoqi@0: } aoqi@0: if (type.is_null()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_LinkageError(), "bad signature", empty); aoqi@0: } aoqi@0: aoqi@0: // call java.lang.invoke.MethodHandleNatives::linkMethodHandleConstant(Class caller, int refKind, Class callee, String name, Object type) -> MethodHandle aoqi@0: JavaCallArguments args; aoqi@0: args.push_oop(caller->java_mirror()); // the referring class aoqi@0: args.push_int(ref_kind); aoqi@0: args.push_oop(callee->java_mirror()); // the target class aoqi@0: args.push_oop(name()); aoqi@0: args.push_oop(type()); aoqi@0: JavaValue result(T_OBJECT); aoqi@0: JavaCalls::call_static(&result, aoqi@0: SystemDictionary::MethodHandleNatives_klass(), aoqi@0: vmSymbols::linkMethodHandleConstant_name(), aoqi@0: vmSymbols::linkMethodHandleConstant_signature(), aoqi@0: &args, CHECK_(empty)); aoqi@0: return Handle(THREAD, (oop) result.get_jobject()); aoqi@0: } aoqi@0: aoqi@0: // Ask Java code to find or construct a java.lang.invoke.CallSite for the given aoqi@0: // name and signature, as interpreted relative to the given class loader. aoqi@0: methodHandle SystemDictionary::find_dynamic_call_site_invoker(KlassHandle caller, aoqi@0: Handle bootstrap_specifier, aoqi@0: Symbol* name, aoqi@0: Symbol* type, aoqi@0: Handle *appendix_result, aoqi@0: Handle *method_type_result, aoqi@0: TRAPS) { aoqi@0: methodHandle empty; aoqi@0: Handle bsm, info; aoqi@0: if (java_lang_invoke_MethodHandle::is_instance(bootstrap_specifier())) { aoqi@0: bsm = bootstrap_specifier; aoqi@0: } else { aoqi@0: assert(bootstrap_specifier->is_objArray(), ""); aoqi@0: objArrayHandle args(THREAD, (objArrayOop) bootstrap_specifier()); aoqi@0: int len = args->length(); aoqi@0: assert(len >= 1, ""); aoqi@0: bsm = Handle(THREAD, args->obj_at(0)); aoqi@0: if (len > 1) { aoqi@0: objArrayOop args1 = oopFactory::new_objArray(SystemDictionary::Object_klass(), len-1, CHECK_(empty)); aoqi@0: for (int i = 1; i < len; i++) aoqi@0: args1->obj_at_put(i-1, args->obj_at(i)); aoqi@0: info = Handle(THREAD, args1); aoqi@0: } aoqi@0: } aoqi@0: guarantee(java_lang_invoke_MethodHandle::is_instance(bsm()), aoqi@0: "caller must supply a valid BSM"); aoqi@0: aoqi@0: Handle method_name = java_lang_String::create_from_symbol(name, CHECK_(empty)); aoqi@0: Handle method_type = find_method_handle_type(type, caller, CHECK_(empty)); aoqi@0: aoqi@0: // This should not happen. JDK code should take care of that. aoqi@0: if (caller.is_null() || method_type.is_null()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad invokedynamic", empty); aoqi@0: } aoqi@0: aoqi@0: objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty)); aoqi@0: assert(appendix_box->obj_at(0) == NULL, ""); aoqi@0: aoqi@0: // call java.lang.invoke.MethodHandleNatives::linkCallSite(caller, bsm, name, mtype, info, &appendix) aoqi@0: JavaCallArguments args; aoqi@0: args.push_oop(caller->java_mirror()); aoqi@0: args.push_oop(bsm()); aoqi@0: args.push_oop(method_name()); aoqi@0: args.push_oop(method_type()); aoqi@0: args.push_oop(info()); aoqi@0: args.push_oop(appendix_box); aoqi@0: JavaValue result(T_OBJECT); aoqi@0: JavaCalls::call_static(&result, aoqi@0: SystemDictionary::MethodHandleNatives_klass(), aoqi@0: vmSymbols::linkCallSite_name(), aoqi@0: vmSymbols::linkCallSite_signature(), aoqi@0: &args, CHECK_(empty)); aoqi@0: Handle mname(THREAD, (oop) result.get_jobject()); aoqi@0: (*method_type_result) = method_type; aoqi@0: return unpack_method_and_appendix(mname, caller, appendix_box, appendix_result, THREAD); aoqi@0: } aoqi@0: aoqi@0: // Since the identity hash code for symbols changes when the symbols are aoqi@0: // moved from the regular perm gen (hash in the mark word) to the shared aoqi@0: // spaces (hash is the address), the classes loaded into the dictionary aoqi@0: // may be in the wrong buckets. aoqi@0: aoqi@0: void SystemDictionary::reorder_dictionary() { aoqi@0: dictionary()->reorder_dictionary(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void SystemDictionary::copy_buckets(char** top, char* end) { aoqi@0: dictionary()->copy_buckets(top, end); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void SystemDictionary::copy_table(char** top, char* end) { aoqi@0: dictionary()->copy_table(top, end); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void SystemDictionary::reverse() { aoqi@0: dictionary()->reverse(); aoqi@0: } aoqi@0: aoqi@0: int SystemDictionary::number_of_classes() { aoqi@0: return dictionary()->number_of_entries(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ---------------------------------------------------------------------------- iklam@7089: void SystemDictionary::print_shared(bool details) { iklam@7089: shared_dictionary()->print(details); iklam@7089: } aoqi@0: iklam@7089: void SystemDictionary::print(bool details) { iklam@7089: dictionary()->print(details); aoqi@0: aoqi@0: // Placeholders aoqi@0: GCMutexLocker mu(SystemDictionary_lock); aoqi@0: placeholders()->print(); aoqi@0: aoqi@0: // loader constraints - print under SD_lock aoqi@0: constraints()->print(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void SystemDictionary::verify() { aoqi@0: guarantee(dictionary() != NULL, "Verify of system dictionary failed"); aoqi@0: guarantee(constraints() != NULL, aoqi@0: "Verify of loader constraints failed"); aoqi@0: guarantee(dictionary()->number_of_entries() >= 0 && aoqi@0: placeholders()->number_of_entries() >= 0, aoqi@0: "Verify of system dictionary failed"); aoqi@0: aoqi@0: // Verify dictionary aoqi@0: dictionary()->verify(); aoqi@0: aoqi@0: GCMutexLocker mu(SystemDictionary_lock); aoqi@0: placeholders()->verify(); aoqi@0: aoqi@0: // Verify constraint table aoqi@0: guarantee(constraints() != NULL, "Verify of loader constraints failed"); aoqi@0: constraints()->verify(dictionary(), placeholders()); aoqi@0: } aoqi@0: aoqi@0: // utility function for class load event aoqi@0: void SystemDictionary::post_class_load_event(const Ticks& start_time, aoqi@0: instanceKlassHandle k, aoqi@0: Handle initiating_loader) { aoqi@0: #if INCLUDE_TRACE aoqi@0: EventClassLoad event(UNTIMED); aoqi@0: if (event.should_commit()) { aoqi@0: event.set_starttime(start_time); aoqi@0: event.set_loadedClass(k()); aoqi@0: oop defining_class_loader = k->class_loader(); aoqi@0: event.set_definingClassLoader(defining_class_loader != NULL ? aoqi@0: defining_class_loader->klass() : (Klass*)NULL); aoqi@0: oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader(); aoqi@0: event.set_initiatingClassLoader(class_loader != NULL ? aoqi@0: class_loader->klass() : (Klass*)NULL); aoqi@0: event.commit(); aoqi@0: } mgronlun@7367: #endif // INCLUDE_TRACE aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: // statistics code aoqi@0: class ClassStatistics: AllStatic { aoqi@0: private: aoqi@0: static int nclasses; // number of classes aoqi@0: static int nmethods; // number of methods aoqi@0: static int nmethoddata; // number of methodData aoqi@0: static int class_size; // size of class objects in words aoqi@0: static int method_size; // size of method objects in words aoqi@0: static int debug_size; // size of debug info in methods aoqi@0: static int methoddata_size; // size of methodData objects in words aoqi@0: aoqi@0: static void do_class(Klass* k) { aoqi@0: nclasses++; aoqi@0: class_size += k->size(); aoqi@0: if (k->oop_is_instance()) { aoqi@0: InstanceKlass* ik = (InstanceKlass*)k; aoqi@0: class_size += ik->methods()->size(); aoqi@0: class_size += ik->constants()->size(); aoqi@0: class_size += ik->local_interfaces()->size(); aoqi@0: class_size += ik->transitive_interfaces()->size(); aoqi@0: // We do not have to count implementors, since we only store one! aoqi@0: // SSS: How should these be accounted now that they have moved? aoqi@0: // class_size += ik->fields()->length(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static void do_method(Method* m) { aoqi@0: nmethods++; aoqi@0: method_size += m->size(); aoqi@0: // class loader uses same objArray for empty vectors, so don't count these aoqi@0: if (m->has_stackmap_table()) { aoqi@0: method_size += m->stackmap_data()->size(); aoqi@0: } aoqi@0: aoqi@0: MethodData* mdo = m->method_data(); aoqi@0: if (mdo != NULL) { aoqi@0: nmethoddata++; aoqi@0: methoddata_size += mdo->size(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: static void print() { aoqi@0: SystemDictionary::classes_do(do_class); aoqi@0: SystemDictionary::methods_do(do_method); aoqi@0: tty->print_cr("Class statistics:"); aoqi@0: tty->print_cr("%d classes (%d bytes)", nclasses, class_size * oopSize); aoqi@0: tty->print_cr("%d methods (%d bytes = %d base + %d debug info)", nmethods, aoqi@0: (method_size + debug_size) * oopSize, method_size * oopSize, debug_size * oopSize); aoqi@0: tty->print_cr("%d methoddata (%d bytes)", nmethoddata, methoddata_size * oopSize); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: int ClassStatistics::nclasses = 0; aoqi@0: int ClassStatistics::nmethods = 0; aoqi@0: int ClassStatistics::nmethoddata = 0; aoqi@0: int ClassStatistics::class_size = 0; aoqi@0: int ClassStatistics::method_size = 0; aoqi@0: int ClassStatistics::debug_size = 0; aoqi@0: int ClassStatistics::methoddata_size = 0; aoqi@0: aoqi@0: void SystemDictionary::print_class_statistics() { aoqi@0: ResourceMark rm; aoqi@0: ClassStatistics::print(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class MethodStatistics: AllStatic { aoqi@0: public: aoqi@0: enum { aoqi@0: max_parameter_size = 10 aoqi@0: }; aoqi@0: private: aoqi@0: aoqi@0: static int _number_of_methods; aoqi@0: static int _number_of_final_methods; aoqi@0: static int _number_of_static_methods; aoqi@0: static int _number_of_native_methods; aoqi@0: static int _number_of_synchronized_methods; aoqi@0: static int _number_of_profiled_methods; aoqi@0: static int _number_of_bytecodes; aoqi@0: static int _parameter_size_profile[max_parameter_size]; aoqi@0: static int _bytecodes_profile[Bytecodes::number_of_java_codes]; aoqi@0: aoqi@0: static void initialize() { aoqi@0: _number_of_methods = 0; aoqi@0: _number_of_final_methods = 0; aoqi@0: _number_of_static_methods = 0; aoqi@0: _number_of_native_methods = 0; aoqi@0: _number_of_synchronized_methods = 0; aoqi@0: _number_of_profiled_methods = 0; aoqi@0: _number_of_bytecodes = 0; aoqi@0: for (int i = 0; i < max_parameter_size ; i++) _parameter_size_profile[i] = 0; aoqi@0: for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile [j] = 0; aoqi@0: }; aoqi@0: aoqi@0: static void do_method(Method* m) { aoqi@0: _number_of_methods++; aoqi@0: // collect flag info aoqi@0: if (m->is_final() ) _number_of_final_methods++; aoqi@0: if (m->is_static() ) _number_of_static_methods++; aoqi@0: if (m->is_native() ) _number_of_native_methods++; aoqi@0: if (m->is_synchronized()) _number_of_synchronized_methods++; aoqi@0: if (m->method_data() != NULL) _number_of_profiled_methods++; aoqi@0: // collect parameter size info (add one for receiver, if any) aoqi@0: _parameter_size_profile[MIN2(m->size_of_parameters() + (m->is_static() ? 0 : 1), max_parameter_size - 1)]++; aoqi@0: // collect bytecodes info aoqi@0: { aoqi@0: Thread *thread = Thread::current(); aoqi@0: HandleMark hm(thread); aoqi@0: BytecodeStream s(methodHandle(thread, m)); aoqi@0: Bytecodes::Code c; aoqi@0: while ((c = s.next()) >= 0) { aoqi@0: _number_of_bytecodes++; aoqi@0: _bytecodes_profile[c]++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: static void print() { aoqi@0: initialize(); aoqi@0: SystemDictionary::methods_do(do_method); aoqi@0: // generate output aoqi@0: tty->cr(); aoqi@0: tty->print_cr("Method statistics (static):"); aoqi@0: // flag distribution aoqi@0: tty->cr(); aoqi@0: tty->print_cr("%6d final methods %6.1f%%", _number_of_final_methods , _number_of_final_methods * 100.0F / _number_of_methods); aoqi@0: tty->print_cr("%6d static methods %6.1f%%", _number_of_static_methods , _number_of_static_methods * 100.0F / _number_of_methods); aoqi@0: tty->print_cr("%6d native methods %6.1f%%", _number_of_native_methods , _number_of_native_methods * 100.0F / _number_of_methods); aoqi@0: tty->print_cr("%6d synchronized methods %6.1f%%", _number_of_synchronized_methods, _number_of_synchronized_methods * 100.0F / _number_of_methods); aoqi@0: tty->print_cr("%6d profiled methods %6.1f%%", _number_of_profiled_methods, _number_of_profiled_methods * 100.0F / _number_of_methods); aoqi@0: // parameter size profile aoqi@0: tty->cr(); aoqi@0: { int tot = 0; aoqi@0: int avg = 0; aoqi@0: for (int i = 0; i < max_parameter_size; i++) { aoqi@0: int n = _parameter_size_profile[i]; aoqi@0: tot += n; aoqi@0: avg += n*i; aoqi@0: tty->print_cr("parameter size = %1d: %6d methods %5.1f%%", i, n, n * 100.0F / _number_of_methods); aoqi@0: } aoqi@0: assert(tot == _number_of_methods, "should be the same"); aoqi@0: tty->print_cr(" %6d methods 100.0%%", _number_of_methods); aoqi@0: tty->print_cr("(average parameter size = %3.1f including receiver, if any)", (float)avg / _number_of_methods); aoqi@0: } aoqi@0: // bytecodes profile aoqi@0: tty->cr(); aoqi@0: { int tot = 0; aoqi@0: for (int i = 0; i < Bytecodes::number_of_java_codes; i++) { aoqi@0: if (Bytecodes::is_defined(i)) { aoqi@0: Bytecodes::Code c = Bytecodes::cast(i); aoqi@0: int n = _bytecodes_profile[c]; aoqi@0: tot += n; aoqi@0: tty->print_cr("%9d %7.3f%% %s", n, n * 100.0F / _number_of_bytecodes, Bytecodes::name(c)); aoqi@0: } aoqi@0: } aoqi@0: assert(tot == _number_of_bytecodes, "should be the same"); aoqi@0: tty->print_cr("%9d 100.000%%", _number_of_bytecodes); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: int MethodStatistics::_number_of_methods; aoqi@0: int MethodStatistics::_number_of_final_methods; aoqi@0: int MethodStatistics::_number_of_static_methods; aoqi@0: int MethodStatistics::_number_of_native_methods; aoqi@0: int MethodStatistics::_number_of_synchronized_methods; aoqi@0: int MethodStatistics::_number_of_profiled_methods; aoqi@0: int MethodStatistics::_number_of_bytecodes; aoqi@0: int MethodStatistics::_parameter_size_profile[MethodStatistics::max_parameter_size]; aoqi@0: int MethodStatistics::_bytecodes_profile[Bytecodes::number_of_java_codes]; aoqi@0: aoqi@0: aoqi@0: void SystemDictionary::print_method_statistics() { aoqi@0: MethodStatistics::print(); aoqi@0: } aoqi@0: aoqi@0: #endif // PRODUCT