]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/coccinelle/api/setup_timer.cocci
coccinelle: Improve setup_timer.cocci matching
[mirror_ubuntu-bionic-kernel.git] / scripts / coccinelle / api / setup_timer.cocci
CommitLineData
c5eda8fd
VT
1/// Use setup_timer function instead of initializing timer with the function
2/// and data fields
3// Confidence: High
4// Copyright: (C) 2016 Vaishali Thakkar, Oracle. GPLv2
1b18d05c 5// Copyright: (C) 2017 Kees Cook, Google. GPLv2
c5eda8fd
VT
6// Options: --no-includes --include-headers
7// Keywords: init_timer, setup_timer
8
9virtual patch
10virtual context
11virtual org
12virtual report
13
1b18d05c
KC
14// Match the common cases first to avoid Coccinelle parsing loops with
15// "... when" clauses.
16
c5eda8fd
VT
17@match_immediate_function_data_after_init_timer
18depends on patch && !context && !org && !report@
19expression e, func, da;
20@@
21
1b18d05c
KC
22-init_timer
23+setup_timer
24 ( \(&e\|e\)
25+, func, da
26 );
27(
28-\(e.function\|e->function\) = func;
29-\(e.data\|e->data\) = da;
30|
31-\(e.data\|e->data\) = da;
32-\(e.function\|e->function\) = func;
33)
34
35@match_immediate_function_data_before_init_timer
36depends on patch && !context && !org && !report@
37expression e, func, da;
38@@
39
40(
41-\(e.function\|e->function\) = func;
42-\(e.data\|e->data\) = da;
43|
44-\(e.data\|e->data\) = da;
45-\(e.function\|e->function\) = func;
46)
47-init_timer
48+setup_timer
49 ( \(&e\|e\)
50+, func, da
51 );
52
53@match_function_and_data_after_init_timer
54depends on patch && !context && !org && !report@
55expression e, e2, e3, e4, e5, func, da;
56@@
c5eda8fd 57
1b18d05c
KC
58-init_timer
59+setup_timer
60 ( \(&e\|e\)
61+, func, da
62 );
63 ... when != func = e2
64 when != da = e3
c5eda8fd
VT
65(
66-e.function = func;
1b18d05c 67... when != da = e4
c5eda8fd
VT
68-e.data = da;
69|
1b18d05c
KC
70-e->function = func;
71... when != da = e4
72-e->data = da;
73|
c5eda8fd 74-e.data = da;
1b18d05c 75... when != func = e5
c5eda8fd 76-e.function = func;
1b18d05c
KC
77|
78-e->data = da;
79... when != func = e5
80-e->function = func;
c5eda8fd
VT
81)
82
1b18d05c 83@match_function_and_data_before_init_timer
c5eda8fd 84depends on patch && !context && !org && !report@
1b18d05c 85expression e, e2, e3, e4, e5, func, da;
c5eda8fd 86@@
c5eda8fd 87(
1b18d05c
KC
88-e.function = func;
89... when != da = e4
90-e.data = da;
91|
92-e->function = func;
93... when != da = e4
94-e->data = da;
c5eda8fd 95|
1b18d05c
KC
96-e.data = da;
97... when != func = e5
98-e.function = func;
99|
100-e->data = da;
101... when != func = e5
102-e->function = func;
c5eda8fd 103)
1b18d05c
KC
104... when != func = e2
105 when != da = e3
106-init_timer
107+setup_timer
108 ( \(&e\|e\)
109+, func, da
110 );
c5eda8fd
VT
111
112@r1 exists@
1b18d05c 113expression t;
c5eda8fd
VT
114identifier f;
115position p;
116@@
117
118f(...) { ... when any
1b18d05c 119 init_timer@p(\(&t\|t\))
c5eda8fd
VT
120 ... when any
121}
122
123@r2 exists@
1b18d05c 124expression r1.t;
c5eda8fd 125identifier g != r1.f;
c5eda8fd
VT
126expression e8;
127@@
128
129g(...) { ... when any
1b18d05c 130 \(t.data\|t->data\) = e8
c5eda8fd
VT
131 ... when any
132}
133
134// It is dangerous to use setup_timer if data field is initialized
135// in another function.
136
137@script:python depends on r2@
138p << r1.p;
139@@
140
141cocci.include_match(False)
142
143@r3 depends on patch && !context && !org && !report@
1b18d05c 144expression r1.t, func, e7;
c5eda8fd
VT
145position r1.p;
146@@
147
1b18d05c
KC
148(
149-init_timer@p(&t);
150+setup_timer(&t, func, 0UL);
151... when != func = e7
152-t.function = func;
153|
154-t.function = func;
155... when != func = e7
156-init_timer@p(&t);
157+setup_timer(&t, func, 0UL);
158|
159-init_timer@p(t);
160+setup_timer(t, func, 0UL);
161... when != func = e7
162-t->function = func;
163|
164-t->function = func;
165... when != func = e7
166-init_timer@p(t);
167+setup_timer(t, func, 0UL);
168)
c5eda8fd
VT
169
170// ----------------------------------------------------------------------------
171
172@match_immediate_function_data_after_init_timer_context
173depends on !patch && (context || org || report)@
174expression da, e, func;
175position j0, j1, j2;
176@@
177
178* init_timer@j0 (&e);
179(
180* e@j1.function = func;
181* e@j2.data = da;
182|
183* e@j1.data = da;
184* e@j2.function = func;
185)
186
187@match_function_and_data_after_init_timer_context
bc27b77d 188depends on !patch && (context || org || report)@
c5eda8fd 189expression a, b, e1, e2, e3, e4, e5;
bc27b77d 190position j0 != match_immediate_function_data_after_init_timer_context.j0,j1,j2;
c5eda8fd
VT
191@@
192
193* init_timer@j0 (&e1);
194... when != a = e2
195 when != b = e3
196(
197* e1@j1.function = a;
198... when != b = e4
199* e1@j2.data = b;
200|
201* e1@j1.data = b;
202... when != a = e5
203* e1@j2.function = a;
204)
205
bc27b77d 206@r3_context depends on !patch && (context || org || report)@
c5eda8fd
VT
207expression c, e6, e7;
208position r1.p;
bc27b77d
JL
209position j0 !=
210 {match_immediate_function_data_after_init_timer_context.j0,
211 match_function_and_data_after_init_timer_context.j0}, j1;
c5eda8fd
VT
212@@
213
214* init_timer@j0@p (&e6);
215... when != c = e7
216* e6@j1.function = c;
217
218// ----------------------------------------------------------------------------
219
220@script:python match_immediate_function_data_after_init_timer_org
221depends on org@
222j0 << match_immediate_function_data_after_init_timer_context.j0;
223j1 << match_immediate_function_data_after_init_timer_context.j1;
224j2 << match_immediate_function_data_after_init_timer_context.j2;
225@@
226
227msg = "Use setup_timer function."
228coccilib.org.print_todo(j0[0], msg)
229coccilib.org.print_link(j1[0], "")
230coccilib.org.print_link(j2[0], "")
231
232@script:python match_function_and_data_after_init_timer_org depends on org@
233j0 << match_function_and_data_after_init_timer_context.j0;
234j1 << match_function_and_data_after_init_timer_context.j1;
235j2 << match_function_and_data_after_init_timer_context.j2;
236@@
237
238msg = "Use setup_timer function."
239coccilib.org.print_todo(j0[0], msg)
240coccilib.org.print_link(j1[0], "")
241coccilib.org.print_link(j2[0], "")
242
243@script:python r3_org depends on org@
244j0 << r3_context.j0;
245j1 << r3_context.j1;
246@@
247
248msg = "Use setup_timer function."
249coccilib.org.print_todo(j0[0], msg)
250coccilib.org.print_link(j1[0], "")
251
252// ----------------------------------------------------------------------------
253
254@script:python match_immediate_function_data_after_init_timer_report
255depends on report@
256j0 << match_immediate_function_data_after_init_timer_context.j0;
257j1 << match_immediate_function_data_after_init_timer_context.j1;
258@@
259
260msg = "Use setup_timer function for function on line %s." % (j1[0].line)
261coccilib.report.print_report(j0[0], msg)
262
263@script:python match_function_and_data_after_init_timer_report depends on report@
264j0 << match_function_and_data_after_init_timer_context.j0;
265j1 << match_function_and_data_after_init_timer_context.j1;
266@@
267
268msg = "Use setup_timer function for function on line %s." % (j1[0].line)
269coccilib.report.print_report(j0[0], msg)
270
271@script:python r3_report depends on report@
272j0 << r3_context.j0;
273j1 << r3_context.j1;
274@@
275
276msg = "Use setup_timer function for function on line %s." % (j1[0].line)
277coccilib.report.print_report(j0[0], msg)