-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathlinkResolver.cpp
1960 lines (1737 loc) · 86.6 KB
/
linkResolver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "cds/archiveUtils.hpp"
#include "classfile/classLoader.hpp"
#include "classfile/defaultMethods.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "compiler/compilationPolicy.hpp"
#include "compiler/compileBroker.hpp"
#include "gc/shared/collectedHeap.inline.hpp"
#include "interpreter/bootstrapInfo.hpp"
#include "interpreter/bytecode.hpp"
#include "interpreter/interpreterRuntime.hpp"
#include "interpreter/linkResolver.hpp"
#include "jvm.h"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/resourceArea.hpp"
#include "oops/constantPool.inline.hpp"
#include "oops/cpCache.inline.hpp"
#include "oops/instanceKlass.inline.hpp"
#include "oops/klass.inline.hpp"
#include "oops/method.inline.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
#include "oops/resolvedIndyEntry.hpp"
#include "oops/symbolHandle.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/methodHandles.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/javaThread.inline.hpp"
#include "runtime/perfData.hpp"
#include "runtime/reflection.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/macros.hpp"
#if INCLUDE_JFR
#include "jfr/jfr.hpp"
#endif
//------------------------------------------------------------------------------------------------------------------------
// Implementation of CallInfo
void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
int vtable_index = Method::nonvirtual_vtable_index;
set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
}
void CallInfo::set_interface(Klass* resolved_klass,
const methodHandle& resolved_method,
const methodHandle& selected_method,
int itable_index, TRAPS) {
// This is only called for interface methods. If the resolved_method
// comes from java/lang/Object, it can be the subject of a virtual call, so
// we should pick the vtable index from the resolved method.
// In that case, the caller must call set_virtual instead of set_interface.
assert(resolved_method->method_holder()->is_interface(), "");
assert(itable_index == resolved_method->itable_index(), "");
set_common(resolved_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
}
void CallInfo::set_virtual(Klass* resolved_klass,
const methodHandle& resolved_method,
const methodHandle& selected_method,
int vtable_index, TRAPS) {
assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
set_common(resolved_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
}
void CallInfo::set_handle(Klass* resolved_klass,
const methodHandle& resolved_method,
Handle resolved_appendix, TRAPS) {
guarantee(resolved_method.not_null(), "resolved method is null");
assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
resolved_method->is_compiled_lambda_form(),
"linkMethod must return one of these");
int vtable_index = Method::nonvirtual_vtable_index;
assert(!resolved_method->has_vtable_index(), "");
set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
_resolved_appendix = resolved_appendix;
}
// Redefinition safepoint may have updated the method. Make sure the new version of the method is returned.
// Callers are responsible for not safepointing and storing this method somewhere safe where redefinition
// can replace it if runs again. Safe places are constant pool cache and code cache metadata.
// The old method is safe in CallInfo since its a methodHandle (it won't get deleted), and accessed with these
// accessors.
Method* CallInfo::resolved_method() const {
if (JvmtiExport::can_hotswap_or_post_breakpoint() && _resolved_method->is_old()) {
return _resolved_method->get_new_method();
} else {
return _resolved_method();
}
}
Method* CallInfo::selected_method() const {
if (JvmtiExport::can_hotswap_or_post_breakpoint() && _selected_method->is_old()) {
return _selected_method->get_new_method();
} else {
return _selected_method();
}
}
void CallInfo::set_common(Klass* resolved_klass,
const methodHandle& resolved_method,
const methodHandle& selected_method,
CallKind kind,
int index,
TRAPS) {
if (selected_method.not_null()) {
assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
}
_resolved_klass = resolved_klass;
_resolved_method = resolved_method;
_selected_method = selected_method;
_call_kind = kind;
_call_index = index;
_resolved_appendix = Handle();
DEBUG_ONLY(verify()); // verify before making side effects
if (selected_method.not_null()) {
CompilationPolicy::compile_if_required(selected_method, THREAD);
}
}
// utility query for unreflecting a method
CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
Klass* resolved_method_holder = resolved_method->method_holder();
if (resolved_klass == nullptr) { // 2nd argument defaults to holder of 1st
resolved_klass = resolved_method_holder;
}
_resolved_klass = resolved_klass;
_resolved_method = methodHandle(THREAD, resolved_method);
_selected_method = methodHandle(THREAD, resolved_method);
// classify:
CallKind kind = CallInfo::unknown_kind;
int index = resolved_method->vtable_index();
if (resolved_method->can_be_statically_bound()) {
kind = CallInfo::direct_call;
} else if (!resolved_method_holder->is_interface()) {
// Could be an Object method inherited into an interface, but still a vtable call.
kind = CallInfo::vtable_call;
} else if (!resolved_klass->is_interface()) {
// A default or miranda method. Compute the vtable index.
index = LinkResolver::vtable_index_of_interface_method(resolved_klass, _resolved_method);
assert(index >= 0 , "we should have valid vtable index at this point");
kind = CallInfo::vtable_call;
} else if (resolved_method->has_vtable_index()) {
// Can occur if an interface redeclares a method of Object.
#ifdef ASSERT
// Ensure that this is really the case.
Klass* object_klass = vmClasses::Object_klass();
Method * object_resolved_method = object_klass->vtable().method_at(index);
assert(object_resolved_method->name() == resolved_method->name(),
"Object and interface method names should match at vtable index %d, %s != %s",
index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
assert(object_resolved_method->signature() == resolved_method->signature(),
"Object and interface method signatures should match at vtable index %d, %s != %s",
index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
#endif // ASSERT
kind = CallInfo::vtable_call;
} else {
// A regular interface call.
kind = CallInfo::itable_call;
index = resolved_method->itable_index();
}
assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
_call_kind = kind;
_call_index = index;
_resolved_appendix = Handle();
// Find or create a ResolvedMethod instance for this Method*
set_resolved_method_name(CHECK);
DEBUG_ONLY(verify());
}
void CallInfo::set_resolved_method_name(TRAPS) {
assert(_resolved_method() != nullptr, "Should already have a Method*");
oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);
_resolved_method_name = Handle(THREAD, rmethod_name);
}
#ifdef ASSERT
void CallInfo::verify() {
switch (call_kind()) { // the meaning and allowed value of index depends on kind
case CallInfo::direct_call:
if (_call_index == Method::nonvirtual_vtable_index) break;
// else fall through to check vtable index:
case CallInfo::vtable_call:
assert(resolved_klass()->verify_vtable_index(_call_index), "");
break;
case CallInfo::itable_call:
assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
break;
case CallInfo::unknown_kind:
assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
break;
default:
fatal("Unexpected call kind %d", call_kind());
}
}
#endif // ASSERT
#ifndef PRODUCT
void CallInfo::print() {
ResourceMark rm;
const char* kindstr;
switch (_call_kind) {
case direct_call: kindstr = "direct"; break;
case vtable_call: kindstr = "vtable"; break;
case itable_call: kindstr = "itable"; break;
default : kindstr = "unknown"; break;
}
tty->print_cr("Call %s@%d %s", kindstr, _call_index,
_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
}
#endif
//------------------------------------------------------------------------------------------------------------------------
// Implementation of LinkInfo
LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, Bytecodes::Code code, TRAPS) {
// resolve klass
_resolved_klass = pool->klass_ref_at(index, code, CHECK);
// Get name, signature, and static klass
_name = pool->name_ref_at(index, code);
_signature = pool->signature_ref_at(index, code);
_tag = pool->tag_ref_at(index, code);
_current_klass = pool->pool_holder();
_current_method = current_method;
// Coming from the constant pool always checks access
_check_access = true;
_check_loader_constraints = true;
}
LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, Bytecodes::Code code, TRAPS) {
// resolve klass
_resolved_klass = pool->klass_ref_at(index, code, CHECK);
// Get name, signature, and static klass
_name = pool->name_ref_at(index, code);
_signature = pool->signature_ref_at(index, code);
_tag = pool->tag_ref_at(index, code);
_current_klass = pool->pool_holder();
_current_method = methodHandle();
// Coming from the constant pool always checks access
_check_access = true;
_check_loader_constraints = true;
}
#ifndef PRODUCT
void LinkInfo::print() {
ResourceMark rm;
tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s check_loader_constraints=%s",
_resolved_klass->name()->as_C_string(),
_name->as_C_string(),
_signature->as_C_string(),
_current_klass == nullptr ? "(none)" : _current_klass->name()->as_C_string(),
_check_access ? "true" : "false",
_check_loader_constraints ? "true" : "false");
}
#endif // PRODUCT
//------------------------------------------------------------------------------------------------------------------------
// Klass resolution
void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) {
Klass* base_klass = sel_klass;
if (sel_klass->is_objArray_klass()) {
base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
}
// The element type could be a typeArray - we only need the access
// check if it is a reference to another class.
if (!base_klass->is_instance_klass()) {
return; // no relevant check to do
}
Reflection::VerifyClassAccessResults vca_result =
Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);
if (vca_result != Reflection::ACCESS_OK) {
ResourceMark rm(THREAD);
char* msg = Reflection::verify_class_access_msg(ref_klass,
InstanceKlass::cast(base_klass),
vca_result);
// Names are all known to be < 64k so we know this formatted message is not excessively large.
bool same_module = (base_klass->module() == ref_klass->module());
if (msg == nullptr) {
Exceptions::fthrow(
THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"failed to access class %s from class %s (%s%s%s)",
base_klass->external_name(),
ref_klass->external_name(),
(same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),
(same_module) ? "" : "; ",
(same_module) ? "" : ref_klass->class_in_module_of_loader());
} else {
// Use module specific message returned by verify_class_access_msg().
Exceptions::fthrow(
THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"%s", msg);
}
}
}
//------------------------------------------------------------------------------------------------------------------------
// Method resolution
//
// According to JVM spec. $5.4.3c & $5.4.3d
// Look up method in klasses, including static methods
// Then look up local default methods
Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
bool checkpolymorphism,
bool in_imethod_resolve) {
NoSafepointVerifier nsv; // Method* returned may not be reclaimed
Klass* klass = link_info.resolved_klass();
Symbol* name = link_info.name();
Symbol* signature = link_info.signature();
// Ignore overpasses so statics can be found during resolution
Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::skip);
if (klass->is_array_klass()) {
// Only consider klass and super klass for arrays
return result;
}
InstanceKlass* ik = InstanceKlass::cast(klass);
// JDK 8, JVMS 5.4.3.4: Interface method resolution should
// ignore static and non-public methods of java.lang.Object,
// like clone and finalize.
if (in_imethod_resolve &&
result != nullptr &&
ik->is_interface() &&
(result->is_static() || !result->is_public()) &&
result->method_holder() == vmClasses::Object_klass()) {
result = nullptr;
}
// Before considering default methods, check for an overpass in the
// current class if a method has not been found.
if (result == nullptr) {
result = ik->find_method(name, signature);
}
if (result == nullptr) {
Array<Method*>* default_methods = ik->default_methods();
if (default_methods != nullptr) {
result = InstanceKlass::find_method(default_methods, name, signature);
}
}
if (checkpolymorphism && result != nullptr) {
vmIntrinsics::ID iid = result->intrinsic_id();
if (MethodHandles::is_signature_polymorphic(iid)) {
// Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
return nullptr;
}
}
return result;
}
// returns first instance method
// Looks up method in classes, then looks up local default methods
Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
Symbol* name,
Symbol* signature,
Klass::PrivateLookupMode private_mode) {
Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
while (result != nullptr && result->is_static() && result->method_holder()->super() != nullptr) {
Klass* super_klass = result->method_holder()->super();
result = super_klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
}
if (klass->is_array_klass()) {
// Only consider klass and super klass for arrays
return result;
}
if (result == nullptr) {
Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
if (default_methods != nullptr) {
result = InstanceKlass::find_method(default_methods, name, signature);
assert(result == nullptr || !result->is_static(), "static defaults not allowed");
}
}
return result;
}
int LinkResolver::vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method) {
InstanceKlass* ik = InstanceKlass::cast(klass);
return ik->vtable_index_of_interface_method(resolved_method());
}
Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
// Specify 'true' in order to skip default methods when searching the
// interfaces. Function lookup_method_in_klasses() already looked for
// the method in the default methods table.
return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::DefaultsLookupMode::skip);
}
Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
Handle *appendix_result_or_null,
TRAPS) {
ResourceMark rm(THREAD);
Klass* klass = link_info.resolved_klass();
Symbol* name = link_info.name();
Symbol* full_signature = link_info.signature();
LogTarget(Info, methodhandles) lt_mh;
vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
log_info(methodhandles)("lookup_polymorphic_method iid=%s %s.%s%s",
vmIntrinsics::name_at(iid), klass->external_name(),
name->as_C_string(), full_signature->as_C_string());
if ((klass == vmClasses::MethodHandle_klass() ||
klass == vmClasses::VarHandle_klass()) &&
iid != vmIntrinsics::_none) {
if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
// Most of these do not need an up-call to Java to resolve, so can be done anywhere.
// Do not erase last argument type (MemberName) if it is a static linkTo method.
bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
TempNewSymbol basic_signature =
MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg);
log_info(methodhandles)("lookup_polymorphic_method %s %s => basic %s",
name->as_C_string(),
full_signature->as_C_string(),
basic_signature->as_C_string());
Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
basic_signature,
CHECK_NULL);
if (result != nullptr) {
assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
assert(basic_signature == result->signature(), "predict the result signature");
if (lt_mh.is_enabled()) {
LogStream ls(lt_mh);
ls.print("lookup_polymorphic_method => intrinsic ");
result->print_on(&ls);
}
}
return result;
} else if (iid == vmIntrinsics::_invokeGeneric
&& THREAD->can_call_java()
&& appendix_result_or_null != nullptr) {
// This is a method with type-checking semantics.
// We will ask Java code to spin an adapter method for it.
if (!MethodHandles::enabled()) {
// Make sure the Java part of the runtime has been booted up.
Klass* natives = vmClasses::MethodHandleNatives_klass();
if (natives == nullptr || InstanceKlass::cast(natives)->is_not_initialized()) {
SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
Handle(),
true,
CHECK_NULL);
}
}
Handle appendix;
Method* result = SystemDictionary::find_method_handle_invoker(klass,
name,
full_signature,
link_info.current_klass(),
&appendix,
CHECK_NULL);
if (lt_mh.is_enabled()) {
LogStream ls(lt_mh);
ls.print("lookup_polymorphic_method => (via Java) ");
result->print_on(&ls);
ls.print(" lookup_polymorphic_method => appendix = ");
appendix.is_null() ? ls.print_cr("(none)") : appendix->print_on(&ls);
}
if (result != nullptr) {
#ifdef ASSERT
ResourceMark rm(THREAD);
TempNewSymbol basic_signature =
MethodHandles::lookup_basic_type_signature(full_signature);
int actual_size_of_params = result->size_of_parameters();
int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
// +1 for MethodHandle.this, +1 for trailing MethodType
if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
if (appendix.not_null()) expected_size_of_params += 1;
if (actual_size_of_params != expected_size_of_params) {
tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
result->print();
}
assert(actual_size_of_params == expected_size_of_params,
"%d != %d", actual_size_of_params, expected_size_of_params);
#endif //ASSERT
assert(appendix_result_or_null != nullptr, "");
(*appendix_result_or_null) = appendix;
}
return result;
}
}
return nullptr;
}
static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass) {
assert(ref_klass->is_instance_klass(), "must be");
assert(sel_klass->is_instance_klass(), "must be");
InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
const char* nest_host_error_1 = ref_ik->nest_host_error();
const char* nest_host_error_2 = sel_ik->nest_host_error();
if (nest_host_error_1 != nullptr || nest_host_error_2 != nullptr) {
ss->print(", (%s%s%s)",
(nest_host_error_1 != nullptr) ? nest_host_error_1 : "",
(nest_host_error_1 != nullptr && nest_host_error_2 != nullptr) ? ", " : "",
(nest_host_error_2 != nullptr) ? nest_host_error_2 : "");
}
}
void LinkResolver::check_method_accessability(Klass* ref_klass,
Klass* resolved_klass,
Klass* sel_klass,
const methodHandle& sel_method,
TRAPS) {
AccessFlags flags = sel_method->access_flags();
// Special case: arrays always override "clone". JVMS 2.15.
// If the resolved klass is an array class, and the declaring class
// is java.lang.Object and the method is "clone", set the flags
// to public.
//
// We'll check for the method name first, as that's most likely
// to be false (so we'll short-circuit out of these tests).
if (sel_method->name() == vmSymbols::clone_name() &&
sel_klass == vmClasses::Object_klass() &&
resolved_klass->is_array_klass()) {
// We need to change "protected" to "public".
assert(flags.is_protected(), "clone not protected?");
u2 new_flags = flags.as_method_flags();
new_flags = new_flags & (~JVM_ACC_PROTECTED);
new_flags = new_flags | JVM_ACC_PUBLIC;
flags.set_flags(new_flags);
}
// assert(extra_arg_result_or_null != nullptr, "must be able to return extra argument");
bool can_access = Reflection::verify_member_access(ref_klass,
resolved_klass,
sel_klass,
flags,
true, false, CHECK);
// Any existing exceptions that may have been thrown
// have been allowed to propagate.
if (!can_access) {
ResourceMark rm(THREAD);
stringStream ss;
bool same_module = (sel_klass->module() == ref_klass->module());
ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
ref_klass->external_name(),
sel_method->is_abstract() ? "abstract " : "",
sel_method->is_protected() ? "protected " : "",
sel_method->is_private() ? "private " : "",
sel_method->external_name(),
(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
(same_module) ? "" : "; ",
(same_module) ? "" : sel_klass->class_in_module_of_loader()
);
// For private access see if there was a problem with nest host
// resolution, and if so report that as part of the message.
if (sel_method->is_private()) {
print_nest_host_error_on(&ss, ref_klass, sel_klass);
}
// Names are all known to be < 64k so we know this formatted message is not excessively large.
Exceptions::fthrow(THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"%s",
ss.as_string()
);
return;
}
}
void LinkResolver::resolve_continuation_enter(CallInfo& callinfo, TRAPS) {
Klass* resolved_klass = vmClasses::Continuation_klass();
Symbol* method_name = vmSymbols::enter_name();
Symbol* method_signature = vmSymbols::continuationEnter_signature();
Klass* current_klass = resolved_klass;
LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
Method* resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK);
callinfo.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
}
Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
const constantPoolHandle& pool, int index, TRAPS) {
// This method is used only
// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
// and
// (2) in Bytecode_invoke::static_target
// It appears to fail when applied to an invokeinterface call site.
// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
// resolve klass
if (code == Bytecodes::_invokedynamic) {
Klass* resolved_klass = vmClasses::MethodHandle_klass();
Symbol* method_name = vmSymbols::invoke_name();
Symbol* method_signature = pool->signature_ref_at(index, code);
Klass* current_klass = pool->pool_holder();
LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
return resolve_method(link_info, code, THREAD);
}
LinkInfo link_info(pool, index, methodHandle(), code, CHECK_NULL);
Klass* resolved_klass = link_info.resolved_klass();
if (pool->has_preresolution()
|| ((resolved_klass == vmClasses::MethodHandle_klass() || resolved_klass == vmClasses::VarHandle_klass()) &&
MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
Method* result = ConstantPool::method_at_if_loaded(pool, index);
if (result != nullptr) {
return result;
}
}
if (code == Bytecodes::_invokeinterface) {
return resolve_interface_method(link_info, code, THREAD);
} else if (code == Bytecodes::_invokevirtual) {
return resolve_method(link_info, code, THREAD);
} else if (!resolved_klass->is_interface()) {
return resolve_method(link_info, code, THREAD);
} else {
return resolve_interface_method(link_info, code, THREAD);
}
}
// Check and print a loader constraint violation message for method or interface method
void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
const methodHandle& resolved_method,
const char* method_type, TRAPS) {
Handle current_loader(THREAD, link_info.current_klass()->class_loader());
Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
ResourceMark rm(THREAD);
Symbol* failed_type_symbol =
SystemDictionary::check_signature_loaders(link_info.signature(),
/*klass_being_linked*/ nullptr, // We are not linking class
current_loader,
resolved_loader, true);
if (failed_type_symbol != nullptr) {
Klass* current_class = link_info.current_klass();
ClassLoaderData* current_loader_data = current_class->class_loader_data();
assert(current_loader_data != nullptr, "current class has no class loader data");
Klass* resolved_method_class = resolved_method->method_holder();
ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
assert(target_loader_data != nullptr, "resolved method's class has no class loader data");
stringStream ss;
ss.print("loader constraint violation: when resolving %s '", method_type);
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
ss.print("' the class loader %s of the current class, %s,"
" and the class loader %s for the method's defining class, %s, have"
" different Class objects for the type %s used in the signature (%s; %s)",
current_loader_data->loader_name_and_id(),
current_class->name()->as_C_string(),
target_loader_data->loader_name_and_id(),
resolved_method_class->name()->as_C_string(),
failed_type_symbol->as_C_string(),
current_class->class_in_module_of_loader(false, true),
resolved_method_class->class_in_module_of_loader(false, true));
THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
}
}
void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
Klass* current_klass,
Klass* sel_klass, TRAPS) {
Handle ref_loader(THREAD, current_klass->class_loader());
Handle sel_loader(THREAD, sel_klass->class_loader());
ResourceMark rm(THREAD); // needed for check_signature_loaders
Symbol* failed_type_symbol =
SystemDictionary::check_signature_loaders(sig,
/*klass_being_linked*/ nullptr, // We are not linking class
ref_loader, sel_loader,
false);
if (failed_type_symbol != nullptr) {
stringStream ss;
const char* failed_type_name = failed_type_symbol->as_klass_external_name();
ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
"the class loader %s of the current class, %s, "
"and the class loader %s for the field's defining %s, %s, "
"have different Class objects for type %s (%s; %s)",
field->as_C_string(),
failed_type_name,
current_klass->class_loader_data()->loader_name_and_id(),
current_klass->external_name(),
sel_klass->class_loader_data()->loader_name_and_id(),
sel_klass->external_kind(),
sel_klass->external_name(),
failed_type_name,
current_klass->class_in_module_of_loader(false, true),
sel_klass->class_in_module_of_loader(false, true));
THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
}
}
Method* LinkResolver::resolve_method(const LinkInfo& link_info,
Bytecodes::Code code, TRAPS) {
Handle nested_exception;
Klass* resolved_klass = link_info.resolved_klass();
// 1. For invokevirtual, cannot call an interface method
if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
ResourceMark rm(THREAD);
char buf[200];
jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
resolved_klass->external_name());
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
}
// 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
ResourceMark rm(THREAD);
stringStream ss;
ss.print("Method '");
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
ss.print("' must be Methodref constant");
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
}
// 3. lookup method in resolved klass and its super klasses
methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
// 4. lookup method in all the interfaces implemented by the resolved klass
if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
if (resolved_method.is_null()) {
// JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
Method* method = lookup_polymorphic_method(link_info, (Handle*)nullptr, THREAD);
resolved_method = methodHandle(THREAD, method);
if (HAS_PENDING_EXCEPTION) {
nested_exception = Handle(THREAD, PENDING_EXCEPTION);
CLEAR_PENDING_EXCEPTION;
}
}
}
// 5. method lookup failed
if (resolved_method.is_null()) {
ResourceMark rm(THREAD);
stringStream ss;
ss.print("'");
Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
ss.print("'");
THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
ss.as_string(), nested_exception, nullptr);
}
// 6. access checks, access checking may be turned off when calling from within the VM.
Klass* current_klass = link_info.current_klass();
if (link_info.check_access()) {
assert(current_klass != nullptr , "current_klass should not be null");
// check if method can be accessed by the referring class
check_method_accessability(current_klass,
resolved_klass,
resolved_method->method_holder(),
resolved_method,
CHECK_NULL);
}
if (link_info.check_loader_constraints()) {
// check loader constraints
check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
}
return resolved_method();
}
static void trace_method_resolution(const char* prefix,
Klass* klass,
Klass* resolved_klass,
Method* method,
bool logitables,
int index = -1) {
#ifndef PRODUCT
ResourceMark rm;
Log(itables) logi;
LogStream lsi(logi.trace());
Log(vtables) logv;
LogStream lsv(logv.trace());
outputStream* st;
if (logitables) {
st = &lsi;
} else {
st = &lsv;
}
st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
prefix,
(klass == nullptr ? "<null>" : klass->internal_name()),
resolved_klass->internal_name(),
Method::name_and_sig_as_C_string(resolved_klass,
method->name(),
method->signature()),
method->method_holder()->internal_name());
method->print_linkage_flags(st);
if (index != -1) {
st->print("vtable_index:%d", index);
}
st->cr();
#endif // PRODUCT
}
// Do linktime resolution of a method in the interface within the context of the specified bytecode.
Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
Klass* resolved_klass = link_info.resolved_klass();
// check if klass is interface
if (!resolved_klass->is_interface()) {
ResourceMark rm(THREAD);
char buf[200];
jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
}
// check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
ResourceMark rm(THREAD);
stringStream ss;
ss.print("Method '");
Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
ss.print("' must be InterfaceMethodref constant");
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
}
// lookup method in this interface or its super, java.lang.Object
// JDK8: also look for static methods
methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
// lookup method in all the super-interfaces
resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
}
if (resolved_method.is_null()) {
// no method found
ResourceMark rm(THREAD);
stringStream ss;
ss.print("'");
Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
ss.print("'");
THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
}
if (link_info.check_access()) {
// JDK8 adds non-public interface methods, and accessability check requirement
Klass* current_klass = link_info.current_klass();
assert(current_klass != nullptr , "current_klass should not be null");
// check if method can be accessed by the referring class
check_method_accessability(current_klass,
resolved_klass,
resolved_method->method_holder(),
resolved_method,
CHECK_NULL);
}
if (link_info.check_loader_constraints()) {
check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
}
if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
ResourceMark rm(THREAD);
stringStream ss;
ss.print("Expected instance not static method '");
Method::print_external_name(&ss, resolved_klass,
resolved_method->name(), resolved_method->signature());
ss.print("'");
THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
}
if (log_develop_is_enabled(Trace, itables)) {
char buf[200];
jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
Bytecodes::name(code));
trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
}
return resolved_method();
}
//------------------------------------------------------------------------------------------------------------------------
// Field resolution
void LinkResolver::check_field_accessability(Klass* ref_klass,
Klass* resolved_klass,
Klass* sel_klass,
const fieldDescriptor& fd,
TRAPS) {
bool can_access = Reflection::verify_member_access(ref_klass,
resolved_klass,
sel_klass,
fd.access_flags(),
true, false, CHECK);
// Any existing exceptions that may have been thrown, for example LinkageErrors
// from nest-host resolution, have been allowed to propagate.
if (!can_access) {
bool same_module = (sel_klass->module() == ref_klass->module());
ResourceMark rm(THREAD);
stringStream ss;
ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
ref_klass->external_name(),
fd.is_protected() ? "protected " : "",
fd.is_private() ? "private " : "",
sel_klass->external_name(),
fd.name()->as_C_string(),
(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
(same_module) ? "" : "; ",
(same_module) ? "" : sel_klass->class_in_module_of_loader()
);
// For private access see if there was a problem with nest host
// resolution, and if so report that as part of the message.
if (fd.is_private()) {
print_nest_host_error_on(&ss, ref_klass, sel_klass);
}
// Names are all known to be < 64k so we know this formatted message is not excessively large.
Exceptions::fthrow(THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"%s",
ss.as_string()
);
return;
}
}
void LinkResolver::resolve_field_access(fieldDescriptor& fd,
const constantPoolHandle& pool,
int index,
const methodHandle& method,
Bytecodes::Code byte,
bool initialize_class, TRAPS) {
LinkInfo link_info(pool, index, method, byte, CHECK);
resolve_field(fd, link_info, byte, initialize_class, CHECK);
}
void LinkResolver::resolve_field(fieldDescriptor& fd,
const LinkInfo& link_info,
Bytecodes::Code byte, bool initialize_class,
TRAPS) {
assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||