1///
2/// Use zeroing allocator rather than allocator followed by memset with 0
3///
4/// This considers some simple cases that are common and easy to validate
5/// Note in particular that there are no ...s in the rule, so all of the
6/// matched code has to be contiguous
7///
8// Confidence: High
9// Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.  GPLv2.
10// Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.  GPLv2.
11// Copyright: (C) 2017 Himanshu Jha GPLv2.
12// URL: http://coccinelle.lip6.fr/rules/kzalloc.html
13// Options: --no-includes --include-headers
14//
15// Keywords: kmalloc, kzalloc
16// Version min: < 2.6.12 kmalloc
17// Version min:   2.6.14 kzalloc
18//
19
20virtual context
21virtual patch
22virtual org
23virtual report
24
25//----------------------------------------------------------
26//  For context mode
27//----------------------------------------------------------
28
29@depends on context@
30type T, T2;
31expression x;
32expression E1;
33statement S;
34@@
35
36* x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
37  kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
38  devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
39  if ((x==NULL) || ...) S
40* memset((T2)x,0,E1);
41
42//----------------------------------------------------------
43//  For patch mode
44//----------------------------------------------------------
45
46@depends on patch@
47type T, T2;
48expression x;
49expression E1,E2,E3,E4;
50statement S;
51@@
52
53(
54- x = kmalloc(E1,E2);
55+ x = kzalloc(E1,E2);
56|
57- x = (T *)kmalloc(E1,E2);
58+ x = kzalloc(E1,E2);
59|
60- x = (T)kmalloc(E1,E2);
61+ x = (T)kzalloc(E1,E2);
62|
63- x = vmalloc(E1);
64+ x = vzalloc(E1);
65|
66- x = (T *)vmalloc(E1);
67+ x = vzalloc(E1);
68|
69- x = (T)vmalloc(E1);
70+ x = (T)vzalloc(E1);
71|
72- x = kmalloc_node(E1,E2,E3);
73+ x = kzalloc_node(E1,E2,E3);
74|
75- x = (T *)kmalloc_node(E1,E2,E3);
76+ x = kzalloc_node(E1,E2,E3);
77|
78- x = (T)kmalloc_node(E1,E2,E3);
79+ x = (T)kzalloc_node(E1,E2,E3);
80|
81- x = kmem_cache_alloc(E3,E4);
82+ x = kmem_cache_zalloc(E3,E4);
83|
84- x = (T *)kmem_cache_alloc(E3,E4);
85+ x = kmem_cache_zalloc(E3,E4);
86|
87- x = (T)kmem_cache_alloc(E3,E4);
88+ x = (T)kmem_cache_zalloc(E3,E4);
89|
90- x = kmem_alloc(E1,E2);
91+ x = kmem_zalloc(E1,E2);
92|
93- x = (T *)kmem_alloc(E1,E2);
94+ x = kmem_zalloc(E1,E2);
95|
96- x = (T)kmem_alloc(E1,E2);
97+ x = (T)kmem_zalloc(E1,E2);
98|
99- x = devm_kmalloc(E2,E1,E3);
100+ x = devm_kzalloc(E2,E1,E3);
101|
102- x = (T *)devm_kmalloc(E2,E1,E3);
103+ x = devm_kzalloc(E2,E1,E3);
104|
105- x = (T)devm_kmalloc(E2,E1,E3);
106+ x = (T)devm_kzalloc(E2,E1,E3);
107|
108- x = kvmalloc(E1,E2);
109+ x = kvzalloc(E1,E2);
110|
111- x = (T *)kvmalloc(E1,E2);
112+ x = kvzalloc(E1,E2);
113|
114- x = (T)kvmalloc(E1,E2);
115+ x = (T)kvzalloc(E1,E2);
116|
117- x = kvmalloc_node(E1,E2,E3);
118+ x = kvzalloc_node(E1,E2,E3);
119|
120- x = (T *)kvmalloc_node(E1,E2,E3);
121+ x = kvzalloc_node(E1,E2,E3);
122|
123- x = (T)kvmalloc_node(E1,E2,E3);
124+ x = (T)kvzalloc_node(E1,E2,E3);
125)
126  if ((x==NULL) || ...) S
127- memset((T2)x,0,E1);
128
129//----------------------------------------------------------
130//  For org mode
131//----------------------------------------------------------
132
133@r depends on org || report@
134type T, T2;
135expression x;
136expression E1,E2;
137statement S;
138position p;
139@@
140
141 x = (T)kmalloc@p(E1,E2);
142 if ((x==NULL) || ...) S
143 memset((T2)x,0,E1);
144
145@script:python depends on org@
146p << r.p;
147x << r.x;
148@@
149
150msg="%s" % (x)
151msg_safe=msg.replace("[","@(").replace("]",")")
152coccilib.org.print_todo(p[0], msg_safe)
153
154@script:python depends on report@
155p << r.p;
156x << r.x;
157@@
158
159msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
160coccilib.report.print_report(p[0], msg)
161
162//-----------------------------------------------------------------
163@r1 depends on org || report@
164type T, T2;
165expression x;
166expression E1;
167statement S;
168position p;
169@@
170
171 x = (T)vmalloc@p(E1);
172 if ((x==NULL) || ...) S
173 memset((T2)x,0,E1);
174
175@script:python depends on org@
176p << r1.p;
177x << r1.x;
178@@
179
180msg="%s" % (x)
181msg_safe=msg.replace("[","@(").replace("]",")")
182coccilib.org.print_todo(p[0], msg_safe)
183
184@script:python depends on report@
185p << r1.p;
186x << r1.x;
187@@
188
189msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
190coccilib.report.print_report(p[0], msg)
191
192//-----------------------------------------------------------------
193@r2 depends on org || report@
194type T, T2;
195expression x;
196expression E1,E2,E3,E4;
197statement S;
198position p;
199@@
200
201 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
202 if ((x==NULL) || ...) S
203 memset((T2)x,0,E1);
204
205@script:python depends on org@
206p << r2.p;
207x << r2.x;
208@@
209
210msg="%s" % (x)
211msg_safe=msg.replace("[","@(").replace("]",")")
212coccilib.org.print_todo(p[0], msg_safe)
213
214@script:python depends on report@
215p << r2.p;
216x << r2.x;
217@@
218
219msg="WARNING: dma_alloc_coherent use in %s already zeroes out memory,  so memset is not needed" % (x)
220coccilib.report.print_report(p[0], msg)
221
222//-----------------------------------------------------------------
223@r3 depends on org || report@
224type T, T2;
225expression x;
226expression E1,E2,E3;
227statement S;
228position p;
229@@
230
231 x = (T)kmalloc_node@p(E1,E2,E3);
232 if ((x==NULL) || ...) S
233 memset((T2)x,0,E1);
234
235@script:python depends on org@
236p << r3.p;
237x << r3.x;
238@@
239
240msg="%s" % (x)
241msg_safe=msg.replace("[","@(").replace("]",")")
242coccilib.org.print_todo(p[0], msg_safe)
243
244@script:python depends on report@
245p << r3.p;
246x << r3.x;
247@@
248
249msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
250coccilib.report.print_report(p[0], msg)
251
252//-----------------------------------------------------------------
253@r4 depends on org || report@
254type T, T2;
255expression x;
256expression E1,E2,E3;
257statement S;
258position p;
259@@
260
261 x = (T)kmem_cache_alloc@p(E2,E3);
262 if ((x==NULL) || ...) S
263 memset((T2)x,0,E1);
264
265@script:python depends on org@
266p << r4.p;
267x << r4.x;
268@@
269
270msg="%s" % (x)
271msg_safe=msg.replace("[","@(").replace("]",")")
272coccilib.org.print_todo(p[0], msg_safe)
273
274@script:python depends on report@
275p << r4.p;
276x << r4.x;
277@@
278
279msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
280coccilib.report.print_report(p[0], msg)
281
282//-----------------------------------------------------------------
283@r5 depends on org || report@
284type T, T2;
285expression x;
286expression E1,E2;
287statement S;
288position p;
289@@
290
291 x = (T)kmem_alloc@p(E1,E2);
292 if ((x==NULL) || ...) S
293 memset((T2)x,0,E1);
294
295@script:python depends on org@
296p << r5.p;
297x << r5.x;
298@@
299
300msg="%s" % (x)
301msg_safe=msg.replace("[","@(").replace("]",")")
302coccilib.org.print_todo(p[0], msg_safe)
303
304@script:python depends on report@
305p << r5.p;
306x << r5.x;
307@@
308
309msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
310coccilib.report.print_report(p[0], msg)
311
312//-----------------------------------------------------------------
313@r6 depends on org || report@
314type T, T2;
315expression x;
316expression E1,E2,E3;
317statement S;
318position p;
319@@
320
321 x = (T)devm_kmalloc@p(E2,E1,E3);
322 if ((x==NULL) || ...) S
323 memset((T2)x,0,E1);
324
325@script:python depends on org@
326p << r6.p;
327x << r6.x;
328@@
329
330msg="%s" % (x)
331msg_safe=msg.replace("[","@(").replace("]",")")
332coccilib.org.print_todo(p[0], msg_safe)
333
334@script:python depends on report@
335p << r6.p;
336x << r6.x;
337@@
338
339msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
340coccilib.report.print_report(p[0], msg)
341
342//-----------------------------------------------------------------
343@r7 depends on org || report@
344type T, T2;
345expression x;
346expression E1,E2;
347statement S;
348position p;
349@@
350
351 x = (T)kvmalloc@p(E1,E2);
352 if ((x==NULL) || ...) S
353 memset((T2)x,0,E1);
354
355@script:python depends on org@
356p << r7.p;
357x << r7.x;
358@@
359
360msg="%s" % (x)
361msg_safe=msg.replace("[","@(").replace("]",")")
362coccilib.org.print_todo(p[0], msg_safe)
363
364@script:python depends on report@
365p << r7.p;
366x << r7.x;
367@@
368
369msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
370coccilib.report.print_report(p[0], msg)
371
372//-----------------------------------------------------------------
373@r9 depends on org || report@
374type T, T2;
375expression x;
376expression E1,E2,E3;
377statement S;
378position p;
379@@
380
381 x = (T)kvmalloc_node@p(E1,E2,E3);
382 if ((x==NULL) || ...) S
383 memset((T2)x,0,E1);
384
385@script:python depends on org@
386p << r9.p;
387x << r9.x;
388@@
389
390msg="%s" % (x)
391msg_safe=msg.replace("[","@(").replace("]",")")
392coccilib.org.print_todo(p[0], msg_safe)
393
394@script:python depends on report@
395p << r9.p;
396x << r9.x;
397@@
398
399msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
400coccilib.report.print_report(p[0], msg)
401