mini_lock.cocci 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if (...) {
  65. ... when != E1
  66. * return@r ...;
  67. }
  68. ...+>
  69. *unlock@up(E1,...);
  70. @script:python depends on org@
  71. p << prelocked.p1;
  72. lock << err.lock;
  73. unlock << err.unlock;
  74. p2 << err.r;
  75. @@
  76. cocci.print_main(lock,p)
  77. cocci.print_secs(unlock,p2)
  78. @script:python depends on report@
  79. p << prelocked.p1;
  80. lock << err.lock;
  81. unlock << err.unlock;
  82. p2 << err.r;
  83. @@
  84. msg = "preceding lock on line %s" % (p[0].line)
  85. coccilib.report.print_report(p2[0],msg)