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