1010from lldbsuite .test import lldbutil
1111from lldbsuite .test_event .build_exception import BuildError
1212
13- class AsanTestCase (TestBase ):
13+ class MemoryHistoryTestCase (TestBase ):
1414 @skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
1515 @expectedFailureNetBSD
1616 @skipUnlessAddressSanitizer
17- def test (self ):
18- self .build (make_targets = ["asan" ])
19- self .asan_tests ()
17+ def test_compiler_rt_asan (self ):
18+ self .build (make_targets = ["compiler_rt- asan" ])
19+ self .compiler_rt_asan_tests ()
2020
21- @skipIf ( oslist = no_match ([ "macosx" ]))
22- @skipIf (bugnumber = "rdar://144997976 " )
21+ @skipUnlessDarwin
22+ @skipIf (bugnumber = "rdar://109913184&143590169 " )
2323 def test_libsanitizers_asan (self ):
2424 try :
25- self .build (make_targets = ["libsanitizers" ])
25+ self .build (make_targets = ["libsanitizers-asan " ])
2626 except BuildError as e :
2727 self .skipTest ("failed to build with libsanitizers" )
28- self .libsanitizer_tests ()
28+ self .libsanitizers_asan_tests ()
29+
30+ @skipUnlessDarwin
31+ @skipIf (macos_version = ["<" , "15.5" ])
32+ def test_libsanitizers_traces (self ):
33+ self .build (make_targets = ["libsanitizers-traces" ])
34+ self .libsanitizers_traces_tests ()
2935
3036 def setUp (self ):
3137 # Call super's setUp().
@@ -36,35 +42,60 @@ def setUp(self):
3642 self .line_breakpoint = line_number ("main.c" , "// break line" )
3743
3844 # Test line numbers: rdar://126237493
39- def libsanitizer_tests (self ):
40- target = self .createTestTarget ()
41-
42- self .runCmd (
43- "env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
44- )
45-
46- self .runCmd ("run" )
47-
48- # In libsanitizers, memory history is not supported until a report has been generated
49- self .expect (
50- "thread list" ,
51- "Process should be stopped due to ASan report" ,
52- substrs = ["stopped" , "stop reason = Use of deallocated memory" ],
53- )
54-
55- # test the 'memory history' command
45+ # for libsanitizers and remove `skip_line_numbers` parameter
46+ def check_traces (self , skip_line_numbers = False ):
5647 self .expect (
5748 "memory history 'pointer'" ,
5849 substrs = [
5950 "Memory deallocated by Thread" ,
6051 "a.out`f2" ,
61- "main.c" ,
52+ "main.c" if skip_line_numbers else f"main.c: { self . line_free } " ,
6253 "Memory allocated by Thread" ,
6354 "a.out`f1" ,
64- "main.c" ,
55+ "main.c" if skip_line_numbers else f"main.c: { self . line_malloc } " ,
6556 ],
6657 )
6758
59+ # Set breakpoint: after free, but before bug
60+ def set_breakpoint (self , target ):
61+ bkpt = target .BreakpointCreateByLocation ("main.c" , self .line_breakpoint )
62+ self .assertGreater (bkpt .GetNumLocations (), 0 , "Set the breakpoint successfully" )
63+
64+ def run_to_breakpoint (self , target ):
65+ self .set_breakpoint (target )
66+ self .runCmd ("run" )
67+ self .expect (
68+ "thread list" ,
69+ STOPPED_DUE_TO_BREAKPOINT ,
70+ substrs = ["stopped" , "stop reason = breakpoint" ],
71+ )
72+
73+ def libsanitizers_traces_tests (self ):
74+ target = self .createTestTarget ()
75+
76+ self .runCmd ("env SanitizersAllocationTraces=all" )
77+
78+ self .run_to_breakpoint (target )
79+ self .check_traces (skip_line_numbers = True )
80+
81+ def libsanitizers_asan_tests (self ):
82+ target = self .createTestTarget ()
83+
84+ self .runCmd ("env SanitizersAddress=1 MallocSanitizerZone=1" )
85+
86+ self .run_to_breakpoint (target )
87+ self .check_traces (skip_line_numbers = True )
88+
89+ self .runCmd ("continue" )
90+
91+ # Stop on report
92+ self .expect (
93+ "thread list" ,
94+ "Process should be stopped due to ASan report" ,
95+ substrs = ["stopped" , "stop reason = Use of deallocated memory" ],
96+ )
97+ self .check_traces (skip_line_numbers = True )
98+
6899 # do the same using SB API
69100 process = self .dbg .GetSelectedTarget ().process
70101 val = (
@@ -97,12 +128,12 @@ def libsanitizer_tests(self):
97128 "main.c" ,
98129 )
99130
100- def asan_tests (self ):
131+ def compiler_rt_asan_tests (self ):
101132 target = self .createTestTarget ()
102133
103134 self .registerSanitizerLibrariesWithTarget (target )
104135
105- self .runCmd ( "breakpoint set -f main.c -l %d" % self . line_breakpoint )
136+ self .set_breakpoint ( target )
106137
107138 # "memory history" command should not work without a process
108139 self .expect (
@@ -135,18 +166,7 @@ def asan_tests(self):
135166 substrs = ["1 match found" ],
136167 )
137168
138- # test the 'memory history' command
139- self .expect (
140- "memory history 'pointer'" ,
141- substrs = [
142- "Memory deallocated by Thread" ,
143- "a.out`f2" ,
144- "main.c:%d" % self .line_free ,
145- "Memory allocated by Thread" ,
146- "a.out`f1" ,
147- "main.c:%d" % self .line_malloc ,
148- ],
149- )
169+ self .check_traces ()
150170
151171 # do the same using SB API
152172 process = self .dbg .GetSelectedTarget ().process
@@ -198,6 +218,8 @@ def asan_tests(self):
198218 substrs = ["stopped" , "stop reason = Use of deallocated memory" ],
199219 )
200220
221+ self .check_traces ()
222+
201223 # make sure the 'memory history' command still works even when we're
202224 # generating a report now
203225 self .expect (
0 commit comments