All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] metavariables in added attribute arguments
@ 2023-04-27 20:50 Kees Cook
  2023-04-28  8:18 ` Julia Lawall
  2023-04-29 13:17 ` Julia Lawall
  0 siblings, 2 replies; 5+ messages in thread
From: Kees Cook @ 2023-04-27 20:50 UTC (permalink / raw)
  To: cocci

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [cocci] metavariables in added attribute arguments
  2023-04-27 20:50 [cocci] metavariables in added attribute arguments Kees Cook
@ 2023-04-28  8:18 ` Julia Lawall
  2023-04-29 13:17 ` Julia Lawall
  1 sibling, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2023-04-28  8:18 UTC (permalink / raw)
  To: Kees Cook; +Cc: cocci



On Thu, 27 Apr 2023, Kees Cook wrote:

> 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

Sorry, but the documentation is quite out of date in that regard.
Attribute metavariables are supported everywhere now.

> 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?

I'll look into it.  There has been a lot of work on attributes recently,
but I was focusing on individual names, and not things with arguments, so
something may have been overlooked.

julia

>
> Thanks!
>
> -Kees
>
> --
> Kees Cook
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [cocci] metavariables in added attribute arguments
  2023-04-27 20:50 [cocci] metavariables in added attribute arguments Kees Cook
  2023-04-28  8:18 ` Julia Lawall
@ 2023-04-29 13:17 ` Julia Lawall
  2023-05-01 20:54   ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2023-04-29 13:17 UTC (permalink / raw)
  To: Kees Cook; +Cc: cocci



On Thu, 27 Apr 2023, Kees Cook wrote:

> 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?

The problem is fixed.  Actually, attributes in this position were just not
supported at all.  Thanks very much for the report.

julia

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [cocci] metavariables in added attribute arguments
  2023-04-29 13:17 ` Julia Lawall
@ 2023-05-01 20:54   ` Kees Cook
  2023-05-01 21:27     ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2023-05-01 20:54 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Sat, Apr 29, 2023 at 03:17:40PM +0200, Julia Lawall wrote:
> > [...]
> > @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?
> 
> The problem is fixed.  Actually, attributes in this position were just not
> supported at all.  Thanks very much for the report.

Hurray! Thank you; the latest git works as I'd expect now. :) Now to get
my patch for the Linux kernel split up...

 153 files changed, 157 insertions(+), 157 deletions(-)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [cocci] metavariables in added attribute arguments
  2023-05-01 20:54   ` Kees Cook
@ 2023-05-01 21:27     ` Julia Lawall
  0 siblings, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2023-05-01 21:27 UTC (permalink / raw)
  To: Kees Cook; +Cc: Julia Lawall, cocci



On Mon, 1 May 2023, Kees Cook wrote:

> On Sat, Apr 29, 2023 at 03:17:40PM +0200, Julia Lawall wrote:
> > > [...]
> > > @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?
> >
> > The problem is fixed.  Actually, attributes in this position were just not
> > supported at all.  Thanks very much for the report.
>
> Hurray! Thank you; the latest git works as I'd expect now. :) Now to get
> my patch for the Linux kernel split up...
>
>  153 files changed, 157 insertions(+), 157 deletions(-)

:)

julia

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-05-04  5:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-27 20:50 [cocci] metavariables in added attribute arguments Kees Cook
2023-04-28  8:18 ` Julia Lawall
2023-04-29 13:17 ` Julia Lawall
2023-05-01 20:54   ` Kees Cook
2023-05-01 21:27     ` Julia Lawall

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.