105 lines · python
1"""2Tests basic UndefinedBehaviorSanitizer support (detecting an alignment error).3"""4 5import os6import lldb7from lldbsuite.test.lldbtest import *8from lldbsuite.test.decorators import *9import lldbsuite.test.lldbutil as lldbutil10import json11 12 13class UbsanBasicTestCase(TestBase):14 @skipUnlessUndefinedBehaviorSanitizer15 @no_debug_info_test16 def test(self):17 self.build()18 self.ubsan_tests()19 20 def setUp(self):21 # Call super's setUp().22 TestBase.setUp(self)23 self.line_align = line_number("main.c", "// align line")24 25 def ubsan_tests(self):26 # Load the test27 exe = self.getBuildArtifact("a.out")28 target = self.dbg.CreateTarget(exe)29 self.assertTrue(target, VALID_TARGET)30 self.registerSanitizerLibrariesWithTarget(target)31 32 self.runCmd("run")33 34 process = self.dbg.GetSelectedTarget().process35 thread = process.GetSelectedThread()36 frame = thread.GetSelectedFrame()37 38 # the stop reason of the thread should be breakpoint.39 self.expect(40 "thread list",41 "A ubsan issue should be detected",42 substrs=["stopped", "stop reason ="],43 )44 45 stop_reason = thread.GetStopReason()46 self.assertStopReason(stop_reason, lldb.eStopReasonInstrumentation)47 48 # test that the UBSan dylib is present49 self.expect(50 "image lookup -n __ubsan_on_report",51 "__ubsan_on_report should be present",52 substrs=["1 match found"],53 )54 55 if self.platformIsDarwin():56 # We should not be stopped in the sanitizer library.57 self.assertIn("main", frame.GetFunctionName())58 else:59 self.assertIn("__ubsan_on_report", frame.GetFunctionName())60 61 # The stopped thread backtrace should contain either 'align line'62 found = False63 for i in range(thread.GetNumFrames()):64 frame = thread.GetFrameAtIndex(i)65 if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c":66 if frame.GetLineEntry().GetLine() == self.line_align:67 found = True68 self.assertTrue(found)69 70 backtraces = thread.GetStopReasonExtendedBacktraces(71 lldb.eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer72 )73 self.assertEqual(backtraces.GetSize(), 1)74 75 self.expect(76 "thread info -s",77 "The extended stop info should contain the UBSan provided fields",78 substrs=[79 "col",80 "description",81 "filename",82 "instrumentation_class",83 "line",84 "memory_address",85 ],86 )87 88 output_lines = self.res.GetOutput().split("\n")89 json_line = "\n".join(output_lines[2:])90 data = json.loads(json_line)91 92 self.assertEqual(data["instrumentation_class"], "UndefinedBehaviorSanitizer")93 self.assertEqual(data["description"], "misaligned-pointer-use")94 self.assertEqual(os.path.basename(data["filename"]), "main.c")95 self.assertEqual(data["line"], self.line_align)96 97 for count in range(0, 8):98 process.Continue()99 stop_reason = thread.GetStopReason()100 self.assertEqual(101 stop_reason,102 lldb.eStopReasonInstrumentation,103 "Round {0} wasn't instrumentation".format(count),104 )105