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