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

Side by Side Diff: util/mac/checked_mach_address_range_test.cc

Issue 513453002: Add CheckedMachAddressRange and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 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 Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/mac/checked_mach_address_range.h"
16
17 #include <mach/mach.h>
18
19 #include <limits>
20
21 #include "base/basictypes.h"
22 #include "base/strings/stringprintf.h"
23 #include "build/build_config.h"
24 #include "gtest/gtest.h"
25 #include "util/mac/process_reader.h"
26
27 namespace {
28
29 using namespace crashpad;
30
31 #if defined(ARCH_CPU_64_BITS)
32 const bool kValid64Invalid32 = true;
33 #else
34 const bool kValid64Invalid32 = false;
35 #endif
36
37 const mach_vm_address_t kMaxAddress =
38 std::numeric_limits<mach_vm_address_t>::max();
39 const mach_vm_address_t kMaxSize = std::numeric_limits<mach_vm_size_t>::max();
40
41 TEST(CheckedMachAddressRange, IsValid) {
42 const struct TestData {
43 mach_vm_address_t base;
44 mach_vm_size_t size;
45 bool valid;
46 } kTestData[] = {
47 {0, 0, true},
48 {0, 1, true},
49 {0, 2, true},
50 {0, 0x7fffffff, true},
51 {0, 0x80000000, true},
52 {0, 0xfffffffe, true},
53 {0, 0xffffffff, true},
54 {0, kMaxSize, kValid64Invalid32},
55 {1, 0, true},
56 {1, 1, true},
57 {1, 2, true},
58 {1, 0x7fffffff, true},
59 {1, 0x80000000, true},
60 {1, 0xfffffffe, true},
61 {1, 0xffffffff, kValid64Invalid32},
62 {1, kMaxSize, false},
63 {0x7fffffff, 0, true},
64 {0x7fffffff, 1, true},
65 {0x7fffffff, 2, true},
66 {0x7fffffff, 0x7fffffff, true},
67 {0x7fffffff, 0x80000000, true},
68 {0x7fffffff, 0xfffffffe, kValid64Invalid32},
69 {0x7fffffff, 0xffffffff, kValid64Invalid32},
70 {0x80000000, 0, true},
71 {0x80000000, 1, true},
72 {0x80000000, 2, true},
73 {0x80000000, 0x7fffffff, true},
74 {0x80000000, 0x80000000, kValid64Invalid32},
75 {0x80000000, 0xfffffffe, kValid64Invalid32},
76 {0x80000000, 0xffffffff, kValid64Invalid32},
77 {0xfffffffe, 0, true},
78 {0xfffffffe, 1, true},
79 {0xfffffffe, 2, kValid64Invalid32},
80 {0xfffffffe, 0x7fffffff, kValid64Invalid32},
81 {0xfffffffe, 0x80000000, kValid64Invalid32},
82 {0xfffffffe, 0xfffffffe, kValid64Invalid32},
83 {0xfffffffe, 0xffffffff, kValid64Invalid32},
84 {0xffffffff, 0, true},
85 {0xffffffff, 1, kValid64Invalid32},
86 {0xffffffff, 2, kValid64Invalid32},
87 {0xffffffff, 0x7fffffff, kValid64Invalid32},
88 {0xffffffff, 0x80000000, kValid64Invalid32},
89 {0xffffffff, 0xfffffffe, kValid64Invalid32},
90 {0xffffffff, 0xffffffff, kValid64Invalid32},
91 {kMaxAddress, 0, kValid64Invalid32},
92 {kMaxAddress, 1, false},
93 };
94
95 ProcessReader process_reader;
96 ASSERT_TRUE(process_reader.Initialize(mach_task_self()));
97
98 for (size_t index = 0; index < arraysize(kTestData); ++index) {
99 const TestData& testcase = kTestData[index];
100 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx",
101 index,
102 testcase.base,
103 testcase.size));
104
105 CheckedMachAddressRange range(
106 &process_reader, testcase.base, testcase.size);
107 EXPECT_EQ(testcase.valid, range.IsValid());
108 }
109 }
110
111 TEST(CheckedMachAddressRange, ContainsValue) {
112 const struct TestData {
113 mach_vm_address_t value;
114 bool valid;
115 } kTestData[] = {
116 {0, false},
117 {1, false},
118 {0x1fff, false},
119 {0x2000, true},
120 {0x2001, true},
121 {0x2ffe, true},
122 {0x2fff, true},
123 {0x3000, false},
124 {0x3001, false},
125 {0x7fffffff, false},
126 {0x80000000, false},
127 {0x80000001, false},
128 {0x80001fff, false},
129 {0x80002000, false},
130 {0x80002001, false},
131 {0x80002ffe, false},
132 {0x80002fff, false},
133 {0x80003000, false},
134 {0x80003001, false},
135 {0xffffcfff, false},
136 {0xffffdfff, false},
137 {0xffffefff, false},
138 {0xffffffff, false},
139 {0x100000000, false},
140 {kMaxAddress, false},
141 };
142
143 ProcessReader process_reader;
144 ASSERT_TRUE(process_reader.Initialize(mach_task_self()));
145
146 CheckedMachAddressRange parent_range(&process_reader, 0x2000, 0x1000);
147 ASSERT_TRUE(parent_range.IsValid());
148
149 for (size_t index = 0; index < arraysize(kTestData); ++index) {
150 const TestData& testcase = kTestData[index];
151 SCOPED_TRACE(
152 base::StringPrintf("index %zu, value 0x%llx", index, testcase.value));
153
154 EXPECT_EQ(testcase.valid, parent_range.ContainsValue(testcase.value));
155 }
156
157 CheckedMachAddressRange parent_range_64(&process_reader, 0x100000000, 0x1000);
158 ASSERT_EQ(kValid64Invalid32, parent_range_64.IsValid());
159 if (parent_range_64.IsValid()) {
160 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff));
161 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000));
162 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001));
163 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff));
164 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000));
165 }
166 }
167
168 TEST(CheckedMachAddressRange, ContainsRange) {
169 const struct TestData {
170 mach_vm_address_t base;
171 mach_vm_size_t size;
172 bool valid;
173 } kTestData[] = {
174 {0, 0, false},
175 {0, 1, false},
176 {0x2000, 0x1000, true},
177 {0, 0x2000, false},
178 {0x3000, 0x1000, false},
179 {0x1800, 0x1000, false},
180 {0x2800, 0x1000, false},
181 {0x2000, 0x800, true},
182 {0x2800, 0x800, true},
183 {0x2400, 0x800, true},
184 {0x2800, 0, true},
185 {0x2000, 0xffffdfff, false},
186 {0x2800, 0xffffd7ff, false},
187 {0x3000, 0xffffcfff, false},
188 {0xfffffffe, 1, false},
189 {0xffffffff, 0, false},
190 {0x1fff, 0, false},
191 {0x2000, 0, true},
192 {0x2001, 0, true},
193 {0x2fff, 0, true},
194 {0x3000, 0, true},
195 {0x3001, 0, false},
196 {0x1fff, 1, false},
197 {0x2000, 1, true},
198 {0x2001, 1, true},
199 {0x2fff, 1, true},
200 {0x3000, 1, false},
201 {0x3001, 1, false},
202 };
203
204 ProcessReader process_reader;
205 ASSERT_TRUE(process_reader.Initialize(mach_task_self()));
206
207 CheckedMachAddressRange parent_range(&process_reader, 0x2000, 0x1000);
208 ASSERT_TRUE(parent_range.IsValid());
209
210 for (size_t index = 0; index < arraysize(kTestData); ++index) {
211 const TestData& testcase = kTestData[index];
212 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx",
213 index,
214 testcase.base,
215 testcase.size));
216
217 CheckedMachAddressRange child_range(
218 &process_reader, testcase.base, testcase.size);
219 ASSERT_TRUE(child_range.IsValid());
220 EXPECT_EQ(testcase.valid, parent_range.ContainsRange(child_range));
221 }
222
223 CheckedMachAddressRange parent_range_64(&process_reader, 0x100000000, 0x1000);
224 ASSERT_EQ(kValid64Invalid32, parent_range_64.IsValid());
225 if (parent_range_64.IsValid()) {
226 CheckedMachAddressRange child_range_64(&process_reader, 0xffffffff, 2);
227 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64));
228
229 child_range_64.SetRange(&process_reader, 0x100000000, 2);
230 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64));
231
232 child_range_64.SetRange(&process_reader, 0x100000ffe, 2);
233 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64));
234
235 child_range_64.SetRange(&process_reader, 0x100000fff, 2);
236 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64));
237 }
238 }
239
240 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698