kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scripts: coccinelle: add uses of memzero_explicit
@ 2014-12-10 20:08 Julia Lawall
  2014-12-10 21:43 ` SF Markus Elfring
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Julia Lawall @ 2014-12-10 20:08 UTC (permalink / raw)
  To: cocci

Memzero_explicit is a version of memset that is resistent to compiler
optimizations when the set region is about to go out of scope.

This was suggested by Daniel Borkmann

Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>

---
 scripts/coccinelle/api/memzero_explicit.cocci |  289 ++++++++++++++++++++++++++
 1 file changed, 289 insertions(+)

diff --git a/scripts/coccinelle/api/memzero_explicit.cocci b/scripts/coccinelle/api/memzero_explicit.cocci
new file mode 100644
index 0000000..bec0350
--- /dev/null
+++ b/scripts/coccinelle/api/memzero_explicit.cocci
@@ -0,0 +1,289 @@
+/// Replace memset on a variable that is about to go out of scope by
+/// memzero_explicit to prevent removal by compiler optimizations.
+///
+// Confidence: High
+// Copyright: (C) 2014 Julia Lawall, Inria, GPLv2
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@ar1 depends on patch && !context && !org && !report@
+identifier x;
+local idexpression e;
+type T,T1;
+@@
+
+{
+... when any
+T x[...];
+... when any
+    when exists
+(
+e = (T1)x
+|
+e = (T1)&x[0]
+)
+... when any
+    when exists
+- memset
++ memzero_explicit
+  (x,
+-0,
+  ...)
+... when != x
+    when != e
+    when strict
+}
+
+@str1 depends on patch && !context && !org && !report@
+identifier x;
+local idexpression e;
+type T,T1;
+@@
+
+{
+... when any
+T1 x;
+... when any
+    when exists
+e = (T)&x
+... when any
+    when exists
+- memset
++ memzero_explicit
+  (&x,
+-0,
+  ...)
+... when != x
+    when != e
+    when strict
+}
+
+// ------------------------------------------------------------------------
+
+@ar2 depends on patch && !context && !org && !report@
+identifier x;
+type T,T1;
+expression e;
+@@
+
+{
+... when any
+T x[...];
+... when any
+    when exists
+    when != e = (T1)x
+    when != e = (T1)&x[0]
+- memset
++ memzero_explicit
+  (x,
+-0,
+  ...)
+... when != x
+    when strict
+}
+
+@str2 depends on patch && !context && !org && !report@
+identifier x;
+expression e;
+type T,T1;
+@@
+
+{
+... when any
+T1 x;
+... when any
+    when exists
+    when != e = (T)&x
+- memset
++ memzero_explicit
+  (&x,
+-0,
+  ...)
+... when != x
+    when strict
+}
+
+// ----------------------------------------------------------------------------
+
+@ar1_context depends on !patch && (context || org || report)@
+type T, T1;
+identifier x;
+local idexpression e;
+position j0, j1, j2;
+@@
+
+{
+... when any
+T x@j1[...];
+... when any
+    when exists
+(
+e@j2 = (T1)x
+|
+e@j2 = (T1)&x[0]
+)
+... when any
+    when exists
+ memset@j0
+  (x,
+* 0,
+  ...)
+... when != x
+    when != e
+    when strict
+    when forall
+}
+
+@str1_context depends on !patch && (context || org || report)@
+type T, T1;
+identifier x;
+local idexpression e;
+position j0, j1, j2;
+@@
+
+{
+... when any
+T1 x@j1;
+... when any
+    when exists
+e@j2 = (T)&x
+... when any
+    when exists
+ memset@j0
+  (&x,
+* 0,
+  ...)
+... when != x
+    when != e
+    when strict
+    when forall
+}
+
+@ar2_context depends on !patch && (context || org || report)@
+type T, T1;
+identifier x;
+expression e;
+position j0, j1;
+@@
+
+{
+... when any
+T x@j1[...];
+... when any
+    when exists
+    when != e = (T1)x
+    when != e = (T1)&x[0]
+ memset@j0
+  (x,
+* 0,
+  ...)
+... when != x
+    when strict
+    when forall
+}
+
+@str2_context depends on !patch && (context || org || report)@
+type T, T1;
+identifier x;
+expression e;
+position j0, j1;
+@@
+
+{
+... when any
+T1 x@j1;
+... when any
+    when exists
+    when != e = (T)&x
+ memset@j0
+  (&x,
+* 0,
+  ...)
+... when != x
+    when strict
+    when forall
+}
+
+// ----------------------------------------------------------------------------
+
+@script:python ar1_org depends on org@
+j0 << ar1_context.j0;
+j1 << ar1_context.j1;
+j2 << ar1_context.j2;
+@@
+
+msg = "Memset call."
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "declaration")
+coccilib.org.print_link(j2[0], "alias")
+
+@script:python str1_org depends on org@
+j0 << str1_context.j0;
+j1 << str1_context.j1;
+j2 << str1_context.j2;
+@@
+
+msg = "Memset call."
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "declaration")
+coccilib.org.print_link(j2[0], "alias")
+
+@script:python ar2_org depends on org@
+j0 << ar2_context.j0;
+j1 << ar2_context.j1;
+@@
+
+msg = "Memset call."
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "declaration")
+
+@script:python str2_org depends on org@
+j0 << str2_context.j0;
+j1 << str2_context.j1;
+@@
+
+msg = "Memset call."
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "declaration")
+
+// ----------------------------------------------------------------------------
+
+@script:python ar1_report depends on report@
+j0 << ar1_context.j0;
+j1 << ar1_context.j1;
+j2 << ar1_context.j2;
+@@
+
+msg = "Memset call, declaration on line %s, alias on line %s." % \
+           (j1[0].line,j2[0].line)
+coccilib.report.print_report(j0[0], msg)
+
+@script:python str1_report depends on report@
+j0 << str1_context.j0;
+j1 << str1_context.j1;
+j2 << str1_context.j2;
+@@
+
+msg = "Memset call, declaration on line %s, alias on line %s." % \
+           (j1[0].line,j2[0].line)
+coccilib.report.print_report(j0[0], msg)
+
+@script:python ar2_report depends on report@
+j0 << ar2_context.j0;
+j1 << ar2_context.j1;
+@@
+
+msg = "Memset call, declaration on line %s." % (j1[0].line)
+coccilib.report.print_report(j0[0], msg)
+
+@script:python str2_report depends on report@
+j0 << str2_context.j0;
+j1 << str2_context.j1;
+@@
+
+msg = "Memset call declaration on line %s." % (j1[0].line)
+coccilib.report.print_report(j0[0], msg)
+


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

* Re: [PATCH] scripts: coccinelle: add uses of memzero_explicit
  2014-12-10 20:08 [PATCH] scripts: coccinelle: add uses of memzero_explicit Julia Lawall
@ 2014-12-10 21:43 ` SF Markus Elfring
  2014-12-10 23:18 ` Daniel Borkmann
  2014-12-10 23:48 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: SF Markus Elfring @ 2014-12-10 21:43 UTC (permalink / raw)
  To: cocci

> Memzero_explicit is a version of memset that is resistent to compiler
> optimizations when the set region is about to go out of scope.

Thanks for another interesting update suggestion.


> This was suggested by Daniel Borkmann

How do you think about to amend such a commit message here?

Would it eventually be better to use a tag like "Suggested-by" there
(instead of finishing this sentence with a full stop)?

Regards,
Markus

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

* Re: [PATCH] scripts: coccinelle: add uses of memzero_explicit
  2014-12-10 20:08 [PATCH] scripts: coccinelle: add uses of memzero_explicit Julia Lawall
  2014-12-10 21:43 ` SF Markus Elfring
