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 #ifndef MOJO_SYSTEM_MEMORY_H_ | 5 #ifndef MOJO_SYSTEM_MEMORY_H_ |
6 #define MOJO_SYSTEM_MEMORY_H_ | 6 #define MOJO_SYSTEM_MEMORY_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
11 namespace system { | 11 namespace system { |
12 | 12 |
13 // Verify that |count * size_each| bytes can be read from the user |pointer| | 13 // This is just forward-declared, with the definition and explicit |
14 // insofar as possible/necessary. |count| and |size_each| are specified | 14 // instantiations in the .cc file. This is used by |VerifyUserPointer<T>()| |
15 // separately instead of a single size, since |count * size_each| may overflow a | 15 // below, and you should use that instead. |
16 // |size_t|. |count| may be zero but |size_each| must be nonzero. | 16 template <size_t size> |
| 17 bool VerifyUserPointerForSize(const void* pointer, size_t count); |
| 18 |
| 19 // Verify that |count * sizeof(T)| bytes can be read from the user |pointer| |
| 20 // insofar as possible/necessary (note: this is done carefully since |count * |
| 21 // sizeof(T)| may overflow a |size_t|. |count| may be zero. If |T| is |void|, |
| 22 // then the size of each element is taken to be a single byte. |
17 // | 23 // |
18 // For example, if running in kernel mode, this should be a full verification | 24 // For example, if running in kernel mode, this should be a full verification |
19 // that the given memory is owned and readable by the user process. In user | 25 // that the given memory is owned and readable by the user process. In user |
20 // mode, if crashes are acceptable, this may do nothing at all (and always | 26 // mode, if crashes are acceptable, this may do nothing at all (and always |
21 // return true). | 27 // return true). |
22 bool VerifyUserPointer(const void* pointer, size_t count, size_t size_each); | 28 template <typename T> |
| 29 bool VerifyUserPointer(const T* pointer, size_t count) { |
| 30 return VerifyUserPointerForSize<sizeof(T)>(pointer, count); |
| 31 } |
| 32 |
| 33 // Special-case |T| equals |void| so that the size is in bytes, as indicated |
| 34 // above. |
| 35 template <> |
| 36 inline bool VerifyUserPointer<void>(const void* pointer, size_t count) { |
| 37 return VerifyUserPointerForSize<1>(pointer, count); |
| 38 } |
23 | 39 |
24 } // namespace system | 40 } // namespace system |
25 } // namespace mojo | 41 } // namespace mojo |
26 | 42 |
27 #endif // MOJO_SYSTEM_MEMORY_H_ | 43 #endif // MOJO_SYSTEM_MEMORY_H_ |
OLD | NEW |