1 /*
2 * Copyright (c) 1997, 2025, 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 #ifndef SHARE_OOPS_METHOD_HPP
26 #define SHARE_OOPS_METHOD_HPP
27
28 #include "code/compressedStream.hpp"
29 #include "compiler/compilerDefinitions.hpp"
30 #include "oops/annotations.hpp"
31 #include "oops/constantPool.hpp"
32 #include "oops/instanceKlass.hpp"
33 #include "oops/methodFlags.hpp"
34 #include "oops/oop.hpp"
35 #include "utilities/accessFlags.hpp"
36 #include "utilities/align.hpp"
37 #include "utilities/growableArray.hpp"
38 #include "utilities/macros.hpp"
39 #include "utilities/vmEnums.hpp"
40 #if INCLUDE_JFR
41 #include "jfr/support/jfrTraceIdExtension.hpp"
42 #endif
43
44
45 // A Method represents a Java method.
46 //
47 // Note that most applications load thousands of methods, so keeping the size of this
48 // class small has a big impact on footprint.
49 //
50 // Note that native_function and signature_handler have to be at fixed offsets
51 // (required by the interpreter)
52 //
53 // Method embedded field layout (after declared fields):
54 // [EMBEDDED native_function (present only if native) ]
55 // [EMBEDDED signature_handler (present only if native) ]
56
57 class CheckedExceptionElement;
58 class LocalVariableTableElement;
59 class AdapterHandlerEntry;
60 class MethodData;
61 class MethodCounters;
62 class MethodTrainingData;
63 class ConstMethod;
64 class InlineTableSizes;
65 class nmethod;
66 class InterpreterOopMap;
67
68 class Method : public Metadata {
69 friend class VMStructs;
70 friend class JVMCIVMStructs;
71 friend class MethodTest;
72 private:
73 // If you add a new field that points to any metaspace object, you
74 // must add this field to Method::metaspace_pointers_do().
75 ConstMethod* _constMethod; // Method read-only data.
76 MethodData* _method_data;
77 MethodCounters* _method_counters;
78 AdapterHandlerEntry* _adapter;
79 int _vtable_index; // vtable index of this method (see VtableIndexFlag)
80 AccessFlags _access_flags; // Access flags
81 MethodFlags _flags;
82
83 u2 _intrinsic_id; // vmSymbols::intrinsic_id (0 == _none)
84
85 JFR_ONLY(DEFINE_TRACE_FLAG;)
86
87 #ifndef PRODUCT
88 int64_t _compiled_invocation_count;
89
90 Symbol* _name;
91 #endif
92 // Entry point for calling both from and to the interpreter.
93 address _i2i_entry; // All-args-on-stack calling convention
94 // Entry point for calling from compiled code, to compiled code if it exists
95 // or else the interpreter.
96 volatile address _from_compiled_entry; // Cache of: _code ? _code->entry_point() : _adapter->c2i_entry()
97 // The entry point for calling both from and to compiled code is
98 // "_code->entry_point()". Because of tiered compilation and de-opt, this
99 // field can come and go. It can transition from null to not-null at any
100 // time (whenever a compile completes). It can transition from not-null to
101 // null only at safepoints (because of a de-opt).
102 nmethod* volatile _code; // Points to the corresponding piece of native code
103 volatile address _from_interpreted_entry; // Cache of _code ? _adapter->i2c_entry() : _i2i_entry
104
105 // Constructor
106 Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name);
107 public:
108
109 static Method* allocate(ClassLoaderData* loader_data,
110 int byte_code_size,
111 AccessFlags access_flags,
112 InlineTableSizes* sizes,
113 ConstMethod::MethodType method_type,
114 Symbol* name,
115 TRAPS);
116
117 // CDS and vtbl checking can create an empty Method to get vtbl pointer.
118 Method(){}
119
120 virtual bool is_method() const { return true; }
121
122 #if INCLUDE_CDS
123 void remove_unshareable_info();
124 void restore_unshareable_info(TRAPS);
125 static void restore_archived_method_handle_intrinsic(methodHandle m, TRAPS);
126 #endif
127
128 // accessors for instance variables
129
130 ConstMethod* constMethod() const { return _constMethod; }
131 void set_constMethod(ConstMethod* xconst) { _constMethod = xconst; }
132
133
134 static address make_adapters(const methodHandle& mh, TRAPS);
135 address from_compiled_entry() const;
136 address from_interpreted_entry() const;
137
138 // access flag
139 AccessFlags access_flags() const { return _access_flags; }
140 void set_access_flags(AccessFlags flags) { _access_flags = flags; }
141
142 // name
143 Symbol* name() const { return constants()->symbol_at(name_index()); }
144 u2 name_index() const { return constMethod()->name_index(); }
145 void set_name_index(int index) { constMethod()->set_name_index(index); }
146
147 // signature
148 Symbol* signature() const { return constants()->symbol_at(signature_index()); }
149 u2 signature_index() const { return constMethod()->signature_index(); }
150 void set_signature_index(int index) { constMethod()->set_signature_index(index); }
151
152 // generics support
153 Symbol* generic_signature() const { int idx = generic_signature_index(); return ((idx != 0) ? constants()->symbol_at(idx) : nullptr); }
154 u2 generic_signature_index() const { return constMethod()->generic_signature_index(); }
155
156 // annotations support
157 AnnotationArray* annotations() const {
158 return constMethod()->method_annotations();
159 }
160 AnnotationArray* parameter_annotations() const {
161 return constMethod()->parameter_annotations();
162 }
163 AnnotationArray* annotation_default() const {
164 return constMethod()->default_annotations();
165 }
166 AnnotationArray* type_annotations() const {
167 return constMethod()->type_annotations();
168 }
169
170 // Helper routine: get klass name + "." + method name + signature as
171 // C string, for the purpose of providing more useful
172 // fatal error handling. The string is allocated in resource
173 // area if a buffer is not provided by the caller.
174 char* name_and_sig_as_C_string() const;
175 char* name_and_sig_as_C_string(char* buf, int size) const;
176
177 // Static routine in the situations we don't have a Method*
178 static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature);
179 static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size);
180
181 // Get return type + klass name + "." + method name + ( parameters types )
182 // as a C string or print it to an outputStream.
183 // This is to be used to assemble strings passed to Java, so that
184 // the text more resembles Java code. Used in exception messages.
185 // Memory is allocated in the resource area; the caller needs
186 // a ResourceMark.
187 const char* external_name() const;
188 void print_external_name(outputStream *os) const;
189
190 static const char* external_name( Klass* klass, Symbol* method_name, Symbol* signature);
191 static void print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature);
192
193 Bytecodes::Code java_code_at(int bci) const {
194 return Bytecodes::java_code_at(this, bcp_from(bci));
195 }
196 Bytecodes::Code code_at(int bci) const {
197 return Bytecodes::code_at(this, bcp_from(bci));
198 }
199
200 // JVMTI breakpoints
201 #if !INCLUDE_JVMTI
202 Bytecodes::Code orig_bytecode_at(int bci) const {
203 ShouldNotReachHere();
204 return Bytecodes::_shouldnotreachhere;
205 }
206 void set_orig_bytecode_at(int bci, Bytecodes::Code code) {
207 ShouldNotReachHere();
208 };
209 u2 number_of_breakpoints() const {return 0;}
210 #else // !INCLUDE_JVMTI
211 Bytecodes::Code orig_bytecode_at(int bci) const;
212 void set_orig_bytecode_at(int bci, Bytecodes::Code code);
213 void set_breakpoint(int bci);
214 void clear_breakpoint(int bci);
215 void clear_all_breakpoints();
216 // Tracking number of breakpoints, for fullspeed debugging.
217 // Only mutated by VM thread.
218 inline u2 number_of_breakpoints() const;
219 inline void incr_number_of_breakpoints(Thread* current);
220 inline void decr_number_of_breakpoints(Thread* current);
221 // Initialization only
222 inline void clear_number_of_breakpoints();
223 #endif // !INCLUDE_JVMTI
224
225 // index into InstanceKlass methods() array
226 // note: also used by jfr
227 u2 method_idnum() const { return constMethod()->method_idnum(); }
228 void set_method_idnum(u2 idnum) { constMethod()->set_method_idnum(idnum); }
229
230 u2 orig_method_idnum() const { return constMethod()->orig_method_idnum(); }
231 void set_orig_method_idnum(u2 idnum) { constMethod()->set_orig_method_idnum(idnum); }
232
233 // code size
234 u2 code_size() const { return constMethod()->code_size(); }
235
236 // method size in words
237 int method_size() const { return sizeof(Method)/wordSize + ( is_native() ? 2 : 0 ); }
238
239 // constant pool for Klass* holding this method
240 ConstantPool* constants() const { return constMethod()->constants(); }
241 void set_constants(ConstantPool* c) { constMethod()->set_constants(c); }
242
243 // max stack
244 // return original max stack size for method verification
245 u2 verifier_max_stack() const { return constMethod()->max_stack(); }
246 int max_stack() const { return constMethod()->max_stack() + extra_stack_entries(); }
247 void set_max_stack(int size) { constMethod()->set_max_stack(size); }
248
249 // max locals
250 u2 max_locals() const { return constMethod()->max_locals(); }
251 void set_max_locals(int size) { constMethod()->set_max_locals(size); }
252
253 void set_deprecated() { constMethod()->set_deprecated(); }
254 bool deprecated() const { return constMethod()->deprecated(); }
255
256 void set_deprecated_for_removal() { constMethod()->set_deprecated_for_removal(); }
257 bool deprecated_for_removal() const { return constMethod()->deprecated_for_removal(); }
258
259 inline int highest_comp_level() const;
260 void set_highest_comp_level(int level);
261 int highest_osr_comp_level() const;
262 void set_highest_osr_comp_level(int level);
263
264 #if COMPILER2_OR_JVMCI
265 // Count of times method was exited via exception while interpreting
266 inline void interpreter_throwout_increment(Thread* current);
267 #endif
268
269 inline int interpreter_throwout_count() const;
270
271 u2 size_of_parameters() const { return constMethod()->size_of_parameters(); }
272
273 bool has_stackmap_table() const {
274 return constMethod()->has_stackmap_table();
275 }
276
277 Array<u1>* stackmap_data() const {
278 return constMethod()->stackmap_data();
279 }
280
281 void set_stackmap_data(Array<u1>* sd) {
282 constMethod()->set_stackmap_data(sd);
283 }
284
285 // exception handler table
286 bool has_exception_handler() const
287 { return constMethod()->has_exception_table(); }
288 u2 exception_table_length() const
289 { return constMethod()->exception_table_length(); }
290 ExceptionTableElement* exception_table_start() const
291 { return constMethod()->exception_table_start(); }
292
293 // Finds the first entry point bci of an exception handler for an
294 // exception of klass ex_klass thrown at throw_bci. A value of null
295 // for ex_klass indicates that the exception klass is not known; in
296 // this case it matches any constraint class. Returns -1 if the
297 // exception cannot be handled in this method. The handler
298 // constraint classes are loaded if necessary. Note that this may
299 // throw an exception if loading of the constraint classes causes
300 // an IllegalAccessError (bugid 4307310) or an OutOfMemoryError.
301 // If an exception is thrown, returns the bci of the
302 // exception handler which caused the exception to be thrown, which
303 // is needed for proper retries. See, for example,
304 // InterpreterRuntime::exception_handler_for_exception.
305 static int fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS);
306
307 static bool register_native(Klass* k,
308 Symbol* name,
309 Symbol* signature,
310 address entry,
311 TRAPS);
312
313 // method data access
314 MethodData* method_data() const {
315 return _method_data;
316 }
317 void set_method_data(MethodData* data);
318
319 MethodTrainingData* training_data_or_null() const;
320 bool init_training_data(MethodTrainingData* td);
321
322 // mark an exception handler as entered (used to prune dead catch blocks in C2)
323 void set_exception_handler_entered(int handler_bci);
324
325 MethodCounters* method_counters() const {
326 return _method_counters;
327 }
328
329 void clear_method_counters() {
330 _method_counters = nullptr;
331 }
332
333 bool init_method_counters(MethodCounters* counters);
334
335 inline int prev_event_count() const;
336 inline void set_prev_event_count(int count);
337 inline jlong prev_time() const;
338 inline void set_prev_time(jlong time);
339 inline float rate() const;
340 inline void set_rate(float rate);
341
342 inline int invocation_count() const;
343 inline int backedge_count() const;
344
345 bool was_executed_more_than(int n);
346 bool was_never_executed() { return !was_executed_more_than(0); }
347
348 static void build_profiling_method_data(const methodHandle& method, TRAPS);
349 static bool install_training_method_data(const methodHandle& method);
350 static MethodCounters* build_method_counters(Thread* current, Method* m);
351
352 inline int interpreter_invocation_count() const;
353
354 #ifndef PRODUCT
355 int64_t compiled_invocation_count() const { return _compiled_invocation_count;}
356 void set_compiled_invocation_count(int count) { _compiled_invocation_count = (int64_t)count; }
357 #else
358 // for PrintMethodData in a product build
359 int64_t compiled_invocation_count() const { return 0; }
360 #endif // not PRODUCT
361
362 // nmethod/verified compiler entry
363 address verified_code_entry();
364 bool check_code() const; // Not inline to avoid circular ref
365 nmethod* code() const;
366
367 // Locks NMethodState_lock if not held.
368 void unlink_code(nmethod *compare);
369 // Locks NMethodState_lock if not held.
370 void unlink_code();
371
372 private:
373 // Either called with NMethodState_lock held or from constructor.
374 void clear_code();
375
376 void clear_method_data() {
377 _method_data = nullptr;
378 }
379
380 public:
381 static void set_code(const methodHandle& mh, nmethod* code);
382 void set_adapter_entry(AdapterHandlerEntry* adapter) {
383 _adapter = adapter;
384 }
385 void set_from_compiled_entry(address entry) {
386 _from_compiled_entry = entry;
387 }
388
389 address get_i2c_entry();
390 address get_c2i_entry();
391 address get_c2i_unverified_entry();
392 address get_c2i_no_clinit_check_entry();
393 AdapterHandlerEntry* adapter() const {
394 return _adapter;
395 }
396 // setup entry points
397 void link_method(const methodHandle& method, TRAPS);
398 // clear entry points. Used by sharing code during dump time
399 void unlink_method() NOT_CDS_RETURN;
400 void remove_unshareable_flags() NOT_CDS_RETURN;
401
402 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
403 virtual MetaspaceObj::Type type() const { return MethodType; }
404
405 // vtable index
406 enum VtableIndexFlag {
407 // Valid vtable indexes are non-negative (>= 0).
408 // These few negative values are used as sentinels.
409 itable_index_max = -10, // first itable index, growing downward
410 pending_itable_index = -9, // itable index will be assigned
411 invalid_vtable_index = -4, // distinct from any valid vtable index
412 garbage_vtable_index = -3, // not yet linked; no vtable layout yet
413 nonvirtual_vtable_index = -2 // there is no need for vtable dispatch
414 // 6330203 Note: Do not use -1, which was overloaded with many meanings.
415 };
416 DEBUG_ONLY(bool valid_vtable_index() const { return _vtable_index >= nonvirtual_vtable_index; })
417 bool has_vtable_index() const { return _vtable_index >= 0; }
418 int vtable_index() const { return _vtable_index; }
419 void set_vtable_index(int index);
420 DEBUG_ONLY(bool valid_itable_index() const { return _vtable_index <= pending_itable_index; })
421 bool has_itable_index() const { return _vtable_index <= itable_index_max; }
422 int itable_index() const { assert(valid_itable_index(), "");
423 return itable_index_max - _vtable_index; }
424 void set_itable_index(int index);
425
426 // interpreter entry
427 address interpreter_entry() const { return _i2i_entry; }
428 // Only used when first initialize so we can set _i2i_entry and _from_interpreted_entry
429 void set_interpreter_entry(address entry) {
430 if (_i2i_entry != entry) {
431 _i2i_entry = entry;
432 }
433 if (_from_interpreted_entry != entry) {
434 _from_interpreted_entry = entry;
435 }
436 }
437
438 // native function (used for native methods only)
439 enum {
440 native_bind_event_is_interesting = true
441 };
442 address native_function() const { return *(native_function_addr()); }
443
444 // Must specify a real function (not null).
445 // Use clear_native_function() to unregister.
446 void set_native_function(address function, bool post_event_flag);
447 bool has_native_function() const;
448 void clear_native_function();
449
450 // signature handler (used for native methods only)
451 address signature_handler() const { return *(signature_handler_addr()); }
452 void set_signature_handler(address handler);
453
454 // Interpreter oopmap support.
455 // If handle is already available, call with it for better performance.
456 void mask_for(int bci, InterpreterOopMap* mask);
457 void mask_for(const methodHandle& this_mh, int bci, InterpreterOopMap* mask);
458
459 // operations on invocation counter
460 void print_invocation_count(outputStream* st);
461
462 // byte codes
463 void set_code(address code) { return constMethod()->set_code(code); }
464 address code_base() const { return constMethod()->code_base(); }
465 bool contains(address bcp) const { return constMethod()->contains(bcp); }
466
467 // prints byte codes
468 void print_codes(int flags = 0) const { print_codes_on(tty, flags); }
469 void print_codes_on(outputStream* st, int flags = 0) const;
470 void print_codes_on(int from, int to, outputStream* st, int flags = 0) const;
471
472 // method parameters
473 bool has_method_parameters() const
474 { return constMethod()->has_method_parameters(); }
475 int method_parameters_length() const
476 { return constMethod()->method_parameters_length(); }
477 MethodParametersElement* method_parameters_start() const
478 { return constMethod()->method_parameters_start(); }
479
480 // checked exceptions
481 u2 checked_exceptions_length() const
482 { return constMethod()->checked_exceptions_length(); }
483 CheckedExceptionElement* checked_exceptions_start() const
484 { return constMethod()->checked_exceptions_start(); }
485
486 // localvariable table
487 bool has_localvariable_table() const
488 { return constMethod()->has_localvariable_table(); }
489 u2 localvariable_table_length() const
490 { return constMethod()->localvariable_table_length(); }
491 LocalVariableTableElement* localvariable_table_start() const
492 { return constMethod()->localvariable_table_start(); }
493
494 bool has_linenumber_table() const
495 { return constMethod()->has_linenumber_table(); }
496 u_char* compressed_linenumber_table() const
497 { return constMethod()->compressed_linenumber_table(); }
498
499 // method holder (the Klass* holding this method)
500 InstanceKlass* method_holder() const { return constants()->pool_holder(); }
501
502 Symbol* klass_name() const; // returns the name of the method holder
503 BasicType result_type() const { return constMethod()->result_type(); }
504 bool is_returning_oop() const { BasicType r = result_type(); return is_reference_type(r); }
505 bool is_returning_fp() const { BasicType r = result_type(); return (r == T_FLOAT || r == T_DOUBLE); }
506
507 // Checked exceptions thrown by this method (resolved to mirrors)
508 objArrayHandle resolved_checked_exceptions(TRAPS) { return resolved_checked_exceptions_impl(this, THREAD); }
509
510 // Access flags
511 bool is_public() const { return access_flags().is_public(); }
512 bool is_private() const { return access_flags().is_private(); }
513 bool is_protected() const { return access_flags().is_protected(); }
514 bool is_package_private() const { return !is_public() && !is_private() && !is_protected(); }
515 bool is_static() const { return access_flags().is_static(); }
516 bool is_final() const { return access_flags().is_final(); }
517 bool is_synchronized() const { return access_flags().is_synchronized();}
518 bool is_native() const { return access_flags().is_native(); }
519 bool is_abstract() const { return access_flags().is_abstract(); }
520 bool is_synthetic() const { return access_flags().is_synthetic(); }
521
522 // returns true if contains only return operation
523 bool is_empty_method() const;
524
525 // returns true if this is a vanilla constructor
526 bool is_vanilla_constructor() const;
527
528 // checks method and its method holder
529 bool is_final_method() const;
530 bool is_final_method(AccessFlags class_access_flags) const;
531 // interface method declared with 'default' - excludes private interface methods
532 bool is_default_method() const;
533
534 // true if method needs no dynamic dispatch (final and/or no vtable entry)
535 bool can_be_statically_bound() const;
536 bool can_be_statically_bound(InstanceKlass* context) const;
537 bool can_be_statically_bound(AccessFlags class_access_flags) const;
538
539 // true if method can omit stack trace in throw in compiled code.
540 bool can_omit_stack_trace();
541
542 // Flags getting and setting.
543 #define M_STATUS_GET_SET(name, ignore) \
544 bool name() const { return _flags.name(); } \
545 void set_##name(bool x) { _flags.set_##name(x); } \
546 void set_##name() { _flags.set_##name(true); }
547 M_STATUS_DO(M_STATUS_GET_SET)
548 #undef M_STATUS_GET_SET
549
550 // returns true if the method has any backward branches.
551 bool has_loops() {
552 return has_loops_flag_init() ? has_loops_flag() : compute_has_loops_flag();
553 };
554
555 bool compute_has_loops_flag();
556 bool set_has_loops() {
557 // set both the flags and that it's been initialized.
558 set_has_loops_flag();
559 set_has_loops_flag_init();
560 return true;
561 }
562
563 // returns true if the method has any monitors.
564 bool has_monitors() const { return is_synchronized() || has_monitor_bytecodes(); }
565
566 // monitor matching. This returns a conservative estimate of whether the monitorenter/monitorexit bytecodes
567 // properly nest in the method. It might return false, even though they actually nest properly, since the info.
568 // has not been computed yet.
569 bool guaranteed_monitor_matching() const { return monitor_matching(); }
570 void set_guaranteed_monitor_matching() { set_monitor_matching(); }
571
572 // returns true if the method is an accessor function (setter/getter).
573 bool is_accessor() const;
574
575 // returns true if the method is a getter
576 bool is_getter() const;
577
578 // returns true if the method is a setter
579 bool is_setter() const;
580
581 // returns true if the method does nothing but return a constant of primitive type
582 bool is_constant_getter() const;
583
584 // returns true if the method is static OR if the classfile version < 51
585 bool has_valid_initializer_flags() const;
586
587 // returns true if the method name is <clinit> and the method has
588 // valid static initializer flags.
589 bool is_static_initializer() const;
590
591 // returns true if the method name is <init>
592 bool is_object_initializer() const;
593
594 // returns true if the method name is wait0
595 bool is_object_wait0() const;
596
597 // compiled code support
598 // NOTE: code() is inherently racy as deopt can be clearing code
599 // simultaneously. Use with caution.
600 bool has_compiled_code() const;
601
602 bool needs_clinit_barrier() const;
603
604 // sizing
605 static int header_size() {
606 return align_up((int)sizeof(Method), wordSize) / wordSize;
607 }
608 static int size(bool is_native);
609 int size() const { return method_size(); }
610 void log_touched(Thread* current);
611 static void print_touched_methods(outputStream* out);
612
613 // interpreter support
614 static ByteSize const_offset() { return byte_offset_of(Method, _constMethod ); }
615 static ByteSize access_flags_offset() { return byte_offset_of(Method, _access_flags ); }
616 static ByteSize from_compiled_offset() { return byte_offset_of(Method, _from_compiled_entry); }
617 static ByteSize code_offset() { return byte_offset_of(Method, _code); }
618
619 static ByteSize method_counters_offset() {
620 return byte_offset_of(Method, _method_counters);
621 }
622 #ifndef PRODUCT
623 static ByteSize compiled_invocation_counter_offset() { return byte_offset_of(Method, _compiled_invocation_count); }
624 #endif // not PRODUCT
625 static ByteSize native_function_offset() { return in_ByteSize(sizeof(Method)); }
626 static ByteSize from_interpreted_offset() { return byte_offset_of(Method, _from_interpreted_entry ); }
627 static ByteSize interpreter_entry_offset() { return byte_offset_of(Method, _i2i_entry ); }
628 static ByteSize signature_handler_offset() { return in_ByteSize(sizeof(Method) + wordSize); }
629 static ByteSize itable_index_offset() { return byte_offset_of(Method, _vtable_index ); }
630
631 // for code generation
632 static ByteSize method_data_offset() { return byte_offset_of(Method, _method_data); }
633 static ByteSize intrinsic_id_offset() { return byte_offset_of(Method, _intrinsic_id); }
634 static int intrinsic_id_size_in_bytes() { return sizeof(u2); }
635
636 // Static methods that are used to implement member methods where an exposed this pointer
637 // is needed due to possible GCs
638 static objArrayHandle resolved_checked_exceptions_impl(Method* method, TRAPS);
639
640 // Returns the byte code index from the byte code pointer
641 int bci_from(address bcp) const;
642 address bcp_from(int bci) const;
643 address bcp_from(address bcp) const;
644 int validate_bci_from_bcp(address bcp) const;
645 int validate_bci(int bci) const;
646
647 // Returns the line number for a bci if debugging information for the method is prowided,
648 // -1 is returned otherwise.
649 int line_number_from_bci(int bci) const;
650
651 // Reflection support
652 bool is_overridden_in(Klass* k) const;
653
654 // Stack walking support
655 bool is_ignored_by_security_stack_walk() const;
656
657 // JSR 292 support
658 bool is_method_handle_intrinsic() const; // MethodHandles::is_signature_polymorphic_intrinsic(intrinsic_id)
659 bool is_compiled_lambda_form() const; // intrinsic_id() == vmIntrinsics::_compiledLambdaForm
660 bool has_member_arg() const; // intrinsic_id() == vmIntrinsics::_linkToSpecial, etc.
661 static methodHandle make_method_handle_intrinsic(vmIntrinsicID iid, // _invokeBasic, _linkToVirtual
662 Symbol* signature, //anything at all
663 TRAPS);
664 // Some special methods don't need to be findable by nmethod iterators and are permanent.
665 bool can_be_allocated_in_NonNMethod_space() const { return is_method_handle_intrinsic(); }
666
667 // Continuation
668 inline bool is_continuation_enter_intrinsic() const;
669 inline bool is_continuation_yield_intrinsic() const;
670 inline bool is_continuation_native_intrinsic() const;
671 inline bool is_special_native_intrinsic() const;
672
673 static Klass* check_non_bcp_klass(Klass* klass);
674
675 enum {
676 // How many extra stack entries for invokedynamic
677 extra_stack_entries_for_jsr292 = 1
678 };
679
680 // this operates only on invoke methods:
681 // presize interpreter frames for extra interpreter stack entries, if needed
682 // Account for the extra appendix argument for invokehandle/invokedynamic
683 static int extra_stack_entries() { return extra_stack_entries_for_jsr292; }
684 static int extra_stack_words(); // = extra_stack_entries() * Interpreter::stackElementSize
685
686 // RedefineClasses() support:
687 bool on_stack() const { return on_stack_flag(); }
688 void set_on_stack(const bool value);
689
690 void record_gc_epoch();
691
692 // see the definition in Method*.cpp for the gory details
693 bool should_not_be_cached() const;
694
695 // Rewriting support
696 static methodHandle clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
697 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS);
698
699 // jmethodID handling
700 // Because the useful life-span of a jmethodID cannot be determined,
701 // once created they are never reclaimed. The methods to which they refer,
702 // however, can be GC'ed away if the class is unloaded or if the method is
703 // made obsolete or deleted -- in these cases, the jmethodID
704 // refers to null (as is the case for any weak reference).
705 static jmethodID make_jmethod_id(ClassLoaderData* cld, Method* mh);
706
707 // Use resolve_jmethod_id() in situations where the caller is expected
708 // to provide a valid jmethodID; the only sanity checks are in asserts;
709 // result guaranteed not to be null.
710 static Method* resolve_jmethod_id(jmethodID mid);
711
712 // Use checked_resolve_jmethod_id() in situations where the caller
713 // should provide a valid jmethodID, but might not. Null is returned
714 // when the jmethodID does not refer to a valid method.
715 static Method* checked_resolve_jmethod_id(jmethodID mid);
716
717 static void change_method_associated_with_jmethod_id(jmethodID old_jmid_ptr, Method* new_method);
718 static bool validate_jmethod_id(jmethodID mid);
719
720 // Clear jmethodID
721 void clear_jmethod_id();
722 static void print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) PRODUCT_RETURN;
723
724 // Get this method's jmethodID -- allocate if it doesn't exist
725 jmethodID jmethod_id();
726
727 // Lookup the jmethodID for this method. Return null if not found.
728 // NOTE that this function can be called from a signal handler
729 // (see AsyncGetCallTrace support for Forte Analyzer) and this
730 // needs to be async-safe. No allocation should be done and
731 // so handles are not used to avoid deadlock.
732 jmethodID find_jmethod_id_or_null() { return method_holder()->jmethod_id_or_null(this); }
733
734 // Support for inlining of intrinsic methods
735 vmIntrinsicID intrinsic_id() const { return (vmIntrinsicID) _intrinsic_id; }
736 void set_intrinsic_id(vmIntrinsicID id) { _intrinsic_id = (u2) id; }
737
738 // Helper routines for intrinsic_id() and vmIntrinsics::method().
739 void init_intrinsic_id(vmSymbolID klass_id); // updates from _none if a match
740 static vmSymbolID klass_id_for_intrinsics(const Klass* holder);
741
742 bool caller_sensitive() const { return constMethod()->caller_sensitive(); }
743 void set_caller_sensitive() { constMethod()->set_caller_sensitive(); }
744
745 bool changes_current_thread() const { return constMethod()->changes_current_thread(); }
746 void set_changes_current_thread() { constMethod()->set_changes_current_thread(); }
747
748 bool jvmti_hide_events() const { return constMethod()->jvmti_hide_events(); }
749 void set_jvmti_hide_events() { constMethod()->set_jvmti_hide_events(); }
750
751 bool jvmti_mount_transition() const { return constMethod()->jvmti_mount_transition(); }
752 void set_jvmti_mount_transition() { constMethod()->set_jvmti_mount_transition(); }
753
754 bool is_hidden() const { return constMethod()->is_hidden(); }
755 void set_is_hidden() { constMethod()->set_is_hidden(); }
756
757 bool is_scoped() const { return constMethod()->is_scoped(); }
758 void set_scoped() { constMethod()->set_is_scoped(); }
759
760 bool intrinsic_candidate() const { return constMethod()->intrinsic_candidate(); }
761 void set_intrinsic_candidate() { constMethod()->set_intrinsic_candidate(); }
762
763 bool has_injected_profile() const { return constMethod()->has_injected_profile(); }
764 void set_has_injected_profile() { constMethod()->set_has_injected_profile(); }
765
766 bool has_reserved_stack_access() const { return constMethod()->reserved_stack_access(); }
767 void set_has_reserved_stack_access() { constMethod()->set_reserved_stack_access(); }
768
769 JFR_ONLY(DEFINE_TRACE_FLAG_ACCESSOR;)
770
771 ConstMethod::MethodType method_type() const {
772 return _constMethod->method_type();
773 }
774 bool is_overpass() const { return method_type() == ConstMethod::OVERPASS; }
775
776 // On-stack replacement support
777 bool has_osr_nmethod(int level, bool match_level) {
778 return method_holder()->lookup_osr_nmethod(this, InvocationEntryBci, level, match_level) != nullptr;
779 }
780
781 nmethod* lookup_osr_nmethod_for(int bci, int level, bool match_level) {
782 return method_holder()->lookup_osr_nmethod(this, bci, level, match_level);
783 }
784
785 // Find if klass for method is loaded
786 bool is_klass_loaded_by_klass_index(int klass_index) const;
787 bool is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved = false) const;
788
789 // Indicates whether compilation failed earlier for this method, or
790 // whether it is not compilable for another reason like having a
791 // breakpoint set in it.
792 bool is_not_compilable(int comp_level = CompLevel_any) const;
793 void set_not_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true);
794 void set_not_compilable_quietly(const char* reason, int comp_level = CompLevel_all) {
795 set_not_compilable(reason, comp_level, false);
796 }
797 bool is_not_osr_compilable(int comp_level = CompLevel_any) const;
798 void set_not_osr_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true);
799 void set_not_osr_compilable_quietly(const char* reason, int comp_level = CompLevel_all) {
800 set_not_osr_compilable(reason, comp_level, false);
801 }
802 bool is_always_compilable() const;
803
804 private:
805 void print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason);
806
807 public:
808 MethodCounters* get_method_counters(Thread* current) {
809 if (_method_counters == nullptr) {
810 build_method_counters(current, this);
811 }
812 return _method_counters;
813 }
814
815 void clear_is_not_c1_compilable() { set_is_not_c1_compilable(false); }
816 void clear_is_not_c2_compilable() { set_is_not_c2_compilable(false); }
817 void clear_is_not_c2_osr_compilable() { set_is_not_c2_osr_compilable(false); }
818
819 // not_c1_osr_compilable == not_c1_compilable
820 bool is_not_c1_osr_compilable() const { return is_not_c1_compilable(); }
821 void set_is_not_c1_osr_compilable() { set_is_not_c1_compilable(); }
822 void clear_is_not_c1_osr_compilable() { clear_is_not_c1_compilable(); }
823
824 // Background compilation support
825 void clear_queued_for_compilation() { set_queued_for_compilation(false); }
826
827 // Resolve all classes in signature, return 'true' if successful
828 static bool load_signature_classes(const methodHandle& m, TRAPS);
829
830 // Printing
831 void print_short_name(outputStream* st = tty) const; // prints as klassname::methodname; Exposed so field engineers can debug VM
832 #if INCLUDE_JVMTI
833 void print_name(outputStream* st = tty) const; // prints as "virtual void foo(int)"; exposed for -Xlog:redefine+class
834 #else
835 void print_name(outputStream* st = tty) const PRODUCT_RETURN; // prints as "virtual void foo(int)"
836 #endif
837
838 typedef int (*method_comparator_func)(Method* a, Method* b);
839
840 // Helper routine used for method sorting
841 static void sort_methods(Array<Method*>* methods, bool set_idnums = true, method_comparator_func func = nullptr);
842
843 // Deallocation function for redefine classes or if an error occurs
844 void deallocate_contents(ClassLoaderData* loader_data);
845
846 void release_C_heap_structures();
847
848 Method* get_new_method() const {
849 InstanceKlass* holder = method_holder();
850 Method* new_method = holder->method_with_idnum(orig_method_idnum());
851
852 assert(new_method != nullptr, "method_with_idnum() should not be null");
853 assert(this != new_method, "sanity check");
854 return new_method;
855 }
856
857 // Printing
858 #ifndef PRODUCT
859 void print_on(outputStream* st) const;
860 #endif
861 void print_value_on(outputStream* st) const;
862 void print_linkage_flags(outputStream* st) PRODUCT_RETURN;
863
864 const char* internal_name() const { return "{method}"; }
865
866 // Check for valid method pointer
867 static bool has_method_vptr(const void* ptr);
868 static bool is_valid_method(const Method* m);
869
870 // Verify
871 void verify() { verify_on(tty); }
872 void verify_on(outputStream* st);
873
874 private:
875
876 // Inlined elements
877 address* native_function_addr() const { assert(is_native(), "must be native"); return (address*) (this+1); }
878 address* signature_handler_addr() const { return native_function_addr() + 1; }
879 };
880
881
882 // Utility class for compressing line number tables
883
884 class CompressedLineNumberWriteStream: public CompressedWriteStream {
885 private:
886 int _bci;
887 int _line;
888 public:
889 // Constructor
890 CompressedLineNumberWriteStream(int initial_size) : CompressedWriteStream(initial_size), _bci(0), _line(0) {}
891 CompressedLineNumberWriteStream(u_char* buffer, int initial_size) : CompressedWriteStream(buffer, initial_size), _bci(0), _line(0) {}
892
893 // Write (bci, line number) pair to stream
894 void write_pair_regular(int bci_delta, int line_delta);
895
896 // If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned)
897 // we save it as one byte, otherwise we write a 0xFF escape character
898 // and use regular compression. 0x0 is used as end-of-stream terminator.
899 void write_pair_inline(int bci, int line);
900
901 void write_pair(int bci, int line);
902
903 // Write end-of-stream marker
904 void write_terminator() { write_byte(0); }
905 };
906
907
908 // Utility class for decompressing line number tables
909
910 class CompressedLineNumberReadStream: public CompressedReadStream {
911 private:
912 int _bci;
913 int _line;
914 public:
915 // Constructor
916 CompressedLineNumberReadStream(u_char* buffer);
917 // Read (bci, line number) pair from stream. Returns false at end-of-stream.
918 bool read_pair();
919 // Accessing bci and line number (after calling read_pair)
920 int bci() const { return _bci; }
921 int line() const { return _line; }
922 };
923
924
925 #if INCLUDE_JVMTI
926
927 /// Fast Breakpoints.
928
929 // If this structure gets more complicated (because bpts get numerous),
930 // move it into its own header.
931
932 // There is presently no provision for concurrent access
933 // to breakpoint lists, which is only OK for JVMTI because
934 // breakpoints are written only at safepoints, and are read
935 // concurrently only outside of safepoints.
936
937 class BreakpointInfo : public CHeapObj<mtClass> {
938 friend class VMStructs;
939 private:
940 Bytecodes::Code _orig_bytecode;
941 int _bci;
942 u2 _name_index; // of method
943 u2 _signature_index; // of method
944 BreakpointInfo* _next; // simple storage allocation
945
946 public:
947 BreakpointInfo(Method* m, int bci);
948
949 // accessors
950 Bytecodes::Code orig_bytecode() { return _orig_bytecode; }
951 void set_orig_bytecode(Bytecodes::Code code) { _orig_bytecode = code; }
952 int bci() { return _bci; }
953
954 BreakpointInfo* next() const { return _next; }
955 void set_next(BreakpointInfo* n) { _next = n; }
956
957 // helps for searchers
958 bool match(const Method* m, int bci) {
959 return bci == _bci && match(m);
960 }
961
962 bool match(const Method* m) {
963 return _name_index == m->name_index() &&
964 _signature_index == m->signature_index();
965 }
966
967 void set(Method* method);
968 void clear(Method* method);
969 };
970
971 #endif // INCLUDE_JVMTI
972
973 // Utility class for access exception handlers
974 class ExceptionTable : public StackObj {
975 private:
976 ExceptionTableElement* _table;
977 u2 _length;
978
979 public:
980 ExceptionTable(const Method* m) {
981 if (m->has_exception_handler()) {
982 _table = m->exception_table_start();
983 _length = m->exception_table_length();
984 } else {
985 _table = nullptr;
986 _length = 0;
987 }
988 }
989
990 u2 length() const {
991 return _length;
992 }
993
994 u2 start_pc(int idx) const {
995 assert(idx < _length, "out of bounds");
996 return _table[idx].start_pc;
997 }
998
999 void set_start_pc(int idx, u2 value) {
1000 assert(idx < _length, "out of bounds");
1001 _table[idx].start_pc = value;
1002 }
1003
1004 u2 end_pc(int idx) const {
1005 assert(idx < _length, "out of bounds");
1006 return _table[idx].end_pc;
1007 }
1008
1009 void set_end_pc(int idx, u2 value) {
1010 assert(idx < _length, "out of bounds");
1011 _table[idx].end_pc = value;
1012 }
1013
1014 u2 handler_pc(int idx) const {
1015 assert(idx < _length, "out of bounds");
1016 return _table[idx].handler_pc;
1017 }
1018
1019 void set_handler_pc(int idx, u2 value) {
1020 assert(idx < _length, "out of bounds");
1021 _table[idx].handler_pc = value;
1022 }
1023
1024 u2 catch_type_index(int idx) const {
1025 assert(idx < _length, "out of bounds");
1026 return _table[idx].catch_type_index;
1027 }
1028
1029 void set_catch_type_index(int idx, u2 value) {
1030 assert(idx < _length, "out of bounds");
1031 _table[idx].catch_type_index = value;
1032 }
1033 };
1034
1035 #endif // SHARE_OOPS_METHOD_HPP