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