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 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace system { | 12 namespace system { |
13 | 13 |
14 namespace internal { | 14 template <size_t size> |
15 | 15 bool VerifyUserPointerForSize(const void* pointer, size_t count) { |
16 template <size_t size, size_t alignment> | |
17 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { | |
18 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
19 // check that it's non-null and aligned. (A faster user mode implementation is | |
20 // also possible if this check is skipped.) | |
21 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0; | |
22 } | |
23 | |
24 // Explicitly instantiate the sizes we need. Add instantiations as needed. | |
25 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<1, 1>( | |
26 const void*); | |
27 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 4>( | |
28 const void*); | |
29 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<8, 8>( | |
30 const void*); | |
31 | |
32 template <size_t size, size_t alignment> | |
33 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { | |
34 if (count > std::numeric_limits<size_t>::max() / size) | 16 if (count > std::numeric_limits<size_t>::max() / size) |
35 return false; | 17 return false; |
36 | 18 |
37 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | 19 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
38 // check that it's non-null and aligned if |count| is nonzero. (A faster user | 20 // check that it's non-null if |size| is nonzero. (A faster user mode |
39 // mode implementation is also possible if this check is skipped.) | 21 // implementation is also possible if this check is skipped.) |
40 return count == 0 || | 22 return count == 0 || !!pointer; |
41 (!!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0); | |
42 } | 23 } |
43 | 24 |
44 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 25 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
45 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<1, 1>( | 26 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<1>( |
46 const void*, size_t); | 27 const void*, size_t); |
47 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<4, 4>( | 28 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<4>( |
48 const void*, size_t); | 29 const void*, size_t); |
49 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<8, 8>( | 30 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<8>( |
50 const void*, size_t); | 31 const void*, size_t); |
51 | 32 |
52 } // nameespace internal | |
53 | |
54 template <size_t alignment> | |
55 bool VerifyUserPointerWithSize(const void* pointer, size_t size) { | |
56 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
57 // check that it's non-null and aligned. (A faster user mode implementation is | |
58 // also possible if this check is skipped.) | |
59 return size == 0 || | |
60 (!!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0); | |
61 } | |
62 | |
63 // Explicitly instantiate the alignments we need. Add instantiations as needed. | |
64 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<1>(const void*, | |
65 size_t); | |
66 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<4>(const void*, | |
67 size_t); | |
68 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<8>(const void*, | |
69 size_t); | |
70 | |
71 } // namespace system | 33 } // namespace system |
72 } // namespace mojo | 34 } // namespace mojo |
OLD | NEW |