From f03af8844acbd63f257c51577834d2c126a41f47 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 20 Mar 2020 18:09:00 -0700 Subject: Read fault address on arm64 using proposed kernel API. On aarch64, the top 8 bits of the address (i.e. the tag bits) of the fault address in si_addr are always clear. This isn't ideal for MTE which will require these bits in order to correctly diagnose tag mismatches. A proposed kernel patch [1] exposes the full fault address including the tag bits as part of the ucontext. Change debuggerd to read this fault address if available. [1] https://patchwork.kernel.org/patch/11435077/ Bug: 135772972 Change-Id: Ia05be574113860f4e9ecc36a310c4b740e0c4afb --- debuggerd/libdebuggerd/utility.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'debuggerd/libdebuggerd/utility.cpp') diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp index 0a1d2a4da..3bf28b6c2 100644 --- a/debuggerd/libdebuggerd/utility.cpp +++ b/debuggerd/libdebuggerd/utility.cpp @@ -449,3 +449,40 @@ void log_backtrace(log_t* log, unwindstack::Unwinder* unwinder, const char* pref _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(i).c_str()); } } + +#if defined(__aarch64__) +#define FAR_MAGIC 0x46415201 + +struct far_context { + struct _aarch64_ctx head; + __u64 far; +}; +#endif + +uintptr_t get_fault_address(const siginfo_t* siginfo, const ucontext_t* ucontext) { + (void)ucontext; +#if defined(__aarch64__) + // This relies on a kernel patch: + // https://patchwork.kernel.org/patch/11435077/ + // that hasn't been accepted into the kernel yet. TODO(pcc): Update this to + // use the official interface once it lands. + auto* begin = reinterpret_cast(ucontext->uc_mcontext.__reserved); + auto* end = begin + sizeof(ucontext->uc_mcontext.__reserved); + auto* ptr = begin; + while (1) { + auto* ctx = reinterpret_cast(ptr); + if (ctx->magic == 0) { + break; + } + if (ctx->magic == FAR_MAGIC) { + auto* far_ctx = reinterpret_cast(ctx); + return far_ctx->far; + } + ptr += ctx->size; + if (ctx->size % sizeof(void*) != 0 || ptr < begin || ptr >= end) { + break; + } + } +#endif + return reinterpret_cast(siginfo->si_addr); +} -- cgit v1.2.3