From 6afe775d2ca25bb266f9c343861a1c33858f53db Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 16 Sep 2014 21:48:22 +0000 Subject: tsan: support longjmp out of signal handlers Fixes https://code.google.com/p/thread-sanitizer/issues/detail?id=75 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@217908 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tsan/signal_longjmp.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/tsan/signal_longjmp.cc (limited to 'test/tsan') diff --git a/test/tsan/signal_longjmp.cc b/test/tsan/signal_longjmp.cc new file mode 100644 index 000000000..ce0993146 --- /dev/null +++ b/test/tsan/signal_longjmp.cc @@ -0,0 +1,63 @@ +// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s + +// Test case for longjumping out of signal handler: +// https://code.google.com/p/thread-sanitizer/issues/detail?id=71 + +#include +#include +#include +#include + +sigjmp_buf fault_jmp; +volatile int fault_expected; + +void sigfault_handler(int sig) { + if (!fault_expected) + abort(); + + /* just return from sighandler to proper place */ + fault_expected = 0; + siglongjmp(fault_jmp, 1); +} + +#define MUST_FAULT(code) do { \ + fault_expected = 1; \ + if (!sigsetjmp(fault_jmp, 1)) { \ + code; /* should pagefault -> sihandler does longjmp */ \ + fprintf(stderr, "%s not faulted\n", #code); \ + abort(); \ + } else { \ + fprintf(stderr, "%s faulted ok\n", #code); \ + } \ +} while (0) + +int main() { + struct sigaction act; + act.sa_handler = sigfault_handler; + act.sa_flags = 0; + if (sigemptyset(&act.sa_mask)) { + perror("sigemptyset"); + exit(1); + } + + if (sigaction(SIGSEGV, &act, NULL)) { + perror("sigaction"); + exit(1); + } + + MUST_FAULT(((volatile int *volatile)0)[0] = 0); + MUST_FAULT(((volatile int *volatile)0)[1] = 1); + MUST_FAULT(((volatile int *volatile)0)[3] = 1); + + // Ensure that tsan does not think that we are + // in a signal handler. + void *volatile p = malloc(10); + ((volatile int*)p)[1] = 1; + free((void*)p); + + fprintf(stderr, "DONE\n"); + return 0; +} + +// CHECK-NOT: WARNING: ThreadSanitizer +// CHECK: DONE -- cgit v1.2.3