Skip to content

Commit 3d31b40

Browse files
committed
[LLDB] Add asan tests for libsanitizers.
This patch tests LLDB integration with libsanitizers for ASan. This integration works through the ASanLibsanitizers plugin in the InstrumentationRuntime. rdar://111856681
1 parent 026165f commit 3d31b40

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
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: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ class AsanTestCase(TestBase):
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+
self.build(make_targets=["libsanitizers"])
24+
self.libsanitizer_tests()
25+
2126
def setUp(self):
2227
# Call super's setUp().
2328
TestBase.setUp(self)
@@ -26,6 +31,67 @@ def setUp(self):
2631
self.line_free = line_number("main.c", "// free line")
2732
self.line_breakpoint = line_number("main.c", "// break line")
2833

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

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ class AsanTestReportDataCase(TestBase):
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+
self.build(make_targets=["libsanitizers"])
25+
self.asan_tests(libsanitizers=True)
26+
2227
def setUp(self):
2328
# Call super's setUp().
2429
TestBase.setUp(self)
@@ -29,10 +34,15 @@ def setUp(self):
2934
self.line_crash = line_number("main.c", "// BOOM line")
3035
self.col_crash = 16
3136

32-
def asan_tests(self):
37+
def asan_tests(self, libsanitizers=False):
3338
target = self.createTestTarget()
3439

35-
self.registerSanitizerLibrariesWithTarget(target)
40+
if libsanitizers:
41+
self.runCmd(
42+
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
43+
)
44+
else:
45+
self.registerSanitizerLibrariesWithTarget(target)
3646

3747
self.runCmd("run")
3848

0 commit comments

Comments
 (0)