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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "mojo/public/cpp/system/macros.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 class Handle; | |
17 | |
18 namespace internal { | |
19 | |
20 // BoundsChecker is used to validate object sizes, pointers and handle indexes | |
21 // for payload of incoming messages. | |
22 class BoundsChecker { | |
23 public: | |
24 // [|data|, |data_num_bytes|) specifies the valid memory range. | |
Tom Sepez
2014/05/22 19:39:21
nit: not sure vertical bars help inside the [) ran
yzshen1
2014/05/22 20:56:22
Done, I removed them.
| |
25 // [0, |num_handles|) specifies the valid range of handle indexes. | |
26 BoundsChecker(const void* data, uint32_t data_num_bytes, | |
27 size_t num_handles); | |
28 | |
29 ~BoundsChecker(); | |
30 | |
31 // Claims the specified memory range. | |
32 // The method fails if the range overlaps claimed ranges, or it is not | |
33 // contained inside the valid memory range, or it is of zero length. | |
34 bool ClaimMemory(const void* position, uint32_t num_bytes); | |
35 // Claims the specified encoded handle (which is basically a handle index). | |
Tom Sepez
2014/05/22 19:39:21
nit: blank line here.
yzshen1
2014/05/22 20:56:22
Done.
| |
36 // The method fails if the handle has already been claimed or it is outside | |
37 // of the valid range. The method succeeds if |encoded_handle| is invalid | |
38 // handle. | |
Tom Sepez
2014/05/22 19:39:21
nit: while absolutely true, this is a strange sent
yzshen1
2014/05/22 20:56:22
Revised the comment.
| |
39 bool ClaimHandle(const Handle& encoded_handle); | |
40 | |
41 // Returns true if the specified range is contained inside the valid memory | |
42 // range, no matter whether the range overlaps claimed ranges or not. | |
43 // The method returns false if the range is of zero length. | |
44 bool IsWithinBounds(const void* position, uint32_t num_bytes) const; | |
45 | |
46 const std::vector<uintptr_t>& GetClaimedRangesForTesting() const; | |
47 | |
48 private: | |
49 void InsertOrMergeClaimedRange(std::vector<uintptr_t>::iterator pos, | |
50 uintptr_t begin, uintptr_t end); | |
51 | |
52 bool InternalIsWithinBounds(uintptr_t begin, uintptr_t end) const; | |
53 | |
54 // The valid memory range is [|data_begin_|, |data_end_|). | |
55 uintptr_t data_begin_; | |
56 uintptr_t data_end_; | |
57 | |
58 // This vector is sorted in increasing order, with an even number of elements. | |
Tom Sepez
2014/05/22 19:39:21
I think you'll want a vector of struct Range { uin
yzshen1
2014/05/22 20:56:22
I did consider this alternative, but think that th
| |
59 // The (2*i)-th and (2*i+1)-th element form a claimed range: | |
60 // [|claimed_ranges_[2*i]|, |claimed_ranges_[2*i+1]|). | |
61 // (Please note that |claimed_ranges_[2*i+1]| itself is not included in the | |
62 // range.) | |
63 std::vector<uintptr_t> claimed_ranges_; | |
64 | |
65 std::vector<bool> claimed_handles_; | |
66 | |
67 MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChecker); | |
68 }; | |
69 | |
70 } // namespace internal | |
71 } // namespace mojo | |
72 | |
73 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | |
OLD | NEW |