2014-01-28 01:28:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <fcntl.h>
|
2014-01-28 01:28:31 +01:00
|
|
|
#include <sys/mman.h>
|
2015-11-19 22:44:20 +01:00
|
|
|
#include <sys/user.h>
|
2014-06-12 22:16:36 +02:00
|
|
|
#include <sys/types.h>
|
2014-01-28 01:28:31 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <gtest/gtest.h>
|
2014-06-12 22:16:36 +02:00
|
|
|
|
2023-09-21 23:11:19 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2023-10-19 23:10:31 +02:00
|
|
|
static const size_t kPageSize = getpagesize();
|
|
|
|
|
2014-06-12 22:16:36 +02:00
|
|
|
TEST(sys_mman, mmap_std) {
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
ASSERT_EQ(0, munmap(map, 4096));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap64_std) {
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap64(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
ASSERT_EQ(0, munmap(map, 4096));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_file_bad_offset) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, 100, PROT_READ, MAP_SHARED, tf.fd, 1);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_EQ(MAP_FAILED, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap64_file_bad_offset) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap64(nullptr, 100, PROT_READ, MAP_SHARED, tf.fd, 1);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_EQ(MAP_FAILED, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STR_SSIZE(str) static_cast<ssize_t>(sizeof(str))
|
|
|
|
|
|
|
|
#define STRING_MSG "012345678\nabcdefgh\n"
|
|
|
|
#define INITIAL_MSG "000000000\n00000000\n"
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_file_read) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
|
|
|
|
ASSERT_EQ(STR_SSIZE(STRING_MSG), write(tf.fd, STRING_MSG, sizeof(STRING_MSG)));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, sizeof(STRING_MSG), PROT_READ, MAP_SHARED, tf.fd, 0);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
char* data = reinterpret_cast<char*>(map);
|
|
|
|
ASSERT_STREQ(STRING_MSG, data);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, sizeof(STRING_MSG)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_file_write) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
|
|
|
|
ASSERT_EQ(STR_SSIZE(INITIAL_MSG), write(tf.fd, INITIAL_MSG, sizeof(INITIAL_MSG)));
|
|
|
|
lseek(tf.fd, 0, SEEK_SET);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, sizeof(STRING_MSG), PROT_WRITE, MAP_SHARED, tf.fd, 0);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
close(tf.fd);
|
|
|
|
|
|
|
|
memcpy(map, STRING_MSG, sizeof(STRING_MSG));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, sizeof(STRING_MSG)));
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
tf.fd = open(tf.path, O_RDWR);
|
2014-06-12 22:16:36 +02:00
|
|
|
char buf[sizeof(STRING_MSG)];
|
|
|
|
memset(buf, 0, sizeof(STRING_MSG));
|
|
|
|
ASSERT_EQ(STR_SSIZE(STRING_MSG), read(tf.fd, buf, sizeof(STRING_MSG)));
|
|
|
|
|
|
|
|
ASSERT_STREQ(STRING_MSG, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PAGE0_MSG "00PAGE00"
|
|
|
|
#define PAGE1_MSG "111PAGE111"
|
|
|
|
#define PAGE2_MSG "2222PAGE2222"
|
|
|
|
#define END_MSG "E"
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_file_read_at_offset) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
size_t pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
|
|
|
|
// Create the file with three pages worth of data.
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE0_MSG), write(tf.fd, PAGE0_MSG, sizeof(PAGE0_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE1_MSG), write(tf.fd, PAGE1_MSG, sizeof(PAGE1_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 2 * pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE2_MSG), write(tf.fd, PAGE2_MSG, sizeof(PAGE2_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 3 * pagesize - sizeof(END_MSG), SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(END_MSG), write(tf.fd, END_MSG, sizeof(END_MSG)));
|
|
|
|
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 0, SEEK_SET));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, pagesize, PROT_READ, MAP_SHARED, tf.fd, pagesize);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
char* data = reinterpret_cast<char*>(map);
|
|
|
|
ASSERT_STREQ(PAGE1_MSG, data);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
map = mmap(nullptr, pagesize, PROT_READ, MAP_SHARED, tf.fd, 2 * pagesize);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
data = reinterpret_cast<char*>(map);
|
|
|
|
ASSERT_STREQ(PAGE2_MSG, data);
|
|
|
|
ASSERT_STREQ(END_MSG, data+pagesize-sizeof(END_MSG));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
2014-01-28 01:28:31 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 22:16:36 +02:00
|
|
|
#define NEWPAGE1_MSG "1NEW1PAGE1"
|
|
|
|
#define NEWPAGE2_MSG "22NEW22PAGE22"
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_file_write_at_offset) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
size_t pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
|
|
|
|
// Create the file with three pages worth of data.
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE0_MSG), write(tf.fd, PAGE0_MSG, sizeof(PAGE0_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE1_MSG), write(tf.fd, PAGE1_MSG, sizeof(PAGE1_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 2 * pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(PAGE2_MSG), write(tf.fd, PAGE2_MSG, sizeof(PAGE2_MSG)));
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 3 * pagesize - sizeof(END_MSG), SEEK_SET));
|
|
|
|
ASSERT_EQ(STR_SSIZE(END_MSG), write(tf.fd, END_MSG, sizeof(END_MSG)));
|
|
|
|
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 0, SEEK_SET));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, pagesize, PROT_WRITE, MAP_SHARED, tf.fd, pagesize);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
close(tf.fd);
|
|
|
|
|
|
|
|
memcpy(map, NEWPAGE1_MSG, sizeof(NEWPAGE1_MSG));
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
tf.fd = open(tf.path, O_RDWR);
|
2018-08-03 02:31:13 +02:00
|
|
|
map = mmap(nullptr, pagesize, PROT_WRITE, MAP_SHARED, tf.fd, 2 * pagesize);
|
2014-06-12 22:16:36 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
close(tf.fd);
|
|
|
|
|
|
|
|
memcpy(map, NEWPAGE2_MSG, sizeof(NEWPAGE2_MSG));
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
tf.fd = open(tf.path, O_RDWR);
|
2014-06-12 22:16:36 +02:00
|
|
|
char buf[pagesize];
|
|
|
|
ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize));
|
|
|
|
ASSERT_STREQ(PAGE0_MSG, buf);
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize));
|
|
|
|
ASSERT_STREQ(NEWPAGE1_MSG, buf);
|
|
|
|
ASSERT_NE(-1, lseek(tf.fd, 2 * pagesize, SEEK_SET));
|
|
|
|
ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize));
|
|
|
|
ASSERT_STREQ(NEWPAGE2_MSG, buf);
|
|
|
|
ASSERT_STREQ(END_MSG, buf+pagesize-sizeof(END_MSG));
|
2014-01-28 01:28:31 +01:00
|
|
|
}
|
2014-11-26 05:17:27 +01:00
|
|
|
|
|
|
|
TEST(sys_mman, posix_madvise) {
|
|
|
|
TemporaryFile tempfile;
|
|
|
|
size_t pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
char buf[pagesize];
|
|
|
|
|
|
|
|
// Prepare environment.
|
|
|
|
ASSERT_EQ(static_cast<ssize_t>(pagesize), write(tempfile.fd, buf, pagesize));
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, tempfile.fd, 0);
|
2014-11-26 05:17:27 +01:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
// Verify different options of posix_madvise.
|
|
|
|
ASSERT_EQ(0, posix_madvise(map, pagesize, POSIX_MADV_NORMAL));
|
|
|
|
ASSERT_EQ(0, posix_madvise(map, pagesize, POSIX_MADV_SEQUENTIAL));
|
|
|
|
ASSERT_EQ(0, posix_madvise(map, pagesize, POSIX_MADV_RANDOM));
|
|
|
|
ASSERT_EQ(0, posix_madvise(map, pagesize, POSIX_MADV_WILLNEED));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that memory can still access after posix_madvise(POSIX_MADV_DONTNEED).
|
|
|
|
// We should test on MAP_ANONYMOUS memory to verify whether the memory is discarded,
|
|
|
|
// because the content of non MAP_ANONYMOUS memory can be reread from file.
|
|
|
|
TEST(sys_mman, posix_madvise_POSIX_MADV_DONTNEED) {
|
|
|
|
size_t pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* map = mmap(nullptr, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2014-11-26 05:17:27 +01:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
int* int_ptr = reinterpret_cast<int*>(map);
|
|
|
|
for (int i = 0; i < static_cast<int>(pagesize / sizeof(int)); ++i) {
|
|
|
|
*int_ptr++ = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(0, posix_madvise(map, pagesize, POSIX_MADV_DONTNEED));
|
|
|
|
|
|
|
|
int_ptr = reinterpret_cast<int*>(map);
|
|
|
|
for (int i = 0; i < static_cast<int>(pagesize / sizeof(int)); ++i) {
|
|
|
|
ASSERT_EQ(i, *int_ptr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(map, pagesize));
|
|
|
|
}
|
2015-11-03 11:14:08 +01:00
|
|
|
|
|
|
|
TEST(sys_mman, mremap) {
|
2023-05-05 01:29:00 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wnonnull"
|
2015-11-03 11:14:08 +01:00
|
|
|
ASSERT_EQ(MAP_FAILED, mremap(nullptr, 0, 0, 0));
|
2023-05-05 01:29:00 +02:00
|
|
|
#pragma clang diagnostic pop
|
2015-11-03 11:14:08 +01:00
|
|
|
}
|
2015-11-07 16:40:26 +01:00
|
|
|
|
2016-07-15 02:07:17 +02:00
|
|
|
constexpr size_t kHuge = size_t(PTRDIFF_MAX) + 1;
|
2015-11-07 16:40:26 +01:00
|
|
|
|
|
|
|
TEST(sys_mman, mmap_PTRDIFF_MAX) {
|
2016-07-15 02:07:17 +02:00
|
|
|
ASSERT_EQ(MAP_FAILED, mmap(nullptr, kHuge, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
2015-11-07 16:40:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mremap_PTRDIFF_MAX) {
|
2023-10-19 23:10:31 +02:00
|
|
|
void* map = mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2015-11-07 16:40:26 +01:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
2019-08-05 22:53:01 +02:00
|
|
|
|
2023-10-19 23:10:31 +02:00
|
|
|
ASSERT_EQ(MAP_FAILED, mremap(map, kPageSize, kHuge, MREMAP_MAYMOVE));
|
2019-08-05 22:53:01 +02:00
|
|
|
|
2023-10-19 23:10:31 +02:00
|
|
|
ASSERT_EQ(0, munmap(map, kPageSize));
|
2016-07-15 02:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mmap_bug_27265969) {
|
2023-10-19 23:10:31 +02:00
|
|
|
char* base = reinterpret_cast<char*>(
|
|
|
|
mmap(nullptr, kPageSize * 2, PROT_EXEC | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
|
2016-07-15 02:07:17 +02:00
|
|
|
// Some kernels had bugs that would cause segfaults here...
|
2023-10-19 23:10:31 +02:00
|
|
|
__builtin___clear_cache(base, base + (kPageSize * 2));
|
2015-11-07 16:40:26 +01:00
|
|
|
}
|
2019-08-05 22:53:01 +02:00
|
|
|
|
|
|
|
TEST(sys_mman, mlock) {
|
2023-10-19 23:10:31 +02:00
|
|
|
void* map = mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2019-08-05 22:53:01 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
// Not really anything we can assert about this.
|
2023-10-19 23:10:31 +02:00
|
|
|
mlock(map, kPageSize);
|
2019-08-05 22:53:01 +02:00
|
|
|
|
2023-10-19 23:10:31 +02:00
|
|
|
ASSERT_EQ(0, munmap(map, kPageSize));
|
2019-08-05 22:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sys_mman, mlock2) {
|
|
|
|
#if defined(__GLIBC__)
|
|
|
|
GTEST_SKIP() << "needs glibc 2.27";
|
|
|
|
#else
|
2023-10-19 23:10:31 +02:00
|
|
|
void* map = mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2019-08-05 22:53:01 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, map);
|
|
|
|
|
|
|
|
// Not really anything we can assert about this.
|
2023-10-19 23:10:31 +02:00
|
|
|
mlock2(map, kPageSize, MLOCK_ONFAULT);
|
2019-08-05 22:53:01 +02:00
|
|
|
|
2023-10-19 23:10:31 +02:00
|
|
|
ASSERT_EQ(0, munmap(map, kPageSize));
|
2019-08-05 22:53:01 +02:00
|
|
|
#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);
|
2023-09-23 02:15:25 +02:00
|
|
|
if (fd == -1 && errno == ENOSYS) GTEST_SKIP() << "no memfd_create() in this kernel";
|
|
|
|
ASSERT_NE(-1, fd) << strerror(errno);
|
|
|
|
|
2019-08-05 22:53:01 +02:00
|
|
|
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
|
|
|
|
}
|