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

Find mutex inside struct which is possibly used without init,
provide the name of the struct and of the mutex, the position
where the struct is malloced and where the mutex get locked.

Signed-off-by: Yuan Can <yuancan@huawei.com>
---
changes in v2:
- adjust commit msg
- add Confidence: tag
- print out more information in the report
---
 .../coccinelle/locks/missing_mutex_init.cocci | 76 +++++++++++++++++++
 1 file changed, 76 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..bf522efc7f24
--- /dev/null
+++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
@@ -0,0 +1,76 @@
+// 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.
+///
+// Confidence: Low
+// 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;
+position p;
+@@
+
+mutex_lock@p(&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;
+p1 << r4.p;
+s << r3.s;
+fld << r3.fld;
+@@
+msg = "Mutex %s inside the struct %s malloced at line %s is possibly used at line %s without init." % (fld, s, p[0].line, p1[0].line);
+cocci.print_main(msg, p)
+
+@script:python depends on report@
+p << r5.p;
+p1 << r4.p;
+s << r3.s;
+fld << r3.fld;
+@@
+msg = "Mutex %s inside the struct %s malloced at line %s is possibly used at line %s without init." % (fld, s, p[0].line, p1[0].line);
+coccilib.report.print_report(p[0], msg)
-- 
2.17.1


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

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

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-20  2:58 [cocci] [PATCH v2 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-20  2:58 ` [cocci] [PATCH v2 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
2022-09-20 19:21 ` [cocci] [PATCH v2 1/2] coccinelle: locks: add missing_mutex_init.cocci script Markus Elfring
2022-09-20 20:13   ` Julia Lawall
2022-09-20 20:24     ` [cocci] [v2 " Markus Elfring
2022-09-22 11:22       ` Yuan Can
2022-09-22 12:27         ` Julia Lawall
2022-09-22 11:29     ` [cocci] [PATCH v2 " Yuan Can
2022-09-22 11:35   ` 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).