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