1From 077bfc4e7bf67efda437583bac7e94197da88881 Mon Sep 17 00:00:00 2001
2From: Jakub Jelinek <jakub@redhat.com>
3Date: Sat, 17 Apr 2021 11:27:14 +0200
4Subject: [PATCH] sanitizer: Fix asan against glibc 2.34 [PR100114]
5
6As mentioned in the PR, SIGSTKSZ is no longer a compile time constant in
7glibc 2.34 and later, so
8static const uptr kAltStackSize = SIGSTKSZ * 4;
9needs dynamic initialization, but is used by a function called indirectly
10from .preinit_array and therefore before the variable is constructed.
11This results in using 0 size instead and all asan instrumented programs
12die with:
13==91==ERROR: AddressSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22)
14
15Here is a cherry-pick from upstream to fix this.
16
172021-04-17  Jakub Jelinek  <jakub@redhat.com>
18
19	PR sanitizer/100114
20	* sanitizer_common/sanitizer_posix_libcdep.cc: Cherry-pick
21	llvm-project revisions 82150606fb11d28813ae6da1101f5bda638165fe
22	and b93629dd335ffee2fc4b9b619bf86c3f9e6b0023.
23
24(cherry picked from commit 950bac27d63c1c2ac3a6ed867692d6a13f21feb3)
25---
26 .../sanitizer_common/sanitizer_posix_libcdep.cc     | 13 ++++++++-----
27 1 file changed, 8 insertions(+), 5 deletions(-)
28
29diff --git a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc
30index 1a37118c2..066079b39 100644
31--- a/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc
32+++ b/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc
33@@ -159,7 +159,11 @@ bool SupportsColoredOutput(fd_t fd) {
34
35 #if !SANITIZER_GO
36 // TODO(glider): different tools may require different altstack size.
37-static const uptr kAltStackSize = SIGSTKSZ * 4;  // SIGSTKSZ is not enough.
38+static uptr GetAltStackSize() {
39+  // SIGSTKSZ is not enough.
40+  static const uptr kAltStackSize = SIGSTKSZ * 4;
41+  return kAltStackSize;
42+}
43
44 void SetAlternateSignalStack() {
45   stack_t altstack, oldstack;
46@@ -170,10 +174,9 @@ void SetAlternateSignalStack() {
47   // TODO(glider): the mapped stack should have the MAP_STACK flag in the
48   // future. It is not required by man 2 sigaltstack now (they're using
49   // malloc()).
50-  void* base = MmapOrDie(kAltStackSize, __func__);
51-  altstack.ss_sp = (char*) base;
52+  altstack.ss_size = GetAltStackSize();
53+  altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__);
54   altstack.ss_flags = 0;
55-  altstack.ss_size = kAltStackSize;
56   CHECK_EQ(0, sigaltstack(&altstack, nullptr));
57 }
58
59@@ -181,7 +184,7 @@ void UnsetAlternateSignalStack() {
60   stack_t altstack, oldstack;
61   altstack.ss_sp = nullptr;
62   altstack.ss_flags = SS_DISABLE;
63-  altstack.ss_size = kAltStackSize;  // Some sane value required on Darwin.
64+  altstack.ss_size = GetAltStackSize();  // Some sane value required on Darwin.
65   CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
66   UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
67 }
68--
692.20.1
70
71