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