1 /*
   2  * Copyright (c) 1999, 2019, 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 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "ci/ciConstant.hpp"
  28 #include "ci/ciEnv.hpp"
  29 #include "ci/ciField.hpp"
  30 #include "ci/ciInstance.hpp"
  31 #include "ci/ciInstanceKlass.hpp"
  32 #include "ci/ciMethod.hpp"
  33 #include "ci/ciNullObject.hpp"
  34 #include "ci/ciReplay.hpp"
  35 #include "ci/ciUtilities.inline.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/vmSymbols.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "code/scopeDesc.hpp"
  40 #include "compiler/compileBroker.hpp"
  41 #include "compiler/compileLog.hpp"
  42 #include "compiler/disassembler.hpp"
  43 #include "gc/shared/collectedHeap.inline.hpp"
  44 #include "interpreter/linkResolver.hpp"
  45 #include "jfr/jfrEvents.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "memory/universe.hpp"
  50 #include "oops/constantPool.inline.hpp"
  51 #include "oops/cpCache.inline.hpp"
  52 #include "oops/method.inline.hpp"
  53 #include "oops/methodData.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "prims/jvmtiExport.hpp"
  58 #include "runtime/init.hpp"
  59 #include "runtime/reflection.hpp"
  60 #include "runtime/jniHandles.inline.hpp"
  61 #include "runtime/safepointVerifiers.hpp"
  62 #include "runtime/sharedRuntime.hpp"
  63 #include "runtime/thread.inline.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/macros.hpp"
  66 #ifdef COMPILER1
  67 #include "c1/c1_Runtime1.hpp"
  68 #endif
  69 #ifdef COMPILER2
  70 #include "opto/runtime.hpp"
  71 #endif
  72 
  73 // ciEnv
  74 //
  75 // This class is the top level broker for requests from the compiler
  76 // to the VM.
  77 
  78 ciObject*              ciEnv::_null_object_instance;
  79 
  80 #define WK_KLASS_DEFN(name, ignore_s, ignore_o) ciInstanceKlass* ciEnv::_##name = NULL;
  81 WK_KLASSES_DO(WK_KLASS_DEFN)
  82 #undef WK_KLASS_DEFN
  83 
  84 ciSymbol*        ciEnv::_unloaded_cisymbol = NULL;
  85 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
  86 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;
  87 
  88 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
  89 jobject ciEnv::_ArrayStoreException_handle = NULL;
  90 jobject ciEnv::_ClassCastException_handle = NULL;
  91 
  92 #ifndef PRODUCT
  93 static bool firstEnv = true;
  94 #endif /* PRODUCT */
  95 
  96 // ------------------------------------------------------------------
  97 // ciEnv::ciEnv
  98 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter)
  99   : _ciEnv_arena(mtCompiler) {
 100   VM_ENTRY_MARK;
 101 
 102   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 103   thread->set_env(this);
 104   assert(ciEnv::current() == this, "sanity");
 105 
 106   _oop_recorder = NULL;
 107   _debug_info = NULL;
 108   _dependencies = NULL;
 109   _failure_reason = NULL;
 110   _inc_decompile_count_on_failure = true;
 111   _compilable = MethodCompilable;
 112   _break_at_compile = false;
 113   _compiler_data = NULL;
 114 #ifndef PRODUCT
 115   assert(!firstEnv, "not initialized properly");
 116 #endif /* !PRODUCT */
 117 
 118   _system_dictionary_modification_counter = system_dictionary_modification_counter;
 119   _num_inlined_bytecodes = 0;
 120   assert(task == NULL || thread->task() == task, "sanity");
 121   _task = task;
 122   _log = NULL;
 123 
 124   // Temporary buffer for creating symbols and such.
 125   _name_buffer = NULL;
 126   _name_buffer_len = 0;
 127 
 128   _arena   = &_ciEnv_arena;
 129   _factory = new (_arena) ciObjectFactory(_arena, 128);
 130 
 131   // Preload commonly referenced system ciObjects.
 132 
 133   // During VM initialization, these instances have not yet been created.
 134   // Assertions ensure that these instances are not accessed before
 135   // their initialization.
 136 
 137   assert(Universe::is_fully_initialized(), "should be complete");
 138 
 139   oop o = Universe::null_ptr_exception_instance();
 140   assert(o != NULL, "should have been initialized");
 141   _NullPointerException_instance = get_object(o)->as_instance();
 142   o = Universe::arithmetic_exception_instance();
 143   assert(o != NULL, "should have been initialized");
 144   _ArithmeticException_instance = get_object(o)->as_instance();
 145 
 146   _ArrayIndexOutOfBoundsException_instance = NULL;
 147   _ArrayStoreException_instance = NULL;
 148   _ClassCastException_instance = NULL;
 149   _the_null_string = NULL;
 150   _the_min_jint_string = NULL;
 151 
 152   _jvmti_can_hotswap_or_post_breakpoint = false;
 153   _jvmti_can_access_local_variables = false;
 154   _jvmti_can_post_on_exceptions = false;
 155   _jvmti_can_pop_frame = false;
 156 }
 157 
 158 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) {
 159   ASSERT_IN_VM;
 160 
 161   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 162   CompilerThread* current_thread = CompilerThread::current();
 163   assert(current_thread->env() == NULL, "must be");
 164   current_thread->set_env(this);
 165   assert(ciEnv::current() == this, "sanity");
 166 
 167   _oop_recorder = NULL;
 168   _debug_info = NULL;
 169   _dependencies = NULL;
 170   _failure_reason = NULL;
 171   _inc_decompile_count_on_failure = true;
 172   _compilable = MethodCompilable_never;
 173   _break_at_compile = false;
 174   _compiler_data = NULL;
 175 #ifndef PRODUCT
 176   assert(firstEnv, "must be first");
 177   firstEnv = false;
 178 #endif /* !PRODUCT */
 179 
 180   _system_dictionary_modification_counter = 0;
 181   _num_inlined_bytecodes = 0;
 182   _task = NULL;
 183   _log = NULL;
 184 
 185   // Temporary buffer for creating symbols and such.
 186   _name_buffer = NULL;
 187   _name_buffer_len = 0;
 188 
 189   _arena   = arena;
 190   _factory = new (_arena) ciObjectFactory(_arena, 128);
 191 
 192   // Preload commonly referenced system ciObjects.
 193 
 194   // During VM initialization, these instances have not yet been created.
 195   // Assertions ensure that these instances are not accessed before
 196   // their initialization.
 197 
 198   assert(Universe::is_fully_initialized(), "must be");
 199 
 200   _NullPointerException_instance = NULL;
 201   _ArithmeticException_instance = NULL;
 202   _ArrayIndexOutOfBoundsException_instance = NULL;
 203   _ArrayStoreException_instance = NULL;
 204   _ClassCastException_instance = NULL;
 205   _the_null_string = NULL;
 206   _the_min_jint_string = NULL;
 207 
 208   _jvmti_can_hotswap_or_post_breakpoint = false;
 209   _jvmti_can_access_local_variables = false;
 210   _jvmti_can_post_on_exceptions = false;
 211   _jvmti_can_pop_frame = false;
 212 }
 213 
 214 ciEnv::~ciEnv() {
 215   GUARDED_VM_ENTRY(
 216       CompilerThread* current_thread = CompilerThread::current();
 217       _factory->remove_symbols();
 218       // Need safepoint to clear the env on the thread.  RedefineClasses might
 219       // be reading it.
 220       current_thread->set_env(NULL);
 221   )
 222 }
 223 
 224 // ------------------------------------------------------------------
 225 // Cache Jvmti state
 226 void ciEnv::cache_jvmti_state() {
 227   VM_ENTRY_MARK;
 228   // Get Jvmti capabilities under lock to get consistant values.
 229   MutexLocker mu(JvmtiThreadState_lock);
 230   _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint();
 231   _jvmti_can_access_local_variables     = JvmtiExport::can_access_local_variables();
 232   _jvmti_can_post_on_exceptions         = JvmtiExport::can_post_on_exceptions();
 233   _jvmti_can_pop_frame                  = JvmtiExport::can_pop_frame();
 234 }
 235 
 236 bool ciEnv::jvmti_state_changed() const {
 237   if (!_jvmti_can_access_local_variables &&
 238       JvmtiExport::can_access_local_variables()) {
 239     return true;
 240   }
 241   if (!_jvmti_can_hotswap_or_post_breakpoint &&
 242       JvmtiExport::can_hotswap_or_post_breakpoint()) {
 243     return true;
 244   }
 245   if (!_jvmti_can_post_on_exceptions &&
 246       JvmtiExport::can_post_on_exceptions()) {
 247     return true;
 248   }
 249   if (!_jvmti_can_pop_frame &&
 250       JvmtiExport::can_pop_frame()) {
 251     return true;
 252   }
 253   return false;
 254 }
 255 
 256 // ------------------------------------------------------------------
 257 // Cache DTrace flags
 258 void ciEnv::cache_dtrace_flags() {
 259   // Need lock?
 260   _dtrace_extended_probes = ExtendedDTraceProbes;
 261   if (_dtrace_extended_probes) {
 262     _dtrace_monitor_probes  = true;
 263     _dtrace_method_probes   = true;
 264     _dtrace_alloc_probes    = true;
 265   } else {
 266     _dtrace_monitor_probes  = DTraceMonitorProbes;
 267     _dtrace_method_probes   = DTraceMethodProbes;
 268     _dtrace_alloc_probes    = DTraceAllocProbes;
 269   }
 270 }
 271 
 272 // ------------------------------------------------------------------
 273 // helper for lazy exception creation
 274 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
 275   VM_ENTRY_MARK;
 276   if (handle == NULL) {
 277     // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
 278     Klass* k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
 279     jobject objh = NULL;
 280     if (!HAS_PENDING_EXCEPTION && k != NULL) {
 281       oop obj = InstanceKlass::cast(k)->allocate_instance(THREAD);
 282       if (!HAS_PENDING_EXCEPTION)
 283         objh = JNIHandles::make_global(Handle(THREAD, obj));
 284     }
 285     if (HAS_PENDING_EXCEPTION) {
 286       CLEAR_PENDING_EXCEPTION;
 287     } else {
 288       handle = objh;
 289     }
 290   }
 291   oop obj = JNIHandles::resolve(handle);
 292   return obj == NULL? NULL: get_object(obj)->as_instance();
 293 }
 294 
 295 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 296   if (_ArrayIndexOutOfBoundsException_instance == NULL) {
 297     _ArrayIndexOutOfBoundsException_instance
 298           = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
 299           vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 300   }
 301   return _ArrayIndexOutOfBoundsException_instance;
 302 }
 303 ciInstance* ciEnv::ArrayStoreException_instance() {
 304   if (_ArrayStoreException_instance == NULL) {
 305     _ArrayStoreException_instance
 306           = get_or_create_exception(_ArrayStoreException_handle,
 307           vmSymbols::java_lang_ArrayStoreException());
 308   }
 309   return _ArrayStoreException_instance;
 310 }
 311 ciInstance* ciEnv::ClassCastException_instance() {
 312   if (_ClassCastException_instance == NULL) {
 313     _ClassCastException_instance
 314           = get_or_create_exception(_ClassCastException_handle,
 315           vmSymbols::java_lang_ClassCastException());
 316   }
 317   return _ClassCastException_instance;
 318 }
 319 
 320 ciInstance* ciEnv::the_null_string() {
 321   if (_the_null_string == NULL) {
 322     VM_ENTRY_MARK;
 323     _the_null_string = get_object(Universe::the_null_string())->as_instance();
 324   }
 325   return _the_null_string;
 326 }
 327 
 328 ciInstance* ciEnv::the_min_jint_string() {
 329   if (_the_min_jint_string == NULL) {
 330     VM_ENTRY_MARK;
 331     _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
 332   }
 333   return _the_min_jint_string;
 334 }
 335 
 336 // ------------------------------------------------------------------
 337 // ciEnv::get_method_from_handle
 338 ciMethod* ciEnv::get_method_from_handle(Method* method) {
 339   VM_ENTRY_MARK;
 340   return get_metadata(method)->as_method();
 341 }
 342 
 343 // ------------------------------------------------------------------
 344 // ciEnv::array_element_offset_in_bytes
 345 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
 346   VM_ENTRY_MARK;
 347   objArrayOop a = (objArrayOop)a_h->get_oop();
 348   assert(a->is_objArray(), "");
 349   int length = a->length();
 350   oop o = o_h->get_oop();
 351   for (int i = 0; i < length; i++) {
 352     if (a->obj_at(i) == o)  return i;
 353   }
 354   return -1;
 355 }
 356 
 357 
 358 // ------------------------------------------------------------------
 359 // ciEnv::check_klass_accessiblity
 360 //
 361 // Note: the logic of this method should mirror the logic of
 362 // ConstantPool::verify_constant_pool_resolve.
 363 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
 364                                       Klass* resolved_klass) {
 365   if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
 366     return true;
 367   }
 368   if (accessing_klass->is_obj_array_klass()) {
 369     accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
 370   }
 371   if (!accessing_klass->is_instance_klass()) {
 372     return true;
 373   }
 374 
 375   if (resolved_klass->is_objArray_klass()) {
 376     // Find the element klass, if this is an array.
 377     resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
 378   }
 379   if (resolved_klass->is_instance_klass()) {
 380     return (Reflection::verify_class_access(accessing_klass->get_Klass(),
 381                                             InstanceKlass::cast(resolved_klass),
 382                                             true) == Reflection::ACCESS_OK);
 383   }
 384   return true;
 385 }
 386 
 387 // ------------------------------------------------------------------
 388 // ciEnv::get_klass_by_name_impl
 389 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
 390                                        const constantPoolHandle& cpool,
 391                                        ciSymbol* name,
 392                                        bool require_local) {
 393   ASSERT_IN_VM;
 394   EXCEPTION_CONTEXT;
 395 
 396   // Now we need to check the SystemDictionary
 397   Symbol* sym = name->get_symbol();
 398   if (sym->byte_at(0) == 'L' &&
 399     sym->byte_at(sym->utf8_length()-1) == ';') {
 400     // This is a name from a signature.  Strip off the trimmings.
 401     // Call recursive to keep scope of strippedsym.
 402     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
 403                     sym->utf8_length()-2,
 404                     KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
 405     ciSymbol* strippedname = get_symbol(strippedsym);
 406     return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local);
 407   }
 408 
 409   // Check for prior unloaded klass.  The SystemDictionary's answers
 410   // can vary over time but the compiler needs consistency.
 411   ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
 412   if (unloaded_klass != NULL) {
 413     if (require_local)  return NULL;
 414     return unloaded_klass;
 415   }
 416 
 417   Handle loader(THREAD, (oop)NULL);
 418   Handle domain(THREAD, (oop)NULL);
 419   if (accessing_klass != NULL) {
 420     loader = Handle(THREAD, accessing_klass->loader());
 421     domain = Handle(THREAD, accessing_klass->protection_domain());
 422   }
 423 
 424   // setup up the proper type to return on OOM
 425   ciKlass* fail_type;
 426   if (sym->byte_at(0) == '[') {
 427     fail_type = _unloaded_ciobjarrayklass;
 428   } else {
 429     fail_type = _unloaded_ciinstance_klass;
 430   }
 431   Klass* found_klass;
 432   {
 433     ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
 434     MutexLocker ml(Compile_lock);
 435     Klass* kls;
 436     if (!require_local) {
 437       kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
 438                                                                        KILL_COMPILE_ON_FATAL_(fail_type));
 439     } else {
 440       kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
 441                                                            KILL_COMPILE_ON_FATAL_(fail_type));
 442     }
 443     found_klass = kls;
 444   }
 445 
 446   // If we fail to find an array klass, look again for its element type.
 447   // The element type may be available either locally or via constraints.
 448   // In either case, if we can find the element type in the system dictionary,
 449   // we must build an array type around it.  The CI requires array klasses
 450   // to be loaded if their element klasses are loaded, except when memory
 451   // is exhausted.
 452   if (sym->byte_at(0) == '[' &&
 453       (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
 454     // We have an unloaded array.
 455     // Build it on the fly if the element class exists.
 456     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
 457                                                  sym->utf8_length()-1,
 458                                                  KILL_COMPILE_ON_FATAL_(fail_type));
 459 
 460     // Get element ciKlass recursively.
 461     ciKlass* elem_klass =
 462       get_klass_by_name_impl(accessing_klass,
 463                              cpool,
 464                              get_symbol(elem_sym),
 465                              require_local);
 466     if (elem_klass != NULL && elem_klass->is_loaded()) {
 467       // Now make an array for it
 468       return ciObjArrayKlass::make_impl(elem_klass);
 469     }
 470   }
 471 
 472   if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
 473     // Look inside the constant pool for pre-resolved class entries.
 474     for (int i = cpool->length() - 1; i >= 1; i--) {
 475       if (cpool->tag_at(i).is_klass()) {
 476         Klass* kls = cpool->resolved_klass_at(i);
 477         if (kls->name() == sym) {
 478           found_klass = kls;
 479           break;
 480         }
 481       }
 482     }
 483   }
 484 
 485   if (found_klass != NULL) {
 486     // Found it.  Build a CI handle.
 487     return get_klass(found_klass);
 488   }
 489 
 490   if (require_local)  return NULL;
 491 
 492   // Not yet loaded into the VM, or not governed by loader constraints.
 493   // Make a CI representative for it.
 494   return get_unloaded_klass(accessing_klass, name);
 495 }
 496 
 497 // ------------------------------------------------------------------
 498 // ciEnv::get_klass_by_name
 499 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
 500                                   ciSymbol* klass_name,
 501                                   bool require_local) {
 502   GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
 503                                                  constantPoolHandle(),
 504                                                  klass_name,
 505                                                  require_local);)
 506 }
 507 
 508 // ------------------------------------------------------------------
 509 // ciEnv::get_klass_by_index_impl
 510 //
 511 // Implementation of get_klass_by_index.
 512 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool,
 513                                         int index,
 514                                         bool& is_accessible,
 515                                         ciInstanceKlass* accessor) {
 516   EXCEPTION_CONTEXT;
 517   Klass* klass = NULL;
 518   Symbol* klass_name = NULL;
 519 
 520   if (cpool->tag_at(index).is_symbol()) {
 521     klass_name = cpool->symbol_at(index);
 522   } else {
 523     // Check if it's resolved if it's not a symbol constant pool entry.
 524     klass =  ConstantPool::klass_at_if_loaded(cpool, index);
 525     // Try to look it up by name.
 526     if (klass == NULL) {
 527       klass_name = cpool->klass_name_at(index);
 528     }
 529   }
 530 
 531   if (klass == NULL) {
 532     // Not found in constant pool.  Use the name to do the lookup.
 533     ciKlass* k = get_klass_by_name_impl(accessor,
 534                                         cpool,
 535                                         get_symbol(klass_name),
 536                                         false);
 537     // Calculate accessibility the hard way.
 538     if (!k->is_loaded()) {
 539       is_accessible = false;
 540     } else if (!oopDesc::equals(k->loader(), accessor->loader()) &&
 541                get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
 542       // Loaded only remotely.  Not linked yet.
 543       is_accessible = false;
 544     } else {
 545       // Linked locally, and we must also check public/private, etc.
 546       is_accessible = check_klass_accessibility(accessor, k->get_Klass());
 547     }
 548     return k;
 549   }
 550 
 551   // Check for prior unloaded klass.  The SystemDictionary's answers
 552   // can vary over time but the compiler needs consistency.
 553   ciSymbol* name = get_symbol(klass->name());
 554   ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
 555   if (unloaded_klass != NULL) {
 556     is_accessible = false;
 557     return unloaded_klass;
 558   }
 559 
 560   // It is known to be accessible, since it was found in the constant pool.
 561   is_accessible = true;
 562   return get_klass(klass);
 563 }
 564 
 565 // ------------------------------------------------------------------
 566 // ciEnv::get_klass_by_index
 567 //
 568 // Get a klass from the constant pool.
 569 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool,
 570                                    int index,
 571                                    bool& is_accessible,
 572                                    ciInstanceKlass* accessor) {
 573   GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
 574 }
 575 
 576 // ------------------------------------------------------------------
 577 // ciEnv::get_constant_by_index_impl
 578 //
 579 // Implementation of get_constant_by_index().
 580 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 581                                              int pool_index, int cache_index,
 582                                              ciInstanceKlass* accessor) {
 583   bool ignore_will_link;
 584   EXCEPTION_CONTEXT;
 585   int index = pool_index;
 586   if (cache_index >= 0) {
 587     assert(index < 0, "only one kind of index at a time");
 588     index = cpool->object_to_cp_index(cache_index);
 589     oop obj = cpool->resolved_references()->obj_at(cache_index);
 590     if (obj != NULL) {
 591       if (oopDesc::equals(obj, Universe::the_null_sentinel())) {
 592         return ciConstant(T_OBJECT, get_object(NULL));
 593       }
 594       BasicType bt = T_OBJECT;
 595       if (cpool->tag_at(index).is_dynamic_constant())
 596         bt = FieldType::basic_type(cpool->uncached_signature_ref_at(index));
 597       if (is_reference_type(bt)) {
 598       } else {
 599         // we have to unbox the primitive value
 600         if (!is_java_primitive(bt))  return ciConstant();
 601         jvalue value;
 602         BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
 603         assert(bt2 == bt, "");
 604         switch (bt2) {
 605         case T_DOUBLE:  return ciConstant(value.d);
 606         case T_FLOAT:   return ciConstant(value.f);
 607         case T_LONG:    return ciConstant(value.j);
 608         case T_INT:     return ciConstant(bt2, value.i);
 609         case T_SHORT:   return ciConstant(bt2, value.s);
 610         case T_BYTE:    return ciConstant(bt2, value.b);
 611         case T_CHAR:    return ciConstant(bt2, value.c);
 612         case T_BOOLEAN: return ciConstant(bt2, value.z);
 613         default:  return ciConstant();
 614         }
 615       }
 616       ciObject* ciobj = get_object(obj);
 617       if (ciobj->is_array()) {
 618         return ciConstant(T_ARRAY, ciobj);
 619       } else {
 620         assert(ciobj->is_instance(), "should be an instance");
 621         return ciConstant(T_OBJECT, ciobj);
 622       }
 623     }
 624   }
 625   constantTag tag = cpool->tag_at(index);
 626   if (tag.is_int()) {
 627     return ciConstant(T_INT, (jint)cpool->int_at(index));
 628   } else if (tag.is_long()) {
 629     return ciConstant((jlong)cpool->long_at(index));
 630   } else if (tag.is_float()) {
 631     return ciConstant((jfloat)cpool->float_at(index));
 632   } else if (tag.is_double()) {
 633     return ciConstant((jdouble)cpool->double_at(index));
 634   } else if (tag.is_string()) {
 635     oop string = NULL;
 636     assert(cache_index >= 0, "should have a cache index");
 637     if (cpool->is_pseudo_string_at(index)) {
 638       string = cpool->pseudo_string_at(index, cache_index);
 639     } else {
 640       string = cpool->string_at(index, cache_index, THREAD);
 641       if (HAS_PENDING_EXCEPTION) {
 642         CLEAR_PENDING_EXCEPTION;
 643         record_out_of_memory_failure();
 644         return ciConstant();
 645       }
 646     }
 647     ciObject* constant = get_object(string);
 648     if (constant->is_array()) {
 649       return ciConstant(T_ARRAY, constant);
 650     } else {
 651       assert (constant->is_instance(), "must be an instance, or not? ");
 652       return ciConstant(T_OBJECT, constant);
 653     }
 654   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
 655     // 4881222: allow ldc to take a class type
 656     ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
 657     if (HAS_PENDING_EXCEPTION) {
 658       CLEAR_PENDING_EXCEPTION;
 659       record_out_of_memory_failure();
 660       return ciConstant();
 661     }
 662     assert (klass->is_instance_klass() || klass->is_array_klass(),
 663             "must be an instance or array klass ");
 664     return ciConstant(T_OBJECT, klass->java_mirror());
 665   } else if (tag.is_method_type()) {
 666     // must execute Java code to link this CP entry into cache[i].f1
 667     ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
 668     ciObject* ciobj = get_unloaded_method_type_constant(signature);
 669     return ciConstant(T_OBJECT, ciobj);
 670   } else if (tag.is_method_handle()) {
 671     // must execute Java code to link this CP entry into cache[i].f1
 672     int ref_kind        = cpool->method_handle_ref_kind_at(index);
 673     int callee_index    = cpool->method_handle_klass_index_at(index);
 674     ciKlass* callee     = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
 675     ciSymbol* name      = get_symbol(cpool->method_handle_name_ref_at(index));
 676     ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
 677     ciObject* ciobj     = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
 678     return ciConstant(T_OBJECT, ciobj);
 679   } else if (tag.is_dynamic_constant()) {
 680     return ciConstant();
 681   } else {
 682     ShouldNotReachHere();
 683     return ciConstant();
 684   }
 685 }
 686 
 687 // ------------------------------------------------------------------
 688 // ciEnv::get_constant_by_index
 689 //
 690 // Pull a constant out of the constant pool.  How appropriate.
 691 //
 692 // Implementation note: this query is currently in no way cached.
 693 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool,
 694                                         int pool_index, int cache_index,
 695                                         ciInstanceKlass* accessor) {
 696   GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
 697 }
 698 
 699 // ------------------------------------------------------------------
 700 // ciEnv::get_field_by_index_impl
 701 //
 702 // Implementation of get_field_by_index.
 703 //
 704 // Implementation note: the results of field lookups are cached
 705 // in the accessor klass.
 706 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
 707                                         int index) {
 708   ciConstantPoolCache* cache = accessor->field_cache();
 709   if (cache == NULL) {
 710     ciField* field = new (arena()) ciField(accessor, index);
 711     return field;
 712   } else {
 713     ciField* field = (ciField*)cache->get(index);
 714     if (field == NULL) {
 715       field = new (arena()) ciField(accessor, index);
 716       cache->insert(index, field);
 717     }
 718     return field;
 719   }
 720 }
 721 
 722 // ------------------------------------------------------------------
 723 // ciEnv::get_field_by_index
 724 //
 725 // Get a field by index from a klass's constant pool.
 726 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
 727                                    int index) {
 728   GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
 729 }
 730 
 731 // ------------------------------------------------------------------
 732 // ciEnv::lookup_method
 733 //
 734 // Perform an appropriate method lookup based on accessor, holder,
 735 // name, signature, and bytecode.
 736 Method* ciEnv::lookup_method(ciInstanceKlass* accessor,
 737                              ciKlass*         holder,
 738                              Symbol*          name,
 739                              Symbol*          sig,
 740                              Bytecodes::Code  bc,
 741                              constantTag      tag) {
 742   // Accessibility checks are performed in ciEnv::get_method_by_index_impl.
 743   assert(check_klass_accessibility(accessor, holder->get_Klass()), "holder not accessible");
 744 
 745   InstanceKlass* accessor_klass = accessor->get_instanceKlass();
 746   Klass* holder_klass = holder->get_Klass();
 747   methodHandle dest_method;
 748   LinkInfo link_info(holder_klass, name, sig, accessor_klass, LinkInfo::needs_access_check, tag);
 749   switch (bc) {
 750   case Bytecodes::_invokestatic:
 751     dest_method =
 752       LinkResolver::resolve_static_call_or_null(link_info);
 753     break;
 754   case Bytecodes::_invokespecial:
 755     dest_method =
 756       LinkResolver::resolve_special_call_or_null(link_info);
 757     break;
 758   case Bytecodes::_invokeinterface:
 759     dest_method =
 760       LinkResolver::linktime_resolve_interface_method_or_null(link_info);
 761     break;
 762   case Bytecodes::_invokevirtual:
 763     dest_method =
 764       LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
 765     break;
 766   default: ShouldNotReachHere();
 767   }
 768 
 769   return dest_method();
 770 }
 771 
 772 
 773 // ------------------------------------------------------------------
 774 // ciEnv::get_method_by_index_impl
 775 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 776                                           int index, Bytecodes::Code bc,
 777                                           ciInstanceKlass* accessor) {
 778   if (bc == Bytecodes::_invokedynamic) {
 779     ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
 780     bool is_resolved = !cpce->is_f1_null();
 781     // FIXME: code generation could allow for null (unlinked) call site
 782     // The call site could be made patchable as follows:
 783     // Load the appendix argument from the constant pool.
 784     // Test the appendix argument and jump to a known deopt routine if it is null.
 785     // Jump through a patchable call site, which is initially a deopt routine.
 786     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 787     // As with other two-component call sites, both values must be independently verified.
 788 
 789     if (is_resolved) {
 790       // Get the invoker Method* from the constant pool.
 791       // (The appendix argument, if any, will be noted in the method's signature.)
 792       Method* adapter = cpce->f1_as_method();
 793       return get_method(adapter);
 794     }
 795 
 796     // Fake a method that is equivalent to a declared method.
 797     ciInstanceKlass* holder    = get_instance_klass(SystemDictionary::MethodHandle_klass());
 798     ciSymbol*        name      = ciSymbol::invokeBasic_name();
 799     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index));
 800     return get_unloaded_method(holder, name, signature, accessor);
 801   } else {
 802     const int holder_index = cpool->klass_ref_index_at(index);
 803     bool holder_is_accessible;
 804     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 805 
 806     // Get the method's name and signature.
 807     Symbol* name_sym = cpool->name_ref_at(index);
 808     Symbol* sig_sym  = cpool->signature_ref_at(index);
 809 
 810     if (cpool->has_preresolution()
 811         || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) &&
 812             MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
 813       // Short-circuit lookups for JSR 292-related call sites.
 814       // That is, do not rely only on name-based lookups, because they may fail
 815       // if the names are not resolvable in the boot class loader (7056328).
 816       switch (bc) {
 817       case Bytecodes::_invokevirtual:
 818       case Bytecodes::_invokeinterface:
 819       case Bytecodes::_invokespecial:
 820       case Bytecodes::_invokestatic:
 821         {
 822           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 823           if (m != NULL) {
 824             return get_method(m);
 825           }
 826         }
 827         break;
 828       default:
 829         break;
 830       }
 831     }
 832 
 833     if (holder_is_accessible) {  // Our declared holder is loaded.
 834       constantTag tag = cpool->tag_ref_at(index);
 835       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 836       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 837       if (m != NULL &&
 838           (bc == Bytecodes::_invokestatic
 839            ?  m->method_holder()->is_not_initialized()
 840            : !m->method_holder()->is_loaded())) {
 841         m = NULL;
 842       }
 843 #ifdef ASSERT
 844       if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
 845         m = NULL;
 846       }
 847 #endif
 848       if (m != NULL) {
 849         // We found the method.
 850         return get_method(m);
 851       }
 852     }
 853 
 854     // Either the declared holder was not loaded, or the method could
 855     // not be found.  Create a dummy ciMethod to represent the failed
 856     // lookup.
 857     ciSymbol* name      = get_symbol(name_sym);
 858     ciSymbol* signature = get_symbol(sig_sym);
 859     return get_unloaded_method(holder, name, signature, accessor);
 860   }
 861 }
 862 
 863 
 864 // ------------------------------------------------------------------
 865 // ciEnv::get_instance_klass_for_declared_method_holder
 866 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
 867   // For the case of <array>.clone(), the method holder can be a ciArrayKlass
 868   // instead of a ciInstanceKlass.  For that case simply pretend that the
 869   // declared holder is Object.clone since that's where the call will bottom out.
 870   // A more correct fix would trickle out through many interfaces in CI,
 871   // requiring ciInstanceKlass* to become ciKlass* and many more places would
 872   // require checks to make sure the expected type was found.  Given that this
 873   // only occurs for clone() the more extensive fix seems like overkill so
 874   // instead we simply smear the array type into Object.
 875   guarantee(method_holder != NULL, "no method holder");
 876   if (method_holder->is_instance_klass()) {
 877     return method_holder->as_instance_klass();
 878   } else if (method_holder->is_array_klass()) {
 879     return current()->Object_klass();
 880   } else {
 881     ShouldNotReachHere();
 882   }
 883   return NULL;
 884 }
 885 
 886 
 887 // ------------------------------------------------------------------
 888 // ciEnv::get_method_by_index
 889 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool,
 890                                      int index, Bytecodes::Code bc,
 891                                      ciInstanceKlass* accessor) {
 892   GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
 893 }
 894 
 895 
 896 // ------------------------------------------------------------------
 897 // ciEnv::name_buffer
 898 char *ciEnv::name_buffer(int req_len) {
 899   if (_name_buffer_len < req_len) {
 900     if (_name_buffer == NULL) {
 901       _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
 902       _name_buffer_len = req_len;
 903     } else {
 904       _name_buffer =
 905         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 906       _name_buffer_len = req_len;
 907     }
 908   }
 909   return _name_buffer;
 910 }
 911 
 912 // ------------------------------------------------------------------
 913 // ciEnv::is_in_vm
 914 bool ciEnv::is_in_vm() {
 915   return JavaThread::current()->thread_state() == _thread_in_vm;
 916 }
 917 
 918 bool ciEnv::system_dictionary_modification_counter_changed() {
 919   return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
 920 }
 921 
 922 // ------------------------------------------------------------------
 923 // ciEnv::validate_compile_task_dependencies
 924 //
 925 // Check for changes during compilation (e.g. class loads, evolution,
 926 // breakpoints, call site invalidation).
 927 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 928   if (failing())  return;  // no need for further checks
 929 
 930   bool counter_changed = system_dictionary_modification_counter_changed();
 931   Dependencies::DepType result = dependencies()->validate_dependencies(_task, counter_changed);
 932   if (result != Dependencies::end_marker) {
 933     if (result == Dependencies::call_site_target_value) {
 934       _inc_decompile_count_on_failure = false;
 935       record_failure("call site target change");
 936     } else if (Dependencies::is_klass_type(result)) {
 937       record_failure("concurrent class loading");
 938     } else {
 939       record_failure("invalid non-klass dependency");
 940     }
 941   }
 942 }
 943 
 944 // ------------------------------------------------------------------
 945 // ciEnv::register_method
 946 void ciEnv::register_method(ciMethod* target,
 947                             int entry_bci,
 948                             CodeOffsets* offsets,
 949                             int orig_pc_offset,
 950                             CodeBuffer* code_buffer,
 951                             int frame_words,
 952                             OopMapSet* oop_map_set,
 953                             ExceptionHandlerTable* handler_table,
 954                             ImplicitExceptionTable* inc_table,
 955                             AbstractCompiler* compiler,
 956                             bool has_unsafe_access,
 957                             bool has_wide_vectors,
 958                             RTMState  rtm_state) {
 959   VM_ENTRY_MARK;
 960   nmethod* nm = NULL;
 961   {
 962     // To prevent compile queue updates.
 963     MutexLocker locker(MethodCompileQueue_lock, THREAD);
 964 
 965     // Prevent SystemDictionary::add_to_hierarchy from running
 966     // and invalidating our dependencies until we install this method.
 967     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
 968     MutexLocker ml(Compile_lock);
 969     NoSafepointVerifier nsv;
 970 
 971     // Change in Jvmti state may invalidate compilation.
 972     if (!failing() && jvmti_state_changed()) {
 973       record_failure("Jvmti state change invalidated dependencies");
 974     }
 975 
 976     // Change in DTrace flags may invalidate compilation.
 977     if (!failing() &&
 978         ( (!dtrace_extended_probes() && ExtendedDTraceProbes) ||
 979           (!dtrace_method_probes() && DTraceMethodProbes) ||
 980           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
 981       record_failure("DTrace flags change invalidated dependencies");
 982     }
 983 
 984     if (!failing()) {
 985       if (log() != NULL) {
 986         // Log the dependencies which this compilation declares.
 987         dependencies()->log_all_dependencies();
 988       }
 989 
 990       // Encode the dependencies now, so we can check them right away.
 991       dependencies()->encode_content_bytes();
 992 
 993       // Check for {class loads, evolution, breakpoints, ...} during compilation
 994       validate_compile_task_dependencies(target);
 995     }
 996 
 997     methodHandle method(THREAD, target->get_Method());
 998 
 999 #if INCLUDE_RTM_OPT
1000     if (!failing() && (rtm_state != NoRTM) &&
1001         (method()->method_data() != NULL) &&
1002         (method()->method_data()->rtm_state() != rtm_state)) {
1003       // Preemptive decompile if rtm state was changed.
1004       record_failure("RTM state change invalidated rtm code");
1005     }
1006 #endif
1007 
1008     if (failing()) {
1009       // While not a true deoptimization, it is a preemptive decompile.
1010       MethodData* mdo = method()->method_data();
1011       if (mdo != NULL && _inc_decompile_count_on_failure) {
1012         mdo->inc_decompile_count();
1013       }
1014 
1015       // All buffers in the CodeBuffer are allocated in the CodeCache.
1016       // If the code buffer is created on each compile attempt
1017       // as in C2, then it must be freed.
1018       code_buffer->free_blob();
1019       return;
1020     }
1021 
1022     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1023     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1024 
1025     nm =  nmethod::new_nmethod(method,
1026                                compile_id(),
1027                                entry_bci,
1028                                offsets,
1029                                orig_pc_offset,
1030                                debug_info(), dependencies(), code_buffer,
1031                                frame_words, oop_map_set,
1032                                handler_table, inc_table,
1033                                compiler, task()->comp_level());
1034 
1035     // Free codeBlobs
1036     code_buffer->free_blob();
1037 
1038     if (nm != NULL) {
1039       nm->set_has_unsafe_access(has_unsafe_access);
1040       nm->set_has_wide_vectors(has_wide_vectors);
1041 #if INCLUDE_RTM_OPT
1042       nm->set_rtm_state(rtm_state);
1043 #endif
1044 
1045       // Record successful registration.
1046       // (Put nm into the task handle *before* publishing to the Java heap.)
1047       if (task() != NULL) {
1048         task()->set_code(nm);
1049       }
1050 
1051       if (entry_bci == InvocationEntryBci) {
1052         if (TieredCompilation) {
1053           // If there is an old version we're done with it
1054           CompiledMethod* old = method->code();
1055           if (TraceMethodReplacement && old != NULL) {
1056             ResourceMark rm;
1057             char *method_name = method->name_and_sig_as_C_string();
1058             tty->print_cr("Replacing method %s", method_name);
1059           }
1060           if (old != NULL) {
1061             old->make_not_used();
1062           }
1063         }
1064         if (TraceNMethodInstalls) {
1065           ResourceMark rm;
1066           char *method_name = method->name_and_sig_as_C_string();
1067           ttyLocker ttyl;
1068           tty->print_cr("Installing method (%d) %s ",
1069                         task()->comp_level(),
1070                         method_name);
1071         }
1072         // Allow the code to be executed
1073         method->set_code(method, nm);
1074       } else {
1075         if (TraceNMethodInstalls) {
1076           ResourceMark rm;
1077           char *method_name = method->name_and_sig_as_C_string();
1078           ttyLocker ttyl;
1079           tty->print_cr("Installing osr method (%d) %s @ %d",
1080                         task()->comp_level(),
1081                         method_name,
1082                         entry_bci);
1083         }
1084         method->method_holder()->add_osr_nmethod(nm);
1085       }
1086       nm->make_in_use();
1087     }
1088   }  // safepoints are allowed again
1089 
1090   if (nm != NULL) {
1091     // JVMTI -- compiled method notification (must be done outside lock)
1092     nm->post_compiled_method_load_event();
1093   } else {
1094     // The CodeCache is full.
1095     record_failure("code cache is full");
1096   }
1097 }
1098 
1099 
1100 // ------------------------------------------------------------------
1101 // ciEnv::find_system_klass
1102 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1103   VM_ENTRY_MARK;
1104   return get_klass_by_name_impl(NULL, constantPoolHandle(), klass_name, false);
1105 }
1106 
1107 // ------------------------------------------------------------------
1108 // ciEnv::comp_level
1109 int ciEnv::comp_level() {
1110   if (task() == NULL)  return CompLevel_highest_tier;
1111   return task()->comp_level();
1112 }
1113 
1114 // ------------------------------------------------------------------
1115 // ciEnv::compile_id
1116 uint ciEnv::compile_id() {
1117   if (task() == NULL)  return 0;
1118   return task()->compile_id();
1119 }
1120 
1121 // ------------------------------------------------------------------
1122 // ciEnv::notice_inlined_method()
1123 void ciEnv::notice_inlined_method(ciMethod* method) {
1124   _num_inlined_bytecodes += method->code_size_for_inlining();
1125 }
1126 
1127 // ------------------------------------------------------------------
1128 // ciEnv::num_inlined_bytecodes()
1129 int ciEnv::num_inlined_bytecodes() const {
1130   return _num_inlined_bytecodes;
1131 }
1132 
1133 // ------------------------------------------------------------------
1134 // ciEnv::record_failure()
1135 void ciEnv::record_failure(const char* reason) {
1136   if (_failure_reason == NULL) {
1137     // Record the first failure reason.
1138     _failure_reason = reason;
1139   }
1140 }
1141 
1142 void ciEnv::report_failure(const char* reason) {
1143   EventCompilationFailure event;
1144   if (event.should_commit()) {
1145     event.set_compileId(compile_id());
1146     event.set_failureMessage(reason);
1147     event.commit();
1148   }
1149 }
1150 
1151 // ------------------------------------------------------------------
1152 // ciEnv::record_method_not_compilable()
1153 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
1154   int new_compilable =
1155     all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
1156 
1157   // Only note transitions to a worse state
1158   if (new_compilable > _compilable) {
1159     if (log() != NULL) {
1160       if (all_tiers) {
1161         log()->elem("method_not_compilable");
1162       } else {
1163         log()->elem("method_not_compilable_at_tier level='%d'",
1164                     current()->task()->comp_level());
1165       }
1166     }
1167     _compilable = new_compilable;
1168 
1169     // Reset failure reason; this one is more important.
1170     _failure_reason = NULL;
1171     record_failure(reason);
1172   }
1173 }
1174 
1175 // ------------------------------------------------------------------
1176 // ciEnv::record_out_of_memory_failure()
1177 void ciEnv::record_out_of_memory_failure() {
1178   // If memory is low, we stop compiling methods.
1179   record_method_not_compilable("out of memory");
1180 }
1181 
1182 ciInstance* ciEnv::unloaded_ciinstance() {
1183   GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
1184 }
1185 
1186 // ------------------------------------------------------------------
1187 // ciEnv::dump_replay_data*
1188 
1189 // Don't change thread state and acquire any locks.
1190 // Safe to call from VM error reporter.
1191 
1192 void ciEnv::dump_compile_data(outputStream* out) {
1193   CompileTask* task = this->task();
1194   if (task) {
1195     Method* method = task->method();
1196     int entry_bci = task->osr_bci();
1197     int comp_level = task->comp_level();
1198     out->print("compile %s %s %s %d %d",
1199                method->klass_name()->as_quoted_ascii(),
1200                method->name()->as_quoted_ascii(),
1201                method->signature()->as_quoted_ascii(),
1202                entry_bci, comp_level);
1203     if (compiler_data() != NULL) {
1204       if (is_c2_compile(comp_level)) {
1205 #ifdef COMPILER2
1206         // Dump C2 inlining data.
1207         ((Compile*)compiler_data())->dump_inline_data(out);
1208 #endif
1209       } else if (is_c1_compile(comp_level)) {
1210 #ifdef COMPILER1
1211         // Dump C1 inlining data.
1212         ((Compilation*)compiler_data())->dump_inline_data(out);
1213 #endif
1214       }
1215     }
1216     out->cr();
1217   }
1218 }
1219 
1220 void ciEnv::dump_replay_data_unsafe(outputStream* out) {
1221   ResourceMark rm;
1222 #if INCLUDE_JVMTI
1223   out->print_cr("JvmtiExport can_access_local_variables %d",     _jvmti_can_access_local_variables);
1224   out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
1225   out->print_cr("JvmtiExport can_post_on_exceptions %d",         _jvmti_can_post_on_exceptions);
1226 #endif // INCLUDE_JVMTI
1227 
1228   GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
1229   out->print_cr("# %d ciObject found", objects->length());
1230   for (int i = 0; i < objects->length(); i++) {
1231     objects->at(i)->dump_replay_data(out);
1232   }
1233   dump_compile_data(out);
1234   out->flush();
1235 }
1236 
1237 void ciEnv::dump_replay_data(outputStream* out) {
1238   GUARDED_VM_ENTRY(
1239     MutexLocker ml(Compile_lock);
1240     dump_replay_data_unsafe(out);
1241   )
1242 }
1243 
1244 void ciEnv::dump_replay_data(int compile_id) {
1245   static char buffer[O_BUFLEN];
1246   int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
1247   if (ret > 0) {
1248     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1249     if (fd != -1) {
1250       FILE* replay_data_file = os::open(fd, "w");
1251       if (replay_data_file != NULL) {
1252         fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1253         dump_replay_data(&replay_data_stream);
1254         tty->print_cr("# Compiler replay data is saved as: %s", buffer);
1255       } else {
1256         tty->print_cr("# Can't open file to dump replay data.");
1257       }
1258     }
1259   }
1260 }
1261 
1262 void ciEnv::dump_inline_data(int compile_id) {
1263   static char buffer[O_BUFLEN];
1264   int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
1265   if (ret > 0) {
1266     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1267     if (fd != -1) {
1268       FILE* inline_data_file = os::open(fd, "w");
1269       if (inline_data_file != NULL) {
1270         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1271         GUARDED_VM_ENTRY(
1272           MutexLocker ml(Compile_lock);
1273           dump_compile_data(&replay_data_stream);
1274         )
1275         replay_data_stream.flush();
1276         tty->print("# Compiler inline data is saved as: ");
1277         tty->print_cr("%s", buffer);
1278       } else {
1279         tty->print_cr("# Can't open file to dump inline data.");
1280       }
1281     }
1282   }
1283 }