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