Skip to contentSkip to conversation

Coccinelle : Script to detect cast after memory allocation

PATCH In review Himangi Saraogi 3 participants 8 messages Jun 14, 2014 Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Coccinelle : Script to detect cast after memory allocation

This script detects cases of use of cast for the value returned by kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc, kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes the cast as it is not useful. This Coccinelle script replaces drop_kmalloc_cast.cocci as it removes the casting in more limited cases of kmalloc, kzalloc and kcalloc.

---
 scripts/coccinelle/api/alloc/alloc_cast.cocci      | 72 ++++++++++++++++++++++
 .../coccinelle/api/alloc/drop_kmalloc_cast.cocci   | 67 --------------------
 2 files changed, 72 insertions(+), 67 deletions(-)
 create mode 100644 scripts/coccinelle/api/alloc/alloc_cast.cocci
 delete mode 100644 scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci
scripts/coccinelle/api/alloc/alloc_cast.cocci new file +72 -0
new file mode 100644
index 0000000..6c308ee
@@ -0,0 +1,72 @@
+/// Remove casting the values returned by memory allocation functions
+/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
+///
+//# This makes an effort to find cases of casting of values returned by
+//# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
+//# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
+//# the casting as it is not required. The result in the patch case may
+//#need some reformatting.
+//
+// Confidence: High
+// Copyright: 2014, Himangi Saraogi  GPLv2.
+// Comments:
+// Options: --no-includes --include-headers
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+ at depends on context@
+type T;
+@@
+
+* (T *)
+  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
+   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...)
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+ at depends on patch@
+type T;
+@@
+
+- (T *)
+  (\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
+   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...))
+
+//----------------------------------------------------------
+//  For org and report mode
+//----------------------------------------------------------
+
+ at r depends on org || report@
+type T;
+position p;
+@@
+
+ (T at p *)\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
+   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...)
+
+ at script:python depends on org@
+p << r.p;
+t << r.T;
+@@
+
+coccilib.org.print_safe_todo(p[0], t)
+
+ at script:python depends on report@
+p << r.p;
+t << r.T;
+@@
+
+msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
+coccilib.report.print_report(p[0], msg)
+
+
scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci deleted +0 -67

67 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 6 lines · Himangi Saraogi

This script detects cases of use of cast for the value returned by kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc, kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes the cast as it is not useful. This Coccinelle script replaces drop_kmalloc_cast.cocci as it removes the casting in more limited cases of kmalloc, kzalloc and kcalloc.

Perhaps make this more generic for any void *?

Something like:

@@
void *t;
type other;
@@

- (other *)t
+ t

#2 lore raw
Quoted 17 lines · Joe Perches
Quoted 6 lines · Himangi Saraogi

This script detects cases of use of cast for the value returned by kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc, kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes the cast as it is not useful. This Coccinelle script replaces drop_kmalloc_cast.cocci as it removes the casting in more limited cases of kmalloc, kzalloc and kcalloc.

Perhaps make this more generic for any void *?

Something like:

@@
void *t;
type other;
@@

Review

Quoted 2 lines
(quoted diff) +1 -1
-       (other *)t
+       t

Himangi, could you try it and check that you get at least the results that you get with the current rule? Because I'm not sure that Coccinelle would have the type information available to know what is the return type of eg kmalloc. Maybe you would need to give the option --recursive-includes.

Perhaps it would be reasonable to add this among the specific functions. That is, t could be part of the disjunctions.

julia

#3 lore raw
Quoted 1 line · Julia Lawall

On Sat, 14 Jun 2014, Joe Perches wrote:

[]

Quoted 11 lines

Review

Quoted 11 lines

Perhaps make this more generic for any void *?

Something like:

@@
void *t;
type other;
@@

(quoted diff) +1 -1
-       (other *)t
+       t

[]

Quoted 2 lines

Perhaps it would be reasonable to add this among the specific functions. That is, t could be part of the disjunctions.

You do have to make sure that the
"casted to" type is not dereferenced.

ie: don't transform

void func(void *foo)

{
	unsigned long bar = *(unsigned long *)foo;
}

Also there may be some __user cast types and
such that may be necessary to exclude too.

#4 lore raw

PATCH 1/1 Coccinelle : Script to detect cast after memory allocation

Hi,

I have run the generic rule but it does not detect the cases of cast where the k[mzc]alloc or the kmem functions are used. I have used flags like recursive-includes, as suggested by Julia, but not any of the cases covered by the original script are detected.

Thanks
Himangi

Quoted 23 lines · Joe Perches
Quoted 1 line · Julia Lawall

On Sat, 14 Jun 2014, Joe Perches wrote:

[]

Quoted 11 lines

Review

Quoted 11 lines

Perhaps make this more generic for any void *?

Something like:

@@
void *t;
type other;
@@

(quoted diff) +1 -1
-       (other *)t
+       t

