# Stack Protectors in Swift

**URL:** <https://forums.swift.org/t/stack-protectors-in-swift/60163>\
**Category:** Compiler\
**Created:** [September 7, 2022, 2:54pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163 "2022-09-07T14:54:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Eckstein](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erik_eckstein/32/320_2.png) [@Erik\_Eckstein](https://forums.swift.org/u/Erik_Eckstein)\
**Post date:** [September 7, 2022, 2:54pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/1 "2022-09-07T14:54:22Z")

</div>

Stack protectors (also referred to as “stack canaries”) are additional code which is inserted to detect buffer overflows on the stack frame which can potentially overwrite the return address.  
The clang compiler already has this feature for C/C++ code. We'd like to add this security feature also in the Swift compiler.  
Swift is a safe language. So why do we need stack protectors in Swift? For two reasons:

- In case unsafe operations (e.g. `UnsafeMutablePointer`) are used on stack allocated variables

```swift
func foo() {
  var x = 0
  withUnsafeMutablePointer(to: &x) {
    $0[1] = 0xdeadbeef
  }
}

```

- If C/C++/ObjectiveC functions are called from Swift with pointers to stack allocated variables. Even if the buffer overflow happens on the C/C++/ObjectiveC side, clang’s stack protection doesn’t help here because the stack allocation is done on the Swift side.

```swift
func foo() {
  var x = 0
  withUnsafeMutablePointer(to: &x) {
    _ = memset($0, 0, 10)
  }
}

```

Stack allocation happens in swift in the following cases:

- for large or generic variables in local scopes
- contexts of non-escaping closures are allocated on the stack
- class allocation can be stack promoted (including Array buffers).

According to some initial experiments, turning on stack protectors unconditionally for all Swift functions (which do stack allocations), results in significant code size increases. This is bad.

So the question is how can we detect the cases listed above and only insert stack protectors in functions where a potential overflow can occur?

### Function-Local Analysis

The compiler can detect if an address of a stack allocated variable is in danger of causing an overflow within the scope of a function. Technically, it’s a simple analysis which checks all uses of `alloc_stack`, `alloc_ref [stack]` or `partial_apply [on_stack]` instructions and triggers if there is e.g. an `address_to_pointer` instruction in the use list.  
Functions which contain such SIL patterns are marked as needing a stack protector.

### Inter-procedural Analysis

It gets much more complicated if the stack allocation and the potential overflow are in different functions. This can happen if a stack address is passed to other functions, e.g. via an `inout`parameter. It would still be too expensive to do a conservative default approach and insert stack protectors for all functions which pass stack addresses to other functions.

We can do two things to solve this problem:

If all call paths from a function with a stack allocation to functions with potential overflows are statically known, the compiler can do an inter-procedural analysis and insert stack protectors accordingly.

```swift
func foo() {
  var x = 0 // allocation is done here
  bar(&x)
}

func bar(_ x: inout Int) { // only called from foo()
  withUnsafeMutablePointer(to: &x) {
    $0[1] = 0xdeadbeef // overflow happens here
  }
}

```

Sometimes it’s not the case that a call path is statically known. For example if a function call is dynamically dispatched, e.g. a vtable or witness table call. Or if a function is public and can be called from other modules.  
But in this case it’s still possible to let such functions handle stack protection locally: the value which is passed via inout can be moved to a function-local stack area and then the stack protection logic can be done in such a function instead of its caller.

```swift
public func bar(_ x: inout Int) { // callers are unknown
  var tmp = x // move, inserted by the compiler
  withUnsafeMutablePointer(to: &tmp) {
    $0[1] = 0xdeadbeef
  }
  x = tmp // move, inserted by the compiler
}

```

Implementation: the inserted move is a `copy_addr [take] to [initialization]` which compiles down to a simple move. This even works with generic types (which are potentially not bitwise movable, e.g. weak ObjectiveC references). In this case the “initializeWithTake” value witness function is called which in most cases does a simple memcpy.

This approach has the benefit that we only have to pay for stack protection if there is a risk of an overflow, but not for the majority of regular “safe” Swift code.  
The downside is that moving a value to a temporary stack location and back again can be slow in case of large values. Also, we are losing pointer identity, but Swift does not guarantee that anyway.

### Escape Hatch

In some cases the user might want to opt out of stack protection, e.g. if moving a value causes too much performance overhead or if pointer identity must be preserved.

Stack protection for a specific address-to-pointer conversion can be disabled by using “unprotected” versions of `withUnsafePointer`:

- `_withUnprotectedUnsafeMutablePointer`
- `_withUnprotectedUnsafePointer`
- `_withUnprotectedUnsafeBytes`

Those functions are underscored for now, but might be go through a swift evolution proposal.

Originally, I considered a function attribute, e.g. `_noStackProtection`. But this is problematic because it might get lost during function inlining. Also, I think it’s better to control the behavior of specific pointers rather than all pointers in a function.

In addition to this fine grained opt-out mechanism, it’s also possible to disable stack protection with a command line option for the whole compilation unit.

### Debug Builds

Stack protection should also be enabled in debug builds. The main reason is that in case the compiler has to move an argument (see above), this should also be done in a debug build so that this behavior is tested (tests are usually run with debug builds).  
In fact, in debug builds all pointers will be moved, because the library functions `withUnsafePointer` et al are _not_ inlined and the move is done in those library functions.

### Initial Implementation

An initial implementation is here: [Support for stack protectors. by eeckstein · Pull Request #60933 · swiftlang/swift · GitHub](https://github.com/apple/swift/pull/60933)

---

<div class="post-metadata">

**Author:** ![tbkka](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tbkka/32/7532_2.png) [@tbkka](https://forums.swift.org/u/tbkka)\
**Post date:** [September 7, 2022, 3:17pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/2 "2022-09-07T15:17:57Z")

</div>

> [@Erik\_Eckstein](#):
>
> The downside is that moving a value to a temporary stack location and back again can be slow in case of large values. Also, we are losing pointer identity, but Swift does guarantee that anyway.

I think you mean "does _not_ guarantee pointer identity anyway."

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 7, 2022, 3:21pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/3 "2022-09-07T15:21:11Z")

</div>

> [@Erik\_Eckstein](#):
>
> So the question is how can we detect the cases listed above and only insert stack protectors in functions where a potential overflow can occur?

Can you share any numbers about this? Like, for some example projects, what is the code-size impact of unconditional stack protection, and how does that change with this conditional protection?

---

<div class="post-metadata">

**Author:** ![Erik\_Eckstein](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erik_eckstein/32/320_2.png) [@Erik\_Eckstein](https://forums.swift.org/u/Erik_Eckstein)\
**Post date:** [September 8, 2022, 8:05am UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/4 "2022-09-08T08:05:30Z")

</div>

We saw code size increases of ~5% for unconditional stack protection. With this approach there is no cost at all, unless unsafe pointer operations are used.

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [September 8, 2022, 4:55pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/5 "2022-09-08T16:55:09Z")

</div>

Was that on publicly available projects? If not, could you share approximately how many LOCs these projects had?

---

<div class="post-metadata">

**Author:** ![Erik\_Eckstein](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erik_eckstein/32/320_2.png) [@Erik\_Eckstein](https://forums.swift.org/u/Erik_Eckstein)\
**Post date:** [September 8, 2022, 5:50pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/6 "2022-09-08T17:50:19Z")

</div>

Sorry, no it's not a public project.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [November 25, 2022, 7:19pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/7 "2022-11-25T19:19:06Z")

</div>

I've noticed this runtime stack protection happening even in places where I would expect the compiler to provide things safe statically.

I'm using `withUnsafeBytes(of: self)` to work around [the "outlined init with take" issue](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187). It's important that I do that, because just applying it to one tiny enum improved the performance of lots of complex processing by up to 24%. Obviously I don't _like_ to write this kind of code, and I don't recommend anybody does that by default, but it's a work-around for a demonstrable issue.

And it's still an issue, as of the latest nightly:

```swift
enum MyEnum {
    case one
    case four
}

struct HasAnEnum {
    var something: String
    var somethingTwo: String
    var value: MyEnum?
}

func test(_ input: HasAnEnum) -> Int {
    // This load results in very expensive runtime calls!
    let x = input.value
    if case .four = x { return 4 }
    return -1
}

func test2(_ input: HasAnEnum) -> Int {
    // Super ugly, but we can wrap this in a computed property to contain it.
    // It's so much faster.
    let x = withUnsafeBytes(of: input) {
        let offset = MemoryLayout<HasAnEnum>.offset(of: \.value)!
        return $0.load(fromByteOffset: offset, as: MyEnum?.self)
    }
    if case .four = x { return 4 }
    return -1
}

```

[Godbolt](https://swift.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,selection:(endColumn:2,endLineNumber:13,positionColumn:2,positionLineNumber:13,selectionStartColumn:1,selectionStartLineNumber:7,startColumn:1,startLineNumber:7),source:'func+test(_+input:+HasAnEnum)+-%3E+Int+%7B%0A++++let+x+%3D+input.value%0A++++if+case+.four+%3D+x+%7B+return+4+%7D%0A++++return+-1%0A%7D%0A%0Afunc+test2(_+input:+HasAnEnum)+-%3E+Int+%7B%0A++++let+x+%3D+withUnsafeBytes(of:+input)+%7B%0A++++++++%240.load(fromByteOffset:+MemoryLayout%3CHasAnEnum%3E.offset(of:+%5C.value)!!,+as:+MyEnum%3F.self)%0A++++%7D%0A++++if+case+.four+%3D+x+%7B+return+4+%7D%0A++++return+-1%0A%7D%0A%0Aenum+MyEnum+%7B%0A++++case+one%0A++++case+four%0A%7D%0A%0Astruct+HasAnEnum+%7B%0A++++var+something:+String%0A++++var+somethingTwo:+String%0A++++var+value:+MyEnum%3F%0A%7D'),l:'5',n:'0',o:'Swift+source+%231',t:'0')),k:56.86684073107051,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:swiftnightly,deviceViewOpen:'1',filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,libs:!(),options:'-O',selection:(endColumn:4,endLineNumber:16,positionColumn:4,positionLineNumber:16,selectionStartColumn:4,selectionStartLineNumber:16,startColumn:4,startLineNumber:16),source:1),l:'5',n:'0',o:'+x86-64+swiftc+nightly+(Editor+%231)',t:'0')),k:43.133159268929504,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Unfortunately, I'm seeing that this load requires runtime stack protection checks. It seems strange - we get the offset from `MemoryLayout`, and the compiler constant-folds that (not seeing any runtime calls), and there doesn't seem to be any complex inter-procedural stuff going on. I'd expect the compiler to know that this is in-bounds.

Is this something that could reasonably be improved? Or does this involve some complex analysis that I'm not appreciating?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [January 12, 2023, 2:08pm UTC](https://forums.swift.org/t/stack-protectors-in-swift/60163/8 "2023-01-12T14:08:02Z")

</div>

We seem to be missing `_withUnprotectedUnsafeMutableBytes` (as a counterpart of `withUnsafeMutableBytes`). Only the non-mutating version is available.

Here's another example of a function which the compiler thinks needs stack protection, even though clearly no out-of-bounds writes can occur.

```swift
func test() -> UInt64 {
    UInt64(repeatingByte: 0x2F)
}

extension UInt64 {

  /// Creates an 8-byte integer, each of which is equal to the given byte.
  ///
  @inlinable @inline(__always)
  internal init(repeatingByte byte: UInt8) {
    self = 0
    withUnsafeMutableBytes(of: &self) {
      $0[0] = byte
      $0[1] = byte
      $0[2] = byte
      $0[3] = byte
      $0[4] = byte
      $0[5] = byte
      $0[6] = byte
      $0[7] = byte
    }
  }
}

```

In fact, for the function `test()`, the compiler constant-folds all of the code using unsafe pointers, but _still_ keeps stack protection. Compiled on nightly at -O.

```swift
output.test() -> Swift.UInt64:
        push rax
        mov rax, qword ptr fs:[40]
        mov qword ptr [rsp], rax
        mov rax, qword ptr fs:[40]
        cmp rax, qword ptr [rsp]
        jne .LBB1_2
        movabs rax, 3399988123389603631 ; 0x2F2F2F2F2F2F2F2F
        pop rcx
        ret
.LBB1_2:
        call __stack_chk_fail@PLT

```

And on 5.7:

```swift
output.test() -> Swift.UInt64:
        movabs rax, 3399988123389603631
        ret

```

[Godbolt](https://swift.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,selection:(endColumn:19,endLineNumber:13,positionColumn:19,positionLineNumber:13,selectionStartColumn:19,selectionStartLineNumber:13,startColumn:19,startLineNumber:13),source:'func+test()+-%3E+UInt64+%7B%0A++++UInt64(repeatingByte:+0x2F)%0A%7D%0A%0Aextension+UInt64+%7B%0A%0A++///+Creates+an+8-byte+integer,+each+of+which+is+equal+to+the+given+byte.%0A++///%0A++@inlinable+@inline(__always)%0A++internal+init(repeatingByte+byte:+UInt8)+%7B%0A++++self+%3D+0%0A++++withUnsafeMutableBytes(of:+%26self)+%7B%0A++++++%240%5B0%5D+%3D+byte%0A++++++%240%5B1%5D+%3D+byte%0A++++++%240%5B2%5D+%3D+byte%0A++++++%240%5B3%5D+%3D+byte%0A++++++%240%5B4%5D+%3D+byte%0A++++++%240%5B5%5D+%3D+byte%0A++++++%240%5B6%5D+%3D+byte%0A++++++%240%5B7%5D+%3D+byte%0A++++%7D%0A++%7D%0A%7D%0A'),l:'5',n:'0',o:'Swift+source+%231',t:'0')),k:46.00522193211488,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:swiftnightly,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,libs:!(),options:'-O',selection:(endColumn:37,endLineNumber:16,positionColumn:37,positionLineNumber:16,selectionStartColumn:37,selectionStartLineNumber:16,startColumn:37,startLineNumber:16),source:1),l:'5',n:'0',o:'+x86-64+swiftc+nightly+(Editor+%231)',t:'0')),k:53.99477806788512,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)
