Skip to content

Commit 24154e3

Browse files
authored
Merge pull request #84937 from kavon/6.2-the-realloc-strikes-back-rdar162440304
[6.2] MoveOnlyChecker: avoid dangling reference in custom alloc
2 parents f449b7b + 743599b commit 24154e3

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyBorrowToDestructureUtils.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,19 @@ struct AvailableValueStore {
143143
liveness.getNumSubElements()),
144144
numBits(liveness.getNumSubElements()) {}
145145

146-
std::pair<AvailableValues *, bool> get(SILBasicBlock *block) {
146+
std::pair<AvailableValues, bool> get(SILBasicBlock *block) {
147147
auto iter = blockToValues.try_emplace(block, AvailableValues());
148148

149149
if (!iter.second) {
150-
return {&iter.first->second, false};
150+
return {iter.first->second, false};
151151
}
152152

153+
ASSERT(dataStore.size() >= (nextOffset + numBits) && "underallocated!");
154+
153155
iter.first->second.values =
154156
MutableArrayRef<SILValue>(&dataStore[nextOffset], numBits);
155157
nextOffset += numBits;
156-
return {&iter.first->second, true};
158+
return {iter.first->second, true};
157159
}
158160
};
159161

@@ -239,7 +241,7 @@ struct borrowtodestructure::Implementation {
239241

240242
void cleanup();
241243

242-
AvailableValues &computeAvailableValues(SILBasicBlock *block);
244+
AvailableValues computeAvailableValues(SILBasicBlock *block);
243245

244246
/// Returns mark_unresolved_non_copyable_value if we are processing borrows or
245247
/// the enum argument if we are processing switch_enum.
@@ -691,23 +693,24 @@ static void dumpSmallestTypeAvailable(
691693
/// ensures that we match at the source level the assumption by users that they
692694
/// can use entire valid parts as late as possible. If we were to do it earlier
693695
/// we would emit errors too early.
694-
AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
696+
AvailableValues Implementation::computeAvailableValues(SILBasicBlock *block) {
695697
LLVM_DEBUG(llvm::dbgs() << " Computing Available Values For bb"
696698
<< block->getDebugID() << '\n');
697699

698700
// First grab our block. If we already have state for the block, just return
699701
// its available values. We already computed the available values and
700702
// potentially updated it with new destructured values for our block.
703+
ASSERT(blockToAvailableValues.has_value());
701704
auto pair = blockToAvailableValues->get(block);
702705
if (!pair.second) {
703706
LLVM_DEBUG(llvm::dbgs()
704707
<< " Already have values! Returning them!\n");
705-
LLVM_DEBUG(pair.first->print(llvm::dbgs(), " "));
706-
return *pair.first;
708+
LLVM_DEBUG(pair.first.print(llvm::dbgs(), " "));
709+
return pair.first;
707710
}
708711

709712
LLVM_DEBUG(llvm::dbgs() << " No values computed! Initializing!\n");
710-
auto &newValues = *pair.first;
713+
AvailableValues newValues = pair.first;
711714

712715
// Otherwise, we need to initialize our available values with predecessor
713716
// information.
@@ -814,7 +817,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
814817
<< " Recursively loading its available values to "
815818
"compute initial smallest type available for block bb"
816819
<< block->getDebugID() << '\n');
817-
auto &predAvailableValues = computeAvailableValues(bb);
820+
auto predAvailableValues = computeAvailableValues(bb);
818821
LLVM_DEBUG(
819822
llvm::dbgs()
820823
<< " Computing initial smallest type available for block bb"
@@ -841,7 +844,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
841844
<< bb->getDebugID() << '\n');
842845
LLVM_DEBUG(llvm::dbgs()
843846
<< " Recursively loading its available values!\n");
844-
auto &predAvailableValues = computeAvailableValues(bb);
847+
auto predAvailableValues = computeAvailableValues(bb);
845848
for (unsigned i : range(predAvailableValues.size())) {
846849
if (!smallestTypeAvailable[i].has_value())
847850
continue;
@@ -881,7 +884,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
881884
for (auto *predBlock : predsSkippingBackEdges) {
882885
SWIFT_DEFER { typeSpanToValue.clear(); };
883886

884-
auto &predAvailableValues = computeAvailableValues(predBlock);
887+
auto predAvailableValues = computeAvailableValues(predBlock);
885888

886889
// First go through our available values and initialize our interval map. We
887890
// should never fail to insert. We want to insert /all/ available values so
@@ -997,7 +1000,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
9971000
// phis.
9981001
SILValue sameValue;
9991002
for (auto *predBlock : predsSkippingBackEdges) {
1000-
auto &predAvailableValues = computeAvailableValues(predBlock);
1003+
auto predAvailableValues = computeAvailableValues(predBlock);
10011004
if (!sameValue) {
10021005
sameValue = predAvailableValues[i];
10031006
} else if (sameValue != predAvailableValues[i]) {
@@ -1018,7 +1021,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
10181021
}
10191022

10201023
for (auto *predBlock : predsSkippingBackEdges) {
1021-
auto &predAvailableValues = computeAvailableValues(predBlock);
1024+
auto predAvailableValues = computeAvailableValues(predBlock);
10221025
addNewEdgeValueToBranch(predBlock->getTerminator(), block,
10231026
predAvailableValues[i], deleter);
10241027
}
@@ -1181,7 +1184,7 @@ void Implementation::rewriteUses(InstructionDeleter *deleter) {
11811184
// block.
11821185
LLVM_DEBUG(llvm::dbgs()
11831186
<< " Found needed bits! Propagating available values!\n");
1184-
auto &availableValues = computeAvailableValues(block);
1187+
auto availableValues = computeAvailableValues(block);
11851188
LLVM_DEBUG(llvm::dbgs() << " Computed available values for block bb"
11861189
<< block->getDebugID() << '\n';
11871190
availableValues.print(llvm::dbgs(), " "));

0 commit comments

Comments
 (0)