1126370Sphk//===----------------------------------------------------------------------===// 2126370Sphk// 3126370Sphk// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4126370Sphk// See https://llvm.org/LICENSE.txt for license information. 5126370Sphk// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6126370Sphk// 7126370Sphk// 8126370Sphk// Implements unw_* functions from <libunwind.h> 9126370Sphk// 10126370Sphk//===----------------------------------------------------------------------===// 11126370Sphk 12126370Sphk#include <libunwind.h> 13126370Sphk 14126370Sphk#include "config.h" 15126370Sphk#include "libunwind_ext.h" 16126370Sphk 17126370Sphk#include <stdlib.h> 18126370Sphk 19126370Sphk// Define the __has_feature extension for compilers that do not support it so 20126370Sphk// that we can later check for the presence of ASan in a compiler-neutral way. 21126370Sphk#if !defined(__has_feature) 22126370Sphk#define __has_feature(feature) 0 23126370Sphk#endif 24126370Sphk 25126370Sphk#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 26126370Sphk#include <sanitizer/asan_interface.h> 27126370Sphk#endif 28126370Sphk 29126370Sphk#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__) 30126370Sphk#include "AddressSpace.hpp" 31126370Sphk#include "UnwindCursor.hpp" 32126370Sphk 33126370Sphkusing namespace libunwind; 34126370Sphk 35126370Sphk/// internal object to represent this processes address space 36126370SphkLocalAddressSpace LocalAddressSpace::sThisAddressSpace; 37126370Sphk 38126370Sphk_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space = 39126370Sphk (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace; 40126370Sphk 41126370Sphk/// Create a cursor of a thread in this process given 'context' recorded by 42130585Sphk/// __unw_getcontext(). 43221121Sattilio_LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor, 44126370Sphk unw_context_t *context) { 45126370Sphk _LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)", 46221121Sattilio static_cast<void *>(cursor), 47221121Sattilio static_cast<void *>(context)); 48221121Sattilio#if defined(__i386__) 49221121Sattilio# define REGISTER_KIND Registers_x86 50221121Sattilio#elif defined(__x86_64__) 51221121Sattilio# define REGISTER_KIND Registers_x86_64 52221121Sattilio#elif defined(__powerpc64__) 53221121Sattilio# define REGISTER_KIND Registers_ppc64 54221121Sattilio#elif defined(__powerpc__) 55221121Sattilio# define REGISTER_KIND Registers_ppc 56221121Sattilio#elif defined(__aarch64__) 57221121Sattilio# define REGISTER_KIND Registers_arm64 58221121Sattilio#elif defined(__arm__) 59221121Sattilio# define REGISTER_KIND Registers_arm 60221121Sattilio#elif defined(__or1k__) 61221121Sattilio# define REGISTER_KIND Registers_or1k 62221121Sattilio#elif defined(__hexagon__) 63221121Sattilio# define REGISTER_KIND Registers_hexagon 64221121Sattilio#elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 65221121Sattilio# define REGISTER_KIND Registers_mips_o32 66221121Sattilio#elif defined(__mips64) 67221121Sattilio# define REGISTER_KIND Registers_mips_newabi 68221121Sattilio#elif defined(__mips__) 69221121Sattilio# warning The MIPS architecture is not supported with this ABI and environment! 70221121Sattilio#elif defined(__sparc__) && defined(__arch64__) 71221121Sattilio#define REGISTER_KIND Registers_sparc64 72221121Sattilio#elif defined(__sparc__) 73130585Sphk# define REGISTER_KIND Registers_sparc 74126370Sphk#elif defined(__riscv) 75126370Sphk# define REGISTER_KIND Registers_riscv 76126370Sphk#elif defined(__ve__) 77126370Sphk# define REGISTER_KIND Registers_ve 78126370Sphk#elif defined(__s390x__) 79126370Sphk# define REGISTER_KIND Registers_s390x 80126370Sphk#elif defined(__loongarch__) && __loongarch_grlen == 64 81221121Sattilio#define REGISTER_KIND Registers_loongarch 82126370Sphk#else 83126370Sphk# error Architecture not supported 84126370Sphk#endif 85221121Sattilio // Use "placement new" to allocate UnwindCursor in the cursor buffer. 86221121Sattilio new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) 87167950Sn_hibma UnwindCursor<LocalAddressSpace, REGISTER_KIND>( 88165260Sn_hibma context, LocalAddressSpace::sThisAddressSpace); 89165260Sn_hibma#undef REGISTER_KIND 90221121Sattilio AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 91221121Sattilio co->setInfoBasedOnIPRegister(); 92221121Sattilio 93126370Sphk return UNW_ESUCCESS; 94126370Sphk} 95221121Sattilio_LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) 96221121Sattilio 97221121Sattilio/// Get value of specified register at cursor position in stack frame. 98221121Sattilio_LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, 99221121Sattilio unw_word_t *value) { 100221121Sattilio _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)", 101221121Sattilio static_cast<void *>(cursor), regNum, 102221121Sattilio static_cast<void *>(value)); 103221121Sattilio AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 104221121Sattilio if (co->validReg(regNum)) { 105221121Sattilio *value = co->getReg(regNum); 106221121Sattilio return UNW_ESUCCESS; 107221121Sattilio } 108221121Sattilio return UNW_EBADREG; 109221121Sattilio} 110221121Sattilio_LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) 111221121Sattilio 112126370Sphk/// Set value of specified register at cursor position in stack frame. 113126370Sphk_LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, 114126370Sphk unw_word_t value) { 115126370Sphk _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR 116126370Sphk ")", 117126370Sphk static_cast<void *>(cursor), regNum, value); 118126370Sphk typedef LocalAddressSpace::pint_t pint_t; 119126370Sphk AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 120126370Sphk if (co->validReg(regNum)) { 121126370Sphk co->setReg(regNum, (pint_t)value); 122126370Sphk // special case altering IP to re-find info (being called by personality 123126370Sphk // function) 124126370Sphk if (regNum == UNW_REG_IP) { 125126370Sphk unw_proc_info_t info; 126126370Sphk // First, get the FDE for the old location and then update it. 127126370Sphk co->getInfo(&info); 128126370Sphk co->setInfoBasedOnIPRegister(false); 129126370Sphk // If the original call expects stack adjustment, perform this now. 130126370Sphk // Normal frame unwinding would have included the offset already in the 131126370Sphk // CFA computation. 132126370Sphk // Note: for PA-RISC and other platforms where the stack grows up, 133126370Sphk // this should actually be - info.gp. LLVM doesn't currently support 134126370Sphk // any such platforms and Clang doesn't export a macro for them. 135126370Sphk if (info.gp) 136126370Sphk co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); 137 } 138 return UNW_ESUCCESS; 139 } 140 return UNW_EBADREG; 141} 142_LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) 143 144/// Get value of specified float register at cursor position in stack frame. 145_LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 146 unw_fpreg_t *value) { 147 _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)", 148 static_cast<void *>(cursor), regNum, 149 static_cast<void *>(value)); 150 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 151 if (co->validFloatReg(regNum)) { 152 *value = co->getFloatReg(regNum); 153 return UNW_ESUCCESS; 154 } 155 return UNW_EBADREG; 156} 157_LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) 158 159/// Set value of specified float register at cursor position in stack frame. 160_LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 161 unw_fpreg_t value) { 162#if defined(_LIBUNWIND_ARM_EHABI) 163 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)", 164 static_cast<void *>(cursor), regNum, value); 165#else 166 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", 167 static_cast<void *>(cursor), regNum, value); 168#endif 169 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 170 if (co->validFloatReg(regNum)) { 171 co->setFloatReg(regNum, value); 172 return UNW_ESUCCESS; 173 } 174 return UNW_EBADREG; 175} 176_LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) 177 178/// Move cursor to next frame. 179_LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { 180 _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor)); 181 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 182 return co->step(); 183} 184_LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) 185 186// Move cursor to next frame and for stage2 of unwinding. 187// This resets MTE tags of tagged frames to zero. 188extern "C" _LIBUNWIND_HIDDEN int __unw_step_stage2(unw_cursor_t *cursor) { 189 _LIBUNWIND_TRACE_API("__unw_step_stage2(cursor=%p)", 190 static_cast<void *>(cursor)); 191 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 192 return co->step(true); 193} 194 195/// Get unwind info at cursor position in stack frame. 196_LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, 197 unw_proc_info_t *info) { 198 _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)", 199 static_cast<void *>(cursor), static_cast<void *>(info)); 200 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 201 co->getInfo(info); 202 if (info->end_ip == 0) 203 return UNW_ENOINFO; 204 return UNW_ESUCCESS; 205} 206_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) 207 208/// Resume execution at cursor position (aka longjump). 209_LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) { 210 _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor)); 211#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 212 // Inform the ASan runtime that now might be a good time to clean stuff up. 213 __asan_handle_no_return(); 214#endif 215 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 216 co->jumpto(); 217 return UNW_EUNSPEC; 218} 219_LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) 220 221/// Get name of function at cursor position in stack frame. 222_LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf, 223 size_t bufLen, unw_word_t *offset) { 224 _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)", 225 static_cast<void *>(cursor), static_cast<void *>(buf), 226 static_cast<unsigned long>(bufLen)); 227 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 228 if (co->getFunctionName(buf, bufLen, offset)) 229 return UNW_ESUCCESS; 230 return UNW_EUNSPEC; 231} 232_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) 233 234/// Checks if a register is a floating-point register. 235_LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, 236 unw_regnum_t regNum) { 237 _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)", 238 static_cast<void *>(cursor), regNum); 239 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 240 return co->validFloatReg(regNum); 241} 242_LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) 243 244/// Checks if a register is a floating-point register. 245_LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor, 246 unw_regnum_t regNum) { 247 _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)", 248 static_cast<void *>(cursor), regNum); 249 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 250 return co->getRegisterName(regNum); 251} 252_LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) 253 254/// Checks if current frame is signal trampoline. 255_LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) { 256 _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)", 257 static_cast<void *>(cursor)); 258 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 259 return co->isSignalFrame(); 260} 261_LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) 262 263#ifdef _AIX 264_LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) { 265 _LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)", 266 static_cast<void *>(cursor)); 267 AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor); 268 return co->getDataRelBase(); 269} 270_LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base) 271#endif 272 273#ifdef __arm__ 274// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD 275_LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { 276 _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)", 277 static_cast<void *>(cursor)); 278 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 279 return co->saveVFPAsX(); 280} 281_LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X) 282#endif 283 284 285#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 286/// SPI: walks cached DWARF entries 287_LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( 288 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 289 _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)", 290 reinterpret_cast<void *>(func)); 291 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); 292} 293_LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, 294 unw_iterate_dwarf_unwind_cache) 295 296/// IPI: for __register_frame() 297void __unw_add_dynamic_fde(unw_word_t fde) { 298 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 299 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 300 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( 301 LocalAddressSpace::sThisAddressSpace, 302 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); 303 if (message == NULL) { 304 // dynamically registered FDEs don't have a mach_header group they are in. 305 // Use fde as mh_group 306 unw_word_t mh_group = fdeInfo.fdeStart; 307 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, 308 fdeInfo.pcStart, fdeInfo.pcEnd, 309 fdeInfo.fdeStart); 310 } else { 311 _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message); 312 } 313} 314 315/// IPI: for __deregister_frame() 316void __unw_remove_dynamic_fde(unw_word_t fde) { 317 // fde is own mh_group 318 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); 319} 320 321void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) { 322 // The eh_frame section start serves as the mh_group 323 unw_word_t mh_group = eh_frame_start; 324 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 325 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 326 auto p = (LocalAddressSpace::pint_t)eh_frame_start; 327 while (LocalAddressSpace::sThisAddressSpace.get32(p)) { 328 if (CFI_Parser<LocalAddressSpace>::decodeFDE( 329 LocalAddressSpace::sThisAddressSpace, p, &fdeInfo, &cieInfo, 330 true) == NULL) { 331 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, 332 fdeInfo.pcStart, fdeInfo.pcEnd, 333 fdeInfo.fdeStart); 334 p += fdeInfo.fdeLength; 335 } else if (CFI_Parser<LocalAddressSpace>::parseCIE( 336 LocalAddressSpace::sThisAddressSpace, p, &cieInfo) == NULL) { 337 p += cieInfo.cieLength; 338 } else 339 return; 340 } 341} 342 343void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) { 344 // The eh_frame section start serves as the mh_group 345 DwarfFDECache<LocalAddressSpace>::removeAllIn( 346 (LocalAddressSpace::pint_t)eh_frame_start); 347} 348 349#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 350#endif // !defined(__USING_SJLJ_EXCEPTIONS__) && 351 // !defined(__USING_WASM_EXCEPTIONS__) 352 353#ifdef __APPLE__ 354 355namespace libunwind { 356 357static constexpr size_t MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS = 8; 358 359static RWMutex findDynamicUnwindSectionsLock; 360static size_t numDynamicUnwindSectionsFinders = 0; 361static unw_find_dynamic_unwind_sections 362 dynamicUnwindSectionsFinders[MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS] = {0}; 363 364bool findDynamicUnwindSections(void *addr, unw_dynamic_unwind_sections *info) { 365 bool found = false; 366 findDynamicUnwindSectionsLock.lock_shared(); 367 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { 368 if (dynamicUnwindSectionsFinders[i]((unw_word_t)addr, info)) { 369 found = true; 370 break; 371 } 372 } 373 findDynamicUnwindSectionsLock.unlock_shared(); 374 return found; 375} 376 377} // namespace libunwind 378 379int __unw_add_find_dynamic_unwind_sections( 380 unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) { 381 findDynamicUnwindSectionsLock.lock(); 382 383 // Check that we have enough space... 384 if (numDynamicUnwindSectionsFinders == MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS) { 385 findDynamicUnwindSectionsLock.unlock(); 386 return UNW_ENOMEM; 387 } 388 389 // Check for value already present... 390 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { 391 if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) { 392 findDynamicUnwindSectionsLock.unlock(); 393 return UNW_EINVAL; 394 } 395 } 396 397 // Success -- add callback entry. 398 dynamicUnwindSectionsFinders[numDynamicUnwindSectionsFinders++] = 399 find_dynamic_unwind_sections; 400 findDynamicUnwindSectionsLock.unlock(); 401 402 return UNW_ESUCCESS; 403} 404 405int __unw_remove_find_dynamic_unwind_sections( 406 unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) { 407 findDynamicUnwindSectionsLock.lock(); 408 409 // Find index to remove. 410 size_t finderIdx = numDynamicUnwindSectionsFinders; 411 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) { 412 if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) { 413 finderIdx = i; 414 break; 415 } 416 } 417 418 // If no such registration is present then error out. 419 if (finderIdx == numDynamicUnwindSectionsFinders) { 420 findDynamicUnwindSectionsLock.unlock(); 421 return UNW_EINVAL; 422 } 423 424 // Remove entry. 425 for (size_t i = finderIdx; i != numDynamicUnwindSectionsFinders - 1; ++i) 426 dynamicUnwindSectionsFinders[i] = dynamicUnwindSectionsFinders[i + 1]; 427 dynamicUnwindSectionsFinders[--numDynamicUnwindSectionsFinders] = nullptr; 428 429 findDynamicUnwindSectionsLock.unlock(); 430 return UNW_ESUCCESS; 431} 432 433#endif // __APPLE__ 434 435// Add logging hooks in Debug builds only 436#ifndef NDEBUG 437#include <stdlib.h> 438 439_LIBUNWIND_HIDDEN 440bool logAPIs() { 441 // do manual lock to avoid use of _cxa_guard_acquire or initializers 442 static bool checked = false; 443 static bool log = false; 444 if (!checked) { 445 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); 446 checked = true; 447 } 448 return log; 449} 450 451_LIBUNWIND_HIDDEN 452bool logUnwinding() { 453 // do manual lock to avoid use of _cxa_guard_acquire or initializers 454 static bool checked = false; 455 static bool log = false; 456 if (!checked) { 457 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); 458 checked = true; 459 } 460 return log; 461} 462 463_LIBUNWIND_HIDDEN 464bool logDWARF() { 465 // do manual lock to avoid use of _cxa_guard_acquire or initializers 466 static bool checked = false; 467 static bool log = false; 468 if (!checked) { 469 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); 470 checked = true; 471 } 472 return log; 473} 474 475#endif // NDEBUG 476 477