OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/strings/string_piece.h" |
12 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
13 | 14 |
14 namespace mojo { | 15 namespace mojo { |
15 | 16 |
16 class Handle; | 17 class Handle; |
| 18 class Message; |
17 | 19 |
18 namespace internal { | 20 namespace internal { |
19 | 21 |
20 // BoundsChecker is used to validate object sizes, pointers and handle indices | 22 // ValidationContext is used when validating object sizes, pointers and handle |
21 // for payload of incoming messages. | 23 // indices in the payload of incoming messages. |
22 class BoundsChecker { | 24 class ValidationContext { |
23 public: | 25 public: |
24 // [data, data + data_num_bytes) specifies the initial valid memory range. | 26 // [data, data + data_num_bytes) specifies the initial valid memory range. |
25 // [0, num_handles) specifies the initial valid range of handle indices. | 27 // [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); | 28 // |
| 29 // If provided, |message| and |description| provide additional information |
| 30 // to use when reporting validation errors. In addition if |message| is |
| 31 // provided, the MojoNotifyBadMessage API will be used to notify the system of |
| 32 // such errors. |
| 33 ValidationContext(const void* data, |
| 34 uint32_t data_num_bytes, |
| 35 size_t num_handles, |
| 36 Message* message = nullptr, |
| 37 const base::StringPiece& description = ""); |
27 | 38 |
28 ~BoundsChecker(); | 39 ~ValidationContext(); |
29 | 40 |
30 // Claims the specified memory range. | 41 // Claims the specified memory range. |
31 // The method succeeds if the range is valid to claim. (Please see | 42 // The method succeeds if the range is valid to claim. (Please see |
32 // the comments for IsValidRange().) | 43 // the comments for IsValidRange().) |
33 // On success, the valid memory range is shrinked to begin right after the end | 44 // On success, the valid memory range is shrinked to begin right after the end |
34 // of the claimed range. | 45 // of the claimed range. |
35 bool ClaimMemory(const void* position, uint32_t num_bytes); | 46 bool ClaimMemory(const void* position, uint32_t num_bytes); |
36 | 47 |
37 // Claims the specified encoded handle (which is basically a handle index). | 48 // Claims the specified encoded handle (which is basically a handle index). |
38 // The method succeeds if: | 49 // The method succeeds if: |
39 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|. | 50 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|. |
40 // - the handle is contained inside the valid range of handle indices. In this | 51 // - 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. | 52 // case, the valid range is shinked to begin right after the claimed handle. |
42 bool ClaimHandle(const Handle_Data& encoded_handle); | 53 bool ClaimHandle(const Handle_Data& encoded_handle); |
43 | 54 |
44 // Returns true if the specified range is not empty, and the range is | 55 // Returns true if the specified range is not empty, and the range is |
45 // contained inside the valid memory range. | 56 // contained inside the valid memory range. |
46 bool IsValidRange(const void* position, uint32_t num_bytes) const; | 57 bool IsValidRange(const void* position, uint32_t num_bytes) const; |
47 | 58 |
| 59 Message* message() const { return message_; } |
| 60 const base::StringPiece& description() const { return description_; } |
| 61 |
48 private: | 62 private: |
49 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const; | 63 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const; |
50 | 64 |
| 65 Message* const message_; |
| 66 const base::StringPiece description_; |
| 67 |
51 // [data_begin_, data_end_) is the valid memory range. | 68 // [data_begin_, data_end_) is the valid memory range. |
52 uintptr_t data_begin_; | 69 uintptr_t data_begin_; |
53 uintptr_t data_end_; | 70 uintptr_t data_end_; |
54 | 71 |
55 // [handle_begin_, handle_end_) is the valid handle index range. | 72 // [handle_begin_, handle_end_) is the valid handle index range. |
56 uint32_t handle_begin_; | 73 uint32_t handle_begin_; |
57 uint32_t handle_end_; | 74 uint32_t handle_end_; |
58 | 75 |
59 DISALLOW_COPY_AND_ASSIGN(BoundsChecker); | 76 DISALLOW_COPY_AND_ASSIGN(ValidationContext); |
60 }; | 77 }; |
61 | 78 |
62 } // namespace internal | 79 } // namespace internal |
63 } // namespace mojo | 80 } // namespace mojo |
64 | 81 |
65 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_ | 82 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
OLD | NEW |