Skip to content

Add asan tests for libsanitizers. #88349

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

Conversation

usama54321
Copy link
Member

This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2024

@llvm/pr-subscribers-lldb

Author: Usama Hameed (usama54321)

Changes

This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681


Full diff: https://github.com/llvm/llvm-project/pull/88349.diff

3 Files Affected:

  • (modified) lldb/test/API/functionalities/asan/Makefile (+5-1)
  • (modified) lldb/test/API/functionalities/asan/TestMemoryHistory.py (+65-1)
  • (modified) lldb/test/API/functionalities/asan/TestReportData.py (+11-3)
diff --git a/lldb/test/API/functionalities/asan/Makefile b/lldb/test/API/functionalities/asan/Makefile
index 4913a18d8cc6f9..d66696fed7078f 100644
--- a/lldb/test/API/functionalities/asan/Makefile
+++ b/lldb/test/API/functionalities/asan/Makefile
@@ -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
diff --git a/lldb/test/API/functionalities/asan/TestMemoryHistory.py b/lldb/test/API/functionalities/asan/TestMemoryHistory.py
index 00162ae8822c74..484f26e3fed5eb 100644
--- a/lldb/test/API/functionalities/asan/TestMemoryHistory.py
+++ b/lldb/test/API/functionalities/asan/TestMemoryHistory.py
@@ -15,9 +15,14 @@ class AsanTestCase(TestBase):
     @expectedFailureNetBSD
     @skipUnlessAddressSanitizer
     def test(self):
-        self.build()
+        self.build(make_targets=["asan"])
         self.asan_tests()
 
+    @skipIf(macos_version=["<", "15.0"], oslist=no_match(["macosx"]))
+    def test_libsanitizers_asan(self):
+        self.build(make_targets=["libsanitizers"])
+        self.libsanitizer_tests()
+
     def setUp(self):
         # Call super's setUp().
         TestBase.setUp(self)
@@ -26,6 +31,65 @@ 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
+        # 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",
+        )
+
+        self.expect(
+            "thread list",
+            "Process should be stopped due to ASan report",
+            substrs=["stopped", "stop reason = Use of deallocated memory"],
+        )
+
     def asan_tests(self):
         target = self.createTestTarget()
 
diff --git a/lldb/test/API/functionalities/asan/TestReportData.py b/lldb/test/API/functionalities/asan/TestReportData.py
index 543c5fe66a208d..558b7be12d8f07 100644
--- a/lldb/test/API/functionalities/asan/TestReportData.py
+++ b/lldb/test/API/functionalities/asan/TestReportData.py
@@ -16,9 +16,14 @@ class AsanTestReportDataCase(TestBase):
     @skipUnlessAddressSanitizer
     @skipIf(archs=["i386"], bugnumber="llvm.org/PR36710")
     def test(self):
-        self.build()
+        self.build(make_targets=["asan"])
         self.asan_tests()
 
+    @skipIf(macos_version=["<", "15.0"], oslist=no_match(["macosx"]))
+    def test_libsanitizers_asan(self):
+        self.build(make_targets=["libsanitizers"])
+        self.asan_tests(libsanitizers=True)
+
     def setUp(self):
         # Call super's setUp().
         TestBase.setUp(self)
@@ -29,10 +34,13 @@ 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")
 

@usama54321
Copy link
Member Author

I reused the test in TestReportData because the output should match exactly with current ASan. For TestMemoryHistory, libsanitizers currently only supports the memory history plugin for addresses for which a report has been generated, so I wrote a test separately.

Copy link

github-actions bot commented Apr 11, 2024

✅ With the latest revision this PR passed the Python code formatter.

@usama54321 usama54321 force-pushed the libsanitizers-asan-tests branch 3 times, most recently from de4ed32 to 3d31b40 Compare April 11, 2024 16:36
@usama54321 usama54321 force-pushed the libsanitizers-asan-tests branch from 3d31b40 to c336a2a Compare April 11, 2024 18:07
Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do move the stop reason test up before all the other tests, since it conditions them.

Other than that, this LGTM.

@usama54321 usama54321 force-pushed the libsanitizers-asan-tests branch from c336a2a to b25a5a0 Compare April 11, 2024 18:40
@usama54321
Copy link
Member Author

Added a util function to skip tests if libsanitizers is not present in the shared cache

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thetruestblue
Copy link
Contributor

thetruestblue commented Apr 11, 2024

Will this test primarily be run at-desk? Or will it be a part of existing CI? I know we have discussed this previously, I just want to confirm as I'm putting together documentation for this kind of testing for libsanitizers. I'd love to make note somewhere where we expect to run this.

Otherwise this test lgtm from the sanitizers side.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dyld_shared_cache_util doesn't come preinstalled on macOS (you can build it yourself locally from source) so I think we should use lldb to check if the library is loaded.

@usama54321 usama54321 force-pushed the libsanitizers-asan-tests branch from b25a5a0 to 59ee902 Compare April 11, 2024 20:51
@usama54321
Copy link
Member Author

Changed the no_libsanitizers function to use the LLDB image list command

@usama54321
Copy link
Member Author

Will this test primarily be run at-desk? Or will it be a part of existing CI? I know we have discussed this previously, I just want to confirm as I'm putting together documentation for this kind of testing for libsanitizers. I'd love to make note somewhere where we expect to run this.

Otherwise this test lgtm from the sanitizers side.

This is primarily for at-desk testing until libsanitizers is available in the CI. Let's add that to internal documentation

@usama54321 usama54321 requested a review from JDevlieghere April 11, 2024 21:59
Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

This patch tests LLDB integration with libsanitizers for ASan. This
integration works through the ASanLibsanitizers plugin in the
InstrumentationRuntime.

rdar://111856681
@usama54321 usama54321 force-pushed the libsanitizers-asan-tests branch from 59ee902 to 1875748 Compare April 16, 2024 02:32
@usama54321 usama54321 merged commit 82f479b into llvm:main Apr 16, 2024
4 checks passed
adrian-prantl added a commit that referenced this pull request Apr 16, 2024
This reverts commit 82f479b due to bot breakage.
@adrian-prantl
Copy link
Collaborator

@usama54321 I had to revert this because it broke both the arm64 and x86 bots: https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/lldb-cmake/1078/

usama54321 added a commit to usama54321/apple-llvm-project that referenced this pull request Apr 16, 2024
This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681
usama54321 added a commit that referenced this pull request Apr 16, 2024
The previous patch was reverted because the test fails to build when
libsanitizers is not present. This patch catches the BuildError
exception and skips the test appropriately.

This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681
usama54321 added a commit to usama54321/apple-llvm-project that referenced this pull request Apr 17, 2024
The previous patch was reverted because the test fails to build when
libsanitizers is not present. This patch catches the BuildError
exception and skips the test appropriately.

This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681
usama54321 added a commit to swiftlang/llvm-project that referenced this pull request Apr 18, 2024
The previous patch was reverted because the test fails to build when
libsanitizers is not present. This patch catches the BuildError
exception and skips the test appropriately.

This patch tests LLDB integration with libsanitizers for ASan.

rdar://111856681
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants