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