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 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 uint AOTCacheAccess::delta_from_base_address(address addr) {
62 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
63 assert(ArchiveBuilder::is_active(), "must be");
64 ArchiveBuilder* builder = ArchiveBuilder::current();
65 address requested_addr = builder->to_requested(builder->get_buffered_addr(addr));
66 return (uint)pointer_delta(requested_addr, (address)MetaspaceShared::requested_base_address(), 1);
67 }
68
69 uint AOTCacheAccess::convert_method_to_offset(Method* method) {
70 assert(CDSConfig::is_using_archive() && !CDSConfig::is_dumping_final_static_archive(), "must be");
71 assert(MetaspaceShared::is_in_shared_metaspace(method), "method %p is not in AOTCache", method);
72 uint offset = (uint)pointer_delta((address)method, (address)SharedBaseAddress, 1);
73 return offset;
74 }
75
76 #if INCLUDE_CDS_JAVA_HEAP
77 int AOTCacheAccess::get_archived_object_permanent_index(oop obj) {
78 return HeapShared::get_archived_object_permanent_index(obj);
79 }
80
81 oop AOTCacheAccess::get_archived_object(int permanent_index) {
82 oop o = HeapShared::get_archived_object(permanent_index);
83 assert(oopDesc::is_oop_or_null(o), "sanity");
84 return o;
85 }
86
87 static void test_cds_heap_access_api_for_object(oop obj) {
88 LogStreamHandle(Info, cds, jit) log;
89
90 obj->print_on(&log);
91 log.cr();
92
93 int n = AOTCacheAccess::get_archived_object_permanent_index(obj); // call this when AOTCodeCaching is on
94 if (n < 0) {
95 log.print_cr("*** This object is not in CDS archive");
96 } else {
97 log.print_cr("AOTCacheAccess::get_archived_object_permanent_index(s) = %d", n);
98 oop archived_obj = AOTCacheAccess::get_archived_object(n); // call this when AOTCodeCaching is on
99 if (archived_obj == obj || archived_obj == HeapShared::orig_to_scratch_object(obj)) {
100 log.print_cr("AOTCacheAccess::get_archived_object(%d) returns the same object, as expected", n);
101 } else {
102 log.print_cr("Error!!! AOTCacheAccess::get_archived_object(%d) returns an unexpected object", n);
103 if (archived_obj == nullptr) {
104 log.print_cr("--> null");
105 } else {
106 archived_obj->print_on(&log);
107 log.cr();
108 }
109 }
110 }
111 }
112
113 // TEMP: examples for using the AOTCacheAccess::get_archived_object_permanent_index() and AOTCacheAccess::get_archived_object()
114 // APIs for the AOT compiler.
115
116 void AOTCacheAccess::test_heap_access_api() {
117 ResourceMark rm;
118 const char* tests[] = {
119 "",
120 "null",
121 "NARROW",
122 "not in cds",
123 nullptr,
124 };
125
126 LogStreamHandle(Info, cds, jit) log;
127
128 int i;
129 for (i = 0; tests[i] != nullptr; i++) {
130 EXCEPTION_MARK;
131 log.print_cr("Test %d ======================================== \"%s\"", i, tests[i]);
132 oop s = StringTable::intern(tests[i], CHECK);
133 test_cds_heap_access_api_for_object(s);
134 }
135
136 log.print_cr("Test %d ======================================== Universe::null_ptr_exception_instance()", i);
137 test_cds_heap_access_api_for_object(Universe::null_ptr_exception_instance());
138 }
139
140 #endif // INCLUDE_CDS_JAVA_HEAP
141
142 void* AOTCacheAccess::allocate_aot_code_region(size_t size) {
143 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
144 return (void*)ArchiveBuilder::ac_region_alloc(size);
145 }
146
147 size_t AOTCacheAccess::get_aot_code_region_size() {
148 return _aot_code_region_size;
149 }
150
151 void AOTCacheAccess::set_aot_code_region_size(size_t sz) {
152 _aot_code_region_size = sz;
153 }
154
155 bool AOTCacheAccess::map_aot_code_region(ReservedSpace rs) {
156 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
157 assert(UseSharedSpaces && static_mapinfo != nullptr, "must be");
158 return static_mapinfo->map_aot_code_region(rs);
159 }
160
161 bool AOTCacheAccess::is_aot_code_region_empty() {
162 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
163 return ArchiveBuilder::current()->ac_region()->is_empty();
164 }
165
166 void AOTCacheAccess::set_pointer(address* ptr, address value) {
167 ArchiveBuilder* builder = ArchiveBuilder::current();
168 if (value != nullptr && !builder->is_in_buffer_space(value)) {
169 value = builder->get_buffered_addr(value);
170 }
171 *ptr = value;
172 ArchivePtrMarker::mark_pointer(ptr);
173 }
|