[]

Quoted 2 lines

Perhaps it would be reasonable to add this among the specific functions. That is, t could be part of the disjunctions.

You do have to make sure that the
"casted to" type is not dereferenced.

ie: don't transform

Quoted 11 lines

void func(void *foo)

{
        unsigned long bar = *(unsigned long *)foo;
}

Also there may be some __user cast types and
such that may be necessary to exclude too.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20140615/9561dd11/attachment.html>

#5 lore raw
Quoted 6 lines · Himangi Saraogi

Hi,

I have run the generic rule but it does not detect the cases of cast where the k[mzc]alloc or the kmem functions are used. I have used flags like recursive-includes, as suggested by Julia, but not any of the cases covered by the original script are detected.

Are other things detected? You could always expand the rule to be more comprehensive.

julia

Quoted 42 lines

Thanks
Himangi

On 14 June 2014 22:31, Joe Perches <[email protected]> wrote:
On Sat, 2014-06-14 at 18:11 +0200, Julia Lawall wrote:
> On Sat, 14 Jun 2014, Joe Perches wrote:
[]

Quoted 11 lines
Quoted 11 lines

Perhaps make this more generic for any void *?

Something like:

@@
void *t;
type other;
@@

- ? ? ? (other *)t
+ ? ? ? t

[]

Quoted 1 line

Perhaps it would be reasonable to add this among the specific

functions.

Quoted 1 line

That is, t could be part of the disjunctions.

You do have to make sure that the
"casted to" type is not dereferenced.

ie: don't transform

void func(void *foo)
{
? ? ? ? unsigned long bar = *(unsigned long *)foo; }

Also there may be some __user cast types and
such that may be necessary to exclude too.

#6 lore raw
In reply to Himangi Saraogi
Quoted 1 line · Himangi Saraogi

Hi,

Hi Himangi.

Quoted 4 lines

I have run the generic rule but it does not detect the cases of cast where the k[mzc]alloc or the kmem functions are used. I have used flags like recursive-includes, as suggested by Julia, but not any of the cases covered by the original script are detected.

Odd.

When I tried it I got things like:

$ cat void.cocci 
@@
void *t;
type other;
@@

- (other *)t
+ t

$ spatch --version
spatch version 1.0.0-rc14 without Python support and with PCRE support
$ spatch --sp-file void.cocci --recursive-includes drivers/block/cciss_scsi.c
drivers/block/cciss_scsi.c
+++ /tmp/cocci-output-8427-7875f0-cciss_scsi.c
@@ -704,8 +704,7 @@ cciss_scsi_setup(ctlr_info_t *h)
 	struct cciss_scsi_adapter_data_t * shba;

ccissscsi[h->ctlr].ndevices = 0;
- shba = (struct cciss_scsi_adapter_data_t *)
- kmalloc(sizeof(*shba), GFP_KERNEL);
+ shba = kmalloc(sizeof(*shba), GFP_KERNEL);
if (shba == NULL)
return;
shba->scsi_host = NULL;

#7 lore raw

PATCH 1/1 Coccinelle : Script to detect cast after memory allocation

Quoted 10 lines · Joe Perches
Quoted 1 line · Himangi Saraogi

Hi,

Hi Himangi.

Quoted 4 lines

I have run the generic rule but it does not detect the cases of cast where the k[mzc]alloc or the kmem functions are used. I have used flags like recursive-includes, as suggested by Julia, but not any of the cases covered by the original script are detected.

Quoted 5 lines

Odd.

When I tried it I got things like:

Thanks for your feedback. Sorry, yes it does detect, I had already sent

patches for the files and hence my tree did not have the casts.

Quoted 20 lines
$ cat void.cocci
@@
void *t;
type other;
@@
(quoted diff) +1 -1
-       (other *)t
+       t
$ spatch --version
spatch version 1.0.0-rc14 without Python support and with PCRE support
$ spatch --sp-file void.cocci --recursive-includes
drivers/block/cciss_scsi.c
drivers/block/cciss_scsi.c
+++ /tmp/cocci-output-8427-7875f0-cciss_scsi.c

Review cciss_scsi_setup(ctlr_info_t *h) · line 710

Quoted 9 lines
(quoted diff) +1 -2
@@ -704,8 +704,7 @@ cciss_scsi_setup(ctlr_info_t *h)
        struct cciss_scsi_adapter_data_t * shba;
 
        ccissscsi[h->ctlr].ndevices = 0;
-       shba = (struct cciss_scsi_adapter_data_t *)
-               kmalloc(sizeof(*shba), GFP_KERNEL);
+       shba = kmalloc(sizeof(*shba), GFP_KERNEL);
        if (shba == NULL)
                return;
        shba->scsi_host = NULL;

I'll be extending the patch to handle generic cases.
Thanks
Himangi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20140615/7d1a3777/attachment.html>