| 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"); | 
|  | 26 | int fd = mHeap->getHeapID(); | 
|  | 27 | EXPECT_NE(fd, -1); | 
|  | 28 | EXPECT_FALSE(ashmem_valid(fd)); | 
|  | 29 | EXPECT_NE(fcntl(fd, F_GET_SEALS), -1); | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | TEST(MemoryHeapBase, MemfdSealed) { | 
|  | 33 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 34 | MemoryHeapBase::FORCE_MEMFD, | 
|  | 35 | "Test mapping"); | 
|  | 36 | int fd = mHeap->getHeapID(); | 
|  | 37 | EXPECT_NE(fd, -1); | 
|  | 38 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_SEAL); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | TEST(MemoryHeapBase, MemfdUnsealed) { | 
|  | 42 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 43 | MemoryHeapBase::FORCE_MEMFD | | 
|  | 44 | MemoryHeapBase::MEMFD_ALLOW_SEALING, | 
|  | 45 | "Test mapping"); | 
|  | 46 | int fd = mHeap->getHeapID(); | 
|  | 47 | EXPECT_NE(fd, -1); | 
|  | 48 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), 0); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | TEST(MemoryHeapBase, MemfdSealedProtected) { | 
|  | 52 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 53 | MemoryHeapBase::FORCE_MEMFD | | 
|  | 54 | MemoryHeapBase::READ_ONLY, | 
|  | 55 | "Test mapping"); | 
|  | 56 | int fd = mHeap->getHeapID(); | 
|  | 57 | EXPECT_NE(fd, -1); | 
|  | 58 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_SEAL | F_SEAL_FUTURE_WRITE); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | TEST(MemoryHeapBase, MemfdUnsealedProtected) { | 
|  | 62 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 63 | MemoryHeapBase::FORCE_MEMFD | | 
|  | 64 | MemoryHeapBase::READ_ONLY | | 
|  | 65 | MemoryHeapBase::MEMFD_ALLOW_SEALING, | 
|  | 66 | "Test mapping"); | 
|  | 67 | int fd = mHeap->getHeapID(); | 
|  | 68 | EXPECT_NE(fd, -1); | 
|  | 69 | EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_FUTURE_WRITE); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | #else | 
|  | 73 | TEST(MemoryHeapBase, HostMemfdExpected) { | 
|  | 74 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 75 | MemoryHeapBase::READ_ONLY, | 
|  | 76 | "Test mapping"); | 
|  | 77 | int fd = mHeap->getHeapID(); | 
|  | 78 | void* ptr = mHeap->getBase(); | 
|  | 79 | EXPECT_NE(ptr, MAP_FAILED); | 
|  | 80 | EXPECT_TRUE(ashmem_valid(fd)); | 
|  | 81 | EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | TEST(MemoryHeapBase,HostMemfdException) { | 
|  | 85 | auto mHeap = sp<MemoryHeapBase>::make(8192, | 
|  | 86 | MemoryHeapBase::FORCE_MEMFD | | 
|  | 87 | MemoryHeapBase::READ_ONLY | | 
|  | 88 | MemoryHeapBase::MEMFD_ALLOW_SEALING, | 
|  | 89 | "Test mapping"); | 
|  | 90 | int fd = mHeap->getHeapID(); | 
|  | 91 | void* ptr = mHeap->getBase(); | 
|  | 92 | EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY); | 
|  | 93 | EXPECT_TRUE(ashmem_valid(fd)); | 
|  | 94 | EXPECT_NE(ptr, MAP_FAILED); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | #endif |