summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn <salyzyn@google.com>2018-01-18 07:59:38 -0800
committerMark Salyzyn <salyzyn@google.com>2018-01-26 09:50:57 -0800
commit276758dab8e24b29be0abb1b6bdadd7c897e8701 (patch)
tree2ad6303d8fb04df29bccb66e698081b313f9b2d8
parentbb4d172f702744ae492000c68a4d43e7d1f478c5 (diff)
downloadsystem_core-276758dab8e24b29be0abb1b6bdadd7c897e8701.tar.gz
system_core-276758dab8e24b29be0abb1b6bdadd7c897e8701.tar.bz2
system_core-276758dab8e24b29be0abb1b6bdadd7c897e8701.zip
libcutils: test: increase use of ashmem_valid(fd)
There may be evidence of ashmem_valid(fd) reporting that the file descriptor is an ashmem node. Increase testing of ashmem_valid(fd), reporting that the node _is_ ashmem, to inspire confidence in the positive result. Scan all file descriptors in the system, and for those that pass ashmem_valid, get a non-zero size reference back. Some clang-format-isms applied. Test: libcutils-test --gtest_filter=AshmemTest.* Bug: 72021458 Change-Id: I77d746b57a89a6afa1b829dddfdc4dd319f6b684
-rw-r--r--libcutils/tests/AshmemTest.cpp70
1 files changed, 48 insertions, 22 deletions
diff --git a/libcutils/tests/AshmemTest.cpp b/libcutils/tests/AshmemTest.cpp
index a87e23e30..b37d020fb 100644
--- a/libcutils/tests/AshmemTest.cpp
+++ b/libcutils/tests/AshmemTest.cpp
@@ -14,11 +14,18 @@
* limitations under the License.
*/
+#include <errno.h>
+#include <linux/fs.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/macros.h>
#include <android-base/unique_fd.h>
#include <cutils/ashmem.h>
#include <gtest/gtest.h>
-#include <linux/fs.h>
-#include <sys/mman.h>
using android::base::unique_fd;
@@ -31,15 +38,21 @@ void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
}
void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_TRUE(ashmem_valid(fd));
*region = mmap(nullptr, size, prot, MAP_SHARED, fd, off);
ASSERT_NE(MAP_FAILED, *region);
}
void TestProtDenied(const unique_fd &fd, size_t size, int prot) {
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_TRUE(ashmem_valid(fd));
EXPECT_EQ(MAP_FAILED, mmap(nullptr, size, prot, MAP_SHARED, fd, 0));
}
void TestProtIs(const unique_fd& fd, int prot) {
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_TRUE(ashmem_valid(fd));
EXPECT_EQ(prot, ioctl(fd, ASHMEM_GET_PROT_MASK));
}
@@ -86,18 +99,23 @@ TEST(AshmemTest, ForkTest) {
ASSERT_EQ(0, memcmp(region1, &data, size));
EXPECT_EQ(0, munmap(region1, size));
- ASSERT_EXIT({
- void *region2 = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (region2 == MAP_FAILED) {
- _exit(1);
- }
- if (memcmp(region2, &data, size) != 0) {
- _exit(2);
- }
- memset(region2, 0, size);
- munmap(region2, size);
- _exit(0);
- }, ::testing::ExitedWithCode(0),"");
+ ASSERT_EXIT(
+ {
+ if (!ashmem_valid(fd)) {
+ _exit(3);
+ }
+ void* region2 = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (region2 == MAP_FAILED) {
+ _exit(1);
+ }
+ if (memcmp(region2, &data, size) != 0) {
+ _exit(2);
+ }
+ memset(region2, 0, size);
+ munmap(region2, size);
+ _exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
memset(&data, 0, size);
void *region2;
@@ -146,6 +164,7 @@ TEST(AshmemTest, FileOperationsTest) {
};
for (const auto& cfg : seeks) {
errno = 0;
+ ASSERT_TRUE(ashmem_valid(fd));
auto off = lseek(fd, cfg.offset, cfg.whence);
ASSERT_EQ(cfg.ret, off) << "lseek(" << cfg.offset << ", " << cfg.whence << ") failed"
<< (errno ? ": " : "") << (errno ? strerror(errno) : "");
@@ -196,15 +215,19 @@ TEST(AshmemTest, ForkProtTest) {
constexpr size_t size = PAGE_SIZE;
int protFlags[] = { PROT_READ, PROT_WRITE };
- for (int i = 0; i < 2; i++) {
+ for (size_t i = 0; i < arraysize(protFlags); i++) {
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
- ASSERT_EXIT({
- if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
- _exit(0);
- } else {
- _exit(1);
- }
- }, ::testing::ExitedWithCode(0), "");
+ ASSERT_EXIT(
+ {
+ if (!ashmem_valid(fd)) {
+ _exit(3);
+ } else if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
+ _exit(0);
+ } else {
+ _exit(1);
+ }
+ },
+ ::testing::ExitedWithCode(0), "");
ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, protFlags[1-i]));
}
}
@@ -227,6 +250,9 @@ TEST(AshmemTest, ForkMultiRegionTest) {
ASSERT_EXIT({
for (int i = 0; i < nRegions; i++) {
+ if (!ashmem_valid(fd[i])) {
+ _exit(3);
+ }
void *region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
if (region == MAP_FAILED) {
_exit(1);