Skip to content

Commit a241cf5

Browse files
committed
[MemoryLifetimeVerifier] Permit leaks in dead-ends
1 parent 1eafced commit a241cf5

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,8 @@ class SILFunction
16921692
}
16931693

16941694
/// Verifies the lifetime of memory locations in the function.
1695-
void verifyMemoryLifetime(CalleeCache *calleeCache);
1695+
void verifyMemoryLifetime(CalleeCache *calleeCache,
1696+
DeadEndBlocks *deadEndBlocks);
16961697

16971698
/// Verifies ownership of the function.
16981699
/// Since we don't have complete lifetimes everywhere, computes DeadEndBlocks

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#define DEBUG_TYPE "sil-memory-lifetime-verifier"
1414
#include "swift/Basic/Assertions.h"
15-
#include "swift/SIL/MemoryLocations.h"
15+
#include "swift/SIL/ApplySite.h"
16+
#include "swift/SIL/BasicBlockDatastructures.h"
17+
#include "swift/SIL/BasicBlockUtils.h"
1618
#include "swift/SIL/BitDataflow.h"
1719
#include "swift/SIL/CalleeCache.h"
20+
#include "swift/SIL/MemoryLocations.h"
1821
#include "swift/SIL/SILBasicBlock.h"
1922
#include "swift/SIL/SILFunction.h"
20-
#include "swift/SIL/ApplySite.h"
21-
#include "swift/SIL/BasicBlockDatastructures.h"
2223
#include "llvm/Support/CommandLine.h"
2324

2425
using namespace swift;
@@ -43,6 +44,7 @@ class MemoryLifetimeVerifier {
4344

4445
SILFunction *function;
4546
CalleeCache *calleeCache;
47+
DeadEndBlocks *deadEndBlocks;
4648
MemoryLocations locations;
4749

4850
/// alloc_stack memory locations which are used for store_borrow.
@@ -140,11 +142,12 @@ class MemoryLifetimeVerifier {
140142
}
141143

142144
public:
143-
MemoryLifetimeVerifier(SILFunction *function, CalleeCache *calleeCache) :
144-
function(function),
145-
calleeCache(calleeCache),
146-
locations(/*handleNonTrivialProjections*/ true,
147-
/*handleTrivialLocations*/ true) {}
145+
MemoryLifetimeVerifier(SILFunction *function, CalleeCache *calleeCache,
146+
DeadEndBlocks *deadEndBlocks)
147+
: function(function), calleeCache(calleeCache),
148+
deadEndBlocks(deadEndBlocks),
149+
locations(/*handleNonTrivialProjections*/ true,
150+
/*handleTrivialLocations*/ true) {}
148151

149152
/// The main entry point to verify the lifetime of all memory locations in
150153
/// the function.
@@ -884,7 +887,12 @@ void MemoryLifetimeVerifier::checkBlock(SILBasicBlock *block, Bits &bits) {
884887
}
885888
case SILInstructionKind::DeallocStackInst: {
886889
SILValue opVal = cast<DeallocStackInst>(&I)->getOperand();
887-
requireBitsClear(bits & nonTrivialLocations, opVal, &I);
890+
if (!deadEndBlocks->isDeadEnd(I.getParent())) {
891+
// TODO: rdar://159311784: Maybe at some point the invariant will be
892+
// enforced that values stored into addresses
893+
// don't leak in dead-ends.
894+
requireBitsClear(bits & nonTrivialLocations, opVal, &I);
895+
}
888896
// Needed to clear any bits of trivial locations (which are not required
889897
// to be zero).
890898
locations.clearBits(bits, opVal);
@@ -974,7 +982,8 @@ void MemoryLifetimeVerifier::verify() {
974982

975983
} // anonymous namespace
976984

977-
void SILFunction::verifyMemoryLifetime(CalleeCache *calleeCache) {
978-
MemoryLifetimeVerifier verifier(this, calleeCache);
985+
void SILFunction::verifyMemoryLifetime(CalleeCache *calleeCache,
986+
DeadEndBlocks *deadEndBlocks) {
987+
MemoryLifetimeVerifier verifier(this, calleeCache, deadEndBlocks);
979988
verifier.verify();
980989
}

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7385,7 +7385,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
73857385

73867386
if (F->hasOwnership() && F->shouldVerifyOwnership() &&
73877387
!mod.getASTContext().hadError()) {
7388-
F->verifyMemoryLifetime(calleeCache);
7388+
F->verifyMemoryLifetime(calleeCache, &getDeadEndBlocks());
73897389
}
73907390
}
73917391

test/SIL/memory_lifetime.sil

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,3 +864,11 @@ bb0:
864864
%10 = tuple ()
865865
return %10
866866
}
867+
868+
sil [ossa] @storage_leaked_into_dead_ends : $@convention(thin) () -> () {
869+
%t = apply undef() : $@convention(thin) () -> (@owned T)
870+
%t_addr = alloc_stack $T
871+
store %t to [init] %t_addr
872+
dealloc_stack %t_addr
873+
unreachable
874+
}

0 commit comments

Comments
 (0)