Creating function definitions from function pointers in a struct with coccinelle
Hello,
I am new to coccinelle and am interested in creating a patch which can define functions based on the function pointers available in a struct declaration. Since the names of the pointers don't exactly match the
function definitions I want, I used a python script to modify the names. Below is my script:
@m_rule@
field list f_list;
identifier I;
@@
struct I {f_list
};
@script:python p_rule@
nf << m_rule.f_list;n_list;
@@new_nf = []
for f in nf:
parts = f.split("(")r_type = parts[0]
if '*' in r_type:
r_type=r_type.strip()
else:r_type = r_type.lstrip()f_name = parts[1].split(")")[0].replace("*", "").strip() params = parts[2].split(")")[0] new_nf.append(r_type + "pv_" + f_name + "(" + params + ")") new_nf.append(r_type + "hvm_" + f_name + "(" + params + ")")
Please how can I write a new rule that defines several lines of function definitions from the p_rule new_nf list available in my python structure?
I tried adding the list in a plus(+) entry but that does not work.
Thank you in advance for your help.
Caleb
Quoted 6 lines · Fonyuy-Asheri Caleb
Hello,
I am new to coccinelle and am interested in creating a patch which can define functions based on the function pointers available in a struct declaration. Since the names of the pointers don't exactly match the
function definitions I want, I used a python script to modify the names. Below is my script:
Quoted 7 lines
@m_rule@
field listf_list;
identifier I;
@@
struct I {
f_list
};
I'm not 100% sure if this work, but it could be nicer to match the declaration of a function pointer, ie
struct I {
...
T (*I1)(...);
...
}
Also, it is better to omit the ; at the end, because a structure type could be declared in a variable declaration, where there is no ; immediately after it.
Quoted 19 lines
@script:python p_rule@
nf << m_rule.f_list;n_list;
@@new_nf= []
for f in nf:
parts = f.split("(")
r_type= parts[0]
if '*' inr_type:
r_type=r_type.strip()
else:
r_type=r_type.lstrip()
f_name= parts[1].split(")")[0].replace("*", "").strip() params = parts[2].split(")")[0]new_nf.append(r_type + "pv_" + f_name + "(" + params + ")")new_nf.append(r_type + "hvm_" + f_name + "(" + params + ")")
Quoted 4 lines
Please how can I write a new rule that defines several lines of function definitions from the
p_rulenew_nflist available in my python structure?I tried adding the list in a plus(+) entry but that does not work.
I'm not really sure what you are trying to do here. Maybe if you could give an example of source code an the results you want to achieve that would be more understandable.
An alternative is not to bother with the type declarations, but instead to see how some fields are used. A function pointer field is a field that has a function name stored in it. So you could do:
@fname@
identifier fn;
@@
fn(...) { ... }
@@
type T;
T x;
identifier id;
identifier fname.fn;
@@
x.id = fn
This might be less complete than what you propose.
julia
Quoted 7 lines
Thank you in advance for your help.
Caleb
Thank you for your prompt response
Quoted 4 lines
I'm not really sure what you are trying to do here. Maybe if you could give an example of source code an the results you want to achieve that would be more understandable.
I have structures defined as such in the code base but this is a problem for the work I want to do.
struct save_ops {
void (*start_copy)(void *data, const char *filename);
void *(*copy_finished)(const char *filename);
void (*check_copied)(void *data);
};The aim of my patch is to define the following functions from the above struct
void hvm_start_copy(void *data, const char *filename); void *hvm_copy_finished(const char *filename); void hvm_check_copied(void *data);
void pv_start_copy(void *data, const char *filename); void *pv_copy_finished(const char *filename); void pv_check_copied(void *data);
The python m_rule I have permits me to match the list of function pointers and the python script permits me to rename the pointers to the function names as I have them here. Having at least the pv functions will be good for me.
The issue now is to be able to write these to the resulting patch.
Caleb
Quoted 27 lines · Fonyuy-Asheri Caleb
Thank you for your prompt response
Quoted 4 lines
I'm not really sure what you are trying to do here. Maybe if you could give an example of source code an the results you want to achieve that would be more understandable.
I have structures defined as such in the code base but this is a problem for the work I want to do.
struct save_ops { void (*start_copy)(void *data, const char *filename); void *(*copy_finished)(const char *filename); void (*check_copied)(void *data); };The aim of my patch is to define the following functions from the above struct
void
hvm_start_copy(void *data, const char *filename); void *hvm_copy_finished(const char *filename); voidhvm_check_copied(void *data);void
pv_start_copy(void *data, const char *filename); void *pv_copy_finished(const char *filename); voidpv_check_copied(void *data);
Quoted 5 lines
The python
m_ruleI have permits me to match the list of function pointers and the python script permits me to rename the pointers to the function names as I have them here. Having at least the pv functions will be good for me.The issue now is to be able to write these to the resulting patch.
OK, maybe:
@@
identifier I, fn;
parameter list ps;
fresh identifier fnhvm = "hvm_" ## fn;
fresh identifier fnpv = "pv_" ## fn;
@@
struct I {
...
T (*fn)(ps);
...
};
++ void fnhvm(ps);
++ void fnpv(ps);
If that doesn't work, maybe have one rule to add the hvm functions and another to add the pv functions. That would have the side benefit that the hvm functions would come out together and the pv functions would come out together. Both groups would be attached right after the struct declaration, so you would want to do the pv functions first and then the hvm functions.
julia
Thanks a lot Julia.
It works now.
Caleb
----- Original Message -----
Quoted 66 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:32:15 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 27 lines · Fonyuy-Asheri Caleb
Thank you for your prompt response
Quoted 4 lines
I'm not really sure what you are trying to do here. Maybe if you could give an example of source code an the results you want to achieve that would be more understandable.
I have structures defined as such in the code base but this is a problem for the work I want to do.
struct save_ops { void (*start_copy)(void *data, const char *filename); void *(*copy_finished)(const char *filename); void (*check_copied)(void *data); };The aim of my patch is to define the following functions from the above struct
void
hvm_start_copy(void *data, const char *filename); void *hvm_copy_finished(const char *filename); voidhvm_check_copied(void *data);void
pv_start_copy(void *data, const char *filename); void *pv_copy_finished(const char *filename); voidpv_check_copied(void *data);Quoted 5 lines
The python
m_ruleI have permits me to match the list of function pointers and the python script permits me to rename the pointers to the function names as I have them here. Having at least the pv functions will be good for me.The issue now is to be able to write these to the resulting patch.
OK, maybe:
@@
identifier I, fn;
parameter list ps;
fresh identifier fnhvm = "hvm_" ## fn;
fresh identifier fnpv = "pv_" ## fn;
@@struct I {
...
T (*fn)(ps);
...
};
++ void fnhvm(ps);
++ void fnpv(ps);If that doesn't work, maybe have one rule to add the hvm functions and another to add the pv functions. That would have the side benefit that the hvm functions would come out together and the pv functions would come out together. Both groups would be attached right after the struct declaration, so you would want to do the pv functions first and then the hvm functions.
julia
Quoted 3 lines · Fonyuy-Asheri Caleb
Thanks a lot Julia.
It works now.
Excellent :)
julia
Quoted 72 lines
Caleb
----- Original Message -----
Quoted 66 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:32:15 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 27 lines · Fonyuy-Asheri Caleb
Thank you for your prompt response
Quoted 4 lines
I'm not really sure what you are trying to do here. Maybe if you could give an example of source code an the results you want to achieve that would be more understandable.
I have structures defined as such in the code base but this is a problem for the work I want to do.
struct save_ops { void (*start_copy)(void *data, const char *filename); void *(*copy_finished)(const char *filename); void (*check_copied)(void *data); };The aim of my patch is to define the following functions from the above struct
void
hvm_start_copy(void *data, const char *filename); void *hvm_copy_finished(const char *filename); voidhvm_check_copied(void *data);void
pv_start_copy(void *data, const char *filename); void *pv_copy_finished(const char *filename); voidpv_check_copied(void *data);Quoted 5 lines
The python
m_ruleI have permits me to match the list of function pointers and the python script permits me to rename the pointers to the function names as I have them here. Having at least the pv functions will be good for me.The issue now is to be able to write these to the resulting patch.
OK, maybe:
@@
identifier I, fn;
parameter list ps;
fresh identifier fnhvm = "hvm_" ## fn;
fresh identifier fnpv = "pv_" ## fn;
@@struct I {
...
T (*fn)(ps);
...
};
++ void fnhvm(ps);
++ void fnpv(ps);If that doesn't work, maybe have one rule to add the hvm functions and another to add the pv functions. That would have the side benefit that the hvm functions would come out together and the pv functions would come out together. Both groups would be attached right after the struct declaration, so you would want to do the pv functions first and then the hvm functions.
julia
Follow up question please:
How can i match function pointer calls such as
ctx->ops.my_function(int id, struct data_struct data);
or
ctx->my_function(int id, struct data_struct data);
I can't seem to get this working.
Caleb
----- Original Message -----
Quoted 16 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:56:20 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 3 lines · Fonyuy-Asheri Caleb
Thanks a lot Julia.
It works now.
Excellent :)
julia
Quoted 5 lines · Fonyuy-Asheri Caleb
Follow up question please:
How can i match function pointer calls such as
ctx->ops.my_function(int id, struct data_struct data);
When you call a function, you don't put the types on the arguments. If you want to specify the types, you can make metavariables of a certain type like:
@@
int id;
struct data_struct data;
@@
julia
Quoted 28 lines
or
ctx->my_function(int id, struct data_struct data);I can't seem to get this working.
Caleb
----- Original Message -----
Quoted 16 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:56:20 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 3 lines · Fonyuy-Asheri Caleb
Thanks a lot Julia.
It works now.
Excellent :)
julia
That was a mistake. Sorry about that.
I actually meant thisctx->ops.my_function(id, data);
ctx->my_function(id, data);
with the intention of picking up my_function and the parameter list.
Caleb
Quoted 46 lines · Fonyuy-Asheri Caleb
Quoted 6 lines
Follow up question please:
How can i match function pointer calls such as
ctx->ops.my_function(int id, struct data_struct data);When you call a function, you don't put the types on the arguments. If you want to specify the types, you can make metavariables of a certain type like:
@@
int id;
structdata_structdata;
@@julia
Quoted 28 lines
or
ctx->my_function(int id, struct data_struct data);I can't seem to get this working.
Caleb
----- Original Message -----
Quoted 17 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:56:20 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 3 lines · Fonyuy-Asheri Caleb
Thanks a lot Julia.
It works now.
Excellent :)
julia
Quoted 8 lines · Fonyuy-Asheri Caleb
That was a mistake. Sorry about that.
I actually meant this
ctx->ops.my_function(id, data);
ctx->my_function(id, data);with the intention of picking up
my_functionand the parameter list.
Sorry, it's not clear to me what you want. If you want the complete list of arguments and don't know how many there are, you may want to use:
expression list es;
julia
Quoted 51 lines
Caleb
Quoted 46 lines · Fonyuy-Asheri Caleb
Quoted 6 lines
Follow up question please:
How can i match function pointer calls such as
ctx->ops.my_function(int id, struct data_struct data);When you call a function, you don't put the types on the arguments. If you want to specify the types, you can make metavariables of a certain type like:
@@
int id;
structdata_structdata;
@@julia
Quoted 28 lines
or
ctx->my_function(int id, struct data_struct data);I can't seem to get this working.
Caleb
----- Original Message -----
Quoted 17 lines
From: "Julia Lawall" <[email protected]>
To: "Fonyuy-Asheri Caleb" <[email protected]>
Cc: "Julia Lawall" <[email protected]>, "cocci" <[email protected]>
Sent: Monday, February 10, 2025 12:56:20 PM
Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelleQuoted 3 lines · Fonyuy-Asheri Caleb
Thanks a lot Julia.
It works now.
Excellent :)
julia
Quoted 6 lines
I actually meant this
ctx->ops.my_function(id, data);
ctx->my_function(id, data);with the intention of picking up
my_functionand the parameter list.
You can choose metavariables as needed for function calls.
Examples:
expression ctx;
expression list el;
identifier ops, my_function;
long id;
struct my_info * data;
Regards,
Markus