# Short post on an issue with backtrace() on macos While implementing stacktrace symbolication on macos, I noticed that some return addresses returned by [`backtrace()`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/backtrace.3.html) would sometime make [`dladdr()`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dladdr.3.html) fail. [`atos`](https://manp.gs/mac/1/atos) would also not work with these addresses. Upon closer inspection, I found that the high-bits of the pointers were set, and that masking them off solves the problem. I don't know exactly why the high bits are for. Perhaps it has to do with the [arm64e 'pointer authentication'](https://developer.apple.com/documentation/security/preparing-your-app-to-work-with-pointer-authentication) feature. Although the high bits seemed to be always 0xffff80. Anyway, it works when masking off the high bits, so that's good enough for me. Here are examples of working and non-working addresses I wrote down while investigating this: ``` // addresses that worked: 0x1036a7914 0x102dcf914 0x104ecf914 0x106853914 // addresses that didn't: 0xffff80014577b913 0xffff800146a3b913 0xffff800103597913 0xffff800146bb3913 ``` And here's a function to clean up addresses returned by backtrace: ``` static void* _rdx_stacktrace_macos_cleanup_pointer(void* addr) { // example address to mask: 0xffff800103597913 return (void*) ((uintptr_t) addr & (uintptr_t) ~0xffff800000000000); } ``` NOTE: I noticed this behaviour on macos 12.7.6 (Monterey). It may or may not have been fixed it in more recent macos versions.