Skip to content

Commit fdca208

Browse files
committed
SIL: add the SILFunction.needsStackProtection flag
Indicates that stack protectors are inserted into this function to detect stack related buffer overflows.
1 parent 78e1713 commit fdca208

File tree

9 files changed

+47
-5
lines changed

9 files changed

+47
-5
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/PassContext.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ extension RefCountingInst {
240240
RefCountingInst_setIsAtomic(bridged, isAtomic)
241241
}
242242
}
243+
244+
extension Function {
245+
func set(needStackProtection: Bool, _ context: PassContext) {
246+
context.notifyFunctionDataChanged()
247+
SILFunction_setNeedStackProtection(bridged, needStackProtection ? 1 : 0)
248+
}
249+
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
103103
SILFunction_isSwift51RuntimeAvailable(bridged) != 0
104104
}
105105

106+
public var needsStackProtection: Bool {
107+
SILFunction_needsStackProtection(bridged) != 0
108+
}
109+
106110
// Only to be called by PassContext
107111
public func _modifyEffects(_ body: (inout FunctionEffects) -> ()) {
108112
body(&effects)

docs/SIL.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ the top-level `switch_enum`_.
10531053
sil-function-attribute ::= '[weak_imported]'
10541054

10551055
Cross-module references to this function should always use weak linking.
1056+
::
1057+
1058+
sil-function-attribute ::= '[stack_protection]'
1059+
1060+
Stack protectors are inserted into this function to detect stack related
1061+
buffer overflows.
10561062
::
10571063

10581064
sil-function-attribute ::= '[available' sil-version-tuple ']'

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ SwiftInt SILFunction_isPossiblyUsedExternally(BridgedFunction function);
266266
SwiftInt SILFunction_isAvailableExternally(BridgedFunction function);
267267
SwiftInt SILFunction_hasSemanticsAttr(BridgedFunction function,
268268
llvm::StringRef attrName);
269+
SwiftInt SILFunction_needsStackProtection(BridgedFunction function);
270+
void SILFunction_setNeedStackProtection(BridgedFunction function,
271+
SwiftInt needSP);
269272

270273
llvm::StringRef SILGlobalVariable_getName(BridgedGlobalVar global);
271274
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);

include/swift/SIL/SILFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ class SILFunction
336336
/// Check whether this is a distributed method.
337337
unsigned IsDistributed : 1;
338338

339+
unsigned stackProtection : 1;
340+
339341
/// True if this function is inlined at least once. This means that the
340342
/// debug info keeps a pointer to this function.
341343
unsigned Inlined : 1;
@@ -832,6 +834,9 @@ class SILFunction
832834
IsDistributed = value;
833835
}
834836

837+
bool needsStackProtection() const { return stackProtection; }
838+
void setNeedStackProtection(bool needSP) { stackProtection = needSP; }
839+
835840
/// Get the DeclContext of this function.
836841
DeclContext *getDeclContext() const { return DeclCtxt; }
837842

