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 = dma_alloc_coherent(E2,E1,E3,E4);
73+ x = dma_zalloc_coherent(E2,E1,E3,E4);
74|
75- x = (T *)dma_alloc_coherent(E2,E1,E3,E4);
76+ x = dma_zalloc_coherent(E2,E1,E3,E4);
77|
78- x = (T)dma_alloc_coherent(E2,E1,E3,E4);
79+ x = (T)dma_zalloc_coherent(E2,E1,E3,E4);
80|
81- x = kmalloc_node(E1,E2,E3);
82+ x = kzalloc_node(E1,E2,E3);
83|
84- x = (T *)kmalloc_node(E1,E2,E3);
85+ x = kzalloc_node(E1,E2,E3);
86|
87- x = (T)kmalloc_node(E1,E2,E3);
88+ x = (T)kzalloc_node(E1,E2,E3);
89|
90- x = kmem_cache_alloc(E3,E4);
91+ x = kmem_cache_zalloc(E3,E4);
92|
93- x = (T *)kmem_cache_alloc(E3,E4);
94+ x = kmem_cache_zalloc(E3,E4);
95|
96- x = (T)kmem_cache_alloc(E3,E4);
97+ x = (T)kmem_cache_zalloc(E3,E4);
98|
99- x = kmem_alloc(E1,E2);
100+ x = kmem_zalloc(E1,E2);
101|
102- x = (T *)kmem_alloc(E1,E2);
103+ x = kmem_zalloc(E1,E2);
104|
105- x = (T)kmem_alloc(E1,E2);
106+ x = (T)kmem_zalloc(E1,E2);
107|
108- x = devm_kmalloc(E2,E1,E3);
109+ x = devm_kzalloc(E2,E1,E3);
110|
111- x = (T *)devm_kmalloc(E2,E1,E3);
112+ x = devm_kzalloc(E2,E1,E3);
113|
114- x = (T)devm_kmalloc(E2,E1,E3);
115+ x = (T)devm_kzalloc(E2,E1,E3);
116|
117- x = kvmalloc(E1,E2);
118+ x = kvzalloc(E1,E2);
119|
120- x = (T *)kvmalloc(E1,E2);
121+ x = kvzalloc(E1,E2);
122|
123- x = (T)kvmalloc(E1,E2);
124+ x = (T)kvzalloc(E1,E2);
125|
126- x = kvmalloc_node(E1,E2,E3);
127+ x = kvzalloc_node(E1,E2,E3);
128|
129- x = (T *)kvmalloc_node(E1,E2,E3);
130+ x = kvzalloc_node(E1,E2,E3);
131|
132- x = (T)kvmalloc_node(E1,E2,E3);
133+ x = (T)kvzalloc_node(E1,E2,E3);
134)
135  if ((x==NULL) || ...) S
136- memset((T2)x,0,E1);
137
138//----------------------------------------------------------
139//  For org mode
140//----------------------------------------------------------
141
142@r depends on org || report@
143type T, T2;
144expression x;
145expression E1,E2;
146statement S;
147position p;
148@@
149
150 x = (T)kmalloc@p(E1,E2);
151 if ((x==NULL) || ...) S
152 memset((T2)x,0,E1);
153
154@script:python depends on org@
155p << r.p;
156x << r.x;
157@@
158
159msg="%s" % (x)
160msg_safe=msg.replace("[","@(").replace("]",")")
161coccilib.org.print_todo(p[0], msg_safe)
162
163@script:python depends on report@
164p << r.p;
165x << r.x;
166@@
167
168msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
169coccilib.report.print_report(p[0], msg)
170
171//-----------------------------------------------------------------
172@r1 depends on org || report@
173type T, T2;
174expression x;
175expression E1;
176statement S;
177position p;
178@@
179
180 x = (T)vmalloc@p(E1);
181 if ((x==NULL) || ...) S
182 memset((T2)x,0,E1);
183
184@script:python depends on org@
185p << r1.p;
186x << r1.x;
187@@
188
189msg="%s" % (x)
190msg_safe=msg.replace("[","@(").replace("]",")")
191coccilib.org.print_todo(p[0], msg_safe)
192
193@script:python depends on report@
194p << r1.p;
195x << r1.x;
196@@
197
198msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
199coccilib.report.print_report(p[0], msg)
200
201//-----------------------------------------------------------------
202@r2 depends on org || report@
203type T, T2;
204expression x;
205expression E1,E2,E3,E4;
206statement S;
207position p;
208@@
209
210 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
211 if ((x==NULL) || ...) S
212 memset((T2)x,0,E1);
213
214@script:python depends on org@
215p << r2.p;
216x << r2.x;
217@@
218
219msg="%s" % (x)
220msg_safe=msg.replace("[","@(").replace("]",")")
221coccilib.org.print_todo(p[0], msg_safe)
222
223@script:python depends on report@
224p << r2.p;
225x << r2.x;
226@@
227
228msg="WARNING: dma_zalloc_coherent should be used for %s, instead of dma_alloc_coherent/memset" % (x)
229coccilib.report.print_report(p[0], msg)
230
231//-----------------------------------------------------------------
232@r3 depends on org || report@
233type T, T2;
234expression x;
235expression E1,E2,E3;
236statement S;
237position p;
238@@
239
240 x = (T)kmalloc_node@p(E1,E2,E3);
241 if ((x==NULL) || ...) S
242 memset((T2)x,0,E1);
243
244@script:python depends on org@
245p << r3.p;
246x << r3.x;
247@@
248
249msg="%s" % (x)
250msg_safe=msg.replace("[","@(").replace("]",")")
251coccilib.org.print_todo(p[0], msg_safe)
252
253@script:python depends on report@
254p << r3.p;
255x << r3.x;
256@@
257
258msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
259coccilib.report.print_report(p[0], msg)
260
261//-----------------------------------------------------------------
262@r4 depends on org || report@
263type T, T2;
264expression x;
265expression E1,E2,E3;
266statement S;
267position p;
268@@
269
270 x = (T)kmem_cache_alloc@p(E2,E3);
271 if ((x==NULL) || ...) S
272 memset((T2)x,0,E1);
273
274@script:python depends on org@
275p << r4.p;
276x << r4.x;
277@@
278
279msg="%s" % (x)
280msg_safe=msg.replace("[","@(").replace("]",")")
281coccilib.org.print_todo(p[0], msg_safe)
282
283@script:python depends on report@
284p << r4.p;
285x << r4.x;
286@@
287
288msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
289coccilib.report.print_report(p[0], msg)
290
291//-----------------------------------------------------------------
292@r5 depends on org || report@
293type T, T2;
294expression x;
295expression E1,E2;
296statement S;
297position p;
298@@
299
300 x = (T)kmem_alloc@p(E1,E2);
301 if ((x==NULL) || ...) S
302 memset((T2)x,0,E1);
303
304@script:python depends on org@
305p << r5.p;
306x << r5.x;
307@@
308
309msg="%s" % (x)
310msg_safe=msg.replace("[","@(").replace("]",")")
311coccilib.org.print_todo(p[0], msg_safe)
312
313@script:python depends on report@
314p << r5.p;
315x << r5.x;
316@@
317
318msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
319coccilib.report.print_report(p[0], msg)
320
321//-----------------------------------------------------------------
322@r6 depends on org || report@
323type T, T2;
324expression x;
325expression E1,E2,E3;
326statement S;
327position p;
328@@
329
330 x = (T)devm_kmalloc@p(E2,E1,E3);
331 if ((x==NULL) || ...) S
332 memset((T2)x,0,E1);
333
334@script:python depends on org@
335p << r6.p;
336x << r6.x;
337@@
338
339msg="%s" % (x)
340msg_safe=msg.replace("[","@(").replace("]",")")
341coccilib.org.print_todo(p[0], msg_safe)
342
343@script:python depends on report@
344p << r6.p;
345x << r6.x;
346@@
347
348msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
349coccilib.report.print_report(p[0], msg)
350
351//-----------------------------------------------------------------
352@r7 depends on org || report@
353type T, T2;
354expression x;
355expression E1,E2;
356statement S;
357position p;
358@@
359
360 x = (T)kvmalloc@p(E1,E2);
361 if ((x==NULL) || ...) S
362 memset((T2)x,0,E1);
363
364@script:python depends on org@
365p << r7.p;
366x << r7.x;
367@@
368
369msg="%s" % (x)
370msg_safe=msg.replace("[","@(").replace("]",")")
371coccilib.org.print_todo(p[0], msg_safe)
372
373@script:python depends on report@
374p << r7.p;
375x << r7.x;
376@@
377
378msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
379coccilib.report.print_report(p[0], msg)
380
381//-----------------------------------------------------------------
382@r9 depends on org || report@
383type T, T2;
384expression x;
385expression E1,E2,E3;
386statement S;
387position p;
388@@
389
390 x = (T)kvmalloc_node@p(E1,E2,E3);
391 if ((x==NULL) || ...) S
392 memset((T2)x,0,E1);
393
394@script:python depends on org@
395p << r9.p;
396x << r9.x;
397@@
398
399msg="%s" % (x)
400msg_safe=msg.replace("[","@(").replace("]",")")
401coccilib.org.print_todo(p[0], msg_safe)
402
403@script:python depends on report@
404p << r9.p;
405x << r9.x;
406@@
407
408msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
409coccilib.report.print_report(p[0], msg)
410