[llvm-branch-commits] [compiler-rt] cc08a27 - Sanitizer built against glibc 2.34 doesn't work

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jun 21 21:43:50 PDT 2021


Author: Vitaly Buka
Date: 2021-06-22T00:43:08-04:00
New Revision: cc08a27d2ecc1458a8871c989add0b49afc24f12

URL: https://github.com/llvm/llvm-project/commit/cc08a27d2ecc1458a8871c989add0b49afc24f12
DIFF: https://github.com/llvm/llvm-project/commit/cc08a27d2ecc1458a8871c989add0b49afc24f12.diff

LOG: 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

(cherry picked from commit 82150606fb11d28813ae6da1101f5bda638165fe)

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
index d29438cf9dbd5..2b10bdd37293a 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -165,7 +165,11 @@ bool SupportsColoredOutput(fd_t fd) {
 
 #if !SANITIZER_GO
 // TODO(glider): 
diff erent tools may require 
diff erent altstack size.
-static const uptr kAltStackSize = SIGSTKSZ * 4;  // SIGSTKSZ is not enough.
+static uptr GetAltStackSize() {
+  // SIGSTKSZ is not enough.
+  static const uptr kAltStackSize = SIGSTKSZ * 4;
+  return kAltStackSize;
+}
 
 void SetAlternateSignalStack() {
   stack_t altstack, oldstack;
@@ -176,10 +180,10 @@ void SetAlternateSignalStack() {
   // TODO(glider): the mapped stack should have the MAP_STACK flag in the
   // future. It is not required by man 2 sigaltstack now (they're using
   // malloc()).
-  void* base = MmapOrDie(kAltStackSize, __func__);
+  void *base = MmapOrDie(GetAltStackSize(), __func__);
   altstack.ss_sp = (char*) base;
   altstack.ss_flags = 0;
-  altstack.ss_size = kAltStackSize;
+  altstack.ss_size = GetAltStackSize();
   CHECK_EQ(0, sigaltstack(&altstack, nullptr));
 }
 
@@ -187,7 +191,7 @@ void UnsetAlternateSignalStack() {
   stack_t altstack, oldstack;
   altstack.ss_sp = nullptr;
   altstack.ss_flags = SS_DISABLE;
-  altstack.ss_size = kAltStackSize;  // Some sane value required on Darwin.
+  altstack.ss_size = GetAltStackSize();  // Some sane value required on Darwin.
   CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
   UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
 }


        


More information about the llvm-branch-commits mailing list