81 lines · cpp
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-exception7//8//===----------------------------------------------------------------------===//9 10// UNSUPPORTED: target={{.*-apple.*}}11// UNSUPPORTED: target={{.*-aix.*}}12// UNSUPPORTED: target={{.*-windows.*}}13 14// TODO: Figure out why this fails with Memory Sanitizer.15// XFAIL: msan16 17// Basic test for _Unwind_ForcedUnwind.18// See libcxxabi/test/forced_unwind* tests too.19 20#undef NDEBUG21#include <assert.h>22#include <signal.h>23#include <stdint.h>24#include <stdio.h>25#include <stdlib.h>26#include <string.h>27#include <sys/types.h>28#include <unistd.h>29#include <unwind.h>30 31// Using __attribute__((section("main_func"))) is Linux specific, but then32// this entire test is marked as requiring Linux, so we should be good.33//34// We don't use dladdr() because on musl it's a no-op when statically linked.35extern char __start_main_func;36extern char __stop_main_func;37 38void foo();39_Unwind_Exception ex;40 41_Unwind_Reason_Code stop(int version, _Unwind_Action actions,42 _Unwind_Exception_Class exceptionClass,43 _Unwind_Exception *exceptionObject,44 struct _Unwind_Context *context,45 void *stop_parameter) {46 assert(version == 1);47 assert((actions & _UA_FORCE_UNWIND) != 0);48 (void)exceptionClass;49 assert(exceptionObject == &ex);50 assert(stop_parameter == &foo);51 52 // Unwind until the main is reached, above frames depend on the platform and53 // architecture.54 uintptr_t ip = _Unwind_GetIP(context);55 if (ip >= (uintptr_t)&__start_main_func &&56 ip < (uintptr_t)&__stop_main_func) {57 _Exit(0);58 }59 60 return _URC_NO_REASON;61}62 63__attribute__((noinline)) void foo() {64 65 // Arm EHABI defines struct _Unwind_Control_Block as exception66 // object. Ensure struct _Unwind_Exception* work there too,67 // because _Unwind_Exception in this case is just an alias.68 struct _Unwind_Exception *e = &ex;69#if defined(_LIBUNWIND_ARM_EHABI)70 // Create a mock exception object.71 memset(e, '\0', sizeof(*e));72 memcpy(&e->exception_class, "CLNGUNW", sizeof(e->exception_class));73#endif74 _Unwind_ForcedUnwind(e, stop, (void *)&foo);75}76 77__attribute__((section("main_func"))) int main(int, char **) {78 foo();79 return -2;80}81