Skip to contentSkip to conversation

how to check a for "don't care" state of a variable?

cocci Kees Cook 4 participants 15 messages Oct 8, 2022 – Oct 10, 2022

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

Hi!

I was looking at doing some conversions for this[1], and I wasn't sure how to check for removing a variable assignment if there was either no more uses or the next use was an assignment itself.

i.e.:

	var = 1;
	other = func(var);

turns into:

	other = func(1);

but this would not:

	var = 1;
	other = func(var);
	report(var);

without getting confused by:

	var = 1;
	other = func(var);
	var = 5;

@multi_line@
identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; identifier RAND;
expression E;
type TYPE;
@@

(

-       RAND = randfunc();
        ... when != RAND
-       RAND %= (E);
+       RAND = prandom_u32_max(E);
|
-       RAND = randfunc();
        ... when != RAND

- ((TYPE)RAND % (E))
+ prandom_u32_max(E)
// This is wrong: we need to check that RAND is either assigned or never // accessed again
)

I thought I could do this:

	... when != RAND
	<... RAND = EXP; ...>

But this appears to be invalid syntax...

-Kees

[1] https://lore.kernel.org/lkml/202210071241.445289C5@keescook/

Signature · 1 line
Kees Cook
#1 lore raw
Quoted 21 lines · Kees Cook

Hi!

I was looking at doing some conversions for this[1], and I wasn't sure how to check for removing a variable assignment if there was either no more uses or the next use was an assignment itself.

i.e.:

	var = 1;
	other = func(var);

turns into:

	other = func(1);

but this would not:

	var = 1;
	other = func(var);
	report(var);
Quoted 5 lines

without getting confused by:

	var = 1;
	other = func(var);
	var = 5;

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

julia

Quoted 39 lines

@multi_line@
identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; identifier RAND;
expression E;
type TYPE;
@@

(

(quoted diff) +1 -2
-       RAND = randfunc();
        ... when != RAND
-       RAND %= (E);
+       RAND = prandom_u32_max(E);

|

(quoted diff) +1 -2
-       RAND = randfunc();
        ... when != RAND
-       ((TYPE)RAND % (E))
+       prandom_u32_max(E)

// This is wrong: we need to check that RAND is either assigned or never // accessed again
)

I thought I could do this:

	... when != RAND
	<... RAND = EXP; ...>

But this appears to be invalid syntax...

-Kees

[1] https://lore.kernel.org/lkml/202210071241.445289C5@keescook/

--
Kees Cook

#2 lore raw
Quoted 21 lines
Quoted 21 lines

I was looking at doing some conversions for this[1], and I wasn't sure how to check for removing a variable assignment if there was either no more uses or the next use was an assignment itself.

i.e.:

	var = 1;
	other = func(var);

turns into:

	other = func(1);

but this would not:

	var = 1;
	other = func(var);
	report(var);

without getting confused by:

Quoted 8 lines
Quoted 3 lines
	var = 1;
	other = func(var);
	var = 5;

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found,

Does such an SmPL code indicate a need for a double check of the affected C source code?

Quoted 1 line

but won't require the presence of that thing for the match to be successful.

Can such an advice trigger further improvements for the software documentation?

Regards,
Markus

#3 lore raw
Quoted 24 lines · Markus Elfring
Quoted 24 lines
Quoted 24 lines

I was looking at doing some conversions for this[1], and I wasn't sure how to check for removing a variable assignment if there was either no more uses or the next use was an assignment itself.

i.e.:

	var = 1;
	other = func(var);

turns into:

	other = func(1);

but this would not:

	var = 1;
	other = func(var);
	report(var);

without getting confused by:

	var = 1;
	other = func(var);
	var = 5;
Quoted 8 lines
Quoted 5 lines

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found,

Does such an SmPL code indicate a need for a double check of the affected C source code?

No idea what the question means. But there should not be any extra overhead for using ?

julia

Quoted 9 lines
Quoted 3 lines

but won't require the presence of that thing for the match to be successful.

Can such an advice trigger further improvements for the software documentation?

Regards,
Markus

#4 lore raw
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found,

Does such an SmPL code indicate a need for a double check of the affected C source code?

No idea what the question means.

I guess that another wording variant will help.

Quoted 1 line

But there should not be any extra overhead for using ?

I imagine that the handling of optional items will trigger special run time consequences according to such data processing.
The impact can become clearer, can't it?

Regards,
Markus

#5 lore raw
In reply to Julia Lawall
Quoted 5 lines

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found,

You suggested to delete an assignment statement by a single SmPL rule. I would miss the replacement then for the assignment target in a macro/function call parameter.

Quoted 1 line

but won't require the presence of that thing for the match to be successful.

Thus I propose to reconsider the transformation approach in more detail.

I would expect that it needs to be determined if the assignment target would be used at other places within a code block before a code modification can be applied by a subsequent SmPL rule.
Will the algorithm require any additional case distinctions accordingly?

Regards,
Markus

author
#6 lore raw
In reply to Julia Lawall
Quoted 6 lines · Julia Lawall

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

Thank you! This worked perfectly. :)

I'm starting to realize I should document my set of "with cocci, how do I ...?" answers somewhere.

Signature · 1 line
Kees Cook
#7 lore raw
Quoted 5 lines · Kees Cook
Quoted 4 lines · Julia Lawall

- x = e;
... when != x
? x = e';

Quoted 7 lines
Quoted 2 lines

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

Thank you! This worked perfectly. :)

I'm starting to realize I should document my set of "with cocci, how do I ...?" answers somewhere.

Sure. I can add a link from the Coccinelle web page when you have something.

julia

#8 lore raw
In reply to Kees Cook
Quoted 7 lines
Quoted 6 lines

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

Thank you! This worked perfectly. :)

How can the shown SmPL code work according to your application requirements when no code replacement was specified (besides a deletion) here?

Do you care for any corresponding case distinctions of a safe transformation algorithm?

Regards,
Markus

author
#9 lore raw
In reply to Kees Cook
Quoted 5 lines · Kees Cook
Quoted 4 lines · Julia Lawall

- x = e;
... when != x
? x = e';

Quoted 7 lines
Quoted 2 lines

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

Thank you! This worked perfectly. :)

I'm starting to realize I should document my set of "with cocci, how do I ...?" answers somewhere.

Okay, initial dump is here:

https://github.com/kees/kernel-tools/tree/trunk/coccinelle

And I've sent a PR for some missing docs in Coccinelle itself:

https://github.com/coccinelle/coccinelle/pull/290

(BTW, it looks like there are some other github PRs that can be closed?)

Signature · 1 line
Kees Cook
#10 lore raw
Quoted 9 lines · Kees Cook
Quoted 8 lines · Kees Cook
Quoted 6 lines · Julia Lawall

- x = e;
... when != x
? x = e';

The ? will stop the match if the thing is found, but won't require the presence of that thing for the match to be successful.

Quoted 8 lines
Quoted 4 lines

Thank you! This worked perfectly. :)

I'm starting to realize I should document my set of "with cocci, how do I ...?" answers somewhere.

Okay, initial dump is here:

https://github.com/kees/kernel-tools/tree/trunk/coccinelle

Thanks. I have added a link from the documentation page.

Quoted 3 lines

And I've sent a PR for some missing docs in Coccinelle itself:

https://github.com/coccinelle/coccinelle/pull/290

Could you send it by email please?

thanks,
julia

Quoted 6 lines

(BTW, it looks like there are some other github PRs that can be closed?)

--
Kees Cook

author
#11 lore raw
Quoted 1 line · Julia Lawall

Thanks. I have added a link from the documentation page.

Thanks!

Quoted 5 lines
Quoted 3 lines

And I've sent a PR for some missing docs in Coccinelle itself:

https://github.com/coccinelle/coccinelle/pull/290

Could you send it by email please?

Done now. Is there a good place for finding this python documentation? I didn't see anything that really showed how to use it, and have mostly gone off of existing examples.

Signature · 1 line
Kees Cook
#12 lore raw
Quoted 5 lines · Kees Cook
Quoted 1 line · Julia Lawall

Thanks. I have added a link from the documentation page.

Thanks!

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

And I've sent a PR for some missing docs in Coccinelle itself:

https://github.com/coccinelle/coccinelle/pull/290

Could you send it by email please?

Done now.

Thanks

Quoted 3 lines

Is there a good place for finding this python documentation? I didn't see anything that really showed how to use it, and have mostly gone off of existing examples.

I'm not really an expert on what is supported in python. Someone once suggested that reflection could be used to find out what functions are available. Normally, they are the same as are supported for OCaml.

julia

#13 lore raw
Quoted 19 lines · Julia Lawall
Quoted 5 lines · Kees Cook
Quoted 1 line · Julia Lawall

Thanks. I have added a link from the documentation page.

Thanks!

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

And I've sent a PR for some missing docs in Coccinelle itself:

https://github.com/coccinelle/coccinelle/pull/290

Could you send it by email please?

Done now.

Thanks

Quoted 7 lines
Quoted 3 lines

Is there a good place for finding this python documentation? I didn't see anything that really showed how to use it, and have mostly gone off of existing examples.

I'm not really an expert on what is supported in python. Someone once suggested that reflection could be used to find out what functions are available. Normally, they are the same as are supported for OCaml.

The important one I found was cocci.include_match(False).

Jason

#14 lore raw

Re: [cocci] Reconsidering selected SmPL code

In reply to Kees Cook
Quoted 5 lines
Quoted 2 lines

I'm starting to realize I should document my set of "with cocci, how do I ...?" answers somewhere.

Okay, initial dump is here:

https://github.com/kees/kernel-tools/tree/trunk/coccinelle

I suggest to reconsider published implementation details a bit more.
https://github.com/kees/kernel-tools/blob/0d128055b31a7737eaa23c040e57a0f34a41e6c1/coccinelle/README.md#user-content-use-regular-expressions-to-quickly-match-identifiers

@display_with_regex@
identifier func =~ "^(?:get_random_(?:int|u32)|prandom_u32)$"; @@
*func
 ()

@display_with_disjunction@
@@
(
*get_random_int
|
*get_random_u32
|
*prandom_u32
)
 ()

Would you like to compare software run time characteristics in more detail?

https://github.com/kees/kernel-tools/blob/0d128055b31a7737eaa23c040e57a0f34a41e6c1/coccinelle/README.md#user-content-limit-matches-when-variable-contents-arent-used-again

I propose to take additional SmPL script design options into account.

https://github.com/kees/kernel-tools/blob/0d128055b31a7737eaa23c040e57a0f34a41e6c1/coccinelle/README.md#user-content-match-variables-of-a-given-type

Will further collateral evolution happen according to mentioned metavariable declarations?

https://github.com/kees/kernel-tools/blob/246d5fe3c1abf0a25778acc5d3b8af02511241b0/coccinelle/examples/rand-pass2.cocci#L1

I wonder why the source code search pattern “prandom_bytes(...)@p” was not used for the SmPL rule “hit”.

I imagine that the addition of constraints for metavariables with
the type “binary operator” can help to avoid duplicate code in related SmPL rules.

Regards,
Markus