Files
swift-mirror/test/SILOptimizer/arc_crash.swift
Erik Eckstein 5bc036661c SIL optimizer: add the LetPropertyLowering pass
It lowers let property accesses of classes.
Lowering consists of two tasks:

* In class initializers, insert `end_init_let_ref` instructions at places where all let-fields are initialized.
  This strictly separates the life-range of the class into a region where let fields are still written during
  initialization and a region where let fields are truly immutable.

* Add the `[immutable]` flag to all `ref_element_addr` instructions (for let-fields) which are in the "immutable"
  region. This includes the region after an inserted `end_init_let_ref` in an class initializer, but also all
  let-field accesses in other functions than the initializer and the destructor.

This pass should run after DefiniteInitialization but before RawSILInstLowering (because it relies on `mark_uninitialized` still present in the class initializer).
Note that it's not mandatory to run this pass. If it doesn't run, SIL is still correct.

Simplified example (after lowering):

  bb0(%0 : @owned C):                           // = self of the class initializer
    %1 = mark_uninitialized %0
    %2 = ref_element_addr %1, #C.l              // a let-field
    store %init_value to %2
    %3 = end_init_let_ref %1                    // inserted by lowering
    %4 = ref_element_addr [immutable] %3, #C.l  // set to immutable by lowering
    %5 = load %4
2023-09-19 15:10:30 +02:00

55 lines
1.6 KiB
Swift

// RUN: %target-swift-frontend -O %s -parse-as-library -emit-sil -enforce-exclusivity=none -Xllvm -sil-disable-pass=function-signature-opts | %FileCheck %s
// REQUIRES: swift_in_compiler
// Test ARC optimizations on source level tests that have been
// miscompiled and crash (e.g. because of use-after-free).
// -----------------------------------------------------------------------------
// rdar://74469299 (ARC miscompile: EscapeAnalysis::mayReleaseContent;
// potential use-after-free)
// -----------------------------------------------------------------------------
public class Base {
var i = 3
init() {}
}
public class Node : Base {
var node: Base
@inline(never)
init(node: Base) { self.node = node }
}
struct Queue {
var node: Node
}
@inline(never)
func useQueue(q: __owned Queue) {}
@inline(never)
func useNode(n: Base) -> Int {
return n.i
}
// CHECK-LABEL: sil [noinline] @$s9arc_crash14testMayReleaseAA4BaseCyF : $@convention(thin) () -> @owned Base {
// CHECK: [[BASE:%.*]] = alloc_ref $Base
// CHECK: [[EI:%.*]] = end_init_let_ref [[BASE]]
// CHECK: strong_retain [[EI]] : $Base
// CHECK: apply %{{.*}} : $@convention(thin) (@owned Queue) -> ()
// CHECK-LABEL: } // end sil function '$s9arc_crash14testMayReleaseAA4BaseCyF'
@inline(never)
public func testMayRelease() -> Base {
let n2 = Base()
let n1 = Node(node: n2)
let q = Queue(node: n1)
// n2 must not be release before useQueue.
useQueue(q: q)
return n2
}
// This crashes when testMayRelease releases the object too early.
// print("Object:")
// print(testMayRelease())
// -----------------------------------------------------------------------------