@ 2014-12-10 23:18 ` Daniel Borkmann
  2014-12-10 23:48 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2014-12-10 23:18 UTC (permalink / raw)
  To: kernel-janitors

On 12/10/2014 09:08 PM, Julia Lawall wrote:
> Memzero_explicit is a version of memset that is resistent to compiler
> optimizations when the set region is about to go out of scope.
>
> This was suggested by Daniel Borkmann
>
> Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>

This looks great, thanks a lot Julia!

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

* Re: [PATCH] scripts: coccinelle: add uses of memzero_explicit
  2014-12-10 20:08 [PATCH] scripts: coccinelle: add uses of memzero_explicit Julia Lawall
  2014-12-10 21:43 ` SF Markus Elfring
  2014-12-10 23:18 ` Daniel Borkmann
@ 2014-12-10 23:48 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2014-12-10 23:48 UTC (permalink / raw)
  To: kernel-janitors

On 12/10/2014 10:43 PM, SF Markus Elfring wrote:
>> Memzero_explicit is a version of memset that is resistent to compiler
>> optimizations when the set region is about to go out of scope.
>
> Thanks for another interesting update suggestion.
>
>> This was suggested by Daniel Borkmann
>
> How do you think about to amend such a commit message here?
>
> Would it eventually be better to use a tag like "Suggested-by" there
> (instead of finishing this sentence with a full stop)?

It might be better in future, but it's not a big deal.

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

end of thread, other threads:[~2014-12-10 23:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-10 20:08 [PATCH] scripts: coccinelle: add uses of memzero_explicit Julia Lawall
2014-12-10 21:43 ` SF Markus Elfring
2014-12-10 23:18 ` Daniel Borkmann
2014-12-10 23:48 ` Daniel Borkmann

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).