1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/shared_memory_handle.h"
#include <mach/mach_vm.h>
#include <sys/mman.h>
#include <unistd.h>
#include "base/posix/eintr_wrapper.h"
namespace base {
static_assert(sizeof(SharedMemoryHandle::Type) <=
sizeof(SharedMemoryHandle::TypeWireFormat),
"Size of enum SharedMemoryHandle::Type exceeds size of type "
"transmitted over wire.");
SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX), file_descriptor_() {}
SharedMemoryHandle::SharedMemoryHandle(
const base::FileDescriptor& file_descriptor)
: type_(POSIX), file_descriptor_(file_descriptor) {}
SharedMemoryHandle::SharedMemoryHandle(int fd, bool auto_close)
: type_(POSIX), file_descriptor_(fd, auto_close) {}
SharedMemoryHandle::SharedMemoryHandle(mach_vm_size_t size) {
type_ = MACH;
mach_port_t named_right;
kern_return_t kr = mach_make_memory_entry_64(
mach_task_self(),
&size,
0, // Address.
MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE,
&named_right,
MACH_PORT_NULL); // Parent handle.
if (kr != KERN_SUCCESS) {
memory_object_ = MACH_PORT_NULL;
return;
}
memory_object_ = named_right;
size_ = size;
pid_ = GetCurrentProcId();
}
SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object,
mach_vm_size_t size,
base::ProcessId pid)
: type_(MACH), memory_object_(memory_object), size_(size), pid_(pid) {}
SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle)
: type_(handle.type_) {
CopyRelevantData(handle);
}
SharedMemoryHandle& SharedMemoryHandle::operator=(
const SharedMemoryHandle& handle) {
if (this == &handle)
return *this;
type_ = handle.type_;
CopyRelevantData(handle);
return *this;
}
SharedMemoryHandle SharedMemoryHandle::Duplicate() const {
switch (type_) {
case POSIX: {
if (!IsValid())
return SharedMemoryHandle();
int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd));
if (duped_fd < 0)
return SharedMemoryHandle();
return SharedMemoryHandle(duped_fd, true);
}
case MACH: {
if (!IsValid())
return SharedMemoryHandle(MACH_PORT_NULL, 0, 0);
// Increment the ref count.
kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_,
MACH_PORT_RIGHT_SEND, 1);
DCHECK_EQ(kr, KERN_SUCCESS);
return SharedMemoryHandle(*this);
}
}
}
bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const {
if (!IsValid() && !handle.IsValid())
return true;
if (type_ != handle.type_)
return false;
switch (type_) {
case POSIX:
return file_descriptor_ == handle.file_descriptor_;
case MACH:
return memory_object_ == handle.memory_object_ && size_ == handle.size_ &&
pid_ == handle.pid_;
}
}
bool SharedMemoryHandle::operator!=(const SharedMemoryHandle& handle) const {
return !(*this == handle);
}
SharedMemoryHandle::Type SharedMemoryHandle::GetType() const {
return type_;
}
bool SharedMemoryHandle::IsValid() const {
switch (type_) {
case POSIX:
return file_descriptor_.fd >= 0;
case MACH:
return memory_object_ != MACH_PORT_NULL;
}
}
void SharedMemoryHandle::SetFileHandle(int fd, bool auto_close) {
DCHECK(!IsValid());
file_descriptor_.fd = fd;
file_descriptor_.auto_close = auto_close;
type_ = POSIX;
}
const FileDescriptor SharedMemoryHandle::GetFileDescriptor() const {
DCHECK_EQ(type_, POSIX);
return file_descriptor_;
}
mach_port_t SharedMemoryHandle::GetMemoryObject() const {
DCHECK_EQ(type_, MACH);
return memory_object_;
}
bool SharedMemoryHandle::GetSize(size_t* size) const {
if (!IsValid())
return false;
switch (type_) {
case SharedMemoryHandle::POSIX:
struct stat st;
if (fstat(file_descriptor_.fd, &st) != 0)
return false;
if (st.st_size < 0)
return false;
*size = st.st_size;
return true;
case SharedMemoryHandle::MACH:
*size = size_;
return true;
}
}
bool SharedMemoryHandle::MapAt(off_t offset,
size_t bytes,
void** memory,
bool read_only) {
DCHECK(IsValid());
switch (type_) {
case SharedMemoryHandle::POSIX:
*memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE),
MAP_SHARED, file_descriptor_.fd, offset);
return *memory && *memory != reinterpret_cast<void*>(-1);
case SharedMemoryHandle::MACH:
DCHECK_EQ(pid_, GetCurrentProcId());
kern_return_t kr = mach_vm_map(
mach_task_self(),
reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter
bytes,
0, // Alignment mask
VM_FLAGS_ANYWHERE,
memory_object_,
offset,
FALSE, // Copy
VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection
VM_PROT_READ | VM_PROT_WRITE, // Maximum protection
VM_INHERIT_NONE);
return kr == KERN_SUCCESS;
}
}
void SharedMemoryHandle::Close() const {
if (!IsValid())
return;
switch (type_) {
case POSIX:
if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0)
DPLOG(ERROR) << "Error closing fd.";
break;
case MACH:
kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_);
if (kr != KERN_SUCCESS)
DPLOG(ERROR) << "Error deallocating mach port: " << kr;
break;
}
}
void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) {
switch (type_) {
case POSIX:
file_descriptor_ = handle.file_descriptor_;
break;
case MACH:
memory_object_ = handle.memory_object_;
size_ = handle.size_;
pid_ = handle.pid_;
break;
}
}
} // namespace base
|