blob: adf7e3742590d80b531be3a204fb77052a2c5bc4 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/ashmem.h>
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019/*
20 * Implementation of the user-space ashmem API for devices, which have our
21 * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version,
22 * used by the simulator.
23 */
Mark Salyzyne37111d2016-02-02 09:19:39 -080024#define LOG_TAG "ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
Mark Salyzync2d8aad2016-02-02 08:05:54 -080026#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <fcntl.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070028#include <linux/ashmem.h>
Joel Fernandes51944042018-12-18 13:32:31 -080029#include <linux/memfd.h>
30#include <log/log.h>
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080031#include <pthread.h>
Joel Fernandes51944042018-12-18 13:32:31 -080032#include <stdio.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080033#include <string.h>
34#include <sys/ioctl.h>
Joel Fernandes51944042018-12-18 13:32:31 -080035#include <sys/mman.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080036#include <sys/stat.h>
Joel Fernandes51944042018-12-18 13:32:31 -080037#include <sys/syscall.h>
Elliott Hughesd77b5372017-05-17 11:36:51 -070038#include <sys/sysmacros.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080039#include <sys/types.h>
40#include <unistd.h>
Joel Fernandes51944042018-12-18 13:32:31 -080041
Tri Vo92fd3ca2019-09-24 14:06:38 -070042#include <android-base/file.h>
Joel Fernandes51944042018-12-18 13:32:31 -080043#include <android-base/properties.h>
Tri Vo92fd3ca2019-09-24 14:06:38 -070044#include <android-base/strings.h>
Joel Fernandes51944042018-12-18 13:32:31 -080045#include <android-base/unique_fd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080047/* ashmem identity */
48static dev_t __ashmem_rdev;
49/*
50 * If we trigger a signal handler in the middle of locked activity and the
51 * signal handler calls ashmem, we could get into a deadlock state.
52 */
53static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
54
Tri Vo2891ba02019-01-28 17:56:43 -080055/*
Joel Fernandes51944042018-12-18 13:32:31 -080056 * has_memfd_support() determines if the device can use memfd. memfd support
57 * has been there for long time, but certain things in it may be missing. We
58 * check for needed support in it. Also we check if the VNDK version of
59 * libcutils being used is new enough, if its not, then we cannot use memfd
60 * since the older copies may be using ashmem so we just use ashmem. Once all
61 * Android devices that are getting updates are new enough (ex, they were
62 * originally shipped with Android release > P), then we can just use memfd and
63 * delete all ashmem code from libcutils (while preserving the interface).
64 *
65 * NOTE:
66 * The sys.use_memfd property is set by default to false in Android
67 * to temporarily disable memfd, till vendor and apps are ready for it.
68 * The main issue: either apps or vendor processes can directly make ashmem
69 * IOCTLs on FDs they receive by assuming they are ashmem, without going
70 * through libcutils. Such fds could have very well be originally created with
71 * libcutils hence they could be memfd. Thus the IOCTLs will break.
72 *
73 * Set default value of sys.use_memfd property to true once the issue is
74 * resolved, so that the code can then self-detect if kernel support is present
75 * on the device. The property can also set to true from adb shell, for
76 * debugging.
77 */
78
79static bool debug_log = false; /* set to true for verbose logging and other debug */
80static bool pin_deprecation_warn = true; /* Log the pin deprecation warning only once */
81
82/* Determine if vendor processes would be ok with memfd in the system:
83 *
Kiyoung Kim45d88d42023-09-21 16:03:41 +090084 * Previously this function checked if memfd is supported by checking if
85 * vendor VNDK version is greater than Q. As we can assume all treblelized
86 * device using this code is up to date enough to use memfd, memfd is allowed
87 * if the device is treblelized.
Joel Fernandes51944042018-12-18 13:32:31 -080088 */
89static bool check_vendor_memfd_allowed() {
Kiyoung Kim45d88d42023-09-21 16:03:41 +090090 static bool is_treblelized = android::base::GetBoolProperty("ro.treble.enabled", false);
Joel Fernandes51944042018-12-18 13:32:31 -080091
Kiyoung Kim45d88d42023-09-21 16:03:41 +090092 return is_treblelized;
Joel Fernandes51944042018-12-18 13:32:31 -080093}
94
Joel Fernandes51944042018-12-18 13:32:31 -080095/* Determine if memfd can be supported. This is just one-time hardwork
96 * which will be cached by the caller.
97 */
98static bool __has_memfd_support() {
99 if (check_vendor_memfd_allowed() == false) {
100 return false;
101 }
102
103 /* Used to turn on/off the detection at runtime, in the future this
104 * property will be removed once we switch everything over to ashmem.
105 * Currently it is used only for debugging to switch the system over.
106 */
107 if (!android::base::GetBoolProperty("sys.use_memfd", false)) {
108 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800109 ALOGD("sys.use_memfd=false so memfd disabled");
Joel Fernandes51944042018-12-18 13:32:31 -0800110 }
111 return false;
112 }
113
Elliott Hughes790ef052020-09-03 10:53:16 -0700114 // Check if kernel support exists, otherwise fall back to ashmem.
115 // This code needs to build on old API levels, so we can't use the libc
116 // wrapper.
Isaac J. Manjarresee7a7132024-12-03 09:42:56 -0800117 //
118 // MFD_NOEXEC_SEAL is used to match the semantics of the ashmem device,
119 // which did not have executable permissions. This also seals the executable
120 // permissions of the buffer (i.e. they cannot be changed by fchmod()).
121 //
122 // MFD_NOEXEC_SEAL implies MFD_ALLOW_SEALING.
Joel Fernandes51944042018-12-18 13:32:31 -0800123 android::base::unique_fd fd(
Isaac J. Manjarresee7a7132024-12-03 09:42:56 -0800124 syscall(__NR_memfd_create, "test_android_memfd", MFD_CLOEXEC | MFD_NOEXEC_SEAL));
Joel Fernandes51944042018-12-18 13:32:31 -0800125 if (fd == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800126 ALOGE("memfd_create failed: %m, no memfd support");
Joel Fernandes51944042018-12-18 13:32:31 -0800127 return false;
128 }
129
130 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800131 ALOGE("fcntl(F_ADD_SEALS) failed: %m, no memfd support");
Joel Fernandes51944042018-12-18 13:32:31 -0800132 return false;
133 }
134
Isaac J. Manjarresf518d462024-12-26 11:34:21 -0800135 /*
136 * Ensure that the kernel supports ashmem ioctl commands on memfds. If not,
137 * fall back to using ashmem.
138 */
139 if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, getpagesize())) < 0) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800140 ALOGE("ioctl(ASHMEM_SET_SIZE, %d) failed: %m, no ashmem-memfd compat support",
141 getpagesize());
Isaac J. Manjarresf518d462024-12-26 11:34:21 -0800142 return false;
143 }
144
Joel Fernandes51944042018-12-18 13:32:31 -0800145 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800146 ALOGD("memfd: device has memfd support, using it");
Joel Fernandes51944042018-12-18 13:32:31 -0800147 }
148 return true;
149}
150
151static bool has_memfd_support() {
152 /* memfd_supported is the initial global per-process state of what is known
153 * about memfd.
154 */
155 static bool memfd_supported = __has_memfd_support();
156
157 return memfd_supported;
158}
159
Tri Vo92fd3ca2019-09-24 14:06:38 -0700160static std::string get_ashmem_device_path() {
161 static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
162 std::string boot_id;
163 if (!android::base::ReadFileToString(boot_id_path, &boot_id)) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800164 ALOGE("Failed to read %s: %m", boot_id_path.c_str());
Tri Vo92fd3ca2019-09-24 14:06:38 -0700165 return "";
166 };
167 boot_id = android::base::Trim(boot_id);
168
169 return "/dev/ashmem" + boot_id;
170}
171
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800172/* logistics of getting file descriptor for ashmem */
173static int __ashmem_open_locked()
174{
Tri Vo92fd3ca2019-09-24 14:06:38 -0700175 static const std::string ashmem_device_path = get_ashmem_device_path();
176
Tri Vo92fd3ca2019-09-24 14:06:38 -0700177 if (ashmem_device_path.empty()) {
178 return -1;
Tim Murray8879ed72019-04-04 09:16:32 -0700179 }
180
Tri Vo92fd3ca2019-09-24 14:06:38 -0700181 int fd = TEMP_FAILURE_RETRY(open(ashmem_device_path.c_str(), O_RDWR | O_CLOEXEC));
Steven Moreland4f99dd32020-01-09 14:42:32 -0800182
183 // fallback for APEX w/ use_vendor on Q, which would have still used /dev/ashmem
184 if (fd < 0) {
Hridya Valsaraju9a147032020-08-07 12:22:24 -0700185 int saved_errno = errno;
Steven Moreland4f99dd32020-01-09 14:42:32 -0800186 fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC));
Hridya Valsaraju9a147032020-08-07 12:22:24 -0700187 if (fd < 0) {
188 /* Q launching devices and newer must not reach here since they should have been
189 * able to open ashmem_device_path */
190 ALOGE("Unable to open ashmem device %s (error = %s) and /dev/ashmem(error = %s)",
191 ashmem_device_path.c_str(), strerror(saved_errno), strerror(errno));
192 return fd;
193 }
Steven Moreland4f99dd32020-01-09 14:42:32 -0800194 }
Steven Moreland4f99dd32020-01-09 14:42:32 -0800195 struct stat st;
196 int ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800197 if (ret < 0) {
198 int save_errno = errno;
199 close(fd);
200 errno = save_errno;
201 return ret;
202 }
203 if (!S_ISCHR(st.st_mode) || !st.st_rdev) {
204 close(fd);
205 errno = ENOTTY;
206 return -1;
207 }
208
209 __ashmem_rdev = st.st_rdev;
210 return fd;
211}
212
213static int __ashmem_open()
214{
215 int fd;
216
217 pthread_mutex_lock(&__ashmem_lock);
218 fd = __ashmem_open_locked();
219 pthread_mutex_unlock(&__ashmem_lock);
220
221 return fd;
222}
223
224/* Make sure file descriptor references ashmem, negative number means false */
Mark Salyzynee431112016-08-23 13:58:37 -0700225static int __ashmem_is_ashmem(int fd, int fatal)
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800226{
227 dev_t rdev;
228 struct stat st;
229
Joel Fernandes56cd6512018-07-17 13:00:17 -0700230 if (fstat(fd, &st) < 0) {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800231 return -1;
232 }
233
Mark Salyzyne37111d2016-02-02 09:19:39 -0800234 rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800235 if (S_ISCHR(st.st_mode) && st.st_rdev) {
236 pthread_mutex_lock(&__ashmem_lock);
237 rdev = __ashmem_rdev;
238 if (rdev) {
239 pthread_mutex_unlock(&__ashmem_lock);
240 } else {
241 int fd = __ashmem_open_locked();
242 if (fd < 0) {
243 pthread_mutex_unlock(&__ashmem_lock);
244 return -1;
245 }
246 rdev = __ashmem_rdev;
247 pthread_mutex_unlock(&__ashmem_lock);
248
249 close(fd);
250 }
251
252 if (st.st_rdev == rdev) {
253 return 0;
254 }
255 }
256
Mark Salyzynee431112016-08-23 13:58:37 -0700257 if (fatal) {
258 if (rdev) {
259 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d",
260 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
261 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP,
262 major(rdev), minor(rdev));
263 } else {
264 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o",
265 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
266 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP);
267 }
268 /* NOTREACHED */
Mark Salyzyne37111d2016-02-02 09:19:39 -0800269 }
270
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800271 errno = ENOTTY;
272 return -1;
273}
274
Joel Fernandes56cd6512018-07-17 13:00:17 -0700275static int __ashmem_check_failure(int fd, int result)
276{
277 if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, 1);
278 return result;
279}
280
Joel Fernandes51944042018-12-18 13:32:31 -0800281static bool memfd_is_ashmem(int fd) {
282 static bool fd_check_error_once = false;
283
284 if (__ashmem_is_ashmem(fd, 0) == 0) {
285 if (!fd_check_error_once) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800286 ALOGE("memfd: memfd expected but ashmem fd used - please use libcutils");
Joel Fernandes51944042018-12-18 13:32:31 -0800287 fd_check_error_once = true;
288 }
289
290 return true;
291 }
292
293 return false;
294}
295
Mark Salyzynee431112016-08-23 13:58:37 -0700296int ashmem_valid(int fd)
297{
Joel Fernandes51944042018-12-18 13:32:31 -0800298 if (has_memfd_support() && !memfd_is_ashmem(fd)) {
299 return 1;
300 }
301
Mark Salyzynee431112016-08-23 13:58:37 -0700302 return __ashmem_is_ashmem(fd, 0) >= 0;
303}
304
Joel Fernandes51944042018-12-18 13:32:31 -0800305static int memfd_create_region(const char* name, size_t size) {
Elliott Hughes790ef052020-09-03 10:53:16 -0700306 // This code needs to build on old API levels, so we can't use the libc
307 // wrapper.
Isaac J. Manjarresee7a7132024-12-03 09:42:56 -0800308 //
309 // MFD_NOEXEC_SEAL to match the semantics of the ashmem device, which did
310 // not have executable permissions. This also seals the executable
311 // permissions of the buffer (i.e. they cannot be changed by fchmod()).
312 //
313 // MFD_NOEXEC_SEAL implies MFD_ALLOW_SEALING.
314 android::base::unique_fd fd(syscall(__NR_memfd_create, name, MFD_CLOEXEC | MFD_NOEXEC_SEAL));
Joel Fernandes51944042018-12-18 13:32:31 -0800315
316 if (fd == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800317 ALOGE("memfd_create(%s, %zd) failed: %m", name, size);
Joel Fernandes51944042018-12-18 13:32:31 -0800318 return -1;
319 }
320
321 if (ftruncate(fd, size) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800322 ALOGE("ftruncate(%s, %zd) failed for memfd creation: %m", name, size);
Joel Fernandes51944042018-12-18 13:32:31 -0800323 return -1;
324 }
325
Keith Mokf83c5c82023-08-31 00:31:35 +0000326 // forbid size changes to match ashmem behaviour
327 if (fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK) == -1) {
328 ALOGE("memfd_create(%s, %zd) F_ADD_SEALS failed: %m", name, size);
329 return -1;
330 }
331
Joel Fernandes51944042018-12-18 13:32:31 -0800332 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800333 ALOGE("memfd_create(%s, %zd) success. fd=%d", name, size, fd.get());
Joel Fernandes51944042018-12-18 13:32:31 -0800334 }
335 return fd.release();
336}
337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338/*
339 * ashmem_create_region - creates a new ashmem region and returns the file
340 * descriptor, or <0 on error
341 *
342 * `name' is an optional label to give the region (visible in /proc/pid/maps)
343 * `size' is the size of the region, in page-aligned bytes
344 */
345int ashmem_create_region(const char *name, size_t size)
346{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800347 int ret, save_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
Joel Fernandes51944042018-12-18 13:32:31 -0800349 if (has_memfd_support()) {
350 return memfd_create_region(name ? name : "none", size);
351 }
352
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800353 int fd = __ashmem_open();
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800354 if (fd < 0) {
355 return fd;
356 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800358 if (name) {
359 char buf[ASHMEM_NAME_LEN] = {0};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800361 strlcpy(buf, name, sizeof(buf));
362 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
363 if (ret < 0) {
364 goto error;
365 }
366 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800368 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
369 if (ret < 0) {
370 goto error;
371 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800373 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
375error:
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800376 save_errno = errno;
377 close(fd);
378 errno = save_errno;
379 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380}
381
Joel Fernandes51944042018-12-18 13:32:31 -0800382static int memfd_set_prot_region(int fd, int prot) {
Keith Mokf83c5c82023-08-31 00:31:35 +0000383 int seals = fcntl(fd, F_GET_SEALS);
384 if (seals == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800385 ALOGE("memfd_set_prot_region(%d, %d): F_GET_SEALS failed: %m", fd, prot);
Keith Mokf83c5c82023-08-31 00:31:35 +0000386 return -1;
387 }
388
Joel Fernandes51944042018-12-18 13:32:31 -0800389 if (prot & PROT_WRITE) {
Keith Mokf83c5c82023-08-31 00:31:35 +0000390 /* Now we want the buffer to be read-write, let's check if the buffer
391 * has been previously marked as read-only before, if so return error
392 */
393 if (seals & F_SEAL_FUTURE_WRITE) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800394 ALOGE("memfd_set_prot_region(%d, %d): region is write protected", fd, prot);
Keith Mokf83c5c82023-08-31 00:31:35 +0000395 errno = EINVAL; // inline with ashmem error code, if already in
396 // read-only mode
397 return -1;
398 }
Joel Fernandes51944042018-12-18 13:32:31 -0800399 return 0;
400 }
401
Keith Mokf83c5c82023-08-31 00:31:35 +0000402 /* We would only allow read-only for any future file operations */
Isaac J. Manjarres2a760952024-12-26 18:14:24 -0800403 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800404 ALOGE("memfd_set_prot_region(%d, %d): F_SEAL_FUTURE_WRITE seal failed: %m", fd, prot);
Joel Fernandes51944042018-12-18 13:32:31 -0800405 return -1;
406 }
407
408 return 0;
409}
410
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411int ashmem_set_prot_region(int fd, int prot)
412{
Joel Fernandes51944042018-12-18 13:32:31 -0800413 if (has_memfd_support() && !memfd_is_ashmem(fd)) {
414 return memfd_set_prot_region(fd, prot);
415 }
416
Joel Fernandes56cd6512018-07-17 13:00:17 -0700417 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418}
419
420int ashmem_pin_region(int fd, size_t offset, size_t len)
421{
Joel Fernandes51944042018-12-18 13:32:31 -0800422 if (!pin_deprecation_warn || debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800423 ALOGE("Pinning is deprecated since Android Q. Please use trim or other methods.");
Joel Fernandes51944042018-12-18 13:32:31 -0800424 pin_deprecation_warn = true;
425 }
426
427 if (has_memfd_support() && !memfd_is_ashmem(fd)) {
428 return 0;
429 }
430
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800431 // TODO: should LP64 reject too-large offset/len?
432 ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
Joel Fernandes56cd6512018-07-17 13:00:17 -0700433 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434}
435
436int ashmem_unpin_region(int fd, size_t offset, size_t len)
437{
Joel Fernandes51944042018-12-18 13:32:31 -0800438 if (!pin_deprecation_warn || debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800439 ALOGE("Pinning is deprecated since Android Q. Please use trim or other methods.");
Joel Fernandes51944042018-12-18 13:32:31 -0800440 pin_deprecation_warn = true;
441 }
442
443 if (has_memfd_support() && !memfd_is_ashmem(fd)) {
444 return 0;
445 }
446
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800447 // TODO: should LP64 reject too-large offset/len?
448 ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
Joel Fernandes56cd6512018-07-17 13:00:17 -0700449 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100451
452int ashmem_get_size_region(int fd)
453{
Joel Fernandes51944042018-12-18 13:32:31 -0800454 if (has_memfd_support() && !memfd_is_ashmem(fd)) {
455 struct stat sb;
456
457 if (fstat(fd, &sb) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800458 ALOGE("ashmem_get_size_region(%d): fstat failed: %m", fd);
Joel Fernandes51944042018-12-18 13:32:31 -0800459 return -1;
460 }
461
462 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800463 ALOGD("ashmem_get_size_region(%d): %d", fd, static_cast<int>(sb.st_size));
Joel Fernandes51944042018-12-18 13:32:31 -0800464 }
465
466 return sb.st_size;
467 }
468
Joel Fernandes56cd6512018-07-17 13:00:17 -0700469 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100470}