Chromium Code Reviews| 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 | |
|
darin (slow to review)
2014/05/26 19:17:47
Can we restrict the serialization format in ways t
yzshen1
2014/05/26 21:30:56
We are already enforcing this. (The offset is defi
| |
| 21 // for payload of incoming messages. | |
| 22 class BoundsChecker { | |
| 23 public: | |
| 24 // [data, data_num_bytes) specifies the valid memory range. | |
| 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 | |
| 36 // Claims the specified encoded handle (which is basically a handle index). | |
| 37 // The method fails if the handle has already been claimed or it is outside | |
| 38 // of the valid range. The method succeeds if |encoded_handle|'s value is | |
| 39 // |kEncodedInvalidHandleValue|. | |
| 40 bool ClaimHandle(const Handle& encoded_handle); | |
| 41 | |
| 42 // Returns true if the specified range is contained inside the valid memory | |
| 43 // range, no matter whether the range overlaps claimed ranges or not. | |
| 44 // The method returns false if the range is of zero length. | |
| 45 bool IsWithinBounds(const void* position, uint32_t num_bytes) const; | |
| 46 | |
| 47 const std::vector<uintptr_t>& GetClaimedRangesForTesting() const; | |
| 48 | |
| 49 private: | |
| 50 void InsertOrMergeClaimedRange(std::vector<uintptr_t>::iterator pos, | |
| 51 uintptr_t begin, uintptr_t end); | |
| 52 | |
| 53 bool InternalIsWithinBounds(uintptr_t begin, uintptr_t end) const; | |
| 54 | |
| 55 // The valid memory range is [data_begin_, data_end_). | |
| 56 uintptr_t data_begin_; | |
| 57 uintptr_t data_end_; | |
| 58 | |
| 59 // This vector is sorted in increasing order, with an even number of elements. | |
| 60 // The (2*i)-th and (2*i+1)-th element form a claimed range: | |
| 61 // [claimed_ranges_[2*i], claimed_ranges_[2*i+1]). | |
| 62 // (Please note that |claimed_ranges_[2*i+1]| itself is not included in the | |
| 63 // range.) | |
| 64 std::vector<uintptr_t> claimed_ranges_; | |
| 65 | |
| 66 std::vector<bool> claimed_handles_; | |
| 67 | |
| 68 MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChecker); | |
| 69 }; | |
| 70 | |
| 71 } // namespace internal | |
| 72 } // namespace mojo | |
| 73 | |
| 74 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | |
| OLD | NEW |