summaryrefslogtreecommitdiff
path: root/chromium/mojo/core/ipcz_driver/mojo_trap.cc
blob: f8e1e394f5d5e63e61e308b7dce2966402e52e54 (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2022 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 "mojo/core/ipcz_driver/mojo_trap.h"

#include <cstdint>
#include <tuple>
#include <utility>

#include "base/check_op.h"
#include "base/memory/ref_counted.h"
#include "base/notreached.h"
#include "mojo/core/ipcz_api.h"

namespace mojo::core::ipcz_driver {

namespace {

// Translates Mojo signal conditions to equivalent IpczTrapConditions.
void GetConditionsForSignals(MojoHandleSignals signals,
                             IpczTrapConditions* conditions) {
  conditions->flags |= IPCZ_TRAP_DEAD;

  if (signals & MOJO_HANDLE_SIGNAL_WRITABLE) {
    // TODO: Portals should be able to set default put limits that apply to all
    // of their put operations unless overridden. For now this hack effectively
    // gives all data pipes 2 MB of capacity.
    conditions->flags |= IPCZ_TRAP_BELOW_MAX_REMOTE_BYTES;
    conditions->max_remote_bytes = 2 * 1024 * 1024;
  }

  if (signals & MOJO_HANDLE_SIGNAL_READABLE) {
    // Mojo's readable signal is equivalent to the condition of having more than
    // zero parcels available to retrieve from a portal.
    conditions->flags |= IPCZ_TRAP_ABOVE_MIN_LOCAL_PARCELS;
    conditions->min_local_parcels = 0;
  } else if (signals & MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE) {
    // Data pipe consumers often use the edge-triggered NEW_DATA_READABLE
    // signal, which is effectively equivalent to NEW_LOCAL_PARCEL in ipcz.
    conditions->flags |= IPCZ_TRAP_NEW_LOCAL_PARCEL;
  }

  if (signals & MOJO_HANDLE_SIGNAL_PEER_CLOSED) {
    conditions->flags |= IPCZ_TRAP_PEER_CLOSED;
  }
}

// Given an ipcz trap event resulting from an installed trigger, this translates
// the event into an equivalent Mojo trap event for the containing Mojo trap.
void TranslateIpczToMojoEvent(MojoHandleSignals trigger_signals,
                              uintptr_t trigger_context,
                              IpczTrapConditionFlags current_condition_flags,
                              const IpczPortalStatus& current_status,
                              MojoTrapEvent* event) {
  event->flags = 0;
  event->trigger_context = trigger_context;

  // In practice handles are watched for one or the other of READABALE or
  // NEW_DATA_READABLE, but never both.
  const MojoHandleSignals kRead =
      (trigger_signals & MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE)
          ? MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE
          : MOJO_HANDLE_SIGNAL_READABLE;
  const MojoHandleSignals kWrite = MOJO_HANDLE_SIGNAL_WRITABLE;
  const MojoHandleSignals kPeerClosed = MOJO_HANDLE_SIGNAL_PEER_CLOSED;

  MojoHandleSignals& satisfied = event->signals_state.satisfied_signals;
  MojoHandleSignals& satisfiable = event->signals_state.satisfiable_signals;

  satisfied = 0;
  satisfiable = kPeerClosed | MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED |
                MOJO_HANDLE_SIGNAL_PEER_REMOTE;
  if (!(current_status.flags & IPCZ_PORTAL_STATUS_DEAD)) {
    satisfiable |= kRead | kPeerClosed;
  }
  if (current_status.flags & IPCZ_PORTAL_STATUS_PEER_CLOSED) {
    satisfied |= kPeerClosed;
  } else {
    satisfied |= kWrite;
    satisfiable |= kWrite;
  }

  if (current_status.num_local_parcels > 0) {
    satisfied |= kRead;
  }

  DCHECK((satisfied & satisfiable) == satisfied);
  if ((satisfiable & trigger_signals) == 0) {
    event->result = MOJO_RESULT_FAILED_PRECONDITION;
    return;
  }

  event->result = MOJO_RESULT_OK;
}

}  // namespace

// A Trigger is used as context for every trigger added to a Mojo trap. While a
// trap is armed, each of its Triggers has installed a unique ipcz trap to watch
// for its conditions.
struct MojoTrap::Trigger : public base::RefCountedThreadSafe<Trigger> {
  Trigger(scoped_refptr<MojoTrap> mojo_trap,
          MojoHandle handle,
          MojoHandleSignals signals,
          uintptr_t trigger_context)
      : mojo_trap(std::move(mojo_trap)),
        handle(handle),
        signals(signals),
        trigger_context(trigger_context) {}

  uintptr_t ipcz_context() const { return reinterpret_cast<uintptr_t>(this); }

  static Trigger& FromEvent(const IpczTrapEvent& event) {
    return *reinterpret_cast<Trigger*>(event.context);
  }

  const scoped_refptr<MojoTrap> mojo_trap;
  const MojoHandle handle;
  const MojoHandleSignals signals;
  const uintptr_t trigger_context;
  IpczTrapConditions conditions = {.size = sizeof(conditions), .flags = 0};

  // Access is effectively guarded by the owning MojoTrap's `lock_`.
  bool armed = false;
  bool removed = false;

 private:
  friend class base::RefCountedThreadSafe<Trigger>;

  ~Trigger() = default;
};

MojoTrap::MojoTrap(MojoTrapEventHandler handler) : handler_(handler) {}

MojoTrap::~MojoTrap() = default;

MojoResult MojoTrap::AddTrigger(MojoHandle handle,
                                MojoHandleSignals signals,
                                MojoTriggerCondition condition,
                                uintptr_t trigger_context) {
  if (!handle) {
    return MOJO_RESULT_INVALID_ARGUMENT;
  }

  auto trigger =
      base::MakeRefCounted<Trigger>(this, handle, signals, trigger_context);

  if (condition == MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED) {
    // There's only one user of MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED. It's
    // used for peer remoteness tracking in Mojo bindings lazy serialization.
    // That is effectively a dead feature, so we don't need to support watching
    // for unsatisfied signals.
    trigger->conditions.flags = IPCZ_NO_FLAGS;
  } else {
    GetConditionsForSignals(signals, &trigger->conditions);
  }

  IpczTrapConditionFlags flags;
  IpczPortalStatus status = {sizeof(status)};
  {
    base::AutoLock lock(lock_);
    auto [it, ok] = triggers_.try_emplace(trigger_context, trigger);
    if (!ok) {
      return MOJO_RESULT_ALREADY_EXISTS;
    }

    next_trigger_ = triggers_.begin();

    // Install an ipcz trap to effectively monitor the lifetime of the watched
    // object referenced by `handle`. Installation of the trap should always
    // succeed, and its resulting trap event will always mark the end of this
    // trigger's lifetime. This trap effectively owns a ref to the Trigger, as
    // added here.
    trigger->AddRef();
    IpczTrapConditions removal_conditions = {
        .size = sizeof(removal_conditions),
        .flags = IPCZ_TRAP_REMOVED,
    };
    IpczResult result = GetIpczAPI().Trap(
        handle, &removal_conditions, &TrapRemovalEventHandler,
        trigger->ipcz_context(), IPCZ_NO_FLAGS, nullptr, nullptr, nullptr);
    CHECK_EQ(result, IPCZ_RESULT_OK);

    if (!armed_) {
      return MOJO_RESULT_OK;
    }

    // The Mojo trap is already armed, so attempt to install an ipcz trap for
    // the new trigger immediately.
    result = ArmTrigger(*trigger, &flags, &status);
    if (result == IPCZ_RESULT_OK) {
      return MOJO_RESULT_OK;
    }

    // The new trigger already needs to fire an event. OK.
    armed_ = false;
  }

  MojoTrapEvent event = {.struct_size = sizeof(event)};
  TranslateIpczToMojoEvent(signals, trigger_context, flags, status, &event);
  event.flags = MOJO_TRAP_EVENT_FLAG_WITHIN_API_CALL;
  handler_(&event);
  return MOJO_RESULT_OK;
}

MojoResult MojoTrap::RemoveTrigger(uintptr_t trigger_context) {
  scoped_refptr<Trigger> trigger;
  {
    base::AutoLock lock(lock_);
    auto it = triggers_.find(trigger_context);
    if (it == triggers_.end()) {
      return MOJO_RESULT_NOT_FOUND;
    }
    trigger = std::move(it->second);
    trigger->armed = false;
    trigger->removed = true;
    triggers_.erase(it);
    next_trigger_ = triggers_.begin();
  }

  NotifyTriggerRemoved(*trigger);
  return MOJO_RESULT_OK;
}

MojoResult MojoTrap::Arm(MojoTrapEvent* blocking_events,
                         uint32_t* num_blocking_events) {
  const uint32_t event_capacity =
      num_blocking_events ? *num_blocking_events : 0;
  if (event_capacity > 0 && !blocking_events) {
    return MOJO_RESULT_INVALID_ARGUMENT;
  }

  if (event_capacity > 0 &&
      blocking_events[0].struct_size < sizeof(blocking_events[0])) {
    return MOJO_RESULT_INVALID_ARGUMENT;
  }

  base::AutoLock lock(lock_);
  if (armed_) {
    return MOJO_RESULT_OK;
  }

  if (triggers_.empty()) {
    return MOJO_RESULT_NOT_FOUND;
  }

  uint32_t num_events_returned = 0;
  IpczTrapConditionFlags flags;
  IpczPortalStatus status = {sizeof(status)};

  auto increment_wrapped = [this](TriggerMap::iterator it) {
    lock_.AssertAcquired();
    if (++it != triggers_.end()) {
      return it;
    }
    return triggers_.begin();
  };

  TriggerMap::iterator next_trigger = next_trigger_;
  DCHECK(next_trigger != triggers_.end());

  // We iterate over all triggers, starting just beyond wherever we started last
  // time we were armed. This guards against any single trigger being starved.
  const TriggerMap::iterator end_trigger = next_trigger;
  do {
    auto& [trigger_context, trigger] = *next_trigger;
    next_trigger = increment_wrapped(next_trigger);

    const IpczResult result = ArmTrigger(*trigger, &flags, &status);
    if (result == IPCZ_RESULT_OK) {
      // Trap successfully installed, nothing else to do for this trigger.
      continue;
    }

    if (result != IPCZ_RESULT_FAILED_PRECONDITION) {
      NOTREACHED();
      return result;
    }

    // The ipcz trap failed to install, so this trigger's conditions are already
    // met. Accumulate would-be event details if there's output space.
    if (event_capacity == 0) {
      return MOJO_RESULT_FAILED_PRECONDITION;
    }

    TranslateIpczToMojoEvent(trigger->signals, trigger->trigger_context, flags,
                             status, &blocking_events[num_events_returned++]);
  } while (next_trigger != end_trigger && num_events_returned < event_capacity);

  if (next_trigger != end_trigger) {
    next_trigger_ = next_trigger;
  } else {
    next_trigger_ = increment_wrapped(next_trigger);
  }

  if (num_events_returned > 0) {
    *num_blocking_events = num_events_returned;
    return MOJO_RESULT_FAILED_PRECONDITION;
  }

  // The whole Mojo trap is collectively armed if and only if all of the
  // triggers managed to install an ipcz trap.
  armed_ = true;
  return MOJO_RESULT_OK;
}

void MojoTrap::Close() {
  TriggerMap triggers;
  {
    // Effectively disable all triggers. A disabled trigger may have already
    // installed an ipcz trap which hasn't yet fired an event. This ensures that
    // if any such event does eventually fire, it will be ignored.
    base::AutoLock lock(lock_);
    std::swap(triggers, triggers_);
    next_trigger_ = triggers_.begin();
    for (auto& [trigger_context, trigger] : triggers) {
      trigger->armed = false;

      DCHECK(!trigger->removed);
      trigger->removed = true;
    }
  }

  for (auto& [trigger_context, trigger] : triggers) {
    NotifyTriggerRemoved(*trigger);
  }
}

// static
void MojoTrap::TrapEventHandler(const IpczTrapEvent* event) {
  Trigger::FromEvent(*event).mojo_trap->HandleEvent(*event);
}

// static
void MojoTrap::TrapRemovalEventHandler(const IpczTrapEvent* event) {
  Trigger& trigger = Trigger::FromEvent(*event);
  trigger.mojo_trap->HandleTrapRemoved(*event);

  // Balanced by AddRef when installing the trigger's removal ipcz trap.
  trigger.Release();
}

void MojoTrap::HandleEvent(const IpczTrapEvent& event) {
  Trigger& trigger = Trigger::FromEvent(event);
  {
    base::AutoLock lock(lock_);
    const bool trigger_active = armed_ && trigger.armed && !trigger.removed;
    const bool is_removal = (event.condition_flags & IPCZ_TRAP_REMOVED) != 0;
    trigger.armed = false;
    if (!trigger_active || is_removal) {
      // Removal events are handled separately by ipcz traps established at
      // trigger creation, allowing handle closure to trigger an event even when
      // the Mojo trap isn't armed.
      return;
    }

    armed_ = false;
  }

  MojoTrapEvent mojo_event = {.struct_size = sizeof(mojo_event)};
  TranslateIpczToMojoEvent(trigger.signals, trigger.trigger_context,
                           event.condition_flags, *event.status, &mojo_event);
  mojo_event.flags |= MOJO_TRAP_EVENT_FLAG_WITHIN_API_CALL;

  // Balanced by AddRef when installing the trigger's ipcz trap.
  trigger.Release();

  handler_(&mojo_event);
}

void MojoTrap::HandleTrapRemoved(const IpczTrapEvent& event) {
  Trigger& trigger = Trigger::FromEvent(event);
  {
    base::AutoLock lock(lock_);
    if (trigger.removed) {
      // The Mojo trap may have already been closed, in which case this trigger
      // was already removed and its handler was already notified.
      return;
    }

    triggers_.erase(trigger.trigger_context);
    trigger.removed = true;
    next_trigger_ = triggers_.begin();
  }

  NotifyTriggerRemoved(trigger);
}

IpczResult MojoTrap::ArmTrigger(Trigger& trigger,
                                IpczTrapConditionFlags* satisfied_flags,
                                IpczPortalStatus* status) {
  lock_.AssertAcquired();
  if (trigger.armed) {
    return IPCZ_RESULT_OK;
  }

  // Bump the ref count on the Trigger. This ref is effectively owned by the
  // installed trap, if it's installed successfully.
  trigger.AddRef();
  IpczResult result = GetIpczAPI().Trap(
      trigger.handle, &trigger.conditions, &TrapEventHandler,
      trigger.ipcz_context(), IPCZ_NO_FLAGS, nullptr, satisfied_flags, status);
  if (result == IPCZ_RESULT_OK) {
    trigger.armed = true;
  } else {
    // Balances the AddRef above, since no trap was installed.
    trigger.Release();
  }

  return result;
}

void MojoTrap::NotifyTriggerRemoved(Trigger& trigger) {
  MojoTrapEvent mojo_event = {
      .struct_size = sizeof(mojo_event),
      .flags = MOJO_TRAP_EVENT_FLAG_WITHIN_API_CALL,
      .trigger_context = trigger.trigger_context,
      .result = MOJO_RESULT_CANCELLED,
      .signals_state = {.satisfied_signals = 0, .satisfiable_signals = 0},
  };
  handler_(&mojo_event);
}

}  // namespace mojo::core::ipcz_driver