Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-experimental-checks -analyzer-store=region -verify %s |
Eli Friedman | 2f00552 | 2009-11-14 04:23:25 +0000 | [diff] [blame] | 2 | typedef __typeof(sizeof(int)) size_t; |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 3 | void *malloc(size_t); |
| 4 | void free(void *); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 5 | void *realloc(void *ptr, size_t size); |
| 6 | void *calloc(size_t nmemb, size_t size); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 7 | void __attribute((ownership_returns(malloc))) *my_malloc(size_t); |
| 8 | void __attribute((ownership_takes(malloc, 1))) my_free(void *); |
| 9 | void __attribute((ownership_returns(malloc, 1))) *my_malloc2(size_t); |
| 10 | void __attribute((ownership_holds(malloc, 1))) my_hold(void *); |
| 11 | |
| 12 | // Duplicate attributes are silly, but not an error. |
| 13 | // Duplicate attribute has no extra effect. |
| 14 | // If two are of different kinds, that is an error and reported as such. |
| 15 | void __attribute((ownership_holds(malloc, 1))) |
| 16 | __attribute((ownership_holds(malloc, 1))) |
| 17 | __attribute((ownership_holds(malloc, 3))) my_hold2(void *, void *, void *); |
| 18 | void *my_malloc3(size_t); |
| 19 | void *myglobalpointer; |
| 20 | struct stuff { |
| 21 | void *somefield; |
| 22 | }; |
| 23 | struct stuff myglobalstuff; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 24 | |
| 25 | void f1() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 26 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 27 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 28 | } |
| 29 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 30 | void f2() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 31 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 32 | free(p); |
| 33 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 34 | } |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 36 | // ownership attributes tests |
| 37 | void naf1() { |
| 38 | int *p = my_malloc3(12); |
| 39 | return; // no-warning |
| 40 | } |
| 41 | |
| 42 | void n2af1() { |
| 43 | int *p = my_malloc2(12); |
| 44 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 45 | } |
| 46 | |
| 47 | void af1() { |
| 48 | int *p = my_malloc(12); |
| 49 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 50 | } |
| 51 | |
| 52 | void af1_b() { |
| 53 | int *p = my_malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 54 | } |
| 55 | |
| 56 | void af1_c() { |
| 57 | myglobalpointer = my_malloc(12); // no-warning |
| 58 | } |
| 59 | |
| 60 | void af1_d() { |
| 61 | struct stuff mystuff; |
| 62 | mystuff.somefield = my_malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 63 | } |
| 64 | |
| 65 | // Test that we can pass out allocated memory via pointer-to-pointer. |
| 66 | void af1_e(void **pp) { |
| 67 | *pp = my_malloc(42); // no-warning |
| 68 | } |
| 69 | |
| 70 | void af1_f(struct stuff *somestuff) { |
| 71 | somestuff->somefield = my_malloc(12); // no-warning |
| 72 | } |
| 73 | |
| 74 | // Allocating memory for a field via multiple indirections to our arguments is OK. |
| 75 | void af1_g(struct stuff **pps) { |
| 76 | *pps = my_malloc(sizeof(struct stuff)); // no-warning |
| 77 | (*pps)->somefield = my_malloc(42); // no-warning |
| 78 | } |
| 79 | |
| 80 | void af2() { |
| 81 | int *p = my_malloc(12); |
| 82 | my_free(p); |
| 83 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 84 | } |
| 85 | |
| 86 | void af2b() { |
| 87 | int *p = my_malloc(12); |
| 88 | free(p); |
| 89 | my_free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 90 | } |
| 91 | |
| 92 | void af2c() { |
| 93 | int *p = my_malloc(12); |
| 94 | free(p); |
| 95 | my_hold(p); // expected-warning{{Try to free a memory block that has been released}} |
| 96 | } |
| 97 | |
| 98 | void af2d() { |
| 99 | int *p = my_malloc(12); |
| 100 | free(p); |
| 101 | my_hold2(0, 0, p); // expected-warning{{Try to free a memory block that has been released}} |
| 102 | } |
| 103 | |
| 104 | // No leak if malloc returns null. |
| 105 | void af2e() { |
| 106 | int *p = my_malloc(12); |
| 107 | if (!p) |
| 108 | return; // no-warning |
| 109 | free(p); // no-warning |
| 110 | } |
| 111 | |
| 112 | // This case would inflict a double-free elsewhere. |
| 113 | // However, this case is considered an analyzer bug since it causes false-positives. |
| 114 | void af3() { |
| 115 | int *p = my_malloc(12); |
| 116 | my_hold(p); |
| 117 | free(p); // no-warning |
| 118 | } |
| 119 | |
| 120 | // This case would inflict a double-free elsewhere. |
| 121 | // However, this case is considered an analyzer bug since it causes false-positives. |
| 122 | int * af4() { |
| 123 | int *p = my_malloc(12); |
| 124 | my_free(p); |
| 125 | return p; // no-warning |
| 126 | } |
| 127 | |
| 128 | // This case is (possibly) ok, be conservative |
| 129 | int * af5() { |
| 130 | int *p = my_malloc(12); |
| 131 | my_hold(p); |
| 132 | return p; // no-warning |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 137 | // This case tests that storing malloc'ed memory to a static variable which is |
| 138 | // then returned is not leaked. In the absence of known contracts for functions |
| 139 | // or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 140 | int *f3() { |
| 141 | static int *p = 0; |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 142 | p = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 143 | return p; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 146 | // This case tests that storing malloc'ed memory to a static global variable |
| 147 | // which is then returned is not leaked. In the absence of known contracts for |
| 148 | // functions or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 149 | static int *p_f4 = 0; |
| 150 | int *f4() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 151 | p_f4 = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 152 | return p_f4; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 153 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 154 | |
| 155 | int *f5() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 156 | int *q = malloc(12); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 157 | q = realloc(q, 20); |
| 158 | return q; // no-warning |
| 159 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 160 | |
| 161 | void f6() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 162 | int *p = malloc(12); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 163 | if (!p) |
| 164 | return; // no-warning |
| 165 | else |
| 166 | free(p); |
| 167 | } |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 168 | |
| 169 | char *doit2(); |
| 170 | void pr6069() { |
| 171 | char *buf = doit2(); |
| 172 | free(buf); |
| 173 | } |
Zhongxing Xu | 181cc3d | 2010-02-14 06:49:48 +0000 | [diff] [blame] | 174 | |
| 175 | void pr6293() { |
| 176 | free(0); |
| 177 | } |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 178 | |
| 179 | void f7() { |
| 180 | char *x = (char*) malloc(4); |
| 181 | free(x); |
| 182 | x[0] = 'a'; // expected-warning{{Use dynamically allocated memory after it is freed.}} |
| 183 | } |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 184 | |
| 185 | void PR6123() { |
| 186 | int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 187 | } |
| 188 | |
| 189 | void PR7217() { |
| 190 | int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 191 | buf[1] = 'c'; // not crash |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 192 | } |
Jordy Rose | c580f2e | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 193 | |
| 194 | void mallocCastToVoid() { |
| 195 | void *p = malloc(2); |
| 196 | const void *cp = p; // not crash |
| 197 | free(p); |
| 198 | } |
| 199 | |
| 200 | void mallocCastToFP() { |
| 201 | void *p = malloc(2); |
| 202 | void (*fp)() = p; // not crash |
| 203 | free(p); |
| 204 | } |
| 205 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 206 | // This tests that malloc() buffers are undefined by default |
| 207 | char mallocGarbage () { |
| 208 | char *buf = malloc(2); |
| 209 | char result = buf[1]; // expected-warning{{undefined}} |
| 210 | free(buf); |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | // This tests that calloc() buffers need to be freed |
| 215 | void callocNoFree () { |
| 216 | char *buf = calloc(2,2); |
| 217 | return; // expected-warning{{never released}} |
| 218 | } |
| 219 | |
| 220 | // These test that calloc() buffers are zeroed by default |
| 221 | char callocZeroesGood () { |
| 222 | char *buf = calloc(2,2); |
| 223 | char result = buf[3]; // no-warning |
| 224 | if (buf[1] == 0) { |
| 225 | free(buf); |
| 226 | } |
| 227 | return result; // no-warning |
| 228 | } |
| 229 | |
| 230 | char callocZeroesBad () { |
| 231 | char *buf = calloc(2,2); |
| 232 | char result = buf[3]; // no-warning |
| 233 | if (buf[1] != 0) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 234 | free(buf); // expected-warning{{never executed}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 235 | } |
| 236 | return result; // expected-warning{{never released}} |
| 237 | } |