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