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

2024-11-16 Thread Balazs Benics via cfe-commits

steakhal wrote:

Could you please ellaborate your intentions and motives? The PR description 
wasn't clear to me. @MaxSanchez99 

https://github.com/llvm/llvm-project/pull/116515
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-11-16 Thread via cfe-commits

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 
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;
 
+def PthreadCreateChecker : Checker<"PthreadCreate">,
+   HelpText<"Check for creation of pthread">,
+   Documentation;
+
 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 00..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 {
+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();
+}
+
+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 00..0ca6f13d4ecb45
--- /dev/null
+++ b/clang/test/Analysis/pthreadcreate.c
@@ -0,0 +1,31 @@
+//
+// Created by MaxSa on 11/14/2024.
+//
+
+#include 
+#include 
+#include 
+
+
+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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-11-16 Thread via cfe-commits

llvmbot wrote:




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

Author: None (MaxSanchez99)


Changes



---
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;
 
+def PthreadCreateChecker : Checker<"PthreadCreate">,
+   HelpText<"Check for creation of pthread">,
+   Documentation;
+
 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 00..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 {
+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();
+}
+
+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 00..0ca6f13d4ecb45
--- /dev/null
+++ b/clang/test/Analysis/pthreadcreate.c
@@ -0,0 +1,31 @@
+//
+// Created by MaxSa on 11/14/2024.
+//
+
+#include 
+#include 
+#include 
+
+
+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

``




https://github.com/llvm/llvm-project/pull/116515
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-11-16 Thread via cfe-commits

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

>From 029c4183c5685ac3047aec9154a593339b206a48 Mon Sep 17 00:00:00 2001
From: Maximino Sanchez Jr 
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;
 
+def PthreadCreateChecker : Checker<"PthreadCreate">,
+   HelpText<"Check for creation of pthread">,
+   Documentation;
+
 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 00..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 {
+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();
+}
+
+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 00..0ca6f13d4ecb45
--- /dev/null
+++ b/clang/test/Analysis/pthreadcreate.c
@@ -0,0 +1,31 @@
+//
+// Created by MaxSa on 11/14/2024.
+//
+
+#include 
+#include 
+#include 
+
+
+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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-11-16 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write 
permissions for the repository. In which case you can instead tag reviewers by 
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a 
review by "ping"ing the PR by adding a comment “Ping”. The common courtesy 
"ping" rate is once a week. Please remember that you are asking for valuable 
time from other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/116515
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits