1 /*
   2  * Copyright (c) 2012, 2024, 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 #include "precompiled.hpp"
  25 #include "classfile/javaClasses.inline.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "gc/shared/memAllocator.hpp"
  32 #include "gc/shared/oopStorage.inline.hpp"
  33 #include "jvmci/jniAccessMark.inline.hpp"
  34 #include "jvmci/jvmciCompilerToVM.hpp"
  35 #include "jvmci/jvmciCodeInstaller.hpp"
  36 #include "jvmci/jvmciRuntime.hpp"
  37 #include "jvmci/metadataHandles.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logStream.hpp"
  40 #include "memory/oopFactory.hpp"
  41 #include "memory/universe.hpp"
  42 #include "oops/constantPool.inline.hpp"
  43 #include "oops/klass.inline.hpp"
  44 #include "oops/method.inline.hpp"
  45 #include "oops/objArrayKlass.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "oops/typeArrayOop.inline.hpp"
  48 #include "prims/jvmtiExport.hpp"
  49 #include "prims/methodHandles.hpp"
  50 #include "runtime/arguments.hpp"
  51 #include "runtime/atomic.hpp"
  52 #include "runtime/deoptimization.hpp"
  53 #include "runtime/fieldDescriptor.inline.hpp"
  54 #include "runtime/frame.inline.hpp"
  55 #include "runtime/java.hpp"
  56 #include "runtime/jniHandles.inline.hpp"
  57 #include "runtime/mutex.hpp"
  58 #include "runtime/reflection.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/synchronizer.hpp"
  61 #if INCLUDE_G1GC
  62 #include "gc/g1/g1BarrierSetRuntime.hpp"
  63 #endif // INCLUDE_G1GC
  64 
  65 // Simple helper to see if the caller of a runtime stub which
  66 // entered the VM has been deoptimized
  67 
  68 static bool caller_is_deopted() {
  69   JavaThread* thread = JavaThread::current();
  70   RegisterMap reg_map(thread,
  71                       RegisterMap::UpdateMap::skip,
  72                       RegisterMap::ProcessFrames::include,
  73                       RegisterMap::WalkContinuation::skip);
  74   frame runtime_frame = thread->last_frame();
  75   frame caller_frame = runtime_frame.sender(&reg_map);
  76   assert(caller_frame.is_compiled_frame(), "must be compiled");
  77   return caller_frame.is_deoptimized_frame();
  78 }
  79 
  80 // Stress deoptimization
  81 static void deopt_caller() {
  82   if ( !caller_is_deopted()) {
  83     JavaThread* thread = JavaThread::current();
  84     RegisterMap reg_map(thread,
  85                         RegisterMap::UpdateMap::skip,
  86                         RegisterMap::ProcessFrames::include,
  87                         RegisterMap::WalkContinuation::skip);
  88     frame runtime_frame = thread->last_frame();
  89     frame caller_frame = runtime_frame.sender(&reg_map);
  90     Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint);
  91     assert(caller_is_deopted(), "Must be deoptimized");
  92   }
  93 }
  94 
  95 // Manages a scope for a JVMCI runtime call that attempts a heap allocation.
  96 // If there is a pending OutOfMemoryError upon closing the scope and the runtime
  97 // call is of the variety where allocation failure returns null without an
  98 // exception, the following action is taken:
  99 //   1. The pending OutOfMemoryError is cleared
 100 //   2. null is written to JavaThread::_vm_result
 101 class RetryableAllocationMark {
 102  private:
 103    InternalOOMEMark _iom;
 104  public:
 105   RetryableAllocationMark(JavaThread* thread) : _iom(thread) {}
 106   ~RetryableAllocationMark() {
 107     JavaThread* THREAD = _iom.thread(); // For exception macros.
 108     if (THREAD != nullptr) {
 109       if (HAS_PENDING_EXCEPTION) {
 110         oop ex = PENDING_EXCEPTION;
 111         THREAD->set_vm_result(nullptr);
 112         if (ex->is_a(vmClasses::OutOfMemoryError_klass())) {
 113           CLEAR_PENDING_EXCEPTION;
 114         }
 115       }
 116     }
 117   }
 118 };
 119 
 120 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_or_null(JavaThread* current, Klass* klass))
 121   JRT_BLOCK;
 122   assert(klass->is_klass(), "not a class");
 123   Handle holder(current, klass->klass_holder()); // keep the klass alive
 124   InstanceKlass* h = InstanceKlass::cast(klass);
 125   {
 126     RetryableAllocationMark ram(current);
 127     h->check_valid_for_instantiation(true, CHECK);
 128     if (!h->is_initialized()) {
 129       // Cannot re-execute class initialization without side effects
 130       // so return without attempting the initialization
 131       current->set_vm_result(nullptr);
 132       return;
 133     }
 134     // allocate instance and return via TLS
 135     oop obj = h->allocate_instance(CHECK);
 136     current->set_vm_result(obj);
 137   }
 138   JRT_BLOCK_END;
 139   SharedRuntime::on_slowpath_allocation_exit(current);
 140 JRT_END
 141 
 142 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array_or_null(JavaThread* current, Klass* array_klass, jint length))
 143   JRT_BLOCK;
 144   // Note: no handle for klass needed since they are not used
 145   //       anymore after new_objArray() and no GC can happen before.
 146   //       (This may have to change if this code changes!)
 147   assert(array_klass->is_klass(), "not a class");
 148   oop obj;
 149   if (array_klass->is_typeArray_klass()) {
 150     BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type();
 151     RetryableAllocationMark ram(current);
 152     obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 153   } else {
 154     Handle holder(current, array_klass->klass_holder()); // keep the klass alive
 155     Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
 156     RetryableAllocationMark ram(current);
 157     obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 158   }
 159   // This is pretty rare but this runtime patch is stressful to deoptimization
 160   // if we deoptimize here so force a deopt to stress the path.
 161   if (DeoptimizeALot) {
 162     static int deopts = 0;
 163     if (deopts++ % 2 == 0) {
 164       // Drop the allocation
 165       obj = nullptr;
 166     } else {
 167       deopt_caller();
 168     }
 169   }
 170   current->set_vm_result(obj);
 171   JRT_BLOCK_END;
 172   SharedRuntime::on_slowpath_allocation_exit(current);
 173 JRT_END
 174 
 175 JRT_ENTRY(void, JVMCIRuntime::new_multi_array_or_null(JavaThread* current, Klass* klass, int rank, jint* dims))
 176   assert(klass->is_klass(), "not a class");
 177   assert(rank >= 1, "rank must be nonzero");
 178   Handle holder(current, klass->klass_holder()); // keep the klass alive
 179   RetryableAllocationMark ram(current);
 180   oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 181   current->set_vm_result(obj);
 182 JRT_END
 183 
 184 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array_or_null(JavaThread* current, oopDesc* element_mirror, jint length))
 185   RetryableAllocationMark ram(current);
 186   oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK);
 187   current->set_vm_result(obj);
 188 JRT_END
 189 
 190 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance_or_null(JavaThread* current, oopDesc* type_mirror))
 191   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror));
 192 
 193   if (klass == nullptr) {
 194     ResourceMark rm(current);
 195     THROW(vmSymbols::java_lang_InstantiationException());
 196   }
 197   RetryableAllocationMark ram(current);
 198 
 199   // Create new instance (the receiver)
 200   klass->check_valid_for_instantiation(false, CHECK);
 201 
 202   if (!klass->is_initialized()) {
 203     // Cannot re-execute class initialization without side effects
 204     // so return without attempting the initialization
 205     current->set_vm_result(nullptr);
 206     return;
 207   }
 208 
 209   oop obj = klass->allocate_instance(CHECK);
 210   current->set_vm_result(obj);
 211 JRT_END
 212 
 213 extern void vm_exit(int code);
 214 
 215 // Enter this method from compiled code handler below. This is where we transition
 216 // to VM mode. This is done as a helper routine so that the method called directly
 217 // from compiled code does not have to transition to VM. This allows the entry
 218 // method to see if the nmethod that we have just looked up a handler for has
 219 // been deoptimized while we were in the vm. This simplifies the assembly code
 220 // cpu directories.
 221 //
 222 // We are entering here from exception stub (via the entry method below)
 223 // If there is a compiled exception handler in this method, we will continue there;
 224 // otherwise we will unwind the stack and continue at the caller of top frame method
 225 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 226 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 227 // check to see if the handler we are going to return is now in a nmethod that has
 228 // been deoptimized. If that is the case we return the deopt blob
 229 // unpack_with_exception entry instead. This makes life for the exception blob easier
 230 // because making that same check and diverting is painful from assembly language.
 231 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
 232   // Reset method handle flag.
 233   current->set_is_method_handle_return(false);
 234 
 235   Handle exception(current, ex);
 236 
 237   // The frame we rethrow the exception to might not have been processed by the GC yet.
 238   // The stack watermark barrier takes care of detecting that and ensuring the frame
 239   // has updated oops.
 240   StackWatermarkSet::after_unwind(current);
 241 
 242   nm = CodeCache::find_nmethod(pc);
 243   assert(nm != nullptr, "did not find nmethod");
 244   // Adjust the pc as needed/
 245   if (nm->is_deopt_pc(pc)) {
 246     RegisterMap map(current,
 247                     RegisterMap::UpdateMap::skip,
 248                     RegisterMap::ProcessFrames::include,
 249                     RegisterMap::WalkContinuation::skip);
 250     frame exception_frame = current->last_frame().sender(&map);
 251     // if the frame isn't deopted then pc must not correspond to the caller of last_frame
 252     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
 253     pc = exception_frame.pc();
 254   }
 255   assert(exception.not_null(), "null exceptions should be handled by throw_exception");
 256   assert(oopDesc::is_oop(exception()), "just checking");
 257   // Check that exception is a subclass of Throwable
 258   assert(exception->is_a(vmClasses::Throwable_klass()),
 259          "Exception not subclass of Throwable");
 260 
 261   // debugging support
 262   // tracing
 263   if (log_is_enabled(Info, exceptions)) {
 264     ResourceMark rm;
 265     stringStream tempst;
 266     assert(nm->method() != nullptr, "Unexpected null method()");
 267     tempst.print("JVMCI compiled method <%s>\n"
 268                  " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT,
 269                  nm->method()->print_value_string(), p2i(pc), p2i(current));
 270     Exceptions::log_exception(exception, tempst.as_string());
 271   }
 272   // for AbortVMOnException flag
 273   Exceptions::debug_check_abort(exception);
 274 
 275   // Check the stack guard pages and re-enable them if necessary and there is
 276   // enough space on the stack to do so.  Use fast exceptions only if the guard
 277   // pages are enabled.
 278   bool guard_pages_enabled = current->stack_overflow_state()->reguard_stack_if_needed();
 279 
 280   if (JvmtiExport::can_post_on_exceptions()) {
 281     // To ensure correct notification of exception catches and throws
 282     // we have to deoptimize here.  If we attempted to notify the
 283     // catches and throws during this exception lookup it's possible
 284     // we could deoptimize on the way out of the VM and end back in
 285     // the interpreter at the throw site.  This would result in double
 286     // notifications since the interpreter would also notify about
 287     // these same catches and throws as it unwound the frame.
 288 
 289     RegisterMap reg_map(current,
 290                         RegisterMap::UpdateMap::include,
 291                         RegisterMap::ProcessFrames::include,
 292                         RegisterMap::WalkContinuation::skip);
 293     frame stub_frame = current->last_frame();
 294     frame caller_frame = stub_frame.sender(&reg_map);
 295 
 296     // We don't really want to deoptimize the nmethod itself since we
 297     // can actually continue in the exception handler ourselves but I
 298     // don't see an easy way to have the desired effect.
 299     Deoptimization::deoptimize_frame(current, caller_frame.id(), Deoptimization::Reason_constraint);
 300     assert(caller_is_deopted(), "Must be deoptimized");
 301 
 302     return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 303   }
 304 
 305   // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
 306   if (guard_pages_enabled) {
 307     address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
 308     if (fast_continuation != nullptr) {
 309       // Set flag if return address is a method handle call site.
 310       current->set_is_method_handle_return(nm->is_method_handle_return(pc));
 311       return fast_continuation;
 312     }
 313   }
 314 
 315   // If the stack guard pages are enabled, check whether there is a handler in
 316   // the current method.  Otherwise (guard pages disabled), force an unwind and
 317   // skip the exception cache update (i.e., just leave continuation==nullptr).
 318   address continuation = nullptr;
 319   if (guard_pages_enabled) {
 320 
 321     // New exception handling mechanism can support inlined methods
 322     // with exception handlers since the mappings are from PC to PC
 323 
 324     // Clear out the exception oop and pc since looking up an
 325     // exception handler can cause class loading, which might throw an
 326     // exception and those fields are expected to be clear during
 327     // normal bytecode execution.
 328     current->clear_exception_oop_and_pc();
 329 
 330     bool recursive_exception = false;
 331     continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false, recursive_exception);
 332     // If an exception was thrown during exception dispatch, the exception oop may have changed
 333     current->set_exception_oop(exception());
 334     current->set_exception_pc(pc);
 335 
 336     // The exception cache is used only for non-implicit exceptions
 337     // Update the exception cache only when another exception did
 338     // occur during the computation of the compiled exception handler
 339     // (e.g., when loading the class of the catch type).
 340     // Checking for exception oop equality is not
 341     // sufficient because some exceptions are pre-allocated and reused.
 342     if (continuation != nullptr && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) {
 343       nm->add_handler_for_exception_and_pc(exception, pc, continuation);
 344     }
 345   }
 346 
 347   // Set flag if return address is a method handle call site.
 348   current->set_is_method_handle_return(nm->is_method_handle_return(pc));
 349 
 350   if (log_is_enabled(Info, exceptions)) {
 351     ResourceMark rm;
 352     log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT
 353                          " for exception thrown at PC " PTR_FORMAT,
 354                          p2i(current), p2i(continuation), p2i(pc));
 355   }
 356 
 357   return continuation;
 358 JRT_END
 359 
 360 // Enter this method from compiled code only if there is a Java exception handler
 361 // in the method handling the exception.
 362 // We are entering here from exception stub. We don't do a normal VM transition here.
 363 // We do it in a helper. This is so we can check to see if the nmethod we have just
 364 // searched for an exception handler has been deoptimized in the meantime.
 365 address JVMCIRuntime::exception_handler_for_pc(JavaThread* current) {
 366   oop exception = current->exception_oop();
 367   address pc = current->exception_pc();
 368   // Still in Java mode
 369   DEBUG_ONLY(NoHandleMark nhm);
 370   nmethod* nm = nullptr;
 371   address continuation = nullptr;
 372   {
 373     // Enter VM mode by calling the helper
 374     ResetNoHandleMark rnhm;
 375     continuation = exception_handler_for_pc_helper(current, exception, pc, nm);
 376   }
 377   // Back in JAVA, use no oops DON'T safepoint
 378 
 379   // Now check to see if the compiled method we were called from is now deoptimized.
 380   // If so we must return to the deopt blob and deoptimize the nmethod
 381   if (nm != nullptr && caller_is_deopted()) {
 382     continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 383   }
 384 
 385   assert(continuation != nullptr, "no handler found");
 386   return continuation;
 387 }
 388 
 389 JRT_BLOCK_ENTRY(void, JVMCIRuntime::monitorenter(JavaThread* current, oopDesc* obj, BasicLock* lock))
 390   SharedRuntime::monitor_enter_helper(obj, lock, current);
 391 JRT_END
 392 
 393 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* current, oopDesc* obj, BasicLock* lock))
 394   assert(current == JavaThread::current(), "pre-condition");
 395   assert(current->last_Java_sp(), "last_Java_sp must be set");
 396   assert(oopDesc::is_oop(obj), "invalid lock object pointer dected");
 397   SharedRuntime::monitor_exit_helper(obj, lock, current);
 398 JRT_END
 399 
 400 // Object.notify() fast path, caller does slow path
 401 JRT_LEAF(jboolean, JVMCIRuntime::object_notify(JavaThread* current, oopDesc* obj))
 402   assert(current == JavaThread::current(), "pre-condition");
 403 
 404   // Very few notify/notifyAll operations find any threads on the waitset, so
 405   // the dominant fast-path is to simply return.
 406   // Relatedly, it's critical that notify/notifyAll be fast in order to
 407   // reduce lock hold times.
 408   if (!SafepointSynchronize::is_synchronizing()) {
 409     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 410       return true;
 411     }
 412   }
 413   return false; // caller must perform slow path
 414 
 415 JRT_END
 416 
 417 // Object.notifyAll() fast path, caller does slow path
 418 JRT_LEAF(jboolean, JVMCIRuntime::object_notifyAll(JavaThread* current, oopDesc* obj))
 419   assert(current == JavaThread::current(), "pre-condition");
 420 
 421   if (!SafepointSynchronize::is_synchronizing() ) {
 422     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 423       return true;
 424     }
 425   }
 426   return false; // caller must perform slow path
 427 
 428 JRT_END
 429 
 430 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* current, const char* exception, const char* message))
 431   JRT_BLOCK;
 432   TempNewSymbol symbol = SymbolTable::new_symbol(exception);
 433   SharedRuntime::throw_and_post_jvmti_exception(current, symbol, message);
 434   JRT_BLOCK_END;
 435   return caller_is_deopted();
 436 JRT_END
 437 
 438 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* current, const char* exception, Klass* klass))
 439   JRT_BLOCK;
 440   ResourceMark rm(current);
 441   TempNewSymbol symbol = SymbolTable::new_symbol(exception);
 442   SharedRuntime::throw_and_post_jvmti_exception(current, symbol, klass->external_name());
 443   JRT_BLOCK_END;
 444   return caller_is_deopted();
 445 JRT_END
 446 
 447 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_class_cast_exception(JavaThread* current, const char* exception, Klass* caster_klass, Klass* target_klass))
 448   JRT_BLOCK;
 449   ResourceMark rm(current);
 450   const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass);
 451   TempNewSymbol symbol = SymbolTable::new_symbol(exception);
 452   SharedRuntime::throw_and_post_jvmti_exception(current, symbol, message);
 453   JRT_BLOCK_END;
 454   return caller_is_deopted();
 455 JRT_END
 456 
 457 class ArgumentPusher : public SignatureIterator {
 458  protected:
 459   JavaCallArguments*  _jca;
 460   jlong _argument;
 461   bool _pushed;
 462 
 463   jlong next_arg() {
 464     guarantee(!_pushed, "one argument");
 465     _pushed = true;
 466     return _argument;
 467   }
 468 
 469   float next_float() {
 470     guarantee(!_pushed, "one argument");
 471     _pushed = true;
 472     jvalue v;
 473     v.i = (jint) _argument;
 474     return v.f;
 475   }
 476 
 477   double next_double() {
 478     guarantee(!_pushed, "one argument");
 479     _pushed = true;
 480     jvalue v;
 481     v.j = _argument;
 482     return v.d;
 483   }
 484 
 485   Handle next_object() {
 486     guarantee(!_pushed, "one argument");
 487     _pushed = true;
 488     return Handle(Thread::current(), cast_to_oop(_argument));
 489   }
 490 
 491  public:
 492   ArgumentPusher(Symbol* signature, JavaCallArguments*  jca, jlong argument) : SignatureIterator(signature) {
 493     this->_return_type = T_ILLEGAL;
 494     _jca = jca;
 495     _argument = argument;
 496     _pushed = false;
 497     do_parameters_on(this);
 498   }
 499 
 500   void do_type(BasicType type) {
 501     switch (type) {
 502       case T_OBJECT:
 503       case T_ARRAY:   _jca->push_oop(next_object());         break;
 504       case T_BOOLEAN: _jca->push_int((jboolean) next_arg()); break;
 505       case T_CHAR:    _jca->push_int((jchar) next_arg());    break;
 506       case T_SHORT:   _jca->push_int((jint)  next_arg());    break;
 507       case T_BYTE:    _jca->push_int((jbyte) next_arg());    break;
 508       case T_INT:     _jca->push_int((jint)  next_arg());    break;
 509       case T_LONG:    _jca->push_long((jlong) next_arg());   break;
 510       case T_FLOAT:   _jca->push_float(next_float());        break;
 511       case T_DOUBLE:  _jca->push_double(next_double());      break;
 512       default:        fatal("Unexpected type %s", type2name(type));
 513     }
 514   }
 515 };
 516 
 517 
 518 JRT_ENTRY(jlong, JVMCIRuntime::invoke_static_method_one_arg(JavaThread* current, Method* method, jlong argument))
 519   ResourceMark rm;
 520   HandleMark hm(current);
 521 
 522   methodHandle mh(current, method);
 523   if (mh->size_of_parameters() > 1 && !mh->is_static()) {
 524     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Invoked method must be static and take at most one argument");
 525   }
 526 
 527   Symbol* signature = mh->signature();
 528   JavaCallArguments jca(mh->size_of_parameters());
 529   ArgumentPusher jap(signature, &jca, argument);
 530   BasicType return_type = jap.return_type();
 531   JavaValue result(return_type);
 532   JavaCalls::call(&result, mh, &jca, CHECK_0);
 533 
 534   if (return_type == T_VOID) {
 535     return 0;
 536   } else if (return_type == T_OBJECT || return_type == T_ARRAY) {
 537     current->set_vm_result(result.get_oop());
 538     return 0;
 539   } else {
 540     jvalue *value = (jvalue *) result.get_value_addr();
 541     // Narrow the value down if required (Important on big endian machines)
 542     switch (return_type) {
 543       case T_BOOLEAN:
 544         return (jboolean) value->i;
 545       case T_BYTE:
 546         return (jbyte) value->i;
 547       case T_CHAR:
 548         return (jchar) value->i;
 549       case T_SHORT:
 550         return (jshort) value->i;
 551       case T_INT:
 552       case T_FLOAT:
 553         return value->i;
 554       case T_LONG:
 555       case T_DOUBLE:
 556         return value->j;
 557       default:
 558         fatal("Unexpected type %s", type2name(return_type));
 559         return 0;
 560     }
 561   }
 562 JRT_END
 563 
 564 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline))
 565   ttyLocker ttyl;
 566 
 567   if (obj == nullptr) {
 568     tty->print("null");
 569   } else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) {
 570     if (oopDesc::is_oop_or_null(obj, true)) {
 571       char buf[O_BUFLEN];
 572       tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj));
 573     } else {
 574       tty->print(INTPTR_FORMAT, p2i(obj));
 575     }
 576   } else {
 577     ResourceMark rm;
 578     assert(obj != nullptr && java_lang_String::is_instance(obj), "must be");
 579     char *buf = java_lang_String::as_utf8_string(obj);
 580     tty->print_raw(buf);
 581   }
 582   if (newline) {
 583     tty->cr();
 584   }
 585 JRT_END
 586 
 587 #if INCLUDE_G1GC
 588 
 589 void JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj) {
 590   G1BarrierSetRuntime::write_ref_field_pre_entry(obj, thread);
 591 }
 592 
 593 void JVMCIRuntime::write_barrier_post(JavaThread* thread, volatile CardValue* card_addr) {
 594   G1BarrierSetRuntime::write_ref_field_post_entry(card_addr, thread);
 595 }
 596 
 597 #endif // INCLUDE_G1GC
 598 
 599 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child))
 600   bool ret = true;
 601   if(!Universe::heap()->is_in(parent)) {
 602     tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent));
 603     parent->print();
 604     ret=false;
 605   }
 606   if(!Universe::heap()->is_in(child)) {
 607     tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child));
 608     child->print();
 609     ret=false;
 610   }
 611   return (jint)ret;
 612 JRT_END
 613 
 614 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* current, jlong where, jlong format, jlong value))
 615   ResourceMark rm(current);
 616   const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where;
 617   char *detail_msg = nullptr;
 618   if (format != 0L) {
 619     const char* buf = (char*) (address) format;
 620     size_t detail_msg_length = strlen(buf) * 2;
 621     detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length);
 622     jio_snprintf(detail_msg, detail_msg_length, buf, value);
 623   }
 624   report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg);
 625 JRT_END
 626 
 627 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread))
 628   oop exception = thread->exception_oop();
 629   assert(exception != nullptr, "npe");
 630   thread->set_exception_oop(nullptr);
 631   thread->set_exception_pc(0);
 632   return exception;
 633 JRT_END
 634 
 635 PRAGMA_DIAG_PUSH
 636 PRAGMA_FORMAT_NONLITERAL_IGNORED
 637 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3))
 638   ResourceMark rm;
 639   tty->print(format, v1, v2, v3);
 640 JRT_END
 641 PRAGMA_DIAG_POP
 642 
 643 static void decipher(jlong v, bool ignoreZero) {
 644   if (v != 0 || !ignoreZero) {
 645     void* p = (void *)(address) v;
 646     CodeBlob* cb = CodeCache::find_blob(p);
 647     if (cb) {
 648       if (cb->is_nmethod()) {
 649         char buf[O_BUFLEN];
 650         tty->print("%s [" INTPTR_FORMAT "+" JLONG_FORMAT "]", cb->as_nmethod()->method()->name_and_sig_as_C_string(buf, O_BUFLEN), p2i(cb->code_begin()), (jlong)((address)v - cb->code_begin()));
 651         return;
 652       }
 653       cb->print_value_on(tty);
 654       return;
 655     }
 656     if (Universe::heap()->is_in(p)) {
 657       oop obj = cast_to_oop(p);
 658       obj->print_value_on(tty);
 659       return;
 660     }
 661     tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v);
 662   }
 663 }
 664 
 665 PRAGMA_DIAG_PUSH
 666 PRAGMA_FORMAT_NONLITERAL_IGNORED
 667 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3))
 668   ResourceMark rm;
 669   const char *buf = (const char*) (address) format;
 670   if (vmError) {
 671     if (buf != nullptr) {
 672       fatal(buf, v1, v2, v3);
 673     } else {
 674       fatal("<anonymous error>");
 675     }
 676   } else if (buf != nullptr) {
 677     tty->print(buf, v1, v2, v3);
 678   } else {
 679     assert(v2 == 0, "v2 != 0");
 680     assert(v3 == 0, "v3 != 0");
 681     decipher(v1, false);
 682   }
 683 JRT_END
 684 PRAGMA_DIAG_POP
 685 
 686 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline))
 687   union {
 688       jlong l;
 689       jdouble d;
 690       jfloat f;
 691   } uu;
 692   uu.l = value;
 693   switch (typeChar) {
 694     case 'Z': tty->print(value == 0 ? "false" : "true"); break;
 695     case 'B': tty->print("%d", (jbyte) value); break;
 696     case 'C': tty->print("%c", (jchar) value); break;
 697     case 'S': tty->print("%d", (jshort) value); break;
 698     case 'I': tty->print("%d", (jint) value); break;
 699     case 'F': tty->print("%f", uu.f); break;
 700     case 'J': tty->print(JLONG_FORMAT, value); break;
 701     case 'D': tty->print("%lf", uu.d); break;
 702     default: assert(false, "unknown typeChar"); break;
 703   }
 704   if (newline) {
 705     tty->cr();
 706   }
 707 JRT_END
 708 
 709 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* current, oopDesc* obj))
 710   return (jint) obj->identity_hash();
 711 JRT_END
 712 
 713 JRT_ENTRY(jint, JVMCIRuntime::test_deoptimize_call_int(JavaThread* current, int value))
 714   deopt_caller();
 715   return (jint) value;
 716 JRT_END
 717 
 718 
 719 // Implementation of JVMCI.initializeRuntime()
 720 // When called from libjvmci, `libjvmciOrHotspotEnv` is a libjvmci env so use JVM_ENTRY_NO_ENV.
 721 JVM_ENTRY_NO_ENV(jobject, JVM_GetJVMCIRuntime(JNIEnv *libjvmciOrHotspotEnv, jclass c))
 722   JVMCIENV_FROM_JNI(thread, libjvmciOrHotspotEnv);
 723   if (!EnableJVMCI) {
 724     JVMCI_THROW_MSG_NULL(InternalError, "JVMCI is not enabled");
 725   }
 726   JVMCIENV->runtime()->initialize_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
 727   JVMCIObject runtime = JVMCIENV->runtime()->get_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
 728   return JVMCIENV->get_jobject(runtime);
 729 JVM_END
 730 
 731 // Implementation of Services.readSystemPropertiesInfo(int[] offsets)
 732 // When called from libjvmci, `env` is a libjvmci env so use JVM_ENTRY_NO_ENV.
 733 JVM_ENTRY_NO_ENV(jlong, JVM_ReadSystemPropertiesInfo(JNIEnv *env, jclass c, jintArray offsets_handle))
 734   JVMCIENV_FROM_JNI(thread, env);
 735   if (!EnableJVMCI) {
 736     JVMCI_THROW_MSG_0(InternalError, "JVMCI is not enabled");
 737   }
 738   JVMCIPrimitiveArray offsets = JVMCIENV->wrap(offsets_handle);
 739   JVMCIENV->put_int_at(offsets, 0, SystemProperty::next_offset_in_bytes());
 740   JVMCIENV->put_int_at(offsets, 1, SystemProperty::key_offset_in_bytes());
 741   JVMCIENV->put_int_at(offsets, 2, PathString::value_offset_in_bytes());
 742 
 743   return (jlong) Arguments::system_properties();
 744 JVM_END
 745 
 746 
 747 void JVMCIRuntime::call_getCompiler(TRAPS) {
 748   JVMCIENV_FROM_THREAD(THREAD);
 749   JVMCIENV->check_init(CHECK);
 750   JVMCIObject jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_CHECK);
 751   initialize(JVMCI_CHECK);
 752   JVMCIENV->call_HotSpotJVMCIRuntime_getCompiler(jvmciRuntime, JVMCI_CHECK);
 753 }
 754 
 755 void JVMCINMethodData::initialize(int nmethod_mirror_index,
 756                                   int nmethod_entry_patch_offset,
 757                                   const char* nmethod_mirror_name,
 758                                   FailedSpeculation** failed_speculations)
 759 {
 760   _failed_speculations = failed_speculations;
 761   _nmethod_mirror_index = nmethod_mirror_index;
 762   guarantee(nmethod_entry_patch_offset != -1, "missing entry barrier");
 763   _nmethod_entry_patch_offset = nmethod_entry_patch_offset;
 764   if (nmethod_mirror_name != nullptr) {
 765     _has_name = true;
 766     char* dest = (char*) name();
 767     strcpy(dest, nmethod_mirror_name);
 768   } else {
 769     _has_name = false;
 770   }
 771 }
 772 
 773 void JVMCINMethodData::copy(JVMCINMethodData* data) {
 774   initialize(data->_nmethod_mirror_index, data->_nmethod_entry_patch_offset, data->name(), data->_failed_speculations);
 775 }
 776 
 777 void JVMCINMethodData::add_failed_speculation(nmethod* nm, jlong speculation) {
 778   jlong index = speculation >> JVMCINMethodData::SPECULATION_LENGTH_BITS;
 779   guarantee(index >= 0 && index <= max_jint, "Encoded JVMCI speculation index is not a positive Java int: " INTPTR_FORMAT, index);
 780   int length = speculation & JVMCINMethodData::SPECULATION_LENGTH_MASK;
 781   if (index + length > (uint) nm->speculations_size()) {
 782     fatal(INTPTR_FORMAT "[index: " JLONG_FORMAT ", length: %d out of bounds wrt encoded speculations of length %u", speculation, index, length, nm->speculations_size());
 783   }
 784   address data = nm->speculations_begin() + index;
 785   FailedSpeculation::add_failed_speculation(nm, _failed_speculations, data, length);
 786 }
 787 
 788 oop JVMCINMethodData::get_nmethod_mirror(nmethod* nm, bool phantom_ref) {
 789   if (_nmethod_mirror_index == -1) {
 790     return nullptr;
 791   }
 792   if (phantom_ref) {
 793     return nm->oop_at_phantom(_nmethod_mirror_index);
 794   } else {
 795     return nm->oop_at(_nmethod_mirror_index);
 796   }
 797 }
 798 
 799 void JVMCINMethodData::set_nmethod_mirror(nmethod* nm, oop new_mirror) {
 800   guarantee(_nmethod_mirror_index != -1, "cannot set JVMCI mirror for nmethod");
 801   oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
 802   guarantee(new_mirror != nullptr, "use clear_nmethod_mirror to clear the mirror");
 803   guarantee(*addr == nullptr, "cannot overwrite non-null mirror");
 804 
 805   *addr = new_mirror;
 806 
 807   // Since we've patched some oops in the nmethod,
 808   // (re)register it with the heap.
 809   MutexLocker ml(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 810   Universe::heap()->register_nmethod(nm);
 811 }
 812 
 813 void JVMCINMethodData::invalidate_nmethod_mirror(nmethod* nm) {
 814   oop nmethod_mirror = get_nmethod_mirror(nm, /* phantom_ref */ false);
 815   if (nmethod_mirror == nullptr) {
 816     return;
 817   }
 818 
 819   // Update the values in the mirror if it still refers to nm.
 820   // We cannot use JVMCIObject to wrap the mirror as this is called
 821   // during GC, forbidding the creation of JNIHandles.
 822   JVMCIEnv* jvmciEnv = nullptr;
 823   nmethod* current = (nmethod*) HotSpotJVMCI::InstalledCode::address(jvmciEnv, nmethod_mirror);
 824   if (nm == current) {
 825     if (nm->is_unloading()) {
 826       // Break the link from the mirror to nm such that
 827       // future invocations via the mirror will result in
 828       // an InvalidInstalledCodeException.
 829       HotSpotJVMCI::InstalledCode::set_address(jvmciEnv, nmethod_mirror, 0);
 830       HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
 831       HotSpotJVMCI::HotSpotInstalledCode::set_codeStart(jvmciEnv, nmethod_mirror, 0);
 832     } else if (nm->is_not_entrant()) {
 833       // Zero the entry point so any new invocation will fail but keep
 834       // the address link around that so that existing activations can
 835       // be deoptimized via the mirror (i.e. JVMCIEnv::invalidate_installed_code).
 836       HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
 837       HotSpotJVMCI::HotSpotInstalledCode::set_codeStart(jvmciEnv, nmethod_mirror, 0);
 838     }
 839   }
 840 
 841   if (_nmethod_mirror_index != -1 && nm->is_unloading()) {
 842     // Drop the reference to the nmethod mirror object but don't clear the actual oop reference.  Otherwise
 843     // it would appear that the nmethod didn't need to be unloaded in the first place.
 844     _nmethod_mirror_index = -1;
 845   }
 846 }
 847 
 848 // Handles to objects in the Hotspot heap.
 849 static OopStorage* object_handles() {
 850   return Universe::vm_global();
 851 }
 852 
 853 jlong JVMCIRuntime::make_oop_handle(const Handle& obj) {
 854   assert(!Universe::heap()->is_stw_gc_active(), "can't extend the root set during GC pause");
 855   assert(oopDesc::is_oop(obj()), "not an oop");
 856 
 857   oop* ptr = OopHandle(object_handles(), obj()).ptr_raw();
 858   MutexLocker ml(_lock);
 859   _oop_handles.append(ptr);
 860   return reinterpret_cast<jlong>(ptr);
 861 }
 862 
 863 #ifdef ASSERT
 864 bool JVMCIRuntime::is_oop_handle(jlong handle) {
 865   const oop* ptr = (oop*) handle;
 866   return object_handles()->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
 867 }
 868 #endif
 869 
 870 int JVMCIRuntime::release_and_clear_oop_handles() {
 871   guarantee(_num_attached_threads == cannot_be_attached, "only call during JVMCI runtime shutdown");
 872   int released = release_cleared_oop_handles();
 873   if (_oop_handles.length() != 0) {
 874     for (int i = 0; i < _oop_handles.length(); i++) {
 875       oop* oop_ptr = _oop_handles.at(i);
 876       guarantee(oop_ptr != nullptr, "release_cleared_oop_handles left null entry in _oop_handles");
 877       guarantee(NativeAccess<>::oop_load(oop_ptr) != nullptr, "unexpected cleared handle");
 878       // Satisfy OopHandles::release precondition that all
 879       // handles being released are null.
 880       NativeAccess<>::oop_store(oop_ptr, (oop) nullptr);
 881     }
 882 
 883     // Do the bulk release
 884     object_handles()->release(_oop_handles.adr_at(0), _oop_handles.length());
 885     released += _oop_handles.length();
 886   }
 887   _oop_handles.clear();
 888   return released;
 889 }
 890 
 891 static bool is_referent_non_null(oop* handle) {
 892   return handle != nullptr && NativeAccess<>::oop_load(handle) != nullptr;
 893 }
 894 
 895 // Swaps the elements in `array` at index `a` and index `b`
 896 static void swap(GrowableArray<oop*>* array, int a, int b) {
 897   oop* tmp = array->at(a);
 898   array->at_put(a, array->at(b));
 899   array->at_put(b, tmp);
 900 }
 901 
 902 int JVMCIRuntime::release_cleared_oop_handles() {
 903   // Despite this lock, it's possible for another thread
 904   // to clear a handle's referent concurrently (e.g., a thread
 905   // executing IndirectHotSpotObjectConstantImpl.clear()).
 906   // This is benign - it means there can still be cleared
 907   // handles in _oop_handles when this method returns.
 908   MutexLocker ml(_lock);
 909 
 910   int next = 0;
 911   if (_oop_handles.length() != 0) {
 912     // Key for _oop_handles contents in example below:
 913     //   H: handle with non-null referent
 914     //   h: handle with clear (i.e., null) referent
 915     //   -: null entry
 916 
 917     // Shuffle all handles with non-null referents to the front of the list
 918     // Example: Before: 0HHh-Hh-
 919     //           After: HHHh--h-
 920     for (int i = 0; i < _oop_handles.length(); i++) {
 921       oop* handle = _oop_handles.at(i);
 922       if (is_referent_non_null(handle)) {
 923         if (i != next && !is_referent_non_null(_oop_handles.at(next))) {
 924           // Swap elements at index `next` and `i`
 925           swap(&_oop_handles, next, i);
 926         }
 927         next++;
 928       }
 929     }
 930 
 931     // `next` is now the index of the first null handle or handle with a null referent
 932     int num_alive = next;
 933 
 934     // Shuffle all null handles to the end of the list
 935     // Example: Before: HHHh--h-
 936     //           After: HHHhh---
 937     //       num_alive: 3
 938     for (int i = next; i < _oop_handles.length(); i++) {
 939       oop* handle = _oop_handles.at(i);
 940       if (handle != nullptr) {
 941         if (i != next && _oop_handles.at(next) == nullptr) {
 942           // Swap elements at index `next` and `i`
 943           swap(&_oop_handles, next, i);
 944         }
 945         next++;
 946       }
 947     }
 948     if (next != num_alive) {
 949       int to_release = next - num_alive;
 950 
 951       // `next` is now the index of the first null handle
 952       // Example: to_release: 2
 953 
 954       // Bulk release the handles with a null referent
 955       object_handles()->release(_oop_handles.adr_at(num_alive), to_release);
 956 
 957       // Truncate oop handles to only those with a non-null referent
 958       JVMCI_event_2("compacted oop handles in JVMCI runtime %d from %d to %d", _id, _oop_handles.length(), num_alive);
 959       _oop_handles.trunc_to(num_alive);
 960       // Example: HHH
 961 
 962       return to_release;
 963     }
 964   }
 965   return 0;
 966 }
 967 
 968 jmetadata JVMCIRuntime::allocate_handle(const methodHandle& handle) {
 969   MutexLocker ml(_lock);
 970   return _metadata_handles->allocate_handle(handle);
 971 }
 972 
 973 jmetadata JVMCIRuntime::allocate_handle(const constantPoolHandle& handle) {
 974   MutexLocker ml(_lock);
 975   return _metadata_handles->allocate_handle(handle);
 976 }
 977 
 978 void JVMCIRuntime::release_handle(jmetadata handle) {
 979   MutexLocker ml(_lock);
 980   _metadata_handles->chain_free_list(handle);
 981 }
 982 
 983 // Function for redirecting shared library JavaVM output to tty
 984 static void _log(const char* buf, size_t count) {
 985   tty->write((char*) buf, count);
 986 }
 987 
 988 // Function for redirecting shared library JavaVM fatal error data to a log file.
 989 // The log file is opened on first call to this function.
 990 static void _fatal_log(const char* buf, size_t count) {
 991   JVMCI::fatal_log(buf, count);
 992 }
 993 
 994 // Function for shared library JavaVM to flush tty
 995 static void _flush_log() {
 996   tty->flush();
 997 }
 998 
 999 // Function for shared library JavaVM to exit HotSpot on a fatal error
