90 lines · cpp
1// Regression test for2// https://code.google.com/p/address-sanitizer/issues/detail?id=1803 4// FIXME: Implement.5// XFAIL: hwasan6 7// RUN: %clangxx -O0 %s -o %t8 9// RUN: %env_tool_opts=handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK010// RUN: %env_tool_opts=handle_segv=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK111// RUN: %env_tool_opts=handle_segv=2 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK212 13// RUN: %env_tool_opts=handle_segv=0:allow_user_segv_handler=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK014// RUN: %env_tool_opts=handle_segv=1:allow_user_segv_handler=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK215// RUN: %env_tool_opts=handle_segv=2:allow_user_segv_handler=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK216 17// RUN: %env_tool_opts=handle_segv=0:allow_user_segv_handler=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK018// RUN: %env_tool_opts=handle_segv=1:allow_user_segv_handler=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK119// RUN: %env_tool_opts=handle_segv=2:allow_user_segv_handler=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK220 21// Flaky errors in debuggerd with "waitpid returned unexpected pid (0)" in logcat.22// UNSUPPORTED: android && i386-target-arch23 24// Note: this test case is unusual because it retrieves the original25// (ASan-installed) signal handler; thus, it is incompatible with the26// cloak_sanitizer_signal_handlers runtime option.27 28#include <signal.h>29#include <stdio.h>30#include <stdlib.h>31 32struct sigaction original_sigaction_sigsegv;33 34void User_OnSIGSEGV(int signum, siginfo_t *siginfo, void *context) {35 fprintf(stderr, "User sigaction called\n");36 struct sigaction original_sigaction = {};37 if (signum == SIGSEGV)38 original_sigaction = original_sigaction_sigsegv;39 else {40 printf("Invalid signum");41 exit(1);42 }43 if (original_sigaction.sa_flags | SA_SIGINFO) {44 if (original_sigaction.sa_sigaction)45 original_sigaction.sa_sigaction(signum, siginfo, context);46 } else {47 if (original_sigaction.sa_handler)48 original_sigaction.sa_handler(signum);49 }50 exit(1);51}52 53bool InstallHandler(int signum, struct sigaction *original_sigaction) {54 struct sigaction user_sigaction = {};55 user_sigaction.sa_sigaction = User_OnSIGSEGV;56 user_sigaction.sa_flags = SA_SIGINFO;57 if (sigaction(signum, &user_sigaction, original_sigaction)) {58 perror("sigaction");59 return false;60 }61 return true;62}63 64int main() {65 if (InstallHandler(SIGSEGV, &original_sigaction_sigsegv))66 fprintf(stderr, "User sigaction installed\n");67 68 // Trying to organically segfault by dereferencing a pointer can be tricky69 // when the sanitizer runtime is built with assertions. Additionally, some70 // older platforms may SIGBUS instead.71 raise(SIGSEGV);72 73 return 0;74}75 76// CHECK0-NOT: Sanitizer:DEADLYSIGNAL77// CHECK0-NOT: Sanitizer: SEGV on unknown address78// CHECK0: User sigaction installed79// CHECK0-NEXT: User sigaction called80 81// CHECK1: User sigaction installed82// CHECK1-NEXT: User sigaction called83// CHECK1-NEXT: Sanitizer:DEADLYSIGNAL84// CHECK1: Sanitizer: SEGV on unknown address85 86// CHECK2-NOT: User sigaction called87// CHECK2: User sigaction installed88// CHECK2-NEXT: Sanitizer:DEADLYSIGNAL89// CHECK2: Sanitizer: SEGV on unknown address90