[Sanitizers][Darwin] Fix invalid gap found by FindAvailableMemoryRange
authorMariusz Borsa <m_borsa@apple.com>
Tue, 20 Sep 2022 22:23:58 +0000 (15:23 -0700)
committerMariusz Borsa <m_borsa@apple.com>
Thu, 6 Oct 2022 19:06:30 +0000 (12:06 -0700)
commit7850df3de01fd3933148b04b5818ff780bb776e1
tree634e708dbf0c3ab9543ee2b5ec379ae37303c4e7
parent01470b68f392af8bbf95b2ab48253b1662e2cdc7
[Sanitizers][Darwin] Fix invalid gap found by FindAvailableMemoryRange

An application running with ASAN can fail during shadow memory allocation, with an error
indicating a failure to map shadow memory region due to negative size parameter passed to mmap.

It turns out that the mach_vm_region_recurse() call can return an address of a module
which is beyond the range of the VM address space available to the iOS process,
i.e. greater than the value returned by GetMaxVirtualAddress(). It leads the FindAvailableMemoryRange function
to the an incorrect conclusion that it has found a suitable gap where the shadow memory can fit in,
 while the shadow memory cannot be really allocated in this case.

The fix just takes the maximum VM address into account, causing the function to return 0,
meaning that the VM gap to fit the requested size could not be found.

rdar://66530705

Differential Revision: https://reviews.llvm.org/D134836
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp