OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/system/memory.h" | 5 #include "mojo/system/memory.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 namespace system { | 13 namespace system { |
14 | |
15 namespace internal { | 14 namespace internal { |
16 | 15 |
17 template <size_t alignment> | 16 template <size_t alignment> |
18 bool IsAligned(const void* pointer) { | 17 bool IsAligned(const void* pointer) { |
19 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0; | 18 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0; |
20 } | 19 } |
21 | 20 |
22 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for | 21 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for |
23 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T | 22 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T |
24 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 23 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
25 template <> | 24 template <> |
26 bool IsAligned<8>(const void* pointer) { | 25 bool IsAligned<8>(const void* pointer) { |
27 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; | 26 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; |
28 } | 27 } |
29 #endif | 28 #endif |
30 | 29 |
31 template <size_t size, size_t alignment> | 30 template <size_t size, size_t alignment> |
32 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer) { | 31 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer) { |
33 CHECK(pointer && IsAligned<alignment>(pointer)); | 32 CHECK(pointer && IsAligned<alignment>(pointer)); |
34 } | 33 } |
35 | 34 |
36 template <size_t size, size_t alignment> | |
37 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { | |
38 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
39 // check that it's non-null and aligned. (A faster user mode implementation is | |
40 // also possible if this check is skipped.) | |
41 return !!pointer && IsAligned<alignment>(pointer); | |
42 } | |
43 | |
44 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 35 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
45 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<1, 1>( | 36 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<1, 1>( |
46 const void*); | 37 const void*); |
47 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 4>( | 38 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 4>( |
48 const void*); | 39 const void*); |
49 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 8>( | 40 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 8>( |
50 const void*); | 41 const void*); |
51 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( | |
52 const void*); | |
53 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( | |
54 const void*); | |
55 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( | |
56 const void*); | |
57 // Notwithstanding the comments above about MSVS, whenever we expect an | 42 // Notwithstanding the comments above about MSVS, whenever we expect an |
58 // alignment of 8 for something of size 4, it's due to an explicit (e.g., | 43 // alignment of 8 for something of size 4, it's due to an explicit (e.g., |
59 // #pragma align) alignment specification, which MSVS *does* respect. We want | 44 // #pragma align) alignment specification, which MSVS *does* respect. We want |
60 // this in particular to check that various Options structs are aligned. | 45 // this in particular to check that various Options structs are aligned. |
61 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 46 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
62 template <> | 47 template <> |
63 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 8>( | 48 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 8>( |
64 const void* pointer) { | 49 const void* pointer) { |
65 CHECK(pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0); | 50 CHECK(pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0); |
66 } | 51 } |
67 template <> | |
68 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( | |
69 const void* pointer) { | |
70 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; | |
71 } | |
72 #else | 52 #else |
73 template MOJO_SYSTEM_IMPL_EXPORT void CheckUserPointer<4, 8>( | 53 template MOJO_SYSTEM_IMPL_EXPORT void CheckUserPointer<4, 8>( |
74 const void*); | 54 const void*); |
75 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( | |
76 const void*); | |
77 #endif | 55 #endif |
78 | 56 |
79 template <size_t size, size_t alignment> | 57 template <size_t size, size_t alignment> |
80 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount(const void* pointer, | 58 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount(const void* pointer, |
81 size_t count) { | 59 size_t count) { |
82 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); | 60 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); |
83 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); | 61 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); |
84 } | 62 } |
85 | 63 |
86 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 64 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
(...skipping 13 matching lines...) Expand all Loading... |
100 } | 78 } |
101 | 79 |
102 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 80 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
103 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<1>(const void*, | 81 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<1>(const void*, |
104 size_t); | 82 size_t); |
105 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<4>(const void*, | 83 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<4>(const void*, |
106 size_t); | 84 size_t); |
107 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<8>(const void*, | 85 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<8>(const void*, |
108 size_t); | 86 size_t); |
109 | 87 |
110 template <size_t size, size_t alignment> | |
111 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { | |
112 if (count > std::numeric_limits<size_t>::max() / size) | |
113 return false; | |
114 | |
115 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
116 // check that it's non-null and aligned if |count| is nonzero. (A faster user | |
117 // mode implementation is also possible if this check is skipped.) | |
118 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); | |
119 } | |
120 | |
121 // Explicitly instantiate the sizes we need. Add instantiations as needed. | |
122 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<1, 1>( | |
123 const void*, size_t); | |
124 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<4, 4>( | |
125 const void*, size_t); | |
126 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<8, 8>( | |
127 const void*, size_t); | |
128 | |
129 } // namespace internal | 88 } // namespace internal |
130 | |
131 template <size_t alignment> | |
132 bool VerifyUserPointerWithSize(const void* pointer, size_t size) { | |
133 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
134 // check that it's non-null and aligned. (A faster user mode implementation is | |
135 // also possible if this check is skipped.) | |
136 return size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer)); | |
137 } | |
138 | |
139 // Explicitly instantiate the alignments we need. Add instantiations as needed. | |
140 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, | |
141 size_t); | |
142 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, | |
143 size_t); | |
144 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, | |
145 size_t); | |
146 | |
147 } // namespace system | 89 } // namespace system |
148 } // namespace mojo | 90 } // namespace mojo |
OLD | NEW |