Skip to content

Commit 97b2354

Browse files
committed
SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins: * `Builtin.unprotectedAddressOf` * `Builtin.unprotectedAddressOfBorrow`
1 parent fdca208 commit 97b2354

35 files changed

+290
-76
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ final public
341341
class RawPointerToRefInst : SingleValueInstruction, UnaryInstruction {}
342342

343343
final public
344-
class AddressToPointerInst : SingleValueInstruction, UnaryInstruction {}
344+
class AddressToPointerInst : SingleValueInstruction, UnaryInstruction {
345+
public var needsStackProtection: Bool {
346+
AddressToPointerInst_needsStackProtection(bridged) != 0
347+
}
348+
}
345349

346350
final public
347351
class PointerToAddressInst : SingleValueInstruction, UnaryInstruction {}
@@ -350,6 +354,10 @@ final public
350354
class IndexAddrInst : SingleValueInstruction {
351355
public var base: Value { operands[0].value }
352356
public var index: Value { operands[1].value }
357+
358+
public var needsStackProtection: Bool {
359+
IndexAddrInst_needsStackProtection(bridged) != 0
360+
}
353361
}
354362

355363
final public

docs/SIL.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,7 +4314,7 @@ index_addr
43144314
``````````
43154315
::
43164316

4317-
sil-instruction ::= 'index_addr' sil-operand ',' sil-operand
4317+
sil-instruction ::= 'index_addr' ('[' 'stack_protection' ']')? sil-operand ',' sil-operand
43184318

43194319
%2 = index_addr %0 : $*T, %1 : $Builtin.Int<n>
43204320
// %0 must be of an address type $*T
@@ -4330,6 +4330,9 @@ special behavior in this regard, unlike ``char*`` or ``void*`` in C.) It is
43304330
also undefined behavior to index out of bounds of an array, except to index
43314331
the "past-the-end" address of the array.
43324332

