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/bindings/lib/bindings_serialization.h" | |
10 #include "mojo/public/cpp/system/core.h" | |
11 | |
12 namespace mojo { | |
13 namespace internal { | |
14 | |
15 BoundsChecker::BoundsChecker(const void* data, uint32_t data_num_bytes, | |
16 size_t num_handles) | |
17 : data_begin_(reinterpret_cast<uintptr_t>(data)), | |
18 data_end_(data_begin_ + data_num_bytes), | |
19 claimed_handles_(num_handles, false) { | |
20 if (data_end_ < data_begin_) { | |
21 // The calculation of |data_end_| overflowed. | |
22 // It shouldn't happen but if it does, set the range to empty so | |
23 // IsWithinBounds() and ClaimMemory() always fail. | |
24 assert(false); // Not reached. | |
25 data_end_ = data_begin_; | |
26 } | |
27 } | |
28 | |
29 BoundsChecker::~BoundsChecker() { | |
30 } | |
Tom Sepez
2014/05/23 18:18:35
maybe in debug builds we want to assert that the i
yzshen1
2014/05/23 20:04:07
Done.
| |
31 | |
32 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) { | |
33 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
34 uintptr_t end = begin + num_bytes; | |
35 | |
36 if (!InternalIsWithinBounds(begin, end)) | |
37 return false; | |
38 | |
39 if (claimed_ranges_.empty() || begin >= claimed_ranges_.back()) { | |
40 // This optimization is significant: if the layout of objects in an incoming | |
41 // message are in the same order as our validation code traverses the | |
42 // message, we will always hit this code path. | |
43 InsertOrMergeClaimedRange(claimed_ranges_.end(), begin, end); | |
44 return true; | |
45 } | |
46 | |
47 // Do a binary search to find the smallest index i that satisfies | |
48 // |begin| < |claimed_ranges_[i]|. (The previous if-block guarantees that such | |
Tom Sepez
2014/05/23 18:18:35
std::upper_bound(), no?
yzshen1
2014/05/23 20:04:07
Sounds good. I didn't know about this choice. :)
s
| |
49 // an index must exist if we reach this point.) | |
50 size_t index_left = 0; | |
51 size_t index_right = claimed_ranges_.size() - 1; | |
52 | |
53 while (index_right > index_left) { | |
54 size_t index_middle = (index_left + index_right) / 2; | |
55 if (begin < claimed_ranges_[index_middle]) | |
56 index_right = index_middle; | |
57 else | |
58 index_left = index_middle + 1; | |
59 } | |
60 assert(index_left == index_right); | |
61 | |
62 // |index_left| is an odd number means that |begin| lies in a claimed | |
63 // range. | |
64 if (index_left % 2) | |
65 return false; | |
66 // If |end| is bigger than |claimed_ranges_[index_left]| while |begin| is | |
67 // smaller, then [begin, end) overlaps at least one claimed range. | |
68 if (end > claimed_ranges_[index_left]) | |
69 return false; | |
70 | |
71 InsertOrMergeClaimedRange(claimed_ranges_.begin() + index_left, begin, end); | |
72 return true; | |
73 } | |
74 | |
75 bool BoundsChecker::ClaimHandle(const Handle& encoded_handle) { | |
76 uint32_t index = encoded_handle.value(); | |
77 if (index == kEncodedInvalidHandleValue) | |
78 return true; | |
79 | |
80 if (index >= claimed_handles_.size() || claimed_handles_[index]) | |
81 return false; | |
82 | |
83 claimed_handles_[index] = true; | |
84 return true; | |
85 } | |
86 | |
87 bool BoundsChecker::IsWithinBounds(const void* position, | |
88 uint32_t num_bytes) const { | |
89 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
90 uintptr_t end = begin + num_bytes; | |
91 | |
92 return InternalIsWithinBounds(begin, end); | |
93 } | |
94 | |
95 const std::vector<uintptr_t>& | |
96 BoundsChecker::GetClaimedRangesForTesting() const { | |
97 return claimed_ranges_; | |
98 } | |
99 | |
100 void BoundsChecker::InsertOrMergeClaimedRange( | |
101 std::vector<uintptr_t>::iterator pos, uintptr_t begin, uintptr_t end) { | |
102 bool merge_previous = pos != claimed_ranges_.begin() && | |
103 *(pos - 1) == begin; | |
104 bool merge_next = pos != claimed_ranges_.end() && | |
105 *pos == end; | |
106 | |
107 if (merge_previous && merge_next) { | |
108 *(pos - 1) = *(pos + 1); | |
109 claimed_ranges_.erase(pos, pos + 2); | |
110 } else if (merge_previous) { | |
111 *(pos - 1) = end; | |
112 } else if (merge_next) { | |
113 *pos = begin; | |
114 } else { | |
115 uintptr_t pair[2] = {begin, end}; | |
116 claimed_ranges_.insert(pos, pair, pair + 2); | |
Tom Sepez
2014/05/23 18:32:34
this could be really slow if ranges aren't strictl
yzshen1
2014/05/23 20:04:07
I think this should be okay, because in most cases
| |
117 } | |
118 } | |
119 | |
120 bool BoundsChecker::InternalIsWithinBounds(uintptr_t begin, | |
121 uintptr_t end) const { | |
122 return end > begin && begin >= data_begin_ && end <= data_end_; | |
123 } | |
124 | |
125 } // namespace internal | |
126 } // namespace mojo | |
OLD | NEW |