1 //===-- InstrumentationRuntimeUBSan.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 "InstrumentationRuntimeUBSan.h"
10 
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"
32 #include <cctype>
33 
34 #include <memory>
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 LLDB_PLUGIN_DEFINE(InstrumentationRuntimeUBSan)
40 
41 InstrumentationRuntimeUBSan::~InstrumentationRuntimeUBSan() { Deactivate(); }
42 
43 lldb::InstrumentationRuntimeSP
44 InstrumentationRuntimeUBSan::CreateInstance(const lldb::ProcessSP &process_sp) {
45   return InstrumentationRuntimeSP(new InstrumentationRuntimeUBSan(process_sp));
46 }
47 
48 void InstrumentationRuntimeUBSan::Initialize() {
49   PluginManager::RegisterPlugin(
50       GetPluginNameStatic(),
51       "UndefinedBehaviorSanitizer instrumentation runtime plugin.",
52       CreateInstance, GetTypeStatic);
53 }
54 
55 void InstrumentationRuntimeUBSan::Terminate() {
56   PluginManager::UnregisterPlugin(CreateInstance);
57 }
58 
59 lldb::InstrumentationRuntimeType InstrumentationRuntimeUBSan::GetTypeStatic() {
60   return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer;
61 }
62 
63 static const char *ub_sanitizer_retrieve_report_data_prefix = R"(
64 extern "C" {
65 void
66 __ubsan_get_current_report_data(const char **OutIssueKind,
67     const char **OutMessage, const char **OutFilename, unsigned *OutLine,
68     unsigned *OutCol, char **OutMemoryAddr);
69 }
70 
71 struct data {
72   const char *issue_kind;
73   const char *message;
74   const char *filename;
75   unsigned line;
76   unsigned col;
77   char *memory_addr;
78 };
79 )";
80 
81 static const char *ub_sanitizer_retrieve_report_data_command = R"(
82 data t;
83 __ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line,
84                                 &t.col, &t.memory_addr);
85 t;
86 )";
87 
88 static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp,
89                                ProcessSP process_sp,
90                                const std::string &expression_path) {
91   return return_value_sp->GetValueForExpressionPath(expression_path.c_str())
92       ->GetValueAsUnsigned(0);
93 }
94 
95 static std::string RetrieveString(ValueObjectSP return_value_sp,
96                                   ProcessSP process_sp,
97                                   const std::string &expression_path) {
98   addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path);
99   std::string str;
100   Status error;
101   process_sp->ReadCStringFromMemory(ptr, str, error);
102   return str;
103 }
104 
105 StructuredData::ObjectSP InstrumentationRuntimeUBSan::RetrieveReportData(
106     ExecutionContextRef exe_ctx_ref) {
107   ProcessSP process_sp = GetProcessSP();
108   if (!process_sp)
109     return StructuredData::ObjectSP();
110 
111   ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
112   StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
113   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
114   Target &target = process_sp->GetTarget();
115 
116   if (!frame_sp)
117     return StructuredData::ObjectSP();
118 
119   StreamFileSP Stream = target.GetDebugger().GetOutputStreamSP();
120 
121   EvaluateExpressionOptions options;
122   options.SetUnwindOnError(true);
123   options.SetTryAllThreads(true);
124   options.SetStopOthers(true);
125   options.SetIgnoreBreakpoints(true);
126   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
127   options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix);
128   options.SetAutoApplyFixIts(false);
129   options.SetLanguage(eLanguageTypeObjC_plus_plus);
130 
131   ValueObjectSP main_value;
132   ExecutionContext exe_ctx;
133   Status eval_error;
134   frame_sp->CalculateExecutionContext(exe_ctx);
135   ExpressionResults result = UserExpression::Evaluate(
136       exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "",
137       main_value, eval_error);
138   if (result != eExpressionCompleted) {
139     target.GetDebugger().GetAsyncOutputStream()->Printf(
140         "Warning: Cannot evaluate UndefinedBehaviorSanitizer expression:\n%s\n",
141         eval_error.AsCString());
142     return StructuredData::ObjectSP();
143   }
144 
145   // Gather the PCs of the user frames in the backtrace.
146   StructuredData::Array *trace = new StructuredData::Array();
147   auto trace_sp = StructuredData::ObjectSP(trace);
148   for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
149     const Address FCA = thread_sp->GetStackFrameAtIndex(I)
150                             ->GetFrameCodeAddressForSymbolication();
151     if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
152       continue;
153 
154     lldb::addr_t PC = FCA.GetLoadAddress(&target);
155     trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
156   }
157 
158   std::string IssueKind = RetrieveString(main_value, process_sp, ".issue_kind");
159   std::string ErrMessage = RetrieveString(main_value, process_sp, ".message");
160   std::string Filename = RetrieveString(main_value, process_sp, ".filename");
161   unsigned Line = RetrieveUnsigned(main_value, process_sp, ".line");
162   unsigned Col = RetrieveUnsigned(main_value, process_sp, ".col");
163   uintptr_t MemoryAddr =
164       RetrieveUnsigned(main_value, process_sp, ".memory_addr");
165 
166   auto *d = new StructuredData::Dictionary();
167   auto dict_sp = StructuredData::ObjectSP(d);
168   d->AddStringItem("instrumentation_class", "UndefinedBehaviorSanitizer");
169   d->AddStringItem("description", IssueKind);
170   d->AddStringItem("summary", ErrMessage);
171   d->AddStringItem("filename", Filename);
172   d->AddIntegerItem("line", Line);
173   d->AddIntegerItem("col", Col);
174   d->AddIntegerItem("memory_address", MemoryAddr);
175   d->AddIntegerItem("tid", thread_sp->GetID());
176   d->AddItem("trace", trace_sp);
177   return dict_sp;
178 }
179 
180 static std::string GetStopReasonDescription(StructuredData::ObjectSP report) {
181   llvm::StringRef stop_reason_description_ref;
182   report->GetAsDictionary()->GetValueForKeyAsString(
183       "description", stop_reason_description_ref);
184   std::string stop_reason_description =
185       std::string(stop_reason_description_ref);
186 
187   if (!stop_reason_description.size()) {
188     stop_reason_description = "Undefined behavior detected";
189   } else {
190     stop_reason_description[0] = toupper(stop_reason_description[0]);
191     for (unsigned I = 1; I < stop_reason_description.size(); ++I)
192       if (stop_reason_description[I] == '-')
193         stop_reason_description[I] = ' ';
194   }
195   return stop_reason_description;
196 }
197 
198 bool InstrumentationRuntimeUBSan::NotifyBreakpointHit(
199     void *baton, StoppointCallbackContext *context, user_id_t break_id,
200     user_id_t break_loc_id) {
201   assert(baton && "null baton");
202   if (!baton)
203     return false; ///< false => resume execution.
204 
205   InstrumentationRuntimeUBSan *const instance =
206       static_cast<InstrumentationRuntimeUBSan *>(baton);
207 
208   ProcessSP process_sp = instance->GetProcessSP();
209   ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
210   if (!process_sp || !thread_sp ||
211       process_sp != context->exe_ctx_ref.GetProcessSP())
212     return false;
213 
214   if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
215     return false;
216 
217   StructuredData::ObjectSP report =
218       instance->RetrieveReportData(context->exe_ctx_ref);
219 
220   if (report) {
221     thread_sp->SetStopInfo(
222         InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
223             *thread_sp, GetStopReasonDescription(report), report));
224     return true;
225   }
226 
227   return false;
228 }
229 
230 const RegularExpression &
231 InstrumentationRuntimeUBSan::GetPatternForRuntimeLibrary() {
232   static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_"));
233   return regex;
234 }
235 
236 bool InstrumentationRuntimeUBSan::CheckIfRuntimeIsValid(
237     const lldb::ModuleSP module_sp) {
238   static ConstString ubsan_test_sym("__ubsan_on_report");
239   const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
240       ubsan_test_sym, lldb::eSymbolTypeAny);
241   return symbol != nullptr;
242 }
243 
244 // FIXME: Factor out all the logic we have in common with the {a,t}san plugins.
245 void InstrumentationRuntimeUBSan::Activate() {
246   if (IsActive())
247     return;
248 
249   ProcessSP process_sp = GetProcessSP();
250   if (!process_sp)
251     return;
252 
253   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
254 
255   ConstString symbol_name("__ubsan_on_report");
256   const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
257       symbol_name, eSymbolTypeCode);
258 
259   if (symbol == nullptr)
260     return;
261 
262   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
263     return;
264 
265   Target &target = process_sp->GetTarget();
266   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
267 
268   if (symbol_address == LLDB_INVALID_ADDRESS)
269     return;
270 
271   Breakpoint *breakpoint =
272       process_sp->GetTarget()
273           .CreateBreakpoint(symbol_address, /*internal=*/true,
274                             /*hardware=*/false)
275           .get();
276   breakpoint->SetCallback(InstrumentationRuntimeUBSan::NotifyBreakpointHit,
277                           this, true);
278   breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report");
279   SetBreakpointID(breakpoint->GetID());
280 
281   SetActive(true);
282 }
283 
284 void InstrumentationRuntimeUBSan::Deactivate() {
285   SetActive(false);
286 
287   auto BID = GetBreakpointID();
288   if (BID == LLDB_INVALID_BREAK_ID)
289     return;
290 
291   if (ProcessSP process_sp = GetProcessSP()) {
292     process_sp->GetTarget().RemoveBreakpointByID(BID);
293     SetBreakpointID(LLDB_INVALID_BREAK_ID);
294   }
295 }
296 
297 lldb::ThreadCollectionSP
298 InstrumentationRuntimeUBSan::GetBacktracesFromExtendedStopInfo(
299     StructuredData::ObjectSP info) {
300   ThreadCollectionSP threads;
301   threads = std::make_shared<ThreadCollection>();
302 
303   ProcessSP process_sp = GetProcessSP();
304 
305   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
306           ->GetStringValue() != "UndefinedBehaviorSanitizer")
307     return threads;
308 
309   std::vector<lldb::addr_t> PCs;
310   auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
311   trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
312     PCs.push_back(PC->GetAsInteger()->GetValue());
313     return true;
314   });
315 
316   if (PCs.empty())
317     return threads;
318 
319   StructuredData::ObjectSP thread_id_obj =
320       info->GetObjectForDotSeparatedPath("tid");
321   tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
322 
323   // We gather symbolication addresses above, so no need for HistoryThread to
324   // try to infer the call addresses.
325   bool pcs_are_call_addresses = true;
326   ThreadSP new_thread_sp = std::make_shared<HistoryThread>(
327       *process_sp, tid, PCs, pcs_are_call_addresses);
328   std::string stop_reason_description = GetStopReasonDescription(info);
329   new_thread_sp->SetName(stop_reason_description.c_str());
330 
331   // Save this in the Process' ExtendedThreadList so a strong pointer retains
332   // the object
333   process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
334   threads->AddThread(new_thread_sp);
335 
336   return threads;
337 }
338