swiftlang / swift

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

Swift 6 SIL memory lifetime failure: memory is not initialized, but should be #76055

Open vsarunas opened 2 weeks ago

vsarunas commented 2 weeks ago

Description

Swift 6 (swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-21) fails to build in release, debug and Swift 5.10 work fine.

With -sil-verify-all the function shown is:

3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote, MidLevel,Function, ClosureSpecialize, LowLevel,Function, LateLoopOpt, SIL Debug Info Generator } on SIL for Essentials)
4.  While running pass #2881673 SILFunctionTransform "SILCombine" on SILFunction "@$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5".
 for 'encodeString(_:in:length:)' (at /Users/sarunas/code//package-data-model/Local/Essentials/Sources/Essentials/Types/FinTech/Encoders.swift:57:1)
5.  While verifying SIL function "@$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5".

Reproduction

Stack dump

SIL memory lifetime failure in @$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5: memory is not initialized, but should be
memory location:   %14 = alloc_stack [lexical] [var_decl] $UInt32, var, name "value" // users: %41, %32, %46, %56
at instruction:   %41 = apply [nothrow] %40(%36, %14, %38) : $@convention(thin) (@inout UInt32, @guaranteed @noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()>) -> (@out (), @error Never)

in function:
// specialized encodeString<each A>(_:in:length:)
sil shared [always_inline] [ossa] @$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5 : $@convention(thin) (@guaranteed String, @pack_guaranteed Pack{UInt32}, Int) -> @pack_out Pack{UInt32} {
// %0 "$return_value"                             // user: %62
// %1 "string"                                    // users: %38, %4
// %2 "mem"                                       // user: %25
// %3 "length"                                    // users: %38, %13
bb0(%0 : $*Pack{UInt32}, %1 : @guaranteed $String, %2 : $*Pack{UInt32}, %3 : $Int):
  debug_value %1 : $String, let, name "string", argno 1 // id: %4
  %5 = alloc_stack [lexical] [var_decl] $UInt32, let, name "mem", argno 2 // users: %16, %24, %57
  %6 = integer_literal $Builtin.Word, 0           // user: %9
  %7 = integer_literal $Builtin.Word, 1           // user: %27
  %8 = integer_literal $Builtin.Word, 1           // user: %11
  br bb1(%6 : $Builtin.Word)                      // id: %9

// %10                                            // users: %27, %22, %11
bb1(%10 : $Builtin.Word):                         // Preds: bb3 bb0
  %11 = builtin "cmp_eq_Word"(%10 : $Builtin.Word, %8 : $Builtin.Word) : $Builtin.Int1 // user: %12
  cond_br %11, bb2, bb3                           // id: %12

bb2:                                              // Preds: bb1
  debug_value %3 : $Int, let, name "length", argno 3 // id: %13
  %14 = alloc_stack [lexical] [var_decl] $UInt32, var, name "value" // users: %41, %32, %46, %56
  %15 = alloc_stack $UInt32                       // users: %32, %17, %35
  %16 = load [trivial] %5 : $*UInt32              // user: %17
  store %16 to [trivial] %15 : $*UInt32           // id: %17
  %18 = integer_literal $Builtin.Word, 0          // user: %21
  %19 = integer_literal $Builtin.Word, 1          // user: %33
  %20 = integer_literal $Builtin.Word, 1          // user: %30
  br bb4(%18 : $Builtin.Word)                     // id: %21

bb3:                                              // Preds: bb1
  %22 = dynamic_pack_index %10 of $Pack{UInt32}   // users: %25, %23
  %23 = open_pack_element %22 of <each T where repeat each T : FixedWidthInteger> at <Pack{UInt32}>, shape $each T, uuid "1030B2CE-611E-11EF-A370-6E864D63D508" // users: %25, %24
  %24 = unchecked_addr_cast %5 : $*UInt32 to $*@pack_element("1030B2CE-611E-11EF-A370-6E864D63D508") each T // type-defs: %23; user: %26
  %25 = pack_element_get %22 of %2 : $*Pack{UInt32} as $*@pack_element("1030B2CE-611E-11EF-A370-6E864D63D508") each T // user: %26
  copy_addr %25 to [init] %24 : $*@pack_element("1030B2CE-611E-11EF-A370-6E864D63D508") each T // id: %26
  %27 = builtin "add_Word"(%10 : $Builtin.Word, %7 : $Builtin.Word) : $Builtin.Word // user: %28
  br bb1(%27 : $Builtin.Word)                     // id: %28

// %29                                            // users: %33, %30
bb4(%29 : $Builtin.Word):                         // Preds: bb5 bb2
  %30 = builtin "cmp_eq_Word"(%29 : $Builtin.Word, %20 : $Builtin.Word) : $Builtin.Int1 // user: %31
  cond_br %30, bb6, bb5                           // id: %31

bb5:                                              // Preds: bb4
  copy_addr %15 to [init] %14 : $*UInt32          // id: %32
  %33 = builtin "add_Word"(%29 : $Builtin.Word, %19 : $Builtin.Word) : $Builtin.Word // user: %34
  br bb4(%33 : $Builtin.Word)                     // id: %34

bb6:                                              // Preds: bb4
  dealloc_stack %15 : $*UInt32                    // id: %35
  %36 = alloc_stack $()                           // users: %41, %44
  // function_ref closure #1 in encodeString<each A>(_:in:length:)
  %37 = function_ref @$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFySwXEfU_ : $@convention(thin) @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer, @guaranteed String, Int) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()> // user: %38
  %38 = partial_apply [callee_guaranteed] [on_stack] %37(%1, %3) : $@convention(thin) @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer, @guaranteed String, Int) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()> // users: %42, %41
  %39 = alloc_stack $Never                        // user: %43
  // function_ref specialized withUnsafeMutableBytes<A, B, C>(of:_:)
  %40 = function_ref @$ss22withUnsafeMutableBytes2of_q0_xz_q0_Swq_YKXEtq_YKs5ErrorR_Ri_zRi_0_r1_lFs6UInt32V_s5NeverOytTg5 : $@convention(thin) (@inout UInt32, @guaranteed @noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()>) -> (@out (), @error Never) // user: %41
  %41 = apply [nothrow] %40(%36, %14, %38) : $@convention(thin) (@inout UInt32, @guaranteed @noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()>) -> (@out (), @error Never)
  destroy_value %38 : $@noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1> (UnsafeMutableRawBufferPointer) -> (@out τ_0_1, @error_indirect τ_0_0) for <Never, ()> // id: %42
  dealloc_stack %39 : $*Never                     // id: %43
  dealloc_stack %36 : $*()                        // id: %44
  %45 = alloc_stack $UInt32                       // users: %47, %63, %55
  %46 = load [trivial] %14 : $*UInt32             // user: %47
  store %46 to [trivial] %45 : $*UInt32           // id: %47
  %48 = integer_literal $Builtin.Word, 0          // user: %51
  %49 = integer_literal $Builtin.Word, 1          // user: %65
  %50 = integer_literal $Builtin.Word, 1          // user: %53
  br bb7(%48 : $Builtin.Word)                     // id: %51

