Skip to content

Commit aa4e2da

Browse files
committed
[build-script] Add support for compiling swift with leaks sanitizer.
This only enables the swift compiler (not its output) to be used with leaks sanitizer on linux. Some important notes: On Linux, right now we are not completely leak clean. I was only able to get a -Onone build of the stdlib without triggering lsan (I was unable to run the optimizer and the tests successfully). Additionally even at -Onone, I had to suppress some leaks in the driver. The point of this though is to prevent any further -Onone leaks from being committed to the tree since -Onone leaks that are not bounded (unlike the driver bugs) could cause SourceKit to leak memory. Since SourceKit is a long running process... such a type of leak would be bad. rdar://32876901
1 parent 28beb03 commit aa4e2da

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

utils/build-presets.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,18 @@ build-subdir=buildbot_incremental_asan
898898

899899
enable-asan
900900

901+
[preset: buildbot_incremental_linux,lsan]
902+
build-subdir=buildbot_incremental_lsan
903+
904+
release-debuginfo
905+
assertions
906+
enable-lsan
907+
908+
dash-dash
909+
910+
build-ninja
911+
reconfigure
912+
901913
#===------------------------------------------------------------------------===#
902914
# OS X Package Builders
903915
#===------------------------------------------------------------------------===#

utils/build-script

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,14 @@ class BuildScriptInvocation(object):
682682
# NOT pass them to build-script-impl.
683683
if args.enable_asan:
684684
impl_args += ["--enable-asan"]
685+
# If we have lsan, we need to export our suppression list. The actual
686+
# passing in of the LSAN flag is done via the normal cmake method. We
687+
# do not pass the flag to build-script-impl.
688+
if args.enable_lsan:
689+
supp_file = os.path.join(SWIFT_SOURCE_ROOT, SWIFT_REPO_NAME,
690+
"utils",
691+
"lsan_leaks_suppression_list.txt")
692+
os.environ['LSAN_OPTIONS'] = 'suppressions={}'.format(supp_file)
685693
if args.verbose_build:
686694
impl_args += ["--verbose-build"]
687695
if args.install_symroot:
@@ -2018,6 +2026,10 @@ iterations with -O",
20182026
parser.add_argument(
20192027
"--enable-tsan-runtime",
20202028
help="enable Thread Sanitizer on the swift runtime")
2029+
parser.add_argument(
2030+
"--enable-lsan",
2031+
help="enable Leak Sanitizer for swift tools",
2032+
action=arguments.action.optional_bool)
20212033

20222034
parser.add_argument(
20232035
"--compiler-vendor",

utils/lsan_leaks_suppression_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
leak:*buildCompilation*
2+
leak:*llvm::TableGenMain*

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def common_options(self):
9292
sanitizers.append('Undefined')
9393
if args.enable_tsan:
9494
sanitizers.append('Thread')
95+
if args.enable_lsan:
96+
sanitizers.append('Leaks')
9597
if sanitizers:
9698
define("LLVM_USE_SANITIZER", ";".join(sanitizers))
9799

utils/swift_build_support/tests/test_cmake.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def default_args(self):
3333
enable_asan=False,
3434
enable_ubsan=False,
3535
enable_tsan=False,
36+
enable_lsan=False,
3637
export_compile_commands=False,
3738
distcc=False,
3839
cmake_generator="Ninja",
@@ -148,6 +149,18 @@ def test_common_options_asan_ubsan_tsan(self):
148149
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
149150
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
150151

152+
def test_common_options_lsan(self):
153+
args = self.default_args()
154+
args.enable_lsan = True
155+
cmake = self.cmake(args)
156+
self.assertEqual(
157+
list(cmake.common_options()),
158+
["-G", "Ninja",
159+
"-DLLVM_USE_SANITIZER=Leaks",
160+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
161+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
162+
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
163+
151164
def test_common_options_export_compile_commands(self):
152165
args = self.default_args()
153166
args.export_compile_commands = True

0 commit comments

Comments
 (0)