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