1 /*
  2  * Copyright (c) 2023, 2026, 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 #ifndef SHARE_CODE_AOTCODECACHE_HPP
 26 #define SHARE_CODE_AOTCODECACHE_HPP
 27 
 28 #include "compiler/compilerDefinitions.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "nmt/memTag.hpp"
 31 #include "oops/oopsHierarchy.hpp"
 32 #include "runtime/stubInfo.hpp"
 33 #include "runtime/vm_version.hpp"
 34 #include "utilities/exceptions.hpp"
 35 #include "utilities/sizes.hpp"
 36 
 37 /*
 38  * AOT Code Cache collects code from Code Cache and corresponding metadata
 39  * during application training run.
 40  * In following "production" runs this code and data can be loaded into
 41  * Code Cache skipping its generation.
 42  * Additionaly special compiled code "preload" is generated with class initialization
 43  * barriers which can be called on first Java method invocation.
 44  */
 45 
 46 class AbstractCompiler;
 47 class AOTCodeCache;
 48 class AsmRemarks;
 49 class ciConstant;
 50 class ciEnv;
 51 class ciMethod;
 52 class CodeBlob;
 53 class CodeOffsets;
 54 class CompileTask;
 55 class DbgStrings;
 56 class DebugInformationRecorder;
 57 class Dependencies;
 58 class ExceptionTable;
 59 class ExceptionHandlerTable;
 60 template<typename E>
 61 class GrowableArray;
 62 class ImmutableOopMapSet;
 63 class ImplicitExceptionTable;
 64 class JavaThread;
 65 class Klass;
 66 class methodHandle;
 67 class Metadata;
 68 class Method;
 69 class nmethod;
 70 class OopMapSet;
 71 class OopRecorder;
 72 class outputStream;
 73 class RelocIterator;
 74 class StubCodeGenerator;
 75 
 76 enum class vmIntrinsicID : int;
 77 
 78 #define DO_AOTCODEENTRY_KIND(Fn) \
 79   Fn(None) \
 80   Fn(Adapter) \
 81   Fn(Stub) \
 82   Fn(SharedBlob) \
 83   Fn(C1Blob) \
 84   Fn(C2Blob) \
 85   Fn(Nmethod) \
 86 
 87 // Descriptor of AOT Code Cache's entry
 88 class AOTCodeEntry {
 89   friend class VMStructs;
 90 public:
 91   enum Kind : s1 {
 92 #define DECL_KIND_ENUM(kind) kind,
 93     DO_AOTCODEENTRY_KIND(DECL_KIND_ENUM)
 94 #undef DECL_KIND_ENUM
 95     Kind_count
 96   };
 97 
 98 private:
 99   Kind    _kind;
100   // Next field is exposed to external profilers - keep it as boolean.
101   bool    _for_preload;           // Code can be used for preload (before classes initialized)
102   uint8_t _has_clinit_barriers:1, // Generated code has class init checks (ony in for_preload code)
103           _has_oop_maps:1,
104           _loaded:1,              // Code was loaded for use
105           _load_fail,             // Failed to load due to some klass state
106           _not_entrant;           // Deoptimized
107 
108   uint   _id;          // Adapter's id, vmIntrinsic::ID for stub or Method's offset in AOTCache for nmethod
109   uint   _offset;      // Offset to entry
110   uint   _size;        // Entry size
111   uint   _name_offset; // Method's or intrinsic name
112   uint   _name_size;
113   uint   _num_inlined_bytecodes;
114   uint   _code_offset; // Start of code in cache
115   uint   _code_size;   // Total size of all code sections
116 
117   uint   _comp_level;  // compilation level
118   uint   _comp_id;     // compilation id
119 public:
120   // this constructor is used only by AOTCodeEntry::Stub
121   AOTCodeEntry(uint offset, uint size, uint name_offset, uint name_size,
122                uint code_offset, uint code_size,
123                Kind kind, uint id) {
124     assert(kind == AOTCodeEntry::Stub, "sanity check");
125     _kind         = kind;
126     _id           = id;
127     _offset       = offset;
128     _size         = size;
129     _name_offset  = name_offset;
130     _name_size    = name_size;
131     _code_offset  = code_offset;
132     _code_size    = code_size;
133 
134     _num_inlined_bytecodes = 0;
135     _comp_level   = 0;
136     _comp_id      = 0;
137     _has_oop_maps = false; // unused here
138     _has_clinit_barriers = false;
139     _for_preload  = false;
140     _loaded       = false;
141     _not_entrant  = false;
142     _load_fail    = false;
143   }
144 
145   AOTCodeEntry(Kind kind,         uint id,
146                uint offset,       uint size,
147                uint name_offset,  uint name_size,
148                uint blob_offset,  bool has_oop_maps,
149                uint comp_level = 0,
150                uint comp_id = 0,
151                bool has_clinit_barriers = false,
152                bool for_preload = false) {
153     _kind         = kind;
154     _id           = id;
155     _offset       = offset;
156     _size         = size;
157     _name_offset  = name_offset;
158     _name_size    = name_size;
159     _code_offset  = blob_offset;
160     _code_size    = 0; // unused
161 
162     _num_inlined_bytecodes = 0;
163     _comp_level   = comp_level;
164     _comp_id      = comp_id;
165     _has_oop_maps = has_oop_maps;
166     _has_clinit_barriers = has_clinit_barriers;
167     _for_preload  = for_preload;
168     _loaded       = false;
169     _not_entrant  = false;
170     _load_fail    = false;
171 
172     _loaded       = false;
173     _not_entrant  = false;
174     _load_fail    = false;
175   }
176 
177   void* operator new(size_t x, AOTCodeCache* cache);
178   // Delete is a NOP
179   void operator delete( void *ptr ) {}
180 
181   Method* method();
182 
183   Kind kind()         const { return _kind; }
184   uint id()           const { return _id; }
185 
186   uint offset()       const { return _offset; }
187   void set_offset(uint off) { _offset = off; }
188 
189   uint size()         const { return _size; }
190   uint name_offset()  const { return _name_offset; }
191   uint name_size()    const { return _name_size; }
192   uint code_offset()  const { return _code_offset; }
193   uint code_size()    const { return _code_size; }
194 
195   bool has_oop_maps() const { return _has_oop_maps; }
196   uint num_inlined_bytecodes() const { return _num_inlined_bytecodes; }
197   void set_inlined_bytecodes(int bytes) { _num_inlined_bytecodes = bytes; }
198 
199   uint comp_level()   const { return _comp_level; }
200   uint comp_id()      const { return _comp_id; }
201 
202   bool has_clinit_barriers() const { return _has_clinit_barriers; }
203   bool for_preload()  const { return _for_preload; }
204   bool is_loaded()    const { return _loaded; }
205   void set_loaded()         { _loaded = true; }
206 
207   bool not_entrant()  const { return _not_entrant; }
208   void set_not_entrant()    { _not_entrant = true; }
209   void set_entrant()        { _not_entrant = false; }
210 
211   bool load_fail()  const { return _load_fail; }
212   void set_load_fail()    { _load_fail = true; }
213 
214   void print(outputStream* st) const NOT_CDS_RETURN;
215 
216   static bool is_valid_entry_kind(Kind kind) { return kind > None && kind < Kind_count; }
217   static bool is_blob(Kind kind) { return kind == SharedBlob || kind == C1Blob || kind == C2Blob; }
218   static bool is_adapter(Kind kind) { return kind == Adapter; }
219   bool is_nmethod()  { return _kind == Nmethod; }
220 };
221 
222 // Addresses of stubs, blobs and runtime finctions called from compiled code.
223 class AOTCodeAddressTable : public CHeapObj<mtCode> {
224 private:
225   address* _extrs_addr;
226   address* _stubs_addr;
227   address* _shared_blobs_addr;
228   address* _C1_blobs_addr;
229   address* _C2_blobs_addr;
230   uint     _extrs_length;
231   uint     _stubs_length;
232   uint     _shared_blobs_length;
233   uint     _C1_blobs_length;
234   uint     _C2_blobs_length;
235 
236   bool _extrs_complete;
237   bool _early_stubs_complete;
238   bool _shared_blobs_complete;
239   bool _early_c1_complete;
240   bool _c1_complete;
241   bool _c2_complete;
242   bool _complete;
243 
244 public:
245   AOTCodeAddressTable() :
246     _extrs_addr(nullptr),
247     _stubs_addr(nullptr),
248     _shared_blobs_addr(nullptr),
249     _C1_blobs_addr(nullptr),
250     _C2_blobs_addr(nullptr),
251     _extrs_length(0),
252     _stubs_length(0),
253     _shared_blobs_length(0),
254     _C1_blobs_length(0),
255     _C2_blobs_length(0),
256     _extrs_complete(false),
257     _early_stubs_complete(false),
258     _shared_blobs_complete(false),
259     _early_c1_complete(false),
260     _c1_complete(false),
261     _c2_complete(false),
262     _complete(false)
263   { }
264   ~AOTCodeAddressTable();
265   void init_extrs();
266   void init_early_stubs();
267   void init_shared_blobs();
268   void init_stubs();
269   void init_early_c1();
270   void init_c1();
271   void init_c2();
272   const char* add_C_string(const char* str);
273   int  id_for_C_string(address str);
274   address address_for_C_string(int idx);
275   int  id_for_address(address addr, RelocIterator iter, CodeBlob* blob);
276   address address_for_id(int id);
277   bool c2_complete() const { return _c2_complete; }
278   bool c1_complete() const { return _c1_complete; }
279 };
280 
281 struct AOTCodeSection {
282 public:
283   address _origin_address;
284   uint _size;
285   uint _offset;
286 };
287 
288 enum class DataKind: int {
289   No_Data   = -1,
290   Null      = 0,
291   Klass     = 1,
292   Method    = 2,
293   String    = 3,
294   MH_Oop    = 4,
295   Primitive = 5, // primitive Class object
296   SysLoader = 6, // java_system_loader
297   PlaLoader = 7, // java_platform_loader
298   MethodCnts= 8
299 };
300 
301 struct AOTCodeStats;
302 
303 class AOTCodeCache : public CHeapObj<mtCode> {
304 
305 // Classes used to describe AOT code cache.
306 protected:
307   class Config {
308     size_t  _codeCacheSize;
309     address _compressedOopBase;
310     address _compressedKlassBase;
311     uint _compressedOopShift;
312     uint _compressedKlassShift;
313     uint _contendedPaddingWidth;
314     uint _objectAlignment;
315     uint _gcCardSize;
316     uint _gc;
317     enum Flags {
318       none                     = 0,
319       debugVM                  = 2,
320       compressedOops           = 4,
321       compressedClassPointers  = 8,
322       useTLAB                  = 16,
323       systemClassAssertions    = 32,
324       userClassAssertions      = 64,
325       enableContendedPadding   = 128,
326       restrictContendedPadding = 256,
327       preserveFramePointer     = 512
328     };
329     uint _flags;
330     uint _cpu_features_offset; // offset in the cache where cpu features are stored
331 
332   public:
333     void record(uint cpu_features_offset);
334     bool verify_cpu_features(AOTCodeCache* cache) const;
335     bool verify(AOTCodeCache* cache) const;
336   };
337 
338   class Header : public CHeapObj<mtCode> {
339   private:
340     // Here should be version and other verification fields
341     enum {
342       AOT_CODE_VERSION = 1
343     };
344     uint   _version;         // AOT code version (should match when reading code cache)
345     uint   _cache_size;      // cache size in bytes
346     uint   _strings_count;   // number of recorded C strings
347     uint   _strings_offset;  // offset to recorded C strings
348     uint   _entries_count;   // number of recorded entries
349     uint   _search_table_offset; // offset of table for looking up an AOTCodeEntry
350     uint   _entries_offset;  // offset of AOTCodeEntry array describing entries
351     uint   _preload_entries_count; // entries for pre-loading code
352     uint   _preload_entries_offset;
353     uint   _adapters_count;
354     uint   _shared_blobs_count;
355     uint   _C1_blobs_count;
356     uint   _C2_blobs_count;
357     uint   _stubs_count;
358     Config _config; // must be the last element as there is trailing data stored immediately after Config
359 
360   public:
361     void init(uint cache_size,
362               uint strings_count,  uint strings_offset,
363               uint entries_count,  uint search_table_offset, uint entries_offset,
364               uint preload_entries_count, uint preload_entries_offset,
365               uint adapters_count, uint shared_blobs_count,
366               uint C1_blobs_count, uint C2_blobs_count,
367               uint stubs_count, uint cpu_features_offset) {
368       _version        = AOT_CODE_VERSION;
369       _cache_size     = cache_size;
370       _strings_count  = strings_count;
371       _strings_offset = strings_offset;
372       _entries_count  = entries_count;
373       _search_table_offset = search_table_offset;
374       _entries_offset = entries_offset;
375       _preload_entries_count  = preload_entries_count;
376       _preload_entries_offset = preload_entries_offset;
377       _adapters_count = adapters_count;
378       _shared_blobs_count = shared_blobs_count;
379       _C1_blobs_count = C1_blobs_count;
380       _C2_blobs_count = C2_blobs_count;
381       _stubs_count    = stubs_count;
382       _config.record(cpu_features_offset);
383     }
384 
385     uint cache_size()     const { return _cache_size; }
386     uint strings_count()  const { return _strings_count; }
387     uint strings_offset() const { return _strings_offset; }
388     uint entries_count()  const { return _entries_count; }
389     uint search_table_offset() const { return _search_table_offset; }
390     uint entries_offset() const { return _entries_offset; }
391     uint preload_entries_count()  const { return _preload_entries_count; }
392     uint preload_entries_offset() const { return _preload_entries_offset; }
393     uint adapters_count() const { return _adapters_count; }
394     uint shared_blobs_count()    const { return _shared_blobs_count; }
395     uint C1_blobs_count() const { return _C1_blobs_count; }
396     uint C2_blobs_count() const { return _C2_blobs_count; }
397     uint stubs_count()    const { return _stubs_count; }
398     uint nmethods_count() const { return _preload_entries_count
399                                          + _entries_count
400                                          - _stubs_count
401                                          - _shared_blobs_count
402                                          - _C1_blobs_count
403                                          - _C2_blobs_count
404                                          - _adapters_count; }
405 
406     bool verify(uint load_size)  const;
407     bool verify_config(AOTCodeCache* cache) const { // Called after Universe initialized
408       return _config.verify(cache);
409     }
410   };
411 
412 // Continue with AOTCodeCache class definition.
413 private:
414   Header* _load_header;
415   char*   _load_buffer;    // Aligned buffer for loading AOT code
416   char*   _store_buffer;   // Aligned buffer for storing AOT code
417   char*   _C_store_buffer; // Original unaligned buffer
418 
419   uint   _write_position;  // Position in _store_buffer
420   uint   _load_size;       // Used when reading cache
421   uint   _store_size;      // Used when writing cache
422   bool   _for_use;         // AOT cache is open for using AOT code
423   bool   _for_dump;        // AOT cache is open for dumping AOT code
424   bool   _closing;         // Closing cache file
425   bool   _failed;          // Failed read/write to/from cache (cache is broken?)
426   bool   _lookup_failed;   // Failed to lookup for info (skip only this code load)
427 
428   bool   _for_preload;         // Code for preload
429   bool   _has_clinit_barriers; // Code with clinit barriers
430 
431   AOTCodeAddressTable* _table;
432 
433   AOTCodeEntry* _load_entries;   // Used when reading cache
434   uint*         _search_entries; // sorted by ID table [id, index]
435   AOTCodeEntry* _store_entries;  // Used when writing cache
436   const char*   _C_strings_buf;  // Loaded buffer for _C_strings[] table
437   uint          _store_entries_cnt; // total entries count
438 
439   uint _compile_id;
440   uint _comp_level;
441   uint compile_id() const { return _compile_id; }
442   uint comp_level() const { return _comp_level; }
443 
444   static AOTCodeCache* open_for_use();
445   static AOTCodeCache* open_for_dump();
446 
447   bool set_write_position(uint pos);
448   bool align_write();
449 
450   address reserve_bytes(uint nbytes);
451   uint write_bytes(const void* buffer, uint nbytes);
452   const char* addr(uint offset) const { return _load_buffer + offset; }
453   static AOTCodeAddressTable* addr_table() {
454     return is_on() && (cache()->_table != nullptr) ? cache()->_table : nullptr;
455   }
456 
457   void set_lookup_failed()     { _lookup_failed = true; }
458   void clear_lookup_failed()   { _lookup_failed = false; }
459   bool lookup_failed()   const { return _lookup_failed; }
460 
461   AOTCodeEntry* write_nmethod(nmethod* nm, bool for_preload);
462 
463   // States:
464   //   S >= 0: allow new readers, S readers are currently active
465   //   S <  0: no new readers are allowed; (-S-1) readers are currently active
466   //     (special case: S = -1 means no readers are active, and would never be active again)
467   static volatile int _nmethod_readers;
468 
469   static void wait_for_no_nmethod_readers();
470 
471   class ReadingMark {
472   private:
473     bool _failed;
474   public:
475     ReadingMark();
476     ~ReadingMark();
477     bool failed() {
478       return _failed;
479     }
480   };
481 
482 public:
483   AOTCodeCache(bool is_dumping, bool is_using);
484   ~AOTCodeCache();
485 
486   const char* cache_buffer() const { return _load_buffer; }
487   bool failed() const { return _failed; }
488   void set_failed()   { _failed = true; }
489 
490   static bool is_address_in_aot_cache(address p) NOT_CDS_RETURN_(false);
491   static uint max_aot_code_size();
492 
493   uint load_size() const { return _load_size; }
494   uint write_position() const { return _write_position; }
495 
496   void load_strings();
497   int store_strings();
498 
499   static void init_early_stubs_table() NOT_CDS_RETURN;
500   static void init_shared_blobs_table() NOT_CDS_RETURN;
501   static void init_stubs_table() NOT_CDS_RETURN;
502   static void init_early_c1_table() NOT_CDS_RETURN;
503   static void init_c1_table() NOT_CDS_RETURN;
504   static void init_c2_table() NOT_CDS_RETURN;
505 
506   address address_for_C_string(int idx) const { return _table->address_for_C_string(idx); }
507   address address_for_id(int id) const { return _table->address_for_id(id); }
508 
509   bool for_use()  const { return _for_use  && !_failed; }
510   bool for_dump() const { return _for_dump && !_failed; }
511 
512   bool closing()          const { return _closing; }
513 
514   AOTCodeEntry* add_entry() {
515     _store_entries_cnt++;
516     _store_entries -= 1;
517     return _store_entries;
518   }
519   void preload_aot_code(TRAPS);
520 
521   AOTCodeEntry* find_entry(AOTCodeEntry::Kind kind, uint id, uint comp_level = 0);
522   void invalidate_entry(AOTCodeEntry* entry);
523 
524   void store_cpu_features(char*& buffer, uint buffer_size);
525 
526   bool finish_write();
527 
528   void log_stats_on_exit(AOTCodeStats& stats);
529 
530   static bool load_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) NOT_CDS_RETURN_(false);
531   static bool store_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) NOT_CDS_RETURN_(false);
532 
533   bool write_klass(Klass* klass);
534   bool write_method(Method* method);
535 
536   bool write_relocations(CodeBlob& code_blob, GrowableArray<Handle>* oop_list = nullptr, GrowableArray<Metadata*>* metadata_list = nullptr);
537 
538   bool write_oop_map_set(CodeBlob& cb);
539   bool write_nmethod_reloc_immediates(GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list);
540 
541   jobject read_oop(JavaThread* thread, const methodHandle& comp_method);
542   Metadata* read_metadata(const methodHandle& comp_method);
543 
544   bool write_oop(jobject& jo);
545   bool write_oop(oop obj);
546   bool write_metadata(Metadata* m);
547   bool write_oops(nmethod* nm);
548   bool write_metadata(nmethod* nm);
549 
550 #ifndef PRODUCT
551   bool write_asm_remarks(AsmRemarks& asm_remarks, bool use_string_table);
552   bool write_dbg_strings(DbgStrings& dbg_strings, bool use_string_table);
553 #endif // PRODUCT
554 
555   // save and restore API for non-enumerable code blobs
556   static bool store_code_blob(CodeBlob& blob,
557                               AOTCodeEntry::Kind entry_kind,
558                               uint id, const char* name) NOT_CDS_RETURN_(false);
559 
560   static CodeBlob* load_code_blob(AOTCodeEntry::Kind kind,
561                                   uint id, const char* name) NOT_CDS_RETURN_(nullptr);
562 
563   static bool load_nmethod(ciEnv* env, ciMethod* target, int entry_bci, AbstractCompiler* compiler, CompLevel comp_level) NOT_CDS_RETURN_(false);
564   static AOTCodeEntry* store_nmethod(nmethod* nm, AbstractCompiler* compiler, bool for_preload) NOT_CDS_RETURN_(nullptr);
565 
566   // save and restore API for enumerable code blobs
567   static bool store_code_blob(CodeBlob& blob,
568                               AOTCodeEntry::Kind entry_kind,
569                               BlobId id) NOT_CDS_RETURN_(false);
570 
571   static CodeBlob* load_code_blob(AOTCodeEntry::Kind kind,
572                                   BlobId id) NOT_CDS_RETURN_(nullptr);
573 
574   static uint store_entries_cnt() {
575     if (is_on_for_dump()) {
576       return cache()->_store_entries_cnt;
577     }
578     return -1;
579   }
580 
581 // Static access
582 
583 private:
584   static AOTCodeCache* _cache;
585   DEBUG_ONLY( static bool _passed_init2; )
586 
587   static bool open_cache(bool is_dumping, bool is_using);
588 
589   bool verify_config_on_use() {
590     if (for_use()) {
591       return _load_header->verify_config(this);
592     }
593     return true;
594   }
595 public:
596   static AOTCodeCache* cache() { assert(_passed_init2, "Too early to ask"); return _cache; }
597   static void initialize() NOT_CDS_RETURN;
598   static void init2() NOT_CDS_RETURN;
599   static void close() NOT_CDS_RETURN;
600   static bool is_on() CDS_ONLY({ return cache() != nullptr && !_cache->closing(); }) NOT_CDS_RETURN_(false);
601   static bool is_code_load_thread_on() NOT_CDS_RETURN_(false);
602   static bool is_on_for_use()  CDS_ONLY({ return is_on() && _cache->for_use(); }) NOT_CDS_RETURN_(false);
603   static bool is_on_for_dump() CDS_ONLY({ return is_on() && _cache->for_dump(); }) NOT_CDS_RETURN_(false);
604   static bool is_dumping_code() NOT_CDS_RETURN_(false);
605   static bool is_dumping_stub() NOT_CDS_RETURN_(false);
606   static bool is_dumping_adapter() NOT_CDS_RETURN_(false);
607   static bool is_using_code() NOT_CDS_RETURN_(false);
608   static bool is_using_stub() NOT_CDS_RETURN_(false);
609   static bool is_using_adapter() NOT_CDS_RETURN_(false);
610   static void enable_caching() NOT_CDS_RETURN;
611   static void disable_caching() NOT_CDS_RETURN;
612   static bool is_caching_enabled() NOT_CDS_RETURN_(false);
613 
614   // It is used before AOTCodeCache is initialized.
615   static bool maybe_dumping_code() NOT_CDS_RETURN_(false);
616 
617   static bool allow_const_field(ciConstant& value) NOT_CDS_RETURN_(false);
618   static void invalidate(AOTCodeEntry* entry) NOT_CDS_RETURN;
619   static AOTCodeEntry* find_code_entry(const methodHandle& method, uint comp_level) NOT_CDS_RETURN_(nullptr);
620   static void preload_code(JavaThread* thread) NOT_CDS_RETURN;
621 
622   template<typename Function>
623   static void iterate(Function function) { // lambda enabled API
624     AOTCodeCache* cache = open_for_use();
625     if (cache != nullptr) {
626       ReadingMark rdmk;
627       if (rdmk.failed()) {
628         // Cache is closed, cannot touch anything.
629         return;
630       }
631 
632       uint count = cache->_load_header->entries_count();
633       AOTCodeEntry* load_entries = cache->_load_entries;
634       if (count == 0 || load_entries == nullptr) {
635         return;
636       }
637 
638       for (uint i = 0; i < count; i++) {
639         AOTCodeEntry* entry = &(load_entries[i]);
640         function(entry);
641       }
642     }
643   }
644 
645   static const char* add_C_string(const char* str) NOT_CDS_RETURN_(str);
646 
647   static void print_on(outputStream* st) NOT_CDS_RETURN;
648   static void print_statistics_on(outputStream* st) NOT_CDS_RETURN;
649   static void print_timers_on(outputStream* st) NOT_CDS_RETURN;
650   static void print_unused_entries_on(outputStream* st) NOT_CDS_RETURN;
651 };
652 
653 // Concurent AOT code reader
654 class AOTCodeReader {
655 private:
656   const AOTCodeCache*  _cache;
657   const AOTCodeEntry*  _entry;
658   const char*          _load_buffer; // Loaded AOT code buffer
659   uint  _read_position;              // Position in _load_buffer
660   uint  read_position() const { return _read_position; }
661   void  set_read_position(uint pos);
662   const char* addr(uint offset) const { return _load_buffer + offset; }
663 
664   uint _compile_id;
665   uint _comp_level;
666   uint compile_id() const { return _compile_id; }
667   uint comp_level() const { return _comp_level; }
668 
669   bool _preload;             // Preloading code before method execution
670   bool _lookup_failed;       // Failed to lookup for info (skip only this code load)
671   void set_lookup_failed()     { _lookup_failed = true; }
672   void clear_lookup_failed()   { _lookup_failed = false; }
673   bool lookup_failed()   const { return _lookup_failed; }
674 
675 public:
676   AOTCodeReader(AOTCodeCache* cache, AOTCodeEntry* entry, CompileTask* task);
677 
678   AOTCodeEntry* aot_code_entry() { return (AOTCodeEntry*)_entry; }
679 
680   // convenience method to convert offset in AOTCodeEntry data to its address
681   bool compile_nmethod(ciEnv* env, ciMethod* target, AbstractCompiler* compiler);
682 
683   CodeBlob* compile_code_blob(const char* name);
684 
685   Klass* read_klass(const methodHandle& comp_method);
686   Method* read_method(const methodHandle& comp_method);
687 
688   oop read_oop(JavaThread* thread, const methodHandle& comp_method);
689   Metadata* read_metadata(const methodHandle& comp_method);
690   bool read_oops(OopRecorder* oop_recorder, ciMethod* target);
691   bool read_metadata(OopRecorder* oop_recorder, ciMethod* target);
692 
693   bool read_oop_metadata_list(JavaThread* thread, ciMethod* target, GrowableArray<Handle> &oop_list, GrowableArray<Metadata*> &metadata_list, OopRecorder* oop_recorder);
694   void apply_relocations(nmethod* nm, GrowableArray<Handle> &oop_list, GrowableArray<Metadata*> &metadata_list) NOT_CDS_RETURN;
695 
696   ImmutableOopMapSet* read_oop_map_set();
697 
698   void fix_relocations(CodeBlob* code_blob, GrowableArray<Handle>* oop_list = nullptr, GrowableArray<Metadata*>* metadata_list = nullptr) NOT_CDS_RETURN;
699 #ifndef PRODUCT
700   void read_asm_remarks(AsmRemarks& asm_remarks, bool use_string_table) NOT_CDS_RETURN;
701   void read_dbg_strings(DbgStrings& dbg_strings, bool use_string_table) NOT_CDS_RETURN;
702 #endif // PRODUCT
703 
704   void print_on(outputStream* st);
705 };
706 
707 // +1 for preload code
708 const int AOTCompLevel_count = CompLevel_count + 1; // 6 levels indexed from 0 to 5
709 
710 struct AOTCodeStats {
711 private:
712   struct {
713     uint _kind_cnt[AOTCodeEntry::Kind_count];
714     uint _nmethod_cnt[AOTCompLevel_count];
715     uint _clinit_barriers_cnt;
716   } ccstats; // AOT code stats
717 
718   void check_kind(uint kind) { assert(kind >= AOTCodeEntry::None && kind < AOTCodeEntry::Kind_count, "Invalid AOTCodeEntry kind %d", kind); }
719   void check_complevel(uint lvl) { assert(lvl >= CompLevel_none && lvl < AOTCompLevel_count, "Invalid compilation level %d", lvl); }
720 
721 public:
722   void inc_entry_cnt(uint kind) { check_kind(kind); ccstats._kind_cnt[kind] += 1; }
723   void inc_nmethod_cnt(uint lvl) { check_complevel(lvl); ccstats._nmethod_cnt[lvl] += 1; }
724   void inc_preload_cnt() { ccstats._nmethod_cnt[AOTCompLevel_count-1] += 1; }
725   void inc_clinit_barriers_cnt() { ccstats._clinit_barriers_cnt += 1; }
726 
727   void collect_entry_stats(AOTCodeEntry* entry) {
728     inc_entry_cnt(entry->kind());
729     if (entry->is_nmethod()) {
730       entry->for_preload() ? inc_nmethod_cnt(AOTCompLevel_count-1)
731                            : inc_nmethod_cnt(entry->comp_level());
732       if (entry->has_clinit_barriers()) {
733         inc_clinit_barriers_cnt();
734       }
735     }
736   }
737 
738   uint entry_count(uint kind) { check_kind(kind); return ccstats._kind_cnt[kind]; }
739   uint nmethod_count(uint lvl) { check_complevel(lvl); return ccstats._nmethod_cnt[lvl]; }
740   uint preload_count() { return ccstats._nmethod_cnt[AOTCompLevel_count-1]; }
741   uint clinit_barriers_count() { return ccstats._clinit_barriers_cnt; }
742 
743   uint total_count() {
744     uint total = 0;
745     for (int kind = AOTCodeEntry::None; kind < AOTCodeEntry::Kind_count; kind++) {
746       total += ccstats._kind_cnt[kind];
747     }
748     return total;
749   }
750 
751   static AOTCodeStats add_aot_code_stats(AOTCodeStats stats1, AOTCodeStats stats2);
752 
753   // Runtime stats of the AOT code
754 private:
755   struct {
756     struct {
757       uint _loaded_cnt;
758       uint _invalidated_cnt;
759       uint _load_failed_cnt;
760     } _entry_kinds[AOTCodeEntry::Kind_count],
761       _nmethods[AOTCompLevel_count];
762   } rs; // rs = runtime stats
763 
764 public:
765   void inc_entry_loaded_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._loaded_cnt += 1; }
766   void inc_entry_invalidated_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._invalidated_cnt += 1; }
767   void inc_entry_load_failed_cnt(uint kind) { check_kind(kind); rs._entry_kinds[kind]._load_failed_cnt += 1; }
768 
769   void inc_nmethod_loaded_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._loaded_cnt += 1; }
770   void inc_nmethod_invalidated_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._invalidated_cnt += 1; }
771   void inc_nmethod_load_failed_cnt(uint lvl) { check_complevel(lvl); rs._nmethods[lvl]._load_failed_cnt += 1; }
772 
773   uint entry_loaded_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._loaded_cnt; }
774   uint entry_invalidated_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._invalidated_cnt; }
775   uint entry_load_failed_count(uint kind) { check_kind(kind); return rs._entry_kinds[kind]._load_failed_cnt; }
776 
777   uint nmethod_loaded_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._loaded_cnt; }
778   uint nmethod_invalidated_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._invalidated_cnt; }
779   uint nmethod_load_failed_count(uint lvl) { check_complevel(lvl); return rs._nmethods[lvl]._load_failed_cnt; }
780 
781   void inc_loaded_cnt(AOTCodeEntry* entry) {
782     inc_entry_loaded_cnt(entry->kind());
783     if (entry->is_nmethod()) {
784       entry->for_preload() ? inc_nmethod_loaded_cnt(AOTCompLevel_count-1)
785                            : inc_nmethod_loaded_cnt(entry->comp_level());
786     }
787   }
788 
789   void inc_invalidated_cnt(AOTCodeEntry* entry) {
790     inc_entry_invalidated_cnt(entry->kind());
791     if (entry->is_nmethod()) {
792       entry->for_preload() ? inc_nmethod_invalidated_cnt(AOTCompLevel_count-1)
793                            : inc_nmethod_invalidated_cnt(entry->comp_level());
794     }
795   }
796 
797   void inc_load_failed_cnt(AOTCodeEntry* entry) {
798     inc_entry_load_failed_cnt(entry->kind());
799     if (entry->is_nmethod()) {
800       entry->for_preload() ? inc_nmethod_load_failed_cnt(AOTCompLevel_count-1)
801                            : inc_nmethod_load_failed_cnt(entry->comp_level());
802     }
803   }
804 
805   void collect_entry_runtime_stats(AOTCodeEntry* entry) {
806     if (entry->is_loaded()) {
807       inc_loaded_cnt(entry);
808     }
809     if (entry->not_entrant()) {
810       inc_invalidated_cnt(entry);
811     }
812     if (entry->load_fail()) {
813       inc_load_failed_cnt(entry);
814     }
815   }
816 
817   void collect_all_stats(AOTCodeEntry* entry) {
818     collect_entry_stats(entry);
819     collect_entry_runtime_stats(entry);
820   }
821 
822   AOTCodeStats() {
823     memset(this, 0, sizeof(AOTCodeStats));
824   }
825 };
826 
827 // code cache internal runtime constants area used by AOT code
828 class AOTRuntimeConstants {
829  friend class AOTCodeCache;
830  private:
831   address _card_table_base;
832   uint    _grain_shift;
833   static address _field_addresses_list[];
834   static AOTRuntimeConstants _aot_runtime_constants;
835   // private constructor for unique singleton
836   AOTRuntimeConstants() { }
837   // private for use by friend class AOTCodeCache
838   static void initialize_from_runtime();
839  public:
840 #if INCLUDE_CDS
841   static bool contains(address adr) {
842     address base = (address)&_aot_runtime_constants;
843     address hi = base + sizeof(AOTRuntimeConstants);
844     return (base <= adr && adr < hi);
845   }
846   static address card_table_base_address();
847   static address grain_shift_address() { return (address)&_aot_runtime_constants._grain_shift; }
848   static address* field_addresses_list() {
849     return _field_addresses_list;
850   }
851 #else
852   static bool contains(address adr)      { return false; }
853   static address card_table_address()    { return nullptr; }
854   static address grain_shift_address()   { return nullptr; }
855   static address* field_addresses_list() { return nullptr; }
856 #endif
857 };
858 
859 #endif // SHARE_CODE_AOTCODECACHE_HPP