xref: /aosp_15_r20/art/runtime/jit/jit_memory_region.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jit_memory_region.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <fcntl.h>
20*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
23*795d594fSAndroid Build Coastguard Worker #include <log/log.h>
24*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"  // For RoundDown, RoundUp
25*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"  // For VLOG.
27*795d594fSAndroid Build Coastguard Worker #include "base/membarrier.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/memfd.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/systrace.h"
30*795d594fSAndroid Build Coastguard Worker #include "gc/allocator/art-dlmalloc.h"
31*795d594fSAndroid Build Coastguard Worker #include "jit/jit_scoped_code_cache_write.h"
32*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
33*795d594fSAndroid Build Coastguard Worker #include "palette/palette.h"
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker using android::base::unique_fd;
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
38*795d594fSAndroid Build Coastguard Worker namespace jit {
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker // Data cache will be half of the capacity
41*795d594fSAndroid Build Coastguard Worker // Code cache will be the other half of the capacity.
42*795d594fSAndroid Build Coastguard Worker // TODO: Make this adjustable. Currently must be 2. JitCodeCache relies on that.
43*795d594fSAndroid Build Coastguard Worker static constexpr size_t kCodeAndDataCapacityDivider = 2;
44*795d594fSAndroid Build Coastguard Worker 
Initialize(size_t initial_capacity,size_t max_capacity,bool rwx_memory_allowed,bool is_zygote,std::string * error_msg)45*795d594fSAndroid Build Coastguard Worker bool JitMemoryRegion::Initialize(size_t initial_capacity,
46*795d594fSAndroid Build Coastguard Worker                                  size_t max_capacity,
47*795d594fSAndroid Build Coastguard Worker                                  bool rwx_memory_allowed,
48*795d594fSAndroid Build Coastguard Worker                                  bool is_zygote,
49*795d594fSAndroid Build Coastguard Worker                                  std::string* error_msg) {
50*795d594fSAndroid Build Coastguard Worker   ScopedTrace trace(__PRETTY_FUNCTION__);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   CHECK_GE(max_capacity, initial_capacity);
53*795d594fSAndroid Build Coastguard Worker   CHECK(max_capacity <= 1 * GB) << "The max supported size for JIT code cache is 1GB";
54*795d594fSAndroid Build Coastguard Worker   // Align both capacities to page size, as that's the unit mspaces use.
55*795d594fSAndroid Build Coastguard Worker   initial_capacity_ = RoundDown(initial_capacity, 2 * gPageSize);
56*795d594fSAndroid Build Coastguard Worker   max_capacity_ = RoundDown(max_capacity, 2 * gPageSize);
57*795d594fSAndroid Build Coastguard Worker   current_capacity_ = initial_capacity,
58*795d594fSAndroid Build Coastguard Worker   data_end_ = initial_capacity / kCodeAndDataCapacityDivider;
59*795d594fSAndroid Build Coastguard Worker   exec_end_ = initial_capacity - data_end_;
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   const size_t capacity = max_capacity_;
62*795d594fSAndroid Build Coastguard Worker   const size_t data_capacity = capacity / kCodeAndDataCapacityDivider;
63*795d594fSAndroid Build Coastguard Worker   const size_t exec_capacity = capacity - data_capacity;
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // File descriptor enabling dual-view mapping of code section.
66*795d594fSAndroid Build Coastguard Worker   unique_fd mem_fd;
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker   // The memory mappings we are going to create.
70*795d594fSAndroid Build Coastguard Worker   MemMap data_pages;
71*795d594fSAndroid Build Coastguard Worker   MemMap exec_pages;
72*795d594fSAndroid Build Coastguard Worker   MemMap non_exec_pages;
73*795d594fSAndroid Build Coastguard Worker   MemMap writable_data_pages;
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   if (is_zygote) {
76*795d594fSAndroid Build Coastguard Worker     android_errorWriteLog(0x534e4554, "200284993");  // Report to SafetyNet.
77*795d594fSAndroid Build Coastguard Worker     // Because we are not going to GC code generated by the zygote, just use all available.
78*795d594fSAndroid Build Coastguard Worker     current_capacity_ = max_capacity;
79*795d594fSAndroid Build Coastguard Worker     mem_fd = unique_fd(CreateZygoteMemory(capacity, error_msg));
80*795d594fSAndroid Build Coastguard Worker     if (mem_fd.get() < 0) {
81*795d594fSAndroid Build Coastguard Worker       return false;
82*795d594fSAndroid Build Coastguard Worker     }
83*795d594fSAndroid Build Coastguard Worker   } else {
84*795d594fSAndroid Build Coastguard Worker     // Bionic supports memfd_create, but the call may fail on older kernels.
85*795d594fSAndroid Build Coastguard Worker     mem_fd = unique_fd(art::memfd_create("jit-cache", /* flags= */ 0));
86*795d594fSAndroid Build Coastguard Worker     if (mem_fd.get() < 0) {
87*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
88*795d594fSAndroid Build Coastguard Worker       oss << "Failed to initialize dual view JIT. memfd_create() error: " << strerror(errno);
89*795d594fSAndroid Build Coastguard Worker       if (!rwx_memory_allowed) {
90*795d594fSAndroid Build Coastguard Worker         // Without using RWX page permissions, the JIT can not fallback to single mapping as it
91*795d594fSAndroid Build Coastguard Worker         // requires tranitioning the code pages to RWX for updates.
92*795d594fSAndroid Build Coastguard Worker         *error_msg = oss.str();
93*795d594fSAndroid Build Coastguard Worker         return false;
94*795d594fSAndroid Build Coastguard Worker       }
95*795d594fSAndroid Build Coastguard Worker       VLOG(jit) << oss.str();
96*795d594fSAndroid Build Coastguard Worker     } else if (ftruncate(mem_fd, capacity) != 0) {
97*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
98*795d594fSAndroid Build Coastguard Worker       oss << "Failed to initialize memory file: " << strerror(errno);
99*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
100*795d594fSAndroid Build Coastguard Worker       return false;
101*795d594fSAndroid Build Coastguard Worker     }
102*795d594fSAndroid Build Coastguard Worker   }
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   // Map name specific for android_os_Debug.cpp accounting.
105*795d594fSAndroid Build Coastguard Worker   std::string data_cache_name = is_zygote ? "zygote-data-code-cache" : "data-code-cache";
106*795d594fSAndroid Build Coastguard Worker   std::string exec_cache_name = is_zygote ? "zygote-jit-code-cache" : "jit-code-cache";
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker   std::string error_str;
109*795d594fSAndroid Build Coastguard Worker   int base_flags;
110*795d594fSAndroid Build Coastguard Worker   if (mem_fd.get() >= 0) {
111*795d594fSAndroid Build Coastguard Worker     // Dual view of JIT code cache case. Create an initial mapping of data pages large enough
112*795d594fSAndroid Build Coastguard Worker     // for data and non-writable view of JIT code pages. We use the memory file descriptor to
113*795d594fSAndroid Build Coastguard Worker     // enable dual mapping - we'll create a second mapping using the descriptor below. The
114*795d594fSAndroid Build Coastguard Worker     // mappings will look like:
115*795d594fSAndroid Build Coastguard Worker     //
116*795d594fSAndroid Build Coastguard Worker     //       VA                  PA
117*795d594fSAndroid Build Coastguard Worker     //
118*795d594fSAndroid Build Coastguard Worker     //       +---------------+
119*795d594fSAndroid Build Coastguard Worker     //       | non exec code |\
120*795d594fSAndroid Build Coastguard Worker     //       +---------------+ \
121*795d594fSAndroid Build Coastguard Worker     //       | writable data |\ \
122*795d594fSAndroid Build Coastguard Worker     //       +---------------+ \ \
123*795d594fSAndroid Build Coastguard Worker     //       :               :\ \ \
124*795d594fSAndroid Build Coastguard Worker     //       +---------------+.\.\.+---------------+
125*795d594fSAndroid Build Coastguard Worker     //       |  exec code    |  \ \|     code      |
126*795d594fSAndroid Build Coastguard Worker     //       +---------------+...\.+---------------+
127*795d594fSAndroid Build Coastguard Worker     //       | readonly data |    \|     data      |
128*795d594fSAndroid Build Coastguard Worker     //       +---------------+.....+---------------+
129*795d594fSAndroid Build Coastguard Worker     //
130*795d594fSAndroid Build Coastguard Worker     // In this configuration code updates are written to the non-executable view of the code
131*795d594fSAndroid Build Coastguard Worker     // cache, and the executable view of the code cache has fixed RX memory protections.
132*795d594fSAndroid Build Coastguard Worker     //
133*795d594fSAndroid Build Coastguard Worker     // This memory needs to be mapped shared as the code portions will have two mappings.
134*795d594fSAndroid Build Coastguard Worker     //
135*795d594fSAndroid Build Coastguard Worker     // Additionally, the zyzote will create a dual view of the data portion of
136*795d594fSAndroid Build Coastguard Worker     // the cache. This mapping will be read-only, whereas the second mapping
137*795d594fSAndroid Build Coastguard Worker     // will be writable.
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker     base_flags = MAP_SHARED;
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker     // Create the writable mappings now, so that in case of the zygote, we can
142*795d594fSAndroid Build Coastguard Worker     // prevent any future writable mappings through sealing.
143*795d594fSAndroid Build Coastguard Worker     if (exec_capacity > 0) {
144*795d594fSAndroid Build Coastguard Worker       // For dual view, create the secondary view of code memory used for updating code. This view
145*795d594fSAndroid Build Coastguard Worker       // is never executable.
146*795d594fSAndroid Build Coastguard Worker       std::string name = exec_cache_name + "-rw";
147*795d594fSAndroid Build Coastguard Worker       non_exec_pages = MemMap::MapFile(exec_capacity,
148*795d594fSAndroid Build Coastguard Worker                                        kIsDebugBuild ? kProtR : kProtRW,
149*795d594fSAndroid Build Coastguard Worker                                        base_flags,
150*795d594fSAndroid Build Coastguard Worker                                        mem_fd,
151*795d594fSAndroid Build Coastguard Worker                                        /* start= */ data_capacity,
152*795d594fSAndroid Build Coastguard Worker                                        /* low_4GB= */ false,
153*795d594fSAndroid Build Coastguard Worker                                        name.c_str(),
154*795d594fSAndroid Build Coastguard Worker                                        &error_str);
155*795d594fSAndroid Build Coastguard Worker       if (!non_exec_pages.IsValid()) {
156*795d594fSAndroid Build Coastguard Worker         // This is unexpected.
157*795d594fSAndroid Build Coastguard Worker         *error_msg = "Failed to map non-executable view of JIT code cache";
158*795d594fSAndroid Build Coastguard Worker         return false;
159*795d594fSAndroid Build Coastguard Worker       }
160*795d594fSAndroid Build Coastguard Worker       // Create a dual view of the data cache.
161*795d594fSAndroid Build Coastguard Worker       name = data_cache_name + "-rw";
162*795d594fSAndroid Build Coastguard Worker       writable_data_pages = MemMap::MapFile(data_capacity,
163*795d594fSAndroid Build Coastguard Worker                                             kProtRW,
164*795d594fSAndroid Build Coastguard Worker                                             base_flags,
165*795d594fSAndroid Build Coastguard Worker                                             mem_fd,
166*795d594fSAndroid Build Coastguard Worker                                             /* start= */ 0,
167*795d594fSAndroid Build Coastguard Worker                                             /* low_4GB= */ false,
168*795d594fSAndroid Build Coastguard Worker                                             name.c_str(),
169*795d594fSAndroid Build Coastguard Worker                                             &error_str);
170*795d594fSAndroid Build Coastguard Worker       if (!writable_data_pages.IsValid()) {
171*795d594fSAndroid Build Coastguard Worker         std::ostringstream oss;
172*795d594fSAndroid Build Coastguard Worker         oss << "Failed to create dual data view: " << error_str;
173*795d594fSAndroid Build Coastguard Worker         *error_msg = oss.str();
174*795d594fSAndroid Build Coastguard Worker         return false;
175*795d594fSAndroid Build Coastguard Worker       }
176*795d594fSAndroid Build Coastguard Worker       if (writable_data_pages.MadviseDontFork() != 0) {
177*795d594fSAndroid Build Coastguard Worker         *error_msg = "Failed to MadviseDontFork the writable data view";
178*795d594fSAndroid Build Coastguard Worker         return false;
179*795d594fSAndroid Build Coastguard Worker       }
180*795d594fSAndroid Build Coastguard Worker       if (non_exec_pages.MadviseDontFork() != 0) {
181*795d594fSAndroid Build Coastguard Worker         *error_msg = "Failed to MadviseDontFork the writable code view";
182*795d594fSAndroid Build Coastguard Worker         return false;
183*795d594fSAndroid Build Coastguard Worker       }
184*795d594fSAndroid Build Coastguard Worker       // Now that we have created the writable and executable mappings, prevent creating any new
185*795d594fSAndroid Build Coastguard Worker       // ones.
186*795d594fSAndroid Build Coastguard Worker       if (is_zygote && !ProtectZygoteMemory(mem_fd.get(), error_msg)) {
187*795d594fSAndroid Build Coastguard Worker         return false;
188*795d594fSAndroid Build Coastguard Worker       }
189*795d594fSAndroid Build Coastguard Worker     }
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker     // Map in low 4gb to simplify accessing root tables for x86_64.
192*795d594fSAndroid Build Coastguard Worker     // We could do PC-relative addressing to avoid this problem, but that
193*795d594fSAndroid Build Coastguard Worker     // would require reserving code and data area before submitting, which
194*795d594fSAndroid Build Coastguard Worker     // means more windows for the code memory to be RWX.
195*795d594fSAndroid Build Coastguard Worker     data_pages = MemMap::MapFile(
196*795d594fSAndroid Build Coastguard Worker         data_capacity + exec_capacity,
197*795d594fSAndroid Build Coastguard Worker         kProtR,
198*795d594fSAndroid Build Coastguard Worker         base_flags,
199*795d594fSAndroid Build Coastguard Worker         mem_fd,
200*795d594fSAndroid Build Coastguard Worker         /* start= */ 0,
201*795d594fSAndroid Build Coastguard Worker         /* low_4gb= */ true,
202*795d594fSAndroid Build Coastguard Worker         data_cache_name.c_str(),
203*795d594fSAndroid Build Coastguard Worker         &error_str);
204*795d594fSAndroid Build Coastguard Worker   } else {
205*795d594fSAndroid Build Coastguard Worker     // Single view of JIT code cache case. Create an initial mapping of data pages large enough
206*795d594fSAndroid Build Coastguard Worker     // for data and JIT code pages. The mappings will look like:
207*795d594fSAndroid Build Coastguard Worker     //
208*795d594fSAndroid Build Coastguard Worker     //       VA                  PA
209*795d594fSAndroid Build Coastguard Worker     //
210*795d594fSAndroid Build Coastguard Worker     //       +---------------+...+---------------+
211*795d594fSAndroid Build Coastguard Worker     //       |  exec code    |   |     code      |
212*795d594fSAndroid Build Coastguard Worker     //       +---------------+...+---------------+
213*795d594fSAndroid Build Coastguard Worker     //       |      data     |   |     data      |
214*795d594fSAndroid Build Coastguard Worker     //       +---------------+...+---------------+
215*795d594fSAndroid Build Coastguard Worker     //
216*795d594fSAndroid Build Coastguard Worker     // In this configuration code updates are written to the executable view of the code cache,
217*795d594fSAndroid Build Coastguard Worker     // and the executable view of the code cache transitions RX to RWX for the update and then
218*795d594fSAndroid Build Coastguard Worker     // back to RX after the update.
219*795d594fSAndroid Build Coastguard Worker     base_flags = MAP_PRIVATE | MAP_ANON;
220*795d594fSAndroid Build Coastguard Worker     data_pages = MemMap::MapAnonymous(
221*795d594fSAndroid Build Coastguard Worker         data_cache_name.c_str(),
222*795d594fSAndroid Build Coastguard Worker         data_capacity + exec_capacity,
223*795d594fSAndroid Build Coastguard Worker         kProtRW,
224*795d594fSAndroid Build Coastguard Worker         /* low_4gb= */ true,
225*795d594fSAndroid Build Coastguard Worker         &error_str);
226*795d594fSAndroid Build Coastguard Worker   }
227*795d594fSAndroid Build Coastguard Worker 
228*795d594fSAndroid Build Coastguard Worker   if (!data_pages.IsValid()) {
229*795d594fSAndroid Build Coastguard Worker     std::ostringstream oss;
230*795d594fSAndroid Build Coastguard Worker     oss << "Failed to create read write cache: " << error_str << " size=" << capacity;
231*795d594fSAndroid Build Coastguard Worker     *error_msg = oss.str();
232*795d594fSAndroid Build Coastguard Worker     return false;
233*795d594fSAndroid Build Coastguard Worker   }
234*795d594fSAndroid Build Coastguard Worker 
235*795d594fSAndroid Build Coastguard Worker   if (exec_capacity > 0) {
236*795d594fSAndroid Build Coastguard Worker     uint8_t* const divider = data_pages.Begin() + data_capacity;
237*795d594fSAndroid Build Coastguard Worker     // Set initial permission for executable view to catch any SELinux permission problems early
238*795d594fSAndroid Build Coastguard Worker     // (for processes that cannot map WX pages). Otherwise, this region does not need to be
239*795d594fSAndroid Build Coastguard Worker     // executable as there is no code in the cache yet.
240*795d594fSAndroid Build Coastguard Worker     exec_pages = data_pages.RemapAtEnd(divider,
241*795d594fSAndroid Build Coastguard Worker                                        exec_cache_name.c_str(),
242*795d594fSAndroid Build Coastguard Worker                                        kProtRX,
243*795d594fSAndroid Build Coastguard Worker                                        base_flags | MAP_FIXED,
244*795d594fSAndroid Build Coastguard Worker                                        mem_fd.get(),
245*795d594fSAndroid Build Coastguard Worker                                        (mem_fd.get() >= 0) ? data_capacity : 0,
246*795d594fSAndroid Build Coastguard Worker                                        &error_str);
247*795d594fSAndroid Build Coastguard Worker     if (!exec_pages.IsValid()) {
248*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
249*795d594fSAndroid Build Coastguard Worker       oss << "Failed to create read execute code cache: " << error_str << " size=" << capacity;
250*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
251*795d594fSAndroid Build Coastguard Worker       return false;
252*795d594fSAndroid Build Coastguard Worker     }
253*795d594fSAndroid Build Coastguard Worker   } else {
254*795d594fSAndroid Build Coastguard Worker     // Profiling only. No memory for code required.
255*795d594fSAndroid Build Coastguard Worker   }
256*795d594fSAndroid Build Coastguard Worker 
257*795d594fSAndroid Build Coastguard Worker   data_pages_ = std::move(data_pages);
258*795d594fSAndroid Build Coastguard Worker   exec_pages_ = std::move(exec_pages);
259*795d594fSAndroid Build Coastguard Worker   non_exec_pages_ = std::move(non_exec_pages);
260*795d594fSAndroid Build Coastguard Worker   writable_data_pages_ = std::move(writable_data_pages);
261*795d594fSAndroid Build Coastguard Worker 
262*795d594fSAndroid Build Coastguard Worker   VLOG(jit) << "Created JitMemoryRegion"
263*795d594fSAndroid Build Coastguard Worker             << ": data_pages=" << reinterpret_cast<void*>(data_pages_.Begin())
264*795d594fSAndroid Build Coastguard Worker             << ", exec_pages=" << reinterpret_cast<void*>(exec_pages_.Begin())
265*795d594fSAndroid Build Coastguard Worker             << ", non_exec_pages=" << reinterpret_cast<void*>(non_exec_pages_.Begin())
266*795d594fSAndroid Build Coastguard Worker             << ", writable_data_pages=" << reinterpret_cast<void*>(writable_data_pages_.Begin());
267*795d594fSAndroid Build Coastguard Worker 
268*795d594fSAndroid Build Coastguard Worker   // Now that the pages are initialized, initialize the spaces.
269*795d594fSAndroid Build Coastguard Worker 
270*795d594fSAndroid Build Coastguard Worker   // Initialize the data heap.
271*795d594fSAndroid Build Coastguard Worker   data_mspace_ = create_mspace_with_base(
272*795d594fSAndroid Build Coastguard Worker       HasDualDataMapping() ? writable_data_pages_.Begin() : data_pages_.Begin(),
273*795d594fSAndroid Build Coastguard Worker       data_end_,
274*795d594fSAndroid Build Coastguard Worker       /* locked= */ false);
275*795d594fSAndroid Build Coastguard Worker   CHECK(data_mspace_ != nullptr) << "create_mspace_with_base (data) failed";
276*795d594fSAndroid Build Coastguard Worker 
277*795d594fSAndroid Build Coastguard Worker   // Allow mspace to use the full data capacity.
278*795d594fSAndroid Build Coastguard Worker   // It will still only use as litle memory as possible and ask for MoreCore as needed.
279*795d594fSAndroid Build Coastguard Worker   CHECK(IsAlignedParam(data_capacity, gPageSize));
280*795d594fSAndroid Build Coastguard Worker   mspace_set_footprint_limit(data_mspace_, data_capacity);
281*795d594fSAndroid Build Coastguard Worker 
282*795d594fSAndroid Build Coastguard Worker   // Initialize the code heap.
283*795d594fSAndroid Build Coastguard Worker   MemMap* code_heap = nullptr;
284*795d594fSAndroid Build Coastguard Worker   if (non_exec_pages_.IsValid()) {
285*795d594fSAndroid Build Coastguard Worker     code_heap = &non_exec_pages_;
286*795d594fSAndroid Build Coastguard Worker   } else if (exec_pages_.IsValid()) {
287*795d594fSAndroid Build Coastguard Worker     code_heap = &exec_pages_;
288*795d594fSAndroid Build Coastguard Worker   }
289*795d594fSAndroid Build Coastguard Worker   if (code_heap != nullptr) {
290*795d594fSAndroid Build Coastguard Worker     // Make all pages reserved for the code heap writable. The mspace allocator, that manages the
291*795d594fSAndroid Build Coastguard Worker     // heap, will take and initialize pages in create_mspace_with_base().
292*795d594fSAndroid Build Coastguard Worker     {
293*795d594fSAndroid Build Coastguard Worker       ScopedCodeCacheWrite scc(*this);
294*795d594fSAndroid Build Coastguard Worker       exec_mspace_ = create_mspace_with_base(code_heap->Begin(), exec_end_, false /*locked*/);
295*795d594fSAndroid Build Coastguard Worker     }
296*795d594fSAndroid Build Coastguard Worker     CHECK(exec_mspace_ != nullptr) << "create_mspace_with_base (exec) failed";
297*795d594fSAndroid Build Coastguard Worker     SetFootprintLimit(current_capacity_);
298*795d594fSAndroid Build Coastguard Worker   } else {
299*795d594fSAndroid Build Coastguard Worker     exec_mspace_ = nullptr;
300*795d594fSAndroid Build Coastguard Worker     SetFootprintLimit(current_capacity_);
301*795d594fSAndroid Build Coastguard Worker   }
302*795d594fSAndroid Build Coastguard Worker   return true;
303*795d594fSAndroid Build Coastguard Worker }
304*795d594fSAndroid Build Coastguard Worker 
SetFootprintLimit(size_t new_footprint)305*795d594fSAndroid Build Coastguard Worker void JitMemoryRegion::SetFootprintLimit(size_t new_footprint) {
306*795d594fSAndroid Build Coastguard Worker   size_t data_space_footprint = new_footprint / kCodeAndDataCapacityDivider;
307*795d594fSAndroid Build Coastguard Worker   DCHECK(IsAlignedParam(data_space_footprint, gPageSize));
308*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(data_space_footprint * kCodeAndDataCapacityDivider, new_footprint);
309*795d594fSAndroid Build Coastguard Worker   if (HasCodeMapping()) {
310*795d594fSAndroid Build Coastguard Worker     ScopedCodeCacheWrite scc(*this);
311*795d594fSAndroid Build Coastguard Worker     mspace_set_footprint_limit(exec_mspace_, new_footprint - data_space_footprint);
312*795d594fSAndroid Build Coastguard Worker   }
313*795d594fSAndroid Build Coastguard Worker }
314*795d594fSAndroid Build Coastguard Worker 
IncreaseCodeCacheCapacity()315*795d594fSAndroid Build Coastguard Worker bool JitMemoryRegion::IncreaseCodeCacheCapacity() {
316*795d594fSAndroid Build Coastguard Worker   if (current_capacity_ == max_capacity_) {
317*795d594fSAndroid Build Coastguard Worker     return false;
318*795d594fSAndroid Build Coastguard Worker   }
319*795d594fSAndroid Build Coastguard Worker 
320*795d594fSAndroid Build Coastguard Worker   // Double the capacity if we're below 1MB, or increase it by 1MB if
321*795d594fSAndroid Build Coastguard Worker   // we're above.
322*795d594fSAndroid Build Coastguard Worker   if (current_capacity_ < 1 * MB) {
323*795d594fSAndroid Build Coastguard Worker     current_capacity_ *= 2;
324*795d594fSAndroid Build Coastguard Worker   } else {
325*795d594fSAndroid Build Coastguard Worker     current_capacity_ += 1 * MB;
326*795d594fSAndroid Build Coastguard Worker   }
327*795d594fSAndroid Build Coastguard Worker   if (current_capacity_ > max_capacity_) {
328*795d594fSAndroid Build Coastguard Worker     current_capacity_ = max_capacity_;
329*795d594fSAndroid Build Coastguard Worker   }
330*795d594fSAndroid Build Coastguard Worker 
331*795d594fSAndroid Build Coastguard Worker   VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
332*795d594fSAndroid Build Coastguard Worker 
333*795d594fSAndroid Build Coastguard Worker   SetFootprintLimit(current_capacity_);
334*795d594fSAndroid Build Coastguard Worker 
335*795d594fSAndroid Build Coastguard Worker   return true;
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker 
338*795d594fSAndroid Build Coastguard Worker // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
339*795d594fSAndroid Build Coastguard Worker // is already held.
MoreCore(const void * mspace,intptr_t increment)340*795d594fSAndroid Build Coastguard Worker void* JitMemoryRegion::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
341*795d594fSAndroid Build Coastguard Worker   if (mspace == exec_mspace_) {
342*795d594fSAndroid Build Coastguard Worker     CHECK(exec_mspace_ != nullptr);
343*795d594fSAndroid Build Coastguard Worker     const MemMap* const code_pages = GetUpdatableCodeMapping();
344*795d594fSAndroid Build Coastguard Worker     void* result = code_pages->Begin() + exec_end_;
345*795d594fSAndroid Build Coastguard Worker     exec_end_ += increment;
346*795d594fSAndroid Build Coastguard Worker     return result;
347*795d594fSAndroid Build Coastguard Worker   } else {
348*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(data_mspace_, mspace);
349*795d594fSAndroid Build Coastguard Worker     const MemMap* const writable_data_pages = GetWritableDataMapping();
350*795d594fSAndroid Build Coastguard Worker     void* result = writable_data_pages->Begin() + data_end_;
351*795d594fSAndroid Build Coastguard Worker     data_end_ += increment;
352*795d594fSAndroid Build Coastguard Worker     return result;
353*795d594fSAndroid Build Coastguard Worker   }
354*795d594fSAndroid Build Coastguard Worker }
355*795d594fSAndroid Build Coastguard Worker 
CommitCode(ArrayRef<const uint8_t> reserved_code,ArrayRef<const uint8_t> code,const uint8_t * stack_map)356*795d594fSAndroid Build Coastguard Worker const uint8_t* JitMemoryRegion::CommitCode(ArrayRef<const uint8_t> reserved_code,
357*795d594fSAndroid Build Coastguard Worker                                            ArrayRef<const uint8_t> code,
358*795d594fSAndroid Build Coastguard Worker                                            const uint8_t* stack_map) {
359*795d594fSAndroid Build Coastguard Worker   DCHECK(IsInExecSpace(reserved_code.data()));
360*795d594fSAndroid Build Coastguard Worker   ScopedCodeCacheWrite scc(*this);
361*795d594fSAndroid Build Coastguard Worker 
362*795d594fSAndroid Build Coastguard Worker   size_t alignment = GetInstructionSetCodeAlignment(kRuntimeISA);
363*795d594fSAndroid Build Coastguard Worker   size_t header_size = OatQuickMethodHeader::InstructionAlignedSize();
364*795d594fSAndroid Build Coastguard Worker   size_t total_size = header_size + code.size();
365*795d594fSAndroid Build Coastguard Worker 
366*795d594fSAndroid Build Coastguard Worker   // Each allocation should be on its own set of cache lines.
367*795d594fSAndroid Build Coastguard Worker   // `total_size` covers the OatQuickMethodHeader, the JIT generated machine code,
368*795d594fSAndroid Build Coastguard Worker   // and any alignment padding.
369*795d594fSAndroid Build Coastguard Worker   DCHECK_GT(total_size, header_size);
370*795d594fSAndroid Build Coastguard Worker   DCHECK_LE(total_size, reserved_code.size());
371*795d594fSAndroid Build Coastguard Worker   uint8_t* x_memory = const_cast<uint8_t*>(reserved_code.data());
372*795d594fSAndroid Build Coastguard Worker   uint8_t* w_memory = const_cast<uint8_t*>(GetNonExecutableAddress(x_memory));
373*795d594fSAndroid Build Coastguard Worker   // Ensure the header ends up at expected instruction alignment.
374*795d594fSAndroid Build Coastguard Worker   DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(w_memory + header_size), alignment);
375*795d594fSAndroid Build Coastguard Worker   const uint8_t* result = x_memory + header_size;
376*795d594fSAndroid Build Coastguard Worker 
377*795d594fSAndroid Build Coastguard Worker   // Write the code.
378*795d594fSAndroid Build Coastguard Worker   std::copy(code.begin(), code.end(), w_memory + header_size);
379*795d594fSAndroid Build Coastguard Worker 
380*795d594fSAndroid Build Coastguard Worker   // Write the header.
381*795d594fSAndroid Build Coastguard Worker   OatQuickMethodHeader* method_header =
382*795d594fSAndroid Build Coastguard Worker       OatQuickMethodHeader::FromCodePointer(w_memory + header_size);
383*795d594fSAndroid Build Coastguard Worker   new (method_header) OatQuickMethodHeader((stack_map != nullptr) ? result - stack_map : 0u);
384*795d594fSAndroid Build Coastguard Worker 
385*795d594fSAndroid Build Coastguard Worker   // Both instruction and data caches need flushing to the point of unification where both share
386*795d594fSAndroid Build Coastguard Worker   // a common view of memory. Flushing the data cache ensures the dirty cachelines from the
387*795d594fSAndroid Build Coastguard Worker   // newly added code are written out to the point of unification. Flushing the instruction
388*795d594fSAndroid Build Coastguard Worker   // cache ensures the newly written code will be fetched from the point of unification before
389*795d594fSAndroid Build Coastguard Worker   // use. Memory in the code cache is re-cycled as code is added and removed. The flushes
390*795d594fSAndroid Build Coastguard Worker   // prevent stale code from residing in the instruction cache.
391*795d594fSAndroid Build Coastguard Worker   //
392*795d594fSAndroid Build Coastguard Worker   // Caches are flushed before write permission is removed because some ARMv8 Qualcomm kernels
393*795d594fSAndroid Build Coastguard Worker   // may trigger a segfault if a page fault occurs when requesting a cache maintenance
394*795d594fSAndroid Build Coastguard Worker   // operation. This is a kernel bug that we need to work around until affected devices
395*795d594fSAndroid Build Coastguard Worker   // (e.g. Nexus 5X and 6P) stop being supported or their kernels are fixed.
396*795d594fSAndroid Build Coastguard Worker   //
397*795d594fSAndroid Build Coastguard Worker   // For reference, this behavior is caused by this commit:
398*795d594fSAndroid Build Coastguard Worker   // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
399*795d594fSAndroid Build Coastguard Worker   //
400*795d594fSAndroid Build Coastguard Worker   bool cache_flush_success = true;
401*795d594fSAndroid Build Coastguard Worker   if (HasDualCodeMapping()) {
402*795d594fSAndroid Build Coastguard Worker     // Flush d-cache for the non-executable mapping.
403*795d594fSAndroid Build Coastguard Worker     cache_flush_success = FlushCpuCaches(w_memory, w_memory + total_size);
404*795d594fSAndroid Build Coastguard Worker   }
405*795d594fSAndroid Build Coastguard Worker 
406*795d594fSAndroid Build Coastguard Worker   // Invalidate i-cache for the executable mapping.
407*795d594fSAndroid Build Coastguard Worker   if (cache_flush_success) {
408*795d594fSAndroid Build Coastguard Worker     cache_flush_success = FlushCpuCaches(x_memory, x_memory + total_size);
409*795d594fSAndroid Build Coastguard Worker   }
410*795d594fSAndroid Build Coastguard Worker 
411*795d594fSAndroid Build Coastguard Worker   // If flushing the cache has failed, reject the allocation because we can't guarantee
412*795d594fSAndroid Build Coastguard Worker   // correctness of the instructions present in the processor caches.
413*795d594fSAndroid Build Coastguard Worker   if (!cache_flush_success) {
414*795d594fSAndroid Build Coastguard Worker     PLOG(ERROR) << "Cache flush failed triggering code allocation failure";
415*795d594fSAndroid Build Coastguard Worker     return nullptr;
416*795d594fSAndroid Build Coastguard Worker   }
417*795d594fSAndroid Build Coastguard Worker 
418*795d594fSAndroid Build Coastguard Worker   // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for
419*795d594fSAndroid Build Coastguard Worker   // correctness as code may still be in instruction pipelines despite the i-cache flush. It is
420*795d594fSAndroid Build Coastguard Worker   // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB
421*795d594fSAndroid Build Coastguard Worker   // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to
422*795d594fSAndroid Build Coastguard Worker   // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have
423*795d594fSAndroid Build Coastguard Worker   // hardware support that broadcasts TLB invalidations and so their kernels have no software
424*795d594fSAndroid Build Coastguard Worker   // based TLB shootdown. The sync-core flavor of membarrier was introduced in Linux 4.16 to
425*795d594fSAndroid Build Coastguard Worker   // address this (see mbarrier(2)). The membarrier here will fail on prior kernels and on
426*795d594fSAndroid Build Coastguard Worker   // platforms lacking the appropriate support.
427*795d594fSAndroid Build Coastguard Worker   art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore);
428*795d594fSAndroid Build Coastguard Worker 
429*795d594fSAndroid Build Coastguard Worker   return result;
430*795d594fSAndroid Build Coastguard Worker }
431*795d594fSAndroid Build Coastguard Worker 
FillRootTable(uint8_t * roots_data,const std::vector<Handle<mirror::Object>> & roots)432*795d594fSAndroid Build Coastguard Worker static void FillRootTable(uint8_t* roots_data, const std::vector<Handle<mirror::Object>>& roots)
433*795d594fSAndroid Build Coastguard Worker     REQUIRES(Locks::jit_lock_)
434*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
435*795d594fSAndroid Build Coastguard Worker   GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
436*795d594fSAndroid Build Coastguard Worker   const uint32_t length = roots.size();
437*795d594fSAndroid Build Coastguard Worker   // Put all roots in `roots_data`.
438*795d594fSAndroid Build Coastguard Worker   for (uint32_t i = 0; i < length; ++i) {
439*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Object> object = roots[i].Get();
440*795d594fSAndroid Build Coastguard Worker     gc_roots[i] = GcRoot<mirror::Object>(object);
441*795d594fSAndroid Build Coastguard Worker   }
442*795d594fSAndroid Build Coastguard Worker   // Store the length of the table at the end. This will allow fetching it from a stack_map
443*795d594fSAndroid Build Coastguard Worker   // pointer.
444*795d594fSAndroid Build Coastguard Worker   reinterpret_cast<uint32_t*>(roots_data)[length] = length;
445*795d594fSAndroid Build Coastguard Worker }
446*795d594fSAndroid Build Coastguard Worker 
CommitData(ArrayRef<const uint8_t> reserved_data,const std::vector<Handle<mirror::Object>> & roots,ArrayRef<const uint8_t> stack_map)447*795d594fSAndroid Build Coastguard Worker bool JitMemoryRegion::CommitData(ArrayRef<const uint8_t> reserved_data,
448*795d594fSAndroid Build Coastguard Worker                                  const std::vector<Handle<mirror::Object>>& roots,
449*795d594fSAndroid Build Coastguard Worker                                  ArrayRef<const uint8_t> stack_map) {
450*795d594fSAndroid Build Coastguard Worker   DCHECK(IsInDataSpace(reserved_data.data()));
451*795d594fSAndroid Build Coastguard Worker   uint8_t* roots_data = GetWritableDataAddress(reserved_data.data());
452*795d594fSAndroid Build Coastguard Worker   size_t root_table_size = ComputeRootTableSize(roots.size());
453*795d594fSAndroid Build Coastguard Worker   uint8_t* stack_map_data = roots_data + root_table_size;
454*795d594fSAndroid Build Coastguard Worker   DCHECK_LE(root_table_size + stack_map.size(), reserved_data.size());
455*795d594fSAndroid Build Coastguard Worker   FillRootTable(roots_data, roots);
456*795d594fSAndroid Build Coastguard Worker   memcpy(stack_map_data, stack_map.data(), stack_map.size());
457*795d594fSAndroid Build Coastguard Worker   // Flush data cache, as compiled code references literals in it.
458*795d594fSAndroid Build Coastguard Worker   // TODO(oth): establish whether this is necessary.
459*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(!FlushCpuCaches(roots_data, roots_data + root_table_size + stack_map.size()))) {
460*795d594fSAndroid Build Coastguard Worker     VLOG(jit) << "Failed to flush data in CommitData";
461*795d594fSAndroid Build Coastguard Worker     return false;
462*795d594fSAndroid Build Coastguard Worker   }
463*795d594fSAndroid Build Coastguard Worker   return true;
464*795d594fSAndroid Build Coastguard Worker }
465*795d594fSAndroid Build Coastguard Worker 
AllocateCode(size_t size)466*795d594fSAndroid Build Coastguard Worker const uint8_t* JitMemoryRegion::AllocateCode(size_t size) {
467*795d594fSAndroid Build Coastguard Worker   size_t alignment = GetInstructionSetCodeAlignment(kRuntimeISA);
468*795d594fSAndroid Build Coastguard Worker   void* result = mspace_memalign(exec_mspace_, alignment, size);
469*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(result == nullptr)) {
470*795d594fSAndroid Build Coastguard Worker     return nullptr;
471*795d594fSAndroid Build Coastguard Worker   }
472*795d594fSAndroid Build Coastguard Worker   used_memory_for_code_ += mspace_usable_size(result);
473*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<uint8_t*>(GetExecutableAddress(result));
474*795d594fSAndroid Build Coastguard Worker }
475*795d594fSAndroid Build Coastguard Worker 
FreeCode(const uint8_t * code)476*795d594fSAndroid Build Coastguard Worker void JitMemoryRegion::FreeCode(const uint8_t* code) {
477*795d594fSAndroid Build Coastguard Worker   code = GetNonExecutableAddress(code);
478*795d594fSAndroid Build Coastguard Worker   used_memory_for_code_ -= mspace_usable_size(code);
479*795d594fSAndroid Build Coastguard Worker   mspace_free(exec_mspace_, const_cast<uint8_t*>(code));
480*795d594fSAndroid Build Coastguard Worker }
481*795d594fSAndroid Build Coastguard Worker 
AllocateData(size_t data_size)482*795d594fSAndroid Build Coastguard Worker const uint8_t* JitMemoryRegion::AllocateData(size_t data_size) {
483*795d594fSAndroid Build Coastguard Worker   void* result = mspace_malloc(data_mspace_, data_size);
484*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(result == nullptr)) {
485*795d594fSAndroid Build Coastguard Worker     return nullptr;
486*795d594fSAndroid Build Coastguard Worker   }
487*795d594fSAndroid Build Coastguard Worker   used_memory_for_data_ += mspace_usable_size(result);
488*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<uint8_t*>(GetNonWritableDataAddress(result));
489*795d594fSAndroid Build Coastguard Worker }
490*795d594fSAndroid Build Coastguard Worker 
FreeData(const uint8_t * data)491*795d594fSAndroid Build Coastguard Worker void JitMemoryRegion::FreeData(const uint8_t* data) {
492*795d594fSAndroid Build Coastguard Worker   FreeWritableData(GetWritableDataAddress(data));
493*795d594fSAndroid Build Coastguard Worker }
494*795d594fSAndroid Build Coastguard Worker 
FreeWritableData(uint8_t * writable_data)495*795d594fSAndroid Build Coastguard Worker void JitMemoryRegion::FreeWritableData(uint8_t* writable_data) REQUIRES(Locks::jit_lock_) {
496*795d594fSAndroid Build Coastguard Worker   used_memory_for_data_ -= mspace_usable_size(writable_data);
497*795d594fSAndroid Build Coastguard Worker   mspace_free(data_mspace_, writable_data);
498*795d594fSAndroid Build Coastguard Worker }
499*795d594fSAndroid Build Coastguard Worker 
500*795d594fSAndroid Build Coastguard Worker #if defined(__BIONIC__) && defined(ART_TARGET)
501*795d594fSAndroid Build Coastguard Worker // The code below only works on bionic on target.
502*795d594fSAndroid Build Coastguard Worker 
CreateZygoteMemory(size_t capacity,std::string * error_msg)503*795d594fSAndroid Build Coastguard Worker int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) {
504*795d594fSAndroid Build Coastguard Worker   if (CacheOperationsMaySegFault()) {
505*795d594fSAndroid Build Coastguard Worker     // Zygote JIT requires dual code mappings by design. We can only do this if the cache flush
506*795d594fSAndroid Build Coastguard Worker     // and invalidate instructions work without raising faults.
507*795d594fSAndroid Build Coastguard Worker     *error_msg = "Zygote memory only works with dual mappings";
508*795d594fSAndroid Build Coastguard Worker     return -1;
509*795d594fSAndroid Build Coastguard Worker   }
510*795d594fSAndroid Build Coastguard Worker   /* Check if kernel support exists, otherwise fall back to ashmem */
511*795d594fSAndroid Build Coastguard Worker   static const char* kRegionName = "jit-zygote-cache";
512*795d594fSAndroid Build Coastguard Worker   if (art::IsSealFutureWriteSupported()) {
513*795d594fSAndroid Build Coastguard Worker     int fd = art::memfd_create(kRegionName, MFD_ALLOW_SEALING);
514*795d594fSAndroid Build Coastguard Worker     if (fd == -1) {
515*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
516*795d594fSAndroid Build Coastguard Worker       oss << "Failed to create zygote mapping: " << strerror(errno);
517*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
518*795d594fSAndroid Build Coastguard Worker       return -1;
519*795d594fSAndroid Build Coastguard Worker     }
520*795d594fSAndroid Build Coastguard Worker 
521*795d594fSAndroid Build Coastguard Worker     if (ftruncate(fd, capacity) != 0) {
522*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
523*795d594fSAndroid Build Coastguard Worker       oss << "Failed to create zygote mapping: " << strerror(errno);
524*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
525*795d594fSAndroid Build Coastguard Worker       return -1;
526*795d594fSAndroid Build Coastguard Worker     }
527*795d594fSAndroid Build Coastguard Worker 
528*795d594fSAndroid Build Coastguard Worker     return fd;
529*795d594fSAndroid Build Coastguard Worker   }
530*795d594fSAndroid Build Coastguard Worker 
531*795d594fSAndroid Build Coastguard Worker   LOG(INFO) << "Falling back to ashmem implementation for JIT zygote mapping";
532*795d594fSAndroid Build Coastguard Worker 
533*795d594fSAndroid Build Coastguard Worker   int fd;
534*795d594fSAndroid Build Coastguard Worker   palette_status_t status = PaletteAshmemCreateRegion(kRegionName, capacity, &fd);
535*795d594fSAndroid Build Coastguard Worker   if (status != PALETTE_STATUS_OK) {
536*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(status, PALETTE_STATUS_CHECK_ERRNO);
537*795d594fSAndroid Build Coastguard Worker     std::ostringstream oss;
538*795d594fSAndroid Build Coastguard Worker     oss << "Failed to create zygote mapping: " << strerror(errno);
539*795d594fSAndroid Build Coastguard Worker     *error_msg = oss.str();
540*795d594fSAndroid Build Coastguard Worker     return -1;
541*795d594fSAndroid Build Coastguard Worker   }
542*795d594fSAndroid Build Coastguard Worker   return fd;
543*795d594fSAndroid Build Coastguard Worker }
544*795d594fSAndroid Build Coastguard Worker 
ProtectZygoteMemory(int fd,std::string * error_msg)545*795d594fSAndroid Build Coastguard Worker bool JitMemoryRegion::ProtectZygoteMemory(int fd, std::string* error_msg) {
546*795d594fSAndroid Build Coastguard Worker   if (art::IsSealFutureWriteSupported()) {
547*795d594fSAndroid Build Coastguard Worker     if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL | F_SEAL_FUTURE_WRITE)
548*795d594fSAndroid Build Coastguard Worker             == -1) {
549*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
550*795d594fSAndroid Build Coastguard Worker       oss << "Failed to protect zygote mapping: " << strerror(errno);
551*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
552*795d594fSAndroid Build Coastguard Worker       return false;
553*795d594fSAndroid Build Coastguard Worker     }
554*795d594fSAndroid Build Coastguard Worker   } else {
555*795d594fSAndroid Build Coastguard Worker     palette_status_t status = PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC);
556*795d594fSAndroid Build Coastguard Worker     if (status != PALETTE_STATUS_OK) {
557*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(status, PALETTE_STATUS_CHECK_ERRNO);
558*795d594fSAndroid Build Coastguard Worker       std::ostringstream oss;
559*795d594fSAndroid Build Coastguard Worker       oss << "Failed to protect zygote mapping: " << strerror(errno);
560*795d594fSAndroid Build Coastguard Worker       *error_msg = oss.str();
561*795d594fSAndroid Build Coastguard Worker       return false;
562*795d594fSAndroid Build Coastguard Worker     }
563*795d594fSAndroid Build Coastguard Worker   }
564*795d594fSAndroid Build Coastguard Worker   return true;
565*795d594fSAndroid Build Coastguard Worker }
566*795d594fSAndroid Build Coastguard Worker 
567*795d594fSAndroid Build Coastguard Worker #else
568*795d594fSAndroid Build Coastguard Worker 
CreateZygoteMemory(size_t capacity,std::string * error_msg)569*795d594fSAndroid Build Coastguard Worker int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) {
570*795d594fSAndroid Build Coastguard Worker   // To simplify host building, we don't rely on the latest memfd features.
571*795d594fSAndroid Build Coastguard Worker   LOG(WARNING) << "Returning un-sealable region on non-bionic";
572*795d594fSAndroid Build Coastguard Worker   static const char* kRegionName = "/jit-zygote-cache";
573*795d594fSAndroid Build Coastguard Worker   int fd = art::memfd_create(kRegionName, 0);
574*795d594fSAndroid Build Coastguard Worker   if (fd == -1) {
575*795d594fSAndroid Build Coastguard Worker     std::ostringstream oss;
576*795d594fSAndroid Build Coastguard Worker     oss << "Failed to create zygote mapping: " << strerror(errno);
577*795d594fSAndroid Build Coastguard Worker     *error_msg = oss.str();
578*795d594fSAndroid Build Coastguard Worker     return -1;
579*795d594fSAndroid Build Coastguard Worker   }
580*795d594fSAndroid Build Coastguard Worker   if (ftruncate(fd, capacity) != 0) {
581*795d594fSAndroid Build Coastguard Worker     std::ostringstream oss;
582*795d594fSAndroid Build Coastguard Worker     oss << "Failed to create zygote mapping: " << strerror(errno);
583*795d594fSAndroid Build Coastguard Worker     *error_msg = oss.str();
584*795d594fSAndroid Build Coastguard Worker     return -1;
585*795d594fSAndroid Build Coastguard Worker   }
586*795d594fSAndroid Build Coastguard Worker   return fd;
587*795d594fSAndroid Build Coastguard Worker }
588*795d594fSAndroid Build Coastguard Worker 
ProtectZygoteMemory(int fd,std::string * error_msg)589*795d594fSAndroid Build Coastguard Worker bool JitMemoryRegion::ProtectZygoteMemory([[maybe_unused]] int fd,
590*795d594fSAndroid Build Coastguard Worker                                           [[maybe_unused]] std::string* error_msg) {
591*795d594fSAndroid Build Coastguard Worker   return true;
592*795d594fSAndroid Build Coastguard Worker }
593*795d594fSAndroid Build Coastguard Worker 
594*795d594fSAndroid Build Coastguard Worker #endif
595*795d594fSAndroid Build Coastguard Worker 
596*795d594fSAndroid Build Coastguard Worker }  // namespace jit
597*795d594fSAndroid Build Coastguard Worker }  // namespace art
598