1 /* 2 * Copyright (c) 2008, 2024, 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 "cds/cds_globals.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/systemDictionaryShared.hpp" 32 #include "classfile/vmClasses.hpp" 33 #include "code/codeCache.hpp" 34 #include "code/dependencyContext.hpp" 35 #include "compiler/compileBroker.hpp" 36 #include "interpreter/interpreter.hpp" 37 #include "interpreter/oopMapCache.hpp" 38 #include "interpreter/linkResolver.hpp" 39 #include "jvm_io.h" 40 #include "logging/log.hpp" 41 #include "logging/logStream.hpp" 42 #include "memory/allocation.inline.hpp" 43 #include "memory/oopFactory.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/klass.inline.hpp" 47 #include "oops/objArrayKlass.hpp" 48 #include "oops/objArrayOop.inline.hpp" 49 #include "oops/oop.inline.hpp" 50 #include "oops/typeArrayOop.inline.hpp" 51 #include "prims/methodHandles.hpp" 52 #include "runtime/deoptimization.hpp" 53 #include "runtime/fieldDescriptor.inline.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "runtime/interfaceSupport.inline.hpp" 56 #include "runtime/java.hpp" 57 #include "runtime/javaCalls.hpp" 58 #include "runtime/jniHandles.inline.hpp" 59 #include "runtime/perfData.inline.hpp" 60 #include "runtime/timerTrace.hpp" 61 #include "runtime/reflection.hpp" 62 #include "runtime/safepointVerifiers.hpp" 63 #include "runtime/signature.hpp" 64 #include "runtime/stubRoutines.hpp" 65 #include "sanitizers/leak.hpp" 66 #include "services/management.hpp" 67 #include "utilities/exceptions.hpp" 68 69 70 /* 71 * JSR 292 reference implementation: method handles 72 * The JDK 7 reference implementation represented method handle 73 * combinations as chains. Each link in the chain had a "vmentry" 74 * field which pointed at a bit of assembly code which performed 75 * one transformation before dispatching to the next link in the chain. 76 * 77 * The current reference implementation pushes almost all code generation 78 * responsibility to (trusted) Java code. A method handle contains a 79 * pointer to its "LambdaForm", which embodies all details of the method 80 * handle's behavior. The LambdaForm is a normal Java object, managed 81 * by a runtime coded in Java. 82 */ 83 84 bool MethodHandles::_enabled = false; // set true after successful native linkage 85 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = nullptr; 86 87 /** 88 * Generates method handle adapters. Returns 'false' if memory allocation 89 * failed and true otherwise. 90 */ 91 void MethodHandles::generate_adapters() { 92 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present"); 93 assert(_adapter_code == nullptr, "generate only once"); 94 95 ResourceMark rm; 96 TraceTime timer("MethodHandles adapters generation", TRACETIME_LOG(Info, startuptime)); 97 // The adapter entry is required to be aligned to CodeEntryAlignment. 98 // So we need additional bytes due to alignment. 99 int adapter_num = (int)Interpreter::method_handle_invoke_LAST - (int)Interpreter::method_handle_invoke_FIRST + 1; 100 int max_aligned_bytes = adapter_num * CodeEntryAlignment; 101 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size + max_aligned_bytes); 102 CodeBuffer code(_adapter_code); 103 MethodHandlesAdapterGenerator g(&code); 104 g.generate(); 105 code.log_section_sizes("MethodHandlesAdapterBlob"); 106 } 107 108 //------------------------------------------------------------------------------ 109 // MethodHandlesAdapterGenerator::generate 110 // 111 void MethodHandlesAdapterGenerator::generate() { 112 // Generate generic method handle adapters. 113 // Generate interpreter entries 114 for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; 115 mk <= Interpreter::method_handle_invoke_LAST; 116 mk = Interpreter::MethodKind(1 + (int)mk)) { 117 vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); 118 StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); 119 address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); 120 if (entry != nullptr) { 121 Interpreter::set_entry_for_kind(mk, entry); 122 } 123 // If the entry is not set, it will throw AbstractMethodError. 124 } 125 } 126 127 void MethodHandles::set_enabled(bool z) { 128 if (_enabled != z) { 129 guarantee(z, "can only enable once"); 130 _enabled = z; 131 } 132 } 133 134 // MemberName support 135 136 // import java_lang_invoke_MemberName.* 137 enum { 138 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, 139 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, 140 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, 141 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, 142 CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE, 143 TRUSTED_FINAL = java_lang_invoke_MemberName::MN_TRUSTED_FINAL, 144 HIDDEN_MEMBER = java_lang_invoke_MemberName::MN_HIDDEN_MEMBER, 145 REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, 146 REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, 147 LM_UNCONDITIONAL = java_lang_invoke_MemberName::MN_UNCONDITIONAL_MODE, 148 LM_MODULE = java_lang_invoke_MemberName::MN_MODULE_MODE, 149 LM_TRUSTED = java_lang_invoke_MemberName::MN_TRUSTED_MODE, 150 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE 151 }; 152 153 int MethodHandles::ref_kind_to_flags(int ref_kind) { 154 assert(ref_kind_is_valid(ref_kind), "%d", ref_kind); 155 int flags = (ref_kind << REFERENCE_KIND_SHIFT); 156 if (ref_kind_is_field(ref_kind)) { 157 flags |= IS_FIELD; 158 } else if (ref_kind_is_method(ref_kind)) { 159 flags |= IS_METHOD; 160 } else if (ref_kind == JVM_REF_newInvokeSpecial) { 161 flags |= IS_CONSTRUCTOR; 162 } 163 return flags; 164 } 165 166 Handle MethodHandles::resolve_MemberName_type(Handle mname, Klass* caller, TRAPS) { 167 Handle empty; 168 Handle type(THREAD, java_lang_invoke_MemberName::type(mname())); 169 if (!java_lang_String::is_instance(type())) { 170 return type; // already resolved 171 } 172 Symbol* signature = java_lang_String::as_symbol_or_null(type()); 173 if (signature == nullptr) { 174 return empty; // no such signature exists in the VM 175 } 176 Handle resolved; 177 int flags = java_lang_invoke_MemberName::flags(mname()); 178 switch (flags & ALL_KINDS) { 179 case IS_METHOD: 180 case IS_CONSTRUCTOR: 181 resolved = SystemDictionary::find_method_handle_type(signature, caller, CHECK_(empty)); 182 break; 183 case IS_FIELD: 184 resolved = SystemDictionary::find_field_handle_type(signature, caller, CHECK_(empty)); 185 break; 186 default: 187 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); 188 } 189 if (resolved.is_null()) { 190 THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MemberName type", empty); 191 } 192 return resolved; 193 } 194 195 oop MethodHandles::init_MemberName(Handle mname, Handle target, TRAPS) { 196 // This method is used from java.lang.invoke.MemberName constructors. 197 // It fills in the new MemberName from a java.lang.reflect.Member. 198 oop target_oop = target(); 199 Klass* target_klass = target_oop->klass(); 200 if (target_klass == vmClasses::reflect_Field_klass()) { 201 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 202 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 203 Klass* k = java_lang_Class::as_Klass(clazz); 204 if (k != nullptr && k->is_instance_klass()) { 205 fieldDescriptor fd(InstanceKlass::cast(k), slot); 206 oop mname2 = init_field_MemberName(mname, fd); 207 if (mname2 != nullptr) { 208 // Since we have the reified name and type handy, add them to the result. 209 if (java_lang_invoke_MemberName::name(mname2) == nullptr) 210 java_lang_invoke_MemberName::set_name(mname2, java_lang_reflect_Field::name(target_oop)); 211 if (java_lang_invoke_MemberName::type(mname2) == nullptr) 212 java_lang_invoke_MemberName::set_type(mname2, java_lang_reflect_Field::type(target_oop)); 213 } 214 return mname2; 215 } 216 } else if (target_klass == vmClasses::reflect_Method_klass()) { 217 oop clazz = java_lang_reflect_Method::clazz(target_oop); 218 int slot = java_lang_reflect_Method::slot(target_oop); 219 Klass* k = java_lang_Class::as_Klass(clazz); 220 if (k != nullptr && k->is_instance_klass()) { 221 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); 222 if (m == nullptr || is_signature_polymorphic(m->intrinsic_id())) 223 return nullptr; // do not resolve unless there is a concrete signature 224 CallInfo info(m, k, CHECK_NULL); 225 return init_method_MemberName(mname, info); 226 } 227 } else if (target_klass == vmClasses::reflect_Constructor_klass()) { 228 oop clazz = java_lang_reflect_Constructor::clazz(target_oop); 229 int slot = java_lang_reflect_Constructor::slot(target_oop); 230 Klass* k = java_lang_Class::as_Klass(clazz); 231 if (k != nullptr && k->is_instance_klass()) { 232 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); 233 if (m == nullptr) return nullptr; 234 CallInfo info(m, k, CHECK_NULL); 235 return init_method_MemberName(mname, info); 236 } 237 } 238 return nullptr; 239 } 240 241 oop MethodHandles::init_method_MemberName(Handle mname, CallInfo& info) { 242 assert(info.resolved_appendix().is_null(), "only normal methods here"); 243 methodHandle m(Thread::current(), info.resolved_method()); 244 assert(m.not_null(), "null method handle"); 245 InstanceKlass* m_klass = m->method_holder(); 246 assert(m_klass != nullptr, "null holder for method handle"); 247 int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); 248 int vmindex = Method::invalid_vtable_index; 249 LogTarget(Debug, methodhandles, indy) lt_indy; 250 251 switch (info.call_kind()) { 252 case CallInfo::itable_call: 253 vmindex = info.itable_index(); 254 // More importantly, the itable index only works with the method holder. 255 assert(m_klass->verify_itable_index(vmindex), ""); 256 flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); 257 if (lt_indy.is_enabled()) { 258 ResourceMark rm; 259 LogStream ls(lt_indy); 260 ls.print_cr("memberName: invokeinterface method_holder::method: %s, itableindex: %d, access_flags:", 261 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), 262 vmindex); 263 m->access_flags().print_on(&ls); 264 if (!m->is_abstract()) { 265 if (!m->is_private()) { 266 ls.print("default"); 267 } 268 else { 269 ls.print("private-intf"); 270 } 271 } 272 ls.cr(); 273 } 274 break; 275 276 case CallInfo::vtable_call: 277 vmindex = info.vtable_index(); 278 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 279 assert(info.resolved_klass()->is_subtype_of(m_klass), "virtual call must be type-safe"); 280 if (m_klass->is_interface()) { 281 // This is a vtable call to an interface method (abstract "miranda method" or default method). 282 // The vtable index is meaningless without a class (not interface) receiver type, so get one. 283 // (LinkResolver should help us figure this out.) 284 assert(info.resolved_klass()->is_instance_klass(), "subtype of interface must be an instance klass"); 285 InstanceKlass* m_klass_non_interface = InstanceKlass::cast(info.resolved_klass()); 286 if (m_klass_non_interface->is_interface()) { 287 m_klass_non_interface = vmClasses::Object_klass(); 288 #ifdef ASSERT 289 { ResourceMark rm; 290 Method* m2 = m_klass_non_interface->vtable().method_at(vmindex); 291 assert(m->name() == m2->name() && m->signature() == m2->signature(), 292 "at %d, %s != %s", vmindex, 293 m->name_and_sig_as_C_string(), m2->name_and_sig_as_C_string()); 294 } 295 #endif //ASSERT 296 } 297 if (!m->is_public()) { 298 assert(m->is_public(), "virtual call must be to public interface method"); 299 return nullptr; // elicit an error later in product build 300 } 301 assert(info.resolved_klass()->is_subtype_of(m_klass_non_interface), "virtual call must be type-safe"); 302 m_klass = m_klass_non_interface; 303 } 304 if (lt_indy.is_enabled()) { 305 ResourceMark rm; 306 LogStream ls(lt_indy); 307 ls.print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:", 308 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), 309 m_klass->internal_name(), vmindex); 310 m->access_flags().print_on(&ls); 311 if (m->is_default_method()) { 312 ls.print("default"); 313 } 314 ls.cr(); 315 } 316 break; 317 318 case CallInfo::direct_call: 319 vmindex = Method::nonvirtual_vtable_index; 320 if (m->is_static()) { 321 assert(!m->is_static_initializer(), "Cannot be static initializer"); 322 flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); 323 } else if (m->is_object_initializer()) { 324 flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 325 } else { 326 // "special" reflects that this is a direct call, not that it 327 // necessarily originates from an invokespecial. We can also do 328 // direct calls for private and/or final non-static methods. 329 flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 330 } 331 break; 332 333 default: assert(false, "bad CallInfo"); return nullptr; 334 } 335 336 // @CallerSensitive annotation detected 337 if (m->caller_sensitive()) { 338 flags |= CALLER_SENSITIVE; 339 } 340 341 Handle resolved_method = info.resolved_method_name(); 342 assert(java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method()) == m() || m->is_old(), 343 "Should not change after link resolution"); 344 345 oop mname_oop = mname(); 346 java_lang_invoke_MemberName::set_flags (mname_oop, flags); 347 java_lang_invoke_MemberName::set_method (mname_oop, resolved_method()); 348 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); // vtable/itable index 349 java_lang_invoke_MemberName::set_clazz (mname_oop, m_klass->java_mirror()); 350 // Note: name and type can be lazily computed by resolve_MemberName, 351 // if Java code needs them as resolved String and MethodType objects. 352 // If relevant, the vtable or itable value is stored as vmindex. 353 // This is done eagerly, since it is readily available without 354 // constructing any new objects. 355 return mname(); 356 } 357 358 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) { 359 InstanceKlass* ik = fd.field_holder(); 360 int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); 361 flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); 362 if (fd.is_trusted_final()) flags |= TRUSTED_FINAL; 363 if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); 364 int vmindex = fd.offset(); // determines the field uniquely when combined with static bit 365 366 oop mname_oop = mname(); 367 java_lang_invoke_MemberName::set_flags (mname_oop, flags); 368 java_lang_invoke_MemberName::set_method (mname_oop, nullptr); 369 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 370 java_lang_invoke_MemberName::set_clazz (mname_oop, ik->java_mirror()); 371 372 oop type = field_signature_type_or_null(fd.signature()); 373 oop name = field_name_or_null(fd.name()); 374 if (name != nullptr) 375 java_lang_invoke_MemberName::set_name(mname_oop, name); 376 if (type != nullptr) 377 java_lang_invoke_MemberName::set_type(mname_oop, type); 378 // Note: name and type can be lazily computed by resolve_MemberName, 379 // if Java code needs them as resolved String and Class objects. 380 // Note that the incoming type oop might be pre-resolved (non-null). 381 // The base clazz and field offset (vmindex) must be eagerly stored, 382 // because they unambiguously identify the field. 383 // Although the fieldDescriptor::_index would also identify the field, 384 // we do not use it, because it is harder to decode. 385 // TO DO: maybe intern mname_oop 386 return mname(); 387 } 388 389 // JVM 2.9 Special Methods: 390 // A method is signature polymorphic if and only if all of the following conditions hold : 391 // * It is declared in the java.lang.invoke.MethodHandle/VarHandle classes. 392 // * It has a single formal parameter of type Object[]. 393 // * It has a return type of Object for a polymorphic return type, otherwise a fixed return type. 394 // * It has the ACC_VARARGS and ACC_NATIVE flags set. 395 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) { 396 if (klass == nullptr) 397 return false; 398 // The following test will fail spuriously during bootstrap of MethodHandle itself: 399 // if (klass != vmClasses::MethodHandle_klass()) 400 // Test the name instead: 401 if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle() && 402 klass->name() != vmSymbols::java_lang_invoke_VarHandle()) { 403 return false; 404 } 405 406 // Look up signature polymorphic method with polymorphic return type 407 Symbol* poly_sig = vmSymbols::object_array_object_signature(); 408 InstanceKlass* iklass = InstanceKlass::cast(klass); 409 Method* m = iklass->find_method(name, poly_sig); 410 if (m != nullptr) { 411 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; 412 int flags = m->access_flags().as_int(); 413 if ((flags & required) == required) { 414 return true; 415 } 416 } 417 418 // Look up signature polymorphic method with non-polymorphic (non Object) return type 419 int me; 420 int ms = iklass->find_method_by_name(name, &me); 421 if (ms == -1) return false; 422 for (; ms < me; ms++) { 423 Method* m = iklass->methods()->at(ms); 424 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; 425 int flags = m->access_flags().as_int(); 426 if ((flags & required) == required && ArgumentCount(m->signature()).size() == 1) { 427 return true; 428 } 429 } 430 return false; 431 } 432 433 434 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { 435 assert(is_signature_polymorphic_intrinsic(iid), "%d %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 436 switch (iid) { 437 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); 438 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); 439 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); 440 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); 441 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); 442 case vmIntrinsics::_linkToNative: return vmSymbols::linkToNative_name(); 443 default: 444 fatal("unexpected intrinsic id: %d %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 445 } 446 } 447 448 Bytecodes::Code MethodHandles::signature_polymorphic_intrinsic_bytecode(vmIntrinsics::ID id) { 449 switch(id) { 450 case vmIntrinsics::_linkToVirtual: return Bytecodes::_invokevirtual; 451 case vmIntrinsics::_linkToInterface: return Bytecodes::_invokeinterface; 452 case vmIntrinsics::_linkToStatic: return Bytecodes::_invokestatic; 453 case vmIntrinsics::_linkToSpecial: return Bytecodes::_invokespecial; 454 case vmIntrinsics::_invokeBasic: return Bytecodes::_invokehandle; 455 default: 456 fatal("unexpected id: (%d) %s", (uint)id, vmIntrinsics::name_at(id)); 457 } 458 } 459 460 int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { 461 switch (iid) { 462 case vmIntrinsics::_invokeBasic: return 0; 463 case vmIntrinsics::_linkToNative: return 0; 464 case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; 465 case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; 466 case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; 467 case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; 468 default: 469 fatal("unexpected intrinsic id: %d %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 470 } 471 } 472 473 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { 474 vmSymbolID name_id = vmSymbols::find_sid(name); 475 switch (name_id) { 476 // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. 477 case VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; 478 // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: 479 case VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; 480 481 // There is one static signature-polymorphic method for each JVM invocation mode. 482 case VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; 483 case VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; 484 case VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; 485 case VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; 486 case VM_SYMBOL_ENUM_NAME(linkToNative_name): return vmIntrinsics::_linkToNative; 487 default: break; 488 } 489 490 // Cover the case of invokeExact and any future variants of invokeFoo. 491 Klass* mh_klass = vmClasses::klass_at(VM_CLASS_ID(MethodHandle_klass)); 492 if (mh_klass != nullptr && is_method_handle_invoke_name(mh_klass, name)) { 493 return vmIntrinsics::_invokeGeneric; 494 } 495 496 // Cover the case of methods on VarHandle. 497 Klass* vh_klass = vmClasses::klass_at(VM_CLASS_ID(VarHandle_klass)); 498 if (vh_klass != nullptr && is_method_handle_invoke_name(vh_klass, name)) { 499 return vmIntrinsics::_invokeGeneric; 500 } 501 502 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. 503 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. 504 return vmIntrinsics::_none; 505 } 506 507 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) { 508 if (klass != nullptr && 509 (klass->name() == vmSymbols::java_lang_invoke_MethodHandle() || 510 klass->name() == vmSymbols::java_lang_invoke_VarHandle())) { 511 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 512 if (iid != vmIntrinsics::_none) 513 return iid; 514 if (is_method_handle_invoke_name(klass, name)) 515 return vmIntrinsics::_invokeGeneric; 516 } 517 return vmIntrinsics::_none; 518 } 519 520 // Returns true if method is signature polymorphic and public 521 bool MethodHandles::is_signature_polymorphic_public_name(Klass* klass, Symbol* name) { 522 if (is_signature_polymorphic_name(klass, name)) { 523 InstanceKlass* iklass = InstanceKlass::cast(klass); 524 int me; 525 int ms = iklass->find_method_by_name(name, &me); 526 assert(ms != -1, ""); 527 for (; ms < me; ms++) { 528 Method* m = iklass->methods()->at(ms); 529 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS | JVM_ACC_PUBLIC; 530 int flags = m->access_flags().as_int(); 531 if ((flags & required) == required && ArgumentCount(m->signature()).size() == 1) { 532 return true; 533 } 534 } 535 } 536 return false; 537 } 538 539 // convert the external string or reflective type to an internal signature 540 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { 541 if (java_lang_invoke_MethodType::is_instance(type_str)) { 542 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found); 543 } else if (java_lang_Class::is_instance(type_str)) { 544 return java_lang_Class::as_signature(type_str, false); 545 } else if (java_lang_String::is_instance(type_str)) { 546 if (intern_if_not_found) { 547 return java_lang_String::as_symbol(type_str); 548 } else { 549 return java_lang_String::as_symbol_or_null(type_str); 550 } 551 } else { 552 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", nullptr); 553 } 554 } 555 556 static const char OBJ_SIG[] = "Ljava/lang/Object;"; 557 enum { OBJ_SIG_LEN = 18 }; 558 559 bool MethodHandles::is_basic_type_signature(Symbol* sig) { 560 assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); 561 assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); 562 for (SignatureStream ss(sig, sig->starts_with(JVM_SIGNATURE_FUNC)); !ss.is_done(); ss.next()) { 563 switch (ss.type()) { 564 case T_OBJECT: 565 // only java/lang/Object is valid here 566 if (strncmp((char*) ss.raw_bytes(), OBJ_SIG, OBJ_SIG_LEN) != 0) 567 return false; 568 break; 569 case T_VOID: 570 case T_INT: 571 case T_LONG: 572 case T_FLOAT: 573 case T_DOUBLE: 574 break; 575 default: 576 // subword types (T_BYTE etc.), Q-descriptors, arrays 577 return false; 578 } 579 } 580 return true; 581 } 582 583 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg) { 584 Symbol* bsig = nullptr; 585 if (sig == nullptr) { 586 return sig; 587 } else if (is_basic_type_signature(sig)) { 588 sig->increment_refcount(); 589 return sig; // that was easy 590 } else if (!sig->starts_with(JVM_SIGNATURE_FUNC)) { 591 BasicType bt = Signature::basic_type(sig); 592 if (is_subword_type(bt)) { 593 bsig = vmSymbols::int_signature(); 594 } else { 595 assert(is_reference_type(bt), "is_basic_type_signature was false"); 596 bsig = vmSymbols::object_signature(); 597 } 598 } else { 599 ResourceMark rm; 600 stringStream buffer(128); 601 buffer.put(JVM_SIGNATURE_FUNC); 602 int arg_pos = 0, keep_arg_pos = -1; 603 if (keep_last_arg) 604 keep_arg_pos = ArgumentCount(sig).size() - 1; 605 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 606 BasicType bt = ss.type(); 607 size_t this_arg_pos = buffer.size(); 608 if (ss.at_return_type()) { 609 buffer.put(JVM_SIGNATURE_ENDFUNC); 610 } 611 if (arg_pos == keep_arg_pos) { 612 buffer.write((char*) ss.raw_bytes(), 613 (int) ss.raw_length()); 614 } else if (is_reference_type(bt)) { 615 buffer.write(OBJ_SIG, OBJ_SIG_LEN); 616 } else { 617 if (is_subword_type(bt)) 618 bt = T_INT; 619 buffer.put(type2char(bt)); 620 } 621 arg_pos++; 622 } 623 const char* sigstr = buffer.base(); 624 int siglen = (int) buffer.size(); 625 bsig = SymbolTable::new_symbol(sigstr, siglen); 626 } 627 assert(is_basic_type_signature(bsig) || 628 // detune assert in case the injected argument is not a basic type: 629 keep_last_arg, ""); 630 return bsig; 631 } 632 633 void MethodHandles::print_as_basic_type_signature_on(outputStream* st, 634 Symbol* sig) { 635 st = st ? st : tty; 636 bool prev_type = false; 637 bool is_method = (sig->char_at(0) == JVM_SIGNATURE_FUNC); 638 if (is_method) st->put(JVM_SIGNATURE_FUNC); 639 for (SignatureStream ss(sig, is_method); !ss.is_done(); ss.next()) { 640 if (ss.at_return_type()) 641 st->put(JVM_SIGNATURE_ENDFUNC); 642 else if (prev_type) 643 st->put(','); 644 const char* cp = (const char*) ss.raw_bytes(); 645 if (ss.is_array()) { 646 st->put(JVM_SIGNATURE_ARRAY); 647 if (ss.array_prefix_length() == 1) 648 st->put(cp[1]); 649 else 650 st->put(JVM_SIGNATURE_CLASS); 651 } else { 652 st->put(cp[0]); 653 } 654 } 655 } 656 657 658 659 static oop object_java_mirror() { 660 return vmClasses::Object_klass()->java_mirror(); 661 } 662 663 oop MethodHandles::field_name_or_null(Symbol* s) { 664 if (s == nullptr) return nullptr; 665 return StringTable::lookup(s); 666 } 667 668 oop MethodHandles::field_signature_type_or_null(Symbol* s) { 669 if (s == nullptr) return nullptr; 670 BasicType bt = Signature::basic_type(s); 671 if (is_java_primitive(bt)) { 672 assert(s->utf8_length() == 1, ""); 673 return java_lang_Class::primitive_mirror(bt); 674 } 675 // Here are some more short cuts for common types. 676 // They are optional, since reference types can be resolved lazily. 677 if (bt == T_OBJECT) { 678 if (s == vmSymbols::object_signature()) { 679 return object_java_mirror(); 680 } else if (s == vmSymbols::class_signature()) { 681 return vmClasses::Class_klass()->java_mirror(); 682 } else if (s == vmSymbols::string_signature()) { 683 return vmClasses::String_klass()->java_mirror(); 684 } 685 } 686 return nullptr; 687 } 688 689 // An unresolved member name is a mere symbolic reference. 690 // Resolving it plants a vmtarget/vmindex in it, 691 // which refers directly to JVM internals. 692 Handle MethodHandles::resolve_MemberName(Handle mname, Klass* caller, int lookup_mode, 693 bool speculative_resolve, TRAPS) { 694 Handle empty; 695 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 696 697 if (java_lang_invoke_MemberName::vmtarget(mname()) != nullptr) { 698 // Already resolved. 699 DEBUG_ONLY(intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname())); 700 assert(vmindex >= Method::nonvirtual_vtable_index, ""); 701 return mname; 702 } 703 704 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); 705 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); 706 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); 707 int flags = java_lang_invoke_MemberName::flags(mname()); 708 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 709 if (!ref_kind_is_valid(ref_kind)) { 710 THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); 711 } 712 713 DEBUG_ONLY(intptr_t old_vmindex); 714 assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); 715 716 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { 717 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); 718 } 719 720 InstanceKlass* defc = nullptr; 721 { 722 Klass* defc_klass = java_lang_Class::as_Klass(defc_oop()); 723 if (defc_klass == nullptr) return empty; // a primitive; no resolution possible 724 if (!defc_klass->is_instance_klass()) { 725 if (!defc_klass->is_array_klass()) return empty; 726 defc_klass = vmClasses::Object_klass(); 727 } 728 defc = InstanceKlass::cast(defc_klass); 729 } 730 if (defc == nullptr) { 731 THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); 732 } 733 defc->link_class(CHECK_(empty)); // possible safepoint 734 735 // convert the external string name to an internal symbol 736 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); 737 if (name == nullptr) return empty; // no such name 738 if (name == vmSymbols::class_initializer_name()) 739 return empty; // illegal name 740 741 vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; 742 if ((flags & ALL_KINDS) == IS_METHOD && 743 (defc == vmClasses::MethodHandle_klass() || defc == vmClasses::VarHandle_klass()) && 744 (ref_kind == JVM_REF_invokeVirtual || 745 ref_kind == JVM_REF_invokeSpecial || 746 // static invocation mode is required for _linkToVirtual, etc.: 747 ref_kind == JVM_REF_invokeStatic)) { 748 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 749 if (iid != vmIntrinsics::_none && 750 ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { 751 // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. 752 // For a static reference it could an internal linkage routine like _linkToVirtual, etc. 753 mh_invoke_id = iid; 754 } 755 } 756 757 // convert the external string or reflective type to an internal signature 758 TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); 759 if (type == nullptr) return empty; // no such signature exists in the VM 760 761 // skip access check if it's trusted lookup 762 LinkInfo::AccessCheck access_check = caller != nullptr ? 763 LinkInfo::AccessCheck::required : 764 LinkInfo::AccessCheck::skip; 765 // skip loader constraints if it's trusted lookup or a public lookup 766 LinkInfo::LoaderConstraintCheck loader_constraint_check = (caller != nullptr && (lookup_mode & LM_UNCONDITIONAL) == 0) ? 767 LinkInfo::LoaderConstraintCheck::required : 768 LinkInfo::LoaderConstraintCheck::skip; 769 770 // Time to do the lookup. 771 switch (flags & ALL_KINDS) { 772 case IS_METHOD: 773 { 774 CallInfo result; 775 LinkInfo link_info(defc, name, type, caller, access_check, loader_constraint_check); 776 { 777 assert(!HAS_PENDING_EXCEPTION, ""); 778 if (ref_kind == JVM_REF_invokeStatic) { 779 LinkResolver::resolve_static_call(result, 780 link_info, false, THREAD); 781 } else if (ref_kind == JVM_REF_invokeInterface) { 782 LinkResolver::resolve_interface_call(result, Handle(), defc, 783 link_info, false, THREAD); 784 } else if (mh_invoke_id != vmIntrinsics::_none) { 785 assert(!is_signature_polymorphic_static(mh_invoke_id), ""); 786 LinkResolver::resolve_handle_call(result, link_info, THREAD); 787 } else if (ref_kind == JVM_REF_invokeSpecial) { 788 LinkResolver::resolve_special_call(result, Handle(), 789 link_info, THREAD); 790 } else if (ref_kind == JVM_REF_invokeVirtual) { 791 LinkResolver::resolve_virtual_call(result, Handle(), defc, 792 link_info, false, THREAD); 793 } else { 794 assert(false, "ref_kind=%d", ref_kind); 795 } 796 if (HAS_PENDING_EXCEPTION) { 797 if (speculative_resolve) { 798 CLEAR_PENDING_EXCEPTION; 799 } 800 return empty; 801 } 802 } 803 if (result.resolved_appendix().not_null()) { 804 // The resolved MemberName must not be accompanied by an appendix argument, 805 // since there is no way to bind this value into the MemberName. 806 // Caller is responsible to prevent this from happening. 807 THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); 808 } 809 result.set_resolved_method_name(CHECK_(empty)); 810 oop mname2 = init_method_MemberName(mname, result); 811 return Handle(THREAD, mname2); 812 } 813 case IS_CONSTRUCTOR: 814 { 815 CallInfo result; 816 LinkInfo link_info(defc, name, type, caller, access_check, loader_constraint_check); 817 { 818 assert(!HAS_PENDING_EXCEPTION, ""); 819 if (name == vmSymbols::object_initializer_name()) { 820 LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD); 821 } else { 822 break; // will throw after end of switch 823 } 824 if (HAS_PENDING_EXCEPTION) { 825 if (speculative_resolve) { 826 CLEAR_PENDING_EXCEPTION; 827 } 828 return empty; 829 } 830 } 831 assert(result.is_statically_bound(), ""); 832 result.set_resolved_method_name(CHECK_(empty)); 833 oop mname2 = init_method_MemberName(mname, result); 834 return Handle(THREAD, mname2); 835 } 836 case IS_FIELD: 837 { 838 fieldDescriptor result; // find_field initializes fd if found 839 { 840 assert(!HAS_PENDING_EXCEPTION, ""); 841 LinkInfo link_info(defc, name, type, caller, LinkInfo::AccessCheck::skip, loader_constraint_check); 842 LinkResolver::resolve_field(result, link_info, Bytecodes::_nop, false, THREAD); 843 if (HAS_PENDING_EXCEPTION) { 844 if (speculative_resolve) { 845 CLEAR_PENDING_EXCEPTION; 846 } 847 return empty; 848 } 849 } 850 oop mname2 = init_field_MemberName(mname, result, ref_kind_is_setter(ref_kind)); 851 return Handle(THREAD, mname2); 852 } 853 default: 854 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); 855 } 856 857 return empty; 858 } 859 860 // Conversely, a member name which is only initialized from JVM internals 861 // may have null defc, name, and type fields. 862 // Resolving it plants a vmtarget/vmindex in it, 863 // which refers directly to JVM internals. 864 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { 865 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 866 867 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != nullptr); 868 bool have_name = (java_lang_invoke_MemberName::name(mname()) != nullptr); 869 bool have_type = (java_lang_invoke_MemberName::type(mname()) != nullptr); 870 int flags = java_lang_invoke_MemberName::flags(mname()); 871 872 if (suppress != 0) { 873 if (suppress & _suppress_defc) have_defc = true; 874 if (suppress & _suppress_name) have_name = true; 875 if (suppress & _suppress_type) have_type = true; 876 } 877 878 if (have_defc && have_name && have_type) return; // nothing needed 879 880 switch (flags & ALL_KINDS) { 881 case IS_METHOD: 882 case IS_CONSTRUCTOR: 883 { 884 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 885 if (vmtarget == nullptr) { 886 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); 887 } 888 methodHandle m(THREAD, vmtarget); 889 DEBUG_ONLY(vmtarget = nullptr); // safety 890 if (!have_defc) { 891 InstanceKlass* defc = m->method_holder(); 892 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); 893 } 894 if (!have_name) { 895 //not java_lang_String::create_from_symbol; let's intern member names 896 oop name = StringTable::intern(m->name(), CHECK); 897 java_lang_invoke_MemberName::set_name(mname(), name); 898 } 899 if (!have_type) { 900 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); 901 java_lang_invoke_MemberName::set_type(mname(), type()); 902 } 903 return; 904 } 905 case IS_FIELD: 906 { 907 oop clazz = java_lang_invoke_MemberName::clazz(mname()); 908 if (clazz == nullptr) { 909 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand (as field)"); 910 } 911 InstanceKlass* defc = InstanceKlass::cast(java_lang_Class::as_Klass(clazz)); 912 DEBUG_ONLY(clazz = nullptr); // safety 913 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); 914 bool is_static = ((flags & JVM_ACC_STATIC) != 0); 915 fieldDescriptor fd; // find_field initializes fd if found 916 if (!defc->find_field_from_offset(checked_cast<int>(vmindex), is_static, &fd)) 917 break; // cannot expand 918 if (!have_name) { 919 //not java_lang_String::create_from_symbol; let's intern member names 920 oop name = StringTable::intern(fd.name(), CHECK); 921 java_lang_invoke_MemberName::set_name(mname(), name); 922 } 923 if (!have_type) { 924 // If it is a primitive field type, don't mess with short strings like "I". 925 Handle type (THREAD, field_signature_type_or_null(fd.signature())); 926 if (type.is_null()) { 927 type = java_lang_String::create_from_symbol(fd.signature(), CHECK); 928 } 929 java_lang_invoke_MemberName::set_type(mname(), type()); 930 } 931 return; 932 } 933 } 934 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 935 } 936 937 void MethodHandles::add_dependent_nmethod(oop call_site, nmethod* nm) { 938 assert_locked_or_safepoint(CodeCache_lock); 939 940 oop context = java_lang_invoke_CallSite::context_no_keepalive(call_site); 941 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context); 942 // Try to purge stale entries on updates. 943 // Since GC doesn't clean dependency contexts rooted at CallSiteContext objects, 944 // in order to avoid memory leak, stale entries are purged whenever a dependency list 945 // is changed (both on addition and removal). Though memory reclamation is delayed, 946 // it avoids indefinite memory usage growth. 947 deps.add_dependent_nmethod(nm); 948 } 949 950 void MethodHandles::clean_dependency_context(oop call_site) { 951 oop context = java_lang_invoke_CallSite::context_no_keepalive(call_site); 952 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context); 953 deps.clean_unloading_dependents(); 954 } 955 956 void MethodHandles::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, Handle call_site, Handle target) { 957 assert_lock_strong(Compile_lock); 958 959 CallSiteDepChange changes(call_site, target); 960 { 961 NoSafepointVerifier nsv; 962 MutexLocker ml(CodeCache_lock, Mutex::_no_safepoint_check_flag); 963 964 oop context = java_lang_invoke_CallSite::context_no_keepalive(call_site()); 965 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context); 966 deps.mark_dependent_nmethods(deopt_scope, changes); 967 } 968 } 969 970 void MethodHandles::trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid) { 971 if (log_is_enabled(Info, methodhandles)) { 972 const char* name = vmIntrinsics::name_at(iid); 973 if (*name == '_') name += 1; 974 const size_t len = strlen(name) + 50; 975 char* qname = NEW_C_HEAP_ARRAY(char, len, mtInternal); 976 const char* suffix = ""; 977 if (is_signature_polymorphic(iid)) { 978 if (is_signature_polymorphic_static(iid)) 979 suffix = "/static"; 980 else 981 suffix = "/private"; 982 } 983 jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix); 984 trace_method_handle(_masm, qname); 985 // LSan appears unable to keep track of qname, ignore it. 986 LSAN_IGNORE_OBJECT(qname); 987 // Note: Don't free the allocated char array because it's used 988 // during runtime. 989 } 990 } 991 992 // 993 // Here are the native methods in java.lang.invoke.MethodHandleNatives 994 // They are the private interface between this JVM and the HotSpot-specific 995 // Java code that implements JSR 292 method handles. 996 // 997 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way 998 // that intrinsic (non-JNI) native methods are defined in HotSpot. 999 // 1000 1001 #ifndef PRODUCT 1002 #define EACH_NAMED_CON(template, requirement) \ 1003 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ 1004 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ 1005 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ 1006 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ 1007 template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \ 1008 template(java_lang_invoke_MemberName,MN_TRUSTED_FINAL) \ 1009 template(java_lang_invoke_MemberName,MN_HIDDEN_MEMBER) \ 1010 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ 1011 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ 1012 template(java_lang_invoke_MemberName,MN_NESTMATE_CLASS) \ 1013 template(java_lang_invoke_MemberName,MN_HIDDEN_CLASS) \ 1014 template(java_lang_invoke_MemberName,MN_STRONG_LOADER_LINK) \ 1015 template(java_lang_invoke_MemberName,MN_ACCESS_VM_ANNOTATIONS) \ 1016 template(java_lang_invoke_MemberName,MN_MODULE_MODE) \ 1017 template(java_lang_invoke_MemberName,MN_UNCONDITIONAL_MODE) \ 1018 template(java_lang_invoke_MemberName,MN_TRUSTED_MODE) \ 1019 /*end*/ 1020 1021 #define IGNORE_REQ(req_expr) /* req_expr */ 1022 #define ONE_PLUS(scope,value) 1+ 1023 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; 1024 #define VALUE_COMMA(scope,value) scope::value, 1025 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; 1026 #define STRING_NULL(scope,value) #value "\0" 1027 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; 1028 1029 static bool advertise_con_value(int which) { 1030 if (which < 0) return false; 1031 bool ok = true; 1032 int count = 0; 1033 #define INC_COUNT(scope,value) \ 1034 ++count; 1035 #define CHECK_REQ(req_expr) \ 1036 if (which < count) return ok; \ 1037 ok = (req_expr); 1038 EACH_NAMED_CON(INC_COUNT, CHECK_REQ); 1039 #undef INC_COUNT 1040 #undef CHECK_REQ 1041 assert(count == con_value_count, ""); 1042 if (which < count) return ok; 1043 return false; 1044 } 1045 1046 #undef ONE_PLUS 1047 #undef VALUE_COMMA 1048 #undef STRING_NULL 1049 #undef EACH_NAMED_CON 1050 #endif // PRODUCT 1051 1052 JVM_ENTRY_PROF(jint, MHN_getNamedCon, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { 1053 #ifndef PRODUCT 1054 if (advertise_con_value(which)) { 1055 assert(which >= 0 && which < con_value_count, ""); 1056 int con = con_values[which]; 1057 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); 1058 if (box.not_null() && box->klass() == Universe::objectArrayKlass() && box->length() > 0) { 1059 const char* str = &con_names[0]; 1060 for (int i = 0; i < which; i++) 1061 str += strlen(str) + 1; // skip name and null 1062 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint 1063 box->obj_at_put(0, name); 1064 } 1065 return con; 1066 } 1067 #endif 1068 return 0; 1069 } 1070 JVM_END 1071 1072 // void init(MemberName self, AccessibleObject ref) 1073 JVM_ENTRY_PROF(void, MHN_init_Mem, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { 1074 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1075 if (target_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1076 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1077 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1078 MethodHandles::init_MemberName(mname, target, CHECK); 1079 } 1080 JVM_END 1081 1082 // void expand(MemberName self) 1083 JVM_ENTRY_PROF(void, MHN_expand_Mem, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1084 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1085 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1086 MethodHandles::expand_MemberName(mname, 0, CHECK); 1087 } 1088 JVM_END 1089 1090 // void resolve(MemberName self, Class<?> caller) 1091 JVM_ENTRY_PROF(jobject, MHN_resolve_Mem, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh, 1092 jint lookup_mode, jboolean speculative_resolve)) { 1093 if (mname_jh == nullptr) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } 1094 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1095 1096 // The trusted Java code that calls this method should already have performed 1097 // access checks on behalf of the given caller. But, we can verify this. 1098 // This only verifies from the context of the lookup class. It does not 1099 // verify the lookup context for a Lookup object teleported from one module 1100 // to another. Such Lookup object can only access the intersection of the set 1101 // of accessible classes from both lookup class and previous lookup class. 1102 if (VerifyMethodHandles && (lookup_mode & LM_TRUSTED) == LM_TRUSTED && caller_jh != nullptr && 1103 java_lang_invoke_MemberName::clazz(mname()) != nullptr) { 1104 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); 1105 if (reference_klass != nullptr && reference_klass->is_objArray_klass()) { 1106 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass(); 1107 } 1108 1109 // Reflection::verify_class_access can only handle instance classes. 1110 if (reference_klass != nullptr && reference_klass->is_instance_klass()) { 1111 // Emulate LinkResolver::check_klass_accessability. 1112 Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); 1113 // access check on behalf of the caller if this is not a public lookup 1114 // i.e. lookup mode is not UNCONDITIONAL 1115 if ((lookup_mode & LM_UNCONDITIONAL) == 0 1116 && Reflection::verify_class_access(caller, 1117 InstanceKlass::cast(reference_klass), 1118 true) != Reflection::ACCESS_OK) { 1119 ResourceMark rm(THREAD); 1120 stringStream ss; 1121 ss.print("caller %s tried to access %s", caller->class_in_module_of_loader(), 1122 reference_klass->class_in_module_of_loader()); 1123 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), ss.as_string()); 1124 } 1125 } 1126 } 1127 1128 Klass* caller = caller_jh == nullptr ? nullptr : 1129 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); 1130 Handle resolved = MethodHandles::resolve_MemberName(mname, caller, lookup_mode, 1131 speculative_resolve == JNI_TRUE, 1132 CHECK_NULL); 1133 1134 if (resolved.is_null()) { 1135 int flags = java_lang_invoke_MemberName::flags(mname()); 1136 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1137 if (!MethodHandles::ref_kind_is_valid(ref_kind)) { 1138 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); 1139 } 1140 if (speculative_resolve) { 1141 assert(!HAS_PENDING_EXCEPTION, "No exceptions expected when resolving speculatively"); 1142 return nullptr; 1143 } 1144 if ((flags & ALL_KINDS) == IS_FIELD) { 1145 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "field resolution failed"); 1146 } else if ((flags & ALL_KINDS) == IS_METHOD || 1147 (flags & ALL_KINDS) == IS_CONSTRUCTOR) { 1148 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "method resolution failed"); 1149 } else { 1150 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); 1151 } 1152 } 1153 1154 return JNIHandles::make_local(THREAD, resolved()); 1155 } 1156 JVM_END 1157 1158 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { 1159 if (mname == nullptr || 1160 java_lang_invoke_MemberName::clazz(mname) == nullptr) { 1161 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); 1162 } else { 1163 int flags = java_lang_invoke_MemberName::flags(mname); 1164 if ((flags & IS_FIELD) != 0 && 1165 (must_be_static 1166 ? (flags & JVM_ACC_STATIC) != 0 1167 : (flags & JVM_ACC_STATIC) == 0)) { 1168 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname); 1169 return (jlong) vmindex; 1170 } 1171 } 1172 const char* msg = (must_be_static ? "static field required" : "non-static field required"); 1173 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); 1174 return 0; 1175 } 1176 1177 JVM_ENTRY_PROF(jlong, MHN_objectFieldOffset, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1178 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); 1179 } 1180 JVM_END 1181 1182 JVM_ENTRY_PROF(jlong, MHN_staticFieldOffset, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1183 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); 1184 } 1185 JVM_END 1186 1187 JVM_ENTRY_PROF(jobject, MHN_staticFieldBase, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1188 // use the other function to perform sanity checks: 1189 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); 1190 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); 1191 return JNIHandles::make_local(THREAD, clazz); 1192 } 1193 JVM_END 1194 1195 JVM_ENTRY_PROF(jobject, MHN_getMemberVMInfo, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1196 if (mname_jh == nullptr) return nullptr; 1197 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1198 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); 1199 objArrayHandle result = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 2, CHECK_NULL); 1200 jvalue vmindex_value; vmindex_value.j = (long)vmindex; 1201 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); 1202 result->obj_at_put(0, x); 1203 1204 int flags = java_lang_invoke_MemberName::flags(mname()); 1205 if ((flags & IS_FIELD) != 0) { 1206 x = java_lang_invoke_MemberName::clazz(mname()); 1207 } else { 1208 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 1209 assert(vmtarget != nullptr && vmtarget->is_method(), "vmtarget is only method"); 1210 x = mname(); 1211 } 1212 result->obj_at_put(1, x); 1213 return JNIHandles::make_local(THREAD, result()); 1214 } 1215 JVM_END 1216 1217 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetNormal, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1218 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1219 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh)); 1220 DeoptimizationScope deopt_scope; 1221 { 1222 // Walk all nmethods depending on this call site. 1223 MutexLocker mu(thread, Compile_lock); 1224 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target); 1225 java_lang_invoke_CallSite::set_target(call_site(), target()); 1226 // This is assumed to be an 'atomic' operation by verification. 1227 // So keep it under lock for now. 1228 deopt_scope.deoptimize_marked(); 1229 } 1230 } 1231 JVM_END 1232 1233 // Make sure the class is linked. If the class cannot be verified, etc, an exception will be thrown. 1234 JVM_ENTRY_PROF(void, MHN_checkArchivable, MHN_checkArchivable(JNIEnv *env, jobject igcls, jclass klass_jh)) { 1235 if (klass_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "klass is null"); } 1236 1237 if (CDSConfig::is_dumping_invokedynamic()) { 1238 Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(klass_jh)); 1239 if (klass != nullptr && klass->is_instance_klass()) { 1240 // klass could be null during very early VM start-up 1241 InstanceKlass* ik = InstanceKlass::cast(klass); 1242 ik->link_class(THREAD); // exception will be thrown if unverifiable 1243 if (!ik->is_linked()) { 1244 assert(HAS_PENDING_EXCEPTION, "must be"); 1245 ResourceMark rm; 1246 log_warning(cds)("Cannot use unverifiable class %s in MethodType", 1247 klass->external_name()); 1248 return; 1249 } 1250 1251 if (SystemDictionaryShared::is_jfr_event_class(ik)) { 1252 ResourceMark rm; 1253 log_warning(cds)("Cannot use JFR event class %s in MethodType", 1254 klass->external_name()); 1255 Exceptions::fthrow(THREAD_AND_LOCATION, 1256 vmSymbols::java_lang_NoClassDefFoundError(), 1257 "Class %s, or one of its supertypes, is a JFR event class", 1258 klass->external_name()); 1259 } 1260 } 1261 } 1262 } 1263 JVM_END 1264 1265 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetVolatile, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { 1266 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); 1267 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh)); 1268 DeoptimizationScope deopt_scope; 1269 { 1270 // Walk all nmethods depending on this call site. 1271 MutexLocker mu(thread, Compile_lock); 1272 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target); 1273 java_lang_invoke_CallSite::set_target_volatile(call_site(), target()); 1274 // This is assumed to be an 'atomic' operation by verification. 1275 // So keep it under lock for now. 1276 deopt_scope.deoptimize_marked(); 1277 } 1278 } 1279 JVM_END 1280 1281 JVM_ENTRY_PROF(void, MHN_copyOutBootstrapArguments, MHN_copyOutBootstrapArguments(JNIEnv* env, jobject igcls, 1282 jobject caller_jh, jintArray index_info_jh, 1283 jint start, jint end, 1284 jobjectArray buf_jh, jint pos, 1285 jboolean resolve, jobject ifna_jh)) { 1286 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller_jh)); 1287 if (caller_k == nullptr || !caller_k->is_instance_klass()) { 1288 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad caller"); 1289 } 1290 InstanceKlass* caller = InstanceKlass::cast(caller_k); 1291 typeArrayOop index_info_oop = (typeArrayOop) JNIHandles::resolve(index_info_jh); 1292 if (index_info_oop == nullptr || 1293 index_info_oop->klass() != Universe::intArrayKlass() || 1294 typeArrayOop(index_info_oop)->length() < 2) { 1295 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (0)"); 1296 } 1297 typeArrayHandle index_info(THREAD, index_info_oop); 1298 int bss_index_in_pool = index_info->int_at(1); 1299 // While we are here, take a quick look at the index info: 1300 if (bss_index_in_pool <= 0 || 1301 bss_index_in_pool >= caller->constants()->length() || 1302 index_info->int_at(0) 1303 != caller->constants()->bootstrap_argument_count_at(bss_index_in_pool)) { 1304 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (1)"); 1305 } 1306 objArrayHandle buf(THREAD, (objArrayOop) JNIHandles::resolve(buf_jh)); 1307 if (start < 0) { 1308 for (int pseudo_index = -4; pseudo_index < 0; pseudo_index++) { 1309 if (start == pseudo_index) { 1310 if (start >= end || 0 > pos || pos >= buf->length()) break; 1311 oop pseudo_arg = nullptr; 1312 switch (pseudo_index) { 1313 case -4: // bootstrap method 1314 { 1315 int bsm_index = caller->constants()->bootstrap_method_ref_index_at(bss_index_in_pool); 1316 pseudo_arg = caller->constants()->resolve_possibly_cached_constant_at(bsm_index, CHECK); 1317 break; 1318 } 1319 case -3: // name 1320 { 1321 Symbol* name = caller->constants()->name_ref_at(bss_index_in_pool, Bytecodes::_invokedynamic); 1322 Handle str = java_lang_String::create_from_symbol(name, CHECK); 1323 pseudo_arg = str(); 1324 break; 1325 } 1326 case -2: // type 1327 { 1328 Symbol* type = caller->constants()->signature_ref_at(bss_index_in_pool, Bytecodes::_invokedynamic); 1329 Handle th; 1330 if (type->char_at(0) == JVM_SIGNATURE_FUNC) { 1331 th = SystemDictionary::find_method_handle_type(type, caller, CHECK); 1332 } else { 1333 th = SystemDictionary::find_java_mirror_for_type(type, caller, SignatureStream::NCDFError, CHECK); 1334 } 1335 pseudo_arg = th(); 1336 break; 1337 } 1338 case -1: // argument count 1339 { 1340 int argc = caller->constants()->bootstrap_argument_count_at(bss_index_in_pool); 1341 jvalue argc_value; argc_value.i = (jint)argc; 1342 pseudo_arg = java_lang_boxing_object::create(T_INT, &argc_value, CHECK); 1343 break; 1344 } 1345 } 1346 1347 // Store the pseudo-argument, and advance the pointers. 1348 buf->obj_at_put(pos++, pseudo_arg); 1349 ++start; 1350 } 1351 } 1352 // When we are done with this there may be regular arguments to process too. 1353 } 1354 Handle ifna(THREAD, JNIHandles::resolve(ifna_jh)); 1355 caller->constants()-> 1356 copy_bootstrap_arguments_at(bss_index_in_pool, 1357 start, end, buf, pos, 1358 (resolve == JNI_TRUE), ifna, CHECK); 1359 } 1360 JVM_END 1361 1362 // It is called by a Cleaner object which ensures that dropped CallSites properly 1363 // deallocate their dependency information. 1364 JVM_ENTRY_PROF(void, MHN_clearCallSiteContext, MHN_clearCallSiteContext(JNIEnv* env, jobject igcls, jobject context_jh)) { 1365 Handle context(THREAD, JNIHandles::resolve_non_null(context_jh)); 1366 DeoptimizationScope deopt_scope; 1367 { 1368 NoSafepointVerifier nsv; 1369 MutexLocker ml(THREAD, CodeCache_lock, Mutex::_no_safepoint_check_flag); 1370 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context()); 1371 deps.remove_and_mark_for_deoptimization_all_dependents(&deopt_scope); 1372 // This is assumed to be an 'atomic' operation by verification. 1373 // So keep it under lock for now. 1374 deopt_scope.deoptimize_marked(); 1375 } 1376 } 1377 JVM_END 1378 1379 /** 1380 * Throws a java/lang/UnsupportedOperationException unconditionally. 1381 * This is required by the specification of MethodHandle.invoke if 1382 * invoked directly. 1383 */ 1384 JVM_ENTRY_PROF(jobject, MH_invoke_UOE, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { 1385 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively"); 1386 return nullptr; 1387 } 1388 JVM_END 1389 1390 /** 1391 * Throws a java/lang/UnsupportedOperationException unconditionally. 1392 * This is required by the specification of MethodHandle.invokeExact if 1393 * invoked directly. 1394 */ 1395 JVM_ENTRY_PROF(jobject, MH_invokeExact_UOE, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { 1396 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively"); 1397 return nullptr; 1398 } 1399 JVM_END 1400 1401 /** 1402 * Throws a java/lang/UnsupportedOperationException unconditionally. 1403 * This is required by the specification of VarHandle.{access-mode} if 1404 * invoked directly. 1405 */ 1406 JVM_ENTRY(jobject, VH_UOE(JNIEnv* env, jobject vh, jobjectArray args)) { 1407 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "VarHandle access mode methods cannot be invoked reflectively"); 1408 return nullptr; 1409 } 1410 JVM_END 1411 1412 1413 /// JVM_RegisterMethodHandleMethods 1414 1415 #define LANG "Ljava/lang/" 1416 #define JLINV "Ljava/lang/invoke/" 1417 1418 #define OBJ LANG "Object;" 1419 #define CLS LANG "Class;" 1420 #define STRG LANG "String;" 1421 #define CS JLINV "CallSite;" 1422 #define MT JLINV "MethodType;" 1423 #define MH JLINV "MethodHandle;" 1424 #define MEM JLINV "MemberName;" 1425 #define CTX JLINV "MethodHandleNatives$CallSiteContext;" 1426 1427 #define CC (char*) /*cast a literal from (const char*)*/ 1428 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1429 1430 // These are the native methods on java.lang.invoke.MethodHandleNatives. 1431 static JNINativeMethod MHN_methods[] = { 1432 {CC "init", CC "(" MEM "" OBJ ")V", FN_PTR(MHN_init_Mem)}, 1433 {CC "expand", CC "(" MEM ")V", FN_PTR(MHN_expand_Mem)}, 1434 {CC "resolve", CC "(" MEM "" CLS "IZ)" MEM, FN_PTR(MHN_resolve_Mem)}, 1435 // static native int getNamedCon(int which, Object[] name) 1436 {CC "getNamedCon", CC "(I[" OBJ ")I", FN_PTR(MHN_getNamedCon)}, 1437 {CC "objectFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_objectFieldOffset)}, 1438 {CC "setCallSiteTargetNormal", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetNormal)}, 1439 {CC "setCallSiteTargetVolatile", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, 1440 {CC "copyOutBootstrapArguments", CC "(" CLS "[III[" OBJ "IZ" OBJ ")V", FN_PTR(MHN_copyOutBootstrapArguments)}, 1441 {CC "clearCallSiteContext", CC "(" CTX ")V", FN_PTR(MHN_clearCallSiteContext)}, 1442 {CC "staticFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_staticFieldOffset)}, 1443 {CC "staticFieldBase", CC "(" MEM ")" OBJ, FN_PTR(MHN_staticFieldBase)}, 1444 {CC "getMemberVMInfo", CC "(" MEM ")" OBJ, FN_PTR(MHN_getMemberVMInfo)} 1445 }; 1446 1447 static JNINativeMethod MH_methods[] = { 1448 // UnsupportedOperationException throwers 1449 {CC "invoke", CC "([" OBJ ")" OBJ, FN_PTR(MH_invoke_UOE)}, 1450 {CC "invokeExact", CC "([" OBJ ")" OBJ, FN_PTR(MH_invokeExact_UOE)} 1451 }; 1452 static JNINativeMethod VH_methods[] = { 1453 // UnsupportedOperationException throwers 1454 {CC "get", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1455 {CC "set", CC "([" OBJ ")V", FN_PTR(VH_UOE)}, 1456 {CC "getVolatile", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1457 {CC "setVolatile", CC "([" OBJ ")V", FN_PTR(VH_UOE)}, 1458 {CC "getAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1459 {CC "setRelease", CC "([" OBJ ")V", FN_PTR(VH_UOE)}, 1460 {CC "getOpaque", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1461 {CC "setOpaque", CC "([" OBJ ")V", FN_PTR(VH_UOE)}, 1462 {CC "compareAndSet", CC "([" OBJ ")Z", FN_PTR(VH_UOE)}, 1463 {CC "compareAndExchange", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1464 {CC "compareAndExchangeAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1465 {CC "compareAndExchangeRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1466 {CC "weakCompareAndSetPlain", CC "([" OBJ ")Z", FN_PTR(VH_UOE)}, 1467 {CC "weakCompareAndSet", CC "([" OBJ ")Z", FN_PTR(VH_UOE)}, 1468 {CC "weakCompareAndSetAcquire", CC "([" OBJ ")Z", FN_PTR(VH_UOE)}, 1469 {CC "weakCompareAndSetRelease", CC "([" OBJ ")Z", FN_PTR(VH_UOE)}, 1470 {CC "getAndSet", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1471 {CC "getAndSetAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1472 {CC "getAndSetRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1473 {CC "getAndAdd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1474 {CC "getAndAddAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1475 {CC "getAndAddRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1476 {CC "getAndBitwiseOr", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1477 {CC "getAndBitwiseOrAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1478 {CC "getAndBitwiseOrRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1479 {CC "getAndBitwiseAnd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1480 {CC "getAndBitwiseAndAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1481 {CC "getAndBitwiseAndRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1482 {CC "getAndBitwiseXor", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1483 {CC "getAndBitwiseXorAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}, 1484 {CC "getAndBitwiseXorRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)} 1485 }; 1486 1487 /** 1488 * This one function is exported, used by NativeLookup. 1489 */ 1490 JVM_ENTRY_PROF(void, JVM_RegisterMethodHandleMethods, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 1491 assert(!MethodHandles::enabled(), "must not be enabled"); 1492 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present"); 1493 assert(vmClasses::VarHandle_klass() != nullptr, "should be present"); 1494 1495 oop mh_mirror = vmClasses::MethodHandle_klass()->java_mirror(); 1496 oop vh_mirror = vmClasses::VarHandle_klass()->java_mirror(); 1497 jclass MH_class = (jclass) JNIHandles::make_local(THREAD, mh_mirror); 1498 jclass VH_class = (jclass) JNIHandles::make_local(THREAD, vh_mirror); 1499 1500 { 1501 ThreadToNativeFromVM ttnfv(thread); 1502 1503 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod)); 1504 guarantee(status == JNI_OK && !env->ExceptionCheck(), 1505 "register java.lang.invoke.MethodHandleNative natives"); 1506 1507 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod)); 1508 guarantee(status == JNI_OK && !env->ExceptionCheck(), 1509 "register java.lang.invoke.MethodHandle natives"); 1510 1511 status = env->RegisterNatives(VH_class, VH_methods, sizeof(VH_methods)/sizeof(JNINativeMethod)); 1512 guarantee(status == JNI_OK && !env->ExceptionCheck(), 1513 "register java.lang.invoke.VarHandle natives"); 1514 } 1515 1516 log_debug(methodhandles, indy)("MethodHandle support loaded (using LambdaForms)"); 1517 1518 MethodHandles::set_enabled(true); 1519 } 1520 JVM_END 1521 1522 #define DO_COUNTERS(macro) \ 1523 macro(MHN_init_Mem) \ 1524 macro(MHN_expand_Mem) \ 1525 macro(MHN_resolve_Mem) \ 1526 macro(MHN_checkArchivable) \ 1527 macro(MHN_getNamedCon) \ 1528 macro(MHN_objectFieldOffset) \ 1529 macro(MHN_setCallSiteTargetNormal) \ 1530 macro(MHN_setCallSiteTargetVolatile) \ 1531 macro(MHN_copyOutBootstrapArguments) \ 1532 macro(MHN_clearCallSiteContext) \ 1533 macro(MHN_staticFieldOffset) \ 1534 macro(MHN_staticFieldBase) \ 1535 macro(MHN_getMemberVMInfo) \ 1536 macro(MH_invoke_UOE) \ 1537 macro(MH_invokeExact_UOE) \ 1538 macro(JVM_RegisterMethodHandleMethods) 1539 1540 #define INIT_COUNTER(name) \ 1541 NEWPERFTICKCOUNTERS(_perf_##name##_timer, SUN_RT, #name); \ 1542 NEWPERFEVENTCOUNTER(_perf_##name##_count, SUN_RT, #name "_count"); \ 1543 1544 void MethodHandles::init_counters() { 1545 if (UsePerfData) { 1546 EXCEPTION_MARK; 1547 1548 DO_COUNTERS(INIT_COUNTER) 1549 1550 if (HAS_PENDING_EXCEPTION) { 1551 vm_exit_during_initialization("perf_mhn_init failed unexpectedly"); 1552 } 1553 } 1554 } 1555 1556 #undef INIT_COUNTER 1557 1558 #define PRINT_COUNTER(name) {\ 1559 jlong count = _perf_##name##_count->get_value(); \ 1560 if (count > 0) { \ 1561 st->print_cr(" %-40s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #name, \ 1562 _perf_##name##_timer->elapsed_counter_value_us(), \ 1563 _perf_##name##_timer->thread_counter_value_us(), \ 1564 count); \ 1565 }} 1566 1567 void MethodHandles::print_counters_on(outputStream* st) { 1568 if (ProfileVMCalls && UsePerfData) { 1569 DO_COUNTERS(PRINT_COUNTER) 1570 } else { 1571 st->print_cr(" MHN: no info (%s is disabled)", (UsePerfData ? "ProfileVMCalls" : "UsePerfData")); 1572 } 1573 } 1574 1575 #undef PRINT_COUNTER