1 //===-- UBSanRuntime.cpp ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "UBSanRuntime.h"
11 #include "Plugins/Process/Utility/HistoryThread.h"
12 #include "lldb/Breakpoint/StoppointCallbackContext.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginInterface.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Expression/UserExpression.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Symbol/Symbol.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Symbol/VariableList.h"
25 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/StopInfo.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Utility/RegularExpression.h"
31 #include "lldb/Utility/Stream.h"
37 using namespace lldb_private;
39 UndefinedBehaviorSanitizerRuntime::~UndefinedBehaviorSanitizerRuntime() {
43 lldb::InstrumentationRuntimeSP
44 UndefinedBehaviorSanitizerRuntime::CreateInstance(
45 const lldb::ProcessSP &process_sp) {
46 return InstrumentationRuntimeSP(
47 new UndefinedBehaviorSanitizerRuntime(process_sp));
50 void UndefinedBehaviorSanitizerRuntime::Initialize() {
51 PluginManager::RegisterPlugin(
52 GetPluginNameStatic(),
53 "UndefinedBehaviorSanitizer instrumentation runtime plugin.",
54 CreateInstance, GetTypeStatic);
57 void UndefinedBehaviorSanitizerRuntime::Terminate() {
58 PluginManager::UnregisterPlugin(CreateInstance);
61 lldb_private::ConstString
62 UndefinedBehaviorSanitizerRuntime::GetPluginNameStatic() {
63 return ConstString("UndefinedBehaviorSanitizer");
66 lldb::InstrumentationRuntimeType
67 UndefinedBehaviorSanitizerRuntime::GetTypeStatic() {
68 return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer;
71 static const char *ub_sanitizer_retrieve_report_data_prefix = R"(
74 __ubsan_get_current_report_data(const char **OutIssueKind,
75 const char **OutMessage, const char **OutFilename, unsigned *OutLine,
76 unsigned *OutCol, char **OutMemoryAddr);
80 const char *issue_kind;
89 static const char *ub_sanitizer_retrieve_report_data_command = R"(
91 __ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line,
92 &t.col, &t.memory_addr);
96 static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp,
98 const std::string &expression_path) {
99 return return_value_sp->GetValueForExpressionPath(expression_path.c_str())
100 ->GetValueAsUnsigned(0);
103 static std::string RetrieveString(ValueObjectSP return_value_sp,
104 ProcessSP process_sp,
105 const std::string &expression_path) {
106 addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path);
109 process_sp->ReadCStringFromMemory(ptr, str, error);
113 StructuredData::ObjectSP UndefinedBehaviorSanitizerRuntime::RetrieveReportData(
114 ExecutionContextRef exe_ctx_ref) {
115 ProcessSP process_sp = GetProcessSP();
117 return StructuredData::ObjectSP();
119 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
120 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
121 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
122 Target &target = process_sp->GetTarget();
125 return StructuredData::ObjectSP();
127 StreamFileSP Stream(target.GetDebugger().GetOutputFile());
129 EvaluateExpressionOptions options;
130 options.SetUnwindOnError(true);
131 options.SetTryAllThreads(true);
132 options.SetStopOthers(true);
133 options.SetIgnoreBreakpoints(true);
134 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
135 options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix);
136 options.SetAutoApplyFixIts(false);
137 options.SetLanguage(eLanguageTypeObjC_plus_plus);
139 ValueObjectSP main_value;
140 ExecutionContext exe_ctx;
142 frame_sp->CalculateExecutionContext(exe_ctx);
143 ExpressionResults result = UserExpression::Evaluate(
144 exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "",
145 main_value, eval_error);
146 if (result != eExpressionCompleted) {
147 target.GetDebugger().GetAsyncOutputStream()->Printf(
148 "Warning: Cannot evaluate UndefinedBehaviorSanitizer expression:\n%s\n",
149 eval_error.AsCString());
150 return StructuredData::ObjectSP();
153 // Gather the PCs of the user frames in the backtrace.
154 StructuredData::Array *trace = new StructuredData::Array();
155 auto trace_sp = StructuredData::ObjectSP(trace);
156 for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
158 thread_sp->GetStackFrameAtIndex(I)->GetFrameCodeAddress();
159 if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
162 lldb::addr_t PC = FCA.GetLoadAddress(&target);
163 trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
166 std::string IssueKind = RetrieveString(main_value, process_sp, ".issue_kind");
167 std::string ErrMessage = RetrieveString(main_value, process_sp, ".message");
168 std::string Filename = RetrieveString(main_value, process_sp, ".filename");
169 unsigned Line = RetrieveUnsigned(main_value, process_sp, ".line");
170 unsigned Col = RetrieveUnsigned(main_value, process_sp, ".col");
171 uintptr_t MemoryAddr =
172 RetrieveUnsigned(main_value, process_sp, ".memory_addr");
174 auto *d = new StructuredData::Dictionary();
175 auto dict_sp = StructuredData::ObjectSP(d);
176 d->AddStringItem("instrumentation_class", "UndefinedBehaviorSanitizer");
177 d->AddStringItem("description", IssueKind);
178 d->AddStringItem("summary", ErrMessage);
179 d->AddStringItem("filename", Filename);
180 d->AddIntegerItem("line", Line);
181 d->AddIntegerItem("col", Col);
182 d->AddIntegerItem("memory_address", MemoryAddr);
183 d->AddIntegerItem("tid", thread_sp->GetID());
184 d->AddItem("trace", trace_sp);
188 static std::string GetStopReasonDescription(StructuredData::ObjectSP report) {
189 llvm::StringRef stop_reason_description_ref;
190 report->GetAsDictionary()->GetValueForKeyAsString("description",
191 stop_reason_description_ref);
192 std::string stop_reason_description = stop_reason_description_ref;
194 if (!stop_reason_description.size()) {
195 stop_reason_description = "Undefined behavior detected";
197 stop_reason_description[0] = toupper(stop_reason_description[0]);
198 for (unsigned I = 1; I < stop_reason_description.size(); ++I)
199 if (stop_reason_description[I] == '-')
200 stop_reason_description[I] = ' ';
202 return stop_reason_description;
205 bool UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit(
206 void *baton, StoppointCallbackContext *context, user_id_t break_id,
207 user_id_t break_loc_id) {
208 assert(baton && "null baton");
210 return false; //< false => resume execution.
212 UndefinedBehaviorSanitizerRuntime *const instance =
213 static_cast<UndefinedBehaviorSanitizerRuntime *>(baton);
215 ProcessSP process_sp = instance->GetProcessSP();
216 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
217 if (!process_sp || !thread_sp ||
218 process_sp != context->exe_ctx_ref.GetProcessSP())
221 if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
224 StructuredData::ObjectSP report =
225 instance->RetrieveReportData(context->exe_ctx_ref);
228 thread_sp->SetStopInfo(
229 InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
230 *thread_sp, GetStopReasonDescription(report), report));
237 const RegularExpression &
238 UndefinedBehaviorSanitizerRuntime::GetPatternForRuntimeLibrary() {
239 static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_"));
243 bool UndefinedBehaviorSanitizerRuntime::CheckIfRuntimeIsValid(
244 const lldb::ModuleSP module_sp) {
245 static ConstString ubsan_test_sym("__ubsan_on_report");
246 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
247 ubsan_test_sym, lldb::eSymbolTypeAny);
248 return symbol != nullptr;
251 // FIXME: Factor out all the logic we have in common with the {a,t}san plugins.
252 void UndefinedBehaviorSanitizerRuntime::Activate() {
256 ProcessSP process_sp = GetProcessSP();
260 ModuleSP runtime_module_sp = GetRuntimeModuleSP();
262 ConstString symbol_name("__ubsan_on_report");
263 const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
264 symbol_name, eSymbolTypeCode);
266 if (symbol == nullptr)
269 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
272 Target &target = process_sp->GetTarget();
273 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
275 if (symbol_address == LLDB_INVALID_ADDRESS)
278 Breakpoint *breakpoint =
279 process_sp->GetTarget()
280 .CreateBreakpoint(symbol_address, /*internal=*/true,
283 breakpoint->SetCallback(
284 UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit, this, true);
285 breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report");
286 SetBreakpointID(breakpoint->GetID());
291 void UndefinedBehaviorSanitizerRuntime::Deactivate() {
294 auto BID = GetBreakpointID();
295 if (BID == LLDB_INVALID_BREAK_ID)
298 if (ProcessSP process_sp = GetProcessSP()) {
299 process_sp->GetTarget().RemoveBreakpointByID(BID);
300 SetBreakpointID(LLDB_INVALID_BREAK_ID);
304 lldb::ThreadCollectionSP
305 UndefinedBehaviorSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
306 StructuredData::ObjectSP info) {
307 ThreadCollectionSP threads;
308 threads = std::make_shared<ThreadCollection>();
310 ProcessSP process_sp = GetProcessSP();
312 if (info->GetObjectForDotSeparatedPath("instrumentation_class")
313 ->GetStringValue() != "UndefinedBehaviorSanitizer")
316 std::vector<lldb::addr_t> PCs;
317 auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
318 trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
319 PCs.push_back(PC->GetAsInteger()->GetValue());
326 StructuredData::ObjectSP thread_id_obj =
327 info->GetObjectForDotSeparatedPath("tid");
328 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
330 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, PCs);
331 ThreadSP new_thread_sp(history_thread);
332 std::string stop_reason_description = GetStopReasonDescription(info);
333 new_thread_sp->SetName(stop_reason_description.c_str());
335 // Save this in the Process' ExtendedThreadList so a strong pointer retains
337 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
338 threads->AddThread(new_thread_sp);