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 3 // found in the LICENSE file. 4 5 #include "mojo/system/memory.h" 6 7 #include <limits> 8 9 #include "base/logging.h" 10 #include "build/build_config.h" 11 12 namespace mojo { 13 namespace system { 14 15 namespace internal { 16 17 template <size_t alignment> 18 bool IsAligned(const void* pointer) { 19 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0; 20 } 21 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 24 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) 25 template <> 26 bool IsAligned<8>(const void* pointer) { 27 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; 28 } 29 #endif 30 31 template <size_t size, size_t alignment> 32 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) { 33 // 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 35 // also possible if this check is skipped.) 36 return !!pointer && IsAligned<alignment>(pointer); 37 } 38 39 // Explicitly instantiate the sizes we need. Add instantiations as needed. 40 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<1, 1>( 41 const void*); 42 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 4>( 43 const void*); 44 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<8, 8>( 45 const void*); 46 // 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., 48 // #pragma align) alignment specification, which MSVS *does* respect. We want 49 // this in particular to check that various Options structs are aligned. 50 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) 51 template <> 52 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper<4, 8>( 53 const void* pointer) { 54 return !!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0; 55 } 56 #else 57 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 8>( 58 const void*); 59 #endif 60 61 template <size_t size, size_t alignment> 62 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) { 63 if (count > std::numeric_limits<size_t>::max() / size) 64 return false; 65 66 // 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 68 // mode implementation is also possible if this check is skipped.) 69 return count == 0 || (!!pointer && IsAligned<alignment>(pointer)); 70 } 71 72 // Explicitly instantiate the sizes we need. Add instantiations as needed. 73 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<1, 1>( 74 const void*, size_t); 75 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<4, 4>( 76 const void*, size_t); 77 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper<8, 8>( 78 const void*, size_t); 79 80 } // nameespace internal 81 82 template <size_t alignment> 83 bool VerifyUserPointerWithSize(const void* pointer, size_t size) { 84 // 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 86 // also possible if this check is skipped.) 87 return size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer)); 88 } 89 90 // Explicitly instantiate the alignments we need. Add instantiations as needed. 91 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<1>(const void*, 92 size_t); 93 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<4>(const void*, 94 size_t); 95 template bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize<8>(const void*, 96 size_t); 97 98 } // namespace system 99 } // namespace mojo 100