Testing with --sanitize=address with a single, empty swift test reported leaks (when XCTest is enabled)

I was messing around with a side project, and in the process of setting up CI for a Swift library, I thought "Hey, let's enable the address sanitization when running the tests".

At the moment, I don't have any tests - there's the template placeholder of an empty test:

@Test func example() async throws {
  // Write your test here and use APIs like `#expect(...)` to check expected conditions.
}

When I ran the CI (Linux, Swift 6.2.1), the address sanitizer reported errors:

Test Suite 'All tests' started at 2025-11-25 21:26:22.843
Test Suite 'debug.xctest' started at 2025-11-25 21:26:22.847
Test Suite 'debug.xctest' passed at 2025-11-25 21:26:22.847
	 Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
Test Suite 'All tests' passed at 2025-11-25 21:26:22.847
	 Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
=================================================================
==1648==ERROR: LeakSanitizer: detected memory leaks
Indirect leak of 192 byte(s) in 2 object(s) allocated from:
    #0 0x55dc73bcf54f in malloc /home/build-user/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x7f1ffd079e2e in swift_slowAlloc (/usr/lib/swift/linux/libswiftCore.so+0x44ae2e) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #2 0x7f1ffd07a186 in swift_allocObject (/usr/lib/swift/linux/libswiftCore.so+0x44b186) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #3 0x7f1ffb864515 in $s6XCTest0A8SuiteRunC4testAc2AC_tcfC (/usr/lib/swift/linux/libXCTest.so+0x2b515) (BuildId: a22185aaa9eeba0a6ea4f402d70a76ea10d45ee9)
Indirect leak of 48 byte(s) in 1 object(s) allocated from:
    #0 0x55dc73bcf54f in malloc /home/build-user/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x7f1ffd079e2e in swift_slowAlloc (/usr/lib/swift/linux/libswiftCore.so+0x44ae2e) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #2 0x7f1ffd07a186 in swift_allocObject (/usr/lib/swift/linux/libswiftCore.so+0x44b186) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #3 0x7f1ffb85dfdb in $s6XCTest11XCTMainMisc_9arguments9observersAA19TestSuiteOrExitCodeOSayAA0A4CaseCm04testK5Class_SaySS_yAHKctG8allTeststG_SaySSGSayAA0A11Observation_pGSgtF crtstuff.c
Indirect leak of 48 byte(s) in 1 object(s) allocated from:
    #0 0x55dc73bcf54f in malloc /home/build-user/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x7f1ffd079e2e in swift_slowAlloc (/usr/lib/swift/linux/libswiftCore.so+0x44ae2e) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #2 0x7f1ffd07a186 in swift_allocObject (/usr/lib/swift/linux/libswiftCore.so+0x44b186) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #3 0x7f1ffb85e0c1 in $s6XCTest11XCTMainMisc_9arguments9observersAA19TestSuiteOrExitCodeOSayAA0A4CaseCm04testK5Class_SaySS_yAHKctG8allTeststG_SaySSGSayAA0A11Observation_pGSgtF crtstuff.c
Indirect leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x55dc73bcf54f in malloc /home/build-user/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x7f1ffd079e2e in swift_slowAlloc (/usr/lib/swift/linux/libswiftCore.so+0x44ae2e) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #2 0x7f1ffd07a186 in swift_allocObject (/usr/lib/swift/linux/libswiftCore.so+0x44b186) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #3 0x7f1ffcd9145a in $ss22_ContiguousArrayBufferV20_consumeAndCreateNew14bufferIsUnique15minimumCapacity13growForAppendAByxGSb_SiSbtFSS5IndexV_Tg5Tm Swift.o
    #4 0x7f1ffcf06258 in $sSa034_makeUniqueAndReserveCapacityIfNotB0yyFyXl_Ts5 (/usr/lib/swift/linux/libswiftCore.so+0x2d7258) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #5 0x7f1ffb8644a3 in $s6XCTest0A8SuiteRunC07addTestC0yyAA0aC0CF (/usr/lib/swift/linux/libXCTest.so+0x2b4a3) (BuildId: a22185aaa9eeba0a6ea4f402d70a76ea10d45ee9)
Indirect leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x55dc73bcf54f in malloc /home/build-user/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x7f1ffd079e2e in swift_slowAlloc (/usr/lib/swift/linux/libswiftCore.so+0x44ae2e) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #2 0x7f1ffd07a186 in swift_allocObject (/usr/lib/swift/linux/libswiftCore.so+0x44b186) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #3 0x7f1ffcd9145a in $ss22_ContiguousArrayBufferV20_consumeAndCreateNew14bufferIsUnique15minimumCapacity13growForAppendAByxGSb_SiSbtFSS5IndexV_Tg5Tm Swift.o
    #4 0x7f1ffcf06258 in $sSa034_makeUniqueAndReserveCapacityIfNotB0yyFyXl_Ts5 (/usr/lib/swift/linux/libswiftCore.so+0x2d7258) (BuildId: a0cc6b6d664eafb6eac03bfe8d77abb20d395bb7)
    #5 0x7f1ffb85e10a in $s6XCTest11XCTMainMisc_9arguments9observersAA19TestSuiteOrExitCodeOSayAA0A4CaseCm04testK5Class_SaySS_yAHKctG8allTeststG_SaySSGSayAA0A11Observation_pGSgtF crtstuff.c
SUMMARY: AddressSanitizer: 368 byte(s) leaked in 6 allocation(s).
◇ Test run started.
↳ Testing Library Version: 6.2.1 (c9d57c83568b06d)
↳ Target Platform: x86_64-unknown-linux-gnu
◇ Test example() started.
✔ Test example() passed after 0.001 seconds.
✔ Test run with 1 test in 0 suites passed after 0.001 seconds.

Since I didn't have any tests yet, let alone XCTests, I added --disable-xctest into the CLI invocation, which passed without issue.

Is this a known issue, or is it work filing a bug re: address sanitization not appearing to work cleanly with empty XCTest?

Please file a GitHub issue against the swift-corelibs-xctest repo. We don't explicitly allocate anything there so this may be a false positive, but I'll take a look at it.

1 Like

You got it! enabling address sanitization with an empty set of tests reports leaks on Linux w/ Swift 6.2.1 · Issue #521 · swiftlang/swift-corelibs-xctest · GitHub

Verified - it's not happening on macOS
Is happening on Linux, at least with the OS release of 6.0, 6.1, 6.2, and the current nightly (using a run inside a container to check)

XCTest on macOS is the XCTest framework which shares approximately zero lines of code with swift-corelibs-xctest, so that's not surprising. Thanks for filing!

1 Like