diff options
author | Elliott Hughes <enh@google.com> | 2019-08-05 13:53:01 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2019-08-05 16:31:08 -0700 |
commit | 3d24d2b0883ea828443fd7c36d1b262410618aca (patch) | |
tree | 90ddca40aa1d86f1c182462d5b3386a4375dcc3f /tests/sys_mman_test.cpp | |
parent | a5c9c7c56542b6471575148b8a3654078b180940 (diff) |
Add memfd_create(2) and mlock2(2).
These are old enough now that the latest devices will have kernels that
support them.
Also add basic doc comments to <sys/mman.h>.
Test: treehugger
Change-Id: I1b5ff5db0b6270f5c374287cac1d6a751a0259f5
Diffstat (limited to 'tests/sys_mman_test.cpp')
-rw-r--r-- | tests/sys_mman_test.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/sys_mman_test.cpp b/tests/sys_mman_test.cpp index 0b981981c..e403ea5b5 100644 --- a/tests/sys_mman_test.cpp +++ b/tests/sys_mman_test.cpp @@ -230,7 +230,10 @@ TEST(sys_mman, mmap_PTRDIFF_MAX) { TEST(sys_mman, mremap_PTRDIFF_MAX) { void* map = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); ASSERT_NE(MAP_FAILED, map); + ASSERT_EQ(MAP_FAILED, mremap(map, PAGE_SIZE, kHuge, MREMAP_MAYMOVE)); + + ASSERT_EQ(0, munmap(map, PAGE_SIZE)); } TEST(sys_mman, mmap_bug_27265969) { @@ -239,3 +242,61 @@ TEST(sys_mman, mmap_bug_27265969) { // Some kernels had bugs that would cause segfaults here... __builtin___clear_cache(base, base + (PAGE_SIZE * 2)); } + +TEST(sys_mman, mlock) { + void* map = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(MAP_FAILED, map); + + // Not really anything we can assert about this. + mlock(map, PAGE_SIZE); + + ASSERT_EQ(0, munmap(map, PAGE_SIZE)); +} + +TEST(sys_mman, mlock2) { +#if defined(__GLIBC__) + GTEST_SKIP() << "needs glibc 2.27"; +#else + void* map = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(MAP_FAILED, map); + + // Not really anything we can assert about this. + mlock2(map, PAGE_SIZE, MLOCK_ONFAULT); + + ASSERT_EQ(0, munmap(map, PAGE_SIZE)); +#endif +} + +TEST(sys_mman, memfd_create) { +#if defined(__GLIBC__) + GTEST_SKIP() << "needs glibc 2.27"; +#else + // Is the MFD_CLOEXEC flag obeyed? + errno = 0; + int fd = memfd_create("doesn't matter", 0); + if (fd == -1) { + ASSERT_EQ(ENOSYS, errno); + GTEST_SKIP() << "no memfd_create available"; + } + int f = fcntl(fd, F_GETFD); + ASSERT_NE(-1, f); + ASSERT_FALSE(f & FD_CLOEXEC); + close(fd); + + errno = 0; + fd = memfd_create("doesn't matter", MFD_CLOEXEC); + f = fcntl(fd, F_GETFD); + ASSERT_NE(-1, f); + ASSERT_TRUE(f & FD_CLOEXEC); + + // Can we read and write? + std::string expected("hello, world!"); + ASSERT_TRUE(android::base::WriteStringToFd(expected, fd)); + ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)); + std::string actual; + ASSERT_TRUE(android::base::ReadFdToString(fd, &actual)); + ASSERT_EQ(expected, actual); + + close(fd); +#endif +} |