Skip to content

Commit fe9e01c

Browse files
authored
Merge pull request #84934 from kavon/the-realloc-strikes-back-rdar162440304
MoveOnlyChecker: avoid dangling reference in custom alloc
2 parents c66de29 + d1f3f84 commit fe9e01c

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.
@@ -702,23 +704,24 @@ static void dumpSmallestTypeAvailable(
702704
/// ensures that we match at the source level the assumption by users that they
703705
/// can use entire valid parts as late as possible. If we were to do it earlier
704706
/// we would emit errors too early.
705-
AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
707+
AvailableValues Implementation::computeAvailableValues(SILBasicBlock *block) {
706708
LLVM_DEBUG(llvm::dbgs() << " Computing Available Values For bb"
707709
<< block->getDebugID() << '\n');
708710

709711
// First grab our block. If we already have state for the block, just return
710712
// its available values. We already computed the available values and
711713
// potentially updated it with new destructured values for our block.
714+
ASSERT(blockToAvailableValues.has_value());
712715
auto pair = blockToAvailableValues->get(block);
713716
if (!pair.second) {
714717
LLVM_DEBUG(llvm::dbgs()
715718
<< " Already have values! Returning them!\n");
716-
LLVM_DEBUG(pair.first->print(llvm::dbgs(), " "));
717-
return *pair.first;
719+
LLVM_DEBUG(pair.first.print(llvm::dbgs(), " "));
720+
return pair.first;
718721
}
719722

720723
LLVM_DEBUG(llvm::dbgs() << " No values computed! Initializing!\n");
721-
auto &newValues = *pair.first;
724+
AvailableValues newValues = pair.first;
722725

723726
// Otherwise, we need to initialize our available values with predecessor
724727
// information.
@@ -825,7 +828,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
825828
<< " Recursively loading its available values to "
826829
"compute initial smallest type available for block bb"
827830
<< block->getDebugID() << '\n');
828-
auto &predAvailableValues = computeAvailableValues(bb);
831+
auto predAvailableValues = computeAvailableValues(bb);
829832
LLVM_DEBUG(
830833
llvm::dbgs()
831834
<< " Computing initial smallest type available for block bb"
@@ -852,7 +855,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
852855
<< bb->getDebugID() << '\n');
853856
LLVM_DEBUG(llvm::dbgs()
854857
<< " Recursively loading its available values!\n");
855-
auto &predAvailableValues = computeAvailableValues(bb);
858+
auto predAvailableValues = computeAvailableValues(bb);
856859
for (unsigned i : range(predAvailableValues.size())) {
857860
if (!smallestTypeAvailable[i].has_value())
858861
continue;
@@ -892,7 +895,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
892895
for (auto *predBlock : predsSkippingBackEdges) {
893896
SWIFT_DEFER { typeSpanToValue.clear(); };
894897

895-
auto &predAvailableValues = computeAvailableValues(predBlock);
898+
auto predAvailableValues = computeAvailableValues(predBlock);
896899

897900
// First go through our available values and initialize our interval map. We
898901
// should never fail to insert. We want to insert /all/ available values so
@@ -1008,7 +1011,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
10081011
// phis.
10091012
SILValue sameValue;
10101013
for (auto *predBlock : predsSkippingBackEdges) {
1011-
auto &predAvailableValues = computeAvailableValues(predBlock);
1014+
auto predAvailableValues = computeAvailableValues(predBlock);
10121015
if (!sameValue) {
10131016
sameValue = predAvailableValues[i];
10141017
} else if (sameValue != predAvailableValues[i]) {
@@ -1029,7 +1032,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
10291032
}
10301033

10311034
for (auto *predBlock : predsSkippingBackEdges) {
1032-
auto &predAvailableValues = computeAvailableValues(predBlock);
1035+
auto predAvailableValues = computeAvailableValues(predBlock);
10331036
addNewEdgeValueToBranch(predBlock->getTerminator(), block,
10341037
predAvailableValues[i], deleter);
10351038
}
@@ -1192,7 +1195,7 @@ void Implementation::rewriteUses(InstructionDeleter *deleter) {
11921195
// block.
11931196
LLVM_DEBUG(llvm::dbgs()
11941197
<< " Found needed bits! Propagating available values!\n");
1195-
auto &availableValues = computeAvailableValues(block);
1198+
auto availableValues = computeAvailableValues(block);
11961199
LLVM_DEBUG(llvm::dbgs() << " Computed available values for block bb"
11971200
<< block->getDebugID() << '\n';
11981201
availableValues.print(llvm::dbgs(), " "));

0 commit comments

Comments
 (0)