[clang] Added a PthreadCreateChecker and attempted to register it (PR #116515)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 16 16:39:13 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-static-analyzer-1

Author: None (MaxSanchez99)

<details>
<summary>Changes</summary>



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


5 Files Affected:

- (modified) clang/include/clang/StaticAnalyzer/Checkers/Checkers.td (+4) 
- (modified) clang/lib/Headers/CMakeLists.txt (+2-1) 
- (modified) clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt (+1) 
- (added) clang/lib/StaticAnalyzer/Checkers/PthreadCreateChecker.cpp (+46) 
- (added) clang/test/Analysis/pthreadcreate.c (+31) 


``````````diff
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index b03e707d638742..e7b08b89c358d5 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -606,6 +606,10 @@ def ChrootChecker : Checker<"Chroot">,
   HelpText<"Check improper use of chroot">,
   Documentation<HasDocumentation>;
 
+def PthreadCreateChecker : Checker<"PthreadCreate">,
+   HelpText<"Check for creation of pthread">,
+   Documentation<HasDocumentation>;
+
 def PthreadLockChecker : Checker<"PthreadLock">,
   HelpText<"Simple lock -> unlock checker">,
   Dependencies<[PthreadLockBase]>,
diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt
index a094305bcec5e4..4154dea674cbbc 100644
--- a/clang/lib/Headers/CMakeLists.txt
+++ b/clang/lib/Headers/CMakeLists.txt
@@ -379,7 +379,8 @@ set(zos_wrapper_files
 
 include(GetClangResourceDir)
 get_clang_resource_dir(output_dir PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
-set(out_files)
+set(out_files
+        ../../test/Analysis/pthreadcreate.c)
 set(generated_files)
 
 set(arm_common_generated_files)
diff --git a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
index f40318f46dea1a..ace537837de5f3 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
+++ b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
@@ -92,6 +92,7 @@ add_clang_library(clangStaticAnalyzerCheckers
   PaddingChecker.cpp
   PointerArithChecker.cpp
   PointerSubChecker.cpp
+  PthreadCreateChecker.cpp
   PthreadLockChecker.cpp
   PutenvStackArrayChecker.cpp
   RetainCountChecker/RetainCountChecker.cpp
diff --git a/clang/lib/StaticAnalyzer/Checkers/PthreadCreateChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PthreadCreateChecker.cpp
new file mode 100644
index 00000000000000..e9225fb780c867
--- /dev/null
+++ b/clang/lib/StaticAnalyzer/Checkers/PthreadCreateChecker.cpp
@@ -0,0 +1,46 @@
+//
+// Created by MaxSa on 11/13/2024.
+//
+
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/CheckerManager.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+
+using namespace clang;
+using namespace ento;
+
+class PthreadCreateChecker : public Checker<check::PostCall> {
+public:
+  void checkPostCall(const CallEvent &Call, CheckerContext &Context) const;
+
+};
+
+void PthreadCreateChecker::checkPostCall(const CallEvent &Call, CheckerContext &Context) const {
+  const FunctionDecl *FuncID = Call.getDecl()->getAsFunction();
+  if (!FuncID) {
+    return;
+  }
+
+  if (FuncID->getName() == "pthread_create") {
+    SVal returnVal = Call.getReturnValue();
+    if (returnVal.isZeroConstant()) {
+      llvm::errs() << "Pthread has been created\n";
+    }
+  }
+}
+
+// Register checker
+void ento::registerPthreadCreateChecker(CheckerManager &mgr) {
+  mgr.registerChecker<PthreadCreateChecker>();
+}
+
+bool ento::shouldRegisterPthreadCreateChecker(const CheckerManager &mgr) {
+  return true;
+}
+
+
+
diff --git a/clang/test/Analysis/pthreadcreate.c b/clang/test/Analysis/pthreadcreate.c
new file mode 100644
index 00000000000000..0ca6f13d4ecb45
--- /dev/null
+++ b/clang/test/Analysis/pthreadcreate.c
@@ -0,0 +1,31 @@
+//
+// Created by MaxSa on 11/14/2024.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+
+void* thread_function(void* arg) {
+  printf("thread_function start\n");
+  return nullptr;
+}
+
+int main() {
+  pthread_t thread;
+  int arg = 42;
+
+  if (pthread_create(&thread, NULL, thread_function, &arg)) {
+    perror("pthread_create");
+    exit(1);
+  }
+
+  if (pthread_join(thread, nullptr)) {
+    perror("pthread_join");
+    exit(1);
+  }
+
+  printf("thread exit\n");
+  return 0;
+}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/116515


More information about the cfe-commits mailing list