最新更新:苹果已接受该问题的修复。最终解决方案在相关代码所有者 review 后进行了调整。
蛇年春节假期前,一位同事向我展示了一个由 use-after-free(释放后使用)错误导致的神秘崩溃。最近,我有时间深入研究这个问题,并发现崩溃是由 Swift 编译器的错误编译引起的。下面是最小复现代码,必须使用 -Osize 优化级别编译。我们可以通过在编译过程中启用地址检查器(address sanitizer)来检测 use-after-free 问题。
swift
 1let storage = ValueStorage()
 2storage.append(1)
 3// 运行时崩溃!
 4storage.append(2)
 5
 6public class ValueStorage {
 7
 8 private class Data {
 9
10 var values: [Int] = []
11
12 }
13
14 private var data = Data()
15
16 private func withAutoreleasingUnsafeMutableData<R>(_ body: (_ dataPtr: AutoreleasingUnsafeMutablePointer<Data>) throws -> R) rethrows -> R {
17 try withUnsafeMutablePointer(to: &data) { pointer in
18 try body(AutoreleasingUnsafeMutablePointer(pointer))
19 }
20 }
21
22 public func append(_ value: Int) {
23 withAutoreleasingUnsafeMutableData { dataPtr in
24 // 立即崩溃的行
25 dataPtr.pointee.values.append(value)
26 }
27 }
28
29}
在 Xcode 中打开地址检查器
在 Xcode 中打开地址检查器
UAF 问题出现
UAF 问题出现
有趣的是,将 AutoreleasingUnsafeMutablePointer 替换为 UnsafeMutablePointer 可以解决这个问题。
没有 UAF 问题
没有 UAF 问题

1
调查崩溃现场

反汇编有问题的程序后,我们可以发现 Array 的追加函数内联到了 ValueStorage.append 函数中。关键问题是程序在 Array 重新分配后没有重新获取 Array 的缓冲区对象。这导致使用寄存器 r12 计算的地址指向旧缓冲区(如果确实发生了重新分配)。这段反汇编代码可以被简化为:
asm
 1; 将 `self.values: [Int]` 加载到 r12
 2mov r15, qword [r13 + 0x10]
 3; r14 现在是 `self.values: [Int]`
 4mov r14, r15
 5; 将 Array 的缓冲区对象加载到 r12
 6mov r12, qword [r14 + 0x10]
 7; 重新分配,可能释放旧缓冲区对象
 8call Swift.Array._reserveCapacityAssumingUniqueBuffer
 9; 将新计数设置到旧缓冲区对象
10; use-after-free(释放后使用)发生
11mov qword [r12 + 0x10] rax
以下是 ValueStorage.append 的完整反汇编代码:
ValueStorage.append 的反汇编码
ValueStorage.append 的反汇编码
检查 Swift 标准库代码发现,其实际上是通过访问 self 上的 _buffer 属性来更新元素计数并插入新元素,而不是使用现有的旧缓冲区变量。Swift 编译器错误地删除了目标代码中重新获取 _buffer 对象的操作。
swift
 1// 标准库中的实现
 2@inlinable
 3@_semantics("array.mutate_unknown")
 4@_effects(notEscaping self.**)
 5internal mutating func _appendElementAssumeUniqueAndCapacity(
 6 _ oldCount: Int,
 7 newElement: __owned Element
 8) {
 9 // 使用 `self` 上的 `_buffer`
10 _buffer.mutableCount = oldCount &+ 1
11 // 使用 `self` 上的 `_buffer`
12 (_buffer.mutableFirstElementAddress + oldCount).initialize(to: newElement)
13}
14
15// 可能产生生成目标代码的假想实现
16@inlinable
17@_semantics("array.mutate_unknown")
18@_effects(notEscaping self.**)
19internal mutating func _appendElementAssumeUniqueAndCapacity(
20 _ oldCount: Int,
21 newElement: __owned Element
22 // 显式重用旧缓冲区
23 oldBufer: _Buffer
24) {
25 // 使用 `oldBuffer`
26 oldBufer.mutableCount = oldCount &+ 1
27 (oldBufer.mutableFirstElementAddress + oldCount).initialize(to: newElement)
28}

2
为什么 Swift 编译器删除了代码?

