mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@lip6.fr>
To: Mathieu Malaterre <malat@debian.org>
Cc: Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Michal Marek <michal.lkml@markovi.net>,
	cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org
Subject: Re: cocci script for detecting alloc_apertures mem leak
Date: Thu, 1 Feb 2018 20:54:25 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.20.1802012041460.2188@hadrien> (raw)
In-Reply-To: <CA+7wUsz+Ud6+LFVdVs7-pPMSJmW-7opXCqs2NuYP3=enYxoD4A@mail.gmail.com>



On Thu, 1 Feb 2018, Mathieu Malaterre wrote:

> Hi cocci gurus,
>
> I am wondering if coccinelle can handle detection of kzalloc mem leak
> (within alloc_apertures call) ? Typically:
>
> $ cat drivers/video/fbdev/vesafb.c
> static int vesafb_probe(struct platform_device *dev)
> [...]
>   info->apertures = alloc_apertures(1);
>
> but then:
>
> static void vesafb_destroy(struct fb_info *info)
> {
>   struct vesafb_par *par = info->par;
>
>   fb_dealloc_cmap(&info->cmap);
>   arch_phys_wc_del(par->wc_cookie);
>   if (info->screen_base)
>     iounmap(info->screen_base);
>   release_mem_region(info->apertures->ranges[0].base,
> info->apertures->ranges[0].size);
> }
>
> For reference:
>
> $ cat include/linux/fb.h
> static inline struct apertures_struct *alloc_apertures(unsigned int max_num) {
>   struct apertures_struct *a = kzalloc(sizeof(struct apertures_struct)
>     + max_num * sizeof(struct aperture), GFP_KERNEL);

You could do something like this:

@nm@
identifier i,j,prb,rem;
@@

struct i j = { .prob = prb, .rem = remove, };

@a exists@
identifier nm.prb;
expression e;
@@

prb(...) { <+... e = alloc_apertures(...) ...+> }

@@
identifier nm.rem;
expression a.e;
@@

*rem(...) {
   ... when != kfree(e)
}

This is assuming that the reference to the alloc_apertures value is made
in the same way in the probe and remove function.  If this is not the
case, you have to figure out how to express some relation between them.

This is also assuming that the kfree is directly in the remove function,
not in some function called by it.  If that hypothesis does not hold, it
might be better to just report any cases where ther is no call to kfree(e)
in the whole file.  For this you could replace the last rule with:

@ok@
expression a.e;
@@

kfree(e);

@depends on !ok@
expression a.e;
@@

* e = alloc_apertures(...)

That would be checking for files that don't free the result os
alloc_apertures anywhere.

If you want to do a full interprocedural analysis, it is possible, but
more complicated.  You could look at coccinelle/demos/iteration.cocci.

julia

      reply	other threads:[~2018-02-01 19:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-01 16:29 Mathieu Malaterre
2018-02-01 19:54 ` Julia Lawall [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.20.1802012041460.2188@hadrien \
    --to=julia.lawall@lip6.fr \
    --cc=Gilles.Muller@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=malat@debian.org \
    --cc=michal.lkml@markovi.net \
    --cc=nicolas.palix@imag.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®