Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 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 leaf function can be unwund. |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 11 | // REQUIRES: linux && (target={{aarch64-.+}} || target={{s390x-.+}} || target={{x86_64-.+}}) |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 12 | |
Louis Dionne | 31359e0 | 2022-03-02 17:49:13 -0500 | [diff] [blame] | 13 | // TODO: Figure out why this fails with Memory Sanitizer. |
| 14 | // XFAIL: msan |
| 15 | |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <dlfcn.h> |
| 18 | #include <signal.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | #include <unwind.h> |
| 25 | |
| 26 | _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { |
| 27 | (void)arg; |
| 28 | Dl_info info = { 0, 0, 0, 0 }; |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 29 | |
| 30 | // Unwind util the main is reached, above frames deeped on the platfrom and architecture. |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 31 | if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) && |
| 32 | info.dli_sname && !strcmp("main", info.dli_sname)) { |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 33 | _Exit(0); |
| 34 | } |
| 35 | return _URC_NO_REASON; |
| 36 | } |
| 37 | |
| 38 | void signal_handler(int signum) { |
| 39 | (void)signum; |
| 40 | _Unwind_Backtrace(frame_handler, NULL); |
| 41 | _Exit(-1); |
| 42 | } |
| 43 | |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 44 | __attribute__((noinline)) void crashing_leaf_func(void) { |
Leonard Chan | 74c6eec | 2021-12-03 11:20:06 -0800 | [diff] [blame] | 45 | // libunwind searches for the address before the return address which points |
| 46 | // to the trap instruction. NOP guarantees the trap instruction is not the |
| 47 | // first instruction of the function. |
| 48 | // We should keep this here for other unwinders that also decrement pc. |
| 49 | __asm__ __volatile__("nop"); |
| 50 | __builtin_trap(); |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 53 | int main(int, char**) { |
Leonard Chan | 74c6eec | 2021-12-03 11:20:06 -0800 | [diff] [blame] | 54 | signal(SIGTRAP, signal_handler); |
| 55 | signal(SIGILL, signal_handler); |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 56 | crashing_leaf_func(); |
| 57 | return -2; |
Louis Dionne | f51a154 | 2021-11-22 14:51:09 -0500 | [diff] [blame] | 58 | } |