blob: 8fe1d2bb3d50b79d2d9961f53eb0dcde08b4ddb5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -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
17#define LOG_TAG "MemoryHeapBase"
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <fcntl.h>
Atneya Nair7ade4f42022-02-07 18:16:48 -050021#include <linux/memfd.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <stdint.h>
23#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <sys/ioctl.h>
Atneya Nair7ade4f42022-02-07 18:16:48 -050025#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026#include <sys/stat.h>
Atneya Nair7ade4f42022-02-07 18:16:48 -050027#include <sys/syscall.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <sys/types.h>
29#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mark Salyzyn7823e122016-09-29 08:08:05 -070031#include <binder/MemoryHeapBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <cutils/ashmem.h>
33#include <cutils/atomic.h>
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070034#include <log/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036namespace android {
37
38// ---------------------------------------------------------------------------
39
Atneya Nair7ade4f42022-02-07 18:16:48 -050040#ifdef __BIONIC__
41static int memfd_create_region(const char* name, size_t size) {
42 int fd = memfd_create(name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
43 if (fd == -1) {
44 ALOGE("%s: memfd_create(%s, %zd) failed: %s\n", __func__, name, size, strerror(errno));
45 return -1;
46 }
47
48 if (ftruncate(fd, size) == -1) {
49 ALOGE("%s, ftruncate(%s, %zd) failed for memfd creation: %s\n", __func__, name, size,
50 strerror(errno));
51 close(fd);
52 return -1;
53 }
54 return fd;
55}
56#endif
57
Anu Sundararajan5728a922011-06-22 15:58:59 -050058MemoryHeapBase::MemoryHeapBase()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 : mFD(-1), mSize(0), mBase(MAP_FAILED),
Yi Kong91635562018-06-07 14:38:36 -070060 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061{
62}
63
64MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
65 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -070066 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067{
68 const size_t pagesize = getpagesize();
Atneya Nair7ade4f42022-02-07 18:16:48 -050069 size = ((size + pagesize - 1) & ~(pagesize - 1));
70 int fd = -1;
71 if (mFlags & FORCE_MEMFD) {
72#ifdef __BIONIC__
73 ALOGV("MemoryHeapBase: Attempting to force MemFD");
74 fd = memfd_create_region(name ? name : "MemoryHeapBase", size);
75 if (fd < 0 || (mapfd(fd, true, size) != NO_ERROR)) return;
76 const int SEAL_FLAGS = ((mFlags & READ_ONLY) ? F_SEAL_FUTURE_WRITE : 0) |
Atneya Nair3fd092e2022-05-24 16:50:12 -040077 ((mFlags & MEMFD_ALLOW_SEALING_FLAG) ? 0 : F_SEAL_SEAL);
Atneya Nair7ade4f42022-02-07 18:16:48 -050078 if (SEAL_FLAGS && (fcntl(fd, F_ADD_SEALS, SEAL_FLAGS) == -1)) {
79 ALOGE("MemoryHeapBase: MemFD %s sealing with flags %x failed with error %s", name,
80 SEAL_FLAGS, strerror(errno));
81 munmap(mBase, mSize);
82 mBase = nullptr;
83 mSize = 0;
84 close(fd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 }
Atneya Nair7ade4f42022-02-07 18:16:48 -050086 return;
87#else
Atneya Nair3fd092e2022-05-24 16:50:12 -040088 mFlags &= ~(FORCE_MEMFD | MEMFD_ALLOW_SEALING_FLAG);
Atneya Nair7ade4f42022-02-07 18:16:48 -050089#endif
90 }
Atneya Nair7ade4f42022-02-07 18:16:48 -050091 fd = ashmem_create_region(name ? name : "MemoryHeapBase", size);
92 ALOGE_IF(fd < 0, "MemoryHeapBase: error creating ashmem region: %s", strerror(errno));
93 if (fd < 0 || (mapfd(fd, true, size) != NO_ERROR)) return;
94 if (mFlags & READ_ONLY) {
95 ashmem_set_prot_region(fd, PROT_READ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 }
97}
98
99MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
100 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -0700101 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
Atneya Nair3fd092e2022-05-24 16:50:12 -0400103 if (flags & (FORCE_MEMFD | MEMFD_ALLOW_SEALING_FLAG)) {
Atneya Nair7ade4f42022-02-07 18:16:48 -0500104 LOG_ALWAYS_FATAL("FORCE_MEMFD, MEMFD_ALLOW_SEALING only valid with creating constructor");
105 }
Iliyan Malchev0db1a892009-10-29 22:55:00 -0700106 int open_flags = O_RDWR;
107 if (flags & NO_CACHING)
108 open_flags |= O_SYNC;
109
110 int fd = open(device, open_flags);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000111 ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 if (fd >= 0) {
113 const size_t pagesize = getpagesize();
114 size = ((size + pagesize-1) & ~(pagesize-1));
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700115 if (mapfd(fd, false, size) == NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 mDevice = device;
117 }
118 }
119}
120
Andy Hung5b028522018-10-22 15:29:49 -0700121MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -0700123 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124{
Atneya Nair3fd092e2022-05-24 16:50:12 -0400125 if (flags & (FORCE_MEMFD | MEMFD_ALLOW_SEALING_FLAG)) {
Atneya Nair7ade4f42022-02-07 18:16:48 -0500126 LOG_ALWAYS_FATAL("FORCE_MEMFD, MEMFD_ALLOW_SEALING only valid with creating constructor");
127 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 const size_t pagesize = getpagesize();
129 size = ((size + pagesize-1) & ~(pagesize-1));
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700130 mapfd(fcntl(fd, F_DUPFD_CLOEXEC, 0), false, size, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
Andy Hung5b028522018-10-22 15:29:49 -0700133status_t MemoryHeapBase::init(int fd, void *base, size_t size, int flags, const char* device)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
135 if (mFD != -1) {
136 return INVALID_OPERATION;
137 }
138 mFD = fd;
139 mBase = base;
140 mSize = size;
141 mFlags = flags;
142 mDevice = device;
143 return NO_ERROR;
144}
145
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700146status_t MemoryHeapBase::mapfd(int fd, bool writeableByCaller, size_t size, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
148 if (size == 0) {
149 // try to figure out the size automatically
Elliott Hughesa5a13a32013-11-21 12:22:39 -0800150 struct stat sb;
Andy Hung5b028522018-10-22 15:29:49 -0700151 if (fstat(fd, &sb) == 0) {
152 size = (size_t)sb.st_size;
153 // sb.st_size is off_t which on ILP32 may be 64 bits while size_t is 32 bits.
154 if ((off_t)size != sb.st_size) {
155 ALOGE("%s: size of file %lld cannot fit in memory",
156 __func__, (long long)sb.st_size);
157 return INVALID_OPERATION;
158 }
159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 // if it didn't work, let mmap() fail.
161 }
162
163 if ((mFlags & DONT_MAP_LOCALLY) == 0) {
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700164 int prot = PROT_READ;
165 if (writeableByCaller || (mFlags & READ_ONLY) == 0) {
166 prot |= PROT_WRITE;
167 }
Yi Kong91635562018-06-07 14:38:36 -0700168 void* base = (uint8_t*)mmap(nullptr, size,
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700169 prot, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 if (base == MAP_FAILED) {
Andy Hung5b028522018-10-22 15:29:49 -0700171 ALOGE("mmap(fd=%d, size=%zu) failed (%s)",
172 fd, size, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 close(fd);
174 return -errno;
175 }
Andy Hung5b028522018-10-22 15:29:49 -0700176 //ALOGD("mmap(fd=%d, base=%p, size=%zu)", fd, base, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 mBase = base;
178 mNeedUnmap = true;
179 } else {
Yi Kong91635562018-06-07 14:38:36 -0700180 mBase = nullptr; // not MAP_FAILED
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 mNeedUnmap = false;
182 }
183 mFD = fd;
184 mSize = size;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500185 mOffset = offset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 return NO_ERROR;
187}
188
189MemoryHeapBase::~MemoryHeapBase()
190{
191 dispose();
192}
193
194void MemoryHeapBase::dispose()
195{
196 int fd = android_atomic_or(-1, &mFD);
197 if (fd >= 0) {
198 if (mNeedUnmap) {
Andy Hung5b028522018-10-22 15:29:49 -0700199 //ALOGD("munmap(fd=%d, base=%p, size=%zu)", fd, mBase, mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 munmap(mBase, mSize);
201 }
Yi Kong91635562018-06-07 14:38:36 -0700202 mBase = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 mSize = 0;
204 close(fd);
205 }
206}
207
208int MemoryHeapBase::getHeapID() const {
209 return mFD;
210}
211
212void* MemoryHeapBase::getBase() const {
213 return mBase;
214}
215
216size_t MemoryHeapBase::getSize() const {
217 return mSize;
218}
219
220uint32_t MemoryHeapBase::getFlags() const {
221 return mFlags;
222}
223
224const char* MemoryHeapBase::getDevice() const {
225 return mDevice;
226}
227
Andy Hung5b028522018-10-22 15:29:49 -0700228off_t MemoryHeapBase::getOffset() const {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500229 return mOffset;
230}
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232// ---------------------------------------------------------------------------
Steven Moreland61ff8492019-09-26 16:05:45 -0700233} // namespace android