cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
@ 2022-09-16  6:19 Yuan Can
  2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Yuan Can @ 2022-09-16  6:19 UTC (permalink / raw)
  To: Julia.Lawall, nicolas.palix, cocci; +Cc: yuancan

Find mutex used without init, and point out the
place the mutex is allocated.

Signed-off-by: Yuan Can <yuancan@huawei.com>
---
 .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci

diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci b/scripts/coccinelle/locks/missing_mutex_init.cocci
new file mode 100644
index 000000000000..bec8ce60d0c5
--- /dev/null
+++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// report missing mutex_init()
+///
+/// Report mutex used without initialize. False positives can occur
+/// when the mutex allocation and initialization happens in two
+/// different files.
+///
+// Copyright: (C) 2022 Huawei Technologies Co, Ltd.
+// Comments:
+// Options: --include-headers
+
+virtual org
+virtual report
+
+@r1@
+identifier s, fld;
+struct s *mm;
+@@
+mutex_init(\(&mm->fld\|&(mm->fld)\))
+
+@r2@
+identifier r1.s, r1.fld;
+position p;
+@@
+
+struct s {
+  ...
+  struct mutex fld@p;
+  ...
+};
+
+@r3@
+identifier s, fld;
+position p != {r2.p};
+@@
+
+struct s {
+  ...
+  struct mutex fld@p;
+  ...
+};
+
+@r4@
+identifier r3.fld;
+identifier r3.s;
+struct s *mm;
+@@
+
+mutex_lock(&mm->fld)
+
+@r5 depends on r4@
+identifier r3.s;
+struct s *mm;
+position p;
+@@
+* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)
+
+@script:python depends on org@
+p << r5.p;
+@@
+cocci.print_main("Mutex inside the struct malloced here is possibly used without init.", p)
+
+@script:python depends on report@
+p << r5.p;
+@@
+
+msg = "Mutex inside the struct malloced here is possibly used without init."
+coccilib.report.print_report(p[0], msg)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-09-19  8:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
2022-09-16 14:43 ` [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Markus Elfring
2022-09-17  6:40 ` Markus Elfring
2022-09-18 19:39 ` Julia Lawall
2022-09-19  7:55   ` Yuan Can
2022-09-19  8:01     ` Julia Lawall
2022-09-19  8:10       ` Yuan Can

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).