From 5bc01964ce4add4acdff928455f884569e593ab2 Mon Sep 17 00:00:00 2001 From: usama Date: Thu, 18 Apr 2024 12:02:44 -0700 Subject: [PATCH] Add asan tests for libsanitizers for swift. This patch tests LLDB integration with libsanitizers for ASan for swift. rdar://126704110 --- .../API/functionalities/asan/swift/Makefile | 9 ++-- .../asan/swift/TestAsanSwift.py | 45 +++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lldb/test/API/functionalities/asan/swift/Makefile b/lldb/test/API/functionalities/asan/swift/Makefile index 5f4e49de3baab..31ed4120908e3 100644 --- a/lldb/test/API/functionalities/asan/swift/Makefile +++ b/lldb/test/API/functionalities/asan/swift/Makefile @@ -2,8 +2,11 @@ SWIFT_SOURCES := main.swift # Note the lazy variable setting - needs to use CLANG_RT_DIR, defined in Makefile.rules -LD_EXTRAS = -lclang_rt.asan_osx_dynamic -L$(CLANG_RT_DIR) +asan: LD_EXTRAS = -lclang_rt.asan_osx_dynamic -L$(CLANG_RT_DIR) +asan: SWIFTFLAGS += -sanitize=address +asan: all -include Makefile.rules +libsanitizers: SWIFTFLAGS += -sanitize=address -sanitize-stable-abi +libsanitizers: all -SWIFTFLAGS += -sanitize=address +include Makefile.rules diff --git a/lldb/test/API/functionalities/asan/swift/TestAsanSwift.py b/lldb/test/API/functionalities/asan/swift/TestAsanSwift.py index 96a9181bef6e0..af74761679ab6 100644 --- a/lldb/test/API/functionalities/asan/swift/TestAsanSwift.py +++ b/lldb/test/API/functionalities/asan/swift/TestAsanSwift.py @@ -16,6 +16,7 @@ import lldbsuite.test.decorators as decorators import lldbsuite.test.lldbtest as lldbtest import lldbsuite.test.lldbutil as lldbutil +from lldbsuite.test_event.build_exception import BuildError import os import unittest2 import json @@ -29,8 +30,16 @@ class AsanSwiftTestCase(lldbtest.TestBase): @decorators.skipIfLinux @decorators.skipUnlessSwiftAddressSanitizer def test_asan_swift(self): - self.build() - self.do_test() + self.build(make_targets=["asan"]) + self.do_test_asan() + + @decorators.skipIf(oslist=decorators.no_match(["macosx"])) + def test_libsanitizers_swift(self): + try: + self.build(make_targets=["libsanitizers"]) + except BuildError as e: + self.skipTest("failed to build with libsanitizers") + self.do_test_libsanitizers() def setUp(self): lldbtest.TestBase.setUp(self) @@ -39,7 +48,7 @@ def setUp(self): self.line_breakpoint = lldbtest.line_number( self.main_source, '// breakpoint') - def do_test(self): + def do_test_asan(self): exe_name = "a.out" exe = self.getBuildArtifact(exe_name) @@ -101,3 +110,33 @@ def do_test(self): substrs=[ 'Memory allocated by Thread 1', 'main.swift']) + + # Test line numbers: rdar://126237493 + def do_test_libsanitizers(self): + exe_name = "a.out" + exe = self.getBuildArtifact(exe_name) + + # Create the target + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, lldbtest.VALID_TARGET) + + self.runCmd("env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0") + + self.runCmd("run") + + # the stop reason of the thread should be a ASan report. + self.expect("thread list", "Heap buffer overflow", substrs=[ + 'stopped', 'stop reason = Heap buffer overflow']) + + process = self.dbg.GetSelectedTarget().process + thread = process.GetSelectedThread() + + self.assertEqual( + thread.GetStopReason(), + lldb.eStopReasonInstrumentation) + + self.expect( + "memory history `__asan_get_report_address()`", + substrs=[ + 'Memory allocated by Thread 1', + 'main.swift'])