[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210319142952.1.I2c82789dba4e68d61595862188e8bf4d31a05d38@changeid>
Date: Fri, 19 Mar 2021 14:36:15 +1100
From: Evan Benn <evanbenn@...omium.org>
To: Julia Lawall <Julia.Lawall@...ia.fr>
Cc: Evan Benn <evanbenn@...omium.org>,
Gilles Muller <Gilles.Muller@...ia.fr>,
Michal Marek <michal.lkml@...kovi.net>,
Nicolas Palix <nicolas.palix@...g.fr>, cocci@...teme.lip6.fr,
linux-kernel@...r.kernel.org
Subject: [PATCH] scripts/coccinelle: Add script to detect sign extension
Hello,
I am attempting to create a coccinelle script that will detect possibly buggy
usage of the bitwise operators where integer promotion may result in bugs,
usually due to sign extension.
I know this script needs a lot more work, but I am just beginning to learn the
syntax of coccinelle. At this stage I am mainly looking for advice if this is
even worth continuing, or if I am on the wrong track entirely.
Here is an example of the bug I hope to find:
https://lore.kernel.org/lkml/20210317013758.GA134033@roeck-us.net/
Where ints and unsigned are mixed in bitwise operations, and the sizes differ.
Thanks
Evan Benn
Signed-off-by: Evan Benn <evanbenn@...omium.org>
---
.../coccinelle/tests/int_sign_extend.cocci | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 scripts/coccinelle/tests/int_sign_extend.cocci
diff --git a/scripts/coccinelle/tests/int_sign_extend.cocci b/scripts/coccinelle/tests/int_sign_extend.cocci
new file mode 100644
index 000000000000..bad61e37e4e7
--- /dev/null
+++ b/scripts/coccinelle/tests/int_sign_extend.cocci
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Mixing signed and unsigned types in bitwise operations risks problems when
+/// the 'Usual arithmetic conversions' are applied.
+/// For example:
+/// https://lore.kernel.org/lkml/20210317013758.GA134033@roeck-us.net/
+/// When a signed int and an unsigned int are compared there is no problem.
+/// But if the unsigned is changed to a unsigned long, for example by using BIT
+/// the signed value will be sign-extended and could result in incorrect logic.
+// Confidence:
+// Copyright: (C) 2021 Evan Benn <evanbenn@...omium.org>
+// Comments:
+// Options:
+
+virtual context
+virtual org
+virtual report
+
+@r@
+position p;
+{int} s;
+{unsigned long} u;
+@@
+ s@p & u
+
+@...ipt:python depends on org@
+p << r.p;
+@@
+
+cocci.print_main("sign extension when comparing bits of signed and unsigned values", p)
+
+@...ipt:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0],"sign extension when comparing bits of signed and unsigned values")
--
2.31.0.291.g576ba9dcdaf-goog
Powered by blists - more mailing lists