Skip to contentSkip to conversation

Finding embedded function names?

Cocci Joe Perches 3 participants 8 messages Dec 5, 2014

lore
Share & tools
lore
#0 lore raw

Is it possible for coccinelle to look at the name
of a function that might be embedded in a format?

ie for:

void testme(void)

{
	printf("testme: some message\n");
}

Can it find the "testme" embedded in a format string?

#1 lore raw
Quoted 11 lines · Joe Perches

Is it possible for coccinelle to look at the name
of a function that might be embedded in a format?

ie for:

void testme(void)

{
	printf("testme: some message\n");
}

Can it find the "testme" embedded in a format string?

Yes, by using python/ocaml:

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:ocaml@
c << r.c;
p << r.p;
@@

let ce = (List.hd p).current_element in
if List.length(Str.split_delim (Str.regexp ce) c) > 1 then Printf.printf "%s:%d: %s\n"
(List.hd p).file (List.hd p).line c

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n" drivers/net/wireless/zd1211rw/zd_usb.c:1551: "%s usb_init()\n" drivers/net/wireless/zd1211rw/zd_usb.c:1453: "disconnected\n" drivers/net/wireless/rtlwifi/rtl8723be/dm.c:800:
"rtl8723be_dm_txpower_tracking_callback_thermalmeter\n" drivers/net/wireless/rtlwifi/rtl8723be/fw.c:461:
"rtl8723be_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n" drivers/net/wireless/rtlwifi/rtl8723be/fw.c:464:
"rtl8723be_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n" drivers/net/wireless/rtlwifi/rtl8723be/phy.c:451:
"<===_rtl8723be_phy_convert_txpower_dbm_to_relative_value()\n" drivers/net/wireless/rtlwifi/rtl8192ee/fw.c:721:
"rtl92ee_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n" drivers/net/wireless/rtlwifi/rtl8192ee/fw.c:724:
"rtl92ee_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n" drivers/net/wireless/rtlwifi/rtl8192ee/phy.c:635:
"<==phy_convert_txpwr_dbm_to_rel_val()\n"

The idea would be to replace these by %s and __func__? That would also be possible.

julia

#2 lore raw
Quoted 25 lines · Julia Lawall
Quoted 8 lines · Joe Perches

Is it possible for coccinelle to look at the name
of a function that might be embedded in a format?
ie for:
void testme(void)

{
	printf("testme: some message\n");
}

Can it find the "testme" embedded in a format string?

Yes, by using python/ocaml:

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:ocaml@
c << r.c;
p << r.p;
@@

Quoted 4 lines

let ce = (List.hd p).current_element in
if List.length(Str.split_delim (Str.regexp ce) c) > 1 then Printf.printf "%s:%d: %s\n"
(List.hd p).file (List.hd p).line c

Good to know, thanks.

Quoted 3 lines

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n"

[]

Quoted 2 lines

The idea would be to replace these by %s and __func__? That would also be possible.

Yes and no.

A lot of these are function tracing style messages and those should just be deleted.

Otherwise, yes.

#3 lore raw
Quoted 36 lines · Joe Perches
Quoted 25 lines · Julia Lawall
Quoted 8 lines · Joe Perches

Is it possible for coccinelle to look at the name
of a function that might be embedded in a format?
ie for:
void testme(void)

{
	printf("testme: some message\n");
}

Can it find the "testme" embedded in a format string?

Yes, by using python/ocaml:

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:ocaml@
c << r.c;
p << r.p;
@@

Quoted 4 lines

let ce = (List.hd p).current_element in
if List.length(Str.split_delim (Str.regexp ce) c) > 1 then Printf.printf "%s:%d: %s\n"
(List.hd p).file (List.hd p).line c

Good to know, thanks.

Quoted 3 lines

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n"

Quoted 8 lines

[]

Quoted 2 lines

The idea would be to replace these by %s and __func__? That would also be possible.

Yes and no.

A lot of these are function tracing style messages and those should just be deleted.

Would it be possible to characterize what a function tracing style message would be? Something like "<===_rtl8723be_phy_convert_txpower_dbm_to_relative_value()\n" that looks like it may represent a function exit?

Anything that doesn't contain %?

Anything that is not under an if?

julia

#4 lore raw
Quoted 5 lines · Julia Lawall
Quoted 4 lines · Joe Perches
Quoted 3 lines · Julia Lawall
Quoted 2 lines · Joe Perches

Is it possible for coccinelle to look at the name
of a function that might be embedded in a format?

[]

Quoted 1 line
Quoted 1 line
Quoted 1 line

Yes, by using python/ocaml:

[]

Quoted 12 lines
Quoted 11 lines
Quoted 3 lines

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n"

[]

Quoted 2 lines

The idea would be to replace these by %s and __func__? That would also be possible.

Yes and no.

A lot of these are function tracing style messages and those should just be deleted.

Quoted 6 lines

Would it be possible to characterize what a function tracing style message would be? Something like "<===_rtl8723be_phy_convert_txpower_dbm_to_relative_value()\n" that looks like it may represent a function exit?

Anything that doesn't contain %?

Some of these tracing style messages are also emitting arguments. I'd still delete those.

Quoted 1 line

Anything that is not under an if?

For function entrances:
Maybe something in a function's first few non-definition statements not under an if/while/do/for/switch?

For function exits:
Maybe the last couple statements just before a return statement or ending brace (also not under an if/etc)

#5 lore raw
In reply to Joe Perches
Quoted 28 lines · Joe Perches
Quoted 17 lines · Julia Lawall

On Thu, 4 Dec 2014, Joe Perches wrote:

Yes, by using python/ocaml:

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:ocaml@
c << r.c;
p << r.p;
@@

Quoted 4 lines

let ce = (List.hd p).current_element in
if List.length(Str.split_delim (Str.regexp ce) c) > 1 then Printf.printf "%s:%d: %s\n"
(List.hd p).file (List.hd p).line c

Good to know, thanks.

Quoted 3 lines

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n"

Quoted 8 lines

[]

Quoted 2 lines

The idea would be to replace these by %s and __func__? That would also be possible.

Yes and no.

A lot of these are function tracing style messages and those should just be deleted.

Hardcoding the function name in a literal string also makes typos (or copy-pastos) possible. I extended Julia's code to allow a small edit distance. Requires the Levenshtein python module (on debian, apt-get install python-levenshtein). It's not terribly slow, but to be really useful I (or someone) would need to reduce the number of false positives.

One gets for example

drivers/net/wireless/rtlwifi/rtl8821ae/dm.c:2082:rtl8821ae_dm_txpwr_track_set_pwr():2: "===>rtl8812ae_dm_txpwr_track_set_pwr\n" drivers/net/ethernet/broadcom/bnx2x/bnx2x_link.c:1229:bnx2x_ets_e3b0_config():2: "bnx2x_ets_E3B0_config SP failed\n"

The first of these is probably one of the tracing style messages; the second probably falls in the 'should use %s, __func__' category (other strings in that function actually use lowercase e3b0).

@initialize:python@
@@
import re
from Levenshtein import distance
mindist = 1
maxdist = 2
ignore_leading = True

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:python@
c << r.c;
p << r.p;
@@

func = p[0].current_element
wpattern = "[a-zA-Z_][a-zA-Z0-9_]*"
if ignore_leading:
func = func.strip("_")
wpattern = "[a-zA-Z][a-zA-Z0-9_]*"
lf = len(func)
// ignore extremely short function names
if lf > 3:
words = [w for w in re.findall(wpattern, c) if abs(len(w) - lf) <= maxdist] for w in words:
d = distance(w, func)
if mindist <= d and d <= maxdist:
print "%s:%d:%s():%d: %s" % (p[0].file, int(p[0].line), func, d, c) break

Rasmus

#6 lore raw
Quoted 36 lines · Rasmus Villemoes
Quoted 34 lines · Joe Perches
Quoted 17 lines · Julia Lawall

On Thu, 4 Dec 2014, Joe Perches wrote:

Yes, by using python/ocaml:

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:ocaml@
c << r.c;
p << r.p;
@@

Quoted 4 lines

let ce = (List.hd p).current_element in
if List.length(Str.split_delim (Str.regexp ce) c) > 1 then Printf.printf "%s:%d: %s\n"
(List.hd p).file (List.hd p).line c

Good to know, thanks.

Quoted 3 lines

Here are some results:

drivers/net/wireless/zd1211rw/zd_usb.c:1573: "%s usb_exit()\n"

[]

Quoted 2 lines

The idea would be to replace these by %s and __func__? That would also be possible.

Yes and no.

Quoted 8 lines
Quoted 2 lines

A lot of these are function tracing style messages and those should just be deleted.

Hardcoding the function name in a literal string also makes typos (or copy-pastos) possible. I extended Julia's code to allow a small edit distance. Requires the Levenshtein python module (on debian, apt-get install python-levenshtein). It's not terribly slow, but to be really useful I (or someone) would need to reduce the number of false positives.

Cool. Thanks!

julia

Quoted 50 lines

One gets for example

drivers/net/wireless/rtlwifi/rtl8821ae/dm.c:2082:rtl8821ae_dm_txpwr_track_set_pwr():2: "===>rtl8812ae_dm_txpwr_track_set_pwr\n" drivers/net/ethernet/broadcom/bnx2x/bnx2x_link.c:1229:bnx2x_ets_e3b0_config():2: "bnx2x_ets_E3B0_config SP failed\n"

The first of these is probably one of the tracing style messages; the second probably falls in the 'should use %s, __func__' category (other strings in that function actually use lowercase e3b0).

@initialize:python@
@@
import re
from Levenshtein import distance
mindist = 1
maxdist = 2
ignore_leading = True

@r@
char [] c;
position p;
identifier f;
@@

f(...,c at p,...)

@script:python@
c << r.c;
p << r.p;
@@

func = p[0].current_element
wpattern = "[a-zA-Z_][a-zA-Z0-9_]*"
if ignore_leading:

   func = func.strip("_")
   wpattern = "[a-zA-Z][a-zA-Z0-9_]*"

lf = len(func)
// ignore extremely short function names
if lf > 3:
words = [w for w in re.findall(wpattern, c) if abs(len(w) - lf) <= maxdist] for w in words:
d = distance(w, func)
if mindist <= d and d <= maxdist:
print "%s:%d:%s():%d: %s" % (p[0].file, int(p[0].line), func, d, c) break

Rasmus

#7 lore raw
In reply to Rasmus Villemoes
Quoted 5 lines · Rasmus Villemoes

Hardcoding the function name in a literal string also makes typos (or copy-pastos) possible. I extended Julia's code to allow a small edit distance. Requires the Levenshtein python module (on debian, apt-get install python-levenshtein). It's not terribly slow, but to be really useful I (or someone) would need to reduce the number of false positives.

I agree with Julia, cool, thanks.