Skip to contentSkip to conversation

Coccinelle Constification

DISCUSSION Elizabeth Ferdman 3 participants 5 messages Oct 16, 2016

lore
Share & tools
lore
#0 lore raw

I'd like to try doing constification using Coccinelle but I'm having trouble starting on this task. Bhumika Goyal has submitted lots of these and I've looked at the scripts she uses but I'm very confused about the process.

Do you identify the structs that
need to be const first, then write the cocci script? If not, then what is a basic script to use to find structs that need to be const? I can't find any specific documentation that pertains to constification and coccinelle.

Thanks,
Liz

#1 lore raw

Hello Liz,

I'm a fellow applicant so I might be wrong on some things but let me share my experience so far with coccinelle usage.

Quoted 11 lines · Elizabeth Ferdman

I'd like to try doing constification using Coccinelle but I'm having trouble starting on this task. Bhumika Goyal has submitted lots of these and I've looked at the scripts she uses but I'm very confused about the process.

Do you identify the structs that
need to be const first, then write the cocci script? If not, then what is a basic script to use to find structs that need to be const? I can't find any specific documentation that pertains to constification and coccinelle.

It depends on your approach. Sure you can manually identify by code tracing if the struct qualifies to be made const then make a script to write the changes esp. if there are lots of places to be changed.

You can also define a set of properties/situations where declaring the structure const is justified like this:
https://git.kernel.org/cgit/linux/kernel/git/gregkh/staging.git/commit/drivers/staging?h=staging-testing&id=1c099ed63f8363228a0b075a25511c9feb90e03f

and from there write your script so that it'll detect instances where the criteria you previously defined is met. Now that covers detection, for making the changes I suggest you write another one to cater specifically to a specific driver and also include the transformation that needs to be done.

About the basic script, I suggest you try to match any struct declaration first using the disable optional_qualifier isomorphism stated on the project's small task 1. Start small then build your way up.

Once you get your basic script running, coordinate the rest with Julia. Bhumika's patches are great reference, just observe the 'criteria' at which structures can be deemed 'const'.

I hope I clear some of your confusion,
Eva

Quoted 9 lines

Thanks,
Liz

Signature · 5 lines
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20161016022145.GA2215%40localhost.
For more options, visit https://groups.google.com/d/optout.
#3 lore raw
In reply to Elizabeth Ferdman

Hey Liz,

Eva has already explained the process well but I would like to share my approach :

1. Firstly, I find all structures of the form static struct s i ={...}; by using the script:
@r disable optional_qualifier@
identifier s,i;
@@
* static struct s i ={...};
disable optional_qualifier is used here as we want to look only for the non-const structures.

2. Now pick a structure instance from a file and look how it is used in that file.
If the structure's fields are not getting modified => it can be declared as const.
You will have to analyze the usage carefully so that nothing is missed. Also, you can do a grep to be sure that all the referenced are covered.

3. After this you can write a Coccinelle script to constify all the instances that follows this particular pattern of usage.

Make a rule in the script which matches the structure's reference that is justified for const declaration and stores the reference position. Look for the @ok@ rule in my scripts.
Now make another rule which looks for any other reference other than the above one and stores this position as well.
Look for @bad@ rule for this step.
Next, add another rule @depends on !bad disable optional_qualifier@ that declares the structure as const.

4. After doing the changes compile the affected drivers and run size <affected_file_name> to get the size details of the affected file.

I hope this helps :)

Thanks,
Bhumika

#2 lore raw
In reply to Eva Rachel Retuya
Quoted 18 lines · Eva Rachel Retuya

Hello Liz,

I'm a fellow applicant so I might be wrong on some things but let me share my experience so far with coccinelle usage.

Quoted 11 lines · Elizabeth Ferdman

I'd like to try doing constification using Coccinelle but I'm having trouble starting on this task. Bhumika Goyal has submitted lots of these and I've looked at the scripts she uses but I'm very confused about the process.

Do you identify the structs that
need to be const first, then write the cocci script? If not, then what is a basic script to use to find structs that need to be const? I can't find any specific documentation that pertains to constification and coccinelle.

Quoted 8 lines

It depends on your approach. Sure you can manually identify by code tracing if the struct qualifies to be made const then make a script to write the changes esp. if there are lots of places to be changed.

You can also define a set of properties/situations where declaring the structure const is justified like this:
https://git.kernel.org/cgit/linux/kernel/git/gregkh/staging.git/commit/drivers/staging?h=staging-testing&id=1c099ed63f8363228a0b075a25511c9feb90e03f

Hey Eva,

Thanks so much for your reply and thanks for this link. That criteria is very helpful esp because I'm a beginner in C and that clears up more of what was going on in the long cocci script. It's a good idea to search in greg's staging tree-- I had just been searching inside the outreachy mailing list for the most part.

Liz

Quoted 27 lines

and from there write your script so that it'll detect instances where the criteria you previously defined is met. Now that covers detection, for making the changes I suggest you write another one to cater specifically to a specific driver and also include the transformation that needs to be done.

About the basic script, I suggest you try to match any struct declaration first using the disable optional_qualifier isomorphism stated on the project's small task 1. Start small then build your way up.

Once you get your basic script running, coordinate the rest with Julia. Bhumika's patches are great reference, just observe the 'criteria' at which structures can be deemed 'const'.

I hope I clear some of your confusion,
Eva

Quoted 9 lines

Thanks,
Liz

Signature · 5 lines
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20161016022145.GA2215%40localhost.
For more options, visit https://groups.google.com/d/optout.
#4 lore raw
In reply to Bhumika Goyal
Quoted 34 lines · Bhumika Goyal

Hey Liz,

Eva has already explained the process well but I would like to share my approach :

1. Firstly, I find all structures of the form static struct s i ={...}; by using the script:
@r disable optional_qualifier@
identifier s,i;
@@
* static struct s i ={...};
disable optional_qualifier is used here as we want to look only for the non-const structures.

2. Now pick a structure instance from a file and look how it is used in that file.
If the structure's fields are not getting modified => it can be declared as const.
You will have to analyze the usage carefully so that nothing is missed. Also, you can do a grep to be sure that all the referenced are covered.

3. After this you can write a Coccinelle script to constify all the instances that follows this particular pattern of usage.

Make a rule in the script which matches the structure's reference that is justified for const declaration and stores the reference position. Look for the @ok@ rule in my scripts.
Now make another rule which looks for any other reference other than the above one and stores this position as well.
Look for @bad@ rule for this step.
Next, add another rule @depends on !bad disable optional_qualifier@ that declares the structure as const.

Quoted 4 lines

4. After doing the changes compile the affected drivers and run size <affected_file_name> to get the size details of the affected file.

Hey Bhumika,
Thanks for your reply and all the advice. I understand a little bit more about what's going on in those scripts now and I will give it a try...

Liz

Quoted 11 lines

I hope this helps :)

Thanks,
Bhumika

Signature · 5 lines
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/d11cf10a-059c-48ae-a33f-7950190831f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.