double_lock.cocci 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// Find double locks. False positives may occur when some paths cannot
  3. /// occur at execution, due to the values of variables, and when there is
  4. /// an intervening function call that releases the lock.
  5. ///
  6. // Confidence: Moderate
  7. // Copyright: (C) 2010 Nicolas Palix, DIKU.
  8. // Copyright: (C) 2010 Julia Lawall, DIKU.
  9. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
  10. // URL: https://coccinelle.gitlabpages.inria.fr/website
  11. // Comments:
  12. // Options: --no-includes --include-headers
  13. virtual org
  14. virtual report
  15. @locked@
  16. position p1;
  17. expression E1;
  18. position p;
  19. @@
  20. (
  21. mutex_lock@p1
  22. |
  23. mutex_trylock@p1
  24. |
  25. spin_lock@p1
  26. |
  27. spin_trylock@p1
  28. |
  29. read_lock@p1
  30. |
  31. read_trylock@p1
  32. |
  33. write_lock@p1
  34. |
  35. write_trylock@p1
  36. ) (E1@p,...);
  37. @balanced@
  38. position p1 != locked.p1;
  39. position locked.p;
  40. identifier lock,unlock;
  41. expression x <= locked.E1;
  42. expression E,locked.E1;
  43. expression E2;
  44. @@
  45. if (E) {
  46. <+... when != E1
  47. lock(E1@p,...)
  48. ...+>
  49. }
  50. ... when != E1
  51. when != \(x = E2\|&x\)
  52. when forall
  53. if (E) {
  54. <+... when != E1
  55. unlock@p1(E1,...)
  56. ...+>
  57. }
  58. @r depends on !balanced exists@
  59. expression x <= locked.E1;
  60. expression locked.E1;
  61. expression E2;
  62. identifier lock;
  63. position locked.p,p1,p2;
  64. @@
  65. lock@p1 (E1@p,...);
  66. ... when != E1
  67. when != \(x = E2\|&x\)
  68. lock@p2 (E1,...);
  69. @script:python depends on org@
  70. p1 << r.p1;
  71. p2 << r.p2;
  72. lock << r.lock;
  73. @@
  74. cocci.print_main(lock,p1)
  75. cocci.print_secs("second lock",p2)
  76. @script:python depends on report@
  77. p1 << r.p1;
  78. p2 << r.p2;
  79. lock << r.lock;
  80. @@
  81. msg = "second lock on line %s" % (p2[0].line)
  82. coccilib.report.print_report(p1[0],msg)