Conversation
macOS provides us with two flags that makes reading from an mmap'd file more resilient against crashes: - MAP_RESILIENT_CODESIGN avoids crashes when mapping a macho binary with an invalid signature. Apple has some weird shenanigans going on that makes it validate the signature of mmaped file (even when read-only) under some undocumented circumstances, and this flag avoids this. - MAP_RESILIENT_MEDIA avoids crashes when the underlying data becomes inaccessible - either because the file is no longer present (think removable media getting unplugged) or because the file got truncated to a smaller size. When this happens, it returns zeroes instead.
roblabla
force-pushed
the
no-mmap-crash-macos
branch
from
February 18, 2026 20:04
6eea7fb to
229ed8b
Compare
|
@de-vri-es while you're at it, small up on this change that seems relevant too |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
macOS provides us with two flags that makes reading from an mmap'd file more resilient against crashes:
MAP_RESILIENT_CODESIGN avoids crashes when mapping a macho binary with an invalid signature. Apple has some weird shenanigans going on that makes it validate the signature of mmaped file (even when read-only) under some undocumented circumstances, and this flag avoids this. See Crash on macOS (
EXC_BAD_ACCESS (Code Signature Invalid)) VirusTotal/yara#1309 for more informationMAP_RESILIENT_MEDIA avoids crashes when the underlying data becomes inaccessible - either because the file is no longer present (think removable media getting unplugged) or because the file got truncated to a smaller size. When this happens, it returns zeroes instead.
Note that
MAP_RESILIENT_MEDIAonly works forMAP_PRIVATEmappings.These aren't very well documented, but can be found in the XNU source code: https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.61.12/bsd/sys/mman.h
This PR adds both to every MMAP to make them more robust. Arguably, the second one (
MAP_RESILIENT_MEDIA) does change the semantics ofmmapcompared to how it works on every other platform, and as such we may want to move it to be a flag that the user must provide?