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/g1/heapRegion.hpp"
  33 #include "gc/shared/gcLocker.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     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_pre", address, JVMCIRuntime::write_barrier_pre);
 425     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_identity_hash_code", address, JVMCIRuntime::identity_hash_code);
 426     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_post", address, JVMCIRuntime::write_barrier_post);
 427     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_dynamic_new_instance", address, JVMCIRuntime::dynamic_new_instance);
 428     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_thread_is_interrupted", address, JVMCIRuntime::thread_is_interrupted);
 429     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_exception_handler_for_pc", address, JVMCIRuntime::exception_handler_for_pc);
 430     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_test_deoptimize_call_int", address, JVMCIRuntime::test_deoptimize_call_int);
 431     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_and_post_jvmti_exception", address, JVMCIRuntime::throw_and_post_jvmti_exception);
 432     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_klass_external_name_exception", address, JVMCIRuntime::throw_klass_external_name_exception);
 433     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_class_cast_exception", address, JVMCIRuntime::throw_class_cast_exception);
 434     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_message", address, JVMCIRuntime::vm_message);
 435     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_error", address, JVMCIRuntime::vm_error);
 436 }
 437 
 438 void AOTCodeHeap::link_shared_runtime_symbols() {
 439     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_static_entry", address, SharedRuntime::get_resolve_static_call_stub());
 440     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_virtual_entry", address, SharedRuntime::get_resolve_virtual_call_stub());
 441     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_opt_virtual_entry", address, SharedRuntime::get_resolve_opt_virtual_call_stub());
 442     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_unpack", address, SharedRuntime::deopt_blob()->unpack());
 443     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_uncommon_trap", address, SharedRuntime::deopt_blob()->uncommon_trap());
 444     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_ic_miss_stub", address, SharedRuntime::get_ic_miss_stub());
 445     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_handle_wrong_method_stub", address, SharedRuntime::get_handle_wrong_method_stub());
 446     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_exception_handler_for_return_address", address, SharedRuntime::exception_handler_for_return_address);
 447     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_register_finalizer", address, SharedRuntime::register_finalizer);
 448     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_OSR_migration_end", address, SharedRuntime::OSR_migration_end);
 449     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_dynamic_invoke", address, CompilerRuntime::resolve_dynamic_invoke);
 450     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_string_by_symbol", address, CompilerRuntime::resolve_string_by_symbol);
 451     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_klass_by_symbol", address, CompilerRuntime::resolve_klass_by_symbol);
 452     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_method_by_symbol_and_load_counters", address, CompilerRuntime::resolve_method_by_symbol_and_load_counters);
 453     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_initialize_klass_by_symbol", address, CompilerRuntime::initialize_klass_by_symbol);
 454     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_invocation_event", address, CompilerRuntime::invocation_event);
 455     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_backedge_event", address, CompilerRuntime::backedge_event);
 456 
 457     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dpow", address, SharedRuntime::dpow);
 458     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dexp", address, SharedRuntime::dexp);
 459     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dcos", address, SharedRuntime::dcos);
 460     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dsin", address, SharedRuntime::dsin);
 461     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dtan", address, SharedRuntime::dtan);
 462     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog", address, SharedRuntime::dlog);
 463     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog10", address, SharedRuntime::dlog10);
 464 }
 465 
 466 void AOTCodeHeap::link_stub_routines_symbols() {
 467     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_arraycopy", address, StubRoutines::_jbyte_arraycopy);
 468     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_arraycopy", address, StubRoutines::_jshort_arraycopy);
 469     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_arraycopy", address, StubRoutines::_jint_arraycopy);
 470     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_arraycopy", address, StubRoutines::_jlong_arraycopy);
 471     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy", address, StubRoutines::_oop_arraycopy);
 472     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy_uninit", address, StubRoutines::_oop_arraycopy_uninit);
 473 
 474     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_disjoint_arraycopy", address, StubRoutines::_jbyte_disjoint_arraycopy);
 475     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_disjoint_arraycopy", address, StubRoutines::_jshort_disjoint_arraycopy);
 476     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_disjoint_arraycopy", address, StubRoutines::_jint_disjoint_arraycopy);
 477     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_disjoint_arraycopy", address, StubRoutines::_jlong_disjoint_arraycopy);
 478     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy", address, StubRoutines::_oop_disjoint_arraycopy);
 479     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy_uninit", address, StubRoutines::_oop_disjoint_arraycopy_uninit);
 480 
 481     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_arraycopy", address, StubRoutines::_arrayof_jbyte_arraycopy);
 482     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_arraycopy", address, StubRoutines::_arrayof_jshort_arraycopy);
 483     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_arraycopy", address, StubRoutines::_arrayof_jint_arraycopy);
 484     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_arraycopy", address, StubRoutines::_arrayof_jlong_arraycopy);
 485     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy", address, StubRoutines::_arrayof_oop_arraycopy);
 486     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy_uninit", address, StubRoutines::_arrayof_oop_arraycopy_uninit);
 487 
 488     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_disjoint_arraycopy", address, StubRoutines::_arrayof_jbyte_disjoint_arraycopy);
 489     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_disjoint_arraycopy", address, StubRoutines::_arrayof_jshort_disjoint_arraycopy);
 490     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_disjoint_arraycopy", address, StubRoutines::_arrayof_jint_disjoint_arraycopy);
 491     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_disjoint_arraycopy", address, StubRoutines::_arrayof_jlong_disjoint_arraycopy);
 492     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy", address, StubRoutines::_arrayof_oop_disjoint_arraycopy);
 493     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy_uninit", address, StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit);
 494 
 495     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_unsafe_arraycopy", address, StubRoutines::_unsafe_arraycopy);
 496 
 497     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_checkcast_arraycopy", address, StubRoutines::_checkcast_arraycopy);
 498 
 499     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_generic_arraycopy", address, StubRoutines::_generic_arraycopy);
 500 
 501     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_encryptBlock", address, StubRoutines::_aescrypt_encryptBlock);
 502     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_decryptBlock", address, StubRoutines::_aescrypt_decryptBlock);
 503     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_encryptAESCrypt", address, StubRoutines::_cipherBlockChaining_encryptAESCrypt);
 504     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_decryptAESCrypt", address, StubRoutines::_cipherBlockChaining_decryptAESCrypt);
 505     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_update_bytes_crc32", address, StubRoutines::_updateBytesCRC32);
 506     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc_table_adr", address, StubRoutines::_crc_table_adr);
 507 
 508 
 509     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompress", address, StubRoutines::_sha1_implCompress);
 510     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompressMB", address, StubRoutines::_sha1_implCompressMB);
 511     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompress", address, StubRoutines::_sha256_implCompress);
 512     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompressMB", address, StubRoutines::_sha256_implCompressMB);
 513     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompress", address, StubRoutines::_sha512_implCompress);
 514     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompressMB", address, StubRoutines::_sha512_implCompressMB);
 515     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_multiplyToLen", address, StubRoutines::_multiplyToLen);
 516 
 517     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_counterMode_AESCrypt", address, StubRoutines::_counterMode_AESCrypt);
 518     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_ghash_processBlocks", address, StubRoutines::_ghash_processBlocks);
 519     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc32c_table_addr", address, StubRoutines::_crc32c_table_addr);
 520     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesCRC32C", address, StubRoutines::_updateBytesCRC32C);
 521     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesAdler32", address, StubRoutines::_updateBytesAdler32);
 522     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_squareToLen", address, StubRoutines::_squareToLen);
 523     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_mulAdd", address, StubRoutines::_mulAdd);
 524     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomeryMultiply",  address, StubRoutines::_montgomeryMultiply);
 525     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomerySquare", address, StubRoutines::_montgomerySquare);
 526     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_vectorizedMismatch", address, StubRoutines::_vectorizedMismatch);
 527 
 528     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_throw_delayed_StackOverflowError_entry", address, StubRoutines::_throw_delayed_StackOverflowError_entry);
 529 
 530 }
 531 
 532 void AOTCodeHeap::link_os_symbols() {
 533     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeMillis", address, os::javaTimeMillis);
 534     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeNanos", address, os::javaTimeNanos);
 535 }
 536 
 537 /*
 538  * Link any global symbols in precompiled DSO with dlopen() _dl_handle
 539  * dso_handle.
 540  */
 541 
 542 void AOTCodeHeap::link_global_lib_symbols() {
 543   if (!_lib_symbols_initialized) {
 544     _lib_symbols_initialized = true;
 545 
 546     CollectedHeap* heap = Universe::heap();
 547     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_card_table_address", address, ci_card_table_address());
 548     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_top_address", address, (heap->supports_inline_contig_alloc() ? heap->top_addr() : NULL));
 549     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_end_address", address, (heap->supports_inline_contig_alloc() ? heap->end_addr() : NULL));
 550     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_polling_page", address, os::get_polling_page());
 551     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_klass_base_address", address, Universe::narrow_klass_base());
 552     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_oop_base_address", address, Universe::narrow_oop_base());
 553     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_log_of_heap_region_grain_bytes", int, HeapRegion::LogOfHRGrainBytes);
 554     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_inline_contiguous_allocation_supported", bool, heap->supports_inline_contig_alloc());
 555     link_shared_runtime_symbols();
 556     link_stub_routines_symbols();
 557     link_os_symbols();
 558     link_graal_runtime_symbols();
 559 
 560     // Link primitive array klasses.
 561     link_primitive_array_klasses();
 562   }
 563 }
 564 
 565 #ifndef PRODUCT
 566 int AOTCodeHeap::klasses_seen = 0;
 567 int AOTCodeHeap::aot_klasses_found = 0;
 568 int AOTCodeHeap::aot_klasses_fp_miss = 0;
 569 int AOTCodeHeap::aot_klasses_cl_miss = 0;
 570 int AOTCodeHeap::aot_methods_found = 0;
 571 
 572 void AOTCodeHeap::print_statistics() {
 573   tty->print_cr("Classes seen: %d  AOT classes found: %d  AOT methods found: %d", klasses_seen, aot_klasses_found, aot_methods_found);
 574   tty->print_cr("AOT fingerprint mismatches: %d  AOT class loader mismatches: %d", aot_klasses_fp_miss, aot_klasses_cl_miss);
 575 }
 576 #endif
 577 
 578 Method* AOTCodeHeap::find_method(Klass* klass, Thread* thread, const char* method_name) {
 579   int method_name_len = build_u2_from((address)method_name);
 580   method_name += 2;
 581   const char* signature_name = method_name + method_name_len;
 582   int signature_name_len = build_u2_from((address)signature_name);
 583   signature_name += 2;
 584   // The class should have been loaded so the method and signature should already be
 585   // in the symbol table.  If they're not there, the method doesn't exist.
 586   TempNewSymbol name = SymbolTable::probe(method_name, method_name_len);
 587   TempNewSymbol signature = SymbolTable::probe(signature_name, signature_name_len);
 588 
 589   Method* m;
 590   if (name == NULL || signature == NULL) {
 591     m = NULL;
 592   } else if (name == vmSymbols::object_initializer_name() ||
 593              name == vmSymbols::class_initializer_name()) {
 594     // Never search superclasses for constructors
 595     if (klass->is_instance_klass()) {
 596       m = InstanceKlass::cast(klass)->find_method(name, signature);
 597     } else {
 598       m = NULL;
 599     }
 600   } else {
 601     m = klass->lookup_method(name, signature);
 602     if (m == NULL && klass->is_instance_klass()) {
 603       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
 604     }
 605   }
 606   if (m == NULL) {
 607     // Fatal error because we assume classes and methods should not be changed since aot compilation.
 608     const char* klass_name = klass->external_name();
 609     int klass_len = (int)strlen(klass_name);
 610     char* meta_name = NEW_RESOURCE_ARRAY(char, klass_len + 1 + method_name_len + signature_name_len + 1);
 611     memcpy(meta_name, klass_name, klass_len);
 612     meta_name[klass_len] = '.';
 613     memcpy(&meta_name[klass_len + 1], method_name, method_name_len);
 614     memcpy(&meta_name[klass_len + 1 + method_name_len], signature_name, signature_name_len);
 615     meta_name[klass_len + 1 + method_name_len + signature_name_len] = '\0';
 616     Handle exception = Exceptions::new_exception(thread, vmSymbols::java_lang_NoSuchMethodError(), meta_name);
 617     java_lang_Throwable::print(exception(), tty);
 618     tty->cr();
 619     java_lang_Throwable::print_stack_trace(exception, tty);
 620     tty->cr();
 621     fatal("Failed to find method '%s'", meta_name);
 622   }
 623   NOT_PRODUCT( aot_methods_found++; )
 624   return m;
 625 }
 626 
 627 AOTKlassData* AOTCodeHeap::find_klass(const char *name) {
 628   return (AOTKlassData*) os::dll_lookup(_lib->dl_handle(), name);
 629 }
 630 
 631 AOTKlassData* AOTCodeHeap::find_klass(InstanceKlass* ik) {
 632   ResourceMark rm;
 633   AOTKlassData* klass_data = find_klass(ik->signature_name());
 634   return klass_data;
 635 }
 636 
 637 bool AOTCodeHeap::is_dependent_method(Klass* dependee, AOTCompiledMethod* aot) {
 638   InstanceKlass *dependee_ik = InstanceKlass::cast(dependee);
 639   AOTKlassData* klass_data = find_klass(dependee_ik);
 640   if (klass_data == NULL) {
 641     return false; // no AOT records for this class - no dependencies
 642   }
 643   if (!dependee_ik->has_passed_fingerprint_check()) {
 644     return false; // different class
 645   }
 646 
 647   int methods_offset = klass_data->_dependent_methods_offset;
 648   if (methods_offset >= 0) {
 649     address methods_cnt_adr = _dependencies + methods_offset;
 650     int methods_cnt = *(int*)methods_cnt_adr;
 651     int* indexes = (int*)(methods_cnt_adr + 4);
 652     for (int i = 0; i < methods_cnt; ++i) {
 653       int code_id = indexes[i];
 654       if (_code_to_aot[code_id]._aot == aot) {
 655         return true; // found dependent method
 656       }
 657     }
 658   }
 659   return false;
 660 }
 661 
 662 void AOTCodeHeap::sweep_dependent_methods(int* indexes, int methods_cnt) {
 663   int marked = 0;
 664   for (int i = 0; i < methods_cnt; ++i) {
 665     int code_id = indexes[i];
 666     // Invalidate aot code.
 667     if (Atomic::cmpxchg(invalid, &_code_to_aot[code_id]._state, not_set) != not_set) {
 668       if (_code_to_aot[code_id]._state == in_use) {
 669         AOTCompiledMethod* aot = _code_to_aot[code_id]._aot;
 670         assert(aot != NULL, "aot should be set");
 671         if (!aot->is_runtime_stub()) { // Something is wrong - should not invalidate stubs.
 672           aot->mark_for_deoptimization(false);
 673           marked++;
 674         }
 675       }
 676     }
 677   }
 678   if (marked > 0) {
 679     VM_Deoptimize op;
 680     VMThread::execute(&op);
 681   }
 682 }
 683 
 684 void AOTCodeHeap::sweep_dependent_methods(AOTKlassData* klass_data) {
 685   // Make dependent methods non_entrant forever.
 686   int methods_offset = klass_data->_dependent_methods_offset;
 687   if (methods_offset >= 0) {
 688     address methods_cnt_adr = _dependencies + methods_offset;
 689     int methods_cnt = *(int*)methods_cnt_adr;
 690     int* indexes = (int*)(methods_cnt_adr + 4);
 691     sweep_dependent_methods(indexes, methods_cnt);
 692   }
 693 }
 694 
 695 void AOTCodeHeap::sweep_dependent_methods(InstanceKlass* ik) {
 696   AOTKlassData* klass_data = find_klass(ik);
 697   vmassert(klass_data != NULL, "dependency data missing");
 698   sweep_dependent_methods(klass_data);
 699 }
 700 
 701 void AOTCodeHeap::sweep_method(AOTCompiledMethod *aot) {
 702   int indexes[] = {aot->method_index()};
 703   sweep_dependent_methods(indexes, 1);
 704   vmassert(aot->method()->code() != aot && aot->method()->aot_code() == NULL, "method still active");
 705 }
 706 
 707 
 708 bool AOTCodeHeap::load_klass_data(InstanceKlass* ik, Thread* thread) {
 709   ResourceMark rm;
 710 
 711   NOT_PRODUCT( klasses_seen++; )
 712 
 713   AOTKlassData* klass_data = find_klass(ik);
 714   if (klass_data == NULL) {
 715     return false;
 716   }
 717 
 718   if (!ik->has_passed_fingerprint_check()) {
 719     NOT_PRODUCT( aot_klasses_fp_miss++; )
 720     log_trace(aot, class, fingerprint)("class  %s%s  has bad fingerprint in  %s tid=" INTPTR_FORMAT,
 721                                        ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 722                                        _lib->name(), p2i(thread));
 723     sweep_dependent_methods(klass_data);
 724     return false;
 725   }
 726 
 727   if (ik->has_been_redefined()) {
 728     log_trace(aot, class, load)("class  %s%s in %s  has been redefined tid=" INTPTR_FORMAT,
 729                                 ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 730                                 _lib->name(), p2i(thread));
 731     sweep_dependent_methods(klass_data);
 732     return false;
 733   }
 734 
 735   assert(klass_data->_class_id < _class_count, "invalid class id");
 736   AOTClass* aot_class = &_classes[klass_data->_class_id];
 737   if (aot_class->_classloader != NULL && aot_class->_classloader != ik->class_loader_data()) {
 738     log_trace(aot, class, load)("class  %s  in  %s already loaded for classloader %p vs %p tid=" INTPTR_FORMAT,
 739                                 ik->internal_name(), _lib->name(), aot_class->_classloader, ik->class_loader_data(), p2i(thread));
 740     NOT_PRODUCT( aot_klasses_cl_miss++; )
 741     return false;
 742   }
 743 
 744   if (_lib->config()->_omitAssertions && JavaAssertions::enabled(ik->name()->as_C_string(), ik->class_loader() == NULL)) {
 745     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());
 746     sweep_dependent_methods(klass_data);
 747     return false;
 748   }
 749 
 750   NOT_PRODUCT( aot_klasses_found++; )
 751 
 752   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));
 753 
 754   aot_class->_classloader = ik->class_loader_data();
 755   // Set klass's Resolve (second) got cell.
 756   _klasses_got[klass_data->_got_index] = ik;
 757   if (ik->is_initialized()) {
 758     _klasses_got[klass_data->_got_index - 1] = ik;
 759   }
 760 
 761   // Initialize global symbols of the DSO to the corresponding VM symbol values.
 762   link_global_lib_symbols();
 763 
 764   int methods_offset = klass_data->_compiled_methods_offset;
 765   if (methods_offset >= 0) {
 766     address methods_cnt_adr = _methods_offsets + methods_offset;
 767     int methods_cnt = *(int*)methods_cnt_adr;
 768     // Collect data about compiled methods
 769     AOTMethodData* methods_data = NEW_RESOURCE_ARRAY(AOTMethodData, methods_cnt);
 770     AOTMethodOffsets* methods_offsets = (AOTMethodOffsets*)(methods_cnt_adr + 4);
 771     for (int i = 0; i < methods_cnt; ++i) {
 772       AOTMethodOffsets* method_offsets = &methods_offsets[i];
 773       int code_id = method_offsets->_code_id;
 774       if (_code_to_aot[code_id]._state == invalid) {
 775         continue; // skip AOT methods slots which have been invalidated
 776       }
 777       AOTMethodData* method_data = &methods_data[i];
 778       const char* aot_name = _metaspace_names + method_offsets->_name_offset;
 779       method_data->_name = aot_name;
 780       method_data->_code = _code_space  + method_offsets->_code_offset;
 781       method_data->_meta = (aot_metadata*)(_method_metadata + method_offsets->_meta_offset);
 782       method_data->_metadata_table = (address)_metadata_got + method_offsets->_metadata_got_offset;
 783       method_data->_metadata_size  = method_offsets->_metadata_got_size;
 784       // aot_name format: "<u2_size>Ljava/lang/ThreadGroup;<u2_size>addUnstarted<u2_size>()V"
 785       int klass_len = build_u2_from((address)aot_name);
 786       const char* method_name = aot_name + 2 + klass_len;
 787       Method* m = AOTCodeHeap::find_method(ik, thread, method_name);
 788       methodHandle mh(thread, m);
 789       if (mh->code() != NULL) { // Does it have already compiled code?
 790         continue; // Don't overwrite
 791       }
 792       publish_aot(mh, method_data, code_id);
 793     }
 794   }
 795   return true;
 796 }
 797 
 798 AOTCompiledMethod* AOTCodeHeap::next_in_use_at(int start) const {
 799   for (int index = start; index < _method_count; index++) {
 800     if (_code_to_aot[index]._state != in_use) {
 801       continue; // Skip uninitialized entries.
 802     }
 803     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 804     return aot;
 805   }
 806   return NULL;
 807 }
 808 
 809 void* AOTCodeHeap::first() const {
 810   return next_in_use_at(0);
 811 }
 812 
 813 void* AOTCodeHeap::next(void* p) const {
 814   AOTCompiledMethod *aot = (AOTCompiledMethod *)p;
 815   int next_index = aot->method_index() + 1;
 816   assert(next_index <= _method_count, "");
 817   if (next_index == _method_count) {
 818     return NULL;
 819   }
 820   return next_in_use_at(next_index);
 821 }
 822 
 823 void* AOTCodeHeap::find_start(void* p) const {
 824   if (!contains(p)) {
 825     return NULL;
 826   }
 827   size_t offset = pointer_delta(p, low_boundary(), 1);
 828   // Use segments table
 829   size_t seg_idx = offset / _lib->config()->_codeSegmentSize;
 830   if ((int)(_code_segments[seg_idx]) == 0xff) {
 831     return NULL;
 832   }
 833   while (_code_segments[seg_idx] > 0) {
 834     seg_idx -= (int)_code_segments[seg_idx];
 835   }
 836   int code_offset = (int)seg_idx * _lib->config()->_codeSegmentSize;
 837   int aot_index = *(int*)(_code_space + code_offset);
 838   AOTCompiledMethod* aot = _code_to_aot[aot_index]._aot;
 839   assert(aot != NULL, "should find registered aot method");
 840   return aot;
 841 }
 842 
 843 AOTCompiledMethod* AOTCodeHeap::find_aot(address p) const {
 844   assert(contains(p), "should be here");
 845   return (AOTCompiledMethod *)find_start(p);
 846 }
 847 
 848 CodeBlob* AOTCodeHeap::find_blob_unsafe(void* start) const {
 849   return (CodeBlob*)AOTCodeHeap::find_start(start);
 850 }
 851 
 852 void AOTCodeHeap::oops_do(OopClosure* f) {
 853   for (int i = 0; i < _oop_got_size; i++) {
 854     oop* p = &_oop_got[i];
 855     if (*p == NULL)  continue;  // skip non-oops
 856     f->do_oop(p);
 857   }
 858   for (int index = 0; index < _method_count; index++) {
 859     if (_code_to_aot[index]._state != in_use) {
 860       continue; // Skip uninitialized entries.
 861     }
 862     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 863     aot->do_oops(f);
 864   }
 865 }
 866 
 867 // Scan only klasses_got cells which should have only Klass*,
 868 // metadata_got cells are scanned only for alive AOT methods
 869 // by AOTCompiledMethod::metadata_do().
 870 void AOTCodeHeap::got_metadata_do(void f(Metadata*)) {
 871   for (int i = 1; i < _klasses_got_size; i++) {
 872     Metadata** p = &_klasses_got[i];
 873     Metadata* md = *p;
 874     if (md == NULL)  continue;  // skip non-oops
 875     if (Metaspace::contains(md)) {
 876       f(md);
 877     } else {
 878       intptr_t meta = (intptr_t)md;
 879       fatal("Invalid value in _klasses_got[%d] = " INTPTR_FORMAT, i, meta);
 880     }
 881   }
 882 }
 883 
 884 void AOTCodeHeap::cleanup_inline_caches() {
 885   for (int index = 0; index < _method_count; index++) {
 886     if (_code_to_aot[index]._state != in_use) {
 887       continue; // Skip uninitialized entries.
 888     }
 889     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 890     aot->cleanup_inline_caches();
 891   }
 892 }
 893 
 894 #ifdef ASSERT
 895 int AOTCodeHeap::verify_icholder_relocations() {
 896   int count = 0;
 897   for (int index = 0; index < _method_count; index++) {
 898     if (_code_to_aot[index]._state != in_use) {
 899       continue; // Skip uninitialized entries.
 900     }
 901     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 902     count += aot->verify_icholder_relocations();
 903   }
 904   return count;
 905 }
 906 #endif
 907 
 908 void AOTCodeHeap::flush_evol_dependents_on(InstanceKlass* dependee) {
 909   for (int index = 0; index < _method_count; index++) {
 910     if (_code_to_aot[index]._state != in_use) {
 911       continue; // Skip uninitialized entries.
 912     }
 913     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 914     aot->flush_evol_dependents_on(dependee);
 915   }
 916 }
 917 
 918 void AOTCodeHeap::metadata_do(void f(Metadata*)) {
 919   for (int index = 0; index < _method_count; index++) {
 920     if (_code_to_aot[index]._state != in_use) {
 921       continue; // Skip uninitialized entries.
 922     }
 923     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 924     if (aot->_is_alive()) {
 925       aot->metadata_do(f);
 926     }
 927   }
 928   // Scan klasses_got cells.
 929   got_metadata_do(f);
 930 }
 931 
 932 bool AOTCodeHeap::reconcile_dynamic_klass(AOTCompiledMethod *caller, InstanceKlass* holder, int index, Klass *dyno_klass, const char *descriptor1, const char *descriptor2) {
 933   const char * const descriptors[2] = {descriptor1, descriptor2};
 934   JavaThread *thread = JavaThread::current();
 935   ResourceMark rm(thread);
 936 
 937   AOTKlassData* holder_data = find_klass(holder);
 938   vmassert(holder_data != NULL, "klass %s not found", holder->signature_name());
 939   vmassert(is_dependent_method(holder, caller), "sanity");
 940 
 941   AOTKlassData* dyno_data = NULL;
 942   bool adapter_failed = false;
 943   char buf[64];
 944   int descriptor_index = 0;
 945   // descriptors[0] specific name ("adapter:<method_id>") for matching
 946   // descriptors[1] fall-back name ("adapter") for depdencies
 947   while (descriptor_index < 2) {
 948     const char *descriptor = descriptors[descriptor_index];
 949     if (descriptor == NULL) {
 950       break;
 951     }
 952     jio_snprintf(buf, sizeof buf, "%s<%d:%d>", descriptor, holder_data->_class_id, index);
 953     dyno_data = find_klass(buf);
 954     if (dyno_data != NULL) {
 955       break;
 956     }
 957     // If match failed then try fall-back for dependencies
 958     ++descriptor_index;
 959     adapter_failed = true;
 960   }
 961 
 962   if (dyno_data == NULL && dyno_klass == NULL) {
 963     // all is well, no (appendix) at compile-time, and still none
 964     return true;
 965   }
 966 
 967   if (dyno_data == NULL) {
 968     // no (appendix) at build-time, but now there is
 969     sweep_dependent_methods(holder_data);
 970     return false;
 971   }
 972 
 973   if (adapter_failed) {
 974     // adapter method mismatch
 975     sweep_dependent_methods(holder_data);
 976     sweep_dependent_methods(dyno_data);
 977     return false;
 978   }
 979 
 980   if (dyno_klass == NULL) {
 981     // (appendix) at build-time, none now
 982     sweep_dependent_methods(holder_data);
 983     sweep_dependent_methods(dyno_data);
 984     return false;
 985   }
 986 
 987   // TODO: support array appendix object
 988   if (!dyno_klass->is_instance_klass()) {
 989     sweep_dependent_methods(holder_data);
 990     sweep_dependent_methods(dyno_data);
 991     return false;
 992   }
 993 
 994   InstanceKlass* dyno = InstanceKlass::cast(dyno_klass);
 995 
 996   if (!dyno->is_anonymous()) {
 997     if (_klasses_got[dyno_data->_got_index] != dyno) {
 998       // compile-time class different from runtime class, fail and deoptimize
 999       sweep_dependent_methods(holder_data);
1000       sweep_dependent_methods(dyno_data);
1001       return false;
1002     }
1003 
1004     if (dyno->is_initialized()) {
1005       _klasses_got[dyno_data->_got_index - 1] = dyno;
1006     }
1007     return true;
1008   }
1009 
1010   // TODO: support anonymous supers
1011   if (!dyno->supers_have_passed_fingerprint_checks() || dyno->get_stored_fingerprint() != dyno_data->_fingerprint) {
1012       NOT_PRODUCT( aot_klasses_fp_miss++; )
1013       log_trace(aot, class, fingerprint)("class  %s%s  has bad fingerprint in  %s tid=" INTPTR_FORMAT,
1014           dyno->internal_name(), dyno->is_shared() ? " (shared)" : "",
1015           _lib->name(), p2i(thread));
1016     sweep_dependent_methods(holder_data);
1017     sweep_dependent_methods(dyno_data);
1018     return false;
1019   }
1020 
1021   _klasses_got[dyno_data->_got_index] = dyno;
1022   if (dyno->is_initialized()) {
1023     _klasses_got[dyno_data->_got_index - 1] = dyno;
1024   }
1025 
1026   // TODO: hook up any AOT code
1027   // load_klass_data(dyno_data, thread);
1028   return true;
1029 }
1030 
1031 bool AOTCodeHeap::reconcile_dynamic_method(AOTCompiledMethod *caller, InstanceKlass* holder, int index, Method *adapter_method) {
1032     InstanceKlass *adapter_klass = adapter_method->method_holder();
1033     char buf[64];
1034     jio_snprintf(buf, sizeof buf, "adapter:%d", adapter_method->method_idnum());
1035     if (!reconcile_dynamic_klass(caller, holder, index, adapter_klass, buf, "adapter")) {
1036       return false;
1037     }
1038     return true;
1039 }
1040 
1041 bool AOTCodeHeap::reconcile_dynamic_invoke(AOTCompiledMethod* caller, InstanceKlass* holder, int index, Method* adapter_method, Klass *appendix_klass) {
1042     if (!reconcile_dynamic_klass(caller, holder, index, appendix_klass, "appendix")) {
1043       return false;
1044     }
1045 
1046     if (!reconcile_dynamic_method(caller, holder, index, adapter_method)) {
1047       return false;
1048     }
1049 
1050     return true;
1051 }