Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/interpreter/linkResolver.cpp
64440 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "jvm.h"
27
#include "cds/archiveUtils.hpp"
28
#include "classfile/defaultMethods.hpp"
29
#include "classfile/javaClasses.hpp"
30
#include "classfile/resolutionErrors.hpp"
31
#include "classfile/symbolTable.hpp"
32
#include "classfile/systemDictionary.hpp"
33
#include "classfile/vmClasses.hpp"
34
#include "classfile/vmSymbols.hpp"
35
#include "compiler/compilationPolicy.hpp"
36
#include "compiler/compileBroker.hpp"
37
#include "gc/shared/collectedHeap.inline.hpp"
38
#include "interpreter/bootstrapInfo.hpp"
39
#include "interpreter/bytecode.hpp"
40
#include "interpreter/interpreterRuntime.hpp"
41
#include "interpreter/linkResolver.hpp"
42
#include "logging/log.hpp"
43
#include "logging/logStream.hpp"
44
#include "memory/resourceArea.hpp"
45
#include "oops/constantPool.hpp"
46
#include "oops/cpCache.inline.hpp"
47
#include "oops/instanceKlass.inline.hpp"
48
#include "oops/klass.inline.hpp"
49
#include "oops/method.hpp"
50
#include "oops/objArrayKlass.hpp"
51
#include "oops/objArrayOop.hpp"
52
#include "oops/oop.inline.hpp"
53
#include "prims/methodHandles.hpp"
54
#include "runtime/fieldDescriptor.inline.hpp"
55
#include "runtime/frame.inline.hpp"
56
#include "runtime/handles.inline.hpp"
57
#include "runtime/reflection.hpp"
58
#include "runtime/safepointVerifiers.hpp"
59
#include "runtime/signature.hpp"
60
#include "runtime/thread.inline.hpp"
61
#include "runtime/vmThread.hpp"
62
63
//------------------------------------------------------------------------------------------------------------------------
64
// Implementation of CallInfo
65
66
67
void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
68
int vtable_index = Method::nonvirtual_vtable_index;
69
set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
70
}
71
72
73
void CallInfo::set_interface(Klass* resolved_klass,
74
const methodHandle& resolved_method,
75
const methodHandle& selected_method,
76
int itable_index, TRAPS) {
77
// This is only called for interface methods. If the resolved_method
78
// comes from java/lang/Object, it can be the subject of a virtual call, so
79
// we should pick the vtable index from the resolved method.
80
// In that case, the caller must call set_virtual instead of set_interface.
81
assert(resolved_method->method_holder()->is_interface(), "");
82
assert(itable_index == resolved_method()->itable_index(), "");
83
set_common(resolved_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
84
}
85
86
void CallInfo::set_virtual(Klass* resolved_klass,
87
const methodHandle& resolved_method,
88
const methodHandle& selected_method,
89
int vtable_index, TRAPS) {
90
assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
91
assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
92
CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
93
set_common(resolved_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
94
assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
95
}
96
97
void CallInfo::set_handle(Klass* resolved_klass,
98
const methodHandle& resolved_method,
99
Handle resolved_appendix, TRAPS) {
100
guarantee(resolved_method.not_null(), "resolved method is null");
101
assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
102
resolved_method->is_compiled_lambda_form(),
103
"linkMethod must return one of these");
104
int vtable_index = Method::nonvirtual_vtable_index;
105
assert(!resolved_method->has_vtable_index(), "");
106
set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
107
_resolved_appendix = resolved_appendix;
108
}
109
110
void CallInfo::set_common(Klass* resolved_klass,
111
const methodHandle& resolved_method,
112
const methodHandle& selected_method,
113
CallKind kind,
114
int index,
115
TRAPS) {
116
assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
117
_resolved_klass = resolved_klass;
118
_resolved_method = resolved_method;
119
_selected_method = selected_method;
120
_call_kind = kind;
121
_call_index = index;
122
_resolved_appendix = Handle();
123
DEBUG_ONLY(verify()); // verify before making side effects
124
125
CompilationPolicy::compile_if_required(selected_method, THREAD);
126
}
127
128
// utility query for unreflecting a method
129
CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
130
Klass* resolved_method_holder = resolved_method->method_holder();
131
if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
132
resolved_klass = resolved_method_holder;
133
}
134
_resolved_klass = resolved_klass;
135
_resolved_method = methodHandle(THREAD, resolved_method);
136
_selected_method = methodHandle(THREAD, resolved_method);
137
// classify:
138
CallKind kind = CallInfo::unknown_kind;
139
int index = resolved_method->vtable_index();
140
if (resolved_method->can_be_statically_bound()) {
141
kind = CallInfo::direct_call;
142
} else if (!resolved_method_holder->is_interface()) {
143
// Could be an Object method inherited into an interface, but still a vtable call.
144
kind = CallInfo::vtable_call;
145
} else if (!resolved_klass->is_interface()) {
146
// A default or miranda method. Compute the vtable index.
147
index = LinkResolver::vtable_index_of_interface_method(resolved_klass, _resolved_method);
148
assert(index >= 0 , "we should have valid vtable index at this point");
149
150
kind = CallInfo::vtable_call;
151
} else if (resolved_method->has_vtable_index()) {
152
// Can occur if an interface redeclares a method of Object.
153
154
#ifdef ASSERT
155
// Ensure that this is really the case.
156
Klass* object_klass = vmClasses::Object_klass();
157
Method * object_resolved_method = object_klass->vtable().method_at(index);
158
assert(object_resolved_method->name() == resolved_method->name(),
159
"Object and interface method names should match at vtable index %d, %s != %s",
160
index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
161
assert(object_resolved_method->signature() == resolved_method->signature(),
162
"Object and interface method signatures should match at vtable index %d, %s != %s",
163
index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
164
#endif // ASSERT
165
166
kind = CallInfo::vtable_call;
167
} else {
168
// A regular interface call.
169
kind = CallInfo::itable_call;
170
index = resolved_method->itable_index();
171
}
172
assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
173
_call_kind = kind;
174
_call_index = index;
175
_resolved_appendix = Handle();
176
// Find or create a ResolvedMethod instance for this Method*
177
set_resolved_method_name(CHECK);
178
179
DEBUG_ONLY(verify());
180
}
181
182
void CallInfo::set_resolved_method_name(TRAPS) {
183
assert(_resolved_method() != NULL, "Should already have a Method*");
184
oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);
185
_resolved_method_name = Handle(THREAD, rmethod_name);
186
}
187
188
#ifdef ASSERT
189
void CallInfo::verify() {
190
switch (call_kind()) { // the meaning and allowed value of index depends on kind
191
case CallInfo::direct_call:
192
if (_call_index == Method::nonvirtual_vtable_index) break;
193
// else fall through to check vtable index:
194
case CallInfo::vtable_call:
195
assert(resolved_klass()->verify_vtable_index(_call_index), "");
196
break;
197
case CallInfo::itable_call:
198
assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
199
break;
200
case CallInfo::unknown_kind:
201
assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
202
break;
203
default:
204
fatal("Unexpected call kind %d", call_kind());
205
}
206
}
207
#endif // ASSERT
208
209
#ifndef PRODUCT
210
void CallInfo::print() {
211
ResourceMark rm;
212
const char* kindstr;
213
switch (_call_kind) {
214
case direct_call: kindstr = "direct"; break;
215
case vtable_call: kindstr = "vtable"; break;
216
case itable_call: kindstr = "itable"; break;
217
default : kindstr = "unknown"; break;
218
}
219
tty->print_cr("Call %s@%d %s", kindstr, _call_index,
220
_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
221
}
222
#endif
223
224
//------------------------------------------------------------------------------------------------------------------------
225
// Implementation of LinkInfo
226
227
LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) {
228
// resolve klass
229
_resolved_klass = pool->klass_ref_at(index, CHECK);
230
231
// Get name, signature, and static klass
232
_name = pool->name_ref_at(index);
233
_signature = pool->signature_ref_at(index);
234
_tag = pool->tag_ref_at(index);
235
_current_klass = pool->pool_holder();
236
_current_method = current_method;
237
238
// Coming from the constant pool always checks access
239
_check_access = true;
240
_check_loader_constraints = true;
241
}
242
243
LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
244
// resolve klass
245
_resolved_klass = pool->klass_ref_at(index, CHECK);
246
247
// Get name, signature, and static klass
248
_name = pool->name_ref_at(index);
249
_signature = pool->signature_ref_at(index);
250
_tag = pool->tag_ref_at(index);
251
_current_klass = pool->pool_holder();
252
_current_method = methodHandle();
253
254
// Coming from the constant pool always checks access
255
_check_access = true;
256
_check_loader_constraints = true;
257
}
258
259
#ifndef PRODUCT
260
void LinkInfo::print() {
261
ResourceMark rm;
262
tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s check_loader_constraints=%s",
263
_resolved_klass->name()->as_C_string(),
264
_name->as_C_string(),
265
_signature->as_C_string(),
266
_current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
267
_check_access ? "true" : "false",
268
_check_loader_constraints ? "true" : "false");
269
270
}
271
#endif // PRODUCT
272
//------------------------------------------------------------------------------------------------------------------------
273
// Klass resolution
274
275
void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) {
276
Klass* base_klass = sel_klass;
277
if (sel_klass->is_objArray_klass()) {
278
base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
279
}
280
// The element type could be a typeArray - we only need the access
281
// check if it is a reference to another class.
282
if (!base_klass->is_instance_klass()) {
283
return; // no relevant check to do
284
}
285
286
Reflection::VerifyClassAccessResults vca_result =
287
Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);
288
if (vca_result != Reflection::ACCESS_OK) {
289
ResourceMark rm(THREAD);
290
char* msg = Reflection::verify_class_access_msg(ref_klass,
291
InstanceKlass::cast(base_klass),
292
vca_result);
293
bool same_module = (base_klass->module() == ref_klass->module());
294
if (msg == NULL) {
295
Exceptions::fthrow(
296
THREAD_AND_LOCATION,
297
vmSymbols::java_lang_IllegalAccessError(),
298
"failed to access class %s from class %s (%s%s%s)",
299
base_klass->external_name(),
300
ref_klass->external_name(),
301
(same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),
302
(same_module) ? "" : "; ",
303
(same_module) ? "" : ref_klass->class_in_module_of_loader());
304
} else {
305
// Use module specific message returned by verify_class_access_msg().
306
Exceptions::fthrow(
307
THREAD_AND_LOCATION,
308
vmSymbols::java_lang_IllegalAccessError(),
309
"%s", msg);
310
}
311
}
312
}
313
314
//------------------------------------------------------------------------------------------------------------------------
315
// Method resolution
316
//
317
// According to JVM spec. $5.4.3c & $5.4.3d
318
319
// Look up method in klasses, including static methods
320
// Then look up local default methods
321
Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
322
bool checkpolymorphism,
323
bool in_imethod_resolve) {
324
NoSafepointVerifier nsv; // Method* returned may not be reclaimed
325
326
Klass* klass = link_info.resolved_klass();
327
Symbol* name = link_info.name();
328
Symbol* signature = link_info.signature();
329
330
// Ignore overpasses so statics can be found during resolution
331
Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::skip);
332
333
if (klass->is_array_klass()) {
334
// Only consider klass and super klass for arrays
335
return result;
336
}
337
338
InstanceKlass* ik = InstanceKlass::cast(klass);
339
340
// JDK 8, JVMS 5.4.3.4: Interface method resolution should
341
// ignore static and non-public methods of java.lang.Object,
342
// like clone and finalize.
343
if (in_imethod_resolve &&
344
result != NULL &&
345
ik->is_interface() &&
346
(result->is_static() || !result->is_public()) &&
347
result->method_holder() == vmClasses::Object_klass()) {
348
result = NULL;
349
}
350
351
// Before considering default methods, check for an overpass in the
352
// current class if a method has not been found.
353
if (result == NULL) {
354
result = ik->find_method(name, signature);
355
}
356
357
if (result == NULL) {
358
Array<Method*>* default_methods = ik->default_methods();
359
if (default_methods != NULL) {
360
result = InstanceKlass::find_method(default_methods, name, signature);
361
}
362
}
363
364
if (checkpolymorphism && result != NULL) {
365
vmIntrinsics::ID iid = result->intrinsic_id();
366
if (MethodHandles::is_signature_polymorphic(iid)) {
367
// Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
368
return NULL;
369
}
370
}
371
return result;
372
}
373
374
// returns first instance method
375
// Looks up method in classes, then looks up local default methods
376
Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
377
Symbol* name,
378
Symbol* signature,
379
Klass::PrivateLookupMode private_mode) {
380
Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
381
382
while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
383
Klass* super_klass = result->method_holder()->super();
384
result = super_klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
385
}
386
387
if (klass->is_array_klass()) {
388
// Only consider klass and super klass for arrays
389
return result;
390
}
391
392
if (result == NULL) {
393
Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
394
if (default_methods != NULL) {
395
result = InstanceKlass::find_method(default_methods, name, signature);
396
assert(result == NULL || !result->is_static(), "static defaults not allowed");
397
}
398
}
399
return result;
400
}
401
402
int LinkResolver::vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method) {
403
InstanceKlass* ik = InstanceKlass::cast(klass);
404
return ik->vtable_index_of_interface_method(resolved_method());
405
}
406
407
Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
408
InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
409
410
// Specify 'true' in order to skip default methods when searching the
411
// interfaces. Function lookup_method_in_klasses() already looked for
412
// the method in the default methods table.
413
return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::DefaultsLookupMode::skip);
414
}
415
416
Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
417
Handle *appendix_result_or_null,
418
TRAPS) {
419
ResourceMark rm(THREAD);
420
Klass* klass = link_info.resolved_klass();
421
Symbol* name = link_info.name();
422
Symbol* full_signature = link_info.signature();
423
LogTarget(Info, methodhandles) lt_mh;
424
425
vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
426
log_info(methodhandles)("lookup_polymorphic_method iid=%s %s.%s%s",
427
vmIntrinsics::name_at(iid), klass->external_name(),
428
name->as_C_string(), full_signature->as_C_string());
429
if ((klass == vmClasses::MethodHandle_klass() ||
430
klass == vmClasses::VarHandle_klass()) &&
431
iid != vmIntrinsics::_none) {
432
if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
433
// Most of these do not need an up-call to Java to resolve, so can be done anywhere.
434
// Do not erase last argument type (MemberName) if it is a static linkTo method.
435
bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
436
TempNewSymbol basic_signature =
437
MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg);
438
log_info(methodhandles)("lookup_polymorphic_method %s %s => basic %s",
439
name->as_C_string(),
440
full_signature->as_C_string(),
441
basic_signature->as_C_string());
442
Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
443
basic_signature,
444
CHECK_NULL);
445
if (result != NULL) {
446
assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
447
assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
448
assert(basic_signature == result->signature(), "predict the result signature");
449
if (lt_mh.is_enabled()) {
450
LogStream ls(lt_mh);
451
ls.print("lookup_polymorphic_method => intrinsic ");
452
result->print_on(&ls);
453
}
454
}
455
return result;
456
} else if (iid == vmIntrinsics::_invokeGeneric
457
&& THREAD->can_call_java()
458
&& appendix_result_or_null != NULL) {
459
// This is a method with type-checking semantics.
460
// We will ask Java code to spin an adapter method for it.
461
if (!MethodHandles::enabled()) {
462
// Make sure the Java part of the runtime has been booted up.
463
Klass* natives = vmClasses::MethodHandleNatives_klass();
464
if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
465
SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
466
Handle(),
467
Handle(),
468
true,
469
CHECK_NULL);
470
}
471
}
472
473
Handle appendix;
474
Method* result = SystemDictionary::find_method_handle_invoker(klass,
475
name,
476
full_signature,
477
link_info.current_klass(),
478
&appendix,
479
CHECK_NULL);
480
if (lt_mh.is_enabled()) {
481
LogStream ls(lt_mh);
482
ls.print("lookup_polymorphic_method => (via Java) ");
483
result->print_on(&ls);
484
ls.print(" lookup_polymorphic_method => appendix = ");
485
appendix.is_null() ? ls.print_cr("(none)") : appendix->print_on(&ls);
486
}
487
if (result != NULL) {
488
#ifdef ASSERT
489
ResourceMark rm(THREAD);
490
491
TempNewSymbol basic_signature =
492
MethodHandles::lookup_basic_type_signature(full_signature);
493
int actual_size_of_params = result->size_of_parameters();
494
int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
495
// +1 for MethodHandle.this, +1 for trailing MethodType
496
if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
497
if (appendix.not_null()) expected_size_of_params += 1;
498
if (actual_size_of_params != expected_size_of_params) {
499
tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
500
tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
501
result->print();
502
}
503
assert(actual_size_of_params == expected_size_of_params,
504
"%d != %d", actual_size_of_params, expected_size_of_params);
505
#endif //ASSERT
506
507
assert(appendix_result_or_null != NULL, "");
508
(*appendix_result_or_null) = appendix;
509
}
510
return result;
511
}
512
}
513
return NULL;
514
}
515
516
static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass) {
517
assert(ref_klass->is_instance_klass(), "must be");
518
assert(sel_klass->is_instance_klass(), "must be");
519
InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
520
InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
521
const char* nest_host_error_1 = ref_ik->nest_host_error();
522
const char* nest_host_error_2 = sel_ik->nest_host_error();
523
if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {
524
ss->print(", (%s%s%s)",
525
(nest_host_error_1 != NULL) ? nest_host_error_1 : "",
526
(nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",
527
(nest_host_error_2 != NULL) ? nest_host_error_2 : "");
528
}
529
}
530
531
void LinkResolver::check_method_accessability(Klass* ref_klass,
532
Klass* resolved_klass,
533
Klass* sel_klass,
534
const methodHandle& sel_method,
535
TRAPS) {
536
537
AccessFlags flags = sel_method->access_flags();
538
539
// Special case: arrays always override "clone". JVMS 2.15.
540
// If the resolved klass is an array class, and the declaring class
541
// is java.lang.Object and the method is "clone", set the flags
542
// to public.
543
//
544
// We'll check for the method name first, as that's most likely
545
// to be false (so we'll short-circuit out of these tests).
546
if (sel_method->name() == vmSymbols::clone_name() &&
547
sel_klass == vmClasses::Object_klass() &&
548
resolved_klass->is_array_klass()) {
549
// We need to change "protected" to "public".
550
assert(flags.is_protected(), "clone not protected?");
551
jint new_flags = flags.as_int();
552
new_flags = new_flags & (~JVM_ACC_PROTECTED);
553
new_flags = new_flags | JVM_ACC_PUBLIC;
554
flags.set_flags(new_flags);
555
}
556
// assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
557
558
bool can_access = Reflection::verify_member_access(ref_klass,
559
resolved_klass,
560
sel_klass,
561
flags,
562
true, false, CHECK);
563
// Any existing exceptions that may have been thrown
564
// have been allowed to propagate.
565
if (!can_access) {
566
ResourceMark rm(THREAD);
567
stringStream ss;
568
bool same_module = (sel_klass->module() == ref_klass->module());
569
ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
570
ref_klass->external_name(),
571
sel_method->is_abstract() ? "abstract " : "",
572
sel_method->is_protected() ? "protected " : "",
573
sel_method->is_private() ? "private " : "",
574
sel_method->external_name(),
575
(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
576
(same_module) ? "" : "; ",
577
(same_module) ? "" : sel_klass->class_in_module_of_loader()
578
);
579
580
// For private access see if there was a problem with nest host
581
// resolution, and if so report that as part of the message.
582
if (sel_method->is_private()) {
583
print_nest_host_error_on(&ss, ref_klass, sel_klass);
584
}
585
586
Exceptions::fthrow(THREAD_AND_LOCATION,
587
vmSymbols::java_lang_IllegalAccessError(),
588
"%s",
589
ss.as_string()
590
);
591
return;
592
}
593
}
594
595
Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
596
const constantPoolHandle& pool, int index, TRAPS) {
597
// This method is used only
598
// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
599
// and
600
// (2) in Bytecode_invoke::static_target
601
// It appears to fail when applied to an invokeinterface call site.
602
// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
603
// resolve klass
604
if (code == Bytecodes::_invokedynamic) {
605
Klass* resolved_klass = vmClasses::MethodHandle_klass();
606
Symbol* method_name = vmSymbols::invoke_name();
607
Symbol* method_signature = pool->signature_ref_at(index);
608
Klass* current_klass = pool->pool_holder();
609
LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
610
return resolve_method(link_info, code, THREAD);
611
}
612
613
LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
614
Klass* resolved_klass = link_info.resolved_klass();
615
616
if (pool->has_preresolution()
617
|| ((resolved_klass == vmClasses::MethodHandle_klass() || resolved_klass == vmClasses::VarHandle_klass()) &&
618
MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
619
Method* result = ConstantPool::method_at_if_loaded(pool, index);
620
if (result != NULL) {
621
return result;
622
}
623
}
624
625
if (code == Bytecodes::_invokeinterface) {
626
return resolve_interface_method(link_info, code, THREAD);
627
} else if (code == Bytecodes::_invokevirtual) {
628
return resolve_method(link_info, code, THREAD);
629
} else if (!resolved_klass->is_interface()) {
630
return resolve_method(link_info, code, THREAD);
631
} else {
632
return resolve_interface_method(link_info, code, THREAD);
633
}
634
}
635
636
// Check and print a loader constraint violation message for method or interface method
637
void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
638
const methodHandle& resolved_method,
639
const char* method_type, TRAPS) {
640
Handle current_loader(THREAD, link_info.current_klass()->class_loader());
641
Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
642
643
ResourceMark rm(THREAD);
644
Symbol* failed_type_symbol =
645
SystemDictionary::check_signature_loaders(link_info.signature(),
646
/*klass_being_linked*/ NULL, // We are not linking class
647
current_loader,
648
resolved_loader, true);
649
if (failed_type_symbol != NULL) {
650
Klass* current_class = link_info.current_klass();
651
ClassLoaderData* current_loader_data = current_class->class_loader_data();
652
assert(current_loader_data != NULL, "current class has no class loader data");
653
Klass* resolved_method_class = resolved_method->method_holder();
654
ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
655
assert(target_loader_data != NULL, "resolved method's class has no class loader data");
656
657
stringStream ss;
658
ss.print("loader constraint violation: when resolving %s '", method_type);
659
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
660
ss.print("' the class loader %s of the current class, %s,"
661
" and the class loader %s for the method's defining class, %s, have"
662
" different Class objects for the type %s used in the signature (%s; %s)",
663
current_loader_data->loader_name_and_id(),
664
current_class->name()->as_C_string(),
665
target_loader_data->loader_name_and_id(),
666
resolved_method_class->name()->as_C_string(),
667
failed_type_symbol->as_C_string(),
668
current_class->class_in_module_of_loader(false, true),
669
resolved_method_class->class_in_module_of_loader(false, true));
670
THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
671
}
672
}
673
674
void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
675
Klass* current_klass,
676
Klass* sel_klass, TRAPS) {
677
Handle ref_loader(THREAD, current_klass->class_loader());
678
Handle sel_loader(THREAD, sel_klass->class_loader());
679
680
ResourceMark rm(THREAD); // needed for check_signature_loaders
681
Symbol* failed_type_symbol =
682
SystemDictionary::check_signature_loaders(sig,
683
/*klass_being_linked*/ NULL, // We are not linking class
684
ref_loader, sel_loader,
685
false);
686
if (failed_type_symbol != NULL) {
687
stringStream ss;
688
const char* failed_type_name = failed_type_symbol->as_klass_external_name();
689
690
ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
691
"the class loader %s of the current class, %s, "
692
"and the class loader %s for the field's defining %s, %s, "
693
"have different Class objects for type %s (%s; %s)",
694
field->as_C_string(),
695
failed_type_name,
696
current_klass->class_loader_data()->loader_name_and_id(),
697
current_klass->external_name(),
698
sel_klass->class_loader_data()->loader_name_and_id(),
699
sel_klass->external_kind(),
700
sel_klass->external_name(),
701
failed_type_name,
702
current_klass->class_in_module_of_loader(false, true),
703
sel_klass->class_in_module_of_loader(false, true));
704
THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
705
}
706
}
707
708
Method* LinkResolver::resolve_method(const LinkInfo& link_info,
709
Bytecodes::Code code, TRAPS) {
710
711
Handle nested_exception;
712
Klass* resolved_klass = link_info.resolved_klass();
713
714
// 1. For invokevirtual, cannot call an interface method
715
if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
716
ResourceMark rm(THREAD);
717
char buf[200];
718
jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
719
resolved_klass->external_name());
720
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
721
}
722
723
// 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
724
if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
725
ResourceMark rm(THREAD);
726
stringStream ss;
727
ss.print("Method '");
728
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
729
ss.print("' must be Methodref constant");
730
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
731
}
732
733
// 3. lookup method in resolved klass and its super klasses
734
methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
735
736
// 4. lookup method in all the interfaces implemented by the resolved klass
737
if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
738
resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
739
740
if (resolved_method.is_null()) {
741
// JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
742
Method* method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
743
resolved_method = methodHandle(THREAD, method);
744
if (HAS_PENDING_EXCEPTION) {
745
nested_exception = Handle(THREAD, PENDING_EXCEPTION);
746
CLEAR_PENDING_EXCEPTION;
747
}
748
}
749
}
750
751
// 5. method lookup failed
752
if (resolved_method.is_null()) {
753
ResourceMark rm(THREAD);
754
stringStream ss;
755
ss.print("'");
756
Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
757
ss.print("'");
758
THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
759
ss.as_string(), nested_exception, NULL);
760
}
761
762
// 6. access checks, access checking may be turned off when calling from within the VM.
763
Klass* current_klass = link_info.current_klass();
764
if (link_info.check_access()) {
765
assert(current_klass != NULL , "current_klass should not be null");
766
767
// check if method can be accessed by the referring class
768
check_method_accessability(current_klass,
769
resolved_klass,
770
resolved_method->method_holder(),
771
resolved_method,
772
CHECK_NULL);
773
}
774
if (link_info.check_loader_constraints()) {
775
// check loader constraints
776
check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
777
}
778
779
return resolved_method();
780
}
781
782
static void trace_method_resolution(const char* prefix,
783
Klass* klass,
784
Klass* resolved_klass,
785
Method* method,
786
bool logitables,
787
int index = -1) {
788
#ifndef PRODUCT
789
ResourceMark rm;
790
Log(itables) logi;
791
LogStream lsi(logi.trace());
792
Log(vtables) logv;
793
LogStream lsv(logv.trace());
794
outputStream* st;
795
if (logitables) {
796
st = &lsi;
797
} else {
798
st = &lsv;
799
}
800
st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
801
prefix,
802
(klass == NULL ? "<NULL>" : klass->internal_name()),
803
(resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
804
Method::name_and_sig_as_C_string(resolved_klass,
805
method->name(),
806
method->signature()),
807
method->method_holder()->internal_name());
808
method->print_linkage_flags(st);
809
if (index != -1) {
810
st->print("vtable_index:%d", index);
811
}
812
st->cr();
813
#endif // PRODUCT
814
}
815
816
// Do linktime resolution of a method in the interface within the context of the specied bytecode.
817
Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
818
819
Klass* resolved_klass = link_info.resolved_klass();
820
821
// check if klass is interface
822
if (!resolved_klass->is_interface()) {
823
ResourceMark rm(THREAD);
824
char buf[200];
825
jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
826
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
827
}
828
829
// check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
830
if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
831
ResourceMark rm(THREAD);
832
stringStream ss;
833
ss.print("Method '");
834
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
835
ss.print("' must be InterfaceMethodref constant");
836
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
837
}
838
839
// lookup method in this interface or its super, java.lang.Object
840
// JDK8: also look for static methods
841
methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
842
843
if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
844
// lookup method in all the super-interfaces
845
resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
846
}
847
848
if (resolved_method.is_null()) {
849
// no method found
850
ResourceMark rm(THREAD);
851
stringStream ss;
852
ss.print("'");
853
Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
854
ss.print("'");
855
THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
856
}
857
858
if (link_info.check_access()) {
859
// JDK8 adds non-public interface methods, and accessability check requirement
860
Klass* current_klass = link_info.current_klass();
861
862
assert(current_klass != NULL , "current_klass should not be null");
863
864
// check if method can be accessed by the referring class
865
check_method_accessability(current_klass,
866
resolved_klass,
867
resolved_method->method_holder(),
868
resolved_method,
869
CHECK_NULL);
870
}
871
if (link_info.check_loader_constraints()) {
872
check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
873
}
874
875
if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
876
ResourceMark rm(THREAD);
877
stringStream ss;
878
ss.print("Expected instance not static method '");
879
Method::print_external_name(&ss, resolved_klass,
880
resolved_method->name(), resolved_method->signature());
881
ss.print("'");
882
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
883
}
884
885
if (log_develop_is_enabled(Trace, itables)) {
886
char buf[200];
887
jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
888
Bytecodes::name(code));
889
trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
890
}
891
892
return resolved_method();
893
}
894
895
//------------------------------------------------------------------------------------------------------------------------
896
// Field resolution
897
898
void LinkResolver::check_field_accessability(Klass* ref_klass,
899
Klass* resolved_klass,
900
Klass* sel_klass,
901
const fieldDescriptor& fd,
902
TRAPS) {
903
bool can_access = Reflection::verify_member_access(ref_klass,
904
resolved_klass,
905
sel_klass,
906
fd.access_flags(),
907
true, false, CHECK);
908
// Any existing exceptions that may have been thrown, for example LinkageErrors
909
// from nest-host resolution, have been allowed to propagate.
910
if (!can_access) {
911
bool same_module = (sel_klass->module() == ref_klass->module());
912
ResourceMark rm(THREAD);
913
stringStream ss;
914
ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
915
ref_klass->external_name(),
916
fd.is_protected() ? "protected " : "",
917
fd.is_private() ? "private " : "",
918
sel_klass->external_name(),
919
fd.name()->as_C_string(),
920
(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
921
(same_module) ? "" : "; ",
922
(same_module) ? "" : sel_klass->class_in_module_of_loader()
923
);
924
// For private access see if there was a problem with nest host
925
// resolution, and if so report that as part of the message.
926
if (fd.is_private()) {
927
print_nest_host_error_on(&ss, ref_klass, sel_klass);
928
}
929
Exceptions::fthrow(THREAD_AND_LOCATION,
930
vmSymbols::java_lang_IllegalAccessError(),
931
"%s",
932
ss.as_string()
933
);
934
return;
935
}
936
}
937
938
void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
939
LinkInfo link_info(pool, index, method, CHECK);
940
resolve_field(fd, link_info, byte, true, CHECK);
941
}
942
943
void LinkResolver::resolve_field(fieldDescriptor& fd,
944
const LinkInfo& link_info,
945
Bytecodes::Code byte, bool initialize_class,
946
TRAPS) {
947
assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
948
byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
949
byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||
950
(byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
951
952
bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
953
bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
954
// Check if there's a resolved klass containing the field
955
Klass* resolved_klass = link_info.resolved_klass();
956
Symbol* field = link_info.name();
957
Symbol* sig = link_info.signature();
958
959
if (resolved_klass == NULL) {
960
ResourceMark rm(THREAD);
961
THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
962
}
963
964
// Resolve instance field
965
Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
966
// check if field exists; i.e., if a klass containing the field def has been selected
967
if (sel_klass == NULL) {
968
ResourceMark rm(THREAD);
969
THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
970
}
971
972
// Access checking may be turned off when calling from within the VM.
973
Klass* current_klass = link_info.current_klass();
974
if (link_info.check_access()) {
975
976
// check access
977
check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
978
979
// check for errors
980
if (is_static != fd.is_static()) {
981
ResourceMark rm(THREAD);
982
char msg[200];
983
jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
984
THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
985
}
986
987
// A final field can be modified only
988
// (1) by methods declared in the class declaring the field and
989
// (2) by the <clinit> method (in case of a static field)
990
// or by the <init> method (in case of an instance field).
991
if (is_put && fd.access_flags().is_final()) {
992
993
if (sel_klass != current_klass) {
994
ResourceMark rm(THREAD);
995
stringStream ss;
996
ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
997
is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
998
current_klass->external_name());
999
THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1000
}
1001
1002
if (fd.constants()->pool_holder()->major_version() >= 53) {
1003
Method* m = link_info.current_method();
1004
assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1005
bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1006
fd.is_static() &&
1007
!m->is_static_initializer());
1008
bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1009
!fd.is_static() &&
1010
!m->is_object_initializer());
1011
1012
if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1013
ResourceMark rm(THREAD);
1014
stringStream ss;
1015
ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1016
is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1017
m->name()->as_C_string(),
1018
is_static ? "<clinit>" : "<init>");
1019
THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1020
}
1021
}
1022
}
1023
1024
// initialize resolved_klass if necessary
1025
// note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1026
// according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1027
//
1028
// note 2: we don't want to force initialization if we are just checking
1029
// if the field access is legal; e.g., during compilation
1030
if (is_static && initialize_class) {
1031
sel_klass->initialize(CHECK);
1032
}
1033
}
1034
1035
if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != NULL)) {
1036
check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1037
}
1038
1039
// return information. note that the klass is set to the actual klass containing the
1040
// field, otherwise access of static fields in superclasses will not work.
1041
}
1042
1043
1044
//------------------------------------------------------------------------------------------------------------------------
1045
// Invoke resolution
1046
//
1047
// Naming conventions:
1048
//
1049
// resolved_method the specified method (i.e., static receiver specified via constant pool index)
1050
// sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
1051
// resolved_klass the specified klass (i.e., specified via constant pool index)
1052
// recv_klass the receiver klass
1053
1054
1055
void LinkResolver::resolve_static_call(CallInfo& result,
1056
const LinkInfo& link_info,
1057
bool initialize_class, TRAPS) {
1058
Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1059
1060
// The resolved class can change as a result of this resolution.
1061
Klass* resolved_klass = resolved_method->method_holder();
1062
1063
// Initialize klass (this should only happen if everything is ok)
1064
if (initialize_class && resolved_klass->should_be_initialized()) {
1065
resolved_klass->initialize(CHECK);
1066
// Use updated LinkInfo to reresolve with resolved method holder
1067
LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1068
link_info.current_klass(),
1069
link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1070
link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1071
resolved_method = linktime_resolve_static_method(new_info, CHECK);
1072
}
1073
1074
// setup result
1075
result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1076
}
1077
1078
// throws linktime exceptions
1079
Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1080
1081
Klass* resolved_klass = link_info.resolved_klass();
1082
Method* resolved_method;
1083
if (!resolved_klass->is_interface()) {
1084
resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1085
} else {
1086
resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1087
}
1088
assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1089
1090
// check if static
1091
if (!resolved_method->is_static()) {
1092
ResourceMark rm(THREAD);
1093
stringStream ss;
1094
ss.print("Expected static method '");
1095
resolved_method->print_external_name(&ss);
1096
ss.print("'");
1097
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1098
}
1099
return resolved_method;
1100
}
1101
1102
1103
void LinkResolver::resolve_special_call(CallInfo& result,
1104
Handle recv,
1105
const LinkInfo& link_info,
1106
TRAPS) {
1107
Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1108
runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1109
}
1110
1111
// throws linktime exceptions
1112
Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1113
1114
// Invokespecial is called for multiple special reasons:
1115
// <init>
1116
// local private method invocation, for classes and interfaces
1117
// superclass.method, which can also resolve to a default method
1118
// and the selected method is recalculated relative to the direct superclass
1119
// superinterface.method, which explicitly does not check shadowing
1120
Klass* resolved_klass = link_info.resolved_klass();
1121
Method* resolved_method = NULL;
1122
1123
if (!resolved_klass->is_interface()) {
1124
resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1125
} else {
1126
resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1127
}
1128
1129
// check if method name is <init>, that it is found in same klass as static type
1130
if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1131
resolved_method->method_holder() != resolved_klass) {
1132
ResourceMark rm(THREAD);
1133
stringStream ss;
1134
ss.print("%s: method '", resolved_klass->external_name());
1135
resolved_method->signature()->print_as_signature_external_return_type(&ss);
1136
ss.print(" %s(", resolved_method->name()->as_C_string());
1137
resolved_method->signature()->print_as_signature_external_parameters(&ss);
1138
ss.print(")' not found");
1139
Exceptions::fthrow(
1140
THREAD_AND_LOCATION,
1141
vmSymbols::java_lang_NoSuchMethodError(),
1142
"%s", ss.as_string());
1143
return NULL;
1144
}
1145
1146
// ensure that invokespecial's interface method reference is in
1147
// a direct superinterface, not an indirect superinterface
1148
Klass* current_klass = link_info.current_klass();
1149
if (current_klass != NULL && resolved_klass->is_interface()) {
1150
InstanceKlass* klass_to_check = InstanceKlass::cast(current_klass);
1151
// Disable verification for the dynamically-generated reflection bytecodes.
1152
bool is_reflect = klass_to_check->is_subclass_of(
1153
vmClasses::reflect_MagicAccessorImpl_klass());
1154
1155
if (!is_reflect &&
1156
!klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1157
ResourceMark rm(THREAD);
1158
stringStream ss;
1159
ss.print("Interface method reference: '");
1160
resolved_method->print_external_name(&ss);
1161
ss.print("', is in an indirect superinterface of %s",
1162
current_klass->external_name());
1163
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1164
}
1165
}
1166
1167
// check if not static
1168
if (resolved_method->is_static()) {
1169
ResourceMark rm(THREAD);
1170
stringStream ss;
1171
ss.print("Expecting non-static method '");
1172
resolved_method->print_external_name(&ss);
1173
ss.print("'");
1174
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1175
}
1176
1177
if (log_develop_is_enabled(Trace, itables)) {
1178
trace_method_resolution("invokespecial resolved method: caller-class:",
1179
current_klass, resolved_klass, resolved_method, true);
1180
}
1181
1182
return resolved_method;
1183
}
1184
1185
// throws runtime exceptions
1186
void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1187
const LinkInfo& link_info,
1188
const methodHandle& resolved_method,
1189
Handle recv, TRAPS) {
1190
1191
Klass* resolved_klass = link_info.resolved_klass();
1192
1193
// resolved method is selected method unless we have an old-style lookup
1194
// for a superclass method
1195
// Invokespecial for a superinterface, resolved method is selected method,
1196
// no checks for shadowing
1197
methodHandle sel_method(THREAD, resolved_method());
1198
1199
if (link_info.check_access() &&
1200
// check if the method is not <init>
1201
resolved_method->name() != vmSymbols::object_initializer_name()) {
1202
1203
Klass* current_klass = link_info.current_klass();
1204
1205
// Check if the class of the resolved_klass is a superclass
1206
// (not supertype in order to exclude interface classes) of the current class.
1207
// This check is not performed for super.invoke for interface methods
1208
// in super interfaces.
1209
if (current_klass->is_subclass_of(resolved_klass) &&
1210
current_klass != resolved_klass) {
1211
// Lookup super method
1212
Klass* super_klass = current_klass->super();
1213
Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1214
resolved_method->name(),
1215
resolved_method->signature(),
1216
Klass::PrivateLookupMode::find);
1217
sel_method = methodHandle(THREAD, instance_method);
1218
1219
// check if found
1220
if (sel_method.is_null()) {
1221
ResourceMark rm(THREAD);
1222
stringStream ss;
1223
ss.print("'");
1224
resolved_method->print_external_name(&ss);
1225
ss.print("'");
1226
THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1227
// check loader constraints if found a different method
1228
} else if (link_info.check_loader_constraints() && sel_method() != resolved_method()) {
1229
check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1230
}
1231
}
1232
1233
// Check that the class of objectref (the receiver) is the current class or interface,
1234
// or a subtype of the current class or interface (the sender), otherwise invokespecial
1235
// throws IllegalAccessError.
1236
// The verifier checks that the sender is a subtype of the class in the I/MR operand.
1237
// The verifier also checks that the receiver is a subtype of the sender, if the sender is
1238
// a class. If the sender is an interface, the check has to be performed at runtime.
1239
InstanceKlass* sender = InstanceKlass::cast(current_klass);
1240
if (sender->is_interface() && recv.not_null()) {
1241
Klass* receiver_klass = recv->klass();
1242
if (!receiver_klass->is_subtype_of(sender)) {
1243
ResourceMark rm(THREAD);
1244
char buf[500];
1245
jio_snprintf(buf, sizeof(buf),
1246
"Receiver class %s must be the current class or a subtype of interface %s",
1247
receiver_klass->external_name(),
1248
sender->external_name());
1249
THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1250
}
1251
}
1252
}
1253
1254
// check if not static
1255
if (sel_method->is_static()) {
1256
ResourceMark rm(THREAD);
1257
stringStream ss;
1258
ss.print("Expecting non-static method '");
1259
resolved_method->print_external_name(&ss);
1260
ss.print("'");
1261
THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1262
}
1263
1264
// check if abstract
1265
if (sel_method->is_abstract()) {
1266
ResourceMark rm(THREAD);
1267
stringStream ss;
1268
ss.print("'");
1269
Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1270
ss.print("'");
1271
THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1272
}
1273
1274
if (log_develop_is_enabled(Trace, itables)) {
1275
trace_method_resolution("invokespecial selected method: resolved-class:",
1276
resolved_klass, resolved_klass, sel_method(), true);
1277
}
1278
1279
// setup result
1280
result.set_static(resolved_klass, sel_method, CHECK);
1281
}
1282
1283
void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1284
const LinkInfo& link_info,
1285
bool check_null_and_abstract, TRAPS) {
1286
Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1287
runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1288
link_info.resolved_klass(),
1289
recv, receiver_klass,
1290
check_null_and_abstract, CHECK);
1291
}
1292
1293
// throws linktime exceptions
1294
Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1295
TRAPS) {
1296
// normal method resolution
1297
Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1298
1299
assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1300
assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1301
1302
// check if private interface method
1303
Klass* resolved_klass = link_info.resolved_klass();
1304
Klass* current_klass = link_info.current_klass();
1305
1306
// This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1307
if (resolved_klass->is_interface() && resolved_method->is_private()) {
1308
ResourceMark rm(THREAD);
1309
stringStream ss;
1310
ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1311
resolved_method->print_external_name(&ss);
1312
ss.print("', caller-class: %s",
1313
(current_klass == NULL ? "<null>" : current_klass->internal_name()));
1314
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1315
}
1316
1317
// check if not static
1318
if (resolved_method->is_static()) {
1319
ResourceMark rm(THREAD);
1320
stringStream ss;
1321
ss.print("Expecting non-static method '");
1322
resolved_method->print_external_name(&ss);
1323
ss.print("'");
1324
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1325
}
1326
1327
if (log_develop_is_enabled(Trace, vtables)) {
1328
trace_method_resolution("invokevirtual resolved method: caller-class:",
1329
current_klass, resolved_klass, resolved_method, false);
1330
}
1331
1332
return resolved_method;
1333
}
1334
1335
// throws runtime exceptions
1336
void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1337
const methodHandle& resolved_method,
1338
Klass* resolved_klass,
1339
Handle recv,
1340
Klass* recv_klass,
1341
bool check_null_and_abstract,
1342
TRAPS) {
1343
1344
// setup default return values
1345
int vtable_index = Method::invalid_vtable_index;
1346
methodHandle selected_method;
1347
1348
// runtime method resolution
1349
if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1350
THROW(vmSymbols::java_lang_NullPointerException());
1351
}
1352
1353
// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1354
// has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1355
// a missing receiver might result in a bogus lookup.
1356
assert(resolved_method->method_holder()->is_linked(), "must be linked");
1357
1358
// do lookup based on receiver klass using the vtable index
1359
if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1360
vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1361
assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1362
1363
selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1364
} else {
1365
// at this point we are sure that resolved_method is virtual and not
1366
// a default or miranda method; therefore, it must have a valid vtable index.
1367
assert(!resolved_method->has_itable_index(), "");
1368
vtable_index = resolved_method->vtable_index();
1369
// We could get a negative vtable_index of nonvirtual_vtable_index for private
1370
// methods, or for final methods. Private methods never appear in the vtable
1371
// and never override other methods. As an optimization, final methods are
1372
// never put in the vtable, unless they override an existing method.
1373
// So if we do get nonvirtual_vtable_index, it means the selected method is the
1374
// resolved method, and it can never be changed by an override.
1375
if (vtable_index == Method::nonvirtual_vtable_index) {
1376
assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1377
selected_method = resolved_method;
1378
} else {
1379
selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1380
}
1381
}
1382
1383
// check if method exists
1384
if (selected_method.is_null()) {
1385
throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1386
}
1387
1388
// check if abstract
1389
if (check_null_and_abstract && selected_method->is_abstract()) {
1390
// Pass arguments for generating a verbose error message.
1391
throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1392
}
1393
1394
if (log_develop_is_enabled(Trace, vtables)) {
1395
trace_method_resolution("invokevirtual selected method: receiver-class:",
1396
recv_klass, resolved_klass, selected_method(),
1397
false, vtable_index);
1398
}
1399
// setup result
1400
result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1401
}
1402
1403
void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1404
const LinkInfo& link_info,
1405
bool check_null_and_abstract, TRAPS) {
1406
// throws linktime exceptions
1407
Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1408
methodHandle mh(THREAD, resolved_method);
1409
runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1410
recv, recv_klass, check_null_and_abstract, CHECK);
1411
}
1412
1413
Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1414
TRAPS) {
1415
// normal interface method resolution
1416
Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1417
assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1418
assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1419
1420
return resolved_method;
1421
}
1422
1423
// throws runtime exceptions
1424
void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1425
const methodHandle& resolved_method,
1426
Klass* resolved_klass,
1427
Handle recv,
1428
Klass* recv_klass,
1429
bool check_null_and_abstract, TRAPS) {
1430
1431
// check if receiver exists
1432
if (check_null_and_abstract && recv.is_null()) {
1433
THROW(vmSymbols::java_lang_NullPointerException());
1434
}
1435
1436
// check if receiver klass implements the resolved interface
1437
if (!recv_klass->is_subtype_of(resolved_klass)) {
1438
ResourceMark rm(THREAD);
1439
char buf[200];
1440
jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1441
recv_klass->external_name(),
1442
resolved_klass->external_name());
1443
THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1444
}
1445
1446
methodHandle selected_method = resolved_method;
1447
1448
// resolve the method in the receiver class, unless it is private
1449
if (!resolved_method()->is_private()) {
1450
// do lookup based on receiver klass
1451
// This search must match the linktime preparation search for itable initialization
1452
// to correctly enforce loader constraints for interface method inheritance.
1453
// Private methods are skipped as the resolved method was not private.
1454
Method* method = lookup_instance_method_in_klasses(recv_klass,
1455
resolved_method->name(),
1456
resolved_method->signature(),
1457
Klass::PrivateLookupMode::skip);
1458
selected_method = methodHandle(THREAD, method);
1459
1460
if (selected_method.is_null() && !check_null_and_abstract) {
1461
// In theory this is a harmless placeholder value, but
1462
// in practice leaving in null affects the nsk default method tests.
1463
// This needs further study.
1464
selected_method = resolved_method;
1465
}
1466
// check if method exists
1467
if (selected_method.is_null()) {
1468
// Pass arguments for generating a verbose error message.
1469
throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1470
}
1471
// check access
1472
// Throw Illegal Access Error if selected_method is not public.
1473
if (!selected_method->is_public()) {
1474
ResourceMark rm(THREAD);
1475
stringStream ss;
1476
ss.print("'");
1477
Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1478
ss.print("'");
1479
THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1480
}
1481
// check if abstract
1482
if (check_null_and_abstract && selected_method->is_abstract()) {
1483
throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1484
}
1485
}
1486
1487
if (log_develop_is_enabled(Trace, itables)) {
1488
trace_method_resolution("invokeinterface selected method: receiver-class:",
1489
recv_klass, resolved_klass, selected_method(), true);
1490
}
1491
// setup result
1492
if (resolved_method->has_vtable_index()) {
1493
int vtable_index = resolved_method->vtable_index();
1494
log_develop_trace(itables)(" -- vtable index: %d", vtable_index);
1495
assert(vtable_index == selected_method->vtable_index(), "sanity check");
1496
result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1497
} else if (resolved_method->has_itable_index()) {
1498
int itable_index = resolved_method()->itable_index();
1499
log_develop_trace(itables)(" -- itable index: %d", itable_index);
1500
result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1501
} else {
1502
int index = resolved_method->vtable_index();
1503
log_develop_trace(itables)(" -- non itable/vtable index: %d", index);
1504
assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1505
assert(resolved_method()->is_private() ||
1506
(resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),
1507
"Should only have non-virtual invokeinterface for private or final-Object methods!");
1508
assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1509
// This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1510
result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1511
}
1512
}
1513
1514
1515
Method* LinkResolver::linktime_resolve_interface_method_or_null(
1516
const LinkInfo& link_info) {
1517
EXCEPTION_MARK;
1518
Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1519
if (HAS_PENDING_EXCEPTION) {
1520
CLEAR_PENDING_EXCEPTION;
1521
return NULL;
1522
} else {
1523
return method_result;
1524
}
1525
}
1526
1527
Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1528
const LinkInfo& link_info) {
1529
EXCEPTION_MARK;
1530
Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1531
if (HAS_PENDING_EXCEPTION) {
1532
CLEAR_PENDING_EXCEPTION;
1533
return NULL;
1534
} else {
1535
return method_result;
1536
}
1537
}
1538
1539
Method* LinkResolver::resolve_virtual_call_or_null(
1540
Klass* receiver_klass,
1541
const LinkInfo& link_info) {
1542
EXCEPTION_MARK;
1543
CallInfo info;
1544
resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1545
if (HAS_PENDING_EXCEPTION) {
1546
CLEAR_PENDING_EXCEPTION;
1547
return NULL;
1548
}
1549
return info.selected_method();
1550
}
1551
1552
Method* LinkResolver::resolve_interface_call_or_null(
1553
Klass* receiver_klass,
1554
const LinkInfo& link_info) {
1555
EXCEPTION_MARK;
1556
CallInfo info;
1557
resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1558
if (HAS_PENDING_EXCEPTION) {
1559
CLEAR_PENDING_EXCEPTION;
1560
return NULL;
1561
}
1562
return info.selected_method();
1563
}
1564
1565
int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1566
const LinkInfo& link_info) {
1567
EXCEPTION_MARK;
1568
CallInfo info;
1569
resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1570
/*check_null_or_abstract*/false, THREAD);
1571
if (HAS_PENDING_EXCEPTION) {
1572
CLEAR_PENDING_EXCEPTION;
1573
return Method::invalid_vtable_index;
1574
}
1575
return info.vtable_index();
1576
}
1577
1578
Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1579
EXCEPTION_MARK;
1580
CallInfo info;
1581
resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1582
if (HAS_PENDING_EXCEPTION) {
1583
CLEAR_PENDING_EXCEPTION;
1584
return NULL;
1585
}
1586
return info.selected_method();
1587
}
1588
1589
Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1590
EXCEPTION_MARK;
1591
CallInfo info;
1592
resolve_special_call(info, Handle(), link_info, THREAD);
1593
if (HAS_PENDING_EXCEPTION) {
1594
CLEAR_PENDING_EXCEPTION;
1595
return NULL;
1596
}
1597
return info.selected_method();
1598
}
1599
1600
1601
1602
//------------------------------------------------------------------------------------------------------------------------
1603
// ConstantPool entries
1604
1605
void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1606
switch (byte) {
1607
case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1608
case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1609
case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1610
case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1611
case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1612
case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1613
default : break;
1614
}
1615
return;
1616
}
1617
1618
void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1619
const methodHandle& attached_method,
1620
Bytecodes::Code byte, TRAPS) {
1621
Klass* defc = attached_method->method_holder();
1622
Symbol* name = attached_method->name();
1623
Symbol* type = attached_method->signature();
1624
LinkInfo link_info(defc, name, type);
1625
switch(byte) {
1626
case Bytecodes::_invokevirtual:
1627
resolve_virtual_call(result, recv, recv->klass(), link_info,
1628
/*check_null_and_abstract=*/true, CHECK);
1629
break;
1630
case Bytecodes::_invokeinterface:
1631
resolve_interface_call(result, recv, recv->klass(), link_info,
1632
/*check_null_and_abstract=*/true, CHECK);
1633
break;
1634
case Bytecodes::_invokestatic:
1635
resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1636
break;
1637
case Bytecodes::_invokespecial:
1638
resolve_special_call(result, recv, link_info, CHECK);
1639
break;
1640
default:
1641
fatal("bad call: %s", Bytecodes::name(byte));
1642
break;
1643
}
1644
}
1645
1646
void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1647
LinkInfo link_info(pool, index, CHECK);
1648
resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1649
}
1650
1651
1652
void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1653
const constantPoolHandle& pool, int index, TRAPS) {
1654
LinkInfo link_info(pool, index, CHECK);
1655
resolve_special_call(result, recv, link_info, CHECK);
1656
}
1657
1658
1659
void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1660
const constantPoolHandle& pool, int index,
1661
TRAPS) {
1662
1663
LinkInfo link_info(pool, index, CHECK);
1664
Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1665
resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1666
}
1667
1668
1669
void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1670
LinkInfo link_info(pool, index, CHECK);
1671
Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1672
resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1673
}
1674
1675
bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) {
1676
int cache_index = ConstantPool::decode_cpcache_index(index, true);
1677
ConstantPoolCacheEntry* cpce = pool->cache()->entry_at(cache_index);
1678
if (!cpce->is_f1_null()) {
1679
Klass* resolved_klass = link_info.resolved_klass();
1680
methodHandle method(THREAD, cpce->f1_as_method());
1681
Handle appendix(THREAD, cpce->appendix_if_resolved(pool));
1682
result.set_handle(resolved_klass, method, appendix, CHECK_false);
1683
return true;
1684
} else {
1685
return false;
1686
}
1687
}
1688
1689
void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1690
LinkInfo link_info(pool, index, CHECK);
1691
if (log_is_enabled(Info, methodhandles)) {
1692
ResourceMark rm(THREAD);
1693
log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1694
link_info.signature()->as_C_string());
1695
}
1696
{ // Check if the call site has been bound already, and short circuit:
1697
bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK);
1698
if (is_done) return;
1699
}
1700
resolve_handle_call(result, link_info, CHECK);
1701
}
1702
1703
void LinkResolver::resolve_handle_call(CallInfo& result,
1704
const LinkInfo& link_info,
1705
TRAPS) {
1706
// JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1707
Klass* resolved_klass = link_info.resolved_klass();
1708
assert(resolved_klass == vmClasses::MethodHandle_klass() ||
1709
resolved_klass == vmClasses::VarHandle_klass(), "");
1710
assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1711
Handle resolved_appendix;
1712
Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1713
methodHandle resolved_method(THREAD, m);
1714
1715
if (link_info.check_access()) {
1716
Symbol* name = link_info.name();
1717
vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
1718
if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
1719
// Check if method can be accessed by the referring class.
1720
// MH.linkTo* invocations are not rewritten to invokehandle.
1721
assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid));
1722
1723
Klass* current_klass = link_info.current_klass();
1724
assert(current_klass != NULL , "current_klass should not be null");
1725
check_method_accessability(current_klass,
1726
resolved_klass,
1727
resolved_method->method_holder(),
1728
resolved_method,
1729
CHECK);
1730
} else {
1731
// Java code is free to arbitrarily link signature-polymorphic invokers.
1732
assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid));
1733
assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public");
1734
}
1735
}
1736
result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1737
}
1738
1739
void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1740
ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);
1741
int pool_index = cpce->constant_pool_index();
1742
1743
// Resolve the bootstrap specifier (BSM + optional arguments).
1744
BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1745
1746
// Check if CallSite has been bound already or failed already, and short circuit:
1747
{
1748
bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1749
if (is_done) return;
1750
}
1751
1752
// The initial step in Call Site Specifier Resolution is to resolve the symbolic
1753
// reference to a method handle which will be the bootstrap method for a dynamic
1754
// call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1755
// method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1756
// method's CP index for the CONSTANT_MethodHandle_info. So, there is no need to
1757
// set the indy_rf flag since any subsequent invokedynamic instruction which shares
1758
// this bootstrap method will encounter the resolution of MethodHandleInError.
1759
1760
resolve_dynamic_call(result, bootstrap_specifier, CHECK);
1761
1762
LogTarget(Debug, methodhandles, indy) lt_indy;
1763
if (lt_indy.is_enabled()) {
1764
LogStream ls(lt_indy);
1765
bootstrap_specifier.print_msg_on(&ls, "resolve_invokedynamic");
1766
}
1767
1768
// The returned linkage result is provisional up to the moment
1769
// the interpreter or runtime performs a serialized check of
1770
// the relevant CPCE::f1 field. This is done by the caller
1771
// of this method, via CPCE::set_dynamic_call, which uses
1772
// an ObjectLocker to do the final serialization of updates
1773
// to CPCE state, including f1.
1774
1775
// Log dynamic info to CDS classlist.
1776
ArchiveUtils::log_to_classlist(&bootstrap_specifier, CHECK);
1777
}
1778
1779
void LinkResolver::resolve_dynamic_call(CallInfo& result,
1780
BootstrapInfo& bootstrap_specifier,
1781
TRAPS) {
1782
// JSR 292: this must resolve to an implicitly generated method
1783
// such as MH.linkToCallSite(*...) or some other call-site shape.
1784
// The appendix argument is likely to be a freshly-created CallSite.
1785
// It may also be a MethodHandle from an unwrapped ConstantCallSite,
1786
// or any other reference. The resolved_method as well as the appendix
1787
// are both recorded together via CallInfo::set_handle.
1788
SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1789
Exceptions::wrap_dynamic_exception(/* is_indy */ true, THREAD);
1790
1791
if (HAS_PENDING_EXCEPTION) {
1792
if (!PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) {
1793
// Let any random low-level IE or SOE or OOME just bleed through.
1794
// Basically we pretend that the bootstrap method was never called,
1795
// if it fails this way: We neither record a successful linkage,
1796
// nor do we memorize a LE for posterity.
1797
return;
1798
}
1799
// JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve
1800
// a symbolic reference fails because an error is thrown that is an
1801
// instance of LinkageError (or a subclass), then subsequent attempts to
1802
// resolve the reference always fail with the same error that was thrown
1803
// as a result of the initial resolution attempt.
1804
bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);
1805
if (!recorded_res_status) {
1806
// Another thread got here just before we did. So, either use the method
1807
// that it resolved or throw the LinkageError exception that it threw.
1808
bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1809
if (is_done) return;
1810
}
1811
assert(bootstrap_specifier.invokedynamic_cp_cache_entry()->indy_resolution_failed(),
1812
"Resolution failure flag wasn't set");
1813
}
1814
1815
bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);
1816
// Exceptions::wrap_dynamic_exception not used because
1817
// set_handle doesn't throw linkage errors
1818
}
1819
1820
// Selected method is abstract.
1821
void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1822
const methodHandle& selected_method,
1823
Klass *recv_klass, TRAPS) {
1824
Klass *resolved_klass = resolved_method->method_holder();
1825
ResourceMark rm(THREAD);
1826
stringStream ss;
1827
1828
if (recv_klass != NULL) {
1829
ss.print("Receiver class %s does not define or inherit an "
1830
"implementation of the",
1831
recv_klass->external_name());
1832
} else {
1833
ss.print("Missing implementation of");
1834
}
1835
1836
assert(resolved_method.not_null(), "Sanity");
1837
ss.print(" resolved method '%s%s",
1838
resolved_method->is_abstract() ? "abstract " : "",
1839
resolved_method->is_private() ? "private " : "");
1840
resolved_method->signature()->print_as_signature_external_return_type(&ss);
1841
ss.print(" %s(", resolved_method->name()->as_C_string());
1842
resolved_method->signature()->print_as_signature_external_parameters(&ss);
1843
ss.print(")' of %s %s.",
1844
resolved_klass->external_kind(),
1845
resolved_klass->external_name());
1846
1847
if (selected_method.not_null() && !(resolved_method == selected_method)) {
1848
ss.print(" Selected method is '%s%s",
1849
selected_method->is_abstract() ? "abstract " : "",
1850
selected_method->is_private() ? "private " : "");
1851
selected_method->print_external_name(&ss);
1852
ss.print("'.");
1853
}
1854
1855
THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1856
}
1857
1858