atomic_as_refcounter.cocci 2.2 KB

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