| 1 |
// RUN: %clang_analyze_cc1 -verify -analyzer-output=text %s \ |
| 2 |
// RUN: -analyzer-checker=core \ |
| 3 |
// RUN: -analyzer-checker=cplusplus \ |
| 4 |
// RUN: -analyzer-checker=unix |
| 5 |
|
| 6 |
#include "Inputs/system-header-simulator-for-malloc.h" |
| 7 |
|
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
// unique_ptr test cases |
| 10 |
//===----------------------------------------------------------------------===// |
| 11 |
namespace unique_ptr_tests { |
| 12 |
|
| 13 |
// Custom unique_ptr implementation for testing |
| 14 |
template <typename T> |
| 15 |
struct unique_ptr { |
| 16 |
T* ptr; |
| 17 |
unique_ptr(T* p) : ptr(p) {} |
| 18 |
~unique_ptr() { |
| 19 |
// This destructor intentionally doesn't delete 'ptr' to validate that the |
| 20 |
// heuristic trusts that smart pointers (based on their class name) will |
| 21 |
// release the pointee even if it doesn't understand their destructor. |
| 22 |
} |
| 23 |
unique_ptr(unique_ptr&& other) : ptr(other.ptr) { other.ptr = nullptr; } |
| 24 |
T* get() const { return ptr; } |
| 25 |
}; |
| 26 |
|
| 27 |
template <typename T, typename... Args> |
| 28 |
unique_ptr<T> make_unique(Args&&... args) { |
| 29 |
return unique_ptr<T>(new T(args...)); |
| 30 |
} |
| 31 |
|
| 32 |
// Test 1: Check that we report leaks for malloc when passing smart pointers |
| 33 |
void add_unique_ptr(unique_ptr<int> ptr) { |
| 34 |
// The unique_ptr destructor will be called when ptr goes out of scope |
| 35 |
} |
| 36 |
|
| 37 |
void test_malloc_with_smart_ptr() { |
| 38 |
void *ptr = malloc(4); // expected-note {{Memory is allocated}} |
| 39 |
|
| 40 |
add_unique_ptr(make_unique<int>(1)); |
| 41 |
(void)ptr; |
| 42 |
// expected-warning@+1 {{Potential leak of memory pointed to by 'ptr'}} expected-note@+1 {{Potential leak of memory pointed to by 'ptr'}} |
| 43 |
} |
| 44 |
|
| 45 |
// Test 2: Check that we don't report leaks for unique_ptr in temporary objects |
| 46 |
struct Foo { |
| 47 |
unique_ptr<int> i; |
| 48 |
}; |
| 49 |
|
| 50 |
void add_foo(Foo foo) { |
| 51 |
// The unique_ptr destructor will be called when foo goes out of scope |
| 52 |
} |
| 53 |
|
| 54 |
void test_temporary_object() { |
| 55 |
// No warning should be emitted for this - the memory is managed by unique_ptr |
| 56 |
// in the temporary Foo object, which will properly clean up the memory |
| 57 |
add_foo({make_unique<int>(1)}); |
| 58 |
} |
| 59 |
|
| 60 |
// Test 3: Check that we don't report leaks for smart pointers in base class fields |
| 61 |
struct Base { |
| 62 |
unique_ptr<int> base_ptr; |
| 63 |
Base() : base_ptr(nullptr) {} |
| 64 |
Base(unique_ptr<int>&& ptr) : base_ptr(static_cast<unique_ptr<int>&&>(ptr)) {} |
| 65 |
}; |
| 66 |
|
| 67 |
struct Derived : public Base { |
| 68 |
int derived_field; |
| 69 |
Derived() : Base(), derived_field(0) {} |
| 70 |
Derived(unique_ptr<int>&& ptr, int field) : Base(static_cast<unique_ptr<int>&&>(ptr)), derived_field(field) {} |
| 71 |
}; |
| 72 |
|
| 73 |
void add_derived(Derived derived) { |
| 74 |
// The unique_ptr destructor will be called when derived goes out of scope |
| 75 |
// This should include the base_ptr field from the base class |
| 76 |
} |
| 77 |
|
| 78 |
void test_base_class_smart_ptr() { |
| 79 |
// No warning should be emitted for this - the memory is managed by unique_ptr |
| 80 |
// in the base class field of the temporary Derived object |
| 81 |
add_derived(Derived(make_unique<int>(1), 42)); |
| 82 |
} |
| 83 |
|
| 84 |
// Test 4: Check that we don't report leaks for multiple owning arguments |
| 85 |
struct SinglePtr { |
| 86 |
unique_ptr<int> ptr; |
| 87 |
SinglePtr(unique_ptr<int>&& p) : ptr(static_cast<unique_ptr<int>&&>(p)) {} |
| 88 |
}; |
| 89 |
|
| 90 |
struct MultiPtr { |
| 91 |
unique_ptr<int> ptr1; |
| 92 |
unique_ptr<int> ptr2; |
| 93 |
unique_ptr<int> ptr3; |
| 94 |
|
| 95 |
MultiPtr(unique_ptr<int>&& p1, unique_ptr<int>&& p2, unique_ptr<int>&& p3) |
| 96 |
: ptr1(static_cast<unique_ptr<int>&&>(p1)) |
| 97 |
, ptr2(static_cast<unique_ptr<int>&&>(p2)) |
| 98 |
, ptr3(static_cast<unique_ptr<int>&&>(p3)) {} |
| 99 |
}; |
| 100 |
|
| 101 |
void addMultiple(SinglePtr single, MultiPtr multi) { |
| 102 |
// All unique_ptr destructors will be called when the objects go out of scope |
| 103 |
// This tests handling of multiple by-value arguments with smart pointer fields |
| 104 |
} |
| 105 |
|
| 106 |
void test_multiple_owning_args() { |
| 107 |
// No warning should be emitted - all memory is properly managed by unique_ptr |
| 108 |
// in the temporary objects, which will properly clean up the memory |
| 109 |
addMultiple( |
| 110 |
SinglePtr(make_unique<int>(1)), |
| 111 |
MultiPtr(make_unique<int>(2), make_unique<int>(3), make_unique<int>(4)) |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
// Test 5: Check that we DO report leaks for raw pointers in mixed ownership scenarios |
| 116 |
struct MixedOwnership { |
| 117 |
unique_ptr<int> smart_ptr; // Should NOT leak (smart pointer managed) |
| 118 |
int *raw_ptr; // Should leak (raw pointer) |
| 119 |
|
| 120 |
MixedOwnership() : smart_ptr(make_unique<int>(1)), raw_ptr(new int(42)) {} // expected-note {{Memory is allocated}} |
| 121 |
}; |
| 122 |
|
| 123 |
void consume(MixedOwnership obj) { |
| 124 |
// The unique_ptr destructor will be called when obj goes out of scope |
| 125 |
// But raw_ptr will leak! |
| 126 |
} |
| 127 |
|
| 128 |
void test_mixed_ownership() { |
| 129 |
// This should report a leak for raw_ptr but not for smart_ptr |
| 130 |
consume(MixedOwnership()); // expected-note {{Calling default constructor for 'MixedOwnership'}} expected-note {{Returning from default constructor for 'MixedOwnership'}} |
| 131 |
} // expected-warning {{Potential memory leak}} expected-note {{Potential memory leak}} |
| 132 |
|
| 133 |
// Test 6: Check that we handle direct smart pointer constructor calls correctly |
| 134 |
void test_direct_constructor() { |
| 135 |
// Direct constructor call - should not leak |
| 136 |
int* raw_ptr = new int(42); |
| 137 |
unique_ptr<int> smart(raw_ptr); // This should escape the raw_ptr symbol |
| 138 |
// No leak should be reported here since smart pointer takes ownership |
| 139 |
} |
| 140 |
|
| 141 |
void test_mixed_direct_constructor() { |
| 142 |
int* raw1 = new int(1); |
| 143 |
int* raw2 = new int(2); // expected-note {{Memory is allocated}} |
| 144 |
|
| 145 |
unique_ptr<int> smart(raw1); // This should escape raw1 |
| 146 |
// raw2 should leak since it's not managed by any smart pointer |
| 147 |
int x = *raw2; // expected-warning {{Potential leak of memory pointed to by 'raw2'}} expected-note {{Potential leak of memory pointed to by 'raw2'}} |
| 148 |
} |
| 149 |
|
| 150 |
// Test 7: Multiple memory owning arguments - demonstrates addTransition API usage |
| 151 |
void addMultipleOwningArgs( |
| 152 |
unique_ptr<int> ptr1, |
| 153 |
unique_ptr<int> ptr2, |
| 154 |
unique_ptr<int> ptr3 |
| 155 |
) { |
| 156 |
// All unique_ptr destructors will be called when arguments go out of scope |
| 157 |
// This tests handling of multiple smart pointer parameters in a single call |
| 158 |
} |
| 159 |
|
| 160 |
void test_multiple_memory_owning_arguments() { |
| 161 |
// No warning should be emitted - all memory is properly managed by unique_ptr |
| 162 |
// This test specifically exercises the addTransition API with multiple owning arguments |
| 163 |
addMultipleOwningArgs( |
| 164 |
make_unique<int>(1), |
| 165 |
make_unique<int>(2), |
| 166 |
make_unique<int>(3) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
} // namespace unique_ptr_tests |
| 171 |
|
| 172 |
//===----------------------------------------------------------------------===// |
| 173 |
// Variadic constructor test cases |
| 174 |
//===----------------------------------------------------------------------===// |
| 175 |
namespace variadic_constructor_tests { |
| 176 |
|
| 177 |
// Variadic constructor - test for potential out-of-bounds access |
| 178 |
// This is the only test in this namespace and tests a scenario where Call.getNumArgs() > CD->getNumParams() |
| 179 |
// We use a synthetic unique_ptr here to activate the specific logic in the MallocChecker that will test out of bounds |
| 180 |
template <typename T> |
| 181 |
struct unique_ptr { |
| 182 |
T* ptr; |
| 183 |
|
| 184 |
// Constructor with ellipsis - can receive more arguments than parameters |
| 185 |
unique_ptr(T* p, ...) : ptr(p) {} |
| 186 |
|
| 187 |
~unique_ptr() { |
| 188 |
// This destructor intentionally doesn't delete 'ptr' to validate that the |
| 189 |
// heuristic trusts that smart pointers (based on their class name) will |
| 190 |
// release the pointee even if it doesn't understand their destructor. |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
void process_variadic_smart_ptr(unique_ptr<int> ptr) { |
| 195 |
// Function body doesn't matter for this test |
| 196 |
} |
| 197 |
|
| 198 |
void test_variadic_constructor_bounds() { |
| 199 |
void *malloc_ptr = malloc(4); // expected-note {{Memory is allocated}} |
| 200 |
|
| 201 |
// This call creates a smart pointer with more arguments than formal parameters |
| 202 |
// The constructor has 1 formal parameter (T* p) plus ellipsis, but we pass multiple args |
| 203 |
// This should trigger the bounds checking issue in handleSmartPointerConstructorArguments |
| 204 |
int* raw_ptr = new int(42); |
| 205 |
process_variadic_smart_ptr(unique_ptr<int>(raw_ptr, 1, 2, 3, 4, 5)); |
| 206 |
|
| 207 |
(void)malloc_ptr; |
| 208 |
} // expected-warning {{Potential leak of memory pointed to by 'malloc_ptr'}} |
| 209 |
// expected-note@-1 {{Potential leak of memory pointed to by 'malloc_ptr'}} |
| 210 |
|
| 211 |
} // namespace variadic_constructor_tests |
| 212 |
|
| 213 |
//===----------------------------------------------------------------------===// |
| 214 |
// shared_ptr test cases |
| 215 |
//===----------------------------------------------------------------------===// |
| 216 |
namespace shared_ptr_tests { |
| 217 |
|
| 218 |
// Custom shared_ptr implementation for testing |
| 219 |
template <typename T> |
| 220 |
struct shared_ptr { |
| 221 |
T* ptr; |
| 222 |
shared_ptr(T* p) : ptr(p) {} |
| 223 |
~shared_ptr() { |
| 224 |
// This destructor intentionally doesn't delete 'ptr' to validate that the |
| 225 |
// heuristic trusts that smart pointers (based on their class name) will |
| 226 |
// release the pointee even if it doesn't understand their destructor. |
| 227 |
} |
| 228 |
shared_ptr(shared_ptr&& other) : ptr(other.ptr) { other.ptr = nullptr; } |
| 229 |
T* get() const { return ptr; } |
| 230 |
}; |
| 231 |
|
| 232 |
template <typename T, typename... Args> |
| 233 |
shared_ptr<T> make_shared(Args&&... args) { |
| 234 |
return shared_ptr<T>(new T(args...)); |
| 235 |
} |
| 236 |
|
| 237 |
// Test 1: Check that we don't report leaks for shared_ptr in temporary objects |
| 238 |
struct Foo { |
| 239 |
shared_ptr<int> i; |
| 240 |
}; |
| 241 |
|
| 242 |
void add_foo(Foo foo) { |
| 243 |
// The shared_ptr destructor will be called when foo goes out of scope |
| 244 |
} |
| 245 |
|
| 246 |
void test_temporary_object() { |
| 247 |
// No warning should be emitted for this - the memory is managed by shared_ptr |
| 248 |
// in the temporary Foo object, which will properly clean up the memory |
| 249 |
add_foo({make_shared<int>(1)}); |
| 250 |
} |
| 251 |
|
| 252 |
} // namespace shared_ptr_tests |