| 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 <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/bindings_internal.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 indices | |
| 21 // for payload of incoming messages. | |
| 22 class BoundsChecker { | |
| 23 public: | |
| 24 // [data, data + data_num_bytes) specifies the initial valid memory range. | |
| 25 // [0, num_handles) specifies the initial valid range of handle indices. | |
| 26 BoundsChecker(const void* data, uint32_t data_num_bytes, size_t num_handles); | |
| 27 | |
| 28 ~BoundsChecker(); | |
| 29 | |
| 30 // Claims the specified memory range. | |
| 31 // The method succeeds if the range is valid to claim. (Please see | |
| 32 // the comments for IsValidRange().) | |
| 33 // On success, the valid memory range is shrinked to begin right after the end | |
| 34 // of the claimed range. | |
| 35 bool ClaimMemory(const void* position, uint32_t num_bytes); | |
| 36 | |
| 37 // Claims the specified encoded handle (which is basically a handle index). | |
| 38 // The method succeeds if: | |
| 39 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|. | |
| 40 // - the handle is contained inside the valid range of handle indices. In this | |
| 41 // case, the valid range is shinked to begin right after the claimed handle. | |
| 42 bool ClaimHandle(const Handle_Data& encoded_handle); | |
| 43 | |
| 44 // Returns true if the specified range is not empty, and the range is | |
| 45 // contained inside the valid memory range. | |
| 46 bool IsValidRange(const void* position, uint32_t num_bytes) const; | |
| 47 | |
| 48 private: | |
| 49 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const; | |
| 50 | |
| 51 // [data_begin_, data_end_) is the valid memory range. | |
| 52 uintptr_t data_begin_; | |
| 53 uintptr_t data_end_; | |
| 54 | |
| 55 // [handle_begin_, handle_end_) is the valid handle index range. | |
| 56 uint32_t handle_begin_; | |
| 57 uint32_t handle_end_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(BoundsChecker); | |
| 60 }; | |
| 61 | |
| 62 } // namespace internal | |
| 63 } // namespace mojo | |
| 64 | |
| 65 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | |
| OLD | NEW |