Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Yuan Can <yuancan@huawei.com>
To: Julia Lawall <julia.lawall@inria.fr>
Cc: <nicolas.palix@imag.fr>, <cocci@inria.fr>
Subject: Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
Date: Mon, 19 Sep 2022 16:10:04 +0800	[thread overview]
Message-ID: <33207231-7aa2-a5ad-1823-2d0d43c20f6c@huawei.com> (raw)
In-Reply-To: <alpine.DEB.2.22.394.2209190959040.2725@hadrien>


在 2022/9/19 16:01, Julia Lawall 写道:
>>> I would also suggest to print out more information, to help the user
>>> better undestand where the problem may be.  Specifically, it would be nice
>>> to have the name of the lock field, the position of the declaration of
>>> this field, and the position of the call to mutex_lock.  You already have
>>> the relevant metavariables in r3, and can add a position metavariable in
>>> r4.
>> Agreed, but how could I fetch the name of the lock field? I cannot find a doc
>> describes how
>>
>> to achieve this(or did I miss it?).
> r3.fld.  r3.s could be useful also.
>
> Perhaps your question is about how to reference them from within python?
> Actually, any metavaraible can be imported into the python script, not
> just a potision variable.  So you could write
>
> s << r3.s;

Yes, that is exactly what I want, thank you.

Best regards,

Yuan Can

>
>>> Are there some patches in the Linux kernel that are based on these rules?
>> Yes, there are many patches based on the two rules.
> OK, great, thanks.
>
> julia
>
>> Best regards,
>>
>> Yuan Can
>>
>>> thanks,
>>> julia
>>>
>>>> Signed-off-by: Yuan Can <yuancan@huawei.com>
>>>> ---
>>>>    .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
>>>>    1 file changed, 68 insertions(+)
>>>>    create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
>>>>
>>>> diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> b/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> new file mode 100644
>>>> index 000000000000..bec8ce60d0c5
>>>> --- /dev/null
>>>> +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> @@ -0,0 +1,68 @@
>>>> +// SPDX-License-Identifier: GPL-2.0-only
>>>> +/// report missing mutex_init()
>>>> +///
>>>> +/// Report mutex used without initialize. False positives can occur
>>>> +/// when the mutex allocation and initialization happens in two
>>>> +/// different files.
>>>> +///
>>>> +// Copyright: (C) 2022 Huawei Technologies Co, Ltd.
>>>> +// Comments:
>>>> +// Options: --include-headers
>>>> +
>>>> +virtual org
>>>> +virtual report
>>>> +
>>>> +@r1@
>>>> +identifier s, fld;
>>>> +struct s *mm;
>>>> +@@
>>>> +mutex_init(\(&mm->fld\|&(mm->fld)\))
>>>> +
>>>> +@r2@
>>>> +identifier r1.s, r1.fld;
>>>> +position p;
>>>> +@@
>>>> +
>>>> +struct s {
>>>> +  ...
>>>> +  struct mutex fld@p;
>>>> +  ...
>>>> +};
>>>> +
>>>> +@r3@
>>>> +identifier s, fld;
>>>> +position p != {r2.p};
>>>> +@@
>>>> +
>>>> +struct s {
>>>> +  ...
>>>> +  struct mutex fld@p;
>>>> +  ...
>>>> +};
>>>> +
>>>> +@r4@
>>>> +identifier r3.fld;
>>>> +identifier r3.s;
>>>> +struct s *mm;
>>>> +@@
>>>> +
>>>> +mutex_lock(&mm->fld)
>>>> +
>>>> +@r5 depends on r4@
>>>> +identifier r3.s;
>>>> +struct s *mm;
>>>> +position p;
>>>> +@@
>>>> +* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)
>>>> +
>>>> +@script:python depends on org@
>>>> +p << r5.p;
>>>> +@@
>>>> +cocci.print_main("Mutex inside the struct malloced here is possibly used
>>>> without init.", p)
>>>> +
>>>> +@script:python depends on report@
>>>> +p << r5.p;
>>>> +@@
>>>> +
>>>> +msg = "Mutex inside the struct malloced here is possibly used without
>>>> init."
>>>> +coccilib.report.print_report(p[0], msg)
>>>> --
>>>> 2.17.1
>>>>
>>>>

      reply	other threads:[~2022-09-19  8:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
2022-09-16 14:43 ` [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Markus Elfring
2022-09-17  6:40 ` Markus Elfring
2022-09-18 19:39 ` Julia Lawall
2022-09-19  7:55   ` Yuan Can
2022-09-19  8:01     ` Julia Lawall
2022-09-19  8:10       ` Yuan Can [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=33207231-7aa2-a5ad-1823-2d0d43c20f6c@huawei.com \
    --to=yuancan@huawei.com \
    --cc=cocci@inria.fr \
    --cc=julia.lawall@inria.fr \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).