Debian Codegraph @

clang/test/Analysis/malloc-annotations.c

raw
1 // RUN: %clang_analyze_cc1 -verify \
2 // RUN: -Wno-alloc-size \
3 // RUN: -analyzer-checker=core \
4 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
5 // RUN: -analyzer-checker=unix.Malloc \
6 // RUN: -analyzer-checker=debug.ExprInspection \
7 // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true %s
8
9 typedef __typeof(sizeof(int)) size_t;
10 void *malloc(size_t);
11 void free(void *);
12 void *realloc(void *ptr, size_t size);
13 void *calloc(size_t nmemb, size_t size);
14 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
15 void __attribute((ownership_takes(malloc, 1))) my_free(void *);
16 void my_freeBoth(void *, void *)
17 __attribute((ownership_holds(malloc, 1, 2)));
18 void __attribute((ownership_returns(malloc, 1))) *my_malloc2(size_t);
19 void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
20
21 // Duplicate attributes are silly, but not an error.
22 // Duplicate attribute has no extra effect.
23 // If two are of different kinds, that is an error and reported as such.
24 void __attribute((ownership_holds(malloc, 1)))
25 __attribute((ownership_holds(malloc, 1)))
26 __attribute((ownership_holds(malloc, 3))) my_hold2(void *, void *, void *);
27
28 __attribute((ownership_returns(user_malloc, 1))) void *user_malloc(size_t);
29 __attribute((ownership_takes(user_malloc, 1))) void user_free(void *);
30
31 void clang_analyzer_dump(int);
32
33 void *my_malloc3(size_t);
34 void *myglobalpointer;
35 struct stuff {
36 void *somefield;
37 };
38 struct stuff myglobalstuff;
39
40 void f1(void) {
41 int *p = malloc(12);
42 return; // expected-warning{{Potential leak of memory pointed to by}}
43 }
44
45 void f2(void) {
46 int *p = malloc(12);
47 free(p);
48 free(p); // expected-warning{{Attempt to release already released memory}}
49 }
50
51 void f2_realloc_0(void) {
52 int *p = malloc(12);
53 realloc(p,0);
54 realloc(p,0); // expected-warning{{Attempt to release already released memory}}
55 }
56
57 void f2_realloc_1(void) {
58 int *p = malloc(12);
59 int *q = realloc(p,0); // no-warning
60 }
61
62 // ownership attributes tests
63 void naf1(void) {
64 int *p = my_malloc3(12);
65 return; // no-warning
66 }
67
68 void n2af1(void) {
69 int *p = my_malloc2(12);
70 return; // expected-warning{{Potential leak of memory pointed to by}}
71 }
72
73 void af1(void) {
74 int *p = my_malloc(12);
75 return; // expected-warning{{Potential leak of memory pointed to by}}
76 }
77
78 void af1_b(void) {
79 int *p = my_malloc(12);
80 } // expected-warning{{Potential leak of memory pointed to by}}
81
82 void af1_c(void) {
83 myglobalpointer = my_malloc(12); // no-warning
84 }
85
86 void af1_d(void) {
87 struct stuff mystuff;
88 mystuff.somefield = my_malloc(12);
89 } // expected-warning{{Potential leak of memory pointed to by}}
90
91 // Test that we can pass out allocated memory via pointer-to-pointer.
92 void af1_e(void **pp) {
93 *pp = my_malloc(42); // no-warning
94 }
95
96 void af1_f(struct stuff *somestuff) {
97 somestuff->somefield = my_malloc(12); // no-warning
98 }
99
100 // Allocating memory for a field via multiple indirections to our arguments is OK.
101 void af1_g(struct stuff **pps) {
102 *pps = my_malloc(sizeof(struct stuff)); // no-warning
103 (*pps)->somefield = my_malloc(42); // no-warning
104 }
105
106 void af2(void) {
107 int *p = my_malloc(12);
108 my_free(p);
109 free(p); // expected-warning{{Attempt to release already released memory}}
110 }
111
112 void af2b(void) {
113 int *p = my_malloc(12);
114 free(p);
115 my_free(p); // expected-warning{{Attempt to release already released memory}}
116 }
117
118 void af2c(void) {
119 int *p = my_malloc(12);
120 free(p);
121 my_hold(p); // expected-warning{{Attempt to release already released memory}}
122 }
123
124 void af2d(void) {
125 int *p = my_malloc(12);
126 free(p);
127 my_hold2(0, 0, p); // expected-warning{{Attempt to release already released memory}}
128 }
129
130 // No leak if malloc returns null.
131 void af2e(void) {
132 int *p = my_malloc(12);
133 if (!p)
134 return; // no-warning
135 free(p); // no-warning
136 }
137
138 // This case inflicts a possible double-free.
139 void af3(void) {
140 int *p = my_malloc(12);
141 my_hold(p);
142 free(p); // expected-warning{{Attempt to release non-owned memory}}
143 }
144
145 int * af4(void) {
146 int *p = my_malloc(12);
147 my_free(p);
148 return p; // expected-warning{{Use of memory after it is released}}
149 }
150
151 // This case is (possibly) ok, be conservative
152 int * af5(void) {
153 int *p = my_malloc(12);
154 my_hold(p);
155 return p; // no-warning
156 }
157
158
159
160 // This case tests that storing malloc'ed memory to a static variable which is
161 // then returned is not leaked. In the absence of known contracts for functions
162 // or inter-procedural analysis, this is a conservative answer.
163 int *f3(void) {
164 static int *p = 0;
165 p = malloc(12);
166 return p; // no-warning
167 }
168
169 // This case tests that storing malloc'ed memory to a static global variable
170 // which is then returned is not leaked. In the absence of known contracts for
171 // functions or inter-procedural analysis, this is a conservative answer.
172 static int *p_f4 = 0;
173 int *f4(void) {
174 p_f4 = malloc(12);
175 return p_f4; // no-warning
176 }
177
178 int *f5(void) {
179 int *q = malloc(12);
180 q = realloc(q, 20);
181 return q; // no-warning
182 }
183
184 void f6(void) {
185 int *p = malloc(12);
186 if (!p)
187 return; // no-warning
188 else
189 free(p);
190 }
191
192 void f6_realloc(void) {
193 int *p = malloc(12);
194 if (!p)
195 return; // no-warning
196 else
197 realloc(p,0);
198 }
199
200
201 char *doit2(void);
202 void pr6069(void) {
203 char *buf = doit2();
204 free(buf);
205 }
206
207 void pr6293(void) {
208 free(0);
209 }
210
211 void f7(void) {
212 char *x = (char*) malloc(4);
213 free(x);
214 x[0] = 'a'; // expected-warning{{Use of memory after it is released}}
215 }
216
217 void f7_realloc(void) {
218 char *x = (char*) malloc(4);
219 realloc(x,0);
220 x[0] = 'a'; // expected-warning{{Use of memory after it is released}}
221 }
222
223 void mallocCastToVoid(void) {
224 void *p = malloc(2);
225 const void *cp = p; // not crash
226 free(p);
227 }
228
229 void mallocCastToFP(void) {
230 void *p = malloc(2);
231 void (*fp)(void) = p; // not crash
232 free(p);
233 }
234
235 // This tests that malloc() buffers are undefined by default
236 char mallocGarbage (void) {
237 char *buf = malloc(2);
238 char result = buf[1]; // expected-warning{{uninitialized}}
239 free(buf);
240 return result;
241 }
242
243 // This tests that calloc() buffers need to be freed
244 void callocNoFree (void) {
245 char *buf = calloc(2,2);
246 return; // expected-warning{{Potential leak of memory pointed to by}}
247 }
248
249 // These test that calloc() buffers are zeroed by default
250 char callocZeroesGood (void) {
251 char *buf = calloc(2,2);
252 char result = buf[3]; // no-warning
253 if (buf[1] == 0) {
254 free(buf);
255 }
256 return result; // no-warning
257 }
258
259 char callocZeroesBad (void) {
260 char *buf = calloc(2,2);
261 char result = buf[3]; // no-warning
262 if (buf[1] != 0) {
263 free(buf); // expected-warning{{never executed}}
264 }
265 return result; // expected-warning{{Potential leak of memory pointed to by}}
266 }
267
268 void testMultipleFreeAnnotations(void) {
269 int *p = malloc(12);
270 int *q = malloc(12);
271 my_freeBoth(p, q);
272 }
273
274 void testNoUninitAttr(void) {
275 int *p = user_malloc(sizeof(int));
276 int read = p[0]; // no-warning
277 clang_analyzer_dump(p[0]); // expected-warning{{Unknown}}
278 user_free(p);
279 }
280
281 // Regression test for GH#183344 — crash when a function has both
282 // ownership_returns and ownership_takes attributes.
283 typedef struct GH183344_X GH183344_X;
284 typedef struct GH183344_Y GH183344_Y;
285
286 GH183344_Y *GH183344_X_to_Y(GH183344_X *x)
287 __attribute__((ownership_returns(GH183344_Y)))
288 __attribute__((ownership_takes(GH183344_X, 1)));
289
290 void testGH183344(void) {
291 GH183344_Y *y = GH183344_X_to_Y(0); // no-crash
292 (void)y;
293 } // expected-warning{{Potential leak of memory pointed to by 'y'}}
294
295 // Extended regression tests for GH#183344 — additional combinations of
296 // ownership_returns, ownership_takes, and ownership_holds.
297
298 GH183344_X *GH183344_alloc_X(void)
299 __attribute__((ownership_returns(GH183344_X)));
300 void GH183344_free_X(GH183344_X *)
301 __attribute__((ownership_takes(GH183344_X, 1)));
302 void GH183344_free_Y(GH183344_Y *)
303 __attribute__((ownership_takes(GH183344_Y, 1)));
304
305 // Returns + Holds variant: Y is allocated, X is held (not consumed) by callee.
306 GH183344_Y *GH183344_X_to_Y_hold(GH183344_X *x)
307 __attribute__((ownership_returns(GH183344_Y)))
308 __attribute__((ownership_holds(GH183344_X, 1)));
309
310 // Returns + two Takes (same pool): both X arguments are consumed, Y is
311 // returned. Multiple arg indices in one attribute (same pool) is valid;
312 // two ownership_takes attributes with *different* pool names are not.
313 GH183344_Y *GH183344_combine_XX(GH183344_X *x1, GH183344_X *x2)
314 __attribute__((ownership_returns(GH183344_Y)))
315 __attribute__((ownership_takes(GH183344_X, 1, 2)));
316
317 // No-crash for Returns+Holds with null input — same crash pattern as the
318 // original GH183344 bug but with ownership_holds instead of ownership_takes.
319 void testGH183344_ReturnsHolds_NullInput(void) {
320 GH183344_Y *y = GH183344_X_to_Y_hold(0); // no-crash
321 (void)y;
322 } // expected-warning{{Potential leak of memory pointed to by 'y'}}
323
324 // Returns+Takes: allocate X, convert to Y (X consumed), free Y — no warnings.
325 void testGH183344_ReturnsTakes_CleanUse(void) {
326 GH183344_X *x = GH183344_alloc_X();
327 GH183344_Y *y = GH183344_X_to_Y(x);
328 GH183344_free_Y(y);
329 } // no-warning
330
331 // Returns+Takes: after the conversion X is consumed; freeing it again is a
332 // double-free.
333 void testGH183344_ReturnsTakes_DoubleFreeInput(void) {
334 GH183344_X *x = GH183344_alloc_X();
335 GH183344_Y *y = GH183344_X_to_Y(x);
336 GH183344_free_X(x); // expected-warning{{Attempt to release already released memory}}
337 GH183344_free_Y(y);
338 }
339
340 // Returns+Takes: X is consumed but Y is never freed — leak on Y.
341 void testGH183344_ReturnsTakes_LeakRetval(void) {
342 GH183344_X *x = GH183344_alloc_X();
343 GH183344_Y *y = GH183344_X_to_Y(x);
344 (void)y;
345 } // expected-warning{{Potential leak of memory pointed to by 'y'}}
346
347 // Returns+Holds: after the hold, X is non-owned by the caller; freeing it
348 // produces a "non-owned memory" warning (analogous to af3).
349 void testGH183344_ReturnsHolds_FreeHeldInput(void) {
350 GH183344_X *x = GH183344_alloc_X();
351 GH183344_Y *y = GH183344_X_to_Y_hold(x);
352 GH183344_free_X(x); // expected-warning{{Attempt to release non-owned memory}}
353 GH183344_free_Y(y);
354 }
355
356 // Multiple Takes (same pool) + Returns: both X inputs are consumed, Y is
357 // returned and freed — no warnings.
358 void testGH183344_CombineXX_CleanUse(void) {
359 GH183344_X *x1 = GH183344_alloc_X();
360 GH183344_X *x2 = GH183344_alloc_X();
361 GH183344_Y *y = GH183344_combine_XX(x1, x2);
362 GH183344_free_Y(y);
363 } // no-warning
364
365 // Multiple Takes (same pool): after the combine, x2 is already consumed;
366 // freeing it again is a double-free.
367 void testGH183344_CombineXX_DoubleFreeSecondInput(void) {
368 GH183344_X *x1 = GH183344_alloc_X();
369 GH183344_X *x2 = GH183344_alloc_X();
370 GH183344_Y *y = GH183344_combine_XX(x1, x2);
371 GH183344_free_X(x2); // expected-warning{{Attempt to release already released memory}}
372 GH183344_free_Y(y);
373 }