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