Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #include <binder/MemoryHeapBase.h> |
| 18 | #include <cutils/ashmem.h> |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | using namespace android; |
| 23 | #ifdef __BIONIC__ |
| 24 | TEST(MemoryHeapBase, ForceMemfdRespected) { |
| 25 | auto mHeap = sp<MemoryHeapBase>::make(10, MemoryHeapBase::FORCE_MEMFD, "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 26 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 27 | int fd = mHeap->getHeapID(); |
| 28 | EXPECT_NE(fd, -1); |
| 29 | EXPECT_FALSE(ashmem_valid(fd)); |
| 30 | EXPECT_NE(fcntl(fd, F_GET_SEALS), -1); |
| 31 | } |
| 32 | |
| 33 | TEST(MemoryHeapBase, MemfdSealed) { |
| 34 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 35 | MemoryHeapBase::FORCE_MEMFD, |
| 36 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 37 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 38 | int fd = mHeap->getHeapID(); |
| 39 | EXPECT_NE(fd, -1); |
Keith Mok | 3d9f1e3 | 2023-08-04 22:17:08 +0000 | [diff] [blame] | 40 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL); |
| 41 | EXPECT_EQ(ftruncate(fd, 4096), -1); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(MemoryHeapBase, MemfdUnsealed) { |
| 45 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 46 | MemoryHeapBase::FORCE_MEMFD | |
| 47 | MemoryHeapBase::MEMFD_ALLOW_SEALING, |
| 48 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 49 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 50 | int fd = mHeap->getHeapID(); |
| 51 | EXPECT_NE(fd, -1); |
Keith Mok | 3d9f1e3 | 2023-08-04 22:17:08 +0000 | [diff] [blame] | 52 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_GROW | F_SEAL_SHRINK); |
| 53 | EXPECT_EQ(ftruncate(fd, 4096), -1); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST(MemoryHeapBase, MemfdSealedProtected) { |
| 57 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 58 | MemoryHeapBase::FORCE_MEMFD | |
| 59 | MemoryHeapBase::READ_ONLY, |
| 60 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 61 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 62 | int fd = mHeap->getHeapID(); |
| 63 | EXPECT_NE(fd, -1); |
Keith Mok | 3d9f1e3 | 2023-08-04 22:17:08 +0000 | [diff] [blame] | 64 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), |
| 65 | F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL | F_SEAL_FUTURE_WRITE); |
| 66 | EXPECT_EQ(ftruncate(fd, 4096), -1); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST(MemoryHeapBase, MemfdUnsealedProtected) { |
| 70 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 71 | MemoryHeapBase::FORCE_MEMFD | |
| 72 | MemoryHeapBase::READ_ONLY | |
| 73 | MemoryHeapBase::MEMFD_ALLOW_SEALING, |
| 74 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 75 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 76 | int fd = mHeap->getHeapID(); |
| 77 | EXPECT_NE(fd, -1); |
Keith Mok | 3d9f1e3 | 2023-08-04 22:17:08 +0000 | [diff] [blame] | 78 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_FUTURE_WRITE); |
| 79 | EXPECT_EQ(ftruncate(fd, 4096), -1); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | #else |
| 83 | TEST(MemoryHeapBase, HostMemfdExpected) { |
| 84 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 85 | MemoryHeapBase::READ_ONLY, |
| 86 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 87 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 88 | int fd = mHeap->getHeapID(); |
| 89 | void* ptr = mHeap->getBase(); |
| 90 | EXPECT_NE(ptr, MAP_FAILED); |
| 91 | EXPECT_TRUE(ashmem_valid(fd)); |
| 92 | EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY); |
| 93 | } |
| 94 | |
| 95 | TEST(MemoryHeapBase,HostMemfdException) { |
| 96 | auto mHeap = sp<MemoryHeapBase>::make(8192, |
| 97 | MemoryHeapBase::FORCE_MEMFD | |
| 98 | MemoryHeapBase::READ_ONLY | |
| 99 | MemoryHeapBase::MEMFD_ALLOW_SEALING, |
| 100 | "Test mapping"); |
Atneya Nair | 3fd092e | 2022-05-24 16:50:12 -0400 | [diff] [blame] | 101 | ASSERT_NE(mHeap.get(), nullptr); |
Atneya Nair | 7ade4f4 | 2022-02-07 18:16:48 -0500 | [diff] [blame] | 102 | int fd = mHeap->getHeapID(); |
| 103 | void* ptr = mHeap->getBase(); |
| 104 | EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY); |
| 105 | EXPECT_TRUE(ashmem_valid(fd)); |
| 106 | EXPECT_NE(ptr, MAP_FAILED); |
| 107 | } |
| 108 | |
| 109 | #endif |