| 1 | //===-- ReportRetriever.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 "ReportRetriever.h" |
| 10 | #include "Utility.h" |
| 11 | |
| 12 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 13 | #include "lldb/Core/Debugger.h" |
| 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Expression/UserExpression.h" |
| 16 | #include "lldb/Target/InstrumentationRuntimeStopInfo.h" |
| 17 | #include "lldb/ValueObject/ValueObject.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | const char *address_sanitizer_retrieve_report_data_prefix = R"( |
| 23 | extern "C" |
| 24 | { |
| 25 | int __asan_report_present(); |
| 26 | void *__asan_get_report_pc(); |
| 27 | void *__asan_get_report_bp(); |
| 28 | void *__asan_get_report_sp(); |
| 29 | void *__asan_get_report_address(); |
| 30 | const char *__asan_get_report_description(); |
| 31 | int __asan_get_report_access_type(); |
| 32 | size_t __asan_get_report_access_size(); |
| 33 | } |
| 34 | )" ; |
| 35 | |
| 36 | const char *address_sanitizer_retrieve_report_data_command = R"( |
| 37 | struct { |
| 38 | int present; |
| 39 | int access_type; |
| 40 | void *pc; |
| 41 | void *bp; |
| 42 | void *sp; |
| 43 | void *address; |
| 44 | size_t access_size; |
| 45 | const char *description; |
| 46 | } t; |
| 47 | |
| 48 | t.present = __asan_report_present(); |
| 49 | t.access_type = __asan_get_report_access_type(); |
| 50 | t.pc = __asan_get_report_pc(); |
| 51 | t.bp = __asan_get_report_bp(); |
| 52 | t.sp = __asan_get_report_sp(); |
| 53 | t.address = __asan_get_report_address(); |
| 54 | t.access_size = __asan_get_report_access_size(); |
| 55 | t.description = __asan_get_report_description(); |
| 56 | t |
| 57 | )" ; |
| 58 | |
| 59 | StructuredData::ObjectSP |
| 60 | ReportRetriever::RetrieveReportData(const ProcessSP process_sp) { |
| 61 | if (!process_sp) |
| 62 | return StructuredData::ObjectSP(); |
| 63 | |
| 64 | ThreadSP thread_sp = |
| 65 | process_sp->GetThreadList().GetExpressionExecutionThread(); |
| 66 | |
| 67 | if (!thread_sp) |
| 68 | return StructuredData::ObjectSP(); |
| 69 | |
| 70 | StackFrameSP frame_sp = |
| 71 | thread_sp->GetSelectedFrame(select_most_relevant: DoNoSelectMostRelevantFrame); |
| 72 | |
| 73 | if (!frame_sp) |
| 74 | return StructuredData::ObjectSP(); |
| 75 | |
| 76 | EvaluateExpressionOptions options; |
| 77 | options.SetUnwindOnError(true); |
| 78 | options.SetTryAllThreads(true); |
| 79 | options.SetStopOthers(true); |
| 80 | options.SetIgnoreBreakpoints(true); |
| 81 | options.SetTimeout(process_sp->GetUtilityExpressionTimeout()); |
| 82 | options.SetPrefix(address_sanitizer_retrieve_report_data_prefix); |
| 83 | options.SetAutoApplyFixIts(false); |
| 84 | options.SetLanguage(eLanguageTypeObjC_plus_plus); |
| 85 | |
| 86 | if (auto m = GetPreferredAsanModule(target: process_sp->GetTarget())) { |
| 87 | SymbolContextList sc_list; |
| 88 | sc_list.Append(sc: SymbolContext(std::move(m))); |
| 89 | options.SetPreferredSymbolContexts(std::move(sc_list)); |
| 90 | } |
| 91 | |
| 92 | ValueObjectSP return_value_sp; |
| 93 | ExecutionContext exe_ctx; |
| 94 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 95 | ExpressionResults result = UserExpression::Evaluate( |
| 96 | exe_ctx, options, expr_cstr: address_sanitizer_retrieve_report_data_command, expr_prefix: "" , |
| 97 | result_valobj_sp&: return_value_sp); |
| 98 | if (result != eExpressionCompleted) { |
| 99 | StreamString ss; |
| 100 | ss << "cannot evaluate AddressSanitizer expression:\n" ; |
| 101 | if (return_value_sp) |
| 102 | ss << return_value_sp->GetError().AsCString(); |
| 103 | Debugger::ReportWarning(message: ss.GetString().str(), |
| 104 | debugger_id: process_sp->GetTarget().GetDebugger().GetID()); |
| 105 | return StructuredData::ObjectSP(); |
| 106 | } |
| 107 | |
| 108 | int present = return_value_sp->GetValueForExpressionPath(expression: ".present" ) |
| 109 | ->GetValueAsUnsigned(fail_value: 0); |
| 110 | if (present != 1) |
| 111 | return StructuredData::ObjectSP(); |
| 112 | |
| 113 | addr_t pc = |
| 114 | return_value_sp->GetValueForExpressionPath(expression: ".pc" )->GetValueAsUnsigned(fail_value: 0); |
| 115 | addr_t bp = |
| 116 | return_value_sp->GetValueForExpressionPath(expression: ".bp" )->GetValueAsUnsigned(fail_value: 0); |
| 117 | addr_t sp = |
| 118 | return_value_sp->GetValueForExpressionPath(expression: ".sp" )->GetValueAsUnsigned(fail_value: 0); |
| 119 | addr_t address = return_value_sp->GetValueForExpressionPath(expression: ".address" ) |
| 120 | ->GetValueAsUnsigned(fail_value: 0); |
| 121 | addr_t access_type = |
| 122 | return_value_sp->GetValueForExpressionPath(expression: ".access_type" ) |
| 123 | ->GetValueAsUnsigned(fail_value: 0); |
| 124 | addr_t access_size = |
| 125 | return_value_sp->GetValueForExpressionPath(expression: ".access_size" ) |
| 126 | ->GetValueAsUnsigned(fail_value: 0); |
| 127 | addr_t description_ptr = |
| 128 | return_value_sp->GetValueForExpressionPath(expression: ".description" ) |
| 129 | ->GetValueAsUnsigned(fail_value: 0); |
| 130 | std::string description; |
| 131 | Status error; |
| 132 | process_sp->ReadCStringFromMemory(vm_addr: description_ptr, out_str&: description, error); |
| 133 | |
| 134 | auto dict = std::make_shared<StructuredData::Dictionary>(); |
| 135 | if (!dict) |
| 136 | return StructuredData::ObjectSP(); |
| 137 | |
| 138 | dict->AddStringItem(key: "instrumentation_class" , value: "AddressSanitizer" ); |
| 139 | dict->AddStringItem(key: "stop_type" , value: "fatal_error" ); |
| 140 | dict->AddIntegerItem(key: "pc" , value: pc); |
| 141 | dict->AddIntegerItem(key: "bp" , value: bp); |
| 142 | dict->AddIntegerItem(key: "sp" , value: sp); |
| 143 | dict->AddIntegerItem(key: "address" , value: address); |
| 144 | dict->AddIntegerItem(key: "access_type" , value: access_type); |
| 145 | dict->AddIntegerItem(key: "access_size" , value: access_size); |
| 146 | dict->AddStringItem(key: "description" , value: description); |
| 147 | |
| 148 | return StructuredData::ObjectSP(dict); |
| 149 | } |
| 150 | |
| 151 | std::string |
| 152 | ReportRetriever::FormatDescription(StructuredData::ObjectSP report) { |
| 153 | std::string description = std::string(report->GetAsDictionary() |
| 154 | ->GetValueForKey(key: "description" ) |
| 155 | ->GetAsString() |
| 156 | ->GetValue()); |
| 157 | return llvm::StringSwitch<std::string>(description) |
| 158 | .Case(S: "heap-use-after-free" , Value: "Use of deallocated memory" ) |
| 159 | .Case(S: "heap-buffer-overflow" , Value: "Heap buffer overflow" ) |
| 160 | .Case(S: "stack-buffer-underflow" , Value: "Stack buffer underflow" ) |
| 161 | .Case(S: "initialization-order-fiasco" , Value: "Initialization order problem" ) |
| 162 | .Case(S: "stack-buffer-overflow" , Value: "Stack buffer overflow" ) |
| 163 | .Case(S: "stack-use-after-return" , Value: "Use of stack memory after return" ) |
| 164 | .Case(S: "use-after-poison" , Value: "Use of poisoned memory" ) |
| 165 | .Case(S: "container-overflow" , Value: "Container overflow" ) |
| 166 | .Case(S: "stack-use-after-scope" , Value: "Use of out-of-scope stack memory" ) |
| 167 | .Case(S: "global-buffer-overflow" , Value: "Global buffer overflow" ) |
| 168 | .Case(S: "unknown-crash" , Value: "Invalid memory access" ) |
| 169 | .Case(S: "stack-overflow" , Value: "Stack space exhausted" ) |
| 170 | .Case(S: "null-deref" , Value: "Dereference of null pointer" ) |
| 171 | .Case(S: "wild-jump" , Value: "Jump to non-executable address" ) |
| 172 | .Case(S: "wild-addr-write" , Value: "Write through wild pointer" ) |
| 173 | .Case(S: "wild-addr-read" , Value: "Read from wild pointer" ) |
| 174 | .Case(S: "wild-addr" , Value: "Access through wild pointer" ) |
| 175 | .Case(S: "signal" , Value: "Deadly signal" ) |
| 176 | .Case(S: "double-free" , Value: "Deallocation of freed memory" ) |
| 177 | .Case(S: "new-delete-type-mismatch" , |
| 178 | Value: "Deallocation size different from allocation size" ) |
| 179 | .Case(S: "bad-free" , Value: "Deallocation of non-allocated memory" ) |
| 180 | .Case(S: "alloc-dealloc-mismatch" , |
| 181 | Value: "Mismatch between allocation and deallocation APIs" ) |
| 182 | .Case(S: "bad-malloc_usable_size" , Value: "Invalid argument to malloc_usable_size" ) |
| 183 | .Case(S: "bad-__sanitizer_get_allocated_size" , |
| 184 | Value: "Invalid argument to __sanitizer_get_allocated_size" ) |
| 185 | .Case(S: "param-overlap" , |
| 186 | Value: "Call to function disallowing overlapping memory ranges" ) |
| 187 | .Case(S: "negative-size-param" , Value: "Negative size used when accessing memory" ) |
| 188 | .Case(S: "bad-__sanitizer_annotate_contiguous_container" , |
| 189 | Value: "Invalid argument to __sanitizer_annotate_contiguous_container" ) |
| 190 | .Case(S: "odr-violation" , Value: "Symbol defined in multiple translation units" ) |
| 191 | .Case( |
| 192 | S: "invalid-pointer-pair" , |
| 193 | Value: "Comparison or arithmetic on pointers from different memory regions" ) |
| 194 | // for unknown report codes just show the code |
| 195 | .Default(Value: "AddressSanitizer detected: " + description); |
| 196 | } |
| 197 | |
| 198 | bool ReportRetriever::NotifyBreakpointHit(ProcessSP process_sp, |
| 199 | StoppointCallbackContext *context, |
| 200 | user_id_t break_id, |
| 201 | user_id_t break_loc_id) { |
| 202 | // Make sure this is the right process |
| 203 | if (!process_sp || process_sp != context->exe_ctx_ref.GetProcessSP()) |
| 204 | return false; |
| 205 | |
| 206 | if (process_sp->GetModIDRef().IsLastResumeForUserExpression()) |
| 207 | return false; |
| 208 | |
| 209 | StructuredData::ObjectSP report = RetrieveReportData(process_sp); |
| 210 | if (!report || report->GetType() != lldb::eStructuredDataTypeDictionary) |
| 211 | return false; |
| 212 | |
| 213 | std::string description = FormatDescription(report); |
| 214 | |
| 215 | if (ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP()) |
| 216 | thread_sp->SetStopInfo( |
| 217 | InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData( |
| 218 | thread&: *thread_sp, description, additional_data: report)); |
| 219 | |
| 220 | if (StreamSP stream_sp = |
| 221 | process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()) |
| 222 | stream_sp->Printf(format: "AddressSanitizer report breakpoint hit. Use 'thread " |
| 223 | "info -s' to get extended information about the " |
| 224 | "report.\n" ); |
| 225 | |
| 226 | return true; // Return true to stop the target |
| 227 | } |
| 228 | |
| 229 | Breakpoint *ReportRetriever::SetupBreakpoint(ModuleSP module_sp, |
| 230 | ProcessSP process_sp, |
| 231 | ConstString symbol_name) { |
| 232 | if (!module_sp || !process_sp) |
| 233 | return nullptr; |
| 234 | |
| 235 | const Symbol *symbol = |
| 236 | module_sp->FindFirstSymbolWithNameAndType(name: symbol_name, symbol_type: eSymbolTypeCode); |
| 237 | |
| 238 | if (symbol == nullptr) |
| 239 | return nullptr; |
| 240 | |
| 241 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 242 | return nullptr; |
| 243 | |
| 244 | const Address &address = symbol->GetAddressRef(); |
| 245 | const bool internal = true; |
| 246 | const bool hardware = false; |
| 247 | |
| 248 | Breakpoint *breakpoint = process_sp->GetTarget() |
| 249 | .CreateBreakpoint(addr: address, internal, request_hardware: hardware) |
| 250 | .get(); |
| 251 | |
| 252 | return breakpoint; |
| 253 | } |
| 254 | |