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