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