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

source code of lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp