Skip to content

defer statement fails to increment refcount of consuming binding, causing segmentation fault #70133

Open
@tayloraswift

Description

@tayloraswift

Description

the following test program crashes with a segfault when compiled with a 5.9.1 toolchain.

Reproduction

class Class
{
    init()
    {
    }
}

struct ClassList
{
    var array:[Class]

    init(_ x:consuming Class)
    {
        self.array = []

        defer
        {
            self.array.append(x)
        }

        _ = copy x
    }
}

func main()
{
    let _:ClassList = .init(Class.init())
}

main()
$ swiftc Snippets/_Crashes/Refcounting.swift 
$ ./Refcounting

Stack dump

💣 Program crashed: Bad pointer dereference at 0x00007fd15e861868

Thread 0 "Refcounting" crashed:

0 0x00007fd15e861868
1 0x00007fd15e60429b bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 122 in libswiftCore.so
2 0x00007fd15e5f789b swift_arrayDestroy + 58 in libswiftCore.so
3 0x00007fd15e3a53b7 _ContiguousArrayStorage.__deallocating_deinit + 38 in libswiftCore.so
4 0x00007fd15e6038e0 _swift_release_dealloc + 15 in libswiftCore.so
5 0x00007fd15e60429b bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 122 in libswiftCore.so
6 0x000056073eea4f68 main() + 39 in Refcounting
7 0x000056073eea4d29 main + 8 in Refcounting

Segmentation fault (core dumped)

Expected behavior

the defer statement should correctly increment the ref count of the captured binding.

Environment

$ swift --version
Swift version 5.9.1 (swift-5.9.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwareownershipFeature: Ownership modifiers and semanticsrun-time crashBug → crash: Swift code crashed during execution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions