|
1 | 1 | import cpp |
| 2 | +import semmle.code.cpp.models.interfaces.Allocation |
| 3 | +import semmle.code.cpp.models.interfaces.Deallocation |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * A library routine that allocates memory. |
| 7 | + * |
| 8 | + * DEPRECATED: Use the `AllocationFunction` class instead of this predicate. |
5 | 9 | */ |
6 | | -predicate allocationFunction(Function f) { |
7 | | - exists(string name | |
8 | | - f.hasGlobalOrStdName(name) and |
9 | | - ( |
10 | | - name = "malloc" or |
11 | | - name = "calloc" or |
12 | | - name = "realloc" or |
13 | | - name = "strdup" or |
14 | | - name = "wcsdup" |
15 | | - ) |
16 | | - or |
17 | | - f.hasGlobalName(name) and |
18 | | - ( |
19 | | - name = "_strdup" or |
20 | | - name = "_wcsdup" or |
21 | | - name = "_mbsdup" or |
22 | | - name = "ExAllocatePool" or |
23 | | - name = "ExAllocatePoolWithTag" or |
24 | | - name = "ExAllocatePoolWithTagPriority" or |
25 | | - name = "ExAllocatePoolWithQuota" or |
26 | | - name = "ExAllocatePoolWithQuotaTag" or |
27 | | - name = "ExAllocateFromLookasideListEx" or |
28 | | - name = "ExAllocateFromPagedLookasideList" or |
29 | | - name = "ExAllocateFromNPagedLookasideList" or |
30 | | - name = "ExAllocateTimer" or |
31 | | - name = "IoAllocateMdl" or |
32 | | - name = "IoAllocateWorkItem" or |
33 | | - name = "IoAllocateErrorLogEntry" or |
34 | | - name = "MmAllocateContiguousMemory" or |
35 | | - name = "MmAllocateContiguousNodeMemory" or |
36 | | - name = "MmAllocateContiguousMemorySpecifyCache" or |
37 | | - name = "MmAllocateContiguousMemorySpecifyCacheNode" or |
38 | | - name = "MmAllocateNonCachedMemory" or |
39 | | - name = "MmAllocateMappingAddress" or |
40 | | - name = "MmAllocatePagesForMdl" or |
41 | | - name = "MmAllocatePagesForMdlEx" or |
42 | | - name = "MmAllocateNodePagesForMdlEx" or |
43 | | - name = "MmMapLockedPagesWithReservedMapping" or |
44 | | - name = "MmMapLockedPages" or |
45 | | - name = "MmMapLockedPagesSpecifyCache" or |
46 | | - name = "LocalAlloc" or |
47 | | - name = "LocalReAlloc" or |
48 | | - name = "GlobalAlloc" or |
49 | | - name = "GlobalReAlloc" or |
50 | | - name = "HeapAlloc" or |
51 | | - name = "HeapReAlloc" or |
52 | | - name = "VirtualAlloc" or |
53 | | - name = "CoTaskMemAlloc" or |
54 | | - name = "CoTaskMemRealloc" or |
55 | | - name = "kmem_alloc" or |
56 | | - name = "kmem_zalloc" or |
57 | | - name = "pool_get" or |
58 | | - name = "pool_cache_get" |
59 | | - ) |
60 | | - ) |
61 | | -} |
| 10 | +deprecated predicate allocationFunction(Function f) { f instanceof AllocationFunction } |
62 | 11 |
|
63 | 12 | /** |
64 | 13 | * A call to a library routine that allocates memory. |
| 14 | + * |
| 15 | + * DEPRECATED: Use `AllocationExpr` instead (this also includes `new` expressions). |
65 | 16 | */ |
66 | | -predicate allocationCall(FunctionCall fc) { |
67 | | - allocationFunction(fc.getTarget()) and |
68 | | - ( |
69 | | - // realloc(ptr, 0) only frees the pointer |
70 | | - fc.getTarget().hasGlobalOrStdName("realloc") implies not fc.getArgument(1).getValue() = "0" |
71 | | - ) |
72 | | -} |
| 17 | +deprecated predicate allocationCall(FunctionCall fc) { fc instanceof AllocationExpr } |
73 | 18 |
|
74 | 19 | /** |
75 | 20 | * A library routine that frees memory. |
76 | 21 | */ |
77 | | -predicate freeFunction(Function f, int argNum) { |
78 | | - exists(string name | |
79 | | - f.hasGlobalName(name) and |
80 | | - ( |
81 | | - name = "free" and argNum = 0 |
82 | | - or |
83 | | - name = "realloc" and argNum = 0 |
84 | | - or |
85 | | - name = "kmem_free" and argNum = 0 |
86 | | - or |
87 | | - name = "pool_put" and argNum = 1 |
88 | | - or |
89 | | - name = "pool_cache_put" and argNum = 1 |
90 | | - ) |
91 | | - or |
92 | | - f.hasGlobalOrStdName(name) and |
93 | | - ( |
94 | | - name = "ExFreePoolWithTag" and argNum = 0 |
95 | | - or |
96 | | - name = "ExFreeToLookasideListEx" and argNum = 1 |
97 | | - or |
98 | | - name = "ExFreeToPagedLookasideList" and argNum = 1 |
99 | | - or |
100 | | - name = "ExFreeToNPagedLookasideList" and argNum = 1 |
101 | | - or |
102 | | - name = "ExDeleteTimer" and argNum = 0 |
103 | | - or |
104 | | - name = "IoFreeMdl" and argNum = 0 |
105 | | - or |
106 | | - name = "IoFreeWorkItem" and argNum = 0 |
107 | | - or |
108 | | - name = "IoFreeErrorLogEntry" and argNum = 0 |
109 | | - or |
110 | | - name = "MmFreeContiguousMemory" and argNum = 0 |
111 | | - or |
112 | | - name = "MmFreeContiguousMemorySpecifyCache" and argNum = 0 |
113 | | - or |
114 | | - name = "MmFreeNonCachedMemory" and argNum = 0 |
115 | | - or |
116 | | - name = "MmFreeMappingAddress" and argNum = 0 |
117 | | - or |
118 | | - name = "MmFreePagesFromMdl" and argNum = 0 |
119 | | - or |
120 | | - name = "MmUnmapReservedMapping" and argNum = 0 |
121 | | - or |
122 | | - name = "MmUnmapLockedPages" and argNum = 0 |
123 | | - or |
124 | | - name = "LocalFree" and argNum = 0 |
125 | | - or |
126 | | - name = "GlobalFree" and argNum = 0 |
127 | | - or |
128 | | - name = "HeapFree" and argNum = 2 |
129 | | - or |
130 | | - name = "VirtualFree" and argNum = 0 |
131 | | - or |
132 | | - name = "CoTaskMemFree" and argNum = 0 |
133 | | - or |
134 | | - name = "SysFreeString" and argNum = 0 |
135 | | - or |
136 | | - name = "LocalReAlloc" and argNum = 0 |
137 | | - or |
138 | | - name = "GlobalReAlloc" and argNum = 0 |
139 | | - or |
140 | | - name = "HeapReAlloc" and argNum = 2 |
141 | | - or |
142 | | - name = "CoTaskMemRealloc" and argNum = 0 |
143 | | - ) |
144 | | - ) |
145 | | -} |
| 22 | +predicate freeFunction(Function f, int argNum) { argNum = f.(DeallocationFunction).getFreedArg() } |
146 | 23 |
|
147 | 24 | /** |
148 | 25 | * A call to a library routine that frees memory. |
149 | 26 | */ |
150 | | -predicate freeCall(FunctionCall fc, Expr arg) { |
151 | | - exists(int argNum | |
152 | | - freeFunction(fc.getTarget(), argNum) and |
153 | | - arg = fc.getArgument(argNum) |
154 | | - ) |
155 | | -} |
| 27 | +predicate freeCall(FunctionCall fc, Expr arg) { arg = fc.(DeallocationExpr).getFreedExpr() } |
156 | 28 |
|
157 | 29 | /** |
158 | 30 | * Is e some kind of allocation or deallocation (`new`, `alloc`, `realloc`, `delete`, `free` etc)? |
159 | 31 | */ |
160 | | -predicate isMemoryManagementExpr(Expr e) { isAllocationExpr(e) or isDeallocationExpr(e) } |
| 32 | +predicate isMemoryManagementExpr(Expr e) { isAllocationExpr(e) or e instanceof DeallocationExpr } |
161 | 33 |
|
162 | 34 | /** |
163 | 35 | * Is e an allocation from stdlib.h (`malloc`, `realloc` etc)? |
| 36 | + * |
| 37 | + * DEPRECATED: Use `AllocationExpr` instead (this also includes `new` expressions). |
164 | 38 | */ |
165 | | -predicate isStdLibAllocationExpr(Expr e) { allocationCall(e) } |
| 39 | +deprecated predicate isStdLibAllocationExpr(Expr e) { allocationCall(e) } |
166 | 40 |
|
167 | 41 | /** |
168 | 42 | * Is e some kind of allocation (`new`, `alloc`, `realloc` etc)? |
169 | 43 | */ |
170 | 44 | predicate isAllocationExpr(Expr e) { |
171 | | - allocationCall(e) |
| 45 | + e.(FunctionCall) instanceof AllocationExpr |
172 | 46 | or |
173 | 47 | e = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer())) |
174 | 48 | } |
175 | 49 |
|
176 | 50 | /** |
177 | 51 | * Is e some kind of allocation (`new`, `alloc`, `realloc` etc) with a fixed size? |
| 52 | + * |
| 53 | + * DEPRECATED: Use `AllocationExpr.getSizeBytes()` instead. |
178 | 54 | */ |
179 | | -predicate isFixedSizeAllocationExpr(Expr allocExpr, int size) { |
180 | | - exists(FunctionCall fc, string name | fc = allocExpr and name = fc.getTarget().getName() | |
181 | | - name = "malloc" and |
182 | | - size = fc.getArgument(0).getValue().toInt() |
183 | | - or |
184 | | - name = "alloca" and |
185 | | - size = fc.getArgument(0).getValue().toInt() |
186 | | - or |
187 | | - name = "calloc" and |
188 | | - size = fc.getArgument(0).getValue().toInt() * fc.getArgument(1).getValue().toInt() |
189 | | - or |
190 | | - name = "realloc" and |
191 | | - size = fc.getArgument(1).getValue().toInt() and |
192 | | - size > 0 // realloc(ptr, 0) only frees the pointer |
193 | | - ) |
194 | | - or |
195 | | - size = allocExpr.(NewExpr).getAllocatedType().getSize() |
196 | | - or |
197 | | - size = allocExpr.(NewArrayExpr).getAllocatedType().getSize() |
| 55 | +deprecated predicate isFixedSizeAllocationExpr(Expr allocExpr, int size) { |
| 56 | + size = allocExpr.(AllocationExpr).getSizeBytes() |
198 | 57 | } |
199 | 58 |
|
200 | 59 | /** |
201 | 60 | * Is e some kind of deallocation (`delete`, `free`, `realloc` etc)? |
| 61 | + * |
| 62 | + * DEPRECATED: Use `DeallocationExpr` instead. |
202 | 63 | */ |
203 | | -predicate isDeallocationExpr(Expr e) { |
204 | | - freeCall(e, _) or |
205 | | - e instanceof DeleteExpr or |
206 | | - e instanceof DeleteArrayExpr |
207 | | -} |
| 64 | +deprecated predicate isDeallocationExpr(Expr e) { e instanceof DeallocationExpr } |
0 commit comments