Thu, 09 Dec 2010 15:04:26 -0500
7004582: Add GetThisObject() function to JVMTI 1.2
Summary: Add 'GetThisObject' function
Reviewed-by: never, coleenp
1 /*
2 * Copyright (c) 2008, 2010, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "interpreter/interpreter.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/oopFactory.hpp"
30 #include "prims/methodHandles.hpp"
31 #include "runtime/javaCalls.hpp"
32 #include "runtime/reflection.hpp"
33 #include "runtime/signature.hpp"
34 #include "runtime/stubRoutines.hpp"
36 /*
37 * JSR 292 reference implementation: method handles
38 */
40 bool MethodHandles::_enabled = false; // set true after successful native linkage
42 MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL};
43 const char* MethodHandles::_entry_names[_EK_LIMIT+1] = {
44 "raise_exception",
45 "invokestatic", // how a MH emulates invokestatic
46 "invokespecial", // ditto for the other invokes...
47 "invokevirtual",
48 "invokeinterface",
49 "bound_ref", // these are for BMH...
50 "bound_int",
51 "bound_long",
52 "bound_ref_direct", // (direct versions have a direct methodOop)
53 "bound_int_direct",
54 "bound_long_direct",
56 // starting at _adapter_mh_first:
57 "adapter_retype_only", // these are for AMH...
58 "adapter_retype_raw",
59 "adapter_check_cast",
60 "adapter_prim_to_prim",
61 "adapter_ref_to_prim",
62 "adapter_prim_to_ref",
63 "adapter_swap_args",
64 "adapter_rot_args",
65 "adapter_dup_args",
66 "adapter_drop_args",
67 "adapter_collect_args",
68 "adapter_spread_args",
69 "adapter_flyby",
70 "adapter_ricochet",
72 // optimized adapter types:
73 "adapter_swap_args/1",
74 "adapter_swap_args/2",
75 "adapter_rot_args/1,up",
76 "adapter_rot_args/1,down",
77 "adapter_rot_args/2,up",
78 "adapter_rot_args/2,down",
79 "adapter_prim_to_prim/i2i",
80 "adapter_prim_to_prim/l2i",
81 "adapter_prim_to_prim/d2f",
82 "adapter_prim_to_prim/i2l",
83 "adapter_prim_to_prim/f2d",
84 "adapter_ref_to_prim/unboxi",
85 "adapter_ref_to_prim/unboxl",
86 "adapter_spread_args/0",
87 "adapter_spread_args/1",
88 "adapter_spread_args/more",
90 NULL
91 };
93 // Adapters.
94 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL;
95 int MethodHandles::_adapter_code_size = StubRoutines::method_handles_adapters_code_size;
97 jobject MethodHandles::_raise_exception_method;
99 #ifdef ASSERT
100 bool MethodHandles::spot_check_entry_names() {
101 assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), "");
102 assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), "");
103 assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), "");
104 assert(!strcmp(entry_name(_adapter_ricochet), "adapter_ricochet"), "");
105 assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), "");
106 return true;
107 }
108 #endif
111 //------------------------------------------------------------------------------
112 // MethodHandles::generate_adapters
113 //
114 void MethodHandles::generate_adapters() {
115 if (!EnableMethodHandles || SystemDictionary::MethodHandle_klass() == NULL) return;
117 assert(_adapter_code == NULL, "generate only once");
119 ResourceMark rm;
120 TraceTime timer("MethodHandles adapters generation", TraceStartupTime);
121 _adapter_code = MethodHandlesAdapterBlob::create(_adapter_code_size);
122 if (_adapter_code == NULL)
123 vm_exit_out_of_memory(_adapter_code_size, "CodeCache: no room for MethodHandles adapters");
124 CodeBuffer code(_adapter_code);
125 MethodHandlesAdapterGenerator g(&code);
126 g.generate();
127 }
130 //------------------------------------------------------------------------------
131 // MethodHandlesAdapterGenerator::generate
132 //
133 void MethodHandlesAdapterGenerator::generate() {
134 // Generate generic method handle adapters.
135 for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST;
136 ek < MethodHandles::_EK_LIMIT;
137 ek = MethodHandles::EntryKind(1 + (int)ek)) {
138 StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek));
139 MethodHandles::generate_method_handle_stub(_masm, ek);
140 }
141 }
144 void MethodHandles::set_enabled(bool z) {
145 if (_enabled != z) {
146 guarantee(z && EnableMethodHandles, "can only enable once, and only if -XX:+EnableMethodHandles");
147 _enabled = z;
148 }
149 }
151 // Note: A method which does not have a TRAPS argument cannot block in the GC
152 // or throw exceptions. Such methods are used in this file to do something quick
153 // and local, like parse a data structure. For speed, such methods work on plain
154 // oops, not handles. Trapping methods uniformly operate on handles.
156 methodOop MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype,
157 klassOop& receiver_limit_result, int& decode_flags_result) {
158 if (vmtarget == NULL) return NULL;
159 assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding");
160 if (vmindex < 0) {
161 // this DMH performs no dispatch; it is directly bound to a methodOop
162 // A MemberName may either be directly bound to a methodOop,
163 // or it may use the klass/index form; both forms mean the same thing.
164 methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result);
165 if ((decode_flags_result & _dmf_has_receiver) != 0
166 && java_dyn_MethodType::is_instance(mtype)) {
167 // Extract receiver type restriction from mtype.ptypes[0].
168 objArrayOop ptypes = java_dyn_MethodType::ptypes(mtype);
169 oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0);
170 if (java_lang_Class::is_instance(ptype0))
171 receiver_limit_result = java_lang_Class::as_klassOop(ptype0);
172 }
173 if (vmindex == methodOopDesc::nonvirtual_vtable_index) {
174 // this DMH can be an "invokespecial" version
175 decode_flags_result &= ~_dmf_does_dispatch;
176 } else {
177 assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?");
178 }
179 return m;
180 } else {
181 assert(vmtarget->is_klass(), "must be class or interface");
182 decode_flags_result |= MethodHandles::_dmf_does_dispatch;
183 decode_flags_result |= MethodHandles::_dmf_has_receiver;
184 receiver_limit_result = (klassOop)vmtarget;
185 Klass* tk = Klass::cast((klassOop)vmtarget);
186 if (tk->is_interface()) {
187 // an itable linkage is <interface, itable index>
188 decode_flags_result |= MethodHandles::_dmf_from_interface;
189 return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex);
190 } else {
191 if (!tk->oop_is_instance())
192 tk = instanceKlass::cast(SystemDictionary::Object_klass());
193 return ((instanceKlass*)tk)->method_at_vtable(vmindex);
194 }
195 }
196 }
198 // MemberName and DirectMethodHandle have the same linkage to the JVM internals.
199 // (MemberName is the non-operational name used for queries and setup.)
201 methodOop MethodHandles::decode_DirectMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) {
202 oop vmtarget = sun_dyn_DirectMethodHandle::vmtarget(mh);
203 int vmindex = sun_dyn_DirectMethodHandle::vmindex(mh);
204 oop mtype = sun_dyn_DirectMethodHandle::type(mh);
205 return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result);
206 }
208 methodOop MethodHandles::decode_BoundMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) {
209 assert(sun_dyn_BoundMethodHandle::is_instance(mh), "");
210 assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), "");
211 for (oop bmh = mh;;) {
212 // Bound MHs can be stacked to bind several arguments.
213 oop target = java_dyn_MethodHandle::vmtarget(bmh);
214 if (target == NULL) return NULL;
215 decode_flags_result |= MethodHandles::_dmf_binds_argument;
216 klassOop tk = target->klass();
217 if (tk == SystemDictionary::BoundMethodHandle_klass()) {
218 bmh = target;
219 continue;
220 } else {
221 if (java_dyn_MethodHandle::is_subclass(tk)) {
222 //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH");
223 return decode_MethodHandle(target, receiver_limit_result, decode_flags_result);
224 } else {
225 // Optimized case: binding a receiver to a non-dispatched DMH
226 // short-circuits directly to the methodOop.
227 // (It might be another argument besides a receiver also.)
228 assert(target->is_method(), "must be a simple method");
229 decode_flags_result |= MethodHandles::_dmf_binds_method;
230 methodOop m = (methodOop) target;
231 if (!m->is_static())
232 decode_flags_result |= MethodHandles::_dmf_has_receiver;
233 return m;
234 }
235 }
236 }
237 }
239 methodOop MethodHandles::decode_AdapterMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) {
240 assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), "");
241 for (oop amh = mh;;) {
242 // Adapter MHs can be stacked to convert several arguments.
243 int conv_op = adapter_conversion_op(sun_dyn_AdapterMethodHandle::conversion(amh));
244 decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK;
245 oop target = java_dyn_MethodHandle::vmtarget(amh);
246 if (target == NULL) return NULL;
247 klassOop tk = target->klass();
248 if (tk == SystemDictionary::AdapterMethodHandle_klass()) {
249 amh = target;
250 continue;
251 } else {
252 // must be a BMH (which will bind some more arguments) or a DMH (for the final call)
253 return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result);
254 }
255 }
256 }
258 methodOop MethodHandles::decode_MethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) {
259 if (mh == NULL) return NULL;
260 klassOop mhk = mh->klass();
261 assert(java_dyn_MethodHandle::is_subclass(mhk), "must be a MethodHandle");
262 if (mhk == SystemDictionary::DirectMethodHandle_klass()) {
263 return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result);
264 } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) {
265 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result);
266 } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) {
267 return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result);
268 } else if (sun_dyn_BoundMethodHandle::is_subclass(mhk)) {
269 // could be a JavaMethodHandle (but not an adapter MH)
270 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result);
271 } else {
272 assert(false, "cannot parse this MH");
273 return NULL; // random MH?
274 }
275 }
277 methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) {
278 assert(m->is_method(), "");
279 if (m->is_static()) {
280 // check that signature begins '(L' or '([' (not '(I', '()', etc.)
281 symbolOop sig = m->signature();
282 BasicType recv_bt = char2type(sig->byte_at(1));
283 // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')'
284 assert(sig->byte_at(0) == '(', "must be method sig");
285 // if (recv_bt == T_OBJECT || recv_bt == T_ARRAY)
286 // decode_flags_result |= _dmf_has_receiver;
287 } else {
288 // non-static method
289 decode_flags_result |= _dmf_has_receiver;
290 if (!m->can_be_statically_bound() && !m->is_initializer()) {
291 decode_flags_result |= _dmf_does_dispatch;
292 if (Klass::cast(m->method_holder())->is_interface())
293 decode_flags_result |= _dmf_from_interface;
294 }
295 }
296 return m;
297 }
300 // A trusted party is handing us a cookie to determine a method.
301 // Let's boil it down to the method oop they really want.
302 methodOop MethodHandles::decode_method(oop x, klassOop& receiver_limit_result, int& decode_flags_result) {
303 decode_flags_result = 0;
304 receiver_limit_result = NULL;
305 klassOop xk = x->klass();
306 if (xk == Universe::methodKlassObj()) {
307 return decode_methodOop((methodOop) x, decode_flags_result);
308 } else if (xk == SystemDictionary::MemberName_klass()) {
309 // Note: This only works if the MemberName has already been resolved.
310 return decode_MemberName(x, receiver_limit_result, decode_flags_result);
311 } else if (java_dyn_MethodHandle::is_subclass(xk)) {
312 return decode_MethodHandle(x, receiver_limit_result, decode_flags_result);
313 } else if (xk == SystemDictionary::reflect_Method_klass()) {
314 oop clazz = java_lang_reflect_Method::clazz(x);
315 int slot = java_lang_reflect_Method::slot(x);
316 klassOop k = java_lang_Class::as_klassOop(clazz);
317 if (k != NULL && Klass::cast(k)->oop_is_instance())
318 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot),
319 decode_flags_result);
320 } else if (xk == SystemDictionary::reflect_Constructor_klass()) {
321 oop clazz = java_lang_reflect_Constructor::clazz(x);
322 int slot = java_lang_reflect_Constructor::slot(x);
323 klassOop k = java_lang_Class::as_klassOop(clazz);
324 if (k != NULL && Klass::cast(k)->oop_is_instance())
325 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot),
326 decode_flags_result);
327 } else {
328 // unrecognized object
329 assert(!x->is_method(), "already checked");
330 assert(!sun_dyn_MemberName::is_instance(x), "already checked");
331 }
332 return NULL;
333 }
336 int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) {
337 if (mh->klass() == SystemDictionary::DirectMethodHandle_klass())
338 return 0; // no push/pop
339 int this_vmslots = java_dyn_MethodHandle::vmslots(mh);
340 int last_vmslots = 0;
341 oop last_mh = mh;
342 for (;;) {
343 oop target = java_dyn_MethodHandle::vmtarget(last_mh);
344 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) {
345 last_vmslots = java_dyn_MethodHandle::vmslots(target);
346 break;
347 } else if (!java_dyn_MethodHandle::is_instance(target)) {
348 // might be klass or method
349 assert(target->is_method(), "must get here with a direct ref to method");
350 last_vmslots = methodOop(target)->size_of_parameters();
351 break;
352 }
353 last_mh = target;
354 }
355 // If I am called with fewer VM slots than my ultimate callee,
356 // it must be that I push the additionally needed slots.
357 // Likewise if am called with more VM slots, I will pop them.
358 return (last_vmslots - this_vmslots);
359 }
362 // MemberName support
364 // import sun_dyn_MemberName.*
365 enum {
366 IS_METHOD = sun_dyn_MemberName::MN_IS_METHOD,
367 IS_CONSTRUCTOR = sun_dyn_MemberName::MN_IS_CONSTRUCTOR,
368 IS_FIELD = sun_dyn_MemberName::MN_IS_FIELD,
369 IS_TYPE = sun_dyn_MemberName::MN_IS_TYPE,
370 SEARCH_SUPERCLASSES = sun_dyn_MemberName::MN_SEARCH_SUPERCLASSES,
371 SEARCH_INTERFACES = sun_dyn_MemberName::MN_SEARCH_INTERFACES,
372 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE,
373 VM_INDEX_UNINITIALIZED = sun_dyn_MemberName::VM_INDEX_UNINITIALIZED
374 };
376 Handle MethodHandles::new_MemberName(TRAPS) {
377 Handle empty;
378 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass());
379 if (!k->is_initialized()) k->initialize(CHECK_(empty));
380 return Handle(THREAD, k->allocate_instance(THREAD));
381 }
383 void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) {
384 if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) {
385 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder()
386 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index()
387 int mods = java_lang_reflect_Field::modifiers(target_oop);
388 klassOop k = java_lang_Class::as_klassOop(clazz);
389 int offset = instanceKlass::cast(k)->offset_from_fields(slot);
390 init_MemberName(mname_oop, k, accessFlags_from(mods), offset);
391 } else {
392 int decode_flags = 0; klassOop receiver_limit = NULL;
393 methodOop m = MethodHandles::decode_method(target_oop,
394 receiver_limit, decode_flags);
395 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0);
396 init_MemberName(mname_oop, m, do_dispatch);
397 }
398 }
400 void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) {
401 int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD)
402 | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ));
403 oop vmtarget = m;
404 int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet
405 if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound())
406 vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch
407 assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value");
408 sun_dyn_MemberName::set_vmtarget(mname_oop, vmtarget);
409 sun_dyn_MemberName::set_vmindex(mname_oop, vmindex);
410 sun_dyn_MemberName::set_flags(mname_oop, flags);
411 sun_dyn_MemberName::set_clazz(mname_oop, Klass::cast(m->method_holder())->java_mirror());
412 }
414 void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) {
415 int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ));
416 oop vmtarget = field_holder;
417 int vmindex = offset; // determines the field uniquely when combined with static bit
418 assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex");
419 sun_dyn_MemberName::set_vmtarget(mname_oop, vmtarget);
420 sun_dyn_MemberName::set_vmindex(mname_oop, vmindex);
421 sun_dyn_MemberName::set_flags(mname_oop, flags);
422 sun_dyn_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror());
423 }
426 methodOop MethodHandles::decode_MemberName(oop mname, klassOop& receiver_limit_result, int& decode_flags_result) {
427 int flags = sun_dyn_MemberName::flags(mname);
428 if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return NULL; // not invocable
429 oop vmtarget = sun_dyn_MemberName::vmtarget(mname);
430 int vmindex = sun_dyn_MemberName::vmindex(mname);
431 if (vmindex == VM_INDEX_UNINITIALIZED) return NULL; // not resolved
432 methodOop m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result);
433 oop clazz = sun_dyn_MemberName::clazz(mname);
434 if (clazz != NULL && java_lang_Class::is_instance(clazz)) {
435 klassOop klass = java_lang_Class::as_klassOop(clazz);
436 if (klass != NULL) receiver_limit_result = klass;
437 }
438 return m;
439 }
441 // An unresolved member name is a mere symbolic reference.
442 // Resolving it plants a vmtarget/vmindex in it,
443 // which refers dirctly to JVM internals.
444 void MethodHandles::resolve_MemberName(Handle mname, TRAPS) {
445 assert(sun_dyn_MemberName::is_instance(mname()), "");
446 #ifdef ASSERT
447 // If this assert throws, renegotiate the sentinel value used by the Java code,
448 // so that it is distinct from any valid vtable index value, and any special
449 // values defined in methodOopDesc::VtableIndexFlag.
450 // The point of the slop is to give the Java code and the JVM some room
451 // to independently specify sentinel values.
452 const int sentinel_slop = 10;
453 const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop;
454 assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels");
455 #endif
456 if (sun_dyn_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED)
457 return; // already resolved
458 oop defc_oop = sun_dyn_MemberName::clazz(mname());
459 oop name_str = sun_dyn_MemberName::name(mname());
460 oop type_str = sun_dyn_MemberName::type(mname());
461 int flags = sun_dyn_MemberName::flags(mname());
463 if (defc_oop == NULL || name_str == NULL || type_str == NULL) {
464 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve");
465 }
466 klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop);
467 defc_oop = NULL; // safety
468 if (defc_klassOop == NULL) return; // a primitive; no resolution possible
469 if (!Klass::cast(defc_klassOop)->oop_is_instance()) {
470 if (!Klass::cast(defc_klassOop)->oop_is_array()) return;
471 defc_klassOop = SystemDictionary::Object_klass();
472 }
473 instanceKlassHandle defc(THREAD, defc_klassOop);
474 defc_klassOop = NULL; // safety
475 if (defc.is_null()) {
476 THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class");
477 }
478 defc->link_class(CHECK);
480 // convert the external string name to an internal symbol
481 symbolHandle name(THREAD, java_lang_String::as_symbol_or_null(name_str));
482 if (name.is_null()) return; // no such name
483 name_str = NULL; // safety
485 Handle polymorphic_method_type;
486 bool polymorphic_signature = false;
487 if ((flags & ALL_KINDS) == IS_METHOD &&
488 (defc() == SystemDictionary::InvokeDynamic_klass() ||
489 (defc() == SystemDictionary::MethodHandle_klass() &&
490 methodOopDesc::is_method_handle_invoke_name(name()))))
491 polymorphic_signature = true;
493 // convert the external string or reflective type to an internal signature
494 symbolHandle type; {
495 symbolOop type_sym = NULL;
496 if (java_dyn_MethodType::is_instance(type_str)) {
497 type_sym = java_dyn_MethodType::as_signature(type_str, polymorphic_signature, CHECK);
498 if (polymorphic_signature)
499 polymorphic_method_type = Handle(THREAD, type_str); //preserve exactly
500 } else if (java_lang_Class::is_instance(type_str)) {
501 type_sym = java_lang_Class::as_signature(type_str, false, CHECK);
502 } else if (java_lang_String::is_instance(type_str)) {
503 if (polymorphic_signature) {
504 type = java_lang_String::as_symbol(type_str, CHECK);
505 } else {
506 type_sym = java_lang_String::as_symbol_or_null(type_str);
507 }
508 } else {
509 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized type");
510 }
511 if (type_sym != NULL)
512 type = symbolHandle(THREAD, type_sym);
513 }
514 if (type.is_null()) return; // no such signature exists in the VM
515 type_str = NULL; // safety
517 // Time to do the lookup.
518 switch (flags & ALL_KINDS) {
519 case IS_METHOD:
520 {
521 CallInfo result;
522 {
523 EXCEPTION_MARK;
524 if ((flags & JVM_ACC_STATIC) != 0) {
525 LinkResolver::resolve_static_call(result,
526 defc, name, type, KlassHandle(), false, false, THREAD);
527 } else if (defc->is_interface()) {
528 LinkResolver::resolve_interface_call(result, Handle(), defc,
529 defc, name, type, KlassHandle(), false, false, THREAD);
530 } else {
531 LinkResolver::resolve_virtual_call(result, Handle(), defc,
532 defc, name, type, KlassHandle(), false, false, THREAD);
533 }
534 if (HAS_PENDING_EXCEPTION) {
535 CLEAR_PENDING_EXCEPTION;
536 break; // go to second chance
537 }
538 }
539 methodHandle m = result.resolved_method();
540 oop vmtarget = NULL;
541 int vmindex = methodOopDesc::nonvirtual_vtable_index;
542 if (defc->is_interface()) {
543 vmindex = klassItable::compute_itable_index(m());
544 assert(vmindex >= 0, "");
545 } else if (result.has_vtable_index()) {
546 vmindex = result.vtable_index();
547 assert(vmindex >= 0, "");
548 }
549 assert(vmindex != VM_INDEX_UNINITIALIZED, "");
550 if (vmindex < 0) {
551 assert(result.is_statically_bound(), "");
552 vmtarget = m();
553 } else {
554 vmtarget = result.resolved_klass()->as_klassOop();
555 }
556 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
557 sun_dyn_MemberName::set_vmtarget(mname(), vmtarget);
558 sun_dyn_MemberName::set_vmindex(mname(), vmindex);
559 sun_dyn_MemberName::set_modifiers(mname(), mods);
560 DEBUG_ONLY(int junk; klassOop junk2);
561 assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(),
562 "properly stored for later decoding");
563 return;
564 }
565 case IS_CONSTRUCTOR:
566 {
567 CallInfo result;
568 {
569 EXCEPTION_MARK;
570 if (name() == vmSymbols::object_initializer_name()) {
571 LinkResolver::resolve_special_call(result,
572 defc, name, type, KlassHandle(), false, THREAD);
573 } else {
574 break; // will throw after end of switch
575 }
576 if (HAS_PENDING_EXCEPTION) {
577 CLEAR_PENDING_EXCEPTION;
578 return;
579 }
580 }
581 assert(result.is_statically_bound(), "");
582 methodHandle m = result.resolved_method();
583 oop vmtarget = m();
584 int vmindex = methodOopDesc::nonvirtual_vtable_index;
585 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
586 sun_dyn_MemberName::set_vmtarget(mname(), vmtarget);
587 sun_dyn_MemberName::set_vmindex(mname(), vmindex);
588 sun_dyn_MemberName::set_modifiers(mname(), mods);
589 DEBUG_ONLY(int junk; klassOop junk2);
590 assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(),
591 "properly stored for later decoding");
592 return;
593 }
594 case IS_FIELD:
595 {
596 // This is taken from LinkResolver::resolve_field, sans access checks.
597 fieldDescriptor fd; // find_field initializes fd if found
598 KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name(), type(), &fd));
599 // check if field exists; i.e., if a klass containing the field def has been selected
600 if (sel_klass.is_null()) return;
601 oop vmtarget = sel_klass->as_klassOop();
602 int vmindex = fd.offset();
603 int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS);
604 if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen
605 sun_dyn_MemberName::set_vmtarget(mname(), vmtarget);
606 sun_dyn_MemberName::set_vmindex(mname(), vmindex);
607 sun_dyn_MemberName::set_modifiers(mname(), mods);
608 return;
609 }
610 default:
611 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
612 }
614 // Second chance.
615 if (polymorphic_method_type.not_null()) {
616 // Look on a non-null class loader.
617 Handle cur_class_loader;
618 const int nptypes = java_dyn_MethodType::ptype_count(polymorphic_method_type());
619 for (int i = 0; i <= nptypes; i++) {
620 oop type_mirror;
621 if (i < nptypes) type_mirror = java_dyn_MethodType::ptype(polymorphic_method_type(), i);
622 else type_mirror = java_dyn_MethodType::rtype(polymorphic_method_type());
623 klassOop example_type = java_lang_Class::as_klassOop(type_mirror);
624 if (example_type == NULL) continue;
625 oop class_loader = Klass::cast(example_type)->class_loader();
626 if (class_loader == NULL || class_loader == cur_class_loader()) continue;
627 cur_class_loader = Handle(THREAD, class_loader);
628 methodOop m = SystemDictionary::find_method_handle_invoke(name,
629 type,
630 KlassHandle(THREAD, example_type),
631 THREAD);
632 if (HAS_PENDING_EXCEPTION) {
633 CLEAR_PENDING_EXCEPTION;
634 m = NULL;
635 // try again with a different class loader...
636 }
637 if (m != NULL) {
638 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
639 sun_dyn_MemberName::set_vmtarget(mname(), m);
640 sun_dyn_MemberName::set_vmindex(mname(), m->vtable_index());
641 sun_dyn_MemberName::set_modifiers(mname(), mods);
642 return;
643 }
644 }
645 }
646 }
648 // Conversely, a member name which is only initialized from JVM internals
649 // may have null defc, name, and type fields.
650 // Resolving it plants a vmtarget/vmindex in it,
651 // which refers directly to JVM internals.
652 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
653 assert(sun_dyn_MemberName::is_instance(mname()), "");
654 oop vmtarget = sun_dyn_MemberName::vmtarget(mname());
655 int vmindex = sun_dyn_MemberName::vmindex(mname());
656 if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) {
657 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
658 }
660 bool have_defc = (sun_dyn_MemberName::clazz(mname()) != NULL);
661 bool have_name = (sun_dyn_MemberName::name(mname()) != NULL);
662 bool have_type = (sun_dyn_MemberName::type(mname()) != NULL);
663 int flags = sun_dyn_MemberName::flags(mname());
665 if (suppress != 0) {
666 if (suppress & _suppress_defc) have_defc = true;
667 if (suppress & _suppress_name) have_name = true;
668 if (suppress & _suppress_type) have_type = true;
669 }
671 if (have_defc && have_name && have_type) return; // nothing needed
673 switch (flags & ALL_KINDS) {
674 case IS_METHOD:
675 case IS_CONSTRUCTOR:
676 {
677 klassOop receiver_limit = NULL;
678 int decode_flags = 0;
679 methodHandle m(THREAD, decode_vmtarget(vmtarget, vmindex, NULL,
680 receiver_limit, decode_flags));
681 if (m.is_null()) break;
682 if (!have_defc) {
683 klassOop defc = m->method_holder();
684 if (receiver_limit != NULL && receiver_limit != defc
685 && Klass::cast(receiver_limit)->is_subtype_of(defc))
686 defc = receiver_limit;
687 sun_dyn_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror());
688 }
689 if (!have_name) {
690 //not java_lang_String::create_from_symbol; let's intern member names
691 Handle name = StringTable::intern(m->name(), CHECK);
692 sun_dyn_MemberName::set_name(mname(), name());
693 }
694 if (!have_type) {
695 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
696 sun_dyn_MemberName::set_type(mname(), type());
697 }
698 return;
699 }
700 case IS_FIELD:
701 {
702 // This is taken from LinkResolver::resolve_field, sans access checks.
703 if (!vmtarget->is_klass()) break;
704 if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break;
705 instanceKlassHandle defc(THREAD, (klassOop) vmtarget);
706 bool is_static = ((flags & JVM_ACC_STATIC) != 0);
707 fieldDescriptor fd; // find_field initializes fd if found
708 if (!defc->find_field_from_offset(vmindex, is_static, &fd))
709 break; // cannot expand
710 if (!have_defc) {
711 sun_dyn_MemberName::set_clazz(mname(), defc->java_mirror());
712 }
713 if (!have_name) {
714 //not java_lang_String::create_from_symbol; let's intern member names
715 Handle name = StringTable::intern(fd.name(), CHECK);
716 sun_dyn_MemberName::set_name(mname(), name());
717 }
718 if (!have_type) {
719 Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK);
720 sun_dyn_MemberName::set_type(mname(), type());
721 }
722 return;
723 }
724 }
725 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
726 }
728 int MethodHandles::find_MemberNames(klassOop k,
729 symbolOop name, symbolOop sig,
730 int mflags, klassOop caller,
731 int skip, objArrayOop results) {
732 DEBUG_ONLY(No_Safepoint_Verifier nsv);
733 // this code contains no safepoints!
735 // %%% take caller into account!
737 if (k == NULL || !Klass::cast(k)->oop_is_instance()) return -1;
739 int rfill = 0, rlimit = results->length(), rskip = skip;
740 // overflow measurement:
741 int overflow = 0, overflow_limit = MAX2(1000, rlimit);
743 int match_flags = mflags;
744 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
745 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0);
746 bool local_only = !(search_superc | search_intfc);
747 bool classes_only = false;
749 if (name != NULL) {
750 if (name->utf8_length() == 0) return 0; // a match is not possible
751 }
752 if (sig != NULL) {
753 if (sig->utf8_length() == 0) return 0; // a match is not possible
754 if (sig->byte_at(0) == '(')
755 match_flags &= ~(IS_FIELD | IS_TYPE);
756 else
757 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
758 }
760 if ((match_flags & IS_TYPE) != 0) {
761 // NYI, and Core Reflection works quite well for this query
762 }
764 if ((match_flags & IS_FIELD) != 0) {
765 for (FieldStream st(k, local_only, !search_intfc); !st.eos(); st.next()) {
766 if (name != NULL && st.name() != name)
767 continue;
768 if (sig != NULL && st.signature() != sig)
769 continue;
770 // passed the filters
771 if (rskip > 0) {
772 --rskip;
773 } else if (rfill < rlimit) {
774 oop result = results->obj_at(rfill++);
775 if (!sun_dyn_MemberName::is_instance(result))
776 return -99; // caller bug!
777 MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset());
778 } else if (++overflow >= overflow_limit) {
779 match_flags = 0; break; // got tired of looking at overflow
780 }
781 }
782 }
784 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
785 // watch out for these guys:
786 symbolOop init_name = vmSymbols::object_initializer_name();
787 symbolOop clinit_name = vmSymbols::class_initializer_name();
788 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit>
789 bool negate_name_test = false;
790 // fix name so that it captures the intention of IS_CONSTRUCTOR
791 if (!(match_flags & IS_METHOD)) {
792 // constructors only
793 if (name == NULL) {
794 name = init_name;
795 } else if (name != init_name) {
796 return 0; // no constructors of this method name
797 }
798 } else if (!(match_flags & IS_CONSTRUCTOR)) {
799 // methods only
800 if (name == NULL) {
801 name = init_name;
802 negate_name_test = true; // if we see the name, we *omit* the entry
803 } else if (name == init_name) {
804 return 0; // no methods of this constructor name
805 }
806 } else {
807 // caller will accept either sort; no need to adjust name
808 }
809 for (MethodStream st(k, local_only, !search_intfc); !st.eos(); st.next()) {
810 methodOop m = st.method();
811 symbolOop m_name = m->name();
812 if (m_name == clinit_name)
813 continue;
814 if (name != NULL && ((m_name != name) ^ negate_name_test))
815 continue;
816 if (sig != NULL && m->signature() != sig)
817 continue;
818 // passed the filters
819 if (rskip > 0) {
820 --rskip;
821 } else if (rfill < rlimit) {
822 oop result = results->obj_at(rfill++);
823 if (!sun_dyn_MemberName::is_instance(result))
824 return -99; // caller bug!
825 MethodHandles::init_MemberName(result, m, true);
826 } else if (++overflow >= overflow_limit) {
827 match_flags = 0; break; // got tired of looking at overflow
828 }
829 }
830 }
832 // return number of elements we at leasted wanted to initialize
833 return rfill + overflow;
834 }
837 // Decode this java.lang.Class object into an instanceKlass, if possible.
838 // Throw IAE if not
839 instanceKlassHandle MethodHandles::resolve_instance_klass(oop java_mirror_oop, TRAPS) {
840 instanceKlassHandle empty;
841 klassOop caller = NULL;
842 if (java_lang_Class::is_instance(java_mirror_oop)) {
843 caller = java_lang_Class::as_klassOop(java_mirror_oop);
844 }
845 if (caller == NULL || !Klass::cast(caller)->oop_is_instance()) {
846 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not a class", empty);
847 }
848 return instanceKlassHandle(THREAD, caller);
849 }
853 // Decode the vmtarget field of a method handle.
854 // Sanitize out methodOops, klassOops, and any other non-Java data.
855 // This is for debugging and reflection.
856 oop MethodHandles::encode_target(Handle mh, int format, TRAPS) {
857 assert(java_dyn_MethodHandle::is_instance(mh()), "must be a MH");
858 if (format == ETF_HANDLE_OR_METHOD_NAME) {
859 oop target = java_dyn_MethodHandle::vmtarget(mh());
860 if (target == NULL) {
861 return NULL; // unformed MH
862 }
863 klassOop tklass = target->klass();
864 if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::Object_klass())) {
865 return target; // target is another MH (or something else?)
866 }
867 }
868 if (format == ETF_DIRECT_HANDLE) {
869 oop target = mh();
870 for (;;) {
871 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) {
872 return target;
873 }
874 if (!java_dyn_MethodHandle::is_instance(target)){
875 return NULL; // unformed MH
876 }
877 target = java_dyn_MethodHandle::vmtarget(target);
878 }
879 }
880 // cases of metadata in MH.vmtarget:
881 // - AMH can have methodOop for static invoke with bound receiver
882 // - DMH can have methodOop for static invoke (on variable receiver)
883 // - DMH can have klassOop for dispatched (non-static) invoke
884 klassOop receiver_limit = NULL;
885 int decode_flags = 0;
886 methodOop m = decode_MethodHandle(mh(), receiver_limit, decode_flags);
887 if (m == NULL) return NULL;
888 switch (format) {
889 case ETF_REFLECT_METHOD:
890 // same as jni_ToReflectedMethod:
891 if (m->is_initializer()) {
892 return Reflection::new_constructor(m, THREAD);
893 } else {
894 return Reflection::new_method(m, UseNewReflection, false, THREAD);
895 }
897 case ETF_HANDLE_OR_METHOD_NAME: // method, not handle
898 case ETF_METHOD_NAME:
899 {
900 if (SystemDictionary::MemberName_klass() == NULL) break;
901 instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass());
902 mname_klass->initialize(CHECK_NULL);
903 Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL);
904 sun_dyn_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED);
905 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0);
906 init_MemberName(mname(), m, do_dispatch);
907 expand_MemberName(mname, 0, CHECK_NULL);
908 return mname();
909 }
910 }
912 // Unknown format code.
913 char msg[50];
914 jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format);
915 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg);
916 }
918 static const char* always_null_names[] = {
919 "java/lang/Void",
920 "java/lang/Null",
921 //"java/lang/Nothing",
922 "sun/dyn/empty/Empty",
923 NULL
924 };
926 static bool is_always_null_type(klassOop klass) {
927 if (!Klass::cast(klass)->oop_is_instance()) return false;
928 instanceKlass* ik = instanceKlass::cast(klass);
929 // Must be on the boot class path:
930 if (ik->class_loader() != NULL) return false;
931 // Check the name.
932 symbolOop name = ik->name();
933 for (int i = 0; ; i++) {
934 const char* test_name = always_null_names[i];
935 if (test_name == NULL) break;
936 if (name->equals(test_name))
937 return true;
938 }
939 return false;
940 }
942 bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) {
943 if (src == dst || dst == SystemDictionary::Object_klass())
944 return false; // quickest checks
945 Klass* srck = Klass::cast(src);
946 Klass* dstk = Klass::cast(dst);
947 if (dstk->is_interface()) {
948 // interface receivers can safely be viewed as untyped,
949 // because interface calls always include a dynamic check
950 //dstk = Klass::cast(SystemDictionary::Object_klass());
951 return false;
952 }
953 if (srck->is_interface()) {
954 // interface arguments must be viewed as untyped
955 //srck = Klass::cast(SystemDictionary::Object_klass());
956 return true;
957 }
958 if (is_always_null_type(src)) {
959 // some source types are known to be never instantiated;
960 // they represent references which are always null
961 // such null references never fail to convert safely
962 return false;
963 }
964 return !srck->is_subclass_of(dstk->as_klassOop());
965 }
967 static oop object_java_mirror() {
968 return Klass::cast(SystemDictionary::Object_klass())->java_mirror();
969 }
971 bool MethodHandles::same_basic_type_for_arguments(BasicType src,
972 BasicType dst,
973 bool raw,
974 bool for_return) {
975 if (for_return) {
976 // return values can always be forgotten:
977 if (dst == T_VOID) return true;
978 if (src == T_VOID) return raw && (dst == T_INT);
979 // We allow caller to receive a garbage int, which is harmless.
980 // This trick is pulled by trusted code (see VerifyType.canPassRaw).
981 }
982 assert(src != T_VOID && dst != T_VOID, "should not be here");
983 if (src == dst) return true;
984 if (type2size[src] != type2size[dst]) return false;
985 if (src == T_OBJECT || dst == T_OBJECT) return false;
986 if (raw) return true; // bitwise reinterpretation; caller guarantees safety
987 // allow reinterpretation casts for integral widening
988 if (is_subword_type(src)) { // subwords can fit in int or other subwords
989 if (dst == T_INT) // any subword fits in an int
990 return true;
991 if (src == T_BOOLEAN) // boolean fits in any subword
992 return is_subword_type(dst);
993 if (src == T_BYTE && dst == T_SHORT)
994 return true; // remaining case: byte fits in short
995 }
996 // allow float/fixed reinterpretation casts
997 if (src == T_FLOAT) return dst == T_INT;
998 if (src == T_INT) return dst == T_FLOAT;
999 if (src == T_DOUBLE) return dst == T_LONG;
1000 if (src == T_LONG) return dst == T_DOUBLE;
1001 return false;
1002 }
1004 const char* MethodHandles::check_method_receiver(methodOop m,
1005 klassOop passed_recv_type) {
1006 assert(!m->is_static(), "caller resp.");
1007 if (passed_recv_type == NULL)
1008 return "receiver type is primitive";
1009 if (class_cast_needed(passed_recv_type, m->method_holder())) {
1010 Klass* formal = Klass::cast(m->method_holder());
1011 return SharedRuntime::generate_class_cast_message("receiver type",
1012 formal->external_name());
1013 }
1014 return NULL; // checks passed
1015 }
1017 // Verify that m's signature can be called type-safely by a method handle
1018 // of the given method type 'mtype'.
1019 // It takes a TRAPS argument because it must perform symbol lookups.
1020 void MethodHandles::verify_method_signature(methodHandle m,
1021 Handle mtype,
1022 int first_ptype_pos,
1023 KlassHandle insert_ptype,
1024 TRAPS) {
1025 objArrayHandle ptypes(THREAD, java_dyn_MethodType::ptypes(mtype()));
1026 int pnum = first_ptype_pos;
1027 int pmax = ptypes->length();
1028 int mnum = 0; // method argument
1029 const char* err = NULL;
1030 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
1031 oop ptype_oop = NULL;
1032 if (ss.at_return_type()) {
1033 if (pnum != pmax)
1034 { err = "too many arguments"; break; }
1035 ptype_oop = java_dyn_MethodType::rtype(mtype());
1036 } else {
1037 if (pnum >= pmax)
1038 { err = "not enough arguments"; break; }
1039 if (pnum >= 0)
1040 ptype_oop = ptypes->obj_at(pnum);
1041 else if (insert_ptype.is_null())
1042 ptype_oop = NULL;
1043 else
1044 ptype_oop = insert_ptype->java_mirror();
1045 pnum += 1;
1046 mnum += 1;
1047 }
1048 klassOop pklass = NULL;
1049 BasicType ptype = T_OBJECT;
1050 if (ptype_oop != NULL)
1051 ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass);
1052 else
1053 // null does not match any non-reference; use Object to report the error
1054 pklass = SystemDictionary::Object_klass();
1055 klassOop mklass = NULL;
1056 BasicType mtype = ss.type();
1057 if (mtype == T_ARRAY) mtype = T_OBJECT; // fold all refs to T_OBJECT
1058 if (mtype == T_OBJECT) {
1059 if (ptype_oop == NULL) {
1060 // null matches any reference
1061 continue;
1062 }
1063 KlassHandle pklass_handle(THREAD, pklass); pklass = NULL;
1064 // If we fail to resolve types at this point, we will throw an error.
1065 symbolOop name_oop = ss.as_symbol(CHECK);
1066 symbolHandle name(THREAD, name_oop);
1067 instanceKlass* mk = instanceKlass::cast(m->method_holder());
1068 Handle loader(THREAD, mk->class_loader());
1069 Handle domain(THREAD, mk->protection_domain());
1070 mklass = SystemDictionary::resolve_or_null(name, loader, domain, CHECK);
1071 pklass = pklass_handle();
1072 if (mklass == NULL && pklass != NULL &&
1073 Klass::cast(pklass)->name() == name() &&
1074 m->is_method_handle_invoke()) {
1075 // Assume a match. We can't really decode the signature of MH.invoke*.
1076 continue;
1077 }
1078 }
1079 if (!ss.at_return_type()) {
1080 err = check_argument_type_change(ptype, pklass, mtype, mklass, mnum);
1081 } else {
1082 err = check_return_type_change(mtype, mklass, ptype, pklass); // note reversal!
1083 }
1084 if (err != NULL) break;
1085 }
1087 if (err != NULL) {
1088 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
1089 }
1090 }
1092 // Main routine for verifying the MethodHandle.type of a proposed
1093 // direct or bound-direct method handle.
1094 void MethodHandles::verify_method_type(methodHandle m,
1095 Handle mtype,
1096 bool has_bound_recv,
1097 KlassHandle bound_recv_type,
1098 TRAPS) {
1099 bool m_needs_receiver = !m->is_static();
1101 const char* err = NULL;
1103 int first_ptype_pos = m_needs_receiver ? 1 : 0;
1104 if (has_bound_recv) {
1105 first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them
1106 if (m_needs_receiver && bound_recv_type.is_null())
1107 { err = "bound receiver is not an object"; goto die; }
1108 }
1110 if (m_needs_receiver && err == NULL) {
1111 objArrayOop ptypes = java_dyn_MethodType::ptypes(mtype());
1112 if (ptypes->length() < first_ptype_pos)
1113 { err = "receiver argument is missing"; goto die; }
1114 if (has_bound_recv)
1115 err = check_method_receiver(m(), bound_recv_type->as_klassOop());
1116 else
1117 err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1)));
1118 if (err != NULL) goto die;
1119 }
1121 // Check the other arguments for mistypes.
1122 verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK);
1123 return;
1125 die:
1126 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
1127 }
1129 void MethodHandles::verify_vmslots(Handle mh, TRAPS) {
1130 // Verify vmslots.
1131 int check_slots = argument_slot_count(java_dyn_MethodHandle::type(mh()));
1132 if (java_dyn_MethodHandle::vmslots(mh()) != check_slots) {
1133 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH");
1134 }
1135 }
1137 void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) {
1138 // Verify that argslot points at the given argnum.
1139 int check_slot = argument_slot(java_dyn_MethodHandle::type(mh()), argnum);
1140 if (argslot != check_slot || argslot < 0) {
1141 const char* fmt = "for argnum of %d, vmargslot is %d, should be %d";
1142 size_t msglen = strlen(fmt) + 3*11 + 1;
1143 char* msg = NEW_RESOURCE_ARRAY(char, msglen);
1144 jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot);
1145 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
1146 }
1147 }
1149 // Verify the correspondence between two method types.
1150 // Apart from the advertised changes, caller method type X must
1151 // be able to invoke the callee method Y type with no violations
1152 // of type integrity.
1153 // Return NULL if all is well, else a short error message.
1154 const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end,
1155 int insert_argnum, oop insert_type,
1156 int change_argnum, oop change_type,
1157 int delete_argnum,
1158 oop dst_mtype, int dst_beg, int dst_end,
1159 bool raw) {
1160 objArrayOop src_ptypes = java_dyn_MethodType::ptypes(src_mtype);
1161 objArrayOop dst_ptypes = java_dyn_MethodType::ptypes(dst_mtype);
1163 int src_max = src_ptypes->length();
1164 int dst_max = dst_ptypes->length();
1166 if (src_end == -1) src_end = src_max;
1167 if (dst_end == -1) dst_end = dst_max;
1169 assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob");
1170 assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob");
1172 // pending actions; set to -1 when done:
1173 int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum;
1175 const char* err = NULL;
1177 // Walk along each array of parameter types, including a virtual
1178 // NULL end marker at the end of each.
1179 for (int src_idx = src_beg, dst_idx = dst_beg;
1180 (src_idx <= src_end && dst_idx <= dst_end);
1181 src_idx++, dst_idx++) {
1182 oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx);
1183 oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx);
1184 bool fix_null_src_type = false;
1186 // Perform requested edits.
1187 if (ins_idx == src_idx) {
1188 // note that the inserted guy is never affected by a change or deletion
1189 ins_idx = -1;
1190 src_type = insert_type;
1191 fix_null_src_type = true;
1192 --src_idx; // back up to process src type on next loop
1193 src_idx = src_end;
1194 } else {
1195 // note that the changed guy can be immediately deleted
1196 if (chg_idx == src_idx) {
1197 chg_idx = -1;
1198 assert(src_idx < src_end, "oob");
1199 src_type = change_type;
1200 fix_null_src_type = true;
1201 }
1202 if (del_idx == src_idx) {
1203 del_idx = -1;
1204 assert(src_idx < src_end, "oob");
1205 --dst_idx;
1206 continue; // rerun loop after skipping this position
1207 }
1208 }
1210 if (src_type == NULL && fix_null_src_type)
1211 // explicit null in this case matches any dest reference
1212 src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type);
1214 // Compare the two argument types.
1215 if (src_type != dst_type) {
1216 if (src_type == NULL) return "not enough arguments";
1217 if (dst_type == NULL) return "too many arguments";
1218 err = check_argument_type_change(src_type, dst_type, dst_idx, raw);
1219 if (err != NULL) return err;
1220 }
1221 }
1223 // Now compare return types also.
1224 oop src_rtype = java_dyn_MethodType::rtype(src_mtype);
1225 oop dst_rtype = java_dyn_MethodType::rtype(dst_mtype);
1226 if (src_rtype != dst_rtype) {
1227 err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal!
1228 if (err != NULL) return err;
1229 }
1231 assert(err == NULL, "");
1232 return NULL; // all is well
1233 }
1236 const char* MethodHandles::check_argument_type_change(BasicType src_type,
1237 klassOop src_klass,
1238 BasicType dst_type,
1239 klassOop dst_klass,
1240 int argnum,
1241 bool raw) {
1242 const char* err = NULL;
1243 bool for_return = (argnum < 0);
1245 // just in case:
1246 if (src_type == T_ARRAY) src_type = T_OBJECT;
1247 if (dst_type == T_ARRAY) dst_type = T_OBJECT;
1249 // Produce some nice messages if VerifyMethodHandles is turned on:
1250 if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) {
1251 if (src_type == T_OBJECT) {
1252 if (raw && dst_type == T_INT && is_always_null_type(src_klass))
1253 return NULL; // OK to convert a null pointer to a garbage int
1254 err = ((argnum >= 0)
1255 ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s"
1256 : "type mismatch: returning a %s, but caller expects primitive %s");
1257 } else if (dst_type == T_OBJECT) {
1258 err = ((argnum >= 0)
1259 ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s"
1260 : "type mismatch: returning a primitive %s, but caller expects %s");
1261 } else {
1262 err = ((argnum >= 0)
1263 ? "type mismatch: passing a %s for method argument #%d, which expects %s"
1264 : "type mismatch: returning a %s, but caller expects %s");
1265 }
1266 } else if (src_type == T_OBJECT && dst_type == T_OBJECT &&
1267 class_cast_needed(src_klass, dst_klass)) {
1268 if (!class_cast_needed(dst_klass, src_klass)) {
1269 if (raw)
1270 return NULL; // reverse cast is OK; the MH target is trusted to enforce it
1271 err = ((argnum >= 0)
1272 ? "cast required: passing a %s for method argument #%d, which expects %s"
1273 : "cast required: returning a %s, but caller expects %s");
1274 } else {
1275 err = ((argnum >= 0)
1276 ? "reference mismatch: passing a %s for method argument #%d, which expects %s"
1277 : "reference mismatch: returning a %s, but caller expects %s");
1278 }
1279 } else {
1280 // passed the obstacle course
1281 return NULL;
1282 }
1284 // format, format, format
1285 const char* src_name = type2name(src_type);
1286 const char* dst_name = type2name(dst_type);
1287 if (src_type == T_OBJECT) src_name = Klass::cast(src_klass)->external_name();
1288 if (dst_type == T_OBJECT) dst_name = Klass::cast(dst_klass)->external_name();
1289 if (src_name == NULL) src_name = "unknown type";
1290 if (dst_name == NULL) dst_name = "unknown type";
1292 size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11);
1293 char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1);
1294 if (argnum >= 0) {
1295 assert(strstr(err, "%d") != NULL, "");
1296 jio_snprintf(msg, msglen, err, src_name, argnum, dst_name);
1297 } else {
1298 assert(strstr(err, "%d") == NULL, "");
1299 jio_snprintf(msg, msglen, err, src_name, dst_name);
1300 }
1301 return msg;
1302 }
1304 // Compute the depth within the stack of the given argument, i.e.,
1305 // the combined size of arguments to the right of the given argument.
1306 // For the last argument (ptypes.length-1) this will be zero.
1307 // For the first argument (0) this will be the size of all
1308 // arguments but that one. For the special number -1, this
1309 // will be the size of all arguments, including the first.
1310 // If the argument is neither -1 nor a valid argument index,
1311 // then return a negative number. Otherwise, the result
1312 // is in the range [0..vmslots] inclusive.
1313 int MethodHandles::argument_slot(oop method_type, int arg) {
1314 objArrayOop ptypes = java_dyn_MethodType::ptypes(method_type);
1315 int argslot = 0;
1316 int len = ptypes->length();
1317 if (arg < -1 || arg >= len) return -99;
1318 for (int i = len-1; i > arg; i--) {
1319 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i));
1320 argslot += type2size[bt];
1321 }
1322 assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works");
1323 return argslot;
1324 }
1326 // Given a slot number, return the argument number.
1327 int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) {
1328 objArrayOop ptypes = java_dyn_MethodType::ptypes(method_type);
1329 int argslot = 0;
1330 int len = ptypes->length();
1331 for (int i = len-1; i >= 0; i--) {
1332 if (query_argslot == argslot) return i;
1333 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i));
1334 argslot += type2size[bt];
1335 }
1336 // return pseudo-arg deepest in stack:
1337 if (query_argslot == argslot) return -1;
1338 return -99; // oob slot, or splitting a double-slot arg
1339 }
1341 methodHandle MethodHandles::dispatch_decoded_method(methodHandle m,
1342 KlassHandle receiver_limit,
1343 int decode_flags,
1344 KlassHandle receiver_klass,
1345 TRAPS) {
1346 assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference");
1347 assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument");
1349 if (!m->is_static() &&
1350 (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder())))
1351 // given type does not match class of method, or receiver is null!
1352 // caller should have checked this, but let's be extra careful...
1353 return methodHandle();
1355 if (receiver_limit.not_null() &&
1356 (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit())))
1357 // given type is not limited to the receiver type
1358 // note that a null receiver can match any reference value, for a static method
1359 return methodHandle();
1361 if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) {
1362 // pre-dispatched or static method (null receiver is OK for static)
1363 return m;
1365 } else if (receiver_klass.is_null()) {
1366 // null receiver value; cannot dispatch
1367 return methodHandle();
1369 } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) {
1370 // perform virtual dispatch
1371 int vtable_index = m->vtable_index();
1372 guarantee(vtable_index >= 0, "valid vtable index");
1374 // receiver_klass might be an arrayKlassOop but all vtables start at
1375 // the same place. The cast is to avoid virtual call and assertion.
1376 // See also LinkResolver::runtime_resolve_virtual_method.
1377 instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass());
1378 DEBUG_ONLY(inst->verify_vtable_index(vtable_index));
1379 methodOop m_oop = inst->method_at_vtable(vtable_index);
1380 return methodHandle(THREAD, m_oop);
1382 } else {
1383 // perform interface dispatch
1384 int itable_index = klassItable::compute_itable_index(m());
1385 guarantee(itable_index >= 0, "valid itable index");
1386 instanceKlass* inst = instanceKlass::cast(receiver_klass());
1387 methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD);
1388 return methodHandle(THREAD, m_oop);
1389 }
1390 }
1392 void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) {
1393 // Verify type.
1394 Handle mtype(THREAD, java_dyn_MethodHandle::type(mh()));
1395 verify_method_type(m, mtype, false, KlassHandle(), CHECK);
1397 // Verify vmslots.
1398 if (java_dyn_MethodHandle::vmslots(mh()) != m->size_of_parameters()) {
1399 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH");
1400 }
1401 }
1403 void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) {
1404 // Check arguments.
1405 if (mh.is_null() || m.is_null() ||
1406 (!do_dispatch && m->is_abstract())) {
1407 THROW(vmSymbols::java_lang_InternalError());
1408 }
1410 java_dyn_MethodHandle::init_vmslots(mh());
1412 if (VerifyMethodHandles) {
1413 // The privileged code which invokes this routine should not make
1414 // a mistake about types, but it's better to verify.
1415 verify_DirectMethodHandle(mh, m, CHECK);
1416 }
1418 // Finally, after safety checks are done, link to the target method.
1419 // We will follow the same path as the latter part of
1420 // InterpreterRuntime::resolve_invoke(), which first finds the method
1421 // and then decides how to populate the constant pool cache entry
1422 // that links the interpreter calls to the method. We need the same
1423 // bits, and will use the same calling sequence code.
1425 int vmindex = methodOopDesc::garbage_vtable_index;
1426 oop vmtarget = NULL;
1428 instanceKlass::cast(m->method_holder())->link_class(CHECK);
1430 MethodHandleEntry* me = NULL;
1431 if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) {
1432 // We are simulating an invokeinterface instruction.
1433 // (We might also be simulating an invokevirtual on a miranda method,
1434 // but it is safe to treat it as an invokeinterface.)
1435 assert(!m->can_be_statically_bound(), "no final methods on interfaces");
1436 vmindex = klassItable::compute_itable_index(m());
1437 assert(vmindex >= 0, "(>=0) == do_dispatch");
1438 // Set up same bits as ConstantPoolCacheEntry::set_interface_call().
1439 vmtarget = m->method_holder(); // the interface
1440 me = MethodHandles::entry(MethodHandles::_invokeinterface_mh);
1441 } else if (!do_dispatch || m->can_be_statically_bound()) {
1442 // We are simulating an invokestatic or invokespecial instruction.
1443 // Set up the method pointer, just like ConstantPoolCacheEntry::set_method().
1444 vmtarget = m();
1445 // this does not help dispatch, but it will make it possible to parse this MH:
1446 vmindex = methodOopDesc::nonvirtual_vtable_index;
1447 assert(vmindex < 0, "(>=0) == do_dispatch");
1448 if (!m->is_static()) {
1449 me = MethodHandles::entry(MethodHandles::_invokespecial_mh);
1450 } else {
1451 me = MethodHandles::entry(MethodHandles::_invokestatic_mh);
1452 // Part of the semantics of a static call is an initialization barrier.
1453 // For a DMH, it is done now, when the handle is created.
1454 Klass* k = Klass::cast(m->method_holder());
1455 if (k->should_be_initialized()) {
1456 k->initialize(CHECK);
1457 }
1458 }
1459 } else {
1460 // We are simulating an invokevirtual instruction.
1461 // Set up the vtable index, just like ConstantPoolCacheEntry::set_method().
1462 // The key logic is LinkResolver::runtime_resolve_virtual_method.
1463 vmindex = m->vtable_index();
1464 vmtarget = m->method_holder();
1465 me = MethodHandles::entry(MethodHandles::_invokevirtual_mh);
1466 }
1468 if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
1470 sun_dyn_DirectMethodHandle::set_vmtarget(mh(), vmtarget);
1471 sun_dyn_DirectMethodHandle::set_vmindex(mh(), vmindex);
1472 DEBUG_ONLY(int flags; klassOop rlimit);
1473 assert(MethodHandles::decode_method(mh(), rlimit, flags) == m(),
1474 "properly stored for later decoding");
1475 DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0));
1476 assert(!(actual_do_dispatch && !do_dispatch),
1477 "do not perform dispatch if !do_dispatch specified");
1478 assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch");
1479 assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack");
1481 // Done!
1482 java_dyn_MethodHandle::set_vmentry(mh(), me);
1483 }
1485 void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh,
1486 methodHandle m,
1487 TRAPS) {
1488 // Verify type.
1489 oop receiver = sun_dyn_BoundMethodHandle::argument(mh());
1490 Handle mtype(THREAD, java_dyn_MethodHandle::type(mh()));
1491 KlassHandle bound_recv_type;
1492 if (receiver != NULL) bound_recv_type = KlassHandle(THREAD, receiver->klass());
1493 verify_method_type(m, mtype, true, bound_recv_type, CHECK);
1495 int receiver_pos = m->size_of_parameters() - 1;
1497 // Verify MH.vmargslot, which should point at the bound receiver.
1498 verify_vmargslot(mh, -1, sun_dyn_BoundMethodHandle::vmargslot(mh()), CHECK);
1499 //verify_vmslots(mh, CHECK);
1501 // Verify vmslots.
1502 if (java_dyn_MethodHandle::vmslots(mh()) != receiver_pos) {
1503 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)");
1504 }
1505 }
1507 // Initialize a BMH with a receiver bound directly to a methodOop.
1508 void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh,
1509 methodHandle original_m,
1510 KlassHandle receiver_limit,
1511 int decode_flags,
1512 TRAPS) {
1513 // Check arguments.
1514 if (mh.is_null() || original_m.is_null()) {
1515 THROW(vmSymbols::java_lang_InternalError());
1516 }
1518 KlassHandle receiver_klass;
1519 {
1520 oop receiver_oop = sun_dyn_BoundMethodHandle::argument(mh());
1521 if (receiver_oop != NULL)
1522 receiver_klass = KlassHandle(THREAD, receiver_oop->klass());
1523 }
1524 methodHandle m = dispatch_decoded_method(original_m,
1525 receiver_limit, decode_flags,
1526 receiver_klass,
1527 CHECK);
1528 if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); }
1529 if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); }
1531 java_dyn_MethodHandle::init_vmslots(mh());
1533 if (VerifyMethodHandles) {
1534 verify_BoundMethodHandle_with_receiver(mh, m, CHECK);
1535 }
1537 sun_dyn_BoundMethodHandle::set_vmtarget(mh(), m());
1539 DEBUG_ONLY(int junk; klassOop junk2);
1540 assert(MethodHandles::decode_method(mh(), junk2, junk) == m(), "properly stored for later decoding");
1541 assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot");
1543 // Done!
1544 java_dyn_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh));
1545 }
1547 void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum,
1548 bool direct_to_method, TRAPS) {
1549 Handle ptype_handle(THREAD,
1550 java_dyn_MethodType::ptype(java_dyn_MethodHandle::type(target()), argnum));
1551 KlassHandle ptype_klass;
1552 BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass);
1553 int slots_pushed = type2size[ptype];
1555 oop argument = sun_dyn_BoundMethodHandle::argument(mh());
1557 const char* err = NULL;
1559 switch (ptype) {
1560 case T_OBJECT:
1561 if (argument != NULL)
1562 // we must implicitly convert from the arg type to the outgoing ptype
1563 err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum);
1564 break;
1566 case T_ARRAY: case T_VOID:
1567 assert(false, "array, void do not appear here");
1568 default:
1569 if (ptype != T_INT && !is_subword_type(ptype)) {
1570 err = "unexpected parameter type";
1571 break;
1572 }
1573 // check subrange of Integer.value, if necessary
1574 if (argument == NULL || argument->klass() != SystemDictionary::Integer_klass()) {
1575 err = "bound integer argument must be of type java.lang.Integer";
1576 break;
1577 }
1578 if (ptype != T_INT) {
1579 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
1580 jint value = argument->int_field(value_offset);
1581 int vminfo = adapter_unbox_subword_vminfo(ptype);
1582 jint subword = truncate_subword_from_vminfo(value, vminfo);
1583 if (value != subword) {
1584 err = "bound subword value does not fit into the subword type";
1585 break;
1586 }
1587 }
1588 break;
1589 case T_FLOAT:
1590 case T_DOUBLE:
1591 case T_LONG:
1592 {
1593 // we must implicitly convert from the unboxed arg type to the outgoing ptype
1594 BasicType argbox = java_lang_boxing_object::basic_type(argument);
1595 if (argbox != ptype) {
1596 err = check_argument_type_change(T_OBJECT, (argument == NULL
1597 ? SystemDictionary::Object_klass()
1598 : argument->klass()),
1599 ptype, ptype_klass(), argnum);
1600 assert(err != NULL, "this must be an error");
1601 }
1602 break;
1603 }
1604 }
1606 if (err == NULL) {
1607 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh()));
1608 if (direct_to_method) {
1609 assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots");
1610 assert(slots_pushed <= MethodHandlePushLimit, "");
1611 } else {
1612 int target_pushes = decode_MethodHandle_stack_pushes(target());
1613 assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct");
1614 // do not blow the stack; use a Java-based adapter if this limit is exceeded
1615 // FIXME
1616 // if (slots_pushed + target_pushes > MethodHandlePushLimit)
1617 // err = "too many bound parameters";
1618 }
1619 }
1621 if (err == NULL) {
1622 // Verify the rest of the method type.
1623 err = check_method_type_insertion(java_dyn_MethodHandle::type(mh()),
1624 argnum, ptype_handle(),
1625 java_dyn_MethodHandle::type(target()));
1626 }
1628 if (err != NULL) {
1629 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
1630 }
1631 }
1633 void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) {
1634 // Check arguments.
1635 if (mh.is_null() || target.is_null() || !java_dyn_MethodHandle::is_instance(target())) {
1636 THROW(vmSymbols::java_lang_InternalError());
1637 }
1639 java_dyn_MethodHandle::init_vmslots(mh());
1641 if (VerifyMethodHandles) {
1642 int insert_after = argnum - 1;
1643 verify_vmargslot(mh, insert_after, sun_dyn_BoundMethodHandle::vmargslot(mh()), CHECK);
1644 verify_vmslots(mh, CHECK);
1645 }
1647 // Get bound type and required slots.
1648 oop ptype_oop = java_dyn_MethodType::ptype(java_dyn_MethodHandle::type(target()), argnum);
1649 BasicType ptype = java_lang_Class::as_BasicType(ptype_oop);
1650 int slots_pushed = type2size[ptype];
1652 // If (a) the target is a direct non-dispatched method handle,
1653 // or (b) the target is a dispatched direct method handle and we
1654 // are binding the receiver, cut out the middle-man.
1655 // Do this by decoding the DMH and using its methodOop directly as vmtarget.
1656 bool direct_to_method = false;
1657 if (OptimizeMethodHandles &&
1658 target->klass() == SystemDictionary::DirectMethodHandle_klass() &&
1659 (argnum == 0 || sun_dyn_DirectMethodHandle::vmindex(target()) < 0)) {
1660 int decode_flags = 0; klassOop receiver_limit_oop = NULL;
1661 methodHandle m(THREAD, decode_method(target(), receiver_limit_oop, decode_flags));
1662 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); }
1663 DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg.
1664 assert(sun_dyn_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig");
1665 if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) {
1666 KlassHandle receiver_limit(THREAD, receiver_limit_oop);
1667 init_BoundMethodHandle_with_receiver(mh, m,
1668 receiver_limit, decode_flags,
1669 CHECK);
1670 return;
1671 }
1673 // Even if it is not a bound receiver, we still might be able
1674 // to bind another argument and still invoke the methodOop directly.
1675 if (!(decode_flags & _dmf_does_dispatch)) {
1676 direct_to_method = true;
1677 sun_dyn_BoundMethodHandle::set_vmtarget(mh(), m());
1678 }
1679 }
1680 if (!direct_to_method)
1681 sun_dyn_BoundMethodHandle::set_vmtarget(mh(), target());
1683 if (VerifyMethodHandles) {
1684 verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK);
1685 }
1687 // Next question: Is this a ref, int, or long bound value?
1688 MethodHandleEntry* me = NULL;
1689 if (ptype == T_OBJECT) {
1690 if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh);
1691 else me = MethodHandles::entry(_bound_ref_mh);
1692 } else if (slots_pushed == 2) {
1693 if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh);
1694 else me = MethodHandles::entry(_bound_long_mh);
1695 } else if (slots_pushed == 1) {
1696 if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh);
1697 else me = MethodHandles::entry(_bound_int_mh);
1698 } else {
1699 assert(false, "");
1700 }
1702 // Done!
1703 java_dyn_MethodHandle::set_vmentry(mh(), me);
1704 }
1706 static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) {
1707 char msg[200];
1708 jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err);
1709 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg);
1710 }
1712 void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) {
1713 jint conversion = sun_dyn_AdapterMethodHandle::conversion(mh());
1714 int argslot = sun_dyn_AdapterMethodHandle::vmargslot(mh());
1716 verify_vmargslot(mh, argnum, argslot, CHECK);
1717 verify_vmslots(mh, CHECK);
1719 jint conv_op = adapter_conversion_op(conversion);
1720 if (!conv_op_valid(conv_op)) {
1721 throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD);
1722 return;
1723 }
1724 EntryKind ek = adapter_entry_kind(conv_op);
1726 int stack_move = adapter_conversion_stack_move(conversion);
1727 BasicType src = adapter_conversion_src_type(conversion);
1728 BasicType dest = adapter_conversion_dest_type(conversion);
1729 int vminfo = adapter_conversion_vminfo(conversion); // should be zero
1731 Handle argument(THREAD, sun_dyn_AdapterMethodHandle::argument(mh()));
1732 Handle target(THREAD, sun_dyn_AdapterMethodHandle::vmtarget(mh()));
1733 Handle src_mtype(THREAD, java_dyn_MethodHandle::type(mh()));
1734 Handle dst_mtype(THREAD, java_dyn_MethodHandle::type(target()));
1736 const char* err = NULL;
1738 if (err == NULL) {
1739 // Check that the correct argument is supplied, but only if it is required.
1740 switch (ek) {
1741 case _adapter_check_cast: // target type of cast
1742 case _adapter_ref_to_prim: // wrapper type from which to unbox
1743 case _adapter_prim_to_ref: // wrapper type to box into
1744 case _adapter_collect_args: // array type to collect into
1745 case _adapter_spread_args: // array type to spread from
1746 if (!java_lang_Class::is_instance(argument())
1747 || java_lang_Class::is_primitive(argument()))
1748 { err = "adapter requires argument of type java.lang.Class"; break; }
1749 if (ek == _adapter_collect_args ||
1750 ek == _adapter_spread_args) {
1751 // Make sure it is a suitable collection type. (Array, for now.)
1752 Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument()));
1753 if (!ak->oop_is_objArray()) {
1754 { err = "adapter requires argument of type java.lang.Class<Object[]>"; break; }
1755 }
1756 }
1757 break;
1758 case _adapter_flyby:
1759 case _adapter_ricochet:
1760 if (!java_dyn_MethodHandle::is_instance(argument()))
1761 { err = "MethodHandle adapter argument required"; break; }
1762 break;
1763 default:
1764 if (argument.not_null())
1765 { err = "adapter has spurious argument"; break; }
1766 break;
1767 }
1768 }
1770 if (err == NULL) {
1771 // Check that the src/dest types are supplied if needed.
1772 switch (ek) {
1773 case _adapter_check_cast:
1774 if (src != T_OBJECT || dest != T_OBJECT) {
1775 err = "adapter requires object src/dest conversion subfields";
1776 }
1777 break;
1778 case _adapter_prim_to_prim:
1779 if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) {
1780 err = "adapter requires primitive src/dest conversion subfields"; break;
1781 }
1782 if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) ||
1783 !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) {
1784 err = "adapter cannot convert beween floating and fixed-point"; break;
1785 }
1786 break;
1787 case _adapter_ref_to_prim:
1788 if (src != T_OBJECT || !is_java_primitive(dest)
1789 || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) {
1790 err = "adapter requires primitive dest conversion subfield"; break;
1791 }
1792 break;
1793 case _adapter_prim_to_ref:
1794 if (!is_java_primitive(src) || dest != T_OBJECT
1795 || argument() != Klass::cast(SystemDictionary::box_klass(src))->java_mirror()) {
1796 err = "adapter requires primitive src conversion subfield"; break;
1797 }
1798 break;
1799 case _adapter_swap_args:
1800 case _adapter_rot_args:
1801 {
1802 if (!src || src != dest) {
1803 err = "adapter requires src/dest conversion subfields for swap"; break;
1804 }
1805 int swap_size = type2size[src];
1806 oop src_mtype = sun_dyn_AdapterMethodHandle::type(mh());
1807 oop dest_mtype = sun_dyn_AdapterMethodHandle::type(target());
1808 int slot_limit = sun_dyn_AdapterMethodHandle::vmslots(target());
1809 int src_slot = argslot;
1810 int dest_slot = vminfo;
1811 bool rotate_up = (src_slot > dest_slot); // upward rotation
1812 int src_arg = argnum;
1813 int dest_arg = argument_slot_to_argnum(dest_mtype, dest_slot);
1814 verify_vmargslot(mh, dest_arg, dest_slot, CHECK);
1815 if (!(dest_slot >= src_slot + swap_size) &&
1816 !(src_slot >= dest_slot + swap_size)) {
1817 err = "source, destination slots must be distinct";
1818 } else if (ek == _adapter_swap_args && !(src_slot > dest_slot)) {
1819 err = "source of swap must be deeper in stack";
1820 } else if (ek == _adapter_swap_args) {
1821 err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, dest_arg),
1822 java_dyn_MethodType::ptype(dest_mtype, src_arg),
1823 dest_arg);
1824 } else if (ek == _adapter_rot_args) {
1825 if (rotate_up) {
1826 assert((src_slot > dest_slot) && (src_arg < dest_arg), "");
1827 // rotate up: [dest_slot..src_slot-ss] --> [dest_slot+ss..src_slot]
1828 // that is: [src_arg+1..dest_arg] --> [src_arg..dest_arg-1]
1829 for (int i = src_arg+1; i <= dest_arg && err == NULL; i++) {
1830 err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, i),
1831 java_dyn_MethodType::ptype(dest_mtype, i-1),
1832 i);
1833 }
1834 } else { // rotate down
1835 assert((src_slot < dest_slot) && (src_arg > dest_arg), "");
1836 // rotate down: [src_slot+ss..dest_slot] --> [src_slot..dest_slot-ss]
1837 // that is: [dest_arg..src_arg-1] --> [dst_arg+1..src_arg]
1838 for (int i = dest_arg; i <= src_arg-1 && err == NULL; i++) {
1839 err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, i),
1840 java_dyn_MethodType::ptype(dest_mtype, i+1),
1841 i);
1842 }
1843 }
1844 }
1845 if (err == NULL)
1846 err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, src_arg),
1847 java_dyn_MethodType::ptype(dest_mtype, dest_arg),
1848 src_arg);
1849 }
1850 break;
1851 case _adapter_collect_args:
1852 case _adapter_spread_args:
1853 {
1854 BasicType coll_type = (ek == _adapter_collect_args) ? dest : src;
1855 BasicType elem_type = (ek == _adapter_collect_args) ? src : dest;
1856 if (coll_type != T_OBJECT || elem_type != T_OBJECT) {
1857 err = "adapter requires src/dest subfields"; break;
1858 // later:
1859 // - consider making coll be a primitive array
1860 // - consider making coll be a heterogeneous collection
1861 }
1862 }
1863 break;
1864 default:
1865 if (src != 0 || dest != 0) {
1866 err = "adapter has spurious src/dest conversion subfields"; break;
1867 }
1868 break;
1869 }
1870 }
1872 if (err == NULL) {
1873 // Check the stack_move subfield.
1874 // It must always report the net change in stack size, positive or negative.
1875 int slots_pushed = stack_move / stack_move_unit();
1876 switch (ek) {
1877 case _adapter_prim_to_prim:
1878 case _adapter_ref_to_prim:
1879 case _adapter_prim_to_ref:
1880 if (slots_pushed != type2size[dest] - type2size[src]) {
1881 err = "wrong stack motion for primitive conversion";
1882 }
1883 break;
1884 case _adapter_dup_args:
1885 if (slots_pushed <= 0) {
1886 err = "adapter requires conversion subfield slots_pushed > 0";
1887 }
1888 break;
1889 case _adapter_drop_args:
1890 if (slots_pushed >= 0) {
1891 err = "adapter requires conversion subfield slots_pushed < 0";
1892 }
1893 break;
1894 case _adapter_collect_args:
1895 if (slots_pushed > 1) {
1896 err = "adapter requires conversion subfield slots_pushed <= 1";
1897 }
1898 break;
1899 case _adapter_spread_args:
1900 if (slots_pushed < -1) {
1901 err = "adapter requires conversion subfield slots_pushed >= -1";
1902 }
1903 break;
1904 default:
1905 if (stack_move != 0) {
1906 err = "adapter has spurious stack_move conversion subfield";
1907 }
1908 break;
1909 }
1910 if (err == NULL && stack_move != slots_pushed * stack_move_unit()) {
1911 err = "stack_move conversion subfield must be multiple of stack_move_unit";
1912 }
1913 }
1915 if (err == NULL) {
1916 // Make sure this adapter does not push too deeply.
1917 int slots_pushed = stack_move / stack_move_unit();
1918 int this_vmslots = java_dyn_MethodHandle::vmslots(mh());
1919 int target_vmslots = java_dyn_MethodHandle::vmslots(target());
1920 if (slots_pushed != (target_vmslots - this_vmslots)) {
1921 err = "stack_move inconsistent with previous and current MethodType vmslots";
1922 } else if (slots_pushed > 0) {
1923 // verify stack_move against MethodHandlePushLimit
1924 int target_pushes = decode_MethodHandle_stack_pushes(target());
1925 // do not blow the stack; use a Java-based adapter if this limit is exceeded
1926 if (slots_pushed + target_pushes > MethodHandlePushLimit) {
1927 err = "adapter pushes too many parameters";
1928 }
1929 }
1931 // While we're at it, check that the stack motion decoder works:
1932 DEBUG_ONLY(int target_pushes = decode_MethodHandle_stack_pushes(target()));
1933 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh()));
1934 assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct");
1935 }
1937 if (err == NULL && vminfo != 0) {
1938 switch (ek) {
1939 case _adapter_swap_args:
1940 case _adapter_rot_args:
1941 break; // OK
1942 default:
1943 err = "vminfo subfield is reserved to the JVM";
1944 }
1945 }
1947 // Do additional ad hoc checks.
1948 if (err == NULL) {
1949 switch (ek) {
1950 case _adapter_retype_only:
1951 err = check_method_type_passthrough(src_mtype(), dst_mtype(), false);
1952 break;
1954 case _adapter_retype_raw:
1955 err = check_method_type_passthrough(src_mtype(), dst_mtype(), true);
1956 break;
1958 case _adapter_check_cast:
1959 {
1960 // The actual value being checked must be a reference:
1961 err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype(), argnum),
1962 object_java_mirror(), argnum);
1963 if (err != NULL) break;
1965 // The output of the cast must fit with the destination argument:
1966 Handle cast_class = argument;
1967 err = check_method_type_conversion(src_mtype(),
1968 argnum, cast_class(),
1969 dst_mtype());
1970 }
1971 break;
1973 // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles
1974 }
1975 }
1977 if (err != NULL) {
1978 throw_InternalError_for_bad_conversion(conversion, err, THREAD);
1979 return;
1980 }
1982 }
1984 void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) {
1985 oop argument = sun_dyn_AdapterMethodHandle::argument(mh());
1986 int argslot = sun_dyn_AdapterMethodHandle::vmargslot(mh());
1987 jint conversion = sun_dyn_AdapterMethodHandle::conversion(mh());
1988 jint conv_op = adapter_conversion_op(conversion);
1990 // adjust the adapter code to the internal EntryKind enumeration:
1991 EntryKind ek_orig = adapter_entry_kind(conv_op);
1992 EntryKind ek_opt = ek_orig; // may be optimized
1994 // Finalize the vmtarget field (Java initialized it to null).
1995 if (!java_dyn_MethodHandle::is_instance(target())) {
1996 throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD);
1997 return;
1998 }
1999 sun_dyn_AdapterMethodHandle::set_vmtarget(mh(), target());
2001 if (VerifyMethodHandles) {
2002 verify_AdapterMethodHandle(mh, argnum, CHECK);
2003 }
2005 int stack_move = adapter_conversion_stack_move(conversion);
2006 BasicType src = adapter_conversion_src_type(conversion);
2007 BasicType dest = adapter_conversion_dest_type(conversion);
2008 int vminfo = adapter_conversion_vminfo(conversion); // should be zero
2010 const char* err = NULL;
2012 // Now it's time to finish the case analysis and pick a MethodHandleEntry.
2013 switch (ek_orig) {
2014 case _adapter_retype_only:
2015 case _adapter_retype_raw:
2016 case _adapter_check_cast:
2017 case _adapter_dup_args:
2018 case _adapter_drop_args:
2019 // these work fine via general case code
2020 break;
2022 case _adapter_prim_to_prim:
2023 {
2024 // Non-subword cases are {int,float,long,double} -> {int,float,long,double}.
2025 // And, the {float,double} -> {int,long} cases must be handled by Java.
2026 switch (type2size[src] *4+ type2size[dest]) {
2027 case 1 *4+ 1:
2028 assert(src == T_INT || is_subword_type(src), "source is not float");
2029 // Subword-related cases are int -> {boolean,byte,char,short}.
2030 ek_opt = _adapter_opt_i2i;
2031 vminfo = adapter_prim_to_prim_subword_vminfo(dest);
2032 break;
2033 case 2 *4+ 1:
2034 if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) {
2035 ek_opt = _adapter_opt_l2i;
2036 vminfo = adapter_prim_to_prim_subword_vminfo(dest);
2037 } else if (src == T_DOUBLE && dest == T_FLOAT) {
2038 ek_opt = _adapter_opt_d2f;
2039 } else {
2040 assert(false, "");
2041 }
2042 break;
2043 case 1 *4+ 2:
2044 if (src == T_INT && dest == T_LONG) {
2045 ek_opt = _adapter_opt_i2l;
2046 } else if (src == T_FLOAT && dest == T_DOUBLE) {
2047 ek_opt = _adapter_opt_f2d;
2048 } else {
2049 assert(false, "");
2050 }
2051 break;
2052 default:
2053 assert(false, "");
2054 break;
2055 }
2056 }
2057 break;
2059 case _adapter_ref_to_prim:
2060 {
2061 switch (type2size[dest]) {
2062 case 1:
2063 ek_opt = _adapter_opt_unboxi;
2064 vminfo = adapter_unbox_subword_vminfo(dest);
2065 break;
2066 case 2:
2067 ek_opt = _adapter_opt_unboxl;
2068 break;
2069 default:
2070 assert(false, "");
2071 break;
2072 }
2073 }
2074 break;
2076 case _adapter_prim_to_ref:
2077 goto throw_not_impl; // allocates, hence could block
2079 case _adapter_swap_args:
2080 case _adapter_rot_args:
2081 {
2082 int swap_slots = type2size[src];
2083 int slot_limit = sun_dyn_AdapterMethodHandle::vmslots(mh());
2084 int src_slot = argslot;
2085 int dest_slot = vminfo;
2086 int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1;
2087 switch (swap_slots) {
2088 case 1:
2089 ek_opt = (!rotate ? _adapter_opt_swap_1 :
2090 rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down);
2091 break;
2092 case 2:
2093 ek_opt = (!rotate ? _adapter_opt_swap_2 :
2094 rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down);
2095 break;
2096 default:
2097 assert(false, "");
2098 break;
2099 }
2100 }
2101 break;
2103 case _adapter_collect_args:
2104 goto throw_not_impl; // allocates, hence could block
2106 case _adapter_spread_args:
2107 {
2108 // vminfo will be the required length of the array
2109 int slots_pushed = stack_move / stack_move_unit();
2110 int array_size = slots_pushed + 1;
2111 assert(array_size >= 0, "");
2112 vminfo = array_size;
2113 switch (array_size) {
2114 case 0: ek_opt = _adapter_opt_spread_0; break;
2115 case 1: ek_opt = _adapter_opt_spread_1; break;
2116 default: ek_opt = _adapter_opt_spread_more; break;
2117 }
2118 if ((vminfo & CONV_VMINFO_MASK) != vminfo)
2119 goto throw_not_impl; // overflow
2120 }
2121 break;
2123 case _adapter_flyby:
2124 case _adapter_ricochet:
2125 goto throw_not_impl; // runs Java code, hence could block
2127 default:
2128 // should have failed much earlier; must be a missing case here
2129 assert(false, "incomplete switch");
2130 // and fall through:
2132 throw_not_impl:
2133 // FIXME: these adapters are NYI
2134 err = "adapter not yet implemented in the JVM";
2135 break;
2136 }
2138 if (err != NULL) {
2139 throw_InternalError_for_bad_conversion(conversion, err, THREAD);
2140 return;
2141 }
2143 // Rebuild the conversion value; maybe parts of it were changed.
2144 jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo);
2146 // Finalize the conversion field. (Note that it is final to Java code.)
2147 sun_dyn_AdapterMethodHandle::set_conversion(mh(), new_conversion);
2149 // Done!
2150 java_dyn_MethodHandle::set_vmentry(mh(), entry(ek_opt));
2152 // There should be enough memory barriers on exit from native methods
2153 // to ensure that the MH is fully initialized to all threads before
2154 // Java code can publish it in global data structures.
2155 }
2157 //
2158 // Here are the native methods on sun.dyn.MethodHandleImpl.
2159 // They are the private interface between this JVM and the HotSpot-specific
2160 // Java code that implements JSR 292 method handles.
2161 //
2162 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way
2163 // that intrinsic (non-JNI) native methods are defined in HotSpot.
2164 //
2166 // direct method handles for invokestatic or invokespecial
2167 // void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class<?> caller);
2168 JVM_ENTRY(void, MHI_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh,
2169 jobject target_jh, jboolean do_dispatch, jobject caller_jh)) {
2170 ResourceMark rm; // for error messages
2172 // This is the guy we are initializing:
2173 if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2174 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
2176 // Early returns out of this method leave the DMH in an unfinished state.
2177 assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
2179 // which method are we really talking about?
2180 if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2181 oop target_oop = JNIHandles::resolve_non_null(target_jh);
2182 if (sun_dyn_MemberName::is_instance(target_oop) &&
2183 sun_dyn_MemberName::vmindex(target_oop) == VM_INDEX_UNINITIALIZED) {
2184 Handle mname(THREAD, target_oop);
2185 MethodHandles::resolve_MemberName(mname, CHECK);
2186 target_oop = mname(); // in case of GC
2187 }
2189 int decode_flags = 0; klassOop receiver_limit = NULL;
2190 methodHandle m(THREAD,
2191 MethodHandles::decode_method(target_oop,
2192 receiver_limit, decode_flags));
2193 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); }
2195 // The trusted Java code that calls this method should already have performed
2196 // access checks on behalf of the given caller. But, we can verify this.
2197 if (VerifyMethodHandles && caller_jh != NULL) {
2198 KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh)));
2199 // If this were a bytecode, the first access check would be against
2200 // the "reference class" mentioned in the CONSTANT_Methodref.
2201 // We don't know at this point which class that was, and if we
2202 // check against m.method_holder we might get the wrong answer.
2203 // So we just make sure to handle this check when the resolution
2204 // happens, when we call resolve_MemberName.
2205 //
2206 // (A public class can inherit public members from private supers,
2207 // and it would be wrong to check access against the private super
2208 // if the original symbolic reference was against the public class.)
2209 //
2210 // If there were a bytecode, the next step would be to lookup the method
2211 // in the reference class, then then check the method's access bits.
2212 // Emulate LinkResolver::check_method_accessability.
2213 klassOop resolved_klass = m->method_holder();
2214 if (!Reflection::verify_field_access(caller->as_klassOop(),
2215 resolved_klass, resolved_klass,
2216 m->access_flags(),
2217 true)) {
2218 // %%% following cutout belongs in Reflection::verify_field_access?
2219 bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(),
2220 resolved_klass, THREAD);
2221 if (!same_pm) {
2222 THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string());
2223 }
2224 }
2225 }
2227 MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK);
2228 }
2229 JVM_END
2231 // bound method handles
2232 JVM_ENTRY(void, MHI_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh,
2233 jobject target_jh, int argnum)) {
2234 ResourceMark rm; // for error messages
2236 // This is the guy we are initializing:
2237 if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2238 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
2240 // Early returns out of this method leave the BMH in an unfinished state.
2241 assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
2243 if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2244 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
2246 if (!java_dyn_MethodHandle::is_instance(target())) {
2247 // Target object is a reflective method. (%%% Do we need this alternate path?)
2248 Untested("init_BMH of non-MH");
2249 if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); }
2250 int decode_flags = 0; klassOop receiver_limit_oop = NULL;
2251 methodHandle m(THREAD,
2252 MethodHandles::decode_method(target(),
2253 receiver_limit_oop,
2254 decode_flags));
2255 KlassHandle receiver_limit(THREAD, receiver_limit_oop);
2256 MethodHandles::init_BoundMethodHandle_with_receiver(mh, m,
2257 receiver_limit,
2258 decode_flags,
2259 CHECK);
2260 return;
2261 }
2263 // Build a BMH on top of a DMH or another BMH:
2264 MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK);
2265 }
2266 JVM_END
2268 // adapter method handles
2269 JVM_ENTRY(void, MHI_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh,
2270 jobject target_jh, int argnum)) {
2271 // This is the guy we are initializing:
2272 if (mh_jh == NULL || target_jh == NULL) {
2273 THROW(vmSymbols::java_lang_InternalError());
2274 }
2275 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
2276 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
2278 // Early returns out of this method leave the AMH in an unfinished state.
2279 assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
2281 MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK);
2282 }
2283 JVM_END
2285 // method type forms
2286 JVM_ENTRY(void, MHI_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) {
2287 if (erased_jh == NULL) return;
2288 if (TraceMethodHandles) {
2289 tty->print("creating MethodType form ");
2290 if (WizardMode || Verbose) { // Warning: this calls Java code on the MH!
2291 // call Object.toString()
2292 symbolOop name = vmSymbols::toString_name(), sig = vmSymbols::void_string_signature();
2293 JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh)));
2294 JavaValue result(T_OBJECT);
2295 JavaCalls::call_virtual(&result, SystemDictionary::Object_klass(), name, sig,
2296 &args, CHECK);
2297 Handle str(THREAD, (oop)result.get_jobject());
2298 java_lang_String::print(str, tty);
2299 }
2300 tty->cr();
2301 }
2302 }
2303 JVM_END
2305 // debugging and reflection
2306 JVM_ENTRY(jobject, MHI_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) {
2307 Handle mh(THREAD, JNIHandles::resolve(mh_jh));
2308 if (!java_dyn_MethodHandle::is_instance(mh())) {
2309 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
2310 }
2311 oop target = MethodHandles::encode_target(mh, format, CHECK_NULL);
2312 return JNIHandles::make_local(THREAD, target);
2313 }
2314 JVM_END
2316 JVM_ENTRY(jint, MHI_getConstant(JNIEnv *env, jobject igcls, jint which)) {
2317 switch (which) {
2318 case MethodHandles::GC_JVM_PUSH_LIMIT:
2319 guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF,
2320 "MethodHandlePushLimit parameter must be in valid range");
2321 return MethodHandlePushLimit;
2322 case MethodHandles::GC_JVM_STACK_MOVE_UNIT:
2323 // return number of words per slot, signed according to stack direction
2324 return MethodHandles::stack_move_unit();
2325 case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK:
2326 return MethodHandles::adapter_conversion_ops_supported_mask();
2327 }
2328 return 0;
2329 }
2330 JVM_END
2332 #ifndef PRODUCT
2333 #define EACH_NAMED_CON(template) \
2334 template(MethodHandles,GC_JVM_PUSH_LIMIT) \
2335 template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) \
2336 template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \
2337 template(MethodHandles,ETF_DIRECT_HANDLE) \
2338 template(MethodHandles,ETF_METHOD_NAME) \
2339 template(MethodHandles,ETF_REFLECT_METHOD) \
2340 template(sun_dyn_MemberName,MN_IS_METHOD) \
2341 template(sun_dyn_MemberName,MN_IS_CONSTRUCTOR) \
2342 template(sun_dyn_MemberName,MN_IS_FIELD) \
2343 template(sun_dyn_MemberName,MN_IS_TYPE) \
2344 template(sun_dyn_MemberName,MN_SEARCH_SUPERCLASSES) \
2345 template(sun_dyn_MemberName,MN_SEARCH_INTERFACES) \
2346 template(sun_dyn_MemberName,VM_INDEX_UNINITIALIZED) \
2347 template(sun_dyn_AdapterMethodHandle,OP_RETYPE_ONLY) \
2348 template(sun_dyn_AdapterMethodHandle,OP_RETYPE_RAW) \
2349 template(sun_dyn_AdapterMethodHandle,OP_CHECK_CAST) \
2350 template(sun_dyn_AdapterMethodHandle,OP_PRIM_TO_PRIM) \
2351 template(sun_dyn_AdapterMethodHandle,OP_REF_TO_PRIM) \
2352 template(sun_dyn_AdapterMethodHandle,OP_PRIM_TO_REF) \
2353 template(sun_dyn_AdapterMethodHandle,OP_SWAP_ARGS) \
2354 template(sun_dyn_AdapterMethodHandle,OP_ROT_ARGS) \
2355 template(sun_dyn_AdapterMethodHandle,OP_DUP_ARGS) \
2356 template(sun_dyn_AdapterMethodHandle,OP_DROP_ARGS) \
2357 template(sun_dyn_AdapterMethodHandle,OP_COLLECT_ARGS) \
2358 template(sun_dyn_AdapterMethodHandle,OP_SPREAD_ARGS) \
2359 template(sun_dyn_AdapterMethodHandle,OP_FLYBY) \
2360 template(sun_dyn_AdapterMethodHandle,OP_RICOCHET) \
2361 template(sun_dyn_AdapterMethodHandle,CONV_OP_LIMIT) \
2362 template(sun_dyn_AdapterMethodHandle,CONV_OP_MASK) \
2363 template(sun_dyn_AdapterMethodHandle,CONV_VMINFO_MASK) \
2364 template(sun_dyn_AdapterMethodHandle,CONV_VMINFO_SHIFT) \
2365 template(sun_dyn_AdapterMethodHandle,CONV_OP_SHIFT) \
2366 template(sun_dyn_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \
2367 template(sun_dyn_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \
2368 template(sun_dyn_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \
2369 template(sun_dyn_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \
2370 /*end*/
2372 #define ONE_PLUS(scope,value) 1+
2373 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0;
2374 #define VALUE_COMMA(scope,value) scope::value,
2375 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 };
2376 #define STRING_NULL(scope,value) #value "\0"
2377 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) };
2379 #undef ONE_PLUS
2380 #undef VALUE_COMMA
2381 #undef STRING_NULL
2382 #undef EACH_NAMED_CON
2383 #endif
2385 JVM_ENTRY(jint, MHI_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
2386 #ifndef PRODUCT
2387 if (which >= 0 && which < con_value_count) {
2388 int con = con_values[which];
2389 objArrayOop box = (objArrayOop) JNIHandles::resolve(box_jh);
2390 if (box != NULL && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) {
2391 const char* str = &con_names[0];
2392 for (int i = 0; i < which; i++)
2393 str += strlen(str) + 1; // skip name and null
2394 oop name = java_lang_String::create_oop_from_str(str, CHECK_0);
2395 box->obj_at_put(0, name);
2396 }
2397 return con;
2398 }
2399 #endif
2400 return 0;
2401 }
2402 JVM_END
2404 // void init(MemberName self, AccessibleObject ref)
2405 JVM_ENTRY(void, MHI_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
2406 if (mname_jh == NULL || target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2407 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
2408 oop target_oop = JNIHandles::resolve_non_null(target_jh);
2409 MethodHandles::init_MemberName(mname(), target_oop);
2410 }
2411 JVM_END
2413 // void expand(MemberName self)
2414 JVM_ENTRY(void, MHI_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
2415 if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2416 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
2417 MethodHandles::expand_MemberName(mname, 0, CHECK);
2418 }
2419 JVM_END
2421 // void resolve(MemberName self, Class<?> caller)
2422 JVM_ENTRY(void, MHI_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) {
2423 if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
2424 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
2426 // The trusted Java code that calls this method should already have performed
2427 // access checks on behalf of the given caller. But, we can verify this.
2428 if (VerifyMethodHandles && caller_jh != NULL) {
2429 klassOop reference_klass = java_lang_Class::as_klassOop(sun_dyn_MemberName::clazz(mname()));
2430 if (reference_klass != NULL) {
2431 // Emulate LinkResolver::check_klass_accessability.
2432 klassOop caller = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh));
2433 if (!Reflection::verify_class_access(caller,
2434 reference_klass,
2435 true)) {
2436 THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name());
2437 }
2438 }
2439 }
2441 MethodHandles::resolve_MemberName(mname, CHECK);
2442 }
2443 JVM_END
2445 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
2446 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
2447 JVM_ENTRY(jint, MHI_getMembers(JNIEnv *env, jobject igcls,
2448 jclass clazz_jh, jstring name_jh, jstring sig_jh,
2449 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) {
2450 if (clazz_jh == NULL || results_jh == NULL) return -1;
2451 klassOop k_oop = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz_jh));
2453 objArrayOop results = (objArrayOop) JNIHandles::resolve(results_jh);
2454 if (results == NULL || !results->is_objArray()) return -1;
2456 symbolOop name = NULL, sig = NULL;
2457 if (name_jh != NULL) {
2458 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh));
2459 if (name == NULL) return 0; // a match is not possible
2460 }
2461 if (sig_jh != NULL) {
2462 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh));
2463 if (sig == NULL) return 0; // a match is not possible
2464 }
2466 klassOop caller = NULL;
2467 if (caller_jh != NULL) {
2468 oop caller_oop = JNIHandles::resolve_non_null(caller_jh);
2469 if (!java_lang_Class::is_instance(caller_oop)) return -1;
2470 caller = java_lang_Class::as_klassOop(caller_oop);
2471 }
2473 if (name != NULL && sig != NULL && results != NULL) {
2474 // try a direct resolve
2475 // %%% TO DO
2476 }
2478 int res = MethodHandles::find_MemberNames(k_oop, name, sig, mflags,
2479 caller, skip, results);
2480 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks
2481 return res;
2482 }
2483 JVM_END
2485 JVM_ENTRY(void, MHI_registerBootstrap(JNIEnv *env, jobject igcls, jclass caller_jh, jobject bsm_jh)) {
2486 instanceKlassHandle ik = MethodHandles::resolve_instance_klass(caller_jh, THREAD);
2487 if (!AllowTransitionalJSR292) {
2488 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
2489 "registerBootstrapMethod is only supported in JSR 292 EDR");
2490 }
2491 ik->link_class(CHECK);
2492 if (!java_dyn_MethodHandle::is_instance(JNIHandles::resolve(bsm_jh))) {
2493 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "method handle");
2494 }
2495 const char* err = NULL;
2496 if (ik->is_initialized() || ik->is_in_error_state()) {
2497 err = "too late: class is already initialized";
2498 } else {
2499 ObjectLocker ol(ik, THREAD); // note: this should be a recursive lock
2500 if (ik->is_not_initialized() ||
2501 (ik->is_being_initialized() && ik->is_reentrant_initialization(THREAD))) {
2502 if (ik->bootstrap_method() != NULL) {
2503 err = "class is already equipped with a bootstrap method";
2504 } else {
2505 ik->set_bootstrap_method(JNIHandles::resolve_non_null(bsm_jh));
2506 err = NULL;
2507 }
2508 } else {
2509 err = "class is already initialized";
2510 if (ik->is_being_initialized())
2511 err = "class is already being initialized in a different thread";
2512 }
2513 }
2514 if (err != NULL) {
2515 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), err);
2516 }
2517 }
2518 JVM_END
2520 JVM_ENTRY(jobject, MHI_getBootstrap(JNIEnv *env, jobject igcls, jclass caller_jh)) {
2521 instanceKlassHandle ik = MethodHandles::resolve_instance_klass(caller_jh, THREAD);
2522 return JNIHandles::make_local(THREAD, ik->bootstrap_method());
2523 }
2524 JVM_END
2526 JVM_ENTRY(void, MHI_setCallSiteTarget(JNIEnv *env, jobject igcls, jobject site_jh, jobject target_jh)) {
2527 // No special action required, yet.
2528 oop site_oop = JNIHandles::resolve(site_jh);
2529 if (!java_dyn_CallSite::is_instance(site_oop))
2530 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "not a CallSite");
2531 java_dyn_CallSite::set_target(site_oop, JNIHandles::resolve(target_jh));
2532 }
2533 JVM_END
2536 /// JVM_RegisterMethodHandleMethods
2538 #define ADR "J"
2540 #define LANG "Ljava/lang/"
2541 #define JDYN "Ljava/dyn/"
2542 #define IDYN "Lsun/dyn/"
2544 #define OBJ LANG"Object;"
2545 #define CLS LANG"Class;"
2546 #define STRG LANG"String;"
2547 #define CST JDYN"CallSite;"
2548 #define MT JDYN"MethodType;"
2549 #define MH JDYN"MethodHandle;"
2550 #define MHI IDYN"MethodHandleImpl;"
2551 #define MEM IDYN"MemberName;"
2552 #define AMH IDYN"AdapterMethodHandle;"
2553 #define BMH IDYN"BoundMethodHandle;"
2554 #define DMH IDYN"DirectMethodHandle;"
2556 #define CC (char*) /*cast a literal from (const char*)*/
2557 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
2559 // These are the native methods on sun.dyn.MethodHandleNatives.
2560 static JNINativeMethod methods[] = {
2561 // void init(MemberName self, AccessibleObject ref)
2562 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHI_init_AMH)},
2563 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHI_init_BMH)},
2564 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHI_init_DMH)},
2565 {CC"init", CC"("MT")V", FN_PTR(MHI_init_MT)},
2566 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHI_init_Mem)},
2567 {CC"expand", CC"("MEM")V", FN_PTR(MHI_expand_Mem)},
2568 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHI_resolve_Mem)},
2569 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHI_getTarget)},
2570 {CC"getConstant", CC"(I)I", FN_PTR(MHI_getConstant)},
2571 // static native int getNamedCon(int which, Object[] name)
2572 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHI_getNamedCon)},
2573 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
2574 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
2575 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHI_getMembers)}
2576 };
2578 // More entry points specifically for EnableInvokeDynamic.
2579 static JNINativeMethod methods2[] = {
2580 {CC"registerBootstrap", CC"("CLS MH")V", FN_PTR(MHI_registerBootstrap)},
2581 {CC"getBootstrap", CC"("CLS")"MH, FN_PTR(MHI_getBootstrap)},
2582 {CC"setCallSiteTarget", CC"("CST MH")V", FN_PTR(MHI_setCallSiteTarget)}
2583 };
2586 // This one function is exported, used by NativeLookup.
2588 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
2589 assert(MethodHandles::spot_check_entry_names(), "entry enum is OK");
2591 // note: this explicit warning-producing stuff will be replaced by auto-detection of the JSR 292 classes
2593 if (!EnableMethodHandles) {
2594 warning("JSR 292 method handles are disabled in this JVM. Use -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles to enable.");
2595 return; // bind nothing
2596 }
2598 bool enable_MH = true;
2600 {
2601 ThreadToNativeFromVM ttnfv(thread);
2603 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod));
2604 if (env->ExceptionOccurred()) {
2605 MethodHandles::set_enabled(false);
2606 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
2607 enable_MH = false;
2608 env->ExceptionClear();
2609 }
2610 }
2612 if (enable_MH) {
2613 KlassHandle MHI_klass = SystemDictionaryHandles::MethodHandleImpl_klass();
2614 if (MHI_klass.not_null()) {
2615 symbolHandle raiseException_name = oopFactory::new_symbol_handle("raiseException", CHECK);
2616 symbolHandle raiseException_sig = oopFactory::new_symbol_handle("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK);
2617 methodOop raiseException_method = instanceKlass::cast(MHI_klass->as_klassOop())
2618 ->find_method(raiseException_name(), raiseException_sig());
2619 if (raiseException_method != NULL && raiseException_method->is_static()) {
2620 MethodHandles::set_raise_exception_method(raiseException_method);
2621 } else {
2622 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
2623 enable_MH = false;
2624 }
2625 }
2626 }
2628 if (enable_MH) {
2629 MethodHandles::set_enabled(true);
2630 }
2632 if (!EnableInvokeDynamic) {
2633 warning("JSR 292 invokedynamic is disabled in this JVM. Use -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic to enable.");
2634 return; // bind nothing
2635 }
2637 {
2638 ThreadToNativeFromVM ttnfv(thread);
2640 int status = env->RegisterNatives(MHN_class, methods2, sizeof(methods2)/sizeof(JNINativeMethod));
2641 if (env->ExceptionOccurred()) {
2642 MethodHandles::set_enabled(false);
2643 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
2644 env->ExceptionClear();
2645 } else {
2646 MethodHandles::set_enabled(true);
2647 }
2648 }
2649 }
2650 JVM_END