LLDB mainline
ReportRetriever.cpp
Go to the documentation of this file.
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
12#include "lldb/Core/Debugger.h"
13#include "lldb/Core/Module.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
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
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
60 if (!process_sp)
62
63 ThreadSP thread_sp =
64 process_sp->GetThreadList().GetExpressionExecutionThread();
65
66 if (!thread_sp)
68
69 StackFrameSP frame_sp =
70 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
71
72 if (!frame_sp)
74
76 options.SetUnwindOnError(true);
77 options.SetTryAllThreads(true);
78 options.SetStopOthers(true);
79 options.SetIgnoreBreakpoints(true);
80 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
82 options.SetAutoApplyFixIts(false);
84
85 ValueObjectSP return_value_sp;
86 ExecutionContext exe_ctx;
87 frame_sp->CalculateExecutionContext(exe_ctx);
90 return_value_sp);
91 if (result != eExpressionCompleted) {
92 StreamString ss;
93 ss << "cannot evaluate AddressSanitizer expression:\n";
94 if (return_value_sp)
95 ss << return_value_sp->GetError().AsCString();
97 process_sp->GetTarget().GetDebugger().GetID());
99 }
100
101 int present = return_value_sp->GetValueForExpressionPath(".present")
102 ->GetValueAsUnsigned(0);
103 if (present != 1)
105
106 addr_t pc =
107 return_value_sp->GetValueForExpressionPath(".pc")->GetValueAsUnsigned(0);
108 addr_t bp =
109 return_value_sp->GetValueForExpressionPath(".bp")->GetValueAsUnsigned(0);
110 addr_t sp =
111 return_value_sp->GetValueForExpressionPath(".sp")->GetValueAsUnsigned(0);
112 addr_t address = return_value_sp->GetValueForExpressionPath(".address")
113 ->GetValueAsUnsigned(0);
114 addr_t access_type =
115 return_value_sp->GetValueForExpressionPath(".access_type")
116 ->GetValueAsUnsigned(0);
117 addr_t access_size =
118 return_value_sp->GetValueForExpressionPath(".access_size")
119 ->GetValueAsUnsigned(0);
120 addr_t description_ptr =
121 return_value_sp->GetValueForExpressionPath(".description")
122 ->GetValueAsUnsigned(0);
123 std::string description;
125 process_sp->ReadCStringFromMemory(description_ptr, description, error);
126
127 auto dict = std::make_shared<StructuredData::Dictionary>();
128 if (!dict)
130
131 dict->AddStringItem("instrumentation_class", "AddressSanitizer");
132 dict->AddStringItem("stop_type", "fatal_error");
133 dict->AddIntegerItem("pc", pc);
134 dict->AddIntegerItem("bp", bp);
135 dict->AddIntegerItem("sp", sp);
136 dict->AddIntegerItem("address", address);
137 dict->AddIntegerItem("access_type", access_type);
138 dict->AddIntegerItem("access_size", access_size);
139 dict->AddStringItem("description", description);
140
141 return StructuredData::ObjectSP(dict);
142}
143
144std::string
146 std::string description = std::string(report->GetAsDictionary()
147 ->GetValueForKey("description")
148 ->GetAsString()
149 ->GetValue());
150 return llvm::StringSwitch<std::string>(description)
151 .Case("heap-use-after-free", "Use of deallocated memory")
152 .Case("heap-buffer-overflow", "Heap buffer overflow")
153 .Case("stack-buffer-underflow", "Stack buffer underflow")
154 .Case("initialization-order-fiasco", "Initialization order problem")
155 .Case("stack-buffer-overflow", "Stack buffer overflow")
156 .Case("stack-use-after-return", "Use of stack memory after return")
157 .Case("use-after-poison", "Use of poisoned memory")
158 .Case("container-overflow", "Container overflow")
159 .Case("stack-use-after-scope", "Use of out-of-scope stack memory")
160 .Case("global-buffer-overflow", "Global buffer overflow")
161 .Case("unknown-crash", "Invalid memory access")
162 .Case("stack-overflow", "Stack space exhausted")
163 .Case("null-deref", "Dereference of null pointer")
164 .Case("wild-jump", "Jump to non-executable address")
165 .Case("wild-addr-write", "Write through wild pointer")
166 .Case("wild-addr-read", "Read from wild pointer")
167 .Case("wild-addr", "Access through wild pointer")
168 .Case("signal", "Deadly signal")
169 .Case("double-free", "Deallocation of freed memory")
170 .Case("new-delete-type-mismatch",
171 "Deallocation size different from allocation size")
172 .Case("bad-free", "Deallocation of non-allocated memory")
173 .Case("alloc-dealloc-mismatch",
174 "Mismatch between allocation and deallocation APIs")
175 .Case("bad-malloc_usable_size", "Invalid argument to malloc_usable_size")
176 .Case("bad-__sanitizer_get_allocated_size",
177 "Invalid argument to __sanitizer_get_allocated_size")
178 .Case("param-overlap",
179 "Call to function disallowing overlapping memory ranges")
180 .Case("negative-size-param", "Negative size used when accessing memory")
181 .Case("bad-__sanitizer_annotate_contiguous_container",
182 "Invalid argument to __sanitizer_annotate_contiguous_container")
183 .Case("odr-violation", "Symbol defined in multiple translation units")
184 .Case(
185 "invalid-pointer-pair",
186 "Comparison or arithmetic on pointers from different memory regions")
187 // for unknown report codes just show the code
188 .Default("AddressSanitizer detected: " + description);
189}
190
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(
211 *thread_sp, description, report));
212
213 if (StreamFileSP stream_sp = StreamFileSP(
214 process_sp->GetTarget().GetDebugger().GetOutputStreamSP()))
215 stream_sp->Printf("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
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(symbol_name, 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);
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(symbol_address, internal, hardware)
250 .get();
251
252 return breakpoint;
253}
static llvm::raw_ostream & error(Stream &strm)
const char * address_sanitizer_retrieve_report_data_command
const char * address_sanitizer_retrieve_report_data_prefix
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:370
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:463
A uniqued constant string class.
Definition: ConstString.h:40
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
Definition: Debugger.cpp:1622
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:359
void SetLanguage(lldb::LanguageType language_type)
Definition: Target.h:331
void SetPrefix(const char *prefix)
Definition: Target.h:348
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:392
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:380
void SetStopOthers(bool stop_others=true)
Definition: Target.h:396
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:363
lldb::ThreadSP GetThreadSP() const
Get accessor that creates a strong reference from the weak thread reference contained in this object.
lldb::ProcessSP GetProcessSP() const
Get accessor that creates a strong reference from the weak process reference contained in this object...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static lldb::StopInfoSP CreateStopReasonWithInstrumentationData(Thread &thread, std::string description, StructuredData::ObjectSP additional_data)
static StructuredData::ObjectSP RetrieveReportData(const lldb::ProcessSP process_sp)
static bool NotifyBreakpointHit(lldb::ProcessSP process_sp, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
static std::string FormatDescription(StructuredData::ObjectSP report)
static Breakpoint * SetupBreakpoint(lldb::ModuleSP, lldb::ProcessSP, ConstString)
An error handling class.
Definition: Status.h:115
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
llvm::StringRef GetString() const
std::shared_ptr< Object > ObjectSP
bool ValueIsAddress() const
Definition: Symbol.cpp:165
Address & GetAddressRef()
Definition: Symbol.h:72
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:472
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:424
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:450
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
@ eLanguageTypeObjC_plus_plus
Objective-C++.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::StreamFile > StreamFileSP
Definition: lldb-forward.h:433
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
@ eStructuredDataTypeDictionary
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:373