1""" 2Tests basic UndefinedBehaviorSanitizer support (detecting an alignment error). 3""" 4 5import os 6import lldb 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test.decorators import * 9import lldbsuite.test.lldbutil as lldbutil 10import json 11 12 13class UbsanBasicTestCase(TestBase): 14 15 mydir = TestBase.compute_mydir(__file__) 16 17 @skipUnlessUndefinedBehaviorSanitizer 18 def test(self): 19 self.build() 20 self.ubsan_tests() 21 22 def setUp(self): 23 # Call super's setUp(). 24 TestBase.setUp(self) 25 self.line_align = line_number('main.c', '// align line') 26 27 def ubsan_tests(self): 28 # Load the test 29 exe = self.getBuildArtifact("a.out") 30 target = self.dbg.CreateTarget(exe) 31 self.assertTrue(target, VALID_TARGET) 32 self.registerSanitizerLibrariesWithTarget(target) 33 34 self.runCmd("run") 35 36 process = self.dbg.GetSelectedTarget().process 37 thread = process.GetSelectedThread() 38 frame = thread.GetSelectedFrame() 39 40 # the stop reason of the thread should be breakpoint. 41 self.expect("thread list", "A ubsan issue should be detected", 42 substrs=['stopped', 'stop reason =']) 43 44 stop_reason = thread.GetStopReason() 45 self.assertEqual(stop_reason, lldb.eStopReasonInstrumentation) 46 47 # test that the UBSan dylib is present 48 self.expect( 49 "image lookup -n __ubsan_on_report", 50 "__ubsan_on_report should be present", 51 substrs=['1 match found']) 52 53 # We should be stopped in __ubsan_on_report 54 self.assertTrue("__ubsan_on_report" in frame.GetFunctionName()) 55 56 # The stopped thread backtrace should contain either 'align line' 57 found = False 58 for i in range(thread.GetNumFrames()): 59 frame = thread.GetFrameAtIndex(i) 60 if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c": 61 if frame.GetLineEntry().GetLine() == self.line_align: 62 found = True 63 self.assertTrue(found) 64 65 backtraces = thread.GetStopReasonExtendedBacktraces( 66 lldb.eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer) 67 self.assertEquals(backtraces.GetSize(), 1) 68 69 self.expect( 70 "thread info -s", 71 "The extended stop info should contain the UBSan provided fields", 72 substrs=[ 73 "col", 74 "description", 75 "filename", 76 "instrumentation_class", 77 "line", 78 "memory_address", 79 ]) 80 81 output_lines = self.res.GetOutput().split('\n') 82 json_line = '\n'.join(output_lines[2:]) 83 data = json.loads(json_line) 84 85 self.assertEqual(data["instrumentation_class"], "UndefinedBehaviorSanitizer") 86 self.assertEqual(data["description"], "misaligned-pointer-use") 87 self.assertEqual(os.path.basename(data["filename"]), "main.c") 88 self.assertEqual(data["line"], self.line_align) 89 90 self.runCmd("continue") 91