Skip to content

Commit 1875748

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 1875748

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-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: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
from lldbsuite.test import lldbplatform
1010
from lldbsuite.test import lldbutil
1111

12+
from functionalities.libsanitizers.util import no_libsanitizers
1213

1314
class AsanTestCase(TestBase):
1415
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
1516
@expectedFailureNetBSD
1617
@skipUnlessAddressSanitizer
1718
def test(self):
18-
self.build()
19+
self.build(make_targets=["asan"])
1920
self.asan_tests()
2021

22+
@skipIf(oslist=no_match(["macosx"]))
23+
def test_libsanitizers_asan(self):
24+
self.build(make_targets=["libsanitizers"])
25+
self.libsanitizer_tests()
26+
2127
def setUp(self):
2228
# Call super's setUp().
2329
TestBase.setUp(self)
@@ -26,6 +32,71 @@ def setUp(self):
2632
self.line_free = line_number("main.c", "// free line")
2733
self.line_breakpoint = line_number("main.c", "// break line")
2834

35+
# Test line numbers: rdar://126237493
36+
def libsanitizer_tests(self):
37+
target = self.createTestTarget()
38+
39+
if no_libsanitizers(self):
40+
self.skipTest("libsanitizers not found")
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
56+
self.expect(
57+
"memory history 'pointer'",
58+
substrs=[
59+
"Memory deallocated by Thread",
60+
"a.out`f2",
61+
"main.c",
62+
"Memory allocated by Thread",
63+
"a.out`f1",
64+
"main.c",
65+
],
66+
)
67+
68+
# do the same using SB API
69+
process = self.dbg.GetSelectedTarget().process
70+
val = (
71+
process.GetSelectedThread().GetSelectedFrame().EvaluateExpression("pointer")
72+
)
73+
addr = val.GetValueAsUnsigned()
74+
threads = process.GetHistoryThreads(addr)
75+
self.assertEqual(threads.GetSize(), 2)
76+
77+
history_thread = threads.GetThreadAtIndex(0)
78+
self.assertTrue(history_thread.num_frames >= 2)
79+
self.assertEqual(
80+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
81+
"main.c",
82+
)
83+
84+
history_thread = threads.GetThreadAtIndex(1)
85+
self.assertTrue(history_thread.num_frames >= 2)
86+
self.assertEqual(
87+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
88+
"main.c",
89+
)
90+
91+
# let's free the container (SBThreadCollection) and see if the
92+
# SBThreads still live
93+
threads = None
94+
self.assertTrue(history_thread.num_frames >= 2)
95+
self.assertEqual(
96+
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
97+
"main.c",
98+
)
99+
29100
def asan_tests(self):
30101
target = self.createTestTarget()
31102

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99
from lldbsuite.test.lldbtest import *
1010
from lldbsuite.test import lldbutil
1111

12+
from functionalities.libsanitizers.util import no_libsanitizers
1213

1314
class AsanTestReportDataCase(TestBase):
1415
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
1516
@expectedFailureNetBSD
1617
@skipUnlessAddressSanitizer
1718
@skipIf(archs=["i386"], bugnumber="llvm.org/PR36710")
1819
def test(self):
19-
self.build()
20+
self.build(make_targets=["asan"])
2021
self.asan_tests()
2122

23+
@skipIf(oslist=no_match(["macosx"]))
24+
def test_libsanitizers_asan(self):
25+
self.build(make_targets=["libsanitizers"])
26+
self.asan_tests(libsanitizers=True)
27+
2228
def setUp(self):
2329
# Call super's setUp().
2430
TestBase.setUp(self)
@@ -29,10 +35,18 @@ def setUp(self):
2935
self.line_crash = line_number("main.c", "// BOOM line")
3036
self.col_crash = 16
3137

32-
def asan_tests(self):
38+
def asan_tests(self, libsanitizers=False):
3339
target = self.createTestTarget()
3440

35-
self.registerSanitizerLibrariesWithTarget(target)
41+
if libsanitizers and no_libsanitizers(self):
42+
self.skipTest("libsanitizers not found")
43+
44+
if libsanitizers:
45+
self.runCmd(
46+
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
47+
)
48+
else:
49+
self.registerSanitizerLibrariesWithTarget(target)
3650

3751
self.runCmd("run")
3852

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def no_libsanitizers(testbase):
2+
testbase.runCmd("image list libsystem_sanitizers.dylib", check=False)
3+
return not "libsystem_sanitizers.dylib" in testbase.res.GetOutput()

0 commit comments

Comments
 (0)