Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 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 "tracing/src/ipc/posix_shared_memory.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 27 | #include "gtest/gtest.h" |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 28 | #include "perfetto_base/build_config.h" |
| 29 | #include "perfetto_base/scoped_file.h" |
| 30 | #include "perfetto_base/test/test_task_runner.h" |
| 31 | #include "perfetto_base/utils.h" |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 32 | |
| 33 | namespace perfetto { |
| 34 | namespace { |
| 35 | |
| 36 | const size_t kPageSize = 4096; |
| 37 | |
| 38 | bool IsFileDescriptorClosed(int fd) { |
| 39 | return lseek(fd, 0, SEEK_CUR) == -1 && errno == EBADF; |
| 40 | } |
| 41 | |
| 42 | bool 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 | |
| 65 | TEST(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 | |
| 80 | TEST(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 | |
| 90 | TEST(PosixSharedMemoryTest, AttachToFd) { |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 91 | 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 Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 95 | |
| 96 | std::unique_ptr<PosixSharedMemory> shm = |
Primiano Tucci | fc63ad4 | 2017-11-21 01:39:32 +0000 | [diff] [blame] | 97 | PosixSharedMemory::AttachToFd(base::ScopedFile(fd_num)); |
Primiano Tucci | 69ca126 | 2017-11-20 19:42:22 +0000 | [diff] [blame] | 98 | 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 |