| 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) { |
| 33 CHECK(pointer && IsAligned<alignment>(pointer)); |
| 34 } |
| 35 |
| 36 template <size_t size, size_t alignment> |
| 32 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { | 37 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { |
| 33 // 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 |
| 34 // 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 |
| 35 // also possible if this check is skipped.) | 40 // also possible if this check is skipped.) |
| 36 return !!pointer && IsAligned<alignment>(pointer); | 41 return !!pointer && IsAligned<alignment>(pointer); |
| 37 } | 42 } |
| 38 | 43 |
| 39 // 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>( |
| 46 const void*); |
| 47 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<4, 4>( |
| 48 const void*); |
| 49 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<8, 8>( |
| 50 const void*); |
| 40 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( | 51 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( |
| 41 const void*); | 52 const void*); |
| 42 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( | 53 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( |
| 43 const void*); | 54 const void*); |
| 44 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( | 55 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( |
| 45 const void*); | 56 const void*); |
| 46 // Notwithstanding the comments above about MSVS, whenever we expect an | 57 // Notwithstanding the comments above about MSVS, whenever we expect an |
| 47 // 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., |
| 48 // #pragma align) alignment specification, which MSVS *does* respect. We want | 59 // #pragma align) alignment specification, which MSVS *does* respect. We want |
| 49 // this in particular to check that various Options structs are aligned. | 60 // this in particular to check that various Options structs are aligned. |
| 50 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 61 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 51 template <> | 62 template <> |
| 63 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerHelper<4, 8>( |
| 64 const void* pointer) { |
| 65 CHECK(pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0); |
| 66 } |
| 67 template <> |
| 52 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( | 68 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( |
| 53 const void* pointer) { | 69 const void* pointer) { |
| 54 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; | 70 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; |
| 55 } | 71 } |
| 56 #else | 72 #else |
| 73 template MOJO_SYSTEM_IMPL_EXPORT void CheckUserPointerHelper<4, 8>( |
| 74 const void*); |
| 57 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( | 75 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( |
| 58 const void*); | 76 const void*); |
| 59 #endif | 77 #endif |
| 60 | 78 |
| 61 template <size_t size, size_t alignment> | 79 template <size_t size, size_t alignment> |
| 80 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper( |
| 81 const void* pointer, |
| 82 size_t count) { |
| 83 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); |
| 84 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); |
| 85 } |
| 86 |
| 87 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
| 88 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<1, 1>( |
| 89 const void*, size_t); |
| 90 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<4, 4>( |
| 91 const void*, size_t); |
| 92 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCountHelper<8, 8>( |
| 93 const void*, size_t); |
| 94 |
| 95 template <size_t size, size_t alignment> |
| 62 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { | 96 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { |
| 63 if (count > std::numeric_limits<size_t>::max() / size) | 97 if (count > std::numeric_limits<size_t>::max() / size) |
| 64 return false; | 98 return false; |
| 65 | 99 |
| 66 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | 100 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
| 67 // check that it's non-null and aligned if |count| is nonzero. (A faster user | 101 // check that it's non-null and aligned if |count| is nonzero. (A faster user |
| 68 // mode implementation is also possible if this check is skipped.) | 102 // mode implementation is also possible if this check is skipped.) |
| 69 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); | 103 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); |
| 70 } | 104 } |
| 71 | 105 |
| 72 // Explicitly instantiate the sizes we need. Add instantiations as needed. | 106 // Explicitly instantiate the sizes we need. Add instantiations as needed. |
| 73 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<1, 1>( | 107 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<1, 1>( |
| 74 const void*, size_t); | 108 const void*, size_t); |
| 75 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<4, 4>( | 109 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<4, 4>( |
| 76 const void*, size_t); | 110 const void*, size_t); |
| 77 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<8, 8>( | 111 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<8, 8>( |
| 78 const void*, size_t); | 112 const void*, size_t); |
| 79 | 113 |
| 80 } // nameespace internal | 114 } // namespace internal |
| 81 | 115 |
| 82 template <size_t alignment> | 116 template <size_t alignment> |
| 83 bool VerifyUserPointerWithSize(const void* pointer, size_t size) { | 117 bool VerifyUserPointerWithSize(const void* pointer, size_t size) { |
| 84 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | 118 // TODO(vtl): If running in kernel mode, do a full verification. For now, just |
| 85 // check that it's non-null and aligned. (A faster user mode implementation is | 119 // check that it's non-null and aligned. (A faster user mode implementation is |
| 86 // also possible if this check is skipped.) | 120 // also possible if this check is skipped.) |
| 87 return size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer)); | 121 return size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer)); |
| 88 } | 122 } |
| 89 | 123 |
| 90 // Explicitly instantiate the alignments we need. Add instantiations as needed. | 124 // Explicitly instantiate the alignments we need. Add instantiations as needed. |
| 91 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, | 125 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, |
| 92 size_t); | 126 size_t); |
| 93 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, | 127 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, |
| 94 size_t); | 128 size_t); |
| 95 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, | 129 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, |
| 96 size_t); | 130 size_t); |
| 97 | 131 |
| 98 } // namespace system | 132 } // namespace system |
| 99 } // namespace mojo | 133 } // namespace mojo |
| OLD | NEW |