atomic_as_refcounter.cocci 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Check if refcount_t type and API should be used
  3. // instead of atomic_t type when dealing with refcounters
  4. //
  5. // Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation
  6. //
  7. // Confidence: Moderate
  8. // URL: https://coccinelle.gitlabpages.inria.fr/website
  9. // Options: --include-headers --very-quiet
  10. virtual report
  11. @r1 exists@
  12. identifier a, x;
  13. position p1, p2;
  14. identifier fname =~ ".*free.*";
  15. identifier fname2 =~ ".*destroy.*";
  16. identifier fname3 =~ ".*del.*";
  17. identifier fname4 =~ ".*queue_work.*";
  18. identifier fname5 =~ ".*schedule_work.*";
  19. identifier fname6 =~ ".*call_rcu.*";
  20. @@
  21. (
  22. atomic_dec_and_test@p1(&(a)->x)
  23. |
  24. atomic_dec_and_lock@p1(&(a)->x, ...)
  25. |
  26. atomic_long_dec_and_lock@p1(&(a)->x, ...)
  27. |
  28. atomic_long_dec_and_test@p1(&(a)->x)
  29. |
  30. atomic64_dec_and_test@p1(&(a)->x)
  31. |
  32. local_dec_and_test@p1(&(a)->x)
  33. )
  34. ...
  35. (
  36. fname@p2(a, ...);
  37. |
  38. fname2@p2(...);
  39. |
  40. fname3@p2(...);
  41. |
  42. fname4@p2(...);
  43. |
  44. fname5@p2(...);
  45. |
  46. fname6@p2(...);
  47. )
  48. @script:python depends on report@
  49. p1 << r1.p1;
  50. p2 << r1.p2;
  51. @@
  52. msg = "atomic_dec_and_test variation before object free at line %s."
  53. coccilib.report.print_report(p1[0], msg % (p2[0].line))
  54. @r4 exists@
  55. identifier a, x, y;
  56. position p1, p2;
  57. identifier fname =~ ".*free.*";
  58. @@
  59. (
  60. atomic_dec_and_test@p1(&(a)->x)
  61. |
  62. atomic_dec_and_lock@p1(&(a)->x, ...)
  63. |
  64. atomic_long_dec_and_lock@p1(&(a)->x, ...)
  65. |
  66. atomic_long_dec_and_test@p1(&(a)->x)
  67. |
  68. atomic64_dec_and_test@p1(&(a)->x)
  69. |
  70. local_dec_and_test@p1(&(a)->x)
  71. )
  72. ...
  73. y=a
  74. ...
  75. fname@p2(y, ...);
  76. @script:python depends on report@
  77. p1 << r4.p1;
  78. p2 << r4.p2;
  79. @@
  80. msg = "atomic_dec_and_test variation before object free at line %s."
  81. coccilib.report.print_report(p1[0], msg % (p2[0].line))
  82. @r2 exists@
  83. identifier a, x;
  84. position p1;
  85. @@
  86. (
  87. atomic_add_unless(&(a)->x,-1,1)@p1
  88. |
  89. atomic_long_add_unless(&(a)->x,-1,1)@p1
  90. |
  91. atomic64_add_unless(&(a)->x,-1,1)@p1
  92. )
  93. @script:python depends on report@
  94. p1 << r2.p1;
  95. @@
  96. msg = "atomic_add_unless"
  97. coccilib.report.print_report(p1[0], msg)
  98. @r3 exists@
  99. identifier x;
  100. position p1;
  101. @@
  102. (
  103. x = atomic_add_return@p1(-1, ...);
  104. |
  105. x = atomic_long_add_return@p1(-1, ...);
  106. |
  107. x = atomic64_add_return@p1(-1, ...);
  108. )
  109. @script:python depends on report@
  110. p1 << r3.p1;
  111. @@
  112. msg = "x = atomic_add_return(-1, ...)"
  113. coccilib.report.print_report(p1[0], msg)