1 // Copyright 2014 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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
10 #include "mojo/public/cpp/system/macros.h"
18 // BoundsChecker is used to validate object sizes, pointers and handle indices
19 // for payload of incoming messages.
22 // [data, data + data_num_bytes) specifies the initial valid memory range.
23 // [0, num_handles) specifies the initial valid range of handle indices.
24 BoundsChecker(const void* data, uint32_t data_num_bytes,
29 // Claims the specified memory range.
30 // The method succeeds if the range is valid to claim. (Please see
31 // the comments for IsValidRange().)
32 // On success, the valid memory range is shrinked to begin right after the end
33 // of the claimed range.
34 bool ClaimMemory(const void* position, uint32_t num_bytes);
36 // Claims the specified encoded handle (which is basically a handle index).
37 // The method succeeds if:
38 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|.
39 // - the handle is contained inside the valid range of handle indices. In this
40 // case, the valid range is shinked to begin right after the claimed handle.
41 bool ClaimHandle(const Handle& encoded_handle);
43 // Returns true if the specified range is not empty, and the range is
44 // contained inside the valid memory range.
45 bool IsValidRange(const void* position, uint32_t num_bytes) const;
48 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const;
50 // [data_begin_, data_end_) is the valid memory range.
51 uintptr_t data_begin_;
54 // [handle_begin_, handle_end_) is the valid handle index range.
55 uint32_t handle_begin_;
58 MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChecker);
61 } // namespace internal
64 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_