Files
swift-mirror/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift
Erik Eckstein b2b44c0d83 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
2022-09-08 08:42:25 +02:00

76 lines
2.8 KiB
Swift

//===--- PassRegistration.swift - Register optimization passes -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SIL
import OptimizerBridging
import Parse
@_cdecl("initializeSwiftModules")
public func initializeSwiftModules() {
registerSILClasses()
registerSwiftPasses()
initializeSwiftParseModules()
}
private func registerPass(
_ pass: ModulePass,
_ runFn: @escaping (@convention(c) (BridgedPassContext) -> ())) {
pass.name._withStringRef { nameStr in
SILPassManager_registerModulePass(nameStr, runFn)
}
}
private func registerPass(
_ pass: FunctionPass,
_ runFn: @escaping (@convention(c) (BridgedFunctionPassCtxt) -> ())) {
pass.name._withStringRef { nameStr in
SILPassManager_registerFunctionPass(nameStr, runFn)
}
}
private func registerPass<InstType: Instruction>(
_ pass: InstructionPass<InstType>,
_ runFn: @escaping (@convention(c) (BridgedInstructionPassCtxt) -> ())) {
pass.name._withStringRef { nameStr in
SILCombine_registerInstructionPass(nameStr, runFn)
}
}
private func registerSwiftPasses() {
// Module passes
registerPass(stackProtection, { stackProtection.run($0) })
// Function passes
registerPass(mergeCondFailsPass, { mergeCondFailsPass.run($0) })
registerPass(computeEffects, { computeEffects.run($0) })
registerPass(objCBridgingOptimization, { objCBridgingOptimization.run($0) })
registerPass(stackPromotion, { stackPromotion.run($0) })
registerPass(functionStackProtection, { functionStackProtection.run($0) })
registerPass(assumeSingleThreadedPass, { assumeSingleThreadedPass.run($0) })
registerPass(releaseDevirtualizerPass, { releaseDevirtualizerPass.run($0) })
// Instruction passes
registerPass(simplifyBeginCOWMutationPass, { simplifyBeginCOWMutationPass.run($0) })
registerPass(simplifyGlobalValuePass, { simplifyGlobalValuePass.run($0) })
registerPass(simplifyStrongRetainPass, { simplifyStrongRetainPass.run($0) })
registerPass(simplifyStrongReleasePass, { simplifyStrongReleasePass.run($0) })
// Test passes
registerPass(functionUsesDumper, { functionUsesDumper.run($0) })
registerPass(silPrinterPass, { silPrinterPass.run($0) })
registerPass(escapeInfoDumper, { escapeInfoDumper.run($0) })
registerPass(addressEscapeInfoDumper, { addressEscapeInfoDumper.run($0) })
registerPass(accessDumper, { accessDumper.run($0) })
registerPass(rangeDumper, { rangeDumper.run($0) })
registerPass(runUnitTests, { runUnitTests.run($0) })
}