-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_sizeof_allocations.cocci
121 lines (108 loc) · 2.83 KB
/
complex_sizeof_allocations.cocci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: GPL-2.0-only
/// Detect allocation that create custom objects by combining different sizeof()
/// and mark the types with stores to __uncontained_complex_alloc easily parsable
/// by llvm
///
// Confidence: Moderate
// Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
// Copyright: (C) 2012 Gilles Muller, INRIA/LIP6.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: --no-includes --include-headers
virtual context
virtual org
virtual report
virtual patch
@alloc_function@
expression c;
identifier func =~ "^(kmalloc|kzalloc|kmalloc_node|kzalloc_node|vmalloc|vzalloc|kvmalloc|kvzalloc|kvmalloc_node|kvzalloc_node|kmem_alloc|kmem_zalloc|vmalloc_node|vzalloc_node)$";
@@
c = func(...);
@alloc_function2@
expression c;
identifier func2 =~ "^(kmem_cache_create)$";
@@
c = func2(...);
@simple_sizeof@
type t;
expression E, c, first_param;
position p;
identifier alloc_function.func;
identifier alloc_function2.func2;
@@
(
c = func(sizeof(t), ...)@p;
|
c = func(sizeof E, ...)@p;
|
c = func2(first_param, sizeof(t), ...)@p;
|
c = func2(first_param, sizeof E, ...)@p;
)
@complex_sizeof_type exists@
type t;
expression c, first_param;
fresh identifier i = "__uncontained_tmp";
position p1 != simple_sizeof.p;
identifier alloc_function.func;
identifier alloc_function2.func2;
@@
(
c = func(<+... sizeof(t) ...+>, ...)@p1;
++ {
++ t i;
++ __uncontained_complex_alloc = (unsigned long)&i;
++ }
|
c = func2(first_param, <+... sizeof(t) ...+>, ...)@p1;
++ {
++ t i;
++ __uncontained_complex_alloc = (unsigned long)&i;
++ }
)
@complex_sizeof_var exists@
expression E, c, first_param;
fresh identifier i = "__uncontained_tmp";
position p1 != simple_sizeof.p;
identifier alloc_function.func;
identifier alloc_function2.func2;
@@
(
c = func(<+... sizeof E ...+>, ...)@p1;
++ {
++ typeof (E) i;
++ __uncontained_complex_alloc = (unsigned long)&i;
++ }
|
c = func2(first_param, <+... sizeof E ...+>, ...)@p1;
++ {
++ typeof (E) i;
++ __uncontained_complex_alloc = (unsigned long)&i;
++ }
)
@add_glob_declaration depends on complex_sizeof_type || complex_sizeof_var@
@@
#include <...>
+
+ #ifndef _UNCONTAINED_COMPLEX_ALLOC_H
+ #define _UNCONTAINED_COMPLEX_ALLOC_H
+ static volatile unsigned long __uncontained_complex_alloc;
+ #endif /*_UNCONTAINED_COMPLEX_ALLOC_H*/
@add_glob_declaration2 depends on (complex_sizeof_type || complex_sizeof_var) && !add_glob_declaration@
@@
#include "..."
+
+ #ifndef _UNCONTAINED_COMPLEX_ALLOC_H
+ #define _UNCONTAINED_COMPLEX_ALLOC_H
+ static volatile unsigned long __uncontained_complex_alloc;
+ #endif /*_UNCONTAINED_COMPLEX_ALLOC_H*/
@script:python depends on report@
p1 << complex_sizeof_type.p1;
@@
msg = "Detected custom type allocation"
coccilib.report.print_report(p1[0], msg)
@script:python depends on report@
p1 << complex_sizeof_var.p1;
@@
msg = "Detected custom type allocation"
coccilib.report.print_report(p1[0], msg)