swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.53k stars 10.35k forks source link

LinearLifetimeChecker SIL verification found an ownership error in silgen emitConstructor #60841

Open tayloraswift opened 2 years ago

tayloraswift commented 2 years ago

Describe the bug

compiling the following test program crashes the compiler (and also sourcekit-lsp). the crash is new, i could not reproduce it on the 5.6.2 release toolchain, or the DEVELOPMENT-SNAPSHOT-2022-08-01-a nightly.

Steps To Reproduce

  1. save the following to test.swift:
struct C<T> where T:AdditiveArithmetic
{
    var x:[Int] 
    let y:T

    init() 
    {
        self.x = [0]
        self.y = .zero
    }

    var z:Int
    {
        _read
        {
            yield  self.x[0]
        }
        _modify
        {
            yield &self.x[0]
        }
    }
}
struct S 
{
    var c:C<Int>

    init()
    {
        self.c = .init()
        let _:Int = self.c.z
    }
}
  1. run swiftc test.swift with a recent nightly:
$ swiftc test2.swift 
error: compile command failed due to signal 6 (use -v to see invocation)
Begin Error in Function: '$s5test21SVACycfC'
Found outside of lifetime use?!
Value:   %17 = load_borrow %16 : $*C<Int>                // users: %27, %19
Consuming User:   end_borrow %17 : $C<Int>                        // id: %27
Non Consuming User:   end_borrow %19 : $*C<Int>                       // id: %30
Block: bb0

End Error in Function: '$s5test21SVACycfC'
Found ownership error?!
triggering standard assertion failure routine
UNREACHABLE executed at /home/build-user/swift/lib/SIL/Verifier/LinearLifetimeCheckerPrivate.h:211!
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.      Program arguments: /home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend -frontend -c -primary-file test2.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -new-driver-path /home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/lib/swift -module-name test2 -o /tmp/TemporaryDirectory.urmXsl/test2-1.o
1.      Swift version 5.8-dev (LLVM a049441a0549936, Swift a973fe5375d3851)
2.      Compiling with the current language version
3.      While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "test2.swift")
4.      While silgen emitConstructor SIL function "@$s5test21SVACycfC".
 for 'init()' (at test2.swift:28:5)
5.      While verifying SIL function "@$s5test21SVACycfC".
 for 'init()' (at test2.swift:28:5)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x67cbe23]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x67c9afe]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x67cc1af]
/lib64/libpthread.so.0(+0x118e0)[0x7f7c76eca8e0]
/lib64/libc.so.6(gsignal+0x110)[0x7f7c751edca0]
/lib64/libc.so.6(abort+0x148)[0x7f7c751ef148]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x66fa711]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x87dcee]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x8e5b0d]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x8e72c7]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x87b4f4]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x87d622]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x87d4bc]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x8a2e8d]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x8a588f]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x895826]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x888b75]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x886e9e]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x87f2fb]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd578ee]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd56883]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd58cd4]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd5903a]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xe1db1e]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xe1d818]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd5b835]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xe0f1ff]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd5ff2a]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0xd5d025]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x68c4bd]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x68ef56]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x4c1f4c]
/lib64/libc.so.6(__libc_start_main+0xea)[0x7f7c751db13a]
/home/ec2-user/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2022-09-06-a/usr/bin/swift-frontend[0x4c111a]

Expected behavior

the compiler should not crash.

Environment (please fill out the following information)

tayloraswift commented 2 years ago

i did binary search on this month’s nightlies, and found that the crash does not occur in any nightly up to and including DEVELOPMENT-SNAPSHOT-2022-08-18-a. DEVELOPMENT-SNAPSHOT-2022-08-24-a is the only affected nightly. so the regression must have taken place in the last ten days. :(

tayloraswift commented 2 years ago

confirmed the crash is still occurring in DEVELOPMENT-SNAPSHOT-2022-08-30-a. :(

tayloraswift commented 2 years ago

confirmed the crash is still occurring in DEVELOPMENT-SNAPSHOT-2022-09-06-a.

tayloraswift commented 2 years ago

reduced the reproducer to

struct C<T> where T:AdditiveArithmetic
{
    var x:[Int] 
    let y:T

    init() 
    {
        self.x = [0]
        self.y = .zero
    }

    var z:Int
    {
        _read
        {
            yield  self.x[0]
        }
        _modify
        {
            yield &self.x[0]
        }
    }
}
struct S 
{
    var c:C<Int>

    init()
    {
        self.c = .init()
        let _:Int = self.c.z
    }
}

despite not being used anywhere, the y member is required to cause the crash; without it no crash occurs.

atrick commented 2 years ago

rdar://100371186 (Issue #60841: LinearLifetimeChecker SIL verification found an ownership error in silgen emitConstructor)