Commit 01a8dea
committed
[sanitizer_common] Add darwin-specific MemoryRangeIsAvailable (llvm#167797)
The fixes a TOCTOU bug in the code that initializes shadow memory in
ASAN:
https://github.com/llvm/llvm-project/blob/4b05581bae0e3432cfa514788418fb2fc2144904/compiler-rt/lib/asan/asan_shadow_setup.cpp#L66-L91
1. During initialization, we call `FindDynamicShadowStart` to search the
memory mapping for enough space to dynamically allocate shadow memory.
2. We call `MemoryRangeIsAvailable(shadow_start, kHighShadowEnd);`,
which goes into `MemoryMappingLayout`.
3. We actually map the shadow with `ReserveShadowMemoryRange`.
In step 2, `MemoryMappingLayout` makes various allocations using the
internal allocator. This can cause the allocator to map more memory! In
some cases, this can actually allocate memory that overlaps with the
shadow region returned by` FindDynamicShadowStart` in step 1. This is
not actually fatal, but it memory corruption; MAP_FIXED is allowed to
overlap other regions, and the effect is any overlapping memory is
zeroed.
------
To address this, this PR implements `MemoryRangeIsAvailable` on Darwin
without any heap allocations:
- Move `IntervalsAreSeparate` into sanitizer_common.h
- Guard existing sanitizer_posix implementation of
`MemoryRangeIsAvailable` behind !SANITIZER_APPLE
- `IsAddressInMappedRegion` in sanitizer_mac becomes
`MemoryRangeIsAvailable`, which also checks for overlap with the DYLD
shared cache.
After this fix, it should be possible to re-land llvm#166005, which
triggered this issue on the x86 iOS simulators.
rdar://164208439
(cherry picked from commit 6a89439)1 parent 7122374 commit 01a8dea
File tree
5 files changed
+47
-20
lines changed- compiler-rt/lib
- sanitizer_common
- tsan/rtl
5 files changed
+47
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
484 | 484 | | |
485 | 485 | | |
486 | 486 | | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
487 | 494 | | |
488 | 495 | | |
489 | 496 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
106 | 108 | | |
107 | 109 | | |
108 | 110 | | |
| |||
1397 | 1399 | | |
1398 | 1400 | | |
1399 | 1401 | | |
1400 | | - | |
1401 | | - | |
1402 | | - | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
1403 | 1416 | | |
1404 | 1417 | | |
1405 | 1418 | | |
1406 | 1419 | | |
1407 | | - | |
| 1420 | + | |
1408 | 1421 | | |
| 1422 | + | |
1409 | 1423 | | |
1410 | 1424 | | |
1411 | 1425 | | |
| |||
1417 | 1431 | | |
1418 | 1432 | | |
1419 | 1433 | | |
1420 | | - | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
1421 | 1452 | | |
1422 | 1453 | | |
1423 | 1454 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
80 | | - | |
81 | 79 | | |
82 | 80 | | |
83 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
| 228 | + | |
235 | 229 | | |
236 | | - | |
237 | | - | |
238 | | - | |
| 230 | + | |
239 | 231 | | |
240 | 232 | | |
241 | 233 | | |
| |||
251 | 243 | | |
252 | 244 | | |
253 | 245 | | |
254 | | - | |
255 | 246 | | |
256 | 247 | | |
257 | 248 | | |
| |||
265 | 256 | | |
266 | 257 | | |
267 | 258 | | |
268 | | - | |
| 259 | + | |
269 | 260 | | |
270 | 261 | | |
271 | 262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
238 | | - | |
| 238 | + | |
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
| |||
0 commit comments