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

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 16 16:38:22 PST 2024


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

None

>From 029c4183c5685ac3047aec9154a593339b206a48 Mon Sep 17 00:00:00 2001
From: Maximino Sanchez Jr <maxsan at DESKTOP-TVMD718.>
Date: Sat, 16 Nov 2024 18:35:39 -0600
Subject: [PATCH] Added a PthreadCreateChecker and attempted to register it

---
 .../clang/StaticAnalyzer/Checkers/Checkers.td |  4 ++
 clang/lib/Headers/CMakeLists.txt              |  3 +-
 .../StaticAnalyzer/Checkers/CMakeLists.txt    |  1 +
 .../Checkers/PthreadCreateChecker.cpp         | 46 +++++++++++++++++++
 clang/test/Analysis/pthreadcreate.c           | 31 +++++++++++++
 5 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 clang/lib/StaticAnalyzer/Checkers/PthreadCreateChecker.cpp
 create mode 100644 clang/test/Analysis/pthreadcreate.c

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



More information about the cfe-commits mailing list