Skip to content

Add asan tests for libsanitizers. (#88349) #88962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lldb/test/API/functionalities/asan/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
C_SOURCES := main.c
CFLAGS_EXTRAS := -fsanitize=address -g -gcolumn-info
asan: CFLAGS_EXTRAS := -fsanitize=address -g -gcolumn-info
asan: all

libsanitizers: CFLAGS_EXTRAS := -fsanitize=address -fsanitize-stable-abi -g -gcolumn-info
libsanitizers: all

include Makefile.rules
74 changes: 72 additions & 2 deletions lldb/test/API/functionalities/asan/TestMemoryHistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbplatform
from lldbsuite.test import lldbutil

from lldbsuite.test_event.build_exception import BuildError

class AsanTestCase(TestBase):
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
@expectedFailureNetBSD
@skipUnlessAddressSanitizer
def test(self):
self.build()
self.build(make_targets=["asan"])
self.asan_tests()

@skipIf(oslist=no_match(["macosx"]))
def test_libsanitizers_asan(self):
try:
self.build(make_targets=["libsanitizers"])
except BuildError as e:
self.skipTest("failed to build with libsanitizers")
self.libsanitizer_tests()

def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
Expand All @@ -26,6 +34,68 @@ def setUp(self):
self.line_free = line_number("main.c", "// free line")
self.line_breakpoint = line_number("main.c", "// break line")

# Test line numbers: rdar://126237493
def libsanitizer_tests(self):
target = self.createTestTarget()

self.runCmd(
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
)

self.runCmd("run")

# In libsanitizers, memory history is not supported until a report has been generated
self.expect(
"thread list",
"Process should be stopped due to ASan report",
substrs=["stopped", "stop reason = Use of deallocated memory"],
)

# test the 'memory history' command
self.expect(
"memory history 'pointer'",
substrs=[
"Memory deallocated by Thread",
"a.out`f2",
"main.c",
"Memory allocated by Thread",
"a.out`f1",
"main.c",
],
)

# do the same using SB API
process = self.dbg.GetSelectedTarget().process
val = (
process.GetSelectedThread().GetSelectedFrame().EvaluateExpression("pointer")
)
addr = val.GetValueAsUnsigned()
threads = process.GetHistoryThreads(addr)
self.assertEqual(threads.GetSize(), 2)

history_thread = threads.GetThreadAtIndex(0)
self.assertTrue(history_thread.num_frames >= 2)
self.assertEqual(
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
"main.c",
)

history_thread = threads.GetThreadAtIndex(1)
self.assertTrue(history_thread.num_frames >= 2)
self.assertEqual(
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
"main.c",
)

# let's free the container (SBThreadCollection) and see if the
# SBThreads still live
threads = None
self.assertTrue(history_thread.num_frames >= 2)
self.assertEqual(
history_thread.frames[1].GetLineEntry().GetFileSpec().GetFilename(),
"main.c",
)

def asan_tests(self):
target = self.createTestTarget()

Expand Down
21 changes: 17 additions & 4 deletions lldb/test/API/functionalities/asan/TestReportData.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil

from lldbsuite.test_event.build_exception import BuildError

class AsanTestReportDataCase(TestBase):
@skipIfFreeBSD # llvm.org/pr21136 runtimes not yet available by default
@expectedFailureNetBSD
@skipUnlessAddressSanitizer
@skipIf(archs=["i386"], bugnumber="llvm.org/PR36710")
def test(self):
self.build()
self.build(make_targets=["asan"])
self.asan_tests()

@skipIf(oslist=no_match(["macosx"]))
def test_libsanitizers_asan(self):
try:
self.build(make_targets=["libsanitizers"])
except BuildError as e:
self.skipTest("failed to build with libsanitizers")
self.asan_tests(libsanitizers=True)

def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
Expand All @@ -29,10 +37,15 @@ def setUp(self):
self.line_crash = line_number("main.c", "// BOOM line")
self.col_crash = 16

def asan_tests(self):
def asan_tests(self, libsanitizers=False):
target = self.createTestTarget()

self.registerSanitizerLibrariesWithTarget(target)
if libsanitizers:
self.runCmd(
"env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0"
)
else:
self.registerSanitizerLibrariesWithTarget(target)

self.runCmd("run")

Expand Down
Loading