[Lldb-commits] [lldb] r370286 - [TSanRuntime] Upstream thread swift race detector.

Davide Italiano via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 28 18:39:26 PDT 2019


Author: davide
Date: Wed Aug 28 18:39:26 2019
New Revision: 370286

URL: http://llvm.org/viewvc/llvm-project?rev=370286&view=rev
Log:
[TSanRuntime] Upstream thread swift race detector.

Summary:
This is self-contained, and doesn't need anything in the
compiler to work. Mainly to reduce the diff between upstream
and downstream.

Patch by Kuba Mracek!

Reviewers: kubamracek

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D66915

Modified:
    lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.cpp
    lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.h

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.cpp?rev=370286&r1=370285&r2=370286&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.cpp (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.cpp Wed Aug 28 18:39:26 2019
@@ -88,6 +88,7 @@ extern "C"
     // TODO: dlsym won't work on Windows.
     void *dlsym(void* handle, const char* symbol);
     int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type);
+    int (*ptr__tsan_get_report_tag)(void *report, unsigned long *tag);
 }
 
 const int REPORT_TRACE_SIZE = 128;
@@ -97,6 +98,7 @@ struct data {
     void *report;
     const char *description;
     int report_count;
+    unsigned long tag;
     
     void *sleep_trace[REPORT_TRACE_SIZE];
     
@@ -163,10 +165,14 @@ const char *thread_sanitizer_retrieve_re
 data t = {0};
 
 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");
+ptr__tsan_get_report_tag = (typeof(ptr__tsan_get_report_tag))(void *)dlsym((void*)-2 /*RTLD_DEFAULT*/, "__tsan_get_report_tag");
 
 t.report = __tsan_get_current_report();
 __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);
 
+if (ptr__tsan_get_report_tag)
+    ptr__tsan_get_report_tag(t.report, &t.tag);
+
 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE;
 for (int i = 0; i < t.stack_count; i++) {
     t.stacks[i].idx = i;
@@ -347,6 +353,9 @@ ThreadSanitizerRuntime::RetrieveReportDa
                            ->GetValueAsUnsigned(0));
   dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace(
                                    main_value, ".sleep_trace")));
+  dict->AddIntegerItem(
+      "tag",
+      main_value->GetValueForExpressionPath(".tag")->GetValueAsUnsigned(0));
 
   StructuredData::Array *stacks = ConvertToStructuredArray(
       main_value, ".stacks", ".stack_count",
@@ -485,8 +494,8 @@ ThreadSanitizerRuntime::RetrieveReportDa
   return StructuredData::ObjectSP(dict);
 }
 
-std::string
-ThreadSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
+std::string ThreadSanitizerRuntime::FormatDescription(
+    StructuredData::ObjectSP report, bool &is_swift_access_race) {
   std::string description = report->GetAsDictionary()
                                 ->GetValueForKey("issue_type")
                                 ->GetAsString()
@@ -521,8 +530,18 @@ ThreadSanitizerRuntime::FormatDescriptio
   } else if (description == "lock-order-inversion") {
     return "Lock order inversion (potential deadlock)";
   } else if (description == "external-race") {
+    auto tag = report->GetAsDictionary()
+                   ->GetValueForKey("tag")
+                   ->GetAsInteger()
+                   ->GetValue();
+    static const unsigned long kSwiftAccessRaceTag = 0x1;
+    if (tag == kSwiftAccessRaceTag) {
+      is_swift_access_race = true;
+      return "Swift access race";
+    }
     return "Race on a library object";
   } else if (description == "swift-access-race") {
+    is_swift_access_race = true;
     return "Swift access race";
   }
 
@@ -616,9 +635,14 @@ ThreadSanitizerRuntime::GenerateSummary(
                             ->GetValueForKey("description")
                             ->GetAsString()
                             ->GetValue();
+  bool is_swift_access_race = report->GetAsDictionary()
+                                  ->GetValueForKey("is_swift_access_race")
+                                  ->GetAsBoolean()
+                                  ->GetValue();
+
   bool skip_one_frame =
-      report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
-      "external-race";
+      (report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() ==
+      "external-race") && (!is_swift_access_race);
 
   addr_t pc = 0;
   if (report->GetAsDictionary()
@@ -810,8 +834,12 @@ bool ThreadSanitizerRuntime::NotifyBreak
       instance->RetrieveReportData(context->exe_ctx_ref);
   std::string stop_reason_description;
   if (report) {
-    std::string issue_description = instance->FormatDescription(report);
+    bool is_swift_access_race = false;
+    std::string issue_description =
+        instance->FormatDescription(report, is_swift_access_race);
     report->GetAsDictionary()->AddStringItem("description", issue_description);
+    report->GetAsDictionary()->AddBooleanItem("is_swift_access_race",
+                                              is_swift_access_race);
     stop_reason_description = issue_description + " detected";
     report->GetAsDictionary()->AddStringItem("stop_description",
                                              stop_reason_description);

Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.h?rev=370286&r1=370285&r2=370286&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.h (original)
+++ lldb/trunk/source/Plugins/InstrumentationRuntime/TSan/TSanRuntime.h Wed Aug 28 18:39:26 2019
@@ -61,7 +61,8 @@ private:
 
   StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref);
 
-  std::string FormatDescription(StructuredData::ObjectSP report);
+  std::string FormatDescription(StructuredData::ObjectSP report,
+                                bool &is_swift_access_race);
 
   std::string GenerateSummary(StructuredData::ObjectSP report);
 




More information about the lldb-commits mailing list