Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,123 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Remove casting the values returned by memory allocation functions
/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
///
//# This makes an effort to find cases of casting of values returned by
//# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
//# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
//# the casting as it is not required. The result in the patch case may
//# need some reformatting.
//
// Confidence: High
// Copyright: (C) 2014 Himangi Saraogi
// Copyright: (C) 2017 Himanshu Jha
// Comments:
// Options: --no-includes --include-headers
//
virtual context
virtual patch
virtual org
virtual report
@initialize:python@
@@
import re
pattern = '__'
m = re.compile(pattern)
@r1 depends on context || patch@
type T;
@@
(T *)
\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@script:python depends on context@
t << r1.T;
@@
if m.search(t) != None:
cocci.include_match(False)
@depends on context && r1@
type r1.T;
@@
* (T *)
\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@script:python depends on patch@
t << r1.T;
@@
if m.search(t) != None:
cocci.include_match(False)
@depends on patch && r1@
type r1.T;
@@
- (T *)
\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
//----------------------------------------------------------
// For org and report mode
//----------------------------------------------------------
@r2 depends on org || report@
type T;
position p;
@@
(T@p *)
\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
@script:python depends on org@
p << r2.p;
t << r2.T;
@@
if m.search(t) != None:
cocci.include_match(False)
else:
coccilib.org.print_safe_todo(p[0], t)
@script:python depends on report@
p << r2.p;
t << r2.T;
@@
if m.search(t) != None:
cocci.include_match(False)
else:
msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
coccilib.report.print_report(p[0], msg)
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Use *_pool_zalloc rather than *_pool_alloc followed by memset with 0
///
// Copyright: (C) 2015 Intel Corp.
// Options: --no-includes --include-headers
//
// Keywords: dma_pool_zalloc, pci_pool_zalloc
//
virtual context
virtual patch
virtual org
virtual report
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@depends on context@
expression x;
statement S;
@@
* x = \(dma_pool_alloc\|pci_pool_alloc\)(...);
if ((x==NULL) || ...) S
* memset(x,0, ...);
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@depends on patch@
expression x;
expression a,b,c;
statement S;
@@
- x = dma_pool_alloc(a,b,c);
+ x = dma_pool_zalloc(a,b,c);
if ((x==NULL) || ...) S
- memset(x,0,...);
@depends on patch@
expression x;
expression a,b,c;
statement S;
@@
- x = pci_pool_alloc(a,b,c);
+ x = pci_pool_zalloc(a,b,c);
if ((x==NULL) || ...) S
- memset(x,0,...);
//----------------------------------------------------------
// For org and report mode
//----------------------------------------------------------
@r depends on org || report@
expression x;
expression a,b,c;
statement S;
position p;
@@
x = @p\(dma_pool_alloc\|pci_pool_alloc\)(a,b,c);
if ((x==NULL) || ...) S
memset(x,0, ...);
@script:python depends on org@
p << r.p;
x << r.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r.p;
x << r.x;
@@
msg="WARNING: *_pool_zalloc should be used for %s, instead of *_pool_alloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
@@ -0,0 +1,411 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Use zeroing allocator rather than allocator followed by memset with 0
///
/// This considers some simple cases that are common and easy to validate
/// Note in particular that there are no ...s in the rule, so all of the
/// matched code has to be contiguous
///
// Confidence: High
// Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.
// Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.
// Copyright: (C) 2017 Himanshu Jha
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options: --no-includes --include-headers
//
// Keywords: kmalloc, kzalloc
// Version min: < 2.6.12 kmalloc
// Version min: 2.6.14 kzalloc
//
virtual context
virtual patch
virtual org
virtual report
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@depends on context@
type T, T2;
expression x;
expression E1;
statement S;
@@
* x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
if ((x==NULL) || ...) S
* memset((T2)x,0,E1);
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@depends on patch@
type T, T2;
expression x;
expression E1,E2,E3,E4;
statement S;
@@
(
- x = kmalloc(E1,E2);
+ x = kzalloc(E1,E2);
|
- x = (T *)kmalloc(E1,E2);
+ x = kzalloc(E1,E2);
|
- x = (T)kmalloc(E1,E2);
+ x = (T)kzalloc(E1,E2);
|
- x = vmalloc(E1);
+ x = vzalloc(E1);
|
- x = (T *)vmalloc(E1);
+ x = vzalloc(E1);
|
- x = (T)vmalloc(E1);
+ x = (T)vzalloc(E1);
|
- x = kmalloc_node(E1,E2,E3);
+ x = kzalloc_node(E1,E2,E3);
|
- x = (T *)kmalloc_node(E1,E2,E3);
+ x = kzalloc_node(E1,E2,E3);
|
- x = (T)kmalloc_node(E1,E2,E3);
+ x = (T)kzalloc_node(E1,E2,E3);
|
- x = kmem_cache_alloc(E3,E4);
+ x = kmem_cache_zalloc(E3,E4);
|
- x = (T *)kmem_cache_alloc(E3,E4);
+ x = kmem_cache_zalloc(E3,E4);
|
- x = (T)kmem_cache_alloc(E3,E4);
+ x = (T)kmem_cache_zalloc(E3,E4);
|
- x = kmem_alloc(E1,E2);
+ x = kmem_zalloc(E1,E2);
|
- x = (T *)kmem_alloc(E1,E2);
+ x = kmem_zalloc(E1,E2);
|
- x = (T)kmem_alloc(E1,E2);
+ x = (T)kmem_zalloc(E1,E2);
|
- x = devm_kmalloc(E2,E1,E3);
+ x = devm_kzalloc(E2,E1,E3);
|
- x = (T *)devm_kmalloc(E2,E1,E3);
+ x = devm_kzalloc(E2,E1,E3);
|
- x = (T)devm_kmalloc(E2,E1,E3);
+ x = (T)devm_kzalloc(E2,E1,E3);
|
- x = kvmalloc(E1,E2);
+ x = kvzalloc(E1,E2);
|
- x = (T *)kvmalloc(E1,E2);
+ x = kvzalloc(E1,E2);
|
- x = (T)kvmalloc(E1,E2);
+ x = (T)kvzalloc(E1,E2);
|
- x = kvmalloc_node(E1,E2,E3);
+ x = kvzalloc_node(E1,E2,E3);
|
- x = (T *)kvmalloc_node(E1,E2,E3);
+ x = kvzalloc_node(E1,E2,E3);
|
- x = (T)kvmalloc_node(E1,E2,E3);
+ x = (T)kvzalloc_node(E1,E2,E3);
)
if ((x==NULL) || ...) S
- memset((T2)x,0,E1);
@depends on patch@
type T, T2;
expression x;
expression E1,E2,E3,E4;
statement S;
@@
x = (T)dma_alloc_coherent(E1, E2, E3, E4);
if ((x==NULL) || ...) S
- memset((T2)x, 0, E2);
//----------------------------------------------------------
// For org mode
//----------------------------------------------------------
@r depends on org || report@
type T, T2;
expression x;
expression E1,E2;
statement S;
position p;
@@
x = (T)kmalloc@p(E1,E2);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r.p;
x << r.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r.p;
x << r.x;
@@
msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r1 depends on org || report@
type T, T2;
expression x;
expression E1;
statement S;
position p;
@@
x = (T)vmalloc@p(E1);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r1.p;
x << r1.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r1.p;
x << r1.x;
@@
msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r2 depends on org || report@
type T, T2;
expression x;
expression E1,E2,E3,E4;
statement S;
position p;
@@
x = (T)dma_alloc_coherent@p(E1,E2,E3,E4);
if ((x==NULL) || ...) S
memset((T2)x,0,E2);
@script:python depends on org@
p << r2.p;
x << r2.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r2.p;
x << r2.x;
@@
msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r3 depends on org || report@
type T, T2;
expression x;
expression E1,E2,E3;
statement S;
position p;
@@
x = (T)kmalloc_node@p(E1,E2,E3);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r3.p;
x << r3.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r3.p;
x << r3.x;
@@
msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r4 depends on org || report@
type T, T2;
expression x;
expression E1,E2,E3;
statement S;
position p;
@@
x = (T)kmem_cache_alloc@p(E2,E3);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r4.p;
x << r4.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r4.p;
x << r4.x;
@@
msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r5 depends on org || report@
type T, T2;
expression x;
expression E1,E2;
statement S;
position p;
@@
x = (T)kmem_alloc@p(E1,E2);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r5.p;
x << r5.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r5.p;
x << r5.x;
@@
msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r6 depends on org || report@
type T, T2;
expression x;
expression E1,E2,E3;
statement S;
position p;
@@
x = (T)devm_kmalloc@p(E2,E1,E3);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r6.p;
x << r6.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r6.p;
x << r6.x;
@@
msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r7 depends on org || report@
type T, T2;
expression x;
expression E1,E2;
statement S;
position p;
@@
x = (T)kvmalloc@p(E1,E2);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r7.p;
x << r7.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r7.p;
x << r7.x;
@@
msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
coccilib.report.print_report(p[0], msg)
//-----------------------------------------------------------------
@r9 depends on org || report@
type T, T2;
expression x;
expression E1,E2,E3;
statement S;
position p;
@@
x = (T)kvmalloc_node@p(E1,E2,E3);
if ((x==NULL) || ...) S
memset((T2)x,0,E1);
@script:python depends on org@
p << r9.p;
x << r9.x;
@@
msg="%s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r9.p;
x << r9.x;
@@
msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
coccilib.report.print_report(p[0], msg)
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: GPL-2.0-only
// Check if refcount_t type and API should be used
// instead of atomic_t type when dealing with refcounters
//
// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation
//
// Confidence: Moderate
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options: --include-headers --very-quiet
virtual report
@r1 exists@
identifier a, x;
position p1, p2;
identifier fname =~ ".*free.*";
identifier fname2 =~ ".*destroy.*";
identifier fname3 =~ ".*del.*";
identifier fname4 =~ ".*queue_work.*";
identifier fname5 =~ ".*schedule_work.*";
identifier fname6 =~ ".*call_rcu.*";
@@
(
atomic_dec_and_test@p1(&(a)->x)
|
atomic_dec_and_lock@p1(&(a)->x, ...)
|
atomic_long_dec_and_lock@p1(&(a)->x, ...)
|
atomic_long_dec_and_test@p1(&(a)->x)
|
atomic64_dec_and_test@p1(&(a)->x)
|
local_dec_and_test@p1(&(a)->x)
)
...
(
fname@p2(a, ...);
|
fname2@p2(...);
|
fname3@p2(...);
|
fname4@p2(...);
|
fname5@p2(...);
|
fname6@p2(...);
)
@script:python depends on report@
p1 << r1.p1;
p2 << r1.p2;
@@
msg = "WARNING: atomic_dec_and_test variation before object free at line %s."
coccilib.report.print_report(p1[0], msg % (p2[0].line))
@r4 exists@
identifier a, x, y;
position p1, p2;
identifier fname =~ ".*free.*";
@@
(
atomic_dec_and_test@p1(&(a)->x)
|
atomic_dec_and_lock@p1(&(a)->x, ...)
|
atomic_long_dec_and_lock@p1(&(a)->x, ...)
|
atomic_long_dec_and_test@p1(&(a)->x)
|
atomic64_dec_and_test@p1(&(a)->x)
|
local_dec_and_test@p1(&(a)->x)
)
...
y=a
...
fname@p2(y, ...);
@script:python depends on report@
p1 << r4.p1;
p2 << r4.p2;
@@
msg = "WARNING: atomic_dec_and_test variation before object free at line %s."
coccilib.report.print_report(p1[0], msg % (p2[0].line))
@r2 exists@
identifier a, x;
position p1;
@@
(
atomic_add_unless(&(a)->x,-1,1)@p1
|
atomic_long_add_unless(&(a)->x,-1,1)@p1
|
atomic64_add_unless(&(a)->x,-1,1)@p1
)
@script:python depends on report@
p1 << r2.p1;
@@
msg = "WARNING: atomic_add_unless"
coccilib.report.print_report(p1[0], msg)
@r3 exists@
identifier x;
position p1;
@@
(
x = atomic_add_return@p1(-1, ...);
|
x = atomic_long_add_return@p1(-1, ...);
|
x = atomic64_add_return@p1(-1, ...);
)
@script:python depends on report@
p1 << r3.p1;
@@
msg = "WARNING: x = atomic_add_return(-1, ...)"
coccilib.report.print_report(p1[0], msg)
@@ -0,0 +1,162 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Detect BQ27XXX_DATA structures with identical registers, dm registers or
/// properties.
//# Doesn't unfold macros used in register or property fields.
//# Requires OCaml scripting
///
// Confidence: High
// Copyright: (C) 2017 Julia Lawall, Inria/LIP6,
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Requires: 1.0.7
// Keywords: BQ27XXX_DATA
virtual report
@initialize:ocaml@
@@
let print_report p msg =
let p = List.hd p in
Printf.printf "%s:%d:%d-%d: %s" p.file p.line p.col p.col_end msg
@str depends on report@
type t;
identifier i,i1,i2;
expression e1,e2;
@@
t i[] = {
...,
[e1] = BQ27XXX_DATA(i1,...),
...,
[e2] = BQ27XXX_DATA(i2,...),
...,
};
@script:ocaml tocheck@
i1 << str.i1;
i2 << str.i2;
i1regs; i2regs;
i1dmregs; i2dmregs;
i1props; i2props;
@@
if not(i1 = i2)
then
begin
i1regs := make_ident (i1 ^ "_regs");
i2regs := make_ident (i2 ^ "_regs");
i1dmregs := make_ident (i1 ^ "_dm_regs");
i2dmregs := make_ident (i2 ^ "_dm_regs");
i1props := make_ident (i1 ^ "_props");
i2props := make_ident (i2 ^ "_props")
end
(* ---------------------------------------------------------------- *)
@getregs1@
typedef u8;
identifier tocheck.i1regs;
initializer list i1regs_vals;
position p1;
@@
u8 i1regs@p1[...] = { i1regs_vals, };
@getregs2@
identifier tocheck.i2regs;
initializer list i2regs_vals;
position p2;
@@
u8 i2regs@p2[...] = { i2regs_vals, };
@script:ocaml@
(_,i1regs_vals) << getregs1.i1regs_vals;
(_,i2regs_vals) << getregs2.i2regs_vals;
i1regs << tocheck.i1regs;
i2regs << tocheck.i2regs;
p1 << getregs1.p1;
p2 << getregs2.p2;
@@
if i1regs < i2regs &&
List.sort compare i1regs_vals = List.sort compare i2regs_vals
then
let msg =
Printf.sprintf
"WARNING %s and %s (line %d) are identical\n"
i1regs i2regs (List.hd p2).line in
print_report p1 msg
(* ---------------------------------------------------------------- *)
@getdmregs1@
identifier tocheck.i1dmregs;
initializer list i1dmregs_vals;
position p1;
@@
struct bq27xxx_dm_reg i1dmregs@p1[] = { i1dmregs_vals, };
@getdmregs2@
identifier tocheck.i2dmregs;
initializer list i2dmregs_vals;
position p2;
@@
struct bq27xxx_dm_reg i2dmregs@p2[] = { i2dmregs_vals, };
@script:ocaml@
(_,i1dmregs_vals) << getdmregs1.i1dmregs_vals;
(_,i2dmregs_vals) << getdmregs2.i2dmregs_vals;
i1dmregs << tocheck.i1dmregs;
i2dmregs << tocheck.i2dmregs;
p1 << getdmregs1.p1;
p2 << getdmregs2.p2;
@@
if i1dmregs < i2dmregs &&
List.sort compare i1dmregs_vals = List.sort compare i2dmregs_vals
then
let msg =
Printf.sprintf
"WARNING %s and %s (line %d) are identical\n"
i1dmregs i2dmregs (List.hd p2).line in
print_report p1 msg
(* ---------------------------------------------------------------- *)
@getprops1@
identifier tocheck.i1props;
initializer list[n1] i1props_vals;
position p1;
@@
enum power_supply_property i1props@p1[] = { i1props_vals, };
@getprops2@
identifier tocheck.i2props;
initializer list[n2] i2props_vals;
position p2;
@@
enum power_supply_property i2props@p2[] = { i2props_vals, };
@script:ocaml@
(_,i1props_vals) << getprops1.i1props_vals;
(_,i2props_vals) << getprops2.i2props_vals;
i1props << tocheck.i1props;
i2props << tocheck.i2props;
p1 << getprops1.p1;
p2 << getprops2.p2;
@@
if i1props < i2props &&
List.sort compare i1props_vals = List.sort compare i2props_vals
then
let msg =
Printf.sprintf
"WARNING %s and %s (line %d) are identical\n"
i1props i2props (List.hd p2).line in
print_report p1 msg
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-2.0
/// Make sure calls to d_find_alias() have a corresponding call to dput().
//
// Keywords: d_find_alias, dput
//
// Confidence: Moderate
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options: --include-headers
virtual context
virtual org
virtual patch
virtual report
@r exists@
local idexpression struct dentry *dent;
expression E, E1;
statement S1, S2;
position p1, p2;
@@
(
if (!(dent@p1 = d_find_alias(...))) S1
|
dent@p1 = d_find_alias(...)
)
<...when != dput(dent)
when != if (...) { <+... dput(dent) ...+> }
when != true !dent || ...
when != dent = E
when != E = dent
if (!dent || ...) S2
...>
(
return <+...dent...+>;
|
return @p2 ...;
|
dent@p2 = E1;
|
E1 = dent;
)
@depends on context@
local idexpression struct dentry *r.dent;
position r.p1,r.p2;
@@
* dent@p1 = ...
...
(
* return@p2 ...;
|
* dent@p2
)
@script:python depends on org@
p1 << r.p1;
p2 << r.p2;
@@
cocci.print_main("Missing call to dput()",p1)
cocci.print_secs("",p2)
@depends on patch@
local idexpression struct dentry *r.dent;
position r.p2;
@@
(
+ dput(dent);
return @p2 ...;
|
+ dput(dent);
dent@p2 = ...;
)
@script:python depends on report@
p1 << r.p1;
p2 << r.p2;
@@
msg = "Missing call to dput() at line %s."
coccilib.report.print_report(p1[0], msg % (p2[0].line))
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// From Documentation/filesystems/sysfs.rst:
/// show() should only use sysfs_emit() or sysfs_emit_at() when formatting
/// the value to be returned to user space.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers
//
virtual report
virtual org
virtual context
virtual patch
@r depends on !patch@
identifier show, dev, attr, buf;
position p;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
* return snprintf@p(...);
...>
}
@rp depends on patch@
identifier show, dev, attr, buf;
expression BUF, SZ, FORMAT;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- snprintf(BUF, SZ, FORMAT
+ sysfs_emit(BUF, FORMAT
,...);
...>
}
@script: python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at")
@script: python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at")
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Use ERR_CAST inlined function instead of ERR_PTR(PTR_ERR(...))
///
// Confidence: High
// Copyright: (C) 2009, 2010 Nicolas Palix, DIKU.
// Copyright: (C) 2009, 2010 Julia Lawall, DIKU.
// Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6.
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options:
//
// Keywords: ERR_PTR, PTR_ERR, ERR_CAST
// Version min: 2.6.25
//
virtual context
virtual patch
virtual org
virtual report
@ depends on context && !patch && !org && !report@
expression x;
@@
* ERR_PTR(PTR_ERR(x))
@ depends on !context && patch && !org && !report @
expression x;
@@
- ERR_PTR(PTR_ERR(x))
+ ERR_CAST(x)
@r depends on !context && !patch && (org || report)@
expression x;
position p;
@@
ERR_PTR@p(PTR_ERR(x))
@script:python depends on org@
p << r.p;
x << r.x;
@@
msg="WARNING ERR_CAST can be used with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r.p;
x << r.x;
@@
msg="WARNING: ERR_CAST can be used with %s" % (x)
coccilib.report.print_report(p[0], msg)
@@ -0,0 +1,228 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Check that kvmalloc'ed memory is freed by kfree functions,
/// vmalloc'ed by vfree functions and kvmalloc'ed by kvfree
/// functions.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers
//
virtual patch
virtual report
virtual org
virtual context
@alloc@
expression E, E1;
position kok, vok;
@@
(
if (...) {
...
E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|
kmalloc_node\|kzalloc_node\|kmalloc_array\|
kmalloc_array_node\|kcalloc_node\)(...)@kok
...
} else {
...
E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|
vzalloc_node\|vmalloc_exec\|vmalloc_32\|
vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|
__vmalloc_node\)(...)@vok
...
}
|
E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(...)@kok
... when != E = E1
when any
if (E == NULL) {
...
E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|
vzalloc_node\|vmalloc_exec\|vmalloc_32\|
vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|
__vmalloc_node\)(...)@vok
...
}
)
@free@
expression E;
position fok;
@@
E = \(kvmalloc\|kvzalloc\|kvcalloc\|kvzalloc_node\|kvmalloc_node\|
kvmalloc_array\)(...)
...
kvfree(E)@fok
@vfree depends on !patch@
expression E;
position a != alloc.kok;
position f != free.fok;
@@
* E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|
* kzalloc_node\|kmalloc_array\|kmalloc_array_node\|
* kcalloc_node\)(...)@a
... when != if (...) { ... E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|__vmalloc_node\)(...); ... }
when != is_vmalloc_addr(E)
when any
* \(vfree\|vfree_atomic\|kvfree\)(E)@f
@depends on patch exists@
expression E;
position a != alloc.kok;
position f != free.fok;
@@
E = \(kmalloc\|kzalloc\|krealloc\|kcalloc\|kmalloc_node\|
kzalloc_node\|kmalloc_array\|kmalloc_array_node\|
kcalloc_node\)(...)@a
... when != if (...) { ... E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|__vmalloc_node_range\|__vmalloc_node\)(...); ... }
when != is_vmalloc_addr(E)
when any
- \(vfree\|vfree_atomic\|kvfree\)(E)@f
+ kfree(E)
@kfree depends on !patch@
expression E;
position a != alloc.vok;
position f != free.fok;
@@
* E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|
* vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|
* __vmalloc_node_range\|__vmalloc_node\)(...)@a
... when != is_vmalloc_addr(E)
when any
* \(kfree\|kfree_sensitive\|kvfree\)(E)@f
@depends on patch exists@
expression E;
position a != alloc.vok;
position f != free.fok;
@@
E = \(vmalloc\|vzalloc\|vmalloc_user\|vmalloc_node\|vzalloc_node\|
vmalloc_exec\|vmalloc_32\|vmalloc_32_user\|__vmalloc\|
__vmalloc_node_range\|__vmalloc_node\)(...)@a
... when != is_vmalloc_addr(E)
when any
- \(kfree\|kvfree\)(E)@f
+ vfree(E)
@kvfree depends on !patch@
expression E;
position a, f;
@@
* E = \(kvmalloc\|kvzalloc\|kvcalloc\|kvzalloc_node\|kvmalloc_node\|
* kvmalloc_array\)(...)@a
... when != is_vmalloc_addr(E)
when any
* \(kfree\|kfree_sensitive\|vfree\|vfree_atomic\)(E)@f
@depends on patch exists@
expression E;
@@
E = \(kvmalloc\|kvzalloc\|kvcalloc\|kvzalloc_node\|kvmalloc_node\|
kvmalloc_array\)(...)
... when != is_vmalloc_addr(E)
when any
- \(kfree\|vfree\)(E)
+ kvfree(E)
@kvfree_switch depends on !patch@
expression alloc.E;
position f;
@@
... when != is_vmalloc_addr(E)
when any
* \(kfree\|kfree_sensitive\|vfree\|vfree_atomic\)(E)@f
@depends on patch exists@
expression alloc.E;
position f;
@@
... when != is_vmalloc_addr(E)
when any
(
- \(kfree\|vfree\)(E)@f
+ kvfree(E)
|
- kfree_sensitive(E)@f
+ kvfree_sensitive(E)
)
@script: python depends on report@
a << vfree.a;
f << vfree.f;
@@
msg = "WARNING kmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.report.print_report(f[0], msg)
@script: python depends on org@
a << vfree.a;
f << vfree.f;
@@
msg = "WARNING kmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.org.print_todo(f[0], msg)
@script: python depends on report@
a << kfree.a;
f << kfree.f;
@@
msg = "WARNING vmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.report.print_report(f[0], msg)
@script: python depends on org@
a << kfree.a;
f << kfree.f;
@@
msg = "WARNING vmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.org.print_todo(f[0], msg)
@script: python depends on report@
a << kvfree.a;
f << kvfree.f;
@@
msg = "WARNING kvmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.report.print_report(f[0], msg)
@script: python depends on org@
a << kvfree.a;
f << kvfree.f;
@@
msg = "WARNING kvmalloc is used to allocate this memory at line %s" % (a[0].line)
coccilib.org.print_todo(f[0], msg)
@script: python depends on report@
ka << alloc.kok;
va << alloc.vok;
f << kvfree_switch.f;
@@
msg = "WARNING kmalloc (line %s) && vmalloc (line %s) are used to allocate this memory" % (ka[0].line, va[0].line)
coccilib.report.print_report(f[0], msg)
@script: python depends on org@
ka << alloc.kok;
va << alloc.vok;
f << kvfree_switch.f;
@@
msg = "WARNING kmalloc (line %s) && vmalloc (line %s) are used to allocate this memory" % (ka[0].line, va[0].line)
coccilib.org.print_todo(f[0], msg)
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Use kfree_sensitive, kvfree_sensitive rather than memset or
/// memzero_explicit followed by kfree.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers
//
// Keywords: kfree_sensitive, kvfree_sensitive
//
virtual context
virtual patch
virtual org
virtual report
@initialize:python@
@@
# kmalloc_oob_in_memset uses memset to explicitly trigger out-of-bounds access
filter = frozenset(['kmalloc_oob_in_memset',
'kfree_sensitive', 'kvfree_sensitive'])
def relevant(p):
return not (filter & {el.current_element for el in p})
@cond@
position ok;
@@
if (...)
\(memset@ok\|memzero_explicit@ok\)(...);
@r depends on !patch forall@
expression E;
position p : script:python() { relevant(p) };
position m != cond.ok;
type T;
@@
(
* memset@m((T)E, 0, ...);
|
* memzero_explicit@m((T)E, ...);
)
... when != E
when strict
* \(kfree\|vfree\|kvfree\)(E)@p;
@rp_memzero depends on patch@
expression E, size;
position p : script:python() { relevant(p) };
position m != cond.ok;
type T;
@@
- memzero_explicit@m((T)E, size);
... when != E
when strict
(
- kfree(E)@p;
+ kfree_sensitive(E);
|
- \(vfree\|kvfree\)(E)@p;
+ kvfree_sensitive(E, size);
)
@rp_memset depends on patch@
expression E, size;
position p : script:python() { relevant(p) };
position m != cond.ok;
type T;
@@
- memset@m((T)E, 0, size);
... when != E
when strict
(
- kfree(E)@p;
+ kfree_sensitive(E);
|
- \(vfree\|kvfree\)(E)@p;
+ kvfree_sensitive(E, size);
)
@script:python depends on report@
p << r.p;
m << r.m;
@@
msg = "WARNING opportunity for kfree_sensitive/kvfree_sensitive (memset at line %s)"
coccilib.report.print_report(p[0], msg % (m[0].line))
@script:python depends on org@
p << r.p;
m << r.m;
@@
msg = "WARNING opportunity for kfree_sensitive/kvfree_sensitive (memset at line %s)"
coccilib.org.print_todo(p[0], msg % (m[0].line))
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Use kmalloc_obj family of macros for allocations
///
// Confidence: High
// Options: --include-headers-for-types --all-includes --include-headers --keep-comments
virtual patch
@initialize:python@
@@
import sys
def alloc_array(name):
func = "FAILED_RENAME"
if name == "kmalloc_array":
func = "kmalloc_objs"
elif name == "kvmalloc_array":
func = "kvmalloc_objs"
elif name == "kcalloc":
func = "kzalloc_objs"
elif name == "kvcalloc":
func = "kvzalloc_objs"
else:
print(f"Unknown transform for {name}", file=sys.stderr)
return func
// This excludes anything that is assigning to or from integral types or
// string literals. Everything else gets the sizeof() extracted for the
// kmalloc_obj() type/var argument. sizeof(void *) is also excluded because
// it will need case-by-case double-checking to make sure the right type is
// being assigned.
@direct depends on patch && !(file in "tools") && !(file in "samples")@
typedef u8, u16, u32, u64;
typedef __u8, __u16, __u32, __u64;
typedef uint8_t, uint16_t, uint32_t, uint64_t;
typedef uchar, ushort, uint, ulong;
typedef __le16, __le32, __le64;
typedef __be16, __be32, __be64;
typedef wchar_t;
type INTEGRAL = {u8,__u8,uint8_t,char,unsigned char,uchar,wchar_t,
u16,__u16,uint16_t,unsigned short,ushort,
u32,__u32,uint32_t,unsigned int,uint,
u64,__u64,uint64_t,unsigned long,ulong,
__le16,__le32,__le64,__be16,__be32,__be64};
char [] STRING;
INTEGRAL *BYTES;
INTEGRAL **BYTES_PTRS;
type TYPE;
expression VAR;
expression GFP;
expression COUNT;
expression FLEX;
expression E;
identifier ALLOC =~ "^kv?[mz]alloc$";
fresh identifier ALLOC_OBJ = ALLOC ## "_obj";
fresh identifier ALLOC_FLEX = ALLOC ## "_flex";
identifier ALLOC_ARRAY = {kmalloc_array,kvmalloc_array,kcalloc,kvcalloc};
fresh identifier ALLOC_OBJS = script:python(ALLOC_ARRAY) { alloc_array(ALLOC_ARRAY) };
@@
(
- VAR = ALLOC((sizeof(*VAR)), GFP)
+ VAR = ALLOC_OBJ(*VAR, GFP)
|
ALLOC((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP)
|
BYTES = ALLOC((sizeof(E)), GFP)
|
BYTES = ALLOC((sizeof(TYPE)), GFP)
|
BYTES_PTRS = ALLOC((sizeof(E)), GFP)
|
BYTES_PTRS = ALLOC((sizeof(TYPE)), GFP)
|
ALLOC((sizeof(void *)), GFP)
|
- ALLOC((sizeof(E)), GFP)
+ ALLOC_OBJ(E, GFP)
|
- ALLOC((sizeof(TYPE)), GFP)
+ ALLOC_OBJ(TYPE, GFP)
|
ALLOC_ARRAY(COUNT, (\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP)
|
BYTES = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
|
BYTES = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
|
BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
|
BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
|
ALLOC_ARRAY((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), COUNT, GFP)
|
BYTES = ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
|
BYTES = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
|
BYTES_PTRS = ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
|
BYTES_PTRS = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
|
ALLOC_ARRAY(COUNT, (sizeof(void *)), GFP)
|
ALLOC_ARRAY((sizeof(void *)), COUNT, GFP)
|
- ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
+ ALLOC_OBJS(E, COUNT, GFP)
|
- ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
+ ALLOC_OBJS(TYPE, COUNT, GFP)
|
- ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
+ ALLOC_OBJS(E, COUNT, GFP)
|
- ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
+ ALLOC_OBJS(TYPE, COUNT, GFP)
|
- ALLOC(struct_size(VAR, FLEX, COUNT), GFP)
+ ALLOC_FLEX(*VAR, FLEX, COUNT, GFP)
|
- ALLOC(struct_size_t(TYPE, FLEX, COUNT), GFP)
+ ALLOC_FLEX(TYPE, FLEX, COUNT, GFP)
)
@drop_gfp_kernel depends on patch && !(file in "tools") && !(file in "samples")@
identifier ALLOC = {kmalloc_obj,kmalloc_objs,kmalloc_flex,
kzalloc_obj,kzalloc_objs,kzalloc_flex,
kvmalloc_obj,kvmalloc_objs,kvmalloc_flex,
kvzalloc_obj,kvzalloc_objs,kvzalloc_flex};
@@
ALLOC(...
- , GFP_KERNEL
)
@@ -0,0 +1,105 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Use kstrdup rather than duplicating its implementation
///
// Confidence: High
// Copyright: (C) 2010-2012 Nicolas Palix.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Comments:
// Options: --no-includes --include-headers
virtual patch
virtual context
virtual org
virtual report
@depends on patch@
expression from,to;
expression flag,E1,E2;
statement S;
@@
- to = kmalloc(strlen(from) + 1,flag);
+ to = kstrdup(from, flag);
... when != \(from = E1 \| to = E1 \)
if (to==NULL || ...) S
... when != \(from = E2 \| to = E2 \)
- strcpy(to, from);
@depends on patch@
expression x,from,to;
expression flag,E1,E2,E3;
statement S;
@@
- x = strlen(from) + 1;
... when != \( x = E1 \| from = E1 \)
- to = \(kmalloc\|kzalloc\)(x,flag);
+ to = kstrdup(from, flag);
... when != \(x = E2 \| from = E2 \| to = E2 \)
if (to==NULL || ...) S
... when != \(x = E3 \| from = E3 \| to = E3 \)
- memcpy(to, from, x);
// ---------------------------------------------------------------------
@r1 depends on !patch exists@
expression from,to;
expression flag,E1,E2;
statement S;
position p1,p2;
@@
* to = kmalloc@p1(strlen(from) + 1,flag);
... when != \(from = E1 \| to = E1 \)
if (to==NULL || ...) S
... when != \(from = E2 \| to = E2 \)
* strcpy@p2(to, from);
@r2 depends on !patch exists@
expression x,from,to;
expression flag,E1,E2,E3;
statement S;
position p1,p2;
@@
* x = strlen(from) + 1;
... when != \( x = E1 \| from = E1 \)
* to = \(kmalloc@p1\|kzalloc@p1\)(x,flag);
... when != \(x = E2 \| from = E2 \| to = E2 \)
if (to==NULL || ...) S
... when != \(x = E3 \| from = E3 \| to = E3 \)
* memcpy@p2(to, from, x);
@script:python depends on org@
p1 << r1.p1;
p2 << r1.p2;
@@
cocci.print_main("WARNING opportunity for kstrdup",p1)
cocci.print_secs("strcpy",p2)
@script:python depends on org@
p1 << r2.p1;
p2 << r2.p2;
@@
cocci.print_main("WARNING opportunity for kstrdup",p1)
cocci.print_secs("memcpy",p2)
@script:python depends on report@
p1 << r1.p1;
p2 << r1.p2;
@@
msg = "WARNING opportunity for kstrdup (strcpy on line %s)" % (p2[0].line)
coccilib.report.print_report(p1[0], msg)
@script:python depends on report@
p1 << r2.p1;
p2 << r2.p2;
@@
msg = "WARNING opportunity for kstrdup (memcpy on line %s)" % (p2[0].line)
coccilib.report.print_report(p1[0], msg)
@@ -0,0 +1,256 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Find if/else condition with kmalloc/vmalloc calls.
/// Suggest to use kvmalloc instead. Same for kvfree.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers
//
virtual patch
virtual report
virtual org
virtual context
@initialize:python@
@@
filter = frozenset(['kvfree'])
def relevant(p):
return not (filter & {el.current_element for el in p})
@kvmalloc depends on !patch@
expression E, E1, size;
identifier flags;
binary operator cmp = {<=, <, ==, >, >=};
identifier x;
type T;
position p;
@@
(
* if (size cmp E1 || ...)@p {
...
* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
* (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
...
} else {
...
* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
...
}
|
* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
* (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
... when != E = E1
when != size = E1
when any
* if (E == NULL)@p {
...
* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
...
}
|
* T x = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
* (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...);
... when != x = E1
when != size = E1
when any
* if (x == NULL)@p {
...
* x = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
...
}
)
@kvfree depends on !patch@
expression E;
position p : script:python() { relevant(p) };
@@
* if (is_vmalloc_addr(E))@p {
...
* vfree(E)
...
} else {
... when != krealloc(E, ...)
when any
* \(kfree\|kfree_sensitive\)(E)
...
}
@depends on patch@
expression E, E1, size, node;
binary operator cmp = {<=, <, ==, >, >=};
identifier flags, x;
type T;
@@
(
- if (size cmp E1)
- E = kmalloc(size, flags);
- else
- E = vmalloc(size);
+ E = kvmalloc(size, flags);
|
- if (size cmp E1)
- E = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- else
- E = vmalloc(size);
+ E = kvmalloc(size, GFP_KERNEL);
|
- E = kmalloc(size, flags | __GFP_NOWARN);
- if (E == NULL)
- E = vmalloc(size);
+ E = kvmalloc(size, flags);
|
- E = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- if (E == NULL)
- E = vmalloc(size);
+ E = kvmalloc(size, GFP_KERNEL);
|
- T x = kmalloc(size, flags | __GFP_NOWARN);
- if (x == NULL)
- x = vmalloc(size);
+ T x = kvmalloc(size, flags);
|
- T x = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- if (x == NULL)
- x = vmalloc(size);
+ T x = kvmalloc(size, GFP_KERNEL);
|
- if (size cmp E1)
- E = kzalloc(size, flags);
- else
- E = vzalloc(size);
+ E = kvzalloc(size, flags);
|
- if (size cmp E1)
- E = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- else
- E = vzalloc(size);
+ E = kvzalloc(size, GFP_KERNEL);
|
- E = kzalloc(size, flags | __GFP_NOWARN);
- if (E == NULL)
- E = vzalloc(size);
+ E = kvzalloc(size, flags);
|
- E = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- if (E == NULL)
- E = vzalloc(size);
+ E = kvzalloc(size, GFP_KERNEL);
|
- T x = kzalloc(size, flags | __GFP_NOWARN);
- if (x == NULL)
- x = vzalloc(size);
+ T x = kvzalloc(size, flags);
|
- T x = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
- if (x == NULL)
- x = vzalloc(size);
+ T x = kvzalloc(size, GFP_KERNEL);
|
- if (size cmp E1)
- E = kmalloc_node(size, flags, node);
- else
- E = vmalloc_node(size, node);
+ E = kvmalloc_node(size, flags, node);
|
- if (size cmp E1)
- E = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- else
- E = vmalloc_node(size, node);
+ E = kvmalloc_node(size, GFP_KERNEL, node);
|
- E = kmalloc_node(size, flags | __GFP_NOWARN, node);
- if (E == NULL)
- E = vmalloc_node(size, node);
+ E = kvmalloc_node(size, flags, node);
|
- E = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- if (E == NULL)
- E = vmalloc_node(size, node);
+ E = kvmalloc_node(size, GFP_KERNEL, node);
|
- T x = kmalloc_node(size, flags | __GFP_NOWARN, node);
- if (x == NULL)
- x = vmalloc_node(size, node);
+ T x = kvmalloc_node(size, flags, node);
|
- T x = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- if (x == NULL)
- x = vmalloc_node(size, node);
+ T x = kvmalloc_node(size, GFP_KERNEL, node);
|
- if (size cmp E1)
- E = kvzalloc_node(size, flags, node);
- else
- E = vzalloc_node(size, node);
+ E = kvzalloc_node(size, flags, node);
|
- if (size cmp E1)
- E = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- else
- E = vzalloc_node(size, node);
+ E = kvzalloc_node(size, GFP_KERNEL, node);
|
- E = kvzalloc_node(size, flags | __GFP_NOWARN, node);
- if (E == NULL)
- E = vzalloc_node(size, node);
+ E = kvzalloc_node(size, flags, node);
|
- E = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- if (E == NULL)
- E = vzalloc_node(size, node);
+ E = kvzalloc_node(size, GFP_KERNEL, node);
|
- T x = kvzalloc_node(size, flags | __GFP_NOWARN, node);
- if (x == NULL)
- x = vzalloc_node(size, node);
+ T x = kvzalloc_node(size, flags, node);
|
- T x = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
- if (x == NULL)
- x = vzalloc_node(size, node);
+ T x = kvzalloc_node(size, GFP_KERNEL, node);
)
@depends on patch@
expression E;
position p : script:python() { relevant(p) };
@@
- if (is_vmalloc_addr(E))@p
- vfree(E);
- else
- kfree(E);
+ kvfree(E);
@script: python depends on report@
p << kvmalloc.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for kvmalloc")
@script: python depends on org@
p << kvmalloc.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for kvmalloc")
@script: python depends on report@
p << kvfree.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for kvfree")
@script: python depends on org@
p << kvfree.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for kvfree")
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Use kmemdup rather than duplicating its implementation
///
// Confidence: High
// Copyright: (C) 2010-2012 Nicolas Palix.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Comments:
// Options: --no-includes --include-headers
virtual patch
virtual context
virtual org
virtual report
@r1@
expression from,to;
expression flag;
position p;
@@
to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
@r2@
expression x,from,to;
expression flag,E1;
position p;
@@
x = strlen(from) + 1;
... when != \( x = E1 \| from = E1 \)
to = \(kmalloc@p\|kzalloc@p\)(x,flag);
@depends on patch@
expression from,to,size,flag;
position p != {r1.p,r2.p};
statement S;
@@
- to = \(kmalloc@p\|kzalloc@p\)(size,flag);
+ to = kmemdup(from,size,flag);
if (to==NULL || ...) S
- memcpy(to, from, size);
@r depends on !patch@
expression from,to,size,flag;
position p != {r1.p,r2.p};
statement S;
@@
* to = \(kmalloc@p\|kzalloc@p\)(size,flag);
if (to==NULL || ...) S
* memcpy(to, from, size);
@script:python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
@script:python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Use memdup_user rather than duplicating its implementation
/// This is a little bit restricted to reduce false positives
///
// Confidence: High
// Copyright: (C) 2010-2012 Nicolas Palix.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Comments:
// Options: --no-includes --include-headers
virtual patch
virtual context
virtual org
virtual report
@initialize:python@
@@
filter = frozenset(['memdup_user', 'vmemdup_user'])
def relevant(p):
return not (filter & {el.current_element for el in p})
@depends on patch@
expression from,to,size;
identifier l1,l2;
position p : script:python() { relevant(p) };
@@
- to = \(kmalloc@p\|kzalloc@p\)
- (size,\(GFP_KERNEL\|GFP_USER\|
- \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
+ to = memdup_user(from,size);
if (
- to==NULL
+ IS_ERR(to)
|| ...) {
<+... when != goto l1;
- -ENOMEM
+ PTR_ERR(to)
...+>
}
- if (copy_from_user(to, from, size) != 0) {
- <+... when != goto l2;
- -EFAULT
- ...+>
- }
@depends on patch@
expression from,to,size;
identifier l1,l2;
position p : script:python() { relevant(p) };
@@
- to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
+ to = vmemdup_user(from,size);
if (
- to==NULL
+ IS_ERR(to)
|| ...) {
<+... when != goto l1;
- -ENOMEM
+ PTR_ERR(to)
...+>
}
- if (copy_from_user(to, from, size) != 0) {
- <+... when != goto l2;
- -EFAULT
- ...+>
- }
@r depends on !patch@
expression from,to,size;
position p : script:python() { relevant(p) };
statement S1,S2;
@@
* to = \(kmalloc@p\|kzalloc@p\)
(size,\(GFP_KERNEL\|GFP_USER\|
\(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
if (to==NULL || ...) S1
if (copy_from_user(to, from, size) != 0)
S2
@rv depends on !patch@
expression from,to,size;
position p : script:python() { relevant(p) };
statement S1,S2;
@@
* to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
if (to==NULL || ...) S1
if (copy_from_user(to, from, size) != 0)
S2
@script:python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for memdup_user")
@script:python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user")
@script:python depends on org@
p << rv.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for vmemdup_user")
@script:python depends on report@
p << rv.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for vmemdup_user")
@@ -0,0 +1,102 @@
// SPDX-License-Identifier: GPL-2.0
/// Remove dev_err() messages after platform_get_irq*() failures
//
// Confidence: Medium
// Options: --include-headers
virtual patch
virtual context
virtual org
virtual report
@depends on context@
expression ret;
struct platform_device *E;
@@
ret =
(
platform_get_irq
|
platform_get_irq_byname
)(E, ...);
if ( \( ret < 0 \| ret <= 0 \) )
{
(
if (ret != -EPROBE_DEFER)
{ ...
*dev_err(...);
... }
|
...
*dev_err(...);
)
...
}
@depends on patch@
expression ret;
struct platform_device *E;
@@
ret =
(
platform_get_irq
|
platform_get_irq_byname
)(E, ...);
if ( \( ret < 0 \| ret <= 0 \) )
{
(
-if (ret != -EPROBE_DEFER)
-{ ...
-dev_err(...);
-... }
|
...
-dev_err(...);
)
...
}
@r depends on org || report@
position p1;
expression ret;
struct platform_device *E;
@@
ret =
(
platform_get_irq
|
platform_get_irq_byname
)(E, ...);
if ( \( ret < 0 \| ret <= 0 \) )
{
(
if (ret != -EPROBE_DEFER)
{ ...
dev_err@p1(...);
... }
|
...
dev_err@p1(...);
)
...
}
@script:python depends on org@
p1 << r.p1;
@@
cocci.print_main(p1)
@script:python depends on report@
p1 << r.p1;
@@
msg = "line %s is redundant because platform_get_irq() already prints an error" % (p1[0].line)
coccilib.report.print_report(p1[0],msg)
@@ -0,0 +1,189 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Remove .owner field if calls are used which set it automatically
///
// Confidence: High
// Copyright: (C) 2014 Wolfram Sang.
virtual patch
virtual context
virtual org
virtual report
@match1@
declarer name builtin_i2c_driver;
declarer name builtin_platform_driver;
declarer name builtin_platform_driver_probe;
declarer name module_i2c_driver;
declarer name module_platform_driver;
declarer name module_platform_driver_probe;
identifier __driver;
@@
(
builtin_i2c_driver(__driver);
|
builtin_platform_driver(__driver);
|
builtin_platform_driver_probe(__driver, ...);
|
module_i2c_driver(__driver);
|
module_platform_driver(__driver);
|
module_platform_driver_probe(__driver, ...);
)
@fix1 depends on match1 && patch && !context && !org && !report@
identifier match1.__driver;
@@
static struct platform_driver __driver = {
.driver = {
- .owner = THIS_MODULE,
}
};
@fix1_i2c depends on match1 && patch && !context && !org && !report@
identifier match1.__driver;
@@
static struct i2c_driver __driver = {
.driver = {
- .owner = THIS_MODULE,
}
};
@match2@
identifier __driver;
@@
(
platform_driver_register(&__driver)
|
platform_driver_probe(&__driver, ...)
|
platform_create_bundle(&__driver, ...)
|
i2c_add_driver(&__driver)
)
@fix2 depends on match2 && patch && !context && !org && !report@
identifier match2.__driver;
@@
static struct platform_driver __driver = {
.driver = {
- .owner = THIS_MODULE,
}
};
@fix2_i2c depends on match2 && patch && !context && !org && !report@
identifier match2.__driver;
@@
static struct i2c_driver __driver = {
.driver = {
- .owner = THIS_MODULE,
}
};
// ----------------------------------------------------------------------------
@fix1_context depends on match1 && !patch && (context || org || report)@
identifier match1.__driver;
position j0;
@@
static struct platform_driver __driver = {
.driver = {
* .owner@j0 = THIS_MODULE,
}
};
@fix1_i2c_context depends on match1 && !patch && (context || org || report)@
identifier match1.__driver;
position j0;
@@
static struct i2c_driver __driver = {
.driver = {
* .owner@j0 = THIS_MODULE,
}
};
@fix2_context depends on match2 && !patch && (context || org || report)@
identifier match2.__driver;
position j0;
@@
static struct platform_driver __driver = {
.driver = {
* .owner@j0 = THIS_MODULE,
}
};
@fix2_i2c_context depends on match2 && !patch && (context || org || report)@
identifier match2.__driver;
position j0;
@@
static struct i2c_driver __driver = {
.driver = {
* .owner@j0 = THIS_MODULE,
}
};
// ----------------------------------------------------------------------------
@script:python fix1_org depends on org@
j0 << fix1_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.org.print_todo(j0[0], msg)
@script:python fix1_i2c_org depends on org@
j0 << fix1_i2c_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.org.print_todo(j0[0], msg)
@script:python fix2_org depends on org@
j0 << fix2_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.org.print_todo(j0[0], msg)
@script:python fix2_i2c_org depends on org@
j0 << fix2_i2c_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.org.print_todo(j0[0], msg)
// ----------------------------------------------------------------------------
@script:python fix1_report depends on report@
j0 << fix1_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.report.print_report(j0[0], msg)
@script:python fix1_i2c_report depends on report@
j0 << fix1_i2c_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.report.print_report(j0[0], msg)
@script:python fix2_report depends on report@
j0 << fix2_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.report.print_report(j0[0], msg)
@script:python fix2_i2c_report depends on report@
j0 << fix2_i2c_context.j0;
@@
msg = "No need to set .owner here. The core will do it."
coccilib.report.print_report(j0[0], msg)
@@ -0,0 +1,113 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Make sure pm_runtime_* calls do not unnecessarily use IS_ERR_VALUE
///
// Keywords: pm_runtime
// Confidence: Medium
// Copyright (C) 2013 Texas Instruments Incorporated -
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options: --include-headers
virtual patch
virtual context
virtual org
virtual report
//----------------------------------------------------------
// Detection
//----------------------------------------------------------
@runtime_bad_err_handle exists@
expression ret;
position p;
@@
(
ret@p = \(pm_runtime_idle\|
pm_runtime_suspend\|
pm_runtime_autosuspend\|
pm_runtime_resume\|
pm_request_idle\|
pm_request_resume\|
pm_request_autosuspend\|
pm_runtime_get\|
pm_runtime_get_sync\|
pm_runtime_put\|
pm_runtime_put_autosuspend\|
pm_runtime_put_sync\|
pm_runtime_put_sync_suspend\|
pm_runtime_put_sync_autosuspend\|
pm_runtime_set_active\|
pm_schedule_suspend\|
pm_generic_runtime_suspend\|
pm_generic_runtime_resume\)(...);
...
IS_ERR_VALUE(ret)
...
)
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@depends on context@
identifier pm_runtime_api;
expression ret;
position runtime_bad_err_handle.p;
@@
(
ret@p = pm_runtime_api(...);
...
* IS_ERR_VALUE(ret)
...
)
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@depends on patch@
identifier pm_runtime_api;
expression ret;
position runtime_bad_err_handle.p;
@@
(
ret@p = pm_runtime_api(...);
...
- IS_ERR_VALUE(ret)
+ ret < 0
...
)
//----------------------------------------------------------
// For org and report mode
//----------------------------------------------------------
@r depends on (org || report) exists@
position p1, p2;
identifier pm_runtime_api;
expression ret;
position runtime_bad_err_handle.p;
@@
(
ret@p = pm_runtime_api@p1(...);
...
IS_ERR_VALUE@p2(ret)
...
)
@script:python depends on org@
p1 << r.p1;
p2 << r.p2;
pm_runtime_api << r.pm_runtime_api;
@@
cocci.print_main(pm_runtime_api,p1)
cocci.print_secs("IS_ERR_VALUE",p2)
@script:python depends on report@
p1 << r.p1;
p2 << r.p2;
pm_runtime_api << r.pm_runtime_api;
@@
msg = "%s returns < 0 as error. Unnecessary IS_ERR_VALUE at line %s" % (pm_runtime_api, p2[0].line)
coccilib.report.print_report(p1[0],msg)
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: GPL-2.0-only
///
/// Use resource_size function on resource object
/// instead of explicit computation.
///
// Confidence: High
// Copyright: (C) 2009, 2010 Nicolas Palix, DIKU.
// Copyright: (C) 2009, 2010 Julia Lawall, DIKU.
// Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6.
// URL: https://coccinelle.gitlabpages.inria.fr/website
// Options:
//
// Keywords: resource_size
// Version min: 2.6.27 resource_size
//
virtual context
virtual patch
virtual org
virtual report
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@r_context depends on context && !patch && !org@
struct resource *res;
@@
* (res->end - res->start) + 1
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@r_patch depends on !context && patch && !org@
struct resource *res;
@@
- (res->end - res->start) + 1
+ resource_size(res)
//----------------------------------------------------------
// For org mode
//----------------------------------------------------------
@r_org depends on !context && !patch && (org || report)@
struct resource *res;
position p;
@@
(res->end@p - res->start) + 1
@rbad_org depends on !context && !patch && (org || report)@
struct resource *res;
position p != r_org.p;
@@
res->end@p - res->start
@script:python depends on org@
p << r_org.p;
x << r_org.res;
@@
msg="ERROR with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << r_org.p;
x << r_org.res;
@@
msg="ERROR: Missing resource_size with %s" % (x)
coccilib.report.print_report(p[0], msg)
@script:python depends on org@
p << rbad_org.p;
x << rbad_org.res;
@@
msg="WARNING with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)
@script:python depends on report@
p << rbad_org.p;
x << rbad_org.res;
@@
msg="WARNING: Suspicious code. resource_size is maybe missing with %s" % (x)
coccilib.report.print_report(p[0], msg)
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0
/// Remove an open coded simple_open() function
/// and replace file operations references to the function
/// with simple_open() instead.
///
// Confidence: High
// Comments:
// Options: --no-includes --include-headers
virtual patch
virtual report
@ open depends on patch @
identifier open_f != simple_open;
identifier i, f;
@@
-int open_f(struct inode *i, struct file *f)
-{
(
-if (i->i_private)
-f->private_data = i->i_private;
|
-f->private_data = i->i_private;
)
-return 0;
-}
@ has_open depends on open @
identifier fops;
identifier open.open_f;
@@
struct file_operations fops = {
...,
-.open = open_f,
+.open = simple_open,
...
};
@ openr depends on report @
identifier open_f != simple_open;
identifier i, f;
position p;
@@
int open_f@p(struct inode *i, struct file *f)
{
(
if (i->i_private)
f->private_data = i->i_private;
|
f->private_data = i->i_private;
)
return 0;
}
@ has_openr depends on openr @
identifier fops;
identifier openr.open_f;
position p;
@@
struct file_operations fops = {
...,
.open = open_f@p,
...
};
@script:python@
pf << openr.p;
ps << has_openr.p;
@@
coccilib.report.print_report(pf[0],"WARNING opportunity for simple_open, see also structure on line %s"%(ps[0].line))
@@ -0,0 +1,369 @@
// SPDX-License-Identifier: GPL-2.0
// Author: Kirill Smelkov (kirr@nexedi.com)
//
// Search for stream-like files that are using nonseekable_open and convert
// them to stream_open. A stream-like file is a file that does not use ppos in
// its read and write. Rationale for the conversion is to avoid deadlock in
// between read and write.
virtual report
virtual patch
virtual explain // explain decisions in the patch (SPFLAGS="-D explain")
// stream-like reader & writer - ones that do not depend on f_pos.
@ stream_reader @
identifier readstream, ppos;
identifier f, buf, len;
type loff_t;
@@
ssize_t readstream(struct file *f, char *buf, size_t len, loff_t *ppos)
{
... when != ppos
}
@ stream_writer @
identifier writestream, ppos;
identifier f, buf, len;
type loff_t;
@@
ssize_t writestream(struct file *f, const char *buf, size_t len, loff_t *ppos)
{
... when != ppos
}
// a function that blocks
@ blocks @
identifier block_f;
identifier wait =~ "^wait_.*";
@@
block_f(...) {
... when exists
wait(...)
... when exists
}
// stream_reader that can block inside.
//
// XXX wait_* can be called not directly from current function (e.g. func -> f -> g -> wait())
// XXX currently reader_blocks supports only direct and 1-level indirect cases.
@ reader_blocks_direct @
identifier stream_reader.readstream;
identifier wait =~ "^wait_.*";
@@
readstream(...)
{
... when exists
wait(...)
... when exists
}
@ reader_blocks_1 @
identifier stream_reader.readstream;
identifier blocks.block_f;
@@
readstream(...)
{
... when exists
block_f(...)
... when exists
}
@ reader_blocks depends on reader_blocks_direct || reader_blocks_1 @
identifier stream_reader.readstream;
@@
readstream(...) {
...
}
// file_operations + whether they have _any_ .read, .write, .llseek ... at all.
//
// XXX add support for file_operations xxx[N] = ... (sound/core/pcm_native.c)
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_read @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
.read = read_f,
};
@ has_read_iter @
identifier fops0.fops;
identifier read_iter_f;
@@
struct file_operations fops = {
.read_iter = read_iter_f,
};
@ has_write @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
.write = write_f,
};
@ has_write_iter @
identifier fops0.fops;
identifier write_iter_f;
@@
struct file_operations fops = {
.write_iter = write_iter_f,
};
@ has_llseek @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
.llseek = llseek_f,
};
@ has_no_llseek @
identifier fops0.fops;
@@
struct file_operations fops = {
};
@ has_noop_llseek @
identifier fops0.fops;
@@
struct file_operations fops = {
.llseek = noop_llseek,
};
@ has_mmap @
identifier fops0.fops;
identifier mmap_f;
@@
struct file_operations fops = {
.mmap = mmap_f,
};
@ has_copy_file_range @
identifier fops0.fops;
identifier copy_file_range_f;
@@
struct file_operations fops = {
.copy_file_range = copy_file_range_f,
};
@ has_remap_file_range @
identifier fops0.fops;
identifier remap_file_range_f;
@@
struct file_operations fops = {
.remap_file_range = remap_file_range_f,
};
@ has_splice_read @
identifier fops0.fops;
identifier splice_read_f;
@@
struct file_operations fops = {
.splice_read = splice_read_f,
};
@ has_splice_write @
identifier fops0.fops;
identifier splice_write_f;
@@
struct file_operations fops = {
.splice_write = splice_write_f,
};
// file_operations that is candidate for stream_open conversion - it does not
// use mmap and other methods that assume @offset access to file.
//
// XXX for simplicity require no .{read/write}_iter and no .splice_{read/write} for now.
// XXX maybe_steam.fops cannot be used in other rules - it gives "bad rule maybe_stream or bad variable fops".
@ maybe_stream depends on (!has_llseek || has_no_llseek || has_noop_llseek) && !has_mmap && !has_copy_file_range && !has_remap_file_range && !has_read_iter && !has_write_iter && !has_splice_read && !has_splice_write @
identifier fops0.fops;
@@
struct file_operations fops = {
};
// ---- conversions ----
// XXX .open = nonseekable_open -> .open = stream_open
// XXX .open = func -> openfunc -> nonseekable_open
// read & write
//
// if both are used in the same file_operations together with an opener -
// under that conditions we can use stream_open instead of nonseekable_open.
@ fops_rw depends on maybe_stream @
identifier fops0.fops, openfunc;
identifier stream_reader.readstream;
identifier stream_writer.writestream;
@@
struct file_operations fops = {
.open = openfunc,
.read = readstream,
.write = writestream,
};
@ report_rw depends on report @
identifier fops_rw.openfunc;
position p1;
@@
openfunc(...) {
<...
nonseekable_open@p1
...>
}
@ script:python depends on report && reader_blocks @
fops << fops0.fops;
p << report_rw.p1;
@@
coccilib.report.print_report(p[0],
"ERROR: %s: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix." % (fops,))
@ script:python depends on report && !reader_blocks @
fops << fops0.fops;
p << report_rw.p1;
@@
coccilib.report.print_report(p[0],
"WARNING: %s: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
@ explain_rw_deadlocked depends on explain && reader_blocks @
identifier fops_rw.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ nonseekable_open /* read & write (was deadlock) */
...>
}
@ explain_rw_nodeadlock depends on explain && !reader_blocks @
identifier fops_rw.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ nonseekable_open /* read & write (no direct deadlock) */
...>
}
@ patch_rw depends on patch @
identifier fops_rw.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ stream_open
...>
}
// read, but not write
@ fops_r depends on maybe_stream && !has_write @
identifier fops0.fops, openfunc;
identifier stream_reader.readstream;
@@
struct file_operations fops = {
.open = openfunc,
.read = readstream,
};
@ report_r depends on report @
identifier fops_r.openfunc;
position p1;
@@
openfunc(...) {
<...
nonseekable_open@p1
...>
}
@ script:python depends on report @
fops << fops0.fops;
p << report_r.p1;
@@
coccilib.report.print_report(p[0],
"WARNING: %s: .read() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
@ explain_r depends on explain @
identifier fops_r.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ nonseekable_open /* read only */
...>
}
@ patch_r depends on patch @
identifier fops_r.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ stream_open
...>
}
// write, but not read
@ fops_w depends on maybe_stream && !has_read @
identifier fops0.fops, openfunc;
identifier stream_writer.writestream;
@@
struct file_operations fops = {
.open = openfunc,
.write = writestream,
};
@ report_w depends on report @
identifier fops_w.openfunc;
position p1;
@@
openfunc(...) {
<...
nonseekable_open@p1
...>
}
@ script:python depends on report @
fops << fops0.fops;
p << report_w.p1;
@@
coccilib.report.print_report(p[0],
"WARNING: %s: .write() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
@ explain_w depends on explain @
identifier fops_w.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ nonseekable_open /* write only */
...>
}
@ patch_w depends on patch @
identifier fops_w.openfunc;
@@
openfunc(...) {
<...
- nonseekable_open
+ stream_open
...>
}
// no read, no write - don't change anything
@@ -0,0 +1,302 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Find places to use string_choices.h's various helpers.
//
// Confidence: Medium
// Options: --no-includes --include-headers
virtual patch
virtual context
virtual report
@str_plural depends on patch@
expression E;
@@
(
- ((E == 1) ? "" : "s")
+ str_plural(E)
|
- ((E > 1) ? "s" : "")
+ str_plural(E)
)
@str_plural_r depends on !patch@
expression E;
position P;
@@
(
* (E@P == 1) ? "" : "s"
|
* (E@P > 1) ? "s" : ""
)
@script:python depends on report@
p << str_plural_r.P;
e << str_plural_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
@str_up_down depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "up" : "down")
+ str_up_down(E)
@str_up_down_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "up" : "down"
@script:python depends on report@
p << str_up_down_r.P;
e << str_up_down_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
@str_down_up depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "down" : "up")
+ str_down_up(E)
@str_down_up_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "down" : "up"
@script:python depends on report@
p << str_down_up_r.P;
e << str_down_up_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e)
@str_true_false depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "true" : "false")
+ str_true_false(E)
@str_true_false_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "true" : "false"
@script:python depends on report@
p << str_true_false_r.P;
e << str_true_false_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
@str_false_true depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "false" : "true")
+ str_false_true(E)
@str_false_true_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "false" : "true"
@script:python depends on report@
p << str_false_true_r.P;
e << str_false_true_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
@str_hi_lo depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "hi" : "lo")
+ str_hi_lo(E)
@str_hi_lo_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "hi" : "lo"
@script:python depends on report@
p << str_hi_lo_r.P;
e << str_hi_lo_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_hi_lo(%s)" % e)
@str_high_low depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "high" : "low")
+ str_high_low(E)
@str_high_low_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "high" : "low"
@script:python depends on report@
p << str_high_low_r.P;
e << str_high_low_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_high_low(%s)" % e)
@str_lo_hi depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "lo" : "hi")
+ str_lo_hi(E)
@str_lo_hi_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "lo" : "hi"
@script:python depends on report@
p << str_lo_hi_r.P;
e << str_lo_hi_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_lo_hi(%s)" % e)
@str_low_high depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "low" : "high")
+ str_low_high(E)
@str_low_high_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "low" : "high"
@script:python depends on report@
p << str_low_high_r.P;
e << str_low_high_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_low_high(%s)" % e)
@str_enable_disable depends on patch@
expression E;
@@
- ((E) ? "enable" : "disable")
+ str_enable_disable(E)
@str_enable_disable_r depends on !patch@
expression E;
position P;
@@
* E@P ? "enable" : "disable"
@script:python depends on report@
p << str_enable_disable_r.P;
e << str_enable_disable_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_enable_disable(%s)" % e)
@str_enabled_disabled depends on patch@
expression E;
@@
- ((E) ? "enabled" : "disabled")
+ str_enabled_disabled(E)
@str_enabled_disabled_r depends on !patch@
expression E;
position P;
@@
* E@P ? "enabled" : "disabled"
@script:python depends on report@
p << str_enabled_disabled_r.P;
e << str_enabled_disabled_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
@str_read_write depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "read" : "write")
+ str_read_write(E)
@str_read_write_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "read" : "write"
@script:python depends on report@
p << str_read_write_r.P;
e << str_read_write_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_read_write(%s)" % e)
@str_write_read depends on patch disable neg_if_exp@
expression E;
@@
- ((E) ? "write" : "read")
+ str_write_read(E)
@str_write_read_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
* E@P ? "write" : "read"
@script:python depends on report@
p << str_write_read_r.P;
e << str_write_read_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_write_read(%s)" % e)
@str_on_off depends on patch@
expression E;
@@
- ((E) ? "on" : "off")
+ str_on_off(E)
@str_on_off_r depends on !patch@
expression E;
position P;
@@
* E@P ? "on" : "off"
@script:python depends on report@
p << str_on_off_r.P;
e << str_on_off_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
@str_yes_no depends on patch@
expression E;
@@
- ((E) ? "yes" : "no")
+ str_yes_no(E)
@str_yes_no_r depends on !patch@
expression E;
position P;
@@
* E@P ? "yes" : "no"
@script:python depends on report@
p << str_yes_no_r.P;
e << str_yes_no_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-2.0
///
/// Use vma_pages function on vma object instead of explicit computation.
///
// Confidence: High
// Keywords: vma_pages vma
// Comment: Based on resource_size.cocci
virtual context
virtual patch
virtual org
virtual report
//----------------------------------------------------------
// For context mode
//----------------------------------------------------------
@r_context depends on context && !patch && !org && !report@
struct vm_area_struct *vma;
@@
* (vma->vm_end - vma->vm_start) >> PAGE_SHIFT
//----------------------------------------------------------
// For patch mode
//----------------------------------------------------------
@r_patch depends on !context && patch && !org && !report@
struct vm_area_struct *vma;
@@
- ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)
+ vma_pages(vma)
//----------------------------------------------------------
// For org mode
//----------------------------------------------------------
@r_org depends on !context && !patch && (org || report)@
struct vm_area_struct *vma;
position p;
@@
(vma->vm_end@p - vma->vm_start) >> PAGE_SHIFT
@script:python depends on report@
p << r_org.p;
x << r_org.vma;
@@
msg="WARNING: Consider using vma_pages helper on %s" % (x)
coccilib.report.print_report(p[0], msg)
@script:python depends on org@
p << r_org.p;
x << r_org.vma;
@@
msg="WARNING: Consider using vma_pages helper on %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)