llvm-project
64 строки · 1.8 Кб
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: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
12
13// TODO: Figure out why this fails with Memory Sanitizer.
14// XFAIL: msan
15
16// Note: this test fails on musl because:
17//
18// (a) musl disables emission of unwind information for its build, and
19// (b) musl's signal trampolines don't include unwind information
20//
21// XFAIL: target={{.*}}-musl
22
23#undef NDEBUG24#include <assert.h>25#include <signal.h>26#include <stdio.h>27#include <stdlib.h>28#include <string.h>29#include <sys/types.h>30#include <unistd.h>31#include <unwind.h>32
33// Using __attribute__((section("main_func"))) is ELF specific, but then
34// this entire test is marked as requiring Linux, so we should be good.
35//
36// We don't use dladdr() because on musl it's a no-op when statically linked.
37extern char __start_main_func;38extern char __stop_main_func;39
40_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {41(void)arg;42
43// Unwind until the main is reached, above frames depend on the platform and44// architecture.45uintptr_t ip = _Unwind_GetIP(ctx);46if (ip >= (uintptr_t)&__start_main_func &&47ip < (uintptr_t)&__stop_main_func) {48_Exit(0);49}50
51return _URC_NO_REASON;52}
53
54void signal_handler(int signum) {55(void)signum;56_Unwind_Backtrace(frame_handler, NULL);57_Exit(-1);58}
59
60__attribute__((section("main_func"))) int main(int, char **) {61signal(SIGUSR1, signal_handler);62kill(getpid(), SIGUSR1);63return -2;64}
65