* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
@ 2020-06-05 5:50 Markus Elfring
0 siblings, 0 replies; 7+ messages in thread
From: Markus Elfring @ 2020-06-05 5:50 UTC (permalink / raw)
To: Julia Lawall, Coccinelle; +Cc: Joe Perches, linux-kernel
> On the other hand, do you really require E to be a pointer?
> If you do that, it will have to find the type of E.
I suggest to reconsider this information.
> If E refers to a structure field, then the type might not be available
> in the current function, and you may need command line argments like
> --all-includes or --recursive-includes.
Will the software documentation need corresponding extensions for
the safe application of the semantic patch language?
Will the used data structure access operator like arrow or dot
influence the interpretation of the software situation?
> Is avoiding transforming the case where E is not verified
> to be a pointer a concern?
I would find it desirable to express constraints for pointer data types
according to the applied programming interfaces.
Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
@ 2020-06-05 6:43 Markus Elfring
0 siblings, 0 replies; 7+ messages in thread
From: Markus Elfring @ 2020-06-05 6:43 UTC (permalink / raw)
To: Denis Efremov, Coccinelle, Gilles Muller, Julia Lawall,
Masahiro Yamada, Michal Marek, Nicolas Palix
Cc: Joe Perches, kernel-janitors, linux-kernel
> Check for memset()/memset_explicit() with 0 followed by
> kfree()/vfree()/kvfree().
Another software evolution will become interesting here.
> +/// memset_explicit with 0 followed by kfree
How do you think about to extend this comment?
Would you like to take the following SmPL code variants into account?
> +virtual context
> +virtual org
> +virtual report
> +virtual patch
+virtual context, org, report, patch
> +(
> +- kfree(E);
> ++ kzfree(E);
> +|
> +- vfree(E);
> ++ kvfree_sensitive(E, size);
> +|
> +- kvfree(E);
> ++ kvfree_sensitive(E, size);
> +)
+(
+-kfree
++kzfree
+ (E);
+|
+-vfree
++kvfree_sensitive
+ (E
++ , size
+ );
+|
+-kvfree
++kvfree_sensitive
+ (E
++ , size
+ );
+)
Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Cocci] [PATCH] coccinelle: api: add kzfree script
@ 2020-06-04 14:08 Denis Efremov
2020-06-04 20:48 ` [Cocci] [PATCH v2] " Denis Efremov
0 siblings, 1 reply; 7+ messages in thread
From: Denis Efremov @ 2020-06-04 14:08 UTC (permalink / raw)
To: Julia Lawall; +Cc: cocci, linux-kernel
Check for memset() with 0 followed by kfree().
Signed-off-by: Denis Efremov <efremov@linux.com>
---
Patches:
1. kzfree in drivers/w1 https://lkml.org/lkml/2020/6/4/438
2. kzfree in drivers/iommu/ https://lkml.org/lkml/2020/6/4/421
3. kzfree in drivers/scsi/ https://lkml.org/lkml/2020/6/4/442
scripts/coccinelle/api/kzfree.cocci | 53 +++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 scripts/coccinelle/api/kzfree.cocci
diff --git a/scripts/coccinelle/api/kzfree.cocci b/scripts/coccinelle/api/kzfree.cocci
new file mode 100644
index 000000000000..c6b8f7676af4
--- /dev/null
+++ b/scripts/coccinelle/api/kzfree.cocci
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Use kzfree rather than memset with 0 followed by kfree
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: kzfree
+//
+
+virtual context
+virtual org
+virtual report
+virtual patch
+
+// Ignore kzfree definition
+// Ignore kasan test
+@r depends on !patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c")@
+expression E;
+position p;
+@@
+
+(
+* memset(E, 0, ...);
+|
+* memset(E, '\0', ...);
+)
+* kfree(E)@p;
+
+@r1 depends on patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c")@
+expression E;
+@@
+
+(
+- memset(E, 0, ...);
+|
+- memset(E, '\0', ...);
+)
+- kfree(E);
++ kzfree(E);
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for kzfree")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for kzfree")
--
2.26.2
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [Cocci] [PATCH v2] coccinelle: api: add kzfree script
2020-06-04 14:08 [Cocci] [PATCH] " Denis Efremov
@ 2020-06-04 20:48 ` Denis Efremov
2020-06-04 20:57 ` Julia Lawall
0 siblings, 1 reply; 7+ messages in thread
From: Denis Efremov @ 2020-06-04 20:48 UTC (permalink / raw)
To: Julia Lawall; +Cc: Joe Perches, cocci, linux-kernel
Check for memset()/memset_explicit() with 0 followed by
kfree()/vfree()/kvfree().
Signed-off-by: Denis Efremov <efremov@linux.com>
---
Changes in v2:
- memset_explicit() added
- kvfree_sensitive() added
- forall added to r1
- ... between memset and kfree added
Unfortunately, it doesn't work as I would expect it to in "patch"
mode. I've added my comment about it in the rule. It can be safely
removed from the patch if I misunderstood something.
Another "strange" behaviour that I faced that r2 rule works only if I
write 2 expression lines:
expression *E;
expression size;
If I try to use a single line "expression *E, size;" then r2 matches nothing.
scripts/coccinelle/api/kzfree.cocci | 65 +++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 scripts/coccinelle/api/kzfree.cocci
diff --git a/scripts/coccinelle/api/kzfree.cocci b/scripts/coccinelle/api/kzfree.cocci
new file mode 100644
index 000000000000..5c7e4bb13bb7
--- /dev/null
+++ b/scripts/coccinelle/api/kzfree.cocci
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Use kzfree, kvfree_sensitive rather than memset or
+/// memset_explicit with 0 followed by kfree
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: kzfree, kvfree_sensitive
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+
+// Ignore kzfree definition
+// Ignore kasan test
+@r depends on !patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c") forall@
+expression *E;
+position p;
+@@
+
+* \(memset\|memset_explicit\)(E, 0, ...);
+ ... when != E
+ when strict
+* \(kfree\|vfree\|kvfree\)(E)@p;
+
+@r1 depends on patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c")@
+expression *E;
+expression size;
+@@
+
+- \(memset\|memset_explicit\)(E, 0, size);
+/// Unfortunately, it doesn't work as in !patch mode.
+/// spatch (v1.0.8) should patch 4 functions in linux 5.7 with this rule
+/// and uncommented "when" lines. With only "... when != E" line 2 functions
+/// are patched, none with "when strict". 3 functions patch is produced by the
+/// rule with "when" lines commented out.
+// ... when != E
+// when strict
+(
+- kfree(E);
++ kzfree(E);
+|
+- vfree(E);
++ kvfree_sensitive(E, size);
+|
+- kvfree(E);
++ kvfree_sensitive(E, size);
+)
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for kzfree/kvfree_sensitive")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for kzfree/kvfree_sensitive")
--
2.26.2
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
2020-06-04 20:48 ` [Cocci] [PATCH v2] " Denis Efremov
@ 2020-06-04 20:57 ` Julia Lawall
2020-06-04 21:03 ` Denis Efremov
2020-06-04 21:25 ` Denis Efremov
0 siblings, 2 replies; 7+ messages in thread
From: Julia Lawall @ 2020-06-04 20:57 UTC (permalink / raw)
To: Denis Efremov; +Cc: Joe Perches, cocci, linux-kernel
On Thu, 4 Jun 2020, Denis Efremov wrote:
> Check for memset()/memset_explicit() with 0 followed by
> kfree()/vfree()/kvfree().
>
> Signed-off-by: Denis Efremov <efremov@linux.com>
> ---
> Changes in v2:
> - memset_explicit() added
> - kvfree_sensitive() added
> - forall added to r1
> - ... between memset and kfree added
> Unfortunately, it doesn't work as I would expect it to in "patch"
> mode. I've added my comment about it in the rule. It can be safely
> removed from the patch if I misunderstood something.
>
> Another "strange" behaviour that I faced that r2 rule works only if I
> write 2 expression lines:
> expression *E;
> expression size;
> If I try to use a single line "expression *E, size;" then r2 matches nothing.
The parser for metavariables is not so smart. Everything to the left of
the first metavariable name is the type. Everything after is the list of
metavariables of that type. So if you put them together you require size
to be a pointer.
On the other hand, do you really require E to be a pointer? If you do
that, it will have to find the type of E. If E refers to a structure
field, then the type might not be available in the current function, and
you may need command line argments like --all-includes or
--recursive-includes. Is avoiding transforming the case where E is not
verified to be a pointer a concern?
julia
>
> scripts/coccinelle/api/kzfree.cocci | 65 +++++++++++++++++++++++++++++
> 1 file changed, 65 insertions(+)
> create mode 100644 scripts/coccinelle/api/kzfree.cocci
>
> diff --git a/scripts/coccinelle/api/kzfree.cocci b/scripts/coccinelle/api/kzfree.cocci
> new file mode 100644
> index 000000000000..5c7e4bb13bb7
> --- /dev/null
> +++ b/scripts/coccinelle/api/kzfree.cocci
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +///
> +/// Use kzfree, kvfree_sensitive rather than memset or
> +/// memset_explicit with 0 followed by kfree
> +///
> +// Confidence: High
> +// Copyright: (C) 2020 Denis Efremov ISPRAS
> +// Options: --no-includes --include-headers
> +//
> +// Keywords: kzfree, kvfree_sensitive
> +//
> +
> +virtual context
> +virtual patch
> +virtual org
> +virtual report
> +
> +
> +// Ignore kzfree definition
> +// Ignore kasan test
> +@r depends on !patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c") forall@
> +expression *E;
> +position p;
> +@@
> +
> +* \(memset\|memset_explicit\)(E, 0, ...);
> + ... when != E
> + when strict
> +* \(kfree\|vfree\|kvfree\)(E)@p;
> +
> +@r1 depends on patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c")@
> +expression *E;
> +expression size;
> +@@
> +
> +- \(memset\|memset_explicit\)(E, 0, size);
> +/// Unfortunately, it doesn't work as in !patch mode.
> +/// spatch (v1.0.8) should patch 4 functions in linux 5.7 with this rule
> +/// and uncommented "when" lines. With only "... when != E" line 2 functions
> +/// are patched, none with "when strict". 3 functions patch is produced by the
> +/// rule with "when" lines commented out.
> +// ... when != E
> +// when strict
> +(
> +- kfree(E);
> ++ kzfree(E);
> +|
> +- vfree(E);
> ++ kvfree_sensitive(E, size);
> +|
> +- kvfree(E);
> ++ kvfree_sensitive(E, size);
> +)
> +
> +@script:python depends on report@
> +p << r.p;
> +@@
> +
> +coccilib.report.print_report(p[0], "WARNING opportunity for kzfree/kvfree_sensitive")
> +
> +@script:python depends on org@
> +p << r.p;
> +@@
> +
> +coccilib.org.print_todo(p[0], "WARNING opportunity for kzfree/kvfree_sensitive")
> --
> 2.26.2
>
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
2020-06-04 20:57 ` Julia Lawall
@ 2020-06-04 21:03 ` Denis Efremov
2020-06-04 21:25 ` Denis Efremov
1 sibling, 0 replies; 7+ messages in thread
From: Denis Efremov @ 2020-06-04 21:03 UTC (permalink / raw)
To: Julia Lawall; +Cc: Joe Perches, cocci, linux-kernel
On 6/4/20 11:57 PM, Julia Lawall wrote:
> On the other hand, do you really require E to be a pointer? If you do
> that, it will have to find the type of E. If E refers to a structure
> field, then the type might not be available in the current function, and
> you may need command line argments like --all-includes or
> --recursive-includes. Is avoiding transforming the case where E is not
> verified to be a pointer a concern?
It's save to use "expression E;" in this rule. Do you want me to send v3?
Any thoughts about commented lines in "patch"? Maybe I do something wrong
trying to use "..." it in r2?
Thanks,
Denis
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
2020-06-04 20:57 ` Julia Lawall
2020-06-04 21:03 ` Denis Efremov
@ 2020-06-04 21:25 ` Denis Efremov
2020-06-06 8:16 ` Julia Lawall
1 sibling, 1 reply; 7+ messages in thread
From: Denis Efremov @ 2020-06-04 21:25 UTC (permalink / raw)
To: Julia Lawall; +Cc: Joe Perches, cocci, linux-kernel
It looks like a good idea to add type cast, like:
+// Ignore kzfree definition
+// Ignore kasan test
+@r depends on !patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c") forall@
+expression *E;
+position p;
+type T;
+@@
+
+* \(memset\|memset_explicit\)((T)E, 0, ...);
+ ... when != E
+ when strict
+* \(kfree\|vfree\|kvfree\)(E)@p;
+
and to exclude file "mm/util.c" because it will contain the definition of
kvfree_sensitive().
I will wait for your recommendation about commented lines and will send v3 after.
Thanks,
Denis
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Cocci] [PATCH v2] coccinelle: api: add kzfree script
2020-06-04 21:25 ` Denis Efremov
@ 2020-06-06 8:16 ` Julia Lawall
0 siblings, 0 replies; 7+ messages in thread
From: Julia Lawall @ 2020-06-06 8:16 UTC (permalink / raw)
To: Denis Efremov; +Cc: Joe Perches, cocci, linux-kernel
On Fri, 5 Jun 2020, Denis Efremov wrote:
> It looks like a good idea to add type cast, like:
>
> +// Ignore kzfree definition
> +// Ignore kasan test
> +@r depends on !patch && !(file in "lib/test_kasan.c") && !(file in "mm/slab_common.c") forall@
> +expression *E;
> +position p;
> +type T;
> +@@
> +
> +* \(memset\|memset_explicit\)((T)E, 0, ...);
> + ... when != E
> + when strict
> +* \(kfree\|vfree\|kvfree\)(E)@p;
> +
>
> and to exclude file "mm/util.c" because it will contain the definition of
> kvfree_sensitive().
>
> I will wait for your recommendation about commented lines and will send v3 after.
Instead of the file in things, maybe it would be simpler to say:
position p : script:ocaml() { not (List.mem (List.hd p).current_element ["kzfree";"..."]) };
Or:
@initialize:ocaml@
@@
let relevant p =
not (List.mem (List.hd p).current_element ["kzfree";"..."])
and then
position p : script:ocaml() { relevant p };
Or the python counterpart. It's true that the script is probably not
relevant to those files at all, but listing the specific functions would
avoid the need for the comments and make the issue more clear.
It's just a suggestion. If you prefer the file in solution, that's ok
too.
julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2020-06-06 8:16 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 5:50 [Cocci] [PATCH v2] coccinelle: api: add kzfree script Markus Elfring
-- strict thread matches above, loose matches on Subject: below --
2020-06-05 6:43 Markus Elfring
2020-06-04 14:08 [Cocci] [PATCH] " Denis Efremov
2020-06-04 20:48 ` [Cocci] [PATCH v2] " Denis Efremov
2020-06-04 20:57 ` Julia Lawall
2020-06-04 21:03 ` Denis Efremov
2020-06-04 21:25 ` Denis Efremov
2020-06-06 8:16 ` Julia Lawall
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).