Skip to contentSkip to conversation

Determining the statement end according to a pointer dereference (with SmPL)?

cocci Markus Elfring 1 participant 2 messages May 21, 2024 – May 22, 2024

lore
Share & tools
lore
#0 lore raw

Hello,

A questionable source code combination can eventually be found also with a tiny script for the semantic patch language like the following.

Update candidate:
platform/x86: ISST: Support partitioned systems
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/platform/x86/intel/speed_select_if/isst_tpmi_core.c?h=next-20240521&id=9d1d36268f3d8276aefd1fad4e0a415dc8c36edd
https://lkml.org/lkml/2024/4/23/1417

See also:
[PATCH v2] platform/x86: ISST: fix use-after-free in tpmi_sst_dev_remove()
https://lore.kernel.org/lkml/[email protected]/

@usage@
expression context, ex;
identifier member;
@@
*kfree(context);
... when != context = ex
when any
*(context)->member

A succinct search pattern can be easily expressed with an SmPL asterisk for an involved pointer dereference.
You would probably like to achieve also a corresponding code transformation. But I find it very challenging so far to determine the end of the statement which contains the found dereference expression.
I would appreciate your advice.

Regards,
Markus

#1 lore raw

Re: [cocci] Analysing statement sequences in consistent ways (with SmPL)?

Quoted 8 lines

@usage@
expression context, ex;
identifier member;
@@
*kfree(context);
... when != context = ex
when any
*(context)->member

The small size of such a script for the semantic patch language is helpful for the clarification of data processing consequences. Unfortunately, this source code search approach is still too simple. It can generate diff outputs which need to be interpreted as false positives.

Example:
https://elixir.bootlin.com/linux/v6.9.1/source/drivers/ata/libata-core.c#L5941
…
@@ -5897,12 +5897,9 @@ int ata_host_register(struct ata_host *h
* allocation time.
*/
for (i = host->n_ports; host->ports[i]; i++)
- kfree(host->ports[i]);

/* give ports names and add SCSI hosts */
for (i = 0; i < host->n_ports; i++) {
- host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
- host->ports[i]->local_port_no = i + 1;
}
…

I guess that such a test case points known software limitations and remaining development challenges out.

See also:
Support source code scopes
2023-10-23
https://github.com/coccinelle/coccinelle/issues/322

Regards,
Markus