summaryrefslogtreecommitdiffstats
path: root/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan
diff options
context:
space:
mode:
authorpatrick <patrick@openbsd.org>2020-08-03 14:33:06 +0000
committerpatrick <patrick@openbsd.org>2020-08-03 14:33:06 +0000
commit061da546b983eb767bad15e67af1174fb0bcf31c (patch)
tree83c78b820819d70aa40c36d90447978b300078c5 /gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan
parentImport LLVM 10.0.0 release including clang, lld and lldb. (diff)
downloadwireguard-openbsd-061da546b983eb767bad15e67af1174fb0bcf31c.tar.xz
wireguard-openbsd-061da546b983eb767bad15e67af1174fb0bcf31c.zip
Import LLVM 10.0.0 release including clang, lld and lldb.
ok hackroom tested by plenty
Diffstat (limited to 'gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan')
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/TestTsanBasic.py135
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/main.c36
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/TestTsanCPPGlobalLocation.py64
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/main.cpp37
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/TestTsanGlobalLocation.py64
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/main.c37
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/TestTsanMultiple.py81
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/main.m137
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/TestTsanThreadLeak.py46
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/main.c23
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/Makefile4
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/TestTsanThreadNumbers.py82
-rw-r--r--gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/main.c57
18 files changed, 823 insertions, 0 deletions
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/Makefile
new file mode 100644
index 00000000000..07133cff358
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/TestTsanBasic.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/TestTsanBasic.py
new file mode 100644
index 00000000000..1b71de5d273
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/TestTsanBasic.py
@@ -0,0 +1,135 @@
+"""
+Tests basic ThreadSanitizer support (detecting a data race).
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+import json
+
+
+class TsanBasicTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def setUp(self):
+ # Call super's setUp().
+ TestBase.setUp(self)
+ self.line_malloc = line_number('main.c', '// malloc line')
+ self.line_thread1 = line_number('main.c', '// thread1 line')
+ self.line_thread2 = line_number('main.c', '// thread2 line')
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be breakpoint.
+ self.expect("thread list", "A data race should be detected",
+ substrs=['stopped', 'stop reason = Data race detected'])
+
+ self.assertEqual(
+ self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
+ lldb.eStopReasonInstrumentation)
+
+ # test that the TSan dylib is present
+ self.expect(
+ "image lookup -n __tsan_get_current_report",
+ "__tsan_get_current_report should be present",
+ substrs=['1 match found'])
+
+ # We should be stopped in __tsan_on_report
+ process = self.dbg.GetSelectedTarget().process
+ thread = process.GetSelectedThread()
+ frame = thread.GetSelectedFrame()
+ self.assertTrue("__tsan_on_report" in frame.GetFunctionName())
+
+ # The stopped thread backtrace should contain either line1 or line2
+ # from main.c.
+ found = False
+ for i in range(0, thread.GetNumFrames()):
+ frame = thread.GetFrameAtIndex(i)
+ if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c":
+ if frame.GetLineEntry().GetLine() == self.line_thread1:
+ found = True
+ if frame.GetLineEntry().GetLine() == self.line_thread2:
+ found = True
+ self.assertTrue(found)
+
+ self.expect(
+ "thread info -s",
+ "The extended stop info should contain the TSan provided fields",
+ substrs=[
+ "instrumentation_class",
+ "description",
+ "mops"])
+
+ output_lines = self.res.GetOutput().split('\n')
+ json_line = '\n'.join(output_lines[2:])
+ data = json.loads(json_line)
+ self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
+ self.assertEqual(data["issue_type"], "data-race")
+ self.assertEqual(len(data["mops"]), 2)
+
+ backtraces = thread.GetStopReasonExtendedBacktraces(
+ lldb.eInstrumentationRuntimeTypeAddressSanitizer)
+ self.assertEqual(backtraces.GetSize(), 0)
+
+ backtraces = thread.GetStopReasonExtendedBacktraces(
+ lldb.eInstrumentationRuntimeTypeThreadSanitizer)
+ self.assertTrue(backtraces.GetSize() >= 2)
+
+ # First backtrace is a memory operation
+ thread = backtraces.GetThreadAtIndex(0)
+ found = False
+ for i in range(0, thread.GetNumFrames()):
+ frame = thread.GetFrameAtIndex(i)
+ if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c":
+ if frame.GetLineEntry().GetLine() == self.line_thread1:
+ found = True
+ if frame.GetLineEntry().GetLine() == self.line_thread2:
+ found = True
+ self.assertTrue(found)
+
+ # Second backtrace is a memory operation
+ thread = backtraces.GetThreadAtIndex(1)
+ found = False
+ for i in range(0, thread.GetNumFrames()):
+ frame = thread.GetFrameAtIndex(i)
+ if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c":
+ if frame.GetLineEntry().GetLine() == self.line_thread1:
+ found = True
+ if frame.GetLineEntry().GetLine() == self.line_thread2:
+ found = True
+ self.assertTrue(found)
+
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be a SIGABRT.
+ self.expect("thread list", "We should be stopped due a SIGABRT",
+ substrs=['stopped', 'stop reason = signal SIGABRT'])
+
+ # test that we're in pthread_kill now (TSan abort the process)
+ self.expect("thread list", "We should be stopped in pthread_kill",
+ substrs=['pthread_kill'])
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/main.c b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/main.c
new file mode 100644
index 00000000000..108ae74d4aa
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/basic/main.c
@@ -0,0 +1,36 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+char *pointer;
+
+void *f1(void *p) {
+ pointer[0] = 'x'; // thread1 line
+ return NULL;
+}
+
+void *f2(void *p) {
+ pointer[0] = 'y'; // thread2 line
+ return NULL;
+}
+
+int main (int argc, char const *argv[])
+{
+ pointer = (char *)malloc(10); // malloc line
+
+ pthread_t t1, t2;
+ pthread_create(&t1, NULL, f1, NULL);
+ pthread_create(&t2, NULL, f2, NULL);
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ return 0;
+}
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/Makefile
new file mode 100644
index 00000000000..5f671971d59
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/Makefile
@@ -0,0 +1,4 @@
+CXX_SOURCES := main.cpp
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/TestTsanCPPGlobalLocation.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/TestTsanCPPGlobalLocation.py
new file mode 100644
index 00000000000..407126d01d3
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/TestTsanCPPGlobalLocation.py
@@ -0,0 +1,64 @@
+"""
+Tests that TSan correctly reports the filename and line number of a racy global C++ variable.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+import json
+
+
+class TsanCPPGlobalLocationTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be breakpoint.
+ self.expect("thread list", "A data race should be detected",
+ substrs=['stopped', 'stop reason = Data race detected'])
+
+ self.expect(
+ "thread info -s",
+ "The extended stop info should contain the TSan provided fields",
+ substrs=[
+ "instrumentation_class",
+ "description",
+ "mops"])
+
+ output_lines = self.res.GetOutput().split('\n')
+ json_line = '\n'.join(output_lines[2:])
+ data = json.loads(json_line)
+ self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
+ self.assertEqual(data["issue_type"], "data-race")
+
+ self.assertTrue(data["location_filename"].endswith("/main.cpp"))
+ self.assertEqual(
+ data["location_line"],
+ line_number(
+ 'main.cpp',
+ '// global variable'))
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/main.cpp b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/main.cpp
new file mode 100644
index 00000000000..04735ef86ff
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/cpp_global_location/main.cpp
@@ -0,0 +1,37 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+long my_global_variable; // global variable
+
+void *f1(void *p) {
+ my_global_variable = 42;
+ return NULL;
+}
+
+void *f2(void *p) {
+ my_global_variable = 43;
+ return NULL;
+}
+
+int main (int argc, char const *argv[])
+{
+ pthread_t t1;
+ pthread_create(&t1, NULL, f1, NULL);
+
+ pthread_t t2;
+ pthread_create(&t2, NULL, f2, NULL);
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ return 0;
+}
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/Makefile
new file mode 100644
index 00000000000..07133cff358
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/TestTsanGlobalLocation.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/TestTsanGlobalLocation.py
new file mode 100644
index 00000000000..7bfd90b0af3
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/TestTsanGlobalLocation.py
@@ -0,0 +1,64 @@
+"""
+Tests that TSan correctly reports the filename and line number of a racy global variable.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+import json
+
+
+class TsanGlobalLocationTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be breakpoint.
+ self.expect("thread list", "A data race should be detected",
+ substrs=['stopped', 'stop reason = Data race detected'])
+
+ self.expect(
+ "thread info -s",
+ "The extended stop info should contain the TSan provided fields",
+ substrs=[
+ "instrumentation_class",
+ "description",
+ "mops"])
+
+ output_lines = self.res.GetOutput().split('\n')
+ json_line = '\n'.join(output_lines[2:])
+ data = json.loads(json_line)
+ self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
+ self.assertEqual(data["issue_type"], "data-race")
+
+ self.assertTrue(data["location_filename"].endswith("/main.c"))
+ self.assertEqual(
+ data["location_line"],
+ line_number(
+ 'main.c',
+ '// global variable'))
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/main.c b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/main.c
new file mode 100644
index 00000000000..4c88d9f0cc0
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/global_location/main.c
@@ -0,0 +1,37 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+long my_global_variable; // global variable
+
+void *f1(void *p) {
+ my_global_variable = 42;
+ return NULL;
+}
+
+void *f2(void *p) {
+ my_global_variable = 43;
+ return NULL;
+}
+
+int main (int argc, char const *argv[])
+{
+ pthread_t t1;
+ pthread_create(&t1, NULL, f1, NULL);
+
+ pthread_t t2;
+ pthread_create(&t2, NULL, f2, NULL);
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ return 0;
+}
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/Makefile
new file mode 100644
index 00000000000..07133cff358
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/TestTsanMultiple.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/TestTsanMultiple.py
new file mode 100644
index 00000000000..b40f950eda5
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/TestTsanMultiple.py
@@ -0,0 +1,81 @@
+"""
+Test ThreadSanitizer when multiple different issues are found.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+import json
+
+
+class TsanMultipleTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("env TSAN_OPTIONS=abort_on_error=0")
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ report_count = 0
+ while self.dbg.GetSelectedTarget().process.GetSelectedThread(
+ ).GetStopReason() == lldb.eStopReasonInstrumentation:
+ report_count += 1
+
+ stop_description = self.dbg.GetSelectedTarget(
+ ).process.GetSelectedThread().GetStopDescription(100)
+
+ self.assertTrue(
+ (stop_description == "Data race detected") or
+ (stop_description == "Use of deallocated memory detected") or
+ (stop_description == "Thread leak detected") or
+ (stop_description == "Use of an uninitialized or destroyed mutex detected") or
+ (stop_description == "Unlock of an unlocked mutex (or by a wrong thread) detected")
+ )
+
+ self.expect(
+ "thread info -s",
+ "The extended stop info should contain the TSan provided fields",
+ substrs=[
+ "instrumentation_class",
+ "description",
+ "mops"])
+
+ output_lines = self.res.GetOutput().split('\n')
+ json_line = '\n'.join(output_lines[2:])
+ data = json.loads(json_line)
+ self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
+
+ backtraces = self.dbg.GetSelectedTarget().process.GetSelectedThread(
+ ).GetStopReasonExtendedBacktraces(lldb.eInstrumentationRuntimeTypeThreadSanitizer)
+ self.assertTrue(backtraces.GetSize() >= 1)
+
+ self.runCmd("continue")
+
+ self.assertEqual(
+ self.dbg.GetSelectedTarget().process.GetState(),
+ lldb.eStateExited,
+ PROCESS_EXITED)
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/main.m b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/main.m
new file mode 100644
index 00000000000..d15bdaa3300
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/multiple/main.m
@@ -0,0 +1,137 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#import <Foundation/Foundation.h>
+#import <pthread.h>
+
+long my_global;
+
+void *Thread1(void *arg) {
+ my_global = 42;
+ return NULL;
+}
+
+void *Thread2(void *arg) {
+ my_global = 144;
+ return NULL;
+}
+
+void TestDataRace1() {
+ pthread_t t1, t2;
+ pthread_create(&t1, NULL, Thread1, NULL);
+ pthread_create(&t2, NULL, Thread2, NULL);
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+}
+
+void TestInvalidMutex() {
+ pthread_mutex_t m = {0};
+ pthread_mutex_lock(&m);
+
+ pthread_mutex_init(&m, NULL);
+ pthread_mutex_lock(&m);
+ pthread_mutex_unlock(&m);
+ pthread_mutex_destroy(&m);
+ pthread_mutex_lock(&m);
+}
+
+void TestMutexWrongLock() {
+ pthread_mutex_t m = {0};
+ pthread_mutex_init(&m, NULL);
+ pthread_mutex_unlock(&m);
+}
+
+long some_global;
+
+void TestDataRaceBlocks1() {
+ dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
+
+ for (int i = 0; i < 2; i++) {
+ dispatch_async(q, ^{
+ some_global++; // race 1
+
+ usleep(100000); // force the blocks to be on different threads
+ });
+ }
+
+ usleep(100000);
+ dispatch_barrier_sync(q, ^{ });
+}
+
+void TestDataRaceBlocks2() {
+ dispatch_queue_t q = dispatch_queue_create("my.queue2", DISPATCH_QUEUE_CONCURRENT);
+
+ char *c;
+
+ c = malloc((rand() % 1000) + 10);
+ for (int i = 0; i < 2; i++) {
+ dispatch_async(q, ^{
+ c[0] = 'x'; // race 2
+ fprintf(stderr, "tid: %p\n", pthread_self());
+ usleep(100000); // force the blocks to be on different threads
+ });
+ }
+ dispatch_barrier_sync(q, ^{ });
+
+ free(c);
+}
+
+void TestUseAfterFree() {
+ char *c;
+
+ c = malloc((rand() % 1000) + 10);
+ free(c);
+ c[0] = 'x';
+}
+
+void TestRacePipe() {
+ dispatch_queue_t q = dispatch_queue_create("my.queue3", DISPATCH_QUEUE_CONCURRENT);
+
+ int a[2];
+ pipe(a);
+ int fd = a[0];
+
+ for (int i = 0; i < 2; i++) {
+ dispatch_async(q, ^{
+ write(fd, "abc", 3);
+ usleep(100000); // force the blocks to be on different threads
+ });
+ dispatch_async(q, ^{
+ close(fd);
+ usleep(100000);
+ });
+ }
+
+ dispatch_barrier_sync(q, ^{ });
+}
+
+void TestThreadLeak() {
+ pthread_t t1;
+ pthread_create(&t1, NULL, Thread1, NULL);
+}
+
+int main(int argc, const char * argv[]) {
+ TestDataRace1();
+
+ TestInvalidMutex();
+
+ TestMutexWrongLock();
+
+ TestDataRaceBlocks1();
+
+ TestDataRaceBlocks2();
+
+ TestUseAfterFree();
+
+ TestRacePipe();
+
+ TestThreadLeak();
+
+ return 0;
+}
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/Makefile
new file mode 100644
index 00000000000..07133cff358
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/TestTsanThreadLeak.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/TestTsanThreadLeak.py
new file mode 100644
index 00000000000..09704e40cd3
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/TestTsanThreadLeak.py
@@ -0,0 +1,46 @@
+"""
+Tests ThreadSanitizer's support to detect a leaked thread.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+
+
+class TsanThreadLeakTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be breakpoint.
+ self.expect("thread list", "A thread leak should be detected",
+ substrs=['stopped', 'stop reason = Thread leak detected'])
+
+ self.assertEqual(
+ self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
+ lldb.eStopReasonInstrumentation)
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/main.c b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/main.c
new file mode 100644
index 00000000000..0c3a2cf84f5
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_leak/main.c
@@ -0,0 +1,23 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void *f1(void *p) {
+ printf("hello\n");
+ return NULL;
+}
+
+int main (int argc, char const *argv[])
+{
+ pthread_t t1;
+ pthread_create(&t1, NULL, f1, NULL);
+
+ return 0;
+}
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/Makefile
new file mode 100644
index 00000000000..07133cff358
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -fsanitize=thread -g
+
+include Makefile.rules
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/TestTsanThreadNumbers.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/TestTsanThreadNumbers.py
new file mode 100644
index 00000000000..61870eac9c4
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/TestTsanThreadNumbers.py
@@ -0,0 +1,82 @@
+"""
+Tests that TSan and LLDB have correct thread numbers.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+import json
+
+
+class TsanThreadNumbersTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @expectedFailureAll(
+ oslist=["linux"],
+ bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
+ @expectedFailureNetBSD
+ @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
+ @skipIfRemote
+ @skipUnlessThreadSanitizer
+ def test(self):
+ self.build()
+ self.tsan_tests()
+
+ def tsan_tests(self):
+ exe = self.getBuildArtifact("a.out")
+ self.expect(
+ "file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.runCmd("run")
+
+ stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
+ if stop_reason == lldb.eStopReasonExec:
+ # On OS X 10.10 and older, we need to re-exec to enable
+ # interceptors.
+ self.runCmd("continue")
+
+ # the stop reason of the thread should be breakpoint.
+ self.expect("thread list", "A data race should be detected",
+ substrs=['stopped', 'stop reason = Data race detected'])
+
+ self.assertEqual(
+ self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(),
+ lldb.eStopReasonInstrumentation)
+
+ report_thread_id = self.dbg.GetSelectedTarget(
+ ).process.GetSelectedThread().GetIndexID()
+
+ self.expect(
+ "thread info -s",
+ "The extended stop info should contain the TSan provided fields",
+ substrs=[
+ "instrumentation_class",
+ "description",
+ "mops"])
+
+ output_lines = self.res.GetOutput().split('\n')
+ json_line = '\n'.join(output_lines[2:])
+ data = json.loads(json_line)
+ self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
+ self.assertEqual(data["issue_type"], "data-race")
+ self.assertEqual(len(data["mops"]), 2)
+
+ self.assertEqual(data["mops"][0]["thread_id"], report_thread_id)
+
+ other_thread_id = data["mops"][1]["thread_id"]
+ self.assertTrue(other_thread_id != report_thread_id)
+ other_thread = self.dbg.GetSelectedTarget(
+ ).process.GetThreadByIndexID(other_thread_id)
+ self.assertTrue(other_thread.IsValid())
+
+ self.runCmd("thread select %d" % other_thread_id)
+
+ self.expect(
+ "thread backtrace",
+ "The other thread should be stopped in f1 or f2",
+ substrs=[
+ "a.out",
+ "main.c"])
diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/main.c b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/main.c
new file mode 100644
index 00000000000..5bc82131402
--- /dev/null
+++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/functionalities/tsan/thread_numbers/main.c
@@ -0,0 +1,57 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char *pointer;
+
+void *nothing(void *p) {
+ return NULL;
+}
+
+void *f1(void *p) {
+ pointer[0] = 'x';
+ sleep(100);
+ return NULL;
+}
+
+void *f2(void *p) {
+ pointer[0] = 'y';
+ sleep(100);
+ return NULL;
+}
+
+int main (int argc, char const *argv[])
+{
+ pointer = (char *)malloc(10);
+
+ for (int i = 0; i < 3; i++) {
+ pthread_t t;
+ pthread_create(&t, NULL, nothing, NULL);
+ pthread_join(t, NULL);
+ }
+
+ pthread_t t1;
+ pthread_create(&t1, NULL, f1, NULL);
+
+ for (int i = 0; i < 3; i++) {
+ pthread_t t;
+ pthread_create(&t, NULL, nothing, NULL);
+ pthread_join(t, NULL);
+ }
+
+ pthread_t t2;
+ pthread_create(&t2, NULL, f2, NULL);
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ return 0;
+}