Text of lldb.llvm.org › cpp_reference › StopInfoMachException_8cpp_source.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

LLDB: StopInfoMachException.cpp Source File

LLDB mainline llvm-project lldb source Plugins Process Utility StopInfoMachException.cpp Go to the documentation of this file. 1 //===-- StopInfoMachException.cpp -----------------------------------------===// 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 9 #include " StopInfoMachException.h " 10 11 #include " lldb/lldb-forward.h " 12 13 #if defined(__APPLE__) 14 // Needed for the EXC_RESOURCE interpretation macros 15 #include <kern/exc_resource.h> 16 #endif 17 18 #include " lldb/Breakpoint/Watchpoint.h " 19 #include " lldb/Symbol/Symbol.h " 20 #include " lldb/Target/ABI.h " 21 #include " lldb/Target/DynamicLoader.h " 22 #include " lldb/Target/ExecutionContext.h " 23 #include " lldb/Target/Process.h " 24 #include " lldb/Target/RegisterContext.h " 25 #include " lldb/Target/Target.h " 26 #include " lldb/Target/Thread.h " 27 #include " lldb/Target/ThreadPlan.h " 28 #include " lldb/Target/UnixSignals.h " 29 #include " lldb/Utility/LLDBLog.h " 30 #include " lldb/Utility/Log.h " 31 #include " lldb/Utility/StreamString.h " 32 #include <optional> 33 34 using namespace lldb ; 35 using namespace lldb_private ; 36 37 /// Information about a pointer-authentication related instruction.

38 struct PtrauthInstructionInfo { 39 bool IsAuthenticated ; 40 bool IsLoad ; 41 bool DoesBranch ; 42 }; 43 44 /// Get any pointer-authentication related information about the instruction 45 /// at address \p at_addr. 46 static std::optional<PtrauthInstructionInfo> 47 GetPtrauthInstructionInfo ( Target &target, const ArchSpec &arch, 48 const Address &at_addr) { 49 const char *plugin_name = nullptr ; 50 const char *flavor = nullptr ; 51 const char *cpu = nullptr ; 52 const char *features = nullptr ; 53 AddressRange range_bounds(at_addr, 4); 54 const bool prefer_file_cache = true ; 55 DisassemblerSP disassembler_sp = 56 Disassembler::DisassembleRange (arch, plugin_name, flavor, cpu, features, 57 target, range_bounds, prefer_file_cache); 58 if (!disassembler_sp) 59 return std::nullopt; 60 61 InstructionList &insn_list = disassembler_sp->GetInstructionList(); 62 InstructionSP insn = insn_list.

GetInstructionAtIndex (0); 63 if (!insn) 64 return std::nullopt; 65 66 return PtrauthInstructionInfo {insn->IsAuthenticated(), insn->IsLoad(), 67 insn->DoesBranch()}; 68 } 69 70 /// Describe the load address of \p addr using the format filename:line:col. 71 static void DescribeAddressBriefly ( Stream &strm, const Address &addr, 72 Target &target) { 73 strm. Printf ( "at address=0x%" PRIx64, addr. GetLoadAddress (&target)); 74 StreamString s; 75 if (addr.

