| 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 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for | 22 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for |
| 23 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T | 23 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T |
| 24 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 24 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 25 template <> | 25 template <> |
| 26 bool IsAligned<8>(const void* pointer) { | 26 bool IsAligned<8>(const void* pointer) { |
| 27 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; | 27 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; |
| 28 } | 28 } |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 template <size_t size, size_t alignment> | 31 template <size_t size, size_t alignment> |
| 32 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper(const void* pointer) { | 32 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer) { |
| 33 CHECK(pointer && IsAligned<alignment>(pointer)); | 33 CHECK(pointer && IsAligned<alignment>(pointer)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 template <size_t size, size_t alignment> | 36 template <size_t size, size_t alignment> |
| 37 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { | 37 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { |
| 38 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | 38 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
| 39 // check that it's non-null and aligned. (A faster user mode implementation is | 39 // check that it's non-null and aligned. (A faster user mode implementation is |
| 40 // also possible if this check is skipped.) | 40 // also possible if this check is skipped.) |
| 41 return !!pointer && IsAligned<alignment>(pointer); | 41 return !!pointer && IsAligned<alignment>(pointer); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 44 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
| 45 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<1, 1>( | 45 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<1, 1>( |
| 46 const void*); | 46 const void*); |
| 47 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<4, 4>( | 47 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 4>( |
| 48 const void*); | 48 const void*); |
| 49 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<8, 8>( | 49 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 8>( |
| 50 const void*); | 50 const void*); |
| 51 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( | 51 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( |
| 52 const void*); | 52 const void*); |
| 53 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( | 53 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( |
| 54 const void*); | 54 const void*); |
| 55 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( | 55 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( |
| 56 const void*); | 56 const void*); |
| 57 // Notwithstanding the comments above about MSVS, whenever we expect an | 57 // Notwithstanding the comments above about MSVS, whenever we expect an |
| 58 // alignment of 8 for something of size 4, it's due to an explicit (e.g., | 58 // alignment of 8 for something of size 4, it's due to an explicit (e.g., |
| 59 // #pragma align) alignment specification, which MSVS *does* respect. We want | 59 // #pragma align) alignment specification, which MSVS *does* respect. We want |
| 60 // this in particular to check that various Options structs are aligned. | 60 // this in particular to check that various Options structs are aligned. |
| 61 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 61 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 62 template <> | 62 template <> |
| 63 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<4, 8>( | 63 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 8>( |
| 64 const void* pointer) { | 64 const void* pointer) { |
| 65 CHECK(pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0); | 65 CHECK(pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0); |
| 66 } | 66 } |
| 67 template <> | 67 template <> |
| 68 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( | 68 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( |
| 69 const void* pointer) { | 69 const void* pointer) { |
| 70 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; | 70 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; |
| 71 } | 71 } |
| 72 #else | 72 #else |
| 73 template MOJO_SYSTEM_IMPL_EXPORT void CheckUserPointerHelper<4, 8>( | 73 template MOJO_SYSTEM_IMPL_EXPORT void CheckUserPointer<4, 8>( |
| 74 const void*); | 74 const void*); |
| 75 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( | 75 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( |
| 76 const void*); | 76 const void*); |
| 77 #endif | 77 #endif |
| 78 | 78 |
| 79 template <size_t size, size_t alignment> | 79 template <size_t size, size_t alignment> |
| 80 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper( | 80 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount(const void* pointer, |
| 81 const void* pointer, | 81 size_t count) { |
| 82 size_t count) { | |
| 83 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); | 82 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); |
| 84 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); | 83 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); |
| 85 } | 84 } |
| 86 | 85 |
| 87 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 86 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
| 88 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<1, 1>( | 87 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount<1, 1>( |
| 89 const void*, size_t); | 88 const void*, size_t); |
| 90 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<4, 4>( | 89 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount<4, 4>( |
| 91 const void*, size_t); | 90 const void*, size_t); |
| 92 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<8, 8>( | 91 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount<8, 8>( |
| 93 const void*, size_t); | 92 const void*, size_t); |
| 94 | 93 |
| 94 template <size_t alignment> |
| 95 void CheckUserPointerWithSize(const void* pointer, size_t size) { |
| 96 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
| 97 // check that it's non-null and aligned. (A faster user mode implementation is |
| 98 // also possible if this check is skipped.) |
| 99 CHECK(size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer))); |
| 100 } |
| 101 |
| 102 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
| 103 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<1>(const void*, |
| 104 size_t); |
| 105 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<4>(const void*, |
| 106 size_t); |
| 107 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize<8>(const void*, |
| 108 size_t); |
| 109 |
| 95 template <size_t size, size_t alignment> | 110 template <size_t size, size_t alignment> |
| 96 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { | 111 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { |
| 97 if (count > std::numeric_limits<size_t>::max() / size) | 112 if (count > std::numeric_limits<size_t>::max() / size) |
| 98 return false; | 113 return false; |
| 99 | 114 |
| 100 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | 115 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
| 101 // check that it's non-null and aligned if |count| is nonzero. (A faster user | 116 // check that it's non-null and aligned if |count| is nonzero. (A faster user |
| 102 // mode implementation is also possible if this check is skipped.) | 117 // mode implementation is also possible if this check is skipped.) |
| 103 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); | 118 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); |
| 104 } | 119 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 // Explicitly instantiate the alignments we need. Add instantiations as needed. | 139 // Explicitly instantiate the alignments we need. Add instantiations as needed. |
| 125 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, | 140 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, |
| 126 size_t); | 141 size_t); |
| 127 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, | 142 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, |
| 128 size_t); | 143 size_t); |
| 129 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, | 144 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, |
| 130 size_t); | 145 size_t); |
| 131 | 146 |
| 132 } // namespace system | 147 } // namespace system |
| 133 } // namespace mojo | 148 } // namespace mojo |
| OLD | NEW |