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