1 /*
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 "src/tracing/ipc/posix_shared_memory.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 #include "gtest/gtest.h"
27 #include "perfetto/base/build_config.h"
28 #include "perfetto/base/file_utils.h"
29 #include "perfetto/base/scoped_file.h"
30 #include "perfetto/base/temp_file.h"
31 #include "perfetto/base/utils.h"
32 #include "src/base/test/test_task_runner.h"
33 #include "src/base/test/vm_test_utils.h"
34 
35 namespace perfetto {
36 namespace {
37 
IsFileDescriptorClosed(int fd)38 bool IsFileDescriptorClosed(int fd) {
39   return lseek(fd, 0, SEEK_CUR) == -1 && errno == EBADF;
40 }
41 
TEST(PosixSharedMemoryTest,DestructorUnmapsMemory)42 TEST(PosixSharedMemoryTest, DestructorUnmapsMemory) {
43   PosixSharedMemory::Factory factory;
44   std::unique_ptr<SharedMemory> shm =
45       factory.CreateSharedMemory(base::kPageSize);
46   void* const shm_start = shm->start();
47   const size_t shm_size = shm->size();
48   ASSERT_NE(nullptr, shm_start);
49   ASSERT_EQ(base::kPageSize, shm_size);
50 
51   memcpy(shm_start, "test", 5);
52   ASSERT_TRUE(base::vm_test_utils::IsMapped(shm_start, shm_size));
53 
54   shm.reset();
55   ASSERT_FALSE(base::vm_test_utils::IsMapped(shm_start, shm_size));
56 }
57 
TEST(PosixSharedMemoryTest,DestructorClosesFD)58 TEST(PosixSharedMemoryTest, DestructorClosesFD) {
59   std::unique_ptr<PosixSharedMemory> shm =
60       PosixSharedMemory::Create(base::kPageSize);
61   int fd = shm->fd();
62   ASSERT_GE(fd, 0);
63   ASSERT_EQ(static_cast<off_t>(base::kPageSize), lseek(fd, 0, SEEK_END));
64 
65   shm.reset();
66   ASSERT_TRUE(IsFileDescriptorClosed(fd));
67 }
68 
TEST(PosixSharedMemoryTest,AttachToFd)69 TEST(PosixSharedMemoryTest, AttachToFd) {
70   base::TempFile tmp_file = base::TempFile::CreateUnlinked();
71   const int fd_num = tmp_file.fd();
72   ASSERT_EQ(0, ftruncate(fd_num, base::kPageSize));
73   ASSERT_EQ(7, base::WriteAll(fd_num, "foobar", 7));
74 
75   std::unique_ptr<PosixSharedMemory> shm =
76       PosixSharedMemory::AttachToFd(tmp_file.ReleaseFD());
77   void* const shm_start = shm->start();
78   const size_t shm_size = shm->size();
79   ASSERT_NE(nullptr, shm_start);
80   ASSERT_EQ(base::kPageSize, shm_size);
81   ASSERT_EQ(0, memcmp("foobar", shm_start, 7));
82 
83   ASSERT_FALSE(IsFileDescriptorClosed(fd_num));
84 
85   shm.reset();
86   ASSERT_TRUE(IsFileDescriptorClosed(fd_num));
87   ASSERT_FALSE(base::vm_test_utils::IsMapped(shm_start, shm_size));
88 }
89 
90 }  // namespace
91 }  // namespace perfetto
92