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    def test(self):
24        self.build()
25        self.tsan_tests()
26
27    def tsan_tests(self):
28        exe = self.getBuildArtifact("a.out")
29        self.expect(
30            "file " + exe,
31            patterns=["Current executable set to .*a.out"])
32
33        self.runCmd("env TSAN_OPTIONS=abort_on_error=0")
34
35        self.runCmd("run")
36
37        stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason()
38        if stop_reason == lldb.eStopReasonExec:
39            # On OS X 10.10 and older, we need to re-exec to enable
40            # interceptors.
41            self.runCmd("continue")
42
43        report_count = 0
44        while self.dbg.GetSelectedTarget().process.GetSelectedThread(
45        ).GetStopReason() == lldb.eStopReasonInstrumentation:
46            report_count += 1
47
48            stop_description = self.dbg.GetSelectedTarget(
49            ).process.GetSelectedThread().GetStopDescription(100)
50
51            self.assertTrue(
52                (stop_description == "Data race detected") or
53                (stop_description == "Use of deallocated memory detected") or
54                (stop_description == "Thread leak detected") or
55                (stop_description == "Use of an uninitialized or destroyed mutex detected") or
56                (stop_description == "Unlock of an unlocked mutex (or by a wrong thread) detected")
57            )
58
59            self.expect(
60                "thread info -s",
61                "The extended stop info should contain the TSan provided fields",
62                substrs=[
63                    "instrumentation_class",
64                    "description",
65                    "mops"])
66
67            output_lines = self.res.GetOutput().split('\n')
68            json_line = '\n'.join(output_lines[2:])
69            data = json.loads(json_line)
70            self.assertEqual(data["instrumentation_class"], "ThreadSanitizer")
71
72            backtraces = self.dbg.GetSelectedTarget().process.GetSelectedThread(
73            ).GetStopReasonExtendedBacktraces(lldb.eInstrumentationRuntimeTypeThreadSanitizer)
74            self.assertTrue(backtraces.GetSize() >= 1)
75
76            self.runCmd("continue")
77
78        self.assertEqual(
79            self.dbg.GetSelectedTarget().process.GetState(),
80            lldb.eStateExited,
81            PROCESS_EXITED)
82