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/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmClasses.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "compiler/compilationPolicy.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "compiler/disassembler.hpp"
  35 #include "gc/shared/barrierSetNMethod.hpp"
  36 #include "gc/shared/collectedHeap.hpp"
  37 #include "interpreter/bytecodeTracer.hpp"
  38 #include "interpreter/interpreter.hpp"
  39 #include "interpreter/interpreterRuntime.hpp"
  40 #include "interpreter/linkResolver.hpp"
  41 #include "interpreter/templateTable.hpp"
  42 #include "jvm_io.h"
  43 #include "logging/log.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/constantPool.inline.hpp"
  48 #include "oops/cpCache.inline.hpp"
  49 #include "oops/flatArrayKlass.hpp"
  50 #include "oops/flatArrayOop.inline.hpp"
  51 #include "oops/inlineKlass.inline.hpp"
  52 #include "oops/instanceKlass.inline.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/methodData.hpp"
  55 #include "oops/method.inline.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/methodHandles.hpp"
  62 #include "prims/nativeLookup.hpp"
  63 #include "runtime/atomic.hpp"
  64 #include "runtime/continuation.hpp"
  65 #include "runtime/deoptimization.hpp"
  66 #include "runtime/fieldDescriptor.inline.hpp"
  67 #include "runtime/frame.inline.hpp"
  68 #include "runtime/handles.inline.hpp"
  69 #include "runtime/icache.hpp"
  70 #include "runtime/interfaceSupport.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/jfieldIDWorkaround.hpp"
  74 #include "runtime/osThread.hpp"
  75 #include "runtime/sharedRuntime.hpp"
  76 #include "runtime/stackWatermarkSet.hpp"
  77 #include "runtime/stubRoutines.hpp"
  78 #include "runtime/synchronizer.hpp"
  79 #include "runtime/threadCritical.hpp"
  80 #include "utilities/align.hpp"
  81 #include "utilities/checkedCast.hpp"
  82 #include "utilities/copy.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/globalDefinitions.hpp"
  85 #ifdef COMPILER2
  86 #include "opto/runtime.hpp"
  87 #endif
  88 
  89 // Helper class to access current interpreter state
  90 class LastFrameAccessor : public StackObj {
  91   frame _last_frame;
  92 public:
  93   LastFrameAccessor(JavaThread* current) {
  94     assert(current == Thread::current(), "sanity");
  95     _last_frame = current->last_frame();
  96   }
  97   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  98   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  99   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
 100   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
 101   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
 102 
 103   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 104   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }
 105 
 106   // pass method to avoid calling unsafe bcp_to_method (partial fix 4926272)
 107   Bytecodes::Code code() const                   { return Bytecodes::code_at(method(), bcp()); }
 108 
 109   Bytecode  bytecode() const                     { return Bytecode(method(), bcp()); }
 110   int get_index_u1(Bytecodes::Code bc) const     { return bytecode().get_index_u1(bc); }
 111   int get_index_u2(Bytecodes::Code bc) const     { return bytecode().get_index_u2(bc); }
 112   int get_index_u4(Bytecodes::Code bc) const     { return bytecode().get_index_u4(bc); }
 113   int number_of_dimensions() const               { return bcp()[3]; }
 114 
 115   oop callee_receiver(Symbol* signature) {
 116     return _last_frame.interpreter_callee_receiver(signature);
 117   }
 118   BasicObjectLock* monitor_begin() const {
 119     return _last_frame.interpreter_frame_monitor_begin();
 120   }
 121   BasicObjectLock* monitor_end() const {
 122     return _last_frame.interpreter_frame_monitor_end();
 123   }
 124   BasicObjectLock* next_monitor(BasicObjectLock* current) const {
 125     return _last_frame.next_monitor_in_interpreter_frame(current);
 126   }
 127 
 128   frame& get_frame()                             { return _last_frame; }
 129 };
 130 
 131 //------------------------------------------------------------------------------------------------------------------------
 132 // State accessors
 133 
 134 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread* current) {
 135   LastFrameAccessor last_frame(current);
 136   last_frame.set_bcp(bcp);
 137   if (ProfileInterpreter) {
 138     // ProfileTraps uses MDOs independently of ProfileInterpreter.
 139     // That is why we must check both ProfileInterpreter and mdo != nullptr.
 140     MethodData* mdo = last_frame.method()->method_data();
 141     if (mdo != nullptr) {
 142       NEEDS_CLEANUP;
 143       last_frame.set_mdp(mdo->bci_to_dp(last_frame.bci()));
 144     }
 145   }
 146 }
 147 
 148 //------------------------------------------------------------------------------------------------------------------------
 149 // Constants
 150 
 151 
 152 JRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* current, bool wide))
 153   // access constant pool
 154   LastFrameAccessor last_frame(current);
 155   ConstantPool* pool = last_frame.method()->constants();
 156   int cp_index = wide ? last_frame.get_index_u2(Bytecodes::_ldc_w) : last_frame.get_index_u1(Bytecodes::_ldc);
 157   constantTag tag = pool->tag_at(cp_index);
 158 
 159   assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
 160   Klass* klass = pool->klass_at(cp_index, CHECK);
 161   oop java_class = klass->java_mirror();
 162   current->set_vm_result(java_class);
 163 JRT_END
 164 
 165 JRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* current, Bytecodes::Code bytecode)) {
 166   assert(bytecode == Bytecodes::_ldc ||
 167          bytecode == Bytecodes::_ldc_w ||
 168          bytecode == Bytecodes::_ldc2_w ||
 169          bytecode == Bytecodes::_fast_aldc ||
 170          bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
 171   ResourceMark rm(current);
 172   const bool is_fast_aldc = (bytecode == Bytecodes::_fast_aldc ||
 173                              bytecode == Bytecodes::_fast_aldc_w);
 174   LastFrameAccessor last_frame(current);
 175   methodHandle m (current, last_frame.method());
 176   Bytecode_loadconstant ldc(m, last_frame.bci());
 177 
 178   // Double-check the size.  (Condy can have any type.)
 179   BasicType type = ldc.result_type();
 180   switch (type2size[type]) {
 181   case 2: guarantee(bytecode == Bytecodes::_ldc2_w, ""); break;
 182   case 1: guarantee(bytecode != Bytecodes::_ldc2_w, ""); break;
 183   default: ShouldNotReachHere();
 184   }
 185 
 186   // Resolve the constant.  This does not do unboxing.
 187   // But it does replace Universe::the_null_sentinel by null.
 188   oop result = ldc.resolve_constant(CHECK);
 189   assert(result != nullptr || is_fast_aldc, "null result only valid for fast_aldc");
 190 
 191 #ifdef ASSERT
 192   {
 193     // The bytecode wrappers aren't GC-safe so construct a new one
 194     Bytecode_loadconstant ldc2(m, last_frame.bci());
 195     int rindex = ldc2.cache_index();
 196     if (rindex < 0)
 197       rindex = m->constants()->cp_to_object_index(ldc2.pool_index());
 198     if (rindex >= 0) {
 199       oop coop = m->constants()->resolved_reference_at(rindex);
 200       oop roop = (result == nullptr ? Universe::the_null_sentinel() : result);
 201       assert(roop == coop, "expected result for assembly code");
 202     }
 203   }
 204 #endif
 205   current->set_vm_result(result);
 206   if (!is_fast_aldc) {
 207     // Tell the interpreter how to unbox the primitive.
 208     guarantee(java_lang_boxing_object::is_instance(result, type), "");
 209     int offset = java_lang_boxing_object::value_offset(type);
 210     intptr_t flags = ((as_TosState(type) << ConstantPoolCache::tos_state_shift)
 211                       | (offset & ConstantPoolCache::field_index_mask));
 212     current->set_vm_result_2((Metadata*)flags);
 213   }
 214 }
 215 JRT_END
 216 
 217 
 218 //------------------------------------------------------------------------------------------------------------------------
 219 // Allocation
 220 
 221 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 222   Klass* k = pool->klass_at(index, CHECK);
 223   InstanceKlass* klass = InstanceKlass::cast(k);
 224 
 225   // Make sure we are not instantiating an abstract klass
 226   klass->check_valid_for_instantiation(true, CHECK);
 227 
 228   // Make sure klass is initialized
 229   klass->initialize(CHECK);
 230 
 231   // At this point the class may not be fully initialized
 232   // because of recursive initialization. If it is fully
 233   // initialized & has_finalized is not set, we rewrite
 234   // it into its fast version (Note: no locking is needed
 235   // here since this is an atomic byte write and can be
 236   // done more than once).
 237   //
 238   // Note: In case of classes with has_finalized we don't
 239   //       rewrite since that saves us an extra check in
 240   //       the fast version which then would call the
 241   //       slow version anyway (and do a call back into
 242   //       Java).
 243   //       If we have a breakpoint, then we don't rewrite
 244   //       because the _breakpoint bytecode would be lost.
 245   oop obj = klass->allocate_instance(CHECK);
 246   current->set_vm_result(obj);
 247 JRT_END
 248 
 249 JRT_ENTRY(void, InterpreterRuntime::uninitialized_static_inline_type_field(JavaThread* current, oopDesc* mirror, ResolvedFieldEntry* entry))
 250   // The interpreter tries to access an inline static field that has not been initialized.
 251   // This situation can happen in different scenarios:
 252   //   1 - if the load or initialization of the field failed during step 8 of
 253   //       the initialization of the holder of the field, in this case the access to the field
 254   //       must fail
 255   //   2 - it can also happen when the initialization of the holder class triggered the initialization of
 256   //       another class which accesses this field in its static initializer, in this case the
 257   //       access must succeed to allow circularity
 258   // The code below tries to load and initialize the field's class again before returning the default value.
 259   // If the field was not initialized because of an error, an exception should be thrown.
 260   // If the class is being initialized, the default value is returned.
 261   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
 262   instanceHandle mirror_h(THREAD, (instanceOop)mirror);
 263   InstanceKlass* klass = entry->field_holder();
 264   u2 index = entry->field_index();
 265   assert(klass == java_lang_Class::as_Klass(mirror), "Not the field holder klass");
 266   assert(klass->field_is_null_free_inline_type(index), "Sanity check");
 267   if (klass->is_being_initialized() && klass->is_init_thread(THREAD)) {
 268     int offset = klass->field_offset(index);
 269     Klass* field_k = klass->get_inline_type_field_klass_or_null(index);
 270     if (field_k == nullptr) {
 271       field_k = SystemDictionary::resolve_or_fail(klass->field_signature(index)->fundamental_name(THREAD),
 272           Handle(THREAD, klass->class_loader()),
 273           Handle(THREAD, klass->protection_domain()),
 274           true, CHECK);
 275       assert(field_k != nullptr, "Should have been loaded or an exception thrown above");
 276       klass->set_inline_type_field_klass(index, InlineKlass::cast(field_k));
 277     }
 278     field_k->initialize(CHECK);
 279     oop defaultvalue = InlineKlass::cast(field_k)->default_value();
 280     // It is safe to initialize the static field because 1) the current thread is the initializing thread
 281     // and is the only one that can access it, and 2) the field is actually not initialized (i.e. null)
 282     // otherwise the JVM should not be executing this code.
 283     mirror_h()->obj_field_put(offset, defaultvalue);
 284     current->set_vm_result(defaultvalue);
 285   } else {
 286     assert(klass->is_in_error_state(), "If not initializing, initialization must have failed to get there");
 287     ResourceMark rm(THREAD);
 288     const char* desc = "Could not initialize class ";
 289     const char* className = klass->external_name();
 290     size_t msglen = strlen(desc) + strlen(className) + 1;
 291     char* message = NEW_RESOURCE_ARRAY(char, msglen);
 292     if (nullptr == message) {
 293       // Out of memory: can't create detailed error message
 294       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
 295     } else {
 296       jio_snprintf(message, msglen, "%s%s", desc, className);
 297       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
 298     }
 299   }
 300 JRT_END
 301 
 302 JRT_ENTRY(void, InterpreterRuntime::read_flat_field(JavaThread* current, oopDesc* obj, int index, Klass* field_holder))
 303   Handle obj_h(THREAD, obj);
 304 
 305   assert(oopDesc::is_oop(obj), "Sanity check");
 306 
 307   assert(field_holder->is_instance_klass(), "Sanity check");
 308   InstanceKlass* klass = InstanceKlass::cast(field_holder);
 309 
 310   assert(klass->field_is_flat(index), "Sanity check");
 311 
 312   InlineKlass* field_vklass = InlineKlass::cast(klass->get_inline_type_field_klass(index));
 313 
 314   oop res = field_vklass->read_flat_field(obj_h(), klass->field_offset(index), CHECK);
 315   current->set_vm_result(res);
 316 JRT_END
 317 
 318 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 319   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 320   current->set_vm_result(obj);
 321 JRT_END
 322 
 323 
 324 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 325   Klass*    klass = pool->klass_at(index, CHECK);
 326   arrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 327   current->set_vm_result(obj);
 328 JRT_END
 329 
 330 JRT_ENTRY(void, InterpreterRuntime::value_array_load(JavaThread* current, arrayOopDesc* array, int index))
 331   flatArrayHandle vah(current, (flatArrayOop)array);
 332   oop value_holder = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, CHECK);
 333   current->set_vm_result(value_holder);
 334 JRT_END
 335 
 336 JRT_ENTRY(void, InterpreterRuntime::value_array_store(JavaThread* current, void* val, arrayOopDesc* array, int index))
 337   assert(val != nullptr, "can't store null into flat array");
 338   ((flatArrayOop)array)->value_copy_to_index(cast_to_oop(val), index);
 339 JRT_END
 340 
 341 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 342   // We may want to pass in more arguments - could make this slightly faster
 343   LastFrameAccessor last_frame(current);
 344   ConstantPool* constants = last_frame.method()->constants();
 345   int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 346   Klass* klass = constants->klass_at(i, CHECK);
 347   int   nof_dims = last_frame.number_of_dimensions();
 348   assert(klass->is_klass(), "not a class");
 349   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 350 
 351   // We must create an array of jints to pass to multi_allocate.
 352   ResourceMark rm(current);
 353   const int small_dims = 10;
 354   jint dim_array[small_dims];
 355   jint *dims = &dim_array[0];
 356   if (nof_dims > small_dims) {
 357     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 358   }
 359   for (int index = 0; index < nof_dims; index++) {
 360     // offset from first_size_address is addressed as local[index]
 361     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 362     dims[index] = first_size_address[n];
 363   }
 364   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 365   current->set_vm_result(obj);
 366 JRT_END
 367 
 368 
 369 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 370   assert(oopDesc::is_oop(obj), "must be a valid oop");
 371   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 372   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 373 JRT_END
 374 
 375 JRT_ENTRY(jboolean, InterpreterRuntime::is_substitutable(JavaThread* current, oopDesc* aobj, oopDesc* bobj))
 376   assert(oopDesc::is_oop(aobj) && oopDesc::is_oop(bobj), "must be valid oops");
 377 
 378   Handle ha(THREAD, aobj);
 379   Handle hb(THREAD, bobj);
 380   JavaValue result(T_BOOLEAN);
 381   JavaCallArguments args;
 382   args.push_oop(ha);
 383   args.push_oop(hb);
 384   methodHandle method(current, Universe::is_substitutable_method());
 385   JavaCalls::call(&result, method, &args, THREAD);
 386   if (HAS_PENDING_EXCEPTION) {
 387     // Something really bad happened because isSubstitutable() should not throw exceptions
 388     // If it is an error, just let it propagate
 389     // If it is an exception, wrap it into an InternalError
 390     if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 391       Handle e(THREAD, PENDING_EXCEPTION);
 392       CLEAR_PENDING_EXCEPTION;
 393       THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
 394     }
 395   }
 396   return result.get_jboolean();
 397 JRT_END
 398 
 399 // Quicken instance-of and check-cast bytecodes
 400 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 401   // Force resolving; quicken the bytecode
 402   LastFrameAccessor last_frame(current);
 403   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 404   ConstantPool* cpool = last_frame.method()->constants();
 405   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 406   // program we might have seen an unquick'd bytecode in the interpreter but have another
 407   // thread quicken the bytecode before we get here.
 408   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 409   Klass* klass = cpool->klass_at(which, CHECK);
 410   current->set_vm_result_2(klass);
 411 JRT_END
 412 
 413 
 414 //------------------------------------------------------------------------------------------------------------------------
 415 // Exceptions
 416 
 417 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,
 418                                          const methodHandle& trap_method, int trap_bci) {
 419   if (trap_method.not_null()) {
 420     MethodData* trap_mdo = trap_method->method_data();
 421     if (trap_mdo == nullptr) {
 422       ExceptionMark em(current);
 423       JavaThread* THREAD = current; // For exception macros.
 424       Method::build_profiling_method_data(trap_method, THREAD);
 425       if (HAS_PENDING_EXCEPTION) {
 426         // Only metaspace OOM is expected. No Java code executed.
 427         assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())),
 428                "we expect only an OOM error here");
 429         CLEAR_PENDING_EXCEPTION;
 430       }
 431       trap_mdo = trap_method->method_data();
 432       // and fall through...
 433     }
 434     if (trap_mdo != nullptr) {
 435       // Update per-method count of trap events.  The interpreter
 436       // is updating the MDO to simulate the effect of compiler traps.
 437       Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
 438     }
 439   }
 440 }
 441 
 442 // Assume the compiler is (or will be) interested in this event.
 443 // If necessary, create an MDO to hold the information, and record it.
 444 void InterpreterRuntime::note_trap(JavaThread* current, int reason) {
 445   assert(ProfileTraps, "call me only if profiling");
 446   LastFrameAccessor last_frame(current);
 447   methodHandle trap_method(current, last_frame.method());
 448   int trap_bci = trap_method->bci_from(last_frame.bcp());
 449   note_trap_inner(current, reason, trap_method, trap_bci);
 450 }
 451 
 452 static Handle get_preinitialized_exception(Klass* k, TRAPS) {
 453   // get klass
 454   InstanceKlass* klass = InstanceKlass::cast(k);
 455   assert(klass->is_initialized(),
 456          "this klass should have been initialized during VM initialization");
 457   // create instance - do not call constructor since we may have no
 458   // (java) stack space left (should assert constructor is empty)
 459   Handle exception;
 460   oop exception_oop = klass->allocate_instance(CHECK_(exception));
 461   exception = Handle(THREAD, exception_oop);
 462   if (StackTraceInThrowable) {
 463     java_lang_Throwable::fill_in_stack_trace(exception);
 464   }
 465   return exception;
 466 }
 467 
 468 // Special handling for stack overflow: since we don't have any (java) stack
 469 // space left we use the pre-allocated & pre-initialized StackOverflowError
 470 // klass to create an stack overflow error instance.  We do not call its
 471 // constructor for the same reason (it is empty, anyway).
 472 JRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* current))
 473   Handle exception = get_preinitialized_exception(
 474                                  vmClasses::StackOverflowError_klass(),
 475                                  CHECK);
 476   // Increment counter for hs_err file reporting
 477   Atomic::inc(&Exceptions::_stack_overflow_errors);
 478   // Remove the ScopedValue bindings in case we got a StackOverflowError
 479   // while we were trying to manipulate ScopedValue bindings.
 480   current->clear_scopedValueBindings();
 481   THROW_HANDLE(exception);
 482 JRT_END
 483 
 484 JRT_ENTRY(void, InterpreterRuntime::throw_delayed_StackOverflowError(JavaThread* current))
 485   Handle exception = get_preinitialized_exception(
 486                                  vmClasses::StackOverflowError_klass(),
 487                                  CHECK);
 488   java_lang_Throwable::set_message(exception(),
 489           Universe::delayed_stack_overflow_error_message());
 490   // Increment counter for hs_err file reporting
 491   Atomic::inc(&Exceptions::_stack_overflow_errors);
 492   // Remove the ScopedValue bindings in case we got a StackOverflowError
 493   // while we were trying to manipulate ScopedValue bindings.
 494   current->clear_scopedValueBindings();
 495   THROW_HANDLE(exception);
 496 JRT_END
 497 
 498 JRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* current, char* name, char* message))
 499   // lookup exception klass
 500   TempNewSymbol s = SymbolTable::new_symbol(name);
 501   if (ProfileTraps) {
 502     if (s == vmSymbols::java_lang_ArithmeticException()) {
 503       note_trap(current, Deoptimization::Reason_div0_check);
 504     } else if (s == vmSymbols::java_lang_NullPointerException()) {
 505       note_trap(current, Deoptimization::Reason_null_check);
 506     }
 507   }
 508   // create exception
 509   Handle exception = Exceptions::new_exception(current, s, message);
 510   current->set_vm_result(exception());
 511 JRT_END
 512 
 513 
 514 JRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* current, char* name, oopDesc* obj))
 515   // Produce the error message first because note_trap can safepoint
 516   ResourceMark rm(current);
 517   const char* klass_name = obj->klass()->external_name();
 518   // lookup exception klass
 519   TempNewSymbol s = SymbolTable::new_symbol(name);
 520   if (ProfileTraps) {
 521     if (s == vmSymbols::java_lang_ArrayStoreException()) {
 522       note_trap(current, Deoptimization::Reason_array_check);
 523     } else {
 524       note_trap(current, Deoptimization::Reason_class_check);
 525     }
 526   }
 527   // create exception, with klass name as detail message
 528   Handle exception = Exceptions::new_exception(current, s, klass_name);
 529   current->set_vm_result(exception());
 530 JRT_END
 531 
 532 JRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* current, arrayOopDesc* a, jint index))
 533   // Produce the error message first because note_trap can safepoint
 534   ResourceMark rm(current);
 535   stringStream ss;
 536   ss.print("Index %d out of bounds for length %d", index, a->length());
 537 
 538   if (ProfileTraps) {
 539     note_trap(current, Deoptimization::Reason_range_check);
 540   }
 541 
 542   THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
 543 JRT_END
 544 
 545 JRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
 546   JavaThread* current, oopDesc* obj))
 547 
 548   // Produce the error message first because note_trap can safepoint
 549   ResourceMark rm(current);
 550   char* message = SharedRuntime::generate_class_cast_message(
 551     current, obj->klass());
 552 
 553   if (ProfileTraps) {
 554     note_trap(current, Deoptimization::Reason_class_check);
 555   }
 556 
 557   // create exception
 558   THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
 559 JRT_END
 560 
 561 // exception_handler_for_exception(...) returns the continuation address,
 562 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
 563 // The exception oop is returned to make sure it is preserved over GC (it
 564 // is only on the stack if the exception was thrown explicitly via athrow).
 565 // During this operation, the expression stack contains the values for the
 566 // bci where the exception happened. If the exception was propagated back
 567 // from a call, the expression stack contains the values for the bci at the
 568 // invoke w/o arguments (i.e., as if one were inside the call).
 569 // Note that the implementation of this method assumes it's only called when an exception has actually occured
 570 JRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* current, oopDesc* exception))
 571   // We get here after we have unwound from a callee throwing an exception
 572   // into the interpreter. Any deferred stack processing is notified of
 573   // the event via the StackWatermarkSet.
 574   StackWatermarkSet::after_unwind(current);
 575 
 576   LastFrameAccessor last_frame(current);
 577   Handle             h_exception(current, exception);
 578   methodHandle       h_method   (current, last_frame.method());
 579   constantPoolHandle h_constants(current, h_method->constants());
 580   bool               should_repeat;
 581   int                handler_bci;
 582   int                current_bci = last_frame.bci();
 583 
 584   if (current->frames_to_pop_failed_realloc() > 0) {
 585     // Allocation of scalar replaced object used in this frame
 586     // failed. Unconditionally pop the frame.
 587     current->dec_frames_to_pop_failed_realloc();
 588     current->set_vm_result(h_exception());
 589     // If the method is synchronized we already unlocked the monitor
 590     // during deoptimization so the interpreter needs to skip it when
 591     // the frame is popped.
 592     current->set_do_not_unlock_if_synchronized(true);
 593     return Interpreter::remove_activation_entry();
 594   }
 595 
 596   // Need to do this check first since when _do_not_unlock_if_synchronized
 597   // is set, we don't want to trigger any classloading which may make calls
 598   // into java, or surprisingly find a matching exception handler for bci 0
 599   // since at this moment the method hasn't been "officially" entered yet.
 600   if (current->do_not_unlock_if_synchronized()) {
 601     ResourceMark rm;
 602     assert(current_bci == 0,  "bci isn't zero for do_not_unlock_if_synchronized");
 603     current->set_vm_result(exception);
 604     return Interpreter::remove_activation_entry();
 605   }
 606 
 607   do {
 608     should_repeat = false;
 609 
 610     // assertions
 611     assert(h_exception.not_null(), "null exceptions should be handled by athrow");
 612     // Check that exception is a subclass of Throwable.
 613     assert(h_exception->is_a(vmClasses::Throwable_klass()),
 614            "Exception not subclass of Throwable");
 615 
 616     // tracing
 617     if (log_is_enabled(Info, exceptions)) {
 618       ResourceMark rm(current);
 619       stringStream tempst;
 620       tempst.print("interpreter method <%s>\n"
 621                    " at bci %d for thread " INTPTR_FORMAT " (%s)",
 622                    h_method->print_value_string(), current_bci, p2i(current), current->name());
 623       Exceptions::log_exception(h_exception, tempst.as_string());
 624     }
 625 // Don't go paging in something which won't be used.
 626 //     else if (extable->length() == 0) {
 627 //       // disabled for now - interpreter is not using shortcut yet
 628 //       // (shortcut is not to call runtime if we have no exception handlers)
 629 //       // warning("performance bug: should not call runtime if method has no exception handlers");
 630 //     }
 631     // for AbortVMOnException flag
 632     Exceptions::debug_check_abort(h_exception);
 633 
 634     // exception handler lookup
 635     Klass* klass = h_exception->klass();
 636     handler_bci = Method::fast_exception_handler_bci_for(h_method, klass, current_bci, THREAD);
 637     if (HAS_PENDING_EXCEPTION) {
 638       // We threw an exception while trying to find the exception handler.
 639       // Transfer the new exception to the exception handle which will
 640       // be set into thread local storage, and do another lookup for an
 641       // exception handler for this exception, this time starting at the
 642       // BCI of the exception handler which caused the exception to be
 643       // thrown (bug 4307310).
 644       h_exception = Handle(THREAD, PENDING_EXCEPTION);
 645       CLEAR_PENDING_EXCEPTION;
 646       if (handler_bci >= 0) {
 647         current_bci = handler_bci;
 648         should_repeat = true;
 649       }
 650     }
 651   } while (should_repeat == true);
 652 
 653 #if INCLUDE_JVMCI
 654   if (EnableJVMCI && h_method->method_data() != nullptr) {
 655     ResourceMark rm(current);
 656     ProfileData* pdata = h_method->method_data()->allocate_bci_to_data(current_bci, nullptr);
 657     if (pdata != nullptr && pdata->is_BitData()) {
 658       BitData* bit_data = (BitData*) pdata;
 659       bit_data->set_exception_seen();
 660     }
 661   }
 662 #endif
 663 
 664   // notify JVMTI of an exception throw; JVMTI will detect if this is a first
 665   // time throw or a stack unwinding throw and accordingly notify the debugger
 666   if (JvmtiExport::can_post_on_exceptions()) {
 667     JvmtiExport::post_exception_throw(current, h_method(), last_frame.bcp(), h_exception());
 668   }
 669 
 670   address continuation = nullptr;
 671   address handler_pc = nullptr;
 672   if (handler_bci < 0 || !current->stack_overflow_state()->reguard_stack((address) &continuation)) {
 673     // Forward exception to callee (leaving bci/bcp untouched) because (a) no
 674     // handler in this method, or (b) after a stack overflow there is not yet
 675     // enough stack space available to reprotect the stack.
 676     continuation = Interpreter::remove_activation_entry();
 677 #if COMPILER2_OR_JVMCI
 678     // Count this for compilation purposes
 679     h_method->interpreter_throwout_increment(THREAD);
 680 #endif
 681   } else {
 682     // handler in this method => change bci/bcp to handler bci/bcp and continue there
 683     handler_pc = h_method->code_base() + handler_bci;
 684     h_method->set_exception_handler_entered(handler_bci); // profiling
 685 #ifndef ZERO
 686     set_bcp_and_mdp(handler_pc, current);
 687     continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
 688 #else
 689     continuation = (address)(intptr_t) handler_bci;
 690 #endif
 691   }
 692 
 693   // notify debugger of an exception catch
 694   // (this is good for exceptions caught in native methods as well)
 695   if (JvmtiExport::can_post_on_exceptions()) {
 696     JvmtiExport::notice_unwind_due_to_exception(current, h_method(), handler_pc, h_exception(), (handler_pc != nullptr));
 697   }
 698 
 699   current->set_vm_result(h_exception());
 700   return continuation;
 701 JRT_END
 702 
 703 
 704 JRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* current))
 705   assert(current->has_pending_exception(), "must only be called if there's an exception pending");
 706   // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
 707 JRT_END
 708 
 709 
 710 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* current))
 711   THROW(vmSymbols::java_lang_AbstractMethodError());
 712 JRT_END
 713 
 714 // This method is called from the "abstract_entry" of the interpreter.
 715 // At that point, the arguments have already been removed from the stack
 716 // and therefore we don't have the receiver object at our fingertips. (Though,
 717 // on some platforms the receiver still resides in a register...). Thus,
 718 // we have no choice but print an error message not containing the receiver
 719 // type.
 720 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 721                                                                         Method* missingMethod))
 722   ResourceMark rm(current);
 723   assert(missingMethod != nullptr, "sanity");
 724   methodHandle m(current, missingMethod);
 725   LinkResolver::throw_abstract_method_error(m, THREAD);
 726 JRT_END
 727 
 728 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 729                                                                      Klass* recvKlass,
 730                                                                      Method* missingMethod))
 731   ResourceMark rm(current);
 732   methodHandle mh = methodHandle(current, missingMethod);
 733   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 734 JRT_END
 735 
 736 JRT_ENTRY(void, InterpreterRuntime::throw_InstantiationError(JavaThread* current))
 737   THROW(vmSymbols::java_lang_InstantiationError());
 738 JRT_END
 739 
 740 
 741 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 742   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 743 JRT_END
 744 
 745 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 746                                                                               Klass* recvKlass,
 747                                                                               Klass* interfaceKlass))
 748   ResourceMark rm(current);
 749   char buf[1000];
 750   buf[0] = '\0';
 751   jio_snprintf(buf, sizeof(buf),
 752                "Class %s does not implement the requested interface %s",
 753                recvKlass ? recvKlass->external_name() : "nullptr",
 754                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 755   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 756 JRT_END
 757 
 758 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 759   THROW(vmSymbols::java_lang_NullPointerException());
 760 JRT_END
 761 
 762 //------------------------------------------------------------------------------------------------------------------------
 763 // Fields
 764 //
 765 
 766 void InterpreterRuntime::resolve_get_put(JavaThread* current, Bytecodes::Code bytecode) {
 767   // resolve field
 768   fieldDescriptor info;
 769   LastFrameAccessor last_frame(current);
 770   constantPoolHandle pool(current, last_frame.method()->constants());
 771   methodHandle m(current, last_frame.method());
 772   bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_nofast_putfield ||
 773                     bytecode == Bytecodes::_putstatic);
 774   bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
 775 
 776   int field_index = last_frame.get_index_u2(bytecode);
 777   {
 778     JvmtiHideSingleStepping jhss(current);
 779     JavaThread* THREAD = current; // For exception macros.
 780     LinkResolver::resolve_field_access(info, pool, field_index,
 781                                        m, bytecode, CHECK);
 782   } // end JvmtiHideSingleStepping
 783 
 784   // check if link resolution caused cpCache to be updated
 785   if (pool->resolved_field_entry_at(field_index)->is_resolved(bytecode)) return;
 786 
 787 
 788   // compute auxiliary field attributes
 789   TosState state  = as_TosState(info.field_type());
 790 
 791   // Resolution of put instructions on final fields is delayed. That is required so that
 792   // exceptions are thrown at the correct place (when the instruction is actually invoked).
 793   // If we do not resolve an instruction in the current pass, leaving the put_code
 794   // set to zero will cause the next put instruction to the same field to reresolve.
 795 
 796   // Resolution of put instructions to final instance fields with invalid updates (i.e.,
 797   // to final instance fields with updates originating from a method different than <init>)
 798   // is inhibited. A putfield instruction targeting an instance final field must throw
 799   // an IllegalAccessError if the instruction is not in an instance
 800   // initializer method <init>. If resolution were not inhibited, a putfield
 801   // in an initializer method could be resolved in the initializer. Subsequent
 802   // putfield instructions to the same field would then use cached information.
 803   // As a result, those instructions would not pass through the VM. That is,
 804   // checks in resolve_field_access() would not be executed for those instructions
 805   // and the required IllegalAccessError would not be thrown.
 806   //
 807   // Also, we need to delay resolving getstatic and putstatic instructions until the
 808   // class is initialized.  This is required so that access to the static
 809   // field will call the initialization function every time until the class
 810   // is completely initialized ala. in 2.17.5 in JVM Specification.
 811   InstanceKlass* klass = info.field_holder();
 812   bool uninitialized_static = is_static && !klass->is_initialized();
 813   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 814                                       info.has_initialized_final_update();
 815   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 816 
 817   Bytecodes::Code get_code = (Bytecodes::Code)0;
 818   Bytecodes::Code put_code = (Bytecodes::Code)0;
 819   if (!uninitialized_static) {
 820     if (is_static) {
 821       get_code = Bytecodes::_getstatic;
 822     } else {
 823       get_code = Bytecodes::_getfield;
 824     }
 825     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 826         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 827     }
 828   }
 829 
 830   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 831   entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile(),
 832                    info.is_flat(), info.is_null_free_inline_type());
 833   entry->fill_in(info.field_holder(), info.offset(),
 834                  checked_cast<u2>(info.index()), checked_cast<u1>(state),
 835                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 836 }
 837 
 838 
 839 //------------------------------------------------------------------------------------------------------------------------
 840 // Synchronization
 841 //
 842 // The interpreter's synchronization code is factored out so that it can
 843 // be shared by method invocation and synchronized blocks.
 844 //%note synchronization_3
 845 
 846 //%note monitor_1
 847 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 848   assert(LockingMode != LM_LIGHTWEIGHT, "Should call monitorenter_obj() when using the new lightweight locking");
 849 #ifdef ASSERT
 850   current->last_frame().interpreter_frame_verify_monitor(elem);
 851 #endif
 852   Handle h_obj(current, elem->obj());
 853   assert(Universe::heap()->is_in_or_null(h_obj()),
 854          "must be null or an object");
 855   ObjectSynchronizer::enter(h_obj, elem->lock(), current);
 856   assert(Universe::heap()->is_in_or_null(elem->obj()),
 857          "must be null or an object");
 858 #ifdef ASSERT
 859   current->last_frame().interpreter_frame_verify_monitor(elem);
 860 #endif
 861 JRT_END
 862 
 863 // NOTE: We provide a separate implementation for the new lightweight locking to workaround a limitation
 864 // of registers in x86_32. This entry point accepts an oop instead of a BasicObjectLock*.
 865 // The problem is that we would need to preserve the register that holds the BasicObjectLock,
 866 // but we are using that register to hold the thread. We don't have enough registers to
 867 // also keep the BasicObjectLock, but we don't really need it anyway, we only need
 868 // the object. See also InterpreterMacroAssembler::lock_object().
 869 // As soon as legacy stack-locking goes away we could remove the other monitorenter() entry
 870 // point, and only use oop-accepting entries (same for monitorexit() below).
 871 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter_obj(JavaThread* current, oopDesc* obj))
 872   assert(LockingMode == LM_LIGHTWEIGHT, "Should call monitorenter() when not using the new lightweight locking");
 873   Handle h_obj(current, cast_to_oop(obj));
 874   assert(Universe::heap()->is_in_or_null(h_obj()),
 875          "must be null or an object");
 876   ObjectSynchronizer::enter(h_obj, nullptr, current);
 877   return;
 878 JRT_END
 879 
 880 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 881   oop obj = elem->obj();
 882   assert(Universe::heap()->is_in(obj), "must be an object");
 883   // The object could become unlocked through a JNI call, which we have no other checks for.
 884   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 885   if (obj->is_unlocked()) {
 886     if (CheckJNICalls) {
 887       fatal("Object has been unlocked by JNI");
 888     }
 889     return;
 890   }
 891   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 892   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 893   // again at method exit or in the case of an exception.
 894   elem->set_obj(nullptr);
 895 JRT_END
 896 
 897 
 898 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 899   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 900 JRT_END
 901 
 902 
 903 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
 904   // Returns an illegal exception to install into the current thread. The
 905   // pending_exception flag is cleared so normal exception handling does not
 906   // trigger. Any current installed exception will be overwritten. This
 907   // method will be called during an exception unwind.
 908 
 909   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 910   Handle exception(current, current->vm_result());
 911   assert(exception() != nullptr, "vm result should be set");
 912   current->set_vm_result(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
 913   exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
 914   current->set_vm_result(exception());
 915 JRT_END
 916 
 917 
 918 //------------------------------------------------------------------------------------------------------------------------
 919 // Invokes
 920 
 921 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
 922   return method->orig_bytecode_at(method->bci_from(bcp));
 923 JRT_END
 924 
 925 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
 926   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 927 JRT_END
 928 
 929 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
 930   JvmtiExport::post_raw_breakpoint(current, method, bcp);
 931 JRT_END
 932 
 933 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) {
 934   LastFrameAccessor last_frame(current);
 935   // extract receiver from the outgoing argument list if necessary
 936   Handle receiver(current, nullptr);
 937   if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface ||
 938       bytecode == Bytecodes::_invokespecial) {
 939     ResourceMark rm(current);
 940     methodHandle m (current, last_frame.method());
 941     Bytecode_invoke call(m, last_frame.bci());
 942     Symbol* signature = call.signature();
 943     receiver = Handle(current, last_frame.callee_receiver(signature));
 944 
 945     assert(Universe::heap()->is_in_or_null(receiver()),
 946            "sanity check");
 947     assert(receiver.is_null() ||
 948            !Universe::heap()->is_in(receiver->klass()),
 949            "sanity check");
 950   }
 951 
 952   // resolve method
 953   CallInfo info;
 954   constantPoolHandle pool(current, last_frame.method()->constants());
 955   ConstantPoolCache* cache = pool->cache();
 956 
 957   methodHandle resolved_method;
 958 
 959   int method_index = last_frame.get_index_u2(bytecode);
 960   {
 961     JvmtiHideSingleStepping jhss(current);
 962     JavaThread* THREAD = current; // For exception macros.
 963     LinkResolver::resolve_invoke(info, receiver, pool,
 964                                  method_index, bytecode,
 965                                  THREAD);
 966 
 967     if (HAS_PENDING_EXCEPTION) {
 968       if (ProfileTraps && PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_NullPointerException()) {
 969         // Preserve the original exception across the call to note_trap()
 970         PreserveExceptionMark pm(current);
 971         // Recording the trap will help the compiler to potentially recognize this exception as "hot"
 972         note_trap(current, Deoptimization::Reason_null_check);
 973       }
 974       return;
 975     }
 976 
 977     if (JvmtiExport::can_hotswap_or_post_breakpoint() && info.resolved_method()->is_old()) {
 978       resolved_method = methodHandle(current, info.resolved_method()->get_new_method());
 979     } else {
 980       resolved_method = methodHandle(current, info.resolved_method());
 981     }
 982   } // end JvmtiHideSingleStepping
 983 
 984   // check if link resolution caused cpCache to be updated
 985   if (cache->resolved_method_entry_at(method_index)->is_resolved(bytecode)) return;
 986 
 987 #ifdef ASSERT
 988   if (bytecode == Bytecodes::_invokeinterface) {
 989     if (resolved_method->method_holder() == vmClasses::Object_klass()) {
 990       // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
 991       // (see also CallInfo::set_interface for details)
 992       assert(info.call_kind() == CallInfo::vtable_call ||
 993              info.call_kind() == CallInfo::direct_call, "");
 994       assert(resolved_method->is_final() || info.has_vtable_index(),
 995              "should have been set already");
 996     } else if (!resolved_method->has_itable_index()) {
 997       // Resolved something like CharSequence.toString.  Use vtable not itable.
 998       assert(info.call_kind() != CallInfo::itable_call, "");
 999     } else {
1000       // Setup itable entry
1001       assert(info.call_kind() == CallInfo::itable_call, "");
1002       int index = resolved_method->itable_index();
1003       assert(info.itable_index() == index, "");
1004     }
1005   } else if (bytecode == Bytecodes::_invokespecial) {
1006     assert(info.call_kind() == CallInfo::direct_call, "must be direct call");
1007   } else {
1008     assert(info.call_kind() == CallInfo::direct_call ||
1009            info.call_kind() == CallInfo::vtable_call, "");
1010   }
1011 #endif
1012   // Get sender and only set cpCache entry to resolved if it is not an
1013   // interface.  The receiver for invokespecial calls within interface
1014   // methods must be checked for every call.
1015   InstanceKlass* sender = pool->pool_holder();
1016 
1017   switch (info.call_kind()) {
1018   case CallInfo::direct_call:
1019     cache->set_direct_call(bytecode, method_index, resolved_method, sender->is_interface());
1020     break;
1021   case CallInfo::vtable_call:
1022     cache->set_vtable_call(bytecode, method_index, resolved_method, info.vtable_index());
1023     break;
1024   case CallInfo::itable_call:
1025     cache->set_itable_call(
1026       bytecode,
1027       method_index,
1028       info.resolved_klass(),
1029       resolved_method,
1030       info.itable_index());
1031     break;
1032   default:  ShouldNotReachHere();
1033   }
1034 }
1035 
1036 
1037 // First time execution:  Resolve symbols, create a permanent MethodType object.
1038 void InterpreterRuntime::resolve_invokehandle(JavaThread* current) {
1039   const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
1040   LastFrameAccessor last_frame(current);
1041 
1042   // resolve method
1043   CallInfo info;
1044   constantPoolHandle pool(current, last_frame.method()->constants());
1045   int method_index = last_frame.get_index_u2(bytecode);
1046   {
1047     JvmtiHideSingleStepping jhss(current);
1048     JavaThread* THREAD = current; // For exception macros.
1049     LinkResolver::resolve_invoke(info, Handle(), pool,
1050                                  method_index, bytecode,
1051                                  CHECK);
1052   } // end JvmtiHideSingleStepping
1053 
1054   pool->cache()->set_method_handle(method_index, info);
1055 }
1056 
1057 // First time execution:  Resolve symbols, create a permanent CallSite object.
1058 void InterpreterRuntime::resolve_invokedynamic(JavaThread* current) {
1059   LastFrameAccessor last_frame(current);
1060   const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
1061 
1062   // resolve method
1063   CallInfo info;
1064   constantPoolHandle pool(current, last_frame.method()->constants());
1065   int index = last_frame.get_index_u4(bytecode);
1066   {
1067     JvmtiHideSingleStepping jhss(current);
1068     JavaThread* THREAD = current; // For exception macros.
1069     LinkResolver::resolve_invoke(info, Handle(), pool,
1070                                  index, bytecode, CHECK);
1071   } // end JvmtiHideSingleStepping
1072 
1073   pool->cache()->set_dynamic_call(info, pool->decode_invokedynamic_index(index));
1074 }
1075 
1076 // This function is the interface to the assembly code. It returns the resolved
1077 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
1078 // This function will check for redefinition!
1079 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
1080   switch (bytecode) {
1081   case Bytecodes::_getstatic:
1082   case Bytecodes::_putstatic:
1083   case Bytecodes::_getfield:
1084   case Bytecodes::_putfield:
1085     resolve_get_put(current, bytecode);
1086     break;
1087   case Bytecodes::_invokevirtual:
1088   case Bytecodes::_invokespecial:
1089   case Bytecodes::_invokestatic:
1090   case Bytecodes::_invokeinterface:
1091     resolve_invoke(current, bytecode);
1092     break;
1093   case Bytecodes::_invokehandle:
1094     resolve_invokehandle(current);
1095     break;
1096   case Bytecodes::_invokedynamic:
1097     resolve_invokedynamic(current);
1098     break;
1099   default:
1100     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
1101     break;
1102   }
1103 }
1104 JRT_END
1105 
1106 //------------------------------------------------------------------------------------------------------------------------
1107 // Miscellaneous
1108 
1109 
1110 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* current, address branch_bcp) {
1111   // Enable WXWrite: the function is called directly by interpreter.
1112   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1113 
1114   // frequency_counter_overflow_inner can throw async exception.
1115   nmethod* nm = frequency_counter_overflow_inner(current, branch_bcp);
1116   assert(branch_bcp != nullptr || nm == nullptr, "always returns null for non OSR requests");
1117   if (branch_bcp != nullptr && nm != nullptr) {
1118     // This was a successful request for an OSR nmethod.  Because
1119     // frequency_counter_overflow_inner ends with a safepoint check,
1120     // nm could have been unloaded so look it up again.  It's unsafe
1121     // to examine nm directly since it might have been freed and used
1122     // for something else.
1123     LastFrameAccessor last_frame(current);
1124     Method* method =  last_frame.method();
1125     int bci = method->bci_from(last_frame.bcp());
1126     nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
1127     BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1128     if (nm != nullptr && bs_nm != nullptr) {
1129       // in case the transition passed a safepoint we need to barrier this again
1130       if (!bs_nm->nmethod_osr_entry_barrier(nm)) {
1131         nm = nullptr;
1132       }
1133     }
1134   }
1135   if (nm != nullptr && current->is_interp_only_mode()) {
1136     // Normally we never get an nm if is_interp_only_mode() is true, because
1137     // policy()->event has a check for this and won't compile the method when
1138     // true. However, it's possible for is_interp_only_mode() to become true
1139     // during the compilation. We don't want to return the nm in that case
1140     // because we want to continue to execute interpreted.
1141     nm = nullptr;
1142   }
1143 #ifndef PRODUCT
1144   if (TraceOnStackReplacement) {
1145     if (nm != nullptr) {
1146       tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", p2i(nm->osr_entry()));
1147       nm->print();
1148     }
1149   }
1150 #endif
1151   return nm;
1152 }
1153 
1154 JRT_ENTRY(nmethod*,
1155           InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* current, address branch_bcp))
1156   // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
1157   // flag, in case this method triggers classloading which will call into Java.
1158   UnlockFlagSaver fs(current);
1159 
1160   LastFrameAccessor last_frame(current);
1161   assert(last_frame.is_interpreted_frame(), "must come from interpreter");
1162   methodHandle method(current, last_frame.method());
1163   const int branch_bci = branch_bcp != nullptr ? method->bci_from(branch_bcp) : InvocationEntryBci;
1164   const int bci = branch_bcp != nullptr ? method->bci_from(last_frame.bcp()) : InvocationEntryBci;
1165 
1166   nmethod* osr_nm = CompilationPolicy::event(method, method, branch_bci, bci, CompLevel_none, nullptr, CHECK_NULL);
1167 
1168   BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1169   if (osr_nm != nullptr && bs_nm != nullptr) {
1170     if (!bs_nm->nmethod_osr_entry_barrier(osr_nm)) {
1171       osr_nm = nullptr;
1172     }
1173   }
1174   return osr_nm;
1175 JRT_END
1176 
1177 JRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
1178   assert(ProfileInterpreter, "must be profiling interpreter");
1179   int bci = method->bci_from(cur_bcp);
1180   MethodData* mdo = method->method_data();
1181   if (mdo == nullptr)  return 0;
1182   return mdo->bci_to_di(bci);
1183 JRT_END
1184 
1185 #ifdef ASSERT
1186 JRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
1187   assert(ProfileInterpreter, "must be profiling interpreter");
1188 
1189   MethodData* mdo = method->method_data();
1190   assert(mdo != nullptr, "must not be null");
1191 
1192   int bci = method->bci_from(bcp);
1193 
1194   address mdp2 = mdo->bci_to_dp(bci);
1195   if (mdp != mdp2) {
1196     ResourceMark rm;
1197     tty->print_cr("FAILED verify : actual mdp %p   expected mdp %p @ bci %d", mdp, mdp2, bci);
1198     int current_di = mdo->dp_to_di(mdp);
1199     int expected_di  = mdo->dp_to_di(mdp2);
1200     tty->print_cr("  actual di %d   expected di %d", current_di, expected_di);
1201     int expected_approx_bci = mdo->data_at(expected_di)->bci();
1202     int approx_bci = -1;
1203     if (current_di >= 0) {
1204       approx_bci = mdo->data_at(current_di)->bci();
1205     }
1206     tty->print_cr("  actual bci is %d  expected bci %d", approx_bci, expected_approx_bci);
1207     mdo->print_on(tty);
1208     method->print_codes();
1209   }
1210   assert(mdp == mdp2, "wrong mdp");
1211 JRT_END
1212 #endif // ASSERT
1213 
1214 JRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* current, int return_bci))
1215   assert(ProfileInterpreter, "must be profiling interpreter");
1216   ResourceMark rm(current);
1217   LastFrameAccessor last_frame(current);
1218   assert(last_frame.is_interpreted_frame(), "must come from interpreter");
1219   MethodData* h_mdo = last_frame.method()->method_data();
1220 
1221   // Grab a lock to ensure atomic access to setting the return bci and
1222   // the displacement.  This can block and GC, invalidating all naked oops.
1223   MutexLocker ml(RetData_lock);
1224 
1225   // ProfileData is essentially a wrapper around a derived oop, so we
1226   // need to take the lock before making any ProfileData structures.
1227   ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(last_frame.mdp()));
1228   guarantee(data != nullptr, "profile data must be valid");
1229   RetData* rdata = data->as_RetData();
1230   address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
1231   last_frame.set_mdp(new_mdp);
1232 JRT_END
1233 
1234 JRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* current, Method* m))
1235   return Method::build_method_counters(current, m);
1236 JRT_END
1237 
1238 
1239 JRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* current))
1240   // We used to need an explicit preserve_arguments here for invoke bytecodes. However,
1241   // stack traversal automatically takes care of preserving arguments for invoke, so
1242   // this is no longer needed.
1243 
1244   // JRT_END does an implicit safepoint check, hence we are guaranteed to block
1245   // if this is called during a safepoint
1246 
1247   if (JvmtiExport::should_post_single_step()) {
1248     // This function is called by the interpreter when single stepping. Such single
1249     // stepping could unwind a frame. Then, it is important that we process any frames
1250     // that we might return into.
1251     StackWatermarkSet::before_unwind(current);
1252 
1253     // We are called during regular safepoints and when the VM is
1254     // single stepping. If any thread is marked for single stepping,
1255     // then we may have JVMTI work to do.
1256     LastFrameAccessor last_frame(current);
1257     JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1258   }
1259 JRT_END
1260 
1261 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1262   assert(current == JavaThread::current(), "pre-condition");
1263   // This function is called by the interpreter when the return poll found a reason
1264   // to call the VM. The reason could be that we are returning into a not yet safe
1265   // to access frame. We handle that below.
1266   // Note that this path does not check for single stepping, because we do not want
1267   // to single step when unwinding frames for an exception being thrown. Instead,
1268   // such single stepping code will use the safepoint table, which will use the
1269   // InterpreterRuntime::at_safepoint callback.
1270   StackWatermarkSet::before_unwind(current);
1271 JRT_END
1272 
1273 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1274                                                       ResolvedFieldEntry *entry))
1275 
1276   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1277   // check the access_flags for the field in the klass
1278 
1279   InstanceKlass* ik = entry->field_holder();
1280   int index = entry->field_index();
1281   if (!ik->field_status(index).is_access_watched()) return;
1282 
1283   bool is_static = (obj == nullptr);
1284   bool is_flat = entry->is_flat();
1285   HandleMark hm(current);
1286 
1287   Handle h_obj;
1288   if (!is_static) {
1289     // non-static field accessors have an object, but we need a handle
1290     h_obj = Handle(current, obj);
1291   }
1292   InstanceKlass* field_holder = entry->field_holder(); // HERE
1293   jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static, is_flat);
1294   LastFrameAccessor last_frame(current);
1295   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1296 JRT_END
1297 
1298 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1299                                                             ResolvedFieldEntry *entry, jvalue *value))
1300 
1301   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1302   InstanceKlass* ik = entry->field_holder();
1303 
1304   // check the access_flags for the field in the klass
1305   int index = entry->field_index();
1306   // bail out if field modifications are not watched
1307   if (!ik->field_status(index).is_modification_watched()) return;
1308 
1309   char sig_type = '\0';
1310 
1311   switch((TosState)entry->tos_state()) {
1312     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1313     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1314     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1315     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1316     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1317     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1318     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1319     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1320     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1321     default:  ShouldNotReachHere(); return;
1322   }
1323 
1324   bool is_static = (obj == nullptr);
1325   bool is_flat = entry->is_flat();
1326 
1327   HandleMark hm(current);
1328   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static, is_flat);
1329   jvalue fvalue;
1330 #ifdef _LP64
1331   fvalue = *value;
1332 #else
1333   // Long/double values are stored unaligned and also noncontiguously with
1334   // tagged stacks.  We can't just do a simple assignment even in the non-
1335   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1336   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1337   // We assume that the two halves of longs/doubles are stored in interpreter
1338   // stack slots in platform-endian order.
1339   jlong_accessor u;
1340   jint* newval = (jint*)value;
1341   u.words[0] = newval[0];
1342   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1343   fvalue.j = u.long_value;
1344 #endif // _LP64
1345 
1346   Handle h_obj;
1347   if (!is_static) {
1348     // non-static field accessors have an object, but we need a handle
1349     h_obj = Handle(current, obj);
1350   }
1351 
1352   LastFrameAccessor last_frame(current);
1353   JvmtiExport::post_raw_field_modification(current, last_frame.method(), last_frame.bcp(), ik, h_obj,
1354                                            fid, sig_type, &fvalue);
1355 JRT_END
1356 
1357 JRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread* current))
1358   LastFrameAccessor last_frame(current);
1359   JvmtiExport::post_method_entry(current, last_frame.method(), last_frame.get_frame());
1360 JRT_END
1361 
1362 
1363 // This is a JRT_BLOCK_ENTRY because we have to stash away the return oop
1364 // before transitioning to VM, and restore it after transitioning back
1365 // to Java. The return oop at the top-of-stack, is not walked by the GC.
1366 JRT_BLOCK_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread* current))
1367   LastFrameAccessor last_frame(current);
1368   JvmtiExport::post_method_exit(current, last_frame.method(), last_frame.get_frame());
1369 JRT_END
1370 
1371 JRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1372 {
1373   return (Interpreter::contains(Continuation::get_top_return_pc_post_barrier(JavaThread::current(), pc)) ? 1 : 0);
1374 }
1375 JRT_END
1376 
1377 
1378 // Implementation of SignatureHandlerLibrary
1379 
1380 #ifndef SHARING_FAST_NATIVE_FINGERPRINTS
1381 // Dummy definition (else normalization method is defined in CPU
1382 // dependent code)
1383 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
1384   return fingerprint;
1385 }
1386 #endif
1387 
1388 address SignatureHandlerLibrary::set_handler_blob() {
1389   BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1390   if (handler_blob == nullptr) {
1391     return nullptr;
1392   }
1393   address handler = handler_blob->code_begin();
1394   _handler_blob = handler_blob;
1395   _handler = handler;
1396   return handler;
1397 }
1398 
1399 void SignatureHandlerLibrary::initialize() {
1400   if (_fingerprints != nullptr) {
1401     return;
1402   }
1403   if (set_handler_blob() == nullptr) {
1404     vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1405   }
1406 
1407   BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1408                                       SignatureHandlerLibrary::buffer_size);
1409   _buffer = bb->code_begin();
1410 
1411   _fingerprints = new (mtCode) GrowableArray<uint64_t>(32, mtCode);
1412   _handlers     = new (mtCode) GrowableArray<address>(32, mtCode);
1413 }
1414 
1415 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1416   address handler   = _handler;
1417   int     insts_size = buffer->pure_insts_size();
1418   if (handler + insts_size > _handler_blob->code_end()) {
1419     // get a new handler blob
1420     handler = set_handler_blob();
1421   }
1422   if (handler != nullptr) {
1423     memcpy(handler, buffer->insts_begin(), insts_size);
1424     pd_set_handler(handler);
1425     ICache::invalidate_range(handler, insts_size);
1426     _handler = handler + insts_size;
1427   }
1428   return handler;
1429 }
1430 
1431 void SignatureHandlerLibrary::add(const methodHandle& method) {
1432   if (method->signature_handler() == nullptr) {
1433     // use slow signature handler if we can't do better
1434     int handler_index = -1;
1435     // check if we can use customized (fast) signature handler
1436     if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::fp_max_size_of_parameters) {
1437       // use customized signature handler
1438       MutexLocker mu(SignatureHandlerLibrary_lock);
1439       // make sure data structure is initialized
1440       initialize();
1441       // lookup method signature's fingerprint
1442       uint64_t fingerprint = Fingerprinter(method).fingerprint();
1443       // allow CPU dependent code to optimize the fingerprints for the fast handler
1444       fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1445       handler_index = _fingerprints->find(fingerprint);
1446       // create handler if necessary
1447       if (handler_index < 0) {
1448         ResourceMark rm;
1449         ptrdiff_t align_offset = align_up(_buffer, CodeEntryAlignment) - (address)_buffer;
1450         CodeBuffer buffer((address)(_buffer + align_offset),
1451                           checked_cast<int>(SignatureHandlerLibrary::buffer_size - align_offset));
1452         InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1453         // copy into code heap
1454         address handler = set_handler(&buffer);
1455         if (handler == nullptr) {
1456           // use slow signature handler (without memorizing it in the fingerprints)
1457         } else {
1458           // debugging support
1459           if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) {
1460             ttyLocker ttyl;
1461             tty->cr();
1462             tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1463                           _handlers->length(),
1464                           (method->is_static() ? "static" : "receiver"),
1465                           method->name_and_sig_as_C_string(),
1466                           fingerprint,
1467                           buffer.insts_size());
1468             if (buffer.insts_size() > 0) {
1469               Disassembler::decode(handler, handler + buffer.insts_size(), tty
1470                                    NOT_PRODUCT(COMMA &buffer.asm_remarks()));
1471             }
1472 #ifndef PRODUCT
1473             address rh_begin = Interpreter::result_handler(method()->result_type());
1474             if (CodeCache::contains(rh_begin)) {
1475               // else it might be special platform dependent values
1476               tty->print_cr(" --- associated result handler ---");
1477               address rh_end = rh_begin;
1478               while (*(int*)rh_end != 0) {
1479                 rh_end += sizeof(int);
1480               }
1481               Disassembler::decode(rh_begin, rh_end);
1482             } else {
1483               tty->print_cr(" associated result handler: " PTR_FORMAT, p2i(rh_begin));
1484             }
1485 #endif
1486           }
1487           // add handler to library
1488           _fingerprints->append(fingerprint);
1489           _handlers->append(handler);
1490           // set handler index
1491           assert(_fingerprints->length() == _handlers->length(), "sanity check");
1492           handler_index = _fingerprints->length() - 1;
1493         }
1494       }
1495       // Set handler under SignatureHandlerLibrary_lock
1496       if (handler_index < 0) {
1497         // use generic signature handler
1498         method->set_signature_handler(Interpreter::slow_signature_handler());
1499       } else {
1500         // set handler
1501         method->set_signature_handler(_handlers->at(handler_index));
1502       }
1503     } else {
1504       DEBUG_ONLY(JavaThread::current()->check_possible_safepoint());
1505       // use generic signature handler
1506       method->set_signature_handler(Interpreter::slow_signature_handler());
1507     }
1508   }
1509 #ifdef ASSERT
1510   int handler_index = -1;
1511   int fingerprint_index = -2;
1512   {
1513     // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1514     // in any way if accessed from multiple threads. To avoid races with another
1515     // thread which may change the arrays in the above, mutex protected block, we
1516     // have to protect this read access here with the same mutex as well!
1517     MutexLocker mu(SignatureHandlerLibrary_lock);
1518     if (_handlers != nullptr) {
1519       handler_index = _handlers->find(method->signature_handler());
1520       uint64_t fingerprint = Fingerprinter(method).fingerprint();
1521       fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1522       fingerprint_index = _fingerprints->find(fingerprint);
1523     }
1524   }
1525   assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1526          handler_index == fingerprint_index, "sanity check");
1527 #endif // ASSERT
1528 }
1529 
1530 void SignatureHandlerLibrary::add(uint64_t fingerprint, address handler) {
1531   int handler_index = -1;
1532   // use customized signature handler
1533   MutexLocker mu(SignatureHandlerLibrary_lock);
1534   // make sure data structure is initialized
1535   initialize();
1536   fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1537   handler_index = _fingerprints->find(fingerprint);
1538   // create handler if necessary
1539   if (handler_index < 0) {
1540     if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) {
1541       tty->cr();
1542       tty->print_cr("argument handler #%d at " PTR_FORMAT " for fingerprint " UINT64_FORMAT,
1543                     _handlers->length(),
1544                     p2i(handler),
1545                     fingerprint);
1546     }
1547     _fingerprints->append(fingerprint);
1548     _handlers->append(handler);
1549   } else {
1550     if (PrintSignatureHandlers) {
1551       tty->cr();
1552       tty->print_cr("duplicate argument handler #%d for fingerprint " UINT64_FORMAT "(old: " PTR_FORMAT ", new : " PTR_FORMAT ")",
1553                     _handlers->length(),
1554                     fingerprint,
1555                     p2i(_handlers->at(handler_index)),
1556                     p2i(handler));
1557     }
1558   }
1559 }
1560 
1561 
1562 BufferBlob*              SignatureHandlerLibrary::_handler_blob = nullptr;
1563 address                  SignatureHandlerLibrary::_handler      = nullptr;
1564 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = nullptr;
1565 GrowableArray<address>*  SignatureHandlerLibrary::_handlers     = nullptr;
1566 address                  SignatureHandlerLibrary::_buffer       = nullptr;
1567 
1568 
1569 JRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* current, Method* method))
1570   methodHandle m(current, method);
1571   assert(m->is_native(), "sanity check");
1572   // lookup native function entry point if it doesn't exist
1573   if (!m->has_native_function()) {
1574     NativeLookup::lookup(m, CHECK);
1575   }
1576   // make sure signature handler is installed
1577   SignatureHandlerLibrary::add(m);
1578   // The interpreter entry point checks the signature handler first,
1579   // before trying to fetch the native entry point and klass mirror.
1580   // We must set the signature handler last, so that multiple processors
1581   // preparing the same method will be sure to see non-null entry & mirror.
1582 JRT_END
1583 
1584 #if defined(IA32) || defined(AMD64) || defined(ARM)
1585 JRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* current, void* src_address, void* dest_address))
1586   assert(current == JavaThread::current(), "pre-condition");
1587   if (src_address == dest_address) {
1588     return;
1589   }
1590   ResourceMark rm;
1591   LastFrameAccessor last_frame(current);
1592   assert(last_frame.is_interpreted_frame(), "");
1593   jint bci = last_frame.bci();
1594   methodHandle mh(current, last_frame.method());
1595   Bytecode_invoke invoke(mh, bci);
1596   ArgumentSizeComputer asc(invoke.signature());
1597   int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1598   Copy::conjoint_jbytes(src_address, dest_address,
1599                        size_of_arguments * Interpreter::stackElementSize);
1600 JRT_END
1601 #endif
1602 
1603 #if INCLUDE_JVMTI
1604 // This is a support of the JVMTI PopFrame interface.
1605 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1606 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters.
1607 // The member_name argument is a saved reference (in local#0) to the member_name.
1608 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle.
1609 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated.
1610 JRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* current, address member_name,
1611                                                             Method* method, address bcp))
1612   Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1613   if (code != Bytecodes::_invokestatic) {
1614     return;
1615   }
1616   ConstantPool* cpool = method->constants();
1617   int cp_index = Bytes::get_native_u2(bcp + 1);
1618   Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index, code));
1619   Symbol* mname = cpool->name_ref_at(cp_index, code);
1620 
1621   if (MethodHandles::has_member_arg(cname, mname)) {
1622     oop member_name_oop = cast_to_oop(member_name);
1623     if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) {
1624       // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated.
1625       member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop);
1626     }
1627     current->set_vm_result(member_name_oop);
1628   } else {
1629     current->set_vm_result(nullptr);
1630   }
1631 JRT_END
1632 #endif // INCLUDE_JVMTI
1633 
1634 #ifndef PRODUCT
1635 // This must be a JRT_LEAF function because the interpreter must save registers on x86 to
1636 // call this, which changes rsp and makes the interpreter's expression stack not walkable.
1637 // The generated code still uses call_VM because that will set up the frame pointer for
1638 // bcp and method.
1639 JRT_LEAF(intptr_t, InterpreterRuntime::trace_bytecode(JavaThread* current, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2))
1640   assert(current == JavaThread::current(), "pre-condition");
1641   LastFrameAccessor last_frame(current);
1642   assert(last_frame.is_interpreted_frame(), "must be an interpreted frame");
1643   methodHandle mh(current, last_frame.method());
1644   BytecodeTracer::trace_interpreter(mh, last_frame.bcp(), tos, tos2);
1645   return preserve_this_value;
1646 JRT_END
1647 #endif // !PRODUCT