Sanitizer built against glibc 2.34 doesn't work
authorVitaly Buka <vitalybuka@google.com>
Fri, 16 Apr 2021 16:50:24 +0000 (09:50 -0700)
committerVitaly Buka <vitalybuka@google.com>
Fri, 16 Apr 2021 18:21:03 +0000 (11:21 -0700)
commit82150606fb11d28813ae6da1101f5bda638165fe
tree210332394a8189208c005be6cd935d1bb11eb18d
parent3dc24bc31edbc01dea085b24a6a6b024d7ae531c
Sanitizer built against glibc 2.34 doesn't work

As mentioned in https://gcc.gnu.org/PR100114 , glibc starting with the
https://sourceware.org/git/?p=glibc.git;a=commit;h=6c57d320484988e87e446e2e60ce42816bf51d53
change doesn't define SIGSTKSZ and MINSIGSTKSZ macros to constants, but to sysconf function call.
sanitizer_posix_libcdep.cpp has
static const uptr kAltStackSize = SIGSTKSZ * 4;  // SIGSTKSZ is not enough.
which is generally fine, just means that when SIGSTKSZ is not a compile time constant will be initialized later.
The problem is that kAltStackSize is used in SetAlternateSignalStack which is called very early, from .preinit_array
initialization, i.e. far before file scope variables are constructed, which means it is not initialized and
mmapping 0 will fail:
==145==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22)

Here is one possible fix, another one could be to make kAltStackSize a preprocessor macro if _SG_SIGSTKSZ is defined
(but perhaps with having an automatic const variable initialized to it so that sysconf isn't at least called twice
during SetAlternateSignalStack.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D100645
compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp