| 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 26 matching lines...) Expand all Loading... |
| 37 // could do this -- requiring a check on every read -- but that seems to be | 37 // could do this -- requiring a check on every read -- but that seems to be |
| 38 // optimizing for the uncommon case.) | 38 // optimizing for the uncommon case.) |
| 39 if (!consumer_open_no_lock() || !current_num_bytes_) { | 39 if (!consumer_open_no_lock() || !current_num_bytes_) { |
| 40 // Note: There can only be a two-phase *read* (by the consumer) if we still | 40 // Note: There can only be a two-phase *read* (by the consumer) if we still |
| 41 // have data. | 41 // have data. |
| 42 DCHECK(!consumer_in_two_phase_read_no_lock()); | 42 DCHECK(!consumer_in_two_phase_read_no_lock()); |
| 43 DestroyBufferNoLock(); | 43 DestroyBufferNoLock(); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(const void* elements, | 47 MojoResult LocalDataPipe::ProducerWriteDataImplNoLock( |
| 48 uint32_t* num_bytes, | 48 UserPointer<const void> elements, |
| 49 bool all_or_none) { | 49 UserPointer<uint32_t> num_bytes, |
| 50 DCHECK_EQ(*num_bytes % element_num_bytes(), 0u); | 50 uint32_t max_num_bytes_to_write, |
| 51 DCHECK_GT(*num_bytes, 0u); | 51 uint32_t min_num_bytes_to_write) { |
| 52 DCHECK_EQ(max_num_bytes_to_write % element_num_bytes(), 0u); |
| 53 DCHECK_EQ(min_num_bytes_to_write % element_num_bytes(), 0u); |
| 54 DCHECK_GT(max_num_bytes_to_write, 0u); |
| 52 DCHECK(consumer_open_no_lock()); | 55 DCHECK(consumer_open_no_lock()); |
| 53 | 56 |
| 54 size_t num_bytes_to_write = 0; | 57 size_t num_bytes_to_write = 0; |
| 55 if (may_discard()) { | 58 if (may_discard()) { |
| 56 if (all_or_none && *num_bytes > capacity_num_bytes()) | 59 if (min_num_bytes_to_write > capacity_num_bytes()) |
| 57 return MOJO_RESULT_OUT_OF_RANGE; | 60 return MOJO_RESULT_OUT_OF_RANGE; |
| 58 | 61 |
| 59 num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes), | 62 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write), |
| 60 capacity_num_bytes()); | 63 capacity_num_bytes()); |
| 61 if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { | 64 if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { |
| 62 // Discard as much as needed (discard oldest first). | 65 // Discard as much as needed (discard oldest first). |
| 63 MarkDataAsConsumedNoLock( | 66 MarkDataAsConsumedNoLock( |
| 64 num_bytes_to_write - (capacity_num_bytes() - current_num_bytes_)); | 67 num_bytes_to_write - (capacity_num_bytes() - current_num_bytes_)); |
| 65 // No need to wake up write waiters, since we're definitely going to leave | 68 // No need to wake up write waiters, since we're definitely going to leave |
| 66 // the buffer full. | 69 // the buffer full. |
| 67 } | 70 } |
| 68 } else { | 71 } else { |
| 69 if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) { | 72 if (min_num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { |
| 70 // Don't return "should wait" since you can't wait for a specified amount | 73 // Don't return "should wait" since you can't wait for a specified amount |
| 71 // of data. | 74 // of data. |
| 72 return MOJO_RESULT_OUT_OF_RANGE; | 75 return MOJO_RESULT_OUT_OF_RANGE; |
| 73 } | 76 } |
| 74 | 77 |
| 75 num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes), | 78 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write), |
| 76 capacity_num_bytes() - current_num_bytes_); | 79 capacity_num_bytes() - current_num_bytes_); |
| 77 } | 80 } |
| 78 if (num_bytes_to_write == 0) | 81 if (num_bytes_to_write == 0) |
| 79 return MOJO_RESULT_SHOULD_WAIT; | 82 return MOJO_RESULT_SHOULD_WAIT; |
| 80 | 83 |
| 81 // The amount we can write in our first |memcpy()|. | 84 // The amount we can write in our first |memcpy()|. |
| 82 size_t num_bytes_to_write_first = | 85 size_t num_bytes_to_write_first = |
| 83 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock()); | 86 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock()); |
| 84 // Do the first (and possibly only) |memcpy()|. | 87 // Do the first (and possibly only) |memcpy()|. |
| 85 size_t first_write_index = | 88 size_t first_write_index = |
| 86 (start_index_ + current_num_bytes_) % capacity_num_bytes(); | 89 (start_index_ + current_num_bytes_) % capacity_num_bytes(); |
| 87 EnsureBufferNoLock(); | 90 EnsureBufferNoLock(); |
| 88 memcpy(buffer_.get() + first_write_index, elements, num_bytes_to_write_first); | 91 elements.GetArray(buffer_.get() + first_write_index, |
| 92 num_bytes_to_write_first); |
| 89 | 93 |
| 90 if (num_bytes_to_write_first < num_bytes_to_write) { | 94 if (num_bytes_to_write_first < num_bytes_to_write) { |
| 91 // The "second write index" is zero. | 95 // The "second write index" is zero. |
| 92 memcpy(buffer_.get(), | 96 elements.At(num_bytes_to_write_first).GetArray( |
| 93 static_cast<const char*>(elements) + num_bytes_to_write_first, | 97 buffer_.get(), num_bytes_to_write - num_bytes_to_write_first); |
| 94 num_bytes_to_write - num_bytes_to_write_first); | |
| 95 } | 98 } |
| 96 | 99 |
| 97 current_num_bytes_ += num_bytes_to_write; | 100 current_num_bytes_ += num_bytes_to_write; |
| 98 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); | 101 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); |
| 99 *num_bytes = static_cast<uint32_t>(num_bytes_to_write); | 102 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write)); |
| 100 return MOJO_RESULT_OK; | 103 return MOJO_RESULT_OK; |
| 101 } | 104 } |
| 102 | 105 |
| 103 MojoResult LocalDataPipe::ProducerBeginWriteDataImplNoLock( | 106 MojoResult LocalDataPipe::ProducerBeginWriteDataImplNoLock( |
| 104 UserPointer<void*> buffer, | 107 UserPointer<void*> buffer, |
| 105 UserPointer<uint32_t> buffer_num_bytes, | 108 UserPointer<uint32_t> buffer_num_bytes, |
| 106 uint32_t min_num_bytes_to_write) { | 109 uint32_t min_num_bytes_to_write) { |
| 107 DCHECK(consumer_open_no_lock()); | 110 DCHECK(consumer_open_no_lock()); |
| 108 | 111 |
| 109 // The index we need to start writing at. | 112 // The index we need to start writing at. |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 333 |
| 331 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { | 334 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { |
| 332 DCHECK_LE(num_bytes, current_num_bytes_); | 335 DCHECK_LE(num_bytes, current_num_bytes_); |
| 333 start_index_ += num_bytes; | 336 start_index_ += num_bytes; |
| 334 start_index_ %= capacity_num_bytes(); | 337 start_index_ %= capacity_num_bytes(); |
| 335 current_num_bytes_ -= num_bytes; | 338 current_num_bytes_ -= num_bytes; |
| 336 } | 339 } |
| 337 | 340 |
| 338 } // namespace system | 341 } // namespace system |
| 339 } // namespace mojo | 342 } // namespace mojo |
| OLD | NEW |