[compiler-rt] 266a8d5 - [tsan] Fix sigaction interceptor after D107186
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 24 10:19:06 PDT 2021
Author: Vitaly Buka
Date: 2021-08-24T10:19:02-07:00
New Revision: 266a8d5cfe9a351468561aebe18203399e2ccc32
URL: https://github.com/llvm/llvm-project/commit/266a8d5cfe9a351468561aebe18203399e2ccc32
DIFF: https://github.com/llvm/llvm-project/commit/266a8d5cfe9a351468561aebe18203399e2ccc32.diff
LOG: [tsan] Fix sigaction interceptor after D107186
Set SA_SIGINFO only if we set sighandler, or we can set the flag, and
return it as 'old' without actual sigaction set.
Reviewed By: melver
Differential Revision: https://reviews.llvm.org/D108616
Added:
compiler-rt/test/sanitizer_common/TestCases/Posix/replace_sigaction.cpp
Modified:
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index ec3a548f05aa4..788f22791d5e2 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -2459,9 +2459,11 @@ int sigaction_impl(int sig, const __sanitizer_sigaction *act,
#endif
internal_memcpy(&newact, act, sizeof(newact));
internal_sigfillset(&newact.sa_mask);
- newact.sa_flags |= SA_SIGINFO;
- if ((uptr)act->handler != sig_ign && (uptr)act->handler != sig_dfl)
+ if ((act->sa_flags & SA_SIGINFO) ||
+ ((uptr)act->handler != sig_ign && (uptr)act->handler != sig_dfl)) {
+ newact.sa_flags |= SA_SIGINFO;
newact.sigaction = sighandler;
+ }
ReleaseStore(thr, pc, (uptr)&sigactions[sig]);
act = &newact;
}
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/replace_sigaction.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/replace_sigaction.cpp
new file mode 100644
index 0000000000000..a2b44c6122a37
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/replace_sigaction.cpp
@@ -0,0 +1,66 @@
+// RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <signal.h>
+#include <unistd.h>
+
+static bool sahandler_done;
+static bool sasigaction_done;
+
+static void sahandler(int) { sahandler_done = true; }
+
+static void sasigaction(int, siginfo_t *, void *) { sasigaction_done = true; }
+
+template <typename T> void install(T *handler, struct sigaction *prev) {
+ bool siginfo = handler == (T *)&sasigaction;
+ struct sigaction act = {};
+ if (siginfo) {
+ act.sa_flags = SA_SIGINFO;
+ act.sa_sigaction = (decltype(act.sa_sigaction))handler;
+ } else {
+ act.sa_handler = (decltype(act.sa_handler))handler;
+ }
+ int ret = sigaction(SIGHUP, &act, prev);
+ assert(ret == 0);
+
+ if (handler == (T *)&sahandler) {
+ sahandler_done = false;
+ raise(SIGHUP);
+ assert(sahandler_done);
+ }
+
+ if (handler == (T *)&sasigaction) {
+ sasigaction_done = false;
+ raise(SIGHUP);
+ assert(sasigaction_done);
+ }
+}
+
+template <typename T1, typename T2> void test(T1 *from, T2 *to) {
+ install(from, nullptr);
+ struct sigaction prev = {};
+ install(to, &prev);
+
+ bool siginfo_from = (from == (T1 *)&sasigaction);
+ if (siginfo_from) {
+ assert(prev.sa_flags & SA_SIGINFO);
+ assert(prev.sa_sigaction == (decltype(prev.sa_sigaction))from);
+ } else {
+ assert((prev.sa_flags & SA_SIGINFO) == 0);
+ assert(prev.sa_handler == (decltype(prev.sa_handler))from);
+ }
+}
+
+template <typename T> void testAll(T *to) {
+ test(&sahandler, to);
+ test(&sasigaction, to);
+ test(SIG_IGN, to);
+ test(SIG_DFL, to);
+}
+
+int main(void) {
+ testAll(&sahandler);
+ testAll(&sasigaction);
+ testAll(SIG_IGN);
+ testAll(SIG_DFL);
+}
More information about the llvm-commits
mailing list