blob: 96f20db9f6247f37071154122b5ad59739def0e7 [file] [log] [blame]
Connor O'Briena963ae82016-09-12 15:52:04 -07001/*
2 * Copyright (C) 2016 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
Mark Salyzyn276758d2018-01-18 07:59:38 -080017#include <errno.h>
18#include <linux/fs.h>
19#include <stdint.h>
20#include <string.h>
21#include <sys/mman.h>
22#include <sys/types.h>
23#include <unistd.h>
24
Vilas Bhatf791b992024-02-13 16:43:09 +000025#include <vector>
26
Mark Salyzyn276758d2018-01-18 07:59:38 -080027#include <android-base/macros.h>
Greg Hackmannbe11d572017-04-05 10:03:10 -070028#include <android-base/unique_fd.h>
Connor O'Briena963ae82016-09-12 15:52:04 -070029#include <cutils/ashmem.h>
30#include <gtest/gtest.h>
Connor O'Briena963ae82016-09-12 15:52:04 -070031
32using android::base::unique_fd;
33
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080034static void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
Connor O'Briena963ae82016-09-12 15:52:04 -070035 fd = unique_fd(ashmem_create_region(nullptr, size));
36 ASSERT_TRUE(fd >= 0);
37 ASSERT_TRUE(ashmem_valid(fd));
38 ASSERT_EQ(size, static_cast<size_t>(ashmem_get_size_region(fd)));
39 ASSERT_EQ(0, ashmem_set_prot_region(fd, prot));
Elliott Hughes790ef052020-09-03 10:53:16 -070040
41 // We've been inconsistent historically about whether or not these file
42 // descriptors were CLOEXEC. Make sure we're consistent going forward.
43 // https://issuetracker.google.com/165667331
44 ASSERT_EQ(FD_CLOEXEC, (fcntl(fd, F_GETFD) & FD_CLOEXEC));
Connor O'Briena963ae82016-09-12 15:52:04 -070045}
46
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080047static void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {
Mark Salyzyn276758d2018-01-18 07:59:38 -080048 ASSERT_TRUE(fd >= 0);
49 ASSERT_TRUE(ashmem_valid(fd));
Greg Hackmannbe11d572017-04-05 10:03:10 -070050 *region = mmap(nullptr, size, prot, MAP_SHARED, fd, off);
Connor O'Briena963ae82016-09-12 15:52:04 -070051 ASSERT_NE(MAP_FAILED, *region);
52}
53
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080054static void TestProtDenied(const unique_fd &fd, size_t size, int prot) {
Mark Salyzyn276758d2018-01-18 07:59:38 -080055 ASSERT_TRUE(fd >= 0);
56 ASSERT_TRUE(ashmem_valid(fd));
Connor O'Briena963ae82016-09-12 15:52:04 -070057 EXPECT_EQ(MAP_FAILED, mmap(nullptr, size, prot, MAP_SHARED, fd, 0));
58}
59
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080060static void TestProtIs(const unique_fd& fd, int prot) {
Mark Salyzyn276758d2018-01-18 07:59:38 -080061 ASSERT_TRUE(fd >= 0);
62 ASSERT_TRUE(ashmem_valid(fd));
Greg Hackmann4a9531d2017-04-11 15:08:36 -070063 EXPECT_EQ(prot, ioctl(fd, ASHMEM_GET_PROT_MASK));
64}
65
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080066static void FillData(std::vector<uint8_t>& data) {
Vilas Bhatf791b992024-02-13 16:43:09 +000067 for (size_t i = 0; i < data.size(); i++) {
Connor O'Briena963ae82016-09-12 15:52:04 -070068 data[i] = i & 0xFF;
69 }
70}
71
Isaac J. Manjarrescc226cb2025-01-17 11:28:09 -080072static void waitForChildProcessExit(pid_t pid) {
73 int exitStatus;
74 pid_t childPid = waitpid(pid, &exitStatus, 0);
75
76 ASSERT_GT(childPid, 0);
77 ASSERT_TRUE(WIFEXITED(exitStatus));
78 ASSERT_EQ(0, WEXITSTATUS(exitStatus));
79}
80
Isaac J. Manjarres2c044242024-12-27 15:19:41 -080081static void ForkTest(const unique_fd &fd, size_t size) {
82 void* region1 = nullptr;
Vilas Bhatf791b992024-02-13 16:43:09 +000083 std::vector<uint8_t> data(size);
84 FillData(data);
Connor O'Briena963ae82016-09-12 15:52:04 -070085
Connor O'Briena963ae82016-09-12 15:52:04 -070086 ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, &region1));
87
Vilas Bhatf791b992024-02-13 16:43:09 +000088 memcpy(region1, data.data(), size);
89 ASSERT_EQ(0, memcmp(region1, data.data(), size));
Connor O'Briena963ae82016-09-12 15:52:04 -070090 EXPECT_EQ(0, munmap(region1, size));
91
Isaac J. Manjarrescc226cb2025-01-17 11:28:09 -080092
93 pid_t pid = fork();
94 if (!pid) {
95 if (!ashmem_valid(fd)) {
96 _exit(3);
97 }
98
99 void *region2 = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
100 if (region2 == MAP_FAILED) {
101 _exit(1);
102 } else if (memcmp(region2, data.data(), size) != 0){
103 _exit(2);
104 }
105
106 // Clear the ashmem buffer here to ensure that updates to the contents
107 // of the buffer are visible across processes with a reference to the
108 // buffer.
109 memset(region2, 0, size);
110 munmap(region2, size);
111 _exit(0);
112 } else {
113 ASSERT_GT(pid, 0);
114 ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
115 }
Connor O'Briena963ae82016-09-12 15:52:04 -0700116
Vilas Bhatf791b992024-02-13 16:43:09 +0000117 memset(data.data(), 0, size);
Connor O'Briena963ae82016-09-12 15:52:04 -0700118 void *region2;
119 ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, &region2));
Vilas Bhatf791b992024-02-13 16:43:09 +0000120 ASSERT_EQ(0, memcmp(region2, data.data(), size));
Connor O'Briena963ae82016-09-12 15:52:04 -0700121 EXPECT_EQ(0, munmap(region2, size));
122}
123
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800124static void FileOperationsTest(const unique_fd &fd, size_t size) {
Chih-Hung Hsieh0ebbc622022-12-20 11:54:11 -0800125 void* region = nullptr;
Greg Hackmannbe11d572017-04-05 10:03:10 -0700126
Vilas Bhatf791b992024-02-13 16:43:09 +0000127 const size_t pageSize = getpagesize();
Vilas Bhatf791b992024-02-13 16:43:09 +0000128 const size_t dataSize = pageSize * 2;
129 const size_t holeSize = pageSize;
Greg Hackmannbe11d572017-04-05 10:03:10 -0700130 ASSERT_NO_FATAL_FAILURE(TestMmap(fd, dataSize, PROT_READ | PROT_WRITE, &region, holeSize));
131
Vilas Bhatf791b992024-02-13 16:43:09 +0000132 std::vector<uint8_t> data(dataSize);
133 FillData(data);
134 memcpy(region, data.data(), dataSize);
Greg Hackmannbe11d572017-04-05 10:03:10 -0700135
Vilas Bhatf791b992024-02-13 16:43:09 +0000136 const off_t dataStart = holeSize;
137 const off_t dataEnd = dataStart + dataSize;
Greg Hackmannbe11d572017-04-05 10:03:10 -0700138
139 // The sequence of seeks below looks something like this:
140 //
141 // [ ][data][data][ ]
142 // --^ lseek(99, SEEK_SET)
143 // ------^ lseek(dataStart, SEEK_CUR)
144 // ------^ lseek(0, SEEK_DATA)
145 // ------------^ lseek(dataStart, SEEK_HOLE)
146 // ^-- lseek(-99, SEEK_END)
147 // ^------ lseek(-dataStart, SEEK_CUR)
148 const struct {
149 // lseek() parameters
150 off_t offset;
151 int whence;
152 // Expected lseek() return value
153 off_t ret;
154 } seeks[] = {
Vilas Bhatf791b992024-02-13 16:43:09 +0000155 {99, SEEK_SET, 99},
156 {dataStart, SEEK_CUR, dataStart + 99},
157 {0, SEEK_DATA, dataStart},
158 {dataStart, SEEK_HOLE, dataEnd},
159 {-99, SEEK_END, static_cast<off_t>(size) - 99},
160 {-dataStart, SEEK_CUR, dataEnd - 99},
Greg Hackmannbe11d572017-04-05 10:03:10 -0700161 };
162 for (const auto& cfg : seeks) {
163 errno = 0;
Mark Salyzyn276758d2018-01-18 07:59:38 -0800164 ASSERT_TRUE(ashmem_valid(fd));
Greg Hackmannbe11d572017-04-05 10:03:10 -0700165 auto off = lseek(fd, cfg.offset, cfg.whence);
166 ASSERT_EQ(cfg.ret, off) << "lseek(" << cfg.offset << ", " << cfg.whence << ") failed"
167 << (errno ? ": " : "") << (errno ? strerror(errno) : "");
168
169 if (off >= dataStart && off < dataEnd) {
170 off_t dataOff = off - dataStart;
171 ssize_t readSize = dataSize - dataOff;
172 uint8_t buf[readSize];
173
174 ASSERT_EQ(readSize, TEMP_FAILURE_RETRY(read(fd, buf, readSize)));
Vilas Bhatf791b992024-02-13 16:43:09 +0000175 EXPECT_EQ(0, memcmp(buf, &data[dataOff], readSize));
Greg Hackmannbe11d572017-04-05 10:03:10 -0700176 }
177 }
178
179 EXPECT_EQ(0, munmap(region, dataSize));
180}
181
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800182static void ProtTestROBuffer(const unique_fd &fd, size_t size) {
Connor O'Briena963ae82016-09-12 15:52:04 -0700183 void *region;
184
Connor O'Briena963ae82016-09-12 15:52:04 -0700185 TestProtDenied(fd, size, PROT_WRITE);
Isaac J. Manjarres05af2762025-01-15 14:28:41 -0800186 TestProtIs(fd, PROT_READ | PROT_EXEC);
Connor O'Briena963ae82016-09-12 15:52:04 -0700187 ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ, &region));
188 EXPECT_EQ(0, munmap(region, size));
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800189}
Connor O'Briena963ae82016-09-12 15:52:04 -0700190
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800191static void ProtTestRWBuffer(const unique_fd &fd, size_t size) {
Isaac J. Manjarres05af2762025-01-15 14:28:41 -0800192 TestProtIs(fd, PROT_READ | PROT_WRITE | PROT_EXEC);
193 ASSERT_EQ(0, ashmem_set_prot_region(fd, PROT_READ | PROT_EXEC));
Greg Hackmann4a9531d2017-04-11 15:08:36 -0700194 errno = 0;
Isaac J. Manjarres05af2762025-01-15 14:28:41 -0800195 ASSERT_EQ(-1, ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE |
196 PROT_EXEC))
Greg Hackmann4a9531d2017-04-11 15:08:36 -0700197 << "kernel shouldn't allow adding protection bits";
198 EXPECT_EQ(EINVAL, errno);
Isaac J. Manjarres05af2762025-01-15 14:28:41 -0800199 TestProtIs(fd, PROT_READ | PROT_EXEC);
Greg Hackmann4a9531d2017-04-11 15:08:36 -0700200 TestProtDenied(fd, size, PROT_WRITE);
Connor O'Briena963ae82016-09-12 15:52:04 -0700201}
202
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800203static void ForkProtTest(const unique_fd &fd, size_t size) {
Isaac J. Manjarrescc226cb2025-01-17 11:28:09 -0800204 pid_t pid = fork();
205 if (!pid) {
206 // Change buffer mapping permissions to read-only to ensure that
207 // updates to the buffer's mapping permissions are visible across
208 // processes that reference the buffer.
209 if (!ashmem_valid(fd)) {
210 _exit(3);
211 } else if (ashmem_set_prot_region(fd, PROT_READ) == -1) {
212 _exit(1);
213 }
214 _exit(0);
215 } else {
216 ASSERT_GT(pid, 0);
217 ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
218 }
219
Isaac J. Manjarres95c35722024-12-27 13:36:35 -0800220 ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, PROT_WRITE));
Connor O'Briena963ae82016-09-12 15:52:04 -0700221}
222
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800223static void ForkMultiRegionTest(unique_fd fds[], int nRegions, size_t size) {
Vilas Bhatf791b992024-02-13 16:43:09 +0000224 std::vector<uint8_t> data(size);
225 FillData(data);
Connor O'Briena963ae82016-09-12 15:52:04 -0700226
Connor O'Briena963ae82016-09-12 15:52:04 -0700227 for (int i = 0; i < nRegions; i++) {
Chih-Hung Hsieh0ebbc622022-12-20 11:54:11 -0800228 void* region = nullptr;
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800229 ASSERT_NO_FATAL_FAILURE(TestMmap(fds[i], size, PROT_READ | PROT_WRITE, &region));
Vilas Bhatf791b992024-02-13 16:43:09 +0000230 memcpy(region, data.data(), size);
231 ASSERT_EQ(0, memcmp(region, data.data(), size));
Connor O'Briena963ae82016-09-12 15:52:04 -0700232 EXPECT_EQ(0, munmap(region, size));
233 }
234
Isaac J. Manjarrescc226cb2025-01-17 11:28:09 -0800235 pid_t pid = fork();
236 if (!pid) {
237 // Clear each ashmem buffer in the context of the child process to
238 // ensure that the updates are visible to the parent process later.
Connor O'Briena963ae82016-09-12 15:52:04 -0700239 for (int i = 0; i < nRegions; i++) {
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800240 if (!ashmem_valid(fds[i])) {
Mark Salyzyn276758d2018-01-18 07:59:38 -0800241 _exit(3);
242 }
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800243 void *region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fds[i], 0);
Connor O'Briena963ae82016-09-12 15:52:04 -0700244 if (region == MAP_FAILED) {
245 _exit(1);
246 }
Vilas Bhatf791b992024-02-13 16:43:09 +0000247 if (memcmp(region, data.data(), size) != 0) {
Connor O'Briena963ae82016-09-12 15:52:04 -0700248 munmap(region, size);
249 _exit(2);
250 }
251 memset(region, 0, size);
252 munmap(region, size);
253 }
254 _exit(0);
Isaac J. Manjarrescc226cb2025-01-17 11:28:09 -0800255 } else {
256 ASSERT_GT(pid, 0);
257 ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
258 }
Connor O'Briena963ae82016-09-12 15:52:04 -0700259
Vilas Bhatf791b992024-02-13 16:43:09 +0000260 memset(data.data(), 0, size);
Connor O'Briena963ae82016-09-12 15:52:04 -0700261 for (int i = 0; i < nRegions; i++) {
262 void *region;
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800263 ASSERT_NO_FATAL_FAILURE(TestMmap(fds[i], size, PROT_READ | PROT_WRITE, &region));
Vilas Bhatf791b992024-02-13 16:43:09 +0000264 ASSERT_EQ(0, memcmp(region, data.data(), size));
Connor O'Briena963ae82016-09-12 15:52:04 -0700265 EXPECT_EQ(0, munmap(region, size));
266 }
Isaac J. Manjarres2c044242024-12-27 15:19:41 -0800267
268}
269
270TEST(AshmemTest, ForkTest) {
271 const size_t size = getpagesize();
272 unique_fd fd;
273
274 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
275 ASSERT_NO_FATAL_FAILURE(ForkTest(fd, size));
276}
277
278TEST(AshmemTest, FileOperationsTest) {
279 const size_t pageSize = getpagesize();
280 // Allocate a 4-page buffer, but leave page-sized holes on either side in
281 // the test.
282 const size_t size = pageSize * 4;
283 unique_fd fd;
284
285 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
286 ASSERT_NO_FATAL_FAILURE(FileOperationsTest(fd, size));
287}
288
289TEST(AshmemTest, ProtTest) {
290 unique_fd fd;
291 const size_t size = getpagesize();
292
293 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_EXEC));
294 ASSERT_NO_FATAL_FAILURE(ProtTestROBuffer(fd, size));
295
296 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE | PROT_EXEC));
297 ASSERT_NO_FATAL_FAILURE(ProtTestRWBuffer(fd, size));
298}
299
300TEST(AshmemTest, ForkProtTest) {
301 unique_fd fd;
302 const size_t size = getpagesize();
303
304 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
305 ASSERT_NO_FATAL_FAILURE(ForkProtTest(fd, size));
306}
307
308TEST(AshmemTest, ForkMultiRegionTest) {
309 const size_t size = getpagesize();
310 constexpr int nRegions = 16;
311 unique_fd fds[nRegions];
312
313 for (int i = 0; i < nRegions; i++) {
314 ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fds[i], PROT_READ | PROT_WRITE));
315 }
316
317 ASSERT_NO_FATAL_FAILURE(ForkMultiRegionTest(fds, nRegions, size));
Connor O'Briena963ae82016-09-12 15:52:04 -0700318}