| Issue |
242544
|
| Summary |
[asan][Windows] Freeing a chunk mapped at kHighMemBeg releases the whole high-shadow reservation (ReleaseMemoryPagesToOS -> UnmapOrDie MEM_RELEASE)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
brhinescot
|
## Summary
On Windows x64, freeing an allocation whose mapping begins at the first address of high memory makes AddressSanitizer release its **entire high-shadow reservation** (about 14 TB). After that, every shadow access for high memory (the executable image, system DLLs, later allocations) faults on unreserved memory. `ShadowExceptionHandler` cannot commit it, because `VirtualAlloc(MEM_COMMIT)` fails with `ERROR_INVALID_ADDRESS`.
The process then dies with an `access-violation on unknown address` report that has no stack, followed by:
```
AddressSanitizer: CHECK failed: sanitizer_symbolizer_win.cpp:66 "((dbghelp && "failed to load dbghelp.dll")) != (0)"
```
In real programs this happens intermittently, because it depends on whether a large allocation happens to be mapped at that one address. We saw it in roughly 10–20% of runs of GPU-heavy test executables.
## Cause
`asan_allocator.cpp`, `AsanMapUnmapCallback::OnUnmap`, calls `FlushUnneededASanShadowMemory(p, size)`. On Windows 64 (`asan_win.cpp`) that is:
```cpp
ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
```
`ReleaseMemoryPagesToOS` (`sanitizer_win.cpp`) calls `UnmapOrDie((void *)beg, end_aligned - beg_aligned)` with the unaligned `beg`. `UnmapOrDie` first tries:
```cpp
if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) ...
```
`MEM_RELEASE` is expected to fail here, because the shadow range is not the base of an allocation, and to fall back to `MEM_DECOMMIT`. But if the unmapped chunk starts exactly at `kHighMemBeg`, `MemToShadow(p)` is exactly the base of the high-shadow reservation. `VirtualFree(base, 0, MEM_RELEASE)` then succeeds and frees the whole reservation, not just the pages for this chunk.
For a large allocation this is common, not exotic. High memory starts immediately after the high shadow, and `VirtualAlloc(NULL, ...)` returns the lowest free address that fits once lower address space is used up or fragmented.
## Reproducer
Tested with clang 22.1.3 (`e9846648fd6183ee6d8cbdb4502213fcf902a211`, `asan_dynamic` runtime), Windows 11 x64.
The reproducer reserves the free address space below `kHighMemBeg` so the next large mapping lands there deterministically. It then frees a secondary-allocator chunk and reads a global in the image.
```cpp
// Reproducer: freeing a secondary-allocator chunk mapped at the first address of
// high memory releases ASan's entire high-shadow reservation on Windows x64.
// Build: clang-cl /O1 /MD /fsanitize=address release_repro.cpp
#include <windows.h>
#include <cstdio>
#include <cstdlib>
extern "C" __declspec(dllimport) unsigned long long __asan_shadow_memory_dynamic_address;
extern "C" const char* __asan_default_options() {return "quarantine_size_mb=0";}
static int global_in_image=1;
static MEMORY_BASIC_INFORMATION Query(unsigned long long a) {
MEMORY_BASIC_INFORMATION m;VirtualQuery(reinterpret_cast<void*>(a),&m,sizeof m);return m;
}
int main() {
std::setvbuf(stdout,nullptr,_IONBF,0);
const auto offset=__asan_shadow_memory_dynamic_address;
// The reservation that holds the shadow of a system image is the high shadow.
const auto image=reinterpret_cast<unsigned long long>(GetModuleHandleA("ntdll.dll"));
const auto shadow=Query((image>>3)+offset);
const auto shadowBase=reinterpret_cast<unsigned long long>(shadow.AllocationBase);
const auto highMemBeg=(shadowBase-offset)<<3; // its shadow is the reservation's first byte
std::printf("shadow offset %llx, high-shadow reservation %llx, high memory begins %llx\n",offset,shadowBase,highMemBeg);
// Make the next VirtualAlloc(NULL, ...) land at highMemBeg: reserve every free
// region below it. (In real programs this happens by chance once lower address
// space is used up or fragmented.)
for(unsigned long long a=0x10000;a<highMemBeg;) {
const auto m=Query(a);
const auto end=reinterpret_cast<unsigned long long>(m.BaseAddress)+m.RegionSize;
if(m.State==MEM_FREE) {
const auto size=(end>highMemBeg ? highMemBeg:end)-a;
VirtualAlloc(reinterpret_cast<void*>(a),size,MEM_RESERVE,PAGE_NOACCESS);
}
a=end;
}
// A large allocation is served by the secondary (mmap-based) allocator.
auto* chunk=static_cast<char*>(std::malloc(8<<20));
std::printf("8 MB chunk at %p (map begins one page below)\n",static_cast<void*>(chunk));
chunk[0]=1;
std::printf("before free: high-shadow reservation state %lx\n",Query(shadowBase).State);
std::free(chunk); // OnUnmap -> FlushUnneededASanShadowMemory -> ReleaseMemoryPagesToOS -> UnmapOrDie
std::printf("after free: high-shadow reservation state %lx (0x10000 = MEM_FREE)\n",Query(shadowBase).State);
std::fflush(stdout);
// Any instrumented access to high memory now faults on unreserved shadow.
volatile int* p=&global_in_image;
std::printf("reading a global in the image...\n");std::fflush(stdout);
return *p-1;
}
```
```
clang-cl /O1 /MD /fsanitize=address release_repro.cpp
```
Output:
```
shadow offset 1b7fa600000, high-shadow reservation 3eef9ac0000, high memory begins 11b7fa600000
8 MB chunk at 000011B7FA601800 (map begins one page below)
before free: high-shadow reservation state 1000
=================================================================
==13252==ERROR: AddressSanitizer: access-violation on unknown address 0x11b6e0fef078 (pc 0x7ff734f72587 bp 0x0080000ff770 sp 0x0080000ff700 T0)
==13252==The signal is caused by a READ memory access.
AddressSanitizer: CHECK failed: sanitizer_symbolizer_win.cpp:66 "((dbghelp && "failed to load dbghelp.dll")) != (0)" (0x0, 0x0) (tid=33428)
```
The process never reaches the "after free" line: the first instrumented access after `free` (inside `printf`) already faults on the released shadow.
**Control run:** reserving the first allocation-granularity block at `kHighMemBeg` before any allocation (so no chunk can start there) makes the same program pass. The reservation stays `MEM_RESERVE` after the free.
## Suggested fix
`ReleaseMemoryPagesToOS` is meant to give pages back, not to unmap regions, so on Windows it should decommit only, never `MEM_RELEASE`. Something like:
```cpp
void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
uptr beg_aligned = RoundUpTo(beg, GetPageSizeCached()),
end_aligned = RoundDownTo(end, GetPageSizeCached());
if (beg_aligned >= end_aligned) return;
VirtualFree((void *)beg_aligned, end_aligned - beg_aligned, MEM_DECOMMIT);
}
```
Rounding `beg` up rather than down also avoids decommitting the partial first page, which can hold shadow for the neighbouring chunk. Today the unaligned `beg` is passed through as is.
## Workaround
Reserve `[kHighMemBeg, kHighMemBeg + allocation granularity)` with `PAGE_NOACCESS` during startup, before any allocation. `kHighMemBeg` can be computed from `__asan_shadow_memory_dynamic_address` and the allocation base of the shadow of any system image.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs