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

Side by Side Diff: mojo/public/cpp/bindings/lib/bounds_checker.cc

Issue 2064903002: Mojo: Report bindings validation errors via MojoNotifyBadMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 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
OLDNEW
(Empty)
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.
4
5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "base/logging.h"
11 #include "mojo/public/cpp/bindings/lib/serialization_util.h"
12 #include "mojo/public/cpp/system/handle.h"
13
14 namespace mojo {
15 namespace internal {
16
17 BoundsChecker::BoundsChecker(const void* data,
18 uint32_t data_num_bytes,
19 size_t num_handles)
20 : data_begin_(reinterpret_cast<uintptr_t>(data)),
21 data_end_(data_begin_ + data_num_bytes),
22 handle_begin_(0),
23 handle_end_(static_cast<uint32_t>(num_handles)) {
24 if (data_end_ < data_begin_) {
25 // The calculation of |data_end_| overflowed.
26 // It shouldn't happen but if it does, set the range to empty so
27 // IsValidRange() and ClaimMemory() always fail.
28 NOTREACHED();
29 data_end_ = data_begin_;
30 }
31 if (handle_end_ < num_handles) {
32 // Assigning |num_handles| to |handle_end_| overflowed.
33 // It shouldn't happen but if it does, set the handle index range to empty.
34 NOTREACHED();
35 handle_end_ = 0;
36 }
37 }
38
39 BoundsChecker::~BoundsChecker() {
40 }
41
42 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) {
43 uintptr_t begin = reinterpret_cast<uintptr_t>(position);
44 uintptr_t end = begin + num_bytes;
45
46 if (!InternalIsValidRange(begin, end))
47 return false;
48
49 data_begin_ = end;
50 return true;
51 }
52
53 bool BoundsChecker::ClaimHandle(const Handle_Data& encoded_handle) {
54 uint32_t index = encoded_handle.value;
55 if (index == kEncodedInvalidHandleValue)
56 return true;
57
58 if (index < handle_begin_ || index >= handle_end_)
59 return false;
60
61 // |index| + 1 shouldn't overflow, because |index| is not the max value of
62 // uint32_t (it is less than |handle_end_|).
63 handle_begin_ = index + 1;
64 return true;
65 }
66
67 bool BoundsChecker::IsValidRange(const void* position,
68 uint32_t num_bytes) const {
69 uintptr_t begin = reinterpret_cast<uintptr_t>(position);
70 uintptr_t end = begin + num_bytes;
71
72 return InternalIsValidRange(begin, end);
73 }
74
75 bool BoundsChecker::InternalIsValidRange(uintptr_t begin, uintptr_t end) const {
76 return end > begin && begin >= data_begin_ && end <= data_end_;
77 }
78
79 } // namespace internal
80 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/bounds_checker.h ('k') | mojo/public/cpp/bindings/lib/filter_chain.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698