src/share/vm/prims/methodHandles.cpp

Print this page




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/oopMapCache.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/oopFactory.hpp"

  32 #include "prims/jvmtiRedefineClassesTrace.hpp"
  33 #include "prims/methodHandles.hpp"
  34 #include "runtime/compilationPolicy.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/reflection.hpp"
  37 #include "runtime/signature.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 
  40 
  41 /*
  42  * JSR 292 reference implementation: method handles
  43  * The JDK 7 reference implementation represented method handle
  44  * combinations as chains.  Each link in the chain had a "vmentry"
  45  * field which pointed at a bit of assembly code which performed
  46  * one transformation before dispatching to the next link in the chain.
  47  *
  48  * The current reference implementation pushes almost all code generation
  49  * responsibility to (trusted) Java code.  A method handle contains a
  50  * pointer to its "LambdaForm", which embodies all details of the method
  51  * handle's behavior.  The LambdaForm is a normal Java object, managed


 235         assert(m->is_public(), "virtual call must be to public interface method");
 236         return NULL;  // elicit an error later in product build
 237       }
 238       assert(Klass::cast(resolved_klass)->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe");
 239       m_klass = m_klass_non_interface();
 240     }
 241   }
 242 
 243   oop mname_oop = mname();
 244   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
 245   java_lang_invoke_MemberName::set_vmtarget(mname_oop, m);
 246   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);   // vtable/itable index
 247   java_lang_invoke_MemberName::set_clazz(mname_oop,    Klass::cast(m_klass)->java_mirror());
 248   // Note:  name and type can be lazily computed by resolve_MemberName,
 249   // if Java code needs them as resolved String and MethodType objects.
 250   // The clazz must be eagerly stored, because it provides a GC
 251   // root to help keep alive the methodOop.
 252   // If relevant, the vtable or itable value is stored as vmindex.
 253   // This is done eagerly, since it is readily available without
 254   // constructing any new objects.
 255   instanceKlass::cast(m->method_holder())->add_member_name(m->method_idnum(), mname);
 256 

 257   return mname();




 258 }
 259 
 260 Handle MethodHandles::init_method_MemberName(Handle mname, CallInfo& info, TRAPS) {
 261   Handle empty;
 262   if (info.resolved_appendix().not_null()) {
 263     // The resolved MemberName must not be accompanied by an appendix argument,
 264     // since there is no way to bind this value into the MemberName.
 265     // Caller is responsible to prevent this from happening.
 266     THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 267   }
 268   methodHandle m = info.resolved_method();
 269   KlassHandle defc = info.resolved_klass();
 270   int vmindex = -1;
 271   if (defc->is_interface() && Klass::cast(m->method_holder())->is_interface()) {
 272     // LinkResolver does not report itable indexes!  (fix this?)
 273     vmindex = klassItable::compute_itable_index(m());
 274   } else if (m->can_be_statically_bound()) {
 275     // LinkResolver reports vtable index even for final methods!
 276     vmindex = methodOopDesc::nonvirtual_vtable_index;
 277   } else {


 976 
 977 //------------------------------------------------------------------------------
 978 // MemberNameTable
 979 //
 980 
 981 MemberNameTable::MemberNameTable(int methods_cnt)
 982                   : GrowableArray<jweak>(methods_cnt, true) {
 983   assert_locked_or_safepoint(MemberNameTable_lock);
 984 }
 985 
 986 MemberNameTable::~MemberNameTable() {
 987   assert_locked_or_safepoint(MemberNameTable_lock);
 988   int len = this->length();
 989 
 990   for (int idx = 0; idx < len; idx++) {
 991     jweak ref = this->at(idx);
 992     JNIHandles::destroy_weak_global(ref);
 993   }
 994 }
 995 
 996 void MemberNameTable::add_member_name(int index, jweak mem_name_wref) {
 997   assert_locked_or_safepoint(MemberNameTable_lock);
 998   this->at_put_grow(index, mem_name_wref);
 999 }
1000 
1001 // Return a member name oop or NULL.
1002 oop MemberNameTable::get_member_name(int index) {
1003   assert_locked_or_safepoint(MemberNameTable_lock);
1004   jweak ref = this->at(index);
1005   oop mem_name = JNIHandles::resolve(ref);
1006   return mem_name;
1007 }
1008 
1009 oop MemberNameTable::find_member_name_by_method(methodOop old_method) {
1010   assert_locked_or_safepoint(MemberNameTable_lock);
1011   oop found = NULL;
1012   int len = this->length();
1013 
1014   for (int idx = 0; idx < len; idx++) {
1015     oop mem_name = JNIHandles::resolve(this->at(idx));
1016     if (mem_name == NULL) {
1017       continue;
1018     }
1019     methodOop method = (methodOop)java_lang_invoke_MemberName::vmtarget(mem_name);
1020     if (method == old_method) {
1021       found = mem_name;
1022       break;
1023     }
1024   }
1025   return found;
1026 }
1027 
1028 // It is called at safepoint only
1029 void MemberNameTable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
1030                                             int methods_length, bool *trace_name_printed) {
1031   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
1032   // search the MemberNameTable for uses of either obsolete or EMCP methods
1033   for (int j = 0; j < methods_length; j++) {
1034     methodOop old_method = old_methods[j];
1035     methodOop new_method = new_methods[j];
1036     oop mem_name = find_member_name_by_method(old_method);


1037     if (mem_name != NULL) {
1038       java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method);
1039 
1040       if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
1041         if (!(*trace_name_printed)) {
1042           // RC_TRACE_MESG macro has an embedded ResourceMark
1043           RC_TRACE_MESG(("adjust: name=%s",
1044                          Klass::cast(old_method->method_holder())->external_name()));
1045           *trace_name_printed = true;
1046         }
1047         // RC_TRACE macro has an embedded ResourceMark
1048         RC_TRACE(0x00400000, ("MemberName method update: %s(%s)",
1049                               new_method->name()->as_C_string(),
1050                               new_method->signature()->as_C_string()));
1051       }
1052     }
1053   }
1054 }
1055 
1056 //
1057 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1058 // They are the private interface between this JVM and the HotSpot-specific
1059 // Java code that implements JSR 292 method handles.
1060 //
1061 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1062 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1063 //
1064 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
1065   switch (which) {
1066   case MethodHandles::GC_COUNT_GWT:
1067 #ifdef COMPILER2
1068     return true;
1069 #else
1070     return false;
1071 #endif
1072   }




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/oopMapCache.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/oopFactory.hpp"
  32 #include "oops/methodOop.hpp"
  33 #include "prims/jvmtiRedefineClassesTrace.hpp"
  34 #include "prims/methodHandles.hpp"
  35 #include "runtime/compilationPolicy.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "runtime/reflection.hpp"
  38 #include "runtime/signature.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 
  41 
  42 /*
  43  * JSR 292 reference implementation: method handles
  44  * The JDK 7 reference implementation represented method handle
  45  * combinations as chains.  Each link in the chain had a "vmentry"
  46  * field which pointed at a bit of assembly code which performed
  47  * one transformation before dispatching to the next link in the chain.
  48  *
  49  * The current reference implementation pushes almost all code generation
  50  * responsibility to (trusted) Java code.  A method handle contains a
  51  * pointer to its "LambdaForm", which embodies all details of the method
  52  * handle's behavior.  The LambdaForm is a normal Java object, managed


 236         assert(m->is_public(), "virtual call must be to public interface method");
 237         return NULL;  // elicit an error later in product build
 238       }
 239       assert(Klass::cast(resolved_klass)->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe");
 240       m_klass = m_klass_non_interface();
 241     }
 242   }
 243 
 244   oop mname_oop = mname();
 245   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
 246   java_lang_invoke_MemberName::set_vmtarget(mname_oop, m);
 247   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);   // vtable/itable index
 248   java_lang_invoke_MemberName::set_clazz(mname_oop,    Klass::cast(m_klass)->java_mirror());
 249   // Note:  name and type can be lazily computed by resolve_MemberName,
 250   // if Java code needs them as resolved String and MethodType objects.
 251   // The clazz must be eagerly stored, because it provides a GC
 252   // root to help keep alive the methodOop.
 253   // If relevant, the vtable or itable value is stored as vmindex.
 254   // This is done eagerly, since it is readily available without
 255   // constructing any new objects.

 256 
 257   if (instanceKlass::cast(m->method_holder())->add_member_name(mname)) {
 258     return mname();
 259   } else {
 260     // Redefinition caused this to fail.  Return NULL (and an exception?)
 261     return NULL;
 262   }
 263 }
 264 
 265 Handle MethodHandles::init_method_MemberName(Handle mname, CallInfo& info, TRAPS) {
 266   Handle empty;
 267   if (info.resolved_appendix().not_null()) {
 268     // The resolved MemberName must not be accompanied by an appendix argument,
 269     // since there is no way to bind this value into the MemberName.
 270     // Caller is responsible to prevent this from happening.
 271     THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 272   }
 273   methodHandle m = info.resolved_method();
 274   KlassHandle defc = info.resolved_klass();
 275   int vmindex = -1;
 276   if (defc->is_interface() && Klass::cast(m->method_holder())->is_interface()) {
 277     // LinkResolver does not report itable indexes!  (fix this?)
 278     vmindex = klassItable::compute_itable_index(m());
 279   } else if (m->can_be_statically_bound()) {
 280     // LinkResolver reports vtable index even for final methods!
 281     vmindex = methodOopDesc::nonvirtual_vtable_index;
 282   } else {


 981 
 982 //------------------------------------------------------------------------------
 983 // MemberNameTable
 984 //
 985 
 986 MemberNameTable::MemberNameTable(int methods_cnt)
 987                   : GrowableArray<jweak>(methods_cnt, true) {
 988   assert_locked_or_safepoint(MemberNameTable_lock);
 989 }
 990 
 991 MemberNameTable::~MemberNameTable() {
 992   assert_locked_or_safepoint(MemberNameTable_lock);
 993   int len = this->length();
 994 
 995   for (int idx = 0; idx < len; idx++) {
 996     jweak ref = this->at(idx);
 997     JNIHandles::destroy_weak_global(ref);
 998   }
 999 }
1000 
1001 void MemberNameTable::add_member_name(jweak mem_name_wref) {
1002   assert_locked_or_safepoint(MemberNameTable_lock);
1003   this->push(mem_name_wref);
1004 }
1005 







1006 
1007 // It is called at safepoint only for RedefineClasses



















1008 void MemberNameTable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
1009                                             int methods_length, bool *trace_name_printed) {
1010   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
1011   // For each redefined method
1012   for (int j = 0; j < methods_length; j++) {
1013     methodOop old_method = old_methods[j];
1014     methodOop new_method = new_methods[j];
1015     // search the MemberNameTable for uses of either obsolete or EMCP methods
1016     for (int idx = 0; idx < length(); idx++) {
1017       oop mem_name = JNIHandles::resolve(this->at(idx));
1018       if (mem_name != NULL) {
1019         java_lang_invoke_MemberName::adjust_vmtarget(mem_name, old_method, new_method,
1020                                                      trace_name_printed);






1021        }




1022     }

1023   }
1024 }
1025 
1026 //
1027 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1028 // They are the private interface between this JVM and the HotSpot-specific
1029 // Java code that implements JSR 292 method handles.
1030 //
1031 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1032 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1033 //
1034 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
1035   switch (which) {
1036   case MethodHandles::GC_COUNT_GWT:
1037 #ifdef COMPILER2
1038     return true;
1039 #else
1040     return false;
1041 #endif
1042   }