blob: 089e53132d93b4140d80935db0e656143aa54117 [file] [log] [blame]
Anna Zaks231361a2012-02-08 23:16:52 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,experimental.unix.MallocWithAnnotations -analyzer-store=region -verify %s
2typedef __typeof(sizeof(int)) size_t;
3void *malloc(size_t);
4void free(void *);
5void *realloc(void *ptr, size_t size);
6void *calloc(size_t nmemb, size_t size);
7void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
8void __attribute((ownership_takes(malloc, 1))) my_free(void *);
Anna Zaksb3d72752012-03-01 22:06:06 +00009void my_freeBoth(void *, void *)
10 __attribute((ownership_holds(malloc, 1, 2)));
Anna Zaks231361a2012-02-08 23:16:52 +000011void __attribute((ownership_returns(malloc, 1))) *my_malloc2(size_t);
12void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
13
14// Duplicate attributes are silly, but not an error.
15// Duplicate attribute has no extra effect.
16// If two are of different kinds, that is an error and reported as such.
17void __attribute((ownership_holds(malloc, 1)))
18__attribute((ownership_holds(malloc, 1)))
19__attribute((ownership_holds(malloc, 3))) my_hold2(void *, void *, void *);
20void *my_malloc3(size_t);
21void *myglobalpointer;
22struct stuff {
23 void *somefield;
24};
25struct stuff myglobalstuff;
26
27void f1() {
28 int *p = malloc(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +000029 return; // expected-warning{{Memory is never released; potential leak}}
Anna Zaks231361a2012-02-08 23:16:52 +000030}
31
32void f2() {
33 int *p = malloc(12);
34 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +000035 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +000036}
37
38void f2_realloc_0() {
39 int *p = malloc(12);
40 realloc(p,0);
Anna Zaksfebdc322012-02-16 22:26:12 +000041 realloc(p,0); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +000042}
43
44void f2_realloc_1() {
45 int *p = malloc(12);
46 int *q = realloc(p,0); // no-warning
47}
48
49// ownership attributes tests
50void naf1() {
51 int *p = my_malloc3(12);
52 return; // no-warning
53}
54
55void n2af1() {
56 int *p = my_malloc2(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +000057 return; // expected-warning{{Memory is never released; potential leak}}
Anna Zaks231361a2012-02-08 23:16:52 +000058}
59
60void af1() {
61 int *p = my_malloc(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +000062 return; // expected-warning{{Memory is never released; potential leak}}
Anna Zaks231361a2012-02-08 23:16:52 +000063}
64
65void af1_b() {
Anna Zaks3d7c44e2012-03-21 19:45:08 +000066 int *p = my_malloc(12); // expected-warning{{Memory is never released; potential leak}}
Anna Zaks231361a2012-02-08 23:16:52 +000067}
68
69void af1_c() {
70 myglobalpointer = my_malloc(12); // no-warning
71}
72
Anna Zaksac593002012-02-16 03:40:57 +000073// TODO: We will be able to handle this after we add support for tracking allocations stored in struct fields.
Anna Zaks231361a2012-02-08 23:16:52 +000074void af1_d() {
75 struct stuff mystuff;
Anna Zaksac593002012-02-16 03:40:57 +000076 mystuff.somefield = my_malloc(12); // false negative
Anna Zaks231361a2012-02-08 23:16:52 +000077}
78
79// Test that we can pass out allocated memory via pointer-to-pointer.
80void af1_e(void **pp) {
81 *pp = my_malloc(42); // no-warning
82}
83
84void af1_f(struct stuff *somestuff) {
85 somestuff->somefield = my_malloc(12); // no-warning
86}
87
88// Allocating memory for a field via multiple indirections to our arguments is OK.
89void af1_g(struct stuff **pps) {
90 *pps = my_malloc(sizeof(struct stuff)); // no-warning
91 (*pps)->somefield = my_malloc(42); // no-warning
92}
93
94void af2() {
95 int *p = my_malloc(12);
96 my_free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +000097 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +000098}
99
100void af2b() {
101 int *p = my_malloc(12);
102 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000103 my_free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +0000104}
105
106void af2c() {
107 int *p = my_malloc(12);
108 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000109 my_hold(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +0000110}
111
112void af2d() {
113 int *p = my_malloc(12);
114 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000115 my_hold2(0, 0, p); // expected-warning{{Attempt to free released memory}}
Anna Zaks231361a2012-02-08 23:16:52 +0000116}
117
118// No leak if malloc returns null.
119void af2e() {
120 int *p = my_malloc(12);
121 if (!p)
122 return; // no-warning
123 free(p); // no-warning
124}
125
Anna Zaks050cdd72012-06-20 20:57:46 +0000126// This case inflicts a possible double-free.
Anna Zaks231361a2012-02-08 23:16:52 +0000127void af3() {
128 int *p = my_malloc(12);
129 my_hold(p);
Anna Zaks5b7aa342012-06-22 02:04:31 +0000130 free(p); // expected-warning{{Attempt to free non-owned memory}}
Anna Zaks231361a2012-02-08 23:16:52 +0000131}
132
Anna Zaks231361a2012-02-08 23:16:52 +0000133int * af4() {
134 int *p = my_malloc(12);
135 my_free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000136 return p; // expected-warning{{Use of memory after it is freed}}
Anna Zaks231361a2012-02-08 23:16:52 +0000137}
138
139// This case is (possibly) ok, be conservative
140int * af5() {
141 int *p = my_malloc(12);
142 my_hold(p);
143 return p; // no-warning
144}
145
146
147
148// This case tests that storing malloc'ed memory to a static variable which is
149// then returned is not leaked. In the absence of known contracts for functions
150// or inter-procedural analysis, this is a conservative answer.
151int *f3() {
152 static int *p = 0;
153 p = malloc(12);
154 return p; // no-warning
155}
156
157// This case tests that storing malloc'ed memory to a static global variable
158// which is then returned is not leaked. In the absence of known contracts for
159// functions or inter-procedural analysis, this is a conservative answer.
160static int *p_f4 = 0;
161int *f4() {
162 p_f4 = malloc(12);
163 return p_f4; // no-warning
164}
165
166int *f5() {
167 int *q = malloc(12);
168 q = realloc(q, 20);
169 return q; // no-warning
170}
171
172void f6() {
173 int *p = malloc(12);
174 if (!p)
175 return; // no-warning
176 else
177 free(p);
178}
179
180void f6_realloc() {
181 int *p = malloc(12);
182 if (!p)
183 return; // no-warning
184 else
185 realloc(p,0);
186}
187
188
189char *doit2();
190void pr6069() {
191 char *buf = doit2();
192 free(buf);
193}
194
195void pr6293() {
196 free(0);
197}
198
199void f7() {
200 char *x = (char*) malloc(4);
201 free(x);
Anna Zaksfebdc322012-02-16 22:26:12 +0000202 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Anna Zaks231361a2012-02-08 23:16:52 +0000203}
204
205void f7_realloc() {
206 char *x = (char*) malloc(4);
207 realloc(x,0);
Anna Zaksfebdc322012-02-16 22:26:12 +0000208 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Anna Zaks231361a2012-02-08 23:16:52 +0000209}
210
211void PR6123() {
212 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
213}
214
215void PR7217() {
216 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
217 buf[1] = 'c'; // not crash
218}
219
220void mallocCastToVoid() {
221 void *p = malloc(2);
222 const void *cp = p; // not crash
223 free(p);
224}
225
226void mallocCastToFP() {
227 void *p = malloc(2);
228 void (*fp)() = p; // not crash
229 free(p);
230}
231
232// This tests that malloc() buffers are undefined by default
233char mallocGarbage () {
234 char *buf = malloc(2);
235 char result = buf[1]; // expected-warning{{undefined}}
236 free(buf);
237 return result;
238}
239
240// This tests that calloc() buffers need to be freed
241void callocNoFree () {
242 char *buf = calloc(2,2);
243 return; // expected-warning{{never released}}
244}
245
246// These test that calloc() buffers are zeroed by default
247char callocZeroesGood () {
248 char *buf = calloc(2,2);
249 char result = buf[3]; // no-warning
250 if (buf[1] == 0) {
251 free(buf);
252 }
253 return result; // no-warning
254}
255
256char callocZeroesBad () {
257 char *buf = calloc(2,2);
258 char result = buf[3]; // no-warning
259 if (buf[1] != 0) {
260 free(buf); // expected-warning{{never executed}}
261 }
262 return result; // expected-warning{{never released}}
263}
Anna Zaksb3d72752012-03-01 22:06:06 +0000264
265void testMultipleFreeAnnotations() {
266 int *p = malloc(12);
267 int *q = malloc(12);
268 my_freeBoth(p, q);
269}
270