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