0N/A/*
4372N/A * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_implementation/shared/markSweep.inline.hpp"
1879N/A#include "interpreter/interpreter.hpp"
1879N/A#include "interpreter/rewriter.hpp"
1879N/A#include "memory/universe.inline.hpp"
1879N/A#include "oops/cpCacheOop.hpp"
1879N/A#include "oops/objArrayOop.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "prims/jvmtiRedefineClassesTrace.hpp"
3932N/A#include "prims/methodHandles.hpp"
1879N/A#include "runtime/handles.inline.hpp"
0N/A
0N/A
0N/A// Implememtation of ConstantPoolCacheEntry
0N/A
1059N/Avoid ConstantPoolCacheEntry::initialize_entry(int index) {
726N/A assert(0 < index && index < 0x10000, "sanity check");
0N/A _indices = index;
726N/A assert(constant_pool_index() == index, "");
0N/A}
0N/A
1059N/Avoid ConstantPoolCacheEntry::initialize_secondary_entry(int main_index) {
1059N/A assert(0 <= main_index && main_index < 0x10000, "sanity check");
3932N/A _indices = (main_index << main_cp_index_bits);
1059N/A assert(main_entry_index() == main_index, "");
1059N/A}
0N/A
3932N/Aint ConstantPoolCacheEntry::make_flags(TosState state,
3932N/A int option_bits,
3932N/A int field_index_or_method_params) {
3932N/A assert(state < number_of_states, "Invalid state in make_flags");
3932N/A int f = ((int)state << tos_state_shift) | option_bits | field_index_or_method_params;
0N/A // Preserve existing flag bit values
3932N/A // The low bits are a field offset, or else the method parameter size.
0N/A#ifdef ASSERT
3932N/A TosState old_state = flag_state();
3932N/A assert(old_state == (TosState)0 || old_state == state,
0N/A "inconsistent cpCache flags state");
0N/A#endif
0N/A return (_flags | f) ;
0N/A}
0N/A
0N/Avoid ConstantPoolCacheEntry::set_bytecode_1(Bytecodes::Code code) {
3932N/A assert(!is_secondary_entry(), "must not overwrite main_entry_index");
0N/A#ifdef ASSERT
0N/A // Read once.
0N/A volatile Bytecodes::Code c = bytecode_1();
0N/A assert(c == 0 || c == code || code == 0, "update must be consistent");
0N/A#endif
0N/A // Need to flush pending stores here before bytecode is written.
3932N/A OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << bytecode_1_shift));
0N/A}
0N/A
0N/Avoid ConstantPoolCacheEntry::set_bytecode_2(Bytecodes::Code code) {
3932N/A assert(!is_secondary_entry(), "must not overwrite main_entry_index");
0N/A#ifdef ASSERT
0N/A // Read once.
0N/A volatile Bytecodes::Code c = bytecode_2();
0N/A assert(c == 0 || c == code || code == 0, "update must be consistent");
0N/A#endif
0N/A // Need to flush pending stores here before bytecode is written.
3932N/A OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << bytecode_2_shift));
0N/A}
0N/A
3932N/A// Sets f1, ordering with previous writes.
3932N/Avoid ConstantPoolCacheEntry::release_set_f1(oop f1) {
2118N/A // Use barriers as in oop_store
3932N/A assert(f1 != NULL, "");
2118N/A oop* f1_addr = (oop*) &_f1;
2118N/A update_barrier_set_pre(f1_addr, f1);
3932N/A OrderAccess::release_store_ptr((intptr_t*)f1_addr, f1);
3932N/A update_barrier_set((void*) f1_addr, f1);
3932N/A}
3932N/A
3932N/A// Sets flags, but only if the value was previously zero.
3932N/Abool ConstantPoolCacheEntry::init_flags_atomic(intptr_t flags) {
3932N/A intptr_t result = Atomic::cmpxchg_ptr(flags, &_flags, 0);
3932N/A return (result == 0);
2118N/A}
1823N/A
0N/A#ifdef ASSERT
0N/A// It is possible to have two different dummy methodOops created
0N/A// when the resolve code for invoke interface executes concurrently
0N/A// Hence the assertion below is weakened a bit for the invokeinterface
0N/A// case.
0N/Abool ConstantPoolCacheEntry::same_methodOop(oop cur_f1, oop f1) {
0N/A return (cur_f1 == f1 || ((methodOop)cur_f1)->name() ==
0N/A ((methodOop)f1)->name() || ((methodOop)cur_f1)->signature() ==
0N/A ((methodOop)f1)->signature());
0N/A}
0N/A#endif
0N/A
0N/A// Note that concurrent update of both bytecodes can leave one of them
0N/A// reset to zero. This is harmless; the interpreter will simply re-resolve
0N/A// the damaged entry. More seriously, the memory synchronization is needed
0N/A// to flush other fields (f1, f2) completely to memory before the bytecodes
0N/A// are updated, lest other processors see a non-zero bytecode but zero f1/f2.
0N/Avoid ConstantPoolCacheEntry::set_field(Bytecodes::Code get_code,
0N/A Bytecodes::Code put_code,
0N/A KlassHandle field_holder,
2771N/A int field_index,
0N/A int field_offset,
0N/A TosState field_type,
0N/A bool is_final,
0N/A bool is_volatile) {
2223N/A set_f1(field_holder()->java_mirror());
0N/A set_f2(field_offset);
3932N/A assert((field_index & field_index_mask) == field_index,
0N/A "field index does not fit in low flag bits");
3932N/A set_field_flags(field_type,
3932N/A ((is_volatile ? 1 : 0) << is_volatile_shift) |
3932N/A ((is_final ? 1 : 0) << is_final_shift),
3932N/A field_index);
0N/A set_bytecode_1(get_code);
0N/A set_bytecode_2(put_code);
0N/A NOT_PRODUCT(verify(tty));
0N/A}
0N/A
3932N/Avoid ConstantPoolCacheEntry::set_parameter_size(int value) {
3932N/A // This routine is called only in corner cases where the CPCE is not yet initialized.
3932N/A // See AbstractInterpreter::deopt_continue_after_entry.
3932N/A assert(_flags == 0 || parameter_size() == 0 || parameter_size() == value,
3932N/A err_msg("size must not change: parameter_size=%d, value=%d", parameter_size(), value));
3932N/A // Setting the parameter size by itself is only safe if the
3932N/A // current value of _flags is 0, otherwise another thread may have
3932N/A // updated it and we don't want to overwrite that value. Don't
3932N/A // bother trying to update it once it's nonzero but always make
3932N/A // sure that the final parameter size agrees with what was passed.
3932N/A if (_flags == 0) {
3932N/A Atomic::cmpxchg_ptr((value & parameter_size_mask), &_flags, 0);
3932N/A }
3932N/A guarantee(parameter_size() == value,
3932N/A err_msg("size must not change: parameter_size=%d, value=%d", parameter_size(), value));
0N/A}
0N/A
0N/Avoid ConstantPoolCacheEntry::set_method(Bytecodes::Code invoke_code,
0N/A methodHandle method,
0N/A int vtable_index) {
1580N/A assert(!is_secondary_entry(), "");
0N/A assert(method->interpreter_entry() != NULL, "should have been set at this point");
0N/A assert(!method->is_obsolete(), "attempt to write obsolete method to cpCache");
0N/A
0N/A int byte_no = -1;
3932N/A bool change_to_virtual = false;
3932N/A
0N/A switch (invoke_code) {
3932N/A case Bytecodes::_invokeinterface:
3932N/A // We get here from InterpreterRuntime::resolve_invoke when an invokeinterface
3932N/A // instruction somehow links to a non-interface method (in Object).
3932N/A // In that case, the method has no itable index and must be invoked as a virtual.
3932N/A // Set a flag to keep track of this corner case.
3932N/A change_to_virtual = true;
3932N/A
3932N/A // ...and fall through as if we were handling invokevirtual:
0N/A case Bytecodes::_invokevirtual:
3932N/A {
0N/A if (method->can_be_statically_bound()) {
3932N/A // set_f2_as_vfinal_method checks if is_vfinal flag is true.
3932N/A set_method_flags(as_TosState(method->result_type()),
3932N/A ( 1 << is_vfinal_shift) |
3932N/A ((method->is_final_method() ? 1 : 0) << is_final_shift) |
3932N/A ((change_to_virtual ? 1 : 0) << is_forced_virtual_shift),
3932N/A method()->size_of_parameters());
3932N/A set_f2_as_vfinal_method(method());
0N/A } else {
0N/A assert(vtable_index >= 0, "valid index");
3932N/A assert(!method->is_final_method(), "sanity");
3932N/A set_method_flags(as_TosState(method->result_type()),
3932N/A ((change_to_virtual ? 1 : 0) << is_forced_virtual_shift),
3932N/A method()->size_of_parameters());
0N/A set_f2(vtable_index);
0N/A }
0N/A byte_no = 2;
0N/A break;
1580N/A }
1580N/A
0N/A case Bytecodes::_invokespecial:
0N/A case Bytecodes::_invokestatic:
3932N/A // Note: Read and preserve the value of the is_vfinal flag on any
3932N/A // invokevirtual bytecode shared with this constant pool cache entry.
3932N/A // It is cheap and safe to consult is_vfinal() at all times.
3932N/A // Once is_vfinal is set, it must stay that way, lest we get a dangling oop.
3932N/A set_method_flags(as_TosState(method->result_type()),
3932N/A ((is_vfinal() ? 1 : 0) << is_vfinal_shift) |
3932N/A ((method->is_final_method() ? 1 : 0) << is_final_shift),
3932N/A method()->size_of_parameters());
0N/A set_f1(method());
0N/A byte_no = 1;
0N/A break;
0N/A default:
0N/A ShouldNotReachHere();
0N/A break;
0N/A }
0N/A
0N/A // Note: byte_no also appears in TemplateTable::resolve.
0N/A if (byte_no == 1) {
3932N/A assert(invoke_code != Bytecodes::_invokevirtual &&
3932N/A invoke_code != Bytecodes::_invokeinterface, "");
0N/A set_bytecode_1(invoke_code);
0N/A } else if (byte_no == 2) {
0N/A if (change_to_virtual) {
3932N/A assert(invoke_code == Bytecodes::_invokeinterface, "");
0N/A // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
0N/A //
0N/A // Workaround for the case where we encounter an invokeinterface, but we
0N/A // should really have an _invokevirtual since the resolved method is a
0N/A // virtual method in java.lang.Object. This is a corner case in the spec
0N/A // but is presumably legal. javac does not generate this code.
0N/A //
0N/A // We set bytecode_1() to _invokeinterface, because that is the
0N/A // bytecode # used by the interpreter to see if it is resolved.
0N/A // We set bytecode_2() to _invokevirtual.
0N/A // See also interpreterRuntime.cpp. (8/25/2000)
0N/A // Only set resolved for the invokeinterface case if method is public.
0N/A // Otherwise, the method needs to be reresolved with caller for each
0N/A // interface call.
0N/A if (method->is_public()) set_bytecode_1(invoke_code);
0N/A } else {
3932N/A assert(invoke_code == Bytecodes::_invokevirtual, "");
0N/A }
3932N/A // set up for invokevirtual, even if linking for invokeinterface also:
3932N/A set_bytecode_2(Bytecodes::_invokevirtual);
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
0N/A NOT_PRODUCT(verify(tty));
0N/A}
0N/A
0N/A
0N/Avoid ConstantPoolCacheEntry::set_interface_call(methodHandle method, int index) {
1580N/A assert(!is_secondary_entry(), "");
0N/A klassOop interf = method->method_holder();
0N/A assert(instanceKlass::cast(interf)->is_interface(), "must be an interface");
3932N/A assert(!method->is_final_method(), "interfaces do not have final methods; cannot link to one here");
0N/A set_f1(interf);
0N/A set_f2(index);
3932N/A set_method_flags(as_TosState(method->result_type()),
3932N/A 0, // no option bits
3932N/A method()->size_of_parameters());
0N/A set_bytecode_1(Bytecodes::_invokeinterface);
0N/A}
0N/A
0N/A
4009N/Avoid ConstantPoolCacheEntry::set_method_handle(constantPoolHandle cpool,
4048N/A methodHandle adapter,
4048N/A Handle appendix, Handle method_type) {
3932N/A assert(!is_secondary_entry(), "");
4048N/A set_method_handle_common(cpool, Bytecodes::_invokehandle, adapter, appendix, method_type);
1580N/A}
1580N/A
4009N/Avoid ConstantPoolCacheEntry::set_dynamic_call(constantPoolHandle cpool,
4048N/A methodHandle adapter,
4048N/A Handle appendix, Handle method_type) {
3932N/A assert(is_secondary_entry(), "");
4048N/A set_method_handle_common(cpool, Bytecodes::_invokedynamic, adapter, appendix, method_type);
1580N/A}
1580N/A
4009N/Avoid ConstantPoolCacheEntry::set_method_handle_common(constantPoolHandle cpool,
4009N/A Bytecodes::Code invoke_code,
4009N/A methodHandle adapter,
4048N/A Handle appendix, Handle method_type) {
3932N/A // NOTE: This CPCE can be the subject of data races.
3932N/A // There are three words to update: flags, f2, f1 (in that order).
3932N/A // Writers must store all other values before f1.
3932N/A // Readers must test f1 first for non-null before reading other fields.
4009N/A // Competing writers must acquire exclusive access via a lock.
4009N/A // A losing writer waits on the lock until the winner writes f1 and leaves
4009N/A // the lock, so that when the losing writer returns, he can use the linked
4009N/A // cache entry.
4009N/A
4009N/A Thread* THREAD = Thread::current();
4009N/A ObjectLocker ol(cpool, THREAD);
4009N/A if (!is_f1_null()) {
4009N/A return;
4009N/A }
3932N/A
4048N/A const bool has_appendix = appendix.not_null();
4048N/A const bool has_method_type = method_type.not_null();
4048N/A
3932N/A if (!has_appendix) {
3932N/A // The extra argument is not used, but we need a non-null value to signify linkage state.
3932N/A // Set it to something benign that will never leak memory.
3932N/A appendix = Universe::void_mirror();
3932N/A }
3932N/A
4048N/A // Write the flags.
4009N/A set_method_flags(as_TosState(adapter->result_type()),
4048N/A ((has_appendix ? 1 : 0) << has_appendix_shift) |
4048N/A ((has_method_type ? 1 : 0) << has_method_type_shift) |
4048N/A ( 1 << is_vfinal_shift) |
4048N/A ( 1 << is_final_shift),
3932N/A adapter->size_of_parameters());
3932N/A
3932N/A if (TraceInvokeDynamic) {
4048N/A tty->print_cr("set_method_handle bc=%d appendix="PTR_FORMAT"%s method_type="PTR_FORMAT"%s method="PTR_FORMAT" ",
3932N/A invoke_code,
4048N/A (intptr_t)appendix(), (has_appendix ? "" : " (unused)"),
4048N/A (intptr_t)method_type(), (has_method_type ? "" : " (unused)"),
3932N/A (intptr_t)adapter());
3932N/A adapter->print();
3932N/A if (has_appendix) appendix()->print();
3932N/A }
3932N/A
3932N/A // Method handle invokes and invokedynamic sites use both cp cache words.
3932N/A // f1, if not null, contains a value passed as a trailing argument to the adapter.
3932N/A // In the general case, this could be the call site's MethodType,
3932N/A // for use with java.lang.Invokers.checkExactType, or else a CallSite object.
3932N/A // f2 contains the adapter method which manages the actual call.
3932N/A // In the general case, this is a compiled LambdaForm.
3932N/A // (The Java code is free to optimize these calls by binding other
3932N/A // sorts of methods and appendices to call sites.)
3932N/A // JVM-level linking is via f2, as if for invokevfinal, and signatures are erased.
3932N/A // The appendix argument (if any) is added to the signature, and is counted in the parameter_size bits.
3932N/A // In principle this means that the method (with appendix) could take up to 256 parameter slots.
3932N/A //
3932N/A // This means that given a call site like (List)mh.invoke("foo"),
3932N/A // the f2 method has signature '(Ljl/Object;Ljl/invoke/MethodType;)Ljl/Object;',
3932N/A // not '(Ljava/lang/String;)Ljava/util/List;'.
3932N/A // The fact that String and List are involved is encoded in the MethodType in f1.
3932N/A // This allows us to create fewer method oops, while keeping type safety.
3932N/A //
4048N/A
3932N/A set_f2_as_vfinal_method(adapter());
4048N/A
4048N/A // Store MethodType, if any.
4048N/A if (has_method_type) {
4048N/A ConstantPoolCacheEntry* e2 = cpool->cache()->find_secondary_entry_for(this);
4048N/A
4048N/A // Write the flags.
4048N/A e2->set_method_flags(as_TosState(adapter->result_type()),
4048N/A ((has_method_type ? 1 : 0) << has_method_type_shift) |
4048N/A ( 1 << is_vfinal_shift) |
4048N/A ( 1 << is_final_shift),
4048N/A adapter->size_of_parameters());
4048N/A e2->release_set_f1(method_type());
4048N/A }
4048N/A
3932N/A assert(appendix.not_null(), "needed for linkage state");
3932N/A release_set_f1(appendix()); // This must be the last one to set (see NOTE above)!
4048N/A
3932N/A if (!is_secondary_entry()) {
3932N/A // The interpreter assembly code does not check byte_2,
3932N/A // but it is used by is_resolved, method_if_resolved, etc.
3932N/A set_bytecode_2(invoke_code);
3932N/A }
4048N/A
3932N/A NOT_PRODUCT(verify( Error!

 

There was an error!

null

java.lang.NullPointerException