1 // Regression test for 2 // https://code.google.com/p/address-sanitizer/issues/detail?id=180 3 4 // RUN: %clangxx_asan -O0 %s -o %t && ASAN_OPTIONS=allow_user_segv_handler=true not %run %t 2>&1 | FileCheck %s 5 // RUN: %clangxx_asan -O2 %s -o %t && ASAN_OPTIONS=allow_user_segv_handler=true not %run %t 2>&1 | FileCheck %s 6 7 #include <signal.h> 8 #include <stdio.h> 9 10 struct sigaction user_sigaction; 11 struct sigaction original_sigaction; 12 13 void User_OnSIGSEGV(int signum, siginfo_t *siginfo, void *context) { 14 fprintf(stderr, "User sigaction called\n"); 15 if (original_sigaction.sa_flags | SA_SIGINFO) 16 original_sigaction.sa_sigaction(signum, siginfo, context); 17 else 18 original_sigaction.sa_handler(signum); 19 } 20 21 int DoSEGV() { 22 volatile int *x = 0; 23 return *x; 24 } 25 26 int main() { 27 user_sigaction.sa_sigaction = User_OnSIGSEGV; 28 user_sigaction.sa_flags = SA_SIGINFO; 29 #if defined(__APPLE__) && !defined(__LP64__) 30 // On 32-bit Darwin KERN_PROTECTION_FAILURE (SIGBUS) is delivered. 31 int signum = SIGBUS; 32 #else 33 // On 64-bit Darwin KERN_INVALID_ADDRESS (SIGSEGV) is delivered. 34 // On Linux SIGSEGV is delivered as well. 35 int signum = SIGSEGV; 36 #endif 37 if (sigaction(signum, &user_sigaction, &original_sigaction)) { 38 perror("sigaction"); 39 return 1; 40 } 41 fprintf(stderr, "User sigaction installed\n"); 42 return DoSEGV(); 43 } 44 45 // CHECK: User sigaction installed 46 // CHECK-NEXT: User sigaction called 47 // CHECK-NEXT: ASAN:SIGSEGV 48 // CHECK: AddressSanitizer: SEGV on unknown address 49