Skip to content

Commit b56e535

Browse files
ereshetovamasahir0y
authored andcommitted
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. Signed-off-by: Elena Reshetova <[email protected]> Acked-by: Julia Lawall <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 9d3cce1 commit b56e535

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Check if refcount_t type and API should be used
2+
// instead of atomic_t type when dealing with refcounters
3+
//
4+
// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation
5+
//
6+
// Confidence: Moderate
7+
// URL: http://coccinelle.lip6.fr/
8+
// Options: --include-headers --very-quiet
9+
10+
virtual report
11+
12+
@r1 exists@
13+
identifier a, x;
14+
position p1, p2;
15+
identifier fname =~ ".*free.*";
16+
identifier fname2 =~ ".*destroy.*";
17+
identifier fname3 =~ ".*del.*";
18+
identifier fname4 =~ ".*queue_work.*";
19+
identifier fname5 =~ ".*schedule_work.*";
20+
identifier fname6 =~ ".*call_rcu.*";
21+
22+
@@
23+
24+
(
25+
atomic_dec_and_test@p1(&(a)->x)
26+
|
27+
atomic_dec_and_lock@p1(&(a)->x, ...)
28+
|
29+
atomic_long_dec_and_lock@p1(&(a)->x, ...)
30+
|
31+
atomic_long_dec_and_test@p1(&(a)->x)
32+
|
33+
atomic64_dec_and_test@p1(&(a)->x)
34+
|
35+
local_dec_and_test@p1(&(a)->x)
36+
)
37+
...
38+
(
39+
fname@p2(a, ...);
40+
|
41+
fname2@p2(...);
42+
|
43+
fname3@p2(...);
44+
|
45+
fname4@p2(...);
46+
|
47+
fname5@p2(...);
48+
|
49+
fname6@p2(...);
50+
)
51+
52+
53+
@script:python depends on report@
54+
p1 << r1.p1;
55+
p2 << r1.p2;
56+
@@
57+
msg = "atomic_dec_and_test variation before object free at line %s."
58+
coccilib.report.print_report(p1[0], msg % (p2[0].line))
59+
60+
@r4 exists@
61+
identifier a, x, y;
62+
position p1, p2;
63+
identifier fname =~ ".*free.*";
64+
65+
@@
66+
67+
(
68+
atomic_dec_and_test@p1(&(a)->x)
69+
|
70+
atomic_dec_and_lock@p1(&(a)->x, ...)
71+
|
72+
atomic_long_dec_and_lock@p1(&(a)->x, ...)
73+
|
74+
atomic_long_dec_and_test@p1(&(a)->x)
75+
|
76+
atomic64_dec_and_test@p1(&(a)->x)
77+
|
78+
local_dec_and_test@p1(&(a)->x)
79+
)
80+
...
81+
y=a
82+
...
83+
fname@p2(y, ...);
84+
85+
86+
@script:python depends on report@
87+
p1 << r4.p1;
88+
p2 << r4.p2;
89+
@@
90+
msg = "atomic_dec_and_test variation before object free at line %s."
91+
coccilib.report.print_report(p1[0], msg % (p2[0].line))
92+
93+
@r2 exists@
94+
identifier a, x;
95+
position p1;
96+
@@
97+
98+
(
99+
atomic_add_unless(&(a)->x,-1,1)@p1
100+
|
101+
atomic_long_add_unless(&(a)->x,-1,1)@p1
102+
|
103+
atomic64_add_unless(&(a)->x,-1,1)@p1
104+
)
105+
106+
@script:python depends on report@
107+
p1 << r2.p1;
108+
@@
109+
msg = "atomic_add_unless"
110+
coccilib.report.print_report(p1[0], msg)
111+
112+
@r3 exists@
113+
identifier x;
114+
position p1;
115+
@@
116+
117+
(
118+
x = atomic_add_return@p1(-1, ...);
119+
|
120+
x = atomic_long_add_return@p1(-1, ...);
121+
|
122+
x = atomic64_add_return@p1(-1, ...);
123+
)
124+
125+
@script:python depends on report@
126+
p1 << r3.p1;
127+
@@
128+
msg = "x = atomic_add_return(-1, ...)"
129+
coccilib.report.print_report(p1[0], msg)

0 commit comments

Comments
 (0)