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