1// RUN: %clangxx -O1 %s -o %t && %env_tool_opts=handle_sigtrap=1 %run %t 2>&1 | FileCheck %s
2
3// __builtin_debugtrap() does not raise SIGTRAP on these platforms.
4// UNSUPPORTED: target=s390{{.*}}
5
6#include <assert.h>
7#include <signal.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11int in_handler;
12
13void handler(int signo, siginfo_t *info, void *uctx) {
14 fprintf(stderr, format: "in_handler: %d\n", in_handler);
15 fflush(stderr);
16 // CHECK: in_handler: 1
17 _Exit(status: 0);
18}
19
20int main() {
21 struct sigaction a = {}, old = {};
22 a.sa_sigaction = handler;
23 a.sa_flags = SA_SIGINFO;
24 sigaction(SIGTRAP, act: &a, oact: &old);
25
26 a = {};
27 sigaction(SIGTRAP, act: 0, oact: &a);
28 assert(a.sa_sigaction == handler);
29 assert(a.sa_flags & SA_SIGINFO);
30
31 in_handler = 1;
32 // Check that signal handler is not postponed by sanitizer.
33 // Don't use raise here as it calls any signal handler immediately.
34 __builtin_debugtrap();
35 in_handler = 0;
36
37 fprintf(stderr, format: "UNREACHABLE\n");
38 return 1;
39}
40

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/signal_trap_handler.cpp