Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: mojo/system/memory.cc

Issue 304303006: Mojo: Specify/check alignment of pointers more carefully. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix msvs Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/memory.h ('k') | mojo/system/memory_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 11
11 namespace mojo { 12 namespace mojo {
12 namespace system { 13 namespace system {
13 14
14 template <size_t size> 15 namespace internal {
15 bool VerifyUserPointerForSize(const void* pointer, size_t count) { 16
17 template <size_t alignment>
18 bool IsAligned(const void* pointer) {
19 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0;
20 }
21
22 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
23 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for
24 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T
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 MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<1, 1>(
41 const void*);
42 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 4>(
43 const void*);
44 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<8, 8>(
45 const void*);
46
47 template <size_t size, size_t alignment>
48 bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) {
16 if (count > std::numeric_limits<size_t>::max() / size) 49 if (count > std::numeric_limits<size_t>::max() / size)
17 return false; 50 return false;
18 51
19 // TODO(vtl): If running in kernel mode, do a full verification. For now, just 52 // TODO(vtl): If running in kernel mode, do a full verification. For now, just
20 // check that it's non-null if |size| is nonzero. (A faster user mode 53 // check that it's non-null and aligned if |count| is nonzero. (A faster user
21 // implementation is also possible if this check is skipped.) 54 // mode implementation is also possible if this check is skipped.)
22 return count == 0 || !!pointer; 55 return count == 0 || (!!pointer && IsAligned<alignment>(pointer));
23 } 56 }
24 57
25 // Explicitly instantiate the sizes we need. Add instantiations as needed. 58 // Explicitly instantiate the sizes we need. Add instantiations as needed.
26 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<1>( 59 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<1, 1>(
27 const void*, size_t); 60 const void*, size_t);
28 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<4>( 61 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<4, 4>(
29 const void*, size_t); 62 const void*, size_t);
30 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<8>( 63 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<8, 8>(
31 const void*, size_t); 64 const void*, size_t);
32 65
66 } // nameespace internal
67
68 template <size_t alignment>
69 bool VerifyUserPointerWithSize(const void* pointer, size_t size) {
70 // TODO(vtl): If running in kernel mode, do a full verification. For now, just
71 // check that it's non-null and aligned. (A faster user mode implementation is
72 // also possible if this check is skipped.)
73 return size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer));
74 }
75
76 // Explicitly instantiate the alignments we need. Add instantiations as needed.
77 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<1>(const void*,
78 size_t);
79 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<4>(const void*,
80 size_t);
81 template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<8>(const void*,
82 size_t);
83
33 } // namespace system 84 } // namespace system
34 } // namespace mojo 85 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/memory.h ('k') | mojo/system/memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698