1 /* 2 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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/stringTable.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 #include "utilities/exceptions.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 53 * by a runtime coded in Java. 54 */ 55 56 bool MethodHandles::_enabled = false; // set true after successful native linkage 57 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; 58 59 60 /** 61 * Generates method handle adapters. Returns 'false' if memory allocation 62 * failed and true otherwise. 63 */ 64 bool MethodHandles::generate_adapters() { 65 if (SystemDictionary::MethodHandle_klass() == NULL) { 66 return true; 67 } 68 69 assert(_adapter_code == NULL, "generate only once"); 70 71 ResourceMark rm; 72 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); 73 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); 74 if (_adapter_code == NULL) { 75 return false; 76 } 77 78 CodeBuffer code(_adapter_code); 79 MethodHandlesAdapterGenerator g(&code); 80 g.generate(); 81 code.log_section_sizes("MethodHandlesAdapterBlob"); 82 return true; 83 } 84 85 //------------------------------------------------------------------------------ 86 // MethodHandlesAdapterGenerator::generate 87 // 88 void MethodHandlesAdapterGenerator::generate() { 89 // Generate generic method handle adapters. 90 // Generate interpreter entries 91 for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; 92 mk <= Interpreter::method_handle_invoke_LAST; 93 mk = Interpreter::MethodKind(1 + (int)mk)) { 94 vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); 95 StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); 96 address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); 97 if (entry != NULL) { 98 Interpreter::set_entry_for_kind(mk, entry); 99 } 100 // If the entry is not set, it will throw AbstractMethodError. 101 } 102 } 103 104 void MethodHandles::set_enabled(bool z) { 105 if (_enabled != z) { 106 guarantee(z, "can only enable once"); 107 _enabled = z; 108 } 109 } 110 111 // MemberName support 112 113 // import java_lang_invoke_MemberName.* 114 enum { 115 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, 116 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, 117 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, 118 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, 119 CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE, 120 REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, 121 REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, 122 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, 123 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, 124 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE 125 }; 126 127 Handle MethodHandles::new_MemberName(TRAPS) { 128 Handle empty; 129 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); 130 if (!k->is_initialized()) k->initialize(CHECK_(empty)); 131 return Handle(THREAD, k->allocate_instance(THREAD)); 132 } 133 134 oop MethodHandles::init_MemberName(Handle mname, Handle target) { 135 // This method is used from java.lang.invoke.MemberName constructors. 136 // It fills in the new MemberName from a java.lang.reflect.Member. 137 Thread* thread = Thread::current(); 138 oop target_oop = target(); 139 Klass* target_klass = target_oop->klass(); 140 if (target_klass == SystemDictionary::reflect_Field_klass()) { 141 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 142 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 143 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 144 if (!k.is_null() && k->oop_is_instance()) { 145 fieldDescriptor fd(InstanceKlass::cast(k()), slot); 146 oop mname2 = init_field_MemberName(mname, fd); 147 if (mname2 != NULL) { 148 // Since we have the reified name and type handy, add them to the result. 149 if (java_lang_invoke_MemberName::name(mname2) == NULL) 150 java_lang_invoke_MemberName::set_name(mname2, java_lang_reflect_Field::name(target_oop)); 151 if (java_lang_invoke_MemberName::type(mname2) == NULL) 152 java_lang_invoke_MemberName::set_type(mname2, java_lang_reflect_Field::type(target_oop)); 153 } 154 return mname2; 155 } 156 } else if (target_klass == SystemDictionary::reflect_Method_klass()) { 157 oop clazz = java_lang_reflect_Method::clazz(target_oop); 158 int slot = java_lang_reflect_Method::slot(target_oop); 159 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 160 if (!k.is_null() && k->oop_is_instance()) { 161 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); 162 if (m == NULL || is_signature_polymorphic(m->intrinsic_id())) 163 return NULL; // do not resolve unless there is a concrete signature 164 CallInfo info(m, k()); 165 return init_method_MemberName(mname, info); 166 } 167 } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) { 168 oop clazz = java_lang_reflect_Constructor::clazz(target_oop); 169 int slot = java_lang_reflect_Constructor::slot(target_oop); 170 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); 171 if (!k.is_null() && k->oop_is_instance()) { 172 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); 173 if (m == NULL) return NULL; 174 CallInfo info(m, k()); 175 return init_method_MemberName(mname, info); 176 } 177 } 178 return NULL; 179 } 180 181 oop MethodHandles::init_method_MemberName(Handle mname, CallInfo& info) { 182 assert(info.resolved_appendix().is_null(), "only normal methods here"); 183 methodHandle m = info.resolved_method(); 184 KlassHandle m_klass = m->method_holder(); 185 int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); 186 int vmindex = Method::invalid_vtable_index; 187 188 switch (info.call_kind()) { 189 case CallInfo::itable_call: 190 vmindex = info.itable_index(); 191 // More importantly, the itable index only works with the method holder. 192 assert(m_klass->verify_itable_index(vmindex), ""); 193 flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); 194 if (TraceInvokeDynamic) { 195 ResourceMark rm; 196 tty->print_cr("memberName: invokeinterface method_holder::method: %s, itableindex: %d, access_flags:", 197 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), 198 vmindex); 199 m->access_flags().print_on(tty); 200 if (!m->is_abstract()) { 201 tty->print("default"); 202 } 203 tty->cr(); 204 } 205 break; 206 207 case CallInfo::vtable_call: 208 vmindex = info.vtable_index(); 209 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 210 assert(info.resolved_klass()->is_subtype_of(m_klass()), "virtual call must be type-safe"); 211 if (m_klass->is_interface()) { 212 // This is a vtable call to an interface method (abstract "miranda method" or default method). 213 // The vtable index is meaningless without a class (not interface) receiver type, so get one. 214 // (LinkResolver should help us figure this out.) 215 KlassHandle m_klass_non_interface = info.resolved_klass(); 216 if (m_klass_non_interface->is_interface()) { 217 m_klass_non_interface = SystemDictionary::Object_klass(); 218 #ifdef ASSERT 219 { ResourceMark rm; 220 Method* m2 = m_klass_non_interface->vtable()->method_at(vmindex); 221 assert(m->name() == m2->name() && m->signature() == m2->signature(), 222 err_msg("at %d, %s != %s", vmindex, 223 m->name_and_sig_as_C_string(), m2->name_and_sig_as_C_string())); 224 } 225 #endif //ASSERT 226 } 227 if (!m->is_public()) { 228 assert(m->is_public(), "virtual call must be to public interface method"); 229 return NULL; // elicit an error later in product build 230 } 231 assert(info.resolved_klass()->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe"); 232 m_klass = m_klass_non_interface; 233 } 234 if (TraceInvokeDynamic) { 235 ResourceMark rm; 236 tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:", 237 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), 238 m_klass->internal_name(), vmindex); 239 m->access_flags().print_on(tty); 240 if (m->is_default_method()) { 241 tty->print("default"); 242 } 243 tty->cr(); 244 } 245 break; 246 247 case CallInfo::direct_call: 248 vmindex = Method::nonvirtual_vtable_index; 249 if (m->is_static()) { 250 flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); 251 } else if (m->is_initializer()) { 252 flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 253 } else { 254 flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 255 } 256 break; 257 258 default: assert(false, "bad CallInfo"); return NULL; 259 } 260 261 // @CallerSensitive annotation detected 262 if (m->caller_sensitive()) { 263 flags |= CALLER_SENSITIVE; 264 } 265 266 oop mname_oop = mname(); 267 java_lang_invoke_MemberName::set_flags( mname_oop, flags); 268 java_lang_invoke_MemberName::set_vmtarget(mname_oop, m()); 269 java_lang_invoke_MemberName::set_vmindex( mname_oop, vmindex); // vtable/itable index 270 java_lang_invoke_MemberName::set_clazz( mname_oop, m_klass->java_mirror()); 271 // Note: name and type can be lazily computed by resolve_MemberName, 272 // if Java code needs them as resolved String and MethodType objects. 273 // The clazz must be eagerly stored, because it provides a GC 274 // root to help keep alive the Method*. 275 // If relevant, the vtable or itable value is stored as vmindex. 276 // This is done eagerly, since it is readily available without 277 // constructing any new objects. 278 279 return mname(); 280 } 281 282 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) { 283 int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); 284 flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); 285 if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); 286 Metadata* vmtarget = fd.field_holder(); 287 int vmindex = fd.offset(); // determines the field uniquely when combined with static bit 288 oop mname_oop = mname(); 289 java_lang_invoke_MemberName::set_flags(mname_oop, flags); 290 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 291 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 292 java_lang_invoke_MemberName::set_clazz(mname_oop, fd.field_holder()->java_mirror()); 293 oop type = field_signature_type_or_null(fd.signature()); 294 oop name = field_name_or_null(fd.name()); 295 if (name != NULL) 296 java_lang_invoke_MemberName::set_name(mname_oop, name); 297 if (type != NULL) 298 java_lang_invoke_MemberName::set_type(mname_oop, type); 299 // Note: name and type can be lazily computed by resolve_MemberName, 300 // if Java code needs them as resolved String and Class objects. 301 // Note that the incoming type oop might be pre-resolved (non-null). 302 // The base clazz and field offset (vmindex) must be eagerly stored, 303 // because they unambiguously identify the field. 304 // Although the fieldDescriptor::_index would also identify the field, 305 // we do not use it, because it is harder to decode. 306 // TO DO: maybe intern mname_oop 307 return mname(); 308 } 309 310 // JVM 2.9 Special Methods: 311 // A method is signature polymorphic if and only if all of the following conditions hold : 312 // * It is declared in the java.lang.invoke.MethodHandle class. 313 // * It has a single formal parameter of type Object[]. 314 // * It has a return type of Object. 315 // * It has the ACC_VARARGS and ACC_NATIVE flags set. 316 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) { 317 if (klass == NULL) 318 return false; 319 // The following test will fail spuriously during bootstrap of MethodHandle itself: 320 // if (klass != SystemDictionary::MethodHandle_klass()) 321 // Test the name instead: 322 if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle()) 323 return false; 324 Symbol* poly_sig = vmSymbols::object_array_object_signature(); 325 Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig); 326 if (m == NULL) return false; 327 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; 328 int flags = m->access_flags().as_int(); 329 return (flags & required) == required; 330 } 331 332 333 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { 334 assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid)); 335 switch (iid) { 336 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); 337 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); 338 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); 339 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); 340 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); 341 } 342 assert(false, ""); 343 return 0; 344 } 345 346 int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { 347 switch (iid) { 348 case vmIntrinsics::_invokeBasic: return 0; 349 case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; 350 case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; 351 case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; 352 case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; 353 } 354 assert(false, err_msg("iid=%d", iid)); 355 return 0; 356 } 357 358 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { 359 vmSymbols::SID name_id = vmSymbols::find_sid(name); 360 switch (name_id) { 361 // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. 362 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; 363 // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: 364 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; 365 366 // There is one static signature-polymorphic method for each JVM invocation mode. 367 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; 368 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; 369 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; 370 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; 371 } 372 373 // Cover the case of invokeExact and any future variants of invokeFoo. 374 Klass* mh_klass = SystemDictionary::well_known_klass( 375 SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) ); 376 if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) 377 return vmIntrinsics::_invokeGeneric; 378 379 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. 380 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. 381 return vmIntrinsics::_none; 382 } 383 384 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) { 385 if (klass != NULL && 386 klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) { 387 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 388 if (iid != vmIntrinsics::_none) 389 return iid; 390 if (is_method_handle_invoke_name(klass, name)) 391 return vmIntrinsics::_invokeGeneric; 392 } 393 return vmIntrinsics::_none; 394 } 395 396 397 // convert the external string or reflective type to an internal signature 398 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { 399 if (java_lang_invoke_MethodType::is_instance(type_str)) { 400 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL); 401 } else if (java_lang_Class::is_instance(type_str)) { 402 return java_lang_Class::as_signature(type_str, false, CHECK_NULL); 403 } else if (java_lang_String::is_instance(type_str)) { 404 if (intern_if_not_found) { 405 return java_lang_String::as_symbol(type_str, CHECK_NULL); 406 } else { 407 return java_lang_String::as_symbol_or_null(type_str); 408 } 409 } else { 410 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); 411 } 412 } 413 414 static const char OBJ_SIG[] = "Ljava/lang/Object;"; 415 enum { OBJ_SIG_LEN = 18 }; 416 417 bool MethodHandles::is_basic_type_signature(Symbol* sig) { 418 assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); 419 assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); 420 const int len = sig->utf8_length(); 421 for (int i = 0; i < len; i++) { 422 switch (sig->byte_at(i)) { 423 case 'L': 424 // only java/lang/Object is valid here 425 if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i) 426 return false; 427 i += OBJ_SIG_LEN-1; //-1 because of i++ in loop 428 continue; 429 case '(': case ')': case 'V': 430 case 'I': case 'J': case 'F': case 'D': 431 continue; 432 //case '[': 433 //case 'Z': case 'B': case 'C': case 'S': 434 default: 435 return false; 436 } 437 } 438 return true; 439 } 440 441 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) { 442 Symbol* bsig = NULL; 443 if (sig == NULL) { 444 return sig; 445 } else if (is_basic_type_signature(sig)) { 446 sig->increment_refcount(); 447 return sig; // that was easy 448 } else if (sig->byte_at(0) != '(') { 449 BasicType bt = char2type(sig->byte_at(0)); 450 if (is_subword_type(bt)) { 451 bsig = vmSymbols::int_signature(); 452 } else { 453 assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false"); 454 bsig = vmSymbols::object_signature(); 455 } 456 } else { 457 ResourceMark rm; 458 stringStream buffer(128); 459 buffer.put('('); 460 int arg_pos = 0, keep_arg_pos = -1; 461 if (keep_last_arg) 462 keep_arg_pos = ArgumentCount(sig).size() - 1; 463 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 464 BasicType bt = ss.type(); 465 size_t this_arg_pos = buffer.size(); 466 if (ss.at_return_type()) { 467 buffer.put(')'); 468 } 469 if (arg_pos == keep_arg_pos) { 470 buffer.write((char*) ss.raw_bytes(), 471 (int) ss.raw_length()); 472 } else if (bt == T_OBJECT || bt == T_ARRAY) { 473 buffer.write(OBJ_SIG, OBJ_SIG_LEN); 474 } else { 475 if (is_subword_type(bt)) 476 bt = T_INT; 477 buffer.put(type2char(bt)); 478 } 479 arg_pos++; 480 } 481 const char* sigstr = buffer.base(); 482 int siglen = (int) buffer.size(); 483 bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD); 484 } 485 assert(is_basic_type_signature(bsig) || 486 // detune assert in case the injected argument is not a basic type: 487 keep_last_arg, ""); 488 return bsig; 489 } 490 491 void MethodHandles::print_as_basic_type_signature_on(outputStream* st, 492 Symbol* sig, 493 bool keep_arrays, 494 bool keep_basic_names) { 495 st = st ? st : tty; 496 int len = sig->utf8_length(); 497 int array = 0; 498 bool prev_type = false; 499 for (int i = 0; i < len; i++) { 500 char ch = sig->byte_at(i); 501 switch (ch) { 502 case '(': case ')': 503 prev_type = false; 504 st->put(ch); 505 continue; 506 case '[': 507 if (!keep_basic_names && keep_arrays) 508 st->put(ch); 509 array++; 510 continue; 511 case 'L': 512 { 513 if (prev_type) st->put(','); 514 int start = i+1, slash = start; 515 while (++i < len && (ch = sig->byte_at(i)) != ';') { 516 if (ch == '/' || ch == '.' || ch == '$') slash = i+1; 517 } 518 if (slash < i) start = slash; 519 if (!keep_basic_names) { 520 st->put('L'); 521 } else { 522 for (int j = start; j < i; j++) 523 st->put(sig->byte_at(j)); 524 prev_type = true; 525 } 526 break; 527 } 528 default: 529 { 530 if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) { 531 ch = '['; 532 array = 0; 533 } 534 if (prev_type) st->put(','); 535 const char* n = NULL; 536 if (keep_basic_names) 537 n = type2name(char2type(ch)); 538 if (n == NULL) { 539 // unknown letter, or we don't want to know its name 540 st->put(ch); 541 } else { 542 st->print("%s", n); 543 prev_type = true; 544 } 545 break; 546 } 547 } 548 // Switch break goes here to take care of array suffix: 549 if (prev_type) { 550 while (array > 0) { 551 st->print("[]"); 552 --array; 553 } 554 } 555 array = 0; 556 } 557 } 558 559 560 561 static oop object_java_mirror() { 562 return SystemDictionary::Object_klass()->java_mirror(); 563 } 564 565 oop MethodHandles::field_name_or_null(Symbol* s) { 566 if (s == NULL) return NULL; 567 return StringTable::lookup(s); 568 } 569 570 oop MethodHandles::field_signature_type_or_null(Symbol* s) { 571 if (s == NULL) return NULL; 572 BasicType bt = FieldType::basic_type(s); 573 if (is_java_primitive(bt)) { 574 assert(s->utf8_length() == 1, ""); 575 return java_lang_Class::primitive_mirror(bt); 576 } 577 // Here are some more short cuts for common types. 578 // They are optional, since reference types can be resolved lazily. 579 if (bt == T_OBJECT) { 580 if (s == vmSymbols::object_signature()) { 581 return object_java_mirror(); 582 } else if (s == vmSymbols::class_signature()) { 583 return SystemDictionary::Class_klass()->java_mirror(); 584 } else if (s == vmSymbols::string_signature()) { 585 return SystemDictionary::String_klass()->java_mirror(); 586 } 587 } 588 return NULL; 589 } 590 591 592 // An unresolved member name is a mere symbolic reference. 593 // Resolving it plants a vmtarget/vmindex in it, 594 // which refers directly to JVM internals. 595 Handle MethodHandles::resolve_MemberName(Handle mname, KlassHandle caller, TRAPS) { 596 Handle empty; 597 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 598 599 if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) { 600 // Already resolved. 601 DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname())); 602 assert(vmindex >= Method::nonvirtual_vtable_index, ""); 603 return mname; 604 } 605 606 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); 607 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); 608 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); 609 int flags = java_lang_invoke_MemberName::flags(mname()); 610 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 611 if (!ref_kind_is_valid(ref_kind)) { 612 THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); 613 } 614 615 DEBUG_ONLY(int old_vmindex); 616 assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); 617 618 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { 619 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); 620 } 621 622 instanceKlassHandle defc; 623 { 624 Klass* defc_klass = java_lang_Class::as_Klass(defc_oop()); 625 if (defc_klass == NULL) return empty; // a primitive; no resolution possible 626 if (!defc_klass->oop_is_instance()) { 627 if (!defc_klass->oop_is_array()) return empty; 628 defc_klass = SystemDictionary::Object_klass(); 629 } 630 defc = instanceKlassHandle(THREAD, defc_klass); 631 } 632 if (defc.is_null()) { 633 THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); 634 } 635 defc->link_class(CHECK_(empty)); // possible safepoint 636 637 // convert the external string name to an internal symbol 638 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); 639 if (name == NULL) return empty; // no such name 640 if (name == vmSymbols::class_initializer_name()) 641 return empty; // illegal name 642 643 vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; 644 if ((flags & ALL_KINDS) == IS_METHOD && 645 (defc() == SystemDictionary::MethodHandle_klass()) && 646 (ref_kind == JVM_REF_invokeVirtual || 647 ref_kind == JVM_REF_invokeSpecial || 648 // static invocation mode is required for _linkToVirtual, etc.: 649 ref_kind == JVM_REF_invokeStatic)) { 650 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 651 if (iid != vmIntrinsics::_none && 652 ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { 653 // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. 654 // For a static reference it could an internal linkage routine like _linkToVirtual, etc. 655 mh_invoke_id = iid; 656 } 657 } 658 659 // convert the external string or reflective type to an internal signature 660 TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); 661 if (type == NULL) return empty; // no such signature exists in the VM 662 663 // Time to do the lookup. 664 switch (flags & ALL_KINDS) { 665 case IS_METHOD: 666 { 667 CallInfo result; 668 { 669 assert(!HAS_PENDING_EXCEPTION, ""); 670 if (ref_kind == JVM_REF_invokeStatic) { 671 LinkResolver::resolve_static_call(result, 672 defc, name, type, caller, caller.not_null(), false, THREAD); 673 } else if (ref_kind == JVM_REF_invokeInterface) { 674 LinkResolver::resolve_interface_call(result, Handle(), defc, 675 defc, name, type, caller, caller.not_null(), false, THREAD); 676 } else if (mh_invoke_id != vmIntrinsics::_none) { 677 assert(!is_signature_polymorphic_static(mh_invoke_id), ""); 678 LinkResolver::resolve_handle_call(result, 679 defc, name, type, caller, THREAD); 680 } else if (ref_kind == JVM_REF_invokeSpecial) { 681 LinkResolver::resolve_special_call(result, 682 defc, name, type, caller, caller.not_null(), THREAD); 683 } else if (ref_kind == JVM_REF_invokeVirtual) { 684 LinkResolver::resolve_virtual_call(result, Handle(), defc, 685 defc, name, type, caller, caller.not_null(), false, THREAD); 686 } else { 687 assert(false, err_msg("ref_kind=%d", ref_kind)); 688 } 689 if (HAS_PENDING_EXCEPTION) { 690 return empty; 691 } 692 } 693 if (result.resolved_appendix().not_null()) { 694 // The resolved MemberName must not be accompanied by an appendix argument, 695 // since there is no way to bind this value into the MemberName. 696 // Caller is responsible to prevent this from happening. 697 THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); 698 } 699 oop mname2 = init_method_MemberName(mname, result); 700 return Handle(THREAD, mname2); 701 } 702 case IS_CONSTRUCTOR: 703 { 704 CallInfo result; 705 { 706 assert(!HAS_PENDING_EXCEPTION, ""); 707 if (name == vmSymbols::object_initializer_name()) { 708 LinkResolver::resolve_special_call(result, 709 defc, name, type, caller, caller.not_null(), THREAD); 710 } else { 711 break; // will throw after end of switch 712 } 713 if (HAS_PENDING_EXCEPTION) { 714 return empty; 715 } 716 } 717 assert(result.is_statically_bound(), ""); 718 oop mname2 = init_method_MemberName(mname, result); 719 return Handle(THREAD, mname2); 720 } 721 case IS_FIELD: 722 { 723 fieldDescriptor result; // find_field initializes fd if found 724 { 725 assert(!HAS_PENDING_EXCEPTION, ""); 726 LinkResolver::resolve_field(result, defc, name, type, caller, Bytecodes::_nop, false, false, THREAD); 727 if (HAS_PENDING_EXCEPTION) { 728 return empty; 729 } 730 } 731 oop mname2 = init_field_MemberName(mname, result, ref_kind_is_setter(ref_kind)); 732 return Handle(THREAD, mname2); 733 } 734 default: 735 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); 736 } 737 738 return empty; 739 } 740 741 // Conversely, a member name which is only initialized from JVM internals 742 // may have null defc, name, and type fields. 743 // Resolving it plants a vmtarget/vmindex in it, 744 // which refers directly to JVM internals. 745 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { 746 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 747 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 748 int vmindex = java_lang_invoke_MemberName::vmindex(mname()); 749 if (vmtarget == NULL) { 750 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); 751 } 752 753 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); 754 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); 755 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); 756 int flags = java_lang_invoke_MemberName::flags(mname()); 757 758 if (suppress != 0) { 759 if (suppress & _suppress_defc) have_defc = true; 760 if (suppress & _suppress_name) have_name = true; 761 if (suppress & _suppress_type) have_type = true; 762 } 763 764 if (have_defc && have_name && have_type) return; // nothing needed 765 766 switch (flags & ALL_KINDS) { 767 case IS_METHOD: 768 case IS_CONSTRUCTOR: 769 { 770 assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); 771 methodHandle m(THREAD, (Method*)vmtarget); 772 DEBUG_ONLY(vmtarget = NULL); // safety 773 if (m.is_null()) break; 774 if (!have_defc) { 775 InstanceKlass* defc = m->method_holder(); 776 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 777 } 778 if (!have_name) { 779 //not java_lang_String::create_from_symbol; let's intern member names 780 Handle name = StringTable::intern(m->name(), CHECK); 781 java_lang_invoke_MemberName::set_name(mname(), name()); 782 } 783 if (!have_type) { 784 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); 785 java_lang_invoke_MemberName::set_type(mname(), type()); 786 } 787 return; 788 } 789 case IS_FIELD: 790 { 791 assert(vmtarget->is_klass(), "field vmtarget is Klass*"); 792 if (!((Klass*) vmtarget)->oop_is_instance()) break; 793 instanceKlassHandle defc(THREAD, (Klass*) vmtarget); 794 DEBUG_ONLY(vmtarget = NULL); // safety 795 bool is_static = ((flags & JVM_ACC_STATIC) != 0); 796 fieldDescriptor fd; // find_field initializes fd if found 797 if (!defc->find_field_from_offset(vmindex, is_static, &fd)) 798 break; // cannot expand 799 if (!have_defc) { 800 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 801 } 802 if (!have_name) { 803 //not java_lang_String::create_from_symbol; let's intern member names 804 Handle name = StringTable::intern(fd.name(), CHECK); 805 java_lang_invoke_MemberName::set_name(mname(), name()); 806 } 807 if (!have_type) { 808 // If it is a primitive field type, don't mess with short strings like "I". 809 Handle type = field_signature_type_or_null(fd.signature()); 810 if (type.is_null()) { 811 java_lang_String::create_from_symbol(fd.signature(), CHECK); 812 } 813 java_lang_invoke_MemberName::set_type(mname(), type()); 814 } 815 return; 816 } 817 } 818 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 819 } 820 821 int MethodHandles::find_MemberNames(KlassHandle k, 822 Symbol* name, Symbol* sig, 823 int mflags, KlassHandle caller, 824 int skip, objArrayHandle results) { 825 // %%% take caller into account! 826 827 Thread* thread = Thread::current(); 828 829 if (k.is_null() || !k->oop_is_instance()) return -1; 830 831 int rfill = 0, rlimit = results->length(), rskip = skip; 832 // overflow measurement: 833 int overflow = 0, overflow_limit = MAX2(1000, rlimit); 834 835 int match_flags = mflags; 836 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); 837 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); 838 bool local_only = !(search_superc | search_intfc); 839 bool classes_only = false; 840 841 if (name != NULL) { 842 if (name->utf8_length() == 0) return 0; // a match is not possible 843 } 844 if (sig != NULL) { 845 if (sig->utf8_length() == 0) return 0; // a match is not possible 846 if (sig->byte_at(0) == '(') 847 match_flags &= ~(IS_FIELD | IS_TYPE); 848 else 849 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); 850 } 851 852 if ((match_flags & IS_TYPE) != 0) { 853 // NYI, and Core Reflection works quite well for this query 854 } 855 856 if ((match_flags & IS_FIELD) != 0) { 857 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { 858 if (name != NULL && st.name() != name) 859 continue; 860 if (sig != NULL && st.signature() != sig) 861 continue; 862 // passed the filters 863 if (rskip > 0) { 864 --rskip; 865 } else if (rfill < rlimit) { 866 Handle result(thread, results->obj_at(rfill++)); 867 if (!java_lang_invoke_MemberName::is_instance(result())) 868 return -99; // caller bug! 869 oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor()); 870 if (saved != result()) 871 results->obj_at_put(rfill-1, saved); // show saved instance to user 872 } else if (++overflow >= overflow_limit) { 873 match_flags = 0; break; // got tired of looking at overflow 874 } 875 } 876 } 877 878 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { 879 // watch out for these guys: 880 Symbol* init_name = vmSymbols::object_initializer_name(); 881 Symbol* clinit_name = vmSymbols::class_initializer_name(); 882 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit> 883 bool negate_name_test = false; 884 // fix name so that it captures the intention of IS_CONSTRUCTOR 885 if (!(match_flags & IS_METHOD)) { 886 // constructors only 887 if (name == NULL) { 888 name = init_name; 889 } else if (name != init_name) { 890 return 0; // no constructors of this method name 891 } 892 } else if (!(match_flags & IS_CONSTRUCTOR)) { 893 // methods only 894 if (name == NULL) { 895 name = init_name; 896 negate_name_test = true; // if we see the name, we *omit* the entry 897 } else if (name == init_name) { 898 return 0; // no methods of this constructor name 899 } 900 } else { 901 // caller will accept either sort; no need to adjust name 902 } 903 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { 904 Method* m = st.method(); 905 Symbol* m_name = m->name(); 906 if (m_name == clinit_name) 907 continue; 908 if (name != NULL && ((m_name != name) ^ negate_name_test)) 909 continue; 910 if (sig != NULL && m->signature() != sig) 911 continue; 912 // passed the filters 913 if (rskip > 0) { 914 --rskip; 915 } else if (rfill < rlimit) { 916 Handle result(thread, results->obj_at(rfill++)); 917 if (!java_lang_invoke_MemberName::is_instance(result())) 918 return -99; // caller bug! 919 CallInfo info(m); 920 oop saved = MethodHandles::init_method_MemberName(result, info); 921 if (saved != result()) 922 results->obj_at_put(rfill-1, saved); // show saved instance to user 923 } else if (++overflow >= overflow_limit) { 924 match_flags = 0; break; // got tired of looking at overflow 925 } 926 } 927 } 928 929 // return number of elements we at leasted wanted to initialize 930 return rfill + overflow; 931 } 932 933 // 934 // Here are the native methods in java.lang.invoke.MethodHandleNatives 935 // They are the private interface between this JVM and the HotSpot-specific 936 // Java code that implements JSR 292 method handles. 937 // 938 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way 939 // that intrinsic (non-JNI) native methods are defined in HotSpot. 940 // 941 942 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { 943 switch (which) { 944 case MethodHandles::GC_COUNT_GWT: 945 #ifdef COMPILER2 946 return true; 947 #else 948 return false; 949 #endif 950 } 951 return 0; 952 } 953 JVM_END 954 955 #ifndef PRODUCT 956 #define EACH_NAMED_CON(template, requirement) \ 957 template(MethodHandles,GC_COUNT_GWT) \ 958 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ 959 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ 960 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ 961 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ 962 template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \ 963 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ 964 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ 965 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ 966 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ 967 template(MethodHandles,GC_LAMBDA_SUPPORT) \ 968 /*end*/ 969 970 #define IGNORE_REQ(req_expr) /* req_expr */ 971 #define ONE_PLUS(scope,value) 1+ 972 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; 973 #define VALUE_COMMA(scope,value) scope::value, 974 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; 975 #define STRING_NULL(scope,value) #value "\0" 976 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; 977 978 static bool advertise_con_value(int which) { 979 if (which < 0) return false; 980 bool ok = true; 981 int count = 0; 982 #define INC_COUNT(scope,value) \ 983 ++count; 984 #define CHECK_REQ(req_expr) \ 985 if (which < count) return ok; \ 986 ok = (req_expr); 987 EACH_NAMED_CON(INC_COUNT, CHECK_REQ); 988 #undef INC_COUNT 989 #undef CHECK_REQ 990 assert(count == con_value_count, ""); 991 if (which < count) return ok; 992 return false; 993 } 994 995 #undef ONE_PLUS 996 #undef VALUE_COMMA 997 #undef STRING_NULL 998 #undef EACH_NAMED_CON 999 #endif // PRODUCT 1000 1001 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { 1002 #ifndef PRODUCT 1003 if (advertise_con_value(which)) { 1004 assert(which >= 0 && which < con_value_count, ""); 1005 int con = con_values[which]; 1006 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); 1007 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { 1008 const char* str = &con_names[0]; 1009 for (int i = 0; i < which; i++) 1010 str += strlen(str) + 1; // skip name and null 1011 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint 1012 box->obj_at_put(0, name); 1013 } 1014 return con; 1015 } 1016 #endif 1017 return 0; 1018 } 1019 JVM_END 1020 1021 // void init(MemberName self, AccessibleObject ref) 1022 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { 1023 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1024 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1025 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1026 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1027 MethodHandles::init_MemberName(mname, target); 1028 } 1029 JVM_END 1030 1031 // void expand(MemberName self) 1032 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1033 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1034 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1035 MethodHandles::expand_MemberName(mname, 0, CHECK); 1036 } 1037 JVM_END 1038 1039 // void resolve(MemberName self, Class<?> caller) 1040 JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { 1041 if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } 1042 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1043 1044 // The trusted Java code that calls this method should already have performed 1045 // access checks on behalf of the given caller. But, we can verify this. 1046 if (VerifyMethodHandles && caller_jh != NULL && 1047 java_lang_invoke_MemberName::clazz(mname()) != NULL) { 1048 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); 1049 if (reference_klass != NULL && reference_klass->oop_is_objArray()) { 1050 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass(); 1051 } 1052 1053 // Reflection::verify_class_access can only handle instance classes. 1054 if (reference_klass != NULL && reference_klass->oop_is_instance()) { 1055 // Emulate LinkResolver::check_klass_accessability. 1056 Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); 1057 if (!Reflection::verify_class_access(caller, 1058 reference_klass, 1059 true)) { 1060 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name()); 1061 } 1062 } 1063 } 1064 1065 KlassHandle caller(THREAD, 1066 caller_jh == NULL ? (Klass*) NULL : 1067 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh))); 1068 Handle resolved = MethodHandles::resolve_MemberName(mname, caller, CHECK_NULL); 1069 1070 if (resolved.is_null()) { 1071 int flags = java_lang_invoke_MemberName::flags(mname()); 1072 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1073 if (!MethodHandles::ref_kind_is_valid(ref_kind)) { 1074 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); 1075 } 1076 if ((flags & ALL_KINDS) == IS_FIELD) { 1077 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed"); 1078 } else if ((flags & ALL_KINDS) == IS_METHOD || 1079 (flags & ALL_KINDS) == IS_CONSTRUCTOR) { 1080 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed"); 1081 } else { 1082 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); 1083 } 1084 } 1085 1086 return JNIHandles::make_local(THREAD, resolved()); 1087 } 1088 JVM_END 1089 1090 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { 1091 if (mname == NULL || 1092 java_lang_invoke_MemberName::vmtarget(mname) == NULL) { 1093 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); 1094 } else { 1095 int flags = java_lang_invoke_MemberName::flags(mname); 1096 if ((flags & IS_FIELD) != 0 && 1097 (must_be_static 1098 ? (flags & JVM_ACC_STATIC) != 0 1099 : (flags & JVM_ACC_STATIC) == 0)) { 1100 int vmindex = java_lang_invoke_MemberName::vmindex(mname); 1101 return (jlong) vmindex; 1102 } 1103 } 1104 const char* msg = (must_be_static ? "static field required" : "non-static field required"); 1105 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); 1106 return 0; 1107 } 1108 1109 JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1110 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); 1111 } 1112 JVM_END 1113 1114 JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1115 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); 1116 } 1117 JVM_END 1118 1119 JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1120 // use the other function to perform sanity checks: 1121 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); 1122 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); 1123 return JNIHandles::make_local(THREAD, clazz); 1124 } 1125 JVM_END 1126 1127 JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1128 if (mname_jh == NULL) return NULL; 1129 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1130 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); 1131 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 1132 objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); 1133 jvalue vmindex_value; vmindex_value.j = (long)vmindex; 1134 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); 1135 result->obj_at_put(0, x); 1136 x = NULL; 1137 if (vmtarget == NULL) { 1138 x = NULL; 1139 } else if (vmtarget->is_klass()) { 1140 x = ((Klass*) vmtarget)->java_mirror(); 1141 } else if (vmtarget->is_method()) { 1142 x = mname(); 1143 } 1144 result->obj_at_put(1, x); 1145 return JNIHandles::make_local(env, result()); 1146 } 1147 JVM_END 1148 1149 1150 1151 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 1152 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 1153 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, 1154 jclass clazz_jh, jstring name_jh, jstring sig_jh, 1155 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { 1156 if (clazz_jh == NULL || results_jh == NULL) return -1; 1157 KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz_jh))); 1158 1159 objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh)); 1160 if (results.is_null() || !results->is_objArray()) return -1; 1161 1162 TempNewSymbol name = NULL; 1163 TempNewSymbol sig = NULL; 1164 if (name_jh != NULL) { 1165 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); 1166 if (name == NULL) return 0; // a match is not possible 1167 } 1168 if (sig_jh != NULL) { 1169 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); 1170 if (sig == NULL) return 0; // a match is not possible 1171 } 1172 1173 KlassHandle caller; 1174 if (caller_jh != NULL) { 1175 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); 1176 if (!java_lang_Class::is_instance(caller_oop)) return -1; 1177 caller = KlassHandle(THREAD, java_lang_Class::as_Klass(caller_oop)); 1178 } 1179 1180 if (name != NULL && sig != NULL && results.not_null()) { 1181 // try a direct resolve 1182 // %%% TO DO 1183 } 1184 1185 int res = MethodHandles::find_MemberNames(k, name, sig, mflags, 1186 caller, skip, results); 1187 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 1188 return res; 1189 } 1190 JVM_END 1191 1192 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1193 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1194 Handle target (THREAD, JNIHandles::resolve(target_jh)); 1195 { 1196 // Walk all nmethods depending on this call site. 1197 MutexLocker mu(Compile_lock, thread); 1198 Universe::flush_dependents_on(call_site, target); 1199 java_lang_invoke_CallSite::set_target(call_site(), target()); 1200 } 1201 } 1202 JVM_END 1203 1204 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1205 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1206 Handle target (THREAD, JNIHandles::resolve(target_jh)); 1207 { 1208 // Walk all nmethods depending on this call site. 1209 MutexLocker mu(Compile_lock, thread); 1210 Universe::flush_dependents_on(call_site, target); 1211 java_lang_invoke_CallSite::set_target_volatile(call_site(), target()); 1212 } 1213 } 1214 JVM_END 1215 1216 /** 1217 * Throws a java/lang/UnsupportedOperationException unconditionally. 1218 * This is required by the specification of MethodHandle.invoke if 1219 * invoked directly. 1220 */ 1221 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { 1222 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively"); 1223 return NULL; 1224 } 1225 JVM_END 1226 1227 /** 1228 * Throws a java/lang/UnsupportedOperationException unconditionally. 1229 * This is required by the specification of MethodHandle.invokeExact if 1230 * invoked directly. 1231 */ 1232 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { 1233 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively"); 1234 return NULL; 1235 } 1236 JVM_END 1237 1238 /// JVM_RegisterMethodHandleMethods 1239 1240 #undef CS // Solaris builds complain 1241 1242 #define LANG "Ljava/lang/" 1243 #define JLINV "Ljava/lang/invoke/" 1244 1245 #define OBJ LANG"Object;" 1246 #define CLS LANG"Class;" 1247 #define STRG LANG"String;" 1248 #define CS JLINV"CallSite;" 1249 #define MT JLINV"MethodType;" 1250 #define MH JLINV"MethodHandle;" 1251 #define MEM JLINV"MemberName;" 1252 1253 #define CC (char*) /*cast a literal from (const char*)*/ 1254 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1255 1256 // These are the native methods on java.lang.invoke.MethodHandleNatives. 1257 static JNINativeMethod MHN_methods[] = { 1258 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 1259 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 1260 {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)}, 1261 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 1262 // static native int getNamedCon(int which, Object[] name) 1263 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 1264 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 1265 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 1266 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}, 1267 {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)}, 1268 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, 1269 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, 1270 {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)}, 1271 {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)}, 1272 {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)} 1273 }; 1274 1275 static JNINativeMethod MH_methods[] = { 1276 // UnsupportedOperationException throwers 1277 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, 1278 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} 1279 }; 1280 1281 /** 1282 * Helper method to register native methods. 1283 */ 1284 static bool register_natives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { 1285 int status = env->RegisterNatives(clazz, methods, nMethods); 1286 if (status != JNI_OK || env->ExceptionOccurred()) { 1287 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 1288 env->ExceptionClear(); 1289 return false; 1290 } 1291 return true; 1292 } 1293 1294 /** 1295 * This one function is exported, used by NativeLookup. 1296 */ 1297 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 1298 assert(!MethodHandles::enabled(), "must not be enabled"); 1299 bool enable_MH = true; 1300 1301 jclass MH_class = NULL; 1302 if (SystemDictionary::MethodHandle_klass() == NULL) { 1303 enable_MH = false; 1304 } else { 1305 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); 1306 MH_class = (jclass) JNIHandles::make_local(env, mirror); 1307 } 1308 1309 if (enable_MH) { 1310 ThreadToNativeFromVM ttnfv(thread); 1311 1312 if (enable_MH) { 1313 enable_MH = register_natives(env, MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod)); 1314 } 1315 if (enable_MH) { 1316 enable_MH = register_natives(env, MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod)); 1317 } 1318 } 1319 1320 if (TraceInvokeDynamic) { 1321 tty->print_cr("MethodHandle support loaded (using LambdaForms)"); 1322 } 1323 1324 if (enable_MH) { 1325 if (MethodHandles::generate_adapters() == false) { 1326 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for method handle adapters"); 1327 } 1328 MethodHandles::set_enabled(true); 1329 } 1330 } 1331 JVM_END