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/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 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)AOTMetaspace::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(AOTMetaspace::in_aot_cache(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 #endif // INCLUDE_CDS_JAVA_HEAP
88
89 void* AOTCacheAccess::allocate_aot_code_region(size_t size) {
90 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
91 return (void*)ArchiveBuilder::ac_region_alloc(size);
92 }
93
94 size_t AOTCacheAccess::get_aot_code_region_size() {
95 return _aot_code_region_size;
96 }
97
98 void AOTCacheAccess::set_aot_code_region_size(size_t sz) {
99 _aot_code_region_size = sz;
100 }
101
102 bool AOTCacheAccess::map_aot_code_region(ReservedSpace rs) {
103 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
104 assert(UseSharedSpaces && static_mapinfo != nullptr, "must be");
105 return static_mapinfo->map_aot_code_region(rs);
106 }
107
108 bool AOTCacheAccess::is_aot_code_region_empty() {
109 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
110 return ArchiveBuilder::current()->ac_region()->is_empty();
111 }
112
113 void AOTCacheAccess::set_pointer(address* ptr, address value) {
114 ArchiveBuilder* builder = ArchiveBuilder::current();
115 if (value != nullptr && !builder->is_in_buffer_space(value)) {
116 value = builder->get_buffered_addr(value);
117 }
118 *ptr = value;
119 ArchivePtrMarker::mark_pointer(ptr);
120 }