OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/mac/checked_mach_address_range.h" | 15 #include "util/numeric/checked_address_range.h" |
16 | |
17 #include <mach/mach.h> | |
18 | 16 |
19 #include <limits> | 17 #include <limits> |
20 | 18 |
21 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/format_macros.h" |
22 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
23 #include "build/build_config.h" | 22 #include "build/build_config.h" |
24 #include "gtest/gtest.h" | 23 #include "gtest/gtest.h" |
25 | 24 |
26 namespace crashpad { | 25 namespace crashpad { |
27 namespace test { | 26 namespace test { |
28 namespace { | 27 namespace { |
29 | 28 |
| 29 using CheckedAddressRange = |
| 30 internal::CheckedAddressRangeGeneric<uint64_t, uint64_t>; |
| 31 |
30 enum Validity { | 32 enum Validity { |
31 kInvalid = false, | 33 kInvalid = false, |
32 kValid, | 34 kValid, |
33 kValid64Invalid32, | 35 kValid64Invalid32, |
34 }; | 36 }; |
35 | 37 |
36 bool ExpectationForValidity32(Validity validity) { | 38 bool ExpectationForValidity32(Validity validity) { |
37 return validity == kValid; | 39 return validity == kValid; |
38 } | 40 } |
39 | 41 |
40 bool ExpectationForValidity64(Validity validity) { | 42 bool ExpectationForValidity64(Validity validity) { |
41 return validity == kValid || validity == kValid64Invalid32; | 43 return validity == kValid || validity == kValid64Invalid32; |
42 } | 44 } |
43 | 45 |
44 TEST(CheckedMachAddressRange, IsValid) { | 46 TEST(CheckedAddressRange, IsValid) { |
45 const struct TestData { | 47 const struct TestData { |
46 mach_vm_address_t base; | 48 uint64_t base; |
47 mach_vm_size_t size; | 49 uint64_t size; |
48 Validity validity; | 50 Validity validity; |
49 } kTestData[] = { | 51 } kTestData[] = { |
50 {0, 0, kValid}, | 52 {0, 0, kValid}, |
51 {0, 1, kValid}, | 53 {0, 1, kValid}, |
52 {0, 2, kValid}, | 54 {0, 2, kValid}, |
53 {0, 0x7fffffff, kValid}, | 55 {0, 0x7fffffff, kValid}, |
54 {0, 0x80000000, kValid}, | 56 {0, 0x80000000, kValid}, |
55 {0, 0xfffffffe, kValid}, | 57 {0, 0xfffffffe, kValid}, |
56 {0, 0xffffffff, kValid}, | 58 {0, 0xffffffff, kValid}, |
57 {0, 0xffffffffffffffff, kValid64Invalid32}, | 59 {0, 0xffffffffffffffff, kValid64Invalid32}, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 {0x8000000000000000, 0xffffffffffffffff, kInvalid}, | 112 {0x8000000000000000, 0xffffffffffffffff, kInvalid}, |
111 {0xfffffffffffffffe, 0, kValid64Invalid32}, | 113 {0xfffffffffffffffe, 0, kValid64Invalid32}, |
112 {0xfffffffffffffffe, 1, kValid64Invalid32}, | 114 {0xfffffffffffffffe, 1, kValid64Invalid32}, |
113 {0xfffffffffffffffe, 2, kInvalid}, | 115 {0xfffffffffffffffe, 2, kInvalid}, |
114 {0xffffffffffffffff, 0, kValid64Invalid32}, | 116 {0xffffffffffffffff, 0, kValid64Invalid32}, |
115 {0xffffffffffffffff, 1, kInvalid}, | 117 {0xffffffffffffffff, 1, kInvalid}, |
116 }; | 118 }; |
117 | 119 |
118 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 120 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
119 const TestData& testcase = kTestData[index]; | 121 const TestData& testcase = kTestData[index]; |
120 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", | 122 SCOPED_TRACE(base::StringPrintf("index %" PRIuS |
| 123 ", base 0x%llx, size 0x%llx", |
121 index, | 124 index, |
122 testcase.base, | 125 testcase.base, |
123 testcase.size)); | 126 testcase.size)); |
124 | 127 |
125 CheckedMachAddressRange range_32(false, testcase.base, testcase.size); | 128 CheckedAddressRange range_32(false, testcase.base, testcase.size); |
126 EXPECT_EQ(ExpectationForValidity32(testcase.validity), range_32.IsValid()); | 129 EXPECT_EQ(ExpectationForValidity32(testcase.validity), range_32.IsValid()); |
127 | 130 |
128 CheckedMachAddressRange range_64(true, testcase.base, testcase.size); | 131 CheckedAddressRange range_64(true, testcase.base, testcase.size); |
129 EXPECT_EQ(ExpectationForValidity64(testcase.validity), range_64.IsValid()); | 132 EXPECT_EQ(ExpectationForValidity64(testcase.validity), range_64.IsValid()); |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 TEST(CheckedMachAddressRange, ContainsValue) { | 136 TEST(CheckedAddressRange, ContainsValue) { |
134 const struct TestData { | 137 const struct TestData { |
135 mach_vm_address_t value; | 138 uint64_t value; |
136 bool expectation; | 139 bool expectation; |
137 } kTestData[] = { | 140 } kTestData[] = { |
138 {0, false}, | 141 {0, false}, |
139 {1, false}, | 142 {1, false}, |
140 {0x1fff, false}, | 143 {0x1fff, false}, |
141 {0x2000, true}, | 144 {0x2000, true}, |
142 {0x2001, true}, | 145 {0x2001, true}, |
143 {0x2ffe, true}, | 146 {0x2ffe, true}, |
144 {0x2fff, true}, | 147 {0x2fff, true}, |
145 {0x3000, false}, | 148 {0x3000, false}, |
146 {0x3001, false}, | 149 {0x3001, false}, |
147 {0x7fffffff, false}, | 150 {0x7fffffff, false}, |
148 {0x80000000, false}, | 151 {0x80000000, false}, |
149 {0x80000001, false}, | 152 {0x80000001, false}, |
150 {0x80001fff, false}, | 153 {0x80001fff, false}, |
151 {0x80002000, false}, | 154 {0x80002000, false}, |
152 {0x80002001, false}, | 155 {0x80002001, false}, |
153 {0x80002ffe, false}, | 156 {0x80002ffe, false}, |
154 {0x80002fff, false}, | 157 {0x80002fff, false}, |
155 {0x80003000, false}, | 158 {0x80003000, false}, |
156 {0x80003001, false}, | 159 {0x80003001, false}, |
157 {0xffffcfff, false}, | 160 {0xffffcfff, false}, |
158 {0xffffdfff, false}, | 161 {0xffffdfff, false}, |
159 {0xffffefff, false}, | 162 {0xffffefff, false}, |
160 {0xffffffff, false}, | 163 {0xffffffff, false}, |
161 {0x100000000, false}, | 164 {0x100000000, false}, |
162 {0xffffffffffffffff, false}, | 165 {0xffffffffffffffff, false}, |
163 }; | 166 }; |
164 | 167 |
165 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); | 168 CheckedAddressRange parent_range_32(false, 0x2000, 0x1000); |
166 ASSERT_TRUE(parent_range_32.IsValid()); | 169 ASSERT_TRUE(parent_range_32.IsValid()); |
167 | 170 |
168 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 171 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
169 const TestData& testcase = kTestData[index]; | 172 const TestData& testcase = kTestData[index]; |
170 SCOPED_TRACE( | 173 SCOPED_TRACE(base::StringPrintf( |
171 base::StringPrintf("index %zu, value 0x%llx", index, testcase.value)); | 174 "index %" PRIuS ", value 0x%llx", index, testcase.value)); |
172 | 175 |
173 EXPECT_EQ(testcase.expectation, | 176 EXPECT_EQ(testcase.expectation, |
174 parent_range_32.ContainsValue(testcase.value)); | 177 parent_range_32.ContainsValue(testcase.value)); |
175 } | 178 } |
176 | 179 |
177 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); | 180 CheckedAddressRange parent_range_64(true, 0x100000000, 0x1000); |
178 ASSERT_TRUE(parent_range_64.IsValid()); | 181 ASSERT_TRUE(parent_range_64.IsValid()); |
179 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff)); | 182 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff)); |
180 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000)); | 183 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000)); |
181 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001)); | 184 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001)); |
182 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff)); | 185 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff)); |
183 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000)); | 186 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000)); |
184 } | 187 } |
185 | 188 |
186 TEST(CheckedMachAddressRange, ContainsRange) { | 189 TEST(CheckedAddressRange, ContainsRange) { |
187 const struct TestData { | 190 const struct TestData { |
188 mach_vm_address_t base; | 191 uint64_t base; |
189 mach_vm_size_t size; | 192 uint64_t size; |
190 bool expectation; | 193 bool expectation; |
191 } kTestData[] = { | 194 } kTestData[] = { |
192 {0, 0, false}, | 195 {0, 0, false}, |
193 {0, 1, false}, | 196 {0, 1, false}, |
194 {0x2000, 0x1000, true}, | 197 {0x2000, 0x1000, true}, |
195 {0, 0x2000, false}, | 198 {0, 0x2000, false}, |
196 {0x3000, 0x1000, false}, | 199 {0x3000, 0x1000, false}, |
197 {0x1800, 0x1000, false}, | 200 {0x1800, 0x1000, false}, |
198 {0x2800, 0x1000, false}, | 201 {0x2800, 0x1000, false}, |
199 {0x2000, 0x800, true}, | 202 {0x2000, 0x800, true}, |
(...skipping 12 matching lines...) Expand all Loading... |
212 {0x3000, 0, true}, | 215 {0x3000, 0, true}, |
213 {0x3001, 0, false}, | 216 {0x3001, 0, false}, |
214 {0x1fff, 1, false}, | 217 {0x1fff, 1, false}, |
215 {0x2000, 1, true}, | 218 {0x2000, 1, true}, |
216 {0x2001, 1, true}, | 219 {0x2001, 1, true}, |
217 {0x2fff, 1, true}, | 220 {0x2fff, 1, true}, |
218 {0x3000, 1, false}, | 221 {0x3000, 1, false}, |
219 {0x3001, 1, false}, | 222 {0x3001, 1, false}, |
220 }; | 223 }; |
221 | 224 |
222 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); | 225 CheckedAddressRange parent_range_32(false, 0x2000, 0x1000); |
223 ASSERT_TRUE(parent_range_32.IsValid()); | 226 ASSERT_TRUE(parent_range_32.IsValid()); |
224 | 227 |
225 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 228 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
226 const TestData& testcase = kTestData[index]; | 229 const TestData& testcase = kTestData[index]; |
227 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", | 230 SCOPED_TRACE(base::StringPrintf("index %" PRIuS |
| 231 ", base 0x%llx, size 0x%llx", |
228 index, | 232 index, |
229 testcase.base, | 233 testcase.base, |
230 testcase.size)); | 234 testcase.size)); |
231 | 235 |
232 CheckedMachAddressRange child_range_32(false, testcase.base, testcase.size); | 236 CheckedAddressRange child_range_32(false, testcase.base, testcase.size); |
233 ASSERT_TRUE(child_range_32.IsValid()); | 237 ASSERT_TRUE(child_range_32.IsValid()); |
234 EXPECT_EQ(testcase.expectation, | 238 EXPECT_EQ(testcase.expectation, |
235 parent_range_32.ContainsRange(child_range_32)); | 239 parent_range_32.ContainsRange(child_range_32)); |
236 } | 240 } |
237 | 241 |
238 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); | 242 CheckedAddressRange parent_range_64(true, 0x100000000, 0x1000); |
239 ASSERT_TRUE(parent_range_64.IsValid()); | 243 ASSERT_TRUE(parent_range_64.IsValid()); |
240 | 244 |
241 CheckedMachAddressRange child_range_64(true, 0xffffffff, 2); | 245 CheckedAddressRange child_range_64(true, 0xffffffff, 2); |
242 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); | 246 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); |
243 | 247 |
244 child_range_64.SetRange(true, 0x100000000, 2); | 248 child_range_64.SetRange(true, 0x100000000, 2); |
245 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); | 249 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); |
246 | 250 |
247 child_range_64.SetRange(true, 0x100000ffe, 2); | 251 child_range_64.SetRange(true, 0x100000ffe, 2); |
248 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); | 252 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); |
249 | 253 |
250 child_range_64.SetRange(true, 0x100000fff, 2); | 254 child_range_64.SetRange(true, 0x100000fff, 2); |
251 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); | 255 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); |
252 } | 256 } |
253 | 257 |
254 } // namespace | 258 } // namespace |
255 } // namespace test | 259 } // namespace test |
256 } // namespace crashpad | 260 } // namespace crashpad |
OLD | NEW |