54 lines · cpp
1// UNSUPPORTED: android2// UNSUPPORTED: hwasan3 4// RUN: %clangxx -O0 %s -o %t5 6// Sanitizer signal handler not installed; custom signal handler installed7// RUN: %env_tool_opts=handle_segv=0:cloak_sanitizer_signal_handlers=false not %run %t 2>&1 | FileCheck %s --check-prefixes=DEFAULT,CUSTOM8// RUN: %env_tool_opts=handle_segv=0:cloak_sanitizer_signal_handlers=true not %run %t 2>&1 | FileCheck %s --check-prefixes=DEFAULT,CUSTOM9 10// Sanitizer signal handler installed but overriden by custom signal handler11// RUN: %env_tool_opts=handle_segv=1:cloak_sanitizer_signal_handlers=false not %run %t 2>&1 | FileCheck %s --check-prefixes=NONDEFAULT,CUSTOM12// RUN: %env_tool_opts=handle_segv=1:cloak_sanitizer_signal_handlers=true not %run %t 2>&1 | FileCheck %s --check-prefixes=DEFAULT,CUSTOM13 14// Sanitizer signal handler installed immutably15// N.B. for handle_segv=2 with cloaking off, there is a pre-existing difference16// in signal vs. sigaction: signal effectively cloaks the handler.17// RUN: %env_tool_opts=handle_segv=2:cloak_sanitizer_signal_handlers=false not %run %t 2>&1 | FileCheck %s --check-prefixes=NONDEFAULT,SANITIZER18// RUN: %env_tool_opts=handle_segv=2:cloak_sanitizer_signal_handlers=true not %run %t 2>&1 | FileCheck %s --check-prefixes=DEFAULT,SANITIZER19 20#include <signal.h>21#include <stdio.h>22#include <stdlib.h>23 24void handler(int signum, siginfo_t *info, void *context) {25 printf("Custom signal handler\n");26 exit(1);27}28 29int main(int argc, char *argv[]) {30 struct sigaction sa = {0};31 struct sigaction old = {0};32 sa.sa_flags = SA_SIGINFO;33 sa.sa_sigaction = &handler;34 sigaction(SIGSEGV, &sa, &old);35 36 if (reinterpret_cast<void *>(old.sa_sigaction) == SIG_DFL)37 printf("Old handler: default\n");38 // DEFAULT: Old handler: default39 else40 printf("Old handler: non-default\n");41 // NONDEFAULT: Old handler: non-default42 43 fflush(stdout);44 45 // Trying to organically segfault by dereferencing a pointer can be tricky46 // in builds with assertions. Additionally, some older platforms may SIGBUS47 // instead.48 raise(SIGSEGV);49 // CUSTOM: Custom signal handler50 // SANITIZER: Sanitizer:DEADLYSIGNAL51 52 return 0;53}54