| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(vtl): I currently potentially overflow in doing index calculations. | 5 // TODO(vtl): I currently potentially overflow in doing index calculations. |
| 6 // E.g., |start_index_| and |current_num_bytes_| fit into a |uint32_t|, but | 6 // E.g., |start_index_| and |current_num_bytes_| fit into a |uint32_t|, but |
| 7 // their sum may not. This is bad and poses a security risk. (We're currently | 7 // their sum may not. This is bad and poses a security risk. (We're currently |
| 8 // saved by the limit on capacity -- the maximum size of the buffer, checked in | 8 // saved by the limit on capacity -- the maximum size of the buffer, checked in |
| 9 // |DataPipe::ValidateOptions()|, is currently sufficiently small.) | 9 // |DataPipe::ValidateOptions()|, is currently sufficiently small.) |
| 10 | 10 |
| 11 #include "mojo/edk/system/local_data_pipe.h" | 11 #include "mojo/edk/system/local_data_pipe.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 | 16 |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "mojo/edk/system/configuration.h" | 18 #include "mojo/edk/system/constants.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace system { | 21 namespace system { |
| 22 | 22 |
| 23 LocalDataPipe::LocalDataPipe(const MojoCreateDataPipeOptions& options) | 23 LocalDataPipe::LocalDataPipe(const MojoCreateDataPipeOptions& options) |
| 24 : DataPipe(true, true, options), start_index_(0), current_num_bytes_(0) { | 24 : DataPipe(true, true, options), start_index_(0), current_num_bytes_(0) { |
| 25 // Note: |buffer_| is lazily allocated, since a common case will be that one | 25 // Note: |buffer_| is lazily allocated, since a common case will be that one |
| 26 // of the handles is immediately passed off to another process. | 26 // of the handles is immediately passed off to another process. |
| 27 } | 27 } |
| 28 | 28 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE; | 294 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE; |
| 295 } | 295 } |
| 296 return rv; | 296 return rv; |
| 297 } | 297 } |
| 298 | 298 |
| 299 void LocalDataPipe::EnsureBufferNoLock() { | 299 void LocalDataPipe::EnsureBufferNoLock() { |
| 300 DCHECK(producer_open_no_lock()); | 300 DCHECK(producer_open_no_lock()); |
| 301 if (buffer_) | 301 if (buffer_) |
| 302 return; | 302 return; |
| 303 buffer_.reset(static_cast<char*>( | 303 buffer_.reset(static_cast<char*>( |
| 304 base::AlignedAlloc(capacity_num_bytes(), | 304 base::AlignedAlloc(capacity_num_bytes(), kDataPipeBufferAlignmentBytes))); |
| 305 GetConfiguration().data_pipe_buffer_alignment_bytes))); | |
| 306 } | 305 } |
| 307 | 306 |
| 308 void LocalDataPipe::DestroyBufferNoLock() { | 307 void LocalDataPipe::DestroyBufferNoLock() { |
| 309 #ifndef NDEBUG | 308 #ifndef NDEBUG |
| 310 // Scribble on the buffer to help detect use-after-frees. (This also helps the | 309 // Scribble on the buffer to help detect use-after-frees. (This also helps the |
| 311 // unit test detect certain bugs without needing ASAN or similar.) | 310 // unit test detect certain bugs without needing ASAN or similar.) |
| 312 if (buffer_) | 311 if (buffer_) |
| 313 memset(buffer_.get(), 0xcd, capacity_num_bytes()); | 312 memset(buffer_.get(), 0xcd, capacity_num_bytes()); |
| 314 #endif | 313 #endif |
| 315 buffer_.reset(); | 314 buffer_.reset(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 335 | 334 |
| 336 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { | 335 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { |
| 337 DCHECK_LE(num_bytes, current_num_bytes_); | 336 DCHECK_LE(num_bytes, current_num_bytes_); |
| 338 start_index_ += num_bytes; | 337 start_index_ += num_bytes; |
| 339 start_index_ %= capacity_num_bytes(); | 338 start_index_ %= capacity_num_bytes(); |
| 340 current_num_bytes_ -= num_bytes; | 339 current_num_bytes_ -= num_bytes; |
| 341 } | 340 } |
| 342 | 341 |
| 343 } // namespace system | 342 } // namespace system |
| 344 } // namespace mojo | 343 } // namespace mojo |
| OLD | NEW |