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 #include "cds/aotCacheAccess.hpp"
26 #include "cds/aotMetaspace.hpp"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/filemap.hpp"
30 #include "cds/heapShared.hpp"
31 #include "classfile/stringTable.hpp"
32 #include "logging/log.hpp"
33 #include "logging/logStream.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "memory/universe.hpp"
36 #include "memory/virtualspace.hpp"
37 #include "oops/instanceKlass.hpp"
38
39 size_t _aot_code_region_size = 0;
40
41 bool AOTCacheAccess::can_generate_aot_code(address addr) {
42 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
43 return ArchiveBuilder::is_active() && ArchiveBuilder::current()->has_been_archived(addr);
44 }
45
46 bool AOTCacheAccess::can_generate_aot_code_for(InstanceKlass* ik) {
47 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
48 if (!ArchiveBuilder::is_active()) {
49 return false;
50 }
51 ArchiveBuilder* builder = ArchiveBuilder::current();
52 if (!builder->has_been_archived((address)ik)) {
53 return false;
54 }
55 if (ik->defined_by_other_loaders()) {
56 return false;
57 }
58 return true;
59 }
60
61 bool AOTCacheAccess::is_early_aot_inited_class(InstanceKlass* ik) {
62 return ik->has_aot_initialized_mirror() && !class_or_super_has_runtime_setup(ik);
63 }
64
65 bool AOTCacheAccess::class_or_super_has_runtime_setup(InstanceKlass *ik) {
66 if (ik->is_runtime_setup_required()) {
67 return true;
68 }
69 if (ik->super() != nullptr && class_or_super_has_runtime_setup(ik->super())) {
70 return true;
71 }
72
73 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
74 int num_interfaces = interfaces->length();
75 for (int i = 0; i < num_interfaces; i++) {
76 if (class_or_super_has_runtime_setup(interfaces->at(i))) {
77 return true;
78 }
79 }
80
81 return false;
82 }
83
84 uint AOTCacheAccess::delta_from_base_address(address addr) {
85 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
86 assert(ArchiveBuilder::is_active(), "must be");
87 ArchiveBuilder* builder = ArchiveBuilder::current();
88 address requested_addr = builder->to_requested(builder->get_buffered_addr(addr));
89 return (uint)pointer_delta(requested_addr, (address)AOTMetaspace::requested_base_address(), 1);
90 }
91
92 uint AOTCacheAccess::convert_method_to_offset(Method* method) {
93 assert(CDSConfig::is_using_archive() && !CDSConfig::is_dumping_final_static_archive(), "must be");
94 assert(AOTMetaspace::in_aot_cache(method), "method %p is not in AOTCache", method);
95 uint offset = (uint)pointer_delta((address)method, (address)SharedBaseAddress, 1);
96 return offset;
97 }
98
99 #if INCLUDE_CDS_JAVA_HEAP
100 int AOTCacheAccess::get_archived_object_permanent_index(oop obj) {
101 return HeapShared::get_root_index(obj); // -1 if obj is not a root.
102 }
103
104 oop AOTCacheAccess::get_archived_object(int permanent_index) {
105 oop o = HeapShared::get_root(permanent_index);
106 assert(oopDesc::is_oop_or_null(o), "sanity");
107 return o;
108 }
109
110 #endif // INCLUDE_CDS_JAVA_HEAP
111
112 void* AOTCacheAccess::allocate_aot_code_region(size_t size) {
113 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
114 return (void*)ArchiveBuilder::ac_region_alloc(size);
115 }
116
117 size_t AOTCacheAccess::get_aot_code_region_size() {
118 return _aot_code_region_size;
119 }
120
121 void AOTCacheAccess::set_aot_code_region_size(size_t sz) {
122 _aot_code_region_size = sz;
123 }
124
125 bool AOTCacheAccess::map_aot_code_region(ReservedSpace rs) {
126 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
127 assert(UseSharedSpaces && static_mapinfo != nullptr, "must be");
128 return static_mapinfo->map_aot_code_region(rs);
129 }
130
131 bool AOTCacheAccess::is_aot_code_region_empty() {
132 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
133 return ArchiveBuilder::current()->ac_region()->is_empty();
134 }
135
136 void AOTCacheAccess::set_pointer(address* ptr, address value) {
137 ArchiveBuilder* builder = ArchiveBuilder::current();
138 if (value != nullptr && !builder->is_in_buffer_space(value)) {
139 value = builder->get_buffered_addr(value);
140 }
141 *ptr = value;
142 ArchivePtrMarker::mark_pointer(ptr);
143 }