1 //===-- InstrumentationRuntimeTSan.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 "InstrumentationRuntimeTSan.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/ValueObject.h"
18 #include "lldb/Expression/UserExpression.h"
19 #include "lldb/Host/StreamFile.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 
33 #include <memory>
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeTSan)38 LLDB_PLUGIN_DEFINE(InstrumentationRuntimeTSan)
39 
40 lldb::InstrumentationRuntimeSP
41 InstrumentationRuntimeTSan::CreateInstance(const lldb::ProcessSP &process_sp) {
42   return InstrumentationRuntimeSP(new InstrumentationRuntimeTSan(process_sp));
43 }
44 
Initialize()45 void InstrumentationRuntimeTSan::Initialize() {
46   PluginManager::RegisterPlugin(
47       GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
48       CreateInstance, GetTypeStatic);
49 }
50 
Terminate()51 void InstrumentationRuntimeTSan::Terminate() {
52   PluginManager::UnregisterPlugin(CreateInstance);
53 }
54 
GetTypeStatic()55 lldb::InstrumentationRuntimeType InstrumentationRuntimeTSan::GetTypeStatic() {
56   return eInstrumentationRuntimeTypeThreadSanitizer;
57 }
58 
~InstrumentationRuntimeTSan()59 InstrumentationRuntimeTSan::~InstrumentationRuntimeTSan() { Deactivate(); }
60 
61 const char *thread_sanitizer_retrieve_report_data_prefix = R"(
62 extern "C"
63 {
64     void *__tsan_get_current_report();
65     int __tsan_get_report_data(void *report, const char **description, int *count,
66                                int *stack_count, int *mop_count, int *loc_count,
67                                int *mutex_count, int *thread_count,
68                                int *unique_tid_count, void **sleep_trace,
69                                unsigned long trace_size);
70     int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
71                                 unsigned long trace_size);
72     int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
73                               int *size, int *write, int *atomic, void **trace,
74                               unsigned long trace_size);
75     int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
76                               void **addr, unsigned long *start, unsigned long *size, int *tid,
77                               int *fd, int *suppressable, void **trace,
78                               unsigned long trace_size);
79     int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
80                                 int *destroyed, void **trace, unsigned long trace_size);
81     int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
82                                  int *running, const char **name, int *parent_tid,
83                                  void **trace, unsigned long trace_size);
84     int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
85 
86     // TODO: dlsym won't work on Windows.
87     void *dlsym(void* handle, const char* symbol);
88     int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type);
89 }
90 )";
91 
92 const char *thread_sanitizer_retrieve_report_data_command = R"(
93 
94 const int REPORT_TRACE_SIZE = 128;
95 const int REPORT_ARRAY_SIZE = 4;
96 
97 struct {
98     void *report;
99     const char *description;
100     int report_count;
101 
102     void *sleep_trace[REPORT_TRACE_SIZE];
103 
104     int stack_count;
105     struct {
106         int idx;
107         void *trace[REPORT_TRACE_SIZE];
108     } stacks[REPORT_ARRAY_SIZE];
109 
110     int mop_count;
111     struct {
112         int idx;
113         int tid;
114         int size;
115         int write;
116         int atomic;
117         void *addr;
118         void *trace[REPORT_TRACE_SIZE];
119     } mops[REPORT_ARRAY_SIZE];
120 
121     int loc_count;
122     struct {
123         int idx;
124         const char *type;
125         void *addr;
126         unsigned long start;
127         unsigned long size;
128         int tid;
129         int fd;
130         int suppressable;
131         void *trace[REPORT_TRACE_SIZE];
132         const char *object_type;
133     } locs[REPORT_ARRAY_SIZE];
134 
135     int mutex_count;
136     struct {
137         int idx;
138         unsigned long mutex_id;
139         void *addr;
140         int destroyed;
141         void *trace[REPORT_TRACE_SIZE];
142     } mutexes[REPORT_ARRAY_SIZE];
143 
144     int thread_count;
145     struct {
146         int idx;
147         int tid;
148         unsigned long os_id;
149         int running;
150         const char *name;
151         int parent_tid;
152         void *trace[REPORT_TRACE_SIZE];
153     } threads[REPORT_ARRAY_SIZE];
154 
155     int unique_tid_count;
156     struct {
157         int idx;
158         int tid;
159     } unique_tids[REPORT_ARRAY_SIZE];
160 } t = {0};
161 
162 ptr__tsan_get_report_loc_object_type = (typeof(ptr__tsan_get_report_loc_object_type))(void *)dlsym((void*)-2 /*RTLD_DEFAULT*/, "__tsan_get_report_loc_object_type");
163 
164 t.report = __tsan_get_current_report();
165 __tsan_get_report_data(t.report, &t.description, &t.report_count, &t.stack_count, &t.mop_count, &t.loc_count, &t.mutex_count, &t.thread_count, &t.unique_tid_count, t.sleep_trace, REPORT_TRACE_SIZE);
166 
167 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
168 for (int i = 0; i < t.stack_count; i++) {
169     t.stacks[i].idx = i;
170     __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
171 }
172 
173 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
174 for (int i = 0; i < t.mop_count; i++) {
175     t.mops[i].idx = i;
176     __tsan_get_report_mop(t.report, i, &t.mops[i].tid, &t.mops[i].addr, &t.mops[i].size, &t.mops[i].write, &t.mops[i].atomic, t.mops[i].trace, REPORT_TRACE_SIZE);
177 }
178 
179 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
180 for (int i = 0; i < t.loc_count; i++) {
181     t.locs[i].idx = i;
182     __tsan_get_report_loc(t.report, i, &t.locs[i].type, &t.locs[i].addr, &t.locs[i].start, &t.locs[i].size, &t.locs[i].tid, &t.locs[i].fd, &t.locs[i].suppressable, t.locs[i].trace, REPORT_TRACE_SIZE);
183     if (ptr__tsan_get_report_loc_object_type)
184         ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type);
185 }
186 
187 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
188 for (int i = 0; i < t.mutex_count; i++) {
189     t.mutexes[i].idx = i;
190     __tsan_get_report_mutex(t.report, i, &t.mutexes[i].mutex_id, &t.mutexes[i].addr, &t.mutexes[i].destroyed, t.mutexes[i].trace, REPORT_TRACE_SIZE);
191 }
192 
193 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
194 for (int i = 0; i < t.thread_count; i++) {
195     t.threads[i].idx = i;
196     __tsan_get_report_thread(t.report, i, &t.threads[i].tid, &t.threads[i].os_id, &t.threads[i].running, &t.threads[i].name, &t.threads[i].parent_tid, t.threads[i].trace, REPORT_TRACE_SIZE);
197 }
198 
199 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
200 for (int i = 0; i < t.unique_tid_count; i++) {
201     t.unique_tids[i].idx = i;
202     __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
203 }
204 
205 t;
206 )";
207 
208 static StructuredData::ArraySP
CreateStackTrace(ValueObjectSP o,const std::string & trace_item_name=".trace")209 CreateStackTrace(ValueObjectSP o,
210                  const std::string &trace_item_name = ".trace") {
211   auto trace_sp = std::make_shared<StructuredData::Array>();
212   ValueObjectSP trace_value_object =
213       o->GetValueForExpressionPath(trace_item_name.c_str());
214   size_t count = trace_value_object->GetNumChildren();
215   for (size_t j = 0; j < count; j++) {
216     addr_t trace_addr =
217         trace_value_object->GetChildAtIndex(j)->GetValueAsUnsigned(0);
218     if (trace_addr == 0)
219       break;
220     trace_sp->AddIntegerItem(trace_addr);
221   }
222   return trace_sp;
223 }
224 
ConvertToStructuredArray(ValueObjectSP return_value_sp,const std::string & items_name,const std::string & count_name,std::function<void (const ValueObjectSP & o,const StructuredData::DictionarySP & dict)> const & callback)225 static StructuredData::ArraySP ConvertToStructuredArray(
226     ValueObjectSP return_value_sp, const std::string &items_name,
227     const std::string &count_name,
228     std::function<void(const ValueObjectSP &o,
229                        const StructuredData::DictionarySP &dict)> const
230         &callback) {
231   auto array_sp = std::make_shared<StructuredData::Array>();
232   unsigned int count =
233       return_value_sp->GetValueForExpressionPath(count_name.c_str())
234           ->GetValueAsUnsigned(0);
235   ValueObjectSP objects =
236       return_value_sp->GetValueForExpressionPath(items_name.c_str());
237   for (unsigned int i = 0; i < count; i++) {
238     ValueObjectSP o = objects->GetChildAtIndex(i);
239     auto dict_sp = std::make_shared<StructuredData::Dictionary>();
240 
241     callback(o, dict_sp);
242 
243     array_sp->AddItem(dict_sp);
244   }
245   return array_sp;
246 }
247 
RetrieveString(ValueObjectSP return_value_sp,ProcessSP process_sp,const std::string & expression_path)248 static std::string RetrieveString(ValueObjectSP return_value_sp,
249                                   ProcessSP process_sp,
250                                   const std::string &expression_path) {
251   addr_t ptr =
252       return_value_sp->GetValueForExpressionPath(expression_path.c_str())
253           ->GetValueAsUnsigned(0);
254   std::string str;
255   Status error;
256   process_sp->ReadCStringFromMemory(ptr, str, error);
257   return str;
258 }
259 
260 static void
GetRenumberedThreadIds(ProcessSP process_sp,ValueObjectSP data,std::map<uint64_t,user_id_t> & thread_id_map)261 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
262                        std::map<uint64_t, user_id_t> &thread_id_map) {
263   ConvertToStructuredArray(
264       data, ".threads", ".thread_count",
265       [process_sp, &thread_id_map](const ValueObjectSP &o,
266                                    const StructuredData::DictionarySP &dict) {
267         uint64_t thread_id =
268             o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
269         uint64_t thread_os_id =
270             o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
271         user_id_t lldb_user_id = 0;
272 
273         bool can_update = true;
274         ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
275             thread_os_id, can_update);
276         if (lldb_thread) {
277           lldb_user_id = lldb_thread->GetIndexID();
278         } else {
279           // This isn't a live thread anymore.  Ask process to assign a new
280           // Index ID (or return an old one if we've already seen this
281           // thread_os_id). It will also make sure that no new threads are
282           // assigned this Index ID.
283           lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
284         }
285 
286         thread_id_map[thread_id] = lldb_user_id;
287       });
288 }
289 
Renumber(uint64_t id,std::map<uint64_t,user_id_t> & thread_id_map)290 static user_id_t Renumber(uint64_t id,
291                           std::map<uint64_t, user_id_t> &thread_id_map) {
292   auto IT = thread_id_map.find(id);
293   if (IT == thread_id_map.end())
294     return 0;
295 
296   return IT->second;
297 }
298 
RetrieveReportData(ExecutionContextRef exe_ctx_ref)299 StructuredData::ObjectSP InstrumentationRuntimeTSan::RetrieveReportData(
300     ExecutionContextRef exe_ctx_ref) {
301   ProcessSP process_sp = GetProcessSP();
302   if (!process_sp)
303     return StructuredData::ObjectSP();
304 
305   ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
306   StackFrameSP frame_sp =
307       thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
308 
309   if (!frame_sp)
310     return StructuredData::ObjectSP();
311 
312   EvaluateExpressionOptions options;
313   options.SetUnwindOnError(true);
314   options.SetTryAllThreads(true);
315   options.SetStopOthers(true);
316   options.SetIgnoreBreakpoints(true);
317   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
318   options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
319   options.SetAutoApplyFixIts(false);
320   options.SetLanguage(eLanguageTypeObjC_plus_plus);
321 
322   ValueObjectSP main_value;
323   ExecutionContext exe_ctx;
324   Status eval_error;
325   frame_sp->CalculateExecutionContext(exe_ctx);
326   ExpressionResults result = UserExpression::Evaluate(
327       exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
328       main_value, eval_error);
329   if (result != eExpressionCompleted) {
330     StreamString ss;
331     ss << "cannot evaluate ThreadSanitizer expression:\n";
332     ss << eval_error.AsCString();
333     Debugger::ReportWarning(ss.GetString().str(),
334                             process_sp->GetTarget().GetDebugger().GetID());
335     return StructuredData::ObjectSP();
336   }
337 
338   std::map<uint64_t, user_id_t> thread_id_map;
339   GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
340 
341   auto dict = std::make_shared<StructuredData::Dictionary>();
342   dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
343   dict->AddStringItem("issue_type",
344                       RetrieveString(main_value, process_sp, ".description"));
345   dict->AddIntegerItem("report_count",
346                        main_value->GetValueForExpressionPath(".report_count")
347                            ->GetValueAsUnsigned(0));
348   dict->AddItem("sleep_trace", CreateStackTrace(
349                                    main_value, ".sleep_trace"));
350 
351   StructuredData::ArraySP stacks = ConvertToStructuredArray(
352       main_value, ".stacks", ".stack_count",
353       [thread_sp](const ValueObjectSP &o,
354                   const StructuredData::DictionarySP &dict) {
355         dict->AddIntegerItem(
356             "index",
357             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
358         dict->AddItem("trace", CreateStackTrace(o));
359         // "stacks" happen on the current thread
360         dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
361       });
362   dict->AddItem("stacks", stacks);
363 
364   StructuredData::ArraySP mops = ConvertToStructuredArray(
365       main_value, ".mops", ".mop_count",
366       [&thread_id_map](const ValueObjectSP &o,
367                        const StructuredData::DictionarySP &dict) {
368         dict->AddIntegerItem(
369             "index",
370             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
371         dict->AddIntegerItem(
372             "thread_id",
373             Renumber(
374                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
375                 thread_id_map));
376         dict->AddIntegerItem(
377             "size",
378             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
379         dict->AddBooleanItem(
380             "is_write",
381             o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
382         dict->AddBooleanItem(
383             "is_atomic",
384             o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
385         dict->AddIntegerItem(
386             "address",
387             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
388         dict->AddItem("trace", CreateStackTrace(o));
389       });
390   dict->AddItem("mops", mops);
391 
392   StructuredData::ArraySP locs = ConvertToStructuredArray(
393       main_value, ".locs", ".loc_count",
394       [process_sp, &thread_id_map](const ValueObjectSP &o,
395                                    const StructuredData::DictionarySP &dict) {
396         dict->AddIntegerItem(
397             "index",
398             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
399         dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
400         dict->AddIntegerItem(
401             "address",
402             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
403         dict->AddIntegerItem(
404             "start",
405             o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
406         dict->AddIntegerItem(
407             "size",
408             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
409         dict->AddIntegerItem(
410             "thread_id",
411             Renumber(
412                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
413                 thread_id_map));
414         dict->AddIntegerItem(
415             "file_descriptor",
416             o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
417         dict->AddIntegerItem("suppressable",
418                              o->GetValueForExpressionPath(".suppressable")
419                                  ->GetValueAsUnsigned(0));
420         dict->AddItem("trace", CreateStackTrace(o));
421         dict->AddStringItem("object_type",
422                             RetrieveString(o, process_sp, ".object_type"));
423       });
424   dict->AddItem("locs", locs);
425 
426   StructuredData::ArraySP mutexes = ConvertToStructuredArray(
427       main_value, ".mutexes", ".mutex_count",
428       [](const ValueObjectSP &o, const StructuredData::DictionarySP &dict) {
429         dict->AddIntegerItem(
430             "index",
431             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
432         dict->AddIntegerItem(
433             "mutex_id",
434             o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
435         dict->AddIntegerItem(
436             "address",
437             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
438         dict->AddIntegerItem(
439             "destroyed",
440             o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
441         dict->AddItem("trace", CreateStackTrace(o));
442       });
443   dict->AddItem("mutexes", mutexes);
444 
445   StructuredData::ArraySP threads = ConvertToStructuredArray(
446       main_value, ".threads", ".thread_count",
447       [process_sp, &thread_id_map](const ValueObjectSP &o,
448                                    const StructuredData::DictionarySP &dict) {
449         dict->AddIntegerItem(
450             "index",
451             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
452         dict->AddIntegerItem(
453             "thread_id",
454             Renumber(
455                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
456                 thread_id_map));
457         dict->AddIntegerItem(
458             "thread_os_id",
459             o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
460         dict->AddIntegerItem(
461             "running",
462             o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
463         dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
464         dict->AddIntegerItem(
465             "parent_thread_id",
466             Renumber(o->GetValueForExpressionPath(".parent_tid")
467                          ->GetValueAsUnsigned(0),
468                      thread_id_map));
469         dict->AddItem("trace", CreateStackTrace(o));
470       });
471   dict->AddItem("threads", threads);
472 
473   StructuredData::ArraySP unique_tids = ConvertToStructuredArray(
474       main_value, ".unique_tids", ".unique_tid_count",
475       [&thread_id_map](const ValueObjectSP &o,
476                        const StructuredData::DictionarySP &dict) {
477         dict->AddIntegerItem(
478             "index",
479             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
480         dict->AddIntegerItem(
481             "tid",
482             Renumber(
483                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
484                 thread_id_map));
485       });
486   dict->AddItem("unique_tids", unique_tids);
487 
488   return dict;
489 }
490 
491 std::string
FormatDescription(StructuredData::ObjectSP report)492 InstrumentationRuntimeTSan::FormatDescription(StructuredData::ObjectSP report) {
493   std::string description = std::string(report->GetAsDictionary()
494                                             ->GetValueForKey("issue_type")
495                                             ->GetAsString()
496                                             ->GetValue());
497 
498   if (description == "data-race") {
499     return "Data race";
500   } else if (description == "data-race-vptr") {
501     return "Data race on C++ virtual pointer";
502   } else if (description == "heap-use-after-free") {
503     return "Use of deallocated memory";
504   } else if (description == "heap-use-after-free-vptr") {
505     return "Use of deallocated C++ virtual pointer";
506   } else if (description == "thread-leak") {
507     return "Thread leak";
508   } else if (description == "locked-mutex-destroy") {
509     return "Destruction of a locked mutex";
510   } else if (description == "mutex-double-lock") {
511     return "Double lock of a mutex";
512   } else if (description == "mutex-invalid-access") {
513     return "Use of an uninitialized or destroyed mutex";
514   } else if (description == "mutex-bad-unlock") {
515     return "Unlock of an unlocked mutex (or by a wrong thread)";
516   } else if (description == "mutex-bad-read-lock") {
517     return "Read lock of a write locked mutex";
518   } else if (description == "mutex-bad-read-unlock") {
519     return "Read unlock of a write locked mutex";
520   } else if (description == "signal-unsafe-call") {
521     return "Signal-unsafe call inside a signal handler";
522   } else if (description == "errno-in-signal-handler") {
523     return "Overwrite of errno in a signal handler";
524   } else if (description == "lock-order-inversion") {
525     return "Lock order inversion (potential deadlock)";
526   } else if (description == "external-race") {
527     return "Race on a library object";
528   } else if (description == "swift-access-race") {
529     return "Swift access race";
530   }
531 
532   // for unknown report codes just show the code
533   return description;
534 }
535 
Sprintf(const char * format,...)536 static std::string Sprintf(const char *format, ...) {
537   StreamString s;
538   va_list args;
539   va_start(args, format);
540   s.PrintfVarArg(format, args);
541   va_end(args);
542   return std::string(s.GetString());
543 }
544 
GetSymbolNameFromAddress(ProcessSP process_sp,addr_t addr)545 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
546   lldb_private::Address so_addr;
547   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
548                                                                        so_addr))
549     return "";
550 
551   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
552   if (!symbol)
553     return "";
554 
555   std::string sym_name = symbol->GetName().GetCString();
556   return sym_name;
557 }
558 
GetSymbolDeclarationFromAddress(ProcessSP process_sp,addr_t addr,Declaration & decl)559 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
560                                             Declaration &decl) {
561   lldb_private::Address so_addr;
562   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
563                                                                        so_addr))
564     return;
565 
566   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
567   if (!symbol)
568     return;
569 
570   ConstString sym_name = symbol->GetMangled().GetName(Mangled::ePreferMangled);
571 
572   ModuleSP module = symbol->CalculateSymbolContextModule();
573   if (!module)
574     return;
575 
576   VariableList var_list;
577   module->FindGlobalVariables(sym_name, CompilerDeclContext(), 1U, var_list);
578   if (var_list.GetSize() < 1)
579     return;
580 
581   VariableSP var = var_list.GetVariableAtIndex(0);
582   decl = var->GetDeclaration();
583 }
584 
GetFirstNonInternalFramePc(StructuredData::ObjectSP trace,bool skip_one_frame)585 addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc(
586     StructuredData::ObjectSP trace, bool skip_one_frame) {
587   ProcessSP process_sp = GetProcessSP();
588   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
589 
590   StructuredData::Array *trace_array = trace->GetAsArray();
591   for (size_t i = 0; i < trace_array->GetSize(); i++) {
592     if (skip_one_frame && i == 0)
593       continue;
594 
595     auto maybe_addr = trace_array->GetItemAtIndexAsInteger<addr_t>(i);
596     if (!maybe_addr)
597       continue;
598     addr_t addr = *maybe_addr;
599 
600     lldb_private::Address so_addr;
601     if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
602             addr, so_addr))
603       continue;
604 
605     if (so_addr.GetModule() == runtime_module_sp)
606       continue;
607 
608     return addr;
609   }
610 
611   return 0;
612 }
613 
614 std::string
GenerateSummary(StructuredData::ObjectSP report)615 InstrumentationRuntimeTSan::GenerateSummary(StructuredData::ObjectSP report) {
616   ProcessSP process_sp = GetProcessSP();
617 
618   std::string summary = std::string(report->GetAsDictionary()
619                                         ->GetValueForKey("description")
620                                         ->GetAsString()
621                                         ->GetValue());
622   bool skip_one_frame =
623       report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
624       "external-race";
625 
626   addr_t pc = 0;
627   if (report->GetAsDictionary()
628           ->GetValueForKey("mops")
629           ->GetAsArray()
630           ->GetSize() > 0)
631     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
632                                         ->GetValueForKey("mops")
633                                         ->GetAsArray()
634                                         ->GetItemAtIndex(0)
635                                         ->GetAsDictionary()
636                                         ->GetValueForKey("trace"),
637                                     skip_one_frame);
638 
639   if (report->GetAsDictionary()
640           ->GetValueForKey("stacks")
641           ->GetAsArray()
642           ->GetSize() > 0)
643     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
644                                         ->GetValueForKey("stacks")
645                                         ->GetAsArray()
646                                         ->GetItemAtIndex(0)
647                                         ->GetAsDictionary()
648                                         ->GetValueForKey("trace"),
649                                     skip_one_frame);
650 
651   if (pc != 0) {
652     summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
653   }
654 
655   if (report->GetAsDictionary()
656           ->GetValueForKey("locs")
657           ->GetAsArray()
658           ->GetSize() > 0) {
659     StructuredData::ObjectSP loc = report->GetAsDictionary()
660                                        ->GetValueForKey("locs")
661                                        ->GetAsArray()
662                                        ->GetItemAtIndex(0);
663     std::string object_type = std::string(loc->GetAsDictionary()
664                                               ->GetValueForKey("object_type")
665                                               ->GetAsString()
666                                               ->GetValue());
667     if (!object_type.empty()) {
668       summary = "Race on " + object_type + " object";
669     }
670     addr_t addr = loc->GetAsDictionary()
671                       ->GetValueForKey("address")
672                       ->GetUnsignedIntegerValue();
673     if (addr == 0)
674       addr = loc->GetAsDictionary()
675                  ->GetValueForKey("start")
676                  ->GetUnsignedIntegerValue();
677 
678     if (addr != 0) {
679       std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
680       if (!global_name.empty()) {
681         summary = summary + " at " + global_name;
682       } else {
683         summary = summary + " at " + Sprintf("0x%llx", addr);
684       }
685     } else {
686       int fd = loc->GetAsDictionary()
687                    ->GetValueForKey("file_descriptor")
688                    ->GetSignedIntegerValue();
689       if (fd != 0) {
690         summary = summary + " on file descriptor " + Sprintf("%d", fd);
691       }
692     }
693   }
694 
695   return summary;
696 }
697 
GetMainRacyAddress(StructuredData::ObjectSP report)698 addr_t InstrumentationRuntimeTSan::GetMainRacyAddress(
699     StructuredData::ObjectSP report) {
700   addr_t result = (addr_t)-1;
701 
702   report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
703       [&result](StructuredData::Object *o) -> bool {
704         addr_t addr = o->GetObjectForDotSeparatedPath("address")
705                           ->GetUnsignedIntegerValue();
706         if (addr < result)
707           result = addr;
708         return true;
709       });
710 
711   return (result == (addr_t)-1) ? 0 : result;
712 }
713 
GetLocationDescription(StructuredData::ObjectSP report,addr_t & global_addr,std::string & global_name,std::string & filename,uint32_t & line)714 std::string InstrumentationRuntimeTSan::GetLocationDescription(
715     StructuredData::ObjectSP report, addr_t &global_addr,
716     std::string &global_name, std::string &filename, uint32_t &line) {
717   std::string result;
718 
719   ProcessSP process_sp = GetProcessSP();
720 
721   if (report->GetAsDictionary()
722           ->GetValueForKey("locs")
723           ->GetAsArray()
724           ->GetSize() > 0) {
725     StructuredData::ObjectSP loc = report->GetAsDictionary()
726                                        ->GetValueForKey("locs")
727                                        ->GetAsArray()
728                                        ->GetItemAtIndex(0);
729     std::string type = std::string(
730         loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
731     if (type == "global") {
732       global_addr = loc->GetAsDictionary()
733                         ->GetValueForKey("address")
734                         ->GetUnsignedIntegerValue();
735 
736       global_name = GetSymbolNameFromAddress(process_sp, global_addr);
737       if (!global_name.empty()) {
738         result = Sprintf("'%s' is a global variable (0x%llx)",
739                          global_name.c_str(), global_addr);
740       } else {
741         result = Sprintf("0x%llx is a global variable", global_addr);
742       }
743 
744       Declaration decl;
745       GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
746       if (decl.GetFile()) {
747         filename = decl.GetFile().GetPath();
748         line = decl.GetLine();
749       }
750     } else if (type == "heap") {
751       addr_t addr = loc->GetAsDictionary()
752                         ->GetValueForKey("start")
753                         ->GetUnsignedIntegerValue();
754 
755       size_t size = loc->GetAsDictionary()
756                         ->GetValueForKey("size")
757                         ->GetUnsignedIntegerValue();
758 
759       std::string object_type = std::string(loc->GetAsDictionary()
760                                                 ->GetValueForKey("object_type")
761                                                 ->GetAsString()
762                                                 ->GetValue());
763       if (!object_type.empty()) {
764         result = Sprintf("Location is a %ld-byte %s object at 0x%llx", size,
765                          object_type.c_str(), addr);
766       } else {
767         result =
768             Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
769       }
770     } else if (type == "stack") {
771       tid_t tid = loc->GetAsDictionary()
772                       ->GetValueForKey("thread_id")
773                       ->GetUnsignedIntegerValue();
774 
775       result = Sprintf("Location is stack of thread %d", tid);
776     } else if (type == "tls") {
777       tid_t tid = loc->GetAsDictionary()
778                       ->GetValueForKey("thread_id")
779                       ->GetUnsignedIntegerValue();
780 
781       result = Sprintf("Location is TLS of thread %d", tid);
782     } else if (type == "fd") {
783       int fd = loc->GetAsDictionary()
784                    ->GetValueForKey("file_descriptor")
785                    ->GetSignedIntegerValue();
786 
787       result = Sprintf("Location is file descriptor %d", fd);
788     }
789   }
790 
791   return result;
792 }
793 
NotifyBreakpointHit(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)794 bool InstrumentationRuntimeTSan::NotifyBreakpointHit(
795     void *baton, StoppointCallbackContext *context, user_id_t break_id,
796     user_id_t break_loc_id) {
797   assert(baton && "null baton");
798   if (!baton)
799     return false;
800 
801   InstrumentationRuntimeTSan *const instance =
802       static_cast<InstrumentationRuntimeTSan *>(baton);
803 
804   ProcessSP process_sp = instance->GetProcessSP();
805 
806   if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
807     return false;
808 
809   StructuredData::ObjectSP report =
810       instance->RetrieveReportData(context->exe_ctx_ref);
811   std::string stop_reason_description =
812       "unknown thread sanitizer fault (unable to extract thread sanitizer "
813       "report)";
814   if (report) {
815     std::string issue_description = instance->FormatDescription(report);
816     report->GetAsDictionary()->AddStringItem("description", issue_description);
817     stop_reason_description = issue_description + " detected";
818     report->GetAsDictionary()->AddStringItem("stop_description",
819                                              stop_reason_description);
820     std::string summary = instance->GenerateSummary(report);
821     report->GetAsDictionary()->AddStringItem("summary", summary);
822     addr_t main_address = instance->GetMainRacyAddress(report);
823     report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
824 
825     addr_t global_addr = 0;
826     std::string global_name;
827     std::string location_filename;
828     uint32_t location_line = 0;
829     std::string location_description = instance->GetLocationDescription(
830         report, global_addr, global_name, location_filename, location_line);
831     report->GetAsDictionary()->AddStringItem("location_description",
832                                              location_description);
833     if (global_addr != 0) {
834       report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
835     }
836     if (!global_name.empty()) {
837       report->GetAsDictionary()->AddStringItem("global_name", global_name);
838     }
839     if (location_filename != "") {
840       report->GetAsDictionary()->AddStringItem("location_filename",
841                                                location_filename);
842       report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
843     }
844 
845     bool all_addresses_are_same = true;
846     report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
847         [&all_addresses_are_same,
848          main_address](StructuredData::Object *o) -> bool {
849           addr_t addr = o->GetObjectForDotSeparatedPath("address")
850                             ->GetUnsignedIntegerValue();
851           if (main_address != addr)
852             all_addresses_are_same = false;
853           return true;
854         });
855     report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
856                                               all_addresses_are_same);
857   }
858 
859   // Make sure this is the right process
860   if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
861     ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
862     if (thread_sp)
863       thread_sp->SetStopInfo(
864           InstrumentationRuntimeStopInfo::
865               CreateStopReasonWithInstrumentationData(
866                   *thread_sp, stop_reason_description, report));
867 
868     StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream();
869     s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
870              "info -s' to get extended information about the "
871              "report.\n");
872 
873     return true; // Return true to stop the target
874   } else
875     return false; // Let target run
876 }
877 
878 const RegularExpression &
GetPatternForRuntimeLibrary()879 InstrumentationRuntimeTSan::GetPatternForRuntimeLibrary() {
880   static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
881   return regex;
882 }
883 
CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)884 bool InstrumentationRuntimeTSan::CheckIfRuntimeIsValid(
885     const lldb::ModuleSP module_sp) {
886   static ConstString g_tsan_get_current_report("__tsan_get_current_report");
887   const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
888       g_tsan_get_current_report, lldb::eSymbolTypeAny);
889   return symbol != nullptr;
890 }
891 
Activate()892 void InstrumentationRuntimeTSan::Activate() {
893   if (IsActive())
894     return;
895 
896   ProcessSP process_sp = GetProcessSP();
897   if (!process_sp)
898     return;
899 
900   ConstString symbol_name("__tsan_on_report");
901   const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
902       symbol_name, eSymbolTypeCode);
903 
904   if (symbol == nullptr)
905     return;
906 
907   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
908     return;
909 
910   Target &target = process_sp->GetTarget();
911   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
912 
913   if (symbol_address == LLDB_INVALID_ADDRESS)
914     return;
915 
916   const bool internal = true;
917   const bool hardware = false;
918   const bool sync = false;
919   Breakpoint *breakpoint =
920       process_sp->GetTarget()
921           .CreateBreakpoint(symbol_address, internal, hardware)
922           .get();
923   breakpoint->SetCallback(InstrumentationRuntimeTSan::NotifyBreakpointHit, this,
924                           sync);
925   breakpoint->SetBreakpointKind("thread-sanitizer-report");
926   SetBreakpointID(breakpoint->GetID());
927 
928   SetActive(true);
929 }
930 
Deactivate()931 void InstrumentationRuntimeTSan::Deactivate() {
932   if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
933     ProcessSP process_sp = GetProcessSP();
934     if (process_sp) {
935       process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
936       SetBreakpointID(LLDB_INVALID_BREAK_ID);
937     }
938   }
939   SetActive(false);
940 }
GenerateThreadName(const std::string & path,StructuredData::Object * o,StructuredData::ObjectSP main_info)941 static std::string GenerateThreadName(const std::string &path,
942                                       StructuredData::Object *o,
943                                       StructuredData::ObjectSP main_info) {
944   std::string result = "additional information";
945 
946   if (path == "mops") {
947     size_t size =
948         o->GetObjectForDotSeparatedPath("size")->GetUnsignedIntegerValue();
949     tid_t thread_id =
950         o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
951     bool is_write =
952         o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
953     bool is_atomic =
954         o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
955     addr_t addr =
956         o->GetObjectForDotSeparatedPath("address")->GetUnsignedIntegerValue();
957 
958     std::string addr_string = Sprintf(" at 0x%llx", addr);
959 
960     if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
961             ->GetBooleanValue()) {
962       addr_string = "";
963     }
964 
965     if (main_info->GetObjectForDotSeparatedPath("issue_type")
966             ->GetStringValue() == "external-race") {
967       result = Sprintf("%s access by thread %d",
968                        is_write ? "mutating" : "read-only", thread_id);
969     } else if (main_info->GetObjectForDotSeparatedPath("issue_type")
970                    ->GetStringValue() == "swift-access-race") {
971       result = Sprintf("modifying access by thread %d", thread_id);
972     } else {
973       result = Sprintf("%s%s of size %zu%s by thread %" PRIu64,
974                        is_atomic ? "atomic " : "", is_write ? "write" : "read",
975                        size, addr_string.c_str(), thread_id);
976     }
977   }
978 
979   if (path == "threads") {
980     tid_t thread_id =
981         o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
982     result = Sprintf("Thread %zu created", thread_id);
983   }
984 
985   if (path == "locs") {
986     std::string type = std::string(
987         o->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
988     tid_t thread_id =
989         o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
990     int fd = o->GetObjectForDotSeparatedPath("file_descriptor")
991                  ->GetSignedIntegerValue();
992     if (type == "heap") {
993       result = Sprintf("Heap block allocated by thread %" PRIu64, thread_id);
994     } else if (type == "fd") {
995       result = Sprintf("File descriptor %d created by thread %" PRIu64, fd,
996                        thread_id);
997     }
998   }
999 
1000   if (path == "mutexes") {
1001     int mutex_id =
1002         o->GetObjectForDotSeparatedPath("mutex_id")->GetSignedIntegerValue();
1003 
1004     result = Sprintf("Mutex M%d created", mutex_id);
1005   }
1006 
1007   if (path == "stacks") {
1008     tid_t thread_id =
1009         o->GetObjectForDotSeparatedPath("thread_id")->GetUnsignedIntegerValue();
1010     result = Sprintf("Thread %" PRIu64, thread_id);
1011   }
1012 
1013   result[0] = toupper(result[0]);
1014 
1015   return result;
1016 }
1017 
AddThreadsForPath(const std::string & path,ThreadCollectionSP threads,ProcessSP process_sp,StructuredData::ObjectSP info)1018 static void AddThreadsForPath(const std::string &path,
1019                               ThreadCollectionSP threads, ProcessSP process_sp,
1020                               StructuredData::ObjectSP info) {
1021   info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
1022       [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
1023         std::vector<lldb::addr_t> pcs;
1024         o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
1025             [&pcs](StructuredData::Object *pc) -> bool {
1026               pcs.push_back(pc->GetUnsignedIntegerValue());
1027               return true;
1028             });
1029 
1030         if (pcs.size() == 0)
1031           return true;
1032 
1033         StructuredData::ObjectSP thread_id_obj =
1034             o->GetObjectForDotSeparatedPath("thread_os_id");
1035         tid_t tid =
1036             thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0;
1037 
1038         ThreadSP new_thread_sp =
1039             std::make_shared<HistoryThread>(*process_sp, tid, pcs);
1040         new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
1041 
1042         // Save this in the Process' ExtendedThreadList so a strong pointer
1043         // retains the object
1044         process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
1045         threads->AddThread(new_thread_sp);
1046 
1047         return true;
1048       });
1049 }
1050 
1051 lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)1052 InstrumentationRuntimeTSan::GetBacktracesFromExtendedStopInfo(
1053     StructuredData::ObjectSP info) {
1054 
1055   ThreadCollectionSP threads = std::make_shared<ThreadCollection>();
1056 
1057   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1058           ->GetStringValue() != "ThreadSanitizer")
1059     return threads;
1060 
1061   ProcessSP process_sp = GetProcessSP();
1062 
1063   AddThreadsForPath("stacks", threads, process_sp, info);
1064   AddThreadsForPath("mops", threads, process_sp, info);
1065   AddThreadsForPath("locs", threads, process_sp, info);
1066   AddThreadsForPath("mutexes", threads, process_sp, info);
1067   AddThreadsForPath("threads", threads, process_sp, info);
1068 
1069   return threads;
1070 }
1071