zalloc-simple.cocci 9.3 KB

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