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