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     process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
331         "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
332         eval_error.AsCString());
333     return StructuredData::ObjectSP();
334   }
335 
336   std::map<uint64_t, user_id_t> thread_id_map;
337   GetRenumberedThreadIds(process_sp, main_value, thread_id_map);
338 
339   StructuredData::Dictionary *dict = new StructuredData::Dictionary();
340   dict->AddStringItem("instrumentation_class", "ThreadSanitizer");
341   dict->AddStringItem("issue_type",
342                       RetrieveString(main_value, process_sp, ".description"));
343   dict->AddIntegerItem("report_count",
344                        main_value->GetValueForExpressionPath(".report_count")
345                            ->GetValueAsUnsigned(0));
346   dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
347                                    main_value, ".sleep_trace")));
348 
349   StructuredData::Array *stacks = ConvertToStructuredArray(
350       main_value, ".stacks", ".stack_count",
351       [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) {
352         dict->AddIntegerItem(
353             "index",
354             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
355         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
356         // "stacks" happen on the current thread
357         dict->AddIntegerItem("thread_id", thread_sp->GetIndexID());
358       });
359   dict->AddItem("stacks", StructuredData::ObjectSP(stacks));
360 
361   StructuredData::Array *mops = ConvertToStructuredArray(
362       main_value, ".mops", ".mop_count",
363       [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
364         dict->AddIntegerItem(
365             "index",
366             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
367         dict->AddIntegerItem(
368             "thread_id",
369             Renumber(
370                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
371                 thread_id_map));
372         dict->AddIntegerItem(
373             "size",
374             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
375         dict->AddBooleanItem(
376             "is_write",
377             o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0));
378         dict->AddBooleanItem(
379             "is_atomic",
380             o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0));
381         dict->AddIntegerItem(
382             "address",
383             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
384         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
385       });
386   dict->AddItem("mops", StructuredData::ObjectSP(mops));
387 
388   StructuredData::Array *locs = ConvertToStructuredArray(
389       main_value, ".locs", ".loc_count",
390       [process_sp, &thread_id_map](ValueObjectSP o,
391                                    StructuredData::Dictionary *dict) {
392         dict->AddIntegerItem(
393             "index",
394             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
395         dict->AddStringItem("type", RetrieveString(o, process_sp, ".type"));
396         dict->AddIntegerItem(
397             "address",
398             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
399         dict->AddIntegerItem(
400             "start",
401             o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0));
402         dict->AddIntegerItem(
403             "size",
404             o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0));
405         dict->AddIntegerItem(
406             "thread_id",
407             Renumber(
408                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
409                 thread_id_map));
410         dict->AddIntegerItem(
411             "file_descriptor",
412             o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0));
413         dict->AddIntegerItem("suppressable",
414                              o->GetValueForExpressionPath(".suppressable")
415                                  ->GetValueAsUnsigned(0));
416         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
417         dict->AddStringItem("object_type",
418                             RetrieveString(o, process_sp, ".object_type"));
419       });
420   dict->AddItem("locs", StructuredData::ObjectSP(locs));
421 
422   StructuredData::Array *mutexes = ConvertToStructuredArray(
423       main_value, ".mutexes", ".mutex_count",
424       [](ValueObjectSP o, StructuredData::Dictionary *dict) {
425         dict->AddIntegerItem(
426             "index",
427             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
428         dict->AddIntegerItem(
429             "mutex_id",
430             o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0));
431         dict->AddIntegerItem(
432             "address",
433             o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0));
434         dict->AddIntegerItem(
435             "destroyed",
436             o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0));
437         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
438       });
439   dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes));
440 
441   StructuredData::Array *threads = ConvertToStructuredArray(
442       main_value, ".threads", ".thread_count",
443       [process_sp, &thread_id_map](ValueObjectSP o,
444                                    StructuredData::Dictionary *dict) {
445         dict->AddIntegerItem(
446             "index",
447             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
448         dict->AddIntegerItem(
449             "thread_id",
450             Renumber(
451                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
452                 thread_id_map));
453         dict->AddIntegerItem(
454             "thread_os_id",
455             o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0));
456         dict->AddIntegerItem(
457             "running",
458             o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0));
459         dict->AddStringItem("name", RetrieveString(o, process_sp, ".name"));
460         dict->AddIntegerItem(
461             "parent_thread_id",
462             Renumber(o->GetValueForExpressionPath(".parent_tid")
463                          ->GetValueAsUnsigned(0),
464                      thread_id_map));
465         dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o)));
466       });
467   dict->AddItem("threads", StructuredData::ObjectSP(threads));
468 
469   StructuredData::Array *unique_tids = ConvertToStructuredArray(
470       main_value, ".unique_tids", ".unique_tid_count",
471       [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) {
472         dict->AddIntegerItem(
473             "index",
474             o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0));
475         dict->AddIntegerItem(
476             "tid",
477             Renumber(
478                 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0),
479                 thread_id_map));
480       });
481   dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids));
482 
483   return StructuredData::ObjectSP(dict);
484 }
485 
486 std::string
487 InstrumentationRuntimeTSan::FormatDescription(StructuredData::ObjectSP report) {
488   std::string description = std::string(report->GetAsDictionary()
489                                             ->GetValueForKey("issue_type")
490                                             ->GetAsString()
491                                             ->GetValue());
492 
493   if (description == "data-race") {
494     return "Data race";
495   } else if (description == "data-race-vptr") {
496     return "Data race on C++ virtual pointer";
497   } else if (description == "heap-use-after-free") {
498     return "Use of deallocated memory";
499   } else if (description == "heap-use-after-free-vptr") {
500     return "Use of deallocated C++ virtual pointer";
501   } else if (description == "thread-leak") {
502     return "Thread leak";
503   } else if (description == "locked-mutex-destroy") {
504     return "Destruction of a locked mutex";
505   } else if (description == "mutex-double-lock") {
506     return "Double lock of a mutex";
507   } else if (description == "mutex-invalid-access") {
508     return "Use of an uninitialized or destroyed mutex";
509   } else if (description == "mutex-bad-unlock") {
510     return "Unlock of an unlocked mutex (or by a wrong thread)";
511   } else if (description == "mutex-bad-read-lock") {
512     return "Read lock of a write locked mutex";
513   } else if (description == "mutex-bad-read-unlock") {
514     return "Read unlock of a write locked mutex";
515   } else if (description == "signal-unsafe-call") {
516     return "Signal-unsafe call inside a signal handler";
517   } else if (description == "errno-in-signal-handler") {
518     return "Overwrite of errno in a signal handler";
519   } else if (description == "lock-order-inversion") {
520     return "Lock order inversion (potential deadlock)";
521   } else if (description == "external-race") {
522     return "Race on a library object";
523   } else if (description == "swift-access-race") {
524     return "Swift access race";
525   }
526 
527   // for unknown report codes just show the code
528   return description;
529 }
530 
531 static std::string Sprintf(const char *format, ...) {
532   StreamString s;
533   va_list args;
534   va_start(args, format);
535   s.PrintfVarArg(format, args);
536   va_end(args);
537   return std::string(s.GetString());
538 }
539 
540 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) {
541   lldb_private::Address so_addr;
542   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
543                                                                        so_addr))
544     return "";
545 
546   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
547   if (!symbol)
548     return "";
549 
550   std::string sym_name = symbol->GetName().GetCString();
551   return sym_name;
552 }
553 
554 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr,
555                                             Declaration &decl) {
556   lldb_private::Address so_addr;
557   if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr,
558                                                                        so_addr))
559     return;
560 
561   lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol();
562   if (!symbol)
563     return;
564 
565   ConstString sym_name = symbol->GetMangled().GetName(Mangled::ePreferMangled);
566 
567   ModuleSP module = symbol->CalculateSymbolContextModule();
568   if (!module)
569     return;
570 
571   VariableList var_list;
572   module->FindGlobalVariables(sym_name, CompilerDeclContext(), 1U, var_list);
573   if (var_list.GetSize() < 1)
574     return;
575 
576   VariableSP var = var_list.GetVariableAtIndex(0);
577   decl = var->GetDeclaration();
578 }
579 
580 addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc(
581     StructuredData::ObjectSP trace, bool skip_one_frame) {
582   ProcessSP process_sp = GetProcessSP();
583   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
584 
585   StructuredData::Array *trace_array = trace->GetAsArray();
586   for (size_t i = 0; i < trace_array->GetSize(); i++) {
587     if (skip_one_frame && i == 0)
588       continue;
589 
590     addr_t addr;
591     if (!trace_array->GetItemAtIndexAsInteger(i, addr))
592       continue;
593 
594     lldb_private::Address so_addr;
595     if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
596             addr, so_addr))
597       continue;
598 
599     if (so_addr.GetModule() == runtime_module_sp)
600       continue;
601 
602     return addr;
603   }
604 
605   return 0;
606 }
607 
608 std::string
609 InstrumentationRuntimeTSan::GenerateSummary(StructuredData::ObjectSP report) {
610   ProcessSP process_sp = GetProcessSP();
611 
612   std::string summary = std::string(report->GetAsDictionary()
613                                         ->GetValueForKey("description")
614                                         ->GetAsString()
615                                         ->GetValue());
616   bool skip_one_frame =
617       report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
618       "external-race";
619 
620   addr_t pc = 0;
621   if (report->GetAsDictionary()
622           ->GetValueForKey("mops")
623           ->GetAsArray()
624           ->GetSize() > 0)
625     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
626                                         ->GetValueForKey("mops")
627                                         ->GetAsArray()
628                                         ->GetItemAtIndex(0)
629                                         ->GetAsDictionary()
630                                         ->GetValueForKey("trace"),
631                                     skip_one_frame);
632 
633   if (report->GetAsDictionary()
634           ->GetValueForKey("stacks")
635           ->GetAsArray()
636           ->GetSize() > 0)
637     pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
638                                         ->GetValueForKey("stacks")
639                                         ->GetAsArray()
640                                         ->GetItemAtIndex(0)
641                                         ->GetAsDictionary()
642                                         ->GetValueForKey("trace"),
643                                     skip_one_frame);
644 
645   if (pc != 0) {
646     summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
647   }
648 
649   if (report->GetAsDictionary()
650           ->GetValueForKey("locs")
651           ->GetAsArray()
652           ->GetSize() > 0) {
653     StructuredData::ObjectSP loc = report->GetAsDictionary()
654                                        ->GetValueForKey("locs")
655                                        ->GetAsArray()
656                                        ->GetItemAtIndex(0);
657     std::string object_type = std::string(loc->GetAsDictionary()
658                                               ->GetValueForKey("object_type")
659                                               ->GetAsString()
660                                               ->GetValue());
661     if (!object_type.empty()) {
662       summary = "Race on " + object_type + " object";
663     }
664     addr_t addr = loc->GetAsDictionary()
665                       ->GetValueForKey("address")
666                       ->GetAsInteger()
667                       ->GetValue();
668     if (addr == 0)
669       addr = loc->GetAsDictionary()
670                  ->GetValueForKey("start")
671                  ->GetAsInteger()
672                  ->GetValue();
673 
674     if (addr != 0) {
675       std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
676       if (!global_name.empty()) {
677         summary = summary + " at " + global_name;
678       } else {
679         summary = summary + " at " + Sprintf("0x%llx", addr);
680       }
681     } else {
682       int fd = loc->GetAsDictionary()
683                    ->GetValueForKey("file_descriptor")
684                    ->GetAsInteger()
685                    ->GetValue();
686       if (fd != 0) {
687         summary = summary + " on file descriptor " + Sprintf("%d", fd);
688       }
689     }
690   }
691 
692   return summary;
693 }
694 
695 addr_t InstrumentationRuntimeTSan::GetMainRacyAddress(
696     StructuredData::ObjectSP report) {
697   addr_t result = (addr_t)-1;
698 
699   report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
700       [&result](StructuredData::Object *o) -> bool {
701         addr_t addr =
702             o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
703         if (addr < result)
704           result = addr;
705         return true;
706       });
707 
708   return (result == (addr_t)-1) ? 0 : result;
709 }
710 
711 std::string InstrumentationRuntimeTSan::GetLocationDescription(
712     StructuredData::ObjectSP report, addr_t &global_addr,
713     std::string &global_name, std::string &filename, uint32_t &line) {
714   std::string result = "";
715 
716   ProcessSP process_sp = GetProcessSP();
717 
718   if (report->GetAsDictionary()
719           ->GetValueForKey("locs")
720           ->GetAsArray()
721           ->GetSize() > 0) {
722     StructuredData::ObjectSP loc = report->GetAsDictionary()
723                                        ->GetValueForKey("locs")
724                                        ->GetAsArray()
725                                        ->GetItemAtIndex(0);
726     std::string type = std::string(
727         loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
728     if (type == "global") {
729       global_addr = loc->GetAsDictionary()
730                         ->GetValueForKey("address")
731                         ->GetAsInteger()
732                         ->GetValue();
733       global_name = GetSymbolNameFromAddress(process_sp, global_addr);
734       if (!global_name.empty()) {
735         result = Sprintf("'%s' is a global variable (0x%llx)",
736                          global_name.c_str(), global_addr);
737       } else {
738         result = Sprintf("0x%llx is a global variable", global_addr);
739       }
740 
741       Declaration decl;
742       GetSymbolDeclarationFromAddress(process_sp, global_addr, decl);
743       if (decl.GetFile()) {
744         filename = decl.GetFile().GetPath();
745         line = decl.GetLine();
746       }
747     } else if (type == "heap") {
748       addr_t addr = loc->GetAsDictionary()
749                         ->GetValueForKey("start")
750                         ->GetAsInteger()
751                         ->GetValue();
752       long size = loc->GetAsDictionary()
753                       ->GetValueForKey("size")
754                       ->GetAsInteger()
755                       ->GetValue();
756       std::string object_type = std::string(loc->GetAsDictionary()
757                                                 ->GetValueForKey("object_type")
758                                                 ->GetAsString()
759                                                 ->GetValue());
760       if (!object_type.empty()) {
761         result = Sprintf("Location is a %ld-byte %s object at 0x%llx", size,
762                          object_type.c_str(), addr);
763       } else {
764         result =
765             Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr);
766       }
767     } else if (type == "stack") {
768       int tid = loc->GetAsDictionary()
769                     ->GetValueForKey("thread_id")
770                     ->GetAsInteger()
771                     ->GetValue();
772       result = Sprintf("Location is stack of thread %d", tid);
773     } else if (type == "tls") {
774       int tid = loc->GetAsDictionary()
775                     ->GetValueForKey("thread_id")
776                     ->GetAsInteger()
777                     ->GetValue();
778       result = Sprintf("Location is TLS of thread %d", tid);
779     } else if (type == "fd") {
780       int fd = loc->GetAsDictionary()
781                    ->GetValueForKey("file_descriptor")
782                    ->GetAsInteger()
783                    ->GetValue();
784       result = Sprintf("Location is file descriptor %d", fd);
785     }
786   }
787 
788   return result;
789 }
790 
791 bool InstrumentationRuntimeTSan::NotifyBreakpointHit(
792     void *baton, StoppointCallbackContext *context, user_id_t break_id,
793     user_id_t break_loc_id) {
794   assert(baton && "null baton");
795   if (!baton)
796     return false;
797 
798   InstrumentationRuntimeTSan *const instance =
799       static_cast<InstrumentationRuntimeTSan *>(baton);
800 
801   ProcessSP process_sp = instance->GetProcessSP();
802 
803   if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
804     return false;
805 
806   StructuredData::ObjectSP report =
807       instance->RetrieveReportData(context->exe_ctx_ref);
808   std::string stop_reason_description =
809       "unknown thread sanitizer fault (unable to extract thread sanitizer "
810       "report)";
811   if (report) {
812     std::string issue_description = instance->FormatDescription(report);
813     report->GetAsDictionary()->AddStringItem("description", issue_description);
814     stop_reason_description = issue_description + " detected";
815     report->GetAsDictionary()->AddStringItem("stop_description",
816                                              stop_reason_description);
817     std::string summary = instance->GenerateSummary(report);
818     report->GetAsDictionary()->AddStringItem("summary", summary);
819     addr_t main_address = instance->GetMainRacyAddress(report);
820     report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
821 
822     addr_t global_addr = 0;
823     std::string global_name = "";
824     std::string location_filename = "";
825     uint32_t location_line = 0;
826     std::string location_description = instance->GetLocationDescription(
827         report, global_addr, global_name, location_filename, location_line);
828     report->GetAsDictionary()->AddStringItem("location_description",
829                                              location_description);
830     if (global_addr != 0) {
831       report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
832     }
833     if (!global_name.empty()) {
834       report->GetAsDictionary()->AddStringItem("global_name", global_name);
835     }
836     if (location_filename != "") {
837       report->GetAsDictionary()->AddStringItem("location_filename",
838                                                location_filename);
839       report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
840     }
841 
842     bool all_addresses_are_same = true;
843     report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
844         [&all_addresses_are_same,
845          main_address](StructuredData::Object *o) -> bool {
846           addr_t addr =
847               o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
848           if (main_address != addr)
849             all_addresses_are_same = false;
850           return true;
851         });
852     report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
853                                               all_addresses_are_same);
854   }
855 
856   // Make sure this is the right process
857   if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
858     ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
859     if (thread_sp)
860       thread_sp->SetStopInfo(
861           InstrumentationRuntimeStopInfo::
862               CreateStopReasonWithInstrumentationData(
863                   *thread_sp, stop_reason_description, report));
864 
865     StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream();
866     s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
867              "info -s' to get extended information about the "
868              "report.\n");
869 
870     return true; // Return true to stop the target
871   } else
872     return false; // Let target run
873 }
874 
875 const RegularExpression &
876 InstrumentationRuntimeTSan::GetPatternForRuntimeLibrary() {
877   static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
878   return regex;
879 }
880 
881 bool InstrumentationRuntimeTSan::CheckIfRuntimeIsValid(
882     const lldb::ModuleSP module_sp) {
883   static ConstString g_tsan_get_current_report("__tsan_get_current_report");
884   const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
885       g_tsan_get_current_report, lldb::eSymbolTypeAny);
886   return symbol != nullptr;
887 }
888 
889 void InstrumentationRuntimeTSan::Activate() {
890   if (IsActive())
891     return;
892 
893   ProcessSP process_sp = GetProcessSP();
894   if (!process_sp)
895     return;
896 
897   ConstString symbol_name("__tsan_on_report");
898   const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
899       symbol_name, eSymbolTypeCode);
900 
901   if (symbol == nullptr)
902     return;
903 
904   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
905     return;
906 
907   Target &target = process_sp->GetTarget();
908   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
909 
910   if (symbol_address == LLDB_INVALID_ADDRESS)
911     return;
912 
913   bool internal = true;
914   bool hardware = false;
915   Breakpoint *breakpoint =
916       process_sp->GetTarget()
917           .CreateBreakpoint(symbol_address, internal, hardware)
918           .get();
919   breakpoint->SetCallback(InstrumentationRuntimeTSan::NotifyBreakpointHit, this,
920                           true);
921   breakpoint->SetBreakpointKind("thread-sanitizer-report");
922   SetBreakpointID(breakpoint->GetID());
923 
924   SetActive(true);
925 }
926 
927 void InstrumentationRuntimeTSan::Deactivate() {
928   if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
929     ProcessSP process_sp = GetProcessSP();
930     if (process_sp) {
931       process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
932       SetBreakpointID(LLDB_INVALID_BREAK_ID);
933     }
934   }
935   SetActive(false);
936 }
937 static std::string GenerateThreadName(const std::string &path,
938                                       StructuredData::Object *o,
939                                       StructuredData::ObjectSP main_info) {
940   std::string result = "additional information";
941 
942   if (path == "mops") {
943     int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue();
944     int thread_id =
945         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
946     bool is_write =
947         o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue();
948     bool is_atomic =
949         o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue();
950     addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
951 
952     std::string addr_string = Sprintf(" at 0x%llx", addr);
953 
954     if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same")
955             ->GetBooleanValue()) {
956       addr_string = "";
957     }
958 
959     if (main_info->GetObjectForDotSeparatedPath("issue_type")
960             ->GetStringValue() == "external-race") {
961       result = Sprintf("%s access by thread %d",
962                        is_write ? "mutating" : "read-only", thread_id);
963     } else if (main_info->GetObjectForDotSeparatedPath("issue_type")
964                    ->GetStringValue() == "swift-access-race") {
965       result = Sprintf("modifying access by thread %d", thread_id);
966     } else {
967       result = Sprintf("%s%s of size %d%s by thread %d",
968                        is_atomic ? "atomic " : "", is_write ? "write" : "read",
969                        size, addr_string.c_str(), thread_id);
970     }
971   }
972 
973   if (path == "threads") {
974     int thread_id =
975         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
976     result = Sprintf("Thread %d created", thread_id);
977   }
978 
979   if (path == "locs") {
980     std::string type = std::string(
981         o->GetAsDictionary()->GetValueForKey("type")->GetStringValue());
982     int thread_id =
983         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
984     int fd =
985         o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue();
986     if (type == "heap") {
987       result = Sprintf("Heap block allocated by thread %d", thread_id);
988     } else if (type == "fd") {
989       result =
990           Sprintf("File descriptor %d created by thread %t", fd, thread_id);
991     }
992   }
993 
994   if (path == "mutexes") {
995     int mutex_id =
996         o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue();
997 
998     result = Sprintf("Mutex M%d created", mutex_id);
999   }
1000 
1001   if (path == "stacks") {
1002     int thread_id =
1003         o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue();
1004     result = Sprintf("Thread %d", thread_id);
1005   }
1006 
1007   result[0] = toupper(result[0]);
1008 
1009   return result;
1010 }
1011 
1012 static void AddThreadsForPath(const std::string &path,
1013                               ThreadCollectionSP threads, ProcessSP process_sp,
1014                               StructuredData::ObjectSP info) {
1015   info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach(
1016       [process_sp, threads, path, info](StructuredData::Object *o) -> bool {
1017         std::vector<lldb::addr_t> pcs;
1018         o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach(
1019             [&pcs](StructuredData::Object *pc) -> bool {
1020               pcs.push_back(pc->GetAsInteger()->GetValue());
1021               return true;
1022             });
1023 
1024         if (pcs.size() == 0)
1025           return true;
1026 
1027         StructuredData::ObjectSP thread_id_obj =
1028             o->GetObjectForDotSeparatedPath("thread_os_id");
1029         tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
1030 
1031         HistoryThread *history_thread =
1032             new HistoryThread(*process_sp, tid, pcs);
1033         ThreadSP new_thread_sp(history_thread);
1034         new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str());
1035 
1036         // Save this in the Process' ExtendedThreadList so a strong pointer
1037         // retains the object
1038         process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
1039         threads->AddThread(new_thread_sp);
1040 
1041         return true;
1042       });
1043 }
1044 
1045 lldb::ThreadCollectionSP
1046 InstrumentationRuntimeTSan::GetBacktracesFromExtendedStopInfo(
1047     StructuredData::ObjectSP info) {
1048   ThreadCollectionSP threads;
1049   threads = std::make_shared<ThreadCollection>();
1050 
1051   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
1052           ->GetStringValue() != "ThreadSanitizer")
1053     return threads;
1054 
1055   ProcessSP process_sp = GetProcessSP();
1056 
1057   AddThreadsForPath("stacks", threads, process_sp, info);
1058   AddThreadsForPath("mops", threads, process_sp, info);
1059   AddThreadsForPath("locs", threads, process_sp, info);
1060   AddThreadsForPath("mutexes", threads, process_sp, info);
1061   AddThreadsForPath("threads", threads, process_sp, info);
1062 
1063   return threads;
1064 }
1065