lib/SIL/IR/SILFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ void SILFunction::init(
186186
this->IsDynamicReplaceable = isDynamic;
187187
this->ExactSelfClass = isExactSelfClass;
188188
this->IsDistributed = isDistributed;
189+
this->stackProtection = false;
189190
this->Inlined = false;
190191
this->Zombie = false;
191192
this->HasOwnership = true,

lib/SIL/IR/SILPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,9 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
31243124
if (!isExternalDeclaration() && hasOwnership())
31253125
OS << "[ossa] ";
31263126

3127+
if (needsStackProtection())
3128+
OS << "[stack_protection] ";
3129+
31273130
llvm::DenseMap<CanType, Identifier> sugaredTypeNames;
31283131
printSILFunctionNameAndType(OS, this, sugaredTypeNames, &PrintCtx);
31293132

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ static bool parseDeclSILOptional(bool *isTransparent,
983983
PerformanceConstraints *perfConstraints,
984984
bool *isLet,
985985
bool *isWeakImported,
986+
bool *needStackProtection,
986987
AvailabilityContext *availability,
987988
bool *isWithoutActuallyEscapingThunk,
988989
SmallVectorImpl<std::string> *Semantics,
@@ -1015,6 +1016,8 @@ static bool parseDeclSILOptional(bool *isTransparent,
10151016
*isCanonical = true;
10161017
else if (hasOwnershipSSA && SP.P.Tok.getText() == "ossa")
10171018
*hasOwnershipSSA = true;
1019+
else if (needStackProtection && SP.P.Tok.getText() == "stack_protection")
1020+
*needStackProtection = true;
10181021
else if (isThunk && SP.P.Tok.getText() == "thunk")
10191022
*isThunk = IsThunk;
10201023
else if (isThunk && SP.P.Tok.getText() == "signature_optimized_thunk")
@@ -6527,6 +6530,7 @@ bool SILParserState::parseDeclSIL(Parser &P) {
65276530
IsThunk_t isThunk = IsNotThunk;
65286531
SILFunction::Purpose specialPurpose = SILFunction::Purpose::None;
65296532
bool isWeakImported = false;
6533+
bool needStackProtection = false;
65306534
AvailabilityContext availability = AvailabilityContext::alwaysAvailable();
65316535
bool isWithoutActuallyEscapingThunk = false;
65326536
Inline_t inlineStrategy = InlineDefault;
@@ -6546,7 +6550,7 @@ bool SILParserState::parseDeclSIL(Parser &P) {
65466550
&isThunk, &isDynamic, &isDistributed, &isExactSelfClass,
65476551
&DynamicallyReplacedFunction, &AdHocWitnessFunction, &objCReplacementFor, &specialPurpose,
65486552
&inlineStrategy, &optimizationMode, &perfConstr, nullptr,
6549-
&isWeakImported, &availability,
6553+
&isWeakImported, &needStackProtection, &availability,
65506554
&isWithoutActuallyEscapingThunk, &Semantics,
65516555
&SpecAttrs, &ClangDecl, &MRK, &argEffectLocs, FunctionState, M) ||
65526556
P.parseToken(tok::at_sign, diag::expected_sil_function_name) ||
@@ -6779,7 +6783,7 @@ bool SILParserState::parseSILGlobal(Parser &P) {
67796783
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
67806784
nullptr, nullptr, nullptr, nullptr,
67816785
&isLet, nullptr, nullptr, nullptr, nullptr, nullptr,
6782-
nullptr, nullptr, nullptr, State, M) ||
6786+
nullptr, nullptr, nullptr, nullptr, State, M) ||
67836787
P.parseToken(tok::at_sign, diag::expected_sil_value_name) ||
67846788
P.parseIdentifier(GlobalName, NameLoc, /*diagnoseDollarPrefix=*/false,
67856789
diag::expected_sil_value_name) ||
@@ -6830,7 +6834,7 @@ bool SILParserState::parseSILProperty(Parser &P) {
68306834
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
68316835
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
68326836
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
6833-
nullptr, SP, M))
6837+
nullptr, nullptr, SP, M))
68346838
return true;
68356839

68366840
ValueDecl *VD;
@@ -6899,7 +6903,7 @@ bool SILParserState::parseSILVTable(Parser &P) {
68996903
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
69006904
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
69016905
nullptr, nullptr, nullptr, nullptr, nullptr,
6902-
nullptr, nullptr, VTableState, M))
6906+
nullptr, nullptr, nullptr, VTableState, M))
69036907
return true;
69046908

69056909
// Parse the class name.
@@ -7419,7 +7423,7 @@ bool SILParserState::parseSILWitnessTable(Parser &P) {
74197423
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
74207424
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
74217425
nullptr, nullptr, nullptr, nullptr, nullptr,
7422-
nullptr, nullptr, WitnessState, M))
7426+
nullptr, nullptr, nullptr, WitnessState, M))
74237427
return true;
74247428

74257429
// Parse the protocol conformance.

lib/SIL/Utils/SILBridging.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ SwiftInt SILFunction_hasSemanticsAttr(BridgedFunction function,
231231
return f->hasSemanticsAttr(attrName) ? 1 : 0;
232232
}
233233

234+
SwiftInt SILFunction_needsStackProtection(BridgedFunction function) {
235+
return castToFunction(function)->needsStackProtection() ? 1 : 0;
236+
}
237+
238+
void SILFunction_setNeedStackProtection(BridgedFunction function,
239+
SwiftInt needSP) {
240+
castToFunction(function)->setNeedStackProtection(needSP != 0);
241+
}
242+
234243
//===----------------------------------------------------------------------===//
235244
// SILBasicBlock
236245
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)