GetDescription (s, target, eDescriptionLevelBrief )) 76 strm. Printf ( " %s" , s. GetString ().data()); 77 strm. Printf ( ".\n" ); 78 } 79 80 std::optional<addr_t> StopInfoMachException::GetTagFaultAddress () const { 81 const bool bad_access = 82 ( m_value == 1 || m_value == 12); // EXC_BAD_ACCESS or EXC_GUARD 83 const bool tag_fault = ( m_exc_code == 0x106); // EXC_ARM_MTE_TAG_FAULT 84 // Whether the subcode (m_exc_subcode) holds the fault address.

85 const bool has_fault_addr = ( m_exc_data_count >= 2); 86 87 if (bad_access && tag_fault && has_fault_addr) 88 return m_exc_subcode ; // The subcode is the fault address. 89 90 return std::nullopt; 91 } 92 93 static constexpr uint8_t g_mte_tag_shift = 64 - 8; 94 static constexpr addr_t g_mte_tag_mask = ( addr_t )0x0f << g_mte_tag_shift ; 95 96 bool StopInfoMachException::DetermineTagMismatch () { 97 std::optional<addr_t> fault_address = GetTagFaultAddress (); 98 if (!fault_address) 99 return false ; 100 101 const uint64_t bad_address = *fault_address; 102 103 StreamString strm; 104 strm.

Printf ( "EXC_ARM_MTE_TAG_FAULT (code=%" PRIu64 ", address=0x%" PRIx64 105 ")\n" , 106 m_exc_code , bad_address); 107 108 const uint8_t tag = (bad_address & g_mte_tag_mask ) >> g_mte_tag_shift ; 109 const addr_t canonical_addr = bad_address & ~g_mte_tag_mask ; 110 strm. Printf ( 111 "Note: MTE tag mismatch detected: pointer tag=%d, address=0x%" PRIx64, 112 tag, canonical_addr); 113 m_description = std::string(strm. GetString ()); 114 115 return true ; 116 } 117 118 bool StopInfoMachException::DeterminePtrauthFailure ( ExecutionContext &exe_ctx) { 119 bool IsBreakpoint = m_value == 6; // EXC_BREAKPOINT 120 bool IsBadAccess = m_value == 1; // EXC_BAD_ACCESS 121 if (!IsBreakpoint && !IsBadAccess) 122 return false ; 123 124 // Check that we have a live process.

125 if (!exe_ctx. HasProcessScope () || !exe_ctx. HasThreadScope () || 126 !exe_ctx. HasTargetScope ()) 127 return false ; 128 129 Thread &thread = *exe_ctx. GetThreadPtr (); 130 StackFrameSP current_frame = thread.GetStackFrameAtIndex(0); 131 if (!current_frame) 132 return false ; 133 134 Target &target = *exe_ctx. GetTargetPtr (); 135 Process &process = *exe_ctx. GetProcessPtr (); 136 const ArchSpec &arch = target. GetArchitecture (); 137 138 // Check for a ptrauth-enabled target.

139 const bool ptrauth_enabled_target = 140 arch. GetCore () == ArchSpec::eCore_arm_arm64e ; 141 if (!ptrauth_enabled_target) 142 return false ; 143 144 // Set up a stream we can write a diagnostic into. 145 StreamString strm; 146 auto emit_ptrauth_prologue = [&](uint64_t at_address) { 147 strm. Printf ( "EXC_BAD_ACCESS (code=%" PRIu64 ", address=0x%" PRIx64 ")\n" , 148 m_exc_code , at_address); 149 strm. Printf ( "Note: Possible pointer authentication failure detected.\n" ); 150 }; 151 152 ABISP abi_sp = process.

GetABI (); 153 assert(abi_sp && "Missing ABI info" ); 154 155 // Check if we have a "brk 0xc47x" trap, where the value that failed to 156 // authenticate is in x16. 157 Address current_address = current_frame->GetFrameCodeAddress(); 158 if (IsBreakpoint) { 159 RegisterContext *reg_ctx = exe_ctx. GetRegisterContext (); 160 if (!reg_ctx) 161 return false ; 162 163 const RegisterInfo *X16Info = reg_ctx-> GetRegisterInfoByName ( "x16" ); 164 RegisterValue X16Val; 165 if (!reg_ctx-> ReadRegister (X16Info, X16Val)) 166 return false ; 167 uint64_t bad_address = X16Val.

GetAsUInt64 (); 168 169 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address); 170 Address brk_address; 171 if (!target. ResolveLoadAddress (fixed_bad_address, brk_address)) 172 return false ; 173 174 auto brk_ptrauth_info = 175 GetPtrauthInstructionInfo (target, arch, current_address); 176 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) { 177 emit_ptrauth_prologue(bad_address); 178 strm. Printf ( "Found value that failed to authenticate " ); 179 DescribeAddressBriefly (strm, brk_address, target); 180 m_description = std::string(strm.

GetString ()); 181 return true ; 182 } 183 return false ; 184 } 185 186 assert(IsBadAccess && "Handle EXC_BAD_ACCESS only after this point" ); 187 188 // Check that we have the "bad address" from an EXC_BAD_ACCESS. 189 if ( m_exc_data_count < 2) 190 return false ; 191 192 // Ok, we know the Target is valid and that it describes a ptrauth-enabled 193 // device. Now, we need to determine whether this exception was caused by a 194 // ptrauth failure.

195 196 uint64_t bad_address = m_exc_subcode ; 197 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address); 198 uint64_t current_pc = current_address. GetLoadAddress (&target); 199 200 // Detect: LDRAA, LDRAB (Load Register, with pointer authentication). 201 // 202 // If an authenticated load results in an exception, the instruction at the 203 // current PC should be one of LDRAx. 204 if (bad_address != current_pc && fixed_bad_address != current_pc) { 205 auto ptrauth_info = 206 GetPtrauthInstructionInfo (target, arch, current_address); 207 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) { 208 emit_ptrauth_prologue(bad_address); 209 strm.

Printf ( "Found authenticated load instruction " ); 210 DescribeAddressBriefly (strm, current_ad

The rest of this page is on the live site.