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

Unified 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: Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: mojo/system/memory.cc
diff --git a/mojo/system/memory.cc b/mojo/system/memory.cc
index dd8b3d2334ada19a88fbf647a0974a260c0383ba..6199902bf9d1dece3258ac887fedda8249d503ae 100644
--- a/mojo/system/memory.cc
+++ b/mojo/system/memory.cc
@@ -11,24 +11,62 @@
namespace mojo {
namespace system {
-template <size_t size>
-bool VerifyUserPointerForSize(const void* pointer, size_t count) {
+namespace internal {
+
+template <size_t size, size_t alignment>
+bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer) {
+ // TODO(vtl): If running in kernel mode, do a full verification. For now, just
+ // check that it's non-null and aligned. (A faster user mode implementation is
+ // also possible if this check is skipped.)
+ return !!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0;
+}
+
+// Explicitly instantiate the sizes we need. Add instantiations as needed.
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<1, 1>(
+ const void*);
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<4, 4>(
+ const void*);
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerHelper<8, 8>(
+ const void*);
+
+template <size_t size, size_t alignment>
+bool VerifyUserPointerWithCountHelper(const void* pointer, size_t count) {
if (count > std::numeric_limits<size_t>::max() / size)
return false;
// TODO(vtl): If running in kernel mode, do a full verification. For now, just
- // check that it's non-null if |size| is nonzero. (A faster user mode
- // implementation is also possible if this check is skipped.)
- return count == 0 || !!pointer;
+ // check that it's non-null and aligned if |count| is nonzero. (A faster user
+ // mode implementation is also possible if this check is skipped.)
+ return count == 0 ||
+ (!!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0);
}
// Explicitly instantiate the sizes we need. Add instantiations as needed.
-template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<1>(
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<1, 1>(
const void*, size_t);
-template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<4>(
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<4, 4>(
const void*, size_t);
-template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerForSize<8>(
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithCountHelper<8, 8>(
const void*, size_t);
+} // nameespace internal
+
+template <size_t alignment>
+bool VerifyUserPointerWithSize(const void* pointer, size_t size) {
+ // TODO(vtl): If running in kernel mode, do a full verification. For now, just
+ // check that it's non-null and aligned. (A faster user mode implementation is
+ // also possible if this check is skipped.)
+ return size == 0 ||
+ (!!pointer && reinterpret_cast<uintptr_t>(pointer) % alignment == 0);
+}
+
+// Explicitly instantiate the alignments we need. Add instantiations as needed.
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<1>(const void*,
+ size_t);
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<4>(const void*,
+ size_t);
+template MOJO_SYSTEM_IMPL_EXPORT bool VerifyUserPointerWithSize<8>(const void*,
+ size_t);
+
} // namespace system
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698