#include <libunwind.h>
#include "config.h"
#include "libunwind_ext.h"
#include <stdlib.h>
#if !defined(__has_feature)
#define __has_feature(feature) 0
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#include <sanitizer/asan_interface.h>
#endif
#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
#include "AddressSpace.hpp"
#include "UnwindCursor.hpp"
using namespace libunwind;
LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
(unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
_LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor,
unw_context_t *context) {
_LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)",
static_cast<void *>(cursor),
static_cast<void *>(context));
#if defined(__i386__)
# define REGISTER_KIND Registers_x86
#elif defined(__x86_64__)
# define REGISTER_KIND Registers_x86_64
#elif defined(__powerpc64__)
# define REGISTER_KIND Registers_ppc64
#elif defined(__powerpc__)
# define REGISTER_KIND Registers_ppc
#elif defined(__aarch64__)
# define REGISTER_KIND Registers_arm64
#elif defined(__arm__)
# define REGISTER_KIND Registers_arm
#elif defined(__or1k__)
# define REGISTER_KIND Registers_or1k
#elif defined(__hexagon__)
# define REGISTER_KIND Registers_hexagon
#elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
# define REGISTER_KIND Registers_mips_o32
#elif defined(__mips64)
# define REGISTER_KIND Registers_mips_newabi
#elif defined(__mips__)
# warning The MIPS architecture is not supported with this ABI and environment!
#elif defined(__sparc__) && defined(__arch64__)
#define REGISTER_KIND Registers_sparc64
#elif defined(__sparc__)
# define REGISTER_KIND Registers_sparc
#elif defined(__riscv)
# define REGISTER_KIND Registers_riscv
#elif defined(__ve__)
# define REGISTER_KIND Registers_ve
#elif defined(__s390x__)
# define REGISTER_KIND Registers_s390x
#elif defined(__loongarch__) && __loongarch_grlen == 64
#define REGISTER_KIND Registers_loongarch
#else
# error Architecture not supported
#endif
new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor))
UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
context, LocalAddressSpace::sThisAddressSpace);
#undef REGISTER_KIND
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
co->setInfoBasedOnIPRegister();
return UNW_ESUCCESS;
}
_LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local)
_LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
unw_word_t *value) {
_LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
static_cast<void *>(cursor), regNum,
static_cast<void *>(value));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
if (co->validReg(regNum)) {
*value = co->getReg(regNum);
return UNW_ESUCCESS;
}
return UNW_EBADREG;
}
_LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg)
_LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
unw_word_t value) {
_LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR
")",
static_cast<void *>(cursor), regNum, value);
typedef LocalAddressSpace::pint_t pint_t;
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
if (co->validReg(regNum)) {
if (regNum == UNW_REG_IP) {
unw_proc_info_t info;
co->getInfo(&info);
pint_t sp = (pint_t)co->getReg(UNW_REG_SP);
#if defined(_LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING)
{
unw_word_t
__unwind_ptrauth_restricted_intptr(ptrauth_key_return_address, 1,
0) authenticated_value;
unw_word_t opaque_value = (uint64_t)ptrauth_auth_and_resign(
(void *)value, ptrauth_key_return_address, sp,
ptrauth_key_return_address, &authenticated_value);
memmove(reinterpret_cast<void *>(&authenticated_value),
reinterpret_cast<void *>(&opaque_value),
sizeof(authenticated_value));
if (authenticated_value < info.start_ip ||
authenticated_value > info.end_ip)
_LIBUNWIND_ABORT("PC vs frame info mismatch");
pint_t pc = (pint_t)co->getReg(UNW_REG_IP);
if (ptrauth_auth_and_resign((void *)pc, ptrauth_key_return_address, sp,
ptrauth_key_return_address,
sp) != (void *)pc) {
_LIBUNWIND_LOG("Bad unwind through arm64e (0x%zX, 0x%zX)->0x%zX\n",
pc, sp,
(pint_t)ptrauth_auth_data(
(void *)pc, ptrauth_key_return_address, sp));
_LIBUNWIND_ABORT("Bad unwind through arm64e");
}
}
#endif
if (info.gp)
co->setReg(UNW_REG_SP, sp + info.gp);
co->setReg(UNW_REG_IP, value);
co->setInfoBasedOnIPRegister(false);
} else {
co->setReg(regNum, (pint_t)value);
}
return UNW_ESUCCESS;
}
return UNW_EBADREG;
}
_LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg)
_LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
unw_fpreg_t *value) {
_LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
static_cast<void *>(cursor), regNum,
static_cast<void *>(value));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
if (co->validFloatReg(regNum)) {
*value = co->getFloatReg(regNum);
return UNW_ESUCCESS;
}
return UNW_EBADREG;
}
_LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg)
_LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
unw_fpreg_t value) {
#if defined(_LIBUNWIND_ARM_EHABI)
_LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
static_cast<void *>(cursor), regNum, value);
#else
_LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
static_cast<void *>(cursor), regNum, value);
#endif
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
if (co->validFloatReg(regNum)) {
co->setFloatReg(regNum, value);
return UNW_ESUCCESS;
}
return UNW_EBADREG;
}
_LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg)
_LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->step();
}
_LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step)
extern "C" _LIBUNWIND_HIDDEN int __unw_step_stage2(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("__unw_step_stage2(cursor=%p)",
static_cast<void *>(cursor));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->step(true);
}
_LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor,
unw_proc_info_t *info) {
_LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)",
static_cast<void *>(cursor), static_cast<void *>(info));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
co->getInfo(info);
if (info->end_ip == 0)
return UNW_ENOINFO;
return UNW_ESUCCESS;
}
_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info)
_LIBUNWIND_HIDDEN int __unw_resume_with_frames_walked(unw_cursor_t *cursor,
unsigned walkedFrames) {
_LIBUNWIND_TRACE_API("__unw_resume(cursor=%p, walkedFrames=%u)",
static_cast<void *>(cursor), walkedFrames);
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
__asan_handle_no_return();
#endif
#ifdef _LIBUNWIND_TRACE_RET_INJECT
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
co->setWalkedFrames(walkedFrames);
#endif
return __unw_resume(cursor);
}
_LIBUNWIND_WEAK_ALIAS(__unw_resume_with_frames_walked,
unw_resume_with_frames_walked)
_LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor));
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
__asan_handle_no_return();
#endif
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
co->jumpto();
return UNW_EUNSPEC;
}
_LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume)
_LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf,
size_t bufLen, unw_word_t *offset) {
_LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
static_cast<void *>(cursor), static_cast<void *>(buf),
static_cast<unsigned long>(bufLen));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
if (co->getFunctionName(buf, bufLen, offset))
return UNW_ESUCCESS;
return UNW_EUNSPEC;
}
_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name)
_LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor,
unw_regnum_t regNum) {
_LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)",
static_cast<void *>(cursor), regNum);
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->validFloatReg(regNum);
}
_LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg)
_LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor,
unw_regnum_t regNum) {
_LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)",
static_cast<void *>(cursor), regNum);
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->getRegisterName(regNum);
}
_LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname)
_LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)",
static_cast<void *>(cursor));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->isSignalFrame();
}
_LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame)
#ifdef _AIX
_LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)",
static_cast<void *>(cursor));
AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
return co->getDataRelBase();
}
_LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base)
#endif
#ifdef __arm__
_LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) {
_LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)",
static_cast<void *>(cursor));
AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
return co->saveVFPAsX();
}
_LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X)
#endif
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
_LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)(
unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
_LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)",
reinterpret_cast<void *>(func));
DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
}
_LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache,
unw_iterate_dwarf_unwind_cache)
void __unw_add_dynamic_fde(unw_word_t fde) {
CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
LocalAddressSpace::sThisAddressSpace,
(LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
if (message == NULL) {
unw_word_t mh_group = fdeInfo.fdeStart;
DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
fdeInfo.pcStart, fdeInfo.pcEnd,
fdeInfo.fdeStart);
} else {
_LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message);
}
}
void __unw_remove_dynamic_fde(unw_word_t fde) {
DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
}
void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
unw_word_t mh_group = eh_frame_start;
CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
auto p = (LocalAddressSpace::pint_t)eh_frame_start;
while (LocalAddressSpace::sThisAddressSpace.get32(p)) {
if (CFI_Parser<LocalAddressSpace>::decodeFDE(
LocalAddressSpace::sThisAddressSpace, p, &fdeInfo, &cieInfo,
true) == NULL) {
DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
fdeInfo.pcStart, fdeInfo.pcEnd,
fdeInfo.fdeStart);
p += fdeInfo.fdeLength;
} else if (CFI_Parser<LocalAddressSpace>::parseCIE(
LocalAddressSpace::sThisAddressSpace, p, &cieInfo) == NULL) {
p += cieInfo.cieLength;
} else
return;
}
}
void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
DwarfFDECache<LocalAddressSpace>::removeAllIn(
(LocalAddressSpace::pint_t)eh_frame_start);
}
#endif
_LIBUNWIND_HIDDEN const char *__unw_strerror(int error_code) {
switch (error_code) {
case UNW_ESUCCESS:
return "no error";
case UNW_EUNSPEC:
return "unspecified (general) error";
case UNW_ENOMEM:
return "out of memory";
case UNW_EBADREG:
return "bad register number";
case UNW_EREADONLYREG:
return "attempt to write read-only register";
case UNW_ESTOPUNWIND:
return "stop unwinding";
case UNW_EINVALIDIP:
return "invalid IP";
case UNW_EBADFRAME:
return "bad frame";
case UNW_EINVAL:
return "unsupported operation or bad value";
case UNW_EBADVERSION:
return "unwind info has unsupported version";
case UNW_ENOINFO:
return "no unwind info found";
#if defined(_LIBUNWIND_TARGET_AARCH64) && !defined(_LIBUNWIND_IS_NATIVE_ONLY)
case UNW_ECROSSRASIGNING:
return "cross unwind with return address signing";
#endif
}
return "invalid error code";
}
_LIBUNWIND_WEAK_ALIAS(__unw_strerror, unw_strerror)
#endif
#ifdef __APPLE__
namespace libunwind {
static constexpr size_t MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS = 8;
static RWMutex findDynamicUnwindSectionsLock;
static size_t numDynamicUnwindSectionsFinders = 0;
static unw_find_dynamic_unwind_sections
dynamicUnwindSectionsFinders[MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS] = {0};
bool findDynamicUnwindSections(void *addr, unw_dynamic_unwind_sections *info) {
bool found = false;
findDynamicUnwindSectionsLock.lock_shared();
for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
if (dynamicUnwindSectionsFinders[i]((unw_word_t)addr, info)) {
found = true;
break;
}
}
findDynamicUnwindSectionsLock.unlock_shared();
return found;
}
}
int __unw_add_find_dynamic_unwind_sections(
unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) {
findDynamicUnwindSectionsLock.lock();
if (numDynamicUnwindSectionsFinders == MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS) {
findDynamicUnwindSectionsLock.unlock();
return UNW_ENOMEM;
}
for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) {
findDynamicUnwindSectionsLock.unlock();
return UNW_EINVAL;
}
}
dynamicUnwindSectionsFinders[numDynamicUnwindSectionsFinders++] =
find_dynamic_unwind_sections;
findDynamicUnwindSectionsLock.unlock();
return UNW_ESUCCESS;
}
int __unw_remove_find_dynamic_unwind_sections(
unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) {
findDynamicUnwindSectionsLock.lock();
size_t finderIdx = numDynamicUnwindSectionsFinders;
for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) {
finderIdx = i;
break;
}
}
if (finderIdx == numDynamicUnwindSectionsFinders) {
findDynamicUnwindSectionsLock.unlock();
return UNW_EINVAL;
}
for (size_t i = finderIdx; i != numDynamicUnwindSectionsFinders - 1; ++i)
dynamicUnwindSectionsFinders[i] = dynamicUnwindSectionsFinders[i + 1];
dynamicUnwindSectionsFinders[--numDynamicUnwindSectionsFinders] = nullptr;
findDynamicUnwindSectionsLock.unlock();
return UNW_ESUCCESS;
}
#endif
#ifndef NDEBUG
#include <stdlib.h>
_LIBUNWIND_HIDDEN
bool logAPIs() {
static bool checked = false;
static bool log = false;
if (!checked) {
log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
checked = true;
}
return log;
}
_LIBUNWIND_HIDDEN
bool logUnwinding() {
static bool checked = false;
static bool log = false;
if (!checked) {
log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
checked = true;
}
return log;
}
_LIBUNWIND_HIDDEN
bool logDWARF() {
static bool checked = false;
static bool log = false;
if (!checked) {
log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
checked = true;
}
return log;
}
#endif