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 12namespace mojo { 13namespace system { 14namespace internal { 15 16template <size_t alignment> 17bool IsAligned(const void* pointer) { 18 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0; 19} 20 21// MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for 22// which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T 23#if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) 24template <> 25bool IsAligned<8>(const void* pointer) { 26 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; 27} 28#endif 29 30template <size_t size, size_t alignment> 31void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer) { 32 CHECK(pointer && IsAligned<alignment>(pointer)); 33} 34 35// Explicitly instantiate the sizes we need. Add instantiations as needed. 36template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<1, 1>(const void*); 37template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 4>(const void*); 38template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 4>(const void*); 39template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 8>(const void*); 40 41template <size_t size, size_t alignment> 42void MOJO_SYSTEM_IMPL_EXPORT 43CheckUserPointerWithCount(const void* pointer, size_t count) { 44 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); 45 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); 46} 47 48// Explicitly instantiate the sizes we need. Add instantiations as needed. 49template void MOJO_SYSTEM_IMPL_EXPORT 50 CheckUserPointerWithCount<1, 1>(const void*, size_t); 51template void MOJO_SYSTEM_IMPL_EXPORT 52 CheckUserPointerWithCount<4, 4>(const void*, size_t); 53template void MOJO_SYSTEM_IMPL_EXPORT 54 CheckUserPointerWithCount<8, 4>(const void*, size_t); 55template void MOJO_SYSTEM_IMPL_EXPORT 56 CheckUserPointerWithCount<8, 8>(const void*, size_t); 57 58template <size_t alignment> 59void CheckUserPointerWithSize(const void* pointer, size_t size) { 60 // TODO(vtl): If running in kernel mode, do a full verification. For now, just 61 // check that it's non-null and aligned. (A faster user mode implementation is 62 // also possible if this check is skipped.) 63 CHECK(size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer))); 64} 65 66// Explicitly instantiate the sizes we need. Add instantiations as needed. 67template void MOJO_SYSTEM_IMPL_EXPORT 68 CheckUserPointerWithSize<1>(const void*, size_t); 69template void MOJO_SYSTEM_IMPL_EXPORT 70 CheckUserPointerWithSize<4>(const void*, size_t); 71// Whereas the other |Check...()| functions are usually used with integral typs 72// or arrays of integral types, this one is used with Options structs for which 73// alignment has been explicitly been specified (using |MOJO_ALIGNAS()|), which 74// MSVS *does* respect. 75#if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) 76template <> 77void MOJO_SYSTEM_IMPL_EXPORT 78CheckUserPointerWithSize<8>(const void* pointer, size_t size) { 79 CHECK(size == 0 || 80 (!!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0)); 81} 82#else 83template void MOJO_SYSTEM_IMPL_EXPORT 84 CheckUserPointerWithSize<8>(const void*, size_t); 85#endif 86 87} // namespace internal 88} // namespace system 89} // namespace mojo 90