// If there is a personality routine, tell it we are unwinding.
if (frameInfo.handler != 0) {
_Unwind_Personality_Fn p =
- (_Unwind_Personality_Fn)(long)(frameInfo.handler);
+ (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
// EHABI #7.2
exception_object->pr_cache.fnstart = frameInfo.start_ip;
return _URC_FATAL_PHASE2_ERROR;
}
+static _Unwind_Reason_Code
+unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
+ _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
+ void *stop_parameter) {
+ // See comment at the start of unwind_phase1 regarding VRS integrity.
+ __unw_init_local(cursor, uc);
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)",
+ static_cast<void *>(exception_object));
+ // Walk each frame until we reach where search phase said to stop
+ while (true) {
+ // Update info about this frame.
+ unw_proc_info_t frameInfo;
+ if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
+ "failed => _URC_END_OF_STACK",
+ (void *)exception_object);
+ return _URC_FATAL_PHASE2_ERROR;
+ }
+
+ // When tracing, print state information.
+ if (_LIBUNWIND_TRACING_UNWINDING) {
+ char functionBuf[512];
+ const char *functionName = functionBuf;
+ unw_word_t offset;
+ if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
+ &offset) != UNW_ESUCCESS) ||
+ (frameInfo.start_ip + offset > frameInfo.end_ip))
+ functionName = ".anonymous.";
+ _LIBUNWIND_TRACE_UNWINDING(
+ "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
+ ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
+ (void *)exception_object, frameInfo.start_ip, functionName,
+ frameInfo.lsda, frameInfo.handler);
+ }
+
+ // Call stop function at each frame.
+ _Unwind_Action action =
+ (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
+ _Unwind_Reason_Code stopResult =
+ (*stop)(1, action, exception_object->exception_class, exception_object,
+ (_Unwind_Context *)(cursor), stop_parameter);
+ _LIBUNWIND_TRACE_UNWINDING(
+ "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
+ (void *)exception_object, stopResult);
+ if (stopResult != _URC_NO_REASON) {
+ _LIBUNWIND_TRACE_UNWINDING(
+ "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
+ (void *)exception_object);
+ return _URC_FATAL_PHASE2_ERROR;
+ }
+
+ // If there is a personality routine, tell it we are unwinding.
+ if (frameInfo.handler != 0) {
+ _Unwind_Personality_Fn p =
+ (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
+ struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
+ // EHABI #7.2
+ exception_object->pr_cache.fnstart = frameInfo.start_ip;
+ exception_object->pr_cache.ehtp =
+ (_Unwind_EHT_Header *)frameInfo.unwind_info;
+ exception_object->pr_cache.additional = frameInfo.flags;
+ _Unwind_Reason_Code personalityResult =
+ (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object,
+ context);
+ switch (personalityResult) {
+ case _URC_CONTINUE_UNWIND:
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
+ "personality returned "
+ "_URC_CONTINUE_UNWIND",
+ (void *)exception_object);
+ // Destructors called, continue unwinding
+ break;
+ case _URC_INSTALL_CONTEXT:
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
+ "personality returned "
+ "_URC_INSTALL_CONTEXT",
+ (void *)exception_object);
+ // We may get control back if landing pad calls _Unwind_Resume().
+ __unw_resume(cursor);
+ break;
+ default:
+ // Personality routine returned an unknown result code.
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
+ "personality returned %d, "
+ "_URC_FATAL_PHASE2_ERROR",
+ (void *)exception_object, personalityResult);
+ return _URC_FATAL_PHASE2_ERROR;
+ }
+ }
+ }
+
+ // Call stop function one last time and tell it we've reached the end
+ // of the stack.
+ _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
+ "function with _UA_END_OF_STACK",
+ (void *)exception_object);
+ _Unwind_Action lastAction =
+ (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
+ (*stop)(1, lastAction, exception_object->exception_class, exception_object,
+ (struct _Unwind_Context *)(cursor), stop_parameter);
+
+ // Clean up phase did not resume at the frame that the search phase said it
+ // would.
+ return _URC_FATAL_PHASE2_ERROR;
+}
+
/// Called by __cxa_throw. Only returns if there is a fatal error.
_LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception *exception_object) {
unw_cursor_t cursor;
__unw_getcontext(&uc);
- // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
- // which is in the same position as private_1 below.
- // TODO(ajwong): Who wronte the above? Why is it true?
- unwind_phase2(&uc, &cursor, exception_object, true);
+ if (exception_object->unwinder_cache.reserved1)
+ unwind_phase2_forced(
+ &uc, &cursor, exception_object,
+ (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1,
+ (void *)exception_object->unwinder_cache.reserved3);
+ else
+ unwind_phase2(&uc, &cursor, exception_object, true);
// Clients assume _Unwind_Resume() does not return, so all we can do is abort.
_LIBUNWIND_ABORT("_Unwind_Resume() can't return");
_LIBUNWIND_ABORT("unsupported register class");
}
+/// Not used by C++.
+/// Unwinds stack, calling "stop" function at each frame.
+/// Could be used to implement longjmp().
+_LIBUNWIND_EXPORT _Unwind_Reason_Code
+_Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
+ void *stop_parameter) {
+ _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
+ (void *)exception_object, (void *)(uintptr_t)stop);
+ unw_context_t uc;
+ unw_cursor_t cursor;
+ __unw_getcontext(&uc);
+
+ // Mark that this is a forced unwind, so _Unwind_Resume() can do
+ // the right thing.
+ exception_object->unwinder_cache.reserved1 = (uintptr_t)stop;
+ exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter;
+
+ return unwind_phase2_forced(&uc, &cursor, exception_object, stop,
+ stop_parameter);
+}
+
/// Called by personality handler during phase 2 to find the start of the
/// function.
_LIBUNWIND_EXPORT uintptr_t
#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
-#define private_1 private_[0]
+#define PRIVATE_1 private_[0]
+#elif defined(_LIBUNWIND_ARM_EHABI)
+#define PRIVATE_1 unwinder_cache.reserved1
+#else
+#define PRIVATE_1 private_1
#endif
/// Called by __cxa_rethrow().
_LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {
-#if defined(_LIBUNWIND_ARM_EHABI)
- _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld",
- (void *)exception_object,
- (long)exception_object->unwinder_cache.reserved1);
-#else
- _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,
- (void *)exception_object,
- (intptr_t)exception_object->private_1);
-#endif
+ _LIBUNWIND_TRACE_API(
+ "_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,
+ (void *)exception_object, (intptr_t)exception_object->PRIVATE_1);
-#if defined(_LIBUNWIND_ARM_EHABI)
- // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
- // which is in the same position as private_1 below.
- return _Unwind_RaiseException(exception_object);
-#else
// If this is non-forced and a stopping place was found, then this is a
// re-throw.
// Call _Unwind_RaiseException() as if this was a new exception
- if (exception_object->private_1 == 0) {
+ if (exception_object->PRIVATE_1 == 0) {
return _Unwind_RaiseException(exception_object);
// Will return if there is no catch clause, so that __cxa_rethrow can call
// std::terminate().
_Unwind_Resume(exception_object);
_LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"
" which unexpectedly returned");
-#endif
}
-
/// Called by personality handler during phase 2 to get base address for data
/// relative encodings.
_LIBUNWIND_EXPORT uintptr_t
--- /dev/null
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: linux
+
+// Basic test for _Unwind_ForcedUnwind.
+// See libcxxabi/test/forced_unwind* tests too.
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <unwind.h>
+
+void foo();
+_Unwind_Exception ex;
+
+_Unwind_Reason_Code stop(int version, _Unwind_Action actions,
+ uint64_t exceptionClass,
+ _Unwind_Exception *exceptionObject,
+ struct _Unwind_Context *context,
+ void *stop_parameter) {
+ assert(version == 1);
+ assert((actions & _UA_FORCE_UNWIND) != 0);
+ (void)exceptionClass;
+ assert(exceptionObject == &ex);
+ assert(stop_parameter == &foo);
+
+ Dl_info info = {0, 0, 0, 0};
+
+ // Unwind util the main is reached, above frames depend on the platform and
+ // architecture.
+ if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
+ info.dli_sname && !strcmp("main", info.dli_sname)) {
+ _Exit(0);
+ }
+ return _URC_NO_REASON;
+}
+
+__attribute__((noinline)) void foo() {
+
+ // Arm EHABI defines struct _Unwind_Control_Block as exception
+ // object. Ensure struct _Unwind_Exception* work there too,
+ // because _Unwind_Exception in this case is just an alias.
+ struct _Unwind_Exception *e = &ex;
+#if defined(_LIBUNWIND_ARM_EHABI)
+ // Create a mock exception object.
+ memset(e, '\0', sizeof(*e));
+ e->exception_class = 0x434C4E47554E5700; // CLNGUNW\0
+#endif
+ _Unwind_ForcedUnwind(e, stop, (void *)&foo);
+}
+
+int main() {
+ foo();
+ return -2;
+}