Skip to content

Crash when using concurrency, parameter packs and associated type together #68698

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

Closed
rosecoder opened this issue Sep 22, 2023 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software memory safety Feature: memory safety parameter packs Feature → generics: Parameter packs

Comments

@rosecoder
Copy link

Description
Hello. We see a signal SIGABRT with the following output when trying out the new parameter packs in Swift 5.9.

freed pointer was not the last allocation[1]    40944 abort      swift run

Steps to reproduce
Isolated the issue with the following code:

protocol ContextReferenceable {
    associatedtype Reference: Sendable
}

func saveAsync<each Element: ContextReferenceable>(
    of type: (repeat (each Element)).Type
) async throws -> (repeat each Element) {
    let reference: (repeat (each Element).Reference) // referencing `Element.Reference` anywhere in the function body triggers the crash

    throw CancellationError() // normally more code here, but excluded for this example

    // crashes here after function body execution: `freed pointer was not the last allocation`
}

struct Animal: ContextReferenceable {
    typealias Reference = String
}
_ = try await saveAsync(of: Animal.self)

Including a real life example of the crash with some stack trace:
Crash

Expected behavior
No crash 🙂

Environment

  • Swift compiler version info
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx13.0
  • Xcode version info
Xcode 15.0
Build version 15A240d
  • Deployment target: Tested and reproducible with iOS 15, iOS 16 and macOS 13.
@rosecoder rosecoder added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Sep 22, 2023
@simanerush simanerush added SILGen Area → compiler: The SIL generation stage crash Bug: A crash, i.e., an abnormal termination of software memory safety Feature: memory safety parameter packs Feature → generics: Parameter packs labels Dec 6, 2023
@jckarter jckarter removed the SILGen Area → compiler: The SIL generation stage label Dec 6, 2023
@mportiz08
Copy link

Ran into this crash myself and might have some useful info on how to reproduce it for anyone looking into this in the future.

This simplified example code works as expected, but it crashes the compiler only when a local variable is used for the parameter pack value—it crashes with the freed pointer was not the last allocation message when the getValuesTuple implementation is changed to the commented out lines that use an intermediate local variable.

import Foundation

protocol HasAsyncValue {
    associatedtype Value
    func getValue() async throws -> Value
}

struct Fixed: HasAsyncValue {
    let string: String
    func getValue() async throws -> String {
        try await Task.sleep(until: .now + .milliseconds(50))
        return string
    }
}

struct Generic<Value>: HasAsyncValue {
    let value: Value
    func getValue() async throws -> Value {
        try await Task.sleep(until: .now + .milliseconds(50))
        return value
    }
}

func getValuesTuple<each Item: HasAsyncValue>(
    _ item: repeat each Item
) async throws -> (repeat (each Item).Value) {
    /*let result = (repeat try await (each item).getValue())
    return result*/
    return (repeat try await (each item).getValue())
}

do {
    print(try await getValuesTuple(
        Fixed(string: "foo"),
        Generic(value: 42),
        Generic(value: "bar"),
        Fixed(string: "blah"),
        Generic(value: ["foobar":42])
    ))
} catch {
    print("error: \(error)")
}

@hborla hborla removed the triage needed This issue needs more specific labels label Apr 27, 2024
@stephencelis
Copy link
Contributor

This looks like it's related to #67702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software memory safety Feature: memory safety parameter packs Feature → generics: Parameter packs
Projects
None yet
Development

No branches or pull requests

7 participants