blob: 388a1e8b461ce4a13667e8f17da250643bf30bd7 [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
Isaac J. Manjarres78541b72025-03-03 16:06:58 -080047#include "ashmem-internal.h"
48
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080049/* ashmem identity */
50static dev_t __ashmem_rdev;
51/*
52 * If we trigger a signal handler in the middle of locked activity and the
53 * signal handler calls ashmem, we could get into a deadlock state.
54 */
55static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
56
Tri Vo2891ba02019-01-28 17:56:43 -080057/*
Joel Fernandes51944042018-12-18 13:32:31 -080058 * has_memfd_support() determines if the device can use memfd. memfd support
59 * has been there for long time, but certain things in it may be missing. We
60 * check for needed support in it. Also we check if the VNDK version of
61 * libcutils being used is new enough, if its not, then we cannot use memfd
62 * since the older copies may be using ashmem so we just use ashmem. Once all
63 * Android devices that are getting updates are new enough (ex, they were
64 * originally shipped with Android release > P), then we can just use memfd and
65 * delete all ashmem code from libcutils (while preserving the interface).
66 *
67 * NOTE:
68 * The sys.use_memfd property is set by default to false in Android
69 * to temporarily disable memfd, till vendor and apps are ready for it.
70 * The main issue: either apps or vendor processes can directly make ashmem
71 * IOCTLs on FDs they receive by assuming they are ashmem, without going
72 * through libcutils. Such fds could have very well be originally created with
73 * libcutils hence they could be memfd. Thus the IOCTLs will break.
74 *
75 * Set default value of sys.use_memfd property to true once the issue is
76 * resolved, so that the code can then self-detect if kernel support is present
77 * on the device. The property can also set to true from adb shell, for
78 * debugging.
79 */
80
Elliott Hughesc0263252025-02-24 14:23:04 -080081/* set to true for verbose logging and other debug */
82static bool debug_log = false;
Joel Fernandes51944042018-12-18 13:32:31 -080083
84/* Determine if vendor processes would be ok with memfd in the system:
85 *
Kiyoung Kim45d88d42023-09-21 16:03:41 +090086 * Previously this function checked if memfd is supported by checking if
87 * vendor VNDK version is greater than Q. As we can assume all treblelized
88 * device using this code is up to date enough to use memfd, memfd is allowed
89 * if the device is treblelized.
Joel Fernandes51944042018-12-18 13:32:31 -080090 */
91static bool check_vendor_memfd_allowed() {
Kiyoung Kim45d88d42023-09-21 16:03:41 +090092 static bool is_treblelized = android::base::GetBoolProperty("ro.treble.enabled", false);
Joel Fernandes51944042018-12-18 13:32:31 -080093
Kiyoung Kim45d88d42023-09-21 16:03:41 +090094 return is_treblelized;
Joel Fernandes51944042018-12-18 13:32:31 -080095}
96
Joel Fernandes51944042018-12-18 13:32:31 -080097/* Determine if memfd can be supported. This is just one-time hardwork
98 * which will be cached by the caller.
99 */
100static bool __has_memfd_support() {
101 if (check_vendor_memfd_allowed() == false) {
102 return false;
103 }
104
105 /* Used to turn on/off the detection at runtime, in the future this
106 * property will be removed once we switch everything over to ashmem.
107 * Currently it is used only for debugging to switch the system over.
108 */
109 if (!android::base::GetBoolProperty("sys.use_memfd", false)) {
110 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800111 ALOGD("sys.use_memfd=false so memfd disabled");
Joel Fernandes51944042018-12-18 13:32:31 -0800112 }
113 return false;
114 }
115
Elliott Hughes790ef052020-09-03 10:53:16 -0700116 // Check if kernel support exists, otherwise fall back to ashmem.
117 // This code needs to build on old API levels, so we can't use the libc
118 // wrapper.
Isaac J. Manjarresee7a7132024-12-03 09:42:56 -0800119 //
120 // MFD_NOEXEC_SEAL is used to match the semantics of the ashmem device,
121 // which did not have executable permissions. This also seals the executable
122 // permissions of the buffer (i.e. they cannot be changed by fchmod()).
123 //
124 // MFD_NOEXEC_SEAL implies MFD_ALLOW_SEALING.
Isaac J. Manjarres01f60e42025-02-05 01:08:20 -0800125
Joel Fernandes51944042018-12-18 13:32:31 -0800126 android::base::unique_fd fd(
Elliott Hughesc0263252025-02-24 14:23:04 -0800127 syscall(__NR_memfd_create, "test_android_memfd", MFD_CLOEXEC | MFD_NOEXEC_SEAL));
Joel Fernandes51944042018-12-18 13:32:31 -0800128 if (fd == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800129 ALOGE("memfd_create failed: %m, no memfd support");
Joel Fernandes51944042018-12-18 13:32:31 -0800130 return false;
131 }
132
133 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800134 ALOGE("fcntl(F_ADD_SEALS) failed: %m, no memfd support");
Joel Fernandes51944042018-12-18 13:32:31 -0800135 return false;
136 }
137
Elliott Hughesc0263252025-02-24 14:23:04 -0800138 size_t buf_size = getpagesize();
Isaac J. Manjarres01f60e42025-02-05 01:08:20 -0800139 if (ftruncate(fd, buf_size) == -1) {
Elliott Hughesc0263252025-02-24 14:23:04 -0800140 ALOGE("ftruncate(%zd) failed to set memfd buffer size: %m, no memfd support", buf_size);
Isaac J. Manjarres01f60e42025-02-05 01:08:20 -0800141 return false;
142 }
143
Isaac J. Manjarresf518d462024-12-26 11:34:21 -0800144 /*
145 * Ensure that the kernel supports ashmem ioctl commands on memfds. If not,
146 * fall back to using ashmem.
147 */
Isaac J. Manjarres01f60e42025-02-05 01:08:20 -0800148 int ashmem_size = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, 0));
149 if (ashmem_size != static_cast<int>(buf_size)) {
150 ALOGE("ioctl(ASHMEM_GET_SIZE): %d != buf_size: %zd , no ashmem-memfd compat support",
151 ashmem_size, buf_size);
Isaac J. Manjarresf518d462024-12-26 11:34:21 -0800152 return false;
153 }
154
Joel Fernandes51944042018-12-18 13:32:31 -0800155 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800156 ALOGD("memfd: device has memfd support, using it");
Joel Fernandes51944042018-12-18 13:32:31 -0800157 }
158 return true;
159}
160
Isaac J. Manjarres78541b72025-03-03 16:06:58 -0800161bool has_memfd_support() {
Joel Fernandes51944042018-12-18 13:32:31 -0800162 static bool memfd_supported = __has_memfd_support();
Joel Fernandes51944042018-12-18 13:32:31 -0800163 return memfd_supported;
164}
165
Tri Vo92fd3ca2019-09-24 14:06:38 -0700166static std::string get_ashmem_device_path() {
167 static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
168 std::string boot_id;
169 if (!android::base::ReadFileToString(boot_id_path, &boot_id)) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800170 ALOGE("Failed to read %s: %m", boot_id_path.c_str());
Tri Vo92fd3ca2019-09-24 14:06:38 -0700171 return "";
Elliott Hughesc0263252025-02-24 14:23:04 -0800172 }
Tri Vo92fd3ca2019-09-24 14:06:38 -0700173 boot_id = android::base::Trim(boot_id);
174
175 return "/dev/ashmem" + boot_id;
176}
177
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800178/* logistics of getting file descriptor for ashmem */
Elliott Hughesc0263252025-02-24 14:23:04 -0800179static int __ashmem_open_locked() {
Tri Vo92fd3ca2019-09-24 14:06:38 -0700180 static const std::string ashmem_device_path = get_ashmem_device_path();
181
Tri Vo92fd3ca2019-09-24 14:06:38 -0700182 if (ashmem_device_path.empty()) {
183 return -1;
Tim Murray8879ed72019-04-04 09:16:32 -0700184 }
185
Elliott Hughesc0263252025-02-24 14:23:04 -0800186 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(ashmem_device_path.c_str(), O_RDWR | O_CLOEXEC)));
Elliott Hughesc0263252025-02-24 14:23:04 -0800187 if (!fd.ok()) {
Isaac J. Manjarres6d4e75a2025-03-03 14:07:06 -0800188 ALOGE("Unable to open ashmem device: %m");
189 return -1;
Steven Moreland4f99dd32020-01-09 14:42:32 -0800190 }
Elliott Hughesc0263252025-02-24 14:23:04 -0800191
Steven Moreland4f99dd32020-01-09 14:42:32 -0800192 struct stat st;
Elliott Hughesc0263252025-02-24 14:23:04 -0800193 if (TEMP_FAILURE_RETRY(fstat(fd, &st)) == -1) {
194 return -1;
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800195 }
196 if (!S_ISCHR(st.st_mode) || !st.st_rdev) {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800197 errno = ENOTTY;
198 return -1;
199 }
200
201 __ashmem_rdev = st.st_rdev;
Elliott Hughesc0263252025-02-24 14:23:04 -0800202 return fd.release();
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800203}
204
Elliott Hughesc0263252025-02-24 14:23:04 -0800205static int __ashmem_open() {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800206 pthread_mutex_lock(&__ashmem_lock);
Elliott Hughesc0263252025-02-24 14:23:04 -0800207 int fd = __ashmem_open_locked();
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800208 pthread_mutex_unlock(&__ashmem_lock);
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800209 return fd;
210}
211
212/* Make sure file descriptor references ashmem, negative number means false */
Elliott Hughesc0263252025-02-24 14:23:04 -0800213static int __ashmem_is_ashmem(int fd, bool fatal) {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800214 struct stat st;
Joel Fernandes56cd6512018-07-17 13:00:17 -0700215 if (fstat(fd, &st) < 0) {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800216 return -1;
217 }
218
Elliott Hughesc0263252025-02-24 14:23:04 -0800219 dev_t rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800220 if (S_ISCHR(st.st_mode) && st.st_rdev) {
221 pthread_mutex_lock(&__ashmem_lock);
222 rdev = __ashmem_rdev;
223 if (rdev) {
224 pthread_mutex_unlock(&__ashmem_lock);
225 } else {
226 int fd = __ashmem_open_locked();
227 if (fd < 0) {
228 pthread_mutex_unlock(&__ashmem_lock);
229 return -1;
230 }
231 rdev = __ashmem_rdev;
232 pthread_mutex_unlock(&__ashmem_lock);
233
234 close(fd);
235 }
236
237 if (st.st_rdev == rdev) {
238 return 0;
239 }
240 }
241
Mark Salyzynee431112016-08-23 13:58:37 -0700242 if (fatal) {
243 if (rdev) {
244 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d",
245 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
246 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP,
247 major(rdev), minor(rdev));
248 } else {
249 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o",
250 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
251 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP);
252 }
253 /* NOTREACHED */
Mark Salyzyne37111d2016-02-02 09:19:39 -0800254 }
255
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800256 errno = ENOTTY;
257 return -1;
258}
259
Elliott Hughesc0263252025-02-24 14:23:04 -0800260static int __ashmem_check_failure(int fd, int result) {
261 if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, true);
Joel Fernandes56cd6512018-07-17 13:00:17 -0700262 return result;
263}
264
Elliott Hughesc0263252025-02-24 14:23:04 -0800265static bool is_ashmem_fd(int fd) {
Joel Fernandes51944042018-12-18 13:32:31 -0800266 static bool fd_check_error_once = false;
267
Elliott Hughesc0263252025-02-24 14:23:04 -0800268 if (__ashmem_is_ashmem(fd, false) == 0) {
Joel Fernandes51944042018-12-18 13:32:31 -0800269 if (!fd_check_error_once) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800270 ALOGE("memfd: memfd expected but ashmem fd used - please use libcutils");
Joel Fernandes51944042018-12-18 13:32:31 -0800271 fd_check_error_once = true;
272 }
273
274 return true;
275 }
276
277 return false;
278}
279
Elliott Hughesc0263252025-02-24 14:23:04 -0800280static bool is_memfd_fd(int fd) {
281 return has_memfd_support() && !is_ashmem_fd(fd);
282}
283
284int ashmem_valid(int fd) {
285 if (is_memfd_fd(fd)) {
Joel Fernandes51944042018-12-18 13:32:31 -0800286 return 1;
287 }
288
Elliott Hughesc0263252025-02-24 14:23:04 -0800289 return __ashmem_is_ashmem(fd, false) >= 0;
Mark Salyzynee431112016-08-23 13:58:37 -0700290}
291
Joel Fernandes51944042018-12-18 13:32:31 -0800292static int memfd_create_region(const char* name, size_t size) {
Elliott Hughes790ef052020-09-03 10:53:16 -0700293 // This code needs to build on old API levels, so we can't use the libc
294 // wrapper.
Isaac J. Manjarresee7a7132024-12-03 09:42:56 -0800295 //
296 // MFD_NOEXEC_SEAL to match the semantics of the ashmem device, which did
297 // not have executable permissions. This also seals the executable
298 // permissions of the buffer (i.e. they cannot be changed by fchmod()).
299 //
300 // MFD_NOEXEC_SEAL implies MFD_ALLOW_SEALING.
301 android::base::unique_fd fd(syscall(__NR_memfd_create, name, MFD_CLOEXEC | MFD_NOEXEC_SEAL));
Joel Fernandes51944042018-12-18 13:32:31 -0800302
303 if (fd == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800304 ALOGE("memfd_create(%s, %zd) failed: %m", name, size);
Joel Fernandes51944042018-12-18 13:32:31 -0800305 return -1;
306 }
307
308 if (ftruncate(fd, size) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800309 ALOGE("ftruncate(%s, %zd) failed for memfd creation: %m", name, size);
Joel Fernandes51944042018-12-18 13:32:31 -0800310 return -1;
311 }
312
Keith Mokf83c5c82023-08-31 00:31:35 +0000313 // forbid size changes to match ashmem behaviour
314 if (fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK) == -1) {
315 ALOGE("memfd_create(%s, %zd) F_ADD_SEALS failed: %m", name, size);
316 return -1;
317 }
318
Joel Fernandes51944042018-12-18 13:32:31 -0800319 if (debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800320 ALOGE("memfd_create(%s, %zd) success. fd=%d", name, size, fd.get());
Joel Fernandes51944042018-12-18 13:32:31 -0800321 }
322 return fd.release();
323}
324
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325/*
326 * ashmem_create_region - creates a new ashmem region and returns the file
327 * descriptor, or <0 on error
328 *
329 * `name' is an optional label to give the region (visible in /proc/pid/maps)
330 * `size' is the size of the region, in page-aligned bytes
331 */
Elliott Hughesc0263252025-02-24 14:23:04 -0800332int ashmem_create_region(const char* name, size_t size) {
333 if (name == NULL) name = "none";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334
Joel Fernandes51944042018-12-18 13:32:31 -0800335 if (has_memfd_support()) {
Elliott Hughesc0263252025-02-24 14:23:04 -0800336 return memfd_create_region(name, size);
Joel Fernandes51944042018-12-18 13:32:31 -0800337 }
338
Elliott Hughesc0263252025-02-24 14:23:04 -0800339 android::base::unique_fd fd(__ashmem_open());
340 if (!fd.ok() ||
341 TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, name) < 0) ||
342 TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size) < 0)) {
343 return -1;
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800344 }
Elliott Hughesc0263252025-02-24 14:23:04 -0800345 return fd.release();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346}
347
Joel Fernandes51944042018-12-18 13:32:31 -0800348static int memfd_set_prot_region(int fd, int prot) {
Keith Mokf83c5c82023-08-31 00:31:35 +0000349 int seals = fcntl(fd, F_GET_SEALS);
350 if (seals == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800351 ALOGE("memfd_set_prot_region(%d, %d): F_GET_SEALS failed: %m", fd, prot);
Keith Mokf83c5c82023-08-31 00:31:35 +0000352 return -1;
353 }
354
Joel Fernandes51944042018-12-18 13:32:31 -0800355 if (prot & PROT_WRITE) {
Keith Mokf83c5c82023-08-31 00:31:35 +0000356 /* Now we want the buffer to be read-write, let's check if the buffer
357 * has been previously marked as read-only before, if so return error
358 */
359 if (seals & F_SEAL_FUTURE_WRITE) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800360 ALOGE("memfd_set_prot_region(%d, %d): region is write protected", fd, prot);
Keith Mokf83c5c82023-08-31 00:31:35 +0000361 errno = EINVAL; // inline with ashmem error code, if already in
362 // read-only mode
363 return -1;
364 }
Joel Fernandes51944042018-12-18 13:32:31 -0800365 return 0;
366 }
367
Keith Mokf83c5c82023-08-31 00:31:35 +0000368 /* We would only allow read-only for any future file operations */
Isaac J. Manjarres2a760952024-12-26 18:14:24 -0800369 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800370 ALOGE("memfd_set_prot_region(%d, %d): F_SEAL_FUTURE_WRITE seal failed: %m", fd, prot);
Joel Fernandes51944042018-12-18 13:32:31 -0800371 return -1;
372 }
373
374 return 0;
375}
376
Elliott Hughesc0263252025-02-24 14:23:04 -0800377int ashmem_set_prot_region(int fd, int prot) {
378 if (is_memfd_fd(fd)) {
Joel Fernandes51944042018-12-18 13:32:31 -0800379 return memfd_set_prot_region(fd, prot);
380 }
381
Joel Fernandes56cd6512018-07-17 13:00:17 -0700382 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 -0800383}
384
Elliott Hughesc0263252025-02-24 14:23:04 -0800385static int do_pin(int op, int fd, size_t offset, size_t length) {
386 static bool already_warned_about_pin_deprecation = false;
387 if (!already_warned_about_pin_deprecation || debug_log) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800388 ALOGE("Pinning is deprecated since Android Q. Please use trim or other methods.");
Elliott Hughesc0263252025-02-24 14:23:04 -0800389 already_warned_about_pin_deprecation = true;
Joel Fernandes51944042018-12-18 13:32:31 -0800390 }
391
Elliott Hughesc0263252025-02-24 14:23:04 -0800392 if (is_memfd_fd(fd)) {
Joel Fernandes51944042018-12-18 13:32:31 -0800393 return 0;
394 }
395
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800396 // TODO: should LP64 reject too-large offset/len?
Elliott Hughesc0263252025-02-24 14:23:04 -0800397 ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(length) };
398 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, op, &pin)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399}
400
Elliott Hughesc0263252025-02-24 14:23:04 -0800401int ashmem_pin_region(int fd, size_t offset, size_t length) {
402 return do_pin(ASHMEM_PIN, fd, offset, length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100404
Elliott Hughesc0263252025-02-24 14:23:04 -0800405int ashmem_unpin_region(int fd, size_t offset, size_t length) {
406 return do_pin(ASHMEM_UNPIN, fd, offset, length);
407}
408
409int ashmem_get_size_region(int fd) {
410 if (is_memfd_fd(fd)) {
Joel Fernandes51944042018-12-18 13:32:31 -0800411 struct stat sb;
Joel Fernandes51944042018-12-18 13:32:31 -0800412 if (fstat(fd, &sb) == -1) {
Elliott Hughesda4ac7c2025-01-08 15:17:26 -0800413 ALOGE("ashmem_get_size_region(%d): fstat failed: %m", fd);
Joel Fernandes51944042018-12-18 13:32:31 -0800414 return -1;
415 }
Joel Fernandes51944042018-12-18 13:32:31 -0800416 return sb.st_size;
417 }
418
Joel Fernandes56cd6512018-07-17 13:00:17 -0700419 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100420}