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
Create(size_t size,Flags flags)32 std::unique_ptr<SharedMemoryWindows> SharedMemoryWindows::Create(
33 size_t size, Flags flags) {
34 base::ScopedPlatformHandle shmem_handle;
35 std::random_device rnd_dev;
36 uint64_t rnd_key = (static_cast<uint64_t>(rnd_dev()) << 32) | rnd_dev();
37 std::string key = "perfetto_shm_" + base::Uint64ToHexStringNoPrefix(rnd_key);
38
39 SECURITY_ATTRIBUTES security_attributes = {};
40 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
41 if (flags & Flags::kInheritableHandles)
42 security_attributes.bInheritHandle = TRUE;
43
44 shmem_handle.reset(CreateFileMappingA(
45 INVALID_HANDLE_VALUE, // Use paging file.
46 &security_attributes,
47 PAGE_READWRITE,
48 static_cast<DWORD>(size >> 32), // maximum object size (high-order DWORD)
49 static_cast<DWORD>(size), // maximum object size (low-order DWORD)
50 key.c_str()));
51
52 if (!shmem_handle) {
53 PERFETTO_PLOG("CreateFileMapping() call failed");
54 return nullptr;
55 }
56 void* start =
57 MapViewOfFile(*shmem_handle, FILE_MAP_ALL_ACCESS, /*offsetHigh=*/0,
58 /*offsetLow=*/0, size);
59 if (!start) {
60 PERFETTO_PLOG("MapViewOfFile() failed");
61 return nullptr;
62 }
63
64 return std::unique_ptr<SharedMemoryWindows>(new SharedMemoryWindows(
65 start, size, std::move(key), std::move(shmem_handle)));
66 }
67
68 // static
Attach(const std::string & key)69 std::unique_ptr<SharedMemoryWindows> SharedMemoryWindows::Attach(
70 const std::string& key) {
71 base::ScopedPlatformHandle shmem_handle;
72 shmem_handle.reset(
73 OpenFileMappingA(FILE_MAP_ALL_ACCESS, /*inherit=*/false, key.c_str()));
74 if (!shmem_handle) {
75 PERFETTO_PLOG("Failed to OpenFileMapping()");
76 return nullptr;
77 }
78
79 void* start =
80 MapViewOfFile(*shmem_handle, FILE_MAP_ALL_ACCESS, /*offsetHigh=*/0,
81 /*offsetLow=*/0, /*dwNumberOfBytesToMap=*/0);
82 if (!start) {
83 PERFETTO_PLOG("MapViewOfFile() failed");
84 return nullptr;
85 }
86
87 MEMORY_BASIC_INFORMATION info{};
88 if (!VirtualQuery(start, &info, sizeof(info))) {
89 PERFETTO_PLOG("VirtualQuery() failed");
90 return nullptr;
91 }
92 size_t size = info.RegionSize;
93 return std::unique_ptr<SharedMemoryWindows>(
94 new SharedMemoryWindows(start, size, key, std::move(shmem_handle)));
95 }
96
97 // static
AttachToHandleWithKey(base::ScopedPlatformHandle shmem_handle,const std::string & key)98 std::unique_ptr<SharedMemoryWindows> SharedMemoryWindows::AttachToHandleWithKey(
99 base::ScopedPlatformHandle shmem_handle, const std::string& key) {
100 void* start =
101 MapViewOfFile(*shmem_handle, FILE_MAP_ALL_ACCESS, /*offsetHigh=*/0,
102 /*offsetLow=*/0, /*dwNumberOfBytesToMap=*/0);
103 if (!start) {
104 PERFETTO_PLOG("MapViewOfFile() failed");
105 return nullptr;
106 }
107
108 MEMORY_BASIC_INFORMATION info{};
109 if (!VirtualQuery(start, &info, sizeof(info))) {
110 PERFETTO_PLOG("VirtualQuery() failed");
111 return nullptr;
112 }
113 size_t size = info.RegionSize;
114
115 return std::unique_ptr<SharedMemoryWindows>(
116 new SharedMemoryWindows(start, size, key, std::move(shmem_handle)));
117 }
118
SharedMemoryWindows(void * start,size_t size,std::string key,base::ScopedPlatformHandle handle)119 SharedMemoryWindows::SharedMemoryWindows(void* start,
120 size_t size,
121 std::string key,
122 base::ScopedPlatformHandle handle)
123 : start_(start),
124 size_(size),
125 key_(std::move(key)),
126 handle_(std::move(handle)) {}
127
~SharedMemoryWindows()128 SharedMemoryWindows::~SharedMemoryWindows() {
129 if (start_)
130 UnmapViewOfFile(start_);
131 }
132
133 SharedMemoryWindows::Factory::~Factory() = default;
134
CreateSharedMemory(size_t size)135 std::unique_ptr<SharedMemory> SharedMemoryWindows::Factory::CreateSharedMemory(
136 size_t size) {
137 return SharedMemoryWindows::Create(size);
138 }
139
140 } // namespace perfetto
141
142 #endif // !OS_WIN
143