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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // could be rectified, but again seems like optimizing for the uncommon case.) | 170 // could be rectified, but again seems like optimizing for the uncommon case.) |
171 if (!producer_open_no_lock() || !producer_in_two_phase_write_no_lock()) | 171 if (!producer_open_no_lock() || !producer_in_two_phase_write_no_lock()) |
172 DestroyBufferNoLock(); | 172 DestroyBufferNoLock(); |
173 current_num_bytes_ = 0; | 173 current_num_bytes_ = 0; |
174 } | 174 } |
175 | 175 |
176 MojoResult LocalDataPipe::ConsumerReadDataImplNoLock( | 176 MojoResult LocalDataPipe::ConsumerReadDataImplNoLock( |
177 UserPointer<void> elements, | 177 UserPointer<void> elements, |
178 UserPointer<uint32_t> num_bytes, | 178 UserPointer<uint32_t> num_bytes, |
179 uint32_t max_num_bytes_to_read, | 179 uint32_t max_num_bytes_to_read, |
180 uint32_t min_num_bytes_to_read) { | 180 uint32_t min_num_bytes_to_read, |
| 181 bool peek) { |
181 DCHECK_EQ(max_num_bytes_to_read % element_num_bytes(), 0u); | 182 DCHECK_EQ(max_num_bytes_to_read % element_num_bytes(), 0u); |
182 DCHECK_EQ(min_num_bytes_to_read % element_num_bytes(), 0u); | 183 DCHECK_EQ(min_num_bytes_to_read % element_num_bytes(), 0u); |
183 DCHECK_GT(max_num_bytes_to_read, 0u); | 184 DCHECK_GT(max_num_bytes_to_read, 0u); |
184 | 185 |
185 if (min_num_bytes_to_read > current_num_bytes_) { | 186 if (min_num_bytes_to_read > current_num_bytes_) { |
186 // Don't return "should wait" since you can't wait for a specified amount of | 187 // Don't return "should wait" since you can't wait for a specified amount of |
187 // data. | 188 // data. |
188 return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE | 189 return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE |
189 : MOJO_RESULT_FAILED_PRECONDITION; | 190 : MOJO_RESULT_FAILED_PRECONDITION; |
190 } | 191 } |
191 | 192 |
192 size_t num_bytes_to_read = | 193 size_t num_bytes_to_read = |
193 std::min(static_cast<size_t>(max_num_bytes_to_read), current_num_bytes_); | 194 std::min(static_cast<size_t>(max_num_bytes_to_read), current_num_bytes_); |
194 if (num_bytes_to_read == 0) { | 195 if (num_bytes_to_read == 0) { |
195 return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT | 196 return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT |
196 : MOJO_RESULT_FAILED_PRECONDITION; | 197 : MOJO_RESULT_FAILED_PRECONDITION; |
197 } | 198 } |
198 | 199 |
199 // The amount we can read in our first |memcpy()|. | 200 // The amount we can read in our first |memcpy()|. |
200 size_t num_bytes_to_read_first = | 201 size_t num_bytes_to_read_first = |
201 std::min(num_bytes_to_read, GetMaxNumBytesToReadNoLock()); | 202 std::min(num_bytes_to_read, GetMaxNumBytesToReadNoLock()); |
202 elements.PutArray(buffer_.get() + start_index_, num_bytes_to_read_first); | 203 elements.PutArray(buffer_.get() + start_index_, num_bytes_to_read_first); |
203 | 204 |
204 if (num_bytes_to_read_first < num_bytes_to_read) { | 205 if (num_bytes_to_read_first < num_bytes_to_read) { |
205 // The "second read index" is zero. | 206 // The "second read index" is zero. |
206 elements.At(num_bytes_to_read_first) | 207 elements.At(num_bytes_to_read_first) |
207 .PutArray(buffer_.get(), num_bytes_to_read - num_bytes_to_read_first); | 208 .PutArray(buffer_.get(), num_bytes_to_read - num_bytes_to_read_first); |
208 } | 209 } |
209 | 210 |
210 MarkDataAsConsumedNoLock(num_bytes_to_read); | 211 if (!peek) |
| 212 MarkDataAsConsumedNoLock(num_bytes_to_read); |
211 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_read)); | 213 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_read)); |
212 return MOJO_RESULT_OK; | 214 return MOJO_RESULT_OK; |
213 } | 215 } |
214 | 216 |
215 MojoResult LocalDataPipe::ConsumerDiscardDataImplNoLock( | 217 MojoResult LocalDataPipe::ConsumerDiscardDataImplNoLock( |
216 UserPointer<uint32_t> num_bytes, | 218 UserPointer<uint32_t> num_bytes, |
217 uint32_t max_num_bytes_to_discard, | 219 uint32_t max_num_bytes_to_discard, |
218 uint32_t min_num_bytes_to_discard) { | 220 uint32_t min_num_bytes_to_discard) { |
219 DCHECK_EQ(max_num_bytes_to_discard % element_num_bytes(), 0u); | 221 DCHECK_EQ(max_num_bytes_to_discard % element_num_bytes(), 0u); |
220 DCHECK_EQ(min_num_bytes_to_discard % element_num_bytes(), 0u); | 222 DCHECK_EQ(min_num_bytes_to_discard % element_num_bytes(), 0u); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 334 |
333 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { | 335 void LocalDataPipe::MarkDataAsConsumedNoLock(size_t num_bytes) { |
334 DCHECK_LE(num_bytes, current_num_bytes_); | 336 DCHECK_LE(num_bytes, current_num_bytes_); |
335 start_index_ += num_bytes; | 337 start_index_ += num_bytes; |
336 start_index_ %= capacity_num_bytes(); | 338 start_index_ %= capacity_num_bytes(); |
337 current_num_bytes_ -= num_bytes; | 339 current_num_bytes_ -= num_bytes; |
338 } | 340 } |
339 | 341 |
340 } // namespace system | 342 } // namespace system |
341 } // namespace mojo | 343 } // namespace mojo |
OLD | NEW |