instanceKlass.cpp revision 113:ba764ed4b6f2
133965Sjdp/* 233965Sjdp * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 333965Sjdp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 433965Sjdp * 533965Sjdp * This code is free software; you can redistribute it and/or modify it 633965Sjdp * under the terms of the GNU General Public License version 2 only, as 733965Sjdp * published by the Free Software Foundation. 833965Sjdp * 933965Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT 1033965Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1133965Sjdp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1233965Sjdp * version 2 for more details (a copy is included in the LICENSE file that 1333965Sjdp * accompanied this code). 1433965Sjdp * 1533965Sjdp * You should have received a copy of the GNU General Public License version 1633965Sjdp * 2 along with this work; if not, write to the Free Software Foundation, 1733965Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1833965Sjdp * 1933965Sjdp * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2033965Sjdp * CA 95054 USA or visit www.sun.com if you need additional information or 2133965Sjdp * have any questions. 2233965Sjdp * 2333965Sjdp */ 2433965Sjdp 2533965Sjdp# include "incls/_precompiled.incl" 2633965Sjdp# include "incls/_instanceKlass.cpp.incl" 2733965Sjdp 2833965Sjdpbool instanceKlass::should_be_initialized() const { 2933965Sjdp return !is_initialized(); 3033965Sjdp} 3133965Sjdp 3233965SjdpklassVtable* instanceKlass::vtable() const { 3333965Sjdp return new klassVtable(as_klassOop(), start_of_vtable(), vtable_length() / vtableEntry::size()); 3433965Sjdp} 3533965Sjdp 3633965SjdpklassItable* instanceKlass::itable() const { 3733965Sjdp return new klassItable(as_klassOop()); 3833965Sjdp} 3933965Sjdp 4033965Sjdpvoid instanceKlass::eager_initialize(Thread *thread) { 4133965Sjdp if (!EagerInitialization) return; 4233965Sjdp 4333965Sjdp if (this->is_not_initialized()) { 4433965Sjdp // abort if the the class has a class initializer 4533965Sjdp if (this->class_initializer() != NULL) return; 4633965Sjdp 4733965Sjdp // abort if it is java.lang.Object (initialization is handled in genesis) 4833965Sjdp klassOop super = this->super(); 4933965Sjdp if (super == NULL) return; 5033965Sjdp 5133965Sjdp // abort if the super class should be initialized 5233965Sjdp if (!instanceKlass::cast(super)->is_initialized()) return; 5333965Sjdp 5433965Sjdp // call body to expose the this pointer 5533965Sjdp instanceKlassHandle this_oop(thread, this->as_klassOop()); 5633965Sjdp eager_initialize_impl(this_oop); 5733965Sjdp } 5833965Sjdp} 5933965Sjdp 6033965Sjdp 6133965Sjdpvoid instanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) { 6233965Sjdp EXCEPTION_MARK; 6333965Sjdp ObjectLocker ol(this_oop, THREAD); 6433965Sjdp 6533965Sjdp // abort if someone beat us to the initialization 6633965Sjdp if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized() 6733965Sjdp 6833965Sjdp ClassState old_state = this_oop->_init_state; 6933965Sjdp link_class_impl(this_oop, true, THREAD); 7033965Sjdp if (HAS_PENDING_EXCEPTION) { 7133965Sjdp CLEAR_PENDING_EXCEPTION; 7233965Sjdp // Abort if linking the class throws an exception. 7333965Sjdp 7433965Sjdp // Use a test to avoid redundantly resetting the state if there's 7560484Sobrien // no change. Set_init_state() asserts that state changes make 7633965Sjdp // progress, whereas here we might just be spinning in place. 7733965Sjdp if( old_state != this_oop->_init_state ) 7860484Sobrien this_oop->set_init_state (old_state); 7933965Sjdp } else { 8060484Sobrien // linking successfull, mark class as initialized 8133965Sjdp this_oop->set_init_state (fully_initialized); 8233965Sjdp // trace 8360484Sobrien if (TraceClassInitialization) { 8460484Sobrien ResourceMark rm(THREAD); 8533965Sjdp tty->print_cr("[Initialized %s without side effects]", this_oop->external_name()); 8633965Sjdp } 8733965Sjdp } 8833965Sjdp} 8933965Sjdp 9033965Sjdp 9133965Sjdp// See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 9233965Sjdp// process. The step comments refers to the procedure described in that section. 9333965Sjdp// Note: implementation moved to static method to expose the this pointer. 9433965Sjdpvoid instanceKlass::initialize(TRAPS) { 9533965Sjdp if (this->should_be_initialized()) { 9633965Sjdp HandleMark hm(THREAD); 9733965Sjdp instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 9833965Sjdp initialize_impl(this_oop, CHECK); 9933965Sjdp // Note: at this point the class may be initialized 10033965Sjdp // OR it may be in the state of being initialized 10133965Sjdp // in case of recursive initialization! 10233965Sjdp } else { 10333965Sjdp assert(is_initialized(), "sanity check"); 10460484Sobrien } 10533965Sjdp} 10633965Sjdp 10733965Sjdp 10833965Sjdpbool instanceKlass::verify_code( 10933965Sjdp instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) { 11033965Sjdp // 1) Verify the bytecodes 11133965Sjdp Verifier::Mode mode = 11233965Sjdp throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 11333965Sjdp return Verifier::verify(this_oop, mode, CHECK_false); 11433965Sjdp} 11533965Sjdp 11633965Sjdp 11733965Sjdp// Used exclusively by the shared spaces dump mechanism to prevent 11833965Sjdp// classes mapped into the shared regions in new VMs from appearing linked. 11933965Sjdp 12033965Sjdpvoid instanceKlass::unlink_class() { 12133965Sjdp assert(is_linked(), "must be linked"); 12233965Sjdp _init_state = loaded; 12333965Sjdp} 12433965Sjdp 12533965Sjdpvoid instanceKlass::link_class(TRAPS) { 12633965Sjdp assert(is_loaded(), "must be loaded"); 12733965Sjdp if (!is_linked()) { 12833965Sjdp instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 12933965Sjdp link_class_impl(this_oop, true, CHECK); 13033965Sjdp } 13133965Sjdp} 13233965Sjdp 13333965Sjdp// Called to verify that a class can link during initialization, without 13460484Sobrien// throwing a VerifyError. 13560484Sobrienbool instanceKlass::link_class_or_fail(TRAPS) { 13660484Sobrien assert(is_loaded(), "must be loaded"); 13733965Sjdp if (!is_linked()) { 13833965Sjdp instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 13933965Sjdp link_class_impl(this_oop, false, CHECK_false); 14033965Sjdp } 14133965Sjdp return is_linked(); 14233965Sjdp} 14333965Sjdp 14433965Sjdpbool instanceKlass::link_class_impl( 14533965Sjdp instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) { 14633965Sjdp // check for error state 14733965Sjdp if (this_oop->is_in_error_state()) { 14833965Sjdp ResourceMark rm(THREAD); 14933965Sjdp THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), 15033965Sjdp this_oop->external_name(), false); 15133965Sjdp } 15233965Sjdp // return if already verified 15333965Sjdp if (this_oop->is_linked()) { 15433965Sjdp return true; 15533965Sjdp } 15633965Sjdp 15733965Sjdp // Timing 15833965Sjdp // timer handles recursion 15933965Sjdp assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 16033965Sjdp JavaThread* jt = (JavaThread*)THREAD; 16133965Sjdp PerfTraceTimedEvent vmtimer(ClassLoader::perf_class_link_time(), 16233965Sjdp ClassLoader::perf_classes_linked(), 16333965Sjdp jt->get_thread_stat()->class_link_recursion_count_addr()); 16433965Sjdp 16533965Sjdp // link super class before linking this class 16633965Sjdp instanceKlassHandle super(THREAD, this_oop->super()); 16733965Sjdp if (super.not_null()) { 16833965Sjdp if (super->is_interface()) { // check if super class is an interface 16933965Sjdp ResourceMark rm(THREAD); 17033965Sjdp Exceptions::fthrow( 17133965Sjdp THREAD_AND_LOCATION, 17233965Sjdp vmSymbolHandles::java_lang_IncompatibleClassChangeError(), 17333965Sjdp "class %s has interface %s as super class", 17433965Sjdp this_oop->external_name(), 17533965Sjdp super->external_name() 17633965Sjdp ); 17733965Sjdp return false; 17833965Sjdp } 17933965Sjdp 18033965Sjdp link_class_impl(super, throw_verifyerror, CHECK_false); 18133965Sjdp } 18233965Sjdp 18333965Sjdp // link all interfaces implemented by this class before linking this class 18433965Sjdp objArrayHandle interfaces (THREAD, this_oop->local_interfaces()); 18533965Sjdp int num_interfaces = interfaces->length(); 18633965Sjdp for (int index = 0; index < num_interfaces; index++) { 18733965Sjdp HandleMark hm(THREAD); 18833965Sjdp instanceKlassHandle ih(THREAD, klassOop(interfaces->obj_at(index))); 18933965Sjdp link_class_impl(ih, throw_verifyerror, CHECK_false); 19033965Sjdp } 19133965Sjdp 19233965Sjdp // in case the class is linked in the process of linking its superclasses 19333965Sjdp if (this_oop->is_linked()) { 19433965Sjdp return true; 19533965Sjdp } 19633965Sjdp 19733965Sjdp // verification & rewriting 19833965Sjdp { 19933965Sjdp ObjectLocker ol(this_oop, THREAD); 20033965Sjdp // rewritten will have been set if loader constraint error found 20133965Sjdp // on an earlier link attempt 20233965Sjdp // don't verify or rewrite if already rewritten 20333965Sjdp if (!this_oop->is_linked()) { 20433965Sjdp if (!this_oop->is_rewritten()) { 20533965Sjdp { 20633965Sjdp assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 20733965Sjdp JavaThread* jt = (JavaThread*)THREAD; 20833965Sjdp // Timer includes any side effects of class verification (resolution, 20960484Sobrien // etc), but not recursive entry into verify_code(). 21060484Sobrien PerfTraceTime timer(ClassLoader::perf_class_verify_time(), 21160484Sobrien jt->get_thread_stat()->class_verify_recursion_count_addr()); 21260484Sobrien bool verify_ok = verify_code(this_oop, throw_verifyerror, THREAD); 21338889Sjdp if (!verify_ok) { 21438889Sjdp return false; 21538889Sjdp } 21638889Sjdp } 21738889Sjdp 21838889Sjdp // Just in case a side-effect of verify linked this class already 21938889Sjdp // (which can sometimes happen since the verifier loads classes 22038889Sjdp // using custom class loaders, which are free to initialize things) 22138889Sjdp if (this_oop->is_linked()) { 22238889Sjdp return true; 22338889Sjdp } 22438889Sjdp 22538889Sjdp // also sets rewritten 22660484Sobrien this_oop->rewrite_class(CHECK_false); 22760484Sobrien } 22860484Sobrien 22960484Sobrien // Initialize the vtable and interface table after 23060484Sobrien // methods have been rewritten since rewrite may 23160484Sobrien // fabricate new methodOops. 23260484Sobrien // also does loader constraint checking 23360484Sobrien if (!this_oop()->is_shared()) { 23460484Sobrien ResourceMark rm(THREAD); 23560484Sobrien this_oop->vtable()->initialize_vtable(true, CHECK_false); 23660484Sobrien this_oop->itable()->initialize_itable(true, CHECK_false); 23760484Sobrien } 23860484Sobrien#ifdef ASSERT 23960484Sobrien else { 24060484Sobrien ResourceMark rm(THREAD); 24160484Sobrien this_oop->vtable()->verify(tty, true); 24260484Sobrien // In case itable verification is ever added. 24360484Sobrien // this_oop->itable()->verify(tty, true); 24460484Sobrien } 24560484Sobrien#endif 24660484Sobrien this_oop->set_init_state(linked); 24760484Sobrien if (JvmtiExport::should_post_class_prepare()) { 24860484Sobrien Thread *thread = THREAD; 24960484Sobrien assert(thread->is_Java_thread(), "thread->is_Java_thread()"); 25060484Sobrien JvmtiExport::post_class_prepare((JavaThread *) thread, this_oop()); 25160484Sobrien } 25260484Sobrien } 25360484Sobrien } 25460484Sobrien return true; 25560484Sobrien} 25660484Sobrien 25760484Sobrien 25860484Sobrien// Rewrite the byte codes of all of the methods of a class. 25960484Sobrien// Three cases: 26060484Sobrien// During the link of a newly loaded class. 26160484Sobrien// During the preloading of classes to be written to the shared spaces. 26260484Sobrien// - Rewrite the methods and update the method entry points. 26360484Sobrien// 26460484Sobrien// During the link of a class in the shared spaces. 26560484Sobrien// - The methods were already rewritten, update the metho entry points. 26660484Sobrien// 26760484Sobrien// The rewriter must be called exactly once. Rewriting must happen after 26860484Sobrien// verification but before the first method of the class is executed. 26960484Sobrien 27060484Sobrienvoid instanceKlass::rewrite_class(TRAPS) { 27160484Sobrien assert(is_loaded(), "must be loaded"); 27260484Sobrien instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 27360484Sobrien if (this_oop->is_rewritten()) { 27460484Sobrien assert(this_oop()->is_shared(), "rewriting an unshared class?"); 27560484Sobrien return; 27660484Sobrien } 27760484Sobrien Rewriter::rewrite(this_oop, CHECK); // No exception can happen here 27860484Sobrien this_oop->set_rewritten(); 27960484Sobrien} 28060484Sobrien 28160484Sobrien 28260484Sobrienvoid instanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) { 28377298Sobrien // Make sure klass is linked (verified) before initialization 28477298Sobrien // A class could already be verified, since it has been reflected upon. 28577298Sobrien this_oop->link_class(CHECK); 28677298Sobrien 28777298Sobrien // refer to the JVM book page 47 for description of steps 28877298Sobrien // Step 1 28977298Sobrien { ObjectLocker ol(this_oop, THREAD); 29077298Sobrien 29177298Sobrien Thread *self = THREAD; // it's passed the current thread 29277298Sobrien 29377298Sobrien // Step 2 29460484Sobrien // If we were to use wait() instead of waitInterruptibly() then 29533965Sjdp // we might end up throwing IE from link/symbol resolution sites 29633965Sjdp // that aren't expected to throw. This would wreak havoc. See 6320309. 29733965Sjdp while(this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self)) { 29833965Sjdp ol.waitUninterruptibly(CHECK); 29933965Sjdp } 30033965Sjdp 30133965Sjdp // Step 3 30233965Sjdp if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) 30333965Sjdp return; 30433965Sjdp 30533965Sjdp // Step 4 30633965Sjdp if (this_oop->is_initialized()) 30733965Sjdp return; 30833965Sjdp 30933965Sjdp // Step 5 31033965Sjdp if (this_oop->is_in_error_state()) { 31133965Sjdp ResourceMark rm(THREAD); 31233965Sjdp const char* desc = "Could not initialize class "; 31333965Sjdp const char* className = this_oop->external_name(); 31433965Sjdp size_t msglen = strlen(desc) + strlen(className) + 1; 31533965Sjdp char* message = NEW_C_HEAP_ARRAY(char, msglen); 31633965Sjdp if (NULL == message) { 31733965Sjdp // Out of memory: can't create detailed error message 31833965Sjdp THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className); 31933965Sjdp } else { 32033965Sjdp jio_snprintf(message, msglen, "%s%s", desc, className); 32133965Sjdp THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message); 32233965Sjdp } 32333965Sjdp } 32433965Sjdp 32533965Sjdp // Step 6 32633965Sjdp this_oop->set_init_state(being_initialized); 32733965Sjdp this_oop->set_init_thread(self); 32833965Sjdp } 32933965Sjdp 33033965Sjdp // Step 7 33133965Sjdp klassOop super_klass = this_oop->super(); 33233965Sjdp if (super_klass != NULL && !this_oop->is_interface() && Klass::cast(super_klass)->should_be_initialized()) { 33333965Sjdp Klass::cast(super_klass)->initialize(THREAD); 33433965Sjdp 33533965Sjdp if (HAS_PENDING_EXCEPTION) { 33633965Sjdp Handle e(THREAD, PENDING_EXCEPTION); 33733965Sjdp CLEAR_PENDING_EXCEPTION; 33833965Sjdp { 33933965Sjdp EXCEPTION_MARK; 34033965Sjdp this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads 34133965Sjdp CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below 34233965Sjdp } 34333965Sjdp THROW_OOP(e()); 34433965Sjdp } 34533965Sjdp } 34633965Sjdp 34733965Sjdp // Step 8 34833965Sjdp { 34933965Sjdp assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 35033965Sjdp JavaThread* jt = (JavaThread*)THREAD; 35133965Sjdp // Timer includes any side effects of class initialization (resolution, 35233965Sjdp // etc), but not recursive entry into call_class_initializer(). 35333965Sjdp PerfTraceTimedEvent timer(ClassLoader::perf_class_init_time(), 35433965Sjdp ClassLoader::perf_classes_inited(), 35533965Sjdp jt->get_thread_stat()->class_init_recursion_count_addr()); 35633965Sjdp this_oop->call_class_initializer(THREAD); 35733965Sjdp } 35833965Sjdp 35933965Sjdp // Step 9 36033965Sjdp if (!HAS_PENDING_EXCEPTION) { 36133965Sjdp this_oop->set_initialization_state_and_notify(fully_initialized, CHECK); 36233965Sjdp { ResourceMark rm(THREAD); 36333965Sjdp debug_only(this_oop->vtable()->verify(tty, true);) 36433965Sjdp } 36533965Sjdp } 36633965Sjdp else { 36733965Sjdp // Step 10 and 11 36833965Sjdp Handle e(THREAD, PENDING_EXCEPTION); 36933965Sjdp CLEAR_PENDING_EXCEPTION; 37033965Sjdp { 37133965Sjdp EXCEPTION_MARK; 37233965Sjdp this_oop->set_initialization_state_and_notify(initialization_error, THREAD); 37333965Sjdp CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 37433965Sjdp } 37533965Sjdp if (e->is_a(SystemDictionary::error_klass())) { 37677298Sobrien THROW_OOP(e()); 37777298Sobrien } else { 37877298Sobrien JavaCallArguments args(e); 37977298Sobrien THROW_ARG(vmSymbolHandles::java_lang_ExceptionInInitializerError(), 38077298Sobrien vmSymbolHandles::throwable_void_signature(), 38177298Sobrien &args); 38277298Sobrien } 38377298Sobrien } 38477298Sobrien} 38577298Sobrien 38677298Sobrien 38777298Sobrien// Note: implementation moved to static method to expose the this pointer. 38877298Sobrienvoid instanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 38977298Sobrien instanceKlassHandle kh(THREAD, this->as_klassOop()); 39077298Sobrien set_initialization_state_and_notify_impl(kh, state, CHECK); 39177298Sobrien} 39277298Sobrien 39377298Sobrienvoid instanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) { 39477298Sobrien ObjectLocker ol(this_oop, THREAD); 39577298Sobrien this_oop->set_init_state(state); 39677298Sobrien ol.notify_all(CHECK); 39777298Sobrien} 39833965Sjdp 39933965Sjdpvoid instanceKlass::add_implementor(klassOop k) { 40033965Sjdp assert(Compile_lock->owned_by_self(), ""); 40133965Sjdp // Filter out my subinterfaces. 40233965Sjdp // (Note: Interfaces are never on the subklass list.) 40333965Sjdp if (instanceKlass::cast(k)->is_interface()) return; 40433965Sjdp 40533965Sjdp // Filter out subclasses whose supers already implement me. 40633965Sjdp // (Note: CHA must walk subclasses of direct implementors 40733965Sjdp // in order to locate indirect implementors.) 40833965Sjdp klassOop sk = instanceKlass::cast(k)->super(); 40933965Sjdp if (sk != NULL && instanceKlass::cast(sk)->implements_interface(as_klassOop())) 41033965Sjdp // We only need to check one immediate superclass, since the 41133965Sjdp // implements_interface query looks at transitive_interfaces. 41233965Sjdp // Any supers of the super have the same (or fewer) transitive_interfaces. 41333965Sjdp return; 41433965Sjdp 41533965Sjdp // Update number of implementors 41633965Sjdp int i = _nof_implementors++; 41733965Sjdp 41833965Sjdp // Record this implementor, if there are not too many already 41933965Sjdp if (i < implementors_limit) { 42033965Sjdp assert(_implementors[i] == NULL, "should be exactly one implementor"); 42133965Sjdp oop_store_without_check((oop*)&_implementors[i], k); 42233965Sjdp } else if (i == implementors_limit) { 42333965Sjdp // clear out the list on first overflow 42433965Sjdp for (int i2 = 0; i2 < implementors_limit; i2++) 42533965Sjdp oop_store_without_check((oop*)&_implementors[i2], NULL); 42633965Sjdp } 42733965Sjdp 42833965Sjdp // The implementor also implements the transitive_interfaces 42933965Sjdp for (int index = 0; index < local_interfaces()->length(); index++) { 43033965Sjdp instanceKlass::cast(klassOop(local_interfaces()->obj_at(index)))->add_implementor(k); 43133965Sjdp } 43233965Sjdp} 43338889Sjdp 43433965Sjdpvoid instanceKlass::init_implementor() { 43533965Sjdp for (int i = 0; i < implementors_limit; i++) 43633965Sjdp oop_store_without_check((oop*)&_implementors[i], NULL); 43733965Sjdp _nof_implementors = 0; 43833965Sjdp} 43938889Sjdp 44033965Sjdp 44133965Sjdpvoid instanceKlass::process_interfaces(Thread *thread) { 44233965Sjdp // link this class into the implementors list of every interface it implements 44333965Sjdp KlassHandle this_as_oop (thread, this->as_klassOop()); 44433965Sjdp for (int i = local_interfaces()->length() - 1; i >= 0; i--) { 44533965Sjdp assert(local_interfaces()->obj_at(i)->is_klass(), "must be a klass"); 44633965Sjdp instanceKlass* interf = instanceKlass::cast(klassOop(local_interfaces()->obj_at(i))); 44733965Sjdp assert(interf->is_interface(), "expected interface"); 44833965Sjdp interf->add_implementor(this_as_oop()); 44933965Sjdp } 45038889Sjdp} 45133965Sjdp 45233965Sjdpbool instanceKlass::can_be_primary_super_slow() const { 45333965Sjdp if (is_interface()) 45433965Sjdp return false; 45533965Sjdp else 45633965Sjdp return Klass::can_be_primary_super_slow(); 45733965Sjdp} 45833965Sjdp 45933965SjdpobjArrayOop instanceKlass::compute_secondary_supers(int num_extra_slots, TRAPS) { 46033965Sjdp // The secondaries are the implemented interfaces. 46138889Sjdp instanceKlass* ik = instanceKlass::cast(as_klassOop()); 46233965Sjdp objArrayHandle interfaces (THREAD, ik->transitive_interfaces()); 46333965Sjdp int num_secondaries = num_extra_slots + interfaces->length(); 46433965Sjdp if (num_secondaries == 0) { 46533965Sjdp return Universe::the_empty_system_obj_array(); 46633965Sjdp } else if (num_extra_slots == 0) { 46733965Sjdp return interfaces(); 46833965Sjdp } else { 46933965Sjdp // a mix of both 47033965Sjdp objArrayOop secondaries = oopFactory::new_system_objArray(num_secondaries, CHECK_NULL); 47133965Sjdp for (int i = 0; i < interfaces->length(); i++) { 47238889Sjdp secondaries->obj_at_put(num_extra_slots+i, interfaces->obj_at(i)); 47333965Sjdp } 47433965Sjdp return secondaries; 47533965Sjdp } 47633965Sjdp} 47733965Sjdp 47877298Sobrienbool instanceKlass::compute_is_subtype_of(klassOop k) { 47977298Sobrien if (Klass::cast(k)->is_interface()) { 48077298Sobrien return implements_interface(k); 48177298Sobrien } else { 48277298Sobrien return Klass::compute_is_subtype_of(k); 48377298Sobrien } 48477298Sobrien} 48577298Sobrien 48677298Sobrienbool instanceKlass::implements_interface(klassOop k) const { 48777298Sobrien if (as_klassOop() == k) return true; 48877298Sobrien assert(Klass::cast(k)->is_interface(), "should be an interface class"); 48977298Sobrien for (int i = 0; i < transitive_interfaces()->length(); i++) { 49077298Sobrien if (transitive_interfaces()->obj_at(i) == k) { 49177298Sobrien return true; 49277298Sobrien } 49377298Sobrien } 49477298Sobrien return false; 49577298Sobrien} 49677298Sobrien 49777298SobrienobjArrayOop instanceKlass::allocate_objArray(int n, int length, TRAPS) { 49877298Sobrien if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 49977298Sobrien if (length > arrayOopDesc::max_array_length(T_OBJECT)) { 50077298Sobrien THROW_OOP_0(Universe::out_of_memory_error_array_size()); 50177298Sobrien } 50233965Sjdp int size = objArrayOopDesc::object_size(length); 50333965Sjdp klassOop ak = array_klass(n, CHECK_NULL); 50433965Sjdp KlassHandle h_ak (THREAD, ak); 50533965Sjdp objArrayOop o = 50633965Sjdp (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL); 50733965Sjdp return o; 50833965Sjdp} 50933965Sjdp 51033965SjdpinstanceOop instanceKlass::register_finalizer(instanceOop i, TRAPS) { 51133965Sjdp if (TraceFinalizerRegistration) { 51233965Sjdp tty->print("Registered "); 513104834Sobrien i->print_value_on(tty); 514104834Sobrien tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i); 51533965Sjdp } 51633965Sjdp instanceHandle h_i(THREAD, i); 51733965Sjdp // Pass the handle as argument, JavaCalls::call expects oop as jobjects 51833965Sjdp JavaValue result(T_VOID); 51933965Sjdp JavaCallArguments args(h_i); 52033965Sjdp methodHandle mh (THREAD, Universe::finalizer_register_method()); 521104834Sobrien JavaCalls::call(&result, mh, &args, CHECK_NULL); 522104834Sobrien return h_i(); 52333965Sjdp} 52433965Sjdp 52533965SjdpinstanceOop instanceKlass::allocate_instance(TRAPS) { 52633965Sjdp bool has_finalizer_flag = has_finalizer(); // Query before possible GC 52733965Sjdp int size = size_helper(); // Query before forming handle. 52833965Sjdp 52933965Sjdp KlassHandle h_k(THREAD, as_klassOop()); 53033965Sjdp 53133965Sjdp instanceOop i; 53233965Sjdp 53333965Sjdp i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL); 53477298Sobrien if (has_finalizer_flag && !RegisterFinalizersAtInit) { 53577298Sobrien i = register_finalizer(i, CHECK_NULL); 53633965Sjdp } 53733965Sjdp return i; 53833965Sjdp} 53938889Sjdp 540104834SobrieninstanceOop instanceKlass::allocate_permanent_instance(TRAPS) { 54133965Sjdp // Finalizer registration occurs in the Object.<init> constructor 54233965Sjdp // and constructors normally aren't run when allocating perm 54333965Sjdp // instances so simply disallow finalizable perm objects. This can 54433965Sjdp // be relaxed if a need for it is found. 54533965Sjdp assert(!has_finalizer(), "perm objects not allowed to have finalizers"); 54633965Sjdp int size = size_helper(); // Query before forming handle. 54733965Sjdp KlassHandle h_k(THREAD, as_klassOop()); 54833965Sjdp instanceOop i = (instanceOop) 54933965Sjdp CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL); 55033965Sjdp return i; 55133965Sjdp} 55233965Sjdp 55333965Sjdpvoid instanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) { 55433965Sjdp if (is_interface() || is_abstract()) { 55533965Sjdp ResourceMark rm(THREAD); 55633965Sjdp THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 55733965Sjdp : vmSymbols::java_lang_InstantiationException(), external_name()); 558104834Sobrien } 55933965Sjdp if (as_klassOop() == SystemDictionary::class_klass()) { 56033965Sjdp ResourceMark rm(THREAD); 56133965Sjdp THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError() 56233965Sjdp : vmSymbols::java_lang_IllegalAccessException(), external_name()); 56333965Sjdp } 56433965Sjdp} 56533965Sjdp 56633965SjdpklassOop instanceKlass::array_klass_impl(bool or_null, int n, TRAPS) { 56733965Sjdp instanceKlassHandle this_oop(THREAD, as_klassOop()); 56833965Sjdp return array_klass_impl(this_oop, or_null, n, THREAD); 56933965Sjdp} 57033965Sjdp 57133965SjdpklassOop instanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) { 57233965Sjdp if (this_oop->array_klasses() == NULL) { 57333965Sjdp if (or_null) return NULL; 57433965Sjdp 57533965Sjdp ResourceMark rm; 57633965Sjdp JavaThread *jt = (JavaThread *)THREAD; 57733965Sjdp { 57833965Sjdp // Atomic creation of array_klasses 57933965Sjdp MutexLocker mc(Compile_lock, THREAD); // for vtables 58033965Sjdp MutexLocker ma(MultiArray_lock, THREAD); 58133965Sjdp 58233965Sjdp // Check if update has already taken place 58333965Sjdp if (this_oop->array_klasses() == NULL) { 58433965Sjdp objArrayKlassKlass* oakk = 58533965Sjdp (objArrayKlassKlass*)Universe::objArrayKlassKlassObj()->klass_part(); 58633965Sjdp 58733965Sjdp klassOop k = oakk->allocate_objArray_klass(1, this_oop, CHECK_NULL); 58833965Sjdp this_oop->set_array_klasses(k); 58933965Sjdp } 59033965Sjdp } 59133965Sjdp } 59233965Sjdp // _this will always be set at this point 59333965Sjdp objArrayKlass* oak = (objArrayKlass*)this_oop->array_klasses()->klass_part(); 59433965Sjdp if (or_null) { 59533965Sjdp return oak->array_klass_or_null(n); 59633965Sjdp } 59733965Sjdp return oak->array_klass(n, CHECK_NULL); 59833965Sjdp} 59933965Sjdp 60033965SjdpklassOop instanceKlass::array_klass_impl(bool or_null, TRAPS) { 60133965Sjdp return array_klass_impl(or_null, 1, THREAD); 60233965Sjdp} 60333965Sjdp 60433965Sjdpvoid instanceKlass::call_class_initializer(TRAPS) { 60533965Sjdp instanceKlassHandle ik (THREAD, as_klassOop()); 60633965Sjdp call_class_initializer_impl(ik, THREAD); 60733965Sjdp} 60833965Sjdp 60933965Sjdpstatic int call_class_initializer_impl_counter = 0; // for debugging 61033965Sjdp 61133965SjdpmethodOop instanceKlass::class_initializer() { 61233965Sjdp return find_method(vmSymbols::class_initializer_name(), vmSymbols::void_method_signature()); 61333965Sjdp} 61433965Sjdp 61533965Sjdpvoid instanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) { 61633965Sjdp methodHandle h_method(THREAD, this_oop->class_initializer()); 61733965Sjdp assert(!this_oop->is_initialized(), "we cannot initialize twice"); 61833965Sjdp if (TraceClassInitialization) { 61933965Sjdp tty->print("%d Initializing ", call_class_initializer_impl_counter++); 62033965Sjdp this_oop->name()->print_value(); 62133965Sjdp tty->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", (address)this_oop()); 62233965Sjdp } 62333965Sjdp if (h_method() != NULL) { 62433965Sjdp JavaCallArguments args; // No arguments 62533965Sjdp JavaValue result(T_VOID); 62633965Sjdp JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args) 62733965Sjdp } 62833965Sjdp} 62933965Sjdp 63033965Sjdp 63133965Sjdpvoid instanceKlass::mask_for(methodHandle method, int bci, 63233965Sjdp InterpreterOopMap* entry_for) { 63333965Sjdp // Dirty read, then double-check under a lock. 63433965Sjdp if (_oop_map_cache == NULL) { 63533965Sjdp // Otherwise, allocate a new one. 63633965Sjdp MutexLocker x(OopMapCacheAlloc_lock); 63733965Sjdp // First time use. Allocate a cache in C heap 63833965Sjdp if (_oop_map_cache == NULL) { 63933965Sjdp _oop_map_cache = new OopMapCache(); 64033965Sjdp } 64133965Sjdp } 64233965Sjdp // _oop_map_cache is constant after init; lookup below does is own locking. 64333965Sjdp _oop_map_cache->lookup(method, bci, entry_for); 64433965Sjdp} 64533965Sjdp 64633965Sjdp 64733965Sjdpbool instanceKlass::find_local_field(symbolOop name, symbolOop sig, fieldDescriptor* fd) const { 64833965Sjdp const int n = fields()->length(); 64933965Sjdp for (int i = 0; i < n; i += next_offset ) { 65033965Sjdp int name_index = fields()->ushort_at(i + name_index_offset); 65133965Sjdp int sig_index = fields()->ushort_at(i + signature_index_offset); 65233965Sjdp symbolOop f_name = constants()->symbol_at(name_index); 65333965Sjdp symbolOop f_sig = constants()->symbol_at(sig_index); 65433965Sjdp if (f_name == name && f_sig == sig) { 65533965Sjdp fd->initialize(as_klassOop(), i); 65633965Sjdp return true; 65733965Sjdp } 65833965Sjdp } 65933965Sjdp return false; 66033965Sjdp} 66133965Sjdp 66233965Sjdp 66333965Sjdpvoid instanceKlass::field_names_and_sigs_iterate(OopClosure* closure) { 66433965Sjdp const int n = fields()->length(); 665104834Sobrien for (int i = 0; i < n; i += next_offset ) { 66633965Sjdp int name_index = fields()->ushort_at(i + name_index_offset); 66733965Sjdp symbolOop name = constants()->symbol_at(name_index); 668104834Sobrien closure->do_oop((oop*)&name); 66933965Sjdp 67033965Sjdp int sig_index = fields()->ushort_at(i + signature_index_offset); 67133965Sjdp symbolOop sig = constants()->symbol_at(sig_index); 67233965Sjdp closure->do_oop((oop*)&sig); 67333965Sjdp } 67433965Sjdp} 67533965Sjdp 67633965Sjdp 677104834SobrienklassOop instanceKlass::find_interface_field(symbolOop name, symbolOop sig, fieldDescriptor* fd) const { 67833965Sjdp const int n = local_interfaces()->length(); 67933965Sjdp for (int i = 0; i < n; i++) { 68077298Sobrien klassOop intf1 = klassOop(local_interfaces()->obj_at(i)); 68177298Sobrien assert(Klass::cast(intf1)->is_interface(), "just checking type"); 68277298Sobrien // search for field in current interface 68377298Sobrien if (instanceKlass::cast(intf1)->find_local_field(name, sig, fd)) { 68477298Sobrien assert(fd->is_static(), "interface field must be static"); 68577298Sobrien return intf1; 68677298Sobrien } 68777298Sobrien // search for field in direct superinterfaces 68877298Sobrien klassOop intf2 = instanceKlass::cast(intf1)->find_interface_field(name, sig, fd); 68977298Sobrien if (intf2 != NULL) return intf2; 69077298Sobrien } 69177298Sobrien // otherwise field lookup fails 69277298Sobrien return NULL; 69377298Sobrien} 69477298Sobrien 69577298Sobrien 69677298SobrienklassOop instanceKlass::find_field(symbolOop name, symbolOop sig, fieldDescriptor* fd) const { 69777298Sobrien // search order according to newest JVM spec (5.4.3.2, p.167). 69833965Sjdp // 1) search for field in current klass 69933965Sjdp if (find_local_field(name, sig, fd)) { 70033965Sjdp return as_klassOop(); 70133965Sjdp } 70233965Sjdp // 2) search for field recursively in direct superinterfaces 70333965Sjdp { klassOop intf = find_interface_field(name, sig, fd); 70433965Sjdp if (intf != NULL) return intf; 705104834Sobrien } 70633965Sjdp // 3) apply field lookup recursively if superclass exists 70733965Sjdp { klassOop supr = super(); 70833965Sjdp if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, fd); 70933965Sjdp } 71033965Sjdp // 4) otherwise field lookup fails 71133965Sjdp return NULL; 71233965Sjdp} 71360484Sobrien 71460484Sobrien 71560484SobrienklassOop instanceKlass::find_field(symbolOop name, symbolOop sig, bool is_static, fieldDescriptor* fd) const { 71660484Sobrien // search order according to newest JVM spec (5.4.3.2, p.167). 71760484Sobrien // 1) search for field in current klass 71860484Sobrien if (find_local_field(name, sig, fd)) { 71960484Sobrien if (fd->is_static() == is_static) return as_klassOop(); 72033965Sjdp } 72133965Sjdp // 2) search for field recursively in direct superinterfaces 72233965Sjdp if (is_static) { 72333965Sjdp klassOop intf = find_interface_field(name, sig, fd); 72433965Sjdp if (intf != NULL) return intf; 72533965Sjdp } 72633965Sjdp // 3) apply field lookup recursively if superclass exists 72733965Sjdp { klassOop supr = super(); 72833965Sjdp if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, is_static, fd); 72933965Sjdp } 73033965Sjdp // 4) otherwise field lookup fails 73133965Sjdp return NULL; 73233965Sjdp} 73333965Sjdp 73433965Sjdp 73533965Sjdpbool instanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 73633965Sjdp int length = fields()->length(); 73733965Sjdp for (int i = 0; i < length; i += next_offset) { 73833965Sjdp if (offset_from_fields( i ) == offset) { 73933965Sjdp fd->initialize(as_klassOop(), i); 74033965Sjdp if (fd->is_static() == is_static) return true; 74133965Sjdp } 74233965Sjdp } 74333965Sjdp return false; 74433965Sjdp} 74533965Sjdp 74633965Sjdp 74733965Sjdpbool instanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 74877298Sobrien klassOop klass = as_klassOop(); 74933965Sjdp while (klass != NULL) { 75077298Sobrien if (instanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) { 75177298Sobrien return true; 75277298Sobrien } 75377298Sobrien klass = Klass::cast(klass)->super(); 75477298Sobrien } 75577298Sobrien return false; 75677298Sobrien} 75777298Sobrien 75877298Sobrien 75977298Sobrienvoid instanceKlass::methods_do(void f(methodOop method)) { 76077298Sobrien int len = methods()->length(); 76177298Sobrien for (int index = 0; index < len; index++) { 76277298Sobrien methodOop m = methodOop(methods()->obj_at(index)); 76377298Sobrien assert(m->is_method(), "must be method"); 76477298Sobrien f(m); 76577298Sobrien } 76677298Sobrien} 76777298Sobrien 76877298Sobrienvoid instanceKlass::do_local_static_fields(FieldClosure* cl) { 76977298Sobrien fieldDescriptor fd; 77077298Sobrien int length = fields()->length(); 77177298Sobrien for (int i = 0; i < length; i += next_offset) { 77277298Sobrien fd.initialize(as_klassOop(), i); 77377298Sobrien if (fd.is_static()) cl->do_field(&fd); 77477298Sobrien } 77577298Sobrien} 77677298Sobrien 77777298Sobrien 77877298Sobrienvoid instanceKlass::do_local_static_fields(void f(fieldDescriptor*, TRAPS), TRAPS) { 77977298Sobrien instanceKlassHandle h_this(THREAD, as_klassOop()); 78077298Sobrien do_local_static_fields_impl(h_this, f, CHECK); 78177298Sobrien} 78277298Sobrien 78377298Sobrien 78477298Sobrienvoid instanceKlass::do_local_static_fields_impl(instanceKlassHandle this_oop, void f(fieldDescriptor* fd, TRAPS), TRAPS) { 78577298Sobrien fieldDescriptor fd; 78677298Sobrien int length = this_oop->fields()->length(); 78777298Sobrien for (int i = 0; i < length; i += next_offset) { 78877298Sobrien fd.initialize(this_oop(), i); 78977298Sobrien if (fd.is_static()) { f(&fd, CHECK); } // Do NOT remove {}! (CHECK macro expands into several statements) 79077298Sobrien } 79177298Sobrien} 79277298Sobrien 79377298Sobrien 79477298Sobrienstatic int compare_fields_by_offset(int* a, int* b) { 79577298Sobrien return a[0] - b[0]; 79677298Sobrien} 79777298Sobrien 79877298Sobrienvoid instanceKlass::do_nonstatic_fields(FieldClosure* cl) { 79977298Sobrien instanceKlass* super = superklass(); 80077298Sobrien if (super != NULL) { 80177298Sobrien super->do_nonstatic_fields(cl); 80277298Sobrien } 80377298Sobrien fieldDescriptor fd; 80477298Sobrien int length = fields()->length(); 80577298Sobrien // In DebugInfo nonstatic fields are sorted by offset. 80677298Sobrien int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1)); 80777298Sobrien int j = 0; 80877298Sobrien for (int i = 0; i < length; i += next_offset) { 80977298Sobrien fd.initialize(as_klassOop(), i); 81077298Sobrien if (!fd.is_static()) { 81177298Sobrien fields_sorted[j + 0] = fd.offset(); 81277298Sobrien fields_sorted[j + 1] = i; 81377298Sobrien j += 2; 81477298Sobrien } 81577298Sobrien } 81677298Sobrien if (j > 0) { 81777298Sobrien length = j; 81877298Sobrien // _sort_Fn is defined in growableArray.hpp. 81977298Sobrien qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset); 82077298Sobrien for (int i = 0; i < length; i += 2) { 82177298Sobrien fd.initialize(as_klassOop(), fields_sorted[i + 1]); 82277298Sobrien assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields"); 82377298Sobrien cl->do_field(&fd); 82477298Sobrien } 82577298Sobrien } 82677298Sobrien FREE_C_HEAP_ARRAY(int, fields_sorted); 82777298Sobrien} 82833965Sjdp 82933965Sjdp 83033965Sjdpvoid instanceKlass::array_klasses_do(void f(klassOop k)) { 83133965Sjdp if (array_klasses() != NULL) 83233965Sjdp arrayKlass::cast(array_klasses())->array_klasses_do(f); 83333965Sjdp} 83433965Sjdp 83533965Sjdp 83633965Sjdpvoid instanceKlass::with_array_klasses_do(void f(klassOop k)) { 83733965Sjdp f(as_klassOop()); 83833965Sjdp array_klasses_do(f); 83933965Sjdp} 84033965Sjdp 84133965Sjdp#ifdef ASSERT 84233965Sjdpstatic int linear_search(objArrayOop methods, symbolOop name, symbolOop signature) { 84333965Sjdp int len = methods->length(); 844 for (int index = 0; index < len; index++) { 845 methodOop m = (methodOop)(methods->obj_at(index)); 846 assert(m->is_method(), "must be method"); 847 if (m->signature() == signature && m->name() == name) { 848 return index; 849 } 850 } 851 return -1; 852} 853#endif 854 855methodOop instanceKlass::find_method(symbolOop name, symbolOop signature) const { 856 return instanceKlass::find_method(methods(), name, signature); 857} 858 859methodOop instanceKlass::find_method(objArrayOop methods, symbolOop name, symbolOop signature) { 860 int len = methods->length(); 861 // methods are sorted, so do binary search 862 int l = 0; 863 int h = len - 1; 864 while (l <= h) { 865 int mid = (l + h) >> 1; 866 methodOop m = (methodOop)methods->obj_at(mid); 867 assert(m->is_method(), "must be method"); 868 int res = m->name()->fast_compare(name); 869 if (res == 0) { 870 // found matching name; do linear search to find matching signature 871 // first, quick check for common case 872 if (m->signature() == signature) return m; 873 // search downwards through overloaded methods 874 int i; 875 for (i = mid - 1; i >= l; i--) { 876 methodOop m = (methodOop)methods->obj_at(i); 877 assert(m->is_method(), "must be method"); 878 if (m->name() != name) break; 879 if (m->signature() == signature) return m; 880 } 881 // search upwards 882 for (i = mid + 1; i <= h; i++) { 883 methodOop m = (methodOop)methods->obj_at(i); 884 assert(m->is_method(), "must be method"); 885 if (m->name() != name) break; 886 if (m->signature() == signature) return m; 887 } 888 // not found 889#ifdef ASSERT 890 int index = linear_search(methods, name, signature); 891 if (index != -1) fatal1("binary search bug: should have found entry %d", index); 892#endif 893 return NULL; 894 } else if (res < 0) { 895 l = mid + 1; 896 } else { 897 h = mid - 1; 898 } 899 } 900#ifdef ASSERT 901 int index = linear_search(methods, name, signature); 902 if (index != -1) fatal1("binary search bug: should have found entry %d", index); 903#endif 904 return NULL; 905} 906 907methodOop instanceKlass::uncached_lookup_method(symbolOop name, symbolOop signature) const { 908 klassOop klass = as_klassOop(); 909 while (klass != NULL) { 910 methodOop method = instanceKlass::cast(klass)->find_method(name, signature); 911 if (method != NULL) return method; 912 klass = instanceKlass::cast(klass)->super(); 913 } 914 return NULL; 915} 916 917// lookup a method in all the interfaces that this class implements 918methodOop instanceKlass::lookup_method_in_all_interfaces(symbolOop name, 919 symbolOop signature) const { 920 objArrayOop all_ifs = instanceKlass::cast(as_klassOop())->transitive_interfaces(); 921 int num_ifs = all_ifs->length(); 922 instanceKlass *ik = NULL; 923 for (int i = 0; i < num_ifs; i++) { 924 ik = instanceKlass::cast(klassOop(all_ifs->obj_at(i))); 925 methodOop m = ik->lookup_method(name, signature); 926 if (m != NULL) { 927 return m; 928 } 929 } 930 return NULL; 931} 932 933/* jni_id_for_impl for jfieldIds only */ 934JNIid* instanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) { 935 MutexLocker ml(JfieldIdCreation_lock); 936 // Retry lookup after we got the lock 937 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset); 938 if (probe == NULL) { 939 // Slow case, allocate new static field identifier 940 probe = new JNIid(this_oop->as_klassOop(), offset, this_oop->jni_ids()); 941 this_oop->set_jni_ids(probe); 942 } 943 return probe; 944} 945 946 947/* jni_id_for for jfieldIds only */ 948JNIid* instanceKlass::jni_id_for(int offset) { 949 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 950 if (probe == NULL) { 951 probe = jni_id_for_impl(this->as_klassOop(), offset); 952 } 953 return probe; 954} 955 956 957// Lookup or create a jmethodID. 958// This code can be called by the VM thread. For this reason it is critical that 959// there are no blocking operations (safepoints) while the lock is held -- or a 960// deadlock can occur. 961jmethodID instanceKlass::jmethod_id_for_impl(instanceKlassHandle ik_h, methodHandle method_h) { 962 size_t idnum = (size_t)method_h->method_idnum(); 963 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire(); 964 size_t length = 0; 965 jmethodID id = NULL; 966 // array length stored in first element, other elements offset by one 967 if (jmeths == NULL || // If there is no jmethodID array, 968 (length = (size_t)jmeths[0]) <= idnum || // or if it is too short, 969 (id = jmeths[idnum+1]) == NULL) { // or if this jmethodID isn't allocated 970 971 // Do all the safepointing things (allocations) before grabbing the lock. 972 // These allocations will have to be freed if they are unused. 973 974 // Allocate a new array of methods. 975 jmethodID* new_jmeths = NULL; 976 if (length <= idnum) { 977 // A new array will be needed (unless some other thread beats us to it) 978 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count()); 979 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1); 980 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID)); 981 new_jmeths[0] =(jmethodID)size; // array size held in the first element 982 } 983 984 // Allocate a new method ID. 985 jmethodID new_id = NULL; 986 if (method_h->is_old() && !method_h->is_obsolete()) { 987 // The method passed in is old (but not obsolete), we need to use the current version 988 methodOop current_method = ik_h->method_with_idnum((int)idnum); 989 assert(current_method != NULL, "old and but not obsolete, so should exist"); 990 methodHandle current_method_h(current_method == NULL? method_h() : current_method); 991 new_id = JNIHandles::make_jmethod_id(current_method_h); 992 } else { 993 // It is the current version of the method or an obsolete method, 994 // use the version passed in 995 new_id = JNIHandles::make_jmethod_id(method_h); 996 } 997 998 if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) { 999 // No need and unsafe to lock the JmethodIdCreation_lock at safepoint. 1000 id = get_jmethod_id(ik_h, idnum, new_id, new_jmeths); 1001 } else { 1002 MutexLocker ml(JmethodIdCreation_lock); 1003 id = get_jmethod_id(ik_h, idnum, new_id, new_jmeths); 1004 } 1005 } 1006 return id; 1007} 1008 1009 1010jmethodID instanceKlass::get_jmethod_id(instanceKlassHandle ik_h, size_t idnum, 1011 jmethodID new_id, jmethodID* new_jmeths) { 1012 // Retry lookup after we got the lock or ensured we are at safepoint 1013 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire(); 1014 jmethodID id = NULL; 1015 jmethodID to_dealloc_id = NULL; 1016 jmethodID* to_dealloc_jmeths = NULL; 1017 size_t length; 1018 1019 if (jmeths == NULL || (length = (size_t)jmeths[0]) <= idnum) { 1020 if (jmeths != NULL) { 1021 // We have grown the array: copy the existing entries, and delete the old array 1022 for (size_t index = 0; index < length; index++) { 1023 new_jmeths[index+1] = jmeths[index+1]; 1024 } 1025 to_dealloc_jmeths = jmeths; // using the new jmeths, deallocate the old one 1026 } 1027 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths); 1028 } else { 1029 id = jmeths[idnum+1]; 1030 to_dealloc_jmeths = new_jmeths; // using the old jmeths, deallocate the new one 1031 } 1032 if (id == NULL) { 1033 id = new_id; 1034 jmeths[idnum+1] = id; // install the new method ID 1035 } else { 1036 to_dealloc_id = new_id; // the new id wasn't used, mark it for deallocation 1037 } 1038 1039 // Free up unneeded or no longer needed resources 1040 FreeHeap(to_dealloc_jmeths); 1041 if (to_dealloc_id != NULL) { 1042 JNIHandles::destroy_jmethod_id(to_dealloc_id); 1043 } 1044 return id; 1045} 1046 1047 1048// Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles 1049jmethodID instanceKlass::jmethod_id_or_null(methodOop method) { 1050 size_t idnum = (size_t)method->method_idnum(); 1051 jmethodID* jmeths = methods_jmethod_ids_acquire(); 1052 size_t length; // length assigned as debugging crumb 1053 jmethodID id = NULL; 1054 if (jmeths != NULL && // If there is a jmethodID array, 1055 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough, 1056 id = jmeths[idnum+1]; // Look up the id (may be NULL) 1057 } 1058 return id; 1059} 1060 1061 1062// Cache an itable index 1063void instanceKlass::set_cached_itable_index(size_t idnum, int index) { 1064 int* indices = methods_cached_itable_indices_acquire(); 1065 if (indices == NULL || // If there is no index array, 1066 ((size_t)indices[0]) <= idnum) { // or if it is too short 1067 // Lock before we allocate the array so we don't leak 1068 MutexLocker ml(JNICachedItableIndex_lock); 1069 // Retry lookup after we got the lock 1070 indices = methods_cached_itable_indices_acquire(); 1071 size_t length = 0; 1072 // array length stored in first element, other elements offset by one 1073 if (indices == NULL || (length = (size_t)indices[0]) <= idnum) { 1074 size_t size = MAX2(idnum+1, (size_t)idnum_allocated_count()); 1075 int* new_indices = NEW_C_HEAP_ARRAY(int, size+1); 1076 // Copy the existing entries, if any 1077 size_t i; 1078 for (i = 0; i < length; i++) { 1079 new_indices[i+1] = indices[i+1]; 1080 } 1081 // Set all the rest to -1 1082 for (i = length; i < size; i++) { 1083 new_indices[i+1] = -1; 1084 } 1085 if (indices != NULL) { 1086 FreeHeap(indices); // delete any old indices 1087 } 1088 release_set_methods_cached_itable_indices(indices = new_indices); 1089 } 1090 } else { 1091 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 1092 } 1093 // This is a cache, if there is a race to set it, it doesn't matter 1094 indices[idnum+1] = index; 1095} 1096 1097 1098// Retrieve a cached itable index 1099int instanceKlass::cached_itable_index(size_t idnum) { 1100 int* indices = methods_cached_itable_indices_acquire(); 1101 if (indices != NULL && ((size_t)indices[0]) > idnum) { 1102 // indices exist and are long enough, retrieve possible cached 1103 return indices[idnum+1]; 1104 } 1105 return -1; 1106} 1107 1108 1109// 1110// nmethodBucket is used to record dependent nmethods for 1111// deoptimization. nmethod dependencies are actually <klass, method> 1112// pairs but we really only care about the klass part for purposes of 1113// finding nmethods which might need to be deoptimized. Instead of 1114// recording the method, a count of how many times a particular nmethod 1115// was recorded is kept. This ensures that any recording errors are 1116// noticed since an nmethod should be removed as many times are it's 1117// added. 1118// 1119class nmethodBucket { 1120 private: 1121 nmethod* _nmethod; 1122 int _count; 1123 nmethodBucket* _next; 1124 1125 public: 1126 nmethodBucket(nmethod* nmethod, nmethodBucket* next) { 1127 _nmethod = nmethod; 1128 _next = next; 1129 _count = 1; 1130 } 1131 int count() { return _count; } 1132 int increment() { _count += 1; return _count; } 1133 int decrement() { _count -= 1; assert(_count >= 0, "don't underflow"); return _count; } 1134 nmethodBucket* next() { return _next; } 1135 void set_next(nmethodBucket* b) { _next = b; } 1136 nmethod* get_nmethod() { return _nmethod; } 1137}; 1138 1139 1140// 1141// Walk the list of dependent nmethods searching for nmethods which 1142// are dependent on the klassOop that was passed in and mark them for 1143// deoptimization. Returns the number of nmethods found. 1144// 1145int instanceKlass::mark_dependent_nmethods(DepChange& changes) { 1146 assert_locked_or_safepoint(CodeCache_lock); 1147 int found = 0; 1148 nmethodBucket* b = _dependencies; 1149 while (b != NULL) { 1150 nmethod* nm = b->get_nmethod(); 1151 // since dependencies aren't removed until an nmethod becomes a zombie, 1152 // the dependency list may contain nmethods which aren't alive. 1153 if (nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) { 1154 if (TraceDependencies) { 1155 ResourceMark rm; 1156 tty->print_cr("Marked for deoptimization"); 1157 tty->print_cr(" context = %s", this->external_name()); 1158 changes.print(); 1159 nm->print(); 1160 nm->print_dependencies(); 1161 } 1162 nm->mark_for_deoptimization(); 1163 found++; 1164 } 1165 b = b->next(); 1166 } 1167 return found; 1168} 1169 1170 1171// 1172// Add an nmethodBucket to the list of dependencies for this nmethod. 1173// It's possible that an nmethod has multiple dependencies on this klass 1174// so a count is kept for each bucket to guarantee that creation and 1175// deletion of dependencies is consistent. 1176// 1177void instanceKlass::add_dependent_nmethod(nmethod* nm) { 1178 assert_locked_or_safepoint(CodeCache_lock); 1179 nmethodBucket* b = _dependencies; 1180 nmethodBucket* last = NULL; 1181 while (b != NULL) { 1182 if (nm == b->get_nmethod()) { 1183 b->increment(); 1184 return; 1185 } 1186 b = b->next(); 1187 } 1188 _dependencies = new nmethodBucket(nm, _dependencies); 1189} 1190 1191 1192// 1193// Decrement count of the nmethod in the dependency list and remove 1194// the bucket competely when the count goes to 0. This method must 1195// find a corresponding bucket otherwise there's a bug in the 1196// recording of dependecies. 1197// 1198void instanceKlass::remove_dependent_nmethod(nmethod* nm) { 1199 assert_locked_or_safepoint(CodeCache_lock); 1200 nmethodBucket* b = _dependencies; 1201 nmethodBucket* last = NULL; 1202 while (b != NULL) { 1203 if (nm == b->get_nmethod()) { 1204 if (b->decrement() == 0) { 1205 if (last == NULL) { 1206 _dependencies = b->next(); 1207 } else { 1208 last->set_next(b->next()); 1209 } 1210 delete b; 1211 } 1212 return; 1213 } 1214 last = b; 1215 b = b->next(); 1216 } 1217#ifdef ASSERT 1218 tty->print_cr("### %s can't find dependent nmethod:", this->external_name()); 1219 nm->print(); 1220#endif // ASSERT 1221 ShouldNotReachHere(); 1222} 1223 1224 1225#ifndef PRODUCT 1226void instanceKlass::print_dependent_nmethods(bool verbose) { 1227 nmethodBucket* b = _dependencies; 1228 int idx = 0; 1229 while (b != NULL) { 1230 nmethod* nm = b->get_nmethod(); 1231 tty->print("[%d] count=%d { ", idx++, b->count()); 1232 if (!verbose) { 1233 nm->print_on(tty, "nmethod"); 1234 tty->print_cr(" } "); 1235 } else { 1236 nm->print(); 1237 nm->print_dependencies(); 1238 tty->print_cr("--- } "); 1239 } 1240 b = b->next(); 1241 } 1242} 1243 1244 1245bool instanceKlass::is_dependent_nmethod(nmethod* nm) { 1246 nmethodBucket* b = _dependencies; 1247 while (b != NULL) { 1248 if (nm == b->get_nmethod()) { 1249 return true; 1250 } 1251 b = b->next(); 1252 } 1253 return false; 1254} 1255#endif //PRODUCT 1256 1257 1258#ifdef ASSERT 1259template <class T> void assert_is_in(T *p) { 1260 T heap_oop = oopDesc::load_heap_oop(p); 1261 if (!oopDesc::is_null(heap_oop)) { 1262 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1263 assert(Universe::heap()->is_in(o), "should be in heap"); 1264 } 1265} 1266template <class T> void assert_is_in_closed_subset(T *p) { 1267 T heap_oop = oopDesc::load_heap_oop(p); 1268 if (!oopDesc::is_null(heap_oop)) { 1269 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1270 assert(Universe::heap()->is_in_closed_subset(o), "should be in closed"); 1271 } 1272} 1273template <class T> void assert_is_in_reserved(T *p) { 1274 T heap_oop = oopDesc::load_heap_oop(p); 1275 if (!oopDesc::is_null(heap_oop)) { 1276 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1277 assert(Universe::heap()->is_in_reserved(o), "should be in reserved"); 1278 } 1279} 1280template <class T> void assert_nothing(T *p) {} 1281 1282#else 1283template <class T> void assert_is_in(T *p) {} 1284template <class T> void assert_is_in_closed_subset(T *p) {} 1285template <class T> void assert_is_in_reserved(T *p) {} 1286template <class T> void assert_nothing(T *p) {} 1287#endif // ASSERT 1288 1289// 1290// Macros that iterate over areas of oops which are specialized on type of 1291// oop pointer either narrow or wide, depending on UseCompressedOops 1292// 1293// Parameters are: 1294// T - type of oop to point to (either oop or narrowOop) 1295// start_p - starting pointer for region to iterate over 1296// count - number of oops or narrowOops to iterate over 1297// do_oop - action to perform on each oop (it's arbitrary C code which 1298// makes it more efficient to put in a macro rather than making 1299// it a template function) 1300// assert_fn - assert function which is template function because performance 1301// doesn't matter when enabled. 1302#define InstanceKlass_SPECIALIZED_OOP_ITERATE( \ 1303 T, start_p, count, do_oop, \ 1304 assert_fn) \ 1305{ \ 1306 T* p = (T*)(start_p); \ 1307 T* const end = p + (count); \ 1308 while (p < end) { \ 1309 (assert_fn)(p); \ 1310 do_oop; \ 1311 ++p; \ 1312 } \ 1313} 1314 1315#define InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE( \ 1316 T, start_p, count, do_oop, \ 1317 assert_fn) \ 1318{ \ 1319 T* const start = (T*)(start_p); \ 1320 T* p = start + (count); \ 1321 while (start < p) { \ 1322 --p; \ 1323 (assert_fn)(p); \ 1324 do_oop; \ 1325 } \ 1326} 1327 1328#define InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE( \ 1329 T, start_p, count, low, high, \ 1330 do_oop, assert_fn) \ 1331{ \ 1332 T* const l = (T*)(low); \ 1333 T* const h = (T*)(high); \ 1334 assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 && \ 1335 mask_bits((intptr_t)h, sizeof(T)-1) == 0, \ 1336 "bounded region must be properly aligned"); \ 1337 T* p = (T*)(start_p); \ 1338 T* end = p + (count); \ 1339 if (p < l) p = l; \ 1340 if (end > h) end = h; \ 1341 while (p < end) { \ 1342 (assert_fn)(p); \ 1343 do_oop; \ 1344 ++p; \ 1345 } \ 1346} 1347 1348 1349// The following macros call specialized macros, passing either oop or 1350// narrowOop as the specialization type. These test the UseCompressedOops 1351// flag. 1352#define InstanceKlass_OOP_ITERATE(start_p, count, \ 1353 do_oop, assert_fn) \ 1354{ \ 1355 if (UseCompressedOops) { \ 1356 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \ 1357 start_p, count, \ 1358 do_oop, assert_fn) \ 1359 } else { \ 1360 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \ 1361 start_p, count, \ 1362 do_oop, assert_fn) \ 1363 } \ 1364} 1365 1366#define InstanceKlass_BOUNDED_OOP_ITERATE(start_p, count, low, high, \ 1367 do_oop, assert_fn) \ 1368{ \ 1369 if (UseCompressedOops) { \ 1370 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \ 1371 start_p, count, \ 1372 low, high, \ 1373 do_oop, assert_fn) \ 1374 } else { \ 1375 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \ 1376 start_p, count, \ 1377 low, high, \ 1378 do_oop, assert_fn) \ 1379 } \ 1380} 1381 1382#define InstanceKlass_OOP_MAP_ITERATE(obj, do_oop, assert_fn) \ 1383{ \ 1384 /* Compute oopmap block range. The common case \ 1385 is nonstatic_oop_map_size == 1. */ \ 1386 OopMapBlock* map = start_of_nonstatic_oop_maps(); \ 1387 OopMapBlock* const end_map = map + nonstatic_oop_map_size(); \ 1388 if (UseCompressedOops) { \ 1389 while (map < end_map) { \ 1390 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \ 1391 obj->obj_field_addr<narrowOop>(map->offset()), map->length(), \ 1392 do_oop, assert_fn) \ 1393 ++map; \ 1394 } \ 1395 } else { \ 1396 while (map < end_map) { \ 1397 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \ 1398 obj->obj_field_addr<oop>(map->offset()), map->length(), \ 1399 do_oop, assert_fn) \ 1400 ++map; \ 1401 } \ 1402 } \ 1403} 1404 1405#define InstanceKlass_OOP_MAP_REVERSE_ITERATE(obj, do_oop, assert_fn) \ 1406{ \ 1407 OopMapBlock* const start_map = start_of_nonstatic_oop_maps(); \ 1408 OopMapBlock* map = start_map + nonstatic_oop_map_size(); \ 1409 if (UseCompressedOops) { \ 1410 while (start_map < map) { \ 1411 --map; \ 1412 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(narrowOop, \ 1413 obj->obj_field_addr<narrowOop>(map->offset()), map->length(), \ 1414 do_oop, assert_fn) \ 1415 } \ 1416 } else { \ 1417 while (start_map < map) { \ 1418 --map; \ 1419 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(oop, \ 1420 obj->obj_field_addr<oop>(map->offset()), map->length(), \ 1421 do_oop, assert_fn) \ 1422 } \ 1423 } \ 1424} 1425 1426#define InstanceKlass_BOUNDED_OOP_MAP_ITERATE(obj, low, high, do_oop, \ 1427 assert_fn) \ 1428{ \ 1429 /* Compute oopmap block range. The common case is \ 1430 nonstatic_oop_map_size == 1, so we accept the \ 1431 usually non-existent extra overhead of examining \ 1432 all the maps. */ \ 1433 OopMapBlock* map = start_of_nonstatic_oop_maps(); \ 1434 OopMapBlock* const end_map = map + nonstatic_oop_map_size(); \ 1435 if (UseCompressedOops) { \ 1436 while (map < end_map) { \ 1437 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \ 1438 obj->obj_field_addr<narrowOop>(map->offset()), map->length(), \ 1439 low, high, \ 1440 do_oop, assert_fn) \ 1441 ++map; \ 1442 } \ 1443 } else { \ 1444 while (map < end_map) { \ 1445 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \ 1446 obj->obj_field_addr<oop>(map->offset()), map->length(), \ 1447 low, high, \ 1448 do_oop, assert_fn) \ 1449 ++map; \ 1450 } \ 1451 } \ 1452} 1453 1454void instanceKlass::follow_static_fields() { 1455 InstanceKlass_OOP_ITERATE( \ 1456 start_of_static_fields(), static_oop_field_size(), \ 1457 MarkSweep::mark_and_push(p), \ 1458 assert_is_in_closed_subset) 1459} 1460 1461#ifndef SERIALGC 1462void instanceKlass::follow_static_fields(ParCompactionManager* cm) { 1463 InstanceKlass_OOP_ITERATE( \ 1464 start_of_static_fields(), static_oop_field_size(), \ 1465 PSParallelCompact::mark_and_push(cm, p), \ 1466 assert_is_in) 1467} 1468#endif // SERIALGC 1469 1470void instanceKlass::adjust_static_fields() { 1471 InstanceKlass_OOP_ITERATE( \ 1472 start_of_static_fields(), static_oop_field_size(), \ 1473 MarkSweep::adjust_pointer(p), \ 1474 assert_nothing) 1475} 1476 1477#ifndef SERIALGC 1478void instanceKlass::update_static_fields() { 1479 InstanceKlass_OOP_ITERATE( \ 1480 start_of_static_fields(), static_oop_field_size(), \ 1481 PSParallelCompact::adjust_pointer(p), \ 1482 assert_nothing) 1483} 1484 1485void instanceKlass::update_static_fields(HeapWord* beg_addr, HeapWord* end_addr) { 1486 InstanceKlass_BOUNDED_OOP_ITERATE( \ 1487 start_of_static_fields(), static_oop_field_size(), \ 1488 beg_addr, end_addr, \ 1489 PSParallelCompact::adjust_pointer(p), \ 1490 assert_nothing ) 1491} 1492#endif // SERIALGC 1493 1494void instanceKlass::oop_follow_contents(oop obj) { 1495 assert(obj != NULL, "can't follow the content of NULL object"); 1496 obj->follow_header(); 1497 InstanceKlass_OOP_MAP_ITERATE( \ 1498 obj, \ 1499 MarkSweep::mark_and_push(p), \ 1500 assert_is_in_closed_subset) 1501} 1502 1503#ifndef SERIALGC 1504void instanceKlass::oop_follow_contents(ParCompactionManager* cm, 1505 oop obj) { 1506 assert(obj != NULL, "can't follow the content of NULL object"); 1507 obj->follow_header(cm); 1508 InstanceKlass_OOP_MAP_ITERATE( \ 1509 obj, \ 1510 PSParallelCompact::mark_and_push(cm, p), \ 1511 assert_is_in) 1512} 1513#endif // SERIALGC 1514 1515// closure's do_header() method dicates whether the given closure should be 1516// applied to the klass ptr in the object header. 1517 1518#define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ 1519 \ 1520int instanceKlass::oop_oop_iterate##nv_suffix(oop obj, \ 1521 OopClosureType* closure) {\ 1522 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 1523 /* header */ \ 1524 if (closure->do_header()) { \ 1525 obj->oop_iterate_header(closure); \ 1526 } \ 1527 InstanceKlass_OOP_MAP_ITERATE( \ 1528 obj, \ 1529 SpecializationStats:: \ 1530 record_do_oop_call##nv_suffix(SpecializationStats::ik); \ 1531 (closure)->do_oop##nv_suffix(p), \ 1532 assert_is_in_closed_subset) \ 1533 return size_helper(); \ 1534} 1535 1536#define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \ 1537 \ 1538int instanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \ 1539 OopClosureType* closure, \ 1540 MemRegion mr) { \ 1541 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 1542 if (closure->do_header()) { \ 1543 obj->oop_iterate_header(closure, mr); \ 1544 } \ 1545 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \ 1546 obj, mr.start(), mr.end(), \ 1547 (closure)->do_oop##nv_suffix(p), \ 1548 assert_is_in_closed_subset) \ 1549 return size_helper(); \ 1550} 1551 1552ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN) 1553ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceKlass_OOP_OOP_ITERATE_DEFN) 1554ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN_m) 1555ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceKlass_OOP_OOP_ITERATE_DEFN_m) 1556 1557void instanceKlass::iterate_static_fields(OopClosure* closure) { 1558 InstanceKlass_OOP_ITERATE( \ 1559 start_of_static_fields(), static_oop_field_size(), \ 1560 closure->do_oop(p), \ 1561 assert_is_in_reserved) 1562} 1563 1564void instanceKlass::iterate_static_fields(OopClosure* closure, 1565 MemRegion mr) { 1566 InstanceKlass_BOUNDED_OOP_ITERATE( \ 1567 start_of_static_fields(), static_oop_field_size(), \ 1568 mr.start(), mr.end(), \ 1569 (closure)->do_oop_v(p), \ 1570 assert_is_in_closed_subset) 1571} 1572 1573int instanceKlass::oop_adjust_pointers(oop obj) { 1574 int size = size_helper(); 1575 InstanceKlass_OOP_MAP_ITERATE( \ 1576 obj, \ 1577 MarkSweep::adjust_pointer(p), \ 1578 assert_is_in) 1579 obj->adjust_header(); 1580 return size; 1581} 1582 1583#ifndef SERIALGC 1584void instanceKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) { 1585 assert(!pm->depth_first(), "invariant"); 1586 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 1587 obj, \ 1588 if (PSScavenge::should_scavenge(p)) { \ 1589 pm->claim_or_forward_breadth(p); \ 1590 }, \ 1591 assert_nothing ) 1592} 1593 1594void instanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { 1595 assert(pm->depth_first(), "invariant"); 1596 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 1597 obj, \ 1598 if (PSScavenge::should_scavenge(p)) { \ 1599 pm->claim_or_forward_depth(p); \ 1600 }, \ 1601 assert_nothing ) 1602} 1603 1604int instanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { 1605 InstanceKlass_OOP_MAP_ITERATE( \ 1606 obj, \ 1607 PSParallelCompact::adjust_pointer(p), \ 1608 assert_nothing) 1609 return size_helper(); 1610} 1611 1612int instanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj, 1613 HeapWord* beg_addr, HeapWord* end_addr) { 1614 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \ 1615 obj, beg_addr, end_addr, \ 1616 PSParallelCompact::adjust_pointer(p), \ 1617 assert_nothing) 1618 return size_helper(); 1619} 1620 1621void instanceKlass::copy_static_fields(PSPromotionManager* pm) { 1622 assert(!pm->depth_first(), "invariant"); 1623 InstanceKlass_OOP_ITERATE( \ 1624 start_of_static_fields(), static_oop_field_size(), \ 1625 if (PSScavenge::should_scavenge(p)) { \ 1626 pm->claim_or_forward_breadth(p); \ 1627 }, \ 1628 assert_nothing ) 1629} 1630 1631void instanceKlass::push_static_fields(PSPromotionManager* pm) { 1632 assert(pm->depth_first(), "invariant"); 1633 InstanceKlass_OOP_ITERATE( \ 1634 start_of_static_fields(), static_oop_field_size(), \ 1635 if (PSScavenge::should_scavenge(p)) { \ 1636 pm->claim_or_forward_depth(p); \ 1637 }, \ 1638 assert_nothing ) 1639} 1640 1641void instanceKlass::copy_static_fields(ParCompactionManager* cm) { 1642 InstanceKlass_OOP_ITERATE( \ 1643 start_of_static_fields(), static_oop_field_size(), \ 1644 PSParallelCompact::adjust_pointer(p), \ 1645 assert_is_in) 1646} 1647#endif // SERIALGC 1648 1649// This klass is alive but the implementor link is not followed/updated. 1650// Subklass and sibling links are handled by Klass::follow_weak_klass_links 1651 1652void instanceKlass::follow_weak_klass_links( 1653 BoolObjectClosure* is_alive, OopClosure* keep_alive) { 1654 assert(is_alive->do_object_b(as_klassOop()), "this oop should be live"); 1655 if (ClassUnloading) { 1656 for (int i = 0; i < implementors_limit; i++) { 1657 klassOop impl = _implementors[i]; 1658 if (impl == NULL) break; // no more in the list 1659 if (!is_alive->do_object_b(impl)) { 1660 // remove this guy from the list by overwriting him with the tail 1661 int lasti = --_nof_implementors; 1662 assert(lasti >= i && lasti < implementors_limit, "just checking"); 1663 _implementors[i] = _implementors[lasti]; 1664 _implementors[lasti] = NULL; 1665 --i; // rerun the loop at this index 1666 } 1667 } 1668 } else { 1669 for (int i = 0; i < implementors_limit; i++) { 1670 keep_alive->do_oop(&adr_implementors()[i]); 1671 } 1672 } 1673 Klass::follow_weak_klass_links(is_alive, keep_alive); 1674} 1675 1676void instanceKlass::remove_unshareable_info() { 1677 Klass::remove_unshareable_info(); 1678 init_implementor(); 1679} 1680 1681static void clear_all_breakpoints(methodOop m) { 1682 m->clear_all_breakpoints(); 1683} 1684 1685void instanceKlass::release_C_heap_structures() { 1686 // Deallocate oop map cache 1687 if (_oop_map_cache != NULL) { 1688 delete _oop_map_cache; 1689 _oop_map_cache = NULL; 1690 } 1691 1692 // Deallocate JNI identifiers for jfieldIDs 1693 JNIid::deallocate(jni_ids()); 1694 set_jni_ids(NULL); 1695 1696 jmethodID* jmeths = methods_jmethod_ids_acquire(); 1697 if (jmeths != (jmethodID*)NULL) { 1698 release_set_methods_jmethod_ids(NULL); 1699 FreeHeap(jmeths); 1700 } 1701 1702 int* indices = methods_cached_itable_indices_acquire(); 1703 if (indices != (int*)NULL) { 1704 release_set_methods_cached_itable_indices(NULL); 1705 FreeHeap(indices); 1706 } 1707 1708 // release dependencies 1709 nmethodBucket* b = _dependencies; 1710 _dependencies = NULL; 1711 while (b != NULL) { 1712 nmethodBucket* next = b->next(); 1713 delete b; 1714 b = next; 1715 } 1716 1717 // Deallocate breakpoint records 1718 if (breakpoints() != 0x0) { 1719 methods_do(clear_all_breakpoints); 1720 assert(breakpoints() == 0x0, "should have cleared breakpoints"); 1721 } 1722 1723 // deallocate information about previous versions 1724 if (_previous_versions != NULL) { 1725 for (int i = _previous_versions->length() - 1; i >= 0; i--) { 1726 PreviousVersionNode * pv_node = _previous_versions->at(i); 1727 delete pv_node; 1728 } 1729 delete _previous_versions; 1730 _previous_versions = NULL; 1731 } 1732 1733 // deallocate the cached class file 1734 if (_cached_class_file_bytes != NULL) { 1735 os::free(_cached_class_file_bytes); 1736 _cached_class_file_bytes = NULL; 1737 _cached_class_file_len = 0; 1738 } 1739} 1740 1741char* instanceKlass::signature_name() const { 1742 const char* src = (const char*) (name()->as_C_string()); 1743 const int src_length = (int)strlen(src); 1744 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3); 1745 int src_index = 0; 1746 int dest_index = 0; 1747 dest[dest_index++] = 'L'; 1748 while (src_index < src_length) { 1749 dest[dest_index++] = src[src_index++]; 1750 } 1751 dest[dest_index++] = ';'; 1752 dest[dest_index] = '\0'; 1753 return dest; 1754} 1755 1756// different verisons of is_same_class_package 1757bool instanceKlass::is_same_class_package(klassOop class2) { 1758 klassOop class1 = as_klassOop(); 1759 oop classloader1 = instanceKlass::cast(class1)->class_loader(); 1760 symbolOop classname1 = Klass::cast(class1)->name(); 1761 1762 if (Klass::cast(class2)->oop_is_objArray()) { 1763 class2 = objArrayKlass::cast(class2)->bottom_klass(); 1764 } 1765 oop classloader2; 1766 if (Klass::cast(class2)->oop_is_instance()) { 1767 classloader2 = instanceKlass::cast(class2)->class_loader(); 1768 } else { 1769 assert(Klass::cast(class2)->oop_is_typeArray(), "should be type array"); 1770 classloader2 = NULL; 1771 } 1772 symbolOop classname2 = Klass::cast(class2)->name(); 1773 1774 return instanceKlass::is_same_class_package(classloader1, classname1, 1775 classloader2, classname2); 1776} 1777 1778bool instanceKlass::is_same_class_package(oop classloader2, symbolOop classname2) { 1779 klassOop class1 = as_klassOop(); 1780 oop classloader1 = instanceKlass::cast(class1)->class_loader(); 1781 symbolOop classname1 = Klass::cast(class1)->name(); 1782 1783 return instanceKlass::is_same_class_package(classloader1, classname1, 1784 classloader2, classname2); 1785} 1786 1787// return true if two classes are in the same package, classloader 1788// and classname information is enough to determine a class's package 1789bool instanceKlass::is_same_class_package(oop class_loader1, symbolOop class_name1, 1790 oop class_loader2, symbolOop class_name2) { 1791 if (class_loader1 != class_loader2) { 1792 return false; 1793 } else { 1794 ResourceMark rm; 1795 1796 // The symbolOop's are in UTF8 encoding. Since we only need to check explicitly 1797 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding. 1798 // Otherwise, we just compare jbyte values between the strings. 1799 jbyte *name1 = class_name1->base(); 1800 jbyte *name2 = class_name2->base(); 1801 1802 jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/'); 1803 jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/'); 1804 1805 if ((last_slash1 == NULL) || (last_slash2 == NULL)) { 1806 // One of the two doesn't have a package. Only return true 1807 // if the other one also doesn't have a package. 1808 return last_slash1 == last_slash2; 1809 } else { 1810 // Skip over '['s 1811 if (*name1 == '[') { 1812 do { 1813 name1++; 1814 } while (*name1 == '['); 1815 if (*name1 != 'L') { 1816 // Something is terribly wrong. Shouldn't be here. 1817 return false; 1818 } 1819 } 1820 if (*name2 == '[') { 1821 do { 1822 name2++; 1823 } while (*name2 == '['); 1824 if (*name2 != 'L') { 1825 // Something is terribly wrong. Shouldn't be here. 1826 return false; 1827 } 1828 } 1829 1830 // Check that package part is identical 1831 int length1 = last_slash1 - name1; 1832 int length2 = last_slash2 - name2; 1833 1834 return UTF8::equal(name1, length1, name2, length2); 1835 } 1836 } 1837} 1838 1839 1840jint instanceKlass::compute_modifier_flags(TRAPS) const { 1841 klassOop k = as_klassOop(); 1842 jint access = access_flags().as_int(); 1843 1844 // But check if it happens to be member class. 1845 typeArrayOop inner_class_list = inner_classes(); 1846 int length = (inner_class_list == NULL) ? 0 : inner_class_list->length(); 1847 assert (length % instanceKlass::inner_class_next_offset == 0, "just checking"); 1848 if (length > 0) { 1849 typeArrayHandle inner_class_list_h(THREAD, inner_class_list); 1850 instanceKlassHandle ik(THREAD, k); 1851 for (int i = 0; i < length; i += instanceKlass::inner_class_next_offset) { 1852 int ioff = inner_class_list_h->ushort_at( 1853 i + instanceKlass::inner_class_inner_class_info_offset); 1854 1855 // Inner class attribute can be zero, skip it. 1856 // Strange but true: JVM spec. allows null inner class refs. 1857 if (ioff == 0) continue; 1858 1859 // only look at classes that are already loaded 1860 // since we are looking for the flags for our self. 1861 symbolOop inner_name = ik->constants()->klass_name_at(ioff); 1862 if ((ik->name() == inner_name)) { 1863 // This is really a member class. 1864 access = inner_class_list_h->ushort_at(i + instanceKlass::inner_class_access_flags_offset); 1865 break; 1866 } 1867 } 1868 } 1869 // Remember to strip ACC_SUPER bit 1870 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS; 1871} 1872 1873jint instanceKlass::jvmti_class_status() const { 1874 jint result = 0; 1875 1876 if (is_linked()) { 1877 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED; 1878 } 1879 1880 if (is_initialized()) { 1881 assert(is_linked(), "Class status is not consistent"); 1882 result |= JVMTI_CLASS_STATUS_INITIALIZED; 1883 } 1884 if (is_in_error_state()) { 1885 result |= JVMTI_CLASS_STATUS_ERROR; 1886 } 1887 return result; 1888} 1889 1890methodOop instanceKlass::method_at_itable(klassOop holder, int index, TRAPS) { 1891 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable(); 1892 int method_table_offset_in_words = ioe->offset()/wordSize; 1893 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words()) 1894 / itableOffsetEntry::size(); 1895 1896 for (int cnt = 0 ; ; cnt ++, ioe ++) { 1897 // If the interface isn't implemented by the reciever class, 1898 // the VM should throw IncompatibleClassChangeError. 1899 if (cnt >= nof_interfaces) { 1900 THROW_OOP_0(vmSymbols::java_lang_IncompatibleClassChangeError()); 1901 } 1902 1903 klassOop ik = ioe->interface_klass(); 1904 if (ik == holder) break; 1905 } 1906 1907 itableMethodEntry* ime = ioe->first_method_entry(as_klassOop()); 1908 methodOop m = ime[index].method(); 1909 if (m == NULL) { 1910 THROW_OOP_0(vmSymbols::java_lang_AbstractMethodError()); 1911 } 1912 return m; 1913} 1914 1915// On-stack replacement stuff 1916void instanceKlass::add_osr_nmethod(nmethod* n) { 1917 // only one compilation can be active 1918 NEEDS_CLEANUP 1919 // This is a short non-blocking critical region, so the no safepoint check is ok. 1920 OsrList_lock->lock_without_safepoint_check(); 1921 assert(n->is_osr_method(), "wrong kind of nmethod"); 1922 n->set_link(osr_nmethods_head()); 1923 set_osr_nmethods_head(n); 1924 // Remember to unlock again 1925 OsrList_lock->unlock(); 1926} 1927 1928 1929void instanceKlass::remove_osr_nmethod(nmethod* n) { 1930 // This is a short non-blocking critical region, so the no safepoint check is ok. 1931 OsrList_lock->lock_without_safepoint_check(); 1932 assert(n->is_osr_method(), "wrong kind of nmethod"); 1933 nmethod* last = NULL; 1934 nmethod* cur = osr_nmethods_head(); 1935 // Search for match 1936 while(cur != NULL && cur != n) { 1937 last = cur; 1938 cur = cur->link(); 1939 } 1940 if (cur == n) { 1941 if (last == NULL) { 1942 // Remove first element 1943 set_osr_nmethods_head(osr_nmethods_head()->link()); 1944 } else { 1945 last->set_link(cur->link()); 1946 } 1947 } 1948 n->set_link(NULL); 1949 // Remember to unlock again 1950 OsrList_lock->unlock(); 1951} 1952 1953nmethod* instanceKlass::lookup_osr_nmethod(const methodOop m, int bci) const { 1954 // This is a short non-blocking critical region, so the no safepoint check is ok. 1955 OsrList_lock->lock_without_safepoint_check(); 1956 nmethod* osr = osr_nmethods_head(); 1957 while (osr != NULL) { 1958 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain"); 1959 if (osr->method() == m && 1960 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) { 1961 // Found a match - return it. 1962 OsrList_lock->unlock(); 1963 return osr; 1964 } 1965 osr = osr->link(); 1966 } 1967 OsrList_lock->unlock(); 1968 return NULL; 1969} 1970 1971// ----------------------------------------------------------------------------------------------------- 1972#ifndef PRODUCT 1973 1974// Printing 1975 1976void FieldPrinter::do_field(fieldDescriptor* fd) { 1977 if (fd->is_static() == (_obj == NULL)) { 1978 _st->print(" - "); 1979 fd->print_on(_st); 1980 _st->cr(); 1981 } else { 1982 fd->print_on_for(_st, _obj); 1983 _st->cr(); 1984 } 1985} 1986 1987 1988void instanceKlass::oop_print_on(oop obj, outputStream* st) { 1989 Klass::oop_print_on(obj, st); 1990 1991 if (as_klassOop() == SystemDictionary::string_klass()) { 1992 typeArrayOop value = java_lang_String::value(obj); 1993 juint offset = java_lang_String::offset(obj); 1994 juint length = java_lang_String::length(obj); 1995 if (value != NULL && 1996 value->is_typeArray() && 1997 offset <= (juint) value->length() && 1998 offset + length <= (juint) value->length()) { 1999 st->print("string: "); 2000 Handle h_obj(obj); 2001 java_lang_String::print(h_obj, st); 2002 st->cr(); 2003 if (!WizardMode) return; // that is enough 2004 } 2005 } 2006 2007 st->print_cr("fields:"); 2008 FieldPrinter print_nonstatic_field(st, obj); 2009 do_nonstatic_fields(&print_nonstatic_field); 2010 2011 if (as_klassOop() == SystemDictionary::class_klass()) { 2012 klassOop mirrored_klass = java_lang_Class::as_klassOop(obj); 2013 st->print(" - fake entry for mirror: "); 2014 mirrored_klass->print_value_on(st); 2015 st->cr(); 2016 st->print(" - fake entry resolved_constructor: "); 2017 methodOop ctor = java_lang_Class::resolved_constructor(obj); 2018 ctor->print_value_on(st); 2019 klassOop array_klass = java_lang_Class::array_klass(obj); 2020 st->print(" - fake entry for array: "); 2021 array_klass->print_value_on(st); 2022 st->cr(); 2023 st->cr(); 2024 } 2025} 2026 2027void instanceKlass::oop_print_value_on(oop obj, outputStream* st) { 2028 st->print("a "); 2029 name()->print_value_on(st); 2030 obj->print_address_on(st); 2031} 2032 2033#endif // ndef PRODUCT 2034 2035const char* instanceKlass::internal_name() const { 2036 return external_name(); 2037} 2038 2039// Verification 2040 2041class VerifyFieldClosure: public OopClosure { 2042 protected: 2043 template <class T> void do_oop_work(T* p) { 2044 guarantee(Universe::heap()->is_in_closed_subset(p), "should be in heap"); 2045 oop obj = oopDesc::load_decode_heap_oop(p); 2046 if (!obj->is_oop_or_null()) { 2047 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj); 2048 Universe::print(); 2049 guarantee(false, "boom"); 2050 } 2051 } 2052 public: 2053 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); } 2054 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); } 2055}; 2056 2057void instanceKlass::oop_verify_on(oop obj, outputStream* st) { 2058 Klass::oop_verify_on(obj, st); 2059 VerifyFieldClosure blk; 2060 oop_oop_iterate(obj, &blk); 2061} 2062 2063#ifndef PRODUCT 2064 2065void instanceKlass::verify_class_klass_nonstatic_oop_maps(klassOop k) { 2066 // This verification code is disabled. JDK_Version::is_gte_jdk14x_version() 2067 // cannot be called since this function is called before the VM is 2068 // able to determine what JDK version is running with. 2069 // The check below always is false since 1.4. 2070 return; 2071 2072 // This verification code temporarily disabled for the 1.4 2073 // reflection implementation since java.lang.Class now has 2074 // Java-level instance fields. Should rewrite this to handle this 2075 // case. 2076 if (!(JDK_Version::is_gte_jdk14x_version() && UseNewReflection)) { 2077 // Verify that java.lang.Class instances have a fake oop field added. 2078 instanceKlass* ik = instanceKlass::cast(k); 2079 2080 // Check that we have the right class 2081 static bool first_time = true; 2082 guarantee(k == SystemDictionary::class_klass() && first_time, "Invalid verify of maps"); 2083 first_time = false; 2084 const int extra = java_lang_Class::number_of_fake_oop_fields; 2085 guarantee(ik->nonstatic_field_size() == extra, "just checking"); 2086 guarantee(ik->nonstatic_oop_map_size() == 1, "just checking"); 2087 guarantee(ik->size_helper() == align_object_size(instanceOopDesc::header_size() + extra), "just checking"); 2088 2089 // Check that the map is (2,extra) 2090 int offset = java_lang_Class::klass_offset; 2091 2092 OopMapBlock* map = ik->start_of_nonstatic_oop_maps(); 2093 guarantee(map->offset() == offset && map->length() == extra, "just checking"); 2094 } 2095} 2096 2097#endif // ndef PRODUCT 2098 2099// JNIid class for jfieldIDs only 2100// Note to reviewers: 2101// These JNI functions are just moved over to column 1 and not changed 2102// in the compressed oops workspace. 2103JNIid::JNIid(klassOop holder, int offset, JNIid* next) { 2104 _holder = holder; 2105 _offset = offset; 2106 _next = next; 2107 debug_only(_is_static_field_id = false;) 2108} 2109 2110 2111JNIid* JNIid::find(int offset) { 2112 JNIid* current = this; 2113 while (current != NULL) { 2114 if (current->offset() == offset) return current; 2115 current = current->next(); 2116 } 2117 return NULL; 2118} 2119 2120void JNIid::oops_do(OopClosure* f) { 2121 for (JNIid* cur = this; cur != NULL; cur = cur->next()) { 2122 f->do_oop(cur->holder_addr()); 2123 } 2124} 2125 2126void JNIid::deallocate(JNIid* current) { 2127 while (current != NULL) { 2128 JNIid* next = current->next(); 2129 delete current; 2130 current = next; 2131 } 2132} 2133 2134 2135void JNIid::verify(klassOop holder) { 2136 int first_field_offset = instanceKlass::cast(holder)->offset_of_static_fields(); 2137 int end_field_offset; 2138 end_field_offset = first_field_offset + (instanceKlass::cast(holder)->static_field_size() * wordSize); 2139 2140 JNIid* current = this; 2141 while (current != NULL) { 2142 guarantee(current->holder() == holder, "Invalid klass in JNIid"); 2143#ifdef ASSERT 2144 int o = current->offset(); 2145 if (current->is_static_field_id()) { 2146 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid"); 2147 } 2148#endif 2149 current = current->next(); 2150 } 2151} 2152 2153 2154#ifdef ASSERT 2155void instanceKlass::set_init_state(ClassState state) { 2156 bool good_state = as_klassOop()->is_shared() ? (_init_state <= state) 2157 : (_init_state < state); 2158 assert(good_state || state == allocated, "illegal state transition"); 2159 _init_state = state; 2160} 2161#endif 2162 2163 2164// RedefineClasses() support for previous versions: 2165 2166// Add an information node that contains weak references to the 2167// interesting parts of the previous version of the_class. 2168void instanceKlass::add_previous_version(instanceKlassHandle ikh, 2169 BitMap* emcp_methods, int emcp_method_count) { 2170 assert(Thread::current()->is_VM_thread(), 2171 "only VMThread can add previous versions"); 2172 2173 if (_previous_versions == NULL) { 2174 // This is the first previous version so make some space. 2175 // Start with 2 elements under the assumption that the class 2176 // won't be redefined much. 2177 _previous_versions = new (ResourceObj::C_HEAP) 2178 GrowableArray<PreviousVersionNode *>(2, true); 2179 } 2180 2181 // RC_TRACE macro has an embedded ResourceMark 2182 RC_TRACE(0x00000100, ("adding previous version ref for %s @%d, EMCP_cnt=%d", 2183 ikh->external_name(), _previous_versions->length(), emcp_method_count)); 2184 constantPoolHandle cp_h(ikh->constants()); 2185 jobject cp_ref; 2186 if (cp_h->is_shared()) { 2187 // a shared ConstantPool requires a regular reference; a weak 2188 // reference would be collectible 2189 cp_ref = JNIHandles::make_global(cp_h); 2190 } else { 2191 cp_ref = JNIHandles::make_weak_global(cp_h); 2192 } 2193 PreviousVersionNode * pv_node = NULL; 2194 objArrayOop old_methods = ikh->methods(); 2195 2196 if (emcp_method_count == 0) { 2197 // non-shared ConstantPool gets a weak reference 2198 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), NULL); 2199 RC_TRACE(0x00000400, 2200 ("add: all methods are obsolete; flushing any EMCP weak refs")); 2201 } else { 2202 int local_count = 0; 2203 GrowableArray<jweak>* method_refs = new (ResourceObj::C_HEAP) 2204 GrowableArray<jweak>(emcp_method_count, true); 2205 for (int i = 0; i < old_methods->length(); i++) { 2206 if (emcp_methods->at(i)) { 2207 // this old method is EMCP so save a weak ref 2208 methodOop old_method = (methodOop) old_methods->obj_at(i); 2209 methodHandle old_method_h(old_method); 2210 jweak method_ref = JNIHandles::make_weak_global(old_method_h); 2211 method_refs->append(method_ref); 2212 if (++local_count >= emcp_method_count) { 2213 // no more EMCP methods so bail out now 2214 break; 2215 } 2216 } 2217 } 2218 // non-shared ConstantPool gets a weak reference 2219 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), method_refs); 2220 } 2221 2222 _previous_versions->append(pv_node); 2223 2224 // Using weak references allows the interesting parts of previous 2225 // classes to be GC'ed when they are no longer needed. Since the 2226 // caller is the VMThread and we are at a safepoint, this is a good 2227 // time to clear out unused weak references. 2228 2229 RC_TRACE(0x00000400, ("add: previous version length=%d", 2230 _previous_versions->length())); 2231 2232 // skip the last entry since we just added it 2233 for (int i = _previous_versions->length() - 2; i >= 0; i--) { 2234 // check the previous versions array for a GC'ed weak refs 2235 pv_node = _previous_versions->at(i); 2236 cp_ref = pv_node->prev_constant_pool(); 2237 assert(cp_ref != NULL, "cp ref was unexpectedly cleared"); 2238 if (cp_ref == NULL) { 2239 delete pv_node; 2240 _previous_versions->remove_at(i); 2241 // Since we are traversing the array backwards, we don't have to 2242 // do anything special with the index. 2243 continue; // robustness 2244 } 2245 2246 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref); 2247 if (cp == NULL) { 2248 // this entry has been GC'ed so remove it 2249 delete pv_node; 2250 _previous_versions->remove_at(i); 2251 // Since we are traversing the array backwards, we don't have to 2252 // do anything special with the index. 2253 continue; 2254 } else { 2255 RC_TRACE(0x00000400, ("add: previous version @%d is alive", i)); 2256 } 2257 2258 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods(); 2259 if (method_refs != NULL) { 2260 RC_TRACE(0x00000400, ("add: previous methods length=%d", 2261 method_refs->length())); 2262 for (int j = method_refs->length() - 1; j >= 0; j--) { 2263 jweak method_ref = method_refs->at(j); 2264 assert(method_ref != NULL, "weak method ref was unexpectedly cleared"); 2265 if (method_ref == NULL) { 2266 method_refs->remove_at(j); 2267 // Since we are traversing the array backwards, we don't have to 2268 // do anything special with the index. 2269 continue; // robustness 2270 } 2271 2272 methodOop method = (methodOop)JNIHandles::resolve(method_ref); 2273 if (method == NULL || emcp_method_count == 0) { 2274 // This method entry has been GC'ed or the current 2275 // RedefineClasses() call has made all methods obsolete 2276 // so remove it. 2277 JNIHandles::destroy_weak_global(method_ref); 2278 method_refs->remove_at(j); 2279 } else { 2280 // RC_TRACE macro has an embedded ResourceMark 2281 RC_TRACE(0x00000400, 2282 ("add: %s(%s): previous method @%d in version @%d is alive", 2283 method->name()->as_C_string(), method->signature()->as_C_string(), 2284 j, i)); 2285 } 2286 } 2287 } 2288 } 2289 2290 int obsolete_method_count = old_methods->length() - emcp_method_count; 2291 2292 if (emcp_method_count != 0 && obsolete_method_count != 0 && 2293 _previous_versions->length() > 1) { 2294 // We have a mix of obsolete and EMCP methods. If there is more 2295 // than the previous version that we just added, then we have to 2296 // clear out any matching EMCP method entries the hard way. 2297 int local_count = 0; 2298 for (int i = 0; i < old_methods->length(); i++) { 2299 if (!emcp_methods->at(i)) { 2300 // only obsolete methods are interesting 2301 methodOop old_method = (methodOop) old_methods->obj_at(i); 2302 symbolOop m_name = old_method->name(); 2303 symbolOop m_signature = old_method->signature(); 2304 2305 // skip the last entry since we just added it 2306 for (int j = _previous_versions->length() - 2; j >= 0; j--) { 2307 // check the previous versions array for a GC'ed weak refs 2308 pv_node = _previous_versions->at(j); 2309 cp_ref = pv_node->prev_constant_pool(); 2310 assert(cp_ref != NULL, "cp ref was unexpectedly cleared"); 2311 if (cp_ref == NULL) { 2312 delete pv_node; 2313 _previous_versions->remove_at(j); 2314 // Since we are traversing the array backwards, we don't have to 2315 // do anything special with the index. 2316 continue; // robustness 2317 } 2318 2319 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref); 2320 if (cp == NULL) { 2321 // this entry has been GC'ed so remove it 2322 delete pv_node; 2323 _previous_versions->remove_at(j); 2324 // Since we are traversing the array backwards, we don't have to 2325 // do anything special with the index. 2326 continue; 2327 } 2328 2329 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods(); 2330 if (method_refs == NULL) { 2331 // We have run into a PreviousVersion generation where 2332 // all methods were made obsolete during that generation's 2333 // RedefineClasses() operation. At the time of that 2334 // operation, all EMCP methods were flushed so we don't 2335 // have to go back any further. 2336 // 2337 // A NULL method_refs is different than an empty method_refs. 2338 // We cannot infer any optimizations about older generations 2339 // from an empty method_refs for the current generation. 2340 break; 2341 } 2342 2343 for (int k = method_refs->length() - 1; k >= 0; k--) { 2344 jweak method_ref = method_refs->at(k); 2345 assert(method_ref != NULL, 2346 "weak method ref was unexpectedly cleared"); 2347 if (method_ref == NULL) { 2348 method_refs->remove_at(k); 2349 // Since we are traversing the array backwards, we don't 2350 // have to do anything special with the index. 2351 continue; // robustness 2352 } 2353 2354 methodOop method = (methodOop)JNIHandles::resolve(method_ref); 2355 if (method == NULL) { 2356 // this method entry has been GC'ed so skip it 2357 JNIHandles::destroy_weak_global(method_ref); 2358 method_refs->remove_at(k); 2359 continue; 2360 } 2361 2362 if (method->name() == m_name && 2363 method->signature() == m_signature) { 2364 // The current RedefineClasses() call has made all EMCP 2365 // versions of this method obsolete so mark it as obsolete 2366 // and remove the weak ref. 2367 RC_TRACE(0x00000400, 2368 ("add: %s(%s): flush obsolete method @%d in version @%d", 2369 m_name->as_C_string(), m_signature->as_C_string(), k, j)); 2370 2371 method->set_is_obsolete(); 2372 JNIHandles::destroy_weak_global(method_ref); 2373 method_refs->remove_at(k); 2374 break; 2375 } 2376 } 2377 2378 // The previous loop may not find a matching EMCP method, but 2379 // that doesn't mean that we can optimize and not go any 2380 // further back in the PreviousVersion generations. The EMCP 2381 // method for this generation could have already been GC'ed, 2382 // but there still may be an older EMCP method that has not 2383 // been GC'ed. 2384 } 2385 2386 if (++local_count >= obsolete_method_count) { 2387 // no more obsolete methods so bail out now 2388 break; 2389 } 2390 } 2391 } 2392 } 2393} // end add_previous_version() 2394 2395 2396// Determine if instanceKlass has a previous version. 2397bool instanceKlass::has_previous_version() const { 2398 if (_previous_versions == NULL) { 2399 // no previous versions array so answer is easy 2400 return false; 2401 } 2402 2403 for (int i = _previous_versions->length() - 1; i >= 0; i--) { 2404 // Check the previous versions array for an info node that hasn't 2405 // been GC'ed 2406 PreviousVersionNode * pv_node = _previous_versions->at(i); 2407 2408 jobject cp_ref = pv_node->prev_constant_pool(); 2409 assert(cp_ref != NULL, "cp reference was unexpectedly cleared"); 2410 if (cp_ref == NULL) { 2411 continue; // robustness 2412 } 2413 2414 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref); 2415 if (cp != NULL) { 2416 // we have at least one previous version 2417 return true; 2418 } 2419 2420 // We don't have to check the method refs. If the constant pool has 2421 // been GC'ed then so have the methods. 2422 } 2423 2424 // all of the underlying nodes' info has been GC'ed 2425 return false; 2426} // end has_previous_version() 2427 2428methodOop instanceKlass::method_with_idnum(int idnum) { 2429 methodOop m = NULL; 2430 if (idnum < methods()->length()) { 2431 m = (methodOop) methods()->obj_at(idnum); 2432 } 2433 if (m == NULL || m->method_idnum() != idnum) { 2434 for (int index = 0; index < methods()->length(); ++index) { 2435 m = (methodOop) methods()->obj_at(index); 2436 if (m->method_idnum() == idnum) { 2437 return m; 2438 } 2439 } 2440 } 2441 return m; 2442} 2443 2444 2445// Set the annotation at 'idnum' to 'anno'. 2446// We don't want to create or extend the array if 'anno' is NULL, since that is the 2447// default value. However, if the array exists and is long enough, we must set NULL values. 2448void instanceKlass::set_methods_annotations_of(int idnum, typeArrayOop anno, objArrayOop* md_p) { 2449 objArrayOop md = *md_p; 2450 if (md != NULL && md->length() > idnum) { 2451 md->obj_at_put(idnum, anno); 2452 } else if (anno != NULL) { 2453 // create the array 2454 int length = MAX2(idnum+1, (int)_idnum_allocated_count); 2455 md = oopFactory::new_system_objArray(length, Thread::current()); 2456 if (*md_p != NULL) { 2457 // copy the existing entries 2458 for (int index = 0; index < (*md_p)->length(); index++) { 2459 md->obj_at_put(index, (*md_p)->obj_at(index)); 2460 } 2461 } 2462 set_annotations(md, md_p); 2463 md->obj_at_put(idnum, anno); 2464 } // if no array and idnum isn't included there is nothing to do 2465} 2466 2467// Construct a PreviousVersionNode entry for the array hung off 2468// the instanceKlass. 2469PreviousVersionNode::PreviousVersionNode(jobject prev_constant_pool, 2470 bool prev_cp_is_weak, GrowableArray<jweak>* prev_EMCP_methods) { 2471 2472 _prev_constant_pool = prev_constant_pool; 2473 _prev_cp_is_weak = prev_cp_is_weak; 2474 _prev_EMCP_methods = prev_EMCP_methods; 2475} 2476 2477 2478// Destroy a PreviousVersionNode 2479PreviousVersionNode::~PreviousVersionNode() { 2480 if (_prev_constant_pool != NULL) { 2481 if (_prev_cp_is_weak) { 2482 JNIHandles::destroy_weak_global(_prev_constant_pool); 2483 } else { 2484 JNIHandles::destroy_global(_prev_constant_pool); 2485 } 2486 } 2487 2488 if (_prev_EMCP_methods != NULL) { 2489 for (int i = _prev_EMCP_methods->length() - 1; i >= 0; i--) { 2490 jweak method_ref = _prev_EMCP_methods->at(i); 2491 if (method_ref != NULL) { 2492 JNIHandles::destroy_weak_global(method_ref); 2493 } 2494 } 2495 delete _prev_EMCP_methods; 2496 } 2497} 2498 2499 2500// Construct a PreviousVersionInfo entry 2501PreviousVersionInfo::PreviousVersionInfo(PreviousVersionNode *pv_node) { 2502 _prev_constant_pool_handle = constantPoolHandle(); // NULL handle 2503 _prev_EMCP_method_handles = NULL; 2504 2505 jobject cp_ref = pv_node->prev_constant_pool(); 2506 assert(cp_ref != NULL, "constant pool ref was unexpectedly cleared"); 2507 if (cp_ref == NULL) { 2508 return; // robustness 2509 } 2510 2511 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref); 2512 if (cp == NULL) { 2513 // Weak reference has been GC'ed. Since the constant pool has been 2514 // GC'ed, the methods have also been GC'ed. 2515 return; 2516 } 2517 2518 // make the constantPoolOop safe to return 2519 _prev_constant_pool_handle = constantPoolHandle(cp); 2520 2521 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods(); 2522 if (method_refs == NULL) { 2523 // the instanceKlass did not have any EMCP methods 2524 return; 2525 } 2526 2527 _prev_EMCP_method_handles = new GrowableArray<methodHandle>(10); 2528 2529 int n_methods = method_refs->length(); 2530 for (int i = 0; i < n_methods; i++) { 2531 jweak method_ref = method_refs->at(i); 2532 assert(method_ref != NULL, "weak method ref was unexpectedly cleared"); 2533 if (method_ref == NULL) { 2534 continue; // robustness 2535 } 2536 2537 methodOop method = (methodOop)JNIHandles::resolve(method_ref); 2538 if (method == NULL) { 2539 // this entry has been GC'ed so skip it 2540 continue; 2541 } 2542 2543 // make the methodOop safe to return 2544 _prev_EMCP_method_handles->append(methodHandle(method)); 2545 } 2546} 2547 2548 2549// Destroy a PreviousVersionInfo 2550PreviousVersionInfo::~PreviousVersionInfo() { 2551 // Since _prev_EMCP_method_handles is not C-heap allocated, we 2552 // don't have to delete it. 2553} 2554 2555 2556// Construct a helper for walking the previous versions array 2557PreviousVersionWalker::PreviousVersionWalker(instanceKlass *ik) { 2558 _previous_versions = ik->previous_versions(); 2559 _current_index = 0; 2560 // _hm needs no initialization 2561 _current_p = NULL; 2562} 2563 2564 2565// Destroy a PreviousVersionWalker 2566PreviousVersionWalker::~PreviousVersionWalker() { 2567 // Delete the current info just in case the caller didn't walk to 2568 // the end of the previous versions list. No harm if _current_p is 2569 // already NULL. 2570 delete _current_p; 2571 2572 // When _hm is destroyed, all the Handles returned in 2573 // PreviousVersionInfo objects will be destroyed. 2574 // Also, after this destructor is finished it will be 2575 // safe to delete the GrowableArray allocated in the 2576 // PreviousVersionInfo objects. 2577} 2578 2579 2580// Return the interesting information for the next previous version 2581// of the klass. Returns NULL if there are no more previous versions. 2582PreviousVersionInfo* PreviousVersionWalker::next_previous_version() { 2583 if (_previous_versions == NULL) { 2584 // no previous versions so nothing to return 2585 return NULL; 2586 } 2587 2588 delete _current_p; // cleanup the previous info for the caller 2589 _current_p = NULL; // reset to NULL so we don't delete same object twice 2590 2591 int length = _previous_versions->length(); 2592 2593 while (_current_index < length) { 2594 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++); 2595 PreviousVersionInfo * pv_info = new (ResourceObj::C_HEAP) 2596 PreviousVersionInfo(pv_node); 2597 2598 constantPoolHandle cp_h = pv_info->prev_constant_pool_handle(); 2599 if (cp_h.is_null()) { 2600 delete pv_info; 2601 2602 // The underlying node's info has been GC'ed so try the next one. 2603 // We don't have to check the methods. If the constant pool has 2604 // GC'ed then so have the methods. 2605 continue; 2606 } 2607 2608 // Found a node with non GC'ed info so return it. The caller will 2609 // need to delete pv_info when they are done with it. 2610 _current_p = pv_info; 2611 return pv_info; 2612 } 2613 2614 // all of the underlying nodes' info has been GC'ed 2615 return NULL; 2616} // end next_previous_version() 2617