OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 #include "mojo/public/cpp/bindings/lib/validation_util.h" | |
6 | |
7 #include <limits> | |
8 #include <string> | |
9 | |
10 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
11 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
12 | |
13 namespace mojo { | |
14 namespace internal { | |
15 | |
16 bool ValidateEncodedPointer(const uint64_t* offset) { | |
17 // - Make sure |*offset| is no more than 32-bits. | |
18 // - Cast |offset| to uintptr_t so overflow behavior is well defined across | |
19 // 32-bit and 64-bit systems. | |
20 return *offset <= std::numeric_limits<uint32_t>::max() && | |
21 (reinterpret_cast<uintptr_t>(offset) + | |
22 static_cast<uint32_t>(*offset) >= | |
23 reinterpret_cast<uintptr_t>(offset)); | |
24 } | |
25 | |
26 ValidationError ValidateStructHeaderAndClaimMemory( | |
27 const void* data, | |
28 BoundsChecker* bounds_checker, | |
29 std::string* err) { | |
30 if (!IsAligned(data)) { | |
31 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
32 return ValidationError::MISALIGNED_OBJECT; | |
33 } | |
34 if (!bounds_checker->IsValidRange(data, sizeof(StructHeader))) { | |
35 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
36 return ValidationError::ILLEGAL_MEMORY_RANGE; | |
37 } | |
38 | |
39 const StructHeader* header = static_cast<const StructHeader*>(data); | |
40 | |
41 if (header->num_bytes < sizeof(StructHeader)) { | |
42 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
43 return ValidationError::UNEXPECTED_STRUCT_HEADER; | |
44 } | |
45 | |
46 if (!bounds_checker->ClaimMemory(data, header->num_bytes)) { | |
47 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
48 return ValidationError::ILLEGAL_MEMORY_RANGE; | |
49 } | |
50 | |
51 return ValidationError::NONE; | |
52 } | |
53 | |
54 } // namespace internal | |
55 } // namespace mojo | |
OLD | NEW |