mini_lock.cocci 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /// Find missing unlocks. This semantic match considers the specific case
  2. /// where the unlock is missing from an if branch, and there is a lock
  3. /// before the if and an unlock after the if. False positives are due to
  4. /// cases where the if branch represents a case where the function is
  5. /// supposed to exit with the lock held, or where there is some preceding
  6. /// function call that releases the lock.
  7. ///
  8. // Confidence: Moderate
  9. // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
  10. // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
  11. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
  12. // URL: http://coccinelle.lip6.fr/
  13. // Comments:
  14. // Options: -no_includes -include_headers
  15. virtual org
  16. virtual report
  17. @prelocked@
  18. position p1,p;
  19. expression E1;
  20. @@
  21. (
  22. mutex_lock@p1
  23. |
  24. mutex_trylock@p1
  25. |
  26. spin_lock@p1
  27. |
  28. spin_trylock@p1
  29. |
  30. read_lock@p1
  31. |
  32. read_trylock@p1
  33. |
  34. write_lock@p1
  35. |
  36. write_trylock@p1
  37. |
  38. read_lock_irq@p1
  39. |
  40. write_lock_irq@p1
  41. |
  42. read_lock_irqsave@p1
  43. |
  44. write_lock_irqsave@p1
  45. |
  46. spin_lock_irq@p1
  47. |
  48. spin_lock_irqsave@p1
  49. ) (E1@p,...);
  50. @looped@
  51. position r;
  52. @@
  53. for(...;...;...) { <+... return@r ...; ...+> }
  54. @err@
  55. expression E1;
  56. position prelocked.p;
  57. position up != prelocked.p1;
  58. position r!=looped.r;
  59. identifier lock,unlock;
  60. @@
  61. lock(E1@p,...);
  62. <+... when != E1
  63. if (...) {
  64. ... when != E1
  65. return@r ...;
  66. }
  67. ...+>
  68. unlock@up(E1,...);
  69. @script:python depends on org@
  70. p << prelocked.p1;
  71. lock << err.lock;
  72. unlock << err.unlock;
  73. p2 << err.r;
  74. @@
  75. cocci.print_main(lock,p)
  76. cocci.print_secs(unlock,p2)
  77. @script:python depends on report@
  78. p << prelocked.p1;
  79. lock << err.lock;
  80. unlock << err.unlock;
  81. p2 << err.r;
  82. @@
  83. msg = "preceding lock on line %s" % (p[0].line)
  84. coccilib.report.print_report(p2[0],msg)