Skip to content

Commit b5a01ec

Browse files
committed
Add asan tests for libsanitizers for swift.
This patch tests LLDB integration with libsanitizers for ASan for swift. rdar://126704110
1 parent a338c11 commit b5a01ec

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

lldb/test/API/functionalities/asan/swift/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
SWIFT_SOURCES := main.swift
33

44
# Note the lazy variable setting - needs to use CLANG_RT_DIR, defined in Makefile.rules
5-
LD_EXTRAS = -lclang_rt.asan_osx_dynamic -L$(CLANG_RT_DIR)
5+
asan: LD_EXTRAS = -lclang_rt.asan_osx_dynamic -L$(CLANG_RT_DIR)
6+
asan: SWIFTFLAGS += -sanitize=address
7+
asan: all
68

7-
include Makefile.rules
9+
libsanitizers: SWIFTFLAGS += -sanitize=address -sanitize-stable-abi
10+
libsanitizers: all
811

9-
SWIFTFLAGS += -sanitize=address
12+
include Makefile.rules

lldb/test/API/functionalities/asan/swift/TestAsanSwift.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import lldbsuite.test.decorators as decorators
1717
import lldbsuite.test.lldbtest as lldbtest
1818
import lldbsuite.test.lldbutil as lldbutil
19+
from lldbsuite.test_event.build_exception import BuildError
1920
import os
2021
import unittest2
2122
import json
@@ -29,8 +30,16 @@ class AsanSwiftTestCase(lldbtest.TestBase):
2930
@decorators.skipIfLinux
3031
@decorators.skipUnlessSwiftAddressSanitizer
3132
def test_asan_swift(self):
32-
self.build()
33-
self.do_test()
33+
self.build(make_targets=["asan"])
34+
self.do_test_asan()
35+
36+
@decorators.skipIf(oslist=decorators.no_match(["macosx"]))
37+
def test_libsanitizers_swift(self):
38+
try:
39+
self.build(make_targets=["libsanitizers"])
40+
except BuildError as e:
41+
self.skipTest("failed to build with libsanitizers")
42+
self.do_test_libsanitizers()
3443

3544
def setUp(self):
3645
lldbtest.TestBase.setUp(self)
@@ -39,7 +48,7 @@ def setUp(self):
3948
self.line_breakpoint = lldbtest.line_number(
4049
self.main_source, '// breakpoint')
4150

42-
def do_test(self):
51+
def do_test_asan(self):
4352
exe_name = "a.out"
4453
exe = self.getBuildArtifact(exe_name)
4554

@@ -101,3 +110,33 @@ def do_test(self):
101110
substrs=[
102111
'Memory allocated by Thread 1',
103112
'main.swift'])
113+
114+
# Test line numbers: rdar://126237493
115+
def do_test_libsanitizers(self):
116+
exe_name = "a.out"
117+
exe = self.getBuildArtifact(exe_name)
118+
119+
# Create the target
120+
target = self.dbg.CreateTarget(exe)
121+
self.assertTrue(target, lldbtest.VALID_TARGET)
122+
123+
self.runCmd("env SanitizersAddress=1 MallocSanitizerZone=1 MallocSecureAllocator=0")
124+
125+
self.runCmd("run")
126+
127+
# the stop reason of the thread should be a ASan report.
128+
self.expect("thread list", "Heap buffer overflow", substrs=[
129+
'stopped', 'stop reason = Heap buffer overflow'])
130+
131+
process = self.dbg.GetSelectedTarget().process
132+
thread = process.GetSelectedThread()
133+
134+
self.assertEqual(
135+
thread.GetStopReason(),
136+
lldb.eStopReasonInstrumentation)
137+
138+
self.expect(
139+
"memory history `__asan_get_report_address()`",
140+
substrs=[
141+
'Memory allocated by Thread 1',
142+
'main.swift'])

0 commit comments

Comments
 (0)