[ASan] Fix crash in __asan_region_is_poisoned at application memory boundaries (#180223)
`__asan_region_is_poisoned()` can crash when called on a region fully
contained in the last 8 bytes (shadow-granularity) before the end of an
ASan application memory range (kLowMemEnd / kMidMemEnd / kHighMemEnd).
The function performs a fast-path check by rounding UP the begin address
and rounding DOWN the end address of the region (aligned to
`ASAN_SHADOW_GRANULARITY`) and then scanning the corresponding shadow
range via `MemToShadow()` and `mem_is_zero()`. The implementation of
`MemToShadow()` assumes that `RoundUpTo(beg, ASAN_SHADOW_GRANULARITY)`
remains within the same application memory range. That assumption is
incorrect near upper bound of a range: for example, begin address within
the last 8 bytes of the high memory range
(`kHighMemEnd=0x0000'7fff'ffff'ffff`), which is the max user address of
VAS on x86_64, may be rounded UP so it crosses the upper bound
kHighMemEnd. In such cases MemToShadow() is invoked on an out-of-range
address and crashes.
Precisely, the following calls crash in `MemToShadow(aligned_b)` because
`aligned_b = RoundUpTo(beg, ASAN_SHADOW_GRANULARITY)` returns
`0x0000'8000'0000'0000` which is 1 byte beyond the VAS:
`__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fff9, size); // 1 <=
size <= 6
__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fffa, size); // 1 <=
size <= 5
__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fffb, size); // 1 <=
size <= 4
__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fffc, size); // 1 <=
size <= 3
__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fffd, size); // 1 <=
size <= 2
__asan_region_is_poisoned((void*)0x0000'7fff'ffff'fffe, size); // size =
1`
Fix this by detecting cases earlier where the shadow range is empty
(aligned_e < aligned_b) and returning earlier without calling
MemToShadow() or mem_is_zero().
Add tests checking regions at the ends of ASan LowMem, MidMem, and
HighMem application memory ranges to ensure __asan_region_is_poisoned()
no longer crashes on these boundary cases.Welcome to the LLVM project!
This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.
The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.
C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
Other components include: the libc++ C++ standard library, the LLD linker, and more.
Consult the Getting Started with LLVM page for information on building and running LLVM.
For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.
Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.
The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.