| 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 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 100 } |
| 101 | 101 |
| 102 current_num_bytes_ += num_bytes_to_write; | 102 current_num_bytes_ += num_bytes_to_write; |
| 103 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); | 103 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); |
| 104 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write)); | 104 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write)); |
| 105 return MOJO_RESULT_OK; | 105 return MOJO_RESULT_OK; |
| 106 } | 106 } |
| 107 | 107 |
| 108 MojoResult LocalDataPipeImpl::ProducerBeginWriteData( | 108 MojoResult LocalDataPipeImpl::ProducerBeginWriteData( |
| 109 UserPointer<void*> buffer, | 109 UserPointer<void*> buffer, |
| 110 UserPointer<uint32_t> buffer_num_bytes, | 110 UserPointer<uint32_t> buffer_num_bytes) { |
| 111 uint32_t min_num_bytes_to_write) { | |
| 112 DCHECK(consumer_open()); | 111 DCHECK(consumer_open()); |
| 113 | 112 |
| 114 // The index we need to start writing at. | 113 // The index we need to start writing at. |
| 115 size_t write_index = | 114 size_t write_index = |
| 116 (start_index_ + current_num_bytes_) % capacity_num_bytes(); | 115 (start_index_ + current_num_bytes_) % capacity_num_bytes(); |
| 117 | 116 |
| 118 size_t max_num_bytes_to_write = GetMaxNumBytesToWrite(); | 117 size_t max_num_bytes_to_write = GetMaxNumBytesToWrite(); |
| 119 if (min_num_bytes_to_write > max_num_bytes_to_write) { | |
| 120 // Don't return "should wait" since you can't wait for a specified amount | |
| 121 // of data. | |
| 122 return MOJO_RESULT_OUT_OF_RANGE; | |
| 123 } | |
| 124 | |
| 125 // Don't go into a two-phase write if there's no room. | 118 // Don't go into a two-phase write if there's no room. |
| 126 if (max_num_bytes_to_write == 0) | 119 if (max_num_bytes_to_write == 0) |
| 127 return MOJO_RESULT_SHOULD_WAIT; | 120 return MOJO_RESULT_SHOULD_WAIT; |
| 128 | 121 |
| 129 EnsureBuffer(); | 122 EnsureBuffer(); |
| 130 buffer.Put(buffer_.get() + write_index); | 123 buffer.Put(buffer_.get() + write_index); |
| 131 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_write)); | 124 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_write)); |
| 132 set_producer_two_phase_max_num_bytes_written( | 125 set_producer_two_phase_max_num_bytes_written( |
| 133 static_cast<uint32_t>(max_num_bytes_to_write)); | 126 static_cast<uint32_t>(max_num_bytes_to_write)); |
| 134 return MOJO_RESULT_OK; | 127 return MOJO_RESULT_OK; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 | 275 |
| 283 MojoResult LocalDataPipeImpl::ConsumerQueryData( | 276 MojoResult LocalDataPipeImpl::ConsumerQueryData( |
| 284 UserPointer<uint32_t> num_bytes) { | 277 UserPointer<uint32_t> num_bytes) { |
| 285 // Note: This cast is safe, since the capacity fits into a |uint32_t|. | 278 // Note: This cast is safe, since the capacity fits into a |uint32_t|. |
| 286 num_bytes.Put(static_cast<uint32_t>(current_num_bytes_)); | 279 num_bytes.Put(static_cast<uint32_t>(current_num_bytes_)); |
| 287 return MOJO_RESULT_OK; | 280 return MOJO_RESULT_OK; |
| 288 } | 281 } |
| 289 | 282 |
| 290 MojoResult LocalDataPipeImpl::ConsumerBeginReadData( | 283 MojoResult LocalDataPipeImpl::ConsumerBeginReadData( |
| 291 UserPointer<const void*> buffer, | 284 UserPointer<const void*> buffer, |
| 292 UserPointer<uint32_t> buffer_num_bytes, | 285 UserPointer<uint32_t> buffer_num_bytes) { |
| 293 uint32_t min_num_bytes_to_read) { | |
| 294 size_t max_num_bytes_to_read = GetMaxNumBytesToRead(); | 286 size_t max_num_bytes_to_read = GetMaxNumBytesToRead(); |
| 295 if (min_num_bytes_to_read > max_num_bytes_to_read) { | |
| 296 // Don't return "should wait" since you can't wait for a specified amount of | |
| 297 // data. | |
| 298 return producer_open() ? MOJO_RESULT_OUT_OF_RANGE | |
| 299 : MOJO_RESULT_FAILED_PRECONDITION; | |
| 300 } | |
| 301 | |
| 302 // Don't go into a two-phase read if there's no data. | 287 // Don't go into a two-phase read if there's no data. |
| 303 if (max_num_bytes_to_read == 0) { | 288 if (max_num_bytes_to_read == 0) { |
| 304 return producer_open() ? MOJO_RESULT_SHOULD_WAIT | 289 return producer_open() ? MOJO_RESULT_SHOULD_WAIT |
| 305 : MOJO_RESULT_FAILED_PRECONDITION; | 290 : MOJO_RESULT_FAILED_PRECONDITION; |
| 306 } | 291 } |
| 307 | 292 |
| 308 buffer.Put(buffer_.get() + start_index_); | 293 buffer.Put(buffer_.get() + start_index_); |
| 309 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_read)); | 294 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_read)); |
| 310 set_consumer_two_phase_max_num_bytes_read( | 295 set_consumer_two_phase_max_num_bytes_read( |
| 311 static_cast<uint32_t>(max_num_bytes_to_read)); | 296 static_cast<uint32_t>(max_num_bytes_to_read)); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 | 427 |
| 443 void LocalDataPipeImpl::MarkDataAsConsumed(size_t num_bytes) { | 428 void LocalDataPipeImpl::MarkDataAsConsumed(size_t num_bytes) { |
| 444 DCHECK_LE(num_bytes, current_num_bytes_); | 429 DCHECK_LE(num_bytes, current_num_bytes_); |
| 445 start_index_ += num_bytes; | 430 start_index_ += num_bytes; |
| 446 start_index_ %= capacity_num_bytes(); | 431 start_index_ %= capacity_num_bytes(); |
| 447 current_num_bytes_ -= num_bytes; | 432 current_num_bytes_ -= num_bytes; |
| 448 } | 433 } |
| 449 | 434 |
| 450 } // namespace system | 435 } // namespace system |
| 451 } // namespace mojo | 436 } // namespace mojo |
| OLD | NEW |