Skip to content

Commit 614334f

Browse files
committed
Swift Optimizer: add the StackProtection optimization
It decides which functions need stack protection. It sets the `needStackProtection` flags on all function which contain stack-allocated values for which an buffer overflow could occur. Within safe swift code there shouldn't be any buffer overflows. But if the address of a stack variable is converted to an unsafe pointer, it's not in the control of the compiler anymore. This means, if there is any `address_to_pointer` instruction for an `alloc_stack`, such a function is marked for stack protection. Another case is `index_addr` for non-tail allocated memory. This pattern appears if pointer arithmetic is done with unsafe pointers in swift code. If the origin of an unsafe pointer can only be tracked to a function argument, the pass tries to find the root stack allocation for such an argument by doing an inter-procedural analysis. If this is not possible, the fallback is to move the argument into a temporary `alloc_stack` and do the unsafe pointer operations on the temporary. rdar://93677524
1 parent 3c86aa4 commit 614334f

File tree

12 files changed

+1128
-11
lines changed

12 files changed

+1128
-11
lines changed

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_subdirectory(Analysis)
1515
add_subdirectory(DataStructures)
1616
add_subdirectory(InstructionPasses)
1717
add_subdirectory(PassManager)
18+
add_subdirectory(ModulePasses)
1819
add_subdirectory(FunctionPasses)
1920
add_subdirectory(TestPasses)
2021
add_subdirectory(Utilities)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
swift_compiler_sources(Optimizer
10+
StackProtection.swift
11+
)

0 commit comments

Comments
 (0)