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/mac/checked_mach_address_range.h" |
16 | 16 |
17 #include <mach/mach.h> | 17 #include <mach/mach.h> |
18 | 18 |
19 #include <limits> | 19 #include <limits> |
20 | 20 |
21 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
24 #include "gtest/gtest.h" | 24 #include "gtest/gtest.h" |
25 #include "util/mac/process_reader.h" | |
26 | 25 |
27 namespace crashpad { | 26 namespace crashpad { |
28 namespace test { | 27 namespace test { |
29 namespace { | 28 namespace { |
30 | 29 |
31 #if defined(ARCH_CPU_64_BITS) | 30 enum Validity { |
32 const bool kValid64Invalid32 = true; | 31 kInvalid = false, |
33 #else | 32 kValid, |
34 const bool kValid64Invalid32 = false; | 33 kValid64Invalid32, |
35 #endif | 34 }; |
| 35 |
| 36 bool ExpectationForValidity32(Validity validity) { |
| 37 return validity == kValid; |
| 38 } |
| 39 |
| 40 bool ExpectationForValidity64(Validity validity) { |
| 41 return validity == kValid || validity == kValid64Invalid32; |
| 42 } |
36 | 43 |
37 TEST(CheckedMachAddressRange, IsValid) { | 44 TEST(CheckedMachAddressRange, IsValid) { |
38 const struct TestData { | 45 const struct TestData { |
39 mach_vm_address_t base; | 46 mach_vm_address_t base; |
40 mach_vm_size_t size; | 47 mach_vm_size_t size; |
41 bool valid; | 48 Validity validity; |
42 } kTestData[] = { | 49 } kTestData[] = { |
43 {0, 0, true}, | 50 {0, 0, kValid}, |
44 {0, 1, true}, | 51 {0, 1, kValid}, |
45 {0, 2, true}, | 52 {0, 2, kValid}, |
46 {0, 0x7fffffff, true}, | 53 {0, 0x7fffffff, kValid}, |
47 {0, 0x80000000, true}, | 54 {0, 0x80000000, kValid}, |
48 {0, 0xfffffffe, true}, | 55 {0, 0xfffffffe, kValid}, |
49 {0, 0xffffffff, true}, | 56 {0, 0xffffffff, kValid}, |
50 {0, 0xffffffffffffffff, kValid64Invalid32}, | 57 {0, 0xffffffffffffffff, kValid64Invalid32}, |
51 {1, 0, true}, | 58 {1, 0, kValid}, |
52 {1, 1, true}, | 59 {1, 1, kValid}, |
53 {1, 2, true}, | 60 {1, 2, kValid}, |
54 {1, 0x7fffffff, true}, | 61 {1, 0x7fffffff, kValid}, |
55 {1, 0x80000000, true}, | 62 {1, 0x80000000, kValid}, |
56 {1, 0xfffffffe, true}, | 63 {1, 0xfffffffe, kValid}, |
57 {1, 0xffffffff, kValid64Invalid32}, | 64 {1, 0xffffffff, kValid64Invalid32}, |
58 {1, 0xfffffffffffffffe, kValid64Invalid32}, | 65 {1, 0xfffffffffffffffe, kValid64Invalid32}, |
59 {1, 0xffffffffffffffff, false}, | 66 {1, 0xffffffffffffffff, kInvalid}, |
60 {0x7fffffff, 0, true}, | 67 {0x7fffffff, 0, kValid}, |
61 {0x7fffffff, 1, true}, | 68 {0x7fffffff, 1, kValid}, |
62 {0x7fffffff, 2, true}, | 69 {0x7fffffff, 2, kValid}, |
63 {0x7fffffff, 0x7fffffff, true}, | 70 {0x7fffffff, 0x7fffffff, kValid}, |
64 {0x7fffffff, 0x80000000, true}, | 71 {0x7fffffff, 0x80000000, kValid}, |
65 {0x7fffffff, 0xfffffffe, kValid64Invalid32}, | 72 {0x7fffffff, 0xfffffffe, kValid64Invalid32}, |
66 {0x7fffffff, 0xffffffff, kValid64Invalid32}, | 73 {0x7fffffff, 0xffffffff, kValid64Invalid32}, |
67 {0x80000000, 0, true}, | 74 {0x80000000, 0, kValid}, |
68 {0x80000000, 1, true}, | 75 {0x80000000, 1, kValid}, |
69 {0x80000000, 2, true}, | 76 {0x80000000, 2, kValid}, |
70 {0x80000000, 0x7fffffff, true}, | 77 {0x80000000, 0x7fffffff, kValid}, |
71 {0x80000000, 0x80000000, kValid64Invalid32}, | 78 {0x80000000, 0x80000000, kValid64Invalid32}, |
72 {0x80000000, 0xfffffffe, kValid64Invalid32}, | 79 {0x80000000, 0xfffffffe, kValid64Invalid32}, |
73 {0x80000000, 0xffffffff, kValid64Invalid32}, | 80 {0x80000000, 0xffffffff, kValid64Invalid32}, |
74 {0xfffffffe, 0, true}, | 81 {0xfffffffe, 0, kValid}, |
75 {0xfffffffe, 1, true}, | 82 {0xfffffffe, 1, kValid}, |
76 {0xfffffffe, 2, kValid64Invalid32}, | 83 {0xfffffffe, 2, kValid64Invalid32}, |
77 {0xfffffffe, 0x7fffffff, kValid64Invalid32}, | 84 {0xfffffffe, 0x7fffffff, kValid64Invalid32}, |
78 {0xfffffffe, 0x80000000, kValid64Invalid32}, | 85 {0xfffffffe, 0x80000000, kValid64Invalid32}, |
79 {0xfffffffe, 0xfffffffe, kValid64Invalid32}, | 86 {0xfffffffe, 0xfffffffe, kValid64Invalid32}, |
80 {0xfffffffe, 0xffffffff, kValid64Invalid32}, | 87 {0xfffffffe, 0xffffffff, kValid64Invalid32}, |
81 {0xffffffff, 0, true}, | 88 {0xffffffff, 0, kValid}, |
82 {0xffffffff, 1, kValid64Invalid32}, | 89 {0xffffffff, 1, kValid64Invalid32}, |
83 {0xffffffff, 2, kValid64Invalid32}, | 90 {0xffffffff, 2, kValid64Invalid32}, |
84 {0xffffffff, 0x7fffffff, kValid64Invalid32}, | 91 {0xffffffff, 0x7fffffff, kValid64Invalid32}, |
85 {0xffffffff, 0x80000000, kValid64Invalid32}, | 92 {0xffffffff, 0x80000000, kValid64Invalid32}, |
86 {0xffffffff, 0xfffffffe, kValid64Invalid32}, | 93 {0xffffffff, 0xfffffffe, kValid64Invalid32}, |
87 {0xffffffff, 0xffffffff, kValid64Invalid32}, | 94 {0xffffffff, 0xffffffff, kValid64Invalid32}, |
88 {0x7fffffffffffffff, 0, kValid64Invalid32}, | 95 {0x7fffffffffffffff, 0, kValid64Invalid32}, |
89 {0x7fffffffffffffff, 1, kValid64Invalid32}, | 96 {0x7fffffffffffffff, 1, kValid64Invalid32}, |
90 {0x7fffffffffffffff, 2, kValid64Invalid32}, | 97 {0x7fffffffffffffff, 2, kValid64Invalid32}, |
91 {0x7fffffffffffffff, 0x7fffffffffffffff, kValid64Invalid32}, | 98 {0x7fffffffffffffff, 0x7fffffffffffffff, kValid64Invalid32}, |
92 {0x7fffffffffffffff, 0x8000000000000000, kValid64Invalid32}, | 99 {0x7fffffffffffffff, 0x8000000000000000, kValid64Invalid32}, |
93 {0x7fffffffffffffff, 0x8000000000000001, false}, | 100 {0x7fffffffffffffff, 0x8000000000000001, kInvalid}, |
94 {0x7fffffffffffffff, 0xfffffffffffffffe, false}, | 101 {0x7fffffffffffffff, 0xfffffffffffffffe, kInvalid}, |
95 {0x7fffffffffffffff, 0xffffffffffffffff, false}, | 102 {0x7fffffffffffffff, 0xffffffffffffffff, kInvalid}, |
96 {0x8000000000000000, 0, kValid64Invalid32}, | 103 {0x8000000000000000, 0, kValid64Invalid32}, |
97 {0x8000000000000000, 1, kValid64Invalid32}, | 104 {0x8000000000000000, 1, kValid64Invalid32}, |
98 {0x8000000000000000, 2, kValid64Invalid32}, | 105 {0x8000000000000000, 2, kValid64Invalid32}, |
99 {0x8000000000000000, 0x7fffffffffffffff, kValid64Invalid32}, | 106 {0x8000000000000000, 0x7fffffffffffffff, kValid64Invalid32}, |
100 {0x8000000000000000, 0x8000000000000000, false}, | 107 {0x8000000000000000, 0x8000000000000000, kInvalid}, |
101 {0x8000000000000000, 0x8000000000000001, false}, | 108 {0x8000000000000000, 0x8000000000000001, kInvalid}, |
102 {0x8000000000000000, 0xfffffffffffffffe, false}, | 109 {0x8000000000000000, 0xfffffffffffffffe, kInvalid}, |
103 {0x8000000000000000, 0xffffffffffffffff, false}, | 110 {0x8000000000000000, 0xffffffffffffffff, kInvalid}, |
104 {0xfffffffffffffffe, 0, kValid64Invalid32}, | 111 {0xfffffffffffffffe, 0, kValid64Invalid32}, |
105 {0xfffffffffffffffe, 1, kValid64Invalid32}, | 112 {0xfffffffffffffffe, 1, kValid64Invalid32}, |
106 {0xfffffffffffffffe, 2, false}, | 113 {0xfffffffffffffffe, 2, kInvalid}, |
107 {0xffffffffffffffff, 0, kValid64Invalid32}, | 114 {0xffffffffffffffff, 0, kValid64Invalid32}, |
108 {0xffffffffffffffff, 1, false}, | 115 {0xffffffffffffffff, 1, kInvalid}, |
109 }; | 116 }; |
110 | 117 |
111 ProcessReader process_reader; | |
112 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); | |
113 | |
114 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 118 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
115 const TestData& testcase = kTestData[index]; | 119 const TestData& testcase = kTestData[index]; |
116 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", | 120 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", |
117 index, | 121 index, |
118 testcase.base, | 122 testcase.base, |
119 testcase.size)); | 123 testcase.size)); |
120 | 124 |
121 CheckedMachAddressRange range( | 125 CheckedMachAddressRange range_32(false, testcase.base, testcase.size); |
122 &process_reader, testcase.base, testcase.size); | 126 EXPECT_EQ(ExpectationForValidity32(testcase.validity), range_32.IsValid()); |
123 EXPECT_EQ(testcase.valid, range.IsValid()); | 127 |
| 128 CheckedMachAddressRange range_64(true, testcase.base, testcase.size); |
| 129 EXPECT_EQ(ExpectationForValidity64(testcase.validity), range_64.IsValid()); |
124 } | 130 } |
125 } | 131 } |
126 | 132 |
127 TEST(CheckedMachAddressRange, ContainsValue) { | 133 TEST(CheckedMachAddressRange, ContainsValue) { |
128 const struct TestData { | 134 const struct TestData { |
129 mach_vm_address_t value; | 135 mach_vm_address_t value; |
130 bool valid; | 136 bool expectation; |
131 } kTestData[] = { | 137 } kTestData[] = { |
132 {0, false}, | 138 {0, false}, |
133 {1, false}, | 139 {1, false}, |
134 {0x1fff, false}, | 140 {0x1fff, false}, |
135 {0x2000, true}, | 141 {0x2000, true}, |
136 {0x2001, true}, | 142 {0x2001, true}, |
137 {0x2ffe, true}, | 143 {0x2ffe, true}, |
138 {0x2fff, true}, | 144 {0x2fff, true}, |
139 {0x3000, false}, | 145 {0x3000, false}, |
140 {0x3001, false}, | 146 {0x3001, false}, |
141 {0x7fffffff, false}, | 147 {0x7fffffff, false}, |
142 {0x80000000, false}, | 148 {0x80000000, false}, |
143 {0x80000001, false}, | 149 {0x80000001, false}, |
144 {0x80001fff, false}, | 150 {0x80001fff, false}, |
145 {0x80002000, false}, | 151 {0x80002000, false}, |
146 {0x80002001, false}, | 152 {0x80002001, false}, |
147 {0x80002ffe, false}, | 153 {0x80002ffe, false}, |
148 {0x80002fff, false}, | 154 {0x80002fff, false}, |
149 {0x80003000, false}, | 155 {0x80003000, false}, |
150 {0x80003001, false}, | 156 {0x80003001, false}, |
151 {0xffffcfff, false}, | 157 {0xffffcfff, false}, |
152 {0xffffdfff, false}, | 158 {0xffffdfff, false}, |
153 {0xffffefff, false}, | 159 {0xffffefff, false}, |
154 {0xffffffff, false}, | 160 {0xffffffff, false}, |
155 {0x100000000, false}, | 161 {0x100000000, false}, |
156 {0xffffffffffffffff, false}, | 162 {0xffffffffffffffff, false}, |
157 }; | 163 }; |
158 | 164 |
159 ProcessReader process_reader; | 165 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); |
160 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); | 166 ASSERT_TRUE(parent_range_32.IsValid()); |
161 | |
162 CheckedMachAddressRange parent_range(&process_reader, 0x2000, 0x1000); | |
163 ASSERT_TRUE(parent_range.IsValid()); | |
164 | 167 |
165 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 168 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
166 const TestData& testcase = kTestData[index]; | 169 const TestData& testcase = kTestData[index]; |
167 SCOPED_TRACE( | 170 SCOPED_TRACE( |
168 base::StringPrintf("index %zu, value 0x%llx", index, testcase.value)); | 171 base::StringPrintf("index %zu, value 0x%llx", index, testcase.value)); |
169 | 172 |
170 EXPECT_EQ(testcase.valid, parent_range.ContainsValue(testcase.value)); | 173 EXPECT_EQ(testcase.expectation, |
| 174 parent_range_32.ContainsValue(testcase.value)); |
171 } | 175 } |
172 | 176 |
173 CheckedMachAddressRange parent_range_64(&process_reader, 0x100000000, 0x1000); | 177 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); |
174 ASSERT_EQ(kValid64Invalid32, parent_range_64.IsValid()); | 178 ASSERT_TRUE(parent_range_64.IsValid()); |
175 if (parent_range_64.IsValid()) { | 179 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff)); |
176 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff)); | 180 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000)); |
177 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000)); | 181 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001)); |
178 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001)); | 182 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff)); |
179 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff)); | 183 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000)); |
180 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000)); | |
181 } | |
182 } | 184 } |
183 | 185 |
184 TEST(CheckedMachAddressRange, ContainsRange) { | 186 TEST(CheckedMachAddressRange, ContainsRange) { |
185 const struct TestData { | 187 const struct TestData { |
186 mach_vm_address_t base; | 188 mach_vm_address_t base; |
187 mach_vm_size_t size; | 189 mach_vm_size_t size; |
188 bool valid; | 190 bool expectation; |
189 } kTestData[] = { | 191 } kTestData[] = { |
190 {0, 0, false}, | 192 {0, 0, false}, |
191 {0, 1, false}, | 193 {0, 1, false}, |
192 {0x2000, 0x1000, true}, | 194 {0x2000, 0x1000, true}, |
193 {0, 0x2000, false}, | 195 {0, 0x2000, false}, |
194 {0x3000, 0x1000, false}, | 196 {0x3000, 0x1000, false}, |
195 {0x1800, 0x1000, false}, | 197 {0x1800, 0x1000, false}, |
196 {0x2800, 0x1000, false}, | 198 {0x2800, 0x1000, false}, |
197 {0x2000, 0x800, true}, | 199 {0x2000, 0x800, true}, |
198 {0x2800, 0x800, true}, | 200 {0x2800, 0x800, true}, |
(...skipping 11 matching lines...) Expand all Loading... |
210 {0x3000, 0, true}, | 212 {0x3000, 0, true}, |
211 {0x3001, 0, false}, | 213 {0x3001, 0, false}, |
212 {0x1fff, 1, false}, | 214 {0x1fff, 1, false}, |
213 {0x2000, 1, true}, | 215 {0x2000, 1, true}, |
214 {0x2001, 1, true}, | 216 {0x2001, 1, true}, |
215 {0x2fff, 1, true}, | 217 {0x2fff, 1, true}, |
216 {0x3000, 1, false}, | 218 {0x3000, 1, false}, |
217 {0x3001, 1, false}, | 219 {0x3001, 1, false}, |
218 }; | 220 }; |
219 | 221 |
220 ProcessReader process_reader; | 222 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); |
221 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); | 223 ASSERT_TRUE(parent_range_32.IsValid()); |
222 | |
223 CheckedMachAddressRange parent_range(&process_reader, 0x2000, 0x1000); | |
224 ASSERT_TRUE(parent_range.IsValid()); | |
225 | 224 |
226 for (size_t index = 0; index < arraysize(kTestData); ++index) { | 225 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
227 const TestData& testcase = kTestData[index]; | 226 const TestData& testcase = kTestData[index]; |
228 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", | 227 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", |
229 index, | 228 index, |
230 testcase.base, | 229 testcase.base, |
231 testcase.size)); | 230 testcase.size)); |
232 | 231 |
233 CheckedMachAddressRange child_range( | 232 CheckedMachAddressRange child_range_32(false, testcase.base, testcase.size); |
234 &process_reader, testcase.base, testcase.size); | 233 ASSERT_TRUE(child_range_32.IsValid()); |
235 ASSERT_TRUE(child_range.IsValid()); | 234 EXPECT_EQ(testcase.expectation, |
236 EXPECT_EQ(testcase.valid, parent_range.ContainsRange(child_range)); | 235 parent_range_32.ContainsRange(child_range_32)); |
237 } | 236 } |
238 | 237 |
239 CheckedMachAddressRange parent_range_64(&process_reader, 0x100000000, 0x1000); | 238 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); |
240 ASSERT_EQ(kValid64Invalid32, parent_range_64.IsValid()); | 239 ASSERT_TRUE(parent_range_64.IsValid()); |
241 if (parent_range_64.IsValid()) { | |
242 CheckedMachAddressRange child_range_64(&process_reader, 0xffffffff, 2); | |
243 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); | |
244 | 240 |
245 child_range_64.SetRange(&process_reader, 0x100000000, 2); | 241 CheckedMachAddressRange child_range_64(true, 0xffffffff, 2); |
246 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); | 242 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); |
247 | 243 |
248 child_range_64.SetRange(&process_reader, 0x100000ffe, 2); | 244 child_range_64.SetRange(true, 0x100000000, 2); |
249 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); | 245 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); |
250 | 246 |
251 child_range_64.SetRange(&process_reader, 0x100000fff, 2); | 247 child_range_64.SetRange(true, 0x100000ffe, 2); |
252 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); | 248 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); |
253 } | 249 |
| 250 child_range_64.SetRange(true, 0x100000fff, 2); |
| 251 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); |
254 } | 252 } |
255 | 253 |
256 } // namespace | 254 } // namespace |
257 } // namespace test | 255 } // namespace test |
258 } // namespace crashpad | 256 } // namespace crashpad |
OLD | NEW |