// %52                                            // users: %65, %60, %53
bb7(%52 : $Builtin.Word):                         // Preds: bb9 bb6
  %53 = builtin "cmp_eq_Word"(%52 : $Builtin.Word, %50 : $Builtin.Word) : $Builtin.Int1 // user: %54
  cond_br %53, bb8, bb9                           // id: %54

bb8:                                              // Preds: bb7
  dealloc_stack %45 : $*UInt32                    // id: %55
  dealloc_stack %14 : $*UInt32                    // id: %56
  dealloc_stack %5 : $*UInt32                     // id: %57
  %58 = tuple ()                                  // user: %59
  return %58 : $()                                // id: %59

bb9:                                              // Preds: bb7
  %60 = dynamic_pack_index %52 of $Pack{UInt32}   // users: %62, %61
  %61 = open_pack_element %60 of <each T where repeat each T : FixedWidthInteger> at <Pack{UInt32}>, shape $each T, uuid "1030B1FC-611E-11EF-A370-6E864D63D508" // users: %63, %62
  %62 = pack_element_get %60 of %0 : $*Pack{UInt32} as $*@pack_element("1030B1FC-611E-11EF-A370-6E864D63D508") each T // user: %64
  %63 = unchecked_addr_cast %45 : $*UInt32 to $*@pack_element("1030B1FC-611E-11EF-A370-6E864D63D508") each T // type-defs: %61; user: %64
  copy_addr [take] %63 to [init] %62 : $*@pack_element("1030B1FC-611E-11EF-A370-6E864D63D508") each T // id: %64
  %65 = builtin "add_Word"(%52 : $Builtin.Word, %49 : $Builtin.Word) : $Builtin.Word // user: %66
  br bb7(%65 : $Builtin.Word)                     // id: %66
} // end sil function '$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5'

[...]

