The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/ashmem.h> |
| 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | /* |
| 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 Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 24 | #define LOG_TAG "ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Tri Vo | 2891ba0 | 2019-01-28 17:56:43 -0800 | [diff] [blame^] | 26 | #ifndef __ANDROID_VNDK__ |
| 27 | #include <dlfcn.h> |
| 28 | #endif |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 29 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <fcntl.h> |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 31 | #include <linux/ashmem.h> |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 32 | #include <pthread.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/stat.h> |
Elliott Hughes | d77b537 | 2017-05-17 11:36:51 -0700 | [diff] [blame] | 36 | #include <sys/sysmacros.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 41 | #define ASHMEM_DEVICE "/dev/ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 43 | /* ashmem identity */ |
| 44 | static dev_t __ashmem_rdev; |
| 45 | /* |
| 46 | * If we trigger a signal handler in the middle of locked activity and the |
| 47 | * signal handler calls ashmem, we could get into a deadlock state. |
| 48 | */ |
| 49 | static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER; |
| 50 | |
Tri Vo | 2891ba0 | 2019-01-28 17:56:43 -0800 | [diff] [blame^] | 51 | /* |
| 52 | * We use ashmemd to enforce that apps don't open /dev/ashmem directly. Vendor |
| 53 | * code can't access system aidl services per Treble requirements. So we limit |
| 54 | * ashmemd access to the system variant of libcutils. |
| 55 | */ |
| 56 | #ifndef __ANDROID_VNDK__ |
| 57 | using openFdType = int (*)(); |
| 58 | |
| 59 | openFdType initOpenAshmemFd() { |
| 60 | openFdType openFd = nullptr; |
| 61 | void* handle = dlopen("libashmemd_client.so", RTLD_NOW); |
| 62 | if (!handle) { |
| 63 | ALOGE("Failed to dlopen() libashmemd_client.so: %s", dlerror()); |
| 64 | return openFd; |
| 65 | } |
| 66 | |
| 67 | openFd = reinterpret_cast<openFdType>(dlsym(handle, "openAshmemdFd")); |
| 68 | if (!openFd) { |
| 69 | ALOGE("Failed to dlsym() openAshmemdFd() function: %s", dlerror()); |
| 70 | } |
| 71 | return openFd; |
| 72 | } |
| 73 | #endif |
| 74 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 75 | /* logistics of getting file descriptor for ashmem */ |
| 76 | static int __ashmem_open_locked() |
| 77 | { |
| 78 | int ret; |
| 79 | struct stat st; |
| 80 | |
Tri Vo | 2891ba0 | 2019-01-28 17:56:43 -0800 | [diff] [blame^] | 81 | int fd = -1; |
| 82 | #ifndef __ANDROID_VNDK__ |
| 83 | static auto openFd = initOpenAshmemFd(); |
| 84 | if (openFd) { |
| 85 | fd = openFd(); |
| 86 | } |
| 87 | #endif |
| 88 | if (fd < 0) { |
| 89 | fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC)); |
| 90 | } |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 91 | if (fd < 0) { |
| 92 | return fd; |
| 93 | } |
| 94 | |
| 95 | ret = TEMP_FAILURE_RETRY(fstat(fd, &st)); |
| 96 | if (ret < 0) { |
| 97 | int save_errno = errno; |
| 98 | close(fd); |
| 99 | errno = save_errno; |
| 100 | return ret; |
| 101 | } |
| 102 | if (!S_ISCHR(st.st_mode) || !st.st_rdev) { |
| 103 | close(fd); |
| 104 | errno = ENOTTY; |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | __ashmem_rdev = st.st_rdev; |
| 109 | return fd; |
| 110 | } |
| 111 | |
| 112 | static int __ashmem_open() |
| 113 | { |
| 114 | int fd; |
| 115 | |
| 116 | pthread_mutex_lock(&__ashmem_lock); |
| 117 | fd = __ashmem_open_locked(); |
| 118 | pthread_mutex_unlock(&__ashmem_lock); |
| 119 | |
| 120 | return fd; |
| 121 | } |
| 122 | |
| 123 | /* Make sure file descriptor references ashmem, negative number means false */ |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 124 | static int __ashmem_is_ashmem(int fd, int fatal) |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 125 | { |
| 126 | dev_t rdev; |
| 127 | struct stat st; |
| 128 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 129 | if (fstat(fd, &st) < 0) { |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 133 | rdev = 0; /* Too much complexity to sniff __ashmem_rdev */ |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 134 | if (S_ISCHR(st.st_mode) && st.st_rdev) { |
| 135 | pthread_mutex_lock(&__ashmem_lock); |
| 136 | rdev = __ashmem_rdev; |
| 137 | if (rdev) { |
| 138 | pthread_mutex_unlock(&__ashmem_lock); |
| 139 | } else { |
| 140 | int fd = __ashmem_open_locked(); |
| 141 | if (fd < 0) { |
| 142 | pthread_mutex_unlock(&__ashmem_lock); |
| 143 | return -1; |
| 144 | } |
| 145 | rdev = __ashmem_rdev; |
| 146 | pthread_mutex_unlock(&__ashmem_lock); |
| 147 | |
| 148 | close(fd); |
| 149 | } |
| 150 | |
| 151 | if (st.st_rdev == rdev) { |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 156 | if (fatal) { |
| 157 | if (rdev) { |
| 158 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d", |
| 159 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 160 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP, |
| 161 | major(rdev), minor(rdev)); |
| 162 | } else { |
| 163 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o", |
| 164 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 165 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP); |
| 166 | } |
| 167 | /* NOTREACHED */ |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 170 | errno = ENOTTY; |
| 171 | return -1; |
| 172 | } |
| 173 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 174 | static int __ashmem_check_failure(int fd, int result) |
| 175 | { |
| 176 | if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, 1); |
| 177 | return result; |
| 178 | } |
| 179 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 180 | int ashmem_valid(int fd) |
| 181 | { |
| 182 | return __ashmem_is_ashmem(fd, 0) >= 0; |
| 183 | } |
| 184 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | /* |
| 186 | * ashmem_create_region - creates a new ashmem region and returns the file |
| 187 | * descriptor, or <0 on error |
| 188 | * |
| 189 | * `name' is an optional label to give the region (visible in /proc/pid/maps) |
| 190 | * `size' is the size of the region, in page-aligned bytes |
| 191 | */ |
| 192 | int ashmem_create_region(const char *name, size_t size) |
| 193 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 194 | int ret, save_errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 196 | int fd = __ashmem_open(); |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 197 | if (fd < 0) { |
| 198 | return fd; |
| 199 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 201 | if (name) { |
| 202 | char buf[ASHMEM_NAME_LEN] = {0}; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 204 | strlcpy(buf, name, sizeof(buf)); |
| 205 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf)); |
| 206 | if (ret < 0) { |
| 207 | goto error; |
| 208 | } |
| 209 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 211 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size)); |
| 212 | if (ret < 0) { |
| 213 | goto error; |
| 214 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 216 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | |
| 218 | error: |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 219 | save_errno = errno; |
| 220 | close(fd); |
| 221 | errno = save_errno; |
| 222 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int ashmem_set_prot_region(int fd, int prot) |
| 226 | { |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 227 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int ashmem_pin_region(int fd, size_t offset, size_t len) |
| 231 | { |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 232 | // TODO: should LP64 reject too-large offset/len? |
| 233 | ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) }; |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 234 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 235 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int ashmem_unpin_region(int fd, size_t offset, size_t len) |
| 239 | { |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 240 | // TODO: should LP64 reject too-large offset/len? |
| 241 | ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) }; |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 242 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 243 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 245 | |
| 246 | int ashmem_get_size_region(int fd) |
| 247 | { |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 248 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL))); |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 249 | } |