1 /*
   2  * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 
  26 #include "asm/macroAssembler.hpp"
  27 #include "cds/aotCacheAccess.hpp"
  28 #include "cds/cds_globals.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/heapShared.hpp"
  31 #include "cds/metaspaceShared.hpp"
  32 #include "classfile/javaAssertions.hpp"
  33 #include "code/aotCodeCache.hpp"
  34 #include "code/codeCache.hpp"
  35 #include "gc/shared/gcConfig.hpp"
  36 #include "logging/logStream.hpp"
  37 #include "memory/memoryReserver.hpp"
  38 #include "runtime/deoptimization.hpp"
  39 #include "runtime/flags/flagSetting.hpp"
  40 #include "runtime/globals_extension.hpp"
  41 #include "runtime/java.hpp"
  42 #include "runtime/mutexLocker.hpp"
  43 #include "runtime/os.inline.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "runtime/stubRoutines.hpp"
  46 #include "utilities/copy.hpp"
  47 #ifdef COMPILER1
  48 #include "c1/c1_Runtime1.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/runtime.hpp"
  52 #endif
  53 #if INCLUDE_G1GC
  54 #include "gc/g1/g1BarrierSetRuntime.hpp"
  55 #endif
  56 #if INCLUDE_SHENANDOAHGC
  57 #include "gc/shenandoah/shenandoahRuntime.hpp"
  58 #endif
  59 #if INCLUDE_ZGC
  60 #include "gc/z/zBarrierSetRuntime.hpp"
  61 #endif
  62 
  63 #include <sys/stat.h>
  64 #include <errno.h>
  65 
  66 const char* aot_code_entry_kind_name[] = {
  67 #define DECL_KIND_STRING(kind) XSTR(kind),
  68   DO_AOTCODEENTRY_KIND(DECL_KIND_STRING)
  69 #undef DECL_KIND_STRING
  70 };
  71 
  72 static void report_load_failure() {
  73   if (AbortVMOnAOTCodeFailure) {
  74     vm_exit_during_initialization("Unable to use AOT Code Cache.", nullptr);
  75   }
  76   log_info(aot, codecache, init)("Unable to use AOT Code Cache.");
  77   AOTCodeCache::disable_caching();
  78 }
  79 
  80 static void report_store_failure() {
  81   if (AbortVMOnAOTCodeFailure) {
  82     tty->print_cr("Unable to create AOT Code Cache.");
  83     vm_abort(false);
  84   }
  85   log_info(aot, codecache, exit)("Unable to create AOT Code Cache.");
  86   AOTCodeCache::disable_caching();
  87 }
  88 
  89 // The sequence of AOT code caching flags and parametters settings.
  90 //
  91 // 1. The initial AOT code caching flags setting is done
  92 // during call to CDSConfig::check_vm_args_consistency().
  93 //
  94 // 2. The earliest AOT code state check done in compilationPolicy_init()
  95 // where we set number of compiler threads for AOT assembly phase.
  96 //
  97 // 3. We determine presence of AOT code in AOT Cache in
  98 // MetaspaceShared::open_static_archive() which is calles
  99 // after compilationPolicy_init() but before codeCache_init().
 100 //
 101 // 4. AOTCodeCache::initialize() is called during universe_init()
 102 // and does final AOT state and flags settings.
 103 //
 104 // 5. Finally AOTCodeCache::init2() is called after universe_init()
 105 // when all GC settings are finalized.
 106 
 107 // Next methods determine which action we do with AOT code depending
 108 // on phase of AOT process: assembly or production.
 109 
 110 bool AOTCodeCache::is_dumping_adapter() {
 111   return AOTAdapterCaching && is_on_for_dump();
 112 }
 113 
 114 bool AOTCodeCache::is_using_adapter()   {
 115   return AOTAdapterCaching && is_on_for_use();
 116 }
 117 
 118 bool AOTCodeCache::is_dumping_stub() {
 119   return AOTStubCaching && is_on_for_dump();
 120 }
 121 
 122 bool AOTCodeCache::is_using_stub()   {
 123   return AOTStubCaching && is_on_for_use();
 124 }
 125 
 126 // Next methods could be called regardless AOT code cache status.
 127 // Initially they are called during flags parsing and finilized
 128 // in AOTCodeCache::initialize().
 129 void AOTCodeCache::enable_caching() {
 130   FLAG_SET_ERGO_IF_DEFAULT(AOTStubCaching, true);
 131   FLAG_SET_ERGO_IF_DEFAULT(AOTAdapterCaching, true);
 132 }
 133 
 134 void AOTCodeCache::disable_caching() {
 135   FLAG_SET_ERGO(AOTStubCaching, false);
 136   FLAG_SET_ERGO(AOTAdapterCaching, false);
 137 }
 138 
 139 bool AOTCodeCache::is_caching_enabled() {
 140   return AOTStubCaching || AOTAdapterCaching;
 141 }
 142 
 143 static uint32_t encode_id(AOTCodeEntry::Kind kind, int id) {
 144   assert(AOTCodeEntry::is_valid_entry_kind(kind), "invalid AOTCodeEntry kind %d", (int)kind);
 145   // There can be a conflict of id between an Adapter and *Blob, but that should not cause any functional issue
 146   // becasue both id and kind are used to find an entry, and that combination should be unique
 147   if (kind == AOTCodeEntry::Adapter) {
 148     return id;
 149   } else if (kind == AOTCodeEntry::SharedBlob) {
 150     return id;
 151   } else if (kind == AOTCodeEntry::C1Blob) {
 152     return (int)SharedStubId::NUM_STUBIDS + id;
 153   } else {
 154     // kind must be AOTCodeEntry::C2Blob
 155     return (int)SharedStubId::NUM_STUBIDS + COMPILER1_PRESENT((int)C1StubId::NUM_STUBIDS) + id;
 156   }
 157 }
 158 
 159 static uint _max_aot_code_size = 0;
 160 uint AOTCodeCache::max_aot_code_size() {
 161   return _max_aot_code_size;
 162 }
 163 
 164 // It is called from MetaspaceShared::initialize_shared_spaces()
 165 // which is called from universe_init().
 166 // At this point all AOT class linking seetings are finilized
 167 // and AOT cache is open so we can map AOT code region.
 168 void AOTCodeCache::initialize() {
 169 #if defined(ZERO) || !(defined(AMD64) || defined(AARCH64))
 170   log_info(aot, codecache, init)("AOT Code Cache is not supported on this platform.");
 171   disable_caching();
 172   return;
 173 #else
 174   if (FLAG_IS_DEFAULT(AOTCache)) {
 175     log_info(aot, codecache, init)("AOT Code Cache is not used: AOTCache is not specified.");
 176     disable_caching();
 177     return; // AOTCache must be specified to dump and use AOT code
 178   }
 179 
 180   // Disable stubs caching until JDK-8357398 is fixed.
 181   FLAG_SET_ERGO(AOTStubCaching, false);
 182 
 183   if (VerifyOops) {
 184     // Disable AOT stubs caching when VerifyOops flag is on.
 185     // Verify oops code generated a lot of C strings which overflow
 186     // AOT C string table (which has fixed size).
 187     // AOT C string table will be reworked later to handle such cases.
 188     //
 189     // Note: AOT adapters are not affected - they don't have oop operations.
 190     log_info(aot, codecache, init)("AOT Stubs Caching is not supported with VerifyOops.");
 191     FLAG_SET_ERGO(AOTStubCaching, false);
 192   }
 193 
 194   bool is_dumping = false;
 195   bool is_using   = false;
 196   if (CDSConfig::is_dumping_final_static_archive() && CDSConfig::is_dumping_aot_linked_classes()) {
 197     is_dumping = true;
 198     enable_caching();
 199     is_dumping = is_caching_enabled();
 200   } else if (CDSConfig::is_using_archive() && CDSConfig::is_using_aot_linked_classes()) {
 201     enable_caching();
 202     is_using = is_caching_enabled();
 203   } else {
 204     log_info(aot, codecache, init)("AOT Code Cache is not used: AOT Class Linking is not used.");
 205     disable_caching();
 206     return; // nothing to do
 207   }
 208   if (!(is_dumping || is_using)) {
 209     disable_caching();
 210     return; // AOT code caching disabled on command line
 211   }
 212   _max_aot_code_size = AOTCodeMaxSize;
 213   if (!FLAG_IS_DEFAULT(AOTCodeMaxSize)) {
 214     if (!is_aligned(AOTCodeMaxSize, os::vm_allocation_granularity())) {
 215       _max_aot_code_size = align_up(AOTCodeMaxSize, os::vm_allocation_granularity());
 216       log_debug(aot,codecache,init)("Max AOT Code Cache size is aligned up to %uK", (int)(max_aot_code_size()/K));
 217     }
 218   }
 219   size_t aot_code_size = is_using ? AOTCacheAccess::get_aot_code_region_size() : 0;
 220   if (is_using && aot_code_size == 0) {
 221     log_info(aot, codecache, init)("AOT Code Cache is empty");
 222     disable_caching();
 223     return;
 224   }
 225   if (!open_cache(is_dumping, is_using)) {
 226     if (is_using) {
 227       report_load_failure();
 228     } else {
 229       report_store_failure();
 230     }
 231     return;
 232   }
 233   if (is_dumping) {
 234     FLAG_SET_DEFAULT(ForceUnreachable, true);
 235   }
 236   FLAG_SET_DEFAULT(DelayCompilerStubsGeneration, false);
 237 #endif // defined(AMD64) || defined(AARCH64)
 238 }
 239 
 240 static AOTCodeCache*  opened_cache = nullptr; // Use this until we verify the cache
 241 AOTCodeCache* AOTCodeCache::_cache = nullptr;
 242 DEBUG_ONLY( bool AOTCodeCache::_passed_init2 = false; )
 243 
 244 // It is called after universe_init() when all GC settings are finalized.
 245 void AOTCodeCache::init2() {
 246   DEBUG_ONLY( _passed_init2 = true; )
 247   if (opened_cache == nullptr) {
 248     return;
 249   }
 250   if (!opened_cache->verify_config()) {
 251     delete opened_cache;
 252     opened_cache = nullptr;
 253     report_load_failure();
 254     return;
 255   }
 256 
 257   // initialize the table of external routines so we can save
 258   // generated code blobs that reference them
 259   AOTCodeAddressTable* table = opened_cache->_table;
 260   assert(table != nullptr, "should be initialized already");
 261   table->init_extrs();
 262 
 263   // Now cache and address table are ready for AOT code generation
 264   _cache = opened_cache;
 265 }
 266 
 267 bool AOTCodeCache::open_cache(bool is_dumping, bool is_using) {
 268   opened_cache = new AOTCodeCache(is_dumping, is_using);
 269   if (opened_cache->failed()) {
 270     delete opened_cache;
 271     opened_cache = nullptr;
 272     return false;
 273   }
 274   return true;
 275 }
 276 
 277 void AOTCodeCache::close() {
 278   if (is_on()) {
 279     delete _cache; // Free memory
 280     _cache = nullptr;
 281     opened_cache = nullptr;
 282   }
 283 }
 284 
 285 #define DATA_ALIGNMENT HeapWordSize
 286 
 287 AOTCodeCache::AOTCodeCache(bool is_dumping, bool is_using) :
 288   _load_header(nullptr),
 289   _load_buffer(nullptr),
 290   _store_buffer(nullptr),
 291   _C_store_buffer(nullptr),
 292   _write_position(0),
 293   _load_size(0),
 294   _store_size(0),
 295   _for_use(is_using),
 296   _for_dump(is_dumping),
 297   _closing(false),
 298   _failed(false),
 299   _lookup_failed(false),
 300   _table(nullptr),
 301   _load_entries(nullptr),
 302   _search_entries(nullptr),
 303   _store_entries(nullptr),
 304   _C_strings_buf(nullptr),
 305   _store_entries_cnt(0)
 306 {
 307   // Read header at the begining of cache
 308   if (_for_use) {
 309     // Read cache
 310     size_t load_size = AOTCacheAccess::get_aot_code_region_size();
 311     ReservedSpace rs = MemoryReserver::reserve(load_size, mtCode);
 312     if (!rs.is_reserved()) {
 313       log_warning(aot, codecache, init)("Failed to reserved %u bytes of memory for mapping AOT code region into AOT Code Cache", (uint)load_size);
 314       set_failed();
 315       return;
 316     }
 317     if (!AOTCacheAccess::map_aot_code_region(rs)) {
 318       log_warning(aot, codecache, init)("Failed to read/mmap cached code region into AOT Code Cache");
 319       set_failed();
 320       return;
 321     }
 322 
 323     _load_size = (uint)load_size;
 324     _load_buffer = (char*)rs.base();
 325     assert(is_aligned(_load_buffer, DATA_ALIGNMENT), "load_buffer is not aligned");
 326     log_debug(aot, codecache, init)("Mapped %u bytes at address " INTPTR_FORMAT " at AOT Code Cache", _load_size, p2i(_load_buffer));
 327 
 328     _load_header = (Header*)addr(0);
 329     if (!_load_header->verify(_load_size)) {
 330       set_failed();
 331       return;
 332     }
 333     log_info (aot, codecache, init)("Loaded %u AOT code entries from AOT Code Cache", _load_header->entries_count());
 334     log_debug(aot, codecache, init)("  Adapters:  total=%u", _load_header->adapters_count());
 335     log_debug(aot, codecache, init)("  Shared Blobs: total=%u", _load_header->shared_blobs_count());
 336     log_debug(aot, codecache, init)("  C1 Blobs: total=%u", _load_header->C1_blobs_count());
 337     log_debug(aot, codecache, init)("  C2 Blobs: total=%u", _load_header->C2_blobs_count());
 338     log_debug(aot, codecache, init)("  AOT code cache size: %u bytes", _load_header->cache_size());
 339 
 340     // Read strings
 341     load_strings();
 342   }
 343   if (_for_dump) {
 344     _C_store_buffer = NEW_C_HEAP_ARRAY(char, max_aot_code_size() + DATA_ALIGNMENT, mtCode);
 345     _store_buffer = align_up(_C_store_buffer, DATA_ALIGNMENT);
 346     // Entries allocated at the end of buffer in reverse (as on stack).
 347     _store_entries = (AOTCodeEntry*)align_up(_C_store_buffer + max_aot_code_size(), DATA_ALIGNMENT);
 348     log_debug(aot, codecache, init)("Allocated store buffer at address " INTPTR_FORMAT " of size %u", p2i(_store_buffer), max_aot_code_size());
 349   }
 350   _table = new AOTCodeAddressTable();
 351 }
 352 
 353 void AOTCodeCache::init_early_stubs_table() {
 354   AOTCodeAddressTable* table = addr_table();
 355   if (table != nullptr) {
 356     table->init_early_stubs();
 357   }
 358 }
 359 
 360 void AOTCodeCache::init_shared_blobs_table() {
 361   AOTCodeAddressTable* table = addr_table();
 362   if (table != nullptr) {
 363     table->init_shared_blobs();
 364   }
 365 }
 366 
 367 void AOTCodeCache::init_early_c1_table() {
 368   AOTCodeAddressTable* table = addr_table();
 369   if (table != nullptr) {
 370     table->init_early_c1();
 371   }
 372 }
 373 
 374 AOTCodeCache::~AOTCodeCache() {
 375   if (_closing) {
 376     return; // Already closed
 377   }
 378   // Stop any further access to cache.
 379   _closing = true;
 380 
 381   MutexLocker ml(Compile_lock);
 382   if (for_dump()) { // Finalize cache
 383     finish_write();
 384   }
 385   _load_buffer = nullptr;
 386   if (_C_store_buffer != nullptr) {
 387     FREE_C_HEAP_ARRAY(char, _C_store_buffer);
 388     _C_store_buffer = nullptr;
 389     _store_buffer = nullptr;
 390   }
 391   if (_table != nullptr) {
 392     MutexLocker ml(AOTCodeCStrings_lock, Mutex::_no_safepoint_check_flag);
 393     delete _table;
 394     _table = nullptr;
 395   }
 396 }
 397 
 398 void AOTCodeCache::Config::record() {
 399   _flags = 0;
 400 #ifdef ASSERT
 401   _flags |= debugVM;
 402 #endif
 403   if (UseCompressedOops) {
 404     _flags |= compressedOops;
 405   }
 406   if (UseCompressedClassPointers) {
 407     _flags |= compressedClassPointers;
 408   }
 409   if (UseTLAB) {
 410     _flags |= useTLAB;
 411   }
 412   if (JavaAssertions::systemClassDefault()) {
 413     _flags |= systemClassAssertions;
 414   }
 415   if (JavaAssertions::userClassDefault()) {
 416     _flags |= userClassAssertions;
 417   }
 418   if (EnableContended) {
 419     _flags |= enableContendedPadding;
 420   }
 421   if (RestrictContended) {
 422     _flags |= restrictContendedPadding;
 423   }
 424   _compressedOopShift    = CompressedOops::shift();
 425   _compressedOopBase     = CompressedOops::base();
 426   _compressedKlassShift  = CompressedKlassPointers::shift();
 427   _contendedPaddingWidth = ContendedPaddingWidth;
 428   _gc                    = (uint)Universe::heap()->kind();
 429 }
 430 
 431 bool AOTCodeCache::Config::verify() const {
 432   // First checks affect all cached AOT code
 433 #ifdef ASSERT
 434   if ((_flags & debugVM) == 0) {
 435     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created by product VM, it can't be used by debug VM");
 436     return false;
 437   }
 438 #else
 439   if ((_flags & debugVM) != 0) {
 440     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created by debug VM, it can't be used by product VM");
 441     return false;
 442   }
 443 #endif
 444 
 445   CollectedHeap::Name aot_gc = (CollectedHeap::Name)_gc;
 446   if (aot_gc != Universe::heap()->kind()) {
 447     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with different GC: %s vs current %s", GCConfig::hs_err_name(aot_gc), GCConfig::hs_err_name());
 448     return false;
 449   }
 450 
 451   if (((_flags & compressedClassPointers) != 0) != UseCompressedClassPointers) {
 452     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with UseCompressedClassPointers = %s", UseCompressedClassPointers ? "false" : "true");
 453     return false;
 454   }
 455   if (_compressedKlassShift != (uint)CompressedKlassPointers::shift()) {
 456     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with CompressedKlassPointers::shift() = %d vs current %d", _compressedKlassShift, CompressedKlassPointers::shift());
 457     return false;
 458   }
 459 
 460   // The following checks do not affect AOT adapters caching
 461 
 462   if (((_flags & compressedOops) != 0) != UseCompressedOops) {
 463     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with UseCompressedOops = %s", UseCompressedOops ? "false" : "true");
 464     AOTStubCaching = false;
 465   }
 466   if (_compressedOopShift != (uint)CompressedOops::shift()) {
 467     log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with different CompressedOops::shift(): %d vs current %d", _compressedOopShift, CompressedOops::shift());
 468     AOTStubCaching = false;
 469   }
 470 
 471   // This should be the last check as it only disables AOTStubCaching
 472   if ((_compressedOopBase == nullptr || CompressedOops::base() == nullptr) && (_compressedOopBase != CompressedOops::base())) {
 473     log_debug(aot, codecache, init)("AOTStubCaching is disabled: incompatible CompressedOops::base(): %p vs current %p", _compressedOopBase, CompressedOops::base());
 474     AOTStubCaching = false;
 475   }
 476 
 477   return true;
 478 }
 479 
 480 bool AOTCodeCache::Header::verify(uint load_size) const {
 481   if (_version != AOT_CODE_VERSION) {
 482     log_debug(aot, codecache, init)("AOT Code Cache disabled: different AOT Code version %d vs %d recorded in AOT Code header", AOT_CODE_VERSION, _version);
 483     return false;
 484   }
 485   if (load_size < _cache_size) {
 486     log_debug(aot, codecache, init)("AOT Code Cache disabled: AOT Code Cache size %d < %d recorded in AOT Code header", load_size, _cache_size);
 487     return false;
 488   }
 489   return true;
 490 }
 491 
 492 AOTCodeCache* AOTCodeCache::open_for_use() {
 493   if (AOTCodeCache::is_on_for_use()) {
 494     return AOTCodeCache::cache();
 495   }
 496   return nullptr;
 497 }
 498 
 499 AOTCodeCache* AOTCodeCache::open_for_dump() {
 500   if (AOTCodeCache::is_on_for_dump()) {
 501     AOTCodeCache* cache = AOTCodeCache::cache();
 502     cache->clear_lookup_failed(); // Reset bit
 503     return cache;
 504   }
 505   return nullptr;
 506 }
 507 
 508 void copy_bytes(const char* from, address to, uint size) {
 509   assert((int)size > 0, "sanity");
 510   memcpy(to, from, size);
 511   log_trace(aot, codecache)("Copied %d bytes from " INTPTR_FORMAT " to " INTPTR_FORMAT, size, p2i(from), p2i(to));
 512 }
 513 
 514 AOTCodeReader::AOTCodeReader(AOTCodeCache* cache, AOTCodeEntry* entry) {
 515   _cache = cache;
 516   _entry = entry;
 517   _load_buffer = cache->cache_buffer();
 518   _read_position = 0;
 519   _lookup_failed = false;
 520 }
 521 
 522 void AOTCodeReader::set_read_position(uint pos) {
 523   if (pos == _read_position) {
 524     return;
 525   }
 526   assert(pos < _cache->load_size(), "offset:%d >= file size:%d", pos, _cache->load_size());
 527   _read_position = pos;
 528 }
 529 
 530 bool AOTCodeCache::set_write_position(uint pos) {
 531   if (pos == _write_position) {
 532     return true;
 533   }
 534   if (_store_size < _write_position) {
 535     _store_size = _write_position; // Adjust during write
 536   }
 537   assert(pos < _store_size, "offset:%d >= file size:%d", pos, _store_size);
 538   _write_position = pos;
 539   return true;
 540 }
 541 
 542 static char align_buffer[256] = { 0 };
 543 
 544 bool AOTCodeCache::align_write() {
 545   // We are not executing code from cache - we copy it by bytes first.
 546   // No need for big alignment (or at all).
 547   uint padding = DATA_ALIGNMENT - (_write_position & (DATA_ALIGNMENT - 1));
 548   if (padding == DATA_ALIGNMENT) {
 549     return true;
 550   }
 551   uint n = write_bytes((const void*)&align_buffer, padding);
 552   if (n != padding) {
 553     return false;
 554   }
 555   log_trace(aot, codecache)("Adjust write alignment in AOT Code Cache");
 556   return true;
 557 }
 558 
 559 // Check to see if AOT code cache has required space to store "nbytes" of data
 560 address AOTCodeCache::reserve_bytes(uint nbytes) {
 561   assert(for_dump(), "Code Cache file is not created");
 562   uint new_position = _write_position + nbytes;
 563   if (new_position >= (uint)((char*)_store_entries - _store_buffer)) {
 564     log_warning(aot,codecache)("Failed to ensure %d bytes at offset %d in AOT Code Cache. Increase AOTCodeMaxSize.",
 565                                nbytes, _write_position);
 566     set_failed();
 567     report_store_failure();
 568     return nullptr;
 569   }
 570   address buffer = (address)(_store_buffer + _write_position);
 571   log_trace(aot, codecache)("Reserved %d bytes at offset %d in AOT Code Cache", nbytes, _write_position);
 572   _write_position += nbytes;
 573   if (_store_size < _write_position) {
 574     _store_size = _write_position;
 575   }
 576   return buffer;
 577 }
 578 
 579 uint AOTCodeCache::write_bytes(const void* buffer, uint nbytes) {
 580   assert(for_dump(), "Code Cache file is not created");
 581   if (nbytes == 0) {
 582     return 0;
 583   }
 584   uint new_position = _write_position + nbytes;
 585   if (new_position >= (uint)((char*)_store_entries - _store_buffer)) {
 586     log_warning(aot, codecache)("Failed to write %d bytes at offset %d to AOT Code Cache. Increase AOTCodeMaxSize.",
 587                                 nbytes, _write_position);
 588     set_failed();
 589     report_store_failure();
 590     return 0;
 591   }
 592   copy_bytes((const char* )buffer, (address)(_store_buffer + _write_position), nbytes);
 593   log_trace(aot, codecache)("Wrote %d bytes at offset %d to AOT Code Cache", nbytes, _write_position);
 594   _write_position += nbytes;
 595   if (_store_size < _write_position) {
 596     _store_size = _write_position;
 597   }
 598   return nbytes;
 599 }
 600 
 601 void* AOTCodeEntry::operator new(size_t x, AOTCodeCache* cache) {
 602   return (void*)(cache->add_entry());
 603 }
 604 
 605 static bool check_entry(AOTCodeEntry::Kind kind, uint id, AOTCodeEntry* entry) {
 606   if (entry->kind() == kind) {
 607     assert(entry->id() == id, "sanity");
 608     return true; // Found
 609   }
 610   return false;
 611 }
 612 
 613 AOTCodeEntry* AOTCodeCache::find_entry(AOTCodeEntry::Kind kind, uint id) {
 614   assert(_for_use, "sanity");
 615   uint count = _load_header->entries_count();
 616   if (_load_entries == nullptr) {
 617     // Read it
 618     _search_entries = (uint*)addr(_load_header->entries_offset()); // [id, index]
 619     _load_entries = (AOTCodeEntry*)(_search_entries + 2 * count);
 620     log_debug(aot, codecache, init)("Read %d entries table at offset %d from AOT Code Cache", count, _load_header->entries_offset());
 621   }
 622   // Binary search
 623   int l = 0;
 624   int h = count - 1;
 625   while (l <= h) {
 626     int mid = (l + h) >> 1;
 627     int ix = mid * 2;
 628     uint is = _search_entries[ix];
 629     if (is == id) {
 630       int index = _search_entries[ix + 1];
 631       AOTCodeEntry* entry = &(_load_entries[index]);
 632       if (check_entry(kind, id, entry)) {
 633         return entry; // Found
 634       }
 635       // Linear search around to handle id collission
 636       for (int i = mid - 1; i >= l; i--) { // search back
 637         ix = i * 2;
 638         is = _search_entries[ix];
 639         if (is != id) {
 640           break;
 641         }
 642         index = _search_entries[ix + 1];
 643         AOTCodeEntry* entry = &(_load_entries[index]);
 644         if (check_entry(kind, id, entry)) {
 645           return entry; // Found
 646         }
 647       }
 648       for (int i = mid + 1; i <= h; i++) { // search forward
 649         ix = i * 2;
 650         is = _search_entries[ix];
 651         if (is != id) {
 652           break;
 653         }
 654         index = _search_entries[ix + 1];
 655         AOTCodeEntry* entry = &(_load_entries[index]);
 656         if (check_entry(kind, id, entry)) {
 657           return entry; // Found
 658         }
 659       }
 660       break; // Not found match
 661     } else if (is < id) {
 662       l = mid + 1;
 663     } else {
 664       h = mid - 1;
 665     }
 666   }
 667   return nullptr;
 668 }
 669 
 670 extern "C" {
 671   static int uint_cmp(const void *i, const void *j) {
 672     uint a = *(uint *)i;
 673     uint b = *(uint *)j;
 674     return a > b ? 1 : a < b ? -1 : 0;
 675   }
 676 }
 677 
 678 bool AOTCodeCache::finish_write() {
 679   if (!align_write()) {
 680     return false;
 681   }
 682   uint strings_offset = _write_position;
 683   int strings_count = store_strings();
 684   if (strings_count < 0) {
 685     return false;
 686   }
 687   if (!align_write()) {
 688     return false;
 689   }
 690   uint strings_size = _write_position - strings_offset;
 691 
 692   uint entries_count = 0; // Number of entrant (useful) code entries
 693   uint entries_offset = _write_position;
 694 
 695   uint store_count = _store_entries_cnt;
 696   if (store_count > 0) {
 697     uint header_size = (uint)align_up(sizeof(AOTCodeCache::Header),  DATA_ALIGNMENT);
 698     uint code_count = store_count;
 699     uint search_count = code_count * 2;
 700     uint search_size = search_count * sizeof(uint);
 701     uint entries_size = (uint)align_up(code_count * sizeof(AOTCodeEntry), DATA_ALIGNMENT); // In bytes
 702     // _write_position includes size of code and strings
 703     uint code_alignment = code_count * DATA_ALIGNMENT; // We align_up code size when storing it.
 704     uint total_size = header_size + _write_position + code_alignment + search_size + entries_size;
 705     assert(total_size < max_aot_code_size(), "AOT Code size (" UINT32_FORMAT " bytes) is greater than AOTCodeMaxSize(" UINT32_FORMAT " bytes).", total_size, max_aot_code_size());
 706 
 707     // Create ordered search table for entries [id, index];
 708     uint* search = NEW_C_HEAP_ARRAY(uint, search_count, mtCode);
 709     // Allocate in AOT Cache buffer
 710     char* buffer = (char *)AOTCacheAccess::allocate_aot_code_region(total_size + DATA_ALIGNMENT);
 711     char* start = align_up(buffer, DATA_ALIGNMENT);
 712     char* current = start + header_size; // Skip header
 713 
 714     AOTCodeEntry* entries_address = _store_entries; // Pointer to latest entry
 715     uint adapters_count = 0;
 716     uint shared_blobs_count = 0;
 717     uint C1_blobs_count = 0;
 718     uint C2_blobs_count = 0;
 719     uint max_size = 0;
 720     // AOTCodeEntry entries were allocated in reverse in store buffer.
 721     // Process them in reverse order to cache first code first.
 722     for (int i = store_count - 1; i >= 0; i--) {
 723       entries_address[i].set_next(nullptr); // clear pointers before storing data
 724       uint size = align_up(entries_address[i].size(), DATA_ALIGNMENT);
 725       if (size > max_size) {
 726         max_size = size;
 727       }
 728       copy_bytes((_store_buffer + entries_address[i].offset()), (address)current, size);
 729       entries_address[i].set_offset(current - start); // New offset
 730       current += size;
 731       uint n = write_bytes(&(entries_address[i]), sizeof(AOTCodeEntry));
 732       if (n != sizeof(AOTCodeEntry)) {
 733         FREE_C_HEAP_ARRAY(uint, search);
 734         return false;
 735       }
 736       search[entries_count*2 + 0] = entries_address[i].id();
 737       search[entries_count*2 + 1] = entries_count;
 738       entries_count++;
 739       AOTCodeEntry::Kind kind = entries_address[i].kind();
 740       if (kind == AOTCodeEntry::Adapter) {
 741         adapters_count++;
 742       } else if (kind == AOTCodeEntry::SharedBlob) {
 743         shared_blobs_count++;
 744       } else if (kind == AOTCodeEntry::C1Blob) {
 745         C1_blobs_count++;
 746       } else if (kind == AOTCodeEntry::C2Blob) {
 747         C2_blobs_count++;
 748       }
 749     }
 750     if (entries_count == 0) {
 751       log_info(aot, codecache, exit)("AOT Code Cache was not created: no entires");
 752       FREE_C_HEAP_ARRAY(uint, search);
 753       return true; // Nothing to write
 754     }
 755     assert(entries_count <= store_count, "%d > %d", entries_count, store_count);
 756     // Write strings
 757     if (strings_count > 0) {
 758       copy_bytes((_store_buffer + strings_offset), (address)current, strings_size);
 759       strings_offset = (current - start); // New offset
 760       current += strings_size;
 761     }
 762 
 763     uint new_entries_offset = (current - start); // New offset
 764     // Sort and store search table
 765     qsort(search, entries_count, 2*sizeof(uint), uint_cmp);
 766     search_size = 2 * entries_count * sizeof(uint);
 767     copy_bytes((const char*)search, (address)current, search_size);
 768     FREE_C_HEAP_ARRAY(uint, search);
 769     current += search_size;
 770 
 771     // Write entries
 772     entries_size = entries_count * sizeof(AOTCodeEntry); // New size
 773     copy_bytes((_store_buffer + entries_offset), (address)current, entries_size);
 774     current += entries_size;
 775     uint size = (current - start);
 776     assert(size <= total_size, "%d > %d", size , total_size);
 777 
 778     log_debug(aot, codecache, exit)("  Adapters:  total=%u", adapters_count);
 779     log_debug(aot, codecache, exit)("  Shared Blobs:  total=%d", shared_blobs_count);
 780     log_debug(aot, codecache, exit)("  C1 Blobs:      total=%d", C1_blobs_count);
 781     log_debug(aot, codecache, exit)("  C2 Blobs:      total=%d", C2_blobs_count);
 782     log_debug(aot, codecache, exit)("  AOT code cache size: %u bytes, max entry's size: %u bytes", size, max_size);
 783 
 784     // Finalize header
 785     AOTCodeCache::Header* header = (AOTCodeCache::Header*)start;
 786     header->init(size, (uint)strings_count, strings_offset,
 787                  entries_count, new_entries_offset,
 788                  adapters_count, shared_blobs_count,
 789                  C1_blobs_count, C2_blobs_count);
 790 
 791     log_info(aot, codecache, exit)("Wrote %d AOT code entries to AOT Code Cache", entries_count);
 792   }
 793   return true;
 794 }
 795 
 796 //------------------Store/Load AOT code ----------------------
 797 
 798 bool AOTCodeCache::store_code_blob(CodeBlob& blob, AOTCodeEntry::Kind entry_kind, uint id, const char* name, int entry_offset_count, int* entry_offsets) {
 799   AOTCodeCache* cache = open_for_dump();
 800   if (cache == nullptr) {
 801     return false;
 802   }
 803   assert(AOTCodeEntry::is_valid_entry_kind(entry_kind), "invalid entry_kind %d", entry_kind);
 804 
 805   if (AOTCodeEntry::is_adapter(entry_kind) && !is_dumping_adapter()) {
 806     return false;
 807   }
 808   if (AOTCodeEntry::is_blob(entry_kind) && !is_dumping_stub()) {
 809     return false;
 810   }
 811   log_debug(aot, codecache, stubs)("Writing blob '%s' (id=%u, kind=%s) to AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
 812 
 813 #ifdef ASSERT
 814   LogStreamHandle(Trace, aot, codecache, stubs) log;
 815   if (log.is_enabled()) {
 816     FlagSetting fs(PrintRelocations, true);
 817     blob.print_on(&log);
 818   }
 819 #endif
 820   // we need to take a lock to prevent race between compiler threads generating AOT code
 821   // and the main thread generating adapter
 822   MutexLocker ml(Compile_lock);
 823   if (!is_on()) {
 824     return false; // AOT code cache was already dumped and closed.
 825   }
 826   if (!cache->align_write()) {
 827     return false;
 828   }
 829   uint entry_position = cache->_write_position;
 830 
 831   // Write name
 832   uint name_offset = cache->_write_position - entry_position;
 833   uint name_size = (uint)strlen(name) + 1; // Includes '/0'
 834   uint n = cache->write_bytes(name, name_size);
 835   if (n != name_size) {
 836     return false;
 837   }
 838 
 839   // Write CodeBlob
 840   if (!cache->align_write()) {
 841     return false;
 842   }
 843   uint blob_offset = cache->_write_position - entry_position;
 844   address archive_buffer = cache->reserve_bytes(blob.size());
 845   if (archive_buffer == nullptr) {
 846     return false;
 847   }
 848   CodeBlob::archive_blob(&blob, archive_buffer);
 849 
 850   uint reloc_data_size = blob.relocation_size();
 851   n = cache->write_bytes((address)blob.relocation_begin(), reloc_data_size);
 852   if (n != reloc_data_size) {
 853     return false;
 854   }
 855 
 856   bool has_oop_maps = false;
 857   if (blob.oop_maps() != nullptr) {
 858     if (!cache->write_oop_map_set(blob)) {
 859       return false;
 860     }
 861     has_oop_maps = true;
 862   }
 863 
 864 #ifndef PRODUCT
 865   // Write asm remarks
 866   if (!cache->write_asm_remarks(blob)) {
 867     return false;
 868   }
 869   if (!cache->write_dbg_strings(blob)) {
 870     return false;
 871   }
 872 #endif /* PRODUCT */
 873 
 874   if (!cache->write_relocations(blob)) {
 875     if (!cache->failed()) {
 876       // We may miss an address in AOT table - skip this code blob.
 877       cache->set_write_position(entry_position);
 878     }
 879     return false;
 880   }
 881 
 882   // Write entries offsets
 883   n = cache->write_bytes(&entry_offset_count, sizeof(int));
 884   if (n != sizeof(int)) {
 885     return false;
 886   }
 887   for (int i = 0; i < entry_offset_count; i++) {
 888     uint32_t off = (uint32_t)entry_offsets[i];
 889     n = cache->write_bytes(&off, sizeof(uint32_t));
 890     if (n != sizeof(uint32_t)) {
 891       return false;
 892     }
 893   }
 894   uint entry_size = cache->_write_position - entry_position;
 895   AOTCodeEntry* entry = new(cache) AOTCodeEntry(entry_kind, encode_id(entry_kind, id),
 896                                                 entry_position, entry_size, name_offset, name_size,
 897                                                 blob_offset, has_oop_maps, blob.content_begin());
 898   log_debug(aot, codecache, stubs)("Wrote code blob '%s' (id=%u, kind=%s) to AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
 899   return true;
 900 }
 901 
 902 CodeBlob* AOTCodeCache::load_code_blob(AOTCodeEntry::Kind entry_kind, uint id, const char* name, int entry_offset_count, int* entry_offsets) {
 903   AOTCodeCache* cache = open_for_use();
 904   if (cache == nullptr) {
 905     return nullptr;
 906   }
 907   assert(AOTCodeEntry::is_valid_entry_kind(entry_kind), "invalid entry_kind %d", entry_kind);
 908 
 909   if (AOTCodeEntry::is_adapter(entry_kind) && !is_using_adapter()) {
 910     return nullptr;
 911   }
 912   if (AOTCodeEntry::is_blob(entry_kind) && !is_using_stub()) {
 913     return nullptr;
 914   }
 915   log_debug(aot, codecache, stubs)("Reading blob '%s' (id=%u, kind=%s) from AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
 916 
 917   AOTCodeEntry* entry = cache->find_entry(entry_kind, encode_id(entry_kind, id));
 918   if (entry == nullptr) {
 919     return nullptr;
 920   }
 921   AOTCodeReader reader(cache, entry);
 922   CodeBlob* blob = reader.compile_code_blob(name, entry_offset_count, entry_offsets);
 923 
 924   log_debug(aot, codecache, stubs)("%sRead blob '%s' (id=%u, kind=%s) from AOT Code Cache",
 925                                    (blob == nullptr? "Failed to " : ""), name, id, aot_code_entry_kind_name[entry_kind]);
 926   return blob;
 927 }
 928 
 929 CodeBlob* AOTCodeReader::compile_code_blob(const char* name, int entry_offset_count, int* entry_offsets) {
 930   uint entry_position = _entry->offset();
 931 
 932   // Read name
 933   uint name_offset = entry_position + _entry->name_offset();
 934   uint name_size = _entry->name_size(); // Includes '/0'
 935   const char* stored_name = addr(name_offset);
 936 
 937   if (strncmp(stored_name, name, (name_size - 1)) != 0) {
 938     log_warning(aot, codecache, stubs)("Saved blob's name '%s' is different from the expected name '%s'",
 939                                        stored_name, name);
 940     set_lookup_failed(); // Skip this blob
 941     return nullptr;
 942   }
 943 
 944   // Read archived code blob
 945   uint offset = entry_position + _entry->blob_offset();
 946   CodeBlob* archived_blob = (CodeBlob*)addr(offset);
 947   offset += archived_blob->size();
 948 
 949   address reloc_data = (address)addr(offset);
 950   offset += archived_blob->relocation_size();
 951   set_read_position(offset);
 952 
 953   ImmutableOopMapSet* oop_maps = nullptr;
 954   if (_entry->has_oop_maps()) {
 955     oop_maps = read_oop_map_set();
 956   }
 957 
 958   CodeBlob* code_blob = CodeBlob::create(archived_blob,
 959                                          stored_name,
 960                                          reloc_data,
 961                                          oop_maps
 962                                         );
 963   if (code_blob == nullptr) { // no space left in CodeCache
 964     return nullptr;
 965   }
 966 
 967 #ifndef PRODUCT
 968   code_blob->asm_remarks().init();
 969   read_asm_remarks(code_blob->asm_remarks());
 970   code_blob->dbg_strings().init();
 971   read_dbg_strings(code_blob->dbg_strings());
 972 #endif // PRODUCT
 973 
 974   fix_relocations(code_blob);
 975 
 976   // Read entries offsets
 977   offset = read_position();
 978   int stored_count = *(int*)addr(offset);
 979   assert(stored_count == entry_offset_count, "entry offset count mismatch, count in AOT code cache=%d, expected=%d", stored_count, entry_offset_count);
 980   offset += sizeof(int);
 981   set_read_position(offset);
 982   for (int i = 0; i < stored_count; i++) {
 983     uint32_t off = *(uint32_t*)addr(offset);
 984     offset += sizeof(uint32_t);
 985     const char* entry_name = (_entry->kind() == AOTCodeEntry::Adapter) ? AdapterHandlerEntry::entry_name(i) : "";
 986     log_trace(aot, codecache, stubs)("Reading adapter '%s:%s' (0x%x) offset: 0x%x from AOT Code Cache",
 987                                       stored_name, entry_name, _entry->id(), off);
 988     entry_offsets[i] = off;
 989   }
 990 
 991 #ifdef ASSERT
 992   LogStreamHandle(Trace, aot, codecache, stubs) log;
 993   if (log.is_enabled()) {
 994     FlagSetting fs(PrintRelocations, true);
 995     code_blob->print_on(&log);
 996   }
 997 #endif
 998   return code_blob;
 999 }
1000 
1001 // ------------ process code and data --------------
1002 
1003 // Can't use -1. It is valid value for jump to iteself destination
1004 // used by static call stub: see NativeJump::jump_destination().
1005 #define BAD_ADDRESS_ID -2
1006 
1007 bool AOTCodeCache::write_relocations(CodeBlob& code_blob) {
1008   GrowableArray<uint> reloc_data;
1009   RelocIterator iter(&code_blob);
1010   LogStreamHandle(Trace, aot, codecache, reloc) log;
1011   while (iter.next()) {
1012     int idx = reloc_data.append(0); // default value
1013     switch (iter.type()) {
1014       case relocInfo::none:
1015         break;
1016       case relocInfo::runtime_call_type: {
1017         // Record offset of runtime destination
1018         CallRelocation* r = (CallRelocation*)iter.reloc();
1019         address dest = r->destination();
1020         if (dest == r->addr()) { // possible call via trampoline on Aarch64
1021           dest = (address)-1;    // do nothing in this case when loading this relocation
1022         }
1023         int id = _table->id_for_address(dest, iter, &code_blob);
1024         if (id == BAD_ADDRESS_ID) {
1025           return false;
1026         }
1027         reloc_data.at_put(idx, id);
1028         break;
1029       }
1030       case relocInfo::runtime_call_w_cp_type:
1031         log_debug(aot, codecache, reloc)("runtime_call_w_cp_type relocation is not implemented");
1032         return false;
1033       case relocInfo::external_word_type: {
1034         // Record offset of runtime target
1035         address target = ((external_word_Relocation*)iter.reloc())->target();
1036         int id = _table->id_for_address(target, iter, &code_blob);
1037         if (id == BAD_ADDRESS_ID) {
1038           return false;
1039         }
1040         reloc_data.at_put(idx, id);
1041         break;
1042       }
1043       case relocInfo::internal_word_type:
1044         break;
1045       case relocInfo::section_word_type:
1046         break;
1047       case relocInfo::post_call_nop_type:
1048         break;
1049       default:
1050         log_debug(aot, codecache, reloc)("relocation %d unimplemented", (int)iter.type());
1051         return false;
1052         break;
1053     }
1054     if (log.is_enabled()) {
1055       iter.print_current_on(&log);
1056     }
1057   }
1058 
1059   // Write additional relocation data: uint per relocation
1060   // Write the count first
1061   int count = reloc_data.length();
1062   write_bytes(&count, sizeof(int));
1063   for (GrowableArrayIterator<uint> iter = reloc_data.begin();
1064        iter != reloc_data.end(); ++iter) {
1065     uint value = *iter;
1066     int n = write_bytes(&value, sizeof(uint));
1067     if (n != sizeof(uint)) {
1068       return false;
1069     }
1070   }
1071   return true;
1072 }
1073 
1074 void AOTCodeReader::fix_relocations(CodeBlob* code_blob) {
1075   LogStreamHandle(Trace, aot, reloc) log;
1076   uint offset = read_position();
1077   int count = *(int*)addr(offset);
1078   offset += sizeof(int);
1079   if (log.is_enabled()) {
1080     log.print_cr("======== extra relocations count=%d", count);
1081   }
1082   uint* reloc_data = (uint*)addr(offset);
1083   offset += (count * sizeof(uint));
1084   set_read_position(offset);
1085 
1086   RelocIterator iter(code_blob);
1087   int j = 0;
1088   while (iter.next()) {
1089     switch (iter.type()) {
1090       case relocInfo::none:
1091         break;
1092       case relocInfo::runtime_call_type: {
1093         address dest = _cache->address_for_id(reloc_data[j]);
1094         if (dest != (address)-1) {
1095           ((CallRelocation*)iter.reloc())->set_destination(dest);
1096         }
1097         break;
1098       }
1099       case relocInfo::runtime_call_w_cp_type:
1100         // this relocation should not be in cache (see write_relocations)
1101         assert(false, "runtime_call_w_cp_type relocation is not implemented");
1102         break;
1103       case relocInfo::external_word_type: {
1104         address target = _cache->address_for_id(reloc_data[j]);
1105         // Add external address to global table
1106         int index = ExternalsRecorder::find_index(target);
1107         // Update index in relocation
1108         Relocation::add_jint(iter.data(), index);
1109         external_word_Relocation* reloc = (external_word_Relocation*)iter.reloc();
1110         assert(reloc->target() == target, "sanity");
1111         reloc->set_value(target); // Patch address in the code
1112         break;
1113       }
1114       case relocInfo::internal_word_type: {
1115         internal_word_Relocation* r = (internal_word_Relocation*)iter.reloc();
1116         r->fix_relocation_after_aot_load(aot_code_entry()->dumptime_content_start_addr(), code_blob->content_begin());
1117         break;
1118       }
1119       case relocInfo::section_word_type: {
1120         section_word_Relocation* r = (section_word_Relocation*)iter.reloc();
1121         r->fix_relocation_after_aot_load(aot_code_entry()->dumptime_content_start_addr(), code_blob->content_begin());
1122         break;
1123       }
1124       case relocInfo::post_call_nop_type:
1125         break;
1126       default:
1127         assert(false,"relocation %d unimplemented", (int)iter.type());
1128         break;
1129     }
1130     if (log.is_enabled()) {
1131       iter.print_current_on(&log);
1132     }
1133     j++;
1134   }
1135   assert(j == count, "sanity");
1136 }
1137 
1138 bool AOTCodeCache::write_oop_map_set(CodeBlob& cb) {
1139   ImmutableOopMapSet* oopmaps = cb.oop_maps();
1140   int oopmaps_size = oopmaps->nr_of_bytes();
1141   if (!write_bytes(&oopmaps_size, sizeof(int))) {
1142     return false;
1143   }
1144   uint n = write_bytes(oopmaps, oopmaps->nr_of_bytes());
1145   if (n != (uint)oopmaps->nr_of_bytes()) {
1146     return false;
1147   }
1148   return true;
1149 }
1150 
1151 ImmutableOopMapSet* AOTCodeReader::read_oop_map_set() {
1152   uint offset = read_position();
1153   int size = *(int *)addr(offset);
1154   offset += sizeof(int);
1155   ImmutableOopMapSet* oopmaps = (ImmutableOopMapSet *)addr(offset);
1156   offset += size;
1157   set_read_position(offset);
1158   return oopmaps;
1159 }
1160 
1161 #ifndef PRODUCT
1162 bool AOTCodeCache::write_asm_remarks(CodeBlob& cb) {
1163   // Write asm remarks
1164   uint* count_ptr = (uint *)reserve_bytes(sizeof(uint));
1165   if (count_ptr == nullptr) {
1166     return false;
1167   }
1168   uint count = 0;
1169   bool result = cb.asm_remarks().iterate([&] (uint offset, const char* str) -> bool {
1170     log_trace(aot, codecache, stubs)("asm remark offset=%d, str='%s'", offset, str);
1171     uint n = write_bytes(&offset, sizeof(uint));
1172     if (n != sizeof(uint)) {
1173       return false;
1174     }
1175     const char* cstr = add_C_string(str);
1176     int id = _table->id_for_C_string((address)cstr);
1177     assert(id != -1, "asm remark string '%s' not found in AOTCodeAddressTable", str);
1178     n = write_bytes(&id, sizeof(int));
1179     if (n != sizeof(int)) {
1180       return false;
1181     }
1182     count += 1;
1183     return true;
1184   });
1185   *count_ptr = count;
1186   return result;
1187 }
1188 
1189 void AOTCodeReader::read_asm_remarks(AsmRemarks& asm_remarks) {
1190   // Read asm remarks
1191   uint offset = read_position();
1192   uint count = *(uint *)addr(offset);
1193   offset += sizeof(uint);
1194   for (uint i = 0; i < count; i++) {
1195     uint remark_offset = *(uint *)addr(offset);
1196     offset += sizeof(uint);
1197     int remark_string_id = *(uint *)addr(offset);
1198     offset += sizeof(int);
1199     const char* remark = (const char*)_cache->address_for_C_string(remark_string_id);
1200     asm_remarks.insert(remark_offset, remark);
1201   }
1202   set_read_position(offset);
1203 }
1204 
1205 bool AOTCodeCache::write_dbg_strings(CodeBlob& cb) {
1206   // Write dbg strings
1207   uint* count_ptr = (uint *)reserve_bytes(sizeof(uint));
1208   if (count_ptr == nullptr) {
1209     return false;
1210   }
1211   uint count = 0;
1212   bool result = cb.dbg_strings().iterate([&] (const char* str) -> bool {
1213     log_trace(aot, codecache, stubs)("dbg string=%s", str);
1214     const char* cstr = add_C_string(str);
1215     int id = _table->id_for_C_string((address)cstr);
1216     assert(id != -1, "db string '%s' not found in AOTCodeAddressTable", str);
1217     uint n = write_bytes(&id, sizeof(int));
1218     if (n != sizeof(int)) {
1219       return false;
1220     }
1221     count += 1;
1222     return true;
1223   });
1224   *count_ptr = count;
1225   return result;
1226 }
1227 
1228 void AOTCodeReader::read_dbg_strings(DbgStrings& dbg_strings) {
1229   // Read dbg strings
1230   uint offset = read_position();
1231   uint count = *(uint *)addr(offset);
1232   offset += sizeof(uint);
1233   for (uint i = 0; i < count; i++) {
1234     int string_id = *(uint *)addr(offset);
1235     offset += sizeof(int);
1236     const char* str = (const char*)_cache->address_for_C_string(string_id);
1237     dbg_strings.insert(str);
1238   }
1239   set_read_position(offset);
1240 }
1241 #endif // PRODUCT
1242 
1243 //======================= AOTCodeAddressTable ===============
1244 
1245 // address table ids for generated routines, external addresses and C
1246 // string addresses are partitioned into positive integer ranges
1247 // defined by the following positive base and max values
1248 // i.e. [_extrs_base, _extrs_base + _extrs_max -1],
1249 //      [_blobs_base, _blobs_base + _blobs_max -1],
1250 //      ...
1251 //      [_c_str_base, _c_str_base + _c_str_max -1],
1252 
1253 #define _extrs_max 100
1254 #define _stubs_max 3
1255 
1256 #define _shared_blobs_max 20
1257 #define _C1_blobs_max 10
1258 #define _blobs_max (_shared_blobs_max+_C1_blobs_max)
1259 #define _all_max (_extrs_max+_stubs_max+_blobs_max)
1260 
1261 #define _extrs_base 0
1262 #define _stubs_base (_extrs_base + _extrs_max)
1263 #define _shared_blobs_base (_stubs_base + _stubs_max)
1264 #define _C1_blobs_base (_shared_blobs_base + _shared_blobs_max)
1265 #define _blobs_end  (_shared_blobs_base + _blobs_max)
1266 
1267 #define SET_ADDRESS(type, addr)                           \
1268   {                                                       \
1269     type##_addr[type##_length++] = (address) (addr);      \
1270     assert(type##_length <= type##_max, "increase size"); \
1271   }
1272 
1273 static bool initializing_extrs = false;
1274 
1275 void AOTCodeAddressTable::init_extrs() {
1276   if (_extrs_complete || initializing_extrs) return; // Done already
1277 
1278   assert(_blobs_end <= _all_max, "AOTCodeAddress table ranges need adjusting");
1279 
1280   initializing_extrs = true;
1281   _extrs_addr = NEW_C_HEAP_ARRAY(address, _extrs_max, mtCode);
1282 
1283   _extrs_length = 0;
1284 
1285   // Record addresses of VM runtime methods
1286   SET_ADDRESS(_extrs, SharedRuntime::fixup_callers_callsite);
1287   SET_ADDRESS(_extrs, SharedRuntime::handle_wrong_method);
1288   SET_ADDRESS(_extrs, SharedRuntime::handle_wrong_method_abstract);
1289   SET_ADDRESS(_extrs, SharedRuntime::handle_wrong_method_ic_miss);
1290   SET_ADDRESS(_extrs, SharedRuntime::allocate_inline_types);
1291 #if defined(AARCH64) && !defined(ZERO)
1292   SET_ADDRESS(_extrs, JavaThread::aarch64_get_thread_helper);
1293 #endif
1294   {
1295     // Required by Shared blobs
1296     SET_ADDRESS(_extrs, Deoptimization::fetch_unroll_info);
1297     SET_ADDRESS(_extrs, Deoptimization::unpack_frames);
1298     SET_ADDRESS(_extrs, SafepointSynchronize::handle_polling_page_exception);
1299     SET_ADDRESS(_extrs, SharedRuntime::resolve_opt_virtual_call_C);
1300     SET_ADDRESS(_extrs, SharedRuntime::resolve_virtual_call_C);
1301     SET_ADDRESS(_extrs, SharedRuntime::resolve_static_call_C);
1302     SET_ADDRESS(_extrs, SharedRuntime::throw_delayed_StackOverflowError);
1303     SET_ADDRESS(_extrs, SharedRuntime::throw_AbstractMethodError);
1304     SET_ADDRESS(_extrs, SharedRuntime::throw_IncompatibleClassChangeError);
1305     SET_ADDRESS(_extrs, SharedRuntime::throw_NullPointerException_at_call);
1306   }
1307 
1308 #ifdef COMPILER1
1309   {
1310     // Required by C1 blobs
1311     SET_ADDRESS(_extrs, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc));
1312     SET_ADDRESS(_extrs, SharedRuntime::exception_handler_for_return_address);
1313     SET_ADDRESS(_extrs, SharedRuntime::register_finalizer);
1314     SET_ADDRESS(_extrs, Runtime1::is_instance_of);
1315     SET_ADDRESS(_extrs, Runtime1::exception_handler_for_pc);
1316     SET_ADDRESS(_extrs, Runtime1::check_abort_on_vm_exception);
1317     SET_ADDRESS(_extrs, Runtime1::new_instance);
1318     SET_ADDRESS(_extrs, Runtime1::counter_overflow);
1319     SET_ADDRESS(_extrs, Runtime1::new_type_array);
1320     SET_ADDRESS(_extrs, Runtime1::new_object_array);
1321     SET_ADDRESS(_extrs, Runtime1::new_multi_array);
1322     SET_ADDRESS(_extrs, Runtime1::throw_range_check_exception);
1323     SET_ADDRESS(_extrs, Runtime1::throw_index_exception);
1324     SET_ADDRESS(_extrs, Runtime1::throw_div0_exception);
1325     SET_ADDRESS(_extrs, Runtime1::throw_null_pointer_exception);
1326     SET_ADDRESS(_extrs, Runtime1::throw_array_store_exception);
1327     SET_ADDRESS(_extrs, Runtime1::throw_class_cast_exception);
1328     SET_ADDRESS(_extrs, Runtime1::throw_incompatible_class_change_error);
1329     SET_ADDRESS(_extrs, Runtime1::is_instance_of);
1330     SET_ADDRESS(_extrs, Runtime1::monitorenter);
1331     SET_ADDRESS(_extrs, Runtime1::monitorexit);
1332     SET_ADDRESS(_extrs, Runtime1::deoptimize);
1333     SET_ADDRESS(_extrs, Runtime1::access_field_patching);
1334     SET_ADDRESS(_extrs, Runtime1::move_klass_patching);
1335     SET_ADDRESS(_extrs, Runtime1::move_mirror_patching);
1336     SET_ADDRESS(_extrs, Runtime1::move_appendix_patching);
1337     SET_ADDRESS(_extrs, Runtime1::predicate_failed_trap);
1338     SET_ADDRESS(_extrs, Runtime1::unimplemented_entry);
1339     SET_ADDRESS(_extrs, Runtime1::new_null_free_array);
1340     SET_ADDRESS(_extrs, Runtime1::load_flat_array);
1341     SET_ADDRESS(_extrs, Runtime1::store_flat_array);
1342     SET_ADDRESS(_extrs, Runtime1::substitutability_check);
1343     SET_ADDRESS(_extrs, Runtime1::buffer_inline_args);
1344     SET_ADDRESS(_extrs, Runtime1::buffer_inline_args_no_receiver);
1345     SET_ADDRESS(_extrs, Runtime1::throw_identity_exception);
1346     SET_ADDRESS(_extrs, Runtime1::throw_illegal_monitor_state_exception);
1347     SET_ADDRESS(_extrs, Thread::current);
1348     SET_ADDRESS(_extrs, CompressedKlassPointers::base_addr());
1349 #ifndef PRODUCT
1350     SET_ADDRESS(_extrs, os::breakpoint);
1351 #endif
1352   }
1353 #endif
1354 
1355 #ifdef COMPILER2
1356   {
1357     // Required by C2 blobs
1358     SET_ADDRESS(_extrs, Deoptimization::uncommon_trap);
1359     SET_ADDRESS(_extrs, OptoRuntime::handle_exception_C);
1360     SET_ADDRESS(_extrs, OptoRuntime::new_instance_C);
1361     SET_ADDRESS(_extrs, OptoRuntime::new_array_C);
1362     SET_ADDRESS(_extrs, OptoRuntime::new_array_nozero_C);
1363     SET_ADDRESS(_extrs, OptoRuntime::multianewarray2_C);
1364     SET_ADDRESS(_extrs, OptoRuntime::multianewarray3_C);
1365     SET_ADDRESS(_extrs, OptoRuntime::multianewarray4_C);
1366     SET_ADDRESS(_extrs, OptoRuntime::multianewarray5_C);
1367     SET_ADDRESS(_extrs, OptoRuntime::multianewarrayN_C);
1368 #if INCLUDE_JVMTI
1369     SET_ADDRESS(_extrs, SharedRuntime::notify_jvmti_vthread_start);
1370     SET_ADDRESS(_extrs, SharedRuntime::notify_jvmti_vthread_end);
1371     SET_ADDRESS(_extrs, SharedRuntime::notify_jvmti_vthread_mount);
1372     SET_ADDRESS(_extrs, SharedRuntime::notify_jvmti_vthread_unmount);
1373 #endif
1374     SET_ADDRESS(_extrs, OptoRuntime::complete_monitor_locking_C);
1375     SET_ADDRESS(_extrs, OptoRuntime::monitor_notify_C);
1376     SET_ADDRESS(_extrs, OptoRuntime::monitor_notifyAll_C);
1377     SET_ADDRESS(_extrs, OptoRuntime::rethrow_C);
1378     SET_ADDRESS(_extrs, OptoRuntime::slow_arraycopy_C);
1379     SET_ADDRESS(_extrs, OptoRuntime::register_finalizer_C);
1380     SET_ADDRESS(_extrs, OptoRuntime::load_unknown_inline_C);
1381     SET_ADDRESS(_extrs, OptoRuntime::store_unknown_inline_C);
1382 #if defined(AARCH64)
1383     SET_ADDRESS(_extrs, JavaThread::verify_cross_modify_fence_failure);
1384 #endif // AARCH64
1385   }
1386 #endif // COMPILER2
1387 
1388 #if INCLUDE_G1GC
1389   SET_ADDRESS(_extrs, G1BarrierSetRuntime::write_ref_field_post_entry);
1390   SET_ADDRESS(_extrs, G1BarrierSetRuntime::write_ref_field_pre_entry);
1391 #endif
1392 #if INCLUDE_SHENANDOAHGC
1393   SET_ADDRESS(_extrs, ShenandoahRuntime::write_ref_field_pre);
1394   SET_ADDRESS(_extrs, ShenandoahRuntime::load_reference_barrier_phantom);
1395   SET_ADDRESS(_extrs, ShenandoahRuntime::load_reference_barrier_phantom_narrow);
1396 #endif
1397 #if INCLUDE_ZGC
1398   SET_ADDRESS(_extrs, ZBarrierSetRuntime::load_barrier_on_phantom_oop_field_preloaded_addr());
1399 #if defined(AMD64)
1400   SET_ADDRESS(_extrs, &ZPointerLoadShift);
1401 #endif
1402 #endif
1403 #ifndef ZERO
1404 #if defined(AMD64) || defined(AARCH64) || defined(RISCV64)
1405   SET_ADDRESS(_extrs, MacroAssembler::debug64);
1406 #endif
1407 #endif // ZERO
1408 
1409   if (UseCompressedOops) {
1410     SET_ADDRESS(_extrs, CompressedOops::base_addr());
1411   }
1412 
1413   _extrs_complete = true;
1414   log_debug(aot, codecache, init)("External addresses recorded");
1415 }
1416 
1417 static bool initializing_early_stubs = false;
1418 
1419 void AOTCodeAddressTable::init_early_stubs() {
1420   if (_complete || initializing_early_stubs) return; // Done already
1421   initializing_early_stubs = true;
1422   _stubs_addr = NEW_C_HEAP_ARRAY(address, _stubs_max, mtCode);
1423   _stubs_length = 0;
1424   SET_ADDRESS(_stubs, StubRoutines::forward_exception_entry());
1425 
1426   {
1427     // Required by C1 blobs
1428 #if defined(AMD64) && !defined(ZERO)
1429     SET_ADDRESS(_stubs, StubRoutines::x86::double_sign_flip());
1430     SET_ADDRESS(_stubs, StubRoutines::x86::d2l_fixup());
1431 #endif // AMD64
1432   }
1433 
1434   _early_stubs_complete = true;
1435   log_info(aot, codecache, init)("Early stubs recorded");
1436 }
1437 
1438 static bool initializing_shared_blobs = false;
1439 
1440 void AOTCodeAddressTable::init_shared_blobs() {
1441   if (_complete || initializing_shared_blobs) return; // Done already
1442   initializing_shared_blobs = true;
1443   address* blobs_addr = NEW_C_HEAP_ARRAY(address, _blobs_max, mtCode);
1444 
1445   // Divide _shared_blobs_addr array to chunks because they could be initialized in parrallel
1446   _shared_blobs_addr = blobs_addr;
1447   _C1_blobs_addr = _shared_blobs_addr + _shared_blobs_max;
1448 
1449   _shared_blobs_length = 0;
1450   _C1_blobs_length = 0;
1451 
1452   // clear the address table
1453   memset(blobs_addr, 0, sizeof(address)* _blobs_max);
1454 
1455   // Record addresses of generated code blobs
1456   SET_ADDRESS(_shared_blobs, SharedRuntime::get_handle_wrong_method_stub());
1457   SET_ADDRESS(_shared_blobs, SharedRuntime::get_ic_miss_stub());
1458   SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->unpack());
1459   SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->unpack_with_exception());
1460   SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->unpack_with_reexecution());
1461   SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->unpack_with_exception_in_tls());
1462 #if INCLUDE_JVMCI
1463   if (EnableJVMCI) {
1464     SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->uncommon_trap());
1465     SET_ADDRESS(_shared_blobs, SharedRuntime::deopt_blob()->implicit_exception_uncommon_trap());
1466   }
1467 #endif
1468 
1469   _shared_blobs_complete = true;
1470   log_debug(aot, codecache, init)("Early shared blobs recorded");
1471   _complete = true;
1472 }
1473 
1474 void AOTCodeAddressTable::init_early_c1() {
1475 #ifdef COMPILER1
1476   // Runtime1 Blobs
1477   for (int i = 0; i <= (int)C1StubId::forward_exception_id; i++) {
1478     C1StubId id = (C1StubId)i;
1479     if (Runtime1::blob_for(id) == nullptr) {
1480       log_info(aot, codecache, init)("C1 blob %s is missing", Runtime1::name_for(id));
1481       continue;
1482     }
1483     if (Runtime1::entry_for(id) == nullptr) {
1484       log_info(aot, codecache, init)("C1 blob %s is missing entry", Runtime1::name_for(id));
1485       continue;
1486     }
1487     address entry = Runtime1::entry_for(id);
1488     SET_ADDRESS(_C1_blobs, entry);
1489   }
1490 #endif // COMPILER1
1491   assert(_C1_blobs_length <= _C1_blobs_max, "increase _C1_blobs_max to %d", _C1_blobs_length);
1492   _early_c1_complete = true;
1493 }
1494 
1495 #undef SET_ADDRESS
1496 
1497 AOTCodeAddressTable::~AOTCodeAddressTable() {
1498   if (_extrs_addr != nullptr) {
1499     FREE_C_HEAP_ARRAY(address, _extrs_addr);
1500   }
1501   if (_stubs_addr != nullptr) {
1502     FREE_C_HEAP_ARRAY(address, _stubs_addr);
1503   }
1504   if (_shared_blobs_addr != nullptr) {
1505     FREE_C_HEAP_ARRAY(address, _shared_blobs_addr);
1506   }
1507 }
1508 
1509 #ifdef PRODUCT
1510 #define MAX_STR_COUNT 200
1511 #else
1512 #define MAX_STR_COUNT 500
1513 #endif
1514 #define _c_str_max  MAX_STR_COUNT
1515 static const int _c_str_base = _all_max;
1516 
1517 static const char* _C_strings_in[MAX_STR_COUNT] = {nullptr}; // Incoming strings
1518 static const char* _C_strings[MAX_STR_COUNT]    = {nullptr}; // Our duplicates
1519 static int _C_strings_count = 0;
1520 static int _C_strings_s[MAX_STR_COUNT] = {0};
1521 static int _C_strings_id[MAX_STR_COUNT] = {0};
1522 static int _C_strings_used = 0;
1523 
1524 void AOTCodeCache::load_strings() {
1525   uint strings_count  = _load_header->strings_count();
1526   if (strings_count == 0) {
1527     return;
1528   }
1529   uint strings_offset = _load_header->strings_offset();
1530   uint* string_lengths = (uint*)addr(strings_offset);
1531   strings_offset += (strings_count * sizeof(uint));
1532   uint strings_size = _load_header->entries_offset() - strings_offset;
1533   // We have to keep cached strings longer than _cache buffer
1534   // because they are refernced from compiled code which may
1535   // still be executed on VM exit after _cache is freed.
1536   char* p = NEW_C_HEAP_ARRAY(char, strings_size+1, mtCode);
1537   memcpy(p, addr(strings_offset), strings_size);
1538   _C_strings_buf = p;
1539   assert(strings_count <= MAX_STR_COUNT, "sanity");
1540   for (uint i = 0; i < strings_count; i++) {
1541     _C_strings[i] = p;
1542     uint len = string_lengths[i];
1543     _C_strings_s[i] = i;
1544     _C_strings_id[i] = i;
1545     p += len;
1546   }
1547   assert((uint)(p - _C_strings_buf) <= strings_size, "(" INTPTR_FORMAT " - " INTPTR_FORMAT ") = %d > %d ", p2i(p), p2i(_C_strings_buf), (uint)(p - _C_strings_buf), strings_size);
1548   _C_strings_count = strings_count;
1549   _C_strings_used  = strings_count;
1550   log_debug(aot, codecache, init)("  Loaded %d C strings of total length %d at offset %d from AOT Code Cache", _C_strings_count, strings_size, strings_offset);
1551 }
1552 
1553 int AOTCodeCache::store_strings() {
1554   if (_C_strings_used > 0) {
1555     MutexLocker ml(AOTCodeCStrings_lock, Mutex::_no_safepoint_check_flag);
1556     uint offset = _write_position;
1557     uint length = 0;
1558     uint* lengths = (uint *)reserve_bytes(sizeof(uint) * _C_strings_used);
1559     if (lengths == nullptr) {
1560       return -1;
1561     }
1562     for (int i = 0; i < _C_strings_used; i++) {
1563       const char* str = _C_strings[_C_strings_s[i]];
1564       uint len = (uint)strlen(str) + 1;
1565       length += len;
1566       assert(len < 1000, "big string: %s", str);
1567       lengths[i] = len;
1568       uint n = write_bytes(str, len);
1569       if (n != len) {
1570         return -1;
1571       }
1572     }
1573     log_debug(aot, codecache, exit)("  Wrote %d C strings of total length %d at offset %d to AOT Code Cache",
1574                                    _C_strings_used, length, offset);
1575   }
1576   return _C_strings_used;
1577 }
1578 
1579 const char* AOTCodeCache::add_C_string(const char* str) {
1580   if (is_on_for_dump() && str != nullptr) {
1581     MutexLocker ml(AOTCodeCStrings_lock, Mutex::_no_safepoint_check_flag);
1582     AOTCodeAddressTable* table = addr_table();
1583     if (table != nullptr) {
1584       return table->add_C_string(str);
1585     }
1586   }
1587   return str;
1588 }
1589 
1590 const char* AOTCodeAddressTable::add_C_string(const char* str) {
1591   if (_extrs_complete) {
1592     // Check previous strings address
1593     for (int i = 0; i < _C_strings_count; i++) {
1594       if (_C_strings_in[i] == str) {
1595         return _C_strings[i]; // Found previous one - return our duplicate
1596       } else if (strcmp(_C_strings[i], str) == 0) {
1597         return _C_strings[i];
1598       }
1599     }
1600     // Add new one
1601     if (_C_strings_count < MAX_STR_COUNT) {
1602       // Passed in string can be freed and used space become inaccessible.
1603       // Keep original address but duplicate string for future compare.
1604       _C_strings_id[_C_strings_count] = -1; // Init
1605       _C_strings_in[_C_strings_count] = str;
1606       const char* dup = os::strdup(str);
1607       _C_strings[_C_strings_count++] = dup;
1608       log_trace(aot, codecache, stringtable)("add_C_string: [%d] " INTPTR_FORMAT " '%s'", _C_strings_count, p2i(dup), dup);
1609       return dup;
1610     } else {
1611       assert(false, "Number of C strings >= MAX_STR_COUNT");
1612     }
1613   }
1614   return str;
1615 }
1616 
1617 int AOTCodeAddressTable::id_for_C_string(address str) {
1618   if (str == nullptr) {
1619     return -1;
1620   }
1621   MutexLocker ml(AOTCodeCStrings_lock, Mutex::_no_safepoint_check_flag);
1622   for (int i = 0; i < _C_strings_count; i++) {
1623     if (_C_strings[i] == (const char*)str) { // found
1624       int id = _C_strings_id[i];
1625       if (id >= 0) {
1626         assert(id < _C_strings_used, "%d >= %d", id , _C_strings_used);
1627         return id; // Found recorded
1628       }
1629       // Not found in recorded, add new
1630       id = _C_strings_used++;
1631       _C_strings_s[id] = i;
1632       _C_strings_id[i] = id;
1633       return id;
1634     }
1635   }
1636   return -1;
1637 }
1638 
1639 address AOTCodeAddressTable::address_for_C_string(int idx) {
1640   assert(idx < _C_strings_count, "sanity");
1641   return (address)_C_strings[idx];
1642 }
1643 
1644 static int search_address(address addr, address* table, uint length) {
1645   for (int i = 0; i < (int)length; i++) {
1646     if (table[i] == addr) {
1647       return i;
1648     }
1649   }
1650   return BAD_ADDRESS_ID;
1651 }
1652 
1653 address AOTCodeAddressTable::address_for_id(int idx) {
1654   assert(_extrs_complete, "AOT Code Cache VM runtime addresses table is not complete");
1655   if (idx == -1) {
1656     return (address)-1;
1657   }
1658   uint id = (uint)idx;
1659   // special case for symbols based relative to os::init
1660   if (id > (_c_str_base + _c_str_max)) {
1661     return (address)os::init + idx;
1662   }
1663   if (idx < 0) {
1664     fatal("Incorrect id %d for AOT Code Cache addresses table", id);
1665     return nullptr;
1666   }
1667   // no need to compare unsigned id against 0
1668   if (/* id >= _extrs_base && */ id < _extrs_length) {
1669     return _extrs_addr[id - _extrs_base];
1670   }
1671   if (id >= _stubs_base && id < _stubs_base + _stubs_length) {
1672     return _stubs_addr[id - _stubs_base];
1673   }
1674   if (id >= _shared_blobs_base && id < _shared_blobs_base + _shared_blobs_length) {
1675     return _shared_blobs_addr[id - _shared_blobs_base];
1676   }
1677   if (id >= _C1_blobs_base && id < _C1_blobs_base + _C1_blobs_length) {
1678     return _C1_blobs_addr[id - _C1_blobs_base];
1679   }
1680   if (id >= _c_str_base && id < (_c_str_base + (uint)_C_strings_count)) {
1681     return address_for_C_string(id - _c_str_base);
1682   }
1683   fatal("Incorrect id %d for AOT Code Cache addresses table", id);
1684   return nullptr;
1685 }
1686 
1687 int AOTCodeAddressTable::id_for_address(address addr, RelocIterator reloc, CodeBlob* code_blob) {
1688   assert(_extrs_complete, "AOT Code Cache VM runtime addresses table is not complete");
1689   int id = -1;
1690   if (addr == (address)-1) { // Static call stub has jump to itself
1691     return id;
1692   }
1693   // Seach for C string
1694   id = id_for_C_string(addr);
1695   if (id >= 0) {
1696     return id + _c_str_base;
1697   }
1698   if (StubRoutines::contains(addr)) {
1699     // Search in stubs
1700     id = search_address(addr, _stubs_addr, _stubs_length);
1701     if (id < 0) {
1702       StubCodeDesc* desc = StubCodeDesc::desc_for(addr);
1703       if (desc == nullptr) {
1704         desc = StubCodeDesc::desc_for(addr + frame::pc_return_offset);
1705       }
1706       const char* sub_name = (desc != nullptr) ? desc->name() : "<unknown>";
1707       assert(false, "Address " INTPTR_FORMAT " for Stub:%s is missing in AOT Code Cache addresses table", p2i(addr), sub_name);
1708     } else {
1709       return id + _stubs_base;
1710     }
1711   } else {
1712     CodeBlob* cb = CodeCache::find_blob(addr);
1713     if (cb != nullptr) {
1714       // Search in code blobs
1715       int id_base = _shared_blobs_base;
1716       id = search_address(addr, _shared_blobs_addr, _blobs_max);
1717       if (id < 0) {
1718         assert(false, "Address " INTPTR_FORMAT " for Blob:%s is missing in AOT Code Cache addresses table", p2i(addr), cb->name());
1719       } else {
1720         return id_base + id;
1721       }
1722     } else {
1723       // Search in runtime functions
1724       id = search_address(addr, _extrs_addr, _extrs_length);
1725       if (id < 0) {
1726         ResourceMark rm;
1727         const int buflen = 1024;
1728         char* func_name = NEW_RESOURCE_ARRAY(char, buflen);
1729         int offset = 0;
1730         if (os::dll_address_to_function_name(addr, func_name, buflen, &offset)) {
1731           if (offset > 0) {
1732             // Could be address of C string
1733             uint dist = (uint)pointer_delta(addr, (address)os::init, 1);
1734             log_debug(aot, codecache)("Address " INTPTR_FORMAT " (offset %d) for runtime target '%s' is missing in AOT Code Cache addresses table",
1735                                       p2i(addr), dist, (const char*)addr);
1736             assert(dist > (uint)(_all_max + MAX_STR_COUNT), "change encoding of distance");
1737             return dist;
1738           }
1739 #ifdef ASSERT
1740           reloc.print_current_on(tty);
1741           code_blob->print_on(tty);
1742           code_blob->print_code_on(tty);
1743           assert(false, "Address " INTPTR_FORMAT " for runtime target '%s+%d' is missing in AOT Code Cache addresses table", p2i(addr), func_name, offset);
1744 #endif
1745         } else {
1746 #ifdef ASSERT
1747           reloc.print_current_on(tty);
1748           code_blob->print_on(tty);
1749           code_blob->print_code_on(tty);
1750           os::find(addr, tty);
1751           assert(false, "Address " INTPTR_FORMAT " for <unknown>/('%s') is missing in AOT Code Cache addresses table", p2i(addr), (const char*)addr);
1752 #endif
1753         }
1754       } else {
1755         return _extrs_base + id;
1756       }
1757     }
1758   }
1759   return id;
1760 }
1761 
1762 // This is called after initialize() but before init2()
1763 // and _cache is not set yet.
1764 void AOTCodeCache::print_on(outputStream* st) {
1765   if (opened_cache != nullptr && opened_cache->for_use()) {
1766     st->print_cr("\nAOT Code Cache");
1767     uint count = opened_cache->_load_header->entries_count();
1768     uint* search_entries = (uint*)opened_cache->addr(opened_cache->_load_header->entries_offset()); // [id, index]
1769     AOTCodeEntry* load_entries = (AOTCodeEntry*)(search_entries + 2 * count);
1770 
1771     for (uint i = 0; i < count; i++) {
1772       // Use search_entries[] to order ouput
1773       int index = search_entries[2*i + 1];
1774       AOTCodeEntry* entry = &(load_entries[index]);
1775 
1776       uint entry_position = entry->offset();
1777       uint name_offset = entry->name_offset() + entry_position;
1778       const char* saved_name = opened_cache->addr(name_offset);
1779 
1780       st->print_cr("%4u: %10s idx:%4u Id:%u size=%u '%s'",
1781                    i, aot_code_entry_kind_name[entry->kind()], index, entry->id(), entry->size(), saved_name);
1782     }
1783   }
1784 }