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