1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // Ensure that the unwinder can cope with the signal handler.
11 // REQUIRES: linux && (target={{aarch64-.+}} || target={{x86_64-.+}})
12 
13 // TODO: Investigate these failures
14 // XFAIL: asan, tsan, ubsan
15 
16 // TODO: Investigate this failure
17 // XFAIL: 32bits-on-64bits
18 
19 #include <assert.h>
20 #include <dlfcn.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <unwind.h>
28 
frame_handler(struct _Unwind_Context * ctx,void * arg)29 _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
30   (void)arg;
31   Dl_info info = { 0, 0, 0, 0 };
32 
33   // Unwind util the main is reached, above frames depend on the platform and
34   // architecture.
35   if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
36       info.dli_sname && !strcmp("main", info.dli_sname)) {
37     _Exit(0);
38   }
39   return _URC_NO_REASON;
40 }
41 
signal_handler(int signum)42 void signal_handler(int signum) {
43   (void)signum;
44   _Unwind_Backtrace(frame_handler, NULL);
45   _Exit(-1);
46 }
47 
main(int,char **)48 int main(int, char**) {
49   signal(SIGUSR1, signal_handler);
50   kill(getpid(), SIGUSR1);
51   return -2;
52 }
53