1 /*
   2  * Copyright (c) 2016, 2017, 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 
  26 #include "aot/aotCodeHeap.hpp"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/javaAssertions.hpp"
  29 #include "gc/shared/cardTable.hpp"
  30 #include "gc/shared/cardTableModRefBS.hpp"
  31 #include "gc/g1/heapRegion.hpp"
  32 #include "gc/shared/gcLocker.hpp"
  33 #include "interpreter/abstractInterpreter.hpp"
  34 #include "jvmci/compilerRuntime.hpp"
  35 #include "jvmci/jvmciRuntime.hpp"
  36 #include "oops/method.hpp"
  37 #include "runtime/os.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/vm_operations.hpp"
  40 
  41 bool AOTLib::_narrow_oop_shift_initialized = false;
  42 int  AOTLib::_narrow_oop_shift = 0;
  43 int  AOTLib::_narrow_klass_shift = 0;
  44 
  45 address AOTLib::load_symbol(const char *name) {
  46   address symbol = (address) os::dll_lookup(_dl_handle, name);
  47   if (symbol == NULL) {
  48     tty->print_cr("Shared file %s error: missing %s", _name, name);
  49     vm_exit(1);
  50   }
  51   return symbol;
  52 }
  53 
  54 Klass* AOTCodeHeap::get_klass_from_got(const char* klass_name, int klass_len, const Method* method) {
  55   AOTKlassData* klass_data = (AOTKlassData*)_lib->load_symbol(klass_name);
  56   Klass* k = (Klass*)_metaspace_got[klass_data->_got_index];
  57   if (k == NULL) {
  58     Thread* thread = Thread::current();
  59     k = lookup_klass(klass_name, klass_len, method, thread);
  60     // Note, exceptions are cleared.
  61     if (k == NULL) {
  62       fatal("Shared file %s error: klass %s should be resolved already", _lib->name(), klass_name);
  63       vm_exit(1);
  64     }
  65     _metaspace_got[klass_data->_got_index] = k;
  66   }
  67   return k;
  68 }
  69 
  70 Klass* AOTCodeHeap::lookup_klass(const char* name, int len, const Method* method, Thread* thread) {
  71   ResourceMark rm(thread);
  72   assert(method != NULL, "incorrect call parameter");
  73   methodHandle caller(thread, (Method*)method);
  74 
  75   // Use class loader of aot method.
  76   Handle loader(thread, caller->method_holder()->class_loader());
  77   Handle protection_domain(thread, caller->method_holder()->protection_domain());
  78 
  79   // Ignore wrapping L and ;
  80   if (name[0] == 'L') {
  81     assert(len > 2, "small name %s", name);
  82     name++;
  83     len -= 2;
  84   }
  85   TempNewSymbol sym = SymbolTable::probe(name, len);
  86   if (sym == NULL) {
  87     log_debug(aot, class, resolve)("Probe failed for AOT class %s", name);
  88     return NULL;
  89   }
  90   Klass* k = SystemDictionary::find_instance_or_array_klass(sym, loader, protection_domain, thread);
  91   assert(!thread->has_pending_exception(), "should not throw");
  92 
  93   if (k != NULL) {
  94     log_info(aot, class, resolve)("%s %s (lookup)", caller->method_holder()->external_name(), k->external_name());
  95   }
  96   return k;
  97 }
  98 
  99 void AOTLib::handle_config_error(const char* format, ...) {
 100   if (PrintAOT) {
 101     va_list ap;
 102     va_start(ap, format);
 103     tty->vprint_cr(format, ap);
 104     va_end(ap);
 105   }
 106   if (UseAOTStrictLoading) {
 107     vm_exit(1);
 108   }
 109   _valid = false;
 110 }
 111 
 112 void AOTLib::verify_flag(bool aot_flag, bool flag, const char* name) {
 113   if (_valid && aot_flag != flag) {
 114     handle_config_error("Shared file %s error: %s has different value '%s' from current '%s'", _name, name , (aot_flag ? "true" : "false"), (flag ? "true" : "false"));
 115   }
 116 }
 117 
 118 void AOTLib::verify_flag(int aot_flag, int flag, const char* name) {
 119   if (_valid && aot_flag != flag) {
 120     handle_config_error("Shared file %s error: %s has different value '%d' from current '%d'", _name, name , aot_flag, flag);
 121   }
 122 }
 123 
 124 void AOTLib::verify_config() {
 125   GrowableArray<AOTLib*>* libraries = AOTLoader::libraries();
 126   for (GrowableArrayIterator<AOTLib*> lib = libraries->begin(); lib != libraries->end(); ++lib) {
 127     if ((*lib)->_config == _config) {
 128       handle_config_error("AOT library %s already loaded.", (*lib)->_name);
 129       return;
 130     }
 131   }
 132 
 133   if (_header->_version != AOTHeader::AOT_SHARED_VERSION) {
 134     handle_config_error("Invalid version of the shared file %s. Expected %d but was %d", _name, _header->_version, AOTHeader::AOT_SHARED_VERSION);
 135     return;
 136   }
 137 
 138   const char* aot_jvm_version = (const char*)_header + _header->_jvm_version_offset + 2;
 139   if (strcmp(aot_jvm_version, VM_Version::jre_release_version()) != 0) {
 140     handle_config_error("JVM version '%s' recorded in the shared file %s does not match current version '%s'", aot_jvm_version, _name, VM_Version::jre_release_version());
 141     return;
 142   }
 143 
 144   // Debug VM has different layout of runtime and metadata structures
 145 #ifdef ASSERT
 146   verify_flag(_config->_debug_VM, true, "Debug VM version");
 147 #else
 148   verify_flag(!(_config->_debug_VM), true, "Product VM version");
 149 #endif
 150   // Check configuration size
 151   verify_flag(_config->_config_size, AOTConfiguration::CONFIG_SIZE, "AOT configuration size");
 152 
 153   // Check flags
 154   verify_flag(_config->_useCompressedOops, UseCompressedOops, "UseCompressedOops");
 155   verify_flag(_config->_useCompressedClassPointers, UseCompressedClassPointers, "UseCompressedClassPointers");
 156   verify_flag(_config->_useG1GC, UseG1GC, "UseG1GC");
 157   verify_flag(_config->_useCMSGC, UseConcMarkSweepGC, "UseConcMarkSweepGC");
 158   verify_flag(_config->_useTLAB, UseTLAB, "UseTLAB");
 159   verify_flag(_config->_useBiasedLocking, UseBiasedLocking, "UseBiasedLocking");
 160   verify_flag(_config->_objectAlignment, ObjectAlignmentInBytes, "ObjectAlignmentInBytes");
 161   verify_flag(_config->_contendedPaddingWidth, ContendedPaddingWidth, "ContendedPaddingWidth");
 162   verify_flag(_config->_fieldsAllocationStyle, FieldsAllocationStyle, "FieldsAllocationStyle");
 163   verify_flag(_config->_compactFields, CompactFields, "CompactFields");
 164   verify_flag(_config->_enableContended, EnableContended, "EnableContended");
 165   verify_flag(_config->_restrictContended, RestrictContended, "RestrictContended");
 166 
 167   if (!TieredCompilation && _config->_tieredAOT) {
 168     handle_config_error("Shared file %s error: Expected to run with tiered compilation on", _name);
 169   }
 170 
 171   // Shifts are static values which initialized by 0 until java heap initialization.
 172   // AOT libs are loaded before heap initialized so shift values are not set.
 173   // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded.
 174   // Set shifts value based on first AOT library config.
 175   if (UseCompressedOops && _valid) {
 176     if (!_narrow_oop_shift_initialized) {
 177       _narrow_oop_shift = _config->_narrowOopShift;
 178       if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set
 179         _narrow_klass_shift = _config->_narrowKlassShift;
 180       }
 181       _narrow_oop_shift_initialized = true;
 182     } else {
 183       verify_flag(_config->_narrowOopShift, _narrow_oop_shift, "aot_config->_narrowOopShift");
 184       if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set
 185         verify_flag(_config->_narrowKlassShift, _narrow_klass_shift, "aot_config->_narrowKlassShift");
 186       }
 187     }
 188   }
 189 }
 190 
 191 AOTLib::~AOTLib() {
 192   free((void*) _name);
 193 }
 194 
 195 AOTCodeHeap::~AOTCodeHeap() {
 196   if (_classes != NULL) {
 197     FREE_C_HEAP_ARRAY(AOTClass, _classes);
 198   }
 199   if (_code_to_aot != NULL) {
 200     FREE_C_HEAP_ARRAY(CodeToAMethod, _code_to_aot);
 201   }
 202 }
 203 
 204 AOTLib::AOTLib(void* handle, const char* name, int dso_id) : _valid(true), _dl_handle(handle), _dso_id(dso_id) {
 205   _name = (const char*) strdup(name);
 206 
 207   // Verify that VM runs with the same parameters as AOT tool.
 208   _config = (AOTConfiguration*) load_symbol("JVM.config");
 209   _header = (AOTHeader*) load_symbol("JVM.header");
 210 
 211   verify_config();
 212 
 213   if (!_valid && PrintAOT) {
 214       tty->print("%7d ", (int) tty->time_stamp().milliseconds());
 215       tty->print_cr("%4d     skipped %s  aot library", _dso_id, _name);
 216   }
 217 }
 218 
 219 AOTCodeHeap::AOTCodeHeap(AOTLib* lib) :
 220     CodeHeap("CodeHeap 'AOT'", CodeBlobType::AOT), _lib(lib), _classes(NULL), _code_to_aot(NULL) {
 221   assert(_lib->is_valid(), "invalid library");
 222 
 223   _lib_symbols_initialized = false;
 224   _aot_id = 0;
 225 
 226   _class_count = _lib->header()->_class_count;
 227   _method_count = _lib->header()->_method_count;
 228 
 229   // Collect metaspace info: names -> address in .got section
 230   _metaspace_names = (const char*) _lib->load_symbol("JVM.meta.names");
 231   _method_metadata =     (address) _lib->load_symbol("JVM.meth.metadata");
 232   _methods_offsets =     (address) _lib->load_symbol("JVM.methods.offsets");
 233   _klasses_offsets =     (address) _lib->load_symbol("JVM.kls.offsets");
 234   _dependencies    =     (address) _lib->load_symbol("JVM.kls.dependencies");
 235   _code_space      =     (address) _lib->load_symbol("JVM.text");
 236 
 237   // First cell is number of elements.
 238   _metaspace_got      = (Metadata**) _lib->load_symbol("JVM.meta.got");
 239   _metaspace_got_size = _lib->header()->_metaspace_got_size;
 240 
 241   _metadata_got      = (Metadata**) _lib->load_symbol("JVM.metadata.got");
 242   _metadata_got_size = _lib->header()->_metadata_got_size;
 243 
 244   _oop_got      = (oop*) _lib->load_symbol("JVM.oop.got");
 245   _oop_got_size = _lib->header()->_oop_got_size;
 246 
 247   // Collect stubs info
 248   _stubs_offsets = (int*) _lib->load_symbol("JVM.stubs.offsets");
 249 
 250   // code segments table
 251   _code_segments = (address) _lib->load_symbol("JVM.code.segments");
 252 
 253   // method state
 254   _method_state = (jlong*) _lib->load_symbol("JVM.meth.state");
 255 
 256   // Create a table for mapping classes
 257   _classes = NEW_C_HEAP_ARRAY(AOTClass, _class_count, mtCode);
 258   memset(_classes, 0, _class_count * sizeof(AOTClass));
 259 
 260   // Create table for searching AOTCompiledMethod based on pc.
 261   _code_to_aot = NEW_C_HEAP_ARRAY(CodeToAMethod, _method_count, mtCode);
 262   memset(_code_to_aot, 0, _method_count * sizeof(CodeToAMethod));
 263 
 264   _low_boundary = _code_space;
 265   _memory.set_low_boundary((char *)_code_space);
 266   _memory.set_high_boundary((char *)_code_space);
 267   _memory.set_low((char *)_code_space);
 268   _memory.set_high((char *)_code_space);
 269 
 270   _segmap.set_low_boundary((char *)_code_segments);
 271   _segmap.set_low((char *)_code_segments);
 272 
 273   _log2_segment_size = exact_log2(_lib->config()->_codeSegmentSize);
 274 
 275   // Register aot stubs
 276   register_stubs();
 277 
 278   if (PrintAOT || (PrintCompilation && PrintAOT)) {
 279     tty->print("%7d ", (int) tty->time_stamp().milliseconds());
 280     tty->print_cr("%4d     loaded    %s  aot library", _lib->id(), _lib->name());
 281   }
 282 }
 283 
 284 void AOTCodeHeap::publish_aot(const methodHandle& mh, AOTMethodData* method_data, int code_id) {
 285   // The method may be explicitly excluded by the user.
 286   // Or Interpreter uses an intrinsic for this method.
 287   if (CompilerOracle::should_exclude(mh) || !AbstractInterpreter::can_be_compiled(mh)) {
 288     return;
 289   }
 290 
 291   address code = method_data->_code;
 292   const char* name = method_data->_name;
 293   aot_metadata* meta = method_data->_meta;
 294 
 295   if (meta->scopes_pcs_begin() == meta->scopes_pcs_end()) {
 296     // When the AOT compiler compiles something big we fail to generate metadata
 297     // in CodeInstaller::gather_metadata. In that case the scopes_pcs_begin == scopes_pcs_end.
 298     // In all successful cases we always have 2 entries of scope pcs.
 299     log_info(aot, class, resolve)("Failed to load %s (no metadata available)", mh->name_and_sig_as_C_string());
 300     _code_to_aot[code_id]._state = invalid;
 301     return;
 302   }
 303 
 304   jlong* state_adr = &_method_state[code_id];
 305   address metadata_table = method_data->_metadata_table;
 306   int metadata_size = method_data->_metadata_size;
 307   assert(code_id < _method_count, "sanity");
 308   _aot_id++;
 309 
 310 #ifdef ASSERT
 311   if (_aot_id > CIStop || _aot_id < CIStart) {
 312     // Skip compilation
 313     return;
 314   }
 315 #endif
 316   // Check one more time.
 317   if (_code_to_aot[code_id]._state == invalid) {
 318     return;
 319   }
 320   AOTCompiledMethod *aot = new AOTCompiledMethod(code, mh(), meta, metadata_table, metadata_size, state_adr, this, name, code_id, _aot_id);
 321   assert(_code_to_aot[code_id]._aot == NULL, "should be not initialized");
 322   _code_to_aot[code_id]._aot = aot; // Should set this first
 323   if (Atomic::cmpxchg(in_use, (jint*)&_code_to_aot[code_id]._state, not_set) != not_set) {
 324     _code_to_aot[code_id]._aot = NULL; // Clean
 325   } else { // success
 326     // Publish method
 327 #ifdef TIERED
 328     mh->set_aot_code(aot);
 329 #endif
 330     Method::set_code(mh, aot);
 331     if (PrintAOT || (PrintCompilation && PrintAOT)) {
 332       aot->print_on(tty, NULL);
 333     }
 334     // Publish oop only after we are visible to CompiledMethodIterator
 335     aot->set_oop(mh()->method_holder()->klass_holder());
 336   }
 337 }
 338 
 339 void AOTCodeHeap::link_primitive_array_klasses() {
 340   ResourceMark rm;
 341   for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
 342     BasicType t = (BasicType)i;
 343     if (is_java_primitive(t)) {
 344       const Klass* arr_klass = Universe::typeArrayKlassObj(t);
 345       AOTKlassData* klass_data = (AOTKlassData*) os::dll_lookup(_lib->dl_handle(), arr_klass->signature_name());
 346       if (klass_data != NULL) {
 347         // Set both GOT cells, resolved and initialized klass pointers.
 348         // _got_index points to second cell - resolved klass pointer.
 349         _metaspace_got[klass_data->_got_index-1] = (Metadata*)arr_klass; // Initialized
 350         _metaspace_got[klass_data->_got_index  ] = (Metadata*)arr_klass; // Resolved
 351         if (PrintAOT) {
 352           tty->print_cr("[Found  %s  in  %s]", arr_klass->internal_name(), _lib->name());
 353         }
 354       }
 355     }
 356   }
 357 }
 358 
 359 void AOTCodeHeap::register_stubs() {
 360   int stubs_count = _stubs_offsets[0]; // contains number
 361   _stubs_offsets++;
 362   AOTMethodOffsets* stub_offsets = (AOTMethodOffsets*)_stubs_offsets;
 363   for (int i = 0; i < stubs_count; ++i) {
 364     const char* stub_name = _metaspace_names + stub_offsets[i]._name_offset;
 365     address entry = _code_space  + stub_offsets[i]._code_offset;
 366     aot_metadata* meta = (aot_metadata *) (_method_metadata + stub_offsets[i]._meta_offset);
 367     address metadata_table = (address)_metadata_got + stub_offsets[i]._metadata_got_offset;
 368     int metadata_size = stub_offsets[i]._metadata_got_size;
 369     int code_id = stub_offsets[i]._code_id;
 370     assert(code_id < _method_count, "sanity");
 371     jlong* state_adr = &_method_state[code_id];
 372     int len = build_u2_from((address)stub_name);
 373     stub_name += 2;
 374     char* full_name = NEW_C_HEAP_ARRAY(char, len+5, mtCode);
 375     if (full_name == NULL) { // No memory?
 376       break;
 377     }
 378     memcpy(full_name, "AOT ", 4);
 379     memcpy(full_name+4, stub_name, len);
 380     full_name[len+4] = 0;
 381     guarantee(_code_to_aot[code_id]._state != invalid, "stub %s can't be invalidated", full_name);
 382     AOTCompiledMethod* aot = new AOTCompiledMethod(entry, NULL, meta, metadata_table, metadata_size, state_adr, this, full_name, code_id, i);
 383     assert(_code_to_aot[code_id]._aot  == NULL, "should be not initialized");
 384     _code_to_aot[code_id]._aot  = aot;
 385     if (Atomic::cmpxchg(in_use, (jint*)&_code_to_aot[code_id]._state, not_set) != not_set) {
 386       fatal("stab '%s' code state is %d", full_name, _code_to_aot[code_id]._state);
 387     }
 388     // Adjust code buffer boundaries only for stubs because they are last in the buffer.
 389     adjust_boundaries(aot);
 390     if (PrintAOT && Verbose) {
 391       aot->print_on(tty, NULL);
 392     }
 393   }
 394 }
 395 
 396 #define SET_AOT_GLOBAL_SYMBOL_VALUE(AOTSYMNAME, AOTSYMTYPE, VMSYMVAL) \
 397   {                                                                   \
 398     AOTSYMTYPE * adr = (AOTSYMTYPE *) os::dll_lookup(_lib->dl_handle(), AOTSYMNAME);  \
 399     /* Check for a lookup error */                                    \
 400     guarantee(adr != NULL, "AOT Symbol not found %s", AOTSYMNAME);    \
 401     *adr = (AOTSYMTYPE) VMSYMVAL;                                     \
 402   }
 403 
 404 void AOTCodeHeap::link_graal_runtime_symbols()  {
 405     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_monitorenter", address, JVMCIRuntime::monitorenter);
 406     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_monitorexit", address, JVMCIRuntime::monitorexit);
 407     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_object", address, JVMCIRuntime::log_object);
 408     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_printf", address, JVMCIRuntime::log_printf);
 409     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_primitive", address, JVMCIRuntime::log_primitive);
 410     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_instance", address, JVMCIRuntime::new_instance);
 411     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_array", address, JVMCIRuntime::new_array);
 412     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_multi_array", address, JVMCIRuntime::new_multi_array);
 413     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_dynamic_new_array", address, JVMCIRuntime::dynamic_new_array);
 414     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_validate_object", address, JVMCIRuntime::validate_object);
 415     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_pre", address, JVMCIRuntime::write_barrier_pre);
 416     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_identity_hash_code", address, JVMCIRuntime::identity_hash_code);
 417     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_post", address, JVMCIRuntime::write_barrier_post);
 418     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_dynamic_new_instance", address, JVMCIRuntime::dynamic_new_instance);
 419     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_thread_is_interrupted", address, JVMCIRuntime::thread_is_interrupted);
 420     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_exception_handler_for_pc", address, JVMCIRuntime::exception_handler_for_pc);
 421     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_test_deoptimize_call_int", address, JVMCIRuntime::test_deoptimize_call_int);
 422     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_and_post_jvmti_exception", address, JVMCIRuntime::throw_and_post_jvmti_exception);
 423     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_klass_external_name_exception", address, JVMCIRuntime::throw_klass_external_name_exception);
 424     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_class_cast_exception", address, JVMCIRuntime::throw_class_cast_exception);
 425     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_message", address, JVMCIRuntime::vm_message);
 426     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_error", address, JVMCIRuntime::vm_error);
 427 }
 428 
 429 void AOTCodeHeap::link_shared_runtime_symbols() {
 430     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_static_entry", address, SharedRuntime::get_resolve_static_call_stub());
 431     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_virtual_entry", address, SharedRuntime::get_resolve_virtual_call_stub());
 432     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_opt_virtual_entry", address, SharedRuntime::get_resolve_opt_virtual_call_stub());
 433     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_unpack", address, SharedRuntime::deopt_blob()->unpack());
 434     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_uncommon_trap", address, SharedRuntime::deopt_blob()->uncommon_trap());
 435     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_ic_miss_stub", address, SharedRuntime::get_ic_miss_stub());
 436     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_handle_wrong_method_stub", address, SharedRuntime::get_handle_wrong_method_stub());
 437     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_exception_handler_for_return_address", address, SharedRuntime::exception_handler_for_return_address);
 438     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_register_finalizer", address, SharedRuntime::register_finalizer);
 439     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_OSR_migration_end", address, SharedRuntime::OSR_migration_end);
 440     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_string_by_symbol", address, CompilerRuntime::resolve_string_by_symbol);
 441     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_klass_by_symbol", address, CompilerRuntime::resolve_klass_by_symbol);
 442     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_method_by_symbol_and_load_counters", address, CompilerRuntime::resolve_method_by_symbol_and_load_counters);
 443     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_initialize_klass_by_symbol", address, CompilerRuntime::initialize_klass_by_symbol);
 444     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_invocation_event", address, CompilerRuntime::invocation_event);
 445     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_backedge_event", address, CompilerRuntime::backedge_event);
 446 
 447     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dpow", address, SharedRuntime::dpow);
 448     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dexp", address, SharedRuntime::dexp);
 449     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dcos", address, SharedRuntime::dcos);
 450     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dsin", address, SharedRuntime::dsin);
 451     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dtan", address, SharedRuntime::dtan);
 452     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog", address, SharedRuntime::dlog);
 453     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog10", address, SharedRuntime::dlog10);
 454 }
 455 
 456 void AOTCodeHeap::link_stub_routines_symbols() {
 457     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_arraycopy", address, StubRoutines::_jbyte_arraycopy);
 458     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_arraycopy", address, StubRoutines::_jshort_arraycopy);
 459     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_arraycopy", address, StubRoutines::_jint_arraycopy);
 460     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_arraycopy", address, StubRoutines::_jlong_arraycopy);
 461     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy", address, StubRoutines::_oop_arraycopy);
 462     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy_uninit", address, StubRoutines::_oop_arraycopy_uninit);
 463 
 464     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_disjoint_arraycopy", address, StubRoutines::_jbyte_disjoint_arraycopy);
 465     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_disjoint_arraycopy", address, StubRoutines::_jshort_disjoint_arraycopy);
 466     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_disjoint_arraycopy", address, StubRoutines::_jint_disjoint_arraycopy);
 467     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_disjoint_arraycopy", address, StubRoutines::_jlong_disjoint_arraycopy);
 468     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy", address, StubRoutines::_oop_disjoint_arraycopy);
 469     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy_uninit", address, StubRoutines::_oop_disjoint_arraycopy_uninit);
 470 
 471     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_arraycopy", address, StubRoutines::_arrayof_jbyte_arraycopy);
 472     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_arraycopy", address, StubRoutines::_arrayof_jshort_arraycopy);
 473     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_arraycopy", address, StubRoutines::_arrayof_jint_arraycopy);
 474     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_arraycopy", address, StubRoutines::_arrayof_jlong_arraycopy);
 475     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy", address, StubRoutines::_arrayof_oop_arraycopy);
 476     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy_uninit", address, StubRoutines::_arrayof_oop_arraycopy_uninit);
 477 
 478     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_disjoint_arraycopy", address, StubRoutines::_arrayof_jbyte_disjoint_arraycopy);
 479     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_disjoint_arraycopy", address, StubRoutines::_arrayof_jshort_disjoint_arraycopy);
 480     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_disjoint_arraycopy", address, StubRoutines::_arrayof_jint_disjoint_arraycopy);
 481     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_disjoint_arraycopy", address, StubRoutines::_arrayof_jlong_disjoint_arraycopy);
 482     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy", address, StubRoutines::_arrayof_oop_disjoint_arraycopy);
 483     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy_uninit", address, StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit);
 484 
 485     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_checkcast_arraycopy", address, StubRoutines::_checkcast_arraycopy);
 486 
 487     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_encryptBlock", address, StubRoutines::_aescrypt_encryptBlock);
 488     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_decryptBlock", address, StubRoutines::_aescrypt_decryptBlock);
 489     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_encryptAESCrypt", address, StubRoutines::_cipherBlockChaining_encryptAESCrypt);
 490     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_decryptAESCrypt", address, StubRoutines::_cipherBlockChaining_decryptAESCrypt);
 491     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_update_bytes_crc32", address, StubRoutines::_updateBytesCRC32);
 492     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc_table_adr", address, StubRoutines::_crc_table_adr);
 493 
 494 
 495     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompress", address, StubRoutines::_sha1_implCompress);
 496     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompressMB", address, StubRoutines::_sha1_implCompressMB);
 497     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompress", address, StubRoutines::_sha256_implCompress);
 498     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompressMB", address, StubRoutines::_sha256_implCompressMB);
 499     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompress", address, StubRoutines::_sha512_implCompress);
 500     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompressMB", address, StubRoutines::_sha512_implCompressMB);
 501     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_multiplyToLen", address, StubRoutines::_multiplyToLen);
 502 
 503     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_counterMode_AESCrypt", address, StubRoutines::_counterMode_AESCrypt);
 504     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_ghash_processBlocks", address, StubRoutines::_ghash_processBlocks);
 505     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc32c_table_addr", address, StubRoutines::_crc32c_table_addr);
 506     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesCRC32C", address, StubRoutines::_updateBytesCRC32C);
 507     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesAdler32", address, StubRoutines::_updateBytesAdler32);
 508     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_squareToLen", address, StubRoutines::_squareToLen);
 509     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_mulAdd", address, StubRoutines::_mulAdd);
 510     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomeryMultiply",  address, StubRoutines::_montgomeryMultiply);
 511     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomerySquare", address, StubRoutines::_montgomerySquare);
 512     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_vectorizedMismatch", address, StubRoutines::_vectorizedMismatch);
 513 
 514     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_throw_delayed_StackOverflowError_entry", address, StubRoutines::_throw_delayed_StackOverflowError_entry);
 515 
 516 }
 517 
 518 void AOTCodeHeap::link_os_symbols() {
 519     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeMillis", address, os::javaTimeMillis);
 520     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeNanos", address, os::javaTimeNanos);
 521 }
 522 
 523 /*
 524  * Link any global symbols in precompiled DSO with dlopen() _dl_handle
 525  * dso_handle.
 526  */
 527 
 528 void AOTCodeHeap::link_global_lib_symbols() {
 529   if (!_lib_symbols_initialized) {
 530     _lib_symbols_initialized = true;
 531 
 532     CollectedHeap* heap = Universe::heap();
 533     CardTableModRefBS* ct = (CardTableModRefBS*)(heap->barrier_set());
 534     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_card_table_address", address, ct->card_table()->byte_map_base());
 535     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_top_address", address, (heap->supports_inline_contig_alloc() ? heap->top_addr() : NULL));
 536     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_end_address", address, (heap->supports_inline_contig_alloc() ? heap->end_addr() : NULL));
 537     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_polling_page", address, os::get_polling_page());
 538     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_klass_base_address", address, Universe::narrow_klass_base());
 539     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_oop_base_address", address, Universe::narrow_oop_base());
 540     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_log_of_heap_region_grain_bytes", int, HeapRegion::LogOfHRGrainBytes);
 541     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_inline_contiguous_allocation_supported", bool, heap->supports_inline_contig_alloc());
 542     link_shared_runtime_symbols();
 543     link_stub_routines_symbols();
 544     link_os_symbols();
 545     link_graal_runtime_symbols();
 546 
 547     // Link primitive array klasses.
 548     link_primitive_array_klasses();
 549   }
 550 }
 551 
 552 #ifndef PRODUCT
 553 int AOTCodeHeap::klasses_seen = 0;
 554 int AOTCodeHeap::aot_klasses_found = 0;
 555 int AOTCodeHeap::aot_klasses_fp_miss = 0;
 556 int AOTCodeHeap::aot_klasses_cl_miss = 0;
 557 int AOTCodeHeap::aot_methods_found = 0;
 558 
 559 void AOTCodeHeap::print_statistics() {
 560   tty->print_cr("Classes seen: %d  AOT classes found: %d  AOT methods found: %d", klasses_seen, aot_klasses_found, aot_methods_found);
 561   tty->print_cr("AOT fingerprint mismatches: %d  AOT class loader mismatches: %d", aot_klasses_fp_miss, aot_klasses_cl_miss);
 562 }
 563 #endif
 564 
 565 Method* AOTCodeHeap::find_method(Klass* klass, Thread* thread, const char* method_name) {
 566   int method_name_len = build_u2_from((address)method_name);
 567   method_name += 2;
 568   const char* signature_name = method_name + method_name_len;
 569   int signature_name_len = build_u2_from((address)signature_name);
 570   signature_name += 2;
 571   // The class should have been loaded so the method and signature should already be
 572   // in the symbol table.  If they're not there, the method doesn't exist.
 573   TempNewSymbol name = SymbolTable::probe(method_name, method_name_len);
 574   TempNewSymbol signature = SymbolTable::probe(signature_name, signature_name_len);
 575 
 576   Method* m;
 577   if (name == NULL || signature == NULL) {
 578     m = NULL;
 579   } else if (name == vmSymbols::object_initializer_name() ||
 580              name == vmSymbols::class_initializer_name()) {
 581     // Never search superclasses for constructors
 582     if (klass->is_instance_klass()) {
 583       m = InstanceKlass::cast(klass)->find_method(name, signature);
 584     } else {
 585       m = NULL;
 586     }
 587   } else {
 588     m = klass->lookup_method(name, signature);
 589     if (m == NULL && klass->is_instance_klass()) {
 590       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
 591     }
 592   }
 593   if (m == NULL) {
 594     // Fatal error because we assume classes and methods should not be changed since aot compilation.
 595     const char* klass_name = klass->external_name();
 596     int klass_len = (int)strlen(klass_name);
 597     char* meta_name = NEW_RESOURCE_ARRAY(char, klass_len + 1 + method_name_len + signature_name_len + 1);
 598     memcpy(meta_name, klass_name, klass_len);
 599     meta_name[klass_len] = '.';
 600     memcpy(&meta_name[klass_len + 1], method_name, method_name_len);
 601     memcpy(&meta_name[klass_len + 1 + method_name_len], signature_name, signature_name_len);
 602     meta_name[klass_len + 1 + method_name_len + signature_name_len] = '\0';
 603     Handle exception = Exceptions::new_exception(thread, vmSymbols::java_lang_NoSuchMethodError(), meta_name);
 604     java_lang_Throwable::print(exception(), tty);
 605     tty->cr();
 606     java_lang_Throwable::print_stack_trace(exception, tty);
 607     tty->cr();
 608     fatal("Failed to find method '%s'", meta_name);
 609   }
 610   NOT_PRODUCT( aot_methods_found++; )
 611   return m;
 612 }
 613 
 614 AOTKlassData* AOTCodeHeap::find_klass(InstanceKlass* ik) {
 615   ResourceMark rm;
 616   AOTKlassData* klass_data = (AOTKlassData*) os::dll_lookup(_lib->dl_handle(), ik->signature_name());
 617   return klass_data;
 618 }
 619 
 620 bool AOTCodeHeap::is_dependent_method(Klass* dependee, AOTCompiledMethod* aot) {
 621   InstanceKlass *dependee_ik = InstanceKlass::cast(dependee);
 622   AOTKlassData* klass_data = find_klass(dependee_ik);
 623   if (klass_data == NULL) {
 624     return false; // no AOT records for this class - no dependencies
 625   }
 626   if (!dependee_ik->has_passed_fingerprint_check()) {
 627     return false; // different class
 628   }
 629 
 630   int methods_offset = klass_data->_dependent_methods_offset;
 631   if (methods_offset >= 0) {
 632     address methods_cnt_adr = _dependencies + methods_offset;
 633     int methods_cnt = *(int*)methods_cnt_adr;
 634     int* indexes = (int*)(methods_cnt_adr + 4);
 635     for (int i = 0; i < methods_cnt; ++i) {
 636       int code_id = indexes[i];
 637       if (_code_to_aot[code_id]._aot == aot) {
 638         return true; // found dependent method
 639       }
 640     }
 641   }
 642   return false;
 643 }
 644 
 645 void AOTCodeHeap::sweep_dependent_methods(AOTKlassData* klass_data) {
 646   // Make dependent methods non_entrant forever.
 647   int methods_offset = klass_data->_dependent_methods_offset;
 648   if (methods_offset >= 0) {
 649     int marked = 0;
 650     address methods_cnt_adr = _dependencies + methods_offset;
 651     int methods_cnt = *(int*)methods_cnt_adr;
 652     int* indexes = (int*)(methods_cnt_adr + 4);
 653     for (int i = 0; i < methods_cnt; ++i) {
 654       int code_id = indexes[i];
 655       // Invalidate aot code.
 656       if (Atomic::cmpxchg(invalid, (jint*)&_code_to_aot[code_id]._state, not_set) != not_set) {
 657         if (_code_to_aot[code_id]._state == in_use) {
 658           AOTCompiledMethod* aot = _code_to_aot[code_id]._aot;
 659           assert(aot != NULL, "aot should be set");
 660           if (!aot->is_runtime_stub()) { // Something is wrong - should not invalidate stubs.
 661             aot->mark_for_deoptimization(false);
 662             marked++;
 663           }
 664         }
 665       }
 666     }
 667     if (marked > 0) {
 668       VM_Deoptimize op;
 669       VMThread::execute(&op);
 670     }
 671   }
 672 }
 673 
 674 bool AOTCodeHeap::load_klass_data(InstanceKlass* ik, Thread* thread) {
 675   ResourceMark rm;
 676 
 677   NOT_PRODUCT( klasses_seen++; )
 678 
 679   AOTKlassData* klass_data = find_klass(ik);
 680   if (klass_data == NULL) {
 681     return false;
 682   }
 683 
 684   if (!ik->has_passed_fingerprint_check()) {
 685     NOT_PRODUCT( aot_klasses_fp_miss++; )
 686     log_trace(aot, class, fingerprint)("class  %s%s  has bad fingerprint in  %s tid=" INTPTR_FORMAT,
 687                                    ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 688                                    _lib->name(), p2i(thread));
 689     sweep_dependent_methods(klass_data);
 690     return false;
 691   }
 692 
 693   if (ik->has_been_redefined()) {
 694     log_trace(aot, class, load)("class  %s%s in %s  has been redefined tid=" INTPTR_FORMAT,
 695                                    ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 696                                    _lib->name(), p2i(thread));
 697     sweep_dependent_methods(klass_data);
 698     return false;
 699   }
 700 
 701   assert(klass_data->_class_id < _class_count, "invalid class id");
 702   AOTClass* aot_class = &_classes[klass_data->_class_id];
 703   if (aot_class->_classloader != NULL && aot_class->_classloader != ik->class_loader_data()) {
 704     log_trace(aot, class, load)("class  %s  in  %s already loaded for classloader %p vs %p tid=" INTPTR_FORMAT,
 705                              ik->internal_name(), _lib->name(), aot_class->_classloader, ik->class_loader_data(), p2i(thread));
 706     NOT_PRODUCT( aot_klasses_cl_miss++; )
 707     return false;
 708   }
 709 
 710   if (_lib->config()->_omitAssertions && JavaAssertions::enabled(ik->name()->as_C_string(), ik->class_loader() == NULL)) {
 711     log_trace(aot, class, load)("class  %s  in  %s does not have java assertions in compiled code, but assertions are enabled for this execution.", ik->internal_name(), _lib->name());
 712     sweep_dependent_methods(klass_data);
 713     return false;
 714   }
 715 
 716   NOT_PRODUCT( aot_klasses_found++; )
 717 
 718   log_trace(aot, class, load)("found  %s  in  %s for classloader %p tid=" INTPTR_FORMAT, ik->internal_name(), _lib->name(), ik->class_loader_data(), p2i(thread));
 719 
 720   aot_class->_classloader = ik->class_loader_data();
 721   // Set klass's Resolve (second) got cell.
 722   _metaspace_got[klass_data->_got_index] = ik;
 723 
 724   // Initialize global symbols of the DSO to the corresponding VM symbol values.
 725   link_global_lib_symbols();
 726 
 727   int methods_offset = klass_data->_compiled_methods_offset;
 728   if (methods_offset >= 0) {
 729     address methods_cnt_adr = _methods_offsets + methods_offset;
 730     int methods_cnt = *(int*)methods_cnt_adr;
 731     // Collect data about compiled methods
 732     AOTMethodData* methods_data = NEW_RESOURCE_ARRAY(AOTMethodData, methods_cnt);
 733     AOTMethodOffsets* methods_offsets = (AOTMethodOffsets*)(methods_cnt_adr + 4);
 734     for (int i = 0; i < methods_cnt; ++i) {
 735       AOTMethodOffsets* method_offsets = &methods_offsets[i];
 736       int code_id = method_offsets->_code_id;
 737       if (_code_to_aot[code_id]._state == invalid) {
 738         continue; // skip AOT methods slots which have been invalidated
 739       }
 740       AOTMethodData* method_data = &methods_data[i];
 741       const char* aot_name = _metaspace_names + method_offsets->_name_offset;
 742       method_data->_name = aot_name;
 743       method_data->_code = _code_space  + method_offsets->_code_offset;
 744       method_data->_meta = (aot_metadata*)(_method_metadata + method_offsets->_meta_offset);
 745       method_data->_metadata_table = (address)_metadata_got + method_offsets->_metadata_got_offset;
 746       method_data->_metadata_size  = method_offsets->_metadata_got_size;
 747       // aot_name format: "<u2_size>Ljava/lang/ThreadGroup;<u2_size>addUnstarted<u2_size>()V"
 748       int klass_len = build_u2_from((address)aot_name);
 749       const char* method_name = aot_name + 2 + klass_len;
 750       Method* m = AOTCodeHeap::find_method(ik, thread, method_name);
 751       methodHandle mh(thread, m);
 752       if (mh->code() != NULL) { // Does it have already compiled code?
 753         continue; // Don't overwrite
 754       }
 755       publish_aot(mh, method_data, code_id);
 756     }
 757   }
 758   return true;
 759 }
 760 
 761 AOTCompiledMethod* AOTCodeHeap::next_in_use_at(int start) const {
 762   for (int index = start; index < _method_count; index++) {
 763     if (_code_to_aot[index]._state != in_use) {
 764       continue; // Skip uninitialized entries.
 765     }
 766     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 767     return aot;
 768   }
 769   return NULL;
 770 }
 771 
 772 void* AOTCodeHeap::first() const {
 773   return next_in_use_at(0);
 774 }
 775 
 776 void* AOTCodeHeap::next(void* p) const {
 777   AOTCompiledMethod *aot = (AOTCompiledMethod *)p;
 778   int next_index = aot->method_index() + 1;
 779   assert(next_index <= _method_count, "");
 780   if (next_index == _method_count) {
 781     return NULL;
 782   }
 783   return next_in_use_at(next_index);
 784 }
 785 
 786 void* AOTCodeHeap::find_start(void* p) const {
 787   if (!contains(p)) {
 788     return NULL;
 789   }
 790   size_t offset = pointer_delta(p, low_boundary(), 1);
 791   // Use segments table
 792   size_t seg_idx = offset / _lib->config()->_codeSegmentSize;
 793   if ((int)(_code_segments[seg_idx]) == 0xff) {
 794     return NULL;
 795   }
 796   while (_code_segments[seg_idx] > 0) {
 797     seg_idx -= (int)_code_segments[seg_idx];
 798   }
 799   int code_offset = (int)seg_idx * _lib->config()->_codeSegmentSize;
 800   int aot_index = *(int*)(_code_space + code_offset);
 801   AOTCompiledMethod* aot = _code_to_aot[aot_index]._aot;
 802   assert(aot != NULL, "should find registered aot method");
 803   return aot;
 804 }
 805 
 806 AOTCompiledMethod* AOTCodeHeap::find_aot(address p) const {
 807   assert(contains(p), "should be here");
 808   return (AOTCompiledMethod *)find_start(p);
 809 }
 810 
 811 CodeBlob* AOTCodeHeap::find_blob_unsafe(void* start) const {
 812   return (CodeBlob*)AOTCodeHeap::find_start(start);
 813 }
 814 
 815 void AOTCodeHeap::oops_do(OopClosure* f) {
 816   for (int i = 0; i < _oop_got_size; i++) {
 817     oop* p = &_oop_got[i];
 818     if (*p == NULL)  continue;  // skip non-oops
 819     f->do_oop(p);
 820   }
 821   for (int index = 0; index < _method_count; index++) {
 822     if (_code_to_aot[index]._state != in_use) {
 823       continue; // Skip uninitialized entries.
 824     }
 825     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 826     aot->do_oops(f);
 827   }
 828 }
 829 
 830 // Scan only metaspace_got cells which should have only Klass*,
 831 // metadata_got cells are scanned only for alive AOT methods
 832 // by AOTCompiledMethod::metadata_do().
 833 void AOTCodeHeap::got_metadata_do(void f(Metadata*)) {
 834   for (int i = 1; i < _metaspace_got_size; i++) {
 835     Metadata** p = &_metaspace_got[i];
 836     Metadata* md = *p;
 837     if (md == NULL)  continue;  // skip non-oops
 838     if (Metaspace::contains(md)) {
 839       f(md);
 840     } else {
 841       intptr_t meta = (intptr_t)md;
 842       fatal("Invalid value in _metaspace_got[%d] = " INTPTR_FORMAT, i, meta);
 843     }
 844   }
 845 }
 846 
 847 void AOTCodeHeap::cleanup_inline_caches() {
 848   for (int index = 0; index < _method_count; index++) {
 849     if (_code_to_aot[index]._state != in_use) {
 850       continue; // Skip uninitialized entries.
 851     }
 852     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 853     aot->cleanup_inline_caches();
 854   }
 855 }
 856 
 857 #ifdef ASSERT
 858 int AOTCodeHeap::verify_icholder_relocations() {
 859   int count = 0;
 860   for (int index = 0; index < _method_count; index++) {
 861     if (_code_to_aot[index]._state != in_use) {
 862       continue; // Skip uninitialized entries.
 863     }
 864     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 865     count += aot->verify_icholder_relocations();
 866   }
 867   return count;
 868 }
 869 #endif
 870 
 871 void AOTCodeHeap::flush_evol_dependents_on(InstanceKlass* dependee) {
 872   for (int index = 0; index < _method_count; index++) {
 873     if (_code_to_aot[index]._state != in_use) {
 874       continue; // Skip uninitialized entries.
 875     }
 876     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 877     aot->flush_evol_dependents_on(dependee);
 878   }
 879 }
 880 
 881 void AOTCodeHeap::metadata_do(void f(Metadata*)) {
 882   for (int index = 0; index < _method_count; index++) {
 883     if (_code_to_aot[index]._state != in_use) {
 884       continue; // Skip uninitialized entries.
 885     }
 886     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 887     if (aot->_is_alive()) {
 888       aot->metadata_do(f);
 889     }
 890   }
 891   // Scan metaspace_got cells.
 892   got_metadata_do(f);
 893 }