All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: cocci@inria.fr
Subject: [cocci] metavariables in added attribute arguments
Date: Thu, 27 Apr 2023 13:50:35 -0700	[thread overview]
Message-ID: <644ae01c.170a0220.77675.8ed2@mx.google.com> (raw)

Hello,

I am trying to annotate structures with a new attribute that identifies
which struct member contains the count of elements for a given flexible
array member:

struct foo {
	...
	size_t element_count;
	...
	int element_array[];
};

becomes:

struct foo {
	...
	size_t element_count;
	...
	int element_array[] __counted_by(element_count);
};

From the grammar doc, I found this note: "Attribute metavariables are
only allowed in context or minus code, and not in added code". I would
expect that to only apply to the name of the attribute itself, but
that's not what I'm trying to do. Here is the .cocci file:

// Options: --include-headers

@allocated@
identifier STRUCT, ARRAY, COUNTER, CALC;
expression COUNT;
struct STRUCT *PTR;
identifier ALLOC =~ "(devm_)?[kv][cvzm]alloc";
@@

(
        CALC = struct_size(PTR, ARRAY, COUNT);
        ...
        PTR = ALLOC(..., CALC, ...);
|
        PTR = ALLOC(..., struct_size(PTR, ARRAY, COUNT), ...);
)
        ...
        PTR->COUNTER = COUNT;

@annotate@
type COUNTER_TYPE, ARRAY_TYPE;
identifier allocated.STRUCT;
identifier allocated.ARRAY;
identifier allocated.COUNTER;
attribute name __counted_by;
@@

 struct STRUCT {
        ...
        COUNTER_TYPE COUNTER;
        ...
        ARRAY_TYPE ARRAY[]
+       __counted_by(COUNTER)
        ;
 };

This fails like so:

$ cocci element_count.cocci net/packet/af_packet.c
plus: parse error: 
  File "element_count.cocci", line 33, column 15, charpos = 593
  around = 'COUNTER',
  whole content = +     __counted_by(COUNTER)

But I can produce (nonsense) output if I change the replacement to:

	ARRAY_TYPE ARRAY[
+	COUNTER
	]
+	__counted_by
	;

Explicitly using "attribute name __counted_by;" didn't seem to help.
What am I missing?

Thanks!

-Kees

-- 
Kees Cook

             reply	other threads:[~2023-04-28  8:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27 20:50 Kees Cook [this message]
2023-04-28  8:18 ` [cocci] metavariables in added attribute arguments Julia Lawall
2023-04-29 13:17 ` Julia Lawall
2023-05-01 20:54   ` Kees Cook
2023-05-01 21:27     ` Julia Lawall

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=644ae01c.170a0220.77675.8ed2@mx.google.com \
    --to=keescook@chromium.org \
    --cc=cocci@inria.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.