Primiano Tucci | 1c5c317 | 2021-02-19 14:33:13 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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/shared_memory_windows.h" |
| 18 | |
| 19 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <random> |
| 23 | |
| 24 | #include <Windows.h> |
| 25 | |
| 26 | #include "perfetto/base/logging.h" |
| 27 | #include "perfetto/ext/base/string_utils.h" |
| 28 | |
| 29 | namespace perfetto { |
| 30 | |
| 31 | // static |
| 32 | std::unique_ptr<SharedMemoryWindows> SharedMemoryWindows::Create(size_t size) { |
| 33 | base::ScopedPlatformHandle shmem_handle; |
| 34 | std::random_device rnd_dev; |
| 35 | uint64_t rnd_key = (static_cast<uint64_t>(rnd_dev()) << 32) | rnd_dev(); |
| 36 | std::string key = "perfetto_shm_" + base::Uint64ToHexStringNoPrefix(rnd_key); |
| 37 | shmem_handle.reset(CreateFileMappingA( |
| 38 | INVALID_HANDLE_VALUE, // Use paging file. |
| 39 | nullptr, // Default security. |
| 40 | PAGE_READWRITE, |
| 41 | static_cast<DWORD>(size >> 32), // maximum object size (high-order DWORD) |
| 42 | static_cast<DWORD>(size), // maximum object size (low-order DWORD) |
| 43 | key.c_str())); |
| 44 | |
| 45 | if (!shmem_handle) { |
| 46 | PERFETTO_PLOG("CreateFileMapping() call failed"); |
| 47 | return nullptr; |
| 48 | } |
| 49 | void* start = |
| 50 | MapViewOfFile(*shmem_handle, FILE_MAP_ALL_ACCESS, /*offsetHigh=*/0, |
| 51 | /*offsetLow=*/0, size); |
| 52 | if (!start) { |
| 53 | PERFETTO_PLOG("MapViewOfFile() failed"); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | return std::unique_ptr<SharedMemoryWindows>(new SharedMemoryWindows( |
| 58 | start, size, std::move(key), std::move(shmem_handle))); |
| 59 | } |
| 60 | |
| 61 | // static |
| 62 | std::unique_ptr<SharedMemoryWindows> SharedMemoryWindows::Attach( |
| 63 | const std::string& key) { |
| 64 | base::ScopedPlatformHandle shmem_handle; |
| 65 | shmem_handle.reset( |
| 66 | OpenFileMappingA(FILE_MAP_ALL_ACCESS, /*inherit=*/false, key.c_str())); |
| 67 | if (!shmem_handle) { |
| 68 | PERFETTO_PLOG("Failed to OpenFileMapping()"); |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | void* start = |
| 73 | MapViewOfFile(*shmem_handle, FILE_MAP_ALL_ACCESS, /*offsetHigh=*/0, |
| 74 | /*offsetLow=*/0, /*dwNumberOfBytesToMap=*/0); |
| 75 | if (!start) { |
| 76 | PERFETTO_PLOG("MapViewOfFile() failed"); |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | MEMORY_BASIC_INFORMATION info{}; |
| 81 | if (!VirtualQuery(start, &info, sizeof(info))) { |
| 82 | PERFETTO_PLOG("VirtualQuery() failed"); |
| 83 | return nullptr; |
| 84 | } |
| 85 | size_t size = info.RegionSize; |
| 86 | return std::unique_ptr<SharedMemoryWindows>( |
| 87 | new SharedMemoryWindows(start, size, key, std::move(shmem_handle))); |
| 88 | } |
| 89 | |
| 90 | SharedMemoryWindows::SharedMemoryWindows(void* start, |
| 91 | size_t size, |
| 92 | std::string key, |
| 93 | base::ScopedPlatformHandle handle) |
| 94 | : start_(start), |
| 95 | size_(size), |
| 96 | key_(std::move(key)), |
| 97 | handle_(std::move(handle)) {} |
| 98 | |
| 99 | SharedMemoryWindows::~SharedMemoryWindows() { |
| 100 | if (start_) |
| 101 | UnmapViewOfFile(start_); |
| 102 | } |
| 103 | |
| 104 | SharedMemoryWindows::Factory::~Factory() = default; |
| 105 | |
| 106 | std::unique_ptr<SharedMemory> SharedMemoryWindows::Factory::CreateSharedMemory( |
| 107 | size_t size) { |
| 108 | return SharedMemoryWindows::Create(size); |
| 109 | } |
| 110 | |
| 111 | } // namespace perfetto |
| 112 | |
| 113 | #endif // !OS_WIN |