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