Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Kees Cook <kees@kernel.org>
Cc: cocci@inria.fr
Subject: Re: [cocci] __free() attribute inhibits matching?
Date: Thu, 20 Mar 2025 00:25:00 +0100 (CET)	[thread overview]
Message-ID: <1b774129-badf-f889-8d3c-c6492097d3f@inria.fr> (raw)
In-Reply-To: <202503191424.2FA8A7E@keescook>



On Wed, 19 Mar 2025, Kees Cook wrote:

> On Wed, Mar 19, 2025 at 08:44:38AM +0100, Julia Lawall wrote:
> > On Tue, 18 Mar 2025, Kees Cook wrote:
> > > On Tue, Mar 18, 2025 at 06:56:40PM +0100, Julia Lawall wrote:
> > > > I think you want
> > > >
> > > > Expression *m;
> > > >
> > > > A->@m b
> > > >
> > > > B would be an identifier
> > >
> > > But how does "B" get correctly type restricted? (Or in my example,
> > > "NAME"?)
> > >
> > > I tried:
> > >
> > > @direct depends on !(file in "tools") && !(file in "samples")@
> > > type TYPE;
> > > TYPE *P;
> > > TYPE *MEMBER;
> > > TYPE INST;
> > > identifier NAME;
> > > expression COMP;
> > > identifier ALLOC =~ "^kv?[mz]alloc";
> > > fresh identifier ALLOC_OBJ = ALLOC ## "_obj";
> > > @@
> > >
> > >        \(\(P\|COMP.@MEMBER NAME\|COMP->@MEMBER NAME\) \& VAR\)
> > > -       = ALLOC((\(sizeof(*VAR)\|sizeof(TYPE)\|sizeof(INST)\)), GFP);
> > > +       = ALLOC_OBJ(*VAR, GFP);
> > >
> > > This parses, but don't match (from crypto/asymmetric_keys/x509_cert_parser.c):
> > >
> > > 	cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
> > >
> > > But does match (via "P"):
> > >
> > > 	cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
> >
> > What is the command line, eg --all-includes?  You can try
> > --verbose-includes to see what is happening with #include "x509_parser.h"
>
> Okay, my hiccups continue.
>
> I do not seem to be able to mix a "type" metavariable with anything else.
> For example, instead of these rules repeated:
>
> @direct depends on patch@
> type TYPE;
> TYPE INST;
> identifier PTR;
> expression GFP;
> expression TYPED;
> identifier ALLOC =~ "^kv?[mz]alloc$";
> fresh identifier ALLOC_OBJ = ALLOC ## "_obj";
> @@
>
> (
>         TYPE *PTR
> -       = ALLOC((sizeof(*PTR)), GFP);
> +       = ALLOC_OBJ(*PTR, GFP);
> |
>         TYPE *PTR
> -       = ALLOC((sizeof(INST)), GFP);
> +       = ALLOC_OBJ(INST, GFP);
> |
>         TYPE *PTR
> -       = ALLOC((sizeof(TYPE)), GFP);
> +       = ALLOC_OBJ(TYPE, GFP);
> )
>
> I wanted to write it as:
>
>         TYPE *PTR
> -       = ALLOC((sizeof(\(\(*PTR\|INST\|TYPE\) \& TYPED\))), GFP);
> +       = ALLOC_OBJ(TYPED, GFP);
>
> But this doesn't let me even put "TYPE" in the \( \) at all, much less
> put it into the TYPED expression:
>
> minus: parse error:
>   File "scripts/coccinelle/api/kmalloc_objs.cocci", line 55, column 34, charpos = 1235
>   around = 'TYPE',
>   whole content = -     = ALLOC((sizeof(\(\(*PTR\|INST\|TYPE\) \& TYPED\))), GFP);
>
> How do I collapse these three into 1 rule?

A disjunction has to have the same kind of thing in every branch.  INST is
an expression but TYPE is a type, so that doesn't work.  sizeof is
misleading.  There are actually two different sizeof's, one where the
argument is an expression and one where the argument is a type.  One
requires () around the argument while the other does not.

You can just make a () with three copies of sizeof, or even two copies of
sizeof, on for TYPE and one for a disjunction of the other possibilities.

julia

  reply	other threads:[~2025-03-19 23:25 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-17 18:44 [cocci] __free() attribute inhibits matching? Kees Cook
2025-03-17 20:59 ` Julia Lawall
2025-03-17 23:02   ` Kees Cook
2025-03-17 23:13     ` Julia Lawall
2025-03-18 15:39       ` Kees Cook
2025-03-18 15:43         ` Julia Lawall
2025-03-18 16:30           ` Kees Cook
2025-03-18 17:56             ` Julia Lawall
2025-03-19  3:31               ` Kees Cook
2025-03-19  5:55                 ` Julia Lawall
2025-03-19  7:20                   ` [cocci] Checking the determination of type information? Markus Elfring
2025-03-19  7:44                 ` [cocci] __free() attribute inhibits matching? Julia Lawall
2025-03-19 20:06                   ` Kees Cook
2025-03-19 21:30                   ` Kees Cook
2025-03-19 23:25                     ` Julia Lawall [this message]
2025-03-21 13:52                     ` Markus Elfring
2025-03-19 10:24                 ` Markus Elfring
2025-03-19 10:57                 ` Markus Elfring
2025-03-18 17:43 ` Markus Elfring
2025-03-18 18:02   ` Kees Cook
2025-03-18 19:18     ` Markus Elfring

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=1b774129-badf-f889-8d3c-c6492097d3f@inria.fr \
    --to=julia.lawall@inria.fr \
    --cc=cocci@inria.fr \
    --cc=kees@kernel.org \
    /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).