diff options
Diffstat (limited to 'scripts/coccinelle/api')
-rw-r--r-- | scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci | 67 | ||||
-rw-r--r-- | scripts/coccinelle/api/alloc/kzalloc-simple.cocci | 86 | ||||
-rw-r--r-- | scripts/coccinelle/api/err_cast.cocci | 56 | ||||
-rw-r--r-- | scripts/coccinelle/api/kstrdup.cocci | 39 | ||||
-rw-r--r-- | scripts/coccinelle/api/memdup.cocci | 40 | ||||
-rw-r--r-- | scripts/coccinelle/api/memdup_user.cocci | 35 | ||||
-rw-r--r-- | scripts/coccinelle/api/resource_size.cocci | 93 |
7 files changed, 416 insertions, 0 deletions
diff --git a/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci b/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci new file mode 100644 index 000000000000..7d4771d449c3 --- /dev/null +++ b/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci | |||
@@ -0,0 +1,67 @@ | |||
1 | /// | ||
2 | /// Casting (void *) value returned by kmalloc is useless | ||
3 | /// as mentioned in Documentation/CodingStyle, Chap 14. | ||
4 | /// | ||
5 | // Confidence: High | ||
6 | // Copyright: 2009,2010 Nicolas Palix, DIKU. GPLv2. | ||
7 | // URL: http://coccinelle.lip6.fr/ | ||
8 | // Options: -no_includes -include_headers | ||
9 | // | ||
10 | // Keywords: kmalloc, kzalloc, kcalloc | ||
11 | // Version min: < 2.6.12 kmalloc | ||
12 | // Version min: < 2.6.12 kcalloc | ||
13 | // Version min: 2.6.14 kzalloc | ||
14 | // | ||
15 | |||
16 | virtual context | ||
17 | virtual patch | ||
18 | virtual org | ||
19 | virtual report | ||
20 | |||
21 | //---------------------------------------------------------- | ||
22 | // For context mode | ||
23 | //---------------------------------------------------------- | ||
24 | |||
25 | @depends on context@ | ||
26 | type T; | ||
27 | @@ | ||
28 | |||
29 | * (T *) | ||
30 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
31 | |||
32 | //---------------------------------------------------------- | ||
33 | // For patch mode | ||
34 | //---------------------------------------------------------- | ||
35 | |||
36 | @depends on patch@ | ||
37 | type T; | ||
38 | @@ | ||
39 | |||
40 | - (T *) | ||
41 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
42 | |||
43 | //---------------------------------------------------------- | ||
44 | // For org and report mode | ||
45 | //---------------------------------------------------------- | ||
46 | |||
47 | @r depends on org || report@ | ||
48 | type T; | ||
49 | position p; | ||
50 | @@ | ||
51 | |||
52 | (T@p *)\(kmalloc\|kzalloc\|kcalloc\)(...) | ||
53 | |||
54 | @script:python depends on org@ | ||
55 | p << r.p; | ||
56 | t << r.T; | ||
57 | @@ | ||
58 | |||
59 | coccilib.org.print_safe_todo(p[0], t) | ||
60 | |||
61 | @script:python depends on report@ | ||
62 | p << r.p; | ||
63 | t << r.T; | ||
64 | @@ | ||
65 | |||
66 | msg="WARNING: casting value returned by k[cmz]alloc to (%s *) is useless." % (t) | ||
67 | coccilib.report.print_report(p[0], msg) | ||
diff --git a/scripts/coccinelle/api/alloc/kzalloc-simple.cocci b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci new file mode 100644 index 000000000000..046b9b16f8f9 --- /dev/null +++ b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci | |||
@@ -0,0 +1,86 @@ | |||
1 | /// | ||
2 | /// Use kzalloc rather than kmalloc followed by memset with 0 | ||
3 | /// | ||
4 | /// This considers some simple cases that are common and easy to validate | ||
5 | /// Note in particular that there are no ...s in the rule, so all of the | ||
6 | /// matched code has to be contiguous | ||
7 | /// | ||
8 | // Confidence: High | ||
9 | // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2. | ||
10 | // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
11 | // URL: http://coccinelle.lip6.fr/rules/kzalloc.html | ||
12 | // Options: -no_includes -include_headers | ||
13 | // | ||
14 | // Keywords: kmalloc, kzalloc | ||
15 | // Version min: < 2.6.12 kmalloc | ||
16 | // Version min: 2.6.14 kzalloc | ||
17 | // | ||
18 | |||
19 | virtual context | ||
20 | virtual patch | ||
21 | virtual org | ||
22 | virtual report | ||
23 | |||
24 | //---------------------------------------------------------- | ||
25 | // For context mode | ||
26 | //---------------------------------------------------------- | ||
27 | |||
28 | @depends on context@ | ||
29 | type T, T2; | ||
30 | expression x; | ||
31 | expression E1,E2; | ||
32 | statement S; | ||
33 | @@ | ||
34 | |||
35 | * x = (T)kmalloc(E1,E2); | ||
36 | if ((x==NULL) || ...) S | ||
37 | * memset((T2)x,0,E1); | ||
38 | |||
39 | //---------------------------------------------------------- | ||
40 | // For patch mode | ||
41 | //---------------------------------------------------------- | ||
42 | |||
43 | @depends on patch@ | ||
44 | type T, T2; | ||
45 | expression x; | ||
46 | expression E1,E2; | ||
47 | statement S; | ||
48 | @@ | ||
49 | |||
50 | - x = (T)kmalloc(E1,E2); | ||
51 | + x = kzalloc(E1,E2); | ||
52 | if ((x==NULL) || ...) S | ||
53 | - memset((T2)x,0,E1); | ||
54 | |||
55 | //---------------------------------------------------------- | ||
56 | // For org mode | ||
57 | //---------------------------------------------------------- | ||
58 | |||
59 | @r depends on org || report@ | ||
60 | type T, T2; | ||
61 | expression x; | ||
62 | expression E1,E2; | ||
63 | statement S; | ||
64 | position p; | ||
65 | @@ | ||
66 | |||
67 | x = (T)kmalloc@p(E1,E2); | ||
68 | if ((x==NULL) || ...) S | ||
69 | memset((T2)x,0,E1); | ||
70 | |||
71 | @script:python depends on org@ | ||
72 | p << r.p; | ||
73 | x << r.x; | ||
74 | @@ | ||
75 | |||
76 | msg="%s" % (x) | ||
77 | msg_safe=msg.replace("[","@(").replace("]",")") | ||
78 | coccilib.org.print_todo(p[0], msg_safe) | ||
79 | |||
80 | @script:python depends on report@ | ||
81 | p << r.p; | ||
82 | x << r.x; | ||
83 | @@ | ||
84 | |||
85 | msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) | ||
86 | coccilib.report.print_report(p[0], msg) | ||
diff --git a/scripts/coccinelle/api/err_cast.cocci b/scripts/coccinelle/api/err_cast.cocci new file mode 100644 index 000000000000..2ce115000af6 --- /dev/null +++ b/scripts/coccinelle/api/err_cast.cocci | |||
@@ -0,0 +1,56 @@ | |||
1 | /// | ||
2 | /// Use ERR_CAST inlined function instead of ERR_PTR(PTR_ERR(...)) | ||
3 | /// | ||
4 | // Confidence: High | ||
5 | // Copyright: (C) 2009, 2010 Nicolas Palix, DIKU. GPLv2. | ||
6 | // Copyright: (C) 2009, 2010 Julia Lawall, DIKU. GPLv2. | ||
7 | // Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
8 | // URL: http://coccinelle.lip6.fr/ | ||
9 | // Options: | ||
10 | // | ||
11 | // Keywords: ERR_PTR, PTR_ERR, ERR_CAST | ||
12 | // Version min: 2.6.25 | ||
13 | // | ||
14 | |||
15 | virtual context | ||
16 | virtual patch | ||
17 | virtual org | ||
18 | virtual report | ||
19 | |||
20 | |||
21 | @ depends on context && !patch && !org && !report@ | ||
22 | expression x; | ||
23 | @@ | ||
24 | |||
25 | * ERR_PTR(PTR_ERR(x)) | ||
26 | |||
27 | @ depends on !context && patch && !org && !report @ | ||
28 | expression x; | ||
29 | @@ | ||
30 | |||
31 | - ERR_PTR(PTR_ERR(x)) | ||
32 | + ERR_CAST(x) | ||
33 | |||
34 | @r depends on !context && !patch && (org || report)@ | ||
35 | expression x; | ||
36 | position p; | ||
37 | @@ | ||
38 | |||
39 | ERR_PTR@p(PTR_ERR(x)) | ||
40 | |||
41 | @script:python depends on org@ | ||
42 | p << r.p; | ||
43 | x << r.x; | ||
44 | @@ | ||
45 | |||
46 | msg="WARNING ERR_CAST can be used with %s" % (x) | ||
47 | msg_safe=msg.replace("[","@(").replace("]",")") | ||
48 | coccilib.org.print_todo(p[0], msg_safe) | ||
49 | |||
50 | @script:python depends on report@ | ||
51 | p << r.p; | ||
52 | x << r.x; | ||
53 | @@ | ||
54 | |||
55 | msg="WARNING: ERR_CAST can be used with %s" % (x) | ||
56 | coccilib.report.print_report(p[0], msg) | ||
diff --git a/scripts/coccinelle/api/kstrdup.cocci b/scripts/coccinelle/api/kstrdup.cocci new file mode 100644 index 000000000000..e0805ad08d39 --- /dev/null +++ b/scripts/coccinelle/api/kstrdup.cocci | |||
@@ -0,0 +1,39 @@ | |||
1 | /// Use kstrdup rather than duplicating its implementation | ||
2 | /// | ||
3 | // Confidence: High | ||
4 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
5 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
6 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
7 | // URL: http://coccinelle.lip6.fr/ | ||
8 | // Comments: | ||
9 | // Options: -no_includes -include_headers | ||
10 | |||
11 | virtual patch | ||
12 | |||
13 | @@ | ||
14 | expression from,to; | ||
15 | expression flag,E1,E2; | ||
16 | statement S; | ||
17 | @@ | ||
18 | |||
19 | - to = kmalloc(strlen(from) + 1,flag); | ||
20 | + to = kstrdup(from, flag); | ||
21 | ... when != \(from = E1 \| to = E1 \) | ||
22 | if (to==NULL || ...) S | ||
23 | ... when != \(from = E2 \| to = E2 \) | ||
24 | - strcpy(to, from); | ||
25 | |||
26 | @@ | ||
27 | expression x,from,to; | ||
28 | expression flag,E1,E2,E3; | ||
29 | statement S; | ||
30 | @@ | ||
31 | |||
32 | - x = strlen(from) + 1; | ||
33 | ... when != \( x = E1 \| from = E1 \) | ||
34 | - to = \(kmalloc\|kzalloc\)(x,flag); | ||
35 | + to = kstrdup(from, flag); | ||
36 | ... when != \(x = E2 \| from = E2 \| to = E2 \) | ||
37 | if (to==NULL || ...) S | ||
38 | ... when != \(x = E3 \| from = E3 \| to = E3 \) | ||
39 | - memcpy(to, from, x); | ||
diff --git a/scripts/coccinelle/api/memdup.cocci b/scripts/coccinelle/api/memdup.cocci new file mode 100644 index 000000000000..b5d722077dc1 --- /dev/null +++ b/scripts/coccinelle/api/memdup.cocci | |||
@@ -0,0 +1,40 @@ | |||
1 | /// Use kmemdup rather than duplicating its implementation | ||
2 | /// | ||
3 | // Confidence: High | ||
4 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
5 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
6 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
7 | // URL: http://coccinelle.lip6.fr/ | ||
8 | // Comments: | ||
9 | // Options: -no_includes -include_headers | ||
10 | |||
11 | virtual patch | ||
12 | |||
13 | @r1@ | ||
14 | expression from,to; | ||
15 | expression flag; | ||
16 | position p; | ||
17 | @@ | ||
18 | |||
19 | to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag); | ||
20 | |||
21 | @r2@ | ||
22 | expression x,from,to; | ||
23 | expression flag,E1; | ||
24 | position p; | ||
25 | @@ | ||
26 | |||
27 | x = strlen(from) + 1; | ||
28 | ... when != \( x = E1 \| from = E1 \) | ||
29 | to = \(kmalloc@p\|kzalloc@p\)(x,flag); | ||
30 | |||
31 | @@ | ||
32 | expression from,to,size,flag; | ||
33 | position p != {r1.p,r2.p}; | ||
34 | statement S; | ||
35 | @@ | ||
36 | |||
37 | - to = \(kmalloc@p\|kzalloc@p\)(size,flag); | ||
38 | + to = kmemdup(from,size,flag); | ||
39 | if (to==NULL || ...) S | ||
40 | - memcpy(to, from, size); | ||
diff --git a/scripts/coccinelle/api/memdup_user.cocci b/scripts/coccinelle/api/memdup_user.cocci new file mode 100644 index 000000000000..72ce012e878a --- /dev/null +++ b/scripts/coccinelle/api/memdup_user.cocci | |||
@@ -0,0 +1,35 @@ | |||
1 | /// Use kmemdup_user rather than duplicating its implementation | ||
2 | /// This is a little bit restricted to reduce false positives | ||
3 | /// | ||
4 | // Confidence: High | ||
5 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
6 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
7 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
8 | // URL: http://coccinelle.lip6.fr/ | ||
9 | // Comments: | ||
10 | // Options: -no_includes -include_headers | ||
11 | |||
12 | virtual patch | ||
13 | |||
14 | @@ | ||
15 | expression from,to,size,flag; | ||
16 | position p; | ||
17 | identifier l1,l2; | ||
18 | @@ | ||
19 | |||
20 | - to = \(kmalloc@p\|kzalloc@p\)(size,flag); | ||
21 | + to = memdup_user(from,size); | ||
22 | if ( | ||
23 | - to==NULL | ||
24 | + IS_ERR(to) | ||
25 | || ...) { | ||
26 | <+... when != goto l1; | ||
27 | - -ENOMEM | ||
28 | + PTR_ERR(to) | ||
29 | ...+> | ||
30 | } | ||
31 | - if (copy_from_user(to, from, size) != 0) { | ||
32 | - <+... when != goto l2; | ||
33 | - -EFAULT | ||
34 | - ...+> | ||
35 | - } | ||
diff --git a/scripts/coccinelle/api/resource_size.cocci b/scripts/coccinelle/api/resource_size.cocci new file mode 100644 index 000000000000..1935a58b39d9 --- /dev/null +++ b/scripts/coccinelle/api/resource_size.cocci | |||
@@ -0,0 +1,93 @@ | |||
1 | /// | ||
2 | /// Use resource_size function on resource object | ||
3 | /// instead of explicit computation. | ||
4 | /// | ||
5 | // Confidence: High | ||
6 | // Copyright: (C) 2009, 2010 Nicolas Palix, DIKU. GPLv2. | ||
7 | // Copyright: (C) 2009, 2010 Julia Lawall, DIKU. GPLv2. | ||
8 | // Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
9 | // URL: http://coccinelle.lip6.fr/ | ||
10 | // Options: | ||
11 | // | ||
12 | // Keywords: resource_size | ||
13 | // Version min: 2.6.27 resource_size | ||
14 | // | ||
15 | |||
16 | virtual context | ||
17 | virtual patch | ||
18 | virtual org | ||
19 | virtual report | ||
20 | |||
21 | //---------------------------------------------------------- | ||
22 | // For context mode | ||
23 | //---------------------------------------------------------- | ||
24 | |||
25 | @r_context depends on context && !patch && !org@ | ||
26 | struct resource *res; | ||
27 | @@ | ||
28 | |||
29 | * (res->end - res->start) + 1 | ||
30 | |||
31 | //---------------------------------------------------------- | ||
32 | // For patch mode | ||
33 | //---------------------------------------------------------- | ||
34 | |||
35 | @r_patch depends on !context && patch && !org@ | ||
36 | struct resource *res; | ||
37 | @@ | ||
38 | |||
39 | - (res->end - res->start) + 1 | ||
40 | + resource_size(res) | ||
41 | |||
42 | //---------------------------------------------------------- | ||
43 | // For org mode | ||
44 | //---------------------------------------------------------- | ||
45 | |||
46 | |||
47 | @r_org depends on !context && !patch && (org || report)@ | ||
48 | struct resource *res; | ||
49 | position p; | ||
50 | @@ | ||
51 | |||
52 | (res->end@p - res->start) + 1 | ||
53 | |||
54 | @rbad_org depends on !context && !patch && (org || report)@ | ||
55 | struct resource *res; | ||
56 | position p != r_org.p; | ||
57 | @@ | ||
58 | |||
59 | res->end@p - res->start | ||
60 | |||
61 | @script:python depends on org@ | ||
62 | p << r_org.p; | ||
63 | x << r_org.res; | ||
64 | @@ | ||
65 | |||
66 | msg="ERROR with %s" % (x) | ||
67 | msg_safe=msg.replace("[","@(").replace("]",")") | ||
68 | coccilib.org.print_todo(p[0], msg_safe) | ||
69 | |||
70 | @script:python depends on report@ | ||
71 | p << r_org.p; | ||
72 | x << r_org.res; | ||
73 | @@ | ||
74 | |||
75 | msg="ERROR: Missing resource_size with %s" % (x) | ||
76 | coccilib.report.print_report(p[0], msg) | ||
77 | |||
78 | @script:python depends on org@ | ||
79 | p << rbad_org.p; | ||
80 | x << rbad_org.res; | ||
81 | @@ | ||
82 | |||
83 | msg="WARNING with %s" % (x) | ||
84 | msg_safe=msg.replace("[","@(").replace("]",")") | ||
85 | coccilib.org.print_todo(p[0], msg_safe) | ||
86 | |||
87 | @script:python depends on report@ | ||
88 | p << rbad_org.p; | ||
89 | x << rbad_org.res; | ||
90 | @@ | ||
91 | |||
92 | msg="WARNING: Suspicious code. resource_size is maybe missing with %s" % (x) | ||
93 | coccilib.report.print_report(p[0], msg) | ||