1 /*
2 * Copyright (c) 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 #include "cds/aotCacheAccess.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/filemap.hpp"
29 #include "cds/heapShared.hpp"
30 #include "cds/metaspaceShared.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 void* AOTCacheAccess::allocate_aot_code_region(size_t size) {
40 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
41 return (void*)ArchiveBuilder::ac_region_alloc(size);
42 }
43
44 size_t AOTCacheAccess::get_aot_code_region_size() {
45 assert(CDSConfig::is_using_archive(), "must be");
46 FileMapInfo* mapinfo = FileMapInfo::current_info();
47 assert(mapinfo != nullptr, "must be");
48 return mapinfo->region_at(MetaspaceShared::ac)->used_aligned();
49 }
50
51 bool AOTCacheAccess::map_aot_code_region(ReservedSpace rs) {
52 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
53 assert(UseSharedSpaces && static_mapinfo != nullptr, "must be");
54 return static_mapinfo->map_aot_code_region(rs);
55 }
|
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 #include "cds/aotCacheAccess.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/filemap.hpp"
29 #include "cds/heapShared.hpp"
30 #include "cds/metaspaceShared.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 if (CDSConfig::is_dumping_final_static_archive()) {
43 return ArchiveBuilder::is_active() && ArchiveBuilder::current()->has_been_archived(addr);
44 } else {
45 // Old CDS+AOT workflow.
46 return MetaspaceShared::is_in_shared_metaspace(addr);
47 }
48 }
49
50 bool AOTCacheAccess::can_generate_aot_code(InstanceKlass* ik) {
51 if (CDSConfig::is_dumping_final_static_archive()) {
52 if (!ArchiveBuilder::is_active()) {
53 return false;
54 }
55 ArchiveBuilder* builder = ArchiveBuilder::current();
56 if (!builder->has_been_archived((address)ik)) {
57 return false;
58 }
59 InstanceKlass* buffered_ik = builder->get_buffered_addr(ik);
60 if (ik->is_shared_unregistered_class()) {
61 return false;
62 }
63 return true;
64 } else {
65 // Old CDS+AOT workflow.
66 return ik->is_shared() && !ik->is_shared_unregistered_class();
67 }
68 }
69
70 uint AOTCacheAccess::delta_from_shared_address_base(address addr) {
71 if (CDSConfig::is_dumping_final_static_archive()) {
72 assert(ArchiveBuilder::is_active(), "must be");
73 ArchiveBuilder* builder = ArchiveBuilder::current();
74 address requested_addr = builder->to_requested(builder->get_buffered_addr(addr));
75 return (uint)pointer_delta(requested_addr, (address)SharedBaseAddress, 1);
76 } else {
77 // Old CDS+AOT workflow.
78 return (uint)pointer_delta(addr, (address)SharedBaseAddress, 1);
79 }
80 }
81
82 Method* AOTCacheAccess::method_in_aot_code(Method* m) {
83 if (CDSConfig::is_dumping_final_static_archive()) {
84 assert(ArchiveBuilder::is_active(), "must be");
85 ArchiveBuilder* builder = ArchiveBuilder::current();
86 return builder->to_requested(builder->get_buffered_addr(m));
87 } else {
88 // Old CDS+AOT workflow.
89 return m;
90 }
91 }
92
93 #if INCLUDE_CDS_JAVA_HEAP
94 int AOTCacheAccess::get_archived_object_permanent_index(oop obj) {
95 return HeapShared::get_archived_object_permanent_index(obj);
96 }
97
98 oop AOTCacheAccess::get_archived_object(int permanent_index) {
99 oop o = HeapShared::get_archived_object(permanent_index);
100 assert(oopDesc::is_oop_or_null(o), "sanity");
101 return o;
102 }
103
104 static void test_cds_heap_access_api_for_object(oop obj) {
105 LogStreamHandle(Info, cds, jit) log;
106
107 obj->print_on(&log);
108 log.cr();
109
110 int n = AOTCacheAccess::get_archived_object_permanent_index(obj); // call this when AOTCodeCaching is on
111 if (n < 0) {
112 log.print_cr("*** This object is not in CDS archive");
113 } else {
114 log.print_cr("AOTCacheAccess::get_archived_object_permanent_index(s) = %d", n);
115 oop archived_obj = AOTCacheAccess::get_archived_object(n); // call this when AOTCodeCaching is on
116 if (archived_obj == obj || archived_obj == HeapShared::orig_to_scratch_object(obj)) {
117 log.print_cr("AOTCacheAccess::get_archived_object(%d) returns the same object, as expected", n);
118 } else {
119 log.print_cr("Error!!! AOTCacheAccess::get_archived_object(%d) returns an unexpected object", n);
120 if (archived_obj == nullptr) {
121 log.print_cr("--> null");
122 } else {
123 archived_obj->print_on(&log);
124 log.cr();
125 }
126 }
127 }
128 }
129
130 // TEMP: examples for using the AOTCacheAccess::get_archived_object_permanent_index() and AOTCacheAccess::get_archived_object()
131 // APIs for the AOT compiler.
132
133 void AOTCacheAccess::test_heap_access_api() {
134 ResourceMark rm;
135 const char* tests[] = {
136 "",
137 "null",
138 "NARROW",
139 "not in cds",
140 nullptr,
141 };
142
143 LogStreamHandle(Info, cds, jit) log;
144
145 int i;
146 for (i = 0; tests[i] != nullptr; i++) {
147 EXCEPTION_MARK;
148 log.print_cr("Test %d ======================================== \"%s\"", i, tests[i]);
149 oop s = StringTable::intern(tests[i], CHECK);
150 test_cds_heap_access_api_for_object(s);
151 }
152
153 log.print_cr("Test %d ======================================== Universe::null_ptr_exception_instance()", i);
154 test_cds_heap_access_api_for_object(Universe::null_ptr_exception_instance());
155 }
156
157 #endif // INCLUDE_CDS_JAVA_HEAP
158
159 void* AOTCacheAccess::allocate_aot_code_region(size_t size) {
160 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
161 return (void*)ArchiveBuilder::ac_region_alloc(size);
162 }
163
164 size_t AOTCacheAccess::get_aot_code_region_size() {
165 return _aot_code_region_size;
166 }
167
168 void AOTCacheAccess::set_aot_code_region_size(size_t sz) {
169 _aot_code_region_size = sz;
170 }
171
172 bool AOTCacheAccess::map_aot_code_region(ReservedSpace rs) {
173 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
174 assert(UseSharedSpaces && static_mapinfo != nullptr, "must be");
175 return static_mapinfo->map_aot_code_region(rs);
176 }
177
178 bool AOTCacheAccess::is_aot_code_region_empty() {
179 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
180 return ArchiveBuilder::current()->ac_region()->is_empty();
181 }
182
183 void AOTCacheAccess::set_pointer(address* ptr, address value) {
184 ArchiveBuilder* builder = ArchiveBuilder::current();
185 if (value != nullptr && !builder->is_in_buffer_space(value)) {
186 value = builder->get_buffered_addr(value);
187 }
188 *ptr = value;
189 ArchivePtrMarker::mark_pointer(ptr);
190 }
|