blob: 8132d46940c16fe9067e100001e0bb400a3836ed [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) |
77 ((mFlags & MEMFD_ALLOW_SEALING) ? 0 : F_SEAL_SEAL);
78 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
88 mFlags &= ~(FORCE_MEMFD | MEMFD_ALLOW_SEALING);
89#endif
90 }
91 if (mFlags & MEMFD_ALLOW_SEALING) {
92 LOG_ALWAYS_FATAL("Invalid Flags. MEMFD_ALLOW_SEALING only valid with FORCE_MEMFD.");
93 }
94 fd = ashmem_create_region(name ? name : "MemoryHeapBase", size);
95 ALOGE_IF(fd < 0, "MemoryHeapBase: error creating ashmem region: %s", strerror(errno));
96 if (fd < 0 || (mapfd(fd, true, size) != NO_ERROR)) return;
97 if (mFlags & READ_ONLY) {
98 ashmem_set_prot_region(fd, PROT_READ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 }
100}
101
102MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
103 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -0700104 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
Atneya Nair7ade4f42022-02-07 18:16:48 -0500106 if (flags & (FORCE_MEMFD | MEMFD_ALLOW_SEALING)) {
107 LOG_ALWAYS_FATAL("FORCE_MEMFD, MEMFD_ALLOW_SEALING only valid with creating constructor");
108 }
Iliyan Malchev0db1a892009-10-29 22:55:00 -0700109 int open_flags = O_RDWR;
110 if (flags & NO_CACHING)
111 open_flags |= O_SYNC;
112
113 int fd = open(device, open_flags);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000114 ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 if (fd >= 0) {
116 const size_t pagesize = getpagesize();
117 size = ((size + pagesize-1) & ~(pagesize-1));
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700118 if (mapfd(fd, false, size) == NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 mDevice = device;
120 }
121 }
122}
123
Andy Hung5b028522018-10-22 15:29:49 -0700124MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -0700126 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127{
Atneya Nair7ade4f42022-02-07 18:16:48 -0500128 if (flags & (FORCE_MEMFD | MEMFD_ALLOW_SEALING)) {
129 LOG_ALWAYS_FATAL("FORCE_MEMFD, MEMFD_ALLOW_SEALING only valid with creating constructor");
130 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 const size_t pagesize = getpagesize();
132 size = ((size + pagesize-1) & ~(pagesize-1));
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700133 mapfd(fcntl(fd, F_DUPFD_CLOEXEC, 0), false, size, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Andy Hung5b028522018-10-22 15:29:49 -0700136status_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 -0800137{
138 if (mFD != -1) {
139 return INVALID_OPERATION;
140 }
141 mFD = fd;
142 mBase = base;
143 mSize = size;
144 mFlags = flags;
145 mDevice = device;
146 return NO_ERROR;
147}
148
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700149status_t MemoryHeapBase::mapfd(int fd, bool writeableByCaller, size_t size, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
151 if (size == 0) {
152 // try to figure out the size automatically
Elliott Hughesa5a13a32013-11-21 12:22:39 -0800153 struct stat sb;
Andy Hung5b028522018-10-22 15:29:49 -0700154 if (fstat(fd, &sb) == 0) {
155 size = (size_t)sb.st_size;
156 // sb.st_size is off_t which on ILP32 may be 64 bits while size_t is 32 bits.
157 if ((off_t)size != sb.st_size) {
158 ALOGE("%s: size of file %lld cannot fit in memory",
159 __func__, (long long)sb.st_size);
160 return INVALID_OPERATION;
161 }
162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 // if it didn't work, let mmap() fail.
164 }
165
166 if ((mFlags & DONT_MAP_LOCALLY) == 0) {
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700167 int prot = PROT_READ;
168 if (writeableByCaller || (mFlags & READ_ONLY) == 0) {
169 prot |= PROT_WRITE;
170 }
Yi Kong91635562018-06-07 14:38:36 -0700171 void* base = (uint8_t*)mmap(nullptr, size,
Ytai Ben-Tsvidacdbd12020-10-22 15:40:45 -0700172 prot, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 if (base == MAP_FAILED) {
Andy Hung5b028522018-10-22 15:29:49 -0700174 ALOGE("mmap(fd=%d, size=%zu) failed (%s)",
175 fd, size, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 close(fd);
177 return -errno;
178 }
Andy Hung5b028522018-10-22 15:29:49 -0700179 //ALOGD("mmap(fd=%d, base=%p, size=%zu)", fd, base, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 mBase = base;
181 mNeedUnmap = true;
182 } else {
Yi Kong91635562018-06-07 14:38:36 -0700183 mBase = nullptr; // not MAP_FAILED
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 mNeedUnmap = false;
185 }
186 mFD = fd;
187 mSize = size;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500188 mOffset = offset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 return NO_ERROR;
190}
191
192MemoryHeapBase::~MemoryHeapBase()
193{
194 dispose();
195}
196
197void MemoryHeapBase::dispose()
198{
199 int fd = android_atomic_or(-1, &mFD);
200 if (fd >= 0) {
201 if (mNeedUnmap) {
Andy Hung5b028522018-10-22 15:29:49 -0700202 //ALOGD("munmap(fd=%d, base=%p, size=%zu)", fd, mBase, mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 munmap(mBase, mSize);
204 }
Yi Kong91635562018-06-07 14:38:36 -0700205 mBase = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 mSize = 0;
207 close(fd);
208 }
209}
210
211int MemoryHeapBase::getHeapID() const {
212 return mFD;
213}
214
215void* MemoryHeapBase::getBase() const {
216 return mBase;
217}
218
219size_t MemoryHeapBase::getSize() const {
220 return mSize;
221}
222
223uint32_t MemoryHeapBase::getFlags() const {
224 return mFlags;
225}
226
227const char* MemoryHeapBase::getDevice() const {
228 return mDevice;
229}
230
Andy Hung5b028522018-10-22 15:29:49 -0700231off_t MemoryHeapBase::getOffset() const {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500232 return mOffset;
233}
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235// ---------------------------------------------------------------------------
Steven Moreland61ff8492019-09-26 16:05:45 -0700236} // namespace android