OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "mojo/public/cpp/system/core.h" | |
10 | |
11 namespace mojo { | |
12 namespace internal { | |
13 | |
14 BoundsChecker::BoundsChecker(const void* data, uint32_t data_num_bytes, | |
15 size_t num_handles) | |
16 : data_begin_(reinterpret_cast<uintptr_t>(data)), | |
17 data_end_(data_begin_ + data_num_bytes), | |
18 claimed_handles_(num_handles, false) { | |
19 if (data_end_ < data_begin_) { | |
20 // The calculation of |data_end_| overflowed. | |
21 // It shouldn't happen but if it does, set the range to empty so | |
22 // IsWithinBounds() and ClaimMemory() always fail. | |
23 assert(false); // Not reached. | |
24 data_end_ = data_begin_; | |
25 } | |
26 } | |
27 | |
28 BoundsChecker::~BoundsChecker() { | |
29 } | |
30 | |
31 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) { | |
32 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
33 uintptr_t end = begin + num_bytes; | |
34 | |
35 if (!InternalIsWithinBounds(begin, end)) | |
36 return false; | |
37 | |
38 if (claimed_ranges_.empty() || begin >= claimed_ranges_.back()) { | |
39 // This optimization is significant: if the layout of objects in an incoming | |
40 // message are in the same order as our validation code traverses the | |
41 // message, we will always hit this code path. | |
42 InsertOrMergeClaimedRange(claimed_ranges_.end(), begin, end); | |
43 return true; | |
44 } | |
45 | |
46 // Do a binary search to find the smallest index i that satisfies | |
Tom Sepez
2014/05/22 19:39:21
can we use std::binary_search() instead of re-inve
yzshen1
2014/05/22 20:56:22
std::binary_search() is used to check whether an e
| |
47 // |begin| < |claimed_ranges_[i]|. (The previous if-block guarantees that such | |
48 // an index must exist if we reach this point.) | |
49 size_t index_left = 0; | |
50 size_t index_right = claimed_ranges_.size() - 1; | |
51 | |
52 while (index_right > index_left) { | |
53 size_t index_middle = (index_left + index_right) / 2; | |
54 if (begin < claimed_ranges_[index_middle]) | |
55 index_right = index_middle; | |
56 else | |
57 index_left = index_middle + 1; | |
58 } | |
59 assert(index_left == index_right); | |
60 | |
61 // |index_left| is an odd number means that |begin| lies in a claimed | |
62 // range. | |
63 if (index_left % 2) | |
64 return false; | |
65 // If |end| is bigger than |claimed_ranges_[index_left]| while |begin| is | |
66 // smaller, then [|begin|, |end|) overlaps at least one claimed range. | |
67 if (end > claimed_ranges_[index_left]) | |
68 return false; | |
69 | |
70 InsertOrMergeClaimedRange(claimed_ranges_.begin() + index_left, begin, end); | |
71 return true; | |
72 } | |
73 | |
74 bool BoundsChecker::ClaimHandle(const Handle& encoded_handle) { | |
75 uint32_t index = encoded_handle.value(); | |
76 if (index == static_cast<uint32_t>(-1)) | |
Tom Sepez
2014/05/22 19:39:21
is there a kInvalidHandle constant somewhere?
yzshen1
2014/05/22 20:56:22
We have kInvalidHandleValue but it is the "decoded
| |
77 return true; | |
78 | |
79 if (index >= claimed_handles_.size() || claimed_handles_[index]) | |
80 return false; | |
81 | |
82 claimed_handles_[index] = true; | |
83 return true; | |
84 } | |
85 | |
86 bool BoundsChecker::IsWithinBounds(const void* position, | |
87 uint32_t num_bytes) const { | |
88 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
89 uintptr_t end = begin + num_bytes; | |
90 | |
91 return InternalIsWithinBounds(begin, end); | |
92 } | |
93 | |
94 const std::vector<uintptr_t>& | |
95 BoundsChecker::GetClaimedRangesForTesting() const { | |
96 return claimed_ranges_; | |
97 } | |
98 | |
99 void BoundsChecker::InsertOrMergeClaimedRange( | |
100 std::vector<uintptr_t>::iterator pos, uintptr_t begin, uintptr_t end) { | |
101 bool merge_previous = pos != claimed_ranges_.begin() && | |
102 *(pos - 1) == begin; | |
103 bool merge_next = pos != claimed_ranges_.end() && | |
104 *pos == end; | |
105 | |
106 if (merge_previous && merge_next) { | |
107 *(pos - 1) = *(pos + 1); | |
108 claimed_ranges_.erase(pos, pos + 2); | |
109 } else if (merge_previous) { | |
110 *(pos - 1) = end; | |
111 } else if (merge_next) { | |
112 *pos = begin; | |
113 } else { | |
114 uintptr_t pair[2] = {begin, end}; | |
115 claimed_ranges_.insert(pos, pair, pair + 2); | |
116 } | |
117 } | |
118 | |
119 bool BoundsChecker::InternalIsWithinBounds(uintptr_t begin, | |
120 uintptr_t end) const { | |
121 return end > begin && begin >= data_begin_ && end <= data_end_; | |
122 } | |
123 | |
124 } // namespace internal | |
125 } // namespace mojo | |
OLD | NEW |