/
method.cpp
2513 lines (2242 loc) · 87.6 KB
/
method.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, 2022, 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 "precompiled.hpp"
#include "cds/cppVtables.hpp"
#include "cds/metaspaceShared.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/metadataOnStackMark.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmClasses.hpp"
#include "code/codeCache.hpp"
#include "code/debugInfoRec.hpp"
#include "compiler/compilationPolicy.hpp"
#include "gc/shared/collectedHeap.inline.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/bytecodeTracer.hpp"
#include "interpreter/bytecodes.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/oopMapCache.hpp"
#include "logging/log.hpp"
#include "logging/logTag.hpp"
#include "logging/logStream.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/metadataFactory.hpp"
#include "memory/metaspaceClosure.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/constMethod.hpp"
#include "oops/constantPool.hpp"
#include "oops/klass.inline.hpp"
#include "oops/method.inline.hpp"
#include "oops/methodData.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/symbol.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/methodHandles.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomic.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/init.hpp"
#include "runtime/orderAccess.hpp"
#include "runtime/relocator.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/vm_version.hpp"
#include "services/memTracker.hpp"
#include "utilities/align.hpp"
#include "utilities/quickSort.hpp"
#include "utilities/vmError.hpp"
#include "utilities/xmlstream.hpp"
// Implementation of Method
Method* Method::allocate(ClassLoaderData* loader_data,
int byte_code_size,
AccessFlags access_flags,
InlineTableSizes* sizes,
ConstMethod::MethodType method_type,
Symbol* name,
TRAPS) {
assert(!access_flags.is_native() || byte_code_size == 0,
"native methods should not contain byte codes");
ConstMethod* cm = ConstMethod::allocate(loader_data,
byte_code_size,
sizes,
method_type,
CHECK_NULL);
int size = Method::size(access_flags.is_native());
return new (loader_data, size, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags, name);
}
Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
NoSafepointVerifier no_safepoint;
set_constMethod(xconst);
set_access_flags(access_flags);
set_intrinsic_id(vmIntrinsics::_none);
set_force_inline(false);
set_hidden(false);
set_dont_inline(false);
set_has_injected_profile(false);
set_method_data(NULL);
clear_method_counters();
set_vtable_index(Method::garbage_vtable_index);
// Fix and bury in Method*
set_interpreter_entry(NULL); // sets i2i entry and from_int
set_adapter_entry(NULL);
Method::clear_code(); // from_c/from_i get set to c2i/i2i
if (access_flags.is_native()) {
clear_native_function();
set_signature_handler(NULL);
}
NOT_PRODUCT(set_compiled_invocation_count(0);)
// Name is very useful for debugging.
NOT_PRODUCT(_name = name;)
}
// Release Method*. The nmethod will be gone when we get here because
// we've walked the code cache.
void Method::deallocate_contents(ClassLoaderData* loader_data) {
MetadataFactory::free_metadata(loader_data, constMethod());
set_constMethod(NULL);
MetadataFactory::free_metadata(loader_data, method_data());
set_method_data(NULL);
MetadataFactory::free_metadata(loader_data, method_counters());
clear_method_counters();
// The nmethod will be gone when we get here.
if (code() != NULL) _code = NULL;
}
void Method::release_C_heap_structures() {
if (method_data()) {
#if INCLUDE_JVMCI
FailedSpeculation::free_failed_speculations(method_data()->get_failed_speculations_address());
#endif
// Destroy MethodData
method_data()->~MethodData();
}
}
address Method::get_i2c_entry() {
assert(adapter() != NULL, "must have");
return adapter()->get_i2c_entry();
}
address Method::get_c2i_entry() {
assert(adapter() != NULL, "must have");
return adapter()->get_c2i_entry();
}
address Method::get_c2i_unverified_entry() {
assert(adapter() != NULL, "must have");
return adapter()->get_c2i_unverified_entry();
}
address Method::get_c2i_no_clinit_check_entry() {
assert(VM_Version::supports_fast_class_init_checks(), "");
assert(adapter() != NULL, "must have");
return adapter()->get_c2i_no_clinit_check_entry();
}
char* Method::name_and_sig_as_C_string() const {
return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
}
char* Method::name_and_sig_as_C_string(char* buf, int size) const {
return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
}
char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
const char* klass_name = klass->external_name();
int klass_name_len = (int)strlen(klass_name);
int method_name_len = method_name->utf8_length();
int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
strcpy(dest, klass_name);
dest[klass_name_len] = '.';
strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
dest[len] = 0;
return dest;
}
char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
Symbol* klass_name = klass->name();
klass_name->as_klass_external_name(buf, size);
int len = (int)strlen(buf);
if (len < size - 1) {
buf[len++] = '.';
method_name->as_C_string(&(buf[len]), size - len);
len = (int)strlen(buf);
signature->as_C_string(&(buf[len]), size - len);
}
return buf;
}
const char* Method::external_name() const {
return external_name(constants()->pool_holder(), name(), signature());
}
void Method::print_external_name(outputStream *os) const {
print_external_name(os, constants()->pool_holder(), name(), signature());
}
const char* Method::external_name(Klass* klass, Symbol* method_name, Symbol* signature) {
stringStream ss;
print_external_name(&ss, klass, method_name, signature);
return ss.as_string();
}
void Method::print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature) {
signature->print_as_signature_external_return_type(os);
os->print(" %s.%s(", klass->external_name(), method_name->as_C_string());
signature->print_as_signature_external_parameters(os);
os->print(")");
}
int Method::fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS) {
if (log_is_enabled(Debug, exceptions)) {
ResourceMark rm(THREAD);
log_debug(exceptions)("Looking for catch handler for exception of type \"%s\" in method \"%s\"",
ex_klass == NULL ? "NULL" : ex_klass->external_name(), mh->name()->as_C_string());
}
// exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index)
// access exception table
ExceptionTable table(mh());
int length = table.length();
// iterate through all entries sequentially
constantPoolHandle pool(THREAD, mh->constants());
for (int i = 0; i < length; i ++) {
//reacquire the table in case a GC happened
ExceptionTable table(mh());
int beg_bci = table.start_pc(i);
int end_bci = table.end_pc(i);
assert(beg_bci <= end_bci, "inconsistent exception table");
log_debug(exceptions)(" - checking exception table entry for BCI %d to %d",
beg_bci, end_bci);
if (beg_bci <= throw_bci && throw_bci < end_bci) {
// exception handler bci range covers throw_bci => investigate further
log_debug(exceptions)(" - entry covers throw point BCI %d", throw_bci);
int handler_bci = table.handler_pc(i);
int klass_index = table.catch_type_index(i);
if (klass_index == 0) {
if (log_is_enabled(Info, exceptions)) {
ResourceMark rm(THREAD);
log_info(exceptions)("Found catch-all handler for exception of type \"%s\" in method \"%s\" at BCI: %d",
ex_klass == NULL ? "NULL" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci);
}
return handler_bci;
} else if (ex_klass == NULL) {
// Is this even possible?
if (log_is_enabled(Info, exceptions)) {
ResourceMark rm(THREAD);
log_info(exceptions)("NULL exception class is implicitly caught by handler in method \"%s\" at BCI: %d",
mh()->name()->as_C_string(), handler_bci);
}
return handler_bci;
} else {
if (log_is_enabled(Debug, exceptions)) {
ResourceMark rm(THREAD);
log_debug(exceptions)(" - resolving catch type \"%s\"",
pool->klass_name_at(klass_index)->as_C_string());
}
// we know the exception class => get the constraint class
// this may require loading of the constraint class; if verification
// fails or some other exception occurs, return handler_bci
Klass* k = pool->klass_at(klass_index, THREAD);
if (HAS_PENDING_EXCEPTION) {
if (log_is_enabled(Debug, exceptions)) {
ResourceMark rm(THREAD);
log_debug(exceptions)(" - exception \"%s\" occurred resolving catch type",
PENDING_EXCEPTION->klass()->external_name());
}
return handler_bci;
}
assert(k != NULL, "klass not loaded");
if (ex_klass->is_subtype_of(k)) {
if (log_is_enabled(Info, exceptions)) {
ResourceMark rm(THREAD);
log_info(exceptions)("Found matching handler for exception of type \"%s\" in method \"%s\" at BCI: %d",
ex_klass == NULL ? "NULL" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci);
}
return handler_bci;
}
}
}
}
if (log_is_enabled(Debug, exceptions)) {
ResourceMark rm(THREAD);
log_debug(exceptions)("No catch handler found for exception of type \"%s\" in method \"%s\"",
ex_klass->external_name(), mh->name()->as_C_string());
}
return -1;
}
void Method::mask_for(int bci, InterpreterOopMap* mask) {
methodHandle h_this(Thread::current(), this);
// Only GC uses the OopMapCache during thread stack root scanning
// any other uses generate an oopmap but do not save it in the cache.
if (Universe::heap()->is_gc_active()) {
method_holder()->mask_for(h_this, bci, mask);
} else {
OopMapCache::compute_one_oop_map(h_this, bci, mask);
}
return;
}
int Method::bci_from(address bcp) const {
if (is_native() && bcp == 0) {
return 0;
}
// Do not have a ResourceMark here because AsyncGetCallTrace stack walking code
// may call this after interrupting a nested ResourceMark.
assert(is_native() && bcp == code_base() || contains(bcp) || VMError::is_error_reported(),
"bcp doesn't belong to this method. bcp: " INTPTR_FORMAT, p2i(bcp));
return bcp - code_base();
}
int Method::validate_bci(int bci) const {
return (bci == 0 || bci < code_size()) ? bci : -1;
}
// Return bci if it appears to be a valid bcp
// Return -1 otherwise.
// Used by profiling code, when invalid data is a possibility.
// The caller is responsible for validating the Method* itself.
int Method::validate_bci_from_bcp(address bcp) const {
// keep bci as -1 if not a valid bci
int bci = -1;
if (bcp == 0 || bcp == code_base()) {
// code_size() may return 0 and we allow 0 here
// the method may be native
bci = 0;
} else if (contains(bcp)) {
bci = bcp - code_base();
}
// Assert that if we have dodged any asserts, bci is negative.
assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0");
return bci;
}
address Method::bcp_from(int bci) const {
assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()),
"illegal bci: %d for %s method", bci, is_native() ? "native" : "non-native");
address bcp = code_base() + bci;
assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method");
return bcp;
}
address Method::bcp_from(address bcp) const {
if (is_native() && bcp == NULL) {
return code_base();
} else {
return bcp;
}
}
int Method::size(bool is_native) {
// If native, then include pointers for native_function and signature_handler
int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
return align_metadata_size(header_size() + extra_words);
}
Symbol* Method::klass_name() const {
return method_holder()->name();
}
void Method::metaspace_pointers_do(MetaspaceClosure* it) {
log_trace(cds)("Iter(Method): %p", this);
if (!method_holder()->is_rewritten()) {
it->push(&_constMethod, MetaspaceClosure::_writable);
} else {
it->push(&_constMethod);
}
it->push(&_method_data);
it->push(&_method_counters);
NOT_PRODUCT(it->push(&_name);)
}
// Attempt to return method to original state. Clear any pointers
// (to objects outside the shared spaces). We won't be able to predict
// where they should point in a new JVM. Further initialize some
// entries now in order allow them to be write protected later.
void Method::remove_unshareable_info() {
unlink_method();
JFR_ONLY(REMOVE_METHOD_ID(this);)
}
void Method::set_vtable_index(int index) {
if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
// At runtime initialize_vtable is rerun as part of link_class_impl()
// for a shared class loaded by the non-boot loader to obtain the loader
// constraints based on the runtime classloaders' context.
return; // don't write into the shared class
} else {
_vtable_index = index;
}
}
void Method::set_itable_index(int index) {
if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
// At runtime initialize_itable is rerun as part of link_class_impl()
// for a shared class loaded by the non-boot loader to obtain the loader
// constraints based on the runtime classloaders' context. The dumptime
// itable index should be the same as the runtime index.
assert(_vtable_index == itable_index_max - index,
"archived itable index is different from runtime index");
return; // don’t write into the shared class
} else {
_vtable_index = itable_index_max - index;
}
assert(valid_itable_index(), "");
}
// The RegisterNatives call being attempted tried to register with a method that
// is not native. Ask JVM TI what prefixes have been specified. Then check
// to see if the native method is now wrapped with the prefixes. See the
// SetNativeMethodPrefix(es) functions in the JVM TI Spec for details.
static Method* find_prefixed_native(Klass* k, Symbol* name, Symbol* signature, TRAPS) {
#if INCLUDE_JVMTI
ResourceMark rm(THREAD);
Method* method;
int name_len = name->utf8_length();
char* name_str = name->as_utf8();
int prefix_count;
char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
for (int i = 0; i < prefix_count; i++) {
char* prefix = prefixes[i];
int prefix_len = (int)strlen(prefix);
// try adding this prefix to the method name and see if it matches another method name
int trial_len = name_len + prefix_len;
char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
strcpy(trial_name_str, prefix);
strcat(trial_name_str, name_str);
TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len);
if (trial_name == NULL) {
continue; // no such symbol, so this prefix wasn't used, try the next prefix
}
method = k->lookup_method(trial_name, signature);
if (method == NULL) {
continue; // signature doesn't match, try the next prefix
}
if (method->is_native()) {
method->set_is_prefixed_native();
return method; // wahoo, we found a prefixed version of the method, return it
}
// found as non-native, so prefix is good, add it, probably just need more prefixes
name_len = trial_len;
name_str = trial_name_str;
}
#endif // INCLUDE_JVMTI
return NULL; // not found
}
bool Method::register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) {
Method* method = k->lookup_method(name, signature);
if (method == NULL) {
ResourceMark rm(THREAD);
stringStream st;
st.print("Method '");
print_external_name(&st, k, name, signature);
st.print("' name or signature does not match");
THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
}
if (!method->is_native()) {
// trying to register to a non-native method, see if a JVM TI agent has added prefix(es)
method = find_prefixed_native(k, name, signature, THREAD);
if (method == NULL) {
ResourceMark rm(THREAD);
stringStream st;
st.print("Method '");
print_external_name(&st, k, name, signature);
st.print("' is not declared as native");
THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
}
}
if (entry != NULL) {
method->set_native_function(entry, native_bind_event_is_interesting);
} else {
method->clear_native_function();
}
if (log_is_enabled(Debug, jni, resolve)) {
ResourceMark rm(THREAD);
log_debug(jni, resolve)("[Registering JNI native method %s.%s]",
method->method_holder()->external_name(),
method->name()->as_C_string());
}
return true;
}
bool Method::was_executed_more_than(int n) {
// Invocation counter is reset when the Method* is compiled.
// If the method has compiled code we therefore assume it has
// be excuted more than n times.
if (is_accessor() || is_empty_method() || (code() != NULL)) {
// interpreter doesn't bump invocation counter of trivial methods
// compiler does not bump invocation counter of compiled methods
return true;
}
else if ((method_counters() != NULL &&
method_counters()->invocation_counter()->carry()) ||
(method_data() != NULL &&
method_data()->invocation_counter()->carry())) {
// The carry bit is set when the counter overflows and causes
// a compilation to occur. We don't know how many times
// the counter has been reset, so we simply assume it has
// been executed more than n times.
return true;
} else {
return invocation_count() > n;
}
}
void Method::print_invocation_count() {
//---< compose+print method return type, klass, name, and signature >---
if (is_static()) tty->print("static ");
if (is_final()) tty->print("final ");
if (is_synchronized()) tty->print("synchronized ");
if (is_native()) tty->print("native ");
tty->print("%s::", method_holder()->external_name());
name()->print_symbol_on(tty);
signature()->print_symbol_on(tty);
if (WizardMode) {
// dump the size of the byte codes
tty->print(" {%d}", code_size());
}
tty->cr();
// Counting based on signed int counters tends to overflow with
// longer-running workloads on fast machines. The counters under
// consideration here, however, are limited in range by counting
// logic. See InvocationCounter:count_limit for example.
// No "overflow precautions" need to be implemented here.
tty->print_cr (" interpreter_invocation_count: " INT32_FORMAT_W(11), interpreter_invocation_count());
tty->print_cr (" invocation_counter: " INT32_FORMAT_W(11), invocation_count());
tty->print_cr (" backedge_counter: " INT32_FORMAT_W(11), backedge_count());
if (method_data() != NULL) {
tty->print_cr (" decompile_count: " UINT32_FORMAT_W(11), method_data()->decompile_count());
}
#ifndef PRODUCT
if (CountCompiledCalls) {
tty->print_cr (" compiled_invocation_count: " INT64_FORMAT_W(11), compiled_invocation_count());
}
#endif
}
// Build a MethodData* object to hold information about this method
// collected in the interpreter.
void Method::build_interpreter_method_data(const methodHandle& method, TRAPS) {
// Do not profile the method if metaspace has hit an OOM previously
// allocating profiling data. Callers clear pending exception so don't
// add one here.
if (ClassLoaderDataGraph::has_metaspace_oom()) {
return;
}
// Grab a lock here to prevent multiple
// MethodData*s from being created.
MutexLocker ml(THREAD, MethodData_lock);
if (method->method_data() == NULL) {
ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
MethodData* method_data = MethodData::allocate(loader_data, method, THREAD);
if (HAS_PENDING_EXCEPTION) {
CompileBroker::log_metaspace_failure();
ClassLoaderDataGraph::set_metaspace_oom(true);
return; // return the exception (which is cleared)
}
method->set_method_data(method_data);
if (PrintMethodData && (Verbose || WizardMode)) {
ResourceMark rm(THREAD);
tty->print("build_interpreter_method_data for ");
method->print_name(tty);
tty->cr();
// At the end of the run, the MDO, full of data, will be dumped.
}
}
}
MethodCounters* Method::build_method_counters(Thread* current, Method* m) {
// Do not profile the method if metaspace has hit an OOM previously
if (ClassLoaderDataGraph::has_metaspace_oom()) {
return NULL;
}
methodHandle mh(current, m);
MethodCounters* counters;
if (current->is_Java_thread()) {
JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
// Use the TRAPS version for a JavaThread so it will adjust the GC threshold
// if needed.
counters = MethodCounters::allocate_with_exception(mh, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
}
} else {
// Call metaspace allocation that doesn't throw exception if the
// current thread isn't a JavaThread, ie. the VMThread.
counters = MethodCounters::allocate_no_exception(mh);
}
if (counters == NULL) {
CompileBroker::log_metaspace_failure();
ClassLoaderDataGraph::set_metaspace_oom(true);
return NULL;
}
if (!mh->init_method_counters(counters)) {
MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
}
if (LogTouchedMethods) {
mh->log_touched(current);
}
return mh->method_counters();
}
bool Method::init_method_counters(MethodCounters* counters) {
// Try to install a pointer to MethodCounters, return true on success.
return Atomic::replace_if_null(&_method_counters, counters);
}
int Method::extra_stack_words() {
// not an inline function, to avoid a header dependency on Interpreter
return extra_stack_entries() * Interpreter::stackElementSize;
}
// Derive size of parameters, return type, and fingerprint,
// all in one pass, which is run at load time.
// We need the first two, and might as well grab the third.
void Method::compute_from_signature(Symbol* sig) {
// At this point, since we are scanning the signature,
// we might as well compute the whole fingerprint.
Fingerprinter fp(sig, is_static());
set_size_of_parameters(fp.size_of_parameters());
constMethod()->set_result_type(fp.return_type());
constMethod()->set_fingerprint(fp.fingerprint());
}
bool Method::is_vanilla_constructor() const {
// Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
// which only calls the superclass vanilla constructor and possibly does stores of
// zero constants to local fields:
//
// aload_0
// invokespecial
// indexbyte1
// indexbyte2
//
// followed by an (optional) sequence of:
//
// aload_0
// aconst_null / iconst_0 / fconst_0 / dconst_0
// putfield
// indexbyte1
// indexbyte2
//
// followed by:
//
// return
assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
int size = code_size();
// Check if size match
if (size == 0 || size % 5 != 0) return false;
address cb = code_base();
int last = size - 1;
if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
// Does not call superclass default constructor
return false;
}
// Check optional sequence
for (int i = 4; i < last; i += 5) {
if (cb[i] != Bytecodes::_aload_0) return false;
if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
if (cb[i+2] != Bytecodes::_putfield) return false;
}
return true;
}
bool Method::compute_has_loops_flag() {
BytecodeStream bcs(methodHandle(Thread::current(), this));
Bytecodes::Code bc;
while ((bc = bcs.next()) >= 0) {
switch (bc) {
case Bytecodes::_ifeq:
case Bytecodes::_ifnull:
case Bytecodes::_iflt:
case Bytecodes::_ifle:
case Bytecodes::_ifne:
case Bytecodes::_ifnonnull:
case Bytecodes::_ifgt:
case Bytecodes::_ifge:
case Bytecodes::_if_icmpeq:
case Bytecodes::_if_icmpne:
case Bytecodes::_if_icmplt:
case Bytecodes::_if_icmpgt:
case Bytecodes::_if_icmple:
case Bytecodes::_if_icmpge:
case Bytecodes::_if_acmpeq:
case Bytecodes::_if_acmpne:
case Bytecodes::_goto:
case Bytecodes::_jsr:
if (bcs.dest() < bcs.next_bci()) _access_flags.set_has_loops();
break;
case Bytecodes::_goto_w:
case Bytecodes::_jsr_w:
if (bcs.dest_w() < bcs.next_bci()) _access_flags.set_has_loops();
break;
case Bytecodes::_lookupswitch: {
Bytecode_lookupswitch lookupswitch(this, bcs.bcp());
if (lookupswitch.default_offset() < 0) {
_access_flags.set_has_loops();
} else {
for (int i = 0; i < lookupswitch.number_of_pairs(); ++i) {
LookupswitchPair pair = lookupswitch.pair_at(i);
if (pair.offset() < 0) {
_access_flags.set_has_loops();
break;
}
}
}
break;
}
case Bytecodes::_tableswitch: {
Bytecode_tableswitch tableswitch(this, bcs.bcp());
if (tableswitch.default_offset() < 0) {
_access_flags.set_has_loops();
} else {
for (int i = 0; i < tableswitch.length(); ++i) {
if (tableswitch.dest_offset_at(i) < 0) {
_access_flags.set_has_loops();
}
}
}
break;
}
default:
break;
}
}
_access_flags.set_loops_flag_init();
return _access_flags.has_loops();
}
bool Method::is_final_method(AccessFlags class_access_flags) const {
// or "does_not_require_vtable_entry"
// default method or overpass can occur, is not final (reuses vtable entry)
// private methods in classes get vtable entries for backward class compatibility.
if (is_overpass() || is_default_method()) return false;
return is_final() || class_access_flags.is_final();
}
bool Method::is_final_method() const {
return is_final_method(method_holder()->access_flags());
}
bool Method::is_default_method() const {
if (method_holder() != NULL &&
method_holder()->is_interface() &&
!is_abstract() && !is_private()) {
return true;
} else {
return false;
}
}
bool Method::can_be_statically_bound(AccessFlags class_access_flags) const {
if (is_final_method(class_access_flags)) return true;
#ifdef ASSERT
ResourceMark rm;
bool is_nonv = (vtable_index() == nonvirtual_vtable_index);
if (class_access_flags.is_interface()) {
assert(is_nonv == is_static() || is_nonv == is_private(),
"nonvirtual unexpected for non-static, non-private: %s",
name_and_sig_as_C_string());
}
#endif
assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question");
return vtable_index() == nonvirtual_vtable_index;
}
bool Method::can_be_statically_bound() const {
return can_be_statically_bound(method_holder()->access_flags());
}
bool Method::can_be_statically_bound(InstanceKlass* context) const {
return (method_holder() == context) && can_be_statically_bound();
}
/**
* Returns false if this is one of specially treated methods for
* which we have to provide stack trace in throw in compiled code.
* Returns true otherwise.
*/
bool Method::can_omit_stack_trace() {
if (klass_name() == vmSymbols::sun_invoke_util_ValueConversions()) {
return false; // All methods in sun.invoke.util.ValueConversions
}
return true;
}
bool Method::is_accessor() const {
return is_getter() || is_setter();
}
bool Method::is_getter() const {
if (code_size() != 5) return false;
if (size_of_parameters() != 1) return false;
if (java_code_at(0) != Bytecodes::_aload_0) return false;
if (java_code_at(1) != Bytecodes::_getfield) return false;
switch (java_code_at(4)) {
case Bytecodes::_ireturn:
case Bytecodes::_lreturn:
case Bytecodes::_freturn:
case Bytecodes::_dreturn:
case Bytecodes::_areturn:
break;
default:
return false;
}
return true;
}
bool Method::is_setter() const {
if (code_size() != 6) return false;
if (java_code_at(0) != Bytecodes::_aload_0) return false;
switch (java_code_at(1)) {
case Bytecodes::_iload_1:
case Bytecodes::_aload_1:
case Bytecodes::_fload_1:
if (size_of_parameters() != 2) return false;
break;
case Bytecodes::_dload_1:
case Bytecodes::_lload_1:
if (size_of_parameters() != 3) return false;
break;
default:
return false;
}
if (java_code_at(2) != Bytecodes::_putfield) return false;
if (java_code_at(5) != Bytecodes::_return) return false;
return true;
}
bool Method::is_constant_getter() const {
int last_index = code_size() - 1;
// Check if the first 1-3 bytecodes are a constant push
// and the last bytecode is a return.
return (2 <= code_size() && code_size() <= 4 &&
Bytecodes::is_const(java_code_at(0)) &&
Bytecodes::length_for(java_code_at(0)) == last_index &&
Bytecodes::is_return(java_code_at(last_index)));
}
bool Method::is_initializer() const {
return is_object_initializer() || is_static_initializer();
}
bool Method::has_valid_initializer_flags() const {
return (is_static() ||
method_holder()->major_version() < 51);
}
bool Method::is_static_initializer() const {
// For classfiles version 51 or greater, ensure that the clinit method is
// static. Non-static methods with the name "<clinit>" are not static
// initializers. (older classfiles exempted for backward compatibility)
return name() == vmSymbols::class_initializer_name() &&
has_valid_initializer_flags();
}
bool Method::is_object_initializer() const {
return name() == vmSymbols::object_initializer_name();
}
bool Method::needs_clinit_barrier() const {
return is_static() && !method_holder()->is_initialized();
}
objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
int length = method->checked_exceptions_length();
if (length == 0) { // common case
return objArrayHandle(THREAD, Universe::the_empty_class_array());
} else {
methodHandle h_this(THREAD, method);
objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
objArrayHandle mirrors (THREAD, m_oop);
for (int i = 0; i < length; i++) {
CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
if (log_is_enabled(Warning, exceptions) &&
!k->is_subclass_of(vmClasses::Throwable_klass())) {
ResourceMark rm(THREAD);
log_warning(exceptions)(
"Class %s in throws clause of method %s is not a subtype of class java.lang.Throwable",
k->external_name(), method->external_name());
}
mirrors->obj_at_put(i, k->java_mirror());
}
return mirrors;
}
};
int Method::line_number_from_bci(int bci) const {
int best_bci = 0;
int best_line = -1;
if (bci == SynchronizationEntryBCI) bci = 0;
if (0 <= bci && bci < code_size() && has_linenumber_table()) {
// The line numbers are a short array of 2-tuples [start_pc, line_number].
// Not necessarily sorted and not necessarily one-to-one.
CompressedLineNumberReadStream stream(compressed_linenumber_table());
while (stream.read_pair()) {
if (stream.bci() == bci) {
// perfect match
return stream.line();
} else {
// update best_bci/line
if (stream.bci() < bci && stream.bci() >= best_bci) {
best_bci = stream.bci();
best_line = stream.line();
}
}
}
}
return best_line;
}
bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
Thread *thread = Thread::current();
Symbol* klass_name = constants()->klass_name_at(klass_index);
Handle loader(thread, method_holder()->class_loader());
Handle prot (thread, method_holder()->protection_domain());
return SystemDictionary::find_instance_klass(klass_name, loader, prot) != NULL;
} else {
return true;
}
}
bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
int klass_index = constants()->klass_ref_index_at(refinfo_index);
if (must_be_resolved) {
// Make sure klass is resolved in constantpool.
if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
}
return is_klass_loaded_by_klass_index(klass_index);
}
void Method::set_native_function(address function, bool post_event_flag) {
assert(function != NULL, "use clear_native_function to unregister natives");
assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
address* native_function = native_function_addr();
// We can see racers trying to place the same native function into place. Once
// is plenty.
address current = *native_function;
if (current == function) return;
if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
function != NULL) {
// native_method_throw_unsatisfied_link_error_entry() should only
// be passed when post_event_flag is false.