mini_lock.cocci 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. virtual context
  14. virtual org
  15. virtual report
  16. @prelocked depends on !(file in "ext")@
  17. position p1,p;
  18. expression E1;
  19. @@
  20. (
  21. irq_lock@p1
  22. |
  23. k_mutex_lock@p1
  24. |
  25. k_sem_take@p1
  26. |
  27. k_spin_lock@p1
  28. ) (E1@p,...);
  29. @looped depends on !(file in "ext")@
  30. position r;
  31. @@
  32. for(...;...;...) { <+... return@r ...; ...+> }
  33. @balanced exists@
  34. position p1 != prelocked.p1;
  35. position prelocked.p;
  36. position pif;
  37. identifier lock,unlock;
  38. expression x <= prelocked.E1;
  39. expression E,prelocked.E1;
  40. expression E2;
  41. @@
  42. if (E) {
  43. ... when != E1
  44. lock(E1@p,...)
  45. ... when any
  46. }
  47. ... when != E1
  48. when != \(x = E2\|&x\)
  49. if@pif (E) {
  50. ... when != E1
  51. unlock@p1(E1,...)
  52. ... when any
  53. }
  54. @err depends on !(file in "ext") exists@
  55. expression E1;
  56. position prelocked.p,balanced.pif;
  57. position up != prelocked.p1;
  58. position r!=looped.r;
  59. identifier lock,unlock;
  60. statement S1,S2;
  61. @@
  62. *lock(E1@p,...);
  63. ... when != E1
  64. when any
  65. when != if@pif (...) S1
  66. if (...) {
  67. ... when != E1
  68. when != if@pif (...) S2
  69. * return@r ...;
  70. }
  71. ... when != E1
  72. when any
  73. *unlock@up(E1,...);
  74. @script:python depends on org@
  75. p << prelocked.p1;
  76. lock << err.lock;
  77. unlock << err.unlock;
  78. p2 << err.r;
  79. @@
  80. cocci.print_main(lock,p)
  81. cocci.print_secs(unlock,p2)
  82. @script:python depends on report@
  83. p << prelocked.p1;
  84. lock << err.lock;
  85. unlock << err.unlock;
  86. p2 << err.r;
  87. @@
  88. msg = "preceding lock on line %s" % (p[0].line)
  89. coccilib.report.print_report(p2[0],msg)