Skip to contentSkip to conversation

Replacing struct field references with field references from a different struct

cocci Palani kodeswaran 2 participants 2 messages Aug 8, 2022

lore
Share & tools
lore
#0 lore raw

Hello,

I am trying to replace field references inside a struct of given type with references from a different struct (that is passed as an input parameter for a function). I have the following coccinelle rule that is not working

@vlan@
identifier p,c,fn;
type t;
struct vlan_hdr *v;
@@
t fn(struct xdp_md *ctx){
...
- v->h_vlan_encapsulated_proto
+ ctx->vlan_proto
}

However, I have a similar rule that works for another struct type. I am not sure what I am missing in the above rule compared to the one below. The only difference I can see in the source code is that the above rule needs to transform a piece of code inside an if block. I believe the three ellipsis (...) would handle that anyways?

@rule3@

identifier p,c,fn;
                                                    type t;

struct ethhdr *e;
@@
t fn(struct xdp_md *ctx){
...
- e->h_proto
+ ctx->protocol
...
}

Appreciate your help.

Regards,
Palani.

#1 lore raw
Quoted 5 lines · Palani kodeswaran

Hello,
I am trying to replace field references inside a struct of given type with references from a different struct (that is passed as an input parameter for a function).  I have the following coccinelle rule that is not working

Quoted 10 lines

@vlan@
identifier p,c,fn;
type t;
struct vlan_hdr *v;
@@
t fn(struct xdp_md *ctx){
...
- v->h_vlan_encapsulated_proto
+ ctx->vlan_proto
}

I thik that you want to do the replacement everywhere that it is relevant in the function body. The above rule (sort of) says that it should only be done at the end of the function. So:

@vlan@
identifier p,c,fn;
type t;
struct vlan_hdr *v;
@@
t fn(struct xdp_md *ctx){
<...
- v->h_vlan_encapsulated_proto
+ ctx->vlan_proto
...>
}

I would suggest doing the same for your other rule. The other rule your wrote only works when there is exactly one occurrence of the changed thing on every execution path. This is because ... implicitly checks that there is no occurrence of the thing before or after the ... in the matched region. <... ...> says do the change whereever the matched thing occurs. Including nowhere.

julia

Quoted 25 lines

However, I have a similar rule that works for another struct type.  I am not sure what I am missing in the above rule compared to the one below. The only difference I can see in the source code is that the above rule needs to transform a piece of code inside an if block. I believe the three ellipsis (...) would handle that anyways?

@rule3@
identifier p,c,fn;
                                                    type t; struct ethhdr *e;
@@
t fn(struct xdp_md *ctx){
...
- e->h_proto
+ ctx->protocol
...
}

Appreciate your help.

Regards,
Palani.