通过检查中间编译产物,我们可以在"优化 SIL"输出中找到初始错误编译,这可以通过在调用 swiftc 时添加 -emit-sil 参数获得。比较使用 AutoreleasingUnsafeMutablePointer(左侧)和 UnsafeMutablePointer(右侧)程序的优化 SIL,我们发现使用 AutoreleasingUnsafeMutablePointer 时,数组存储的关键 load 指令缺失。
崩溃与不崩溃的 SIL 对比
崩溃与不崩溃的 SIL 对比
为了确定哪个编译器过程移除了这个 load 指令,我们可以使用 -Xllvm 参数启用编译器中的调试打印。具体来说,我们可以使用 --sil-print-function 让编译器在每次有修改时打印指定函数的 SIL:
shell
1swiftc YOUR_SWIFT_SOURCE.swift -Osize \
2 -Xllvm '--sil-print-function=$MangledSwiftFunctionName'
此分析的关键日志可总结为:
sil
 1*** SIL function after #10338, stage MidLevel,Function, pass 37: CSE (cse)
 2// closure #1 in ValueStorage.append(_:)
 3...
 4sil private @$s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_ : $@convention(thin) @substituted <τ_0_0> (AutoreleasingUnsafeMutablePointer<Data>, Int) -> (@out τ_0_0, @error any Error) for <()> {
 5...
 6bb3(%17 : $Optional<AnyObject>):
 7 ...
 8 %26 = load %25 : $*Builtin.BridgeObject
 9 ...
10 %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36
11 %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()
12 ...
13 // load 指令仍然存在
14 %42 = load %25 : $*Builtin.BridgeObject
15 %43 = unchecked_ref_cast %42 : $Builtin.BridgeObject to $__ContiguousArrayStorageBase
16 %44 = ref_element_addr %43 : $__ContiguousArrayStorageBase, #__ContiguousArrayStorageBase.countAndCapacity
17 %45 = struct_element_addr %44 : $*_ArrayBody, #_ArrayBody._storage
18 %46 = struct_element_addr %45 : $*_SwiftArrayBodyStorage, #_SwiftArrayBodyStorage.count
19 store %41 to %46 : $*Int
20
21 *** SIL function after #10339, stage MidLevel,Function, pass 38: RedundantLoadElimination (redundant-load-elimination)
22// closure #1 in ValueStorage.append(_:)
23...
24sil private @$s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_ : $@convention(thin) @substituted <τ_0_0> (AutoreleasingUnsafeMutablePointer<Data>, Int) -> (@out τ_0_0, @error any Error) for <()> {
25...
26bb3(%17 : $Optional<AnyObject>):
27 ...
28 %26 = load %25 : $*Builtin.BridgeObject
29 ...
30 %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> ()
31 %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()
32 ...
33 // load 指令被消除了
34 %42 = unchecked_ref_cast %26 : $Builtin.BridgeObject to $__ContiguousArrayStorageBase
35 %43 = ref_element_addr %42 : $__ContiguousArrayStorageBase, #__ContiguousArrayStorageBase.countAndCapacity
36 %44 = struct_element_addr %43 : $*_ArrayBody, #_ArrayBody._storage
37 %45 = struct_element_addr %44 : $*_SwiftArrayBodyStorage, #_SwiftArrayBodyStorage.count
38 store %41 to %45 : $*Int
从这些日志中,我们可以清楚地看到 redundant load elimination (冗余 load 指令消除,下称 RLE) 过程删除了以下 load 指令:
sil
1%42 = load %25 : $*Builtin.BridgeObject

3
思考修复方案

要完善修复方案,我们首先需要了解 RLE。这个优化过程通过消除虚拟寄存器和实际寄存器的冗余 "get 和 set" 操作来优化代码。考虑下面这个有关虚拟寄存器的例子:
sil
1%1 = load %x
2%2 = store %1
3%3 = load %2
4return %3
一个更优的等效版本会立即返回 %1,因为 %2 只是一个中间结果。这是 RLE 应该正确处理的情况。这里,我们称之为情况 1。
sil
1%1 = load %x
2return %1
然而,考虑这个更复杂的情况:
sil
1%1 = load %x
2call print(%1)
3call Foo(%x)
4%3 = load %x // 我们能消除这一行吗?
5return %3
在这里,消除 %3 = load %x 取决于 Foo 是否修改了 %x 的内容。如果修改了,我们就不能直接返回 %1,因为 %3 = load %x 加载了更新后的内容。这里,我们称之为情况 2。
现在我们可以开始看看 Swift 中 RLE 的有关实现了。其入口位于 RedundantLoadElimination.swift 文件:
swift
1let redundantLoadElimination = FunctionPass(name: "redundant-load-elimination") {
2 (function: Function, context: FunctionPassContext) in
3 eliminateRedundantLoads(in: function, ignoreArrays: false, context)
4}
从这个入口点阅读代码,我们发现其算法与经典的 RLE 方法不同:
  1. 它在每个逆序的基本块中反向迭代所有指令,以查找所有 load 指令
  2. 对于每个 load,它检查其先前的指令以查找:
    • 可用的 store 指令进行优化(对应第一种情况)
    • 可用的 load 指令进行优化(对应第二种情况),如果当两个 load 之间没有对地址有副作用的函数调用的话
  3. 指令扫描有复杂度预算限制
比较 RLE 与 AutoreleasingUnsafeMutablePointer 和 UnsafeMutablePointer 的详细行为,我们可以获得如下日志:
text
1// AutoreleasingUnsafeMutablePointer
2eliminating redundant loads in function: $s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_
3...
4scanning prior instructions for the load: %42 = load %25 : $*Builtin.BridgeObject // users: %52, %43
5...
6visiting instruction: %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()
7transparent
8// %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36
text
1// UnsafeMutablePointer
2eliminating redundant loads in function: $s11NonCrashing12ValueStorageC6appendyySiF
3...
4scanning prior instructions for the load: %35 = load %18 : $*Builtin.BridgeObject // users: %45, %36
5...
6visiting instruction: %29 = apply %28(%27, %12) : $@convention(method) (Int, @inout Array<Int>) -> ()
7overwritten
8// %28 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36
这揭示了:
  • 使用 AutoreleasingUnsafeMutablePointer 时,RLE 认为数组重新分配函数对 load 指令操作数的地址是"透明的",从而启用了 load 指令消除
  • 使用 UnsafeMutablePointer 时,RLE 正确地认识到该函数会覆写地址,从而阻止了 load 指令消除
对于情况 2 所属场景,Swift 6 算法检查 load 的所有先前指令,以确定 load 操作数的源和 load 指令本身之间的函数调用的副作用。
副作用分析
副作用分析
在这里,关键的发现是 Swift 编译器仅在 load 操作数的最终源头有未知逃逸结果时才会考虑函数的副作用。通过在 AliasAnalysis.swift 中的函数设置断点,我发现了两种指针类型之间的关键差异:
getApplyEffect 函数
getApplyEffect 函数
  • 使用 AutoreleasingUnsafeMutablePointer 时,编译器检查 load 指令的操作数的定义源是否逃逸。当确定不逃逸时,编译器将错误地假设函数没有副作用。(line 376)
  • 使用 UnsafeMutablePointer 时,编译器将获取数组重新分配函数的全局副作用(可能来自 @_effects 属性。只有标记为 readOnly 或 readNone 的函数会被视为无副作用。)(line 381)
于是我们需要对第 371 行的 visit 函数进行进一步调查。该行会对 load 指令的操作数执行逃逸分析。下图说明了这个过程:
逃逸分析 1
逃逸分析 1
以下是逃逸分析的过程:
  1. 沿着 use-def chain (使用-定义链) 向上走,分析逃逸行为
  2. 每一步都将构建一个路径,表示如何从该点推导出 load 指令的操作数
但当我们遇到 AutoreleasingUnsafeMutablePointer 的 pointee getter 实现时情况会变得复杂。它在上述逃逸分析过程中是一个非平凡的案例:
swift
 1@frozen
 2public struct AutoreleasingUnsafeMutablePointer<Pointee /* TODO : class */>
 3 : _Pointer {
 4
 5 @inlinable
 6 public var pointee: Pointee {
 7 @_transparent get {
 8 // The memory addressed by this pointer contains a non-owning reference,
 9 // therefore we *must not* point an `UnsafePointer<AnyObject>` to
10 // it---otherwise we would allow the compiler to assume it has a +1
11 // refcount, enabling some optimizations that wouldn't be valid.
12 //
13 // Instead, we need to load the pointee as a +0 unmanaged reference. For
14 // an extra twist, `Pointee` is allowed (but not required) to be an
15 // optional type, so we actually need to load it as an optional, and
16 // explicitly handle the nil case.
17 let unmanaged =
18 UnsafePointer<Optional<Unmanaged<AnyObject>>>(_rawValue).pointee
19 return _unsafeReferenceCast(
20 unmanaged?.takeUnretainedValue(),
21 to: Pointee.self)
22 }
23 ...
24 }
25
26 ...
27}
实现细节揭示,AutoreleasingUnsafeMutablePointer 必须将引用从 Optional<Unmanaged<AnyObject>> 转换为 Pointee 类型以维持 +0 引用计数。这种转换通过 _unsafeReferenceCast 函数执行:
swift
1@_transparent
2@unsafe
3public func _unsafeReferenceCast<T, U>(_ x: T, to: U.Type) -> U {
4 return Builtin.castReference(x)
5}
编译器将其转换为 Builtin.castReference 函数,最终在 SIL 中表示为 unchecked_ref_cast 指令:
sil
1%y = unchecked_ref_cast %x : $SourceSILType to $DesintationSILType
因为 AutoreleasingUnsafeMutablePointer 的引入,使得 $SourceSILType 或 $DesintationSILType 可能是 Optional 类型,最终导致了问题:
sil
1%y = unchecked_ref_cast %x : $Optional<AnyObject> to $Data
此指令可以在 Optional 和非 Optional 类型之间进行转换,有效地包装或解包值。由于逃逸分析沿着 use-def chain (使用-定义链) 走,路径必须严格反映如何从定义点推导出 load 操作数,这些隐式的 Optional 转换会创建的路径不匹配,如图所示:
逃逸分析 2
逃逸分析 2
通过检查 WalkUtils.swift 中的 walkUpDefault 函数我们可以确认这一假设:该函数在向上走期间处理各种指令类型,但缺乏对 unchecked_ref_cast 中 Optional 转换的适当处理:
swift
 1public mutating func walkUpDefault(value def: Value, path: Path) -> WalkResult {
 2 switch def {
 3 ...
 4 case let urc as UncheckedRefCastInst:
 5 if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {
 6 // Sometimes `unchecked_ref_cast` is misused to cast between AnyObject and a class (instead of
 7 // init_existential_ref and open_existential_ref).
 8 // We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
 9 return rootDef(value: urc, path: path)
10 }
11 return walkUp(value: urc.fromInstance, path: path)
12 ...
13 }
14}

4
修复方案

解决方案是在 use-def chain (使用-定义链) 走向上游时考虑 Optional 和非 Optional 类型转换:
swift
 1public mutating func walkUpDefault(value def: Value, path: Path) -> WalkResult {
 2 switch def {
 3 ...
 4 case let urc as UncheckedRefCastInst:
 5 if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {
 6 // Sometimes `unchecked_ref_cast` is misused to cast between AnyObject and a class (instead of
 7 // init_existential_ref and open_existential_ref).
 8 // We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
 9 return rootDef(value: urc, path: path)
10 }
11 switch (urc.type.isOptional, urc.fromInstance.type.isOptional) {
12 case (true, false):
13 if let path = path.popIfMatches(.enumCase, index: 1) {
14 return walkUp(value: urc.fromInstance, path: path)
15 } else {
16 return unmatchedPath(value: urc.fromInstance, path: path)
17 }
18 case (false, true):
19 return walkUp(value: urc.fromInstance, path: path.push(.enumCase, index: 1))
20 default:
21 return walkUp(value: urc.fromInstance, path: path)
22 }
23 ...
24 }
25}
下图说明了这个修复如何适应逃逸分析过程中的 Optional 和非 Optional 类型转换。当逃逸分析过程遇到 Optional 和非 Optional 类型之间的 unchecked_ref_cast 时,该修复通过调整路径以考虑枚举 case 差异,确保了正确的路径转换。
逃逸分析 3
逃逸分析 3
在 def-use chain (定义-使用链) 分析中,walkDownDefault 函数也需要类似的更改:
swift
 1public mutating func walkDownDefault(value operand: Operand, path: Path) -> WalkResult {
 2 let instruction = operand.instruction
 3 switch instruction {
 4 ...
 5 case let urc as UncheckedRefCastInst:
 6 if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {
 7 // Sometimes `unchecked_ref_cast` is misused to cast between AnyObject and a class (instead of
 8 // init_existential_ref and open_existential_ref).
 9 // We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
10 return leafUse(value: operand, path: path)
11 }
12 switch (urc.type.isOptional, urc.fromInstance.type.isOptional) {
13 case (true, false):
14 return walkDownUses(ofValue: operand, path: path.push(.enumCase, index: 1))
15 case (false, true):
16 if let path = path.popIfMatches(.enumCase, index: 1) {
17 return walkDownUses(ofValue: operand, path: path)
18 } else {
19 return unmatchedPath(value: operand, path: path)
20 }
21 default:
22 return walkDownUses(ofValue: operand, path: path)
23 }
24 ...
25 }
26}
实施此修复后,编译使用 AutoreleasingUnsafeMutablePointer 的代码产生的日志显示 RLE 正确识别潜在的副作用:
text
1eliminating redundant loads in function: $s8Crashing12ValueStorageC6appendyySiF
2scanning prior instructions for the load: %45 = load %28 : $*Builtin.BridgeObject // users: %55, %46
3...
4visiting instruction: %39 = apply %38(%37, %23) : $@convention(method) (Int, @inout Array<Int>) -> ()
5overwritten
优化后的 SIL 现在保留了数组重新分配后的关键 load 指令:
sil
 1// ValueStorage.append(_:)
 2// Isolation: unspecified
 3sil [noinline] @$s8Crashing12ValueStorageC6appendyySiF : $@convention(method) (Int, @guaranteed ValueStorage) -> () {
 4[%1: noescape, read c*.v**, write c*.v**, copy c*.v**, destroy c*.v**]
 5[global: read,write,copy,destroy,allocate,deinit_barrier]
 6
 7...
 8
 9bb3(%20 : $Optional<AnyObject>):
10 ...
11 // function_ref specialized Array._reserveCapacityAssumingUniqueBuffer(oldCount:)
12 %38 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> ()
13 %39 = apply %38(%37, %23) : $@convention(method) (Int, @inout Array<Int>) -> ()
14 %45 = load %28 : $*Builtin.BridgeObject
15 ...
16} // end sil function '$s8Crashing12ValueStorageC6appendyySiF'

5
调试 Swift 编译器的技巧

5.1
获取 Swift 编译器的中间产物

要检查 Swift 编译器在每个编译阶段的中间表示:
shell
1swiftc YourSwiftSource.swift -Osize -emit-silgen > YourSwiftSource.silgen.sil # 生成原始 SIL
2swiftc YourSwiftSource.swift -Osize -emit-sil > YourSwiftSource.sil.sil # 生成优化 SIL
3swiftc YourSwiftSource.swift -Osize -emit-irgen > YourSwiftSource.irgen.ll # 生成原始 LLVM IR
4swiftc YourSwiftSource.swift -Osize -emit-ir > YourSwiftSource.ir.ll # 生成优化 LLVM IR

5.2
利用 LLVM 传递参数

LLVM 提供了许多可与 Swift 一起使用的传递参数:
shell
1# 打印指定函数的 SIL 更改
2swiftc -Xllvm '--sil-print-function=$MangledSwiftFunctionName'
3# 在运行每个 SIL 过程前打印其名称
4swiftc -Xllvm '--sil-print-pass-name=pass-name'
5# 打印内联到其他函数中的函数
6swiftc -Xllvm '--sil-print-inlining-callee=true'

5.3
构建 Swift 编译器

需要注意的是,在这篇文章中,我们是在调试编译器的行为细节,但 Swift 编程语言和标准库捆绑在一起。由于问题与 Array.append 函数的内联相关,我们应该构建一个 debug 版本的编译器和一个 release 版本的标准库,以确保 Array.append 的内联成本尽可能低。你可以使用以下命令实现:
shell
1utils/build-script --no-swift-stdlib-assertions \
2 --skip-ios --skip-tvos --skip-watchos --skip-build-benchmarks

5.4
SIL 和 LLVM IR 的语法高亮

你可以在 VS Code(或 Cursor)扩展市场中搜索"WeZZard"以获取相关 IDE 中的 SIL 语法高亮。
你可以在 VS Code(或 Cursor)扩展市场中找到 Ingvar Stepanyan 的"LLVM"扩展,以获取相关 IDE 中的 LLVM IR 语法高亮。