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