Find expressions of given type
Hello,
I'm trying to match all expressions of a given type. I want to check whether all accesses to any part of some struct are locked.
In the hypothetical case (which doesn't happen), every access to that struct would create a local variable of the wanted type and I could then match it with something like this:
@@
identifier i;
@@
...my_specific_type *var;
...var->i
...
Unfortunately, this structure may be also referenced from another structure, like this:
struct A { int foo; int bar; };
struct B { int foo; struct A *bar };
struct B *b;
...b->bar->foo;
I would like to write something like:
@@
expression struct A* E;
@@
...
+E
...
and match every occurence of not only variables of the given type, but also an expression.
I was first trying to find some hint in the doc, found expression struct *E;
which is almost what I want but still too broad.
Does there exist some way to
A) achieve the behaviour I want by config
or
B) implement it?
If A, could you please give me a hint?
If B, I wanted to implement it but got totally lost in the code. Could you please give me a pointer, where to begin? I'd like to contribute the result then.
Best regards and thanks
MQ
[Cocci] Checking lock usage
Quoted 1 line
I want to check whether all accesses to any part of some struct are locked.
I imagine that you will need to perform data-flow analysis for such a task. I guess that there are some software development challenges to achieve it with the semantic patch language.
Quoted 3 lines
struct B *b;
...b->bar->foo;
Are you eventually concerned about the handling of such pointer access chains?
Quoted 1 line
If B, I wanted to implement it but got totally lost in the code.
Would you like to increase your efforts for the software development with the programming language "OCaml"?
Regards,
Markus
Hello Jan,
I'm not sure to understand your problem, but here is a nice feature I use to get expression types. I think this example doesn't do what you want but I guess it shows how to get types from expressions:
@r1@
expression ep;
identifier f, fld;
type t;
t e;
@@
ep at e.fld = f
@script:python@
ep << r1.ep;
fld << r1.fld;
t << r1.t;
@@
print t, ep, fld
There are two important parts:
@r1@
...
type t;
t e;
@@
which just tells coccinelle that you are interested in e of type t, but you don't want to match the declaration. Then the '@e' on the line:
ep at e.fld = f
makes e match the expression that comes before '.fld', and as the type of that expression is t, you can use t on your rules (to print, import it in other rules, ...).
Hope it helps...
Quoted 62 lines · Jan Moskyto Matejka
Hello,
I'm trying to match all expressions of a given type. I want to check whether all accesses to any part of some struct are locked.
In the hypothetical case (which doesn't happen), every access to that struct would create a local variable of the wanted type and I could then match it with something like this:
@@
identifier i;
@@...
my_specific_type*var;
...var->i
...Unfortunately, this structure may be also referenced from another structure, like this:
struct A { int foo; int bar; };
struct B { int foo; struct A *bar };struct B *b;
...b->bar->foo;I would like to write something like:
@@
expression struct A* E;
@@...
+E
...and match every occurence of not only variables of the given type, but also an expression.
I was first trying to find some hint in the doc, found expression struct *E;
which is almost what I want but still too broad.Does there exist some way to
A) achieve the behaviour I want by config
or
B) implement it?If A, could you please give me a hint?
If B, I wanted to implement it but got totally lost in the code. Could you please give me a pointer, where to begin? I'd like to contribute the result then.Best regards and thanks
MQ
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
Signature · 1 line
Peter
I think Peter gave some good suggestions, so I will just add a few hints.
Quoted 9 lines
I would like to write something like:
@@
expression struct A* E;
@@...
+E
...
If you want to match an expression E, don't put ... ... around it. Just put the expression E itself. The way you have the ...s it means since the beginning of the function (for the first one) and up to the end of the fnuction (for the second one). ... also has a semantics of "shortest path", meaning that there is an implicit constraint that the ... doesn't match E. So you would only get functions that have only one occurrence of E. One way to get around this is to put when any on the ... But in your case, and even easier solution is to just drop them completely.
Quoted 5 lines
and match every occurence of not only variables of the given type, but also an expression.
I was first trying to find some hint in the doc, found expression struct *E;
Yeah, this was intended as a special case, because struct * is not a real type.
Quoted 11 lines
which is almost what I want but still too broad.
Does there exist some way to
A) achieve the behaviour I want by config
or
B) implement it?If A, could you please give me a hint?
If B, I wanted to implement it but got totally lost in the code. Could you please give me a pointer, where to begin? I'd like to contribute the result then.
Not sure what you mean by config, but you can declare a metavariable as
t E;
for any type t. No need for "expression" in this case.
The next problem, though, is where it is going to get the type information from. No problem for local variables, but it can be a problem for structure fields. By default, Coccinelle only includes header files that have th same name as the .c file, ie processing of foo.c will include foo.h. If you need more, you can use the command line option --all-includes, which will include all of the header files mentioned explicitly in the .c file, or even --recursive-includes, which does the same but proceeds recursively. The more header files you include, though, the more parsing work will have to be done, and the more time it will take. So you are better off avoiding --recursive-includes if possible.
julia