From e10ea9608a46565bd4d8d0a692647f9ce43b1de5 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Tue, 11 Jan 2022 20:42:16 +0100 Subject: [PATCH] gcc{7,9,10}: apply patches for asan w/glibc-2.34 This should fix a few broken cc-wrapper tests that also check for libasan[1][2][3]: [...] checking whether sanitizers are fully functional... ==243==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22) [...] The underlying issue is that `SIGSTKSZ` isn't a compile-time constant anymore, but in this case the uninitialized `kAltStackSize` was initialized early enough to evalute to `0`[4]. The issue is already fixed in gcc11 and there's GCC 8.5 which also contains the patch, however the backports to v9 and v10 aren't released yet, so we have to apply patches on our own here. For GCC 7.5 I applied the patch from gcc8 as it doesn't seem as if there's an official upstream backport. [1] https://hydra.nixos.org/build/163102264 [2] https://hydra.nixos.org/build/163624687 [3] https://hydra.nixos.org/build/163619227 [4] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100114 --- pkgs/development/compilers/gcc/10/default.nix | 4 +- .../gcc/10/gcc10-asan-glibc-2.34.patch | 70 +++++++++++++++++++ pkgs/development/compilers/gcc/7/default.nix | 3 + .../gcc/7/gcc8-asan-glibc-2.34.patch | 70 +++++++++++++++++++ pkgs/development/compilers/gcc/9/default.nix | 2 +- .../gcc/9/gcc9-asan-glibc-2.34.patch | 70 +++++++++++++++++++ 6 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 pkgs/development/compilers/gcc/10/gcc10-asan-glibc-2.34.patch create mode 100644 pkgs/development/compilers/gcc/7/gcc8-asan-glibc-2.34.patch create mode 100644 pkgs/development/compilers/gcc/9/gcc9-asan-glibc-2.34.patch diff --git a/pkgs/development/compilers/gcc/10/default.nix b/pkgs/development/compilers/gcc/10/default.nix index 4493fd936ec3..6c5adac600c8 100644 --- a/pkgs/development/compilers/gcc/10/default.nix +++ b/pkgs/development/compilers/gcc/10/default.nix @@ -61,8 +61,8 @@ let majorVersion = "10"; inherit (stdenv) buildPlatform hostPlatform targetPlatform; - patches = - optional (targetPlatform != hostPlatform) ../libstdc++-target.patch + patches = [ ./gcc10-asan-glibc-2.34.patch ] + ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch ++ optional noSysDirs ../no-sys-dirs.patch ++ optional (noSysDirs && hostPlatform.isRiscV) ../no-sys-dirs-riscv.patch /* ++ optional (hostPlatform != buildPlatform) (fetchpatch { # XXX: Refine when this should be applied diff --git a/pkgs/development/compilers/gcc/10/gcc10-asan-glibc-2.34.patch b/pkgs/development/compilers/gcc/10/gcc10-asan-glibc-2.34.patch new file mode 100644 index 000000000000..d6d4f41ffdf8 --- /dev/null +++ b/pkgs/development/compilers/gcc/10/gcc10-asan-glibc-2.34.patch @@ -0,0 +1,70 @@ +From 950bac27d63c1c2ac3a6ed867692d6a13f21feb3 Mon Sep 17 00:00:00 2001 +From: Jakub Jelinek +Date: Sat, 17 Apr 2021 11:27:14 +0200 +Subject: [PATCH] sanitizer: Fix asan against glibc 2.34 [PR100114] + +As mentioned in the PR, SIGSTKSZ is no longer a compile time constant in +glibc 2.34 and later, so +static const uptr kAltStackSize = SIGSTKSZ * 4; +needs dynamic initialization, but is used by a function called indirectly +from .preinit_array and therefore before the variable is constructed. +This results in using 0 size instead and all asan instrumented programs +die with: +==91==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22) + +Here is a cherry-pick from upstream to fix this. + +2021-04-17 Jakub Jelinek + + PR sanitizer/100114 + * sanitizer_common/sanitizer_posix_libcdep.cpp: Cherry-pick + llvm-project revisions 82150606fb11d28813ae6da1101f5bda638165fe + and b93629dd335ffee2fc4b9b619bf86c3f9e6b0023. + +(cherry picked from commit d9f462fb372fb02da032cefd6b091d7582c425ae) +--- + .../sanitizer_common/sanitizer_posix_libcdep.cpp | 13 ++++++++----- + 1 file changed, 8 insertions(+), 5 deletions(-) + +diff --git a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp +index 304b3a01a08..ac88fbe074e 100644 +--- a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp ++++ b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp +@@ -169,7 +169,11 @@ bool SupportsColoredOutput(fd_t fd) { + + #if !SANITIZER_GO + // TODO(glider): different tools may require different 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; +@@ -180,10 +184,9 @@ 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__); +- altstack.ss_sp = (char*) base; ++ altstack.ss_size = GetAltStackSize(); ++ altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__); + altstack.ss_flags = 0; +- altstack.ss_size = kAltStackSize; + CHECK_EQ(0, sigaltstack(&altstack, nullptr)); + } + +@@ -191,7 +194,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); + } +-- +2.27.0 + diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix index dcb7d0b91f6f..72f3e06bd32b 100644 --- a/pkgs/development/compilers/gcc/7/default.nix +++ b/pkgs/development/compilers/gcc/7/default.nix @@ -63,6 +63,9 @@ let majorVersion = "7"; ./riscv-pthread-reentrant.patch # https://gcc.gnu.org/ml/gcc-patches/2018-03/msg00297.html ./riscv-no-relax.patch + # Fix for asan w/glibc-2.34. Although there's no upstream backport to v7, + # the patch from gcc 8 seems to work perfectly fine. + ./gcc8-asan-glibc-2.34.patch ./0001-Fix-build-for-glibc-2.31.patch ] diff --git a/pkgs/development/compilers/gcc/7/gcc8-asan-glibc-2.34.patch b/pkgs/development/compilers/gcc/7/gcc8-asan-glibc-2.34.patch new file mode 100644 index 000000000000..5645b97c1d89 --- /dev/null +++ b/pkgs/development/compilers/gcc/7/gcc8-asan-glibc-2.34.patch @@ -0,0 +1,70 @@ +From ef195a39d0d3b929cc676302d074b42c25460601 Mon Sep 17 00:00:00 2001 +From: Jakub Jelinek +Date: Sat, 17 Apr 2021 11:27:14 +0200 +Subject: [PATCH] sanitizer: Fix asan against glibc 2.34 [PR100114] + +As mentioned in the PR, SIGSTKSZ is no longer a compile time constant in +glibc 2.34 and later, so +static const uptr kAltStackSize = SIGSTKSZ * 4; +needs dynamic initialization, but is used by a function called indirectly +from .preinit_array and therefore before the variable is constructed. +This results in using 0 size instead and all asan instrumented programs +die with: +==91==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22) + +Here is a cherry-pick from upstream to fix this. + +2021-04-17 Jakub Jelinek + + PR sanitizer/100114 + * sanitizer_common/sanitizer_posix_libcdep.cc: Cherry-pick + llvm-project revisions 82150606fb11d28813ae6da1101f5bda638165fe + and b93629dd335ffee2fc4b9b619bf86c3f9e6b0023. + +(cherry picked from commit 950bac27d63c1c2ac3a6ed867692d6a13f21feb3) +--- + .../sanitizer_common/sanitizer_posix_libcdep.cc | 13 ++++++++----- + 1 file changed, 8 insertions(+), 5 deletions(-) + +diff --git a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc +index 1a37118c299..066079b3954 100644 +--- a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc ++++ b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc +@@ -159,7 +159,11 @@ bool SupportsColoredOutput(fd_t fd) { + + #if !SANITIZER_GO + // TODO(glider): different tools may require different 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; +@@ -170,10 +174,9 @@ 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__); +- altstack.ss_sp = (char*) base; ++ altstack.ss_size = GetAltStackSize(); ++ altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__); + altstack.ss_flags = 0; +- altstack.ss_size = kAltStackSize; + CHECK_EQ(0, sigaltstack(&altstack, nullptr)); + } + +@@ -181,7 +184,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); + } +-- +2.27.0 + diff --git a/pkgs/development/compilers/gcc/9/default.nix b/pkgs/development/compilers/gcc/9/default.nix index 9d21ed667f6c..62878cb6d877 100644 --- a/pkgs/development/compilers/gcc/9/default.nix +++ b/pkgs/development/compilers/gcc/9/default.nix @@ -78,7 +78,7 @@ let majorVersion = "9"; # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96796 # # This patch can most likely be removed by a post 9.3.0-release. - [ ./avoid-cycling-subreg-reloads.patch ] + [ ./avoid-cycling-subreg-reloads.patch ./gcc9-asan-glibc-2.34.patch ] ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch ++ optional targetPlatform.isNetBSD ../libstdc++-netbsd-ctypes.patch ++ optional noSysDirs ../no-sys-dirs.patch diff --git a/pkgs/development/compilers/gcc/9/gcc9-asan-glibc-2.34.patch b/pkgs/development/compilers/gcc/9/gcc9-asan-glibc-2.34.patch new file mode 100644 index 000000000000..1aea1f9b18a1 --- /dev/null +++ b/pkgs/development/compilers/gcc/9/gcc9-asan-glibc-2.34.patch @@ -0,0 +1,70 @@ +From 3d0135bf3be416bbe2531dc763d19b749eb2b856 Mon Sep 17 00:00:00 2001 +From: Jakub Jelinek +Date: Sat, 17 Apr 2021 11:27:14 +0200 +Subject: [PATCH] sanitizer: Fix asan against glibc 2.34 [PR100114] + +As mentioned in the PR, SIGSTKSZ is no longer a compile time constant in +glibc 2.34 and later, so +static const uptr kAltStackSize = SIGSTKSZ * 4; +needs dynamic initialization, but is used by a function called indirectly +from .preinit_array and therefore before the variable is constructed. +This results in using 0 size instead and all asan instrumented programs +die with: +==91==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22) + +Here is a cherry-pick from upstream to fix this. + +2021-04-17 Jakub Jelinek + + PR sanitizer/100114 + * sanitizer_common/sanitizer_posix_libcdep.cc: Cherry-pick + llvm-project revisions 82150606fb11d28813ae6da1101f5bda638165fe + and b93629dd335ffee2fc4b9b619bf86c3f9e6b0023. + +(cherry picked from commit 950bac27d63c1c2ac3a6ed867692d6a13f21feb3) +--- + .../sanitizer_common/sanitizer_posix_libcdep.cc | 13 ++++++++----- + 1 file changed, 8 insertions(+), 5 deletions(-) + +diff --git a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc +index d2fd76a6d36..1917e29ced2 100644 +--- a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc ++++ b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc +@@ -169,7 +169,11 @@ bool SupportsColoredOutput(fd_t fd) { + + #if !SANITIZER_GO + // TODO(glider): different tools may require different 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; +@@ -180,10 +184,9 @@ 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__); +- altstack.ss_sp = (char*) base; ++ altstack.ss_size = GetAltStackSize(); ++ altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__); + altstack.ss_flags = 0; +- altstack.ss_size = kAltStackSize; + CHECK_EQ(0, sigaltstack(&altstack, nullptr)); + } + +@@ -191,7 +194,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); + } +-- +2.27.0 +