Skip to contentSkip to conversation

metavariables in added attribute arguments

cocci Kees Cook 2 participants 5 messages Apr 28, 2023 – May 4, 2023

lore
Share & tools
lore

Kees Cook

author
#0 lore 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

Signature · 1 line
Kees Cook
#1 lore raw
Quoted 22 lines · Kees Cook

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);
};
Quoted 2 lines

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.

Quoted 55 lines

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[

(quoted diff) +1 -0
+	COUNTER
	]
(quoted diff) +1 -0
+	__counted_by
	;
Quoted 2 lines

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

Quoted 8 lines

Thanks!

-Kees

--
Kees Cook

#2 lore raw
In reply to Kees Cook
Quoted 79 lines · Kees Cook

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[

(quoted diff) +1 -0
+	COUNTER
	]
(quoted diff) +1 -0
+	__counted_by
	;
Quoted 2 lines

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

#4 lore raw
In reply to Kees Cook
Quoted 41 lines · Kees Cook
Quoted 34 lines · Julia Lawall
Quoted 34 lines

[...]
@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[

(quoted diff) +1 -0
+	COUNTER
	]
(quoted diff) +1 -0
+	__counted_by
	;
Quoted 5 lines
Quoted 2 lines

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.

Quoted 4 lines

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

author
#3 lore raw
In reply to Julia Lawall
Quoted 34 lines · Julia Lawall
Quoted 34 lines

[...]
@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[

(quoted diff) +1 -0
+	COUNTER
	]
(quoted diff) +1 -0
+	__counted_by
	;
Quoted 5 lines
Quoted 2 lines

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(-)
Signature · 1 line
Kees Cook