1//===-- TSanRuntime.cpp -----------------------------------------*- C++ -*-===//
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 "TSanRuntime.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
35using namespace lldb;
36using namespace lldb_private;
37
38lldb::InstrumentationRuntimeSP
39ThreadSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
40  return InstrumentationRuntimeSP(new ThreadSanitizerRuntime(process_sp));
41}
42
43void ThreadSanitizerRuntime::Initialize() {
44  PluginManager::RegisterPlugin(
45      GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.",
46      CreateInstance, GetTypeStatic);
47}
48
49void ThreadSanitizerRuntime::Terminate() {
50  PluginManager::UnregisterPlugin(CreateInstance);
51}
52
53lldb_private::ConstString ThreadSanitizerRuntime::GetPluginNameStatic() {
54  return ConstString("ThreadSanitizer");
55}
56
57lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() {
58  return eInstrumentationRuntimeTypeThreadSanitizer;
59}
60
61ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); }
62
63const char *thread_sanitizer_retrieve_report_data_prefix = R"(
64extern "C"
65{
66    void *__tsan_get_current_report();
67    int __tsan_get_report_data(void *report, const char **description, int *count,
68                               int *stack_count, int *mop_count, int *loc_count,
69                               int *mutex_count, int *thread_count,
70                               int *unique_tid_count, void **sleep_trace,
71                               unsigned long trace_size);
72    int __tsan_get_report_stack(void *report, unsigned long idx, void **trace,
73                                unsigned long trace_size);
74    int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr,
75                              int *size, int *write, int *atomic, void **trace,
76                              unsigned long trace_size);
77    int __tsan_get_report_loc(void *report, unsigned long idx, const char **type,
78                              void **addr, unsigned long *start, unsigned long *size, int *tid,
79                              int *fd, int *suppressable, void **trace,
80                              unsigned long trace_size);
81    int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr,
82                                int *destroyed, void **trace, unsigned long trace_size);
83    int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id,
84                                 int *running, const char **name, int *parent_tid,
85                                 void **trace, unsigned long trace_size);
86    int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid);
87
88    // TODO: dlsym won't work on Windows.
89    void *dlsym(void* handle, const char* symbol);
90    int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type);
91}
92
93const int REPORT_TRACE_SIZE = 128;
94const int REPORT_ARRAY_SIZE = 4;
95
96struct data {
97    void *report;
98    const char *description;
99    int report_count;
100
101    void *sleep_trace[REPORT_TRACE_SIZE];
102
103    int stack_count;
104    struct {
105        int idx;
106        void *trace[REPORT_TRACE_SIZE];
107    } stacks[REPORT_ARRAY_SIZE];
108
109    int mop_count;
110    struct {
111        int idx;
112        int tid;
113        int size;
114        int write;
115        int atomic;
116        void *addr;
117        void *trace[REPORT_TRACE_SIZE];
118    } mops[REPORT_ARRAY_SIZE];
119
120    int loc_count;
121    struct {
122        int idx;
123        const char *type;
124        void *addr;
125        unsigned long start;
126        unsigned long size;
127        int tid;
128        int fd;
129        int suppressable;
130        void *trace[REPORT_TRACE_SIZE];
131        const char *object_type;
132    } locs[REPORT_ARRAY_SIZE];
133
134    int mutex_count;
135    struct {
136        int idx;
137        unsigned long mutex_id;
138        void *addr;
139        int destroyed;
140        void *trace[REPORT_TRACE_SIZE];
141    } mutexes[REPORT_ARRAY_SIZE];
142
143    int thread_count;
144    struct {
145        int idx;
146        int tid;
147        unsigned long os_id;
148        int running;
149        const char *name;
150        int parent_tid;
151        void *trace[REPORT_TRACE_SIZE];
152    } threads[REPORT_ARRAY_SIZE];
153
154    int unique_tid_count;
155    struct {
156        int idx;
157        int tid;
158    } unique_tids[REPORT_ARRAY_SIZE];
159};
160)";
161
162const char *thread_sanitizer_retrieve_report_data_command = R"(
163data t = {0};
164
165ptr__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");
166
167t.report = __tsan_get_current_report();
168__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);
169
170if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
171for (int i = 0; i < t.stack_count; i++) {
172    t.stacks[i].idx = i;
173    __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE);
174}
175
176if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE;
177for (int i = 0; i < t.mop_count; i++) {
178    t.mops[i].idx = i;
179    __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);
180}
181
182if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE;
183for (int i = 0; i < t.loc_count; i++) {
184    t.locs[i].idx = i;
185    __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);
186    if (ptr__tsan_get_report_loc_object_type)
187        ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type);
188}
189
190if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE;
191for (int i = 0; i < t.mutex_count; i++) {
192    t.mutexes[i].idx = i;
193    __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);
194}
195
196if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE;
197for (int i = 0; i < t.thread_count; i++) {
198    t.threads[i].idx = i;
199    __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);
200}
201
202if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE;
203for (int i = 0; i < t.unique_tid_count; i++) {
204    t.unique_tids[i].idx = i;
205    __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid);
206}
207
208t;
209)";
210
211static StructuredData::Array *
212CreateStackTrace(ValueObjectSP o,
213                 const std::string &trace_item_name = ".trace") {
214  StructuredData::Array *trace = new StructuredData::Array();
215  ValueObjectSP trace_value_object =
216      o->GetValueForExpressionPath(trace_item_name.c_str());
217  size_t count = trace_value_object->GetNumChildren();
218  for (size_t j = 0; j < count; j++) {
219    addr_t trace_addr =
220        trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0);
221    if (trace_addr == 0)
222      break;
223    trace->AddItem(
224        StructuredData::ObjectSP(new StructuredData::Integer(trace_addr)));
225  }
226  return trace;
227}
228
229static StructuredData::Array *ConvertToStructuredArray(
230    ValueObjectSP return_value_sp, const std::string &items_name,
231    const std::string &count_name,
232    std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const
233        &callback) {
234  StructuredData::Array *array = new StructuredData::Array();
235  unsigned int count =
236      return_value_sp->GetValueForExpressionPath(count_name.c_str())
237          ->GetValueAsUnsigned(0);
238  ValueObjectSP objects =
239      return_value_sp->GetValueForExpressionPath(items_name.c_str());
240  for (unsigned int i = 0; i < count; i++) {
241    ValueObjectSP o = objects->GetChildAtIndex(i, true);
242    StructuredData::Dictionary *dict = new StructuredData::Dictionary();
243
244    callback(o, dict);
245
246    array->AddItem(StructuredData::ObjectSP(dict));
247  }
248  return array;
249}
250
251static std::string RetrieveString(ValueObjectSP return_value_sp,
252                                  ProcessSP process_sp,
253                                  const std::string &expression_path) {
254  addr_t ptr =
255      return_value_sp->GetValueForExpressionPath(expression_path.c_str())
256          ->GetValueAsUnsigned(0);
257  std::string str;
258  Status error;
259  process_sp->ReadCStringFromMemory(ptr, str, error);
260  return str;
261}
262
263static void
264GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data,
265                       std::map<uint64_t, user_id_t> &thread_id_map) {
266  ConvertToStructuredArray(
267      data, ".threads", ".thread_count",
268      [process_sp, &thread_id_map](ValueObjectSP o,
269                                   StructuredData::Dictionary *dict) {
270        uint64_t thread_id =
271            o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0);
272        uint64_t thread_os_id =
273            o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0);
274        user_id_t lldb_user_id = 0;
275
276        bool can_update = true;
277        ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID(
278            thread_os_id, can_update);
279        if (lldb_thread) {
280          lldb_user_id = lldb_thread->GetIndexID();
281        } else {
282          // This isn't a live thread anymore.  Ask process to assign a new
283          // Index ID (or return an old one if we've already seen this
284          // thread_os_id). It will also make sure that no new threads are
285          // assigned this Index ID.
286          lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id);
287        }
288
289        thread_id_map[thread_id] = lldb_user_id;
290      });
291}
292
293static user_id_t Renumber(uint64_t id,
294                          std::map<uint64_t, user_id_t> &thread_id_map) {
295  auto IT = thread_id_map.find(id);
296  if (IT == thread_id_map.end())
297    return 0;
298
299  return IT->second;
300}
301
302StructuredData::ObjectSP
303ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
304  ProcessSP process_sp = GetProcessSP();
305  if (!process_sp)
306    return StructuredData::ObjectSP();
307
308  ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
309  StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
310
311  if (!frame_sp)
312    return StructuredData::ObjectSP();
313
314  EvaluateExpressionOptions options;
315  options.SetUnwindOnError(true);
316  options.SetTryAllThreads(true);
317  options.SetStopOthers(true);
318  options.SetIgnoreBreakpoints(true);
319  options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
320  options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
321  options.SetAutoApplyFixIts(false);
322  options.SetLanguage(eLanguageTypeObjC_plus_plus);
323
324  ValueObjectSP main_value;
325  ExecutionContext exe_ctx;
326  Status eval_error;
327  frame_sp->CalculateExecutionContext(exe_ctx);
328  ExpressionResults result = UserExpression::Evaluate(
329      exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
330      main_value, eval_error);
331  if (result != eExpressionCompleted) {
332    process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
333        "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n",
334        eval_error.AsCString());
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
488std::string
489ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
490  std::string description = 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
533static 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 s.GetString();
540}
541
542static 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
556static 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(
568      lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
569
570  ModuleSP module = symbol->CalculateSymbolContextModule();
571  if (!module)
572    return;
573
574  VariableList var_list;
575  module->FindGlobalVariables(sym_name, nullptr, 1U, var_list);
576  if (var_list.GetSize() < 1)
577    return;
578
579  VariableSP var = var_list.GetVariableAtIndex(0);
580  decl = var->GetDeclaration();
581}
582
583addr_t ThreadSanitizerRuntime::GetFirstNonInternalFramePc(
584    StructuredData::ObjectSP trace, bool skip_one_frame) {
585  ProcessSP process_sp = GetProcessSP();
586  ModuleSP runtime_module_sp = GetRuntimeModuleSP();
587
588  StructuredData::Array *trace_array = trace->GetAsArray();
589  for (size_t i = 0; i < trace_array->GetSize(); i++) {
590    if (skip_one_frame && i == 0)
591      continue;
592
593    addr_t addr;
594    if (!trace_array->GetItemAtIndexAsInteger(i, addr))
595      continue;
596
597    lldb_private::Address so_addr;
598    if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
599            addr, so_addr))
600      continue;
601
602    if (so_addr.GetModule() == runtime_module_sp)
603      continue;
604
605    return addr;
606  }
607
608  return 0;
609}
610
611std::string
612ThreadSanitizerRuntime::GenerateSummary(StructuredData::ObjectSP report) {
613  ProcessSP process_sp = GetProcessSP();
614
615  std::string summary = report->GetAsDictionary()
616                            ->GetValueForKey("description")
617                            ->GetAsString()
618                            ->GetValue();
619  bool skip_one_frame =
620      report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
621      "external-race";
622
623  addr_t pc = 0;
624  if (report->GetAsDictionary()
625          ->GetValueForKey("mops")
626          ->GetAsArray()
627          ->GetSize() > 0)
628    pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
629                                        ->GetValueForKey("mops")
630                                        ->GetAsArray()
631                                        ->GetItemAtIndex(0)
632                                        ->GetAsDictionary()
633                                        ->GetValueForKey("trace"),
634                                    skip_one_frame);
635
636  if (report->GetAsDictionary()
637          ->GetValueForKey("stacks")
638          ->GetAsArray()
639          ->GetSize() > 0)
640    pc = GetFirstNonInternalFramePc(report->GetAsDictionary()
641                                        ->GetValueForKey("stacks")
642                                        ->GetAsArray()
643                                        ->GetItemAtIndex(0)
644                                        ->GetAsDictionary()
645                                        ->GetValueForKey("trace"),
646                                    skip_one_frame);
647
648  if (pc != 0) {
649    summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc);
650  }
651
652  if (report->GetAsDictionary()
653          ->GetValueForKey("locs")
654          ->GetAsArray()
655          ->GetSize() > 0) {
656    StructuredData::ObjectSP loc = report->GetAsDictionary()
657                                       ->GetValueForKey("locs")
658                                       ->GetAsArray()
659                                       ->GetItemAtIndex(0);
660    std::string object_type = loc->GetAsDictionary()
661                                  ->GetValueForKey("object_type")
662                                  ->GetAsString()
663                                  ->GetValue();
664    if (!object_type.empty()) {
665      summary = "Race on " + object_type + " object";
666    }
667    addr_t addr = loc->GetAsDictionary()
668                      ->GetValueForKey("address")
669                      ->GetAsInteger()
670                      ->GetValue();
671    if (addr == 0)
672      addr = loc->GetAsDictionary()
673                 ->GetValueForKey("start")
674                 ->GetAsInteger()
675                 ->GetValue();
676
677    if (addr != 0) {
678      std::string global_name = GetSymbolNameFromAddress(process_sp, addr);
679      if (!global_name.empty()) {
680        summary = summary + " at " + global_name;
681      } else {
682        summary = summary + " at " + Sprintf("0x%llx", addr);
683      }
684    } else {
685      int fd = loc->GetAsDictionary()
686                   ->GetValueForKey("file_descriptor")
687                   ->GetAsInteger()
688                   ->GetValue();
689      if (fd != 0) {
690        summary = summary + " on file descriptor " + Sprintf("%d", fd);
691      }
692    }
693  }
694
695  return summary;
696}
697
698addr_t
699ThreadSanitizerRuntime::GetMainRacyAddress(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 =
705            o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
706        if (addr < result)
707          result = addr;
708        return true;
709      });
710
711  return (result == (addr_t)-1) ? 0 : result;
712}
713
714std::string ThreadSanitizerRuntime::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 =
730        loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue();
731    if (type == "global") {
732      global_addr = loc->GetAsDictionary()
733                        ->GetValueForKey("address")
734                        ->GetAsInteger()
735                        ->GetValue();
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                        ->GetAsInteger()
754                        ->GetValue();
755      long size = loc->GetAsDictionary()
756                      ->GetValueForKey("size")
757                      ->GetAsInteger()
758                      ->GetValue();
759      std::string object_type = 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      int tid = loc->GetAsDictionary()
772                    ->GetValueForKey("thread_id")
773                    ->GetAsInteger()
774                    ->GetValue();
775      result = Sprintf("Location is stack of thread %d", tid);
776    } else if (type == "tls") {
777      int tid = loc->GetAsDictionary()
778                    ->GetValueForKey("thread_id")
779                    ->GetAsInteger()
780                    ->GetValue();
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                   ->GetAsInteger()
786                   ->GetValue();
787      result = Sprintf("Location is file descriptor %d", fd);
788    }
789  }
790
791  return result;
792}
793
794bool ThreadSanitizerRuntime::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  ThreadSanitizerRuntime *const instance =
802      static_cast<ThreadSanitizerRuntime *>(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  if (report) {
813    std::string issue_description = instance->FormatDescription(report);
814    report->GetAsDictionary()->AddStringItem("description", issue_description);
815    stop_reason_description = issue_description + " detected";
816    report->GetAsDictionary()->AddStringItem("stop_description",
817                                             stop_reason_description);
818    std::string summary = instance->GenerateSummary(report);
819    report->GetAsDictionary()->AddStringItem("summary", summary);
820    addr_t main_address = instance->GetMainRacyAddress(report);
821    report->GetAsDictionary()->AddIntegerItem("memory_address", main_address);
822
823    addr_t global_addr = 0;
824    std::string global_name = "";
825    std::string location_filename = "";
826    uint32_t location_line = 0;
827    std::string location_description = instance->GetLocationDescription(
828        report, global_addr, global_name, location_filename, location_line);
829    report->GetAsDictionary()->AddStringItem("location_description",
830                                             location_description);
831    if (global_addr != 0) {
832      report->GetAsDictionary()->AddIntegerItem("global_address", global_addr);
833    }
834    if (!global_name.empty()) {
835      report->GetAsDictionary()->AddStringItem("global_name", global_name);
836    }
837    if (location_filename != "") {
838      report->GetAsDictionary()->AddStringItem("location_filename",
839                                               location_filename);
840      report->GetAsDictionary()->AddIntegerItem("location_line", location_line);
841    }
842
843    bool all_addresses_are_same = true;
844    report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach(
845        [&all_addresses_are_same,
846         main_address](StructuredData::Object *o) -> bool {
847          addr_t addr =
848              o->GetObjectForDotSeparatedPath("address")->GetIntegerValue();
849          if (main_address != addr)
850            all_addresses_are_same = false;
851          return true;
852        });
853    report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same",
854                                              all_addresses_are_same);
855  }
856
857  // Make sure this is the right process
858  if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
859    ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
860    if (thread_sp)
861      thread_sp->SetStopInfo(
862          InstrumentationRuntimeStopInfo::
863              CreateStopReasonWithInstrumentationData(
864                  *thread_sp, stop_reason_description, report));
865
866    StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream();
867    s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
868             "info -s' to get extended information about the "
869             "report.\n");
870
871    return true; // Return true to stop the target
872  } else
873    return false; // Let target run
874}
875
876const RegularExpression &ThreadSanitizerRuntime::GetPatternForRuntimeLibrary() {
877  static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_"));
878  return regex;
879}
880
881bool ThreadSanitizerRuntime::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
889void ThreadSanitizerRuntime::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(ThreadSanitizerRuntime::NotifyBreakpointHit, this,
920                          true);
921  breakpoint->SetBreakpointKind("thread-sanitizer-report");
922  SetBreakpointID(breakpoint->GetID());
923
924  SetActive(true);
925}
926
927void ThreadSanitizerRuntime::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}
937static 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 =
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
1012static 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
1045lldb::ThreadCollectionSP
1046ThreadSanitizerRuntime::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