blob: f312147775b2dff498e7e358623c8468af290319 [file] [log] [blame]
Primiano Tucci69ca1262017-11-20 19:42:22 +00001/*
2 * Copyright (C) 2017 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 "tracing/src/ipc/posix_shared_memory.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <string.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#include "base/build_config.h"
27#include "base/scoped_file.h"
28#include "base/test/test_task_runner.h"
29#include "base/utils.h"
30#include "gtest/gtest.h"
31
32namespace perfetto {
33namespace {
34
35const size_t kPageSize = 4096;
36
37bool IsFileDescriptorClosed(int fd) {
38 return lseek(fd, 0, SEEK_CUR) == -1 && errno == EBADF;
39}
40
41bool IsMapped(void* start, size_t size) {
42#if BUILDFLAG(OS_MACOSX)
43 using PageState = char;
44#else
45 using PageState = unsigned char;
46#endif
47 EXPECT_EQ(0u, size % kPageSize);
48 const size_t num_pages = size / kPageSize;
49 std::unique_ptr<PageState[]> page_states(new PageState[num_pages]);
50 memset(page_states.get(), 0, num_pages * sizeof(PageState));
51 int res = mincore(start, size, page_states.get());
52 // Linux returns ENOMEM when an unmapped memory range is passed.
53 // MacOS instead returns 0 but leaves the page_states empty.
54 if (res == -1 && errno == ENOMEM)
55 return false;
56 EXPECT_EQ(0, res);
57 for (size_t i = 0; i < num_pages; i++) {
58 if (!page_states[i])
59 return false;
60 }
61 return true;
62}
63
64TEST(PosixSharedMemoryTest, DestructorUnmapsMemory) {
65 PosixSharedMemory::Factory factory;
66 std::unique_ptr<SharedMemory> shm = factory.CreateSharedMemory(kPageSize);
67 void* const shm_start = shm->start();
68 const size_t shm_size = shm->size();
69 ASSERT_NE(nullptr, shm_start);
70 ASSERT_EQ(kPageSize, shm_size);
71
72 memcpy(shm_start, "test", 5);
73 ASSERT_TRUE(IsMapped(shm_start, shm_size));
74
75 shm.reset();
76 ASSERT_FALSE(IsMapped(shm_start, shm_size));
77}
78
79TEST(PosixSharedMemoryTest, DestructorClosesFD) {
80 std::unique_ptr<PosixSharedMemory> shm = PosixSharedMemory::Create(kPageSize);
81 int fd = shm->fd();
82 ASSERT_GE(fd, 0);
83 ASSERT_EQ(static_cast<off_t>(kPageSize), lseek(fd, 0, SEEK_END));
84
85 shm.reset();
86 ASSERT_TRUE(IsFileDescriptorClosed(fd));
87}
88
89TEST(PosixSharedMemoryTest, AttachToFd) {
90 static const char kTmpPath[] = "/tmp/perfetto-shm-test";
91 base::ScopedFile fd(open(kTmpPath, O_CREAT | O_RDWR | O_TRUNC));
92 unlink(kTmpPath);
93 const int fd_num = *fd;
94 ASSERT_TRUE(fd);
95 ASSERT_EQ(0, ftruncate(*fd, kPageSize));
96 ASSERT_EQ(7, PERFETTO_EINTR(write(*fd, "foobar", 7)));
97
98 std::unique_ptr<PosixSharedMemory> shm =
99 PosixSharedMemory::AttachToFd(std::move(fd));
100 void* const shm_start = shm->start();
101 const size_t shm_size = shm->size();
102 ASSERT_NE(nullptr, shm_start);
103 ASSERT_EQ(kPageSize, shm_size);
104 ASSERT_EQ(0, memcmp("foobar", shm_start, 7));
105
106 ASSERT_FALSE(IsFileDescriptorClosed(fd_num));
107
108 shm.reset();
109 ASSERT_TRUE(IsFileDescriptorClosed(fd_num));
110 ASSERT_FALSE(IsMapped(shm_start, shm_size));
111}
112
113} // namespace
114} // namespace perfetto