forked from krockot/ipcz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_reference_driver.cc
More file actions
391 lines (338 loc) · 12.9 KB
/
sync_reference_driver.cc
File metadata and controls
391 lines (338 loc) · 12.9 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "reference_drivers/sync_reference_driver.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
#include "ipcz/ipcz.h"
#include "reference_drivers/object.h"
#include "reference_drivers/random.h"
#include "reference_drivers/single_process_reference_driver_base.h"
#include "third_party/abseil-cpp/absl/synchronization/mutex.h"
#include "third_party/abseil-cpp/absl/types/span.h"
#include "util/ref_counted.h"
namespace ipcz::reference_drivers {
namespace {
void CloseAllHandles(absl::Span<const IpczDriverHandle> handles) {
for (IpczDriverHandle handle : handles) {
Object::TakeFromHandle(handle)->Close();
}
}
// Provides shared ownership of a transport object given to the driver by ipcz
// during driver transport activation. Within ipcz this corresponds to a
// DriverTransport object.
class TransportWrapper : public RefCounted<TransportWrapper> {
public:
TransportWrapper(IpczHandle transport,
IpczTransportActivityHandler activity_handler)
: transport_(transport), activity_handler_(activity_handler) {}
void Notify(absl::Span<const uint8_t> data,
absl::Span<const IpczDriverHandle> handles) {
DoNotify(IPCZ_NO_FLAGS, data, handles);
}
void NotifyError() { DoNotify(IPCZ_TRANSPORT_ACTIVITY_ERROR); }
private:
friend class RefCounted<TransportWrapper>;
~TransportWrapper() {
// Since this is destruction, we can safely assume the invocation will be
// exclusive. Otherwise someone is mismanaging a reference count or has
// a UAF bug.
DoNotifyExclusive(IPCZ_TRANSPORT_ACTIVITY_DEACTIVATED, {}, {});
}
// Helper to serialize the invocation of potentially overlapping or reentrant
// notifications from this transport.
void DoNotify(IpczTransportActivityFlags flags,
absl::Span<const uint8_t> data = {},
absl::Span<const IpczDriverHandle> handles = {}) {
{
absl::MutexLock lock(&mutex_);
if (in_notification_) {
DeferredNotification notification = {.flags = flags};
if (!data.empty()) {
notification.data = std::vector<uint8_t>(data.begin(), data.end());
}
if (!handles.empty()) {
notification.handles =
std::vector<IpczDriverHandle>(handles.begin(), handles.end());
}
deferred_notifications_.push_back(std::move(notification));
return;
}
in_notification_ = true;
}
DoNotifyExclusive(flags, data, handles);
// Now flush any notifications that queued while this one was in progress.
// This continues until we complete an iteration with no new notifications
// being queued.
for (;;) {
std::vector<DeferredNotification> notifications;
{
absl::MutexLock lock(&mutex_);
if (deferred_notifications_.empty()) {
in_notification_ = false;
return;
}
notifications.swap(deferred_notifications_);
}
for (const auto& n : notifications) {
DoNotifyExclusive(n.flags, n.data, n.handles);
}
}
}
// Invokes the activity handler unguarded. The caller must ensure that this is
// mututally exclusive with any other invocation of the method.
void DoNotifyExclusive(IpczTransportActivityFlags flags,
absl::Span<const uint8_t> data,
absl::Span<const IpczDriverHandle> handles) {
const IpczResult result = activity_handler_(
transport_, data.empty() ? nullptr : data.data(), data.size(),
handles.empty() ? nullptr : handles.data(), handles.size(), flags,
nullptr);
if (result != IPCZ_RESULT_OK && result != IPCZ_RESULT_UNIMPLEMENTED) {
NotifyError();
}
}
const IpczHandle transport_;
const IpczTransportActivityHandler activity_handler_;
// Queues copies of any pending notifications which were issued while another
// notification was already in progress, either concurrently or reentrantly.
// The queue is always flushed completely as the active notification stack
// unwinds.
struct DeferredNotification {
IpczTransportActivityFlags flags;
std::vector<uint8_t> data;
std::vector<IpczDriverHandle> handles;
};
absl::Mutex mutex_;
bool in_notification_ ABSL_GUARDED_BY(mutex_) = false;
std::vector<DeferredNotification> deferred_notifications_
ABSL_GUARDED_BY(mutex_);
};
struct SavedMessage {
std::vector<uint8_t> data;
std::vector<IpczDriverHandle> handles;
};
// The driver transport implementation for the single-process reference driver.
//
// Each InProcessTransport holds a direct reference to the other endpoint, and
// transmitting from one endpoint directly notifies the peer endpoint, calling
// directly into its ipcz-side DriverTransport's Listener.
//
// This means that cross-node communications through this driver function as
// synchronous calls from one node into another, and as such the implementation
// must be safe for arbitrary reentrancy.
class InProcessTransport
: public ObjectImpl<InProcessTransport, Object::kTransport> {
public:
InProcessTransport() = default;
~InProcessTransport() override = default;
// Object:
IpczResult Close() override {
Deactivate();
Ref<InProcessTransport> peer;
std::vector<SavedMessage> saved_messages;
{
absl::MutexLock lock(&mutex_);
peer = std::move(peer_);
saved_messages = std::move(saved_messages_);
}
if (peer) {
for (SavedMessage& m : saved_messages) {
CloseAllHandles(absl::MakeSpan(m.handles));
}
// NOTE: Although nothing should ever call back into `this` after Close(),
// for consistency with other methods we still take precaution not to call
// into the peer while holding `mutex_`.
peer->OnPeerClosed();
}
return IPCZ_RESULT_OK;
}
void SetPeer(Ref<InProcessTransport> peer) {
absl::MutexLock lock(&mutex_);
ABSL_ASSERT(peer && !peer_);
peer_ = std::move(peer);
}
IpczResult Activate(IpczHandle transport,
IpczTransportActivityHandler activity_handler) {
Ref<TransportWrapper> new_transport =
MakeRefCounted<TransportWrapper>(transport, activity_handler);
Ref<InProcessTransport> peer;
{
absl::MutexLock lock(&mutex_);
ABSL_ASSERT(!transport_);
if (!peer_closed_) {
transport_ = std::move(new_transport);
peer = peer_;
}
}
if (new_transport) {
// If the wrapper wasn't taken by this object, our peer has already been
// closed. Signal a transport error, as peer closure would have done.
new_transport->NotifyError();
return IPCZ_RESULT_OK;
}
// Let the peer know that it can now call into us directly. This may
// re-enter this InProcessTransport, as the peer will synchronously flush
// any queued transmissions before returning, and the logic handling the
// receipt of those transmissions may perform additional operations on this
// transport; all before OnPeerActivated() returns. We must therefore ensure
// `mutex_` is not held while calling this.
peer->OnPeerActivated();
return IPCZ_RESULT_OK;
}
void Deactivate() {
Ref<TransportWrapper> transport;
{
absl::MutexLock lock(&mutex_);
// NOTE: Dropping this reference may in turn lead to ipcz dropping its own
// last reference to `this`, so we must be careful not hold `mutex_` when
// that happens.
transport = std::move(transport_);
}
}
IpczResult Transmit(absl::Span<const uint8_t> data,
absl::Span<const IpczDriverHandle> handles) {
Ref<InProcessTransport> peer;
Ref<TransportWrapper> peer_transport;
{
absl::MutexLock lock(&mutex_);
if (!peer_active_) {
SavedMessage message;
message.data = std::vector<uint8_t>(data.begin(), data.end());
message.handles =
std::vector<IpczDriverHandle>(handles.begin(), handles.end());
saved_messages_.push_back(std::move(message));
return IPCZ_RESULT_OK;
}
peer = peer_;
ABSL_ASSERT(peer);
}
{
absl::MutexLock lock(&peer->mutex_);
peer_transport = peer->transport_;
}
if (peer_transport) {
// NOTE: Notifying the peer of anything may re-enter `this`, so we must be
// careful not hold `mutex_` while doing that.
peer_transport->Notify(data, handles);
} else {
CloseAllHandles(handles);
}
return IPCZ_RESULT_OK;
}
private:
void OnPeerActivated() {
for (;;) {
Ref<InProcessTransport> peer;
std::vector<SavedMessage> saved_messages;
{
absl::MutexLock lock(&mutex_);
ABSL_ASSERT(!peer_active_);
if (saved_messages_.empty()) {
peer_active_ = true;
return;
}
std::swap(saved_messages_, saved_messages);
saved_messages_.clear();
peer = peer_;
}
Ref<TransportWrapper> peer_transport;
{
absl::MutexLock lock(&peer->mutex_);
peer_transport = peer->transport_;
}
if (!peer_transport) {
// NOTE: We could have Close() retain the peer reference if there are
// queued messages, and only drop the reference after flushing them.
// This is not necessary in practice, but if we ever have test scenarios
// that may close one transport before activating its peer, that may be
// a useful thing to do.
return;
}
for (SavedMessage& m : saved_messages) {
peer_transport->Notify(m.data, m.handles);
}
}
}
void OnPeerClosed() {
Ref<TransportWrapper> transport;
{
absl::MutexLock lock(&mutex_);
transport = std::move(transport_);
peer_closed_ = true;
}
if (transport) {
// NOTE: Notifying ipcz of an error here may re-enter this
// InProcessTransport (e.g. to close it), so we must be careful not to
// hold `mutex_` while doing that.
transport->NotifyError();
}
}
absl::Mutex mutex_;
Ref<InProcessTransport> peer_ ABSL_GUARDED_BY(mutex_);
Ref<TransportWrapper> transport_ ABSL_GUARDED_BY(mutex_);
bool peer_active_ ABSL_GUARDED_BY(mutex_) = false;
bool peer_closed_ ABSL_GUARDED_BY(mutex_) = false;
std::vector<SavedMessage> saved_messages_ ABSL_GUARDED_BY(mutex_);
};
IpczResult IPCZ_API CreateTransports(IpczDriverHandle transport0,
IpczDriverHandle transport1,
uint32_t flags,
const void* options,
IpczDriverHandle* new_transport0,
IpczDriverHandle* new_transport1) {
auto first = MakeRefCounted<InProcessTransport>();
auto second = MakeRefCounted<InProcessTransport>();
first->SetPeer(second);
second->SetPeer(first);
*new_transport0 = Object::ReleaseAsHandle(std::move(first));
*new_transport1 = Object::ReleaseAsHandle(std::move(second));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API ActivateTransport(IpczDriverHandle transport,
IpczHandle listener,
IpczTransportActivityHandler handler,
uint32_t flags,
const void* options) {
return InProcessTransport::FromHandle(transport)->Activate(listener, handler);
}
IpczResult IPCZ_API DeactivateTransport(IpczDriverHandle transport,
uint32_t flags,
const void* options) {
InProcessTransport::FromHandle(transport)->Deactivate();
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API Transmit(IpczDriverHandle transport,
const void* data,
size_t num_bytes,
const IpczDriverHandle* handles,
size_t num_handles,
uint32_t flags,
const void* options) {
return InProcessTransport::FromHandle(transport)->Transmit(
absl::MakeSpan(static_cast<const uint8_t*>(data), num_bytes),
absl::MakeSpan(handles, num_handles));
}
} // namespace
const IpczDriver kSyncReferenceDriver = {
sizeof(kSyncReferenceDriver),
kSingleProcessReferenceDriverBase.Close,
kSingleProcessReferenceDriverBase.Serialize,
kSingleProcessReferenceDriverBase.Deserialize,
CreateTransports,
ActivateTransport,
DeactivateTransport,
Transmit,
kSingleProcessReferenceDriverBase.ReportBadTransportActivity,
kSingleProcessReferenceDriverBase.AllocateSharedMemory,
kSingleProcessReferenceDriverBase.GetSharedMemoryInfo,
kSingleProcessReferenceDriverBase.DuplicateSharedMemory,
kSingleProcessReferenceDriverBase.MapSharedMemory,
kSingleProcessReferenceDriverBase.GenerateRandomBytes,
};
} // namespace ipcz::reference_drivers