1000 static void _fatal() {
1001   Thread* thread = Thread::current_or_null_safe();
1002   if (thread != nullptr && thread->is_Java_thread()) {
1003     JavaThread* jthread = JavaThread::cast(thread);
1004     JVMCIRuntime* runtime = jthread->libjvmci_runtime();
1005     if (runtime != nullptr) {
1006       int javaVM_id = runtime->get_shared_library_javavm_id();
1007       fatal("Fatal error in JVMCI shared library JavaVM[%d] owned by JVMCI runtime %d", javaVM_id, runtime->id());
1008     }
1009   }
1010   intx current_thread_id = os::current_thread_id();
1011   fatal("thread " INTX_FORMAT ": Fatal error in JVMCI shared library", current_thread_id);
1012 }
1013 
1014 JVMCIRuntime::JVMCIRuntime(JVMCIRuntime* next, int id, bool for_compile_broker) :
1015   _init_state(uninitialized),
1016   _shared_library_javavm(nullptr),
1017   _shared_library_javavm_id(0),
1018   _id(id),
1019   _next(next),
1020   _metadata_handles(new MetadataHandles()),
1021   _oop_handles(100, mtJVMCI),
1022   _num_attached_threads(0),
1023   _for_compile_broker(for_compile_broker)
1024 {
1025   if (id == -1) {
1026     _lock = JVMCIRuntime_lock;
1027   } else {
1028     stringStream lock_name;
1029     lock_name.print("%s@%d", JVMCIRuntime_lock->name(), id);
1030     Mutex::Rank lock_rank = DEBUG_ONLY(JVMCIRuntime_lock->rank()) NOT_DEBUG(Mutex::safepoint);
1031     _lock = new PaddedMonitor(lock_rank, lock_name.as_string(/*c_heap*/true));
1032   }
1033   JVMCI_event_1("created new %s JVMCI runtime %d (" PTR_FORMAT ")",
1034       id == -1 ? "Java" : for_compile_broker ? "CompileBroker" : "Compiler", id, p2i(this));
1035 }
1036 
1037 JVMCIRuntime* JVMCIRuntime::select_runtime_in_shutdown(JavaThread* thread) {
1038   assert(JVMCI_lock->owner() == thread, "must be");
1039   // When shutting down, use the first available runtime.
1040   for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1041     if (runtime->_num_attached_threads != cannot_be_attached) {
1042       runtime->pre_attach_thread(thread);
1043       JVMCI_event_1("using pre-existing JVMCI runtime %d in shutdown", runtime->id());
1044       return runtime;
1045     }
1046   }
1047   // Lazily initialize JVMCI::_shutdown_compiler_runtime. Safe to
1048   // do here since JVMCI_lock is locked.
1049   if (JVMCI::_shutdown_compiler_runtime == nullptr) {
1050     JVMCI::_shutdown_compiler_runtime = new JVMCIRuntime(nullptr, -2, true);
1051   }
1052   JVMCIRuntime* runtime = JVMCI::_shutdown_compiler_runtime;
1053   JVMCI_event_1("using reserved shutdown JVMCI runtime %d", runtime->id());
1054   return runtime;
1055 }
1056 
1057 JVMCIRuntime* JVMCIRuntime::select_runtime(JavaThread* thread, JVMCIRuntime* skip, int* count) {
1058   assert(JVMCI_lock->owner() == thread, "must be");
1059   bool for_compile_broker = thread->is_Compiler_thread();
1060   for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1061     if (count != nullptr) {
1062       (*count)++;
1063     }
1064     if (for_compile_broker == runtime->_for_compile_broker) {
1065       int count = runtime->_num_attached_threads;
1066       if (count == cannot_be_attached || runtime == skip) {
1067         // Cannot attach to rt
1068         continue;
1069       }
1070       // If selecting for repacking, ignore a runtime without an existing JavaVM
1071       if (skip != nullptr && !runtime->has_shared_library_javavm()) {
1072         continue;
1073       }
1074 
1075       // Select first runtime with sufficient capacity
1076       if (count < (int) JVMCIThreadsPerNativeLibraryRuntime) {
1077         runtime->pre_attach_thread(thread);
1078         return runtime;
1079       }
1080     }
1081   }
1082   return nullptr;
1083 }
1084 
1085 JVMCIRuntime* JVMCIRuntime::select_or_create_runtime(JavaThread* thread) {
1086   assert(JVMCI_lock->owner() == thread, "must be");
1087   int id = 0;
1088   JVMCIRuntime* runtime;
1089   if (JVMCI::using_singleton_shared_library_runtime()) {
1090     runtime = JVMCI::_compiler_runtimes;
1091     guarantee(runtime != nullptr, "must be");
1092     while (runtime->_num_attached_threads == cannot_be_attached) {
1093       // Since there is only a singleton JVMCIRuntime, we
1094       // need to wait for it to be available for attaching.
1095       JVMCI_lock->wait();
1096     }
1097     runtime->pre_attach_thread(thread);
1098   } else {
1099     runtime = select_runtime(thread, nullptr, &id);
1100   }
1101   if (runtime == nullptr) {
1102     runtime = new JVMCIRuntime(JVMCI::_compiler_runtimes, id, thread->is_Compiler_thread());
1103     JVMCI::_compiler_runtimes = runtime;
1104     runtime->pre_attach_thread(thread);
1105   }
1106   return runtime;
1107 }
1108 
1109 JVMCIRuntime* JVMCIRuntime::for_thread(JavaThread* thread) {
1110   assert(thread->libjvmci_runtime() == nullptr, "must be");
1111   // Find the runtime with fewest attached threads
1112   JVMCIRuntime* runtime = nullptr;
1113   {
1114     MutexLocker locker(JVMCI_lock);
1115     runtime = JVMCI::in_shutdown() ? select_runtime_in_shutdown(thread) : select_or_create_runtime(thread);
1116   }
1117   runtime->attach_thread(thread);
1118   return runtime;
1119 }
1120 
1121 const char* JVMCIRuntime::attach_shared_library_thread(JavaThread* thread, JavaVM* javaVM) {
1122   MutexLocker locker(JVMCI_lock);
1123   for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1124     if (runtime->_shared_library_javavm == javaVM) {
1125       if (runtime->_num_attached_threads == cannot_be_attached) {
1126         return "Cannot attach to JVMCI runtime that is shutting down";
1127       }
1128       runtime->pre_attach_thread(thread);
1129       runtime->attach_thread(thread);
1130       return nullptr;
1131     }
1132   }
1133   return "Cannot find JVMCI runtime";
1134 }
1135 
1136 void JVMCIRuntime::pre_attach_thread(JavaThread* thread) {
1137   assert(JVMCI_lock->owner() == thread, "must be");
1138   _num_attached_threads++;
1139 }
1140 
1141 void JVMCIRuntime::attach_thread(JavaThread* thread) {
1142   assert(thread->libjvmci_runtime() == nullptr, "must be");
1143   thread->set_libjvmci_runtime(this);
1144   guarantee(this == JVMCI::_shutdown_compiler_runtime ||
1145             _num_attached_threads > 0,
1146             "missing reservation in JVMCI runtime %d: _num_attached_threads=%d", _id, _num_attached_threads);
1147   JVMCI_event_1("attached to JVMCI runtime %d%s", _id, JVMCI::in_shutdown() ? " [in JVMCI shutdown]" : "");
1148 }
1149 
1150 void JVMCIRuntime::repack(JavaThread* thread) {
1151   JVMCIRuntime* new_runtime = nullptr;
1152   {
1153     MutexLocker locker(JVMCI_lock);
1154     if (JVMCI::using_singleton_shared_library_runtime() || _num_attached_threads != 1 || JVMCI::in_shutdown()) {
1155       return;
1156     }
1157     new_runtime = select_runtime(thread, this, nullptr);
1158   }
1159   if (new_runtime != nullptr) {
1160     JVMCI_event_1("Moving thread from JVMCI runtime %d to JVMCI runtime %d (%d attached)", _id, new_runtime->_id, new_runtime->_num_attached_threads - 1);
1161     detach_thread(thread, "moving thread to another JVMCI runtime");
1162     new_runtime->attach_thread(thread);
1163   }
1164 }
1165 
1166 bool JVMCIRuntime::detach_thread(JavaThread* thread, const char* reason, bool can_destroy_javavm) {
1167   if (this == JVMCI::_shutdown_compiler_runtime || JVMCI::in_shutdown()) {
1168     // Do minimal work when shutting down JVMCI
1169     thread->set_libjvmci_runtime(nullptr);
1170     return false;
1171   }
1172   bool should_shutdown;
1173   bool destroyed_javavm = false;
1174   {
1175     MutexLocker locker(JVMCI_lock);
1176     _num_attached_threads--;
1177     JVMCI_event_1("detaching from JVMCI runtime %d: %s (%d other threads still attached)", _id, reason, _num_attached_threads);
1178     should_shutdown = _num_attached_threads == 0 && !JVMCI::in_shutdown();
1179     if (should_shutdown && !can_destroy_javavm) {
1180       // If it's not possible to destroy the JavaVM on this thread then the VM must
1181       // not be shutdown. This can happen when a shared library thread is the last
1182       // thread to detach from a shared library JavaVM (e.g. GraalServiceThread).
1183       JVMCI_event_1("Cancelled shut down of JVMCI runtime %d", _id);
1184       should_shutdown = false;
1185     }
1186     if (should_shutdown) {
1187       // Prevent other threads from attaching to this runtime
1188       // while it is shutting down and destroying its JavaVM
1189       _num_attached_threads = cannot_be_attached;
1190     }
1191   }
1192   if (should_shutdown) {
1193     // Release the JavaVM resources associated with this
1194     // runtime once there are no threads attached to it.
1195     shutdown();
1196     if (can_destroy_javavm) {
1197       destroyed_javavm = destroy_shared_library_javavm();
1198       if (destroyed_javavm) {
1199         // Can release all handles now that there's no code executing
1200         // that could be using them. Handles for the Java JVMCI runtime
1201         // are never released as we cannot guarantee all compiler threads
1202         // using it have been stopped.
1203         int released = release_and_clear_oop_handles();
1204         JVMCI_event_1("releasing handles for JVMCI runtime %d: oop handles=%d, metadata handles={total=%d, live=%d, blocks=%d}",
1205             _id,
1206             released,
1207             _metadata_handles->num_handles(),
1208             _metadata_handles->num_handles() - _metadata_handles->num_free_handles(),
1209             _metadata_handles->num_blocks());
1210 
1211         // No need to acquire _lock since this is the only thread accessing this runtime
1212         _metadata_handles->clear();
1213       }
1214     }
1215     // Allow other threads to attach to this runtime now
1216     MutexLocker locker(JVMCI_lock);
1217     _num_attached_threads = 0;
1218     if (JVMCI::using_singleton_shared_library_runtime()) {
1219       // Notify any thread waiting to attach to the
1220       // singleton JVMCIRuntime
1221       JVMCI_lock->notify();
1222     }
1223   }
1224   thread->set_libjvmci_runtime(nullptr);
1225   JVMCI_event_1("detached from JVMCI runtime %d", _id);
1226   return destroyed_javavm;
1227 }
1228 
1229 JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err, const char** err_msg) {
1230   MutexLocker locker(_lock);
1231   JavaVM* javaVM = _shared_library_javavm;
1232   if (javaVM == nullptr) {
1233 #ifdef ASSERT
1234     const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceEnomemOnLibjvmciInit");
1235     if (val != nullptr && strcmp(val, "true") == 0) {
1236       *create_JavaVM_err = JNI_ENOMEM;
1237       return nullptr;
1238     }
1239 #endif
1240 
1241     char* sl_path;
1242     void* sl_handle = JVMCI::get_shared_library(sl_path, true);
1243 
1244     jint (*JNI_CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
1245     typedef jint (*JNI_CreateJavaVM_t)(JavaVM **pvm, void **penv, void *args);
1246 
1247     JNI_CreateJavaVM = CAST_TO_FN_PTR(JNI_CreateJavaVM_t, os::dll_lookup(sl_handle, "JNI_CreateJavaVM"));
1248     if (JNI_CreateJavaVM == nullptr) {
1249       fatal("Unable to find JNI_CreateJavaVM in %s", sl_path);
1250     }
1251 
1252     ResourceMark rm;
1253     JavaVMInitArgs vm_args;
1254     vm_args.version = JNI_VERSION_1_2;
1255     vm_args.ignoreUnrecognized = JNI_TRUE;
1256     JavaVMOption options[6];
1257     jlong javaVM_id = 0;
1258 
1259     // Protocol: JVMCI shared library JavaVM should support a non-standard "_javavm_id"
1260     // option whose extraInfo info field is a pointer to which a unique id for the
1261     // JavaVM should be written.
1262     options[0].optionString = (char*) "_javavm_id";
1263     options[0].extraInfo = &javaVM_id;
1264 
1265     options[1].optionString = (char*) "_log";
1266     options[1].extraInfo = (void*) _log;
1267     options[2].optionString = (char*) "_flush_log";
1268     options[2].extraInfo = (void*) _flush_log;
1269     options[3].optionString = (char*) "_fatal";
1270     options[3].extraInfo = (void*) _fatal;
1271     options[4].optionString = (char*) "_fatal_log";
1272     options[4].extraInfo = (void*) _fatal_log;
1273     options[5].optionString = (char*) "_createvm_errorstr";
1274     options[5].extraInfo = (void*) err_msg;
1275 
1276     vm_args.version = JNI_VERSION_1_2;
1277     vm_args.options = options;
1278     vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
1279 
1280     JNIEnv* env = nullptr;
1281     int result = (*JNI_CreateJavaVM)(&javaVM, (void**) &env, &vm_args);
1282     if (result == JNI_OK) {
1283       guarantee(env != nullptr, "missing env");
1284       _shared_library_javavm_id = javaVM_id;
1285       _shared_library_javavm = javaVM;
1286       JVMCI_event_1("created JavaVM[%ld]@" PTR_FORMAT " for JVMCI runtime %d", javaVM_id, p2i(javaVM), _id);
1287       return env;
1288     } else {
1289       *create_JavaVM_err = result;
1290     }
1291   }
1292   return nullptr;
1293 }
1294 
1295 void JVMCIRuntime::init_JavaVM_info(jlongArray info, JVMCI_TRAPS) {
1296   if (info != nullptr) {
1297     typeArrayOop info_oop = (typeArrayOop) JNIHandles::resolve(info);
1298     if (info_oop->length() < 4) {
1299       JVMCI_THROW_MSG(ArrayIndexOutOfBoundsException, err_msg("%d < 4", info_oop->length()));
1300     }
1301     JavaVM* javaVM = _shared_library_javavm;
1302     info_oop->long_at_put(0, (jlong) (address) javaVM);
1303     info_oop->long_at_put(1, (jlong) (address) javaVM->functions->reserved0);
1304     info_oop->long_at_put(2, (jlong) (address) javaVM->functions->reserved1);
1305     info_oop->long_at_put(3, (jlong) (address) javaVM->functions->reserved2);
1306   }
1307 }
1308 
1309 #define JAVAVM_CALL_BLOCK                                             \
1310   guarantee(thread != nullptr && _shared_library_javavm != nullptr, "npe"); \
1311   ThreadToNativeFromVM ttnfv(thread);                                 \
1312   JavaVM* javavm = _shared_library_javavm;
1313 
1314 jint JVMCIRuntime::AttachCurrentThread(JavaThread* thread, void **penv, void *args) {
1315   JAVAVM_CALL_BLOCK
1316   return javavm->AttachCurrentThread(penv, args);
1317 }
1318 
1319 jint JVMCIRuntime::AttachCurrentThreadAsDaemon(JavaThread* thread, void **penv, void *args) {
1320   JAVAVM_CALL_BLOCK
1321   return javavm->AttachCurrentThreadAsDaemon(penv, args);
1322 }
1323 
1324 jint JVMCIRuntime::DetachCurrentThread(JavaThread* thread) {
1325   JAVAVM_CALL_BLOCK
1326   return javavm->DetachCurrentThread();
1327 }
1328 
1329 jint JVMCIRuntime::GetEnv(JavaThread* thread, void **penv, jint version) {
1330   JAVAVM_CALL_BLOCK
1331   return javavm->GetEnv(penv, version);
1332 }
1333 #undef JAVAVM_CALL_BLOCK                                             \
1334 
1335 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
1336   if (is_HotSpotJVMCIRuntime_initialized()) {
1337     if (JVMCIENV->is_hotspot() && UseJVMCINativeLibrary) {
1338       JVMCI_THROW_MSG(InternalError, "JVMCI has already been enabled in the JVMCI shared library");
1339     }
1340   }
1341 
1342   initialize(JVMCI_CHECK);
1343 
1344   // This should only be called in the context of the JVMCI class being initialized
1345   JVMCIObject result = JVMCIENV->call_HotSpotJVMCIRuntime_runtime(JVMCI_CHECK);
1346   result = JVMCIENV->make_global(result);
1347 
1348   OrderAccess::storestore();  // Ensure handle is fully constructed before publishing
1349   _HotSpotJVMCIRuntime_instance = result;
1350 
1351   JVMCI::_is_initialized = true;
1352 }
1353 
1354 JVMCIRuntime::InitState JVMCIRuntime::_shared_library_javavm_refs_init_state = JVMCIRuntime::uninitialized;
1355 JVMCIRuntime::InitState JVMCIRuntime::_hotspot_javavm_refs_init_state = JVMCIRuntime::uninitialized;
1356 
1357 class JavaVMRefsInitialization: public StackObj {
1358   JVMCIRuntime::InitState *_state;
1359   int _id;
1360  public:
1361   JavaVMRefsInitialization(JVMCIRuntime::InitState *state, int id) {
1362     _state = state;
1363     _id = id;
1364     // All classes, methods and fields in the JVMCI shared library
1365     // are in the read-only part of the image. As such, these
1366     // values (and any global handle derived from them via NewGlobalRef)
1367     // are the same for all JavaVM instances created in the
1368     // shared library which means they only need to be initialized
1369     // once. In non-product mode, we check this invariant.
1370     // See com.oracle.svm.jni.JNIImageHeapHandles.
1371     // The same is true for Klass* and field offsets in HotSpotJVMCI.
1372     if (*state == JVMCIRuntime::uninitialized DEBUG_ONLY( || true)) {
1373       *state = JVMCIRuntime::being_initialized;
1374       JVMCI_event_1("initializing JavaVM references in JVMCI runtime %d", id);
1375     } else {
1376       while (*state != JVMCIRuntime::fully_initialized) {
1377         JVMCI_event_1("waiting for JavaVM references initialization in JVMCI runtime %d", id);
1378         JVMCI_lock->wait();
1379       }
1380       JVMCI_event_1("done waiting for JavaVM references initialization in JVMCI runtime %d", id);
1381     }
1382   }
1383 
1384   ~JavaVMRefsInitialization() {
1385     if (*_state == JVMCIRuntime::being_initialized) {
1386       *_state = JVMCIRuntime::fully_initialized;
1387       JVMCI_event_1("initialized JavaVM references in JVMCI runtime %d", _id);
1388       JVMCI_lock->notify_all();
1389     }
1390   }
1391 
1392   bool should_init() {
1393     return *_state == JVMCIRuntime::being_initialized;
1394   }
1395 };
1396 
1397 void JVMCIRuntime::initialize(JVMCI_TRAPS) {
1398   // Check first without _lock
1399   if (_init_state == fully_initialized) {
1400     return;
1401   }
1402 
1403   JavaThread* THREAD = JavaThread::current();
1404 
1405   MutexLocker locker(_lock);
1406   // Check again under _lock
1407   if (_init_state == fully_initialized) {
1408     return;
1409   }
1410 
1411   while (_init_state == being_initialized) {
1412     JVMCI_event_1("waiting for initialization of JVMCI runtime %d", _id);
1413     _lock->wait();
1414     if (_init_state == fully_initialized) {
1415       JVMCI_event_1("done waiting for initialization of JVMCI runtime %d", _id);
1416       return;
1417     }
1418   }
1419 
1420   JVMCI_event_1("initializing JVMCI runtime %d", _id);
1421   _init_state = being_initialized;
1422 
1423   {
1424     MutexUnlocker unlock(_lock);
1425 
1426     HandleMark hm(THREAD);
1427     ResourceMark rm(THREAD);
1428     {
1429       MutexLocker lock_jvmci(JVMCI_lock);
1430       if (JVMCIENV->is_hotspot()) {
1431         JavaVMRefsInitialization initialization(&_hotspot_javavm_refs_init_state, _id);
1432         if (initialization.should_init()) {
1433           MutexUnlocker unlock_jvmci(JVMCI_lock);
1434           HotSpotJVMCI::compute_offsets(CHECK_EXIT);
1435         }
1436       } else {
1437         JavaVMRefsInitialization initialization(&_shared_library_javavm_refs_init_state, _id);
1438         if (initialization.should_init()) {
1439           MutexUnlocker unlock_jvmci(JVMCI_lock);
1440           JNIAccessMark jni(JVMCIENV, THREAD);
1441 
1442           JNIJVMCI::initialize_ids(jni.env());
1443           if (jni()->ExceptionCheck()) {
1444             jni()->ExceptionDescribe();
1445             fatal("JNI exception during init");
1446           }
1447           // _lock is re-locked at this point
1448         }
1449       }
1450     }
1451 
1452     if (!JVMCIENV->is_hotspot()) {
1453       JNIAccessMark jni(JVMCIENV, THREAD);
1454       JNIJVMCI::register_natives(jni.env());
1455     }
1456     create_jvmci_primitive_type(T_BOOLEAN, JVMCI_CHECK_EXIT_((void)0));
1457     create_jvmci_primitive_type(T_BYTE, JVMCI_CHECK_EXIT_((void)0));
1458     create_jvmci_primitive_type(T_CHAR, JVMCI_CHECK_EXIT_((void)0));
1459     create_jvmci_primitive_type(T_SHORT, JVMCI_CHECK_EXIT_((void)0));
1460     create_jvmci_primitive_type(T_INT, JVMCI_CHECK_EXIT_((void)0));
1461     create_jvmci_primitive_type(T_LONG, JVMCI_CHECK_EXIT_((void)0));
1462     create_jvmci_primitive_type(T_FLOAT, JVMCI_CHECK_EXIT_((void)0));
1463     create_jvmci_primitive_type(T_DOUBLE, JVMCI_CHECK_EXIT_((void)0));
1464     create_jvmci_primitive_type(T_VOID, JVMCI_CHECK_EXIT_((void)0));
1465 
1466     DEBUG_ONLY(CodeInstaller::verify_bci_constants(JVMCIENV);)
1467   }
1468 
1469   _init_state = fully_initialized;
1470   JVMCI_event_1("initialized JVMCI runtime %d", _id);
1471   _lock->notify_all();
1472 }
1473 
1474 JVMCIObject JVMCIRuntime::create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS) {
1475   JavaThread* THREAD = JavaThread::current(); // For exception macros.
1476   // These primitive types are long lived and are created before the runtime is fully set up
1477   // so skip registering them for scanning.
1478   JVMCIObject mirror = JVMCIENV->get_object_constant(java_lang_Class::primitive_mirror(type), false, true);
1479   if (JVMCIENV->is_hotspot()) {
1480     JavaValue result(T_OBJECT);
1481     JavaCallArguments args;
1482     args.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(mirror)));
1483     args.push_int(type2char(type));
1484     JavaCalls::call_static(&result, HotSpotJVMCI::HotSpotResolvedPrimitiveType::klass(), vmSymbols::fromMetaspace_name(), vmSymbols::primitive_fromMetaspace_signature(), &args, CHECK_(JVMCIObject()));
1485 
1486     return JVMCIENV->wrap(JNIHandles::make_local(result.get_oop()));
1487   } else {
1488     JNIAccessMark jni(JVMCIENV);
1489     jobject result = jni()->CallStaticObjectMethod(JNIJVMCI::HotSpotResolvedPrimitiveType::clazz(),
1490                                            JNIJVMCI::HotSpotResolvedPrimitiveType_fromMetaspace_method(),
1491                                            mirror.as_jobject(), type2char(type));
1492     if (jni()->ExceptionCheck()) {
1493       return JVMCIObject();
1494     }
1495     return JVMCIENV->wrap(result);
1496   }
1497 }
1498 
1499 void JVMCIRuntime::initialize_JVMCI(JVMCI_TRAPS) {
1500   if (!is_HotSpotJVMCIRuntime_initialized()) {
1501     initialize(JVMCI_CHECK);
1502     JVMCIENV->call_JVMCI_getRuntime(JVMCI_CHECK);
1503     guarantee(_HotSpotJVMCIRuntime_instance.is_non_null(), "NPE in JVMCI runtime %d", _id);
1504   }
1505 }
1506 
1507 JVMCIObject JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
1508   initialize(JVMCI_CHECK_(JVMCIObject()));
1509   initialize_JVMCI(JVMCI_CHECK_(JVMCIObject()));
1510   return _HotSpotJVMCIRuntime_instance;
1511 }
1512 
1513 // Implementation of CompilerToVM.registerNatives()
1514 // When called from libjvmci, `libjvmciOrHotspotEnv` is a libjvmci env so use JVM_ENTRY_NO_ENV.
1515 JVM_ENTRY_NO_ENV(void, JVM_RegisterJVMCINatives(JNIEnv *libjvmciOrHotspotEnv, jclass c2vmClass))
1516   JVMCIENV_FROM_JNI(thread, libjvmciOrHotspotEnv);
1517 
1518   if (!EnableJVMCI) {
1519     JVMCI_THROW_MSG(InternalError, "JVMCI is not enabled");
1520   }
1521 
1522   JVMCIENV->runtime()->initialize(JVMCIENV);
1523 
1524   {
1525     ResourceMark rm(thread);
1526     HandleMark hm(thread);
1527     ThreadToNativeFromVM trans(thread);
1528 
1529     // Ensure _non_oop_bits is initialized
1530     Universe::non_oop_word();
1531     JNIEnv *env = libjvmciOrHotspotEnv;
1532     if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count())) {
1533       if (!env->ExceptionCheck()) {
1534         for (int i = 0; i < CompilerToVM::methods_count(); i++) {
1535           if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods + i, 1)) {
1536             guarantee(false, "Error registering JNI method %s%s", CompilerToVM::methods[i].name, CompilerToVM::methods[i].signature);
1537             break;
1538           }
1539         }
1540       } else {
1541         env->ExceptionDescribe();
1542       }
1543       guarantee(false, "Failed registering CompilerToVM native methods");
1544     }
1545   }
1546 JVM_END
1547 
1548 
1549 void JVMCIRuntime::shutdown() {
1550   if (_HotSpotJVMCIRuntime_instance.is_non_null()) {
1551     JVMCI_event_1("shutting down HotSpotJVMCIRuntime for JVMCI runtime %d", _id);
1552     JVMCIEnv __stack_jvmci_env__(JavaThread::current(), _HotSpotJVMCIRuntime_instance.is_hotspot(),__FILE__, __LINE__);
1553     JVMCIEnv* JVMCIENV = &__stack_jvmci_env__;
1554     if (JVMCIENV->init_error() == JNI_OK) {
1555       JVMCIENV->call_HotSpotJVMCIRuntime_shutdown(_HotSpotJVMCIRuntime_instance);
1556     } else {
1557       JVMCI_event_1("Error in JVMCIEnv for shutdown (err: %d)", JVMCIENV->init_error());
1558     }
1559     if (_num_attached_threads == cannot_be_attached) {
1560       // Only when no other threads are attached to this runtime
1561       // is it safe to reset these fields.
1562       _HotSpotJVMCIRuntime_instance = JVMCIObject();
1563       _init_state = uninitialized;
1564       JVMCI_event_1("shut down JVMCI runtime %d", _id);
1565     }
1566   }
1567 }
1568 
1569 bool JVMCIRuntime::destroy_shared_library_javavm() {
1570   guarantee(_num_attached_threads == cannot_be_attached,
1571       "cannot destroy JavaVM for JVMCI runtime %d with %d attached threads", _id, _num_attached_threads);
1572   JavaVM* javaVM;
1573   int javaVM_id = _shared_library_javavm_id;
1574   {
1575     // Exactly one thread can destroy the JavaVM
1576     // and release the handle to it.
1577     MutexLocker only_one(_lock);
1578     javaVM = _shared_library_javavm;
1579     if (javaVM != nullptr) {
1580       _shared_library_javavm = nullptr;
1581       _shared_library_javavm_id = 0;
1582     }
1583   }
1584   if (javaVM != nullptr) {
1585     int result;
1586     {
1587       // Must transition into native before calling into libjvmci
1588       ThreadToNativeFromVM ttnfv(JavaThread::current());
1589       result = javaVM->DestroyJavaVM();
1590     }
1591     if (result == JNI_OK) {
1592       JVMCI_event_1("destroyed JavaVM[%d]@" PTR_FORMAT " for JVMCI runtime %d", javaVM_id, p2i(javaVM), _id);
1593     } else {
1594       warning("Non-zero result (%d) when calling JNI_DestroyJavaVM on JavaVM[%d]@" PTR_FORMAT, result, javaVM_id, p2i(javaVM));
1595     }
1596     return true;
1597   }
1598   return false;
1599 }
1600 
1601 void JVMCIRuntime::bootstrap_finished(TRAPS) {
1602   if (_HotSpotJVMCIRuntime_instance.is_non_null()) {
1603     JVMCIENV_FROM_THREAD(THREAD);
1604     JVMCIENV->check_init(CHECK);
1605     JVMCIENV->call_HotSpotJVMCIRuntime_bootstrapFinished(_HotSpotJVMCIRuntime_instance, JVMCIENV);
1606   }
1607 }
1608 
1609 void JVMCIRuntime::describe_pending_hotspot_exception(JavaThread* THREAD) {
1610   if (HAS_PENDING_EXCEPTION) {
1611     Handle exception(THREAD, PENDING_EXCEPTION);
1612     CLEAR_PENDING_EXCEPTION;
1613     java_lang_Throwable::print_stack_trace(exception, tty);
1614 
1615     // Clear and ignore any exceptions raised during printing
1616     CLEAR_PENDING_EXCEPTION;
1617   }
1618 }
1619 
1620 
1621 void JVMCIRuntime::fatal_exception(JVMCIEnv* JVMCIENV, const char* message) {
1622   JavaThread* THREAD = JavaThread::current(); // For exception macros.
1623 
1624   static volatile int report_error = 0;
1625   if (!report_error && Atomic::cmpxchg(&report_error, 0, 1) == 0) {
1626     // Only report an error once
1627     tty->print_raw_cr(message);
1628     if (JVMCIENV != nullptr) {
1629       JVMCIENV->describe_pending_exception(tty);
1630     } else {
1631       describe_pending_hotspot_exception(THREAD);
1632     }
1633   } else {
1634     // Allow error reporting thread time to print the stack trace.
1635     THREAD->sleep(200);
1636   }
1637   fatal("Fatal JVMCI exception (see JVMCI Events for stack trace): %s", message);
1638 }
1639 
1640 // ------------------------------------------------------------------
1641 // Note: the logic of this method should mirror the logic of
1642 // constantPoolOopDesc::verify_constant_pool_resolve.
1643 bool JVMCIRuntime::check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass) {
1644   if (accessing_klass->is_objArray_klass()) {
1645     accessing_klass = ObjArrayKlass::cast(accessing_klass)->bottom_klass();
1646   }
1647   if (!accessing_klass->is_instance_klass()) {
1648     return true;
1649   }
1650 
1651   if (resolved_klass->is_objArray_klass()) {
1652     // Find the element klass, if this is an array.
1653     resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
1654   }
1655   if (resolved_klass->is_instance_klass()) {
1656     Reflection::VerifyClassAccessResults result =
1657       Reflection::verify_class_access(accessing_klass, InstanceKlass::cast(resolved_klass), true);
1658     return result == Reflection::ACCESS_OK;
1659   }
1660   return true;
1661 }
1662 
1663 // ------------------------------------------------------------------
1664 Klass* JVMCIRuntime::get_klass_by_name_impl(Klass*& accessing_klass,
1665                                           const constantPoolHandle& cpool,
1666                                           Symbol* sym,
1667                                           bool require_local) {
1668   JVMCI_EXCEPTION_CONTEXT;
1669 
1670   // Now we need to check the SystemDictionary
1671   if (sym->char_at(0) == JVM_SIGNATURE_CLASS &&
1672       sym->char_at(sym->utf8_length()-1) == JVM_SIGNATURE_ENDCLASS) {
1673     // This is a name from a signature.  Strip off the trimmings.
1674     // Call recursive to keep scope of strippedsym.
1675     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
1676                                                         sym->utf8_length()-2);
1677     return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local);
1678   }
1679 
1680   Handle loader;
1681   Handle domain;
1682   if (accessing_klass != nullptr) {
1683     loader = Handle(THREAD, accessing_klass->class_loader());
1684     domain = Handle(THREAD, accessing_klass->protection_domain());
1685   }
1686 
1687   Klass* found_klass = require_local ?
1688                          SystemDictionary::find_instance_or_array_klass(THREAD, sym, loader, domain) :
1689                          SystemDictionary::find_constrained_instance_or_array_klass(THREAD, sym, loader);
1690 
1691   // If we fail to find an array klass, look again for its element type.
1692   // The element type may be available either locally or via constraints.
1693   // In either case, if we can find the element type in the system dictionary,
1694   // we must build an array type around it.  The CI requires array klasses
1695   // to be loaded if their element klasses are loaded, except when memory
1696   // is exhausted.
1697   if (sym->char_at(0) == JVM_SIGNATURE_ARRAY &&
1698       (sym->char_at(1) == JVM_SIGNATURE_ARRAY || sym->char_at(1) == JVM_SIGNATURE_CLASS)) {
1699     // We have an unloaded array.
1700     // Build it on the fly if the element class exists.
1701     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
1702                                                      sym->utf8_length()-1);
1703 
1704     // Get element Klass recursively.
1705     Klass* elem_klass =
1706       get_klass_by_name_impl(accessing_klass,
1707                              cpool,
1708                              elem_sym,
1709                              require_local);
1710     if (elem_klass != nullptr) {
1711       // Now make an array for it
1712       return elem_klass->array_klass(THREAD);
1713     }
1714   }
1715 
1716   if (found_klass == nullptr && !cpool.is_null() && cpool->has_preresolution()) {
1717     // Look inside the constant pool for pre-resolved class entries.
1718     for (int i = cpool->length() - 1; i >= 1; i--) {
1719       if (cpool->tag_at(i).is_klass()) {
1720         Klass*  kls = cpool->resolved_klass_at(i);
1721         if (kls->name() == sym) {
1722           return kls;
1723         }
1724       }
1725     }
1726   }
1727 
1728   return found_klass;
1729 }
1730 
1731 // ------------------------------------------------------------------
1732 Klass* JVMCIRuntime::get_klass_by_name(Klass* accessing_klass,
1733                                   Symbol* klass_name,
1734                                   bool require_local) {
1735   ResourceMark rm;
1736   constantPoolHandle cpool;
1737   return get_klass_by_name_impl(accessing_klass,
1738                                                  cpool,
1739                                                  klass_name,
1740                                                  require_local);
1741 }
1742 
1743 // ------------------------------------------------------------------
1744 // Implementation of get_klass_by_index.
1745 Klass* JVMCIRuntime::get_klass_by_index_impl(const constantPoolHandle& cpool,
1746                                         int index,
1747                                         bool& is_accessible,
1748                                         Klass* accessor) {
1749   JVMCI_EXCEPTION_CONTEXT;
1750   Klass* klass = ConstantPool::klass_at_if_loaded(cpool, index);
1751   Symbol* klass_name = nullptr;
1752   if (klass == nullptr) {
1753     klass_name = cpool->klass_name_at(index);
1754   }
1755 
1756   if (klass == nullptr) {
1757     // Not found in constant pool.  Use the name to do the lookup.
1758     Klass* k = get_klass_by_name_impl(accessor,
1759                                         cpool,
1760                                         klass_name,
1761                                         false);
1762     // Calculate accessibility the hard way.
1763     if (k == nullptr) {
1764       is_accessible = false;
1765     } else if (k->class_loader() != accessor->class_loader() &&
1766                get_klass_by_name_impl(accessor, cpool, k->name(), true) == nullptr) {
1767       // Loaded only remotely.  Not linked yet.
1768       is_accessible = false;
1769     } else {
1770       // Linked locally, and we must also check public/private, etc.
1771       is_accessible = check_klass_accessibility(accessor, k);
1772     }
1773     if (!is_accessible) {
1774       return nullptr;
1775     }
1776     return k;
1777   }
1778 
1779   // It is known to be accessible, since it was found in the constant pool.
1780   is_accessible = true;
1781   return klass;
1782 }
1783 
1784 // ------------------------------------------------------------------
1785 // Get a klass from the constant pool.
1786 Klass* JVMCIRuntime::get_klass_by_index(const constantPoolHandle& cpool,
1787                                    int index,
1788                                    bool& is_accessible,
1789                                    Klass* accessor) {
1790   ResourceMark rm;
1791   Klass* result = get_klass_by_index_impl(cpool, index, is_accessible, accessor);
1792   return result;
1793 }
1794 
1795 // ------------------------------------------------------------------
1796 // Perform an appropriate method lookup based on accessor, holder,
1797 // name, signature, and bytecode.
1798 Method* JVMCIRuntime::lookup_method(InstanceKlass* accessor,
1799                                     Klass*        holder,
1800                                     Symbol*       name,
1801                                     Symbol*       sig,
1802                                     Bytecodes::Code bc,
1803                                     constantTag   tag) {
1804   // Accessibility checks are performed in JVMCIEnv::get_method_by_index_impl().
1805   assert(check_klass_accessibility(accessor, holder), "holder not accessible");
1806 
1807   LinkInfo link_info(holder, name, sig, accessor,
1808                      LinkInfo::AccessCheck::required,
1809                      LinkInfo::LoaderConstraintCheck::required,
1810                      tag);
1811   switch (bc) {
1812     case Bytecodes::_invokestatic:
1813       return LinkResolver::resolve_static_call_or_null(link_info);
1814     case Bytecodes::_invokespecial:
1815       return LinkResolver::resolve_special_call_or_null(link_info);
1816     case Bytecodes::_invokeinterface:
1817       return LinkResolver::linktime_resolve_interface_method_or_null(link_info);
1818     case Bytecodes::_invokevirtual:
1819       return LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
1820     default:
1821       fatal("Unhandled bytecode: %s", Bytecodes::name(bc));
1822       return nullptr; // silence compiler warnings
1823   }
1824 }
1825 
1826 
1827 // ------------------------------------------------------------------
1828 Method* JVMCIRuntime::get_method_by_index_impl(const constantPoolHandle& cpool,
1829                                                int index, Bytecodes::Code bc,
1830                                                InstanceKlass* accessor) {
1831   if (bc == Bytecodes::_invokedynamic) {
1832     if (cpool->resolved_indy_entry_at(index)->is_resolved()) {
1833       return cpool->resolved_indy_entry_at(index)->method();
1834     }
1835 
1836     return nullptr;
1837   }
1838 
1839   int holder_index = cpool->klass_ref_index_at(index, bc);
1840   bool holder_is_accessible;
1841   Klass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
1842 
1843   // Get the method's name and signature.
1844   Symbol* name_sym = cpool->name_ref_at(index, bc);
1845   Symbol* sig_sym  = cpool->signature_ref_at(index, bc);
1846 
1847   if (cpool->has_preresolution()
1848       || ((holder == vmClasses::MethodHandle_klass() || holder == vmClasses::VarHandle_klass()) &&
1849           MethodHandles::is_signature_polymorphic_name(holder, name_sym))) {
1850     // Short-circuit lookups for JSR 292-related call sites.
1851     // That is, do not rely only on name-based lookups, because they may fail
1852     // if the names are not resolvable in the boot class loader (7056328).
1853     switch (bc) {
1854     case Bytecodes::_invokevirtual:
1855     case Bytecodes::_invokeinterface:
1856     case Bytecodes::_invokespecial:
1857     case Bytecodes::_invokestatic:
1858       {
1859         Method* m = ConstantPool::method_at_if_loaded(cpool, index);
1860         if (m != nullptr) {
1861           return m;
1862         }
1863       }
1864       break;
1865     default:
1866       break;
1867     }
1868   }
1869 
1870   if (holder_is_accessible) { // Our declared holder is loaded.
1871     constantTag tag = cpool->tag_ref_at(index, bc);
1872     Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
1873     if (m != nullptr) {
1874       // We found the method.
1875       return m;
1876     }
1877   }
1878 
1879   // Either the declared holder was not loaded, or the method could
1880   // not be found.
1881 
1882   return nullptr;
1883 }
1884 
1885 // ------------------------------------------------------------------
1886 InstanceKlass* JVMCIRuntime::get_instance_klass_for_declared_method_holder(Klass* method_holder) {
1887   // For the case of <array>.clone(), the method holder can be an ArrayKlass*
1888   // instead of an InstanceKlass*.  For that case simply pretend that the
1889   // declared holder is Object.clone since that's where the call will bottom out.
1890   if (method_holder->is_instance_klass()) {
1891     return InstanceKlass::cast(method_holder);
1892   } else if (method_holder->is_array_klass()) {
1893     return vmClasses::Object_klass();
1894   } else {
1895     ShouldNotReachHere();
1896   }
1897   return nullptr;
1898 }
1899 
1900 
1901 // ------------------------------------------------------------------
1902 Method* JVMCIRuntime::get_method_by_index(const constantPoolHandle& cpool,
1903                                      int index, Bytecodes::Code bc,
1904                                      InstanceKlass* accessor) {
1905   ResourceMark rm;
1906   return get_method_by_index_impl(cpool, index, bc, accessor);
1907 }
1908 
1909 // ------------------------------------------------------------------
1910 // Check for changes to the system dictionary during compilation
1911 // class loads, evolution, breakpoints
1912 JVMCI::CodeInstallResult JVMCIRuntime::validate_compile_task_dependencies(Dependencies* dependencies,
1913                                                                           JVMCICompileState* compile_state,
1914                                                                           char** failure_detail,
1915                                                                           bool& failing_dep_is_call_site)
1916 {
1917   failing_dep_is_call_site = false;
1918   // If JVMTI capabilities were enabled during compile, the compilation is invalidated.
1919   if (compile_state != nullptr && compile_state->jvmti_state_changed()) {
1920     *failure_detail = (char*) "Jvmti state change during compilation invalidated dependencies";
1921     return JVMCI::dependencies_failed;
1922   }
1923 
1924   CompileTask* task = compile_state == nullptr ? nullptr : compile_state->task();
1925   Dependencies::DepType result = dependencies->validate_dependencies(task, failure_detail);
1926 
1927   if (result == Dependencies::end_marker) {
1928     return JVMCI::ok;
1929   }
1930   if (result == Dependencies::call_site_target_value) {
1931     failing_dep_is_call_site = true;
1932   }
1933   return JVMCI::dependencies_failed;
1934 }
1935 
1936 // Called after an upcall to `function` while compiling `method`.
1937 // If an exception occurred, it is cleared, the compilation state
1938 // is updated with the failure and this method returns true.
1939 // Otherwise, it returns false.
1940 static bool after_compiler_upcall(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, const char* function) {
1941   if (JVMCIENV->has_pending_exception()) {
1942     ResourceMark rm;
1943     bool reason_on_C_heap = true;
1944     const char* pending_string = nullptr;
1945     const char* pending_stack_trace = nullptr;
1946     JVMCIENV->pending_exception_as_string(&pending_string, &pending_stack_trace);
1947     if (pending_string == nullptr) pending_string = "null";
1948     // Using stringStream instead of err_msg to avoid truncation
1949     stringStream st;
1950     st.print("uncaught exception in %s [%s]", function, pending_string);
1951     const char* failure_reason = os::strdup(st.freeze(), mtJVMCI);
1952     if (failure_reason == nullptr) {
1953       failure_reason = "uncaught exception";
1954       reason_on_C_heap = false;
1955     }
1956     JVMCI_event_1("%s", failure_reason);
1957     Log(jit, compilation) log;
1958     if (log.is_info()) {
1959       log.info("%s while compiling %s", failure_reason, method->name_and_sig_as_C_string());
1960       if (pending_stack_trace != nullptr) {
1961         LogStream ls(log.info());
1962         ls.print_raw_cr(pending_stack_trace);
1963       }
1964     }
1965     JVMCICompileState* compile_state = JVMCIENV->compile_state();
1966     compile_state->set_failure(true, failure_reason, reason_on_C_heap);
1967     compiler->on_upcall(failure_reason, compile_state);
1968     return true;
1969   }
1970   return false;
1971 }
1972 
1973 void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, int entry_bci) {
1974   JVMCI_EXCEPTION_CONTEXT
1975 
1976   JVMCICompileState* compile_state = JVMCIENV->compile_state();
1977 
1978   bool is_osr = entry_bci != InvocationEntryBci;
1979   if (compiler->is_bootstrapping() && is_osr) {
1980     // no OSR compilations during bootstrap - the compiler is just too slow at this point,
1981     // and we know that there are no endless loops
1982     compile_state->set_failure(true, "No OSR during bootstrap");
1983     return;
1984   }
1985   if (JVMCI::in_shutdown()) {
1986     if (UseJVMCINativeLibrary) {
1987       JVMCIRuntime *runtime = JVMCI::compiler_runtime(thread, false);
1988       if (runtime != nullptr) {
1989         runtime->detach_thread(thread, "JVMCI shutdown pre-empted compilation");
1990       }
1991     }
1992     compile_state->set_failure(false, "Avoiding compilation during shutdown");
1993     return;
1994   }
1995 
1996   HandleMark hm(thread);
1997   JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
1998   if (after_compiler_upcall(JVMCIENV, compiler, method, "get_HotSpotJVMCIRuntime")) {
1999     return;
2000   }
2001   JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(method, JVMCIENV);
2002   if (after_compiler_upcall(JVMCIENV, compiler, method, "get_jvmci_method")) {
2003     return;
2004   }
2005 
2006   JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci,
2007                                                                      (jlong) compile_state, compile_state->task()->compile_id());
2008 #ifdef ASSERT
2009   if (JVMCIENV->has_pending_exception()) {
2010     const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.compileMethodExceptionIsFatal");
2011     if (val != nullptr && strcmp(val, "true") == 0) {
2012       fatal_exception(JVMCIENV, "testing JVMCI fatal exception handling");
2013     }
2014   }
2015 #endif
2016 
2017   if (after_compiler_upcall(JVMCIENV, compiler, method, "call_HotSpotJVMCIRuntime_compileMethod")) {
2018     return;
2019   }
2020   compiler->on_upcall(nullptr);
2021   guarantee(result_object.is_non_null(), "call_HotSpotJVMCIRuntime_compileMethod returned null");
2022   JVMCIObject failure_message = JVMCIENV->get_HotSpotCompilationRequestResult_failureMessage(result_object);
2023   if (failure_message.is_non_null()) {
2024     // Copy failure reason into resource memory first ...
2025     const char* failure_reason = JVMCIENV->as_utf8_string(failure_message);
2026     // ... and then into the C heap.
2027     failure_reason = os::strdup(failure_reason, mtJVMCI);
2028     bool retryable = JVMCIENV->get_HotSpotCompilationRequestResult_retry(result_object) != 0;
2029     compile_state->set_failure(retryable, failure_reason, true);
2030   } else {
2031     if (!compile_state->task()->is_success()) {
2032       compile_state->set_failure(true, "no nmethod produced");
2033     } else {
2034       compile_state->task()->set_num_inlined_bytecodes(JVMCIENV->get_HotSpotCompilationRequestResult_inlinedBytecodes(result_object));
2035       compiler->inc_methods_compiled();
2036     }
2037   }
2038   if (compiler->is_bootstrapping()) {
2039     compiler->set_bootstrap_compilation_request_handled();
2040   }
2041 }
2042 
2043 bool JVMCIRuntime::is_gc_supported(JVMCIEnv* JVMCIENV, CollectedHeap::Name name) {
2044   JVMCI_EXCEPTION_CONTEXT
2045 
2046   JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
2047   if (JVMCIENV->has_pending_exception()) {
2048     fatal_exception(JVMCIENV, "Exception during HotSpotJVMCIRuntime initialization");
2049   }
2050   return JVMCIENV->call_HotSpotJVMCIRuntime_isGCSupported(receiver, (int) name);
2051 }
2052 
2053 bool JVMCIRuntime::is_intrinsic_supported(JVMCIEnv* JVMCIENV, jint id) {
2054   JVMCI_EXCEPTION_CONTEXT
2055 
2056   JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
2057   if (JVMCIENV->has_pending_exception()) {
2058     fatal_exception(JVMCIENV, "Exception during HotSpotJVMCIRuntime initialization");
2059   }
2060   return JVMCIENV->call_HotSpotJVMCIRuntime_isIntrinsicSupported(receiver, id);
2061 }
2062 
2063 // ------------------------------------------------------------------
2064 JVMCI::CodeInstallResult JVMCIRuntime::register_method(JVMCIEnv* JVMCIENV,
2065                                                        const methodHandle& method,
2066                                                        nmethod*& nm,
2067                                                        int entry_bci,
2068                                                        CodeOffsets* offsets,
2069                                                        int orig_pc_offset,
2070                                                        CodeBuffer* code_buffer,
2071                                                        int frame_words,
2072                                                        OopMapSet* oop_map_set,
2073                                                        ExceptionHandlerTable* handler_table,
2074                                                        ImplicitExceptionTable* implicit_exception_table,
2075                                                        AbstractCompiler* compiler,
2076                                                        DebugInformationRecorder* debug_info,
2077                                                        Dependencies* dependencies,
2078                                                        int compile_id,
2079                                                        bool has_monitors,
2080                                                        bool has_unsafe_access,
2081                                                        bool has_wide_vector,
2082                                                        JVMCIObject compiled_code,
2083                                                        JVMCIObject nmethod_mirror,
2084                                                        FailedSpeculation** failed_speculations,
2085                                                        char* speculations,
2086                                                        int speculations_len,
2087                                                        int nmethod_entry_patch_offset) {
2088   JVMCI_EXCEPTION_CONTEXT;
2089   CompLevel comp_level = CompLevel_full_optimization;
2090   char* failure_detail = nullptr;
2091 
2092   bool install_default = JVMCIENV->get_HotSpotNmethod_isDefault(nmethod_mirror) != 0;
2093   assert(JVMCIENV->isa_HotSpotNmethod(nmethod_mirror), "must be");
2094   JVMCIObject name = JVMCIENV->get_InstalledCode_name(nmethod_mirror);
2095   const char* nmethod_mirror_name = name.is_null() ? nullptr : JVMCIENV->as_utf8_string(name);
2096   int nmethod_mirror_index;
2097   if (!install_default) {
2098     // Reserve or initialize mirror slot in the oops table.
2099     OopRecorder* oop_recorder = debug_info->oop_recorder();
2100     nmethod_mirror_index = oop_recorder->allocate_oop_index(nmethod_mirror.is_hotspot() ? nmethod_mirror.as_jobject() : nullptr);
2101   } else {
2102     // A default HotSpotNmethod mirror is never tracked by the nmethod
2103     nmethod_mirror_index = -1;
2104   }
2105 
2106   JVMCI::CodeInstallResult result(JVMCI::ok);
2107 
2108   // We require method counters to store some method state (max compilation levels) required by the compilation policy.
2109   if (method->get_method_counters(THREAD) == nullptr) {
2110     result = JVMCI::cache_full;
2111     failure_detail = (char*) "can't create method counters";
2112   }
2113 
2114   if (result == JVMCI::ok) {
2115     // Check if memory should be freed before allocation
2116     CodeCache::gc_on_allocation();
2117 
2118     // To prevent compile queue updates.
2119     MutexLocker locker(THREAD, MethodCompileQueue_lock);
2120 
2121     // Prevent InstanceKlass::add_to_hierarchy from running
2122     // and invalidating our dependencies until we install this method.
2123     MutexLocker ml(Compile_lock);
2124 
2125     // Encode the dependencies now, so we can check them right away.
2126     dependencies->encode_content_bytes();
2127 
2128     // Record the dependencies for the current compile in the log
2129     if (LogCompilation) {
2130       for (Dependencies::DepStream deps(dependencies); deps.next(); ) {
2131         deps.log_dependency();
2132       }
2133     }
2134 
2135     // Check for {class loads, evolution, breakpoints} during compilation
2136     JVMCICompileState* compile_state = JVMCIENV->compile_state();
2137     bool failing_dep_is_call_site;
2138     result = validate_compile_task_dependencies(dependencies, compile_state, &failure_detail, failing_dep_is_call_site);
2139     if (result != JVMCI::ok) {
2140       // While not a true deoptimization, it is a preemptive decompile.
2141       MethodData* mdp = method()->method_data();
2142       if (mdp != nullptr && !failing_dep_is_call_site) {
2143         mdp->inc_decompile_count();
2144 #ifdef ASSERT
2145         if (mdp->decompile_count() > (uint)PerMethodRecompilationCutoff) {
2146           ResourceMark m;
2147           tty->print_cr("WARN: endless recompilation of %s. Method was set to not compilable.", method()->name_and_sig_as_C_string());
2148         }
2149 #endif
2150       }
2151 
2152       // All buffers in the CodeBuffer are allocated in the CodeCache.
2153       // If the code buffer is created on each compile attempt
2154       // as in C2, then it must be freed.
2155       //code_buffer->free_blob();
2156     } else {
2157       JVMCINMethodData* data = JVMCINMethodData::create(nmethod_mirror_index,
2158                                                         nmethod_entry_patch_offset,
2159                                                         nmethod_mirror_name,
2160                                                         failed_speculations);
2161       nm =  nmethod::new_nmethod(method,
2162                                  compile_id,
2163                                  entry_bci,
2164                                  offsets,
2165                                  orig_pc_offset,
2166                                  debug_info, dependencies, code_buffer,
2167                                  frame_words, oop_map_set,
2168                                  handler_table, implicit_exception_table,
2169                                  compiler, comp_level,
2170                                  speculations, speculations_len, data);
2171 
2172 
2173       // Free codeBlobs
2174       if (nm == nullptr) {
2175         // The CodeCache is full.  Print out warning and disable compilation.
2176         {
2177           MutexUnlocker ml(Compile_lock);
2178           MutexUnlocker locker(MethodCompileQueue_lock);
2179           CompileBroker::handle_full_code_cache(CodeCache::get_code_blob_type(comp_level));
2180         }
2181         result = JVMCI::cache_full;
2182       } else {
2183         nm->set_has_unsafe_access(has_unsafe_access);
2184         nm->set_has_wide_vectors(has_wide_vector);
2185         nm->set_has_monitors(has_monitors);
2186 
2187         JVMCINMethodData* data = nm->jvmci_nmethod_data();
2188         assert(data != nullptr, "must be");
2189         if (install_default) {
2190           assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm, /* phantom_ref */ false) == nullptr, "must be");
2191           if (entry_bci == InvocationEntryBci) {
2192             // If there is an old version we're done with it
2193             nmethod* old = method->code();
2194             if (TraceMethodReplacement && old != nullptr) {
2195               ResourceMark rm;
2196               char *method_name = method->name_and_sig_as_C_string();
2197               tty->print_cr("Replacing method %s", method_name);
2198             }
2199             if (old != nullptr ) {
2200               old->make_not_entrant();
2201             }
2202 
2203             LogTarget(Info, nmethod, install) lt;
2204             if (lt.is_enabled()) {
2205               ResourceMark rm;
2206               char *method_name = method->name_and_sig_as_C_string();
2207               lt.print("Installing method (%d) %s [entry point: %p]",
2208                         comp_level, method_name, nm->entry_point());
2209             }
2210             // Allow the code to be executed
2211             MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2212             if (nm->make_in_use()) {
2213               method->set_code(method, nm);
2214             } else {
2215               result = JVMCI::nmethod_reclaimed;
2216             }
2217           } else {
2218             LogTarget(Info, nmethod, install) lt;
2219             if (lt.is_enabled()) {
2220               ResourceMark rm;
2221               char *method_name = method->name_and_sig_as_C_string();
2222               lt.print("Installing osr method (%d) %s @ %d",
2223                         comp_level, method_name, entry_bci);
2224             }
2225             MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2226             if (nm->make_in_use()) {
2227               InstanceKlass::cast(method->method_holder())->add_osr_nmethod(nm);
2228             } else {
2229               result = JVMCI::nmethod_reclaimed;
2230             }
2231           }
2232         } else {
2233           assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm, /* phantom_ref */ false) == HotSpotJVMCI::resolve(nmethod_mirror), "must be");
2234           MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2235           if (!nm->make_in_use()) {
2236             result = JVMCI::nmethod_reclaimed;
2237           }
2238         }
2239       }
2240     }
2241   }
2242 
2243   // String creation must be done outside lock
2244   if (failure_detail != nullptr) {
2245     // A failure to allocate the string is silently ignored.
2246     JVMCIObject message = JVMCIENV->create_string(failure_detail, JVMCIENV);
2247     JVMCIENV->set_HotSpotCompiledNmethod_installationFailureMessage(compiled_code, message);
2248   }
2249 
2250   if (result == JVMCI::ok) {
2251     JVMCICompileState* state = JVMCIENV->compile_state();
2252     if (state != nullptr) {
2253       // Compilation succeeded, post what we know about it
2254       nm->post_compiled_method(state->task());
2255     }
2256   }
2257 
2258   return result;
2259 }
2260 
2261 void JVMCIRuntime::post_compile(JavaThread* thread) {
2262   if (UseJVMCINativeLibrary && JVMCI::one_shared_library_javavm_per_compilation()) {
2263     if (thread->libjvmci_runtime() != nullptr) {
2264       detach_thread(thread, "single use JavaVM");
2265     } else {
2266       // JVMCI shutdown may have already detached the thread
2267     }
2268   }
2269 }