4333+
The ``stack_protection`` flag indicates that stack protection is done for
4334+
the pointer origin.
4335+
43334336
tail_addr
43344337
`````````
43354338
::
@@ -6564,7 +6567,7 @@ address_to_pointer
65646567
``````````````````
65656568
::
65666569

6567-
sil-instruction ::= 'address_to_pointer' sil-operand 'to' sil-type
6570+
sil-instruction ::= 'address_to_pointer' ('[' 'stack_protection' ']')? sil-operand 'to' sil-type
65686571

65696572
%1 = address_to_pointer %0 : $*T to $Builtin.RawPointer
65706573
// %0 must be of an address type $*T
@@ -6576,6 +6579,9 @@ an address equivalent to ``%0``. It is undefined behavior to cast the
65766579
``RawPointer`` to any address type other than its original address type or
65776580
any `layout compatible types`_.
65786581

6582+
The ``stack_protection`` flag indicates that stack protection is done for
6583+
the pointer origin.
6584+
65796585
pointer_to_address
65806586
``````````````````
65816587
::

include/swift/AST/Builtins.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,25 @@ BUILTIN_SIL_OPERATION(ReinterpretCast, "reinterpretCast", Special)
339339
/// only valid for the duration of the original binding.
340340
BUILTIN_SIL_OPERATION(AddressOf, "addressof", Special)
341341

342+
/// unprotectedAddressOf (inout T) -> Builtin.RawPointer
343+
/// Returns a RawPointer pointing to a physical lvalue. The returned pointer is
344+
/// only valid for the duration of the original binding.
345+
/// In contrast to `addressof`, this builtin doesn't trigger an insertion of
346+
/// stack protectors.
347+
BUILTIN_SIL_OPERATION(UnprotectedAddressOf, "unprotectedAddressOf", Special)
348+
342349
/// addressOfBorrow (__shared T) -> Builtin.RawPointer
343350
/// Returns a RawPointer pointing to a borrowed rvalue. The returned pointer is only
344351
/// valid within the scope of the borrow.
345352
BUILTIN_SIL_OPERATION(AddressOfBorrow, "addressOfBorrow", Special)
346353

354+
/// unprotectedAddressOfBorrow (__shared T) -> Builtin.RawPointer
355+
/// Returns a RawPointer pointing to a borrowed rvalue. The returned pointer is only
356+
/// valid within the scope of the borrow.
357+
/// In contrast to `addressOfBorrow`, this builtin doesn't trigger an insertion of
358+
/// stack protectors.
359+
BUILTIN_SIL_OPERATION(UnprotectedAddressOfBorrow, "unprotectedAddressOfBorrow", Special)
360+
347361
/// GepRaw(Builtin.RawPointer, Builtin.Word) -> Builtin.RawPointer
348362
///
349363
/// Adds index bytes to a base pointer.

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
362362

363363
llvm::StringRef CondFailInst_getMessage(BridgedInstruction cfi);
364364
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
365+
SwiftInt AddressToPointerInst_needsStackProtection(BridgedInstruction atp);
366+
SwiftInt IndexAddrInst_needsStackProtection(BridgedInstruction ia);
365367
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
366368
BridgedFunction FunctionRefBaseInst_getReferencedFunction(BridgedInstruction fri);
367369
llvm::StringRef StringLiteralInst_getValue(BridgedInstruction sli);

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,9 +1094,9 @@ class SILBuilder {
10941094
}
10951095

10961096
AddressToPointerInst *createAddressToPointer(SILLocation Loc, SILValue Op,
1097-
SILType Ty) {
1097+
SILType Ty, bool needsStackProtection) {
10981098
return insert(new (getModule()) AddressToPointerInst(
1099-
getSILDebugLocation(Loc), Op, Ty));
1099+
getSILDebugLocation(Loc), Op, Ty, needsStackProtection));
11001100
}
11011101

11021102
PointerToAddressInst *
@@ -2149,9 +2149,9 @@ class SILBuilder {
21492149
//===--------------------------------------------------------------------===//
21502150

21512151
IndexAddrInst *createIndexAddr(SILLocation Loc, SILValue Operand,
2152-
SILValue Index) {
2152+
SILValue Index, bool needsStackProtection) {
21532153
return insert(new (getModule()) IndexAddrInst(getSILDebugLocation(Loc),
2154-
Operand, Index));
2154+
Operand, Index, needsStackProtection));
21552155
}
21562156

21572157
TailAddrInst *createTailAddr(SILLocation Loc, SILValue Operand,

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,8 @@ SILCloner<ImplClass>::visitAddressToPointerInst(AddressToPointerInst *Inst) {
14611461
recordClonedInstruction(
14621462
Inst, getBuilder().createAddressToPointer(getOpLocation(Inst->getLoc()),
14631463
getOpValue(Inst->getOperand()),
1464-
getOpType(Inst->getType())));
1464+
getOpType(Inst->getType()),
1465+
Inst->needsStackProtection()));
14651466
}
14661467

14671468
template<typename ImplClass>
@@ -2638,7 +2639,8 @@ SILCloner<ImplClass>::visitIndexAddrInst(IndexAddrInst *Inst) {
26382639
recordClonedInstruction(
26392640
Inst, getBuilder().createIndexAddr(getOpLocation(Inst->getLoc()),
26402641
getOpValue(Inst->getBase()),
2641-
getOpValue(Inst->getIndex())));
2642+
getOpValue(Inst->getIndex()),
2643+
Inst->needsStackProtection()));
26422644
}
26432645

26442646
template<typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5350,9 +5350,18 @@ class AddressToPointerInst
53505350
ConversionInst>
53515351
{
53525352
friend SILBuilder;
5353+
USE_SHARED_UINT8;
53535354

5354-
AddressToPointerInst(SILDebugLocation DebugLoc, SILValue Operand, SILType Ty)
5355-
: UnaryInstructionBase(DebugLoc, Operand, Ty) {}
5355+
AddressToPointerInst(SILDebugLocation DebugLoc, SILValue Operand, SILType Ty,
5356+
bool needsStackProtection)
5357+
: UnaryInstructionBase(DebugLoc, Operand, Ty) {
5358+
sharedUInt8().AddressToPointerInst.needsStackProtection = needsStackProtection;
5359+
}
5360+
5361+
public:
5362+
bool needsStackProtection() const {
5363+
return sharedUInt8().AddressToPointerInst.needsStackProtection;
5364+
}
53565365
};
53575366

53585367
/// PointerToAddressInst - Convert a Builtin.RawPointer value to a SIL address.
@@ -8132,11 +8141,20 @@ class IndexAddrInst
81328141
: public InstructionBase<SILInstructionKind::IndexAddrInst,
81338142
IndexingInst> {
81348143
friend SILBuilder;
8144+
USE_SHARED_UINT8;
81358145

81368146
enum { Base, Index };
81378147

8138-
IndexAddrInst(SILDebugLocation DebugLoc, SILValue Operand, SILValue Index)
8139-
: InstructionBase(DebugLoc, Operand->getType(), Operand, Index) {}
8148+
IndexAddrInst(SILDebugLocation DebugLoc, SILValue Operand, SILValue Index,
8149+
bool needsStackProtection)
8150+
: InstructionBase(DebugLoc, Operand->getType(), Operand, Index) {
8151+
sharedUInt8().IndexAddrInst.needsStackProtection = needsStackProtection;
8152+
}
8153+
8154+
public:
8155+
bool needsStackProtection() const {
8156+
return sharedUInt8().IndexAddrInst.needsStackProtection;
8157+
}
81408158
};
81418159

81428160
/// TailAddrInst - like IndexingInst, but aligns-up the resulting address to a

include/swift/SIL/SILNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ class alignas(8) SILNode :
194194
SHARED_FIELD(EndAccessInst, bool aborting);
195195
SHARED_FIELD(RefElementAddrInst, bool immutable);
196196
SHARED_FIELD(RefTailAddrInst, bool immutable);
197+
SHARED_FIELD(AddressToPointerInst, bool needsStackProtection);
198+
SHARED_FIELD(IndexAddrInst, bool needsStackProtection);
197199
SHARED_FIELD(HopToExecutorInst, bool mandatory);
198200
SHARED_FIELD(DestroyValueInst, bool poisonRefs);
199201
SHARED_FIELD(EndCOWMutationInst, bool keepUnique);

lib/AST/Builtins.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,13 +2691,15 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
26912691
return getReinterpretCastOperation(Context, Id);
26922692

26932693
case BuiltinValueKind::AddressOf:
2694+
case BuiltinValueKind::UnprotectedAddressOf:
26942695
if (!Types.empty()) return nullptr;
26952696
return getAddressOfOperation(Context, Id);
26962697

26972698
case BuiltinValueKind::LegacyCondFail:
26982699
return getLegacyCondFailOperation(Context, Id);
26992700

27002701
case BuiltinValueKind::AddressOfBorrow:
2702+
case BuiltinValueKind::UnprotectedAddressOfBorrow:
27012703
if (!Types.empty()) return nullptr;
27022704
return getAddressOfBorrowOperation(Context, Id);
27032705

lib/SIL/IR/SILInstruction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,8 @@ namespace {
663663
}
664664

665665
bool visitIndexAddrInst(IndexAddrInst *RHS) {
666-
// We have already compared the operands/types, so we should have equality
667-
// at this point.
668-
return true;
666+
auto *lhs = cast<IndexAddrInst>(LHS);
667+
return lhs->needsStackProtection() == RHS->needsStackProtection();
669668
}
670669

671670
bool visitTailAddrInst(TailAddrInst *RHS) {
@@ -772,7 +771,8 @@ namespace {
772771
}
773772

774773
bool visitAddressToPointerInst(AddressToPointerInst *RHS) {
775-
return true;
774+
auto *lhs = cast<AddressToPointerInst>(LHS);
775+
return lhs->needsStackProtection() == RHS->needsStackProtection();
776776
}
777777

778778
bool visitPointerToAddressInst(PointerToAddressInst *RHS) {

0 commit comments

Comments
 (0)