Skip to content

Heap corruption when awaiting AsyncSequence in top level #65001

Open
@sidepelican

Description

@sidepelican

Description

Awaiting AsyncSequence in top-level code causes runtime heap corruption in release build.

Steps to reproduce

Add https://github.com/apple/swift-async-algorithms to Package.swift and write the code below ( main.swift ).

import AsyncAlgorithms

for await _ in AsyncChannel<Int>().buffer(policy: .unbounded) {
}

And run.

❯ swift run -c release
Building for production...
Build complete! (0.13s)
AsyncStream(91246,0x200ac9b40) malloc: Heap corruption detected, free list is damaged at 0x600002a44030
*** Incorrect guard value: 105553128095744
AsyncStream(91246,0x200ac9b40) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    91246 abort      swift run -c release

This occurs only with top-level code. Therefore, I thought this is a compiler problem, not a swift-async-algorithms problem.
Any operator can be attached to an AsyncSequence.
For example,

for await _ in AsyncChannel<Int>().map({ $0 }) {}

also cause segmentation fault.

Expected behavior

Does not happen segmentation fault or heap corruption.

Environment

  • Swift compiler version info
    swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode version info
    Xcode 14.3
    Build version 14E222b
  • Deployment target: macOS 13

This also reproduces on Linux.

root@df6e19e638c0:/work# swift run -c release
Building for production...
Build complete! (0.15s)
Segmentation fault
root@df6e19e638c0:/work# swift --version
Swift version 5.8 (swift-5.8-RELEASE)
Target: aarch64-unknown-linux-gnu

Metadata

Metadata

Assignees

No one assigned

    Labels

    SILOptimizerArea → compiler: SIL optimization passesasync & awaitFeature → concurrency: asynchronous function aka the async/await patternbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresmemory corruptionoptimized onlyFlag: An issue whose reproduction requires optimized compilationswift 5.8

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions