zalloc-simple.cocci 8.4 KB

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