Skip to content

Commit 58f7543

Browse files
committed
[sanitizer] Move signal blocking code into posix
This will affect only Darwin, as the rest alredy do that. Reviewed By: rsundahl Differential Revision: https://reviews.llvm.org/D156385
1 parent 491b281 commit 58f7543

File tree

6 files changed

+49
-56
lines changed

6 files changed

+49
-56
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,9 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
224224
auto args = asanThreadArgRetval().GetArgs(self);
225225
t->ThreadStart(GetTid());
226226

227-
# if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
228-
SANITIZER_SOLARIS
229227
__sanitizer_sigset_t sigset;
230228
t->GetStartData(sigset);
231229
SetSigProcMask(&sigset, nullptr);
232-
# endif
233230

234231
thread_return_t retval = (*args.routine)(args.arg_retval);
235232
asanThreadArgRetval().Finish(self, retval);
@@ -252,10 +249,7 @@ INTERCEPTOR(int, pthread_create, void *thread, void *attr,
252249
u32 current_tid = GetCurrentTidOrInvalid();
253250

254251
__sanitizer_sigset_t sigset;
255-
# if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
256-
SANITIZER_SOLARIS
257252
ScopedBlockSignals block(&sigset);
258-
# endif
259253

260254
AsanThread *t = AsanThread::Create(sigset, current_tid, &stack, detached);
261255

compiler-rt/lib/asan/asan_posix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ void PlatformTSDDtor(void *tsd) {
110110
key.key = nullptr;
111111
// Make sure that signal handler can not see a stale current thread pointer.
112112
atomic_signal_fence(memory_order_seq_cst);
113+
// After this point it's unsafe to execute signal handlers which may be
114+
// instrumented.
115+
BlockSignals();
113116
AsanThread::TSDDtor(tsd);
114117
}
115118
#else
@@ -138,12 +141,9 @@ void PlatformTSDDtor(void *tsd) {
138141
CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
139142
return;
140143
}
141-
# if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
142-
SANITIZER_SOLARIS
143144
// After this point it's unsafe to execute signal handlers which may be
144-
// instrumented. It's probably not just a Linux issue.
145+
// instrumented.
145146
BlockSignals();
146-
# endif
147147
AsanThread::TSDDtor(tsd);
148148
}
149149
#endif

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,6 @@ const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
156156

157157
namespace __sanitizer {
158158

159-
void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
160-
CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
161-
}
162-
163-
void BlockSignals(__sanitizer_sigset_t *oldset) {
164-
__sanitizer_sigset_t set;
165-
internal_sigfillset(&set);
166-
# if SANITIZER_LINUX && !SANITIZER_ANDROID
167-
// Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
168-
// on any thread, setuid call hangs.
169-
// See test/sanitizer_common/TestCases/Linux/setuid.c.
170-
internal_sigdelset(&set, 33);
171-
# endif
172-
# if SANITIZER_LINUX
173-
// Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
174-
// If this signal is blocked, such calls cannot be handled and the process may
175-
// hang.
176-
internal_sigdelset(&set, 31);
177-
# endif
178-
SetSigProcMask(&set, oldset);
179-
}
180-
181-
ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
182-
BlockSignals(&saved_);
183-
if (copy)
184-
internal_memcpy(copy, &saved_, sizeof(saved_));
185-
}
186-
187-
ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_, nullptr); }
188-
189159
# if SANITIZER_LINUX && defined(__x86_64__)
190160
# include "sanitizer_syscall_linux_x86_64.inc"
191161
# elif SANITIZER_LINUX && SANITIZER_RISCV64

compiler-rt/lib/sanitizer_common/sanitizer_linux.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,7 @@ void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
4646

4747
// Syscall wrappers.
4848
uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
49-
uptr internal_sigaltstack(const void* ss, void* oss);
50-
uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
51-
__sanitizer_sigset_t *oldset);
52-
53-
void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
54-
void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
55-
struct ScopedBlockSignals {
56-
explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
57-
~ScopedBlockSignals();
58-
59-
ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
60-
ScopedBlockSignals(const ScopedBlockSignals &) = delete;
61-
62-
private:
63-
__sanitizer_sigset_t saved_;
64-
};
49+
uptr internal_sigaltstack(const void *ss, void *oss);
6550

6651
# if SANITIZER_GLIBC
6752
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);

compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,35 @@ uptr MmapNamed(void *addr, uptr length, int prot, int flags, const char *name) {
406406
return res;
407407
}
408408

409+
void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
410+
CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
411+
}
412+
413+
void BlockSignals(__sanitizer_sigset_t *oldset) {
414+
__sanitizer_sigset_t set;
415+
internal_sigfillset(&set);
416+
# if SANITIZER_LINUX && !SANITIZER_ANDROID
417+
// Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
418+
// on any thread, setuid call hangs.
419+
// See test/sanitizer_common/TestCases/Linux/setuid.c.
420+
internal_sigdelset(&set, 33);
421+
# endif
422+
# if SANITIZER_LINUX
423+
// Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
424+
// If this signal is blocked, such calls cannot be handled and the process may
425+
// hang.
426+
internal_sigdelset(&set, 31);
427+
# endif
428+
SetSigProcMask(&set, oldset);
429+
}
430+
431+
ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
432+
BlockSignals(&saved_);
433+
if (copy)
434+
internal_memcpy(copy, &saved_, sizeof(saved_));
435+
}
436+
437+
ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_, nullptr); }
409438

410439
} // namespace __sanitizer
411440

compiler-rt/lib/sanitizer_common/sanitizer_posix.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ int internal_sigaction(int signum, const void *act, void *oldact);
9898
void internal_sigfillset(__sanitizer_sigset_t *set);
9999
void internal_sigemptyset(__sanitizer_sigset_t *set);
100100
bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
101+
uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
102+
__sanitizer_sigset_t *oldset);
101103

102104
uptr internal_execve(const char *filename, char *const argv[],
103105
char *const envp[]);
@@ -124,6 +126,19 @@ void DecorateMapping(uptr addr, uptr size, const char *name);
124126
# define __sanitizer_dirsiz(dp) ((dp)->d_reclen)
125127
# endif
126128

129+
void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
130+
void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
131+
struct ScopedBlockSignals {
132+
explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
133+
~ScopedBlockSignals();
134+
135+
ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
136+
ScopedBlockSignals(const ScopedBlockSignals &) = delete;
137+
138+
private:
139+
__sanitizer_sigset_t saved_;
140+
};
141+
127142
} // namespace __sanitizer
128143

129144
#endif // SANITIZER_POSIX

0 commit comments

Comments
 (0)