Thomas Gleixner | 7f904d7 | 2019-06-04 10:11:38 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 2 | /// Find missing unlocks. This semantic match considers the specific case |
| 3 | /// where the unlock is missing from an if branch, and there is a lock |
| 4 | /// before the if and an unlock after the if. False positives are due to |
| 5 | /// cases where the if branch represents a case where the function is |
| 6 | /// supposed to exit with the lock held, or where there is some preceding |
| 7 | /// function call that releases the lock. |
| 8 | /// |
| 9 | // Confidence: Moderate |
Thomas Gleixner | 7f904d7 | 2019-06-04 10:11:38 +0200 | [diff] [blame] | 10 | // Copyright: (C) 2010-2012 Nicolas Palix. |
| 11 | // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. |
| 12 | // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 13 | // URL: http://coccinelle.lip6.fr/ |
| 14 | // Comments: |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 15 | // Options: --no-includes --include-headers |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 16 | |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 17 | virtual context |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 18 | virtual org |
| 19 | virtual report |
| 20 | |
| 21 | @prelocked@ |
| 22 | position p1,p; |
| 23 | expression E1; |
| 24 | @@ |
| 25 | |
| 26 | ( |
| 27 | mutex_lock@p1 |
| 28 | | |
| 29 | mutex_trylock@p1 |
| 30 | | |
| 31 | spin_lock@p1 |
| 32 | | |
| 33 | spin_trylock@p1 |
| 34 | | |
| 35 | read_lock@p1 |
| 36 | | |
| 37 | read_trylock@p1 |
| 38 | | |
| 39 | write_lock@p1 |
| 40 | | |
| 41 | write_trylock@p1 |
| 42 | | |
| 43 | read_lock_irq@p1 |
| 44 | | |
| 45 | write_lock_irq@p1 |
| 46 | | |
| 47 | read_lock_irqsave@p1 |
| 48 | | |
| 49 | write_lock_irqsave@p1 |
| 50 | | |
| 51 | spin_lock_irq@p1 |
| 52 | | |
| 53 | spin_lock_irqsave@p1 |
| 54 | ) (E1@p,...); |
| 55 | |
| 56 | @looped@ |
| 57 | position r; |
| 58 | @@ |
| 59 | |
| 60 | for(...;...;...) { <+... return@r ...; ...+> } |
| 61 | |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 62 | @err exists@ |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 63 | expression E1; |
| 64 | position prelocked.p; |
| 65 | position up != prelocked.p1; |
| 66 | position r!=looped.r; |
| 67 | identifier lock,unlock; |
| 68 | @@ |
| 69 | |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 70 | *lock(E1@p,...); |
Julia Lawall | 32d0572 | 2018-05-21 08:58:48 +0200 | [diff] [blame] | 71 | ... when != E1 |
| 72 | when any |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 73 | if (...) { |
| 74 | ... when != E1 |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 75 | * return@r ...; |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 76 | } |
Julia Lawall | 32d0572 | 2018-05-21 08:58:48 +0200 | [diff] [blame] | 77 | ... when != E1 |
| 78 | when any |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 79 | *unlock@up(E1,...); |
Nicolas Palix | 54c0562 | 2010-08-24 17:39:00 +0200 | [diff] [blame] | 80 | |
| 81 | @script:python depends on org@ |
| 82 | p << prelocked.p1; |
| 83 | lock << err.lock; |
| 84 | unlock << err.unlock; |
| 85 | p2 << err.r; |
| 86 | @@ |
| 87 | |
| 88 | cocci.print_main(lock,p) |
| 89 | cocci.print_secs(unlock,p2) |
| 90 | |
| 91 | @script:python depends on report@ |
| 92 | p << prelocked.p1; |
| 93 | lock << err.lock; |
| 94 | unlock << err.unlock; |
| 95 | p2 << err.r; |
| 96 | @@ |
| 97 | |
| 98 | msg = "preceding lock on line %s" % (p[0].line) |
| 99 | coccilib.report.print_report(p2[0],msg) |