Skip to contentSkip to conversation

Whitelist function in rule

Cocci Paul Chaignon 2 participants 5 messages Jun 2, 2020

lore
Share & tools
lore
#0 lore raw

Hi all,

I'm trying to find calls to ep_tail_call() that are *not* followed by 'return DROP_MISSED_TAIL_CALL'. That works fine (rule1 below), but I then want to whitelist one specific function, send_drop_notify().

I couldn't find a way to do that from the first rule, so I whitelisted send_drop_notify() with a second rule (rule2 below). That seems rather cumbersome and I'm wondering if there's maybe a simpler approach?

@rule1@
position p1;
@@

  (
    ep_tail_call(...);
    ... when forall
    return DROP_MISSED_TAIL_CALL;
  |
    ep_tail_call@p1(...);
  )
 
  @rule2@
  position p2, rule1.p1;
  identifier fn != send_drop_notify;
  @@
 
  fn(...) {
  <+...
  ep_tail_call@p1@p2(...);
  ...+>
  }

When I tried using rule1 as the body of the function in rule2, it just didn't match anything anymore.

Thanks,
Paul
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 6 lines · Paul Chaignon

Hi all,

I'm trying to find calls to ep_tail_call() that are *not* followed by 'return DROP_MISSED_TAIL_CALL'. That works fine (rule1 below), but I then want to whitelist one specific function, send_drop_notify().

Quoted 6 lines

I couldn't find a way to do that from the first rule, so I whitelisted send_drop_notify() with a second rule (rule2 below). That seems rather cumbersome and I'm wondering if there's maybe a simpler approach?

@rule1@
position p1;

In coccinelle/tests/python_poscon.cocci you have an example of how to attach python code to a metavariable:

@initialize:python@
@@

def past_line_4(p, other):

    return int(p[0].line) > 4

@r@
expression e;
@@

f(e)

@@
position p : script:python(r.e) { past_line_4(p, e) }; expression r.e;
@@

g(
-e@p
+27
)

In your case, I think that in the python code you should be able to access p1[0].current_element. You can check that that is different than the name of the function that you want to ignore. There are some limitations on what can be put in the {} in such a metavariable declaration, so if you have problems you can make a function, as shown in the example. That code should return true if you want the match to take place and false otherwise.

julia

Quoted 24 lines

@@

  (
    ep_tail_call(...);
    ... when forall
    return DROP_MISSED_TAIL_CALL;
  |
    ep_tail_call@p1(...);
  )

  @rule2@
  position p2, rule1.p1;
  identifier fn != send_drop_notify;
  @@

  fn(...) {
  <+...
  ep_tail_call@p1@p2(...);
  ...+>
  }

When I tried using rule1 as the body of the function in rule2, it just didn't match anything anymore.

Quoted 7 lines

Thanks,
Paul
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw
Quoted 42 lines · Julia Lawall
Quoted 6 lines · Paul Chaignon

Hi all,

I'm trying to find calls to ep_tail_call() that are *not* followed by 'return DROP_MISSED_TAIL_CALL'. That works fine (rule1 below), but I then want to whitelist one specific function, send_drop_notify().

Quoted 6 lines

I couldn't find a way to do that from the first rule, so I whitelisted send_drop_notify() with a second rule (rule2 below). That seems rather cumbersome and I'm wondering if there's maybe a simpler approach?

@rule1@
position p1;

In coccinelle/tests/python_poscon.cocci you have an example of how to attach python code to a metavariable:

@initialize:python@
@@

def past_line_4(p, other):

    return int(p[0].line) > 4

@r@
expression e;
@@

f(e)

@@
position p : script:python(r.e) { past_line_4(p, e) }; expression r.e;
@@

g(
-e@p
+27
)

Quoted 7 lines

In your case, I think that in the python code you should be able to access p1[0].current_element. You can check that that is different than the name of the function that you want to ignore. There are some limitations on what can be put in the {} in such a metavariable declaration, so if you have problems you can make a function, as shown in the example. That code should return true if you want the match to take place and false otherwise.

Thanks! That worked great!
And the Python filtering looks super useful; I'm likely going to be using that a lot :-)

Paul

Quoted 36 lines

julia

Quoted 31 lines

@@

  (
    ep_tail_call(...);
    ... when forall
    return DROP_MISSED_TAIL_CALL;
  |
    ep_tail_call@p1(...);
  )

  @rule2@
  position p2, rule1.p1;
  identifier fn != send_drop_notify;
  @@

  fn(...) {
  <+...
  ep_tail_call@p1@p2(...);
  ...+>
  }

When I tried using rule1 as the body of the function in rule2, it just didn't match anything anymore.

