1""" 2Tests basic ThreadSanitizer support (detecting a data race). 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 TsanBasicTestCase(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 setUp(self): 28 # Call super's setUp(). 29 TestBase.setUp(self) 30 self.line_malloc = line_number('main.c', '// malloc line') 31 self.line_thread1 = line_number('main.c', '// thread1 line') 32 self.line_thread2 = line_number('main.c', '// thread2 line') 33 34 def tsan_tests(self): 35 exe = self.getBuildArtifact("a.out") 36 self.expect( 37 "file " + exe, 38 patterns=["Current executable set to .*a.out"]) 39 40 self.runCmd("run") 41 42 stop_reason = self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason() 43 if stop_reason == lldb.eStopReasonExec: 44 # On OS X 10.10 and older, we need to re-exec to enable 45 # interceptors. 46 self.runCmd("continue") 47 48 # the stop reason of the thread should be breakpoint. 49 self.expect("thread list", "A data race should be detected", 50 substrs=['stopped', 'stop reason = Data race detected']) 51 52 self.assertEqual( 53 self.dbg.GetSelectedTarget().process.GetSelectedThread().GetStopReason(), 54 lldb.eStopReasonInstrumentation) 55 56 # test that the TSan dylib is present 57 self.expect( 58 "image lookup -n __tsan_get_current_report", 59 "__tsan_get_current_report should be present", 60 substrs=['1 match found']) 61 62 # We should be stopped in __tsan_on_report 63 process = self.dbg.GetSelectedTarget().process 64 thread = process.GetSelectedThread() 65 frame = thread.GetSelectedFrame() 66 self.assertTrue("__tsan_on_report" in frame.GetFunctionName()) 67 68 # The stopped thread backtrace should contain either line1 or line2 69 # from main.c. 70 found = False 71 for i in range(0, thread.GetNumFrames()): 72 frame = thread.GetFrameAtIndex(i) 73 if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c": 74 if frame.GetLineEntry().GetLine() == self.line_thread1: 75 found = True 76 if frame.GetLineEntry().GetLine() == self.line_thread2: 77 found = True 78 self.assertTrue(found) 79 80 self.expect( 81 "thread info -s", 82 "The extended stop info should contain the TSan provided fields", 83 substrs=[ 84 "instrumentation_class", 85 "description", 86 "mops"]) 87 88 output_lines = self.res.GetOutput().split('\n') 89 json_line = '\n'.join(output_lines[2:]) 90 data = json.loads(json_line) 91 self.assertEqual(data["instrumentation_class"], "ThreadSanitizer") 92 self.assertEqual(data["issue_type"], "data-race") 93 self.assertEqual(len(data["mops"]), 2) 94 95 backtraces = thread.GetStopReasonExtendedBacktraces( 96 lldb.eInstrumentationRuntimeTypeAddressSanitizer) 97 self.assertEqual(backtraces.GetSize(), 0) 98 99 backtraces = thread.GetStopReasonExtendedBacktraces( 100 lldb.eInstrumentationRuntimeTypeThreadSanitizer) 101 self.assertTrue(backtraces.GetSize() >= 2) 102 103 # First backtrace is a memory operation 104 thread = backtraces.GetThreadAtIndex(0) 105 found = False 106 for i in range(0, thread.GetNumFrames()): 107 frame = thread.GetFrameAtIndex(i) 108 if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c": 109 if frame.GetLineEntry().GetLine() == self.line_thread1: 110 found = True 111 if frame.GetLineEntry().GetLine() == self.line_thread2: 112 found = True 113 self.assertTrue(found) 114 115 # Second backtrace is a memory operation 116 thread = backtraces.GetThreadAtIndex(1) 117 found = False 118 for i in range(0, thread.GetNumFrames()): 119 frame = thread.GetFrameAtIndex(i) 120 if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c": 121 if frame.GetLineEntry().GetLine() == self.line_thread1: 122 found = True 123 if frame.GetLineEntry().GetLine() == self.line_thread2: 124 found = True 125 self.assertTrue(found) 126 127 self.runCmd("continue") 128 129 # the stop reason of the thread should be a SIGABRT. 130 self.expect("thread list", "We should be stopped due a SIGABRT", 131 substrs=['stopped', 'stop reason = signal SIGABRT']) 132 133 # test that we're in pthread_kill now (TSan abort the process) 134 self.expect("thread list", "We should be stopped in pthread_kill", 135 substrs=['pthread_kill']) 136