zalloc-simple.cocci 8.4 KB

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