| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | // |
| 8 | // Implements unw_* functions from <libunwind.h> |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include <libunwind.h> |
| 13 | |
| 14 | #include "config.h" |
| 15 | #include "libunwind_ext.h" |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #if defined(__FreeBSD__) && defined(__x86_64__) && \ |
| 20 | !defined(_LIBUNWIND_IS_BAREMETAL) |
| 21 | #include <signal.h> |
| 22 | #include <ucontext.h> |
| 23 | #endif |
| 24 | |
| 25 | // Define the __has_feature extension for compilers that do not support it so |
| 26 | // that we can later check for the presence of ASan in a compiler-neutral way. |
| 27 | #if !defined(__has_feature) |
| 28 | #define __has_feature(feature) 0 |
| 29 | #endif |
| 30 | |
| 31 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
| 32 | #include <sanitizer/asan_interface.h> |
| 33 | #endif |
| 34 | |
| 35 | #if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) |
| 36 | #include "AddressSpace.hpp" |
| 37 | #include "UnwindCursor.hpp" |
| 38 | |
| 39 | using namespace libunwind; |
| 40 | |
| 41 | #if defined(__FreeBSD__) && defined(__x86_64__) && \ |
| 42 | !defined(_LIBUNWIND_IS_BAREMETAL) |
| 43 | // Changing FS/GS selectors can clear their bases, while sysarch base setters |
| 44 | // replace the selectors. Let sigreturn restore the complete state atomically. |
| 45 | // use sigreturn() over setcontext() as setcontext() intentionally leaves CS |
| 46 | // unchanged. |
| 47 | extern "C" _LIBUNWIND_HIDDEN void |
| 48 | __libunwind_Registers_x86_64_jumpto(Registers_x86_64 *registers) { |
| 49 | ucontext_t context; |
| 50 | if (getcontext(&context) != 0) |
| 51 | _LIBUNWIND_ABORT("getcontext failed" ); |
| 52 | mcontext_t &mc = context.uc_mcontext; |
| 53 | #define RESTORE(reg, field) mc.field = registers->getRegister(UNW_X86_64_##reg) |
| 54 | RESTORE(RAX, mc_rax); |
| 55 | RESTORE(RBX, mc_rbx); |
| 56 | RESTORE(RCX, mc_rcx); |
| 57 | RESTORE(RDX, mc_rdx); |
| 58 | RESTORE(RDI, mc_rdi); |
| 59 | RESTORE(RSI, mc_rsi); |
| 60 | RESTORE(RBP, mc_rbp); |
| 61 | RESTORE(RSP, mc_rsp); |
| 62 | RESTORE(R8, mc_r8); |
| 63 | RESTORE(R9, mc_r9); |
| 64 | RESTORE(R10, mc_r10); |
| 65 | RESTORE(R11, mc_r11); |
| 66 | RESTORE(R12, mc_r12); |
| 67 | RESTORE(R13, mc_r13); |
| 68 | RESTORE(R14, mc_r14); |
| 69 | RESTORE(R15, mc_r15); |
| 70 | RESTORE(RIP, mc_rip); |
| 71 | RESTORE(RFLAGS, mc_rflags); |
| 72 | RESTORE(CS, mc_cs); |
| 73 | RESTORE(SS, mc_ss); |
| 74 | RESTORE(DS, mc_ds); |
| 75 | RESTORE(ES, mc_es); |
| 76 | RESTORE(FS, mc_fs); |
| 77 | RESTORE(GS, mc_gs); |
| 78 | RESTORE(FS_BASE, mc_fsbase); |
| 79 | RESTORE(GS_BASE, mc_gsbase); |
| 80 | #undef RESTORE |
| 81 | mc.mc_flags |= _MC_HASSEGS | _MC_HASBASES; |
| 82 | sigreturn(&context); |
| 83 | _LIBUNWIND_ABORT("sigreturn failed" ); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | /// internal object to represent this process's address space |
| 88 | LocalAddressSpace LocalAddressSpace::sThisAddressSpace; |
| 89 | |
| 90 | _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space = |
| 91 | (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace; |
| 92 | |
| 93 | /// Create a cursor of a thread in this process given 'context' recorded by |
| 94 | /// __unw_getcontext(). |
| 95 | _LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor, |
| 96 | unw_context_t *context) { |
| 97 | _LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)" , |
| 98 | static_cast<void *>(cursor), |
| 99 | static_cast<void *>(context)); |
| 100 | #if defined(__i386__) |
| 101 | # define REGISTER_KIND Registers_x86 |
| 102 | #elif defined(__x86_64__) |
| 103 | # define REGISTER_KIND Registers_x86_64 |
| 104 | #elif defined(__powerpc64__) |
| 105 | # define REGISTER_KIND Registers_ppc64 |
| 106 | #elif defined(__powerpc__) |
| 107 | # define REGISTER_KIND Registers_ppc |
| 108 | #elif defined(__aarch64__) |
| 109 | # define REGISTER_KIND Registers_arm64 |
| 110 | #elif defined(__arm__) |
| 111 | # define REGISTER_KIND Registers_arm |
| 112 | #elif defined(__or1k__) |
| 113 | # define REGISTER_KIND Registers_or1k |
| 114 | #elif defined(__hexagon__) |
| 115 | # define REGISTER_KIND Registers_hexagon |
| 116 | #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 |
| 117 | # define REGISTER_KIND Registers_mips_o32 |
| 118 | #elif defined(__mips64) |
| 119 | # define REGISTER_KIND Registers_mips_newabi |
| 120 | #elif defined(__mips__) |
| 121 | # warning The MIPS architecture is not supported with this ABI and environment! |
| 122 | #elif defined(__sparc__) && defined(__arch64__) |
| 123 | #define REGISTER_KIND Registers_sparc64 |
| 124 | #elif defined(__sparc__) |
| 125 | # define REGISTER_KIND Registers_sparc |
| 126 | #elif defined(__riscv) |
| 127 | # define REGISTER_KIND Registers_riscv |
| 128 | #elif defined(__ve__) |
| 129 | # define REGISTER_KIND Registers_ve |
| 130 | #elif defined(__s390x__) |
| 131 | # define REGISTER_KIND Registers_s390x |
| 132 | #elif defined(__loongarch__) && __loongarch_grlen == 64 |
| 133 | #define REGISTER_KIND Registers_loongarch |
| 134 | #else |
| 135 | # error Architecture not supported |
| 136 | #endif |
| 137 | // Use "placement new" to allocate UnwindCursor in the cursor buffer. |
| 138 | new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) |
| 139 | UnwindCursor<LocalAddressSpace, REGISTER_KIND>( |
| 140 | context, LocalAddressSpace::sThisAddressSpace); |
| 141 | #undef REGISTER_KIND |
| 142 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 143 | co->setInfoBasedOnIPRegister(); |
| 144 | |
| 145 | return UNW_ESUCCESS; |
| 146 | } |
| 147 | _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) |
| 148 | |
| 149 | /// Get value of specified register at cursor position in stack frame. |
| 150 | _LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 151 | unw_word_t *value) { |
| 152 | _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)" , |
| 153 | static_cast<void *>(cursor), regNum, |
| 154 | static_cast<void *>(value)); |
| 155 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 156 | if (co->validReg(regNum)) { |
| 157 | *value = co->getReg(regNum); |
| 158 | return UNW_ESUCCESS; |
| 159 | } |
| 160 | return UNW_EBADREG; |
| 161 | } |
| 162 | _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) |
| 163 | |
| 164 | /// Set value of specified register at cursor position in stack frame. |
| 165 | _LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 166 | unw_word_t value) { |
| 167 | _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR |
| 168 | ")" , |
| 169 | static_cast<void *>(cursor), regNum, value); |
| 170 | typedef LocalAddressSpace::pint_t pint_t; |
| 171 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 172 | if (co->validReg(regNum)) { |
| 173 | // special case altering IP to re-find info (being called by personality |
| 174 | // function) |
| 175 | if (regNum == UNW_REG_IP) { |
| 176 | unw_proc_info_t info; |
| 177 | // First, get the FDE for the old location and then update it. |
| 178 | co->getInfo(&info); |
| 179 | |
| 180 | pint_t sp = (pint_t)co->getReg(UNW_REG_SP); |
| 181 | |
| 182 | #if defined(_LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING) |
| 183 | { |
| 184 | // It is only valid to set the IP within the current function. This is |
| 185 | // important for ptrauth, otherwise the IP cannot be correctly signed. |
| 186 | // |
| 187 | // However many JITs do not configure CFI frames, so we cannot actually |
| 188 | // enforce this - at least not without an extremely expensive syscall. |
| 189 | // |
| 190 | // For the forseeable future this will need to be a debug only assertion |
| 191 | // so we just strip and assert to avoid the unnecessary auths in release |
| 192 | // builds. |
| 193 | unw_word_t stripped_value = (unw_word_t)ptrauth_strip( |
| 194 | (void *)value, ptrauth_key_return_address); |
| 195 | if (stripped_value < info.start_ip && stripped_value > info.end_ip) |
| 196 | _LIBUNWIND_LOG("Badly behaved use of unw_set_reg: moving IP(0x%zX) " |
| 197 | "outside of CFI bounds function (0x%zX, 0x%zX)" , |
| 198 | stripped_value, info.start_ip, info.end_ip); |
| 199 | |
| 200 | // PC should have been signed with the sp, so we verify that |
| 201 | // roundtripping does not fail. The `ptrauth_auth_and_resign` is |
| 202 | // guaranteed to trap on authentication failure even without FPAC |
| 203 | // feature. |
| 204 | pint_t pc = (pint_t)co->getReg(UNW_REG_IP); |
| 205 | if (ptrauth_auth_and_resign((void *)pc, ptrauth_key_return_address, sp, |
| 206 | ptrauth_key_return_address, |
| 207 | sp) != (void *)pc) { |
| 208 | _LIBUNWIND_LOG( |
| 209 | "Bad unwind with PAuth-enabled ABI (0x%zX, 0x%zX)->0x%zX\n" , pc, |
| 210 | sp, |
| 211 | (pint_t)ptrauth_auth_data((void *)pc, ptrauth_key_return_address, |
| 212 | sp)); |
| 213 | _LIBUNWIND_ABORT("Bad unwind with PAuth-enabled ABI" ); |
| 214 | } |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | // If the original call expects stack adjustment, perform this now. |
| 219 | // Normal frame unwinding would have included the offset already in the |
| 220 | // CFA computation. |
| 221 | // Note: for PA-RISC and other platforms where the stack grows up, |
| 222 | // this should actually be - info.gp. LLVM doesn't currently support |
| 223 | // any such platforms and Clang doesn't export a macro for them. |
| 224 | if (info.gp) |
| 225 | co->setReg(UNW_REG_SP, sp + info.gp); |
| 226 | co->setReg(UNW_REG_IP, value); |
| 227 | co->setInfoBasedOnIPRegister(false); |
| 228 | } else { |
| 229 | co->setReg(regNum, (pint_t)value); |
| 230 | } |
| 231 | return UNW_ESUCCESS; |
| 232 | } |
| 233 | return UNW_EBADREG; |
| 234 | } |
| 235 | _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) |
| 236 | |
| 237 | /// Get value of specified float register at cursor position in stack frame. |
| 238 | _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 239 | unw_fpreg_t *value) { |
| 240 | _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)" , |
| 241 | static_cast<void *>(cursor), regNum, |
| 242 | static_cast<void *>(value)); |
| 243 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 244 | if (co->validFloatReg(regNum)) { |
| 245 | *value = co->getFloatReg(regNum); |
| 246 | return UNW_ESUCCESS; |
| 247 | } |
| 248 | return UNW_EBADREG; |
| 249 | } |
| 250 | _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) |
| 251 | |
| 252 | /// Set value of specified float register at cursor position in stack frame. |
| 253 | _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
| 254 | unw_fpreg_t value) { |
| 255 | #if defined(_LIBUNWIND_ARM_EHABI) |
| 256 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)" , |
| 257 | static_cast<void *>(cursor), regNum, value); |
| 258 | #else |
| 259 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)" , |
| 260 | static_cast<void *>(cursor), regNum, value); |
| 261 | #endif |
| 262 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 263 | if (co->validFloatReg(regNum)) { |
| 264 | co->setFloatReg(regNum, value); |
| 265 | return UNW_ESUCCESS; |
| 266 | } |
| 267 | return UNW_EBADREG; |
| 268 | } |
| 269 | _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) |
| 270 | |
| 271 | /// Move cursor to next frame. |
| 272 | _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { |
| 273 | _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)" , static_cast<void *>(cursor)); |
| 274 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 275 | return co->step(); |
| 276 | } |
| 277 | _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) |
| 278 | |
| 279 | // Move cursor to next frame and for stage2 of unwinding. |
| 280 | // This resets MTE tags of tagged frames to zero. |
| 281 | extern "C" _LIBUNWIND_HIDDEN int __unw_step_stage2(unw_cursor_t *cursor) { |
| 282 | _LIBUNWIND_TRACE_API("__unw_step_stage2(cursor=%p)" , |
| 283 | static_cast<void *>(cursor)); |
| 284 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 285 | return co->step(true); |
| 286 | } |
| 287 | |
| 288 | /// Get unwind info at cursor position in stack frame. |
| 289 | _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, |
| 290 | unw_proc_info_t *info) { |
| 291 | _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)" , |
| 292 | static_cast<void *>(cursor), static_cast<void *>(info)); |
| 293 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 294 | co->getInfo(info); |
| 295 | if (info->end_ip == 0) |
| 296 | return UNW_ENOINFO; |
| 297 | return UNW_ESUCCESS; |
| 298 | } |
| 299 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) |
| 300 | |
| 301 | /// Rebalance the execution flow by injecting the right amount of `ret` |
| 302 | /// instruction relatively to the amount of `walkedFrames` then resume execution |
| 303 | /// at cursor position (aka longjump). |
| 304 | _LIBUNWIND_HIDDEN int __unw_resume_with_frames_walked(unw_cursor_t *cursor, |
| 305 | unsigned walkedFrames) { |
| 306 | _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p, walkedFrames=%u)" , |
| 307 | static_cast<void *>(cursor), walkedFrames); |
| 308 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
| 309 | // Inform the ASan runtime that now might be a good time to clean stuff up. |
| 310 | __asan_handle_no_return(); |
| 311 | #endif |
| 312 | #ifdef _LIBUNWIND_TRACE_RET_INJECT |
| 313 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 314 | co->setWalkedFrames(walkedFrames); |
| 315 | #endif |
| 316 | return __unw_resume(cursor); |
| 317 | } |
| 318 | _LIBUNWIND_WEAK_ALIAS(__unw_resume_with_frames_walked, |
| 319 | unw_resume_with_frames_walked) |
| 320 | |
| 321 | /// Legacy function. Resume execution at cursor position (aka longjump). |
| 322 | _LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) { |
| 323 | _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)" , static_cast<void *>(cursor)); |
| 324 | #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) |
| 325 | // Inform the ASan runtime that now might be a good time to clean stuff up. |
| 326 | __asan_handle_no_return(); |
| 327 | #endif |
| 328 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 329 | co->jumpto(); |
| 330 | return UNW_EUNSPEC; |
| 331 | } |
| 332 | _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) |
| 333 | |
| 334 | /// Get name of function at cursor position in stack frame. |
| 335 | _LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf, |
| 336 | size_t bufLen, unw_word_t *offset) { |
| 337 | _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)" , |
| 338 | static_cast<void *>(cursor), static_cast<void *>(buf), |
| 339 | static_cast<unsigned long>(bufLen)); |
| 340 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 341 | if (co->getFunctionName(buf, bufLen, offset)) |
| 342 | return UNW_ESUCCESS; |
| 343 | return UNW_EUNSPEC; |
| 344 | } |
| 345 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) |
| 346 | |
| 347 | /// Checks if a register is a floating-point register. |
| 348 | _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, |
| 349 | unw_regnum_t regNum) { |
| 350 | _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)" , |
| 351 | static_cast<void *>(cursor), regNum); |
| 352 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 353 | return co->validFloatReg(regNum); |
| 354 | } |
| 355 | _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) |
| 356 | |
| 357 | /// Get name of specified register at cursor position in stack frame. |
| 358 | _LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor, |
| 359 | unw_regnum_t regNum) { |
| 360 | _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)" , |
| 361 | static_cast<void *>(cursor), regNum); |
| 362 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 363 | return co->getRegisterName(regNum); |
| 364 | } |
| 365 | _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) |
| 366 | |
| 367 | /// Checks if current frame is signal trampoline. |
| 368 | _LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) { |
| 369 | _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)" , |
| 370 | static_cast<void *>(cursor)); |
| 371 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 372 | return co->isSignalFrame(); |
| 373 | } |
| 374 | _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) |
| 375 | |
| 376 | #ifdef _AIX |
| 377 | _LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) { |
| 378 | _LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)" , |
| 379 | static_cast<void *>(cursor)); |
| 380 | AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); |
| 381 | return co->getDataRelBase(); |
| 382 | } |
| 383 | _LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base) |
| 384 | #endif |
| 385 | |
| 386 | #ifdef __arm__ |
| 387 | // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD |
| 388 | _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { |
| 389 | _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)" , |
| 390 | static_cast<void *>(cursor)); |
| 391 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 392 | return co->saveVFPAsX(); |
| 393 | } |
| 394 | _LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X) |
| 395 | #endif |
| 396 | |
| 397 | |
| 398 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
| 399 | /// SPI: walks cached DWARF entries |
| 400 | _LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( |
| 401 | unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { |
| 402 | _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)" , |
| 403 | reinterpret_cast<void *>(func)); |
| 404 | DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); |
| 405 | } |
| 406 | _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, |
| 407 | unw_iterate_dwarf_unwind_cache) |
| 408 | |
| 409 | /// IPI: for __register_frame() |
| 410 | void __unw_add_dynamic_fde(unw_word_t fde) { |
| 411 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 412 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 413 | const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 414 | addressSpace&: LocalAddressSpace::sThisAddressSpace, |
| 415 | fdeStart: (LocalAddressSpace::pint_t) fde, fdeInfo: &fdeInfo, cieInfo: &cieInfo); |
| 416 | if (message == NULL) { |
| 417 | // dynamically registered FDEs don't have a mach_header group they are in. |
| 418 | // Use fde as mh_group |
| 419 | unw_word_t mh_group = fdeInfo.fdeStart; |
| 420 | DwarfFDECache<LocalAddressSpace>::add(mh: (LocalAddressSpace::pint_t)mh_group, |
| 421 | ip_start: fdeInfo.pcStart, ip_end: fdeInfo.pcEnd, |
| 422 | fde: fdeInfo.fdeStart); |
| 423 | } else { |
| 424 | _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s" , message); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /// IPI: for __deregister_frame() |
| 429 | void __unw_remove_dynamic_fde(unw_word_t fde) { |
| 430 | // fde is own mh_group |
| 431 | DwarfFDECache<LocalAddressSpace>::removeAllIn(mh: (LocalAddressSpace::pint_t)fde); |
| 432 | } |
| 433 | |
| 434 | void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 435 | // The eh_frame section start serves as the mh_group |
| 436 | unw_word_t mh_group = eh_frame_start; |
| 437 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
| 438 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
| 439 | auto p = (LocalAddressSpace::pint_t)eh_frame_start; |
| 440 | while (LocalAddressSpace::sThisAddressSpace.get32(addr: p)) { |
| 441 | if (CFI_Parser<LocalAddressSpace>::decodeFDE( |
| 442 | addressSpace&: LocalAddressSpace::sThisAddressSpace, fdeStart: p, fdeInfo: &fdeInfo, cieInfo: &cieInfo, |
| 443 | useCIEInfo: true) == NULL) { |
| 444 | DwarfFDECache<LocalAddressSpace>::add(mh: (LocalAddressSpace::pint_t)mh_group, |
| 445 | ip_start: fdeInfo.pcStart, ip_end: fdeInfo.pcEnd, |
| 446 | fde: fdeInfo.fdeStart); |
| 447 | p += fdeInfo.fdeLength; |
| 448 | } else if (CFI_Parser<LocalAddressSpace>::parseCIE( |
| 449 | addressSpace&: LocalAddressSpace::sThisAddressSpace, cie: p, cieInfo: &cieInfo) == NULL) { |
| 450 | p += cieInfo.cieLength; |
| 451 | } else |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) { |
| 457 | // The eh_frame section start serves as the mh_group |
| 458 | DwarfFDECache<LocalAddressSpace>::removeAllIn( |
| 459 | mh: (LocalAddressSpace::pint_t)eh_frame_start); |
| 460 | } |
| 461 | |
| 462 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
| 463 | |
| 464 | /// Maps the UNW_* error code to a textual representation |
| 465 | _LIBUNWIND_HIDDEN const char *__unw_strerror(int error_code) { |
| 466 | switch (error_code) { |
| 467 | case UNW_ESUCCESS: |
| 468 | return "no error" ; |
| 469 | case UNW_EUNSPEC: |
| 470 | return "unspecified (general) error" ; |
| 471 | case UNW_ENOMEM: |
| 472 | return "out of memory" ; |
| 473 | case UNW_EBADREG: |
| 474 | return "bad register number" ; |
| 475 | case UNW_EREADONLYREG: |
| 476 | return "attempt to write read-only register" ; |
| 477 | case UNW_ESTOPUNWIND: |
| 478 | return "stop unwinding" ; |
| 479 | case UNW_EINVALIDIP: |
| 480 | return "invalid IP" ; |
| 481 | case UNW_EBADFRAME: |
| 482 | return "bad frame" ; |
| 483 | case UNW_EINVAL: |
| 484 | return "unsupported operation or bad value" ; |
| 485 | case UNW_EBADVERSION: |
| 486 | return "unwind info has unsupported version" ; |
| 487 | case UNW_ENOINFO: |
| 488 | return "no unwind info found" ; |
| 489 | #if defined(_LIBUNWIND_TARGET_AARCH64) && !defined(_LIBUNWIND_IS_NATIVE_ONLY) |
| 490 | case UNW_ECROSSRASIGNING: |
| 491 | return "cross unwind with return address signing" ; |
| 492 | #endif |
| 493 | } |
| 494 | return "invalid error code" ; |
| 495 | } |
| 496 | _LIBUNWIND_WEAK_ALIAS(__unw_strerror, unw_strerror) |
| 497 | |
| 498 | #endif // !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) |
| 499 | |
| 500 | #ifdef __APPLE__ |
| 501 | |
| 502 | namespace libunwind { |
| 503 | |
| 504 | static constexpr size_t MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS = 8; |
| 505 | |
| 506 | static RWMutex findDynamicUnwindSectionsLock; |
| 507 | static size_t numDynamicUnwindSectionsFinders = 0; |
| 508 | static unw_find_dynamic_unwind_sections |
| 509 | dynamicUnwindSectionsFinders[MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS] = {0}; |
| 510 | |
| 511 | bool findDynamicUnwindSections(void *addr, unw_dynamic_unwind_sections *info) { |
| 512 | bool found = false; |
| 513 | findDynamicUnwindSectionsLock.lock_shared(); |
| 514 | for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { |
| 515 | if (dynamicUnwindSectionsFinders[i]((unw_word_t)addr, info)) { |
| 516 | found = true; |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | findDynamicUnwindSectionsLock.unlock_shared(); |
| 521 | return found; |
| 522 | } |
| 523 | |
| 524 | } // namespace libunwind |
| 525 | |
| 526 | int __unw_add_find_dynamic_unwind_sections( |
| 527 | unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) { |
| 528 | findDynamicUnwindSectionsLock.lock(); |
| 529 | |
| 530 | // Check that we have enough space... |
| 531 | if (numDynamicUnwindSectionsFinders == MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS) { |
| 532 | findDynamicUnwindSectionsLock.unlock(); |
| 533 | return UNW_ENOMEM; |
| 534 | } |
| 535 | |
| 536 | // Check for value already present... |
| 537 | for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { |
| 538 | if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) { |
| 539 | findDynamicUnwindSectionsLock.unlock(); |
| 540 | return UNW_EINVAL; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // Success -- add callback entry. |
| 545 | dynamicUnwindSectionsFinders[numDynamicUnwindSectionsFinders++] = |
| 546 | find_dynamic_unwind_sections; |
| 547 | findDynamicUnwindSectionsLock.unlock(); |
| 548 | |
| 549 | return UNW_ESUCCESS; |
| 550 | } |
| 551 | |
| 552 | int __unw_remove_find_dynamic_unwind_sections( |
| 553 | unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) { |
| 554 | findDynamicUnwindSectionsLock.lock(); |
| 555 | |
| 556 | // Find index to remove. |
| 557 | size_t finderIdx = numDynamicUnwindSectionsFinders; |
| 558 | for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { |
| 559 | if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) { |
| 560 | finderIdx = i; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // If no such registration is present then error out. |
| 566 | if (finderIdx == numDynamicUnwindSectionsFinders) { |
| 567 | findDynamicUnwindSectionsLock.unlock(); |
| 568 | return UNW_EINVAL; |
| 569 | } |
| 570 | |
| 571 | // Remove entry. |
| 572 | for (size_t i = finderIdx; i != numDynamicUnwindSectionsFinders - 1; ++i) |
| 573 | dynamicUnwindSectionsFinders[i] = dynamicUnwindSectionsFinders[i + 1]; |
| 574 | dynamicUnwindSectionsFinders[--numDynamicUnwindSectionsFinders] = nullptr; |
| 575 | |
| 576 | findDynamicUnwindSectionsLock.unlock(); |
| 577 | return UNW_ESUCCESS; |
| 578 | } |
| 579 | |
| 580 | #endif // __APPLE__ |
| 581 | |
| 582 | // Add logging hooks in Debug builds only |
| 583 | #ifndef NDEBUG |
| 584 | #include <stdlib.h> |
| 585 | |
| 586 | _LIBUNWIND_HIDDEN |
| 587 | bool logAPIs() { |
| 588 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 589 | static bool checked = false; |
| 590 | static bool log = false; |
| 591 | if (!checked) { |
| 592 | log = (getenv(name: "LIBUNWIND_PRINT_APIS" ) != NULL); |
| 593 | checked = true; |
| 594 | } |
| 595 | return log; |
| 596 | } |
| 597 | |
| 598 | _LIBUNWIND_HIDDEN |
| 599 | bool logUnwinding() { |
| 600 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 601 | static bool checked = false; |
| 602 | static bool log = false; |
| 603 | if (!checked) { |
| 604 | log = (getenv(name: "LIBUNWIND_PRINT_UNWINDING" ) != NULL); |
| 605 | checked = true; |
| 606 | } |
| 607 | return log; |
| 608 | } |
| 609 | |
| 610 | _LIBUNWIND_HIDDEN |
| 611 | bool logDWARF() { |
| 612 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
| 613 | static bool checked = false; |
| 614 | static bool log = false; |
| 615 | if (!checked) { |
| 616 | log = (getenv(name: "LIBUNWIND_PRINT_DWARF" ) != NULL); |
| 617 | checked = true; |
| 618 | } |
| 619 | return log; |
| 620 | } |
| 621 | |
| 622 | #endif // NDEBUG |
| 623 | |
| 624 | |