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

Unified Diff: mojo/public/cpp/bindings/lib/bounds_checker.h

Issue 289333002: Mojo cpp bindings: validation logic for incoming messages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: simplify BoundsChecker 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/public/cpp/bindings/lib/bounds_checker.h
diff --git a/mojo/public/cpp/bindings/lib/bounds_checker.h b/mojo/public/cpp/bindings/lib/bounds_checker.h
new file mode 100644
index 0000000000000000000000000000000000000000..5e4daeadbfff47bb9fb67801f405d3896e9b1ca7
--- /dev/null
+++ b/mojo/public/cpp/bindings/lib/bounds_checker.h
@@ -0,0 +1,64 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
+
+#include <stdint.h>
+
+#include "mojo/public/cpp/system/macros.h"
+
+namespace mojo {
+
+class Handle;
+
+namespace internal {
+
+// BoundsChecker is used to validate object sizes, pointers and handle indices
+// for payload of incoming messages.
+class BoundsChecker {
+ public:
+ // [data, data + data_num_bytes) specifies the valid memory range.
+ // [0, num_handles) specifies the valid range of handle indices.
+ BoundsChecker(const void* data, uint32_t data_num_bytes,
+ size_t num_handles);
+
+ ~BoundsChecker();
+
+ // Claims the specified memory range.
+ // The method succeeds if the range is within the unclaimed range. (Please see
+ // the comments for IsWithinUnclaimedRange().)
+ bool ClaimMemory(const void* position, uint32_t num_bytes);
+
+ // Claims the specified encoded handle (which is basically a handle index).
+ // The method succeeds if:
+ // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|, or
+ // - the handle is contained inside the valid range of handle indices, and
+ // greater than the max handle index that has been claimed.
+ bool ClaimHandle(const Handle& encoded_handle);
+
+ // Returns true if:
+ // - the specified range is not empty, and
+ // - the range is contained inside the valid memory range, and
+ // - |position| is greater than the max address that has been claimed.
+ bool IsWithinUnclaimedRange(const void* position, uint32_t num_bytes) const;
Tom Sepez 2014/05/28 17:12:52 nit: Looks like when this is used outside of tests
yzshen1 2014/05/28 17:27:52 One issue is that the opposite of "within unclaime
+
+ private:
+ bool InternalIsWithinUnclaimedRange(uintptr_t begin, uintptr_t end) const;
+
+ // [unclaimed_data_begin_, data_end_) is the unclaimed memory range.
+ uintptr_t unclaimed_data_begin_;
+ uintptr_t data_end_;
+
+ // [unclaimed_handle_begin_, handle_end_) is the unclaimed handle index range.
+ uint32_t unclaimed_handle_begin_;
+ uint32_t handle_end_;
+
+ MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChecker);
+};
+
+} // namespace internal
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
« no previous file with comments | « mojo/public/cpp/bindings/lib/bindings_serialization.cc ('k') | mojo/public/cpp/bindings/lib/bounds_checker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698