Coccinelle: add atomic_as_refcounter script
Subsystems (2)
PATCH 1/1 Coccinelle: add atomic_as_refcounter script
atomic_as_refcounter.cocci script allows detecting cases when refcount_t type and API should be used instead of atomic_t.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 131 ++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 scripts/coccinelle/api/atomic_as_refcounter.cocci
scripts/coccinelle/api/atomic_as_refcounter.cocci new file +131 -0
@@ -0,0 +1,131 @@ +// Check if refcount_t type and API should be used +// instead of atomic_t type when dealing with refcounters +// +// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation +// +// Confidence: Moderate +// URL: http://coccinelle.lip6.fr/ +// Options: --include-headers --very-quiet + +virtual report + + at r1 exists@ +identifier a, x; +position p1, p2; +identifier fname =~ ".*free.*"; +identifier fname2 =~ ".*destroy.*"; +identifier fname3 =~ ".*del.*"; +identifier fname4 =~ ".*queue_work.*"; +identifier fname5 =~ ".*schedule_work.*"; +identifier fname6 =~ ".*call_rcu.*"; + +@@ + +( + atomic_dec_and_test at p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +( + fname at p2(a, ...); +| + fname2 at p2(...); +| + fname3 at p2(...); +| + fname4 at p2(...); +| + fname5 at p2(...); +| + fname6 at p2(...); +) + + + at script:python depends on report@ +p1 << r1.p1; +p2 << r1.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + + at r4 exists@ +identifier a, x, y; +position p1, p2; +identifier fname =~ ".*free.*"; + +@@ + +( + atomic_dec_and_test@p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +y=a +... +fname at p2(y, ...); + + + at script:python depends on report@ +p1 << r4.p1; +p2 << r4.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + +@r2 exists@ +identifier a, x; +position p1; +@@ + +( +atomic_add_unless(&(a)->x,-1,1)@p1 +| +atomic_long_add_unless(&(a)->x,-1,1)@p1 +| +atomic64_add_unless(&(a)->x,-1,1)@p1 +) + + at script:python depends on report@ +p1 << r2.p1; +@@ +msg = "atomic_add_unless" +coccilib.report.print_report(p1[0], msg) + + at r3 exists@ +identifier x; +position p1; +@@ + +( +x = atomic_add_return at p1(-1, ...); +| +x = atomic_long_add_return at p1(-1, ...); +| +x = atomic64_add_return at p1(-1, ...); +) + +@script:python depends on report@ +p1 << r3.p1; +@@ +msg = "x = atomic_add_return(-1, ...)" +coccilib.report.print_report(p1[0], msg) + +
Signature · 1 line
2.7.4
Quoted 6 lines · Elena Reshetova
atomic_as_refcounter.cocciscript allows detecting cases whenrefcount_ttype and API should be used instead ofatomic_t.Signed-off-by Elena Reshetova <[email protected]>Acked-by Julia Lawall <[email protected]>
Oops, I think this got lost. Who can take this patch? I thought Julia ran the scripts/coccinelle/ tree, but looking at git log, it looks more like it's Masahiro? Either way, let's get this in the tree. Who can take it?
Thanks!
-Kees
Quoted 145 lines
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 131 ++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 scripts/coccinelle/api/atomic_as_refcounter.cocciscripts/coccinelle/api/atomic_as_refcounter.cocci new file +131
-1@@ -0,0 +1,131 @@ +// Check if refcount_t type and API should be used +// instead of atomic_t type when dealing with refcounters +// +// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation +// +// Confidence: Moderate +// URL: http://coccinelle.lip6.fr/ +// Options: --include-headers --very-quiet + +virtual report + + at r1 exists@ +identifier a, x; +position p1, p2; +identifier fname =~ ".*free.*"; +identifier fname2 =~ ".*destroy.*"; +identifier fname3 =~ ".*del.*"; +identifier fname4 =~ ".*queue_work.*"; +identifier fname5 =~ ".*schedule_work.*"; +identifier fname6 =~ ".*call_rcu.*"; + +@@ + +( + atomic_dec_and_test at p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +( + fname at p2(a, ...); +| + fname2 at p2(...); +| + fname3 at p2(...); +| + fname4 at p2(...); +| + fname5 at p2(...); +| + fname6 at p2(...); +) + + + at script:python depends on report@ +p1 << r1.p1; +p2 << r1.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + + at r4 exists@ +identifier a, x, y; +position p1, p2; +identifier fname =~ ".*free.*"; + +@@ + +( + atomic_dec_and_test at p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +y=a +... +fname at p2(y, ...); + + + at script:python depends on report@ +p1 << r4.p1; +p2 << r4.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + + at r2 exists@ +identifier a, x; +position p1; +@@ + +( +atomic_add_unless(&(a)->x,-1,1)@p1 +| +atomic_long_add_unless(&(a)->x,-1,1)@p1 +| +atomic64_add_unless(&(a)->x,-1,1)@p1 +) + + at script:python depends on report@ +p1 << r2.p1; +@@ +msg = "atomic_add_unless" +coccilib.report.print_report(p1[0], msg) + + at r3 exists@ +identifier x; +position p1; +@@ + +( +x = atomic_add_return at p1(-1, ...); +| +x = atomic_long_add_return at p1(-1, ...); +| +x = atomic64_add_return at p1(-1, ...); +) + + at script:python depends on report@ +p1 << r3.p1; +@@ +msg = "x = atomic_add_return(-1, ...)" +coccilib.report.print_report(p1[0], msg) + + --2.7.4
Signature · 2 lines
Kees Cook Pixel Security
Quoted 9 lines · Kees Cook
Quoted 6 lines · Elena Reshetova
atomic_as_refcounter.cocciscript allows detecting cases whenrefcount_ttype and API should be used instead ofatomic_t.Signed-off-by Elena Reshetova <[email protected]>Acked-by Julia Lawall <[email protected]>
Quoted 6 lines
Reviewed-by Kees Cook <[email protected]>Oops, I think this got lost. Who can take this patch? I thought Julia ran the
scripts/coccinelle/tree, but looking at git log, it looks more like it's Masahiro? Either way, let's get this in the tree. Who can take it?
Masahiro takes patches.
julia
Quoted 157 lines
Thanks!
-Kees
Quoted 143 lines
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 131 ++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 scripts/coccinelle/api/atomic_as_refcounter.cocciscripts/coccinelle/api/atomic_as_refcounter.cocci new file +131
-1@@ -0,0 +1,131 @@ +// Check if refcount_t type and API should be used +// instead of atomic_t type when dealing with refcounters +// +// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation +// +// Confidence: Moderate +// URL: http://coccinelle.lip6.fr/ +// Options: --include-headers --very-quiet + +virtual report + + at r1 exists@ +identifier a, x; +position p1, p2; +identifier fname =~ ".*free.*"; +identifier fname2 =~ ".*destroy.*"; +identifier fname3 =~ ".*del.*"; +identifier fname4 =~ ".*queue_work.*"; +identifier fname5 =~ ".*schedule_work.*"; +identifier fname6 =~ ".*call_rcu.*"; + +@@ + +( + atomic_dec_and_test at p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +( + fname at p2(a, ...); +| + fname2 at p2(...); +| + fname3 at p2(...); +| + fname4 at p2(...); +| + fname5 at p2(...); +| + fname6 at p2(...); +) + + + at script:python depends on report@ +p1 << r1.p1; +p2 << r1.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + + at r4 exists@ +identifier a, x, y; +position p1, p2; +identifier fname =~ ".*free.*"; + +@@ + +( + atomic_dec_and_test at p1(&(a)->x) +| + atomic_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_lock at p1(&(a)->x, ...) +| + atomic_long_dec_and_test at p1(&(a)->x) +| + atomic64_dec_and_test at p1(&(a)->x) +| + local_dec_and_test at p1(&(a)->x) +) +... +y=a +... +fname at p2(y, ...); + + + at script:python depends on report@ +p1 << r4.p1; +p2 << r4.p2; +@@ +msg = "atomic_dec_and_test variation before object free at line %s." +coccilib.report.print_report(p1[0], msg % (p2[0].line)) + + at r2 exists@ +identifier a, x; +position p1; +@@ + +( +atomic_add_unless(&(a)->x,-1,1)@p1 +| +atomic_long_add_unless(&(a)->x,-1,1)@p1 +| +atomic64_add_unless(&(a)->x,-1,1)@p1 +) + + at script:python depends on report@ +p1 << r2.p1; +@@ +msg = "atomic_add_unless" +coccilib.report.print_report(p1[0], msg) + + at r3 exists@ +identifier x; +position p1; +@@ + +( +x = atomic_add_return at p1(-1, ...); +| +x = atomic_long_add_return at p1(-1, ...); +| +x = atomic64_add_return at p1(-1, ...); +) + + at script:python depends on report@ +p1 << r3.p1; +@@ +msg = "x = atomic_add_return(-1, ...)" +coccilib.report.print_report(p1[0], msg) + + --Quoted 2 lines
2.7.4
--
Kees Cook
Pixel Security
2018-06-15 14:06 GMT+09:00 Julia Lawall <[email protected]>:
Quoted 15 lines · Kees Cook
Quoted 12 lines
Quoted 6 lines · Elena Reshetova
atomic_as_refcounter.cocciscript allows detecting cases whenrefcount_ttype and API should be used instead ofatomic_t.Signed-off-by Elena Reshetova <[email protected]>Acked-by Julia Lawall <[email protected]>Reviewed-by Kees Cook <[email protected]>
Quoted 8 lines
Quoted 4 lines
Oops, I think this got lost. Who can take this patch? I thought Julia ran the
scripts/coccinelle/tree, but looking at git log, it looks more like it's Masahiro? Either way, let's get this in the tree. Who can take it?Masahiro takes patches.
julia
Somehow I missed this one.
Now, applied to linux-kbuild. Thanks for the reminder.
(If Julia hosted a git repository, that would be better, though.)
Signature · 2 lines
Best Regards Masahiro Yamada
cover Coccinelle: atomic_as_refcounter: Improvements for source code search specifications
From: Markus Elfring <[email protected]>
Date: Tue, 3 Jul 2018 09:15:26 +0200
This source code search pattern was programmed in the way that some implementation details could be improved further. I suggest to avoid unnecessary code repetition also in this script for the semantic patch language.
Markus Elfring (6):
Omit placeholder specifications from two SmPL constraints Optimise a disjunction in the first SmPL rule Use type “expression” for another metavariable Replace disjunction by a constraint in two SmPL rules Use nested disjunctions in two SmPL rules
Use format strings directly in SmPL rules
.../coccinelle/api/atomic_as_refcounter.cocci | 104 +++++++----------- 1 file changed, 39 insertions(+), 65 deletions(-)
Signature · 1 line
2.18.0
PATCH 1/6 Coccinelle: atomic_as_refcounter: Omit placeholder specifications from two SmPL constraints
From: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 16:33:46 +0200
A string was enclosed by placeholder specifications for regular expressions as constraints for metavariables in a script for the semantic patch language.
The desired search functionality can be achieved also without the notation “.*”. Thus delete it.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +2 -3
@@ -12,7 +12,7 @@ virtual report @r1 exists@ identifier a, x; position p1, p2; -identifier fname =~ ".*free.*"; +identifier fname =~ "free"; identifier fname2 =~ ".*destroy.*"; identifier fname3 =~ ".*del.*"; identifier fname4 =~ ".*queue_work.*";
@@ -60,8 +60,7 @@ coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r4 exists@ identifier a, x, y; position p1, p2; -identifier fname =~ ".*free.*"; - +identifier fname =~ "free"; @@ (
Signature · 1 line
2.18.0
PATCH 2/6 Coccinelle: atomic_as_refcounter: Optimise a disjunction in the first SmPL rule
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 17:14:01 +0200
The names “fname2” till “fname6” do not care for different function parameters in the disjunction at the end of a rule in a script for the semantic patch language.
Thus reduce this disjunction by using a regular expression with an alternation for an optimised constraint.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +1 -14
@@ -13,12 +13,7 @@ virtual report identifier a, x; position p1, p2; identifier fname =~ "free"; -identifier fname2 =~ ".*destroy.*"; -identifier fname3 =~ ".*del.*"; -identifier fname4 =~ ".*queue_work.*"; -identifier fname5 =~ ".*schedule_work.*"; -identifier fname6 =~ ".*call_rcu.*"; - +identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; @@ (
@@ -39,14 +34,6 @@ identifier fname6 =~ ".*call_rcu.*"; fname@p2(a, ...); | fname2@p2(...); -| - fname3@p2(...); -| - fname4@p2(...); -| - fname5@p2(...); -| - fname6@p2(...); )
Signature · 1 line
2.18.0
PATCH 3/6 Coccinelle: atomic_as_refcounter: Use type “expression” for another metavariable
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 17:55:27 +0200
The metavariable “a” is enclosed by parentheses in three rules of a script for the semantic patch language.
Replace its type by “expression” so that the corresponding source code search becomes more powerful.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +6 -3
@@ -10,7 +10,8 @@ virtual report @r1 exists@ -identifier a, x; +expression a; +identifier x; position p1, p2; identifier fname =~ "free"; identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)";
@@ -45,7 +46,8 @@ msg = "atomic_dec_and_test variation before object free at line %s." coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r4 exists@ -identifier a, x, y; +expression a; +identifier x, y; position p1, p2; identifier fname =~ "free"; @@
@@ -77,7 +79,8 @@ msg = "atomic_dec_and_test variation before object free at line %s." coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r2 exists@ -identifier a, x; +expression a; +identifier x; position p1; @@
Signature · 1 line
2.18.0
PATCH 4/6 Coccinelle: atomic_as_refcounter: Replace disjunction by a constraint in two SmPL rules
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 18:45:15 +0200
Three function names were specified for a search of function calls by the means of a disjunction in two rules of a script for the semantic patch language.
Use a regular expression as a constraint for this source code search pattern instead so that duplication of SmPL code can be avoided.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +5 -16
@@ -80,17 +80,11 @@ coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r2 exists@ expression a; -identifier x; +identifier F =~ "^atomic(?:64|_long)?_add_unless$", x; position p1; @@ -( -atomic_add_unless(&(a)->x,-1,1)@p1 -| -atomic_long_add_unless(&(a)->x,-1,1)@p1 -| -atomic64_add_unless(&(a)->x,-1,1)@p1 -) + F(&(a)->x, -1, 1)@p1 @script:python depends on report@ p1 << r2.p1;
@@ -99,17 +93,12 @@ msg = "atomic_add_unless" coccilib.report.print_report(p1[0], msg) @r3 exists@ -identifier x; +expression E; +identifier F =~ "^atomic(?:64|_long)?_add_return$"; position p1; @@ -( -x = atomic_add_return@p1(-1, ...); -| -x = atomic_long_add_return@p1(-1, ...); -| -x = atomic64_add_return@p1(-1, ...); -) + E = F@p1(-1, ...); @script:python depends on report@ p1 << r3.p1;
Signature · 1 line
2.18.0
PATCH 5/6 Coccinelle: atomic_as_refcounter: Use nested disjunctions in two SmPL rules
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 19:21:28 +0200
Six function names were specified for a search of function calls by the means of a disjunction in two rules of a script for the semantic patch language.
Refactor them into two groups so that the SmPL code repetition could be reduced a bit.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +16 -20
@@ -18,17 +18,15 @@ identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; @@ ( - atomic_dec_and_test@p1(&(a)->x) +(atomic_dec_and_test@p1 +|atomic_long_dec_and_test@p1 +|atomic64_dec_and_test@p1 +|local_dec_and_test@p1 +) (&(a)->x) | - atomic_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_test@p1(&(a)->x) -| - atomic64_dec_and_test@p1(&(a)->x) -| - local_dec_and_test@p1(&(a)->x) +(atomic_dec_and_lock@p1 +|atomic_long_dec_and_lock@p1 +) (&(a)->x, ...) ) ... (
@@ -53,17 +51,15 @@ identifier fname =~ "free"; @@ ( - atomic_dec_and_test@p1(&(a)->x) -| - atomic_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_test@p1(&(a)->x) -| - atomic64_dec_and_test@p1(&(a)->x) +(atomic_dec_and_test@p1 +|atomic_long_dec_and_test@p1 +|atomic64_dec_and_test@p1 +|local_dec_and_test@p1 +) (&(a)->x) | - local_dec_and_test@p1(&(a)->x) +(atomic_dec_and_lock@p1 +|atomic_long_dec_and_lock@p1 +) (&(a)->x, ...) ) ... y=a
Signature · 1 line
2.18.0
PATCH 6/6 Coccinelle: atomic_as_refcounter: Use format strings directly in SmPL rules
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 2 Jul 2018 19:46:45 +0200
Format strings were always assigned to the Python variable “msg” before they were used in four rules of a script for the semantic patch language.
Delete these extra variables so that the specified string objects are directly used for the desired data output.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +8 -8
@@ -40,8 +40,9 @@ identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; p1 << r1.p1; p2 << r1.p2; @@ -msg = "atomic_dec_and_test variation before object free at line %s." -coccilib.report.print_report(p1[0], msg % (p2[0].line)) +coccilib.report.print_report(p1[0], + "atomic_dec_and_test variation before object free at line %s." + % (p2[0].line)) @r4 exists@ expression a;
@@ -71,8 +72,9 @@ fname@p2(y, ...); p1 << r4.p1; p2 << r4.p2; @@ -msg = "atomic_dec_and_test variation before object free at line %s." -coccilib.report.print_report(p1[0], msg % (p2[0].line)) +coccilib.report.print_report(p1[0], + "atomic_dec_and_test variation before object free at line %s." + % (p2[0].line)) @r2 exists@ expression a;
@@ -85,8 +87,7 @@ position p1; @script:python depends on report@ p1 << r2.p1; @@ -msg = "atomic_add_unless" -coccilib.report.print_report(p1[0], msg) +coccilib.report.print_report(p1[0], "atomic_add_unless") @r3 exists@ expression E;
@@ -99,5 +100,4 @@ position p1; @script:python depends on report@ p1 << r3.p1; @@ -msg = "x = atomic_add_return(-1, ...)" -coccilib.report.print_report(p1[0], msg) +coccilib.report.print_report(p1[0], "x = atomic_add_return(-1, ...)")
Signature · 1 line
2.18.0
PATCH 1/1 Coccinelle: atomic_as_refcounter: Merge two SmPL rules
From: Markus Elfring <[email protected]>
Date: Wed, 1 Aug 2018 14:16:01 +0200
Two rules of a script for the semantic patch language displayed the same message.
Thus reduce duplicate SmPL code so that the desired data processing is achieved by another nested SmPL disjunction in a single SmPL rule.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 50 ++++--------------- 1 file changed, 10 insertions(+), 40 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +10 -40
@@ -11,12 +11,11 @@ virtual report @r1 exists@ expression a; -identifier x; position p1, p2; -identifier fname =~ "free"; -identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; +identifier x, y, + fname =~ "free", + fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; @@ - ( (atomic_dec_and_test@p1 |atomic_long_dec_and_test@p1
@@ -28,14 +27,17 @@ identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; |atomic_long_dec_and_lock@p1 ) (&(a)->x, ...) ) -... + ... ( - fname@p2(a, ...); +(fname@p2(a, ...) +|fname2@p2(...) +); | - fname2@p2(...); + y = a + ... + fname@p2(y, ...); ) - @script:python depends on report@ p1 << r1.p1; p2 << r1.p2;
@@ -44,38 +46,6 @@ coccilib.report.print_report(p1[0], "atomic_dec_and_test variation before object free at line %s." % (p2[0].line)) -@r4 exists@ -expression a; -identifier x, y; -position p1, p2; -identifier fname =~ "free"; -@@ - -( -(atomic_dec_and_test@p1 -|atomic_long_dec_and_test@p1 -|atomic64_dec_and_test@p1 -|local_dec_and_test@p1 -) (&(a)->x) -| -(atomic_dec_and_lock@p1 -|atomic_long_dec_and_lock@p1 -) (&(a)->x, ...) -) -... -y=a -... -fname@p2(y, ...); - - -@script:python depends on report@ -p1 << r4.p1; -p2 << r4.p2; -@@ -coccilib.report.print_report(p1[0], - "atomic_dec_and_test variation before object free at line %s." - % (p2[0].line)) - @r2 exists@ expression a; identifier F =~ "^atomic(?:64|_long)?_add_unless$", x;
Signature · 1 line
2.18.0
v2 cover Coccinelle: atomic_as_refcounter: Adjustments for source code search specifications
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 19:15:26 +0200
This source code search pattern was programmed in the way that some implementation details could be improved further. I suggest to avoid unnecessary code repetition also in this script for the semantic patch language.
Markus Elfring (8):
Delete an unnecessary SmPL rule
Omit placeholder specifications from a SmPL constraint Optimise a disjunction in the first SmPL rule Use type “expression” for another metavariable Replace disjunction by a constraint in two SmPL rules Use nested disjunctions in the first SmPL rule Use string literals directly in two SmPL rules Use format string directly in the first SmPL rule
---
V2:
Possible changes were recombined after the deletion of a questionable SmPL rule.
.../coccinelle/api/atomic_as_refcounter.cocci | 108 ++++-------------- 1 file changed, 24 insertions(+), 84 deletions(-)
Signature · 1 line
2.18.0
PATCH v2 1/8 Coccinelle: atomic_as_refcounter: Delete an unnecessary SmPL rule
From: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 17:15:26 +0200
A search was specified in the rule “r4” for function calls which should be found by the rule “r1” already in a script for the semantic patch language. It is useless to repeat a source code search there just to determine that a variable assignment was performed also for an input parameter of such a function call (while the same message will be displayed so far). Thus delete an inappropriate search specification.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 33 ------------------- 1 file changed, 33 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +0 -33
@@ -57,39 +57,6 @@ p2 << r1.p2; msg = "atomic_dec_and_test variation before object free at line %s." coccilib.report.print_report(p1[0], msg % (p2[0].line)) -@r4 exists@ -identifier a, x, y; -position p1, p2; -identifier fname =~ ".*free.*"; - -@@ - -( - atomic_dec_and_test@p1(&(a)->x) -| - atomic_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_test@p1(&(a)->x) -| - atomic64_dec_and_test@p1(&(a)->x) -| - local_dec_and_test@p1(&(a)->x) -) -... -y=a -... -fname@p2(y, ...); - - -@script:python depends on report@ -p1 << r4.p1; -p2 << r4.p2; -@@ -msg = "atomic_dec_and_test variation before object free at line %s." -coccilib.report.print_report(p1[0], msg % (p2[0].line)) - @r2 exists@ identifier a, x; position p1;
Signature · 1 line
2.18.0
PATCH v2 2/8 Coccinelle: atomic_as_refcounter: Omit placeholder specifications from a SmPL constraint
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 17:34:56 +0200
A string was enclosed by placeholder specifications for regular expressions as constraints for metavariables in a script for the semantic patch language.
The desired search functionality can be achieved also without the notation “.*”. Thus delete it at a specific place.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +1 -1
@@ -12,7 +12,7 @@ virtual report @r1 exists@ identifier a, x; position p1, p2; -identifier fname =~ ".*free.*"; +identifier fname =~ "free"; identifier fname2 =~ ".*destroy.*"; identifier fname3 =~ ".*del.*"; identifier fname4 =~ ".*queue_work.*";
Signature · 1 line
2.18.0
PATCH v2 3/8 Coccinelle: atomic_as_refcounter: Optimise a disjunction in the first SmPL rule
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 17:43:49 +0200
The variables “fname2” till “fname6” do not care for different function parameters in the disjunction at the end of a rule in a script for the semantic patch language.
Thus reduce this disjunction by using a regular expression with an alternation for an optimised constraint.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +1 -14
@@ -13,12 +13,7 @@ virtual report identifier a, x; position p1, p2; identifier fname =~ "free"; -identifier fname2 =~ ".*destroy.*"; -identifier fname3 =~ ".*del.*"; -identifier fname4 =~ ".*queue_work.*"; -identifier fname5 =~ ".*schedule_work.*"; -identifier fname6 =~ ".*call_rcu.*"; - +identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; @@ (
@@ -39,14 +34,6 @@ identifier fname6 =~ ".*call_rcu.*"; fname@p2(a, ...); | fname2@p2(...); -| - fname3@p2(...); -| - fname4@p2(...); -| - fname5@p2(...); -| - fname6@p2(...); )
Signature · 1 line
2.18.0
PATCH v2 4/8 Coccinelle: atomic_as_refcounter: Use type “expression” for another metavariable
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 18:00:54 +0200
The metavariable “a” is enclosed by parentheses in two rules of a script for the semantic patch language.
Replace its type by “expression” so that the corresponding source code search becomes more powerful.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +4 -2
@@ -10,7 +10,8 @@ virtual report @r1 exists@ -identifier a, x; +expression a; +identifier x; position p1, p2; identifier fname =~ "free"; identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)";
@@ -45,7 +46,8 @@ msg = "atomic_dec_and_test variation before object free at line %s." coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r2 exists@ -identifier a, x; +expression a; +identifier x; position p1; @@
Signature · 1 line
2.18.0
PATCH v2 5/8 Coccinelle: atomic_as_refcounter: Replace disjunction by a constraint in two SmPL rules
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 18:12:10 +0200
Three function names were specified for a search of function calls by the means of a disjunction in two rules of a script for the semantic patch language.
Use a regular expression as a constraint for this source code search pattern instead so that duplication of SmPL code can be avoided.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +5 -18
@@ -47,17 +47,10 @@ coccilib.report.print_report(p1[0], msg % (p2[0].line)) @r2 exists@ expression a; -identifier x; +identifier F =~ "^atomic(?:64|_long)?_add_unless$", x; position p1; @@ - -( -atomic_add_unless(&(a)->x,-1,1)@p1 -| -atomic_long_add_unless(&(a)->x,-1,1)@p1 -| -atomic64_add_unless(&(a)->x,-1,1)@p1 -) + F(&(a)->x, -1, 1)@p1 @script:python depends on report@ p1 << r2.p1;
@@ -66,17 +59,11 @@ msg = "atomic_add_unless" coccilib.report.print_report(p1[0], msg) @r3 exists@ -identifier x; +expression E; +identifier F =~ "^atomic(?:64|_long)?_add_return$"; position p1; @@ - -( -x = atomic_add_return@p1(-1, ...); -| -x = atomic_long_add_return@p1(-1, ...); -| -x = atomic64_add_return@p1(-1, ...); -) + E = F@p1(-1, ...); @script:python depends on report@ p1 << r3.p1;
Signature · 1 line
2.18.0
PATCH v2 6/8 Coccinelle: atomic_as_refcounter: Use nested disjunctions in the first SmPL rule
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 18:22:53 +0200
Six function names were specified for a search of function calls by the means of a disjunction in the rule of a script for the semantic patch language.
Refactor them into two groups so that the SmPL code repetition could be reduced a bit.
--- .../coccinelle/api/atomic_as_refcounter.cocci | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +8 -10
@@ -18,17 +18,15 @@ identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; @@ ( - atomic_dec_and_test@p1(&(a)->x) +(atomic_dec_and_test@p1 +|atomic_long_dec_and_test@p1 +|atomic64_dec_and_test@p1 +|local_dec_and_test@p1 +) (&(a)->x) | - atomic_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_lock@p1(&(a)->x, ...) -| - atomic_long_dec_and_test@p1(&(a)->x) -| - atomic64_dec_and_test@p1(&(a)->x) -| - local_dec_and_test@p1(&(a)->x) +(atomic_dec_and_lock@p1 +|atomic_long_dec_and_lock@p1 +) (&(a)->x, ...) ) ... (
Signature · 1 line
2.18.0
PATCH v2 7/8 Coccinelle: atomic_as_refcounter: Use string literals directly in two SmPL rules
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 18:36:41 +0200
String literals were always assigned to the Python variable “msg” before they were used in two rules of a script for the semantic patch language.
Delete these extra variables so that the specified string objects are directly used for the desired data output.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +2 -4
@@ -53,8 +53,7 @@ position p1; @script:python depends on report@ p1 << r2.p1; @@ -msg = "atomic_add_unless" -coccilib.report.print_report(p1[0], msg) +coccilib.report.print_report(p1[0], "atomic_add_unless") @r3 exists@ expression E;
@@ -66,5 +65,4 @@ position p1; @script:python depends on report@ p1 << r3.p1; @@ -msg = "x = atomic_add_return(-1, ...)" -coccilib.report.print_report(p1[0], msg) +coccilib.report.print_report(p1[0], "x = atomic_add_return(-1, ...)")
Signature · 1 line
2.18.0
PATCH v2 8/8 Coccinelle: atomic_as_refcounter: Use format string directly in the first SmPL rule
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Mon, 16 Jul 2018 18:45:55 +0200
A format string was assigned to the Python variable “msg” before it was used in a rule of a script for the semantic patch language. Delete this extra variable so that the specified string object is directly used for the desired data output.
--- scripts/coccinelle/api/atomic_as_refcounter.cocci | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/coccinelle/api/atomic_as_refcounter.cocci +3 -2
@@ -40,8 +40,9 @@ identifier fname2 =~ "(?:call_rcu|de(?:l|stroy)|(?:queue|schedule)_work)"; p1 << r1.p1; p2 << r1.p2; @@ -msg = "atomic_dec_and_test variation before object free at line %s." -coccilib.report.print_report(p1[0], msg % (p2[0].line)) +coccilib.report.print_report(p1[0], + "atomic_dec_and_test variation before object free at line %s." + % (p2[0].line)) @r2 exists@ expression a;
Signature · 1 line
2.18.0