double_lock.cocci 1.5 KB

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