Skip to content

Commit 48adfe5

Browse files
committed
Add asan tests for libsanitizers. (llvm#88349)
This patch tests LLDB integration with libsanitizers for ASan. rdar://111856681
1 parent 481bd5d commit 48adfe5

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
C_SOURCES := main.c
2-
CFLAGS_EXTRAS := -fsanitize=address -g -gcolumn-info
2+
asan: CFLAGS_EXTRAS := -fsanitize=address -g -gcolumn-info
3+
asan: all
4+
5+
libsanitizers: CFLAGS_EXTRAS := -fsanitize=address -fsanitize-stable-abi -g -gcolumn-info
6+
libsanitizers: all
37

48
include Makefile.rules

lldb/test/API/functionalities/asan/TestMemoryHistory.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@
88
from lldbsuite.test.lldbtest import *
99
from lldbsuite.test import lldbplatform
1010
from lldbsuite.test import lldbutil
11-
11+
from lldbsuite.test_event.build_exception import BuildError
1212

1313
class AsanTestCase(TestBase):
1414
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
1515
@expectedFailureNetBSD
1616
@skipUnlessAddressSanitizer
1717
def test(self):
18-
self.build()
18+
self.build(make_targets=["asan"])
1919
self.asan_tests()
2020

21+
@skipIf(oslist=no_match(["macosx"]))
22+
def test_libsanitizers_asan(self):
23+
try:
24+
self.build(make_targets=["libsanitizers"])
25+
except BuildError as e:
26+
self.skipTest("failed to build with libsanitizers")
27+
self.libsanitizer_tests()
28+
2129
def setUp(self):
2230
# Call super's setUp().
2331
TestBase.setUp(self)
@@ -26,6 +34,68 @@ def setUp(self):
2634
self.line_free = line_number("main.c", "// free line")
2735
self.line_breakpoint = line_number("main.c", "// break line")
2836

37+
# Test line numbers: rdar://126237493
38+
def libsanitizer_tests(self):
39+
target = self.createTestTarget()
40+
41+
self.runCmd(
42+
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
43+
)
44+
45+
self.runCmd("run")
46+
47+
# In libsanitizers, memory history is not supported until a report has been generated
48+
self.expect(
49+
"thread list",
50+
"Process should be stopped due to ASan report",
51+
substrs=["stopped", "stop reason = Use of deallocated memory"],
52+
)
53+
54+
# test the 'memory history' command
55+
self.expect(
56+
"memory history 'pointer'",
57+
substrs=[
58+
"Memory deallocated by Thread",
59+
"a.out`f2",
60+
"main.c",
61+
"Memory allocated by Thread",
62+
"a.out`f1",
63+
"main.c",
64+
],
65+
)
66+
67+
# do the same using SB API
68+
process = self.dbg.GetSelectedTarget().process
69+
val = (
70+
process.GetSelectedThread().GetSelectedFrame().EvaluateExpression("pointer")
71+
)
72+
addr = val.GetValueAsUnsigned()
73+
threads = process.GetHistoryThreads(addr)
74+
self.assertEqual(threads.GetSize(), 2)
75+
76+
history_thread = threads.GetThreadAtIndex(0)
77+
self.assertTrue(history_thread.num_frames >= 2)
78+
self.assertEqual(
79+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
80+
"main.c",
81+
)
82+
83+
history_thread = threads.GetThreadAtIndex(1)
84+
self.assertTrue(history_thread.num_frames >= 2)
85+
self.assertEqual(
86+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
87+
"main.c",
88+
)
89+
90+
# let's free the container (SBThreadCollection) and see if the
91+
# SBThreads still live
92+
threads = None
93+
self.assertTrue(history_thread.num_frames >= 2)
94+
self.assertEqual(
95+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
96+
"main.c",
97+
)
98+
2999
def asan_tests(self):
30100
target = self.createTestTarget()
31101

lldb/test/API/functionalities/asan/TestReportData.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
88
from lldbsuite.test.decorators import *
99
from lldbsuite.test.lldbtest import *
1010
from lldbsuite.test import lldbutil
11-
11+
from lldbsuite.test_event.build_exception import BuildError
1212

1313
class AsanTestReportDataCase(TestBase):
1414
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
1515
@expectedFailureNetBSD
1616
@skipUnlessAddressSanitizer
1717
@skipIf(archs=["i386"], bugnumber="llvm.org/PR36710")
1818
def test(self):
19-
self.build()
19+
self.build(make_targets=["asan"])
2020
self.asan_tests()
2121

22+
@skipIf(oslist=no_match(["macosx"]))
23+
def test_libsanitizers_asan(self):
24+
try:
25+
self.build(make_targets=["libsanitizers"])
26+
except BuildError as e:
27+
self.skipTest("failed to build with libsanitizers")
28+
self.asan_tests(libsanitizers=True)
29+
2230
def setUp(self):
2331
# Call super's setUp().
2432
TestBase.setUp(self)
@@ -29,10 +37,15 @@ def setUp(self):
2937
self.line_crash = line_number("main.c", "// BOOM line")
3038
self.col_crash = 16
3139

32-
def asan_tests(self):
40+
def asan_tests(self, libsanitizers=False):
3341
target = self.createTestTarget()
3442

35-
self.registerSanitizerLibrariesWithTarget(target)
43+
if libsanitizers:
44+
self.runCmd(
45+
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
46+
)
47+
else:
48+
self.registerSanitizerLibrariesWithTarget(target)
3649

3750
self.runCmd("run")
3851

0 commit comments

Comments
 (0)