Re: [PATCH 3/9] Makefile: turn on NO_MMAP when building with ASan
- From
Collin Funk <collin.funk1@gmail.com>
- Date
- Nov 12, 2025, 08:17 UTC
- Message-ID
- <87y0obis17.fsf@gmail.com>
- In-Reply-To
- <20251112080215.GC979063@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 26 quoted lines
> Git often uses mmap() to access on-disk files. This leaves a blind spot > in our SANITIZE=address builds, since ASan does not seem to handle mmap > at all. Nor does the OS notice most out-of-bounds access, since it tends > to round up to the nearest page size (so depending on how big the map > is, you might have to overrun it by up to 4095 bytes to trigger a > segfault). > > The previous commit demonstrates a memory bug that we missed. We could > have made a new test where the out-of-bounds access was much larger, or > where the mapped file ended closer to a page boundary. But the point of > running the test suite with sanitizers is to catch these problems > without having to construct specific tests. > > Let's enable NO_MMAP for our ASan builds by default, which should give > us better coverage. This does increase the memory usage of Git, since > we're copying from the filesystem into heap. But the repositories in the > test suite tend to be small, so the overhead isn't really noticeable > (and ASan already has quite a performance penalty). > > There are a few other known bugs that this patch will help flush out. > However, they aren't directly triggered in the test suite (yet). So > it's safe to turn this on now without breaking the test suite, which > will help us add new tests to demonstrate those other bugs as we fix > them. > > Signed-off-by: Jeff King <peff@peff.net>
I see that an interceptor was added in 2023 [1]. Maybe your compiler is older than that?
On my system:
$ cat main.c
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
int
main (void)
{
char *ptr = mmap (NULL, getpagesize (), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS, -1, 0);
if (ptr == NULL)
abort ();
ptr[getpagesize () + 1] = 'a';
return 0;
}
$ gcc --version | head -n 1
gcc (GCC) 15.2.1 20251022 (Red Hat 15.2.1-3)
$ clang --version | head -n 1
clang version 21.1.4 (Fedora 21.1.4-1.fc43)
$ gcc -fsanitize=address main.c && ./a.out 2>&1 | grep ^SUMMARY:
SUMMARY: AddressSanitizer: SEGV (/home/collin/a.out+0x400554) (BuildId: 1b7a82189bfffb3f73d420e138b9859add25901a) in main
$ clang -fsanitize=address main.c && ./a.out 2>&1 | grep ^SUMMARY:
SUMMARY: AddressSanitizer: SEGV (/home/collin/a.out+0x4e9ee6) (BuildId: aca1d168eacebaa239082d8a45ab74c8470f4b31) in mainCollin
[1] https://github.com/llvm/llvm-project/commit/a34e702aa16fde4cc76e9360d985a64e008e0b23