Path: blob/master/src/hotspot/share/oops/cpCache.cpp
40951 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "cds/heapShared.hpp"26#include "classfile/resolutionErrors.hpp"27#include "classfile/systemDictionary.hpp"28#include "classfile/vmClasses.hpp"29#include "interpreter/bytecodeStream.hpp"30#include "interpreter/bytecodes.hpp"31#include "interpreter/interpreter.hpp"32#include "interpreter/linkResolver.hpp"33#include "interpreter/rewriter.hpp"34#include "logging/log.hpp"35#include "logging/logStream.hpp"36#include "memory/metadataFactory.hpp"37#include "memory/metaspaceClosure.hpp"38#include "memory/resourceArea.hpp"39#include "oops/access.inline.hpp"40#include "oops/compressedOops.hpp"41#include "oops/constantPool.inline.hpp"42#include "oops/cpCache.inline.hpp"43#include "oops/objArrayOop.inline.hpp"44#include "oops/oop.inline.hpp"45#include "prims/methodHandles.hpp"46#include "runtime/arguments.hpp"47#include "runtime/atomic.hpp"48#include "runtime/handles.inline.hpp"49#include "runtime/vm_version.hpp"50#include "utilities/macros.hpp"5152// Implementation of ConstantPoolCacheEntry5354void ConstantPoolCacheEntry::initialize_entry(int index) {55assert(0 < index && index < 0x10000, "sanity check");56_indices = index;57_f1 = NULL;58_f2 = _flags = 0;59assert(constant_pool_index() == index, "");60}6162void ConstantPoolCacheEntry::verify_just_initialized(bool f2_used) {63assert((_indices & (~cp_index_mask)) == 0, "sanity");64assert(_f1 == NULL, "sanity");65assert(_flags == 0, "sanity");66if (!f2_used) {67assert(_f2 == 0, "sanity");68}69}7071void ConstantPoolCacheEntry::reinitialize(bool f2_used) {72_indices &= cp_index_mask;73_f1 = NULL;74_flags = 0;75if (!f2_used) {76_f2 = 0;77}78}7980int ConstantPoolCacheEntry::make_flags(TosState state,81int option_bits,82int field_index_or_method_params) {83assert(state < number_of_states, "Invalid state in make_flags");84int f = ((int)state << tos_state_shift) | option_bits | field_index_or_method_params;85// Preserve existing flag bit values86// The low bits are a field offset, or else the method parameter size.87#ifdef ASSERT88TosState old_state = flag_state();89assert(old_state == (TosState)0 || old_state == state,90"inconsistent cpCache flags state");91#endif92return (_flags | f) ;93}9495void ConstantPoolCacheEntry::set_bytecode_1(Bytecodes::Code code) {96#ifdef ASSERT97// Read once.98volatile Bytecodes::Code c = bytecode_1();99assert(c == 0 || c == code || code == 0, "update must be consistent");100#endif101// Need to flush pending stores here before bytecode is written.102Atomic::release_store(&_indices, _indices | ((u_char)code << bytecode_1_shift));103}104105void ConstantPoolCacheEntry::set_bytecode_2(Bytecodes::Code code) {106#ifdef ASSERT107// Read once.108volatile Bytecodes::Code c = bytecode_2();109assert(c == 0 || c == code || code == 0, "update must be consistent");110#endif111// Need to flush pending stores here before bytecode is written.112Atomic::release_store(&_indices, _indices | ((u_char)code << bytecode_2_shift));113}114115// Sets f1, ordering with previous writes.116void ConstantPoolCacheEntry::release_set_f1(Metadata* f1) {117assert(f1 != NULL, "");118Atomic::release_store(&_f1, f1);119}120121void ConstantPoolCacheEntry::set_indy_resolution_failed() {122Atomic::release_store(&_flags, _flags | (1 << indy_resolution_failed_shift));123}124125// Note that concurrent update of both bytecodes can leave one of them126// reset to zero. This is harmless; the interpreter will simply re-resolve127// the damaged entry. More seriously, the memory synchronization is needed128// to flush other fields (f1, f2) completely to memory before the bytecodes129// are updated, lest other processors see a non-zero bytecode but zero f1/f2.130void ConstantPoolCacheEntry::set_field(Bytecodes::Code get_code,131Bytecodes::Code put_code,132Klass* field_holder,133int field_index,134int field_offset,135TosState field_type,136bool is_final,137bool is_volatile) {138set_f1(field_holder);139set_f2(field_offset);140assert((field_index & field_index_mask) == field_index,141"field index does not fit in low flag bits");142set_field_flags(field_type,143((is_volatile ? 1 : 0) << is_volatile_shift) |144((is_final ? 1 : 0) << is_final_shift),145field_index);146set_bytecode_1(get_code);147set_bytecode_2(put_code);148NOT_PRODUCT(verify(tty));149}150151void ConstantPoolCacheEntry::set_parameter_size(int value) {152// This routine is called only in corner cases where the CPCE is not yet initialized.153// See AbstractInterpreter::deopt_continue_after_entry.154assert(_flags == 0 || parameter_size() == 0 || parameter_size() == value,155"size must not change: parameter_size=%d, value=%d", parameter_size(), value);156// Setting the parameter size by itself is only safe if the157// current value of _flags is 0, otherwise another thread may have158// updated it and we don't want to overwrite that value. Don't159// bother trying to update it once it's nonzero but always make160// sure that the final parameter size agrees with what was passed.161if (_flags == 0) {162intx newflags = (value & parameter_size_mask);163Atomic::cmpxchg(&_flags, (intx)0, newflags);164}165guarantee(parameter_size() == value,166"size must not change: parameter_size=%d, value=%d", parameter_size(), value);167}168169void ConstantPoolCacheEntry::set_direct_or_vtable_call(Bytecodes::Code invoke_code,170const methodHandle& method,171int vtable_index,172bool sender_is_interface) {173bool is_vtable_call = (vtable_index >= 0); // FIXME: split this method on this boolean174assert(method->interpreter_entry() != NULL, "should have been set at this point");175assert(!method->is_obsolete(), "attempt to write obsolete method to cpCache");176177int byte_no = -1;178bool change_to_virtual = false;179InstanceKlass* holder = NULL; // have to declare this outside the switch180switch (invoke_code) {181case Bytecodes::_invokeinterface:182holder = method->method_holder();183// check for private interface method invocations184if (vtable_index == Method::nonvirtual_vtable_index && holder->is_interface() ) {185assert(method->is_private(), "unexpected non-private method");186assert(method->can_be_statically_bound(), "unexpected non-statically-bound method");187// set_f2_as_vfinal_method checks if is_vfinal flag is true.188set_method_flags(as_TosState(method->result_type()),189( 1 << is_vfinal_shift) |190((method->is_final_method() ? 1 : 0) << is_final_shift),191method()->size_of_parameters());192set_f2_as_vfinal_method(method());193byte_no = 2;194set_f1(holder); // interface klass*195break;196}197else {198// We get here from InterpreterRuntime::resolve_invoke when an invokeinterface199// instruction links to a non-interface method (in Object). This can happen when200// an interface redeclares an Object method (like CharSequence declaring toString())201// or when invokeinterface is used explicitly.202// In that case, the method has no itable index and must be invoked as a virtual.203// Set a flag to keep track of this corner case.204assert(holder->is_interface() || holder == vmClasses::Object_klass(), "unexpected holder class");205assert(method->is_public(), "Calling non-public method in Object with invokeinterface");206change_to_virtual = true;207208// ...and fall through as if we were handling invokevirtual:209}210case Bytecodes::_invokevirtual:211{212if (!is_vtable_call) {213assert(method->can_be_statically_bound(), "");214// set_f2_as_vfinal_method checks if is_vfinal flag is true.215set_method_flags(as_TosState(method->result_type()),216( 1 << is_vfinal_shift) |217((method->is_final_method() ? 1 : 0) << is_final_shift) |218((change_to_virtual ? 1 : 0) << is_forced_virtual_shift),219method()->size_of_parameters());220set_f2_as_vfinal_method(method());221} else {222assert(!method->can_be_statically_bound(), "");223assert(vtable_index >= 0, "valid index");224assert(!method->is_final_method(), "sanity");225set_method_flags(as_TosState(method->result_type()),226((change_to_virtual ? 1 : 0) << is_forced_virtual_shift),227method()->size_of_parameters());228set_f2(vtable_index);229}230byte_no = 2;231break;232}233234case Bytecodes::_invokespecial:235case Bytecodes::_invokestatic:236assert(!is_vtable_call, "");237// Note: Read and preserve the value of the is_vfinal flag on any238// invokevirtual bytecode shared with this constant pool cache entry.239// It is cheap and safe to consult is_vfinal() at all times.240// Once is_vfinal is set, it must stay that way, lest we get a dangling oop.241set_method_flags(as_TosState(method->result_type()),242((is_vfinal() ? 1 : 0) << is_vfinal_shift) |243((method->is_final_method() ? 1 : 0) << is_final_shift),244method()->size_of_parameters());245set_f1(method());246byte_no = 1;247break;248default:249ShouldNotReachHere();250break;251}252253// Note: byte_no also appears in TemplateTable::resolve.254if (byte_no == 1) {255assert(invoke_code != Bytecodes::_invokevirtual &&256invoke_code != Bytecodes::_invokeinterface, "");257bool do_resolve = true;258// Don't mark invokespecial to method as resolved if sender is an interface. The receiver259// has to be checked that it is a subclass of the current class every time this bytecode260// is executed.261if (invoke_code == Bytecodes::_invokespecial && sender_is_interface &&262method->name() != vmSymbols::object_initializer_name()) {263do_resolve = false;264}265if (invoke_code == Bytecodes::_invokestatic) {266assert(method->method_holder()->is_initialized() ||267method->method_holder()->is_reentrant_initialization(Thread::current()),268"invalid class initialization state for invoke_static");269270if (!VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) {271// Don't mark invokestatic to method as resolved if the holder class has not yet completed272// initialization. An invokestatic must only proceed if the class is initialized, but if273// we resolve it before then that class initialization check is skipped.274//275// When fast class initialization checks are supported (VM_Version::supports_fast_class_init_checks() == true),276// template interpreter supports fast class initialization check for277// invokestatic which doesn't require call site re-resolution to278// enforce class initialization barrier.279do_resolve = false;280}281}282if (do_resolve) {283set_bytecode_1(invoke_code);284}285} else if (byte_no == 2) {286if (change_to_virtual) {287assert(invoke_code == Bytecodes::_invokeinterface, "");288// NOTE: THIS IS A HACK - BE VERY CAREFUL!!!289//290// Workaround for the case where we encounter an invokeinterface, but we291// should really have an _invokevirtual since the resolved method is a292// virtual method in java.lang.Object. This is a corner case in the spec293// but is presumably legal. javac does not generate this code.294//295// We do not set bytecode_1() to _invokeinterface, because that is the296// bytecode # used by the interpreter to see if it is resolved. In this297// case, the method gets reresolved with caller for each interface call298// because the actual selected method may not be public.299//300// We set bytecode_2() to _invokevirtual.301// See also interpreterRuntime.cpp. (8/25/2000)302} else {303assert(invoke_code == Bytecodes::_invokevirtual ||304(invoke_code == Bytecodes::_invokeinterface &&305((method->is_private() ||306(method->is_final() && method->method_holder() == vmClasses::Object_klass())))),307"unexpected invocation mode");308if (invoke_code == Bytecodes::_invokeinterface &&309(method->is_private() || method->is_final())) {310// We set bytecode_1() to _invokeinterface, because that is the311// bytecode # used by the interpreter to see if it is resolved.312// We set bytecode_2() to _invokevirtual.313set_bytecode_1(invoke_code);314}315}316// set up for invokevirtual, even if linking for invokeinterface also:317set_bytecode_2(Bytecodes::_invokevirtual);318} else {319ShouldNotReachHere();320}321NOT_PRODUCT(verify(tty));322}323324void ConstantPoolCacheEntry::set_direct_call(Bytecodes::Code invoke_code, const methodHandle& method,325bool sender_is_interface) {326int index = Method::nonvirtual_vtable_index;327// index < 0; FIXME: inline and customize set_direct_or_vtable_call328set_direct_or_vtable_call(invoke_code, method, index, sender_is_interface);329}330331void ConstantPoolCacheEntry::set_vtable_call(Bytecodes::Code invoke_code, const methodHandle& method, int index) {332// either the method is a miranda or its holder should accept the given index333assert(method->method_holder()->is_interface() || method->method_holder()->verify_vtable_index(index), "");334// index >= 0; FIXME: inline and customize set_direct_or_vtable_call335set_direct_or_vtable_call(invoke_code, method, index, false);336}337338void ConstantPoolCacheEntry::set_itable_call(Bytecodes::Code invoke_code,339Klass* referenced_klass,340const methodHandle& method, int index) {341assert(method->method_holder()->verify_itable_index(index), "");342assert(invoke_code == Bytecodes::_invokeinterface, "");343InstanceKlass* interf = method->method_holder();344assert(interf->is_interface(), "must be an interface");345assert(!method->is_final_method(), "interfaces do not have final methods; cannot link to one here");346set_f1(referenced_klass);347set_f2((intx)method());348set_method_flags(as_TosState(method->result_type()),3490, // no option bits350method()->size_of_parameters());351set_bytecode_1(Bytecodes::_invokeinterface);352}353354355void ConstantPoolCacheEntry::set_method_handle(const constantPoolHandle& cpool, const CallInfo &call_info) {356set_method_handle_common(cpool, Bytecodes::_invokehandle, call_info);357}358359void ConstantPoolCacheEntry::set_dynamic_call(const constantPoolHandle& cpool, const CallInfo &call_info) {360set_method_handle_common(cpool, Bytecodes::_invokedynamic, call_info);361}362363void ConstantPoolCacheEntry::set_method_handle_common(const constantPoolHandle& cpool,364Bytecodes::Code invoke_code,365const CallInfo &call_info) {366// NOTE: This CPCE can be the subject of data races.367// There are three words to update: flags, refs[f2], f1 (in that order).368// Writers must store all other values before f1.369// Readers must test f1 first for non-null before reading other fields.370// Competing writers must acquire exclusive access via a lock.371// A losing writer waits on the lock until the winner writes f1 and leaves372// the lock, so that when the losing writer returns, he can use the linked373// cache entry.374375JavaThread* current = JavaThread::current();376objArrayHandle resolved_references(current, cpool->resolved_references());377// Use the resolved_references() lock for this cpCache entry.378// resolved_references are created for all classes with Invokedynamic, MethodHandle379// or MethodType constant pool cache entries.380assert(resolved_references() != NULL,381"a resolved_references array should have been created for this class");382ObjectLocker ol(resolved_references, current);383if (!is_f1_null()) {384return;385}386387if (indy_resolution_failed()) {388// Before we got here, another thread got a LinkageError exception during389// resolution. Ignore our success and throw their exception.390ConstantPoolCache* cpCache = cpool->cache();391int index = -1;392for (int i = 0; i < cpCache->length(); i++) {393if (cpCache->entry_at(i) == this) {394index = i;395break;396}397}398guarantee(index >= 0, "Didn't find cpCache entry!");399int encoded_index = ResolutionErrorTable::encode_cpcache_index(400ConstantPool::encode_invokedynamic_index(index));401JavaThread* THREAD = JavaThread::current(); // For exception macros.402ConstantPool::throw_resolution_error(cpool, encoded_index, THREAD);403return;404}405406Method* adapter = call_info.resolved_method();407const Handle appendix = call_info.resolved_appendix();408const bool has_appendix = appendix.not_null();409410// Write the flags.411// MHs and indy are always sig-poly and have a local signature.412set_method_flags(as_TosState(adapter->result_type()),413((has_appendix ? 1 : 0) << has_appendix_shift ) |414( 1 << has_local_signature_shift ) |415( 1 << is_final_shift ),416adapter->size_of_parameters());417418LogStream* log_stream = NULL;419LogStreamHandle(Debug, methodhandles, indy) lsh_indy;420if (lsh_indy.is_enabled()) {421ResourceMark rm;422log_stream = &lsh_indy;423log_stream->print_cr("set_method_handle bc=%d appendix=" PTR_FORMAT "%s method=" PTR_FORMAT " (local signature) ",424invoke_code,425p2i(appendix()),426(has_appendix ? "" : " (unused)"),427p2i(adapter));428adapter->print_on(log_stream);429if (has_appendix) appendix()->print_on(log_stream);430}431432// Method handle invokes and invokedynamic sites use both cp cache words.433// refs[f2], if not null, contains a value passed as a trailing argument to the adapter.434// In the general case, this could be the call site's MethodType,435// for use with java.lang.Invokers.checkExactType, or else a CallSite object.436// f1 contains the adapter method which manages the actual call.437// In the general case, this is a compiled LambdaForm.438// (The Java code is free to optimize these calls by binding other439// sorts of methods and appendices to call sites.)440// JVM-level linking is via f1, as if for invokespecial, and signatures are erased.441// The appendix argument (if any) is added to the signature, and is counted in the parameter_size bits.442// Even with the appendix, the method will never take more than 255 parameter slots.443//444// This means that given a call site like (List)mh.invoke("foo"),445// the f1 method has signature '(Ljl/Object;Ljl/invoke/MethodType;)Ljl/Object;',446// not '(Ljava/lang/String;)Ljava/util/List;'.447// The fact that String and List are involved is encoded in the MethodType in refs[f2].448// This allows us to create fewer Methods, while keeping type safety.449//450451// Store appendix, if any.452if (has_appendix) {453const int appendix_index = f2_as_index();454assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob");455assert(resolved_references->obj_at(appendix_index) == NULL, "init just once");456resolved_references->obj_at_put(appendix_index, appendix());457}458459release_set_f1(adapter); // This must be the last one to set (see NOTE above)!460461// The interpreter assembly code does not check byte_2,462// but it is used by is_resolved, method_if_resolved, etc.463set_bytecode_1(invoke_code);464NOT_PRODUCT(verify(tty));465466if (log_stream != NULL) {467this->print(log_stream, 0);468}469470assert(has_appendix == this->has_appendix(), "proper storage of appendix flag");471assert(this->has_local_signature(), "proper storage of signature flag");472}473474bool ConstantPoolCacheEntry::save_and_throw_indy_exc(475const constantPoolHandle& cpool, int cpool_index, int index, constantTag tag, TRAPS) {476477assert(HAS_PENDING_EXCEPTION, "No exception got thrown!");478assert(PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass()),479"No LinkageError exception");480481// Use the resolved_references() lock for this cpCache entry.482// resolved_references are created for all classes with Invokedynamic, MethodHandle483// or MethodType constant pool cache entries.484JavaThread* current = THREAD;485objArrayHandle resolved_references(current, cpool->resolved_references());486assert(resolved_references() != NULL,487"a resolved_references array should have been created for this class");488ObjectLocker ol(resolved_references, current);489490// if f1 is not null or the indy_resolution_failed flag is set then another491// thread either succeeded in resolving the method or got a LinkageError492// exception, before this thread was able to record its failure. So, clear493// this thread's exception and return false so caller can use the earlier494// thread's result.495if (!is_f1_null() || indy_resolution_failed()) {496CLEAR_PENDING_EXCEPTION;497return false;498}499500Symbol* error = PENDING_EXCEPTION->klass()->name();501Symbol* message = java_lang_Throwable::detail_message(PENDING_EXCEPTION);502503SystemDictionary::add_resolution_error(cpool, index, error, message);504set_indy_resolution_failed();505return true;506}507508Method* ConstantPoolCacheEntry::method_if_resolved(const constantPoolHandle& cpool) {509// Decode the action of set_method and set_interface_call510Bytecodes::Code invoke_code = bytecode_1();511if (invoke_code != (Bytecodes::Code)0) {512Metadata* f1 = f1_ord();513if (f1 != NULL) {514switch (invoke_code) {515case Bytecodes::_invokeinterface:516assert(f1->is_klass(), "");517return f2_as_interface_method();518case Bytecodes::_invokestatic:519case Bytecodes::_invokespecial:520assert(!has_appendix(), "");521case Bytecodes::_invokehandle:522case Bytecodes::_invokedynamic:523assert(f1->is_method(), "");524return (Method*)f1;525default:526break;527}528}529}530invoke_code = bytecode_2();531if (invoke_code != (Bytecodes::Code)0) {532switch (invoke_code) {533case Bytecodes::_invokevirtual:534if (is_vfinal()) {535// invokevirtual536Method* m = f2_as_vfinal_method();537assert(m->is_method(), "");538return m;539} else {540int holder_index = cpool->uncached_klass_ref_index_at(constant_pool_index());541if (cpool->tag_at(holder_index).is_klass()) {542Klass* klass = cpool->resolved_klass_at(holder_index);543return klass->method_at_vtable(f2_as_index());544}545}546break;547default:548break;549}550}551return NULL;552}553554555oop ConstantPoolCacheEntry::appendix_if_resolved(const constantPoolHandle& cpool) {556if (!has_appendix())557return NULL;558const int ref_index = f2_as_index();559objArrayOop resolved_references = cpool->resolved_references();560return resolved_references->obj_at(ref_index);561}562563564#if INCLUDE_JVMTI565566void log_adjust(const char* entry_type, Method* old_method, Method* new_method, bool* trace_name_printed) {567ResourceMark rm;568569if (!(*trace_name_printed)) {570log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name());571*trace_name_printed = true;572}573log_trace(redefine, class, update, constantpool)574("cpc %s entry update: %s", entry_type, new_method->external_name());575}576577// RedefineClasses() API support:578// If this ConstantPoolCacheEntry refers to old_method then update it579// to refer to new_method.580void ConstantPoolCacheEntry::adjust_method_entry(Method* old_method,581Method* new_method, bool * trace_name_printed) {582583if (is_vfinal()) {584// virtual and final so _f2 contains method ptr instead of vtable index585if (f2_as_vfinal_method() == old_method) {586// match old_method so need an update587// NOTE: can't use set_f2_as_vfinal_method as it asserts on different values588_f2 = (intptr_t)new_method;589log_adjust("vfinal", old_method, new_method, trace_name_printed);590}591return;592}593594assert (_f1 != NULL, "should not call with uninteresting entry");595596if (!(_f1->is_method())) {597// _f1 is a Klass* for an interface, _f2 is the method598if (f2_as_interface_method() == old_method) {599_f2 = (intptr_t)new_method;600log_adjust("interface", old_method, new_method, trace_name_printed);601}602} else if (_f1 == old_method) {603_f1 = new_method;604log_adjust("special, static or dynamic", old_method, new_method, trace_name_printed);605}606}607608// a constant pool cache entry should never contain old or obsolete methods609bool ConstantPoolCacheEntry::check_no_old_or_obsolete_entries() {610Method* m = get_interesting_method_entry();611// return false if m refers to a non-deleted old or obsolete method612if (m != NULL) {613assert(m->is_valid() && m->is_method(), "m is a valid method");614return !m->is_old() && !m->is_obsolete(); // old is always set for old and obsolete615} else {616return true;617}618}619620Method* ConstantPoolCacheEntry::get_interesting_method_entry() {621if (!is_method_entry()) {622// not a method entry so not interesting by default623return NULL;624}625Method* m = NULL;626if (is_vfinal()) {627// virtual and final so _f2 contains method ptr instead of vtable index628m = f2_as_vfinal_method();629} else if (is_f1_null()) {630// NULL _f1 means this is a virtual entry so also not interesting631return NULL;632} else {633if (!(_f1->is_method())) {634// _f1 is a Klass* for an interface635m = f2_as_interface_method();636} else {637m = f1_as_method();638}639}640assert(m != NULL && m->is_method(), "sanity check");641if (m == NULL || !m->is_method()) {642return NULL;643}644return m;645}646#endif // INCLUDE_JVMTI647648void ConstantPoolCacheEntry::print(outputStream* st, int index) const {649// print separator650if (index == 0) st->print_cr(" -------------");651// print entry652st->print("%3d (" PTR_FORMAT ") ", index, (intptr_t)this);653st->print_cr("[%02x|%02x|%5d]", bytecode_2(), bytecode_1(),654constant_pool_index());655st->print_cr(" [ " PTR_FORMAT "]", (intptr_t)_f1);656st->print_cr(" [ " PTR_FORMAT "]", (intptr_t)_f2);657st->print_cr(" [ " PTR_FORMAT "]", (intptr_t)_flags);658st->print_cr(" -------------");659}660661void ConstantPoolCacheEntry::verify(outputStream* st) const {662// not implemented yet663}664665// Implementation of ConstantPoolCache666667ConstantPoolCache* ConstantPoolCache::allocate(ClassLoaderData* loader_data,668const intStack& index_map,669const intStack& invokedynamic_index_map,670const intStack& invokedynamic_map, TRAPS) {671672const int length = index_map.length() + invokedynamic_index_map.length();673int size = ConstantPoolCache::size(length);674675return new (loader_data, size, MetaspaceObj::ConstantPoolCacheType, THREAD)676ConstantPoolCache(length, index_map, invokedynamic_index_map, invokedynamic_map);677}678679void ConstantPoolCache::initialize(const intArray& inverse_index_map,680const intArray& invokedynamic_inverse_index_map,681const intArray& invokedynamic_references_map) {682for (int i = 0; i < inverse_index_map.length(); i++) {683ConstantPoolCacheEntry* e = entry_at(i);684int original_index = inverse_index_map.at(i);685e->initialize_entry(original_index);686assert(entry_at(i) == e, "sanity");687}688689// Append invokedynamic entries at the end690int invokedynamic_offset = inverse_index_map.length();691for (int i = 0; i < invokedynamic_inverse_index_map.length(); i++) {692int offset = i + invokedynamic_offset;693ConstantPoolCacheEntry* e = entry_at(offset);694int original_index = invokedynamic_inverse_index_map.at(i);695e->initialize_entry(original_index);696assert(entry_at(offset) == e, "sanity");697}698699for (int ref = 0; ref < invokedynamic_references_map.length(); ref++) {700const int cpci = invokedynamic_references_map.at(ref);701if (cpci >= 0) {702entry_at(cpci)->initialize_resolved_reference_index(ref);703}704}705}706707void ConstantPoolCache::verify_just_initialized() {708DEBUG_ONLY(walk_entries_for_initialization(/*check_only = */ true));709}710711void ConstantPoolCache::remove_unshareable_info() {712walk_entries_for_initialization(/*check_only = */ false);713}714715void ConstantPoolCache::walk_entries_for_initialization(bool check_only) {716Arguments::assert_is_dumping_archive();717// When dumping the archive, we want to clean up the ConstantPoolCache718// to remove any effect of linking due to the execution of Java code --719// each ConstantPoolCacheEntry will have the same contents as if720// ConstantPoolCache::initialize has just returned:721//722// - We keep the ConstantPoolCache::constant_pool_index() bits for all entries.723// - We keep the "f2" field for entries used by invokedynamic and invokehandle724// - All other bits in the entries are cleared to zero.725ResourceMark rm;726727InstanceKlass* ik = constant_pool()->pool_holder();728bool* f2_used = NEW_RESOURCE_ARRAY(bool, length());729memset(f2_used, 0, sizeof(bool) * length());730731Thread* current = Thread::current();732733// Find all the slots that we need to preserve f2734for (int i = 0; i < ik->methods()->length(); i++) {735Method* m = ik->methods()->at(i);736RawBytecodeStream bcs(methodHandle(current, m));737while (!bcs.is_last_bytecode()) {738Bytecodes::Code opcode = bcs.raw_next();739switch (opcode) {740case Bytecodes::_invokedynamic: {741int index = Bytes::get_native_u4(bcs.bcp() + 1);742int cp_cache_index = constant_pool()->invokedynamic_cp_cache_index(index);743f2_used[cp_cache_index] = 1;744}745break;746case Bytecodes::_invokehandle: {747int cp_cache_index = Bytes::get_native_u2(bcs.bcp() + 1);748f2_used[cp_cache_index] = 1;749}750break;751default:752break;753}754}755}756757if (check_only) {758DEBUG_ONLY(759for (int i=0; i<length(); i++) {760entry_at(i)->verify_just_initialized(f2_used[i]);761})762} else {763for (int i=0; i<length(); i++) {764entry_at(i)->reinitialize(f2_used[i]);765}766}767}768769void ConstantPoolCache::deallocate_contents(ClassLoaderData* data) {770assert(!is_shared(), "shared caches are not deallocated");771data->remove_handle(_resolved_references);772set_resolved_references(OopHandle());773MetadataFactory::free_array<u2>(data, _reference_map);774set_reference_map(NULL);775}776777#if INCLUDE_CDS_JAVA_HEAP778oop ConstantPoolCache::archived_references() {779if (_archived_references_index < 0) {780return NULL;781}782return HeapShared::get_root(_archived_references_index);783}784785void ConstantPoolCache::clear_archived_references() {786if (_archived_references_index >= 0) {787HeapShared::clear_root(_archived_references_index);788_archived_references_index = -1;789}790}791792void ConstantPoolCache::set_archived_references(oop o) {793assert(DumpSharedSpaces, "called only during runtime");794_archived_references_index = HeapShared::append_root(o);795}796#endif797798#if INCLUDE_JVMTI799// RedefineClasses() API support:800// If any entry of this ConstantPoolCache points to any of801// old_methods, replace it with the corresponding new_method.802void ConstantPoolCache::adjust_method_entries(bool * trace_name_printed) {803for (int i = 0; i < length(); i++) {804ConstantPoolCacheEntry* entry = entry_at(i);805Method* old_method = entry->get_interesting_method_entry();806if (old_method == NULL || !old_method->is_old()) {807continue; // skip uninteresting entries808}809if (old_method->is_deleted()) {810// clean up entries with deleted methods811entry->initialize_entry(entry->constant_pool_index());812continue;813}814Method* new_method = old_method->get_new_method();815entry_at(i)->adjust_method_entry(old_method, new_method, trace_name_printed);816}817}818819// the constant pool cache should never contain old or obsolete methods820bool ConstantPoolCache::check_no_old_or_obsolete_entries() {821ResourceMark rm;822for (int i = 1; i < length(); i++) {823Method* m = entry_at(i)->get_interesting_method_entry();824if (m != NULL && !entry_at(i)->check_no_old_or_obsolete_entries()) {825log_trace(redefine, class, update, constantpool)826("cpcache check found old method entry: class: %s, old: %d, obsolete: %d, method: %s",827constant_pool()->pool_holder()->external_name(), m->is_old(), m->is_obsolete(), m->external_name());828return false;829}830}831return true;832}833834void ConstantPoolCache::dump_cache() {835for (int i = 1; i < length(); i++) {836if (entry_at(i)->get_interesting_method_entry() != NULL) {837entry_at(i)->print(tty, i);838}839}840}841#endif // INCLUDE_JVMTI842843void ConstantPoolCache::metaspace_pointers_do(MetaspaceClosure* it) {844log_trace(cds)("Iter(ConstantPoolCache): %p", this);845it->push(&_constant_pool);846it->push(&_reference_map);847}848849// Printing850851void ConstantPoolCache::print_on(outputStream* st) const {852st->print_cr("%s", internal_name());853// print constant pool cache entries854for (int i = 0; i < length(); i++) entry_at(i)->print(st, i);855}856857void ConstantPoolCache::print_value_on(outputStream* st) const {858st->print("cache [%d]", length());859print_address_on(st);860st->print(" for ");861constant_pool()->print_value_on(st);862}863864865// Verification866867void ConstantPoolCache::verify_on(outputStream* st) {868// print constant pool cache entries869for (int i = 0; i < length(); i++) entry_at(i)->verify(st);870}871872873