]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | virtual patch | |
10 | virtual context | |
11 | virtual org | |
12 | virtual 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 |
18 | depends on patch && !context && !org && !report@ | |
19 | expression 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 | |
36 | depends on patch && !context && !org && !report@ | |
37 | expression 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 | |
54 | depends on patch && !context && !org && !report@ | |
55 | expression 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 | 84 | depends on patch && !context && !org && !report@ |
1b18d05c | 85 | expression 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 | 113 | expression t; |
c5eda8fd VT |
114 | identifier f; |
115 | position p; | |
116 | @@ | |
117 | ||
118 | f(...) { ... when any | |
1b18d05c | 119 | init_timer@p(\(&t\|t\)) |
c5eda8fd VT |
120 | ... when any |
121 | } | |
122 | ||
123 | @r2 exists@ | |
1b18d05c | 124 | expression r1.t; |
c5eda8fd | 125 | identifier g != r1.f; |
c5eda8fd VT |
126 | expression e8; |
127 | @@ | |
128 | ||
129 | g(...) { ... 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@ | |
138 | p << r1.p; | |
139 | @@ | |
140 | ||
141 | cocci.include_match(False) | |
142 | ||
143 | @r3 depends on patch && !context && !org && !report@ | |
1b18d05c | 144 | expression r1.t, func, e7; |
c5eda8fd VT |
145 | position 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 | |
173 | depends on !patch && (context || org || report)@ | |
174 | expression da, e, func; | |
175 | position 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 | 188 | depends on !patch && (context || org || report)@ |
c5eda8fd | 189 | expression a, b, e1, e2, e3, e4, e5; |
bc27b77d | 190 | position 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 |
207 | expression c, e6, e7; |
208 | position r1.p; | |
bc27b77d JL |
209 | position 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 | |
221 | depends on org@ | |
222 | j0 << match_immediate_function_data_after_init_timer_context.j0; | |
223 | j1 << match_immediate_function_data_after_init_timer_context.j1; | |
224 | j2 << match_immediate_function_data_after_init_timer_context.j2; | |
225 | @@ | |
226 | ||
227 | msg = "Use setup_timer function." | |
228 | coccilib.org.print_todo(j0[0], msg) | |
229 | coccilib.org.print_link(j1[0], "") | |
230 | coccilib.org.print_link(j2[0], "") | |
231 | ||
232 | @script:python match_function_and_data_after_init_timer_org depends on org@ | |
233 | j0 << match_function_and_data_after_init_timer_context.j0; | |
234 | j1 << match_function_and_data_after_init_timer_context.j1; | |
235 | j2 << match_function_and_data_after_init_timer_context.j2; | |
236 | @@ | |
237 | ||
238 | msg = "Use setup_timer function." | |
239 | coccilib.org.print_todo(j0[0], msg) | |
240 | coccilib.org.print_link(j1[0], "") | |
241 | coccilib.org.print_link(j2[0], "") | |
242 | ||
243 | @script:python r3_org depends on org@ | |
244 | j0 << r3_context.j0; | |
245 | j1 << r3_context.j1; | |
246 | @@ | |
247 | ||
248 | msg = "Use setup_timer function." | |
249 | coccilib.org.print_todo(j0[0], msg) | |
250 | coccilib.org.print_link(j1[0], "") | |
251 | ||
252 | // ---------------------------------------------------------------------------- | |
253 | ||
254 | @script:python match_immediate_function_data_after_init_timer_report | |
255 | depends on report@ | |
256 | j0 << match_immediate_function_data_after_init_timer_context.j0; | |
257 | j1 << match_immediate_function_data_after_init_timer_context.j1; | |
258 | @@ | |
259 | ||
260 | msg = "Use setup_timer function for function on line %s." % (j1[0].line) | |
261 | coccilib.report.print_report(j0[0], msg) | |
262 | ||
263 | @script:python match_function_and_data_after_init_timer_report depends on report@ | |
264 | j0 << match_function_and_data_after_init_timer_context.j0; | |
265 | j1 << match_function_and_data_after_init_timer_context.j1; | |
266 | @@ | |
267 | ||
268 | msg = "Use setup_timer function for function on line %s." % (j1[0].line) | |
269 | coccilib.report.print_report(j0[0], msg) | |
270 | ||
271 | @script:python r3_report depends on report@ | |
272 | j0 << r3_context.j0; | |
273 | j1 << r3_context.j1; | |
274 | @@ | |
275 | ||
276 | msg = "Use setup_timer function for function on line %s." % (j1[0].line) | |
277 | coccilib.report.print_report(j0[0], msg) |