3064 3065 KlassHandle caller; 3066 if (caller_jh != NULL) { 3067 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); 3068 if (!java_lang_Class::is_instance(caller_oop)) return -1; 3069 caller = KlassHandle(THREAD, java_lang_Class::as_klassOop(caller_oop)); 3070 } 3071 3072 if (name != NULL && sig != NULL && results.not_null()) { 3073 // try a direct resolve 3074 // %%% TO DO 3075 } 3076 3077 int res = MethodHandles::find_MemberNames(k(), name, sig, mflags, 3078 caller(), skip, results()); 3079 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 3080 return res; 3081 } 3082 JVM_END 3083 3084 methodOop MethodHandles::resolve_raise_exception_method(TRAPS) { 3085 if (_raise_exception_method != NULL) { 3086 // no need to do it twice 3087 return raise_exception_method(); 3088 } 3089 // LinkResolver::resolve_invokedynamic can reach this point 3090 // because an invokedynamic has failed very early (7049415) 3091 KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass(); 3092 if (MHN_klass.not_null()) { 3093 TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK_NULL); 3094 TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK_NULL); 3095 methodOop raiseException_method = instanceKlass::cast(MHN_klass->as_klassOop()) 3096 ->find_method(raiseException_name, raiseException_sig); 3097 if (raiseException_method != NULL && raiseException_method->is_static()) { 3098 return raiseException_method; 3099 } 3100 } 3101 // not found; let the caller deal with it 3102 return NULL; 3103 } 3120 JavaCalls::call(&result, raiseException_method, &args, CHECK); 3121 } 3122 3123 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) { 3124 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL); 3125 THROW_MSG_NULL(UOE_name, "MethodHandle.invoke cannot be invoked reflectively"); 3126 return NULL; 3127 } 3128 JVM_END 3129 3130 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) { 3131 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL); 3132 THROW_MSG_NULL(UOE_name, "MethodHandle.invokeExact cannot be invoked reflectively"); 3133 return NULL; 3134 } 3135 JVM_END 3136 3137 3138 /// JVM_RegisterMethodHandleMethods 3139 3140 #define LANG "Ljava/lang/" 3141 #define JLINV "Ljava/lang/invoke/" 3142 3143 #define OBJ LANG"Object;" 3144 #define CLS LANG"Class;" 3145 #define STRG LANG"String;" 3146 #define MT JLINV"MethodType;" 3147 #define MH JLINV"MethodHandle;" 3148 #define MEM JLINV"MemberName;" 3149 #define AMH JLINV"AdapterMethodHandle;" 3150 #define BMH JLINV"BoundMethodHandle;" 3151 #define DMH JLINV"DirectMethodHandle;" 3152 3153 #define CC (char*) /*cast a literal from (const char*)*/ 3154 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 3155 3156 // These are the native methods on sun.invoke.MethodHandleNatives. 3157 static JNINativeMethod methods[] = { 3158 // void init(MemberName self, AccessibleObject ref) 3159 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, 3160 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, 3161 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, 3162 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, 3163 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 3164 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 3165 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, 3166 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, 3167 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 3168 // static native int getNamedCon(int which, Object[] name) 3169 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 3170 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 3171 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 3172 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} 3173 }; 3174 3175 static JNINativeMethod invoke_methods[] = { 3176 // void init(MemberName self, AccessibleObject ref) 3177 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, 3178 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} 3179 }; 3180 3181 // This one function is exported, used by NativeLookup. 3182 3183 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 3184 assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); 3185 3186 if (!EnableInvokeDynamic) { 3187 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 3188 return; // bind nothing 3189 } 3190 3191 bool enable_MH = true; 3192 3193 { 3194 ThreadToNativeFromVM ttnfv(thread); 3195 3196 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); 3197 if (!env->ExceptionOccurred()) { 3198 const char* L_MH_name = (JLINV "MethodHandle"); 3199 const char* MH_name = L_MH_name+1; 3200 jclass MH_class = env->FindClass(MH_name); 3201 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod)); 3202 } 3203 if (env->ExceptionOccurred()) { 3204 MethodHandles::set_enabled(false); 3205 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 3206 enable_MH = false; 3207 env->ExceptionClear(); 3208 } 3209 } 3210 3211 if (enable_MH) { 3212 methodOop raiseException_method = MethodHandles::resolve_raise_exception_method(CHECK); 3213 if (raiseException_method != NULL) { 3214 MethodHandles::set_raise_exception_method(raiseException_method); 3215 } else { 3216 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 3217 enable_MH = false; 3218 } 3219 } 3220 3221 if (enable_MH) { 3222 MethodHandles::generate_adapters(); 3223 MethodHandles::set_enabled(true); 3224 } 3225 } 3226 JVM_END | 3064 3065 KlassHandle caller; 3066 if (caller_jh != NULL) { 3067 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); 3068 if (!java_lang_Class::is_instance(caller_oop)) return -1; 3069 caller = KlassHandle(THREAD, java_lang_Class::as_klassOop(caller_oop)); 3070 } 3071 3072 if (name != NULL && sig != NULL && results.not_null()) { 3073 // try a direct resolve 3074 // %%% TO DO 3075 } 3076 3077 int res = MethodHandles::find_MemberNames(k(), name, sig, mflags, 3078 caller(), skip, results()); 3079 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 3080 return res; 3081 } 3082 JVM_END 3083 3084 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 3085 oop call_site = JNIHandles::resolve_non_null(call_site_jh); 3086 oop target = JNIHandles::resolve(target_jh); 3087 { 3088 // Walk all nmethods depending on this call site. 3089 MutexLocker mu(Compile_lock, thread); 3090 Universe::flush_dependents_on(call_site, target); 3091 } 3092 java_lang_invoke_CallSite::set_target(call_site, target); 3093 } 3094 JVM_END 3095 3096 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 3097 oop call_site = JNIHandles::resolve_non_null(call_site_jh); 3098 oop target = JNIHandles::resolve(target_jh); 3099 { 3100 // Walk all nmethods depending on this call site. 3101 MutexLocker mu(Compile_lock, thread); 3102 Universe::flush_dependents_on(call_site, target); 3103 } 3104 java_lang_invoke_CallSite::set_target_volatile(call_site, target); 3105 } 3106 JVM_END 3107 3108 methodOop MethodHandles::resolve_raise_exception_method(TRAPS) { 3109 if (_raise_exception_method != NULL) { 3110 // no need to do it twice 3111 return raise_exception_method(); 3112 } 3113 // LinkResolver::resolve_invokedynamic can reach this point 3114 // because an invokedynamic has failed very early (7049415) 3115 KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass(); 3116 if (MHN_klass.not_null()) { 3117 TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK_NULL); 3118 TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK_NULL); 3119 methodOop raiseException_method = instanceKlass::cast(MHN_klass->as_klassOop()) 3120 ->find_method(raiseException_name, raiseException_sig); 3121 if (raiseException_method != NULL && raiseException_method->is_static()) { 3122 return raiseException_method; 3123 } 3124 } 3125 // not found; let the caller deal with it 3126 return NULL; 3127 } 3144 JavaCalls::call(&result, raiseException_method, &args, CHECK); 3145 } 3146 3147 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) { 3148 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL); 3149 THROW_MSG_NULL(UOE_name, "MethodHandle.invoke cannot be invoked reflectively"); 3150 return NULL; 3151 } 3152 JVM_END 3153 3154 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) { 3155 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL); 3156 THROW_MSG_NULL(UOE_name, "MethodHandle.invokeExact cannot be invoked reflectively"); 3157 return NULL; 3158 } 3159 JVM_END 3160 3161 3162 /// JVM_RegisterMethodHandleMethods 3163 3164 #undef CS // Solaris builds complain 3165 3166 #define LANG "Ljava/lang/" 3167 #define JLINV "Ljava/lang/invoke/" 3168 3169 #define OBJ LANG"Object;" 3170 #define CLS LANG"Class;" 3171 #define STRG LANG"String;" 3172 #define CS JLINV"CallSite;" 3173 #define MT JLINV"MethodType;" 3174 #define MH JLINV"MethodHandle;" 3175 #define MEM JLINV"MemberName;" 3176 #define AMH JLINV"AdapterMethodHandle;" 3177 #define BMH JLINV"BoundMethodHandle;" 3178 #define DMH JLINV"DirectMethodHandle;" 3179 3180 #define CC (char*) /*cast a literal from (const char*)*/ 3181 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 3182 3183 // These are the native methods on java.lang.invoke.MethodHandleNatives. 3184 static JNINativeMethod methods[] = { 3185 // void init(MemberName self, AccessibleObject ref) 3186 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, 3187 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, 3188 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, 3189 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, 3190 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 3191 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 3192 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, 3193 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, 3194 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 3195 // static native int getNamedCon(int which, Object[] name) 3196 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 3197 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 3198 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 3199 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} 3200 }; 3201 3202 static JNINativeMethod call_site_methods[] = { 3203 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, 3204 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)} 3205 }; 3206 3207 static JNINativeMethod invoke_methods[] = { 3208 // void init(MemberName self, AccessibleObject ref) 3209 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, 3210 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} 3211 }; 3212 3213 // This one function is exported, used by NativeLookup. 3214 3215 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 3216 assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); 3217 3218 if (!EnableInvokeDynamic) { 3219 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 3220 return; // bind nothing 3221 } 3222 3223 assert(!MethodHandles::enabled(), "must not be enabled"); 3224 bool enable_MH = true; 3225 3226 { 3227 ThreadToNativeFromVM ttnfv(thread); 3228 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); 3229 if (!env->ExceptionOccurred()) { 3230 const char* L_MH_name = (JLINV "MethodHandle"); 3231 const char* MH_name = L_MH_name+1; 3232 jclass MH_class = env->FindClass(MH_name); 3233 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod)); 3234 } 3235 if (env->ExceptionOccurred()) { 3236 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 3237 enable_MH = false; 3238 env->ExceptionClear(); 3239 } 3240 3241 status = env->RegisterNatives(MHN_class, call_site_methods, sizeof(call_site_methods)/sizeof(JNINativeMethod)); 3242 if (env->ExceptionOccurred()) { 3243 // Exception is okay until 7087357 3244 env->ExceptionClear(); 3245 } 3246 } 3247 3248 if (enable_MH) { 3249 methodOop raiseException_method = MethodHandles::resolve_raise_exception_method(CHECK); 3250 if (raiseException_method != NULL) { 3251 MethodHandles::set_raise_exception_method(raiseException_method); 3252 } else { 3253 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 3254 enable_MH = false; 3255 } 3256 } 3257 3258 if (enable_MH) { 3259 MethodHandles::generate_adapters(); 3260 MethodHandles::set_enabled(true); 3261 } 3262 } 3263 JVM_END |