Thanks,
Paul
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
Quoted 51 lines · Paul Chaignon
Quoted 42 lines · Julia Lawall
Quoted 6 lines · Paul Chaignon

Hi all,

I'm trying to find calls to ep_tail_call() that are *not* followed by 'return DROP_MISSED_TAIL_CALL'. That works fine (rule1 below), but I then want to whitelist one specific function, send_drop_notify().

Quoted 6 lines

I couldn't find a way to do that from the first rule, so I whitelisted send_drop_notify() with a second rule (rule2 below). That seems rather cumbersome and I'm wondering if there's maybe a simpler approach?

@rule1@
position p1;

In coccinelle/tests/python_poscon.cocci you have an example of how to attach python code to a metavariable:

@initialize:python@
@@

def past_line_4(p, other):

    return int(p[0].line) > 4

@r@
expression e;
@@

f(e)

@@
position p : script:python(r.e) { past_line_4(p, e) }; expression r.e;
@@

g(
-e@p
+27
)

Quoted 7 lines

In your case, I think that in the python code you should be able to access p1[0].current_element. You can check that that is different than the name of the function that you want to ignore. There are some limitations on what can be put in the {} in such a metavariable declaration, so if you have problems you can make a function, as shown in the example. That code should return true if you want the match to take place and false otherwise.

Quoted 3 lines

Thanks! That worked great!
And the Python filtering looks super useful; I'm likely going to be using that a lot :-)

You can refer to other metavariables, as illustrated by the example in tests, but they have to be nearby, ie no ... between them, and there should be no disjunctions involved. This is to ensure that the matchig that led to th metavariable binding is deterministic. If you violate these constraints, Coccinelle will complain, and you will have to make a separate python rule in the normal way.

julia

Quoted 40 lines

Paul

Quoted 36 lines

julia

Quoted 31 lines

@@

  (
    ep_tail_call(...);
    ... when forall
    return DROP_MISSED_TAIL_CALL;
  |
    ep_tail_call@p1(...);
  )

  @rule2@
  position p2, rule1.p1;
  identifier fn != send_drop_notify;
  @@

  fn(...) {
  <+...
  ep_tail_call@p1@p2(...);
  ...+>
  }

When I tried using rule1 as the body of the function in rule2, it just didn't match anything anymore.

Thanks,
Paul
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw
Quoted 59 lines · Julia Lawall
Quoted 51 lines · Paul Chaignon
Quoted 42 lines · Julia Lawall
Quoted 6 lines · Paul Chaignon

Hi all,

I'm trying to find calls to ep_tail_call() that are *not* followed by 'return DROP_MISSED_TAIL_CALL'. That works fine (rule1 below), but I then want to whitelist one specific function, send_drop_notify().

Quoted 6 lines

I couldn't find a way to do that from the first rule, so I whitelisted send_drop_notify() with a second rule (rule2 below). That seems rather cumbersome and I'm wondering if there's maybe a simpler approach?

@rule1@
position p1;

In coccinelle/tests/python_poscon.cocci you have an example of how to attach python code to a metavariable:

@initialize:python@
@@

def past_line_4(p, other):

    return int(p[0].line) > 4

@r@
expression e;
@@

f(e)

@@
position p : script:python(r.e) { past_line_4(p, e) }; expression r.e;
@@

g(
-e@p
+27
)

Quoted 7 lines

In your case, I think that in the python code you should be able to access p1[0].current_element. You can check that that is different than the name of the function that you want to ignore. There are some limitations on what can be put in the {} in such a metavariable declaration, so if you have problems you can make a function, as shown in the example. That code should return true if you want the match to take place and false otherwise.

Quoted 3 lines

Thanks! That worked great!
And the Python filtering looks super useful; I'm likely going to be using that a lot :-)

Quoted 6 lines

You can refer to other metavariables, as illustrated by the example in tests, but they have to be nearby, ie no ... between them, and there should be no disjunctions involved. This is to ensure that the matchig that led to th metavariable binding is deterministic. If you violate these constraints, Coccinelle will complain, and you will have to make a separate python rule in the normal way.

I see. I often need to whitelist simple cases like the above, so definitely useful despite limitations.

Thanks again for your help!

Paul

Quoted 44 lines

julia

Quoted 40 lines

Paul

Quoted 36 lines

julia

Quoted 31 lines

@@

  (
    ep_tail_call(...);
    ... when forall
    return DROP_MISSED_TAIL_CALL;
  |
    ep_tail_call@p1(...);
  )

  @rule2@
  position p2, rule1.p1;
  identifier fn != send_drop_notify;
  @@

  fn(...) {
  <+...
  ep_tail_call@p1@p2(...);
  ...+>
  }

When I tried using rule1 as the body of the function in rule2, it just didn't match anything anymore.

Thanks,
Paul
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci