Skip to contentSkip to conversation

Determining a return code variable for a jump target (with SmPL)?

cocci Markus Elfring 2 participants 15 messages Sep 21, 2024 – Sep 24, 2024

lore
Share & tools
lore
#0 lore raw

Hello,

I constructed another tiny script variant for the semantic patch language. A corresponding test run generated data which contained some update candidates for further development considerations.
Thus I constructed another small script variant for the semantic patch language.

I would expect then that a patch can be generated with the software combination “Coccinelle 1.2-00038-g2297fc0f”.
Unfortunately, this does not happen according to the attached two files.

How can involved software components be improved?

Regards,
Markus

// See also:
// ath12k_pull_peer_assoc_conf_ev
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L5612
@adjustment2@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+...
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

-   kfree(x);
-   return
+   rc2 =
           -c;
+   goto target
    ;

}
...+>
+rc2 = 0;
+target:
kfree(x);
return
- 0
+ rc2
;

// SPDX-License-Identifier: BSD-3-Clause-Clear

// See also:
// ath12k_pull_peer_assoc_conf_ev
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L5612

// deleted part

static int ath12k_pull_peer_assoc_conf_ev(struct ath12k_base *ab, struct sk_buff *skb,
struct wmi_peer_assoc_conf_arg *peer_assoc_conf)

{
	const void **tb;
	const struct wmi_peer_assoc_conf_event *ev;
	int ret;

	tb = ath12k_wmi_tlv_parse_alloc(ab, skb, GFP_ATOMIC);
	if (IS_ERR(tb)) {
		ret = PTR_ERR(tb);
		ath12k_warn(ab, "failed to parse tlv: %d\n", ret);
		return ret;
	}

	ev = tb[WMI_TAG_PEER_ASSOC_CONF_EVENT];
	if (!ev) {
		ath12k_warn(ab, "failed to fetch peer assoc conf ev");
		kfree(tb);
		return -EPROTO;
	}

	peer_assoc_conf->vdev_id = le32_to_cpu(ev->vdev_id);
	peer_assoc_conf->macaddr = ev->peer_macaddr.addr;

	kfree(tb);
	return 0;
}

// deleted part

#1 lore raw
Quoted 7 lines · Markus Elfring

Hello,

I constructed another tiny script variant for the semantic patch language. A corresponding test run generated data which contained some update candidates for further development considerations.
Thus I constructed another small script variant for the semantic patch language.

Quoted 5 lines

I would expect then that a patch can be generated with the software combination “Coccinelle 1.2-00038-g2297fc0f”.
Unfortunately, this does not happen according to the attached two files.

How can involved software components be improved?

Please put code on which you would like feedback inline in the message so I can comment on it properly.

// See also:
// ath12k_pull_peer_assoc_conf_ev
//
https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L5612
@adjustment2@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+...
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

-   kfree(x);
-   return
+   rc2 =
           -c;
+   goto target
    ;

You seem to expect that the source code contains two semicolons here.

julia

}
...+>
+rc2 = 0;
+target:
kfree(x);
return
- 0
+ rc2
;

Quoted 4 lines

Regards,
Markus

#2 lore raw
Quoted 18 lines

@adjustment2@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+...
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

(quoted diff) +2 -2
-   kfree(x);
-   return
+   rc2 =
           -c;
+   goto target
    ;
Quoted 1 line

You seem to expect that the source code contains two semicolons here.

Thanks for this feedback.

I should have used the following SmPL code at this place.

+ goto target;

I might be “lucky” for some function implementations when they contain only a single integer variable for returning result values.

Regards,
Markus

#3 lore raw
Quoted 3 lines

I might be “lucky” for some function implementations when they contain only a single integer variable for returning result values.

It seems that my source code transformation approach is still too incomplete so far if I would like to be able to improve also a function like ath12k_pull_reg_chan_list_ext_update_ev().
https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683

It contains the following variable declarations.

	int ret, i, j;

SmPL script variant:

// See also:
// ath12k_pull_peer_assoc_conf_ev
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L5612
@adjustment2@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+...
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

-   kfree(x);
-   return
+   rc2 =
           -c;

+ goto target;
}
...+>
+rc2 = 0;
+target:
kfree(x);
return
- 0
+ rc2
;

Test result:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> spatch …/Projekte/Coccinelle/janitor/use_common_kfree_code2.cocci drivers/net/wireless/ath/ath12k/wmi.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
…
SPECIAL NAMES: adding list_for_each_entry as a iterator

previous modification:
MINUS

Quoted 1 line
Quoted 1 line
Quoted 1 line

rc2

According to environment 1:
adjustment2.rc2 -> id ret

current modification:
MINUS

Quoted 1 line
Quoted 1 line
Quoted 1 line

rc2

According to environment 1:
adjustment2.rc2 -> id j

exn while in timeout_function
adjustment2: already tagged token:
C code context
File "drivers/net/wireless/ath/ath12k/wmi.c", line 4975, column 8, charpos = 151798
around = '0',
whole content = return 0;

How will corresponding case distinctions evolve further (with SmPL disjunctions)?

Regards,
Markus

#4 lore raw
Quoted 34 lines · Markus Elfring
Quoted 3 lines

I might be “lucky” for some function implementations when they contain only a single integer variable for returning result values.

It seems that my source code transformation approach is still too incomplete so far if I would like to be able to improve also a function like ath12k_pull_reg_chan_list_ext_update_ev().
https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683

It contains the following variable declarations.

	int ret, i, j;

SmPL script variant:

// See also:
// ath12k_pull_peer_assoc_conf_ev
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L5612
@adjustment2@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+...
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

(quoted diff) +1 -2
-   kfree(x);
-   return
+   rc2 =
           -c;

Review

Quoted 10 lines
(quoted diff) +4 -1
+   goto target;
 }
 ...+>
+rc2 = 0;
+target:
 kfree(x);
 return
-       0
+       rc2
 ;

How do you expect Coccinelle to figure out what is the right value for rc2? The only match is in the declaration, but any int variable declaration satisfies that.

julia

Quoted 40 lines

Test result:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> spatch …/Projekte/Coccinelle/janitor/use_common_kfree_code2.cocci drivers/net/wireless/ath/ath12k/wmi.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
…
SPECIAL NAMES: adding list_for_each_entry as a iterator

previous modification:
MINUS

Quoted 1 line
Quoted 1 line
Quoted 1 line

rc2

According to environment 1:

   adjustment2.rc2 -> id ret

current modification:
MINUS

Quoted 1 line
Quoted 1 line
Quoted 1 line

rc2

According to environment 1:

   adjustment2.rc2 -> id j

exn while in timeout_function
adjustment2: already tagged token:
C code context
File "drivers/net/wireless/ath/ath12k/wmi.c", line 4975, column 8, charpos = 151798
around = '0',
whole content = return 0;

How will corresponding case distinctions evolve further (with SmPL disjunctions)?

Regards,
Markus

#5 lore raw
Quoted 1 line

How do you expect Coccinelle to figure out what is the right value for rc2?

One variable should usually store the desired return values.

Quoted 2 lines

The only match is in the declaration, but any int variable declaration satisfies that.

I would like to increase case distinctions accordingly.
Variables like “i” and “j” are obviously used in some for loops (for example). How should such control flow properties be restricted by the means of the semantic patch language?

Regards,
Markus

#6 lore raw
Quoted 5 lines · Markus Elfring
Quoted 1 line

How do you expect Coccinelle to figure out what is the right value for rc2?

One variable should usually store the desired return values.

Quoted 7 lines
Quoted 2 lines

The only match is in the declaration, but any int variable declaration satisfies that.

I would like to increase case distinctions accordingly.
Variables like “i” and “j” are obviously used in some for loops (for example). How should such control flow properties be restricted by the means of the semantic patch language?

You can use other rules, inheritance, position variables, etc to disqualify a variable that is not used in the way you like, or that is used in a way that you dont like.

julia

#7 lore raw
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

The only match is in the declaration, but any int variable declaration satisfies that.

I would like to increase case distinctions accordingly.
Variables like “i” and “j” are obviously used in some for loops (for example). How should such control flow properties be restricted by the means of the semantic patch language?

You can use other rules, inheritance, position variables, etc to disqualify a variable that is not used in the way you like, or that is used in a way that you dont like.

Would another SmPL code exclusion specification be sufficient (for a while)?

SmPL script variant:

// See also:
// ath12k_pull_reg_chan_list_ext_update_ev()
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683
@adjustment5@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+... when != \( rc2++ \| ++rc2 \| rc2-- \| --rc2 \)
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

-   kfree(x);
-   return
+   rc2 =
           -c;

+ goto target;
}
...+>
+rc2 = 0;
+target:
kfree(x);
return
- 0
+ rc2
;

Test result:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time spatch …/Projekte/Coccinelle/janitor/use_common_kfree_code5.cocci drivers/net/wireless/ath/ath12k/wmi.c …
Note: processing took 17.6s: drivers/net/wireless/ath/ath12k/wmi.c

real    0m17,669s
user    0m17,556s
sys     0m0,106s

Regards,
Markus

#8 lore raw
Quoted 8 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

The only match is in the declaration, but any int variable declaration satisfies that.

I would like to increase case distinctions accordingly.
Variables like “i” and “j” are obviously used in some for loops (for example). How should such control flow properties be restricted by the means of the semantic patch language?

Quoted 5 lines
Quoted 3 lines

You can use other rules, inheritance, position variables, etc to disqualify a variable that is not used in the way you like, or that is used in a way that you dont like.

Would another SmPL code exclusion specification be sufficient (for a while)?

I doubt it would cover everything, but it could be useful for some cases.

julia

Quoted 46 lines

SmPL script variant:

// See also:
// ath12k_pull_reg_chan_list_ext_update_ev()
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683
@adjustment5@
constant c;
expression e1, e2;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
int rc2;
<+... when != \( rc2++ \| ++rc2 \| rc2-- \| --rc2 \)
if (...)
{
... when != \( x = e1 \| e2(..., &x, ...) \)

(quoted diff) +5 -3
-   kfree(x);
-   return
+   rc2 =
           -c;
+   goto target;
 }
 ...+>
+rc2 = 0;
+target:
 kfree(x);
 return
-       0
+       rc2
 ;

Test result:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time spatch …/Projekte/Coccinelle/janitor/use_common_kfree_code5.cocci drivers/net/wireless/ath/ath12k/wmi.c …
Note: processing took 17.6s: drivers/net/wireless/ath/ath12k/wmi.c

real    0m17,669s
user    0m17,556s
sys     0m0,106s

Regards,
Markus

#9 lore raw
Quoted 3 lines
Quoted 1 line

Would another SmPL code exclusion specification be sufficient (for a while)?

I doubt it would cover everything, but it could be useful for some cases.

I became interested in another extension of the discussed source code transformation approach.
I would like to benefit from the SmPL question mark (singleton) together with another SmPL code exclusion specification.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/083506fcc2b833796a749a9da806d4920b545584/docs/manual/cocci_syntax.tex#L1205
https://github.com/coccinelle/coccinelle/blob/2297fc0f90ce8bef78d2977e9aacc188d7e87c6d/docs/manual/cocci_syntax.tex#L1205

SmPL script variant:

// See also:
// ath12k_pull_reg_chan_list_ext_update_ev()
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683
@adjustment7@
constant c;
expression e1, e2;
identifier x;
fresh identifier target = "free_" ## x;
@@
-kfree(x);
?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );
-return
+my_test =

           -c;

+goto target;

Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci use_common_kfree_code7.cocci …
minus: parse error:
File "use_common_kfree_code7.cocci", line 11, column 18, charpos = 282
around = 'when',
whole content = ?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );

I would appreciate your advice for this use case.

Regards,
Markus

#10 lore raw
Quoted 39 lines · Markus Elfring
Quoted 3 lines
Quoted 1 line

Would another SmPL code exclusion specification be sufficient (for a while)?

I doubt it would cover everything, but it could be useful for some cases.

I became interested in another extension of the discussed source code transformation approach.
I would like to benefit from the SmPL question mark (singleton) together with another SmPL code exclusion specification.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/083506fcc2b833796a749a9da806d4920b545584/docs/manual/cocci_syntax.tex#L1205
https://github.com/coccinelle/coccinelle/blob/2297fc0f90ce8bef78d2977e9aacc188d7e87c6d/docs/manual/cocci_syntax.tex#L1205

SmPL script variant:

// See also:
// ath12k_pull_reg_chan_list_ext_update_ev()
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath12k/wmi.c#L4683
@adjustment7@
constant c;
expression e1, e2;
identifier x;
fresh identifier target = "free_" ## x;
@@
-kfree(x);
?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );

(quoted diff) +2 -1
-return
+my_test =
           -c;
+goto target;

Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci use_common_kfree_code7.cocci …
minus: parse error:
File "use_common_kfree_code7.cocci", line 11, column 18, charpos = 282
around = 'when',
whole content = ?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );

Quoted 1 line

I would appreciate your advice for this use case.

I don't understand what you are trying to do. It is not common to find an assignment, for example, in an argument list. In any case, I guess that when is not supported on argument lists.

julia

#11 lore raw
Quoted 9 lines
Quoted 9 lines

Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci use_common_kfree_code7.cocci …
minus: parse error:
File "use_common_kfree_code7.cocci", line 11, column 18, charpos = 282
around = 'when',
whole content = ?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );

Quoted 3 lines
Quoted 1 line

I would appreciate your advice for this use case.

I don't understand what you are trying to do.

I guess that a better understanding will evolve probably if I (or an other contributor) would publish 88 patch hunks for 6 source files in the directory “drivers/net/wireless/ath”.

Quoted 3 lines

It is not common to find an
assignment, for example, in an argument list. In any case, I guess that when is not supported on argument lists.

I hope that the support can be completed for when clauses on SmPL ellipses somehow.

Regards,
Markus

#12 lore raw
Quoted 9 lines · Markus Elfring
Quoted 9 lines
Quoted 9 lines

Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci use_common_kfree_code7.cocci …
minus: parse error:
File "use_common_kfree_code7.cocci", line 11, column 18, charpos = 282
around = 'when',
whole content = ?ath12k_warn( ... when != \( x = e1 \| e2(..., &x, ...) \) );

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I would appreciate your advice for this use case.

I don't understand what you are trying to do.

I guess that a better understanding will evolve probably if I (or an other contributor) would publish 88 patch hunks for 6 source files in the directory “drivers/net/wireless/ath”.

The semantic patch:

@@
expression x,e1;
@@

*ath12k_warn( ...,(<+... x = e1 ...+>),... )

@@
expression x,e2;
@@

*ath12k_warn( ...,(<+... e2(..., &x, ...) ...+>),... )

Gives no results for the first rule and two results for the second one.

You can just have a rule:

@ok@
expression x,e2;
position p;
@@

ath12k_warn@p( ...,(<+... e2(..., &x, ...) ...+>),... )

and then ensure that the call to ath12k_warn that you match is not at position ok.p.

julia

Quoted 11 lines
Quoted 5 lines

It is not common to find an
assignment, for example, in an argument list. In any case, I guess that when is not supported on argument lists.

I hope that the support can be completed for when clauses on SmPL ellipses somehow.

Regards,
Markus

#13 lore raw

Re: [cocci] Improving SmPL code exclusion specifications?

In reply to Julia Lawall
Quoted 4 lines
Quoted 4 lines

I became interested in another extension of the discussed source code transformation approach.
I would like to benefit from the SmPL question mark (singleton) together with another SmPL code exclusion specification.

…

Quoted 3 lines

I don't understand what you are trying to do. It is not common to find an assignment, for example, in an argument list. In any case, I guess that when is not supported on argument lists.

Can the understanding grow in more desirable directions also with information like the following?

Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci avoid_duplicate_function_call3.cocci …
minus: parse error:
File "avoid_duplicate_function_call3.cocci", line 9, column 9, charpos = 358
around = 'when',
whole content = if (... when != e)

Would you occasionally like to exclude any input parameters (which would usually be passed to subsequent macro/function calls)?

Regards,
Markus

#14 lore raw

Re: [cocci] Swapping branches of SmPL disjunctions?

In reply to Markus Elfring
Quoted 1 line

How can involved software components be improved?

I extended my source code transformation approach in the meantime. Another small script variant for the semantic patch language can be handled by the software combination “Coccinelle 1.2-00038-g2297fc0f” (in principle). My SmPL code variation fiddles also with disjunctions.
I tried then out to swap two branches of such a detailed SmPL disjunction.

// See also:
// ath6kl_usb_submit_ctrl_out()
// https://elixir.bootlin.com/linux/v6.11/source/drivers/net/wireless/ath/ath6kl/usb.c#L872
@adjustment3@
constant c;
expression a, b;
idexpression rc;
identifier rc2, x;
fresh identifier target = "free_" ## x;
@@
(int rc2;
<+...
if (...)
{
... when != \( x = a \| b(..., &x, ...) \)

-   kfree(x);
-   return
+   rc2 =
           -c;

+ goto target;
}
...+>
+rc2 = 0;
+target:
kfree(x);
return
- 0
+ rc2
;
|<+...
if (...)
{
... when != \( \( x \| rc \) = a \| b(..., & \( x \| rc \), ...) \)

-   kfree(x);
-   return rc
+   goto target
    ;

}
...+>
+rc = 0;
+target:
kfree(x);
return
- 0
+ rc
;
)

Questionable test result:
Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/janitor> spatch --parse-cocci use_common_kfree_code4.cocci …
minus: parse error:
File "use_common_kfree_code4.cocci", line 30, column 1, charpos = 474
around = '<+...',
whole content = |<+...

Under which circumstances will this version become better supported?

Regards,
Markus