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 #include "cds/cdsConfig.hpp" 26 #include "cds/cppVtables.hpp" 27 #include "cds/metaspaceShared.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderDataGraph.hpp" 30 #include "classfile/metadataOnStackMark.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/vmClasses.hpp" 34 #include "code/codeCache.hpp" 35 #include "code/debugInfoRec.hpp" 36 #include "compiler/compilationPolicy.hpp" 37 #include "gc/shared/collectedHeap.inline.hpp" 38 #include "interpreter/bytecodeStream.hpp" 39 #include "interpreter/bytecodeTracer.hpp" 40 #include "interpreter/bytecodes.hpp" 41 #include "interpreter/interpreter.hpp" 42 #include "interpreter/oopMapCache.hpp" 43 #include "logging/log.hpp" 44 #include "logging/logStream.hpp" 45 #include "logging/logTag.hpp" 46 #include "memory/allocation.inline.hpp" 47 #include "memory/metadataFactory.hpp" 48 #include "memory/metaspaceClosure.hpp" 49 #include "memory/oopFactory.hpp" 50 #include "memory/resourceArea.hpp" 51 #include "memory/universe.hpp" 52 #include "nmt/memTracker.hpp" 53 #include "oops/constMethod.hpp" 54 #include "oops/constantPool.hpp" 55 #include "oops/klass.inline.hpp" 56 #include "oops/method.inline.hpp" 57 #include "oops/methodData.hpp" 58 #include "oops/objArrayKlass.hpp" 59 #include "oops/objArrayOop.inline.hpp" 60 #include "oops/oop.inline.hpp" 61 #include "oops/symbol.hpp" 62 #include "oops/trainingData.hpp" 63 #include "prims/jvmtiExport.hpp" 64 #include "prims/methodHandles.hpp" 65 #include "runtime/atomic.hpp" 66 #include "runtime/arguments.hpp" 67 #include "runtime/continuationEntry.hpp" 68 #include "runtime/frame.inline.hpp" 69 #include "runtime/handles.inline.hpp" 70 #include "runtime/init.hpp" 71 #include "runtime/java.hpp" 72 #include "runtime/orderAccess.hpp" 73 #include "runtime/perfData.hpp" 74 #include "runtime/relocator.hpp" 75 #include "runtime/safepointVerifiers.hpp" 76 #include "runtime/sharedRuntime.hpp" 77 #include "runtime/signature.hpp" 78 #include "runtime/threads.hpp" 79 #include "runtime/vm_version.hpp" 80 #include "utilities/align.hpp" 81 #include "utilities/quickSort.hpp" 82 #include "utilities/vmError.hpp" 83 #include "utilities/xmlstream.hpp" 84 85 // Implementation of Method 86 87 Method* Method::allocate(ClassLoaderData* loader_data, 88 int byte_code_size, 89 AccessFlags access_flags, 90 InlineTableSizes* sizes, 91 ConstMethod::MethodType method_type, 92 Symbol* name, 93 TRAPS) { 94 assert(!access_flags.is_native() || byte_code_size == 0, 95 "native methods should not contain byte codes"); 96 ConstMethod* cm = ConstMethod::allocate(loader_data, 97 byte_code_size, 98 sizes, 99 method_type, 100 CHECK_NULL); 101 int size = Method::size(access_flags.is_native()); 102 return new (loader_data, size, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags, name); 103 } 104 105 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) { 106 NoSafepointVerifier no_safepoint; 107 set_constMethod(xconst); 108 set_access_flags(access_flags); 109 set_intrinsic_id(vmIntrinsics::_none); 110 clear_method_data(); 111 clear_method_counters(); 112 set_vtable_index(Method::garbage_vtable_index); 113 114 // Fix and bury in Method* 115 set_interpreter_entry(nullptr); // sets i2i entry and from_int 116 set_adapter_entry(nullptr); 117 Method::clear_code(); // from_c/from_i get set to c2i/i2i 118 119 if (access_flags.is_native()) { 120 clear_native_function(); 121 set_signature_handler(nullptr); 122 } 123 124 NOT_PRODUCT(set_compiled_invocation_count(0);) 125 // Name is very useful for debugging. 126 NOT_PRODUCT(_name = name;) 127 } 128 129 // Release Method*. The nmethod will be gone when we get here because 130 // we've walked the code cache. 131 void Method::deallocate_contents(ClassLoaderData* loader_data) { 132 MetadataFactory::free_metadata(loader_data, constMethod()); 133 set_constMethod(nullptr); 134 MetadataFactory::free_metadata(loader_data, method_data()); 135 clear_method_data(); 136 MetadataFactory::free_metadata(loader_data, method_counters()); 137 clear_method_counters(); 138 // The nmethod will be gone when we get here. 139 if (code() != nullptr) _code = nullptr; 140 } 141 142 void Method::release_C_heap_structures() { 143 if (method_data()) { 144 method_data()->release_C_heap_structures(); 145 146 // Destroy MethodData embedded lock 147 method_data()->~MethodData(); 148 } 149 } 150 151 address Method::get_i2c_entry() { 152 assert(adapter() != nullptr, "must have"); 153 return adapter()->get_i2c_entry(); 154 } 155 156 address Method::get_c2i_entry() { 157 assert(adapter() != nullptr, "must have"); 158 return adapter()->get_c2i_entry(); 159 } 160 161 address Method::get_c2i_unverified_entry() { 162 assert(adapter() != nullptr, "must have"); 163 return adapter()->get_c2i_unverified_entry(); 164 } 165 166 address Method::get_c2i_no_clinit_check_entry() { 167 assert(VM_Version::supports_fast_class_init_checks(), ""); 168 assert(adapter() != nullptr, "must have"); 169 return adapter()->get_c2i_no_clinit_check_entry(); 170 } 171 172 char* Method::name_and_sig_as_C_string() const { 173 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature()); 174 } 175 176 char* Method::name_and_sig_as_C_string(char* buf, int size) const { 177 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size); 178 } 179 180 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) { 181 const char* klass_name = klass->external_name(); 182 int klass_name_len = (int)strlen(klass_name); 183 int method_name_len = method_name->utf8_length(); 184 int len = klass_name_len + 2 + method_name_len + signature->utf8_length(); 185 char* dest = NEW_RESOURCE_ARRAY(char, len + 1); 186 strcpy(dest, klass_name); 187 dest[klass_name_len + 0] = ':'; 188 dest[klass_name_len + 1] = ':'; 189 strcpy(&dest[klass_name_len + 2], method_name->as_C_string()); 190 strcpy(&dest[klass_name_len + 2 + method_name_len], signature->as_C_string()); 191 dest[len] = 0; 192 return dest; 193 } 194 195 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) { 196 Symbol* klass_name = klass->name(); 197 klass_name->as_klass_external_name(buf, size); 198 int len = (int)strlen(buf); 199 200 if (len < size - 1) { 201 buf[len++] = '.'; 202 203 method_name->as_C_string(&(buf[len]), size - len); 204 len = (int)strlen(buf); 205 206 signature->as_C_string(&(buf[len]), size - len); 207 } 208 209 return buf; 210 } 211 212 const char* Method::external_name() const { 213 return external_name(constants()->pool_holder(), name(), signature()); 214 } 215 216 void Method::print_external_name(outputStream *os) const { 217 print_external_name(os, constants()->pool_holder(), name(), signature()); 218 } 219 220 const char* Method::external_name(Klass* klass, Symbol* method_name, Symbol* signature) { 221 stringStream ss; 222 print_external_name(&ss, klass, method_name, signature); 223 return ss.as_string(); 224 } 225 226 void Method::print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature) { 227 signature->print_as_signature_external_return_type(os); 228 os->print(" %s.%s(", klass->external_name(), method_name->as_C_string()); 229 signature->print_as_signature_external_parameters(os); 230 os->print(")"); 231 } 232 233 int Method::fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS) { 234 if (log_is_enabled(Debug, exceptions)) { 235 ResourceMark rm(THREAD); 236 log_debug(exceptions)("Looking for catch handler for exception of type \"%s\" in method \"%s\"", 237 ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string()); 238 } 239 // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index) 240 // access exception table 241 ExceptionTable table(mh()); 242 int length = table.length(); 243 // iterate through all entries sequentially 244 constantPoolHandle pool(THREAD, mh->constants()); 245 for (int i = 0; i < length; i ++) { 246 //reacquire the table in case a GC happened 247 ExceptionTable table(mh()); 248 int beg_bci = table.start_pc(i); 249 int end_bci = table.end_pc(i); 250 assert(beg_bci <= end_bci, "inconsistent exception table"); 251 log_debug(exceptions)(" - checking exception table entry for BCI %d to %d", 252 beg_bci, end_bci); 253 254 if (beg_bci <= throw_bci && throw_bci < end_bci) { 255 // exception handler bci range covers throw_bci => investigate further 256 log_debug(exceptions)(" - entry covers throw point BCI %d", throw_bci); 257 258 int handler_bci = table.handler_pc(i); 259 int klass_index = table.catch_type_index(i); 260 if (klass_index == 0) { 261 if (log_is_enabled(Info, exceptions)) { 262 ResourceMark rm(THREAD); 263 log_info(exceptions)("Found catch-all handler for exception of type \"%s\" in method \"%s\" at BCI: %d", 264 ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci); 265 } 266 return handler_bci; 267 } else if (ex_klass == nullptr) { 268 // Is this even possible? 269 if (log_is_enabled(Info, exceptions)) { 270 ResourceMark rm(THREAD); 271 log_info(exceptions)("null exception class is implicitly caught by handler in method \"%s\" at BCI: %d", 272 mh()->name()->as_C_string(), handler_bci); 273 } 274 return handler_bci; 275 } else { 276 if (log_is_enabled(Debug, exceptions)) { 277 ResourceMark rm(THREAD); 278 log_debug(exceptions)(" - resolving catch type \"%s\"", 279 pool->klass_name_at(klass_index)->as_C_string()); 280 } 281 // we know the exception class => get the constraint class 282 // this may require loading of the constraint class; if verification 283 // fails or some other exception occurs, return handler_bci 284 Klass* k = pool->klass_at(klass_index, THREAD); 285 if (HAS_PENDING_EXCEPTION) { 286 if (log_is_enabled(Debug, exceptions)) { 287 ResourceMark rm(THREAD); 288 log_debug(exceptions)(" - exception \"%s\" occurred resolving catch type", 289 PENDING_EXCEPTION->klass()->external_name()); 290 } 291 return handler_bci; 292 } 293 assert(k != nullptr, "klass not loaded"); 294 if (ex_klass->is_subtype_of(k)) { 295 if (log_is_enabled(Info, exceptions)) { 296 ResourceMark rm(THREAD); 297 log_info(exceptions)("Found matching handler for exception of type \"%s\" in method \"%s\" at BCI: %d", 298 ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci); 299 } 300 return handler_bci; 301 } 302 } 303 } 304 } 305 306 if (log_is_enabled(Debug, exceptions)) { 307 ResourceMark rm(THREAD); 308 log_debug(exceptions)("No catch handler found for exception of type \"%s\" in method \"%s\"", 309 ex_klass->external_name(), mh->name()->as_C_string()); 310 } 311 312 return -1; 313 } 314 315 void Method::mask_for(int bci, InterpreterOopMap* mask) { 316 methodHandle h_this(Thread::current(), this); 317 mask_for(h_this, bci, mask); 318 } 319 320 void Method::mask_for(const methodHandle& this_mh, int bci, InterpreterOopMap* mask) { 321 assert(this_mh() == this, "Sanity"); 322 method_holder()->mask_for(this_mh, bci, mask); 323 } 324 325 int Method::bci_from(address bcp) const { 326 if (is_native() && bcp == nullptr) { 327 return 0; 328 } 329 // Do not have a ResourceMark here because AsyncGetCallTrace stack walking code 330 // may call this after interrupting a nested ResourceMark. 331 assert((is_native() && bcp == code_base()) || contains(bcp) || VMError::is_error_reported(), 332 "bcp doesn't belong to this method. bcp: " PTR_FORMAT, p2i(bcp)); 333 334 return int(bcp - code_base()); 335 } 336 337 338 int Method::validate_bci(int bci) const { 339 // Called from the verifier, and should return -1 if not valid. 340 return ((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size())) ? bci : -1; 341 } 342 343 // Return bci if it appears to be a valid bcp 344 // Return -1 otherwise. 345 // Used by profiling code, when invalid data is a possibility. 346 // The caller is responsible for validating the Method* itself. 347 int Method::validate_bci_from_bcp(address bcp) const { 348 // keep bci as -1 if not a valid bci 349 int bci = -1; 350 if (bcp == nullptr || bcp == code_base()) { 351 // code_size() may return 0 and we allow 0 here 352 // the method may be native 353 bci = 0; 354 } else if (contains(bcp)) { 355 bci = int(bcp - code_base()); 356 } 357 // Assert that if we have dodged any asserts, bci is negative. 358 assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0"); 359 return bci; 360 } 361 362 address Method::bcp_from(int bci) const { 363 assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()), 364 "illegal bci: %d for %s method", bci, is_native() ? "native" : "non-native"); 365 address bcp = code_base() + bci; 366 assert((is_native() && bcp == code_base()) || contains(bcp), "bcp doesn't belong to this method"); 367 return bcp; 368 } 369 370 address Method::bcp_from(address bcp) const { 371 if (is_native() && bcp == nullptr) { 372 return code_base(); 373 } else { 374 return bcp; 375 } 376 } 377 378 int Method::size(bool is_native) { 379 // If native, then include pointers for native_function and signature_handler 380 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0; 381 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord; 382 return align_metadata_size(header_size() + extra_words); 383 } 384 385 Symbol* Method::klass_name() const { 386 return method_holder()->name(); 387 } 388 389 void Method::metaspace_pointers_do(MetaspaceClosure* it) { 390 LogStreamHandle(Trace, cds) lsh; 391 if (lsh.is_enabled()) { 392 lsh.print("Iter(Method): %p ", this); 393 print_external_name(&lsh); 394 lsh.cr(); 395 } 396 if (method_holder() != nullptr && !method_holder()->is_rewritten()) { 397 // holder is null for MH intrinsic methods 398 it->push(&_constMethod, MetaspaceClosure::_writable); 399 } else { 400 it->push(&_constMethod); 401 } 402 if (CDSConfig::is_dumping_adapters()) { 403 it->push(&_adapter); 404 } 405 it->push(&_method_data); 406 it->push(&_method_counters); 407 NOT_PRODUCT(it->push(&_name);) 408 } 409 410 #if INCLUDE_CDS 411 // Attempt to return method to original state. Clear any pointers 412 // (to objects outside the shared spaces). We won't be able to predict 413 // where they should point in a new JVM. Further initialize some 414 // entries now in order allow them to be write protected later. 415 416 void Method::remove_unshareable_info() { 417 unlink_method(); 418 if (CDSConfig::is_dumping_adapters()) { 419 if (_adapter != nullptr) { 420 _adapter->remove_unshareable_info(); 421 } 422 } else { 423 _adapter = nullptr; 424 } 425 if (method_data() != nullptr) { 426 method_data()->remove_unshareable_info(); 427 } 428 if (method_counters() != nullptr) { 429 method_counters()->remove_unshareable_info(); 430 } 431 JFR_ONLY(REMOVE_METHOD_ID(this);) 432 } 433 434 void Method::restore_adapter(TRAPS) { 435 if (_adapter != nullptr) { 436 _adapter->restore_unshareable_info(CHECK); 437 _from_compiled_entry = _adapter->get_c2i_entry(); 438 } 439 } 440 441 void Method::restore_unshareable_info(TRAPS) { 442 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored"); 443 restore_adapter(CHECK); 444 if (method_data() != nullptr) { 445 method_data()->restore_unshareable_info(CHECK); 446 } 447 if (method_counters() != nullptr) { 448 method_counters()->restore_unshareable_info(CHECK); 449 } 450 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set"); 451 assert(!pending_queue_processed(), "method's pending_queued_processed flag should not be set"); 452 } 453 #endif 454 455 void Method::set_vtable_index(int index) { 456 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) { 457 // At runtime initialize_vtable is rerun as part of link_class_impl() 458 // for a shared class loaded by the non-boot loader to obtain the loader 459 // constraints based on the runtime classloaders' context. 460 return; // don't write into the shared class 461 } else { 462 _vtable_index = index; 463 } 464 } 465 466 void Method::set_itable_index(int index) { 467 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) { 468 // At runtime initialize_itable is rerun as part of link_class_impl() 469 // for a shared class loaded by the non-boot loader to obtain the loader 470 // constraints based on the runtime classloaders' context. The dumptime 471 // itable index should be the same as the runtime index. 472 assert(_vtable_index == itable_index_max - index, 473 "archived itable index is different from runtime index"); 474 return; // don't write into the shared class 475 } else { 476 _vtable_index = itable_index_max - index; 477 } 478 assert(valid_itable_index(), ""); 479 } 480 481 // The RegisterNatives call being attempted tried to register with a method that 482 // is not native. Ask JVM TI what prefixes have been specified. Then check 483 // to see if the native method is now wrapped with the prefixes. See the 484 // SetNativeMethodPrefix(es) functions in the JVM TI Spec for details. 485 static Method* find_prefixed_native(Klass* k, Symbol* name, Symbol* signature, TRAPS) { 486 #if INCLUDE_JVMTI 487 ResourceMark rm(THREAD); 488 Method* method; 489 int name_len = name->utf8_length(); 490 char* name_str = name->as_utf8(); 491 int prefix_count; 492 char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count); 493 for (int i = 0; i < prefix_count; i++) { 494 char* prefix = prefixes[i]; 495 int prefix_len = (int)strlen(prefix); 496 497 // try adding this prefix to the method name and see if it matches another method name 498 int trial_len = name_len + prefix_len; 499 char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1); 500 strcpy(trial_name_str, prefix); 501 strcat(trial_name_str, name_str); 502 TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len); 503 if (trial_name == nullptr) { 504 continue; // no such symbol, so this prefix wasn't used, try the next prefix 505 } 506 method = k->lookup_method(trial_name, signature); 507 if (method == nullptr) { 508 continue; // signature doesn't match, try the next prefix 509 } 510 if (method->is_native()) { 511 method->set_is_prefixed_native(); 512 return method; // wahoo, we found a prefixed version of the method, return it 513 } 514 // found as non-native, so prefix is good, add it, probably just need more prefixes 515 name_len = trial_len; 516 name_str = trial_name_str; 517 } 518 #endif // INCLUDE_JVMTI 519 return nullptr; // not found 520 } 521 522 bool Method::register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) { 523 Method* method = k->lookup_method(name, signature); 524 if (method == nullptr) { 525 ResourceMark rm(THREAD); 526 stringStream st; 527 st.print("Method '"); 528 print_external_name(&st, k, name, signature); 529 st.print("' name or signature does not match"); 530 THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); 531 } 532 if (!method->is_native()) { 533 // trying to register to a non-native method, see if a JVM TI agent has added prefix(es) 534 method = find_prefixed_native(k, name, signature, THREAD); 535 if (method == nullptr) { 536 ResourceMark rm(THREAD); 537 stringStream st; 538 st.print("Method '"); 539 print_external_name(&st, k, name, signature); 540 st.print("' is not declared as native"); 541 THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); 542 } 543 } 544 545 if (entry != nullptr) { 546 method->set_native_function(entry, native_bind_event_is_interesting); 547 } else { 548 method->clear_native_function(); 549 } 550 if (log_is_enabled(Debug, jni, resolve)) { 551 ResourceMark rm(THREAD); 552 log_debug(jni, resolve)("[Registering JNI native method %s.%s]", 553 method->method_holder()->external_name(), 554 method->name()->as_C_string()); 555 } 556 return true; 557 } 558 559 bool Method::was_executed_more_than(int n) { 560 // Invocation counter is reset when the Method* is compiled. 561 // If the method has compiled code we therefore assume it has 562 // be executed more than n times. 563 if (is_accessor() || is_empty_method() || (code() != nullptr)) { 564 // interpreter doesn't bump invocation counter of trivial methods 565 // compiler does not bump invocation counter of compiled methods 566 return true; 567 } 568 else if ((method_counters() != nullptr && 569 method_counters()->invocation_counter()->carry()) || 570 (method_data() != nullptr && 571 method_data()->invocation_counter()->carry())) { 572 // The carry bit is set when the counter overflows and causes 573 // a compilation to occur. We don't know how many times 574 // the counter has been reset, so we simply assume it has 575 // been executed more than n times. 576 return true; 577 } else { 578 return invocation_count() > n; 579 } 580 } 581 582 void Method::print_invocation_count(outputStream* st) { 583 //---< compose+print method return type, klass, name, and signature >--- 584 if (is_static()) { st->print("static "); } 585 if (is_final()) { st->print("final "); } 586 if (is_synchronized()) { st->print("synchronized "); } 587 if (is_native()) { st->print("native "); } 588 st->print("%s::", method_holder()->external_name()); 589 name()->print_symbol_on(st); 590 signature()->print_symbol_on(st); 591 592 if (WizardMode) { 593 // dump the size of the byte codes 594 st->print(" {%d}", code_size()); 595 } 596 st->cr(); 597 598 // Counting based on signed int counters tends to overflow with 599 // longer-running workloads on fast machines. The counters under 600 // consideration here, however, are limited in range by counting 601 // logic. See InvocationCounter:count_limit for example. 602 // No "overflow precautions" need to be implemented here. 603 st->print_cr (" interpreter_invocation_count: " INT32_FORMAT_W(11), interpreter_invocation_count()); 604 st->print_cr (" invocation_counter: " INT32_FORMAT_W(11), invocation_count()); 605 st->print_cr (" backedge_counter: " INT32_FORMAT_W(11), backedge_count()); 606 607 if (method_data() != nullptr) { 608 st->print_cr (" decompile_count: " UINT32_FORMAT_W(11), method_data()->decompile_count()); 609 } 610 611 #ifndef PRODUCT 612 if (CountCompiledCalls) { 613 st->print_cr (" compiled_invocation_count: " INT64_FORMAT_W(11), compiled_invocation_count()); 614 } 615 #endif 616 } 617 618 MethodTrainingData* Method::training_data_or_null() const { 619 MethodCounters* mcs = method_counters(); 620 if (mcs == nullptr) { 621 return nullptr; 622 } else { 623 MethodTrainingData* mtd = mcs->method_training_data(); 624 if (mtd == mcs->method_training_data_sentinel()) { 625 return nullptr; 626 } 627 return mtd; 628 } 629 } 630 631 bool Method::init_training_data(MethodTrainingData* td) { 632 MethodCounters* mcs = method_counters(); 633 if (mcs == nullptr) { 634 return false; 635 } else { 636 return mcs->init_method_training_data(td); 637 } 638 } 639 640 bool Method::install_training_method_data(const methodHandle& method) { 641 MethodTrainingData* mtd = MethodTrainingData::find(method); 642 if (mtd != nullptr && mtd->final_profile() != nullptr) { 643 Atomic::replace_if_null(&method->_method_data, mtd->final_profile()); 644 return true; 645 } 646 return false; 647 } 648 649 // Build a MethodData* object to hold profiling information collected on this 650 // method when requested. 651 void Method::build_profiling_method_data(const methodHandle& method, TRAPS) { 652 if (install_training_method_data(method)) { 653 return; 654 } 655 // Do not profile the method if metaspace has hit an OOM previously 656 // allocating profiling data. Callers clear pending exception so don't 657 // add one here. 658 if (ClassLoaderDataGraph::has_metaspace_oom()) { 659 return; 660 } 661 662 ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); 663 MethodData* method_data = MethodData::allocate(loader_data, method, THREAD); 664 if (HAS_PENDING_EXCEPTION) { 665 CompileBroker::log_metaspace_failure(); 666 ClassLoaderDataGraph::set_metaspace_oom(true); 667 return; // return the exception (which is cleared) 668 } 669 670 if (!Atomic::replace_if_null(&method->_method_data, method_data)) { 671 MetadataFactory::free_metadata(loader_data, method_data); 672 return; 673 } 674 675 if (ForceProfiling && TrainingData::need_data()) { 676 MethodTrainingData* mtd = MethodTrainingData::make(method, false); 677 guarantee(mtd != nullptr, ""); 678 } 679 680 if (PrintMethodData) { 681 ResourceMark rm(THREAD); 682 tty->print("build_profiling_method_data for "); 683 method->print_name(tty); 684 tty->cr(); 685 // At the end of the run, the MDO, full of data, will be dumped. 686 } 687 } 688 689 MethodCounters* Method::build_method_counters(Thread* current, Method* m) { 690 // Do not profile the method if metaspace has hit an OOM previously 691 if (ClassLoaderDataGraph::has_metaspace_oom()) { 692 return nullptr; 693 } 694 695 methodHandle mh(current, m); 696 MethodCounters* counters; 697 if (current->is_Java_thread()) { 698 JavaThread* THREAD = JavaThread::cast(current); // For exception macros. 699 // Use the TRAPS version for a JavaThread so it will adjust the GC threshold 700 // if needed. 701 counters = MethodCounters::allocate_with_exception(mh, THREAD); 702 if (HAS_PENDING_EXCEPTION) { 703 CLEAR_PENDING_EXCEPTION; 704 } 705 } else { 706 // Call metaspace allocation that doesn't throw exception if the 707 // current thread isn't a JavaThread, ie. the VMThread. 708 counters = MethodCounters::allocate_no_exception(mh); 709 } 710 711 if (counters == nullptr) { 712 CompileBroker::log_metaspace_failure(); 713 ClassLoaderDataGraph::set_metaspace_oom(true); 714 return nullptr; 715 } 716 717 if (!mh->init_method_counters(counters)) { 718 MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters); 719 } 720 721 if (ForceProfiling && TrainingData::need_data()) { 722 MethodTrainingData* mtd = MethodTrainingData::make(mh, false); 723 guarantee(mtd != nullptr, ""); 724 } 725 726 return mh->method_counters(); 727 } 728 729 bool Method::init_method_counters(MethodCounters* counters) { 730 // Try to install a pointer to MethodCounters, return true on success. 731 return Atomic::replace_if_null(&_method_counters, counters); 732 } 733 734 void Method::set_exception_handler_entered(int handler_bci) { 735 if (ProfileExceptionHandlers) { 736 MethodData* mdo = method_data(); 737 if (mdo != nullptr) { 738 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci); 739 handler_data.set_exception_handler_entered(); 740 } 741 } 742 } 743 744 int Method::extra_stack_words() { 745 // not an inline function, to avoid a header dependency on Interpreter 746 return extra_stack_entries() * Interpreter::stackElementSize; 747 } 748 749 bool Method::compute_has_loops_flag() { 750 BytecodeStream bcs(methodHandle(Thread::current(), this)); 751 Bytecodes::Code bc; 752 753 while ((bc = bcs.next()) >= 0) { 754 switch (bc) { 755 case Bytecodes::_ifeq: 756 case Bytecodes::_ifnull: 757 case Bytecodes::_iflt: 758 case Bytecodes::_ifle: 759 case Bytecodes::_ifne: 760 case Bytecodes::_ifnonnull: 761 case Bytecodes::_ifgt: 762 case Bytecodes::_ifge: 763 case Bytecodes::_if_icmpeq: 764 case Bytecodes::_if_icmpne: 765 case Bytecodes::_if_icmplt: 766 case Bytecodes::_if_icmpgt: 767 case Bytecodes::_if_icmple: 768 case Bytecodes::_if_icmpge: 769 case Bytecodes::_if_acmpeq: 770 case Bytecodes::_if_acmpne: 771 case Bytecodes::_goto: 772 case Bytecodes::_jsr: 773 if (bcs.dest() < bcs.next_bci()) { 774 return set_has_loops(); 775 } 776 break; 777 778 case Bytecodes::_goto_w: 779 case Bytecodes::_jsr_w: 780 if (bcs.dest_w() < bcs.next_bci()) { 781 return set_has_loops(); 782 } 783 break; 784 785 case Bytecodes::_lookupswitch: { 786 Bytecode_lookupswitch lookupswitch(this, bcs.bcp()); 787 if (lookupswitch.default_offset() < 0) { 788 return set_has_loops(); 789 } else { 790 for (int i = 0; i < lookupswitch.number_of_pairs(); ++i) { 791 LookupswitchPair pair = lookupswitch.pair_at(i); 792 if (pair.offset() < 0) { 793 return set_has_loops(); 794 } 795 } 796 } 797 break; 798 } 799 case Bytecodes::_tableswitch: { 800 Bytecode_tableswitch tableswitch(this, bcs.bcp()); 801 if (tableswitch.default_offset() < 0) { 802 return set_has_loops(); 803 } else { 804 for (int i = 0; i < tableswitch.length(); ++i) { 805 if (tableswitch.dest_offset_at(i) < 0) { 806 return set_has_loops(); 807 } 808 } 809 } 810 break; 811 } 812 default: 813 break; 814 } 815 } 816 817 _flags.set_has_loops_flag_init(true); 818 return false; 819 } 820 821 bool Method::is_final_method(AccessFlags class_access_flags) const { 822 // or "does_not_require_vtable_entry" 823 // default method or overpass can occur, is not final (reuses vtable entry) 824 // private methods in classes get vtable entries for backward class compatibility. 825 if (is_overpass() || is_default_method()) return false; 826 return is_final() || class_access_flags.is_final(); 827 } 828 829 bool Method::is_final_method() const { 830 return is_final_method(method_holder()->access_flags()); 831 } 832 833 bool Method::is_default_method() const { 834 if (method_holder() != nullptr && 835 method_holder()->is_interface() && 836 !is_abstract() && !is_private()) { 837 return true; 838 } else { 839 return false; 840 } 841 } 842 843 bool Method::can_be_statically_bound(AccessFlags class_access_flags) const { 844 if (is_final_method(class_access_flags)) return true; 845 #ifdef ASSERT 846 bool is_nonv = (vtable_index() == nonvirtual_vtable_index); 847 if (class_access_flags.is_interface()) { 848 ResourceMark rm; 849 assert(is_nonv == is_static() || is_nonv == is_private(), 850 "nonvirtual unexpected for non-static, non-private: %s", 851 name_and_sig_as_C_string()); 852 } 853 #endif 854 assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question"); 855 return vtable_index() == nonvirtual_vtable_index; 856 } 857 858 bool Method::can_be_statically_bound() const { 859 return can_be_statically_bound(method_holder()->access_flags()); 860 } 861 862 bool Method::can_be_statically_bound(InstanceKlass* context) const { 863 return (method_holder() == context) && can_be_statically_bound(); 864 } 865 866 /** 867 * Returns false if this is one of specially treated methods for 868 * which we have to provide stack trace in throw in compiled code. 869 * Returns true otherwise. 870 */ 871 bool Method::can_omit_stack_trace() { 872 if (klass_name() == vmSymbols::sun_invoke_util_ValueConversions()) { 873 return false; // All methods in sun.invoke.util.ValueConversions 874 } 875 return true; 876 } 877 878 bool Method::is_accessor() const { 879 return is_getter() || is_setter(); 880 } 881 882 bool Method::is_getter() const { 883 if (code_size() != 5) return false; 884 if (size_of_parameters() != 1) return false; 885 if (java_code_at(0) != Bytecodes::_aload_0) return false; 886 if (java_code_at(1) != Bytecodes::_getfield) return false; 887 switch (java_code_at(4)) { 888 case Bytecodes::_ireturn: 889 case Bytecodes::_lreturn: 890 case Bytecodes::_freturn: 891 case Bytecodes::_dreturn: 892 case Bytecodes::_areturn: 893 break; 894 default: 895 return false; 896 } 897 return true; 898 } 899 900 bool Method::is_setter() const { 901 if (code_size() != 6) return false; 902 if (java_code_at(0) != Bytecodes::_aload_0) return false; 903 switch (java_code_at(1)) { 904 case Bytecodes::_iload_1: 905 case Bytecodes::_aload_1: 906 case Bytecodes::_fload_1: 907 if (size_of_parameters() != 2) return false; 908 break; 909 case Bytecodes::_dload_1: 910 case Bytecodes::_lload_1: 911 if (size_of_parameters() != 3) return false; 912 break; 913 default: 914 return false; 915 } 916 if (java_code_at(2) != Bytecodes::_putfield) return false; 917 if (java_code_at(5) != Bytecodes::_return) return false; 918 return true; 919 } 920 921 bool Method::is_constant_getter() const { 922 int last_index = code_size() - 1; 923 // Check if the first 1-3 bytecodes are a constant push 924 // and the last bytecode is a return. 925 return (2 <= code_size() && code_size() <= 4 && 926 Bytecodes::is_const(java_code_at(0)) && 927 Bytecodes::length_for(java_code_at(0)) == last_index && 928 Bytecodes::is_return(java_code_at(last_index))); 929 } 930 931 bool Method::has_valid_initializer_flags() const { 932 return (is_static() || 933 method_holder()->major_version() < 51); 934 } 935 936 bool Method::is_static_initializer() const { 937 // For classfiles version 51 or greater, ensure that the clinit method is 938 // static. Non-static methods with the name "<clinit>" are not static 939 // initializers. (older classfiles exempted for backward compatibility) 940 return name() == vmSymbols::class_initializer_name() && 941 has_valid_initializer_flags(); 942 } 943 944 bool Method::is_object_initializer() const { 945 return name() == vmSymbols::object_initializer_name(); 946 } 947 948 bool Method::needs_clinit_barrier() const { 949 return is_static() && !method_holder()->is_initialized(); 950 } 951 952 bool Method::code_has_clinit_barriers() const { 953 nmethod* nm = code(); 954 return (nm != nullptr) && nm->has_clinit_barriers(); 955 } 956 957 bool Method::is_object_wait0() const { 958 return klass_name() == vmSymbols::java_lang_Object() 959 && name() == vmSymbols::wait_name(); 960 } 961 962 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) { 963 int length = method->checked_exceptions_length(); 964 if (length == 0) { // common case 965 return objArrayHandle(THREAD, Universe::the_empty_class_array()); 966 } else { 967 methodHandle h_this(THREAD, method); 968 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle())); 969 objArrayHandle mirrors (THREAD, m_oop); 970 for (int i = 0; i < length; i++) { 971 CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe 972 Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle())); 973 if (log_is_enabled(Warning, exceptions) && 974 !k->is_subclass_of(vmClasses::Throwable_klass())) { 975 ResourceMark rm(THREAD); 976 log_warning(exceptions)( 977 "Class %s in throws clause of method %s is not a subtype of class java.lang.Throwable", 978 k->external_name(), method->external_name()); 979 } 980 mirrors->obj_at_put(i, k->java_mirror()); 981 } 982 return mirrors; 983 } 984 }; 985 986 987 int Method::line_number_from_bci(int bci) const { 988 int best_bci = 0; 989 int best_line = -1; 990 if (bci == SynchronizationEntryBCI) bci = 0; 991 if (0 <= bci && bci < code_size() && has_linenumber_table()) { 992 // The line numbers are a short array of 2-tuples [start_pc, line_number]. 993 // Not necessarily sorted and not necessarily one-to-one. 994 CompressedLineNumberReadStream stream(compressed_linenumber_table()); 995 while (stream.read_pair()) { 996 if (stream.bci() == bci) { 997 // perfect match 998 return stream.line(); 999 } else { 1000 // update best_bci/line 1001 if (stream.bci() < bci && stream.bci() >= best_bci) { 1002 best_bci = stream.bci(); 1003 best_line = stream.line(); 1004 } 1005 } 1006 } 1007 } 1008 return best_line; 1009 } 1010 1011 1012 bool Method::is_klass_loaded_by_klass_index(int klass_index) const { 1013 if( constants()->tag_at(klass_index).is_unresolved_klass() ) { 1014 Thread *thread = Thread::current(); 1015 Symbol* klass_name = constants()->klass_name_at(klass_index); 1016 Handle loader(thread, method_holder()->class_loader()); 1017 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr; 1018 } else { 1019 return true; 1020 } 1021 } 1022 1023 1024 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const { 1025 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc); 1026 if (must_be_resolved) { 1027 // Make sure klass is resolved in constantpool. 1028 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false; 1029 } 1030 return is_klass_loaded_by_klass_index(klass_index); 1031 } 1032 1033 1034 void Method::set_native_function(address function, bool post_event_flag) { 1035 assert(function != nullptr, "use clear_native_function to unregister natives"); 1036 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), ""); 1037 address* native_function = native_function_addr(); 1038 1039 // We can see racers trying to place the same native function into place. Once 1040 // is plenty. 1041 address current = *native_function; 1042 if (current == function) return; 1043 if (post_event_flag && JvmtiExport::should_post_native_method_bind() && 1044 function != nullptr) { 1045 // native_method_throw_unsatisfied_link_error_entry() should only 1046 // be passed when post_event_flag is false. 1047 assert(function != 1048 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1049 "post_event_flag mismatch"); 1050 1051 // post the bind event, and possible change the bind function 1052 JvmtiExport::post_native_method_bind(this, &function); 1053 } 1054 *native_function = function; 1055 // This function can be called more than once. We must make sure that we always 1056 // use the latest registered method -> check if a stub already has been generated. 1057 // If so, we have to make it not_entrant. 1058 nmethod* nm = code(); // Put it into local variable to guard against concurrent updates 1059 if (nm != nullptr) { 1060 nm->make_not_entrant("set native function"); 1061 } 1062 } 1063 1064 1065 bool Method::has_native_function() const { 1066 if (is_special_native_intrinsic()) 1067 return false; // special-cased in SharedRuntime::generate_native_wrapper 1068 address func = native_function(); 1069 return (func != nullptr && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry()); 1070 } 1071 1072 1073 void Method::clear_native_function() { 1074 // Note: is_method_handle_intrinsic() is allowed here. 1075 set_native_function( 1076 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1077 !native_bind_event_is_interesting); 1078 this->unlink_code(); 1079 } 1080 1081 1082 void Method::set_signature_handler(address handler) { 1083 address* signature_handler = signature_handler_addr(); 1084 *signature_handler = handler; 1085 } 1086 1087 1088 void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) { 1089 assert(reason != nullptr, "must provide a reason"); 1090 if (PrintCompilation && report) { 1091 ttyLocker ttyl; 1092 tty->print("made not %scompilable on ", is_osr ? "OSR " : ""); 1093 if (comp_level == CompLevel_all) { 1094 tty->print("all levels "); 1095 } else { 1096 tty->print("level %d ", comp_level); 1097 } 1098 this->print_short_name(tty); 1099 int size = this->code_size(); 1100 if (size > 0) { 1101 tty->print(" (%d bytes)", size); 1102 } 1103 if (reason != nullptr) { 1104 tty->print(" %s", reason); 1105 } 1106 tty->cr(); 1107 } 1108 if ((TraceDeoptimization || LogCompilation) && (xtty != nullptr)) { 1109 ttyLocker ttyl; 1110 xtty->begin_elem("make_not_compilable thread='%zu' osr='%d' level='%d'", 1111 os::current_thread_id(), is_osr, comp_level); 1112 if (reason != nullptr) { 1113 xtty->print(" reason=\'%s\'", reason); 1114 } 1115 xtty->method(this); 1116 xtty->stamp(); 1117 xtty->end_elem(); 1118 } 1119 } 1120 1121 bool Method::is_always_compilable() const { 1122 // Generated adapters must be compiled 1123 if (is_special_native_intrinsic() && is_synthetic()) { 1124 assert(!is_not_c1_compilable(), "sanity check"); 1125 assert(!is_not_c2_compilable(), "sanity check"); 1126 return true; 1127 } 1128 1129 return false; 1130 } 1131 1132 bool Method::is_not_compilable(int comp_level) const { 1133 if (number_of_breakpoints() > 0) 1134 return true; 1135 if (is_always_compilable()) 1136 return false; 1137 if (comp_level == CompLevel_any) 1138 return is_not_c1_compilable() && is_not_c2_compilable(); 1139 if (is_c1_compile(comp_level)) 1140 return is_not_c1_compilable(); 1141 if (is_c2_compile(comp_level)) 1142 return is_not_c2_compilable(); 1143 return false; 1144 } 1145 1146 // call this when compiler finds that this method is not compilable 1147 void Method::set_not_compilable(const char* reason, int comp_level, bool report) { 1148 if (is_always_compilable()) { 1149 // Don't mark a method which should be always compilable 1150 return; 1151 } 1152 print_made_not_compilable(comp_level, /*is_osr*/ false, report, reason); 1153 if (comp_level == CompLevel_all) { 1154 set_is_not_c1_compilable(); 1155 set_is_not_c2_compilable(); 1156 } else { 1157 if (is_c1_compile(comp_level)) 1158 set_is_not_c1_compilable(); 1159 if (is_c2_compile(comp_level)) 1160 set_is_not_c2_compilable(); 1161 } 1162 assert(!CompilationPolicy::can_be_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check"); 1163 } 1164 1165 bool Method::is_not_osr_compilable(int comp_level) const { 1166 if (is_not_compilable(comp_level)) 1167 return true; 1168 if (comp_level == CompLevel_any) 1169 return is_not_c1_osr_compilable() && is_not_c2_osr_compilable(); 1170 if (is_c1_compile(comp_level)) 1171 return is_not_c1_osr_compilable(); 1172 if (is_c2_compile(comp_level)) 1173 return is_not_c2_osr_compilable(); 1174 return false; 1175 } 1176 1177 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) { 1178 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason); 1179 if (comp_level == CompLevel_all) { 1180 set_is_not_c1_osr_compilable(); 1181 set_is_not_c2_osr_compilable(); 1182 } else { 1183 if (is_c1_compile(comp_level)) 1184 set_is_not_c1_osr_compilable(); 1185 if (is_c2_compile(comp_level)) 1186 set_is_not_c2_osr_compilable(); 1187 } 1188 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check"); 1189 } 1190 1191 // Revert to using the interpreter and clear out the nmethod 1192 void Method::clear_code() { 1193 // this may be null if c2i adapters have not been made yet 1194 // Only should happen at allocate time. 1195 if (adapter() == nullptr) { 1196 _from_compiled_entry = nullptr; 1197 } else { 1198 _from_compiled_entry = adapter()->get_c2i_entry(); 1199 } 1200 OrderAccess::storestore(); 1201 _from_interpreted_entry = _i2i_entry; 1202 OrderAccess::storestore(); 1203 _code = nullptr; 1204 } 1205 1206 void Method::unlink_code(nmethod *compare) { 1207 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag); 1208 // We need to check if either the _code or _from_compiled_code_entry_point 1209 // refer to this nmethod because there is a race in setting these two fields 1210 // in Method* as seen in bugid 4947125. 1211 if (code() == compare || 1212 from_compiled_entry() == compare->verified_entry_point()) { 1213 clear_code(); 1214 } 1215 } 1216 1217 void Method::unlink_code() { 1218 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag); 1219 clear_code(); 1220 } 1221 1222 #if INCLUDE_CDS 1223 // Called by class data sharing to remove any entry points (which are not shared) 1224 void Method::unlink_method() { 1225 assert(CDSConfig::is_dumping_archive(), "sanity"); 1226 _code = nullptr; 1227 if (!CDSConfig::is_dumping_adapters() || AdapterHandlerLibrary::is_abstract_method_adapter(_adapter)) { 1228 _adapter = nullptr; 1229 } 1230 _i2i_entry = nullptr; 1231 _from_compiled_entry = nullptr; 1232 _from_interpreted_entry = nullptr; 1233 1234 if (is_native()) { 1235 *native_function_addr() = nullptr; 1236 set_signature_handler(nullptr); 1237 } 1238 NOT_PRODUCT(set_compiled_invocation_count(0);) 1239 1240 clear_method_data(); 1241 clear_method_counters(); 1242 clear_is_not_c1_compilable(); 1243 clear_is_not_c1_osr_compilable(); 1244 clear_is_not_c2_compilable(); 1245 clear_is_not_c2_osr_compilable(); 1246 clear_queued_for_compilation(); 1247 set_pending_queue_processed(false); 1248 remove_unshareable_flags(); 1249 } 1250 1251 void Method::remove_unshareable_flags() { 1252 // clear all the flags that shouldn't be in the archived version 1253 assert(!is_old(), "must be"); 1254 assert(!is_obsolete(), "must be"); 1255 assert(!is_deleted(), "must be"); 1256 1257 set_is_prefixed_native(false); 1258 set_queued_for_compilation(false); 1259 set_pending_queue_processed(false); 1260 set_is_not_c2_compilable(false); 1261 set_is_not_c1_compilable(false); 1262 set_is_not_c2_osr_compilable(false); 1263 set_on_stack_flag(false); 1264 set_has_upcall_on_method_entry(false); 1265 set_has_upcall_on_method_exit(false); 1266 } 1267 #endif 1268 1269 // Called when the method_holder is getting linked. Setup entrypoints so the method 1270 // is ready to be called from interpreter, compiler, and vtables. 1271 void Method::link_method(const methodHandle& h_method, TRAPS) { 1272 if (log_is_enabled(Info, perf, class, link)) { 1273 ClassLoader::perf_ik_link_methods_count()->inc(); 1274 } 1275 1276 // If the code cache is full, we may reenter this function for the 1277 // leftover methods that weren't linked. 1278 if (adapter() != nullptr && !adapter()->is_shared()) { 1279 return; 1280 } 1281 assert( _code == nullptr, "nothing compiled yet" ); 1282 1283 // Setup interpreter entrypoint 1284 assert(this == h_method(), "wrong h_method()" ); 1285 1286 assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache"); 1287 address entry = Interpreter::entry_for_method(h_method); 1288 assert(entry != nullptr, "interpreter entry must be non-null"); 1289 // Sets both _i2i_entry and _from_interpreted_entry 1290 set_interpreter_entry(entry); 1291 1292 // Don't overwrite already registered native entries. 1293 if (is_native() && !has_native_function()) { 1294 set_native_function( 1295 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1296 !native_bind_event_is_interesting); 1297 } 1298 1299 // Setup compiler entrypoint. This is made eagerly, so we do not need 1300 // special handling of vtables. An alternative is to make adapters more 1301 // lazily by calling make_adapter() from from_compiled_entry() for the 1302 // normal calls. For vtable calls life gets more complicated. When a 1303 // call-site goes mega-morphic we need adapters in all methods which can be 1304 // called from the vtable. We need adapters on such methods that get loaded 1305 // later. Ditto for mega-morphic itable calls. If this proves to be a 1306 // problem we'll make these lazily later. 1307 if (_adapter == nullptr) { 1308 (void) make_adapters(h_method, CHECK); 1309 } 1310 1311 // ONLY USE the h_method now as make_adapter may have blocked 1312 1313 if (h_method->is_continuation_native_intrinsic()) { 1314 _from_interpreted_entry = nullptr; 1315 _from_compiled_entry = nullptr; 1316 _i2i_entry = nullptr; 1317 if (Continuations::enabled()) { 1318 assert(!Threads::is_vm_complete(), "should only be called during vm init"); 1319 AdapterHandlerLibrary::create_native_wrapper(h_method); 1320 if (!h_method->has_compiled_code()) { 1321 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small"); 1322 } 1323 assert(_from_interpreted_entry == get_i2c_entry(), "invariant"); 1324 } 1325 } 1326 if (_preload_code != nullptr) { 1327 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag); 1328 set_code(h_method, _preload_code); 1329 assert(((nmethod*)_preload_code)->scc_entry() == _scc_entry, "sanity"); 1330 } 1331 } 1332 1333 address Method::make_adapters(const methodHandle& mh, TRAPS) { 1334 PerfTraceElapsedTime timer(ClassLoader::perf_method_adapters_time()); 1335 1336 // Adapters for compiled code are made eagerly here. They are fairly 1337 // small (generally < 100 bytes) and quick to make (and cached and shared) 1338 // so making them eagerly shouldn't be too expensive. 1339 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh); 1340 if (adapter == nullptr ) { 1341 if (!is_init_completed()) { 1342 // Don't throw exceptions during VM initialization because java.lang.* classes 1343 // might not have been initialized, causing problems when constructing the 1344 // Java exception object. 1345 vm_exit_during_initialization("Out of space in CodeCache for adapters"); 1346 } else { 1347 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters"); 1348 } 1349 } 1350 1351 mh->set_adapter_entry(adapter); 1352 mh->_from_compiled_entry = adapter->get_c2i_entry(); 1353 return adapter->get_c2i_entry(); 1354 } 1355 1356 // The verified_code_entry() must be called when a invoke is resolved 1357 // on this method. 1358 1359 // It returns the compiled code entry point, after asserting not null. 1360 // This function is called after potential safepoints so that nmethod 1361 // or adapter that it points to is still live and valid. 1362 // This function must not hit a safepoint! 1363 address Method::verified_code_entry() { 1364 debug_only(NoSafepointVerifier nsv;) 1365 assert(_from_compiled_entry != nullptr, "must be set"); 1366 return _from_compiled_entry; 1367 } 1368 1369 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all 1370 // (could be racing a deopt). 1371 // Not inline to avoid circular ref. 1372 bool Method::check_code() const { 1373 // cached in a register or local. There's a race on the value of the field. 1374 nmethod *code = Atomic::load_acquire(&_code); 1375 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method()); 1376 } 1377 1378 // Install compiled code. Instantly it can execute. 1379 void Method::set_code(const methodHandle& mh, nmethod *code) { 1380 assert_lock_strong(NMethodState_lock); 1381 assert( code, "use clear_code to remove code" ); 1382 assert( mh->check_code(), "" ); 1383 1384 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!"); 1385 1386 // These writes must happen in this order, because the interpreter will 1387 // directly jump to from_interpreted_entry which jumps to an i2c adapter 1388 // which jumps to _from_compiled_entry. 1389 mh->_code = code; // Assign before allowing compiled code to exec 1390 1391 int comp_level = code->comp_level(); 1392 // In theory there could be a race here. In practice it is unlikely 1393 // and not worth worrying about. 1394 if (comp_level > mh->highest_comp_level()) { 1395 mh->set_highest_comp_level(comp_level); 1396 } 1397 1398 OrderAccess::storestore(); 1399 mh->_from_compiled_entry = code->verified_entry_point(); 1400 OrderAccess::storestore(); 1401 1402 if (mh->is_continuation_native_intrinsic()) { 1403 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method 1404 1405 if (mh->is_continuation_enter_intrinsic()) { 1406 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted 1407 mh->_i2i_entry = ContinuationEntry::interpreted_entry(); 1408 } else if (mh->is_continuation_yield_intrinsic()) { 1409 mh->_i2i_entry = mh->get_i2c_entry(); 1410 } else { 1411 guarantee(false, "Unknown Continuation native intrinsic"); 1412 } 1413 // This must come last, as it is what's tested in LinkResolver::resolve_static_call 1414 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry()); 1415 } else if (!mh->is_method_handle_intrinsic()) { 1416 // Instantly compiled code can execute. 1417 mh->_from_interpreted_entry = mh->get_i2c_entry(); 1418 } 1419 } 1420 1421 1422 bool Method::is_overridden_in(Klass* k) const { 1423 InstanceKlass* ik = InstanceKlass::cast(k); 1424 1425 if (ik->is_interface()) return false; 1426 1427 // If method is an interface, we skip it - except if it 1428 // is a miranda method 1429 if (method_holder()->is_interface()) { 1430 // Check that method is not a miranda method 1431 if (ik->lookup_method(name(), signature()) == nullptr) { 1432 // No implementation exist - so miranda method 1433 return false; 1434 } 1435 return true; 1436 } 1437 1438 assert(ik->is_subclass_of(method_holder()), "should be subklass"); 1439 if (!has_vtable_index()) { 1440 return false; 1441 } else { 1442 Method* vt_m = ik->method_at_vtable(vtable_index()); 1443 return vt_m != this; 1444 } 1445 } 1446 1447 1448 // give advice about whether this Method* should be cached or not 1449 bool Method::should_not_be_cached() const { 1450 if (is_old()) { 1451 // This method has been redefined. It is either EMCP or obsolete 1452 // and we don't want to cache it because that would pin the method 1453 // down and prevent it from being collectible if and when it 1454 // finishes executing. 1455 return true; 1456 } 1457 1458 // caching this method should be just fine 1459 return false; 1460 } 1461 1462 1463 /** 1464 * Returns true if this is one of the specially treated methods for 1465 * security related stack walks (like Reflection.getCallerClass). 1466 */ 1467 bool Method::is_ignored_by_security_stack_walk() const { 1468 if (intrinsic_id() == vmIntrinsics::_invoke) { 1469 // This is Method.invoke() -- ignore it 1470 return true; 1471 } 1472 if (method_holder()->is_subclass_of(vmClasses::reflect_MethodAccessorImpl_klass())) { 1473 // This is an auxiliary frame -- ignore it 1474 return true; 1475 } 1476 if (is_method_handle_intrinsic() || is_compiled_lambda_form()) { 1477 // This is an internal adapter frame for method handles -- ignore it 1478 return true; 1479 } 1480 return false; 1481 } 1482 1483 1484 // Constant pool structure for invoke methods: 1485 enum { 1486 _imcp_invoke_name = 1, // utf8: 'invokeExact', etc. 1487 _imcp_invoke_signature, // utf8: (variable Symbol*) 1488 _imcp_limit 1489 }; 1490 1491 // Test if this method is an MH adapter frame generated by Java code. 1492 // Cf. java/lang/invoke/InvokerBytecodeGenerator 1493 bool Method::is_compiled_lambda_form() const { 1494 return intrinsic_id() == vmIntrinsics::_compiledLambdaForm; 1495 } 1496 1497 // Test if this method is an internal MH primitive method. 1498 bool Method::is_method_handle_intrinsic() const { 1499 vmIntrinsics::ID iid = intrinsic_id(); 1500 return (MethodHandles::is_signature_polymorphic(iid) && 1501 MethodHandles::is_signature_polymorphic_intrinsic(iid)); 1502 } 1503 1504 bool Method::has_member_arg() const { 1505 vmIntrinsics::ID iid = intrinsic_id(); 1506 return (MethodHandles::is_signature_polymorphic(iid) && 1507 MethodHandles::has_member_arg(iid)); 1508 } 1509 1510 // Make an instance of a signature-polymorphic internal MH primitive. 1511 methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid, 1512 Symbol* signature, 1513 TRAPS) { 1514 ResourceMark rm(THREAD); 1515 methodHandle empty; 1516 1517 InstanceKlass* holder = vmClasses::MethodHandle_klass(); 1518 Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid); 1519 assert(iid == MethodHandles::signature_polymorphic_name_id(name), ""); 1520 1521 log_info(methodhandles)("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string()); 1522 1523 // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup) 1524 name->increment_refcount(); 1525 signature->increment_refcount(); 1526 1527 int cp_length = _imcp_limit; 1528 ClassLoaderData* loader_data = holder->class_loader_data(); 1529 constantPoolHandle cp; 1530 { 1531 ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty)); 1532 cp = constantPoolHandle(THREAD, cp_oop); 1533 } 1534 cp->copy_fields(holder->constants()); 1535 cp->set_pool_holder(holder); 1536 cp->symbol_at_put(_imcp_invoke_name, name); 1537 cp->symbol_at_put(_imcp_invoke_signature, signature); 1538 cp->set_has_preresolution(); 1539 cp->set_is_for_method_handle_intrinsic(); 1540 1541 // decide on access bits: public or not? 1542 u2 flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL); 1543 bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid); 1544 if (must_be_static) flags_bits |= JVM_ACC_STATIC; 1545 assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods"); 1546 1547 methodHandle m; 1548 { 1549 InlineTableSizes sizes; 1550 Method* m_oop = Method::allocate(loader_data, 0, 1551 accessFlags_from(flags_bits), &sizes, 1552 ConstMethod::NORMAL, 1553 name, 1554 CHECK_(empty)); 1555 m = methodHandle(THREAD, m_oop); 1556 } 1557 m->set_constants(cp()); 1558 m->set_name_index(_imcp_invoke_name); 1559 m->set_signature_index(_imcp_invoke_signature); 1560 assert(MethodHandles::is_signature_polymorphic_name(m->name()), ""); 1561 assert(m->signature() == signature, ""); 1562 m->constMethod()->compute_from_signature(signature, must_be_static); 1563 m->init_intrinsic_id(klass_id_for_intrinsics(m->method_holder())); 1564 assert(m->is_method_handle_intrinsic(), ""); 1565 #ifdef ASSERT 1566 if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id())) m->print(); 1567 assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker"); 1568 assert(m->intrinsic_id() == iid, "correctly predicted iid"); 1569 #endif //ASSERT 1570 1571 // Finally, set up its entry points. 1572 assert(m->can_be_statically_bound(), ""); 1573 m->set_vtable_index(Method::nonvirtual_vtable_index); 1574 m->link_method(m, CHECK_(empty)); 1575 1576 if (iid == vmIntrinsics::_linkToNative) { 1577 m->set_interpreter_entry(m->adapter()->get_i2c_entry()); 1578 } 1579 if (log_is_enabled(Debug, methodhandles)) { 1580 LogTarget(Debug, methodhandles) lt; 1581 LogStream ls(lt); 1582 m->print_on(&ls); 1583 } 1584 1585 return m; 1586 } 1587 1588 #if INCLUDE_CDS 1589 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) { 1590 m->restore_adapter(CHECK); 1591 if (m->adapter() != nullptr) { 1592 m->adapter()->restore_unshareable_info(CHECK); 1593 m->set_from_compiled_entry(m->adapter()->get_c2i_entry()); 1594 } 1595 m->link_method(m, CHECK); 1596 1597 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) { 1598 m->set_interpreter_entry(m->adapter()->get_i2c_entry()); 1599 } 1600 } 1601 #endif 1602 1603 Klass* Method::check_non_bcp_klass(Klass* klass) { 1604 if (klass != nullptr && klass->class_loader() != nullptr) { 1605 if (klass->is_objArray_klass()) 1606 klass = ObjArrayKlass::cast(klass)->bottom_klass(); 1607 return klass; 1608 } 1609 return nullptr; 1610 } 1611 1612 1613 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length, 1614 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) { 1615 // Code below does not work for native methods - they should never get rewritten anyway 1616 assert(!m->is_native(), "cannot rewrite native methods"); 1617 // Allocate new Method* 1618 AccessFlags flags = m->access_flags(); 1619 1620 ConstMethod* cm = m->constMethod(); 1621 int checked_exceptions_len = cm->checked_exceptions_length(); 1622 int localvariable_len = cm->localvariable_table_length(); 1623 int exception_table_len = cm->exception_table_length(); 1624 int method_parameters_len = cm->method_parameters_length(); 1625 int method_annotations_len = cm->method_annotations_length(); 1626 int parameter_annotations_len = cm->parameter_annotations_length(); 1627 int type_annotations_len = cm->type_annotations_length(); 1628 int default_annotations_len = cm->default_annotations_length(); 1629 1630 InlineTableSizes sizes( 1631 localvariable_len, 1632 new_compressed_linenumber_size, 1633 exception_table_len, 1634 checked_exceptions_len, 1635 method_parameters_len, 1636 cm->generic_signature_index(), 1637 method_annotations_len, 1638 parameter_annotations_len, 1639 type_annotations_len, 1640 default_annotations_len, 1641 0); 1642 1643 ClassLoaderData* loader_data = m->method_holder()->class_loader_data(); 1644 Method* newm_oop = Method::allocate(loader_data, 1645 new_code_length, 1646 flags, 1647 &sizes, 1648 m->method_type(), 1649 m->name(), 1650 CHECK_(methodHandle())); 1651 methodHandle newm (THREAD, newm_oop); 1652 1653 // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod* 1654 ConstMethod* newcm = newm->constMethod(); 1655 int new_const_method_size = newm->constMethod()->size(); 1656 1657 // This works because the source and target are both Methods. Some compilers 1658 // (e.g., clang) complain that the target vtable pointer will be stomped, 1659 // so cast away newm()'s and m()'s Methodness. 1660 memcpy((void*)newm(), (void*)m(), sizeof(Method)); 1661 1662 // Create shallow copy of ConstMethod. 1663 memcpy(newcm, m->constMethod(), sizeof(ConstMethod)); 1664 1665 // Reset correct method/const method, method size, and parameter info 1666 newm->set_constMethod(newcm); 1667 newm->constMethod()->set_code_size(new_code_length); 1668 newm->constMethod()->set_constMethod_size(new_const_method_size); 1669 assert(newm->code_size() == new_code_length, "check"); 1670 assert(newm->method_parameters_length() == method_parameters_len, "check"); 1671 assert(newm->checked_exceptions_length() == checked_exceptions_len, "check"); 1672 assert(newm->exception_table_length() == exception_table_len, "check"); 1673 assert(newm->localvariable_table_length() == localvariable_len, "check"); 1674 // Copy new byte codes 1675 memcpy(newm->code_base(), new_code, new_code_length); 1676 // Copy line number table 1677 if (new_compressed_linenumber_size > 0) { 1678 memcpy(newm->compressed_linenumber_table(), 1679 new_compressed_linenumber_table, 1680 new_compressed_linenumber_size); 1681 } 1682 // Copy method_parameters 1683 if (method_parameters_len > 0) { 1684 memcpy(newm->method_parameters_start(), 1685 m->method_parameters_start(), 1686 method_parameters_len * sizeof(MethodParametersElement)); 1687 } 1688 // Copy checked_exceptions 1689 if (checked_exceptions_len > 0) { 1690 memcpy(newm->checked_exceptions_start(), 1691 m->checked_exceptions_start(), 1692 checked_exceptions_len * sizeof(CheckedExceptionElement)); 1693 } 1694 // Copy exception table 1695 if (exception_table_len > 0) { 1696 memcpy(newm->exception_table_start(), 1697 m->exception_table_start(), 1698 exception_table_len * sizeof(ExceptionTableElement)); 1699 } 1700 // Copy local variable number table 1701 if (localvariable_len > 0) { 1702 memcpy(newm->localvariable_table_start(), 1703 m->localvariable_table_start(), 1704 localvariable_len * sizeof(LocalVariableTableElement)); 1705 } 1706 // Copy stackmap table 1707 if (m->has_stackmap_table()) { 1708 int code_attribute_length = m->stackmap_data()->length(); 1709 Array<u1>* stackmap_data = 1710 MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_(methodHandle())); 1711 memcpy((void*)stackmap_data->adr_at(0), 1712 (void*)m->stackmap_data()->adr_at(0), code_attribute_length); 1713 newm->set_stackmap_data(stackmap_data); 1714 } 1715 1716 // copy annotations over to new method 1717 newcm->copy_annotations_from(loader_data, cm, CHECK_(methodHandle())); 1718 return newm; 1719 } 1720 1721 vmSymbolID Method::klass_id_for_intrinsics(const Klass* holder) { 1722 // if loader is not the default loader (i.e., non-null), we can't know the intrinsics 1723 // because we are not loading from core libraries 1724 // exception: the AES intrinsics come from lib/ext/sunjce_provider.jar 1725 // which does not use the class default class loader so we check for its loader here 1726 const InstanceKlass* ik = InstanceKlass::cast(holder); 1727 if ((ik->class_loader() != nullptr) && !SystemDictionary::is_platform_class_loader(ik->class_loader())) { 1728 return vmSymbolID::NO_SID; // regardless of name, no intrinsics here 1729 } 1730 1731 // see if the klass name is well-known: 1732 Symbol* klass_name = ik->name(); 1733 vmSymbolID id = vmSymbols::find_sid(klass_name); 1734 if (id != vmSymbolID::NO_SID && vmIntrinsics::class_has_intrinsics(id)) { 1735 return id; 1736 } else { 1737 return vmSymbolID::NO_SID; 1738 } 1739 } 1740 1741 void Method::init_intrinsic_id(vmSymbolID klass_id) { 1742 assert(_intrinsic_id == static_cast<int>(vmIntrinsics::_none), "do this just once"); 1743 const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte)); 1744 assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size"); 1745 assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), ""); 1746 1747 // the klass name is well-known: 1748 assert(klass_id == klass_id_for_intrinsics(method_holder()), "must be"); 1749 assert(klass_id != vmSymbolID::NO_SID, "caller responsibility"); 1750 1751 // ditto for method and signature: 1752 vmSymbolID name_id = vmSymbols::find_sid(name()); 1753 if (klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) 1754 && klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle) 1755 && name_id == vmSymbolID::NO_SID) { 1756 return; 1757 } 1758 vmSymbolID sig_id = vmSymbols::find_sid(signature()); 1759 if (klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) 1760 && klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle) 1761 && sig_id == vmSymbolID::NO_SID) { 1762 return; 1763 } 1764 1765 u2 flags = access_flags().as_method_flags(); 1766 vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); 1767 if (id != vmIntrinsics::_none) { 1768 set_intrinsic_id(id); 1769 if (id == vmIntrinsics::_Class_cast) { 1770 // Even if the intrinsic is rejected, we want to inline this simple method. 1771 set_force_inline(); 1772 } 1773 return; 1774 } 1775 1776 // A few slightly irregular cases: 1777 switch (klass_id) { 1778 // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*., VarHandle 1779 case VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle): 1780 case VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle): 1781 if (!is_native()) break; 1782 id = MethodHandles::signature_polymorphic_name_id(method_holder(), name()); 1783 if (is_static() != MethodHandles::is_signature_polymorphic_static(id)) 1784 id = vmIntrinsics::_none; 1785 break; 1786 1787 default: 1788 break; 1789 } 1790 1791 if (id != vmIntrinsics::_none) { 1792 // Set up its iid. It is an alias method. 1793 set_intrinsic_id(id); 1794 return; 1795 } 1796 } 1797 1798 bool Method::load_signature_classes(const methodHandle& m, TRAPS) { 1799 if (!THREAD->can_call_java()) { 1800 // There is nothing useful this routine can do from within the Compile thread. 1801 // Hopefully, the signature contains only well-known classes. 1802 // We could scan for this and return true/false, but the caller won't care. 1803 return false; 1804 } 1805 bool sig_is_loaded = true; 1806 ResourceMark rm(THREAD); 1807 for (ResolvingSignatureStream ss(m()); !ss.is_done(); ss.next()) { 1808 if (ss.is_reference()) { 1809 // load everything, including arrays "[Lfoo;" 1810 Klass* klass = ss.as_klass(SignatureStream::ReturnNull, THREAD); 1811 // We are loading classes eagerly. If a ClassNotFoundException or 1812 // a LinkageError was generated, be sure to ignore it. 1813 if (HAS_PENDING_EXCEPTION) { 1814 if (PENDING_EXCEPTION->is_a(vmClasses::ClassNotFoundException_klass()) || 1815 PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) { 1816 CLEAR_PENDING_EXCEPTION; 1817 } else { 1818 return false; 1819 } 1820 } 1821 if( klass == nullptr) { sig_is_loaded = false; } 1822 } 1823 } 1824 return sig_is_loaded; 1825 } 1826 1827 // Exposed so field engineers can debug VM 1828 void Method::print_short_name(outputStream* st) const { 1829 ResourceMark rm; 1830 #ifdef PRODUCT 1831 st->print(" %s::", method_holder()->external_name()); 1832 #else 1833 st->print(" %s::", method_holder()->internal_name()); 1834 #endif 1835 name()->print_symbol_on(st); 1836 if (WizardMode) signature()->print_symbol_on(st); 1837 else if (MethodHandles::is_signature_polymorphic(intrinsic_id())) 1838 MethodHandles::print_as_basic_type_signature_on(st, signature()); 1839 } 1840 1841 // Comparer for sorting an object array containing 1842 // Method*s. 1843 static int method_comparator(Method* a, Method* b) { 1844 return a->name()->fast_compare(b->name()); 1845 } 1846 1847 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 1848 // default_methods also uses this without the ordering for fast find_method 1849 void Method::sort_methods(Array<Method*>* methods, bool set_idnums, method_comparator_func func) { 1850 int length = methods->length(); 1851 if (length > 1) { 1852 if (func == nullptr) { 1853 func = method_comparator; 1854 } 1855 { 1856 NoSafepointVerifier nsv; 1857 QuickSort::sort(methods->data(), length, func); 1858 } 1859 // Reset method ordering 1860 if (set_idnums) { 1861 for (u2 i = 0; i < length; i++) { 1862 Method* m = methods->at(i); 1863 m->set_method_idnum(i); 1864 m->set_orig_method_idnum(i); 1865 } 1866 } 1867 } 1868 } 1869 1870 //----------------------------------------------------------------------------------- 1871 // Non-product code unless JVM/TI needs it 1872 1873 #if !defined(PRODUCT) || INCLUDE_JVMTI 1874 class SignatureTypePrinter : public SignatureTypeNames { 1875 private: 1876 outputStream* _st; 1877 bool _use_separator; 1878 1879 void type_name(const char* name) { 1880 if (_use_separator) _st->print(", "); 1881 _st->print("%s", name); 1882 _use_separator = true; 1883 } 1884 1885 public: 1886 SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) { 1887 _st = st; 1888 _use_separator = false; 1889 } 1890 1891 void print_parameters() { _use_separator = false; do_parameters_on(this); } 1892 void print_returntype() { _use_separator = false; do_type(return_type()); } 1893 }; 1894 1895 1896 void Method::print_name(outputStream* st) const { 1897 Thread *thread = Thread::current(); 1898 ResourceMark rm(thread); 1899 st->print("%s ", is_static() ? "static" : "virtual"); 1900 if (WizardMode) { 1901 st->print("%s.", method_holder()->internal_name()); 1902 name()->print_symbol_on(st); 1903 signature()->print_symbol_on(st); 1904 } else { 1905 SignatureTypePrinter sig(signature(), st); 1906 sig.print_returntype(); 1907 st->print(" %s.", method_holder()->internal_name()); 1908 name()->print_symbol_on(st); 1909 st->print("("); 1910 sig.print_parameters(); 1911 st->print(")"); 1912 } 1913 } 1914 #endif // !PRODUCT || INCLUDE_JVMTI 1915 1916 1917 void Method::print_codes_on(outputStream* st, int flags) const { 1918 print_codes_on(0, code_size(), st, flags); 1919 } 1920 1921 void Method::print_codes_on(int from, int to, outputStream* st, int flags) const { 1922 Thread *thread = Thread::current(); 1923 ResourceMark rm(thread); 1924 methodHandle mh (thread, (Method*)this); 1925 BytecodeTracer::print_method_codes(mh, from, to, st, flags); 1926 } 1927 1928 CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) { 1929 _bci = 0; 1930 _line = 0; 1931 }; 1932 1933 bool CompressedLineNumberReadStream::read_pair() { 1934 jubyte next = read_byte(); 1935 // Check for terminator 1936 if (next == 0) return false; 1937 if (next == 0xFF) { 1938 // Escape character, regular compression used 1939 _bci += read_signed_int(); 1940 _line += read_signed_int(); 1941 } else { 1942 // Single byte compression used 1943 _bci += next >> 3; 1944 _line += next & 0x7; 1945 } 1946 return true; 1947 } 1948 1949 #if INCLUDE_JVMTI 1950 1951 Bytecodes::Code Method::orig_bytecode_at(int bci) const { 1952 BreakpointInfo* bp = method_holder()->breakpoints(); 1953 for (; bp != nullptr; bp = bp->next()) { 1954 if (bp->match(this, bci)) { 1955 return bp->orig_bytecode(); 1956 } 1957 } 1958 { 1959 ResourceMark rm; 1960 fatal("no original bytecode found in %s at bci %d", name_and_sig_as_C_string(), bci); 1961 } 1962 return Bytecodes::_shouldnotreachhere; 1963 } 1964 1965 void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) { 1966 assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way"); 1967 BreakpointInfo* bp = method_holder()->breakpoints(); 1968 for (; bp != nullptr; bp = bp->next()) { 1969 if (bp->match(this, bci)) { 1970 bp->set_orig_bytecode(code); 1971 // and continue, in case there is more than one 1972 } 1973 } 1974 } 1975 1976 void Method::set_breakpoint(int bci) { 1977 InstanceKlass* ik = method_holder(); 1978 BreakpointInfo *bp = new BreakpointInfo(this, bci); 1979 bp->set_next(ik->breakpoints()); 1980 ik->set_breakpoints(bp); 1981 // do this last: 1982 bp->set(this); 1983 } 1984 1985 static void clear_matches(Method* m, int bci) { 1986 InstanceKlass* ik = m->method_holder(); 1987 BreakpointInfo* prev_bp = nullptr; 1988 BreakpointInfo* next_bp; 1989 for (BreakpointInfo* bp = ik->breakpoints(); bp != nullptr; bp = next_bp) { 1990 next_bp = bp->next(); 1991 // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint). 1992 if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) { 1993 // do this first: 1994 bp->clear(m); 1995 // unhook it 1996 if (prev_bp != nullptr) 1997 prev_bp->set_next(next_bp); 1998 else 1999 ik->set_breakpoints(next_bp); 2000 delete bp; 2001 // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods 2002 // at same location. So we have multiple matching (method_index and bci) 2003 // BreakpointInfo nodes in BreakpointInfo list. We should just delete one 2004 // breakpoint for clear_breakpoint request and keep all other method versions 2005 // BreakpointInfo for future clear_breakpoint request. 2006 // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints) 2007 // which is being called when class is unloaded. We delete all the Breakpoint 2008 // information for all versions of method. We may not correctly restore the original 2009 // bytecode in all method versions, but that is ok. Because the class is being unloaded 2010 // so these methods won't be used anymore. 2011 if (bci >= 0) { 2012 break; 2013 } 2014 } else { 2015 // This one is a keeper. 2016 prev_bp = bp; 2017 } 2018 } 2019 } 2020 2021 void Method::clear_breakpoint(int bci) { 2022 assert(bci >= 0, ""); 2023 clear_matches(this, bci); 2024 } 2025 2026 void Method::clear_all_breakpoints() { 2027 clear_matches(this, -1); 2028 } 2029 2030 #endif // INCLUDE_JVMTI 2031 2032 int Method::highest_osr_comp_level() const { 2033 const MethodCounters* mcs = method_counters(); 2034 if (mcs != nullptr) { 2035 return mcs->highest_osr_comp_level(); 2036 } else { 2037 return CompLevel_none; 2038 } 2039 } 2040 2041 void Method::set_highest_comp_level(int level) { 2042 MethodCounters* mcs = method_counters(); 2043 if (mcs != nullptr) { 2044 mcs->set_highest_comp_level(level); 2045 } 2046 } 2047 2048 void Method::set_highest_osr_comp_level(int level) { 2049 MethodCounters* mcs = method_counters(); 2050 if (mcs != nullptr) { 2051 mcs->set_highest_osr_comp_level(level); 2052 } 2053 } 2054 2055 #if INCLUDE_JVMTI 2056 2057 BreakpointInfo::BreakpointInfo(Method* m, int bci) { 2058 _bci = bci; 2059 _name_index = m->name_index(); 2060 _signature_index = m->signature_index(); 2061 _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci); 2062 if (_orig_bytecode == Bytecodes::_breakpoint) 2063 _orig_bytecode = m->orig_bytecode_at(_bci); 2064 _next = nullptr; 2065 } 2066 2067 void BreakpointInfo::set(Method* method) { 2068 #ifdef ASSERT 2069 { 2070 Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci); 2071 if (code == Bytecodes::_breakpoint) 2072 code = method->orig_bytecode_at(_bci); 2073 assert(orig_bytecode() == code, "original bytecode must be the same"); 2074 } 2075 #endif 2076 Thread *thread = Thread::current(); 2077 *method->bcp_from(_bci) = Bytecodes::_breakpoint; 2078 method->incr_number_of_breakpoints(thread); 2079 { 2080 // Deoptimize all dependents on this method 2081 HandleMark hm(thread); 2082 methodHandle mh(thread, method); 2083 CodeCache::mark_dependents_on_method_for_breakpoint(mh); 2084 } 2085 } 2086 2087 void BreakpointInfo::clear(Method* method) { 2088 *method->bcp_from(_bci) = orig_bytecode(); 2089 assert(method->number_of_breakpoints() > 0, "must not go negative"); 2090 method->decr_number_of_breakpoints(Thread::current()); 2091 } 2092 2093 #endif // INCLUDE_JVMTI 2094 2095 // jmethodID handling 2096 2097 // This is a block allocating object, sort of like JNIHandleBlock, only a 2098 // lot simpler. 2099 // It's allocated on the CHeap because once we allocate a jmethodID, we can 2100 // never get rid of it. 2101 2102 static const int min_block_size = 8; 2103 2104 class JNIMethodBlockNode : public CHeapObj<mtClass> { 2105 friend class JNIMethodBlock; 2106 Method** _methods; 2107 int _number_of_methods; 2108 int _top; 2109 JNIMethodBlockNode* _next; 2110 2111 public: 2112 2113 JNIMethodBlockNode(int num_methods = min_block_size); 2114 2115 ~JNIMethodBlockNode() { FREE_C_HEAP_ARRAY(Method*, _methods); } 2116 2117 void ensure_methods(int num_addl_methods) { 2118 if (_top < _number_of_methods) { 2119 num_addl_methods -= _number_of_methods - _top; 2120 if (num_addl_methods <= 0) { 2121 return; 2122 } 2123 } 2124 if (_next == nullptr) { 2125 _next = new JNIMethodBlockNode(MAX2(num_addl_methods, min_block_size)); 2126 } else { 2127 _next->ensure_methods(num_addl_methods); 2128 } 2129 } 2130 }; 2131 2132 class JNIMethodBlock : public CHeapObj<mtClass> { 2133 JNIMethodBlockNode _head; 2134 JNIMethodBlockNode *_last_free; 2135 public: 2136 static Method* const _free_method; 2137 2138 JNIMethodBlock(int initial_capacity = min_block_size) 2139 : _head(initial_capacity), _last_free(&_head) {} 2140 2141 void ensure_methods(int num_addl_methods) { 2142 _last_free->ensure_methods(num_addl_methods); 2143 } 2144 2145 Method** add_method(Method* m) { 2146 for (JNIMethodBlockNode* b = _last_free; b != nullptr; b = b->_next) { 2147 if (b->_top < b->_number_of_methods) { 2148 // top points to the next free entry. 2149 int i = b->_top; 2150 b->_methods[i] = m; 2151 b->_top++; 2152 _last_free = b; 2153 return &(b->_methods[i]); 2154 } else if (b->_top == b->_number_of_methods) { 2155 // if the next free entry ran off the block see if there's a free entry 2156 for (int i = 0; i < b->_number_of_methods; i++) { 2157 if (b->_methods[i] == _free_method) { 2158 b->_methods[i] = m; 2159 _last_free = b; 2160 return &(b->_methods[i]); 2161 } 2162 } 2163 // Only check each block once for frees. They're very unlikely. 2164 // Increment top past the end of the block. 2165 b->_top++; 2166 } 2167 // need to allocate a next block. 2168 if (b->_next == nullptr) { 2169 b->_next = _last_free = new JNIMethodBlockNode(); 2170 } 2171 } 2172 guarantee(false, "Should always allocate a free block"); 2173 return nullptr; 2174 } 2175 2176 bool contains(Method** m) { 2177 if (m == nullptr) return false; 2178 for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) { 2179 if (b->_methods <= m && m < b->_methods + b->_number_of_methods) { 2180 // This is a bit of extra checking, for two reasons. One is 2181 // that contains() deals with pointers that are passed in by 2182 // JNI code, so making sure that the pointer is aligned 2183 // correctly is valuable. The other is that <= and > are 2184 // technically not defined on pointers, so the if guard can 2185 // pass spuriously; no modern compiler is likely to make that 2186 // a problem, though (and if one did, the guard could also 2187 // fail spuriously, which would be bad). 2188 ptrdiff_t idx = m - b->_methods; 2189 if (b->_methods + idx == m) { 2190 return true; 2191 } 2192 } 2193 } 2194 return false; // not found 2195 } 2196 2197 // During class unloading the methods are cleared, which is different 2198 // than freed. 2199 void clear_all_methods() { 2200 for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) { 2201 for (int i = 0; i< b->_number_of_methods; i++) { 2202 b->_methods[i] = nullptr; 2203 } 2204 } 2205 } 2206 #ifndef PRODUCT 2207 int count_methods() { 2208 // count all allocated methods 2209 int count = 0; 2210 for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) { 2211 for (int i = 0; i< b->_number_of_methods; i++) { 2212 if (b->_methods[i] != _free_method) count++; 2213 } 2214 } 2215 return count; 2216 } 2217 #endif // PRODUCT 2218 }; 2219 2220 // Something that can't be mistaken for an address or a markWord 2221 Method* const JNIMethodBlock::_free_method = (Method*)55; 2222 2223 JNIMethodBlockNode::JNIMethodBlockNode(int num_methods) : _top(0), _next(nullptr) { 2224 _number_of_methods = MAX2(num_methods, min_block_size); 2225 _methods = NEW_C_HEAP_ARRAY(Method*, _number_of_methods, mtInternal); 2226 for (int i = 0; i < _number_of_methods; i++) { 2227 _methods[i] = JNIMethodBlock::_free_method; 2228 } 2229 } 2230 2231 void Method::ensure_jmethod_ids(ClassLoaderData* cld, int capacity) { 2232 // Have to add jmethod_ids() to class loader data thread-safely. 2233 // Also have to add the method to the list safely, which the lock 2234 // protects as well. 2235 MutexLocker ml(JmethodIdCreation_lock, Mutex::_no_safepoint_check_flag); 2236 if (cld->jmethod_ids() == nullptr) { 2237 cld->set_jmethod_ids(new JNIMethodBlock(capacity)); 2238 } else { 2239 cld->jmethod_ids()->ensure_methods(capacity); 2240 } 2241 } 2242 2243 // Add a method id to the jmethod_ids 2244 jmethodID Method::make_jmethod_id(ClassLoaderData* cld, Method* m) { 2245 // Have to add jmethod_ids() to class loader data thread-safely. 2246 // Also have to add the method to the list safely, which the lock 2247 // protects as well. 2248 assert(JmethodIdCreation_lock->owned_by_self(), "sanity check"); 2249 2250 ResourceMark rm; 2251 log_debug(jmethod)("Creating jmethodID for Method %s", m->external_name()); 2252 if (cld->jmethod_ids() == nullptr) { 2253 cld->set_jmethod_ids(new JNIMethodBlock()); 2254 } 2255 // jmethodID is a pointer to Method* 2256 return (jmethodID)cld->jmethod_ids()->add_method(m); 2257 } 2258 2259 jmethodID Method::jmethod_id() { 2260 methodHandle mh(Thread::current(), this); 2261 return method_holder()->get_jmethod_id(mh); 2262 } 2263 2264 void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) { 2265 // Can't assert the method_holder is the same because the new method has the 2266 // scratch method holder. 2267 assert(resolve_jmethod_id(jmid)->method_holder()->class_loader() 2268 == new_method->method_holder()->class_loader() || 2269 new_method->method_holder()->class_loader() == nullptr, // allow Unsafe substitution 2270 "changing to a different class loader"); 2271 // Just change the method in place, jmethodID pointer doesn't change. 2272 *((Method**)jmid) = new_method; 2273 } 2274 2275 bool Method::is_method_id(jmethodID mid) { 2276 Method* m = resolve_jmethod_id(mid); 2277 assert(m != nullptr, "should be called with non-null method"); 2278 InstanceKlass* ik = m->method_holder(); 2279 ClassLoaderData* cld = ik->class_loader_data(); 2280 if (cld->jmethod_ids() == nullptr) return false; 2281 return (cld->jmethod_ids()->contains((Method**)mid)); 2282 } 2283 2284 Method* Method::checked_resolve_jmethod_id(jmethodID mid) { 2285 if (mid == nullptr) return nullptr; 2286 Method* o = resolve_jmethod_id(mid); 2287 if (o == nullptr || o == JNIMethodBlock::_free_method) { 2288 return nullptr; 2289 } 2290 // Method should otherwise be valid. Assert for testing. 2291 assert(is_valid_method(o), "should be valid jmethodid"); 2292 // If the method's class holder object is unreferenced, but not yet marked as 2293 // unloaded, we need to return null here too because after a safepoint, its memory 2294 // will be reclaimed. 2295 return o->method_holder()->is_loader_alive() ? o : nullptr; 2296 }; 2297 2298 void Method::set_on_stack(const bool value) { 2299 // Set both the method itself and its constant pool. The constant pool 2300 // on stack means some method referring to it is also on the stack. 2301 constants()->set_on_stack(value); 2302 2303 bool already_set = on_stack_flag(); 2304 set_on_stack_flag(value); 2305 if (value && !already_set) { 2306 MetadataOnStackMark::record(this); 2307 } 2308 } 2309 2310 void Method::record_gc_epoch() { 2311 // If any method is on the stack in continuations, none of them can be reclaimed, 2312 // so save the marking cycle to check for the whole class in the cpCache. 2313 // The cpCache is writeable. 2314 constants()->cache()->record_gc_epoch(); 2315 } 2316 2317 // Called when the class loader is unloaded to make all methods weak. 2318 void Method::clear_jmethod_ids(ClassLoaderData* loader_data) { 2319 loader_data->jmethod_ids()->clear_all_methods(); 2320 } 2321 2322 void Method::clear_jmethod_id() { 2323 // Being at a safepoint prevents racing against other class redefinitions 2324 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); 2325 // The jmethodID is not stored in the Method instance, we need to look it up first 2326 jmethodID methodid = find_jmethod_id_or_null(); 2327 // We need to make sure that jmethodID actually resolves to this method 2328 // - multiple redefined versions may share jmethodID slots and if a method 2329 // has already been rewired to a newer version we could be removing reference 2330 // to a still existing method instance 2331 if (methodid != nullptr && *((Method**)methodid) == this) { 2332 *((Method**)methodid) = nullptr; 2333 } 2334 } 2335 2336 bool Method::has_method_vptr(const void* ptr) { 2337 Method m; 2338 // This assumes that the vtbl pointer is the first word of a C++ object. 2339 return dereference_vptr(&m) == dereference_vptr(ptr); 2340 } 2341 2342 // Check that this pointer is valid by checking that the vtbl pointer matches 2343 bool Method::is_valid_method(const Method* m) { 2344 if (m == nullptr) { 2345 return false; 2346 } else if ((intptr_t(m) & (wordSize-1)) != 0) { 2347 // Quick sanity check on pointer. 2348 return false; 2349 } else if (!os::is_readable_range(m, m + 1)) { 2350 return false; 2351 } else if (m->is_shared()) { 2352 return CppVtables::is_valid_shared_method(m); 2353 } else if (Metaspace::contains_non_shared(m)) { 2354 return has_method_vptr((const void*)m); 2355 } else { 2356 return false; 2357 } 2358 } 2359 2360 #ifndef PRODUCT 2361 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) { 2362 out->print("%d", loader_data->jmethod_ids()->count_methods()); 2363 } 2364 #endif // PRODUCT 2365 2366 2367 // Printing 2368 2369 #ifndef PRODUCT 2370 2371 void Method::print_on(outputStream* st) const { 2372 ResourceMark rm; 2373 assert(is_method(), "must be method"); 2374 st->print_cr("%s", internal_name()); 2375 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this)); 2376 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr(); 2377 st->print (" - constants: " PTR_FORMAT " ", p2i(constants())); 2378 constants()->print_value_on(st); st->cr(); 2379 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr(); 2380 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr(); 2381 st->print (" - name: "); name()->print_value_on(st); st->cr(); 2382 st->print (" - signature: "); signature()->print_value_on(st); st->cr(); 2383 st->print_cr(" - max stack: %d", max_stack()); 2384 st->print_cr(" - max locals: %d", max_locals()); 2385 st->print_cr(" - size of params: %d", size_of_parameters()); 2386 st->print_cr(" - method size: %d", method_size()); 2387 if (intrinsic_id() != vmIntrinsics::_none) 2388 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id())); 2389 if (highest_comp_level() != CompLevel_none) 2390 st->print_cr(" - highest level: %d", highest_comp_level()); 2391 st->print_cr(" - vtable index: %d", _vtable_index); 2392 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry())); 2393 st->print( " - adapters: "); 2394 AdapterHandlerEntry* a = ((Method*)this)->adapter(); 2395 if (a == nullptr) 2396 st->print_cr(PTR_FORMAT, p2i(a)); 2397 else 2398 a->print_adapter_on(st); 2399 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry())); 2400 st->print_cr(" - code size: %d", code_size()); 2401 if (code_size() != 0) { 2402 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base())); 2403 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size())); 2404 } 2405 if (method_data() != nullptr) { 2406 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data())); 2407 } 2408 st->print_cr(" - checked ex length: %d", checked_exceptions_length()); 2409 if (checked_exceptions_length() > 0) { 2410 CheckedExceptionElement* table = checked_exceptions_start(); 2411 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table)); 2412 if (Verbose) { 2413 for (int i = 0; i < checked_exceptions_length(); i++) { 2414 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index)); 2415 } 2416 } 2417 } 2418 if (has_linenumber_table()) { 2419 u_char* table = compressed_linenumber_table(); 2420 st->print_cr(" - linenumber start: " PTR_FORMAT, p2i(table)); 2421 if (Verbose) { 2422 CompressedLineNumberReadStream stream(table); 2423 while (stream.read_pair()) { 2424 st->print_cr(" - line %d: %d", stream.line(), stream.bci()); 2425 } 2426 } 2427 } 2428 st->print_cr(" - localvar length: %d", localvariable_table_length()); 2429 if (localvariable_table_length() > 0) { 2430 LocalVariableTableElement* table = localvariable_table_start(); 2431 st->print_cr(" - localvar start: " PTR_FORMAT, p2i(table)); 2432 if (Verbose) { 2433 for (int i = 0; i < localvariable_table_length(); i++) { 2434 int bci = table[i].start_bci; 2435 int len = table[i].length; 2436 const char* name = constants()->printable_name_at(table[i].name_cp_index); 2437 const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index); 2438 int slot = table[i].slot; 2439 st->print_cr(" - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot); 2440 } 2441 } 2442 } 2443 if (code() != nullptr) { 2444 st->print (" - compiled code: "); 2445 code()->print_value_on(st); 2446 } 2447 if (is_native()) { 2448 st->print_cr(" - native function: " PTR_FORMAT, p2i(native_function())); 2449 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler())); 2450 } 2451 } 2452 2453 void Method::print_linkage_flags(outputStream* st) { 2454 access_flags().print_on(st); 2455 if (is_default_method()) { 2456 st->print("default "); 2457 } 2458 if (is_overpass()) { 2459 st->print("overpass "); 2460 } 2461 } 2462 #endif //PRODUCT 2463 2464 void Method::print_value_on(outputStream* st) const { 2465 assert(is_method(), "must be method"); 2466 st->print("%s", internal_name()); 2467 print_address_on(st); 2468 st->print(" "); 2469 name()->print_value_on(st); 2470 st->print(" "); 2471 signature()->print_value_on(st); 2472 st->print(" in "); 2473 method_holder()->print_value_on(st); 2474 if (WizardMode) st->print("#%d", _vtable_index); 2475 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals()); 2476 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code()); 2477 } 2478 2479 // Verification 2480 2481 void Method::verify_on(outputStream* st) { 2482 guarantee(is_method(), "object must be method"); 2483 guarantee(constants()->is_constantPool(), "should be constant pool"); 2484 MethodData* md = method_data(); 2485 guarantee(md == nullptr || 2486 md->is_methodData(), "should be method data"); 2487 }