Artem Dergachev | 46f3462 | 2018-12-15 02:13:26 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -fblocks -analyze -analyzer-output=text\ |
| 2 | // RUN: -analyzer-checker=core,osx -verify %s |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 3 | |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 4 | #include "os_object_base.h" |
| 5 | #include "os_smart_ptr.h" |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 6 | |
George Karpenkov | 83fb536 | 2018-10-31 17:38:46 +0000 | [diff] [blame] | 7 | struct OSIterator : public OSObject { |
George Karpenkov | a1c3bb8 | 2018-11-30 02:17:31 +0000 | [diff] [blame] | 8 | static const OSMetaClass * const metaClass; |
George Karpenkov | 83fb536 | 2018-10-31 17:38:46 +0000 | [diff] [blame] | 9 | }; |
| 10 | |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 11 | struct OSArray : public OSObject { |
| 12 | unsigned int getCount(); |
| 13 | |
George Karpenkov | a717bc7 | 2018-12-05 18:34:54 +0000 | [diff] [blame] | 14 | OSIterator * getIterator(); |
| 15 | |
| 16 | OSObject *identity() override; |
| 17 | |
George Karpenkov | a71ec6c | 2018-12-06 22:06:44 +0000 | [diff] [blame] | 18 | virtual OSObject *generateObject(OSObject *input); |
| 19 | |
George Karpenkov | a717bc7 | 2018-12-05 18:34:54 +0000 | [diff] [blame] | 20 | virtual void consumeReference(OS_CONSUME OSArray *other); |
| 21 | |
George Karpenkov | b0b6195 | 2018-12-06 22:07:12 +0000 | [diff] [blame] | 22 | void putIntoArray(OSArray *array) OS_CONSUMES_THIS; |
| 23 | |
| 24 | template <typename T> |
| 25 | void putIntoT(T *owner) OS_CONSUMES_THIS; |
| 26 | |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 27 | static OSArray *generateArrayHasCode() { |
| 28 | return new OSArray; |
| 29 | } |
| 30 | |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 31 | static OSArray *withCapacity(unsigned int capacity); |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 32 | static void consumeArray(OS_CONSUME OSArray * array); |
| 33 | |
| 34 | static OSArray* consumeArrayHasCode(OS_CONSUME OSArray * array) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | static OS_RETURNS_NOT_RETAINED OSArray *MaskedGetter(); |
| 39 | static OS_RETURNS_RETAINED OSArray *getOoopsActuallyCreate(); |
| 40 | |
George Karpenkov | 41dc8de | 2018-10-11 22:59:16 +0000 | [diff] [blame] | 41 | static const OSMetaClass * const metaClass; |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
George Karpenkov | a717bc7 | 2018-12-05 18:34:54 +0000 | [diff] [blame] | 44 | struct MyArray : public OSArray { |
| 45 | void consumeReference(OSArray *other) override; |
| 46 | |
| 47 | OSObject *identity() override; |
George Karpenkov | a71ec6c | 2018-12-06 22:06:44 +0000 | [diff] [blame] | 48 | |
| 49 | OSObject *generateObject(OSObject *input) override; |
George Karpenkov | a717bc7 | 2018-12-05 18:34:54 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 52 | struct OtherStruct { |
| 53 | static void doNothingToArray(OSArray *array); |
George Karpenkov | 3cfa04e | 2018-10-25 23:38:41 +0000 | [diff] [blame] | 54 | OtherStruct(OSArray *arr); |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
George Karpenkov | db0c66e | 2019-01-22 19:50:47 +0000 | [diff] [blame] | 57 | bool test_meta_cast_no_leak(OSMetaClassBase *arg) { |
| 58 | return arg && arg->metaCast("blah") != nullptr; |
| 59 | } |
George Karpenkov | 41dc8de | 2018-10-11 22:59:16 +0000 | [diff] [blame] | 60 | |
George Karpenkov | 041c9fa | 2018-12-08 01:18:40 +0000 | [diff] [blame] | 61 | void escape(void *); |
George Karpenkov | 255b058 | 2018-12-21 19:13:40 +0000 | [diff] [blame] | 62 | void escape_with_source(void *p) {} |
George Karpenkov | ff01486 | 2018-12-11 01:13:40 +0000 | [diff] [blame] | 63 | bool coin(); |
| 64 | |
George Karpenkov | 5be959c | 2019-01-11 23:35:17 +0000 | [diff] [blame] | 65 | typedef int kern_return_t; |
| 66 | typedef kern_return_t IOReturn; |
| 67 | typedef kern_return_t OSReturn; |
| 68 | #define kOSReturnSuccess 0 |
| 69 | #define kIOReturnSuccess 0 |
| 70 | |
| 71 | bool write_into_out_param_on_success(OS_RETURNS_RETAINED OSObject **obj); |
| 72 | |
| 73 | void use_out_param() { |
| 74 | OSObject *obj; |
| 75 | if (write_into_out_param_on_success(&obj)) { |
| 76 | obj->release(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void use_out_param_leak() { |
| 81 | OSObject *obj; |
| 82 | write_into_out_param_on_success(&obj); // expected-note-re{{Call to function 'write_into_out_param_on_success' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'obj' (assuming the call returns non-zero){{$}}}} |
| 83 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 84 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 85 | |
| 86 | bool write_into_out_param_on_failure(OS_RETURNS_RETAINED_ON_ZERO OSObject **obj); |
| 87 | |
| 88 | void use_out_param_leak2() { |
| 89 | OSObject *obj; |
| 90 | write_into_out_param_on_failure(&obj); // expected-note-re{{Call to function 'write_into_out_param_on_failure' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'obj' (assuming the call returns zero){{$}}}} |
| 91 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 92 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 93 | |
| 94 | void use_out_param_on_failure() { |
| 95 | OSObject *obj; |
| 96 | if (!write_into_out_param_on_failure(&obj)) { |
| 97 | obj->release(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | IOReturn write_into_out_param_on_nonzero(OS_RETURNS_RETAINED_ON_NONZERO OSObject **obj); |
| 102 | |
| 103 | void use_out_param_on_nonzero() { |
| 104 | OSObject *obj; |
| 105 | if (write_into_out_param_on_nonzero(&obj) != kIOReturnSuccess) { |
| 106 | obj->release(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool write_into_two_out_params(OS_RETURNS_RETAINED OSObject **a, |
| 111 | OS_RETURNS_RETAINED OSObject **b); |
| 112 | |
| 113 | void use_write_into_two_out_params() { |
| 114 | OSObject *obj1; |
| 115 | OSObject *obj2; |
| 116 | if (write_into_two_out_params(&obj1, &obj2)) { |
| 117 | obj1->release(); |
| 118 | obj2->release(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void use_write_two_out_params_leak() { |
| 123 | OSObject *obj1; |
| 124 | OSObject *obj2; |
| 125 | write_into_two_out_params(&obj1, &obj2); // expected-note-re{{Call to function 'write_into_two_out_params' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'a' (assuming the call returns non-zero){{$}}}} |
| 126 | // expected-note-re@-1{{Call to function 'write_into_two_out_params' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'b' (assuming the call returns non-zero){{$}}}} |
| 127 | } // expected-warning{{Potential leak of an object stored into 'obj1'}} |
| 128 | // expected-warning@-1{{Potential leak of an object stored into 'obj2'}} |
| 129 | // expected-note@-2{{Object leaked: object allocated and stored into 'obj1' is not referenced later in this execution path and has a retain count of +1}} |
| 130 | // expected-note@-3{{Object leaked: object allocated and stored into 'obj2' is not referenced later in this execution path and has a retain count of +1}} |
| 131 | |
| 132 | void always_write_into_two_out_params(OS_RETURNS_RETAINED OSObject **a, |
| 133 | OS_RETURNS_RETAINED OSObject **b); |
| 134 | |
| 135 | void use_always_write_into_two_out_params() { |
| 136 | OSObject *obj1; |
| 137 | OSObject *obj2; |
| 138 | always_write_into_two_out_params(&obj1, &obj2); |
| 139 | obj1->release(); |
| 140 | obj2->release(); |
| 141 | } |
| 142 | |
| 143 | void use_always_write_into_two_out_params_leak() { |
| 144 | OSObject *obj1; |
| 145 | OSObject *obj2; |
| 146 | always_write_into_two_out_params(&obj1, &obj2); // expected-note-re{{Call to function 'always_write_into_two_out_params' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'a'{{$}}}} |
| 147 | // expected-note-re@-1{{Call to function 'always_write_into_two_out_params' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'b'{{$}}}} |
| 148 | } // expected-warning{{Potential leak of an object stored into 'obj1'}} |
| 149 | // expected-warning@-1{{Potential leak of an object stored into 'obj2'}} |
| 150 | // expected-note@-2{{Object leaked: object allocated and stored into 'obj1' is not referenced later in this execution path and has a retain count of +1}} |
| 151 | // expected-note@-3{{Object leaked: object allocated and stored into 'obj2' is not referenced later in this execution path and has a retain count of +1}} |
| 152 | |
| 153 | char *write_into_out_param_on_nonnull(OS_RETURNS_RETAINED OSObject **obj); |
| 154 | |
| 155 | void use_out_param_osreturn_on_nonnull() { |
| 156 | OSObject *obj; |
| 157 | if (write_into_out_param_on_nonnull(&obj)) { |
| 158 | obj->release(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void use_out_param_leak_osreturn_on_nonnull() { |
| 163 | OSObject *obj; |
| 164 | write_into_out_param_on_nonnull(&obj); // expected-note-re{{Call to function 'write_into_out_param_on_nonnull' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'obj' (assuming the call returns non-zero){{$}}}} |
| 165 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 166 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 167 | |
| 168 | bool write_optional_out_param(OS_RETURNS_RETAINED OSObject **obj=nullptr); |
| 169 | |
| 170 | void use_optional_out_param() { |
| 171 | if (write_optional_out_param()) {}; |
| 172 | } |
| 173 | |
| 174 | OSReturn write_into_out_param_on_os_success(OS_RETURNS_RETAINED OSObject **obj); |
| 175 | |
| 176 | void write_into_non_retained_out_param(OS_RETURNS_NOT_RETAINED OSObject **obj); |
| 177 | |
| 178 | void use_write_into_non_retained_out_param() { |
| 179 | OSObject *obj; |
| 180 | write_into_non_retained_out_param(&obj); |
| 181 | } |
| 182 | |
| 183 | void use_write_into_non_retained_out_param_uaf() { |
| 184 | OSObject *obj; |
| 185 | write_into_non_retained_out_param(&obj); // expected-note-re{{Call to function 'write_into_non_retained_out_param' writes an OSObject of type 'OSObject' with a +0 retain count into an out parameter 'obj'{{$}}}} |
| 186 | obj->release(); // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} |
| 187 | // expected-note@-1{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} |
| 188 | } |
| 189 | |
| 190 | void always_write_into_out_param(OS_RETURNS_RETAINED OSObject **obj); |
| 191 | |
| 192 | void pass_through_out_param(OSObject **obj) { |
| 193 | always_write_into_out_param(obj); |
| 194 | } |
| 195 | |
| 196 | void always_write_into_out_param_has_source(OS_RETURNS_RETAINED OSObject **obj) { |
| 197 | *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
| 198 | } |
| 199 | |
| 200 | void use_always_write_into_out_param_has_source_leak() { |
| 201 | OSObject *obj; |
| 202 | always_write_into_out_param_has_source(&obj); // expected-note{{Calling 'always_write_into_out_param_has_source'}} |
| 203 | // expected-note@-1{{Returning from 'always_write_into_out_param_has_source'}} |
| 204 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 205 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 206 | |
| 207 | void use_void_out_param_osreturn() { |
| 208 | OSObject *obj; |
| 209 | always_write_into_out_param(&obj); |
| 210 | obj->release(); |
| 211 | } |
| 212 | |
| 213 | void use_void_out_param_osreturn_leak() { |
| 214 | OSObject *obj; |
| 215 | always_write_into_out_param(&obj); // expected-note-re{{Call to function 'always_write_into_out_param' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'obj'{{$}}}} |
| 216 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 217 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 218 | |
| 219 | void use_out_param_osreturn() { |
| 220 | OSObject *obj; |
| 221 | if (write_into_out_param_on_os_success(&obj) == kOSReturnSuccess) { |
| 222 | obj->release(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void use_out_param_leak_osreturn() { |
| 227 | OSObject *obj; |
| 228 | write_into_out_param_on_os_success(&obj); // expected-note-re{{Call to function 'write_into_out_param_on_os_success' writes an OSObject of type 'OSObject' with a +1 retain count into an out parameter 'obj' (assuming the call returns zero){{$}}}} |
| 229 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 230 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 231 | |
George Karpenkov | 0339151 | 2019-01-16 23:21:38 +0000 | [diff] [blame] | 232 | void cleanup(OSObject **obj); |
| 233 | |
| 234 | void test_cleanup_escaping() { |
| 235 | __attribute__((cleanup(cleanup))) OSObject *obj; |
| 236 | always_write_into_out_param(&obj); // no-warning, the value has escaped. |
| 237 | } |
| 238 | |
George Karpenkov | 5be959c | 2019-01-11 23:35:17 +0000 | [diff] [blame] | 239 | struct StructWithField { |
| 240 | OSObject *obj; |
| 241 | |
| 242 | void initViaOutParamCall() { // no warning on writing into fields |
| 243 | always_write_into_out_param(&obj); |
| 244 | } |
| 245 | |
| 246 | }; |
| 247 | |
George Karpenkov | f508532 | 2018-12-21 02:16:23 +0000 | [diff] [blame] | 248 | bool os_consume_violation_two_args(OS_CONSUME OSObject *obj, bool extra) { |
| 249 | if (coin()) { // expected-note{{Assuming the condition is false}} |
| 250 | // expected-note@-1{{Taking false branch}} |
| 251 | escape(obj); |
| 252 | return true; |
| 253 | } |
George Karpenkov | 79f0340 | 2018-12-21 19:13:28 +0000 | [diff] [blame] | 254 | return false; // expected-note{{Parameter 'obj' is marked as consuming, but the function did not consume the reference}} |
George Karpenkov | f508532 | 2018-12-21 02:16:23 +0000 | [diff] [blame] | 255 | } |
| 256 | |
George Karpenkov | ff01486 | 2018-12-11 01:13:40 +0000 | [diff] [blame] | 257 | bool os_consume_violation(OS_CONSUME OSObject *obj) { |
| 258 | if (coin()) { // expected-note{{Assuming the condition is false}} |
| 259 | // expected-note@-1{{Taking false branch}} |
| 260 | escape(obj); |
| 261 | return true; |
| 262 | } |
George Karpenkov | 79f0340 | 2018-12-21 19:13:28 +0000 | [diff] [blame] | 263 | return false; // expected-note{{Parameter 'obj' is marked as consuming, but the function did not consume the reference}} |
George Karpenkov | ff01486 | 2018-12-11 01:13:40 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void os_consume_ok(OS_CONSUME OSObject *obj) { |
| 267 | escape(obj); |
| 268 | } |
| 269 | |
| 270 | void use_os_consume_violation() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 271 | OSObject *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
George Karpenkov | ff01486 | 2018-12-11 01:13:40 +0000 | [diff] [blame] | 272 | os_consume_violation(obj); // expected-note{{Calling 'os_consume_violation'}} |
| 273 | // expected-note@-1{{Returning from 'os_consume_violation'}} |
| 274 | } // expected-note{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 275 | // expected-warning@-1{{Potential leak of an object stored into 'obj'}} |
| 276 | |
George Karpenkov | f508532 | 2018-12-21 02:16:23 +0000 | [diff] [blame] | 277 | void use_os_consume_violation_two_args() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 278 | OSObject *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
George Karpenkov | f508532 | 2018-12-21 02:16:23 +0000 | [diff] [blame] | 279 | os_consume_violation_two_args(obj, coin()); // expected-note{{Calling 'os_consume_violation_two_args'}} |
| 280 | // expected-note@-1{{Returning from 'os_consume_violation_two_args'}} |
| 281 | } // expected-note{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 282 | // expected-warning@-1{{Potential leak of an object stored into 'obj'}} |
| 283 | |
George Karpenkov | ff01486 | 2018-12-11 01:13:40 +0000 | [diff] [blame] | 284 | void use_os_consume_ok() { |
| 285 | OSObject *obj = new OSObject; |
| 286 | os_consume_ok(obj); |
| 287 | } |
George Karpenkov | 041c9fa | 2018-12-08 01:18:40 +0000 | [diff] [blame] | 288 | |
| 289 | void test_escaping_into_voidstar() { |
| 290 | OSObject *obj = new OSObject; |
| 291 | escape(obj); |
| 292 | } |
| 293 | |
George Karpenkov | 255b058 | 2018-12-21 19:13:40 +0000 | [diff] [blame] | 294 | void test_escape_has_source() { |
| 295 | OSObject *obj = new OSObject; |
| 296 | if (obj) |
George Karpenkov | d76cc59 | 2018-12-21 19:40:44 +0000 | [diff] [blame] | 297 | escape_with_source(obj); |
George Karpenkov | 255b058 | 2018-12-21 19:13:40 +0000 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
George Karpenkov | a71ec6c | 2018-12-06 22:06:44 +0000 | [diff] [blame] | 301 | void test_no_infinite_check_recursion(MyArray *arr) { |
| 302 | OSObject *input = new OSObject; |
| 303 | OSObject *o = arr->generateObject(input); |
| 304 | o->release(); |
| 305 | input->release(); |
| 306 | } |
| 307 | |
| 308 | |
George Karpenkov | a717bc7 | 2018-12-05 18:34:54 +0000 | [diff] [blame] | 309 | void check_param_attribute_propagation(MyArray *parent) { |
| 310 | OSArray *arr = new OSArray; |
| 311 | parent->consumeReference(arr); |
| 312 | } |
| 313 | |
| 314 | unsigned int check_attribute_propagation(OSArray *arr) { |
| 315 | OSObject *other = arr->identity(); |
| 316 | OSArray *casted = OSDynamicCast(OSArray, other); |
| 317 | if (casted) |
| 318 | return casted->getCount(); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | unsigned int check_attribute_indirect_propagation(MyArray *arr) { |
| 323 | OSObject *other = arr->identity(); |
| 324 | OSArray *casted = OSDynamicCast(OSArray, other); |
| 325 | if (casted) |
| 326 | return casted->getCount(); |
| 327 | return 0; |
| 328 | } |
| 329 | |
George Karpenkov | b0b6195 | 2018-12-06 22:07:12 +0000 | [diff] [blame] | 330 | void check_consumes_this(OSArray *owner) { |
| 331 | OSArray *arr = new OSArray; |
| 332 | arr->putIntoArray(owner); |
| 333 | } |
| 334 | |
| 335 | void check_consumes_this_with_template(OSArray *owner) { |
| 336 | OSArray *arr = new OSArray; |
| 337 | arr->putIntoT(owner); |
| 338 | } |
| 339 | |
George Karpenkov | be3f4bd | 2018-11-30 20:43:42 +0000 | [diff] [blame] | 340 | void check_free_no_error() { |
| 341 | OSArray *arr = OSArray::withCapacity(10); |
| 342 | arr->retain(); |
| 343 | arr->retain(); |
| 344 | arr->retain(); |
| 345 | arr->free(); |
| 346 | } |
| 347 | |
| 348 | void check_free_use_after_free() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 349 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | be3f4bd | 2018-11-30 20:43:42 +0000 | [diff] [blame] | 350 | arr->retain(); // expected-note{{Reference count incremented. The object now has a +2 retain count}} |
| 351 | arr->free(); // expected-note{{Object released}} |
| 352 | arr->retain(); // expected-warning{{Reference-counted object is used after it is released}} |
| 353 | // expected-note@-1{{Reference-counted object is used after it is released}} |
| 354 | } |
| 355 | |
| 356 | unsigned int check_leak_explicit_new() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 357 | OSArray *arr = new OSArray; // expected-note{{Operator 'new' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | 79ed11c | 2018-12-11 01:13:20 +0000 | [diff] [blame] | 358 | return arr->getCount(); // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}} |
| 359 | // expected-warning@-1{{Potential leak of an object stored into 'arr'}} |
George Karpenkov | be3f4bd | 2018-11-30 20:43:42 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | unsigned int check_leak_factory() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 363 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | be3f4bd | 2018-11-30 20:43:42 +0000 | [diff] [blame] | 364 | return arr->getCount(); // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}} |
| 365 | // expected-warning@-1{{Potential leak of an object stored into 'arr'}} |
| 366 | } |
| 367 | |
George Karpenkov | 3bdbeb1 | 2018-11-30 02:18:10 +0000 | [diff] [blame] | 368 | void check_get_object() { |
| 369 | OSObject::getObject(); |
| 370 | } |
| 371 | |
| 372 | void check_Get_object() { |
| 373 | OSObject::GetObject(); |
| 374 | } |
| 375 | |
George Karpenkov | 83fb536 | 2018-10-31 17:38:46 +0000 | [diff] [blame] | 376 | void check_custom_iterator_rule(OSArray *arr) { |
| 377 | OSIterator *it = arr->getIterator(); |
| 378 | it->release(); |
| 379 | } |
| 380 | |
George Karpenkov | e2f0954 | 2018-11-30 02:17:57 +0000 | [diff] [blame] | 381 | void check_iterator_leak(OSArray *arr) { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 382 | arr->getIterator(); // expected-note{{Call to method 'OSArray::getIterator' returns an OSObject of type 'OSIterator' with a +1 retain count}} |
| 383 | } // expected-note{{Object leaked: allocated object of type 'OSIterator' is not referenced later}} |
| 384 | // expected-warning@-1{{Potential leak of an object of type 'OSIterator}}' |
George Karpenkov | e2f0954 | 2018-11-30 02:17:57 +0000 | [diff] [blame] | 385 | |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 386 | void check_no_invalidation() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 387 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 388 | OtherStruct::doNothingToArray(arr); |
| 389 | } // expected-warning{{Potential leak of an object stored into 'arr'}} |
| 390 | // expected-note@-1{{Object leaked}} |
| 391 | |
George Karpenkov | 3cfa04e | 2018-10-25 23:38:41 +0000 | [diff] [blame] | 392 | void check_no_invalidation_other_struct() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 393 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | 3cfa04e | 2018-10-25 23:38:41 +0000 | [diff] [blame] | 394 | OtherStruct other(arr); // expected-warning{{Potential leak}} |
| 395 | // expected-note@-1{{Object leaked}} |
| 396 | } |
| 397 | |
George Karpenkov | 6fd5c86d | 2018-10-31 17:38:29 +0000 | [diff] [blame] | 398 | struct ArrayOwner : public OSObject { |
| 399 | OSArray *arr; |
| 400 | ArrayOwner(OSArray *arr) : arr(arr) {} |
| 401 | |
| 402 | static ArrayOwner* create(OSArray *arr) { |
| 403 | return new ArrayOwner(arr); |
| 404 | } |
| 405 | |
| 406 | OSArray *getArray() { |
| 407 | return arr; |
| 408 | } |
| 409 | |
| 410 | OSArray *createArray() { |
| 411 | return OSArray::withCapacity(10); |
| 412 | } |
| 413 | |
| 414 | OSArray *createArraySourceUnknown(); |
| 415 | |
| 416 | OSArray *getArraySourceUnknown(); |
| 417 | }; |
| 418 | |
George Karpenkov | a1c3bb8 | 2018-11-30 02:17:31 +0000 | [diff] [blame] | 419 | OSArray *generateArray() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 420 | return OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
| 421 | // expected-note@-1{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | a1c3bb8 | 2018-11-30 02:17:31 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | unsigned int check_leak_good_error_message() { |
| 425 | unsigned int out; |
| 426 | { |
| 427 | OSArray *leaked = generateArray(); // expected-note{{Calling 'generateArray'}} |
| 428 | // expected-note@-1{{Returning from 'generateArray'}} |
| 429 | out = leaked->getCount(); // expected-warning{{Potential leak of an object stored into 'leaked'}} |
| 430 | // expected-note@-1{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}} |
| 431 | } |
| 432 | return out; |
| 433 | } |
| 434 | |
| 435 | unsigned int check_leak_msg_temporary() { |
George Karpenkov | f893ea1 | 2018-11-30 02:17:44 +0000 | [diff] [blame] | 436 | return generateArray()->getCount(); // expected-warning{{Potential leak of an object}} |
| 437 | // expected-note@-1{{Calling 'generateArray'}} |
| 438 | // expected-note@-2{{Returning from 'generateArray'}} |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 439 | // expected-note@-3{{Object leaked: allocated object of type 'OSArray' is not referenced later in this execution path and has a retain count of +1}} |
George Karpenkov | a1c3bb8 | 2018-11-30 02:17:31 +0000 | [diff] [blame] | 440 | } |
| 441 | |
George Karpenkov | 6fd5c86d | 2018-10-31 17:38:29 +0000 | [diff] [blame] | 442 | void check_confusing_getters() { |
| 443 | OSArray *arr = OSArray::withCapacity(10); |
| 444 | |
| 445 | ArrayOwner *AO = ArrayOwner::create(arr); |
| 446 | AO->getArray(); |
| 447 | |
| 448 | AO->release(); |
| 449 | arr->release(); |
| 450 | } |
| 451 | |
George Karpenkov | 081c477 | 2018-10-23 23:11:50 +0000 | [diff] [blame] | 452 | void check_rc_consumed() { |
| 453 | OSArray *arr = OSArray::withCapacity(10); |
| 454 | OSArray::consumeArray(arr); |
| 455 | } |
| 456 | |
| 457 | void check_rc_consume_temporary() { |
| 458 | OSArray::consumeArray(OSArray::withCapacity(10)); |
| 459 | } |
| 460 | |
| 461 | void check_rc_getter() { |
| 462 | OSArray *arr = OSArray::MaskedGetter(); |
| 463 | (void)arr; |
| 464 | } |
| 465 | |
| 466 | void check_rc_create() { |
| 467 | OSArray *arr = OSArray::getOoopsActuallyCreate(); |
| 468 | arr->release(); |
| 469 | } |
| 470 | |
| 471 | |
George Karpenkov | 41dc8de | 2018-10-11 22:59:16 +0000 | [diff] [blame] | 472 | void check_dynamic_cast() { |
| 473 | OSArray *arr = OSDynamicCast(OSArray, OSObject::generateObject(1)); |
| 474 | arr->release(); |
| 475 | } |
| 476 | |
George Karpenkov | 3c2ed8f | 2018-10-25 23:38:07 +0000 | [diff] [blame] | 477 | unsigned int check_dynamic_cast_no_null_on_orig(OSObject *obj) { |
| 478 | OSArray *arr = OSDynamicCast(OSArray, obj); |
| 479 | if (arr) { |
| 480 | return arr->getCount(); |
| 481 | } else { |
| 482 | |
| 483 | // The fact that dynamic cast has failed should not imply that |
| 484 | // the input object was null. |
| 485 | return obj->foo(); // no-warning |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void check_dynamic_cast_null_branch(OSObject *obj) { |
George Karpenkov | e2f0954 | 2018-11-30 02:17:57 +0000 | [diff] [blame] | 490 | OSArray *arr1 = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject}} |
George Karpenkov | a9e2956 | 2019-01-22 19:51:00 +0000 | [diff] [blame] | 491 | OSArray *arr = OSDynamicCast(OSArray, obj); // expected-note{{Assuming dynamic cast returns null due to type mismatch}} |
George Karpenkov | 3c2ed8f | 2018-10-25 23:38:07 +0000 | [diff] [blame] | 492 | if (!arr) // expected-note{{Taking true branch}} |
George Karpenkov | a1c3bb8 | 2018-11-30 02:17:31 +0000 | [diff] [blame] | 493 | return; // expected-warning{{Potential leak of an object stored into 'arr1'}} |
George Karpenkov | 3c2ed8f | 2018-10-25 23:38:07 +0000 | [diff] [blame] | 494 | // expected-note@-1{{Object leaked}} |
| 495 | arr1->release(); |
| 496 | } |
| 497 | |
George Karpenkov | 41dc8de | 2018-10-11 22:59:16 +0000 | [diff] [blame] | 498 | void check_dynamic_cast_null_check() { |
George Karpenkov | e2f0954 | 2018-11-30 02:17:57 +0000 | [diff] [blame] | 499 | OSArray *arr = OSDynamicCast(OSArray, OSObject::generateObject(1)); // expected-note{{Call to method 'OSObject::generateObject' returns an OSObject}} |
George Karpenkov | 3c2ed8f | 2018-10-25 23:38:07 +0000 | [diff] [blame] | 500 | // expected-warning@-1{{Potential leak of an object}} |
| 501 | // expected-note@-2{{Object leaked}} |
George Karpenkov | a9e2956 | 2019-01-22 19:51:00 +0000 | [diff] [blame] | 502 | // expected-note@-3{{Assuming dynamic cast returns null due to type mismatch}} |
George Karpenkov | 41dc8de | 2018-10-11 22:59:16 +0000 | [diff] [blame] | 503 | if (!arr) |
| 504 | return; |
| 505 | arr->release(); |
| 506 | } |
| 507 | |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 508 | void use_after_release() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 509 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 510 | arr->release(); // expected-note{{Object released}} |
| 511 | arr->getCount(); // expected-warning{{Reference-counted object is used after it is released}} |
| 512 | // expected-note@-1{{Reference-counted object is used after it is released}} |
| 513 | } |
| 514 | |
| 515 | void potential_leak() { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 516 | OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type 'OSArray' with a +1 retain count}} |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 517 | arr->retain(); // expected-note{{Reference count incremented. The object now has a +2 retain count}} |
| 518 | arr->release(); // expected-note{{Reference count decremented. The object now has a +1 retain count}} |
| 519 | arr->getCount(); |
| 520 | } // expected-warning{{Potential leak of an object stored into 'arr'}} |
| 521 | // expected-note@-1{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}} |
| 522 | |
| 523 | void proper_cleanup() { |
| 524 | OSArray *arr = OSArray::withCapacity(10); // +1 |
| 525 | arr->retain(); // +2 |
| 526 | arr->release(); // +1 |
| 527 | arr->getCount(); |
| 528 | arr->release(); // 0 |
| 529 | } |
| 530 | |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 531 | unsigned int no_warning_on_getter(ArrayOwner *owner) { |
| 532 | OSArray *arr = owner->getArray(); |
| 533 | return arr->getCount(); |
| 534 | } |
| 535 | |
| 536 | unsigned int warn_on_overrelease(ArrayOwner *owner) { |
George Karpenkov | 6fd5c86d | 2018-10-31 17:38:29 +0000 | [diff] [blame] | 537 | // FIXME: summaries are not applied in case the source of the getter/setter |
| 538 | // is known. |
| 539 | // rdar://45681203 |
| 540 | OSArray *arr = owner->getArray(); |
| 541 | arr->release(); |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 542 | return arr->getCount(); |
| 543 | } |
| 544 | |
| 545 | unsigned int nowarn_on_release_of_created(ArrayOwner *owner) { |
| 546 | OSArray *arr = owner->createArray(); |
| 547 | unsigned int out = arr->getCount(); |
| 548 | arr->release(); |
| 549 | return out; |
| 550 | } |
| 551 | |
| 552 | unsigned int nowarn_on_release_of_created_source_unknown(ArrayOwner *owner) { |
| 553 | OSArray *arr = owner->createArraySourceUnknown(); |
| 554 | unsigned int out = arr->getCount(); |
| 555 | arr->release(); |
| 556 | return out; |
| 557 | } |
| 558 | |
| 559 | unsigned int no_warn_ok_release(ArrayOwner *owner) { |
| 560 | OSArray *arr = owner->getArray(); // +0 |
| 561 | arr->retain(); // +1 |
| 562 | arr->release(); // +0 |
| 563 | return arr->getCount(); // no-warning |
| 564 | } |
| 565 | |
| 566 | unsigned int warn_on_overrelease_with_unknown_source(ArrayOwner *owner) { |
George Karpenkov | 4f64b38 | 2019-01-10 18:15:57 +0000 | [diff] [blame] | 567 | OSArray *arr = owner->getArraySourceUnknown(); // expected-note{{Call to method 'ArrayOwner::getArraySourceUnknown' returns an OSObject of type 'OSArray' with a +0 retain count}} |
George Karpenkov | ab0011e | 2018-08-23 00:26:59 +0000 | [diff] [blame] | 568 | arr->release(); // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} |
| 569 | // expected-note@-1{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} |
| 570 | return arr->getCount(); |
| 571 | } |
| 572 | |
| 573 | unsigned int ok_release_with_unknown_source(ArrayOwner *owner) { |
| 574 | OSArray *arr = owner->getArraySourceUnknown(); // +0 |
| 575 | arr->retain(); // +1 |
| 576 | arr->release(); // +0 |
| 577 | return arr->getCount(); |
| 578 | } |
Artem Dergachev | 46f3462 | 2018-12-15 02:13:26 +0000 | [diff] [blame] | 579 | |
| 580 | OSObject *getObject(); |
| 581 | typedef bool (^Blk)(OSObject *); |
| 582 | |
| 583 | void test_escape_to_unknown_block(Blk blk) { |
| 584 | blk(getObject()); // no-crash |
| 585 | } |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 586 | |
| 587 | using OSObjectPtr = os::smart_ptr<OSObject>; |
| 588 | |
| 589 | void test_smart_ptr_uaf() { |
| 590 | OSObject *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
| 591 | { |
| 592 | OSObjectPtr p(obj); // expected-note{{Calling constructor for 'smart_ptr<OSObject>'}} |
| 593 | // expected-note@-1{{Returning from constructor for 'smart_ptr<OSObject>'}} |
| 594 | // expected-note@os_smart_ptr.h:13{{Taking true branch}} |
| 595 | // expected-note@os_smart_ptr.h:14{{Calling 'smart_ptr::_retain'}} |
George Karpenkov | 0f3bbba | 2019-01-29 19:29:07 +0000 | [diff] [blame] | 596 | // expected-note@os_smart_ptr.h:71{{Reference count incremented. The object now has a +2 retain count}} |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 597 | // expected-note@os_smart_ptr.h:14{{Returning from 'smart_ptr::_retain'}} |
| 598 | } // expected-note{{Calling '~smart_ptr'}} |
| 599 | // expected-note@os_smart_ptr.h:35{{Taking true branch}} |
| 600 | // expected-note@os_smart_ptr.h:36{{Calling 'smart_ptr::_release'}} |
George Karpenkov | 0f3bbba | 2019-01-29 19:29:07 +0000 | [diff] [blame] | 601 | // expected-note@os_smart_ptr.h:76{{Reference count decremented. The object now has a +1 retain count}} |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 602 | // expected-note@os_smart_ptr.h:36{{Returning from 'smart_ptr::_release'}} |
| 603 | // expected-note@-5{{Returning from '~smart_ptr'}} |
| 604 | obj->release(); // expected-note{{Object released}} |
| 605 | obj->release(); // expected-warning{{Reference-counted object is used after it is released}} |
| 606 | // expected-note@-1{{Reference-counted object is used after it is released}} |
| 607 | } |
| 608 | |
| 609 | void test_smart_ptr_leak() { |
| 610 | OSObject *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
| 611 | { |
| 612 | OSObjectPtr p(obj); // expected-note{{Calling constructor for 'smart_ptr<OSObject>'}} |
| 613 | // expected-note@-1{{Returning from constructor for 'smart_ptr<OSObject>'}} |
| 614 | // expected-note@os_smart_ptr.h:13{{Taking true branch}} |
| 615 | // expected-note@os_smart_ptr.h:14{{Calling 'smart_ptr::_retain'}} |
George Karpenkov | 0f3bbba | 2019-01-29 19:29:07 +0000 | [diff] [blame] | 616 | // expected-note@os_smart_ptr.h:71{{Reference count incremented. The object now has a +2 retain count}} |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 617 | // expected-note@os_smart_ptr.h:14{{Returning from 'smart_ptr::_retain'}} |
| 618 | } // expected-note{{Calling '~smart_ptr'}} |
| 619 | // expected-note@os_smart_ptr.h:35{{Taking true branch}} |
| 620 | // expected-note@os_smart_ptr.h:36{{Calling 'smart_ptr::_release'}} |
George Karpenkov | 0f3bbba | 2019-01-29 19:29:07 +0000 | [diff] [blame] | 621 | // expected-note@os_smart_ptr.h:76{{Reference count decremented. The object now has a +1 retain count}} |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 622 | // expected-note@os_smart_ptr.h:36{{Returning from 'smart_ptr::_release'}} |
| 623 | // expected-note@-5{{Returning from '~smart_ptr'}} |
| 624 | } // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 625 | // expected-note@-1{{Object leaked: object allocated and stored into 'obj' is not referenced later in this execution path and has a retain count of +1}} |
| 626 | |
| 627 | void test_smart_ptr_no_leak() { |
| 628 | OSObject *obj = new OSObject; |
| 629 | { |
| 630 | OSObjectPtr p(obj); |
| 631 | } |
| 632 | obj->release(); |
| 633 | } |
| 634 | |
| 635 | void test_ostypealloc_correct_diagnostic_name() { |
| 636 | OSArray *arr = OSTypeAlloc(OSArray); // expected-note{{Call to method 'OSMetaClass::alloc' returns an OSObject of type 'OSArray' with a +1 retain count}} |
| 637 | arr->retain(); // expected-note{{Reference count incremented. The object now has a +2 retain count}} |
| 638 | arr->release(); // expected-note{{Reference count decremented. The object now has a +1 retain count}} |
| 639 | } // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}} |
| 640 | // expected-warning@-1{{Potential leak of an object stored into 'arr'}} |
| 641 | |
| 642 | void escape_elsewhere(OSObject *obj); |
| 643 | |
| 644 | void test_free_on_escaped_object_diagnostics() { |
| 645 | OSObject *obj = new OSObject; // expected-note{{Operator 'new' returns an OSObject of type 'OSObject' with a +1 retain count}} |
| 646 | escape_elsewhere(obj); // expected-note{{Object is now not exclusively owned}} |
| 647 | obj->free(); // expected-note{{'free' called on an object that may be referenced elsewhere}} |
| 648 | // expected-warning@-1{{'free' called on an object that may be referenced elsewhere}} |
| 649 | } |
| 650 | |
George Karpenkov | 0f3bbba | 2019-01-29 19:29:07 +0000 | [diff] [blame] | 651 | void test_tagged_retain_no_leak() { |
| 652 | OSObject *obj = new OSObject; |
| 653 | obj->taggedRelease(); |
| 654 | } |
| 655 | |
| 656 | void test_tagged_retain_no_uaf() { |
| 657 | OSObject *obj = new OSObject; |
| 658 | obj->taggedRetain(); |
| 659 | obj->release(); |
| 660 | obj->release(); |
| 661 | } |
| 662 | |