Skip to content

Commit 3f732c4

Browse files
[mlir][Transforms] Fix use-after-free in llvm#82474 (llvm#82504)
When a `ModifyOperationRewrite` is committed, the operation may already have been erased, so `OperationName` must be cached in the rewrite object. Note: This will no longer be needed with llvm#81757, which adds a "cleanup" method to `IRRewrite`.
1 parent 13b0321 commit 3f732c4

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Diff for: mlir/lib/Transforms/Utils/DialectConversion.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -965,14 +965,14 @@ class ModifyOperationRewrite : public OperationRewrite {
965965
ModifyOperationRewrite(ConversionPatternRewriterImpl &rewriterImpl,
966966
Operation *op)
967967
: OperationRewrite(Kind::ModifyOperation, rewriterImpl, op),
968-
loc(op->getLoc()), attrs(op->getAttrDictionary()),
968+
name(op->getName()), loc(op->getLoc()), attrs(op->getAttrDictionary()),
969969
operands(op->operand_begin(), op->operand_end()),
970970
successors(op->successor_begin(), op->successor_end()) {
971971
if (OpaqueProperties prop = op->getPropertiesStorage()) {
972972
// Make a copy of the properties.
973973
propertiesStorage = operator new(op->getPropertiesStorageSize());
974974
OpaqueProperties propCopy(propertiesStorage);
975-
op->getName().initOpProperties(propCopy, /*init=*/prop);
975+
name.initOpProperties(propCopy, /*init=*/prop);
976976
}
977977
}
978978

@@ -988,7 +988,9 @@ class ModifyOperationRewrite : public OperationRewrite {
988988
void commit() override {
989989
if (propertiesStorage) {
990990
OpaqueProperties propCopy(propertiesStorage);
991-
op->getName().destroyOpProperties(propCopy);
991+
// Note: The operation may have been erased in the mean time, so
992+
// OperationName must be stored in this object.
993+
name.destroyOpProperties(propCopy);
992994
operator delete(propertiesStorage);
993995
propertiesStorage = nullptr;
994996
}
@@ -1003,13 +1005,14 @@ class ModifyOperationRewrite : public OperationRewrite {
10031005
if (propertiesStorage) {
10041006
OpaqueProperties propCopy(propertiesStorage);
10051007
op->copyProperties(propCopy);
1006-
op->getName().destroyOpProperties(propCopy);
1008+
name.destroyOpProperties(propCopy);
10071009
operator delete(propertiesStorage);
10081010
propertiesStorage = nullptr;
10091011
}
10101012
}
10111013

10121014
private:
1015+
OperationName name;
10131016
LocationAttr loc;
10141017
DictionaryAttr attrs;
10151018
SmallVector<Value, 8> operands;

0 commit comments

Comments
 (0)