[llvm] Resolve static analyser report on pointer dereferencing after null check (PR #88278)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 15 01:19:37 PDT 2024


https://github.com/mmoadeli updated https://github.com/llvm/llvm-project/pull/88278

>From b942678ab21b820102b99b39dff6e06dfd422586 Mon Sep 17 00:00:00 2001
From: M Moadeli <mahmoud.moadeli at codeplay.com>
Date: Wed, 10 Apr 2024 15:34:06 +0100
Subject: [PATCH 1/2] Address static analyzer check failure on using a pointer
 after null check.

---
 .../AMDGPU/AMDGPULowerModuleLDSPass.cpp       | 34 ++++++++-----------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
index 595f09664c55e4..b2ab9d77ebe0f9 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
@@ -1052,21 +1052,18 @@ class AMDGPULowerModuleLDS {
   void removeNoLdsKernelIdFromReachable(CallGraph &CG, Function *KernelRoot) {
     KernelRoot->removeFnAttr("amdgpu-no-lds-kernel-id");
 
-    SmallVector<Function *> Tmp({CG[KernelRoot]->getFunction()});
-    if (!Tmp.back())
-      return;
-
+    SmallVector<Function *> WorkList({CG[KernelRoot]->getFunction()});
     SmallPtrSet<Function *, 8> Visited;
     bool SeenUnknownCall = false;
 
-    do {
-      Function *F = Tmp.pop_back_val();
+    while (!WorkList.empty()) {
+      Function *F = WorkList.pop_back_val();
 
-      for (auto &N : *CG[F]) {
-        if (!N.second)
+      for (auto &CallRecord : *CG[F]) {
+        if (!CallRecord.second)
           continue;
 
-        Function *Callee = N.second->getFunction();
+        Function *Callee = CallRecord.second->getFunction();
         if (!Callee) {
           if (!SeenUnknownCall) {
             SeenUnknownCall = true;
@@ -1074,21 +1071,20 @@ class AMDGPULowerModuleLDS {
             // If we see any indirect calls, assume nothing about potential
             // targets.
             // TODO: This could be refined to possible LDS global users.
-            for (auto &N : *CG.getExternalCallingNode()) {
-              Function *PotentialCallee = N.second->getFunction();
-              if (!isKernelLDS(PotentialCallee))
+            for (auto &ExternalCallRecord : *CG.getExternalCallingNode()) {
+              Function *PotentialCallee =
+                  ExternalCallRecord.second->getFunction();
+              if (PotentialCallee && !isKernelLDS(PotentialCallee))
                 PotentialCallee->removeFnAttr("amdgpu-no-lds-kernel-id");
             }
-
-            continue;
           }
+        } else {
+          Callee->removeFnAttr("amdgpu-no-lds-kernel-id");
+          if (Visited.insert(Callee).second)
+            WorkList.push_back(Callee);
         }
-
-        Callee->removeFnAttr("amdgpu-no-lds-kernel-id");
-        if (Visited.insert(Callee).second)
-          Tmp.push_back(Callee);
       }
-    } while (!Tmp.empty());
+    }
   }
 
   DenseMap<Function *, GlobalVariable *> lowerDynamicLDSVariables(

>From 907ed89f84b4cb4966ab7a5a44e2a9a849474c9e Mon Sep 17 00:00:00 2001
From: M Moadeli <mahmoud.moadeli at codeplay.com>
Date: Mon, 15 Apr 2024 09:18:57 +0100
Subject: [PATCH 2/2] Assert on validity of externally calling function.

---
 llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
index b2ab9d77ebe0f9..c8bf9dd39e389c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
@@ -1074,7 +1074,8 @@ class AMDGPULowerModuleLDS {
             for (auto &ExternalCallRecord : *CG.getExternalCallingNode()) {
               Function *PotentialCallee =
                   ExternalCallRecord.second->getFunction();
-              if (PotentialCallee && !isKernelLDS(PotentialCallee))
+              assert(PotentialCallee);
+              if (!isKernelLDS(PotentialCallee))
                 PotentialCallee->removeFnAttr("amdgpu-no-lds-kernel-id");
             }
           }



More information about the llvm-commits mailing list