Debian Codegraph @

clang/test/Analysis/MismatchedDeallocator-checker-test.mm

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s
3
4 #include "Inputs/system-header-simulator-objc.h"
5 #include "Inputs/system-header-simulator-cxx.h"
6
7 typedef __typeof__(sizeof(int)) size_t;
8 void *malloc(size_t);
9 void *realloc(void *ptr, size_t size);
10 void *calloc(size_t nmemb, size_t size);
11 char *strdup(const char *s);
12 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
13
14 void free(void *);
15 void __attribute((ownership_takes(malloc, 1))) my_free(void *);
16
17 void __attribute((ownership_returns(malloc1))) *my_malloc1(size_t);
18 void __attribute((ownership_takes(malloc1, 1))) my_free1(void *);
19
20 void __attribute((ownership_returns(malloc2))) *my_malloc2(size_t);
21
22 // The order of these declarations are important to verify that analisys still works even
23 // if there are less specific declarations of the same functions
24 void __attribute((ownership_returns(malloc3))) *my_malloc3(size_t);
25 void *my_malloc3(size_t);
26
27 void *my_malloc4(size_t);
28 void __attribute((ownership_returns(malloc4))) *my_malloc4(size_t);
29
30 //---------------------------------------------------------------
31 // Test if an allocation function matches deallocation function
32 //---------------------------------------------------------------
33
34 //--------------- test malloc family
35 void testMalloc1() {
36 int *p = (int *)malloc(sizeof(int));
37 delete p; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
38 }
39
40 void testMalloc2() {
41 int *p = (int *)malloc(8);
42 int *q = (int *)realloc(p, 16);
43 delete q; // expected-warning{{Memory allocated by 'realloc()' should be deallocated by 'free()', not 'delete'}}
44 }
45
46 void testMalloc3() {
47 int *p = (int *)calloc(1, sizeof(int));
48 delete p; // expected-warning{{Memory allocated by 'calloc()' should be deallocated by 'free()', not 'delete'}}
49 }
50
51 void testMalloc4(const char *s) {
52 char *p = strdup(s);
53 delete p; // expected-warning{{Memory allocated by 'strdup()' should be deallocated by 'free()', not 'delete'}}
54 }
55
56 void testMalloc5() {
57 int *p = (int *)my_malloc(sizeof(int));
58 delete p; // expected-warning{{Memory allocated by 'my_malloc()' should be deallocated by 'free()', not 'delete'}}
59 }
60
61 void testMalloc6() {
62 int *p = (int *)malloc(sizeof(int));
63 operator delete(p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete'}}
64 }
65
66 void testMalloc7() {
67 int *p = (int *)malloc(sizeof(int));
68 delete[] p; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete[]'}}
69 }
70
71 void testMalloc8() {
72 int *p = (int *)malloc(sizeof(int));
73 operator delete[](p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete[]'}}
74 }
75
76 void testMalloc9() {
77 int *p = (int *)my_malloc(sizeof(int));
78 my_free(p); // no warning
79 }
80
81 void testMalloc10() {
82 int *p = (int *)my_malloc1(sizeof(int));
83 my_free1(p); // no warning
84 }
85
86 void testMalloc11() {
87 int *p = (int *)my_malloc1(sizeof(int));
88 my_free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'my_free()', which takes ownership of 'malloc'}}
89 }
90
91 void testMalloc12() {
92 int *p = (int *)my_malloc2(sizeof(int));
93 my_free1(p); // expected-warning{{Memory allocated by 'my_malloc2()' should be deallocated by function that takes ownership of 'malloc2', not 'my_free1()', which takes ownership of 'malloc1'}}
94 }
95
96 void testMalloc13() {
97 int *p = (int *)my_malloc1(sizeof(int));
98 free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'free()'}}
99 }
100
101 void testMalloc14() {
102 int *p = (int *)my_malloc3(sizeof(int));
103 free(p); // expected-warning{{Memory allocated by 'my_malloc3()' should be deallocated by function that takes ownership of 'malloc3', not 'free()'}}
104 }
105
106 void testMalloc15() {
107 int *p = (int *)my_malloc4(sizeof(int));
108 free(p); // expected-warning{{Memory allocated by 'my_malloc4()' should be deallocated by function that takes ownership of 'malloc4', not 'free()'}}
109 }
110
111 void testAlloca() {
112 int *p = (int *)__builtin_alloca(sizeof(int));
113 delete p; // expected-warning{{Memory allocated by 'alloca()' should not be deallocated}}
114 }
115
116 //--------------- test new family
117 void testNew1() {
118 int *p = new int;
119 free(p); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not 'free()'}}
120 }
121
122 void testNew2() {
123 int *p = (int *)operator new(0);
124 free(p); // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'free()'}}
125 }
126
127 void testNew3() {
128 int *p = new int[1];
129 free(p); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'free()'}}
130 }
131
132 void testNew4() {
133 int *p = new int;
134 realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not 'realloc()'}}
135 }
136
137 void testNew5() {
138 int *p = (int *)operator new(0);
139 realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'realloc()'}}
140 }
141
142 void testNew6() {
143 int *p = new int[1];
144 realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'realloc()'}}
145 }
146
147 int *allocInt() {
148 return new int;
149 }
150 void testNew7() {
151 int *p = allocInt();
152 delete[] p; // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not 'delete[]'}}
153 }
154
155 void testNew8() {
156 int *p = (int *)operator new(0);
157 delete[] p; // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'delete[]'}}
158 }
159
160 int *allocIntArray(unsigned c) {
161 return new int[c];
162 }
163
164 void testNew9() {
165 int *p = allocIntArray(1);
166 delete p; // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}}
167 }
168
169 void testNew10() {
170 int *p = (int *)operator new[](0);
171 delete p; // expected-warning{{Memory allocated by 'operator new[]' should be deallocated by 'delete[]', not 'delete'}}
172 }
173
174 void testNew11(NSUInteger dataLength) {
175 int *p = new int;
176 NSData *d = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1]; // expected-warning{{+dataWithBytesNoCopy:length:freeWhenDone: cannot take ownership of memory allocated by 'new'}}
177 }
178
179 //-------------------------------------------------------
180 // Check for intersection with unix.Malloc bounded with
181 // unix.MismatchedDeallocator
182 //-------------------------------------------------------
183
184 // new/delete oparators are subjects of cplusplus.NewDelete.
185 void testNewDeleteNoWarn() {
186 int i;
187 delete &i; // no-warning
188
189 int *p1 = new int;
190 delete ++p1; // no-warning
191
192 int *p2 = new int;
193 delete p2;
194 delete p2; // no-warning
195
196 int *p3 = new int; // no-warning
197 }
198
199 void testDeleteOpAfterFree() {
200 int *p = (int *)malloc(sizeof(int));
201 free(p);
202 operator delete(p); // no-warning
203 }
204
205 void testDeleteAfterFree() {
206 int *p = (int *)malloc(sizeof(int));
207 free(p);
208 delete p; // no-warning
209 }
210
211 void testStandardPlacementNewAfterFree() {
212 int *p = (int *)malloc(sizeof(int));
213 free(p);
214 p = new(p) int; // no-warning
215 }
216
217 //---------------------------------------------------------------
218 // Check for intersection with cplusplus.NewDelete bounded with
219 // unix.MismatchedDeallocator
220 //---------------------------------------------------------------
221
222 // malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
223 void testMallocFreeNoWarn() {
224 int i;
225 free(&i); // no-warning
226
227 int *p1 = (int *)malloc(sizeof(int));
228 free(++p1); // no-warning
229
230 int *p2 = (int *)malloc(sizeof(int));
231 free(p2);
232 free(p2); // no-warning
233
234 int *p3 = (int *)malloc(sizeof(int)); // no-warning
235 }
236
237 void testFreeAfterDelete() {
238 int *p = new int;
239 delete p;
240 free(p); // no-warning
241 }
242
243 void testStandardPlacementNewAfterDelete() {
244 int *p = new int;
245 delete p;
246 p = new(p) int; // no-warning
247 }
248
249
250 // Smart pointer example
251 template <typename T>
252 struct SimpleSmartPointer {
253 T *ptr;
254
255 explicit SimpleSmartPointer(T *p = 0) : ptr(p) {}
256 ~SimpleSmartPointer() {
257 delete ptr;
258 // expected-warning@-1 {{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}}
259 // expected-warning@-2 {{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
260 }
261 };
262
263 void testSimpleSmartPointerArrayNew() {
264 {
265 SimpleSmartPointer<int> a(new int);
266 } // no-warning
267
268 {
269 SimpleSmartPointer<int> a(new int[4]);
270 }
271 }
272
273 void testSimpleSmartPointerMalloc() {
274 {
275 SimpleSmartPointer<int> a(new int);
276 } // no-warning
277
278 {
279 SimpleSmartPointer<int> a((int *)malloc(4));
280 }
281 }