Skip to content

Fix dangling IPOAmendableCB function_ref. #120698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented Dec 20, 2024

The IPOAmendableCB's type is llvm::function_ref, it is error-prone to write code (e.g.

AC.IPOAmendableCB = [](const Function &F) {
) that assign a temporary lambda to an IPOAmendableCB object, which is a use-after-free issue.

This patch changes the IPOAmendableCB to std::function, to avoid the dangling issue.

@hokein hokein requested review from arsenm and Xazax-hun December 20, 2024 08:45
@llvmbot llvmbot added backend:AMDGPU llvm:transforms clang:openmp OpenMP related changes to Clang labels Dec 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-backend-amdgpu

Author: Haojian Wu (hokein)

Changes

A temporary lambda is assigned to a llvm::function_ref, this lambda is destroyed at the end of the full expression, any use of the function_ref afterwards is undefined behavior.


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

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp (+2-1)
  • (modified) llvm/lib/Transforms/IPO/OpenMPOpt.cpp (+2-1)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
index 21f9c50c352563..227a49fcc1a491 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
@@ -1352,9 +1352,10 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
         return !AMDGPU::isEntryFunctionCC(Callee.getCallingConv()) &&
                (NumAssumedCallees <= IndirectCallSpecializationThreshold);
       };
-  AC.IPOAmendableCB = [](const Function &F) {
+  auto IPOAmendableCB = [](const Function &F) {
     return F.getCallingConv() == CallingConv::AMDGPU_KERNEL;
   };
+  AC.IPOAmendableCB = IPOAmendableCB;
 
   Attributor A(Functions, InfoCache, AC);
 
diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
index b40ab357670b86..ee4e4a85d5a0e0 100644
--- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
+++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
@@ -5809,9 +5809,10 @@ PreservedAnalyses OpenMPOptPass::run(Module &M, ModuleAnalysisManager &AM) {
   AC.OREGetter = OREGetter;
   AC.PassName = DEBUG_TYPE;
   AC.InitializationCallback = OpenMPOpt::registerAAsForFunction;
-  AC.IPOAmendableCB = [](const Function &F) {
+  auto IPOAmendableCB = [](const Function &F) {
     return F.hasFnAttribute("kernel");
   };
+  AC.IPOAmendableCB = IPOAmendableCB;
 
   Attributor A(Functions, InfoCache, AC);
 

@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Haojian Wu (hokein)

Changes

A temporary lambda is assigned to a llvm::function_ref, this lambda is destroyed at the end of the full expression, any use of the function_ref afterwards is undefined behavior.


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

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp (+2-1)
  • (modified) llvm/lib/Transforms/IPO/OpenMPOpt.cpp (+2-1)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
index 21f9c50c352563..227a49fcc1a491 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
@@ -1352,9 +1352,10 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
         return !AMDGPU::isEntryFunctionCC(Callee.getCallingConv()) &&
                (NumAssumedCallees <= IndirectCallSpecializationThreshold);
       };
-  AC.IPOAmendableCB = [](const Function &F) {
+  auto IPOAmendableCB = [](const Function &F) {
     return F.getCallingConv() == CallingConv::AMDGPU_KERNEL;
   };
+  AC.IPOAmendableCB = IPOAmendableCB;
 
   Attributor A(Functions, InfoCache, AC);
 
diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
index b40ab357670b86..ee4e4a85d5a0e0 100644
--- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
+++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
@@ -5809,9 +5809,10 @@ PreservedAnalyses OpenMPOptPass::run(Module &M, ModuleAnalysisManager &AM) {
   AC.OREGetter = OREGetter;
   AC.PassName = DEBUG_TYPE;
   AC.InitializationCallback = OpenMPOpt::registerAAsForFunction;
-  AC.IPOAmendableCB = [](const Function &F) {
+  auto IPOAmendableCB = [](const Function &F) {
     return F.hasFnAttribute("kernel");
   };
+  AC.IPOAmendableCB = IPOAmendableCB;
 
   Attributor A(Functions, InfoCache, AC);
 

A temporary lambda is assigned to a llvm::function_ref, this lambda is
destroyed at the end of the full expression, any use of the function_ref
afterwards is undefined behavior.
@jdoerfert
Copy link
Member

LG

@jdoerfert jdoerfert self-requested a review January 7, 2025 18:46
@hokein hokein merged commit 2ab447a into llvm:main Jan 7, 2025
6 of 8 checks passed
@hokein hokein deleted the dangling-ipo-cb branch January 7, 2025 20:53
hokein added a commit that referenced this pull request Jan 8, 2025
This can enable clang to detect dangling assignment issues, see #120698.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants