blob: af5f234b8da681a78f11a6a4256e68c586da80b1 [file] [log] [blame]
Daniel Kiss6bf52ee2021-08-11 10:11:30 +02001// -*- 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// REQUIRES: linux
11
Louis Dionne31359e02022-03-02 17:49:13 -050012// TODO: Figure out why this fails with Memory Sanitizer.
13// XFAIL: msan
14
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020015// Basic test for _Unwind_ForcedUnwind.
16// See libcxxabi/test/forced_unwind* tests too.
17
18#include <assert.h>
19#include <dlfcn.h>
20#include <signal.h>
21#include <stdint.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
29void foo();
30_Unwind_Exception ex;
31
32_Unwind_Reason_Code stop(int version, _Unwind_Action actions,
Daniel Kiss0517d862021-09-02 11:30:26 +020033 _Unwind_Exception_Class exceptionClass,
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020034 _Unwind_Exception *exceptionObject,
35 struct _Unwind_Context *context,
36 void *stop_parameter) {
37 assert(version == 1);
38 assert((actions & _UA_FORCE_UNWIND) != 0);
39 (void)exceptionClass;
40 assert(exceptionObject == &ex);
41 assert(stop_parameter == &foo);
42
43 Dl_info info = {0, 0, 0, 0};
44
45 // Unwind util the main is reached, above frames depend on the platform and
46 // architecture.
47 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
48 info.dli_sname && !strcmp("main", info.dli_sname)) {
49 _Exit(0);
50 }
51 return _URC_NO_REASON;
52}
53
54__attribute__((noinline)) void foo() {
55
56 // Arm EHABI defines struct _Unwind_Control_Block as exception
57 // object. Ensure struct _Unwind_Exception* work there too,
58 // because _Unwind_Exception in this case is just an alias.
59 struct _Unwind_Exception *e = &ex;
60#if defined(_LIBUNWIND_ARM_EHABI)
61 // Create a mock exception object.
62 memset(e, '\0', sizeof(*e));
Daniel Kiss0517d862021-09-02 11:30:26 +020063 strcpy(reinterpret_cast<char *>(&e->exception_class), "CLNGUNW");
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020064#endif
65 _Unwind_ForcedUnwind(e, stop, (void *)&foo);
66}
67
68int main() {
69 foo();
70 return -2;
71}