1 // RUN: %clangxx -std=c++11 %s -o %t
2 // RUN: %env_asan_opts=handle_segv=1 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
3 // RUN: %env_asan_opts=handle_segv=2 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
5 // RUN: %clangxx -std=c++11 -DTEST_INSTALL_SIG_HANDLER %s -o %t
6 // RUN: %env_asan_opts=handle_segv=0 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-HANDLER
7 // RUN: %env_asan_opts=handle_segv=1 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
8 // RUN: %env_asan_opts=handle_segv=2 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
10 // RUN: %clangxx -std=c++11 -DTEST_INSTALL_SIG_ACTION %s -o %t
11 // RUN: %env_asan_opts=handle_segv=0 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ACTION
12 // RUN: %env_asan_opts=handle_segv=1 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
13 // RUN: %env_asan_opts=handle_segv=2 LD_PRELOAD=%shared_libasan not %run %t 2>&1 | FileCheck %s
15 // REQUIRES: asan-dynamic-runtime
17 // This way of setting LD_PRELOAD does not work with Android test runner.
20 // Issue #109573: Cannot use syscall(__NR_rt_sigaction) on Linux/sparc64.
21 // XFAIL: target={{sparc.*-.*-linux.*}}
28 #include <sys/syscall.h>
31 const char *handler
= nullptr;
32 void SigHandler(int signum
) { handler
= "TestSigHandler"; }
33 void SigAction(int, siginfo_t
*, void *) { handler
= "TestSigAction"; }
35 struct KernelSigaction
{
39 __sighandler_t handler
;
41 __sighandler_t handler
;
48 #if defined(__x86_64__)
49 extern "C" void restorer();
50 asm("restorer:mov $15,%rax\nsyscall");
53 int InternalSigaction(int sig
, KernelSigaction
*act
, KernelSigaction
*oact
) {
55 #if defined(__x86_64__)
56 act
->flags
|= 0x04000000;
57 act
->restorer
= &restorer
;
60 return syscall(__NR_rt_sigaction
, sig
, act
, oact
, NSIG
/ 8);
63 struct KernelSigaction pre_asan
= {};
66 int res
= InternalSigaction(SIGSEGV
, nullptr, &pre_asan
);
68 assert(pre_asan
.handler
== SIG_DFL
|| pre_asan
.handler
== SIG_IGN
);
69 #if defined(TEST_INSTALL_SIG_HANDLER)
71 pre_asan
.handler
= &SigHandler
;
72 res
= InternalSigaction(SIGSEGV
, &pre_asan
, nullptr);
74 #elif defined(TEST_INSTALL_SIG_ACTION)
76 pre_asan
.flags
= SA_SIGINFO
| SA_NODEFER
;
77 pre_asan
.handler
= (__sighandler_t
)&SigAction
;
78 res
= InternalSigaction(SIGSEGV
, &pre_asan
, nullptr);
83 __attribute__((section(".preinit_array"), used
))
84 void (*__local_test_preinit
)(void) = Init
;
86 bool ExpectUserHandler() {
87 #if defined(TEST_INSTALL_SIG_HANDLER) || defined(TEST_INSTALL_SIG_ACTION)
88 return !strcmp(getenv("ASAN_OPTIONS"), "handle_segv=0");
93 int main(int argc
, char *argv
[]) {
94 KernelSigaction post_asan
= {};
95 InternalSigaction(SIGSEGV
, nullptr, &post_asan
);
97 assert(post_asan
.handler
!= SIG_DFL
);
98 assert(post_asan
.handler
!= SIG_IGN
);
99 assert(ExpectUserHandler() ==
100 (post_asan
.handler
== pre_asan
.handler
));
103 printf("%s\n", handler
);
107 // CHECK-NOT: TestSig
108 // CHECK: AddressSanitizer:DEADLYSIGNAL
110 // CHECK-HANDLER-NOT: AddressSanitizer:DEADLYSIGNAL
111 // CHECK-HANDLER: TestSigHandler
113 // CHECK-ACTION-NOT: AddressSanitizer:DEADLYSIGNAL
114 // CHECK-ACTION: TestSigAction