1.  Apple Swift version 6.0-dev (LLVM c3efe9282719c35, Swift e26305bb70cce53)
2.  Compiling with effective version 5.10
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote, MidLevel,Function, ClosureSpecialize, LowLevel,Function, LateLoopOpt, SIL Debug Info Generator } on SIL for Essentials)
4.  While running pass #2881751 SILFunctionTransform "NonTransparentFunctionOwnershipModelEliminator" on SILFunction "@$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5".
 for 'encodeString(_:in:length:)' (at /Users/sarunas/code//package-data-model/Local/Essentials/Sources/Essentials/Types/FinTech/Encoders.swift:57:1)
5.  Found verification error when verifying before lowering ownership. Please re-run with -sil-verify-all to identify the actual pass that introduced the verification error.
6.  While verifying SIL function "@$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5".
 for 'encodeString(_:in:length:)' (at /Users/sarunas/code//package-data-model/Local/Essentials/Sources/Essentials/Types/FinTech/Encoders.swift:57:1)
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):
0  swift-frontend           0x0000000107dfeafc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107dfd2c8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107dff144 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018af82584 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018af51c20 pthread_kill + 288
5  libsystem_c.dylib        0x000000018ae5ea30 abort + 180
6  swift-frontend           0x0000000108178824 (anonymous namespace)::MemoryLifetimeVerifier::reportError(llvm::Twine const&, int, swift::SILInstruction*) (.cold.2) + 0
7  swift-frontend           0x00000001038cd038 (anonymous namespace)::MemoryLifetimeVerifier::requireBitsSetForArgument(llvm::SmallBitVector const&, swift::Operand*) + 0
8  swift-frontend           0x00000001038cd26c (anonymous namespace)::MemoryLifetimeVerifier::requireBitsSetForArgument(llvm::SmallBitVector const&, swift::Operand*) + 564
9  swift-frontend           0x00000001038ccbc4 (anonymous namespace)::MemoryLifetimeVerifier::checkFuncArgument(llvm::SmallBitVector&, swift::Operand&, swift::SILArgumentConvention, swift::SILInstruction*) + 68
10 swift-frontend           0x00000001038cb164 (anonymous namespace)::MemoryLifetimeVerifier::checkBlock(swift::SILBasicBlock*, llvm::SmallBitVector&) + 380
11 swift-frontend           0x00000001038ca158 (anonymous namespace)::MemoryLifetimeVerifier::verify() + 3668
12 swift-frontend           0x00000001038c9248 swift::SILFunction::verifyMemoryLifetime(swift::CalleeCache*) + 128
13 swift-frontend           0x00000001038d81a0 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 11912
14 swift-frontend           0x00000001038d12b0 swift::SILFunction::verify(swift::CalleeCache*, bool, bool, bool) const + 204
15 swift-frontend           0x00000001033528b0 (anonymous namespace)::OwnershipModelEliminator::run() + 268
16 swift-frontend           0x0000000103359178 swift::SILPassManager::runPassOnFunction(unsigned int, swift::SILFunction*) + 1432
17 swift-frontend           0x0000000103359fe4 swift::SILPassManager::runFunctionPasses(unsigned int, unsigned int) + 1060
18 swift-frontend           0x0000000103357158 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
19 swift-frontend           0x00000001033570d8 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
20 swift-frontend           0x00000001033924e0 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
21 swift-frontend           0x0000000103372a3c swift::ExecuteSILPipelineRequest::OutputType swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()>(swift::ExecuteSILPipelineRequest const&, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()) + 204
22 swift-frontend           0x0000000103357334 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 64
23 swift-frontend           0x00000001033750b8 swift::runSILOptimizationPasses(swift::SILModule&) + 156
24 swift-frontend           0x0000000102b93dcc swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 636
25 swift-frontend           0x0000000102952e7c performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 796
26 swift-frontend           0x0000000102952778 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1216
27 swift-frontend           0x000000010295e254 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
28 swift-frontend           0x0000000102954420 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
29 swift-frontend           0x0000000102953af4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2344
30 swift-frontend           0x0000000102768e68 swift::mainEntry(int, char const**) + 3096
31 dyld                     0x000000018abc7154 start + 2476

Expected behavior

Compile

Environment

Apple Swift version 6.0-dev (LLVM c3efe9282719c35, Swift e26305bb70cce53)
Target: arm64-apple-macosx14.0

Additional information

No response

hassila commented 2 weeks ago

A small update to help narrow it down:

The build works on macOS in release mode with the toolchain shipped with Xcode 16b6:

hassila@min ~/g/p/L/OrdoEssentials (main)> swift --version
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx14.0

It also works in debug mode on both Linux and macOS with the toolchain snapshot above (2024-08-21).

It always fails in release mode on both Linux and macOS with the toolchain snapshot above (2024-08-21).

hassila commented 2 weeks ago

Just reproduced with 08-22 toolchain too, just changing this setting in Xcode 16b6 makes the toolchain crash - the built in version is fine.

image
vsarunas commented 2 weeks ago

Working around with adding to swiftSettings unsafeFlags:

        "-Xllvm", "-sil-disable-pass=NonTransparentFunctionOwnershipModelEliminator",
        "-Xllvm", "-sil-disable-pass-only-function=$s14Essentials12encodeString_2in6lengthxxQp_tSS_xxQp_tSitRvzs17FixedWidthIntegerRzlFs6UInt32V_QP_Tg5"