Skip to content

Conversation

@thurstond
Copy link
Contributor

@thurstond thurstond commented Oct 22, 2025

mapInfoOp.getMembers() on line 258 is use-after-free, because cloneModifyAndErase (line 255) erased mapInfoOp. Fix the issue by replacing subsequent mapInfoOp usages with clonedOp.

Similarly, update memberMapInfoOp to avoid subsequent use-after-free.

mapInfoOp should not be accessed here because cloneModifyAndErase (line 255) erased it. Fix the issue by replacing mapInfoOp with the cloned op.
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-mlir-openmp

Author: Thurston Dang (thurstond)

Changes

mapInfoOp.getMembers() on line 258 was use-after-free, because cloneModifyAndErase (line 255) erased it. Fix the issue by replacing mapInfoOp with the cloned op.


Full diff: https://github.com/llvm/llvm-project/pull/164712.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp (+2-1)
diff --git a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp index 735b905bffb85..dafc1cbacdac7 100644 --- a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp +++ b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp @@ -252,7 +252,8 @@ class PrepareForOMPOffloadPrivatizationPass // variable, rewrite all the uses of the original variable with // the heap-allocated variable. rewriter.setInsertionPoint(targetOp); - rewriter.setInsertionPoint(cloneModifyAndErase(mapInfoOp)); + mapInfoOp = cloneModifyAndErase(mapInfoOp); + rewriter.setInsertionPoint(mapInfoOp); // Fix any members that may use varPtr to now use heapMem for (auto member : mapInfoOp.getMembers()) { 
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-mlir

Author: Thurston Dang (thurstond)

Changes

mapInfoOp.getMembers() on line 258 was use-after-free, because cloneModifyAndErase (line 255) erased it. Fix the issue by replacing mapInfoOp with the cloned op.


Full diff: https://github.com/llvm/llvm-project/pull/164712.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp (+2-1)
diff --git a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp index 735b905bffb85..dafc1cbacdac7 100644 --- a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp +++ b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp @@ -252,7 +252,8 @@ class PrepareForOMPOffloadPrivatizationPass // variable, rewrite all the uses of the original variable with // the heap-allocated variable. rewriter.setInsertionPoint(targetOp); - rewriter.setInsertionPoint(cloneModifyAndErase(mapInfoOp)); + mapInfoOp = cloneModifyAndErase(mapInfoOp); + rewriter.setInsertionPoint(mapInfoOp); // Fix any members that may use varPtr to now use heapMem for (auto member : mapInfoOp.getMembers()) { 
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-flang-openmp

Author: Thurston Dang (thurstond)

Changes

mapInfoOp.getMembers() on line 258 was use-after-free, because cloneModifyAndErase (line 255) erased it. Fix the issue by replacing mapInfoOp with the cloned op.


Full diff: https://github.com/llvm/llvm-project/pull/164712.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp (+2-1)
diff --git a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp index 735b905bffb85..dafc1cbacdac7 100644 --- a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp +++ b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp @@ -252,7 +252,8 @@ class PrepareForOMPOffloadPrivatizationPass // variable, rewrite all the uses of the original variable with // the heap-allocated variable. rewriter.setInsertionPoint(targetOp); - rewriter.setInsertionPoint(cloneModifyAndErase(mapInfoOp)); + mapInfoOp = cloneModifyAndErase(mapInfoOp); + rewriter.setInsertionPoint(mapInfoOp); // Fix any members that may use varPtr to now use heapMem for (auto member : mapInfoOp.getMembers()) { 
@thurstond thurstond changed the title [flang[mlir] Fix-forward heap use-after-free in #155348 [flang][mlir] Fix-forward heap use-after-free in #155348 Oct 22, 2025
@thurstond thurstond marked this pull request as draft October 22, 2025 21:52
@thurstond thurstond marked this pull request as ready for review October 22, 2025 22:00
// the heap-allocated variable.
rewriter.setInsertionPoint(targetOp);
rewriter.setInsertionPoint(cloneModifyAndErase(mapInfoOp));
auto clonedOp = cast<omp::MapInfoOp>(cloneModifyAndErase(mapInfoOp));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just updating it:

Suggested change
auto clonedOp = cast<omp::MapInfoOp>(cloneModifyAndErase(mapInfoOp));
mapInfoOp = cast<omp::MapInfoOp>(cloneModifyAndErase(mapInfoOp));

That way no risk of misusing mapInfoOp anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks! Done: 7fdd34a

Also fix another use-after-free
@thurstond thurstond requested a review from joker-eph October 22, 2025 22:14
@thurstond
Copy link
Contributor Author

@joker-eph I updated the patch to fix a second-use-after-free

@github-actions
Copy link

github-actions bot commented Oct 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@thurstond thurstond merged commit cea0037 into llvm:main Oct 22, 2025
10 checks passed
mikolaj-pirog pushed a commit to mikolaj-pirog/llvm-project that referenced this pull request Oct 23, 2025
…4712) `mapInfoOp.getMembers()` on line 258 is use-after-free, because cloneModifyAndErase (line 255) erased `mapInfoOp`. Fix the issue by replacing subsequent `mapInfoOp` usages with `clonedOp`. Similarly, update `memberMapInfoOp` to avoid subsequent use-after-free.
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
…4712) `mapInfoOp.getMembers()` on line 258 is use-after-free, because cloneModifyAndErase (line 255) erased `mapInfoOp`. Fix the issue by replacing subsequent `mapInfoOp` usages with `clonedOp`. Similarly, update `memberMapInfoOp` to avoid subsequent use-after-free.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
…4712) `mapInfoOp.getMembers()` on line 258 is use-after-free, because cloneModifyAndErase (line 255) erased `mapInfoOp`. Fix the issue by replacing subsequent `mapInfoOp` usages with `clonedOp`. Similarly, update `memberMapInfoOp` to avoid subsequent use-after-free.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
…4712) `mapInfoOp.getMembers()` on line 258 is use-after-free, because cloneModifyAndErase (line 255) erased `mapInfoOp`. Fix the issue by replacing subsequent `mapInfoOp` usages with `clonedOp`. Similarly, update `memberMapInfoOp` to avoid subsequent use-after-free.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment