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