Skip to content

Conversation

@ndrewh
Copy link

@ndrewh ndrewh commented Nov 21, 2025

[sanitizer-common] [Darwin] Provide warnings for common sandbox issues llvm#165907
[sanitizer_common] Add darwin-specific MemoryRangeIsAvailable llvm#167797
[sanitizer-common] [Darwin] Fix overlapping dyld segment addresses llvm#167800

llvm#165907)

We currently do not handle errors in task_set_exc_guard_behavior. If
this fails, mmap can unexpectedly crash.
We also do not currently provide a clear warning if no external
symbolizers are found.

rdar://163798535
(cherry picked from commit 148a42b)
…67797)

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)
…ttempt 2) (llvm#167800)

This re-lands llvm#166005, which was reverted due to the issue described in
llvm#167797.

There are 4 small changes:
- Fix LoadedModule leak by calling Clear() on the modules list
- Fix internal_strncpy calls that are not null-terminated
- Improve test to accept the dylib being loaded from a different path
than compiled `{{.*}}[[DYLIB]]`
- strcmp => internal_strncmp

This should not be merged until after llvm#167797.

rdar://163149325
(cherry picked from commit 4fe79a7)
@ndrewh ndrewh requested a review from DanBlackwell November 21, 2025 06:36
@ndrewh ndrewh requested a review from a team as a code owner November 21, 2025 06:36
@ndrewh
Copy link
Author

ndrewh commented Nov 21, 2025

@swift-ci please test

@ndrewh
Copy link
Author

ndrewh commented Nov 21, 2025

This will also need llvm#169008, oops

@ndrewh ndrewh marked this pull request as draft November 21, 2025 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant