Skip to contentSkip to conversation

Finding missing return value checks for some function calls with SmPL

Cocci SF Markus Elfring 3 participants 12 messages Dec 2, 2016 – Dec 7, 2016 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

I would like to try another search pattern out with the semantic patch language from the software ?Coccinelle 1.0.6-00022-g862f5b29 (OCaml 4.03)?.

SmPL script example:

@find_too_late_checking@
expression ex1, ex2;
identifier action1 =~ "^(?x)
(?:
kzalloc
| kmalloc_array
)$",

           action2 =~ "^(?x)

(?:
kzalloc
| kmalloc_array
)$",

           work;

type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any
}

Source code example:

static void __init cpg_mstp_clocks_init(struct device_node *np)

{
	struct mstp_clock_group *group;
	const char *idxname;
	struct clk **clks;
	unsigned int i;

	group = kzalloc(sizeof(*group), GFP_KERNEL);
	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
	if (group == NULL || clks == NULL) {
		kfree(group);
		kfree(clks);
		pr_err("%s: failed to allocate group\n", __func__);
		return;
	}
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking2.cocci ../Probe/clk-mstp-excerpt1.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
HANDLING: ../Probe/clk-mstp-excerpt1.c

Now I wonder why the first assignment is not marked together with the function call for further considerations by my source code analysis approach so far. I would appreciate your advices.

Regards,
Markus

#1 lore raw
Quoted 2 lines

Now I wonder why the first assignment is not marked together with the function call for further considerations by my source code analysis approach so far.

I should have adjusted one of the used regular expressions a bit more like the following.

@find_too_late_checking@
expression ex1, ex2;
identifier action1 =~ "^(?x)
(?:
kzalloc
| kmalloc_array
)$",

           action2 =~ "^(?x)

(?:
kzalloc
| kmalloc(?:_array)?
)$",

           work;

type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking2.cocci ../Probe/clk-mstp-excerpt1.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
HANDLING: ../Probe/clk-mstp-excerpt1.c

tmp/cocci-output-20109-52f048-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -1
diff = 
@@ -5,7 +5,6 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
 	if (group == NULL || clks == NULL) {
 		kfree(group);

I imagine that there are further software improvements to consider around such use cases.

Regards,
Markus

#2 lore raw
Quoted 46 lines · SF Markus Elfring
Quoted 2 lines

Now I wonder why the first assignment is not marked together with the function call for further considerations by my source code analysis approach so far.

I should have adjusted one of the used regular expressions a bit more like the following.

@find_too_late_checking@
expression ex1, ex2;
identifier action1 =~ "^(?x)
(?:
kzalloc
| kmalloc_array
)$",

           action2 =~ "^(?x)

(?:
kzalloc
| kmalloc(?:_array)?
)$",

           work;

type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking2.cocci ../Probe/clk-mstp-excerpt1.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
HANDLING: ../Probe/clk-mstp-excerpt1.c
diff =

tmp/cocci-output-20109-52f048-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -1
@@ -5,7 +5,6 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
 	if (group == NULL || clks == NULL) {
 		kfree(group);
Quoted 2 lines

I imagine that there are further software improvements to consider around such use cases.

Not sure what you are asking about here. Note that it is unlikely that anyone would accept a patch on the above code. Group is tested before it is used.

julia

#3 lore raw
Quoted 16 lines
Quoted 16 lines

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking2.cocci ../Probe/clk-mstp-excerpt1.c
init_defs_builtins: /usr/local/lib64/coccinelle/standard.h
HANDLING: ../Probe/clk-mstp-excerpt1.c
diff =

tmp/cocci-output-20109-52f048-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -1
@@ -5,7 +5,6 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
 	if (group == NULL || clks == NULL) {
 		kfree(group);
Quoted 4 lines
Quoted 2 lines

I imagine that there are further software improvements to consider around such use cases.

Not sure what you are asking about here.

A bit more explanation can be found in my update suggestion ?[PATCH 3/4] clk/Renesas-MSTP: Less function calls in cpg_mstp_clocks_init() after error detection? from 2016-09-14.

https://patchwork.kernel.org/patch/9332367/
https://lkml.kernel.org/r/<[email protected]>

Quoted 1 line

Note that it is unlikely that anyone would accept a patch on the above code.

I would agree because I used the asterisk functionality from the Coccinelle software just to try another source code search pattern out a bit more for test purposes.

I guess that corresponding syntax elements can be clarified further.

Quoted 1 line

Group is tested before it is used.

But can it be that this assignment is checked a bit too late?

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.10.1612021324520.3056@hadrien

#5 lore raw
Quoted 3 lines
Quoted 1 line

But can it be that this assignment is checked a bit too late?

Yes,

Thanks that you can agree to such a view.

Quoted 1 line

but no one will care.

I am curious if further software development opinions will appear around similar situations.

Quoted 1 line

The code is more concise as is.

I have got an other opinion for such an implementation detail.

Quoted 2 lines

If the alloc fails, the machine is already in a very bad state, so one more alloc attempt doesn't matter.

Would it be nicer to avoid a questionable function call (and a corresponding variable assignment) when it was determined that the desired task could not be completed for a moment?

Regards,
Markus

#6 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

In reply to SF Markus Elfring

Hello,

I tried another source code search pattern out with the semantic patch language from the software ?Coccinelle 1.0.6-00022-g862f5b29 (OCaml 4.03)?.

SmPL script example:

@find_too_late_checking@
expression assign1, assign2, ex1, ex2;
identifier action1 =~ "^(?x)
(?:
k(?:[cmz]alloc|(?:m(?:alloc_array|em_cache_alloc(?:_node)?)|zalloc_node))
| of_find_matching_node
# Alternation placeholder
)$",

           action2 =~ "^(?x)

(?:
k(?:[cmz]alloc|(?:m(?:alloc_array|em_cache_alloc(?:_node)?)|zalloc_node))
| of_find_matching_node
# Alternation placeholder
)$",

           work;

statement is, es;
type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any

     when != (ex1 = assign1)
     when != (ex2 = assign2)
 if (
*      \( !(ex1) \| (ex1) == NULL \)
    || \( !(ex2) \| (ex2) == NULL \)
    || ...)

is
else
es
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking1.cocci ../Probe/clk-mstp-excerpt1.c ?

tmp/cocci-output-23612-4723e1-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -2
diff = 
@@ -5,9 +5,7 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
-	if (group == NULL || clks == NULL) {
 		kfree(group);
 		kfree(clks);
 		pr_err("%s: failed to allocate group\n", __func__);

This source code analysis approach is incomplete so far. It demonstrates a few aspects where further software extensions might help.

1. The semantic patch language supports data processing with regular expressions to some degree as direct constraints for metavariables.
A ?regexp? can be specified for several metavariables. Such regular expressions can become very detailed and big so that I got the desire to avoid code duplication there as much as possible.
So it would be nice if SmPL variables can share a specific compiled ?regexp? from a single specification place.

2. The support for script constraints was recently mentioned.
https://github.com/coccinelle/coccinelle/commit/cbc751b30d9e02390d60ebed643c8e4a3fa0bb2b

But I am unsure about their current development and documentation status. Are there any more imaginations floating around for the notation and usage of named (or ad hoc) predicates?

Regards,
Markus

#7 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

Quoted 73 lines · SF Markus Elfring

Hello,

I tried another source code search pattern out with the semantic patch language from the software ?Coccinelle 1.0.6-00022-g862f5b29 (OCaml 4.03)?.

SmPL script example:

@find_too_late_checking@
expression assign1, assign2, ex1, ex2;
identifier action1 =~ "^(?x)
(?:

   k(?:[cmz]alloc|(?:m(?:alloc_array|em_cache_alloc(?:_node)?)|zalloc_node))
|  of_find_matching_node
# Alternation placeholder
)$",
           action2 =~ "^(?x)
(?:
   k(?:[cmz]alloc|(?:m(?:alloc_array|em_cache_alloc(?:_node)?)|zalloc_node))
|  of_find_matching_node
# Alternation placeholder
)$",
           work;

statement is, es;
type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any

     when != (ex1 = assign1)
     when != (ex2 = assign2)
 if (
*      \( !(ex1) \| (ex1) == NULL \)
    || \( !(ex2) \| (ex2) == NULL \)
    || ...)

is
else
es
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking1.cocci ../Probe/clk-mstp-excerpt1.c ?
diff =

tmp/cocci-output-23612-4723e1-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -2
@@ -5,9 +5,7 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
-	if (group == NULL || clks == NULL) {
 		kfree(group);
 		kfree(clks);
 		pr_err("%s: failed to allocate group\n", __func__);

This source code analysis approach is incomplete so far. It demonstrates a few aspects where further software extensions might help.

1. The semantic patch language supports data processing with regular expressions to some degree as direct constraints for metavariables.
A ?regexp? can be specified for several metavariables. Such regular expressions can become very detailed and big so that I got the desire to avoid code duplication there as much as possible.
So it would be nice if SmPL variables can share a specific compiled ?regexp? from a single specification place.

Quoted 6 lines

2. The support for script constraints was recently mentioned.
https://github.com/coccinelle/coccinelle/commit/cbc751b30d9e02390d60ebed643c8e4a3fa0bb2b

But I am unsure about their current development and documentation status. Are there any more imaginations floating around for the notation and usage of named (or ad hoc) predicates?

I don't anticipate any changes in user-facing functionality in either of these cases in the short term.

julia

#8 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

Quoted 7 lines
Quoted 5 lines

https://github.com/coccinelle/coccinelle/commit/cbc751b30d9e02390d60ebed643c8e4a3fa0bb2b

But I am unsure about their current development and documentation status. Are there any more imaginations floating around for the notation and usage of named (or ad hoc) predicates?

I don't anticipate any changes in user-facing functionality

I would appreciate a bit more explicit information for open isses.

Quoted 1 line

in either of these cases in the short term.

Have we got just different views about the relevance of curly brackets around the specification of a function name (instead of a complete function body)?

Would other delimiters help to make the corresponding data passing a bit shorter?

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.10.1612021456580.3056@hadrien

#10 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

Quoted 1 line

I don't think there is anything to be more explicit about.

Do you find the documentation complete for this functionality at the moment?

Quoted 1 line

I don't foresee any change in the regular expression syntax,

I imagine a need for advanced configuration possibilities around ?regexp? engines.

Quoted 1 line

and I don't forsee any change in the constraint script syntax.

Is the current approach the official one already?

Quoted 2 lines

Perhaps the constraint scrpt syntax could be generalized to allow more in practice than function calls.

Which software development concerns can hinder progress in the way I imagine so far?

Quoted 1 line

That might happen some day, but it is an extremely low priority.

I am curious if the involved dependencies can be clarified further.

Quoted 4 lines
Quoted 2 lines

Have we got just different views about the relevance of curly brackets around the specification of a function name (instead of a complete function body)?

No idea what this means. What curly braces? What function name?

I find this kind of feedback strange.

I hope that communication difficulties can be resolved better around the provided test case, can't it?
https://github.com/coccinelle/coccinelle/blob/cbc751b30d9e02390d60ebed643c8e4a3fa0bb2b/tests/idcon_ocaml.cocci

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.10.1612021526560.3056@hadrien

#12 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

Quoted 3 lines
Quoted 1 line

Do you find the documentation complete for this functionality at the moment?

No idea.

How can we achieve a better common understanding on this aspect for example?

Quoted 3 lines
Quoted 1 line

I imagine a need for advanced configuration possibilities around ?regexp? engines.

You imagine many things.

Yes, of course. ;-)

Quoted 1 line

Changes will only happen when there is a concrete need.

I came along some development needs also together with software experiments around SmPL scripts.
How do your needs look different over time?

Quoted 3 lines
Quoted 1 line

Is the current approach the official one already?

Yes.

Thanks for this information.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Perhaps the constraint scrpt syntax could be generalized to allow more in practice than function calls.

Which software development concerns can hinder progress in the way I imagine so far?

Quoted 8 lines

Lack of time.

Quoted 4 lines
Quoted 2 lines

That might happen some day, but it is an extremely low priority.

I am curious if the involved dependencies can be clarified further.

I doubt there are any dependencies.

Our software development capacity is limited as usual.
Will future software research activities improve the situation a bit more?

Quoted 3 lines
Quoted 1 line

https://github.com/coccinelle/coccinelle/blob/cbc751b30d9e02390d60ebed643c8e4a3fa0bb2b/tests/idcon_ocaml.cocci

In the test case the braces are around a function call, not a function name.

This detail is also clear for me to some degree.

Quoted 1 line

No idea what could be relevant or irrelevant about it.

I got further ideas for this software area after a delay.

Quoted 1 line

That is the syntax that has been implemented.

I wonder still why such a variant was chosen.

Quoted 2 lines

It may change when someone when someone motivated enough to work on the implementation writes the code to make the change and tests the result.

My motivation could eventually increase if the understanding of the corresponding OCaml source code could become better somehow besides other factors.

Quoted 2 lines

It won't change just because someone finds it conceptually inelegant and sends lots of emails about it.

So there is some usual change resistance. I dare to provide intensive feedback in the hope that details can be uncovered which can support more progress.

Quoted 1 line

It's actually strange that you want to define regexps in other places,

This detail is just another aspect for the involved software evolution.

Quoted 1 line

but you don't want to define scripts in other places.

I got also further ideas around the placement of script fragments.

Quoted 2 lines

Why not just define a python function that you can call in a script to do your big regexp for you?

This approach can only work safely if script constraints will be completely documented in the manual (besides one test case in OCaml and Python).

Regards,
Markus

#13 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

In reply to a message not in the archive
Quoted 2 lines

Why not just define a python function that you can call in a script to do your big regexp for you?

Would any more developers and reviewers like to extend a source code analysis approach with a regular expression (or other data structure) like the following?

SmPL script example:

@initialize:python@
@@
import re
compiled = re.compile("""^
(?:
k(?:

       [cmz]alloc
    |  (?:m(?:
              alloc_array
           |  em(?:
                   _cache_alloc(?:_node)?
                |  dup
                )
           )
       |  zalloc_node
       )
    |  str(?:
             dup(?:_const)?
          |  ndup
          )
    )

| of_find_matching_node
# Alternation placeholder
)$""", re.VERBOSE)

def is_selected(id):
match = compiled.search(id)
if match:
return True
else:
return False

@find_too_late_checking@
expression assign1, assign2, ex1, ex2;
identifier action1: script:python() { is_selected(action1) },
action2: script:python() { is_selected(action2) }, work;
statement is, es;
type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any

     when != (ex1 = assign1)
     when != (ex2 = assign2)
 if (
*      \( !(ex1) \| (ex1) == NULL \)
    || \( !(ex2) \| (ex2) == NULL \)
    || ...)

is
else
es
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt show_too_late_checking-python-20161202.cocci ../Probe/clk-mstp-excerpt1.c ?

tmp/cocci-output-20769-f189ac-clk-mstp-excerpt1.c renamed from Probe/clk-mstp-excerpt1.c +0 -2
diff = 
@@ -5,9 +5,7 @@ static void __init cpg_mstp_clocks_init(
 	struct clk **clks;
 	unsigned int i;
 
-	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
-	if (group == NULL || clks == NULL) {
 		kfree(group);
 		kfree(clks);
 		pr_err("%s: failed to allocate group\n", __func__);

Regards,
Markus

#14 lore raw

[Cocci] Comparing SmPL script constraints with direct regular expression interface

Quoted 5 lines · SF Markus Elfring
Quoted 2 lines

Why not just define a python function that you can call in a script to do your big regexp for you?

Would any more developers and reviewers like to extend a source code analysis approach with a regular expression (or other data structure) like the following?

Why would you want to use that?
Just use the regexp directly, e.g.
identifier action1 = "regexp";
I'm using big regexp'es that way. Yes, I do find the perl style compact regexp easier to read.

And for more specialized stuff I'm passing the identifier through a python rule for further filtering.

bye
michael

Quoted 61 lines

SmPL script example:

@initialize:python@
@@
import re
compiled = re.compile("""^
(?:

   k(?:
       [cmz]alloc
    |  (?:m(?:
              alloc_array
           |  em(?:
                   _cache_alloc(?:_node)?
                |  dup
                )
           )
       |  zalloc_node
       )
    |  str(?:
             dup(?:_const)?
          |  ndup
          )
    )

| of_find_matching_node
# Alternation placeholder
)$""", re.VERBOSE)

def is_selected(id):
match = compiled.search(id)
if match:
return True
else:
return False

@find_too_late_checking@
expression assign1, assign2, ex1, ex2;
identifier action1: script:python() { is_selected(action1) },
action2: script:python() { is_selected(action2) }, work;
statement is, es;
type return_type;
@@
return_type work(...)
{
... when any
*ex1 = action1(...);
ex2 = action2(...);
... when any

     when != (ex1 = assign1)
     when != (ex2 = assign2)
 if (
*      \( !(ex1) \| (ex1) == NULL \)
    || \( !(ex2) \| (ex2) == NULL \)
    || ...)

is
else
es
... when any
}