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
|
// Copyright 2013 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.
// TODO(vtl): I currently potentially overflow in doing index calculations.
// E.g., |start_index_| and |current_num_bytes_| fit into a |uint32_t|, but
// their sum may not. This is bad and poses a security risk. (We're currently
// saved by the limit on capacity -- the maximum size of the buffer, checked in
// |DataPipe::ValidateOptions()|, is currently sufficiently small.)
#include "mojo/edk/system/local_data_pipe.h"
#include <string.h>
#include <algorithm>
#include "base/logging.h"
#include "mojo/edk/system/configuration.h"
namespace mojo {
namespace system {
LocalDataPipe::LocalDataPipe(const MojoCreateDataPipeOptions& options)
: DataPipe(true, true, options), start_index_(0), current_num_bytes_(0) {
// Note: |buffer_| is lazily allocated, since a common case will be that one
// of the handles is immediately passed off to another process.
}
LocalDataPipe::~LocalDataPipe() {
}
void LocalDataPipe::ProducerCloseImplNoLock() {
// If the consumer is still open and we still have data, we have to keep the
// buffer around. Currently, we won't free it even if it empties later. (We
// could do this -- requiring a check on every read -- but that seems to be
// optimizing for the uncommon case.)
if (!consumer_open_no_lock() || !current_num_bytes_) {
// Note: There can only be a two-phase *read* (by the consumer) if we still
// have data.
DCHECK(!consumer_in_two_phase_read_no_lock());
DestroyBufferNoLock();
}
}
MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(
UserPointer<const void> elements,
UserPointer<uint32_t> num_bytes,
uint32_t max_num_bytes_to_write,
uint32_t min_num_bytes_to_write) {
DCHECK_EQ(max_num_bytes_to_write % element_num_bytes(), 0u);
DCHECK_EQ(min_num_bytes_to_write % element_num_bytes(), 0u);
DCHECK_GT(max_num_bytes_to_write, 0u);
DCHECK(consumer_open_no_lock());
size_t num_bytes_to_write = 0;
if (may_discard()) {
if (min_num_bytes_to_write > capacity_num_bytes())
return MOJO_RESULT_OUT_OF_RANGE;
num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write),
capacity_num_bytes());
if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
// Discard as much as needed (discard oldest first).
MarkDataAsConsumedNoLock(num_bytes_to_write -
(capacity_num_bytes() - current_num_bytes_));
// No need to wake up write waiters, since we're definitely going to leave
// the buffer full.
}
} else {
if (min_num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
// Don't return "should wait" since you can't wait for a specified amount
// of data.
return MOJO_RESULT_OUT_OF_RANGE;
}
num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write),
capacity_num_bytes() - current_num_bytes_);
}
if (num_bytes_to_write == 0)
return MOJO_RESULT_SHOULD_WAIT;
// The amount we can write in our first |memcpy()|.
size_t num_bytes_to_write_first =
std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock());
// Do the first (and possibly only) |memcpy()|.
size_t first_write_index =
(start_index_ + current_num_bytes_) % capacity_num_bytes();
EnsureBufferNoLock();
elements.GetArray(buffer_.get() + first_write_index,
num_bytes_to_write_first);
if (num_bytes_to_write_first < num_bytes_to_write) {
// The "second write index" is zero.
elements.At(num_bytes_to_write_first)
.GetArray(buffer_.get(), num_bytes_to_write - num_bytes_to_write_first);
}
current_num_bytes_ += num_bytes_to_write;
DCHECK_LE(current_num_bytes_, capacity_num_bytes());
num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ProducerBeginWriteDataImplNoLock(
UserPointer<void*> buffer,
UserPointer<uint32_t> buffer_num_bytes,
uint32_t min_num_bytes_to_write) {
DCHECK(consumer_open_no_lock());
// The index we need to start writing at.
size_t write_index =
(start_index_ + current_num_bytes_) % capacity_num_bytes();
size_t max_num_bytes_to_write = GetMaxNumBytesToWriteNoLock();
if (min_num_bytes_to_write > max_num_bytes_to_write) {
// In "may discard" mode, we can always write from the write index to the
// end of the buffer.
if (may_discard() &&
min_num_bytes_to_write <= capacity_num_bytes() - write_index) {
// To do so, we need to discard an appropriate amount of data.
// We should only reach here if the start index is after the write index!
DCHECK_GE(start_index_, write_index);
DCHECK_GT(min_num_bytes_to_write - max_num_bytes_to_write, 0u);
MarkDataAsConsumedNoLock(min_num_bytes_to_write - max_num_bytes_to_write);
max_num_bytes_to_write = min_num_bytes_to_write;
} else {
// Don't return "should wait" since you can't wait for a specified amount
// of data.
return MOJO_RESULT_OUT_OF_RANGE;
}
}
// Don't go into a two-phase write if there's no room.
if (max_num_bytes_to_write == 0)
return MOJO_RESULT_SHOULD_WAIT;
EnsureBufferNoLock();
buffer.Put(buffer_.get() + write_index);
buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_write));
set_producer_two_phase_max_num_bytes_written_no_lock(
static_cast<uint32_t>(max_num_bytes_to_write));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ProducerEndWriteDataImplNoLock(
uint32_t num_bytes_written) {
DCHECK_LE(num_bytes_written,
producer_two_phase_max_num_bytes_written_no_lock());
current_num_bytes_ += num_bytes_written;
DCHECK_LE(current_num_bytes_, capacity_num_bytes());
set_producer_two_phase_max_num_bytes_written_no_lock(0);
return MOJO_RESULT_OK;
}
HandleSignalsState LocalDataPipe::ProducerGetHandleSignalsStateImplNoLock()
const {
HandleSignalsState rv;
if (consumer_open_no_lock()) {
if ((may_discard() || current_num_bytes_ < capacity_num_bytes()) &&
!producer_in_two_phase_write_no_lock())
rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_WRITABLE;
rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_WRITABLE;
} else {
rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
}
rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
return rv;
}
void LocalDataPipe::ConsumerCloseImplNoLock() {
// If the producer is around and in a two-phase write, we have to keep the
// buffer around. (We then don't free it until the producer is closed. This
// could be rectified, but again seems like optimizing for the uncommon case.)
if (!producer_open_no_lock() || !producer_in_two_phase_write_no_lock())
DestroyBufferNoLock();
current_num_bytes_ = 0;
}
MojoResult LocalDataPipe::ConsumerReadDataImplNoLock(
UserPointer<void> elements,
UserPointer<uint32_t> num_bytes,
uint32_t max_num_bytes_to_read,
uint32_t min_num_bytes_to_read,
bool peek) {
DCHECK_EQ(max_num_bytes_to_read % element_num_bytes(), 0u);
DCHECK_EQ(min_num_bytes_to_read % element_num_bytes(), 0u);
DCHECK_GT(max_num_bytes_to_read, 0u);
if (min_num_bytes_to_read > current_num_bytes_) {
// Don't return "should wait" since you can't wait for a specified amount of
// data.
return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE
: MOJO_RESULT_FAILED_PRECONDITION;
}
size_t num_bytes_to_read =
std::min(static_cast<size_t>(max_num_bytes_to_read), current_num_bytes_);
if (num_bytes_to_read == 0) {
return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT
: MOJO_RESULT_FAILED_PRECONDITION;
}
// The amount we can read in our first |memcpy()|.
size_t num_bytes_to_read_first =
std::min(num_bytes_to_read, GetMaxNumBytesToReadNoLock());
elements.PutArray(buffer_.get() + start_index_, num_bytes_to_read_first);
if (num_bytes_to_read_first < num_bytes_to_read) {
// The "second read index" is zero.
elements.At(num_bytes_to_read_first)
.PutArray(buffer_.get(), num_bytes_to_read - num_bytes_to_read_first);
}
if (!peek)
MarkDataAsConsumedNoLock(num_bytes_to_read);
num_bytes.Put(static_cast<uint32_t>(num_bytes_to_read));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ConsumerDiscardDataImplNoLock(
UserPointer<uint32_t> num_bytes,
uint32_t max_num_bytes_to_discard,
uint32_t min_num_bytes_to_discard) {
DCHECK_EQ(max_num_bytes_to_discard % element_num_bytes(), 0u);
DCHECK_EQ(min_num_bytes_to_discard % element_num_bytes(), 0u);
DCHECK_GT(max_num_bytes_to_discard, 0u);
if (min_num_bytes_to_discard > current_num_bytes_) {
// Don't return "should wait" since you can't wait for a specified amount of
// data.
return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE
: MOJO_RESULT_FAILED_PRECONDITION;
}
// Be consistent with other operations; error if no data available.
if (current_num_bytes_ == 0) {
return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT
: MOJO_RESULT_FAILED_PRECONDITION;
}
size_t num_bytes_to_discard = std::min(
static_cast<size_t>(max_num_bytes_to_discard), current_num_bytes_);
MarkDataAsConsumedNoLock(num_bytes_to_discard);
num_bytes.Put(static_cast<uint32_t>(num_bytes_to_discard));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ConsumerQueryDataImplNoLock(
UserPointer<uint32_t> num_bytes) {
// Note: This cast is safe, since the capacity fits into a |uint32_t|.
num_bytes.Put(static_cast<uint32_t>(current_num_bytes_));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ConsumerBeginReadDataImplNoLock(
UserPointer<const void*> buffer,
UserPointer<uint32_t> buffer_num_bytes,
uint32_t min_num_bytes_to_read) {
size_t max_num_bytes_to_read = GetMaxNumBytesToReadNoLock();
if (min_num_bytes_to_read > max_num_bytes_to_read) {
// Don't return "should wait" since you can't wait for a specified amount of
// data.
return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE
: MOJO_RESULT_FAILED_PRECONDITION;
}
// Don't go into a two-phase read if there's no data.
if (max_num_bytes_to_read == 0) {
return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT
: MOJO_RESULT_FAILED_PRECONDITION;
}
buffer.Put(buffer_.get() + start_index_);
buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_read));
set_consumer_two_phase_max_num_bytes_read_no_lock(
static_cast<uint32_t>(max_num_bytes_to_read));
return MOJO_RESULT_OK;
}
MojoResult LocalDataPipe::ConsumerEndReadDataImplNoLock(
uint32_t num_bytes_read) {
DCHECK_LE(num_bytes_read, consumer_two_phase_max_num_bytes_read_no_lock());
DCHECK_LE(start_index_ + num_bytes_read, capacity_num_bytes());
MarkDataAsConsumedNoLock(num_bytes_read);
set_consumer_two_phase_max_num_bytes_read_no_lock(0);
return MOJO_RESULT_OK;
}
HandleSignalsState LocalDataPipe::ConsumerGetHandleSignalsStateImplNoLock()
const {
HandleSignalsState rv;
if (current_num_bytes_ > 0) {
if (!consumer_in_two_phase_read_no_lock())
rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_READABLE;
rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE;
} else if (producer_open_no_lock()) {
rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE;
}
if (!producer_open_no_lock())
rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
return rv;
}
void LocalDataPipe::EnsureBufferNoLock() {
DCHECK(producer_open_no_lock());
if (buffer_)
return;
buffer_.reset(static_cast<char*>(
base::AlignedAlloc(capacity_num_bytes(),
GetConfiguration().data_pipe_buffer_alignment_bytes)));
}
void LocalDataPipe::DestroyBufferNoLock() {
#ifndef NDEBUG
// Scribble on the buffer to help detect use-after-frees. (This also helps the
// unit test detect certain bugs without needing ASAN or similar.)
if (buffer_)
memset(buffer_.get(), 0xcd, capacity_num_bytes());
#endif
buffer_.reset();
}
size_t LocalDataPipe::GetMaxNumBytesToWriteNoLock() {
size_t next_index = start_index_ + current_num_bytes_;
if (next_index >= capacity_num_bytes()) {
next_index %= capacity_num_bytes();
DCHECK_GE(start_index_, next_index);
DCHECK_EQ(start_index_ - next_index,
capacity_num_bytes() - current_num_bytes_);
return start_index_ - next_index;
}
return capacity_num_bytes() - next_index;
}
size_t LocalDataPipe::GetMaxNumBytesToReadNoLock() {
if (start_index_ + current_num_bytes_ > capacity_num_bytes())
return capacity_num_bytes() - start_index_;
return current_num_bytes_;
}
void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) {
DCHECK_LE(num_bytes, current_num_bytes_);
start_index_ += num_bytes;
start_index_ %= capacity_num_bytes();
current_num_bytes_ -= num_bytes;
}
} // namespace system
} // namespace mojo
|