1"""
2Test ThreadSanitizer when multiple different issues are found.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9import json
10
11
12class TsanMultipleTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    @expectedFailureAll(
17        oslist=["linux"],
18        bugnumber="non-core functionality, need to reenable and fix later (DES 2014.11.07)")
19    @expectedFailureNetBSD
20    @skipIfFreeBSD  # llvm.org/pr21136 runtimes not yet available by default
21    @skipIfRemote
22    @skipUnlessThreadSanitizer
23    @add_test_categories(["objc"])
24    def test(self):
25        self.build()
26        self.tsan_tests()
27
28    def tsan_tests(self):
29        exe = self.getBuildArtifact("a.out")
30        self.expect(
31            "file " + exe,
32            patterns=["Current executable set to .*a.out"])
33
34        self.runCmd("env TSAN_OPTIONS=abort_on_error=0")
35
36        self.runCmd("run")
37
38        stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
39        if stop_reason == lldb.eStopReasonExec:
40            # On OS X 10.10 and older, we need to re-exec to enable
41            # interceptors.
42            self.runCmd("continue")
43
44        report_count = 0
45        while self.dbg.GetSelectedTarget().process.GetSelectedThread(
46        ).GetStopReason() == lldb.eStopReasonInstrumentation:
47            report_count += 1
48
49            stop_description = self.dbg.GetSelectedTarget(
50            ).process.GetSelectedThread().GetStopDescription(100)
51
52            self.assertTrue(
53                (stop_description == "Data race detected") or
54                (stop_description == "Use of deallocated memory detected") or
55                (stop_description == "Thread leak detected") or
56                (stop_description == "Use of an uninitialized or destroyed mutex detected") or
57                (stop_description == "Unlock of an unlocked mutex (or by a wrong thread) detected")
58            )
59
60            self.expect(
61                "thread info -s",
62                "The extended stop info should contain the TSan provided fields",
63                substrs=[
64                    "instrumentation_class",
65                    "description",
66                    "mops"])
67
68            output_lines = self.res.GetOutput().split('\n')
69            json_line = '\n'.join(output_lines[2:])
70            data = json.loads(json_line)
71            self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
72
73            backtraces = self.dbg.GetSelectedTarget().process.GetSelectedThread(
74            ).GetStopReasonExtendedBacktraces(lldb.eInstrumentationRuntimeTypeThreadSanitizer)
75            self.assertTrue(backtraces.GetSize() >= 1)
76
77            self.runCmd("continue")
78
79        self.assertEqual(
80            self.dbg.GetSelectedTarget().process.GetState(),
81            lldb.eStateExited,
82            PROCESS_EXITED)
83