mini_lock.cocci 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-2012 Nicolas Palix. GPLv2.
  10. // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
  11. // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
  12. // URL: http://coccinelle.lip6.fr/
  13. // Comments:
  14. // Options: --no-includes --include-headers
  15. virtual context
  16. virtual org
  17. virtual report
  18. @prelocked@
  19. position p1,p;
  20. expression E1;
  21. @@
  22. (
  23. mutex_lock@p1
  24. |
  25. mutex_trylock@p1
  26. |
  27. spin_lock@p1
  28. |
  29. spin_trylock@p1
  30. |
  31. read_lock@p1
  32. |
  33. read_trylock@p1
  34. |
  35. write_lock@p1
  36. |
  37. write_trylock@p1
  38. |
  39. read_lock_irq@p1
  40. |
  41. write_lock_irq@p1
  42. |
  43. read_lock_irqsave@p1
  44. |
  45. write_lock_irqsave@p1
  46. |
  47. spin_lock_irq@p1
  48. |
  49. spin_lock_irqsave@p1
  50. ) (E1@p,...);
  51. @looped@
  52. position r;
  53. @@
  54. for(...;...;...) { <+... return@r ...; ...+> }
  55. @err exists@
  56. expression E1;
  57. position prelocked.p;
  58. position up != prelocked.p1;
  59. position r!=looped.r;
  60. identifier lock,unlock;
  61. @@
  62. *lock(E1@p,...);
  63. ... when != E1
  64. when any
  65. if (...) {
  66. ... when != E1
  67. * return@r ...;
  68. }
  69. ... when != E1
  70. when any
  71. *unlock@up(E1,...);
  72. @script:python depends on org@
  73. p << prelocked.p1;
  74. lock << err.lock;
  75. unlock << err.unlock;
  76. p2 << err.r;
  77. @@
  78. cocci.print_main(lock,p)
  79. cocci.print_secs(unlock,p2)
  80. @script:python depends on report@
  81. p << prelocked.p1;
  82. lock << err.lock;
  83. unlock << err.unlock;
  84. p2 << err.r;
  85. @@
  86. msg = "preceding lock on line %s" % (p[0].line)
  87. coccilib.report.print_report(p2[0],msg)