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 #include "mojo/public/cpp/bindings/lib/validation_context.h" | 5 #include "mojo/public/cpp/bindings/lib/validation_context.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include "base/logging.h" | 7 #include "base/logging.h" |
11 #include "mojo/public/cpp/bindings/lib/serialization_util.h" | |
12 #include "mojo/public/cpp/bindings/message.h" | |
13 #include "mojo/public/cpp/system/handle.h" | |
14 | 8 |
15 namespace mojo { | 9 namespace mojo { |
16 namespace internal { | 10 namespace internal { |
17 | 11 |
18 ValidationContext::ValidationContext(const void* data, | 12 ValidationContext::ValidationContext(const void* data, |
19 size_t data_num_bytes, | 13 size_t data_num_bytes, |
20 size_t num_handles, | 14 size_t num_handles, |
21 Message* message, | 15 Message* message, |
22 const base::StringPiece& description) | 16 const base::StringPiece& description) |
23 : message_(message), | 17 : message_(message), |
(...skipping 13 matching lines...) Expand all Loading... |
37 // Assigning |num_handles| to |handle_end_| overflowed. | 31 // Assigning |num_handles| to |handle_end_| overflowed. |
38 // It shouldn't happen but if it does, set the handle index range to empty. | 32 // It shouldn't happen but if it does, set the handle index range to empty. |
39 NOTREACHED(); | 33 NOTREACHED(); |
40 handle_end_ = 0; | 34 handle_end_ = 0; |
41 } | 35 } |
42 } | 36 } |
43 | 37 |
44 ValidationContext::~ValidationContext() { | 38 ValidationContext::~ValidationContext() { |
45 } | 39 } |
46 | 40 |
47 bool ValidationContext::ClaimMemory(const void* position, uint32_t num_bytes) { | |
48 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
49 uintptr_t end = begin + num_bytes; | |
50 | |
51 if (!InternalIsValidRange(begin, end)) | |
52 return false; | |
53 | |
54 data_begin_ = end; | |
55 return true; | |
56 } | |
57 | |
58 bool ValidationContext::ClaimHandle(const Handle_Data& encoded_handle) { | |
59 uint32_t index = encoded_handle.value; | |
60 if (index == kEncodedInvalidHandleValue) | |
61 return true; | |
62 | |
63 if (index < handle_begin_ || index >= handle_end_) | |
64 return false; | |
65 | |
66 // |index| + 1 shouldn't overflow, because |index| is not the max value of | |
67 // uint32_t (it is less than |handle_end_|). | |
68 handle_begin_ = index + 1; | |
69 return true; | |
70 } | |
71 | |
72 bool ValidationContext::IsValidRange(const void* position, | |
73 uint32_t num_bytes) const { | |
74 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | |
75 uintptr_t end = begin + num_bytes; | |
76 | |
77 return InternalIsValidRange(begin, end); | |
78 } | |
79 | |
80 bool ValidationContext::InternalIsValidRange(uintptr_t begin, | |
81 uintptr_t end) const { | |
82 return end > begin && begin >= data_begin_ && end <= data_end_; | |
83 } | |
84 | |
85 } // namespace internal | 41 } // namespace internal |
86 } // namespace mojo | 42 } // namespace mojo |
OLD | NEW |