1 /*
   2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "code/compiledIC.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/compiledMethod.inline.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "compiler/abstractCompiler.hpp"
  37 #include "compiler/compileBroker.hpp"
  38 #include "compiler/disassembler.hpp"
  39 #include "gc/shared/barrierSet.hpp"
  40 #include "gc/shared/collectedHeap.hpp"
  41 #include "gc/shared/gcLocker.inline.hpp"
  42 #include "interpreter/interpreter.hpp"
  43 #include "interpreter/interpreterRuntime.hpp"
  44 #include "jvm.h"
  45 #include "jfr/jfrEvents.hpp"
  46 #include "logging/log.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "metaprogramming/primitiveConversions.hpp"
  50 #include "oops/compiledICHolder.inline.hpp"
  51 #include "oops/klass.hpp"
  52 #include "oops/method.inline.hpp"
  53 #include "oops/objArrayKlass.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "prims/forte.hpp"
  56 #include "prims/jvmtiExport.hpp"
  57 #include "prims/jvmtiThreadState.hpp"
  58 #include "prims/methodHandles.hpp"
  59 #include "prims/nativeLookup.hpp"
  60 #include "runtime/atomic.hpp"
  61 #include "runtime/frame.inline.hpp"
  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/init.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/java.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/jniHandles.inline.hpp"
  68 #include "runtime/sharedRuntime.hpp"
  69 #include "runtime/stackWatermarkSet.hpp"
  70 #include "runtime/stubRoutines.hpp"
  71 #include "runtime/synchronizer.hpp"
  72 #include "runtime/vframe.inline.hpp"
  73 #include "runtime/vframeArray.hpp"
  74 #include "runtime/vm_version.hpp"
  75 #include "utilities/copy.hpp"
  76 #include "utilities/dtrace.hpp"
  77 #include "utilities/events.hpp"
  78 #include "utilities/resourceHash.hpp"
  79 #include "utilities/macros.hpp"
  80 #include "utilities/xmlstream.hpp"
  81 #ifdef COMPILER1
  82 #include "c1/c1_Runtime1.hpp"
  83 #endif
  84 
  85 // Shared stub locations
  86 RuntimeStub*        SharedRuntime::_wrong_method_blob;
  87 RuntimeStub*        SharedRuntime::_wrong_method_abstract_blob;
  88 RuntimeStub*        SharedRuntime::_ic_miss_blob;
  89 RuntimeStub*        SharedRuntime::_resolve_opt_virtual_call_blob;
  90 RuntimeStub*        SharedRuntime::_resolve_virtual_call_blob;
  91 RuntimeStub*        SharedRuntime::_resolve_static_call_blob;
  92 address             SharedRuntime::_resolve_static_call_entry;
  93 
  94 DeoptimizationBlob* SharedRuntime::_deopt_blob;
  95 SafepointBlob*      SharedRuntime::_polling_page_vectors_safepoint_handler_blob;
  96 SafepointBlob*      SharedRuntime::_polling_page_safepoint_handler_blob;
  97 SafepointBlob*      SharedRuntime::_polling_page_return_handler_blob;
  98 
  99 #ifdef COMPILER2
 100 UncommonTrapBlob*   SharedRuntime::_uncommon_trap_blob;
 101 #endif // COMPILER2
 102 
 103 nmethod*            SharedRuntime::_cont_doYield_stub;
 104 
 105 //----------------------------generate_stubs-----------------------------------
 106 void SharedRuntime::generate_stubs() {
 107   _wrong_method_blob                   = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::handle_wrong_method),          "wrong_method_stub");
 108   _wrong_method_abstract_blob          = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::handle_wrong_method_abstract), "wrong_method_abstract_stub");
 109   _ic_miss_blob                        = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::handle_wrong_method_ic_miss),  "ic_miss_stub");
 110   _resolve_opt_virtual_call_blob       = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_opt_virtual_call_C),   "resolve_opt_virtual_call");
 111   _resolve_virtual_call_blob           = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_virtual_call_C),       "resolve_virtual_call");
 112   _resolve_static_call_blob            = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_static_call_C),        "resolve_static_call");
 113   _resolve_static_call_entry           = _resolve_static_call_blob->entry_point();
 114 
 115   AdapterHandlerLibrary::initialize();
 116 
 117 #if COMPILER2_OR_JVMCI
 118   // Vectors are generated only by C2 and JVMCI.
 119   bool support_wide = is_wide_vector(MaxVectorSize);
 120   if (support_wide) {
 121     _polling_page_vectors_safepoint_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), POLL_AT_VECTOR_LOOP);
 122   }
 123 #endif // COMPILER2_OR_JVMCI
 124   _polling_page_safepoint_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), POLL_AT_LOOP);
 125   _polling_page_return_handler_blob    = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), POLL_AT_RETURN);
 126 
 127   generate_deopt_blob();
 128 
 129 #ifdef COMPILER2
 130   generate_uncommon_trap_blob();
 131 #endif // COMPILER2
 132 }
 133 
 134 #include <math.h>
 135 
 136 // Implementation of SharedRuntime
 137 
 138 #ifndef PRODUCT
 139 // For statistics
 140 int SharedRuntime::_ic_miss_ctr = 0;
 141 int SharedRuntime::_wrong_method_ctr = 0;
 142 int SharedRuntime::_resolve_static_ctr = 0;
 143 int SharedRuntime::_resolve_virtual_ctr = 0;
 144 int SharedRuntime::_resolve_opt_virtual_ctr = 0;
 145 int SharedRuntime::_implicit_null_throws = 0;
 146 int SharedRuntime::_implicit_div0_throws = 0;
 147 
 148 int64_t SharedRuntime::_nof_normal_calls = 0;
 149 int64_t SharedRuntime::_nof_inlined_calls = 0;
 150 int64_t SharedRuntime::_nof_megamorphic_calls = 0;
 151 int64_t SharedRuntime::_nof_static_calls = 0;
 152 int64_t SharedRuntime::_nof_inlined_static_calls = 0;
 153 int64_t SharedRuntime::_nof_interface_calls = 0;
 154 int64_t SharedRuntime::_nof_inlined_interface_calls = 0;
 155 
 156 int SharedRuntime::_new_instance_ctr=0;
 157 int SharedRuntime::_new_array_ctr=0;
 158 int SharedRuntime::_multi2_ctr=0;
 159 int SharedRuntime::_multi3_ctr=0;
 160 int SharedRuntime::_multi4_ctr=0;
 161 int SharedRuntime::_multi5_ctr=0;
 162 int SharedRuntime::_mon_enter_stub_ctr=0;
 163 int SharedRuntime::_mon_exit_stub_ctr=0;
 164 int SharedRuntime::_mon_enter_ctr=0;
 165 int SharedRuntime::_mon_exit_ctr=0;
 166 int SharedRuntime::_partial_subtype_ctr=0;
 167 int SharedRuntime::_jbyte_array_copy_ctr=0;
 168 int SharedRuntime::_jshort_array_copy_ctr=0;
 169 int SharedRuntime::_jint_array_copy_ctr=0;
 170 int SharedRuntime::_jlong_array_copy_ctr=0;
 171 int SharedRuntime::_oop_array_copy_ctr=0;
 172 int SharedRuntime::_checkcast_array_copy_ctr=0;
 173 int SharedRuntime::_unsafe_array_copy_ctr=0;
 174 int SharedRuntime::_generic_array_copy_ctr=0;
 175 int SharedRuntime::_slow_array_copy_ctr=0;
 176 int SharedRuntime::_find_handler_ctr=0;
 177 int SharedRuntime::_rethrow_ctr=0;
 178 
 179 int     SharedRuntime::_ICmiss_index                    = 0;
 180 int     SharedRuntime::_ICmiss_count[SharedRuntime::maxICmiss_count];
 181 address SharedRuntime::_ICmiss_at[SharedRuntime::maxICmiss_count];
 182 
 183 
 184 void SharedRuntime::trace_ic_miss(address at) {
 185   for (int i = 0; i < _ICmiss_index; i++) {
 186     if (_ICmiss_at[i] == at) {
 187       _ICmiss_count[i]++;
 188       return;
 189     }
 190   }
 191   int index = _ICmiss_index++;
 192   if (_ICmiss_index >= maxICmiss_count) _ICmiss_index = maxICmiss_count - 1;
 193   _ICmiss_at[index] = at;
 194   _ICmiss_count[index] = 1;
 195 }
 196 
 197 void SharedRuntime::print_ic_miss_histogram() {
 198   if (ICMissHistogram) {
 199     tty->print_cr("IC Miss Histogram:");
 200     int tot_misses = 0;
 201     for (int i = 0; i < _ICmiss_index; i++) {
 202       tty->print_cr("  at: " INTPTR_FORMAT "  nof: %d", p2i(_ICmiss_at[i]), _ICmiss_count[i]);
 203       tot_misses += _ICmiss_count[i];
 204     }
 205     tty->print_cr("Total IC misses: %7d", tot_misses);
 206   }
 207 }
 208 #endif // PRODUCT
 209 
 210 
 211 JRT_LEAF(jlong, SharedRuntime::lmul(jlong y, jlong x))
 212   return x * y;
 213 JRT_END
 214 
 215 
 216 JRT_LEAF(jlong, SharedRuntime::ldiv(jlong y, jlong x))
 217   if (x == min_jlong && y == CONST64(-1)) {
 218     return x;
 219   } else {
 220     return x / y;
 221   }
 222 JRT_END
 223 
 224 
 225 JRT_LEAF(jlong, SharedRuntime::lrem(jlong y, jlong x))
 226   if (x == min_jlong && y == CONST64(-1)) {
 227     return 0;
 228   } else {
 229     return x % y;
 230   }
 231 JRT_END
 232 
 233 
 234 #ifdef _WIN64
 235 const juint  float_sign_mask  = 0x7FFFFFFF;
 236 const juint  float_infinity   = 0x7F800000;
 237 const julong double_sign_mask = CONST64(0x7FFFFFFFFFFFFFFF);
 238 const julong double_infinity  = CONST64(0x7FF0000000000000);
 239 #endif
 240 
 241 #if !defined(X86) || !defined(TARGET_COMPILER_gcc) || defined(_WIN64)
 242 JRT_LEAF(jfloat, SharedRuntime::frem(jfloat x, jfloat y))
 243 #ifdef _WIN64
 244   // 64-bit Windows on amd64 returns the wrong values for
 245   // infinity operands.
 246   juint xbits = PrimitiveConversions::cast<juint>(x);
 247   juint ybits = PrimitiveConversions::cast<juint>(y);
 248   // x Mod Infinity == x unless x is infinity
 249   if (((xbits & float_sign_mask) != float_infinity) &&
 250        ((ybits & float_sign_mask) == float_infinity) ) {
 251     return x;
 252   }
 253   return ((jfloat)fmod_winx64((double)x, (double)y));
 254 #else
 255   return ((jfloat)fmod((double)x,(double)y));
 256 #endif
 257 JRT_END
 258 
 259 JRT_LEAF(jdouble, SharedRuntime::drem(jdouble x, jdouble y))
 260 #ifdef _WIN64
 261   julong xbits = PrimitiveConversions::cast<julong>(x);
 262   julong ybits = PrimitiveConversions::cast<julong>(y);
 263   // x Mod Infinity == x unless x is infinity
 264   if (((xbits & double_sign_mask) != double_infinity) &&
 265        ((ybits & double_sign_mask) == double_infinity) ) {
 266     return x;
 267   }
 268   return ((jdouble)fmod_winx64((double)x, (double)y));
 269 #else
 270   return ((jdouble)fmod((double)x,(double)y));
 271 #endif
 272 JRT_END
 273 #endif // !X86 || !TARGET_COMPILER_gcc || _WIN64
 274 
 275 JRT_LEAF(jfloat, SharedRuntime::i2f(jint x))
 276   return (jfloat)x;
 277 JRT_END
 278 
 279 #ifdef __SOFTFP__
 280 JRT_LEAF(jfloat, SharedRuntime::fadd(jfloat x, jfloat y))
 281   return x + y;
 282 JRT_END
 283 
 284 JRT_LEAF(jfloat, SharedRuntime::fsub(jfloat x, jfloat y))
 285   return x - y;
 286 JRT_END
 287 
 288 JRT_LEAF(jfloat, SharedRuntime::fmul(jfloat x, jfloat y))
 289   return x * y;
 290 JRT_END
 291 
 292 JRT_LEAF(jfloat, SharedRuntime::fdiv(jfloat x, jfloat y))
 293   return x / y;
 294 JRT_END
 295 
 296 JRT_LEAF(jdouble, SharedRuntime::dadd(jdouble x, jdouble y))
 297   return x + y;
 298 JRT_END
 299 
 300 JRT_LEAF(jdouble, SharedRuntime::dsub(jdouble x, jdouble y))
 301   return x - y;
 302 JRT_END
 303 
 304 JRT_LEAF(jdouble, SharedRuntime::dmul(jdouble x, jdouble y))
 305   return x * y;
 306 JRT_END
 307 
 308 JRT_LEAF(jdouble, SharedRuntime::ddiv(jdouble x, jdouble y))
 309   return x / y;
 310 JRT_END
 311 
 312 JRT_LEAF(jdouble, SharedRuntime::i2d(jint x))
 313   return (jdouble)x;
 314 JRT_END
 315 
 316 JRT_LEAF(jdouble, SharedRuntime::f2d(jfloat x))
 317   return (jdouble)x;
 318 JRT_END
 319 
 320 JRT_LEAF(int,  SharedRuntime::fcmpl(float x, float y))
 321   return x>y ? 1 : (x==y ? 0 : -1);  /* x<y or is_nan*/
 322 JRT_END
 323 
 324 JRT_LEAF(int,  SharedRuntime::fcmpg(float x, float y))
 325   return x<y ? -1 : (x==y ? 0 : 1);  /* x>y or is_nan */
 326 JRT_END
 327 
 328 JRT_LEAF(int,  SharedRuntime::dcmpl(double x, double y))
 329   return x>y ? 1 : (x==y ? 0 : -1); /* x<y or is_nan */
 330 JRT_END
 331 
 332 JRT_LEAF(int,  SharedRuntime::dcmpg(double x, double y))
 333   return x<y ? -1 : (x==y ? 0 : 1);  /* x>y or is_nan */
 334 JRT_END
 335 
 336 // Functions to return the opposite of the aeabi functions for nan.
 337 JRT_LEAF(int, SharedRuntime::unordered_fcmplt(float x, float y))
 338   return (x < y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 339 JRT_END
 340 
 341 JRT_LEAF(int, SharedRuntime::unordered_dcmplt(double x, double y))
 342   return (x < y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 343 JRT_END
 344 
 345 JRT_LEAF(int, SharedRuntime::unordered_fcmple(float x, float y))
 346   return (x <= y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 347 JRT_END
 348 
 349 JRT_LEAF(int, SharedRuntime::unordered_dcmple(double x, double y))
 350   return (x <= y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 351 JRT_END
 352 
 353 JRT_LEAF(int, SharedRuntime::unordered_fcmpge(float x, float y))
 354   return (x >= y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 355 JRT_END
 356 
 357 JRT_LEAF(int, SharedRuntime::unordered_dcmpge(double x, double y))
 358   return (x >= y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 359 JRT_END
 360 
 361 JRT_LEAF(int, SharedRuntime::unordered_fcmpgt(float x, float y))
 362   return (x > y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 363 JRT_END
 364 
 365 JRT_LEAF(int, SharedRuntime::unordered_dcmpgt(double x, double y))
 366   return (x > y) ? 1 : ((g_isnan(x) || g_isnan(y)) ? 1 : 0);
 367 JRT_END
 368 
 369 // Intrinsics make gcc generate code for these.
 370 float  SharedRuntime::fneg(float f)   {
 371   return -f;
 372 }
 373 
 374 double SharedRuntime::dneg(double f)  {
 375   return -f;
 376 }
 377 
 378 #endif // __SOFTFP__
 379 
 380 #if defined(__SOFTFP__) || defined(E500V2)
 381 // Intrinsics make gcc generate code for these.
 382 double SharedRuntime::dabs(double f)  {
 383   return (f <= (double)0.0) ? (double)0.0 - f : f;
 384 }
 385 
 386 #endif
 387 
 388 #if defined(__SOFTFP__) || defined(PPC)
 389 double SharedRuntime::dsqrt(double f) {
 390   return sqrt(f);
 391 }
 392 #endif
 393 
 394 JRT_LEAF(jint, SharedRuntime::f2i(jfloat  x))
 395   if (g_isnan(x))
 396     return 0;
 397   if (x >= (jfloat) max_jint)
 398     return max_jint;
 399   if (x <= (jfloat) min_jint)
 400     return min_jint;
 401   return (jint) x;
 402 JRT_END
 403 
 404 
 405 JRT_LEAF(jlong, SharedRuntime::f2l(jfloat  x))
 406   if (g_isnan(x))
 407     return 0;
 408   if (x >= (jfloat) max_jlong)
 409     return max_jlong;
 410   if (x <= (jfloat) min_jlong)
 411     return min_jlong;
 412   return (jlong) x;
 413 JRT_END
 414 
 415 
 416 JRT_LEAF(jint, SharedRuntime::d2i(jdouble x))
 417   if (g_isnan(x))
 418     return 0;
 419   if (x >= (jdouble) max_jint)
 420     return max_jint;
 421   if (x <= (jdouble) min_jint)
 422     return min_jint;
 423   return (jint) x;
 424 JRT_END
 425 
 426 
 427 JRT_LEAF(jlong, SharedRuntime::d2l(jdouble x))
 428   if (g_isnan(x))
 429     return 0;
 430   if (x >= (jdouble) max_jlong)
 431     return max_jlong;
 432   if (x <= (jdouble) min_jlong)
 433     return min_jlong;
 434   return (jlong) x;
 435 JRT_END
 436 
 437 
 438 JRT_LEAF(jfloat, SharedRuntime::d2f(jdouble x))
 439   return (jfloat)x;
 440 JRT_END
 441 
 442 
 443 JRT_LEAF(jfloat, SharedRuntime::l2f(jlong x))
 444   return (jfloat)x;
 445 JRT_END
 446 
 447 
 448 JRT_LEAF(jdouble, SharedRuntime::l2d(jlong x))
 449   return (jdouble)x;
 450 JRT_END
 451 
 452 
 453 // Exception handling across interpreter/compiler boundaries
 454 //
 455 // exception_handler_for_return_address(...) returns the continuation address.
 456 // The continuation address is the entry point of the exception handler of the
 457 // previous frame depending on the return address.
 458 
 459 address SharedRuntime::raw_exception_handler_for_return_address(JavaThread* current, address return_address) {
 460   // Note: This is called when we have unwound the frame of the callee that did
 461   // throw an exception. So far, no check has been performed by the StackWatermarkSet.
 462   // Notably, the stack is not walkable at this point, and hence the check must
 463   // be deferred until later. Specifically, any of the handlers returned here in
 464   // this function, will get dispatched to, and call deferred checks to
 465   // StackWatermarkSet::after_unwind at a point where the stack is walkable.
 466   assert(frame::verify_return_pc(return_address), "must be a return address: " INTPTR_FORMAT, p2i(return_address));
 467   assert(current->frames_to_pop_failed_realloc() == 0 || Interpreter::contains(return_address), "missed frames to pop?");
 468 
 469   // Reset method handle flag.
 470   current->set_is_method_handle_return(false);
 471 
 472 #if INCLUDE_JVMCI
 473   // JVMCI's ExceptionHandlerStub expects the thread local exception PC to be clear
 474   // and other exception handler continuations do not read it
 475   current->set_exception_pc(nullptr);
 476 #endif // INCLUDE_JVMCI
 477 
 478   if (Continuation::is_return_barrier_entry(return_address)) {
 479     return StubRoutines::cont_returnBarrierExc();
 480   }
 481 
 482   // write lock needed because we might update the pc desc cache via PcDescCache::add_pc_desc
 483   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
 484 
 485   // The fastest case first
 486   CodeBlob* blob = CodeCache::find_blob(return_address);
 487   CompiledMethod* nm = (blob != nullptr) ? blob->as_compiled_method_or_null() : nullptr;
 488   if (nm != nullptr) {
 489     // Set flag if return address is a method handle call site.
 490     current->set_is_method_handle_return(nm->is_method_handle_return(return_address));
 491     // native nmethods don't have exception handlers
 492     assert(!nm->is_native_method() || nm->method()->is_continuation_enter_intrinsic(), "no exception handler");
 493     assert(nm->header_begin() != nm->exception_begin(), "no exception handler");
 494     if (nm->is_deopt_pc(return_address)) {
 495       // If we come here because of a stack overflow, the stack may be
 496       // unguarded. Reguard the stack otherwise if we return to the
 497       // deopt blob and the stack bang causes a stack overflow we
 498       // crash.
 499       StackOverflow* overflow_state = current->stack_overflow_state();
 500       bool guard_pages_enabled = overflow_state->reguard_stack_if_needed();
 501       if (overflow_state->reserved_stack_activation() != current->stack_base()) {
 502         overflow_state->set_reserved_stack_activation(current->stack_base());
 503       }
 504       assert(guard_pages_enabled, "stack banging in deopt blob may cause crash");
 505       // The deferred StackWatermarkSet::after_unwind check will be performed in
 506       // Deoptimization::fetch_unroll_info (with exec_mode == Unpack_exception)
 507       return SharedRuntime::deopt_blob()->unpack_with_exception();
 508     } else {
 509       // The deferred StackWatermarkSet::after_unwind check will be performed in
 510       // * OptoRuntime::handle_exception_C_helper for C2 code
 511       // * exception_handler_for_pc_helper via Runtime1::handle_exception_from_callee_id for C1 code
 512       return nm->exception_begin();
 513     }
 514   }
 515 
 516   // Entry code
 517   if (StubRoutines::returns_to_call_stub(return_address)) {
 518     // The deferred StackWatermarkSet::after_unwind check will be performed in
 519     // JavaCallWrapper::~JavaCallWrapper
 520     return StubRoutines::catch_exception_entry();
 521   }
 522   if (blob != nullptr && blob->is_upcall_stub()) {
 523     return ((UpcallStub*)blob)->exception_handler();
 524   }
 525   // Interpreted code
 526   if (Interpreter::contains(return_address)) {
 527     // The deferred StackWatermarkSet::after_unwind check will be performed in
 528     // InterpreterRuntime::exception_handler_for_exception
 529     return Interpreter::rethrow_exception_entry();
 530   }
 531 
 532   guarantee(blob == nullptr || !blob->is_runtime_stub(), "caller should have skipped stub");
 533   guarantee(!VtableStubs::contains(return_address), "null exceptions in vtables should have been handled already!");
 534 
 535 #ifndef PRODUCT
 536   { ResourceMark rm;
 537     tty->print_cr("No exception handler found for exception at " INTPTR_FORMAT " - potential problems:", p2i(return_address));
 538     os::print_location(tty, (intptr_t)return_address);
 539     tty->print_cr("a) exception happened in (new?) code stubs/buffers that is not handled here");
 540     tty->print_cr("b) other problem");
 541   }
 542 #endif // PRODUCT
 543 
 544   ShouldNotReachHere();
 545   return nullptr;
 546 }
 547 
 548 
 549 JRT_LEAF(address, SharedRuntime::exception_handler_for_return_address(JavaThread* current, address return_address))
 550   return raw_exception_handler_for_return_address(current, return_address);
 551 JRT_END
 552 
 553 
 554 address SharedRuntime::get_poll_stub(address pc) {
 555   address stub;
 556   // Look up the code blob
 557   CodeBlob *cb = CodeCache::find_blob(pc);
 558 
 559   // Should be an nmethod
 560   guarantee(cb != nullptr && cb->is_compiled(), "safepoint polling: pc must refer to an nmethod");
 561 
 562   // Look up the relocation information
 563   assert(((CompiledMethod*)cb)->is_at_poll_or_poll_return(pc),
 564       "safepoint polling: type must be poll at pc " INTPTR_FORMAT, p2i(pc));
 565 
 566 #ifdef ASSERT
 567   if (!((NativeInstruction*)pc)->is_safepoint_poll()) {
 568     tty->print_cr("bad pc: " PTR_FORMAT, p2i(pc));
 569     Disassembler::decode(cb);
 570     fatal("Only polling locations are used for safepoint");
 571   }
 572 #endif
 573 
 574   bool at_poll_return = ((CompiledMethod*)cb)->is_at_poll_return(pc);
 575   bool has_wide_vectors = ((CompiledMethod*)cb)->has_wide_vectors();
 576   if (at_poll_return) {
 577     assert(SharedRuntime::polling_page_return_handler_blob() != nullptr,
 578            "polling page return stub not created yet");
 579     stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
 580   } else if (has_wide_vectors) {
 581     assert(SharedRuntime::polling_page_vectors_safepoint_handler_blob() != nullptr,
 582            "polling page vectors safepoint stub not created yet");
 583     stub = SharedRuntime::polling_page_vectors_safepoint_handler_blob()->entry_point();
 584   } else {
 585     assert(SharedRuntime::polling_page_safepoint_handler_blob() != nullptr,
 586            "polling page safepoint stub not created yet");
 587     stub = SharedRuntime::polling_page_safepoint_handler_blob()->entry_point();
 588   }
 589   log_debug(safepoint)("... found polling page %s exception at pc = "
 590                        INTPTR_FORMAT ", stub =" INTPTR_FORMAT,
 591                        at_poll_return ? "return" : "loop",
 592                        (intptr_t)pc, (intptr_t)stub);
 593   return stub;
 594 }
 595 
 596 void SharedRuntime::throw_and_post_jvmti_exception(JavaThread* current, Handle h_exception) {
 597   if (JvmtiExport::can_post_on_exceptions()) {
 598     vframeStream vfst(current, true);
 599     methodHandle method = methodHandle(current, vfst.method());
 600     address bcp = method()->bcp_from(vfst.bci());
 601     JvmtiExport::post_exception_throw(current, method(), bcp, h_exception());
 602   }
 603 
 604 #if INCLUDE_JVMCI
 605   if (EnableJVMCI && UseJVMCICompiler) {
 606     vframeStream vfst(current, true);
 607     methodHandle method = methodHandle(current, vfst.method());
 608     int bci = vfst.bci();
 609     MethodData* trap_mdo = method->method_data();
 610     if (trap_mdo != nullptr) {
 611       // Set exception_seen if the exceptional bytecode is an invoke
 612       Bytecode_invoke call = Bytecode_invoke_check(method, bci);
 613       if (call.is_valid()) {
 614         ResourceMark rm(current);
 615         ProfileData* pdata = trap_mdo->allocate_bci_to_data(bci, nullptr);
 616         if (pdata != nullptr && pdata->is_BitData()) {
 617           BitData* bit_data = (BitData*) pdata;
 618           bit_data->set_exception_seen();
 619         }
 620       }
 621     }
 622   }
 623 #endif
 624 
 625   Exceptions::_throw(current, __FILE__, __LINE__, h_exception);
 626 }
 627 
 628 void SharedRuntime::throw_and_post_jvmti_exception(JavaThread* current, Symbol* name, const char *message) {
 629   Handle h_exception = Exceptions::new_exception(current, name, message);
 630   throw_and_post_jvmti_exception(current, h_exception);
 631 }
 632 
 633 #if INCLUDE_JVMTI
 634 JRT_ENTRY(void, SharedRuntime::notify_jvmti_vthread_start(oopDesc* vt, jboolean hide, JavaThread* current))
 635   assert(hide == JNI_FALSE, "must be VTMS transition finish");
 636   jobject vthread = JNIHandles::make_local(const_cast<oopDesc*>(vt));
 637   JvmtiVTMSTransitionDisabler::VTMS_vthread_start(vthread);
 638   JNIHandles::destroy_local(vthread);
 639 JRT_END
 640 
 641 JRT_ENTRY(void, SharedRuntime::notify_jvmti_vthread_end(oopDesc* vt, jboolean hide, JavaThread* current))
 642   assert(hide == JNI_TRUE, "must be VTMS transition start");
 643   jobject vthread = JNIHandles::make_local(const_cast<oopDesc*>(vt));
 644   JvmtiVTMSTransitionDisabler::VTMS_vthread_end(vthread);
 645   JNIHandles::destroy_local(vthread);
 646 JRT_END
 647 
 648 JRT_ENTRY(void, SharedRuntime::notify_jvmti_vthread_mount(oopDesc* vt, jboolean hide, JavaThread* current))
 649   jobject vthread = JNIHandles::make_local(const_cast<oopDesc*>(vt));
 650   JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(vthread, hide);
 651   JNIHandles::destroy_local(vthread);
 652 JRT_END
 653 
 654 JRT_ENTRY(void, SharedRuntime::notify_jvmti_vthread_unmount(oopDesc* vt, jboolean hide, JavaThread* current))
 655   jobject vthread = JNIHandles::make_local(const_cast<oopDesc*>(vt));
 656   JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(vthread, hide);
 657   JNIHandles::destroy_local(vthread);
 658 JRT_END
 659 #endif // INCLUDE_JVMTI
 660 
 661 // The interpreter code to call this tracing function is only
 662 // called/generated when UL is on for redefine, class and has the right level
 663 // and tags. Since obsolete methods are never compiled, we don't have
 664 // to modify the compilers to generate calls to this function.
 665 //
 666 JRT_LEAF(int, SharedRuntime::rc_trace_method_entry(
 667     JavaThread* thread, Method* method))
 668   if (method->is_obsolete()) {
 669     // We are calling an obsolete method, but this is not necessarily
 670     // an error. Our method could have been redefined just after we
 671     // fetched the Method* from the constant pool.
 672     ResourceMark rm;
 673     log_trace(redefine, class, obsolete)("calling obsolete method '%s'", method->name_and_sig_as_C_string());
 674   }
 675   return 0;
 676 JRT_END
 677 
 678 // ret_pc points into caller; we are returning caller's exception handler
 679 // for given exception
 680 address SharedRuntime::compute_compiled_exc_handler(CompiledMethod* cm, address ret_pc, Handle& exception,
 681                                                     bool force_unwind, bool top_frame_only, bool& recursive_exception_occurred) {
 682   assert(cm != nullptr, "must exist");
 683   ResourceMark rm;
 684 
 685 #if INCLUDE_JVMCI
 686   if (cm->is_compiled_by_jvmci()) {
 687     // lookup exception handler for this pc
 688     int catch_pco = ret_pc - cm->code_begin();
 689     ExceptionHandlerTable table(cm);
 690     HandlerTableEntry *t = table.entry_for(catch_pco, -1, 0);
 691     if (t != nullptr) {
 692       return cm->code_begin() + t->pco();
 693     } else {
 694       return Deoptimization::deoptimize_for_missing_exception_handler(cm);
 695     }
 696   }
 697 #endif // INCLUDE_JVMCI
 698 
 699   nmethod* nm = cm->as_nmethod();
 700   ScopeDesc* sd = nm->scope_desc_at(ret_pc);
 701   // determine handler bci, if any
 702   EXCEPTION_MARK;
 703 
 704   int handler_bci = -1;
 705   int scope_depth = 0;
 706   if (!force_unwind) {
 707     int bci = sd->bci();
 708     bool recursive_exception = false;
 709     do {
 710       bool skip_scope_increment = false;
 711       // exception handler lookup
 712       Klass* ek = exception->klass();
 713       methodHandle mh(THREAD, sd->method());
 714       handler_bci = Method::fast_exception_handler_bci_for(mh, ek, bci, THREAD);
 715       if (HAS_PENDING_EXCEPTION) {
 716         recursive_exception = true;
 717         // We threw an exception while trying to find the exception handler.
 718         // Transfer the new exception to the exception handle which will
 719         // be set into thread local storage, and do another lookup for an
 720         // exception handler for this exception, this time starting at the
 721         // BCI of the exception handler which caused the exception to be
 722         // thrown (bugs 4307310 and 4546590). Set "exception" reference
 723         // argument to ensure that the correct exception is thrown (4870175).
 724         recursive_exception_occurred = true;
 725         exception = Handle(THREAD, PENDING_EXCEPTION);
 726         CLEAR_PENDING_EXCEPTION;
 727         if (handler_bci >= 0) {
 728           bci = handler_bci;
 729           handler_bci = -1;
 730           skip_scope_increment = true;
 731         }
 732       }
 733       else {
 734         recursive_exception = false;
 735       }
 736       if (!top_frame_only && handler_bci < 0 && !skip_scope_increment) {
 737         sd = sd->sender();
 738         if (sd != nullptr) {
 739           bci = sd->bci();
 740         }
 741         ++scope_depth;
 742       }
 743     } while (recursive_exception || (!top_frame_only && handler_bci < 0 && sd != nullptr));
 744   }
 745 
 746   // found handling method => lookup exception handler
 747   int catch_pco = ret_pc - nm->code_begin();
 748 
 749   ExceptionHandlerTable table(nm);
 750   HandlerTableEntry *t = table.entry_for(catch_pco, handler_bci, scope_depth);
 751   if (t == nullptr && (nm->is_compiled_by_c1() || handler_bci != -1)) {
 752     // Allow abbreviated catch tables.  The idea is to allow a method
 753     // to materialize its exceptions without committing to the exact
 754     // routing of exceptions.  In particular this is needed for adding
 755     // a synthetic handler to unlock monitors when inlining
 756     // synchronized methods since the unlock path isn't represented in
 757     // the bytecodes.
 758     t = table.entry_for(catch_pco, -1, 0);
 759   }
 760 
 761 #ifdef COMPILER1
 762   if (t == nullptr && nm->is_compiled_by_c1()) {
 763     assert(nm->unwind_handler_begin() != nullptr, "");
 764     return nm->unwind_handler_begin();
 765   }
 766 #endif
 767 
 768   if (t == nullptr) {
 769     ttyLocker ttyl;
 770     tty->print_cr("MISSING EXCEPTION HANDLER for pc " INTPTR_FORMAT " and handler bci %d, catch_pco: %d", p2i(ret_pc), handler_bci, catch_pco);
 771     tty->print_cr("   Exception:");
 772     exception->print();
 773     tty->cr();
 774     tty->print_cr(" Compiled exception table :");
 775     table.print();
 776     nm->print();
 777     nm->print_code();
 778     guarantee(false, "missing exception handler");
 779     return nullptr;
 780   }
 781 
 782   return nm->code_begin() + t->pco();
 783 }
 784 
 785 JRT_ENTRY(void, SharedRuntime::throw_AbstractMethodError(JavaThread* current))
 786   // These errors occur only at call sites
 787   throw_and_post_jvmti_exception(current, vmSymbols::java_lang_AbstractMethodError());
 788 JRT_END
 789 
 790 JRT_ENTRY(void, SharedRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 791   // These errors occur only at call sites
 792   throw_and_post_jvmti_exception(current, vmSymbols::java_lang_IncompatibleClassChangeError(), "vtable stub");
 793 JRT_END
 794 
 795 JRT_ENTRY(void, SharedRuntime::throw_ArithmeticException(JavaThread* current))
 796   throw_and_post_jvmti_exception(current, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
 797 JRT_END
 798 
 799 JRT_ENTRY(void, SharedRuntime::throw_NullPointerException(JavaThread* current))
 800   throw_and_post_jvmti_exception(current, vmSymbols::java_lang_NullPointerException(), nullptr);
 801 JRT_END
 802 
 803 JRT_ENTRY(void, SharedRuntime::throw_NullPointerException_at_call(JavaThread* current))
 804   // This entry point is effectively only used for NullPointerExceptions which occur at inline
 805   // cache sites (when the callee activation is not yet set up) so we are at a call site
 806   throw_and_post_jvmti_exception(current, vmSymbols::java_lang_NullPointerException(), nullptr);
 807 JRT_END
 808 
 809 JRT_ENTRY(void, SharedRuntime::throw_StackOverflowError(JavaThread* current))
 810   throw_StackOverflowError_common(current, false);
 811 JRT_END
 812 
 813 JRT_ENTRY(void, SharedRuntime::throw_delayed_StackOverflowError(JavaThread* current))
 814   throw_StackOverflowError_common(current, true);
 815 JRT_END
 816 
 817 void SharedRuntime::throw_StackOverflowError_common(JavaThread* current, bool delayed) {
 818   // We avoid using the normal exception construction in this case because
 819   // it performs an upcall to Java, and we're already out of stack space.
 820   JavaThread* THREAD = current; // For exception macros.
 821   Klass* k = vmClasses::StackOverflowError_klass();
 822   oop exception_oop = InstanceKlass::cast(k)->allocate_instance(CHECK);
 823   if (delayed) {
 824     java_lang_Throwable::set_message(exception_oop,
 825                                      Universe::delayed_stack_overflow_error_message());
 826   }
 827   Handle exception (current, exception_oop);
 828   if (StackTraceInThrowable) {
 829     java_lang_Throwable::fill_in_stack_trace(exception);
 830   }
 831   // Remove the ScopedValue bindings in case we got a
 832   // StackOverflowError while we were trying to remove ScopedValue
 833   // bindings.
 834   current->clear_scopedValueBindings();
 835   // Increment counter for hs_err file reporting
 836   Atomic::inc(&Exceptions::_stack_overflow_errors);
 837   throw_and_post_jvmti_exception(current, exception);
 838 }
 839 
 840 address SharedRuntime::continuation_for_implicit_exception(JavaThread* current,
 841                                                            address pc,
 842                                                            ImplicitExceptionKind exception_kind)
 843 {
 844   address target_pc = nullptr;
 845 
 846   if (Interpreter::contains(pc)) {
 847     switch (exception_kind) {
 848       case IMPLICIT_NULL:           return Interpreter::throw_NullPointerException_entry();
 849       case IMPLICIT_DIVIDE_BY_ZERO: return Interpreter::throw_ArithmeticException_entry();
 850       case STACK_OVERFLOW:          return Interpreter::throw_StackOverflowError_entry();
 851       default:                      ShouldNotReachHere();
 852     }
 853   } else {
 854     switch (exception_kind) {
 855       case STACK_OVERFLOW: {
 856         // Stack overflow only occurs upon frame setup; the callee is
 857         // going to be unwound. Dispatch to a shared runtime stub
 858         // which will cause the StackOverflowError to be fabricated
 859         // and processed.
 860         // Stack overflow should never occur during deoptimization:
 861         // the compiled method bangs the stack by as much as the
 862         // interpreter would need in case of a deoptimization. The
 863         // deoptimization blob and uncommon trap blob bang the stack
 864         // in a debug VM to verify the correctness of the compiled
 865         // method stack banging.
 866         assert(current->deopt_mark() == nullptr, "no stack overflow from deopt blob/uncommon trap");
 867         Events::log_exception(current, "StackOverflowError at " INTPTR_FORMAT, p2i(pc));
 868         return StubRoutines::throw_StackOverflowError_entry();
 869       }
 870 
 871       case IMPLICIT_NULL: {
 872         if (VtableStubs::contains(pc)) {
 873           // We haven't yet entered the callee frame. Fabricate an
 874           // exception and begin dispatching it in the caller. Since
 875           // the caller was at a call site, it's safe to destroy all
 876           // caller-saved registers, as these entry points do.
 877           VtableStub* vt_stub = VtableStubs::stub_containing(pc);
 878 
 879           // If vt_stub is null, then return null to signal handler to report the SEGV error.
 880           if (vt_stub == nullptr) return nullptr;
 881 
 882           if (vt_stub->is_abstract_method_error(pc)) {
 883             assert(!vt_stub->is_vtable_stub(), "should never see AbstractMethodErrors from vtable-type VtableStubs");
 884             Events::log_exception(current, "AbstractMethodError at " INTPTR_FORMAT, p2i(pc));
 885             // Instead of throwing the abstract method error here directly, we re-resolve
 886             // and will throw the AbstractMethodError during resolve. As a result, we'll
 887             // get a more detailed error message.
 888             return SharedRuntime::get_handle_wrong_method_stub();
 889           } else {
 890             Events::log_exception(current, "NullPointerException at vtable entry " INTPTR_FORMAT, p2i(pc));
 891             // Assert that the signal comes from the expected location in stub code.
 892             assert(vt_stub->is_null_pointer_exception(pc),
 893                    "obtained signal from unexpected location in stub code");
 894             return StubRoutines::throw_NullPointerException_at_call_entry();
 895           }
 896         } else {
 897           CodeBlob* cb = CodeCache::find_blob(pc);
 898 
 899           // If code blob is null, then return null to signal handler to report the SEGV error.
 900           if (cb == nullptr) return nullptr;
 901 
 902           // Exception happened in CodeCache. Must be either:
 903           // 1. Inline-cache check in C2I handler blob,
 904           // 2. Inline-cache check in nmethod, or
 905           // 3. Implicit null exception in nmethod
 906 
 907           if (!cb->is_compiled()) {
 908             bool is_in_blob = cb->is_adapter_blob() || cb->is_method_handles_adapter_blob();
 909             if (!is_in_blob) {
 910               // Allow normal crash reporting to handle this
 911               return nullptr;
 912             }
 913             Events::log_exception(current, "NullPointerException in code blob at " INTPTR_FORMAT, p2i(pc));
 914             // There is no handler here, so we will simply unwind.
 915             return StubRoutines::throw_NullPointerException_at_call_entry();
 916           }
 917 
 918           // Otherwise, it's a compiled method.  Consult its exception handlers.
 919           CompiledMethod* cm = (CompiledMethod*)cb;
 920           if (cm->inlinecache_check_contains(pc)) {
 921             // exception happened inside inline-cache check code
 922             // => the nmethod is not yet active (i.e., the frame
 923             // is not set up yet) => use return address pushed by
 924             // caller => don't push another return address
 925             Events::log_exception(current, "NullPointerException in IC check " INTPTR_FORMAT, p2i(pc));
 926             return StubRoutines::throw_NullPointerException_at_call_entry();
 927           }
 928 
 929           if (cm->method()->is_method_handle_intrinsic()) {
 930             // exception happened inside MH dispatch code, similar to a vtable stub
 931             Events::log_exception(current, "NullPointerException in MH adapter " INTPTR_FORMAT, p2i(pc));
 932             return StubRoutines::throw_NullPointerException_at_call_entry();
 933           }
 934 
 935 #ifndef PRODUCT
 936           _implicit_null_throws++;
 937 #endif
 938           target_pc = cm->continuation_for_implicit_null_exception(pc);
 939           // If there's an unexpected fault, target_pc might be null,
 940           // in which case we want to fall through into the normal
 941           // error handling code.
 942         }
 943 
 944         break; // fall through
 945       }
 946 
 947 
 948       case IMPLICIT_DIVIDE_BY_ZERO: {
 949         CompiledMethod* cm = CodeCache::find_compiled(pc);
 950         guarantee(cm != nullptr, "must have containing compiled method for implicit division-by-zero exceptions");
 951 #ifndef PRODUCT
 952         _implicit_div0_throws++;
 953 #endif
 954         target_pc = cm->continuation_for_implicit_div0_exception(pc);
 955         // If there's an unexpected fault, target_pc might be null,
 956         // in which case we want to fall through into the normal
 957         // error handling code.
 958         break; // fall through
 959       }
 960 
 961       default: ShouldNotReachHere();
 962     }
 963 
 964     assert(exception_kind == IMPLICIT_NULL || exception_kind == IMPLICIT_DIVIDE_BY_ZERO, "wrong implicit exception kind");
 965 
 966     if (exception_kind == IMPLICIT_NULL) {
 967 #ifndef PRODUCT
 968       // for AbortVMOnException flag
 969       Exceptions::debug_check_abort("java.lang.NullPointerException");
 970 #endif //PRODUCT
 971       Events::log_exception(current, "Implicit null exception at " INTPTR_FORMAT " to " INTPTR_FORMAT, p2i(pc), p2i(target_pc));
 972     } else {
 973 #ifndef PRODUCT
 974       // for AbortVMOnException flag
 975       Exceptions::debug_check_abort("java.lang.ArithmeticException");
 976 #endif //PRODUCT
 977       Events::log_exception(current, "Implicit division by zero exception at " INTPTR_FORMAT " to " INTPTR_FORMAT, p2i(pc), p2i(target_pc));
 978     }
 979     return target_pc;
 980   }
 981 
 982   ShouldNotReachHere();
 983   return nullptr;
 984 }
 985 
 986 
 987 /**
 988  * Throws an java/lang/UnsatisfiedLinkError.  The address of this method is
 989  * installed in the native function entry of all native Java methods before
 990  * they get linked to their actual native methods.
 991  *
 992  * \note
 993  * This method actually never gets called!  The reason is because
 994  * the interpreter's native entries call NativeLookup::lookup() which
 995  * throws the exception when the lookup fails.  The exception is then
 996  * caught and forwarded on the return from NativeLookup::lookup() call
 997  * before the call to the native function.  This might change in the future.
 998  */
 999 JNI_ENTRY(void*, throw_unsatisfied_link_error(JNIEnv* env, ...))
1000 {
1001   // We return a bad value here to make sure that the exception is
1002   // forwarded before we look at the return value.
1003   THROW_(vmSymbols::java_lang_UnsatisfiedLinkError(), (void*)badAddress);
1004 }
1005 JNI_END
1006 
1007 address SharedRuntime::native_method_throw_unsatisfied_link_error_entry() {
1008   return CAST_FROM_FN_PTR(address, &throw_unsatisfied_link_error);
1009 }
1010 
1011 JRT_ENTRY_NO_ASYNC(void, SharedRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
1012 #if INCLUDE_JVMCI
1013   if (!obj->klass()->has_finalizer()) {
1014     return;
1015   }
1016 #endif // INCLUDE_JVMCI
1017   assert(oopDesc::is_oop(obj), "must be a valid oop");
1018   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1019   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1020 JRT_END
1021 
1022 jlong SharedRuntime::get_java_tid(JavaThread* thread) {
1023   assert(thread != nullptr, "No thread");
1024   if (thread == nullptr) {
1025     return 0;
1026   }
1027   guarantee(Thread::current() != thread || thread->is_oop_safe(),
1028             "current cannot touch oops after its GC barrier is detached.");
1029   oop obj = thread->threadObj();
1030   return (obj == nullptr) ? 0 : java_lang_Thread::thread_id(obj);
1031 }
1032 
1033 /**
1034  * This function ought to be a void function, but cannot be because
1035  * it gets turned into a tail-call on sparc, which runs into dtrace bug
1036  * 6254741.  Once that is fixed we can remove the dummy return value.
1037  */
1038 int SharedRuntime::dtrace_object_alloc(oopDesc* o) {
1039   return dtrace_object_alloc(JavaThread::current(), o, o->size());
1040 }
1041 
1042 int SharedRuntime::dtrace_object_alloc(JavaThread* thread, oopDesc* o) {
1043   return dtrace_object_alloc(thread, o, o->size());
1044 }
1045 
1046 int SharedRuntime::dtrace_object_alloc(JavaThread* thread, oopDesc* o, size_t size) {
1047   assert(DTraceAllocProbes, "wrong call");
1048   Klass* klass = o->klass();
1049   Symbol* name = klass->name();
1050   HOTSPOT_OBJECT_ALLOC(
1051                    get_java_tid(thread),
1052                    (char *) name->bytes(), name->utf8_length(), size * HeapWordSize);
1053   return 0;
1054 }
1055 
1056 JRT_LEAF(int, SharedRuntime::dtrace_method_entry(
1057     JavaThread* current, Method* method))
1058   assert(current == JavaThread::current(), "pre-condition");
1059 
1060   assert(DTraceMethodProbes, "wrong call");
1061   Symbol* kname = method->klass_name();
1062   Symbol* name = method->name();
1063   Symbol* sig = method->signature();
1064   HOTSPOT_METHOD_ENTRY(
1065       get_java_tid(current),
1066       (char *) kname->bytes(), kname->utf8_length(),
1067       (char *) name->bytes(), name->utf8_length(),
1068       (char *) sig->bytes(), sig->utf8_length());
1069   return 0;
1070 JRT_END
1071 
1072 JRT_LEAF(int, SharedRuntime::dtrace_method_exit(
1073     JavaThread* current, Method* method))
1074   assert(current == JavaThread::current(), "pre-condition");
1075   assert(DTraceMethodProbes, "wrong call");
1076   Symbol* kname = method->klass_name();
1077   Symbol* name = method->name();
1078   Symbol* sig = method->signature();
1079   HOTSPOT_METHOD_RETURN(
1080       get_java_tid(current),
1081       (char *) kname->bytes(), kname->utf8_length(),
1082       (char *) name->bytes(), name->utf8_length(),
1083       (char *) sig->bytes(), sig->utf8_length());
1084   return 0;
1085 JRT_END
1086 
1087 
1088 // Finds receiver, CallInfo (i.e. receiver method), and calling bytecode)
1089 // for a call current in progress, i.e., arguments has been pushed on stack
1090 // put callee has not been invoked yet.  Used by: resolve virtual/static,
1091 // vtable updates, etc.  Caller frame must be compiled.
1092 Handle SharedRuntime::find_callee_info(Bytecodes::Code& bc, CallInfo& callinfo, TRAPS) {
1093   JavaThread* current = THREAD;
1094   ResourceMark rm(current);
1095 
1096   // last java frame on stack (which includes native call frames)
1097   vframeStream vfst(current, true);  // Do not skip and javaCalls
1098 
1099   return find_callee_info_helper(vfst, bc, callinfo, THREAD);
1100 }
1101 
1102 Method* SharedRuntime::extract_attached_method(vframeStream& vfst) {
1103   CompiledMethod* caller = vfst.nm();
1104 
1105   address pc = vfst.frame_pc();
1106   { // Get call instruction under lock because another thread may be busy patching it.
1107     CompiledICLocker ic_locker(caller);
1108     return caller->attached_method_before_pc(pc);
1109   }
1110   return nullptr;
1111 }
1112 
1113 // Finds receiver, CallInfo (i.e. receiver method), and calling bytecode
1114 // for a call current in progress, i.e., arguments has been pushed on stack
1115 // but callee has not been invoked yet.  Caller frame must be compiled.
1116 Handle SharedRuntime::find_callee_info_helper(vframeStream& vfst, Bytecodes::Code& bc,
1117                                               CallInfo& callinfo, TRAPS) {
1118   Handle receiver;
1119   Handle nullHandle;  // create a handy null handle for exception returns
1120   JavaThread* current = THREAD;
1121 
1122   assert(!vfst.at_end(), "Java frame must exist");
1123 
1124   // Find caller and bci from vframe
1125   methodHandle caller(current, vfst.method());
1126   int          bci   = vfst.bci();
1127 
1128   if (caller->is_continuation_enter_intrinsic()) {
1129     bc = Bytecodes::_invokestatic;
1130     LinkResolver::resolve_continuation_enter(callinfo, CHECK_NH);
1131     return receiver;
1132   }
1133 
1134   Bytecode_invoke bytecode(caller, bci);
1135   int bytecode_index = bytecode.index();
1136   bc = bytecode.invoke_code();
1137 
1138   methodHandle attached_method(current, extract_attached_method(vfst));
1139   if (attached_method.not_null()) {
1140     Method* callee = bytecode.static_target(CHECK_NH);
1141     vmIntrinsics::ID id = callee->intrinsic_id();
1142     // When VM replaces MH.invokeBasic/linkTo* call with a direct/virtual call,
1143     // it attaches statically resolved method to the call site.
1144     if (MethodHandles::is_signature_polymorphic(id) &&
1145         MethodHandles::is_signature_polymorphic_intrinsic(id)) {
1146       bc = MethodHandles::signature_polymorphic_intrinsic_bytecode(id);
1147 
1148       // Adjust invocation mode according to the attached method.
1149       switch (bc) {
1150         case Bytecodes::_invokevirtual:
1151           if (attached_method->method_holder()->is_interface()) {
1152             bc = Bytecodes::_invokeinterface;
1153           }
1154           break;
1155         case Bytecodes::_invokeinterface:
1156           if (!attached_method->method_holder()->is_interface()) {
1157             bc = Bytecodes::_invokevirtual;
1158           }
1159           break;
1160         case Bytecodes::_invokehandle:
1161           if (!MethodHandles::is_signature_polymorphic_method(attached_method())) {
1162             bc = attached_method->is_static() ? Bytecodes::_invokestatic
1163                                               : Bytecodes::_invokevirtual;
1164           }
1165           break;
1166         default:
1167           break;
1168       }
1169     }
1170   }
1171 
1172   assert(bc != Bytecodes::_illegal, "not initialized");
1173 
1174   bool has_receiver = bc != Bytecodes::_invokestatic &&
1175                       bc != Bytecodes::_invokedynamic &&
1176                       bc != Bytecodes::_invokehandle;
1177 
1178   // Find receiver for non-static call
1179   if (has_receiver) {
1180     // This register map must be update since we need to find the receiver for
1181     // compiled frames. The receiver might be in a register.
1182     RegisterMap reg_map2(current,
1183                          RegisterMap::UpdateMap::include,
1184                          RegisterMap::ProcessFrames::include,
1185                          RegisterMap::WalkContinuation::skip);
1186     frame stubFrame   = current->last_frame();
1187     // Caller-frame is a compiled frame
1188     frame callerFrame = stubFrame.sender(&reg_map2);
1189 
1190     if (attached_method.is_null()) {
1191       Method* callee = bytecode.static_target(CHECK_NH);
1192       if (callee == nullptr) {
1193         THROW_(vmSymbols::java_lang_NoSuchMethodException(), nullHandle);
1194       }
1195     }
1196 
1197     // Retrieve from a compiled argument list
1198     receiver = Handle(current, callerFrame.retrieve_receiver(&reg_map2));
1199     assert(oopDesc::is_oop_or_null(receiver()), "");
1200 
1201     if (receiver.is_null()) {
1202       THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle);
1203     }
1204   }
1205 
1206   // Resolve method
1207   if (attached_method.not_null()) {
1208     // Parameterized by attached method.
1209     LinkResolver::resolve_invoke(callinfo, receiver, attached_method, bc, CHECK_NH);
1210   } else {
1211     // Parameterized by bytecode.
1212     constantPoolHandle constants(current, caller->constants());
1213     LinkResolver::resolve_invoke(callinfo, receiver, constants, bytecode_index, bc, CHECK_NH);
1214   }
1215 
1216 #ifdef ASSERT
1217   // Check that the receiver klass is of the right subtype and that it is initialized for virtual calls
1218   if (has_receiver) {
1219     assert(receiver.not_null(), "should have thrown exception");
1220     Klass* receiver_klass = receiver->klass();
1221     Klass* rk = nullptr;
1222     if (attached_method.not_null()) {
1223       // In case there's resolved method attached, use its holder during the check.
1224       rk = attached_method->method_holder();
1225     } else {
1226       // Klass is already loaded.
1227       constantPoolHandle constants(current, caller->constants());
1228       rk = constants->klass_ref_at(bytecode_index, bc, CHECK_NH);
1229     }
1230     Klass* static_receiver_klass = rk;
1231     assert(receiver_klass->is_subtype_of(static_receiver_klass),
1232            "actual receiver must be subclass of static receiver klass");
1233     if (receiver_klass->is_instance_klass()) {
1234       if (InstanceKlass::cast(receiver_klass)->is_not_initialized()) {
1235         tty->print_cr("ERROR: Klass not yet initialized!!");
1236         receiver_klass->print();
1237       }
1238       assert(!InstanceKlass::cast(receiver_klass)->is_not_initialized(), "receiver_klass must be initialized");
1239     }
1240   }
1241 #endif
1242 
1243   return receiver;
1244 }
1245 
1246 methodHandle SharedRuntime::find_callee_method(TRAPS) {
1247   JavaThread* current = THREAD;
1248   ResourceMark rm(current);
1249   // We need first to check if any Java activations (compiled, interpreted)
1250   // exist on the stack since last JavaCall.  If not, we need
1251   // to get the target method from the JavaCall wrapper.
1252   vframeStream vfst(current, true);  // Do not skip any javaCalls
1253   methodHandle callee_method;
1254   if (vfst.at_end()) {
1255     // No Java frames were found on stack since we did the JavaCall.
1256     // Hence the stack can only contain an entry_frame.  We need to
1257     // find the target method from the stub frame.
1258     RegisterMap reg_map(current,
1259                         RegisterMap::UpdateMap::skip,
1260                         RegisterMap::ProcessFrames::include,
1261                         RegisterMap::WalkContinuation::skip);
1262     frame fr = current->last_frame();
1263     assert(fr.is_runtime_frame(), "must be a runtimeStub");
1264     fr = fr.sender(&reg_map);
1265     assert(fr.is_entry_frame(), "must be");
1266     // fr is now pointing to the entry frame.
1267     callee_method = methodHandle(current, fr.entry_frame_call_wrapper()->callee_method());
1268   } else {
1269     Bytecodes::Code bc;
1270     CallInfo callinfo;
1271     find_callee_info_helper(vfst, bc, callinfo, CHECK_(methodHandle()));
1272     callee_method = methodHandle(current, callinfo.selected_method());
1273   }
1274   assert(callee_method()->is_method(), "must be");
1275   return callee_method;
1276 }
1277 
1278 // Resolves a call.
1279 methodHandle SharedRuntime::resolve_helper(bool is_virtual, bool is_optimized, TRAPS) {
1280   methodHandle callee_method;
1281   callee_method = resolve_sub_helper(is_virtual, is_optimized, THREAD);
1282   if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
1283     int retry_count = 0;
1284     while (!HAS_PENDING_EXCEPTION && callee_method->is_old() &&
1285            callee_method->method_holder() != vmClasses::Object_klass()) {
1286       // If has a pending exception then there is no need to re-try to
1287       // resolve this method.
1288       // If the method has been redefined, we need to try again.
1289       // Hack: we have no way to update the vtables of arrays, so don't
1290       // require that java.lang.Object has been updated.
1291 
1292       // It is very unlikely that method is redefined more than 100 times
1293       // in the middle of resolve. If it is looping here more than 100 times
1294       // means then there could be a bug here.
1295       guarantee((retry_count++ < 100),
1296                 "Could not resolve to latest version of redefined method");
1297       // method is redefined in the middle of resolve so re-try.
1298       callee_method = resolve_sub_helper(is_virtual, is_optimized, THREAD);
1299     }
1300   }
1301   return callee_method;
1302 }
1303 
1304 // This fails if resolution required refilling of IC stubs
1305 bool SharedRuntime::resolve_sub_helper_internal(methodHandle callee_method, const frame& caller_frame,
1306                                                 CompiledMethod* caller_nm, bool is_virtual, bool is_optimized,
1307                                                 Handle receiver, CallInfo& call_info, Bytecodes::Code invoke_code, TRAPS) {
1308   StaticCallInfo static_call_info;
1309   CompiledICInfo virtual_call_info;
1310 
1311   // Make sure the callee nmethod does not get deoptimized and removed before
1312   // we are done patching the code.
1313   CompiledMethod* callee = callee_method->code();
1314 
1315   if (callee != nullptr) {
1316     assert(callee->is_compiled(), "must be nmethod for patching");
1317   }
1318 
1319   if (callee != nullptr && !callee->is_in_use()) {
1320     // Patch call site to C2I adapter if callee nmethod is deoptimized or unloaded.
1321     callee = nullptr;
1322   }
1323 #ifdef ASSERT
1324   address dest_entry_point = callee == nullptr ? 0 : callee->entry_point(); // used below
1325 #endif
1326 
1327   bool is_nmethod = caller_nm->is_nmethod();
1328 
1329   if (is_virtual) {
1330     assert(receiver.not_null() || invoke_code == Bytecodes::_invokehandle, "sanity check");
1331     bool static_bound = call_info.resolved_method()->can_be_statically_bound();
1332     Klass* klass = invoke_code == Bytecodes::_invokehandle ? nullptr : receiver->klass();
1333     CompiledIC::compute_monomorphic_entry(callee_method, klass,
1334                      is_optimized, static_bound, is_nmethod, virtual_call_info,
1335                      CHECK_false);
1336   } else {
1337     // static call
1338     CompiledStaticCall::compute_entry(callee_method, is_nmethod, static_call_info);
1339   }
1340 
1341   // grab lock, check for deoptimization and potentially patch caller
1342   {
1343     CompiledICLocker ml(caller_nm);
1344 
1345     // Lock blocks for safepoint during which both nmethods can change state.
1346 
1347     // Now that we are ready to patch if the Method* was redefined then
1348     // don't update call site and let the caller retry.
1349     // Don't update call site if callee nmethod was unloaded or deoptimized.
1350     // Don't update call site if callee nmethod was replaced by an other nmethod
1351     // which may happen when multiply alive nmethod (tiered compilation)
1352     // will be supported.
1353     if (!callee_method->is_old() &&
1354         (callee == nullptr || (callee->is_in_use() && callee_method->code() == callee))) {
1355       NoSafepointVerifier nsv;
1356 #ifdef ASSERT
1357       // We must not try to patch to jump to an already unloaded method.
1358       if (dest_entry_point != 0) {
1359         CodeBlob* cb = CodeCache::find_blob(dest_entry_point);
1360         assert((cb != nullptr) && cb->is_compiled() && (((CompiledMethod*)cb) == callee),
1361                "should not call unloaded nmethod");
1362       }
1363 #endif
1364       if (is_virtual) {
1365         CompiledIC* inline_cache = CompiledIC_before(caller_nm, caller_frame.pc());
1366         if (inline_cache->is_clean()) {
1367           if (!inline_cache->set_to_monomorphic(virtual_call_info)) {
1368             return false;
1369           }
1370         }
1371       } else {
1372         if (VM_Version::supports_fast_class_init_checks() &&
1373             invoke_code == Bytecodes::_invokestatic &&
1374             callee_method->needs_clinit_barrier() &&
1375             callee != nullptr && callee->is_compiled_by_jvmci()) {
1376           return true; // skip patching for JVMCI
1377         }
1378         CompiledStaticCall* ssc = caller_nm->compiledStaticCall_before(caller_frame.pc());
1379         if (is_nmethod && caller_nm->method()->is_continuation_enter_intrinsic()) {
1380           ssc->compute_entry_for_continuation_entry(callee_method, static_call_info);
1381         }
1382         if (ssc->is_clean()) ssc->set(static_call_info);
1383       }
1384     }
1385   } // unlock CompiledICLocker
1386   return true;
1387 }
1388 
1389 // Resolves a call.  The compilers generate code for calls that go here
1390 // and are patched with the real destination of the call.
1391 methodHandle SharedRuntime::resolve_sub_helper(bool is_virtual, bool is_optimized, TRAPS) {
1392   JavaThread* current = THREAD;
1393   ResourceMark rm(current);
1394   RegisterMap cbl_map(current,
1395                       RegisterMap::UpdateMap::skip,
1396                       RegisterMap::ProcessFrames::include,
1397                       RegisterMap::WalkContinuation::skip);
1398   frame caller_frame = current->last_frame().sender(&cbl_map);
1399 
1400   CodeBlob* caller_cb = caller_frame.cb();
1401   guarantee(caller_cb != nullptr && caller_cb->is_compiled(), "must be called from compiled method");
1402   CompiledMethod* caller_nm = caller_cb->as_compiled_method_or_null();
1403 
1404   // determine call info & receiver
1405   // note: a) receiver is null for static calls
1406   //       b) an exception is thrown if receiver is null for non-static calls
1407   CallInfo call_info;
1408   Bytecodes::Code invoke_code = Bytecodes::_illegal;
1409   Handle receiver = find_callee_info(invoke_code, call_info, CHECK_(methodHandle()));
1410   methodHandle callee_method(current, call_info.selected_method());
1411 
1412   assert((!is_virtual && invoke_code == Bytecodes::_invokestatic ) ||
1413          (!is_virtual && invoke_code == Bytecodes::_invokespecial) ||
1414          (!is_virtual && invoke_code == Bytecodes::_invokehandle ) ||
1415          (!is_virtual && invoke_code == Bytecodes::_invokedynamic) ||
1416          ( is_virtual && invoke_code != Bytecodes::_invokestatic ), "inconsistent bytecode");
1417 
1418   assert(!caller_nm->is_unloading(), "It should not be unloading");
1419 
1420 #ifndef PRODUCT
1421   // tracing/debugging/statistics
1422   int *addr = (is_optimized) ? (&_resolve_opt_virtual_ctr) :
1423                 (is_virtual) ? (&_resolve_virtual_ctr) :
1424                                (&_resolve_static_ctr);
1425   Atomic::inc(addr);
1426 
1427   if (TraceCallFixup) {
1428     ResourceMark rm(current);
1429     tty->print("resolving %s%s (%s) call to",
1430                (is_optimized) ? "optimized " : "", (is_virtual) ? "virtual" : "static",
1431                Bytecodes::name(invoke_code));
1432     callee_method->print_short_name(tty);
1433     tty->print_cr(" at pc: " INTPTR_FORMAT " to code: " INTPTR_FORMAT,
1434                   p2i(caller_frame.pc()), p2i(callee_method->code()));
1435   }
1436 #endif
1437 
1438   if (invoke_code == Bytecodes::_invokestatic) {
1439     assert(callee_method->method_holder()->is_initialized() ||
1440            callee_method->method_holder()->is_reentrant_initialization(current),
1441            "invalid class initialization state for invoke_static");
1442     if (!VM_Version::supports_fast_class_init_checks() && callee_method->needs_clinit_barrier()) {
1443       // In order to keep class initialization check, do not patch call
1444       // site for static call when the class is not fully initialized.
1445       // Proper check is enforced by call site re-resolution on every invocation.
1446       //
1447       // When fast class initialization checks are supported (VM_Version::supports_fast_class_init_checks() == true),
1448       // explicit class initialization check is put in nmethod entry (VEP).
1449       assert(callee_method->method_holder()->is_linked(), "must be");
1450       return callee_method;
1451     }
1452   }
1453 
1454   // JSR 292 key invariant:
1455   // If the resolved method is a MethodHandle invoke target, the call
1456   // site must be a MethodHandle call site, because the lambda form might tail-call
1457   // leaving the stack in a state unknown to either caller or callee
1458   // TODO detune for now but we might need it again
1459 //  assert(!callee_method->is_compiled_lambda_form() ||
1460 //         caller_nm->is_method_handle_return(caller_frame.pc()), "must be MH call site");
1461 
1462   // Compute entry points. This might require generation of C2I converter
1463   // frames, so we cannot be holding any locks here. Furthermore, the
1464   // computation of the entry points is independent of patching the call.  We
1465   // always return the entry-point, but we only patch the stub if the call has
1466   // not been deoptimized.  Return values: For a virtual call this is an
1467   // (cached_oop, destination address) pair. For a static call/optimized
1468   // virtual this is just a destination address.
1469 
1470   // Patching IC caches may fail if we run out if transition stubs.
1471   // We refill the ic stubs then and try again.
1472   for (;;) {
1473     ICRefillVerifier ic_refill_verifier;
1474     bool successful = resolve_sub_helper_internal(callee_method, caller_frame, caller_nm,
1475                                                   is_virtual, is_optimized, receiver,
1476                                                   call_info, invoke_code, CHECK_(methodHandle()));
1477     if (successful) {
1478       return callee_method;
1479     } else {
1480       InlineCacheBuffer::refill_ic_stubs();
1481     }
1482   }
1483 
1484 }
1485 
1486 
1487 // Inline caches exist only in compiled code
1488 JRT_BLOCK_ENTRY(address, SharedRuntime::handle_wrong_method_ic_miss(JavaThread* current))
1489 #ifdef ASSERT
1490   RegisterMap reg_map(current,
1491                       RegisterMap::UpdateMap::skip,
1492                       RegisterMap::ProcessFrames::include,
1493                       RegisterMap::WalkContinuation::skip);
1494   frame stub_frame = current->last_frame();
1495   assert(stub_frame.is_runtime_frame(), "sanity check");
1496   frame caller_frame = stub_frame.sender(&reg_map);
1497   assert(!caller_frame.is_interpreted_frame() && !caller_frame.is_entry_frame() && !caller_frame.is_upcall_stub_frame(), "unexpected frame");
1498 #endif /* ASSERT */
1499 
1500   methodHandle callee_method;
1501   JRT_BLOCK
1502     callee_method = SharedRuntime::handle_ic_miss_helper(CHECK_NULL);
1503     // Return Method* through TLS
1504     current->set_vm_result_2(callee_method());
1505   JRT_BLOCK_END
1506   // return compiled code entry point after potential safepoints
1507   assert(callee_method->verified_code_entry() != nullptr, " Jump to zero!");
1508   return callee_method->verified_code_entry();
1509 JRT_END
1510 
1511 
1512 // Handle call site that has been made non-entrant
1513 JRT_BLOCK_ENTRY(address, SharedRuntime::handle_wrong_method(JavaThread* current))
1514   // 6243940 We might end up in here if the callee is deoptimized
1515   // as we race to call it.  We don't want to take a safepoint if
1516   // the caller was interpreted because the caller frame will look
1517   // interpreted to the stack walkers and arguments are now
1518   // "compiled" so it is much better to make this transition
1519   // invisible to the stack walking code. The i2c path will
1520   // place the callee method in the callee_target. It is stashed
1521   // there because if we try and find the callee by normal means a
1522   // safepoint is possible and have trouble gc'ing the compiled args.
1523   RegisterMap reg_map(current,
1524                       RegisterMap::UpdateMap::skip,
1525                       RegisterMap::ProcessFrames::include,
1526                       RegisterMap::WalkContinuation::skip);
1527   frame stub_frame = current->last_frame();
1528   assert(stub_frame.is_runtime_frame(), "sanity check");
1529   frame caller_frame = stub_frame.sender(&reg_map);
1530 
1531   if (caller_frame.is_interpreted_frame() ||
1532       caller_frame.is_entry_frame() ||
1533       caller_frame.is_upcall_stub_frame()) {
1534     Method* callee = current->callee_target();
1535     guarantee(callee != nullptr && callee->is_method(), "bad handshake");
1536     current->set_vm_result_2(callee);
1537     current->set_callee_target(nullptr);
1538     if (caller_frame.is_entry_frame() && VM_Version::supports_fast_class_init_checks()) {
1539       // Bypass class initialization checks in c2i when caller is in native.
1540       // JNI calls to static methods don't have class initialization checks.
1541       // Fast class initialization checks are present in c2i adapters and call into
1542       // SharedRuntime::handle_wrong_method() on the slow path.
1543       //
1544       // JVM upcalls may land here as well, but there's a proper check present in
1545       // LinkResolver::resolve_static_call (called from JavaCalls::call_static),
1546       // so bypassing it in c2i adapter is benign.
1547       return callee->get_c2i_no_clinit_check_entry();
1548     } else {
1549       return callee->get_c2i_entry();
1550     }
1551   }
1552 
1553   // Must be compiled to compiled path which is safe to stackwalk
1554   methodHandle callee_method;
1555   JRT_BLOCK
1556     // Force resolving of caller (if we called from compiled frame)
1557     callee_method = SharedRuntime::reresolve_call_site(CHECK_NULL);
1558     current->set_vm_result_2(callee_method());
1559   JRT_BLOCK_END
1560   // return compiled code entry point after potential safepoints
1561   assert(callee_method->verified_code_entry() != nullptr, " Jump to zero!");
1562   return callee_method->verified_code_entry();
1563 JRT_END
1564 
1565 // Handle abstract method call
1566 JRT_BLOCK_ENTRY(address, SharedRuntime::handle_wrong_method_abstract(JavaThread* current))
1567   // Verbose error message for AbstractMethodError.
1568   // Get the called method from the invoke bytecode.
1569   vframeStream vfst(current, true);
1570   assert(!vfst.at_end(), "Java frame must exist");
1571   methodHandle caller(current, vfst.method());
1572   Bytecode_invoke invoke(caller, vfst.bci());
1573   DEBUG_ONLY( invoke.verify(); )
1574 
1575   // Find the compiled caller frame.
1576   RegisterMap reg_map(current,
1577                       RegisterMap::UpdateMap::include,
1578                       RegisterMap::ProcessFrames::include,
1579                       RegisterMap::WalkContinuation::skip);
1580   frame stubFrame = current->last_frame();
1581   assert(stubFrame.is_runtime_frame(), "must be");
1582   frame callerFrame = stubFrame.sender(&reg_map);
1583   assert(callerFrame.is_compiled_frame(), "must be");
1584 
1585   // Install exception and return forward entry.
1586   address res = StubRoutines::throw_AbstractMethodError_entry();
1587   JRT_BLOCK
1588     methodHandle callee(current, invoke.static_target(current));
1589     if (!callee.is_null()) {
1590       oop recv = callerFrame.retrieve_receiver(&reg_map);
1591       Klass *recv_klass = (recv != nullptr) ? recv->klass() : nullptr;
1592       res = StubRoutines::forward_exception_entry();
1593       LinkResolver::throw_abstract_method_error(callee, recv_klass, CHECK_(res));
1594     }
1595   JRT_BLOCK_END
1596   return res;
1597 JRT_END
1598 
1599 
1600 // resolve a static call and patch code
1601 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_static_call_C(JavaThread* current ))
1602   methodHandle callee_method;
1603   bool enter_special = false;
1604   JRT_BLOCK
1605     callee_method = SharedRuntime::resolve_helper(false, false, CHECK_NULL);
1606     current->set_vm_result_2(callee_method());
1607 
1608     if (current->is_interp_only_mode()) {
1609       RegisterMap reg_map(current,
1610                           RegisterMap::UpdateMap::skip,
1611                           RegisterMap::ProcessFrames::include,
1612                           RegisterMap::WalkContinuation::skip);
1613       frame stub_frame = current->last_frame();
1614       assert(stub_frame.is_runtime_frame(), "must be a runtimeStub");
1615       frame caller = stub_frame.sender(&reg_map);
1616       enter_special = caller.cb() != nullptr && caller.cb()->is_compiled()
1617         && caller.cb()->as_compiled_method()->method()->is_continuation_enter_intrinsic();
1618     }
1619   JRT_BLOCK_END
1620 
1621   if (current->is_interp_only_mode() && enter_special) {
1622     // enterSpecial is compiled and calls this method to resolve the call to Continuation::enter
1623     // but in interp_only_mode we need to go to the interpreted entry
1624     // The c2i won't patch in this mode -- see fixup_callers_callsite
1625     //
1626     // This should probably be done in all cases, not just enterSpecial (see JDK-8218403),
1627     // but that's part of a larger fix, and the situation is worse for enterSpecial, as it has no
1628     // interpreted version.
1629     return callee_method->get_c2i_entry();
1630   }
1631 
1632   // return compiled code entry point after potential safepoints
1633   assert(callee_method->verified_code_entry() != nullptr, " Jump to zero!");
1634   return callee_method->verified_code_entry();
1635 JRT_END
1636 
1637 
1638 // resolve virtual call and update inline cache to monomorphic
1639 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_virtual_call_C(JavaThread* current))
1640   methodHandle callee_method;
1641   JRT_BLOCK
1642     callee_method = SharedRuntime::resolve_helper(true, false, CHECK_NULL);
1643     current->set_vm_result_2(callee_method());
1644   JRT_BLOCK_END
1645   // return compiled code entry point after potential safepoints
1646   assert(callee_method->verified_code_entry() != nullptr, " Jump to zero!");
1647   return callee_method->verified_code_entry();
1648 JRT_END
1649 
1650 
1651 // Resolve a virtual call that can be statically bound (e.g., always
1652 // monomorphic, so it has no inline cache).  Patch code to resolved target.
1653 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_opt_virtual_call_C(JavaThread* current))
1654   methodHandle callee_method;
1655   JRT_BLOCK
1656     callee_method = SharedRuntime::resolve_helper(true, true, CHECK_NULL);
1657     current->set_vm_result_2(callee_method());
1658   JRT_BLOCK_END
1659   // return compiled code entry point after potential safepoints
1660   assert(callee_method->verified_code_entry() != nullptr, " Jump to zero!");
1661   return callee_method->verified_code_entry();
1662 JRT_END
1663 
1664 // The handle_ic_miss_helper_internal function returns false if it failed due
1665 // to either running out of vtable stubs or ic stubs due to IC transitions
1666 // to transitional states. The needs_ic_stub_refill value will be set if
1667 // the failure was due to running out of IC stubs, in which case handle_ic_miss_helper
1668 // refills the IC stubs and tries again.
1669 bool SharedRuntime::handle_ic_miss_helper_internal(Handle receiver, CompiledMethod* caller_nm,
1670                                                    const frame& caller_frame, methodHandle callee_method,
1671                                                    Bytecodes::Code bc, CallInfo& call_info,
1672                                                    bool& needs_ic_stub_refill, TRAPS) {
1673   CompiledICLocker ml(caller_nm);
1674   CompiledIC* inline_cache = CompiledIC_before(caller_nm, caller_frame.pc());
1675   bool should_be_mono = false;
1676   if (inline_cache->is_optimized()) {
1677     if (TraceCallFixup) {
1678       ResourceMark rm(THREAD);
1679       tty->print("OPTIMIZED IC miss (%s) call to", Bytecodes::name(bc));
1680       callee_method->print_short_name(tty);
1681       tty->print_cr(" code: " INTPTR_FORMAT, p2i(callee_method->code()));
1682     }
1683     should_be_mono = true;
1684   } else if (inline_cache->is_icholder_call()) {
1685     CompiledICHolder* ic_oop = inline_cache->cached_icholder();
1686     if (ic_oop != nullptr) {
1687       if (!ic_oop->is_loader_alive()) {
1688         // Deferred IC cleaning due to concurrent class unloading
1689         if (!inline_cache->set_to_clean()) {
1690           needs_ic_stub_refill = true;
1691           return false;
1692         }
1693       } else if (receiver()->klass() == ic_oop->holder_klass()) {
1694         // This isn't a real miss. We must have seen that compiled code
1695         // is now available and we want the call site converted to a
1696         // monomorphic compiled call site.
1697         // We can't assert for callee_method->code() != nullptr because it
1698         // could have been deoptimized in the meantime
1699         if (TraceCallFixup) {
1700           ResourceMark rm(THREAD);
1701           tty->print("FALSE IC miss (%s) converting to compiled call to", Bytecodes::name(bc));
1702           callee_method->print_short_name(tty);
1703           tty->print_cr(" code: " INTPTR_FORMAT, p2i(callee_method->code()));
1704         }
1705         should_be_mono = true;
1706       }
1707     }
1708   }
1709 
1710   if (should_be_mono) {
1711     // We have a path that was monomorphic but was going interpreted
1712     // and now we have (or had) a compiled entry. We correct the IC
1713     // by using a new icBuffer.
1714     CompiledICInfo info;
1715     Klass* receiver_klass = receiver()->klass();
1716     inline_cache->compute_monomorphic_entry(callee_method,
1717                                             receiver_klass,
1718                                             inline_cache->is_optimized(),
1719                                             false, caller_nm->is_nmethod(),
1720                                             info, CHECK_false);
1721     if (!inline_cache->set_to_monomorphic(info)) {
1722       needs_ic_stub_refill = true;
1723       return false;
1724     }
1725   } else if (!inline_cache->is_megamorphic() && !inline_cache->is_clean()) {
1726     // Potential change to megamorphic
1727 
1728     bool successful = inline_cache->set_to_megamorphic(&call_info, bc, needs_ic_stub_refill, CHECK_false);
1729     if (needs_ic_stub_refill) {
1730       return false;
1731     }
1732     if (!successful) {
1733       if (!inline_cache->set_to_clean()) {
1734         needs_ic_stub_refill = true;
1735         return false;
1736       }
1737     }
1738   } else {
1739     // Either clean or megamorphic
1740   }
1741   return true;
1742 }
1743 
1744 methodHandle SharedRuntime::handle_ic_miss_helper(TRAPS) {
1745   JavaThread* current = THREAD;
1746   ResourceMark rm(current);
1747   CallInfo call_info;
1748   Bytecodes::Code bc;
1749 
1750   // receiver is null for static calls. An exception is thrown for null
1751   // receivers for non-static calls
1752   Handle receiver = find_callee_info(bc, call_info, CHECK_(methodHandle()));
1753   // Compiler1 can produce virtual call sites that can actually be statically bound
1754   // If we fell thru to below we would think that the site was going megamorphic
1755   // when in fact the site can never miss. Worse because we'd think it was megamorphic
1756   // we'd try and do a vtable dispatch however methods that can be statically bound
1757   // don't have vtable entries (vtable_index < 0) and we'd blow up. So we force a
1758   // reresolution of the  call site (as if we did a handle_wrong_method and not an
1759   // plain ic_miss) and the site will be converted to an optimized virtual call site
1760   // never to miss again. I don't believe C2 will produce code like this but if it
1761   // did this would still be the correct thing to do for it too, hence no ifdef.
1762   //
1763   if (call_info.resolved_method()->can_be_statically_bound()) {
1764     methodHandle callee_method = SharedRuntime::reresolve_call_site(CHECK_(methodHandle()));
1765     if (TraceCallFixup) {
1766       RegisterMap reg_map(current,
1767                           RegisterMap::UpdateMap::skip,
1768                           RegisterMap::ProcessFrames::include,
1769                           RegisterMap::WalkContinuation::skip);
1770       frame caller_frame = current->last_frame().sender(&reg_map);
1771       ResourceMark rm(current);
1772       tty->print("converting IC miss to reresolve (%s) call to", Bytecodes::name(bc));
1773       callee_method->print_short_name(tty);
1774       tty->print_cr(" from pc: " INTPTR_FORMAT, p2i(caller_frame.pc()));
1775       tty->print_cr(" code: " INTPTR_FORMAT, p2i(callee_method->code()));
1776     }
1777     return callee_method;
1778   }
1779 
1780   methodHandle callee_method(current, call_info.selected_method());
1781 
1782 #ifndef PRODUCT
1783   Atomic::inc(&_ic_miss_ctr);
1784 
1785   // Statistics & Tracing
1786   if (TraceCallFixup) {
1787     ResourceMark rm(current);
1788     tty->print("IC miss (%s) call to", Bytecodes::name(bc));
1789     callee_method->print_short_name(tty);
1790     tty->print_cr(" code: " INTPTR_FORMAT, p2i(callee_method->code()));
1791   }
1792 
1793   if (ICMissHistogram) {
1794     MutexLocker m(VMStatistic_lock);
1795     RegisterMap reg_map(current,
1796                         RegisterMap::UpdateMap::skip,
1797                         RegisterMap::ProcessFrames::include,
1798                         RegisterMap::WalkContinuation::skip);
1799     frame f = current->last_frame().real_sender(&reg_map);// skip runtime stub
1800     // produce statistics under the lock
1801     trace_ic_miss(f.pc());
1802   }
1803 #endif
1804 
1805   // install an event collector so that when a vtable stub is created the
1806   // profiler can be notified via a DYNAMIC_CODE_GENERATED event. The
1807   // event can't be posted when the stub is created as locks are held
1808   // - instead the event will be deferred until the event collector goes
1809   // out of scope.
1810   JvmtiDynamicCodeEventCollector event_collector;
1811 
1812   // Update inline cache to megamorphic. Skip update if we are called from interpreted.
1813   // Transitioning IC caches may require transition stubs. If we run out
1814   // of transition stubs, we have to drop locks and perform a safepoint
1815   // that refills them.
1816   RegisterMap reg_map(current,
1817                       RegisterMap::UpdateMap::skip,
1818                       RegisterMap::ProcessFrames::include,
1819                       RegisterMap::WalkContinuation::skip);
1820   frame caller_frame = current->last_frame().sender(&reg_map);
1821   CodeBlob* cb = caller_frame.cb();
1822   CompiledMethod* caller_nm = cb->as_compiled_method();
1823 
1824   for (;;) {
1825     ICRefillVerifier ic_refill_verifier;
1826     bool needs_ic_stub_refill = false;
1827     bool successful = handle_ic_miss_helper_internal(receiver, caller_nm, caller_frame, callee_method,
1828                                                      bc, call_info, needs_ic_stub_refill, CHECK_(methodHandle()));
1829     if (successful || !needs_ic_stub_refill) {
1830       return callee_method;
1831     } else {
1832       InlineCacheBuffer::refill_ic_stubs();
1833     }
1834   }
1835 }
1836 
1837 static bool clear_ic_at_addr(CompiledMethod* caller_nm, address call_addr, bool is_static_call) {
1838   CompiledICLocker ml(caller_nm);
1839   if (is_static_call) {
1840     CompiledStaticCall* ssc = caller_nm->compiledStaticCall_at(call_addr);
1841     if (!ssc->is_clean()) {
1842       return ssc->set_to_clean();
1843     }
1844   } else {
1845     // compiled, dispatched call (which used to call an interpreted method)
1846     CompiledIC* inline_cache = CompiledIC_at(caller_nm, call_addr);
1847     if (!inline_cache->is_clean()) {
1848       return inline_cache->set_to_clean();
1849     }
1850   }
1851   return true;
1852 }
1853 
1854 //
1855 // Resets a call-site in compiled code so it will get resolved again.
1856 // This routines handles both virtual call sites, optimized virtual call
1857 // sites, and static call sites. Typically used to change a call sites
1858 // destination from compiled to interpreted.
1859 //
1860 methodHandle SharedRuntime::reresolve_call_site(TRAPS) {
1861   JavaThread* current = THREAD;
1862   ResourceMark rm(current);
1863   RegisterMap reg_map(current,
1864                       RegisterMap::UpdateMap::skip,
1865                       RegisterMap::ProcessFrames::include,
1866                       RegisterMap::WalkContinuation::skip);
1867   frame stub_frame = current->last_frame();
1868   assert(stub_frame.is_runtime_frame(), "must be a runtimeStub");
1869   frame caller = stub_frame.sender(&reg_map);
1870 
1871   // Do nothing if the frame isn't a live compiled frame.
1872   // nmethod could be deoptimized by the time we get here
1873   // so no update to the caller is needed.
1874 
1875   if ((caller.is_compiled_frame() && !caller.is_deoptimized_frame()) ||
1876       (caller.is_native_frame() && ((CompiledMethod*)caller.cb())->method()->is_continuation_enter_intrinsic())) {
1877 
1878     address pc = caller.pc();
1879 
1880     // Check for static or virtual call
1881     bool is_static_call = false;
1882     CompiledMethod* caller_nm = CodeCache::find_compiled(pc);
1883 
1884     // Default call_addr is the location of the "basic" call.
1885     // Determine the address of the call we a reresolving. With
1886     // Inline Caches we will always find a recognizable call.
1887     // With Inline Caches disabled we may or may not find a
1888     // recognizable call. We will always find a call for static
1889     // calls and for optimized virtual calls. For vanilla virtual
1890     // calls it depends on the state of the UseInlineCaches switch.
1891     //
1892     // With Inline Caches disabled we can get here for a virtual call
1893     // for two reasons:
1894     //   1 - calling an abstract method. The vtable for abstract methods
1895     //       will run us thru handle_wrong_method and we will eventually
1896     //       end up in the interpreter to throw the ame.
1897     //   2 - a racing deoptimization. We could be doing a vanilla vtable
1898     //       call and between the time we fetch the entry address and
1899     //       we jump to it the target gets deoptimized. Similar to 1
1900     //       we will wind up in the interprter (thru a c2i with c2).
1901     //
1902     address call_addr = nullptr;
1903     {
1904       // Get call instruction under lock because another thread may be
1905       // busy patching it.
1906       CompiledICLocker ml(caller_nm);
1907       // Location of call instruction
1908       call_addr = caller_nm->call_instruction_address(pc);
1909     }
1910 
1911     // Check relocations for the matching call to 1) avoid false positives,
1912     // and 2) determine the type.
1913     if (call_addr != nullptr) {
1914       // On x86 the logic for finding a call instruction is blindly checking for a call opcode 5
1915       // bytes back in the instruction stream so we must also check for reloc info.
1916       RelocIterator iter(caller_nm, call_addr, call_addr+1);
1917       bool ret = iter.next(); // Get item
1918       if (ret) {
1919         bool is_static_call = false;
1920         switch (iter.type()) {
1921           case relocInfo::static_call_type:
1922             is_static_call = true;
1923 
1924           case relocInfo::virtual_call_type:
1925           case relocInfo::opt_virtual_call_type:
1926             // Cleaning the inline cache will force a new resolve. This is more robust
1927             // than directly setting it to the new destination, since resolving of calls
1928             // is always done through the same code path. (experience shows that it
1929             // leads to very hard to track down bugs, if an inline cache gets updated
1930             // to a wrong method). It should not be performance critical, since the
1931             // resolve is only done once.
1932             guarantee(iter.addr() == call_addr, "must find call");
1933             for (;;) {
1934               ICRefillVerifier ic_refill_verifier;
1935               if (!clear_ic_at_addr(caller_nm, call_addr, is_static_call)) {
1936                 InlineCacheBuffer::refill_ic_stubs();
1937               } else {
1938                 break;
1939               }
1940             }
1941             break;
1942           default:
1943             break;
1944         }
1945       }
1946     }
1947   }
1948 
1949   methodHandle callee_method = find_callee_method(CHECK_(methodHandle()));
1950 
1951 
1952 #ifndef PRODUCT
1953   Atomic::inc(&_wrong_method_ctr);
1954 
1955   if (TraceCallFixup) {
1956     ResourceMark rm(current);
1957     tty->print("handle_wrong_method reresolving call to");
1958     callee_method->print_short_name(tty);
1959     tty->print_cr(" code: " INTPTR_FORMAT, p2i(callee_method->code()));
1960   }
1961 #endif
1962 
1963   return callee_method;
1964 }
1965 
1966 address SharedRuntime::handle_unsafe_access(JavaThread* thread, address next_pc) {
1967   // The faulting unsafe accesses should be changed to throw the error
1968   // synchronously instead. Meanwhile the faulting instruction will be
1969   // skipped over (effectively turning it into a no-op) and an
1970   // asynchronous exception will be raised which the thread will
1971   // handle at a later point. If the instruction is a load it will
1972   // return garbage.
1973 
1974   // Request an async exception.
1975   thread->set_pending_unsafe_access_error();
1976 
1977   // Return address of next instruction to execute.
1978   return next_pc;
1979 }
1980 
1981 #ifdef ASSERT
1982 void SharedRuntime::check_member_name_argument_is_last_argument(const methodHandle& method,
1983                                                                 const BasicType* sig_bt,
1984                                                                 const VMRegPair* regs) {
1985   ResourceMark rm;
1986   const int total_args_passed = method->size_of_parameters();
1987   const VMRegPair*    regs_with_member_name = regs;
1988         VMRegPair* regs_without_member_name = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed - 1);
1989 
1990   const int member_arg_pos = total_args_passed - 1;
1991   assert(member_arg_pos >= 0 && member_arg_pos < total_args_passed, "oob");
1992   assert(sig_bt[member_arg_pos] == T_OBJECT, "dispatch argument must be an object");
1993 
1994   java_calling_convention(sig_bt, regs_without_member_name, total_args_passed - 1);
1995 
1996   for (int i = 0; i < member_arg_pos; i++) {
1997     VMReg a =    regs_with_member_name[i].first();
1998     VMReg b = regs_without_member_name[i].first();
1999     assert(a->value() == b->value(), "register allocation mismatch: a= %d, b= %d", a->value(), b->value());
2000   }
2001   assert(regs_with_member_name[member_arg_pos].first()->is_valid(), "bad member arg");
2002 }
2003 #endif
2004 
2005 bool SharedRuntime::should_fixup_call_destination(address destination, address entry_point, address caller_pc, Method* moop, CodeBlob* cb) {
2006   if (destination != entry_point) {
2007     CodeBlob* callee = CodeCache::find_blob(destination);
2008     // callee == cb seems weird. It means calling interpreter thru stub.
2009     if (callee != nullptr && (callee == cb || callee->is_adapter_blob())) {
2010       // static call or optimized virtual
2011       if (TraceCallFixup) {
2012         tty->print("fixup callsite           at " INTPTR_FORMAT " to compiled code for", p2i(caller_pc));
2013         moop->print_short_name(tty);
2014         tty->print_cr(" to " INTPTR_FORMAT, p2i(entry_point));
2015       }
2016       return true;
2017     } else {
2018       if (TraceCallFixup) {
2019         tty->print("failed to fixup callsite at " INTPTR_FORMAT " to compiled code for", p2i(caller_pc));
2020         moop->print_short_name(tty);
2021         tty->print_cr(" to " INTPTR_FORMAT, p2i(entry_point));
2022       }
2023       // assert is too strong could also be resolve destinations.
2024       // assert(InlineCacheBuffer::contains(destination) || VtableStubs::contains(destination), "must be");
2025     }
2026   } else {
2027     if (TraceCallFixup) {
2028       tty->print("already patched callsite at " INTPTR_FORMAT " to compiled code for", p2i(caller_pc));
2029       moop->print_short_name(tty);
2030       tty->print_cr(" to " INTPTR_FORMAT, p2i(entry_point));
2031     }
2032   }
2033   return false;
2034 }
2035 
2036 // ---------------------------------------------------------------------------
2037 // We are calling the interpreter via a c2i. Normally this would mean that
2038 // we were called by a compiled method. However we could have lost a race
2039 // where we went int -> i2c -> c2i and so the caller could in fact be
2040 // interpreted. If the caller is compiled we attempt to patch the caller
2041 // so he no longer calls into the interpreter.
2042 JRT_LEAF(void, SharedRuntime::fixup_callers_callsite(Method* method, address caller_pc))
2043   Method* moop(method);
2044 
2045   AARCH64_PORT_ONLY(assert(pauth_ptr_is_raw(caller_pc), "should be raw"));
2046 
2047   // It's possible that deoptimization can occur at a call site which hasn't
2048   // been resolved yet, in which case this function will be called from
2049   // an nmethod that has been patched for deopt and we can ignore the
2050   // request for a fixup.
2051   // Also it is possible that we lost a race in that from_compiled_entry
2052   // is now back to the i2c in that case we don't need to patch and if
2053   // we did we'd leap into space because the callsite needs to use
2054   // "to interpreter" stub in order to load up the Method*. Don't
2055   // ask me how I know this...
2056 
2057   // Result from nmethod::is_unloading is not stable across safepoints.
2058   NoSafepointVerifier nsv;
2059 
2060   CompiledMethod* callee = moop->code();
2061   if (callee == nullptr) {
2062     return;
2063   }
2064 
2065   // write lock needed because we might update the pc desc cache via PcDescCache::add_pc_desc
2066   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, JavaThread::current()));
2067 
2068   CodeBlob* cb = CodeCache::find_blob(caller_pc);
2069   if (cb == nullptr || !cb->is_compiled() || callee->is_unloading()) {
2070     return;
2071   }
2072 
2073   // The check above makes sure this is a nmethod.
2074   CompiledMethod* nm = cb->as_compiled_method_or_null();
2075   assert(nm, "must be");
2076 
2077   // Get the return PC for the passed caller PC.
2078   address return_pc = caller_pc + frame::pc_return_offset;
2079 
2080   // There is a benign race here. We could be attempting to patch to a compiled
2081   // entry point at the same time the callee is being deoptimized. If that is
2082   // the case then entry_point may in fact point to a c2i and we'd patch the
2083   // call site with the same old data. clear_code will set code() to null
2084   // at the end of it. If we happen to see that null then we can skip trying
2085   // to patch. If we hit the window where the callee has a c2i in the
2086   // from_compiled_entry and the null isn't present yet then we lose the race
2087   // and patch the code with the same old data. Asi es la vida.
2088 
2089   if (moop->code() == nullptr) return;
2090 
2091   if (nm->is_in_use()) {
2092     // Expect to find a native call there (unless it was no-inline cache vtable dispatch)
2093     CompiledICLocker ic_locker(nm);
2094     if (NativeCall::is_call_before(return_pc)) {
2095       ResourceMark mark;
2096       NativeCallWrapper* call = nm->call_wrapper_before(return_pc);
2097       //
2098       // bug 6281185. We might get here after resolving a call site to a vanilla
2099       // virtual call. Because the resolvee uses the verified entry it may then
2100       // see compiled code and attempt to patch the site by calling us. This would
2101       // then incorrectly convert the call site to optimized and its downhill from
2102       // there. If you're lucky you'll get the assert in the bugid, if not you've
2103       // just made a call site that could be megamorphic into a monomorphic site
2104       // for the rest of its life! Just another racing bug in the life of
2105       // fixup_callers_callsite ...
2106       //
2107       RelocIterator iter(nm, call->instruction_address(), call->next_instruction_address());
2108       iter.next();
2109       assert(iter.has_current(), "must have a reloc at java call site");
2110       relocInfo::relocType typ = iter.reloc()->type();
2111       if (typ != relocInfo::static_call_type &&
2112            typ != relocInfo::opt_virtual_call_type &&
2113            typ != relocInfo::static_stub_type) {
2114         return;
2115       }
2116       if (nm->method()->is_continuation_enter_intrinsic()) {
2117         if (ContinuationEntry::is_interpreted_call(call->instruction_address())) {
2118           return;
2119         }
2120       }
2121       address destination = call->destination();
2122       address entry_point = callee->verified_entry_point();
2123       if (should_fixup_call_destination(destination, entry_point, caller_pc, moop, cb)) {
2124         call->set_destination_mt_safe(entry_point);
2125       }
2126     }
2127   }
2128 JRT_END
2129 
2130 
2131 // same as JVM_Arraycopy, but called directly from compiled code
2132 JRT_ENTRY(void, SharedRuntime::slow_arraycopy_C(oopDesc* src,  jint src_pos,
2133                                                 oopDesc* dest, jint dest_pos,
2134                                                 jint length,
2135                                                 JavaThread* current)) {
2136 #ifndef PRODUCT
2137   _slow_array_copy_ctr++;
2138 #endif
2139   // Check if we have null pointers
2140   if (src == nullptr || dest == nullptr) {
2141     THROW(vmSymbols::java_lang_NullPointerException());
2142   }
2143   // Do the copy.  The casts to arrayOop are necessary to the copy_array API,
2144   // even though the copy_array API also performs dynamic checks to ensure
2145   // that src and dest are truly arrays (and are conformable).
2146   // The copy_array mechanism is awkward and could be removed, but
2147   // the compilers don't call this function except as a last resort,
2148   // so it probably doesn't matter.
2149   src->klass()->copy_array((arrayOopDesc*)src, src_pos,
2150                                         (arrayOopDesc*)dest, dest_pos,
2151                                         length, current);
2152 }
2153 JRT_END
2154 
2155 // The caller of generate_class_cast_message() (or one of its callers)
2156 // must use a ResourceMark in order to correctly free the result.
2157 char* SharedRuntime::generate_class_cast_message(
2158     JavaThread* thread, Klass* caster_klass) {
2159 
2160   // Get target class name from the checkcast instruction
2161   vframeStream vfst(thread, true);
2162   assert(!vfst.at_end(), "Java frame must exist");
2163   Bytecode_checkcast cc(vfst.method(), vfst.method()->bcp_from(vfst.bci()));
2164   constantPoolHandle cpool(thread, vfst.method()->constants());
2165   Klass* target_klass = ConstantPool::klass_at_if_loaded(cpool, cc.index());
2166   Symbol* target_klass_name = nullptr;
2167   if (target_klass == nullptr) {
2168     // This klass should be resolved, but just in case, get the name in the klass slot.
2169     target_klass_name = cpool->klass_name_at(cc.index());
2170   }
2171   return generate_class_cast_message(caster_klass, target_klass, target_klass_name);
2172 }
2173 
2174 
2175 // The caller of generate_class_cast_message() (or one of its callers)
2176 // must use a ResourceMark in order to correctly free the result.
2177 char* SharedRuntime::generate_class_cast_message(
2178     Klass* caster_klass, Klass* target_klass, Symbol* target_klass_name) {
2179   const char* caster_name = caster_klass->external_name();
2180 
2181   assert(target_klass != nullptr || target_klass_name != nullptr, "one must be provided");
2182   const char* target_name = target_klass == nullptr ? target_klass_name->as_klass_external_name() :
2183                                                    target_klass->external_name();
2184 
2185   size_t msglen = strlen(caster_name) + strlen("class ") + strlen(" cannot be cast to class ") + strlen(target_name) + 1;
2186 
2187   const char* caster_klass_description = "";
2188   const char* target_klass_description = "";
2189   const char* klass_separator = "";
2190   if (target_klass != nullptr && caster_klass->module() == target_klass->module()) {
2191     caster_klass_description = caster_klass->joint_in_module_of_loader(target_klass);
2192   } else {
2193     caster_klass_description = caster_klass->class_in_module_of_loader();
2194     target_klass_description = (target_klass != nullptr) ? target_klass->class_in_module_of_loader() : "";
2195     klass_separator = (target_klass != nullptr) ? "; " : "";
2196   }
2197 
2198   // add 3 for parenthesis and preceding space
2199   msglen += strlen(caster_klass_description) + strlen(target_klass_description) + strlen(klass_separator) + 3;
2200 
2201   char* message = NEW_RESOURCE_ARRAY_RETURN_NULL(char, msglen);
2202   if (message == nullptr) {
2203     // Shouldn't happen, but don't cause even more problems if it does
2204     message = const_cast<char*>(caster_klass->external_name());
2205   } else {
2206     jio_snprintf(message,
2207                  msglen,
2208                  "class %s cannot be cast to class %s (%s%s%s)",
2209                  caster_name,
2210                  target_name,
2211                  caster_klass_description,
2212                  klass_separator,
2213                  target_klass_description
2214                  );
2215   }
2216   return message;
2217 }
2218 
2219 JRT_LEAF(void, SharedRuntime::reguard_yellow_pages())
2220   (void) JavaThread::current()->stack_overflow_state()->reguard_stack();
2221 JRT_END
2222 
2223 void SharedRuntime::monitor_enter_helper(oopDesc* obj, BasicLock* lock, JavaThread* current) {
2224   if (!SafepointSynchronize::is_synchronizing()) {
2225     // Only try quick_enter() if we're not trying to reach a safepoint
2226     // so that the calling thread reaches the safepoint more quickly.
2227     if (ObjectSynchronizer::quick_enter(obj, current, lock)) {
2228       return;
2229     }
2230   }
2231   // NO_ASYNC required because an async exception on the state transition destructor
2232   // would leave you with the lock held and it would never be released.
2233   // The normal monitorenter NullPointerException is thrown without acquiring a lock
2234   // and the model is that an exception implies the method failed.
2235   JRT_BLOCK_NO_ASYNC
2236   Handle h_obj(THREAD, obj);
2237   ObjectSynchronizer::enter(h_obj, lock, current);
2238   assert(!HAS_PENDING_EXCEPTION, "Should have no exception here");
2239   JRT_BLOCK_END
2240 }
2241 
2242 // Handles the uncommon case in locking, i.e., contention or an inflated lock.
2243 JRT_BLOCK_ENTRY(void, SharedRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current))
2244   SharedRuntime::monitor_enter_helper(obj, lock, current);
2245 JRT_END
2246 
2247 void SharedRuntime::monitor_exit_helper(oopDesc* obj, BasicLock* lock, JavaThread* current) {
2248   assert(JavaThread::current() == current, "invariant");
2249   // Exit must be non-blocking, and therefore no exceptions can be thrown.
2250   ExceptionMark em(current);
2251   // The object could become unlocked through a JNI call, which we have no other checks for.
2252   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
2253   if (obj->is_unlocked()) {
2254     if (CheckJNICalls) {
2255       fatal("Object has been unlocked by JNI");
2256     }
2257     return;
2258   }
2259   ObjectSynchronizer::exit(obj, lock, current);
2260 }
2261 
2262 // Handles the uncommon cases of monitor unlocking in compiled code
2263 JRT_LEAF(void, SharedRuntime::complete_monitor_unlocking_C(oopDesc* obj, BasicLock* lock, JavaThread* current))
2264   assert(current == JavaThread::current(), "pre-condition");
2265   SharedRuntime::monitor_exit_helper(obj, lock, current);
2266 JRT_END
2267 
2268 #ifndef PRODUCT
2269 
2270 void SharedRuntime::print_statistics() {
2271   ttyLocker ttyl;
2272   if (xtty != nullptr)  xtty->head("statistics type='SharedRuntime'");
2273 
2274   SharedRuntime::print_ic_miss_histogram();
2275 
2276   // Dump the JRT_ENTRY counters
2277   if (_new_instance_ctr) tty->print_cr("%5d new instance requires GC", _new_instance_ctr);
2278   if (_new_array_ctr) tty->print_cr("%5d new array requires GC", _new_array_ctr);
2279   if (_multi2_ctr) tty->print_cr("%5d multianewarray 2 dim", _multi2_ctr);
2280   if (_multi3_ctr) tty->print_cr("%5d multianewarray 3 dim", _multi3_ctr);
2281   if (_multi4_ctr) tty->print_cr("%5d multianewarray 4 dim", _multi4_ctr);
2282   if (_multi5_ctr) tty->print_cr("%5d multianewarray 5 dim", _multi5_ctr);
2283 
2284   tty->print_cr("%5d inline cache miss in compiled", _ic_miss_ctr);
2285   tty->print_cr("%5d wrong method", _wrong_method_ctr);
2286   tty->print_cr("%5d unresolved static call site", _resolve_static_ctr);
2287   tty->print_cr("%5d unresolved virtual call site", _resolve_virtual_ctr);
2288   tty->print_cr("%5d unresolved opt virtual call site", _resolve_opt_virtual_ctr);
2289 
2290   if (_mon_enter_stub_ctr) tty->print_cr("%5d monitor enter stub", _mon_enter_stub_ctr);
2291   if (_mon_exit_stub_ctr) tty->print_cr("%5d monitor exit stub", _mon_exit_stub_ctr);
2292   if (_mon_enter_ctr) tty->print_cr("%5d monitor enter slow", _mon_enter_ctr);
2293   if (_mon_exit_ctr) tty->print_cr("%5d monitor exit slow", _mon_exit_ctr);
2294   if (_partial_subtype_ctr) tty->print_cr("%5d slow partial subtype", _partial_subtype_ctr);
2295   if (_jbyte_array_copy_ctr) tty->print_cr("%5d byte array copies", _jbyte_array_copy_ctr);
2296   if (_jshort_array_copy_ctr) tty->print_cr("%5d short array copies", _jshort_array_copy_ctr);
2297   if (_jint_array_copy_ctr) tty->print_cr("%5d int array copies", _jint_array_copy_ctr);
2298   if (_jlong_array_copy_ctr) tty->print_cr("%5d long array copies", _jlong_array_copy_ctr);
2299   if (_oop_array_copy_ctr) tty->print_cr("%5d oop array copies", _oop_array_copy_ctr);
2300   if (_checkcast_array_copy_ctr) tty->print_cr("%5d checkcast array copies", _checkcast_array_copy_ctr);
2301   if (_unsafe_array_copy_ctr) tty->print_cr("%5d unsafe array copies", _unsafe_array_copy_ctr);
2302   if (_generic_array_copy_ctr) tty->print_cr("%5d generic array copies", _generic_array_copy_ctr);
2303   if (_slow_array_copy_ctr) tty->print_cr("%5d slow array copies", _slow_array_copy_ctr);
2304   if (_find_handler_ctr) tty->print_cr("%5d find exception handler", _find_handler_ctr);
2305   if (_rethrow_ctr) tty->print_cr("%5d rethrow handler", _rethrow_ctr);
2306 
2307   AdapterHandlerLibrary::print_statistics();
2308 
2309   if (xtty != nullptr)  xtty->tail("statistics");
2310 }
2311 
2312 inline double percent(int64_t x, int64_t y) {
2313   return 100.0 * x / MAX2(y, (int64_t)1);
2314 }
2315 
2316 class MethodArityHistogram {
2317  public:
2318   enum { MAX_ARITY = 256 };
2319  private:
2320   static uint64_t _arity_histogram[MAX_ARITY]; // histogram of #args
2321   static uint64_t _size_histogram[MAX_ARITY];  // histogram of arg size in words
2322   static uint64_t _total_compiled_calls;
2323   static uint64_t _max_compiled_calls_per_method;
2324   static int _max_arity;                       // max. arity seen
2325   static int _max_size;                        // max. arg size seen
2326 
2327   static void add_method_to_histogram(nmethod* nm) {
2328     Method* method = (nm == nullptr) ? nullptr : nm->method();
2329     if (method != nullptr) {
2330       ArgumentCount args(method->signature());
2331       int arity   = args.size() + (method->is_static() ? 0 : 1);
2332       int argsize = method->size_of_parameters();
2333       arity   = MIN2(arity, MAX_ARITY-1);
2334       argsize = MIN2(argsize, MAX_ARITY-1);
2335       uint64_t count = (uint64_t)method->compiled_invocation_count();
2336       _max_compiled_calls_per_method = count > _max_compiled_calls_per_method ? count : _max_compiled_calls_per_method;
2337       _total_compiled_calls    += count;
2338       _arity_histogram[arity]  += count;
2339       _size_histogram[argsize] += count;
2340       _max_arity = MAX2(_max_arity, arity);
2341       _max_size  = MAX2(_max_size, argsize);
2342     }
2343   }
2344 
2345   void print_histogram_helper(int n, uint64_t* histo, const char* name) {
2346     const int N = MIN2(9, n);
2347     double sum = 0;
2348     double weighted_sum = 0;
2349     for (int i = 0; i <= n; i++) { sum += histo[i]; weighted_sum += i*histo[i]; }
2350     if (sum >= 1.0) { // prevent divide by zero or divide overflow
2351       double rest = sum;
2352       double percent = sum / 100;
2353       for (int i = 0; i <= N; i++) {
2354         rest -= histo[i];
2355         tty->print_cr("%4d: " UINT64_FORMAT_W(12) " (%5.1f%%)", i, histo[i], histo[i] / percent);
2356       }
2357       tty->print_cr("rest: " INT64_FORMAT_W(12) " (%5.1f%%)", (int64_t)rest, rest / percent);
2358       tty->print_cr("(avg. %s = %3.1f, max = %d)", name, weighted_sum / sum, n);
2359       tty->print_cr("(total # of compiled calls = " INT64_FORMAT_W(14) ")", _total_compiled_calls);
2360       tty->print_cr("(max # of compiled calls   = " INT64_FORMAT_W(14) ")", _max_compiled_calls_per_method);
2361     } else {
2362       tty->print_cr("Histogram generation failed for %s. n = %d, sum = %7.5f", name, n, sum);
2363     }
2364   }
2365 
2366   void print_histogram() {
2367     tty->print_cr("\nHistogram of call arity (incl. rcvr, calls to compiled methods only):");
2368     print_histogram_helper(_max_arity, _arity_histogram, "arity");
2369     tty->print_cr("\nHistogram of parameter block size (in words, incl. rcvr):");
2370     print_histogram_helper(_max_size, _size_histogram, "size");
2371     tty->cr();
2372   }
2373 
2374  public:
2375   MethodArityHistogram() {
2376     // Take the Compile_lock to protect against changes in the CodeBlob structures
2377     MutexLocker mu1(Compile_lock, Mutex::_safepoint_check_flag);
2378     // Take the CodeCache_lock to protect against changes in the CodeHeap structure
2379     MutexLocker mu2(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2380     _max_arity = _max_size = 0;
2381     _total_compiled_calls = 0;
2382     _max_compiled_calls_per_method = 0;
2383     for (int i = 0; i < MAX_ARITY; i++) _arity_histogram[i] = _size_histogram[i] = 0;
2384     CodeCache::nmethods_do(add_method_to_histogram);
2385     print_histogram();
2386   }
2387 };
2388 
2389 uint64_t MethodArityHistogram::_arity_histogram[MethodArityHistogram::MAX_ARITY];
2390 uint64_t MethodArityHistogram::_size_histogram[MethodArityHistogram::MAX_ARITY];
2391 uint64_t MethodArityHistogram::_total_compiled_calls;
2392 uint64_t MethodArityHistogram::_max_compiled_calls_per_method;
2393 int MethodArityHistogram::_max_arity;
2394 int MethodArityHistogram::_max_size;
2395 
2396 void SharedRuntime::print_call_statistics(uint64_t comp_total) {
2397   tty->print_cr("Calls from compiled code:");
2398   int64_t total  = _nof_normal_calls + _nof_interface_calls + _nof_static_calls;
2399   int64_t mono_c = _nof_normal_calls - _nof_megamorphic_calls;
2400   int64_t mono_i = _nof_interface_calls;
2401   tty->print_cr("\t" INT64_FORMAT_W(12) " (100%%)  total non-inlined   ", total);
2402   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.1f%%) |- virtual calls       ", _nof_normal_calls, percent(_nof_normal_calls, total));
2403   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- inlined          ", _nof_inlined_calls, percent(_nof_inlined_calls, _nof_normal_calls));
2404   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- monomorphic      ", mono_c, percent(mono_c, _nof_normal_calls));
2405   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- megamorphic      ", _nof_megamorphic_calls, percent(_nof_megamorphic_calls, _nof_normal_calls));
2406   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.1f%%) |- interface calls     ", _nof_interface_calls, percent(_nof_interface_calls, total));
2407   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- inlined          ", _nof_inlined_interface_calls, percent(_nof_inlined_interface_calls, _nof_interface_calls));
2408   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- monomorphic      ", mono_i, percent(mono_i, _nof_interface_calls));
2409   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.1f%%) |- static/special calls", _nof_static_calls, percent(_nof_static_calls, total));
2410   tty->print_cr("\t" INT64_FORMAT_W(12) " (%4.0f%%) |  |- inlined          ", _nof_inlined_static_calls, percent(_nof_inlined_static_calls, _nof_static_calls));
2411   tty->cr();
2412   tty->print_cr("Note 1: counter updates are not MT-safe.");
2413   tty->print_cr("Note 2: %% in major categories are relative to total non-inlined calls;");
2414   tty->print_cr("        %% in nested categories are relative to their category");
2415   tty->print_cr("        (and thus add up to more than 100%% with inlining)");
2416   tty->cr();
2417 
2418   MethodArityHistogram h;
2419 }
2420 #endif
2421 
2422 #ifndef PRODUCT
2423 static int _lookups; // number of calls to lookup
2424 static int _equals;  // number of buckets checked with matching hash
2425 static int _hits;    // number of successful lookups
2426 static int _compact; // number of equals calls with compact signature
2427 #endif
2428 
2429 // A simple wrapper class around the calling convention information
2430 // that allows sharing of adapters for the same calling convention.
2431 class AdapterFingerPrint : public CHeapObj<mtCode> {
2432  private:
2433   enum {
2434     _basic_type_bits = 4,
2435     _basic_type_mask = right_n_bits(_basic_type_bits),
2436     _basic_types_per_int = BitsPerInt / _basic_type_bits,
2437     _compact_int_count = 3
2438   };
2439   // TO DO:  Consider integrating this with a more global scheme for compressing signatures.
2440   // For now, 4 bits per components (plus T_VOID gaps after double/long) is not excessive.
2441 
2442   union {
2443     int  _compact[_compact_int_count];
2444     int* _fingerprint;
2445   } _value;
2446   int _length; // A negative length indicates the fingerprint is in the compact form,
2447                // Otherwise _value._fingerprint is the array.
2448 
2449   // Remap BasicTypes that are handled equivalently by the adapters.
2450   // These are correct for the current system but someday it might be
2451   // necessary to make this mapping platform dependent.
2452   static int adapter_encoding(BasicType in) {
2453     switch (in) {
2454       case T_BOOLEAN:
2455       case T_BYTE:
2456       case T_SHORT:
2457       case T_CHAR:
2458         // There are all promoted to T_INT in the calling convention
2459         return T_INT;
2460 
2461       case T_OBJECT:
2462       case T_ARRAY:
2463         // In other words, we assume that any register good enough for
2464         // an int or long is good enough for a managed pointer.
2465 #ifdef _LP64
2466         return T_LONG;
2467 #else
2468         return T_INT;
2469 #endif
2470 
2471       case T_INT:
2472       case T_LONG:
2473       case T_FLOAT:
2474       case T_DOUBLE:
2475       case T_VOID:
2476         return in;
2477 
2478       default:
2479         ShouldNotReachHere();
2480         return T_CONFLICT;
2481     }
2482   }
2483 
2484  public:
2485   AdapterFingerPrint(int total_args_passed, BasicType* sig_bt) {
2486     // The fingerprint is based on the BasicType signature encoded
2487     // into an array of ints with eight entries per int.
2488     int* ptr;
2489     int len = (total_args_passed + (_basic_types_per_int-1)) / _basic_types_per_int;
2490     if (len <= _compact_int_count) {
2491       assert(_compact_int_count == 3, "else change next line");
2492       _value._compact[0] = _value._compact[1] = _value._compact[2] = 0;
2493       // Storing the signature encoded as signed chars hits about 98%
2494       // of the time.
2495       _length = -len;
2496       ptr = _value._compact;
2497     } else {
2498       _length = len;
2499       _value._fingerprint = NEW_C_HEAP_ARRAY(int, _length, mtCode);
2500       ptr = _value._fingerprint;
2501     }
2502 
2503     // Now pack the BasicTypes with 8 per int
2504     int sig_index = 0;
2505     for (int index = 0; index < len; index++) {
2506       int value = 0;
2507       for (int byte = 0; sig_index < total_args_passed && byte < _basic_types_per_int; byte++) {
2508         int bt = adapter_encoding(sig_bt[sig_index++]);
2509         assert((bt & _basic_type_mask) == bt, "must fit in 4 bits");
2510         value = (value << _basic_type_bits) | bt;
2511       }
2512       ptr[index] = value;
2513     }
2514   }
2515 
2516   ~AdapterFingerPrint() {
2517     if (_length > 0) {
2518       FREE_C_HEAP_ARRAY(int, _value._fingerprint);
2519     }
2520   }
2521 
2522   int value(int index) {
2523     if (_length < 0) {
2524       return _value._compact[index];
2525     }
2526     return _value._fingerprint[index];
2527   }
2528   int length() {
2529     if (_length < 0) return -_length;
2530     return _length;
2531   }
2532 
2533   bool is_compact() {
2534     return _length <= 0;
2535   }
2536 
2537   unsigned int compute_hash() {
2538     int hash = 0;
2539     for (int i = 0; i < length(); i++) {
2540       int v = value(i);
2541       hash = (hash << 8) ^ v ^ (hash >> 5);
2542     }
2543     return (unsigned int)hash;
2544   }
2545 
2546   const char* as_string() {
2547     stringStream st;
2548     st.print("0x");
2549     for (int i = 0; i < length(); i++) {
2550       st.print("%x", value(i));
2551     }
2552     return st.as_string();
2553   }
2554 
2555 #ifndef PRODUCT
2556   // Reconstitutes the basic type arguments from the fingerprint,
2557   // producing strings like LIJDF
2558   const char* as_basic_args_string() {
2559     stringStream st;
2560     bool long_prev = false;
2561     for (int i = 0; i < length(); i++) {
2562       unsigned val = (unsigned)value(i);
2563       // args are packed so that first/lower arguments are in the highest
2564       // bits of each int value, so iterate from highest to the lowest
2565       for (int j = 32 - _basic_type_bits; j >= 0; j -= _basic_type_bits) {
2566         unsigned v = (val >> j) & _basic_type_mask;
2567         if (v == 0) {
2568           assert(i == length() - 1, "Only expect zeroes in the last word");
2569           continue;
2570         }
2571         if (long_prev) {
2572           long_prev = false;
2573           if (v == T_VOID) {
2574             st.print("J");
2575           } else {
2576             st.print("L");
2577           }
2578         }
2579         switch (v) {
2580           case T_INT:    st.print("I");    break;
2581           case T_LONG:   long_prev = true; break;
2582           case T_FLOAT:  st.print("F");    break;
2583           case T_DOUBLE: st.print("D");    break;
2584           case T_VOID:   break;
2585           default: ShouldNotReachHere();
2586         }
2587       }
2588     }
2589     if (long_prev) {
2590       st.print("L");
2591     }
2592     return st.as_string();
2593   }
2594 #endif // !product
2595 
2596   bool equals(AdapterFingerPrint* other) {
2597     if (other->_length != _length) {
2598       return false;
2599     }
2600     if (_length < 0) {
2601       assert(_compact_int_count == 3, "else change next line");
2602       return _value._compact[0] == other->_value._compact[0] &&
2603              _value._compact[1] == other->_value._compact[1] &&
2604              _value._compact[2] == other->_value._compact[2];
2605     } else {
2606       for (int i = 0; i < _length; i++) {
2607         if (_value._fingerprint[i] != other->_value._fingerprint[i]) {
2608           return false;
2609         }
2610       }
2611     }
2612     return true;
2613   }
2614 
2615   static bool equals(AdapterFingerPrint* const& fp1, AdapterFingerPrint* const& fp2) {
2616     NOT_PRODUCT(_equals++);
2617     return fp1->equals(fp2);
2618   }
2619 
2620   static unsigned int compute_hash(AdapterFingerPrint* const& fp) {
2621     return fp->compute_hash();
2622   }
2623 };
2624 
2625 // A hashtable mapping from AdapterFingerPrints to AdapterHandlerEntries
2626 using AdapterHandlerTable = ResourceHashtable<AdapterFingerPrint*, AdapterHandlerEntry*, 293,
2627                   AnyObj::C_HEAP, mtCode,
2628                   AdapterFingerPrint::compute_hash,
2629                   AdapterFingerPrint::equals>;
2630 static AdapterHandlerTable* _adapter_handler_table;
2631 
2632 // Find a entry with the same fingerprint if it exists
2633 static AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt) {
2634   NOT_PRODUCT(_lookups++);
2635   assert_lock_strong(AdapterHandlerLibrary_lock);
2636   AdapterFingerPrint fp(total_args_passed, sig_bt);
2637   AdapterHandlerEntry** entry = _adapter_handler_table->get(&fp);
2638   if (entry != nullptr) {
2639 #ifndef PRODUCT
2640     if (fp.is_compact()) _compact++;
2641     _hits++;
2642 #endif
2643     return *entry;
2644   }
2645   return nullptr;
2646 }
2647 
2648 #ifndef PRODUCT
2649 static void print_table_statistics() {
2650   auto size = [&] (AdapterFingerPrint* key, AdapterHandlerEntry* a) {
2651     return sizeof(*key) + sizeof(*a);
2652   };
2653   TableStatistics ts = _adapter_handler_table->statistics_calculate(size);
2654   ts.print(tty, "AdapterHandlerTable");
2655   tty->print_cr("AdapterHandlerTable (table_size=%d, entries=%d)",
2656                 _adapter_handler_table->table_size(), _adapter_handler_table->number_of_entries());
2657   tty->print_cr("AdapterHandlerTable: lookups %d equals %d hits %d compact %d",
2658                 _lookups, _equals, _hits, _compact);
2659 }
2660 #endif
2661 
2662 // ---------------------------------------------------------------------------
2663 // Implementation of AdapterHandlerLibrary
2664 AdapterHandlerEntry* AdapterHandlerLibrary::_abstract_method_handler = nullptr;
2665 AdapterHandlerEntry* AdapterHandlerLibrary::_no_arg_handler = nullptr;
2666 AdapterHandlerEntry* AdapterHandlerLibrary::_int_arg_handler = nullptr;
2667 AdapterHandlerEntry* AdapterHandlerLibrary::_obj_arg_handler = nullptr;
2668 AdapterHandlerEntry* AdapterHandlerLibrary::_obj_int_arg_handler = nullptr;
2669 AdapterHandlerEntry* AdapterHandlerLibrary::_obj_obj_arg_handler = nullptr;
2670 const int AdapterHandlerLibrary_size = 16*K;
2671 BufferBlob* AdapterHandlerLibrary::_buffer = nullptr;
2672 
2673 BufferBlob* AdapterHandlerLibrary::buffer_blob() {
2674   return _buffer;
2675 }
2676 
2677 static void post_adapter_creation(const AdapterBlob* new_adapter,
2678                                   const AdapterHandlerEntry* entry) {
2679   if (Forte::is_enabled() || JvmtiExport::should_post_dynamic_code_generated()) {
2680     char blob_id[256];
2681     jio_snprintf(blob_id,
2682                  sizeof(blob_id),
2683                  "%s(%s)",
2684                  new_adapter->name(),
2685                  entry->fingerprint()->as_string());
2686     if (Forte::is_enabled()) {
2687       Forte::register_stub(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2688     }
2689 
2690     if (JvmtiExport::should_post_dynamic_code_generated()) {
2691       JvmtiExport::post_dynamic_code_generated(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2692     }
2693   }
2694 }
2695 
2696 void AdapterHandlerLibrary::initialize() {
2697   ResourceMark rm;
2698   AdapterBlob* no_arg_blob = nullptr;
2699   AdapterBlob* int_arg_blob = nullptr;
2700   AdapterBlob* obj_arg_blob = nullptr;
2701   AdapterBlob* obj_int_arg_blob = nullptr;
2702   AdapterBlob* obj_obj_arg_blob = nullptr;
2703   {
2704     _adapter_handler_table = new (mtCode) AdapterHandlerTable();
2705     MutexLocker mu(AdapterHandlerLibrary_lock);
2706 
2707     // Create a special handler for abstract methods.  Abstract methods
2708     // are never compiled so an i2c entry is somewhat meaningless, but
2709     // throw AbstractMethodError just in case.
2710     // Pass wrong_method_abstract for the c2i transitions to return
2711     // AbstractMethodError for invalid invocations.
2712     address wrong_method_abstract = SharedRuntime::get_handle_wrong_method_abstract_stub();
2713     _abstract_method_handler = AdapterHandlerLibrary::new_entry(new AdapterFingerPrint(0, nullptr),
2714                                                                 StubRoutines::throw_AbstractMethodError_entry(),
2715                                                                 wrong_method_abstract, wrong_method_abstract);
2716 
2717     _buffer = BufferBlob::create("adapters", AdapterHandlerLibrary_size);
2718     _no_arg_handler = create_adapter(no_arg_blob, 0, nullptr, true);
2719 
2720     BasicType obj_args[] = { T_OBJECT };
2721     _obj_arg_handler = create_adapter(obj_arg_blob, 1, obj_args, true);
2722 
2723     BasicType int_args[] = { T_INT };
2724     _int_arg_handler = create_adapter(int_arg_blob, 1, int_args, true);
2725 
2726     BasicType obj_int_args[] = { T_OBJECT, T_INT };
2727     _obj_int_arg_handler = create_adapter(obj_int_arg_blob, 2, obj_int_args, true);
2728 
2729     BasicType obj_obj_args[] = { T_OBJECT, T_OBJECT };
2730     _obj_obj_arg_handler = create_adapter(obj_obj_arg_blob, 2, obj_obj_args, true);
2731 
2732     assert(no_arg_blob != nullptr &&
2733           obj_arg_blob != nullptr &&
2734           int_arg_blob != nullptr &&
2735           obj_int_arg_blob != nullptr &&
2736           obj_obj_arg_blob != nullptr, "Initial adapters must be properly created");
2737   }
2738 
2739   // Outside of the lock
2740   post_adapter_creation(no_arg_blob, _no_arg_handler);
2741   post_adapter_creation(obj_arg_blob, _obj_arg_handler);
2742   post_adapter_creation(int_arg_blob, _int_arg_handler);
2743   post_adapter_creation(obj_int_arg_blob, _obj_int_arg_handler);
2744   post_adapter_creation(obj_obj_arg_blob, _obj_obj_arg_handler);
2745 }
2746 
2747 AdapterHandlerEntry* AdapterHandlerLibrary::new_entry(AdapterFingerPrint* fingerprint,
2748                                                       address i2c_entry,
2749                                                       address c2i_entry,
2750                                                       address c2i_unverified_entry,
2751                                                       address c2i_no_clinit_check_entry) {
2752   // Insert an entry into the table
2753   return new AdapterHandlerEntry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry,
2754                                  c2i_no_clinit_check_entry);
2755 }
2756 
2757 AdapterHandlerEntry* AdapterHandlerLibrary::get_simple_adapter(const methodHandle& method) {
2758   if (method->is_abstract()) {
2759     return _abstract_method_handler;
2760   }
2761   int total_args_passed = method->size_of_parameters(); // All args on stack
2762   if (total_args_passed == 0) {
2763     return _no_arg_handler;
2764   } else if (total_args_passed == 1) {
2765     if (!method->is_static()) {
2766       return _obj_arg_handler;
2767     }
2768     switch (method->signature()->char_at(1)) {
2769       case JVM_SIGNATURE_CLASS:
2770       case JVM_SIGNATURE_ARRAY:
2771         return _obj_arg_handler;
2772       case JVM_SIGNATURE_INT:
2773       case JVM_SIGNATURE_BOOLEAN:
2774       case JVM_SIGNATURE_CHAR:
2775       case JVM_SIGNATURE_BYTE:
2776       case JVM_SIGNATURE_SHORT:
2777         return _int_arg_handler;
2778     }
2779   } else if (total_args_passed == 2 &&
2780              !method->is_static()) {
2781     switch (method->signature()->char_at(1)) {
2782       case JVM_SIGNATURE_CLASS:
2783       case JVM_SIGNATURE_ARRAY:
2784         return _obj_obj_arg_handler;
2785       case JVM_SIGNATURE_INT:
2786       case JVM_SIGNATURE_BOOLEAN:
2787       case JVM_SIGNATURE_CHAR:
2788       case JVM_SIGNATURE_BYTE:
2789       case JVM_SIGNATURE_SHORT:
2790         return _obj_int_arg_handler;
2791     }
2792   }
2793   return nullptr;
2794 }
2795 
2796 class AdapterSignatureIterator : public SignatureIterator {
2797  private:
2798   BasicType stack_sig_bt[16];
2799   BasicType* sig_bt;
2800   int index;
2801 
2802  public:
2803   AdapterSignatureIterator(Symbol* signature,
2804                            fingerprint_t fingerprint,
2805                            bool is_static,
2806                            int total_args_passed) :
2807     SignatureIterator(signature, fingerprint),
2808     index(0)
2809   {
2810     sig_bt = (total_args_passed <= 16) ? stack_sig_bt : NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2811     if (!is_static) { // Pass in receiver first
2812       sig_bt[index++] = T_OBJECT;
2813     }
2814     do_parameters_on(this);
2815   }
2816 
2817   BasicType* basic_types() {
2818     return sig_bt;
2819   }
2820 
2821 #ifdef ASSERT
2822   int slots() {
2823     return index;
2824   }
2825 #endif
2826 
2827  private:
2828 
2829   friend class SignatureIterator;  // so do_parameters_on can call do_type
2830   void do_type(BasicType type) {
2831     sig_bt[index++] = type;
2832     if (type == T_LONG || type == T_DOUBLE) {
2833       sig_bt[index++] = T_VOID; // Longs & doubles take 2 Java slots
2834     }
2835   }
2836 };
2837 
2838 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(const methodHandle& method) {
2839   // Use customized signature handler.  Need to lock around updates to
2840   // the _adapter_handler_table (it is not safe for concurrent readers
2841   // and a single writer: this could be fixed if it becomes a
2842   // problem).
2843 
2844   // Fast-path for trivial adapters
2845   AdapterHandlerEntry* entry = get_simple_adapter(method);
2846   if (entry != nullptr) {
2847     return entry;
2848   }
2849 
2850   ResourceMark rm;
2851   AdapterBlob* new_adapter = nullptr;
2852 
2853   // Fill in the signature array, for the calling-convention call.
2854   int total_args_passed = method->size_of_parameters(); // All args on stack
2855 
2856   AdapterSignatureIterator si(method->signature(), method->constMethod()->fingerprint(),
2857                               method->is_static(), total_args_passed);
2858   assert(si.slots() == total_args_passed, "");
2859   BasicType* sig_bt = si.basic_types();
2860   {
2861     MutexLocker mu(AdapterHandlerLibrary_lock);
2862 
2863     // Lookup method signature's fingerprint
2864     entry = lookup(total_args_passed, sig_bt);
2865 
2866     if (entry != nullptr) {
2867 #ifdef ASSERT
2868       if (VerifyAdapterSharing) {
2869         AdapterBlob* comparison_blob = nullptr;
2870         AdapterHandlerEntry* comparison_entry = create_adapter(comparison_blob, total_args_passed, sig_bt, false);
2871         assert(comparison_blob == nullptr, "no blob should be created when creating an adapter for comparison");
2872         assert(comparison_entry->compare_code(entry), "code must match");
2873         // Release the one just created and return the original
2874         delete comparison_entry;
2875       }
2876 #endif
2877       return entry;
2878     }
2879 
2880     entry = create_adapter(new_adapter, total_args_passed, sig_bt, /* allocate_code_blob */ true);
2881   }
2882 
2883   // Outside of the lock
2884   if (new_adapter != nullptr) {
2885     post_adapter_creation(new_adapter, entry);
2886   }
2887   return entry;
2888 }
2889 
2890 AdapterHandlerEntry* AdapterHandlerLibrary::create_adapter(AdapterBlob*& new_adapter,
2891                                                            int total_args_passed,
2892                                                            BasicType* sig_bt,
2893                                                            bool allocate_code_blob) {
2894 
2895   // StubRoutines::_final_stubs_code is initialized after this function can be called. As a result,
2896   // VerifyAdapterCalls and VerifyAdapterSharing can fail if we re-use code that generated prior
2897   // to all StubRoutines::_final_stubs_code being set. Checks refer to runtime range checks generated
2898   // in an I2C stub that ensure that an I2C stub is called from an interpreter frame or stubs.
2899   bool contains_all_checks = StubRoutines::final_stubs_code() != nullptr;
2900 
2901   VMRegPair stack_regs[16];
2902   VMRegPair* regs = (total_args_passed <= 16) ? stack_regs : NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2903 
2904   // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2905   int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed);
2906   BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2907   CodeBuffer buffer(buf);
2908   short buffer_locs[20];
2909   buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2910                                           sizeof(buffer_locs)/sizeof(relocInfo));
2911 
2912   // Make a C heap allocated version of the fingerprint to store in the adapter
2913   AdapterFingerPrint* fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
2914   MacroAssembler _masm(&buffer);
2915   AdapterHandlerEntry* entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2916                                                 total_args_passed,
2917                                                 comp_args_on_stack,
2918                                                 sig_bt,
2919                                                 regs,
2920                                                 fingerprint);
2921 
2922 #ifdef ASSERT
2923   if (VerifyAdapterSharing) {
2924     entry->save_code(buf->code_begin(), buffer.insts_size());
2925     if (!allocate_code_blob) {
2926       return entry;
2927     }
2928   }
2929 #endif
2930 
2931   new_adapter = AdapterBlob::create(&buffer);
2932   NOT_PRODUCT(int insts_size = buffer.insts_size());
2933   if (new_adapter == nullptr) {
2934     // CodeCache is full, disable compilation
2935     // Ought to log this but compile log is only per compile thread
2936     // and we're some non descript Java thread.
2937     return nullptr;
2938   }
2939   entry->relocate(new_adapter->content_begin());
2940 #ifndef PRODUCT
2941   // debugging support
2942   if (PrintAdapterHandlers || PrintStubCode) {
2943     ttyLocker ttyl;
2944     entry->print_adapter_on(tty);
2945     tty->print_cr("i2c argument handler #%d for: %s %s (%d bytes generated)",
2946                   _adapter_handler_table->number_of_entries(), fingerprint->as_basic_args_string(),
2947                   fingerprint->as_string(), insts_size);
2948     tty->print_cr("c2i argument handler starts at " INTPTR_FORMAT, p2i(entry->get_c2i_entry()));
2949     if (Verbose || PrintStubCode) {
2950       address first_pc = entry->base_address();
2951       if (first_pc != nullptr) {
2952         Disassembler::decode(first_pc, first_pc + insts_size, tty
2953                              NOT_PRODUCT(COMMA &new_adapter->asm_remarks()));
2954         tty->cr();
2955       }
2956     }
2957   }
2958 #endif
2959 
2960   // Add the entry only if the entry contains all required checks (see sharedRuntime_xxx.cpp)
2961   // The checks are inserted only if -XX:+VerifyAdapterCalls is specified.
2962   if (contains_all_checks || !VerifyAdapterCalls) {
2963     assert_lock_strong(AdapterHandlerLibrary_lock);
2964     _adapter_handler_table->put(fingerprint, entry);
2965   }
2966   return entry;
2967 }
2968 
2969 address AdapterHandlerEntry::base_address() {
2970   address base = _i2c_entry;
2971   if (base == nullptr)  base = _c2i_entry;
2972   assert(base <= _c2i_entry || _c2i_entry == nullptr, "");
2973   assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == nullptr, "");
2974   assert(base <= _c2i_no_clinit_check_entry || _c2i_no_clinit_check_entry == nullptr, "");
2975   return base;
2976 }
2977 
2978 void AdapterHandlerEntry::relocate(address new_base) {
2979   address old_base = base_address();
2980   assert(old_base != nullptr, "");
2981   ptrdiff_t delta = new_base - old_base;
2982   if (_i2c_entry != nullptr)
2983     _i2c_entry += delta;
2984   if (_c2i_entry != nullptr)
2985     _c2i_entry += delta;
2986   if (_c2i_unverified_entry != nullptr)
2987     _c2i_unverified_entry += delta;
2988   if (_c2i_no_clinit_check_entry != nullptr)
2989     _c2i_no_clinit_check_entry += delta;
2990   assert(base_address() == new_base, "");
2991 }
2992 
2993 
2994 AdapterHandlerEntry::~AdapterHandlerEntry() {
2995   delete _fingerprint;
2996 #ifdef ASSERT
2997   FREE_C_HEAP_ARRAY(unsigned char, _saved_code);
2998 #endif
2999 }
3000 
3001 
3002 #ifdef ASSERT
3003 // Capture the code before relocation so that it can be compared
3004 // against other versions.  If the code is captured after relocation
3005 // then relative instructions won't be equivalent.
3006 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) {
3007   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
3008   _saved_code_length = length;
3009   memcpy(_saved_code, buffer, length);
3010 }
3011 
3012 
3013 bool AdapterHandlerEntry::compare_code(AdapterHandlerEntry* other) {
3014   assert(_saved_code != nullptr && other->_saved_code != nullptr, "code not saved");
3015 
3016   if (other->_saved_code_length != _saved_code_length) {
3017     return false;
3018   }
3019 
3020   return memcmp(other->_saved_code, _saved_code, _saved_code_length) == 0;
3021 }
3022 #endif
3023 
3024 
3025 /**
3026  * Create a native wrapper for this native method.  The wrapper converts the
3027  * Java-compiled calling convention to the native convention, handles
3028  * arguments, and transitions to native.  On return from the native we transition
3029  * back to java blocking if a safepoint is in progress.
3030  */
3031 void AdapterHandlerLibrary::create_native_wrapper(const methodHandle& method) {
3032   ResourceMark rm;
3033   nmethod* nm = nullptr;
3034 
3035   // Check if memory should be freed before allocation
3036   CodeCache::gc_on_allocation();
3037 
3038   assert(method->is_native(), "must be native");
3039   assert(method->is_special_native_intrinsic() ||
3040          method->has_native_function(), "must have something valid to call!");
3041 
3042   {
3043     // Perform the work while holding the lock, but perform any printing outside the lock
3044     MutexLocker mu(AdapterHandlerLibrary_lock);
3045     // See if somebody beat us to it
3046     if (method->code() != nullptr) {
3047       return;
3048     }
3049 
3050     const int compile_id = CompileBroker::assign_compile_id(method, CompileBroker::standard_entry_bci);
3051     assert(compile_id > 0, "Must generate native wrapper");
3052 
3053 
3054     ResourceMark rm;
3055     BufferBlob*  buf = buffer_blob(); // the temporary code buffer in CodeCache
3056     if (buf != nullptr) {
3057       CodeBuffer buffer(buf);
3058 
3059       if (method->is_continuation_enter_intrinsic()) {
3060         buffer.initialize_stubs_size(192);
3061       }
3062 
3063       struct { double data[20]; } locs_buf;
3064       struct { double data[20]; } stubs_locs_buf;
3065       buffer.insts()->initialize_shared_locs((relocInfo*)&locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
3066 #if defined(AARCH64) || defined(PPC64)
3067       // On AArch64 with ZGC and nmethod entry barriers, we need all oops to be
3068       // in the constant pool to ensure ordering between the barrier and oops
3069       // accesses. For native_wrappers we need a constant.
3070       // On PPC64 the continuation enter intrinsic needs the constant pool for the compiled
3071       // static java call that is resolved in the runtime.
3072       if (PPC64_ONLY(method->is_continuation_enter_intrinsic() &&) true) {
3073         buffer.initialize_consts_size(8 PPC64_ONLY(+ 24));
3074       }
3075 #endif
3076       buffer.stubs()->initialize_shared_locs((relocInfo*)&stubs_locs_buf, sizeof(stubs_locs_buf) / sizeof(relocInfo));
3077       MacroAssembler _masm(&buffer);
3078 
3079       // Fill in the signature array, for the calling-convention call.
3080       const int total_args_passed = method->size_of_parameters();
3081 
3082       VMRegPair stack_regs[16];
3083       VMRegPair* regs = (total_args_passed <= 16) ? stack_regs : NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
3084 
3085       AdapterSignatureIterator si(method->signature(), method->constMethod()->fingerprint(),
3086                               method->is_static(), total_args_passed);
3087       BasicType* sig_bt = si.basic_types();
3088       assert(si.slots() == total_args_passed, "");
3089       BasicType ret_type = si.return_type();
3090 
3091       // Now get the compiled-Java arguments layout.
3092       SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed);
3093 
3094       // Generate the compiled-to-native wrapper code
3095       nm = SharedRuntime::generate_native_wrapper(&_masm, method, compile_id, sig_bt, regs, ret_type);
3096 
3097       if (nm != nullptr) {
3098         {
3099           MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
3100           if (nm->make_in_use()) {
3101             method->set_code(method, nm);
3102           }
3103         }
3104 
3105         DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_simple));
3106         if (directive->PrintAssemblyOption) {
3107           nm->print_code();
3108         }
3109         DirectivesStack::release(directive);
3110       }
3111     }
3112   } // Unlock AdapterHandlerLibrary_lock
3113 
3114 
3115   // Install the generated code.
3116   if (nm != nullptr) {
3117     const char *msg = method->is_static() ? "(static)" : "";
3118     CompileTask::print_ul(nm, msg);
3119     if (PrintCompilation) {
3120       ttyLocker ttyl;
3121       CompileTask::print(tty, nm, msg);
3122     }
3123     nm->post_compiled_method_load_event();
3124   }
3125 }
3126 
3127 // -------------------------------------------------------------------------
3128 // Java-Java calling convention
3129 // (what you use when Java calls Java)
3130 
3131 //------------------------------name_for_receiver----------------------------------
3132 // For a given signature, return the VMReg for parameter 0.
3133 VMReg SharedRuntime::name_for_receiver() {
3134   VMRegPair regs;
3135   BasicType sig_bt = T_OBJECT;
3136   (void) java_calling_convention(&sig_bt, &regs, 1);
3137   // Return argument 0 register.  In the LP64 build pointers
3138   // take 2 registers, but the VM wants only the 'main' name.
3139   return regs.first();
3140 }
3141 
3142 VMRegPair *SharedRuntime::find_callee_arguments(Symbol* sig, bool has_receiver, bool has_appendix, int* arg_size) {
3143   // This method is returning a data structure allocating as a
3144   // ResourceObject, so do not put any ResourceMarks in here.
3145 
3146   BasicType *sig_bt = NEW_RESOURCE_ARRAY(BasicType, 256);
3147   VMRegPair *regs = NEW_RESOURCE_ARRAY(VMRegPair, 256);
3148   int cnt = 0;
3149   if (has_receiver) {
3150     sig_bt[cnt++] = T_OBJECT; // Receiver is argument 0; not in signature
3151   }
3152 
3153   for (SignatureStream ss(sig); !ss.at_return_type(); ss.next()) {
3154     BasicType type = ss.type();
3155     sig_bt[cnt++] = type;
3156     if (is_double_word_type(type))
3157       sig_bt[cnt++] = T_VOID;
3158   }
3159 
3160   if (has_appendix) {
3161     sig_bt[cnt++] = T_OBJECT;
3162   }
3163 
3164   assert(cnt < 256, "grow table size");
3165 
3166   int comp_args_on_stack;
3167   comp_args_on_stack = java_calling_convention(sig_bt, regs, cnt);
3168 
3169   // the calling convention doesn't count out_preserve_stack_slots so
3170   // we must add that in to get "true" stack offsets.
3171 
3172   if (comp_args_on_stack) {
3173     for (int i = 0; i < cnt; i++) {
3174       VMReg reg1 = regs[i].first();
3175       if (reg1->is_stack()) {
3176         // Yuck
3177         reg1 = reg1->bias(out_preserve_stack_slots());
3178       }
3179       VMReg reg2 = regs[i].second();
3180       if (reg2->is_stack()) {
3181         // Yuck
3182         reg2 = reg2->bias(out_preserve_stack_slots());
3183       }
3184       regs[i].set_pair(reg2, reg1);
3185     }
3186   }
3187 
3188   // results
3189   *arg_size = cnt;
3190   return regs;
3191 }
3192 
3193 // OSR Migration Code
3194 //
3195 // This code is used convert interpreter frames into compiled frames.  It is
3196 // called from very start of a compiled OSR nmethod.  A temp array is
3197 // allocated to hold the interesting bits of the interpreter frame.  All
3198 // active locks are inflated to allow them to move.  The displaced headers and
3199 // active interpreter locals are copied into the temp buffer.  Then we return
3200 // back to the compiled code.  The compiled code then pops the current
3201 // interpreter frame off the stack and pushes a new compiled frame.  Then it
3202 // copies the interpreter locals and displaced headers where it wants.
3203 // Finally it calls back to free the temp buffer.
3204 //
3205 // All of this is done NOT at any Safepoint, nor is any safepoint or GC allowed.
3206 
3207 JRT_LEAF(intptr_t*, SharedRuntime::OSR_migration_begin( JavaThread *current) )
3208   assert(current == JavaThread::current(), "pre-condition");
3209 
3210   // During OSR migration, we unwind the interpreted frame and replace it with a compiled
3211   // frame. The stack watermark code below ensures that the interpreted frame is processed
3212   // before it gets unwound. This is helpful as the size of the compiled frame could be
3213   // larger than the interpreted frame, which could result in the new frame not being
3214   // processed correctly.
3215   StackWatermarkSet::before_unwind(current);
3216 
3217   //
3218   // This code is dependent on the memory layout of the interpreter local
3219   // array and the monitors. On all of our platforms the layout is identical
3220   // so this code is shared. If some platform lays the their arrays out
3221   // differently then this code could move to platform specific code or
3222   // the code here could be modified to copy items one at a time using
3223   // frame accessor methods and be platform independent.
3224 
3225   frame fr = current->last_frame();
3226   assert(fr.is_interpreted_frame(), "");
3227   assert(fr.interpreter_frame_expression_stack_size()==0, "only handle empty stacks");
3228 
3229   // Figure out how many monitors are active.
3230   int active_monitor_count = 0;
3231   for (BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
3232        kptr < fr.interpreter_frame_monitor_begin();
3233        kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
3234     if (kptr->obj() != nullptr) active_monitor_count++;
3235   }
3236 
3237   // QQQ we could place number of active monitors in the array so that compiled code
3238   // could double check it.
3239 
3240   Method* moop = fr.interpreter_frame_method();
3241   int max_locals = moop->max_locals();
3242   // Allocate temp buffer, 1 word per local & 2 per active monitor
3243   int buf_size_words = max_locals + active_monitor_count * BasicObjectLock::size();
3244   intptr_t *buf = NEW_C_HEAP_ARRAY(intptr_t,buf_size_words, mtCode);
3245 
3246   // Copy the locals.  Order is preserved so that loading of longs works.
3247   // Since there's no GC I can copy the oops blindly.
3248   assert(sizeof(HeapWord)==sizeof(intptr_t), "fix this code");
3249   Copy::disjoint_words((HeapWord*)fr.interpreter_frame_local_at(max_locals-1),
3250                        (HeapWord*)&buf[0],
3251                        max_locals);
3252 
3253   // Inflate locks.  Copy the displaced headers.  Be careful, there can be holes.
3254   int i = max_locals;
3255   for (BasicObjectLock *kptr2 = fr.interpreter_frame_monitor_end();
3256        kptr2 < fr.interpreter_frame_monitor_begin();
3257        kptr2 = fr.next_monitor_in_interpreter_frame(kptr2) ) {
3258     if (kptr2->obj() != nullptr) {         // Avoid 'holes' in the monitor array
3259       BasicLock *lock = kptr2->lock();
3260       // Inflate so the object's header no longer refers to the BasicLock.
3261       if (lock->displaced_header().is_unlocked()) {
3262         // The object is locked and the resulting ObjectMonitor* will also be
3263         // locked so it can't be async deflated until ownership is dropped.
3264         // See the big comment in basicLock.cpp: BasicLock::move_to().
3265         ObjectSynchronizer::inflate_helper(kptr2->obj());
3266       }
3267       // Now the displaced header is free to move because the
3268       // object's header no longer refers to it.
3269       buf[i++] = (intptr_t)lock->displaced_header().value();
3270       buf[i++] = cast_from_oop<intptr_t>(kptr2->obj());
3271     }
3272   }
3273   assert(i - max_locals == active_monitor_count*2, "found the expected number of monitors");
3274 
3275   RegisterMap map(current,
3276                   RegisterMap::UpdateMap::skip,
3277                   RegisterMap::ProcessFrames::include,
3278                   RegisterMap::WalkContinuation::skip);
3279   frame sender = fr.sender(&map);
3280   if (sender.is_interpreted_frame()) {
3281     current->push_cont_fastpath(sender.sp());
3282   }
3283 
3284   return buf;
3285 JRT_END
3286 
3287 JRT_LEAF(void, SharedRuntime::OSR_migration_end( intptr_t* buf) )
3288   FREE_C_HEAP_ARRAY(intptr_t, buf);
3289 JRT_END
3290 
3291 bool AdapterHandlerLibrary::contains(const CodeBlob* b) {
3292   bool found = false;
3293   auto findblob = [&] (AdapterFingerPrint* key, AdapterHandlerEntry* a) {
3294     return (found = (b == CodeCache::find_blob(a->get_i2c_entry())));
3295   };
3296   assert_locked_or_safepoint(AdapterHandlerLibrary_lock);
3297   _adapter_handler_table->iterate(findblob);
3298   return found;
3299 }
3300 
3301 void AdapterHandlerLibrary::print_handler_on(outputStream* st, const CodeBlob* b) {
3302   bool found = false;
3303   auto findblob = [&] (AdapterFingerPrint* key, AdapterHandlerEntry* a) {
3304     if (b == CodeCache::find_blob(a->get_i2c_entry())) {
3305       found = true;
3306       st->print("Adapter for signature: ");
3307       a->print_adapter_on(st);
3308       return true;
3309     } else {
3310       return false; // keep looking
3311     }
3312   };
3313   assert_locked_or_safepoint(AdapterHandlerLibrary_lock);
3314   _adapter_handler_table->iterate(findblob);
3315   assert(found, "Should have found handler");
3316 }
3317 
3318 void AdapterHandlerEntry::print_adapter_on(outputStream* st) const {
3319   st->print("AHE@" INTPTR_FORMAT ": %s", p2i(this), fingerprint()->as_string());
3320   if (get_i2c_entry() != nullptr) {
3321     st->print(" i2c: " INTPTR_FORMAT, p2i(get_i2c_entry()));
3322   }
3323   if (get_c2i_entry() != nullptr) {
3324     st->print(" c2i: " INTPTR_FORMAT, p2i(get_c2i_entry()));
3325   }
3326   if (get_c2i_unverified_entry() != nullptr) {
3327     st->print(" c2iUV: " INTPTR_FORMAT, p2i(get_c2i_unverified_entry()));
3328   }
3329   if (get_c2i_no_clinit_check_entry() != nullptr) {
3330     st->print(" c2iNCI: " INTPTR_FORMAT, p2i(get_c2i_no_clinit_check_entry()));
3331   }
3332   st->cr();
3333 }
3334 
3335 #ifndef PRODUCT
3336 
3337 void AdapterHandlerLibrary::print_statistics() {
3338   print_table_statistics();
3339 }
3340 
3341 #endif /* PRODUCT */
3342 
3343 JRT_LEAF(void, SharedRuntime::enable_stack_reserved_zone(JavaThread* current))
3344   assert(current == JavaThread::current(), "pre-condition");
3345   StackOverflow* overflow_state = current->stack_overflow_state();
3346   overflow_state->enable_stack_reserved_zone(/*check_if_disabled*/true);
3347   overflow_state->set_reserved_stack_activation(current->stack_base());
3348 JRT_END
3349 
3350 frame SharedRuntime::look_for_reserved_stack_annotated_method(JavaThread* current, frame fr) {
3351   ResourceMark rm(current);
3352   frame activation;
3353   CompiledMethod* nm = nullptr;
3354   int count = 1;
3355 
3356   assert(fr.is_java_frame(), "Must start on Java frame");
3357 
3358   RegisterMap map(JavaThread::current(),
3359                   RegisterMap::UpdateMap::skip,
3360                   RegisterMap::ProcessFrames::skip,
3361                   RegisterMap::WalkContinuation::skip); // don't walk continuations
3362   for (; !fr.is_first_frame(); fr = fr.sender(&map)) {
3363     if (!fr.is_java_frame()) {
3364       continue;
3365     }
3366 
3367     Method* method = nullptr;
3368     bool found = false;
3369     if (fr.is_interpreted_frame()) {
3370       method = fr.interpreter_frame_method();
3371       if (method != nullptr && method->has_reserved_stack_access()) {
3372         found = true;
3373       }
3374     } else {
3375       CodeBlob* cb = fr.cb();
3376       if (cb != nullptr && cb->is_compiled()) {
3377         nm = cb->as_compiled_method();
3378         method = nm->method();
3379         // scope_desc_near() must be used, instead of scope_desc_at() because on
3380         // SPARC, the pcDesc can be on the delay slot after the call instruction.
3381         for (ScopeDesc *sd = nm->scope_desc_near(fr.pc()); sd != nullptr; sd = sd->sender()) {
3382           method = sd->method();
3383           if (method != nullptr && method->has_reserved_stack_access()) {
3384             found = true;
3385       }
3386     }
3387       }
3388     }
3389     if (found) {
3390       activation = fr;
3391       warning("Potentially dangerous stack overflow in "
3392               "ReservedStackAccess annotated method %s [%d]",
3393               method->name_and_sig_as_C_string(), count++);
3394       EventReservedStackActivation event;
3395       if (event.should_commit()) {
3396         event.set_method(method);
3397         event.commit();
3398       }
3399     }
3400   }
3401   return activation;
3402 }
3403 
3404 void SharedRuntime::on_slowpath_allocation_exit(JavaThread* current) {
3405   // After any safepoint, just before going back to compiled code,
3406   // we inform the GC that we will be doing initializing writes to
3407   // this object in the future without emitting card-marks, so
3408   // GC may take any compensating steps.
3409 
3410   oop new_obj = current->vm_result();
3411   if (new_obj == nullptr) return;
3412 
3413   BarrierSet *bs = BarrierSet::barrier_set();
3414   bs->on_slowpath_allocation_exit(current, new_obj);
3415 }