Skip to content
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

Add asan tests for libsanitizers for swift. #8611

Open
wants to merge 1 commit into
base: stable/20230725
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lldb/test/API/functionalities/asan/swift/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 42 additions & 3 deletions lldb/test/API/functionalities/asan/swift/TestAsanSwift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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'])