2018-08-30 04:29:17 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -std=c++11 -verify -analyzer-config eagerly-assume=false %s
|
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -std=c++11 -DTEST_INLINABLE_ALLOCATORS -verify -analyzer-config eagerly-assume=false %s
|
2013-03-25 09:35:45 +08:00
|
|
|
#include "Inputs/system-header-simulator-cxx.h"
|
2010-04-20 13:48:57 +08:00
|
|
|
|
2012-06-20 09:32:01 +08:00
|
|
|
void clang_analyzer_eval(bool);
|
|
|
|
|
2012-07-17 07:38:09 +08:00
|
|
|
typedef __typeof__(sizeof(int)) size_t;
|
2012-06-20 09:32:01 +08:00
|
|
|
extern "C" void *malloc(size_t);
|
2013-03-25 09:35:45 +08:00
|
|
|
extern "C" void free(void *);
|
2012-06-20 09:32:01 +08:00
|
|
|
|
2012-06-26 04:48:28 +08:00
|
|
|
int someGlobal;
|
2013-04-11 06:21:41 +08:00
|
|
|
|
|
|
|
class SomeClass {
|
|
|
|
public:
|
|
|
|
void f(int *p);
|
|
|
|
};
|
|
|
|
|
2012-06-26 04:48:28 +08:00
|
|
|
void testImplicitlyDeclaredGlobalNew() {
|
|
|
|
if (someGlobal != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// This used to crash because the global operator new is being implicitly
|
|
|
|
// declared and it does not have a valid source location. (PR13090)
|
|
|
|
void *x = ::operator new(0);
|
|
|
|
::operator delete(x);
|
|
|
|
|
|
|
|
// Check that the new/delete did not invalidate someGlobal;
|
|
|
|
clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2012-06-20 09:32:01 +08:00
|
|
|
void *testPlacementNew() {
|
|
|
|
int *x = (int *)malloc(sizeof(int));
|
|
|
|
*x = 1;
|
|
|
|
clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
|
|
|
|
|
|
|
|
void *y = new (x) int;
|
|
|
|
clang_analyzer_eval(x == y); // expected-warning{{TRUE}};
|
2018-01-25 04:59:40 +08:00
|
|
|
clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
|
2012-06-20 09:32:01 +08:00
|
|
|
|
|
|
|
return y;
|
2010-04-21 10:22:25 +08:00
|
|
|
}
|
2010-04-20 13:48:57 +08:00
|
|
|
|
2012-06-20 09:32:01 +08:00
|
|
|
void *operator new(size_t, size_t, int *);
|
|
|
|
void *testCustomNew() {
|
|
|
|
int x[1] = {1};
|
|
|
|
clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
|
|
|
|
|
|
|
|
void *y = new (0, x) int;
|
|
|
|
clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
|
|
|
|
|
|
|
|
return y; // no-warning
|
2010-04-20 13:48:57 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 06:21:47 +08:00
|
|
|
void *operator new(size_t, void *, void *);
|
|
|
|
void *testCustomNewMalloc() {
|
|
|
|
int *x = (int *)malloc(sizeof(int));
|
|
|
|
|
|
|
|
// Should be no-warning (the custom allocator could have freed x).
|
|
|
|
void *y = new (0, x) int; // no-warning
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2012-07-17 07:38:09 +08:00
|
|
|
void testScalarInitialization() {
|
|
|
|
int *n = new int(3);
|
|
|
|
clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
new (n) int();
|
|
|
|
clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
new (n) int{3};
|
|
|
|
clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
new (n) int{};
|
|
|
|
clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2012-08-28 01:50:07 +08:00
|
|
|
struct PtrWrapper {
|
|
|
|
int *x;
|
|
|
|
|
|
|
|
PtrWrapper(int *input) : x(input) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
PtrWrapper *testNewInvalidation() {
|
|
|
|
// Ensure that we don't consider this a leak.
|
2013-03-28 02:10:35 +08:00
|
|
|
return new PtrWrapper(static_cast<int *>(malloc(4))); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNewInvalidationPlacement(PtrWrapper *w) {
|
|
|
|
// Ensure that we don't consider this a leak.
|
|
|
|
new (w) PtrWrapper(static_cast<int *>(malloc(4))); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
int **testNewInvalidationScalar() {
|
|
|
|
// Ensure that we don't consider this a leak.
|
|
|
|
return new (int *)(static_cast<int *>(malloc(4))); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNewInvalidationScalarPlacement(int **p) {
|
|
|
|
// Ensure that we don't consider this a leak.
|
|
|
|
new (p) (int *)(static_cast<int *>(malloc(4))); // no-warning
|
2012-08-28 01:50:07 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 09:31:42 +08:00
|
|
|
void testCacheOut(PtrWrapper w) {
|
|
|
|
extern bool coin();
|
|
|
|
if (coin())
|
|
|
|
w.x = 0;
|
|
|
|
new (&w.x) (int*)(0); // we cache out here; don't crash
|
|
|
|
}
|
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
void testUseAfter(int *p) {
|
|
|
|
SomeClass *c = new SomeClass;
|
|
|
|
free(p);
|
|
|
|
c->f(p); // expected-warning{{Use of memory after it is freed}}
|
|
|
|
delete c;
|
|
|
|
}
|
2013-03-30 09:31:42 +08:00
|
|
|
|
2013-03-25 09:35:45 +08:00
|
|
|
// new/delete oparators are subjects of cplusplus.NewDelete.
|
|
|
|
void testNewDeleteNoWarn() {
|
|
|
|
int i;
|
|
|
|
delete &i; // no-warning
|
|
|
|
|
|
|
|
int *p1 = new int;
|
|
|
|
delete ++p1; // no-warning
|
|
|
|
|
|
|
|
int *p2 = new int;
|
|
|
|
delete p2;
|
|
|
|
delete p2; // no-warning
|
|
|
|
|
|
|
|
int *p3 = new int; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void testDeleteMallocked() {
|
|
|
|
int *x = (int *)malloc(sizeof(int));
|
[analyzer][MallocChecker] Make NewDeleteLeaks depend on DynamicMemoryModeling rather than NewDelete
If you remember the mail [1] I sent out about how I envision the future of the
already existing checkers to look dependencywise, one my main points was that no
checker that emits diagnostics should be a dependency. This is more problematic
for some checkers (ahem, RetainCount [2]) more than for others, like this one.
The MallocChecker family is a mostly big monolithic modeling class some small
reporting checkers that only come to action when we are constructing a warning
message, after the actual bug was detected. The implication of this is that
NewDeleteChecker doesn't really do anything to depend on, so this change was
relatively simple.
The only thing that complicates this change is that FreeMemAux (MallocCheckers
method that models general memory deallocation) returns after calling a bug
reporting method, regardless whether the report was ever emitted (which may not
always happen, for instance, if the checker responsible for the report isn't
enabled). This return unfortunately happens before cleaning up the maps in the
GDM keeping track of the state of symbols (whether they are released, whether
that release was successful, etc). What this means is that upon disabling some
checkers, we would never clean up the map and that could've lead to false
positives, e.g.:
error: 'warning' diagnostics seen but not expected:
File clang/test/Analysis/NewDelete-intersections.mm Line 66: Potential leak of memory pointed to by 'p'
File clang/test/Analysis/NewDelete-intersections.mm Line 73: Potential leak of memory pointed to by 'p'
File clang/test/Analysis/NewDelete-intersections.mm Line 77: Potential leak of memory pointed to by 'p'
error: 'warning' diagnostics seen but not expected:
File clang/test/Analysis/NewDelete-checker-test.cpp Line 111: Undefined or garbage value returned to caller
File clang/test/Analysis/NewDelete-checker-test.cpp Line 200: Potential leak of memory pointed to by 'p'
error: 'warning' diagnostics seen but not expected:
File clang/test/Analysis/new.cpp Line 137: Potential leak of memory pointed to by 'x'
There two possible approaches I had in mind:
Make bug reporting methods of MallocChecker returns whether they succeeded, and
proceed with the rest of FreeMemAux if not,
Halt execution with a sink node upon failure. I decided to go with this, as
described in the code.
As you can see from the removed/changed test files, before the big checker
dependency effort landed, there were tests to check for all the weird
configurations of enabled/disabled checkers and their messy interactions, I
largely repurposed these.
[1] http://lists.llvm.org/pipermail/cfe-dev/2019-August/063070.html
[2] http://lists.llvm.org/pipermail/cfe-dev/2019-August/063205.html
Differential Revision: https://reviews.llvm.org/D77474
2020-02-26 01:02:18 +08:00
|
|
|
// unix.MismatchedDeallocator would catch this, but we're not testing it here.
|
|
|
|
delete x;
|
|
|
|
}
|
2013-03-25 09:35:45 +08:00
|
|
|
|
|
|
|
void testDeleteOpAfterFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
operator delete(p); // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testDeleteAfterFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
delete p; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testStandardPlacementNewAfterFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
p = new(p) int; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCustomPlacementNewAfterFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
p = new(0, p) int; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
2012-08-28 01:50:07 +08:00
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
void testUsingThisAfterDelete() {
|
|
|
|
SomeClass *c = new SomeClass;
|
|
|
|
delete c;
|
|
|
|
c->f(0); // no-warning
|
|
|
|
}
|
|
|
|
|
2013-07-11 03:14:10 +08:00
|
|
|
void testAggregateNew() {
|
|
|
|
struct Point { int x, y; };
|
|
|
|
new Point{1, 2}; // no crash
|
|
|
|
|
|
|
|
Point p;
|
|
|
|
new (&p) Point{1, 2}; // no crash
|
|
|
|
clang_analyzer_eval(p.x == 1); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(p.y == 2); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2012-06-20 13:34:32 +08:00
|
|
|
//--------------------------------
|
|
|
|
// Incorrectly-modelled behavior
|
|
|
|
//--------------------------------
|
|
|
|
|
2012-07-17 07:38:09 +08:00
|
|
|
int testNoInitialization() {
|
2012-06-20 13:34:32 +08:00
|
|
|
int *n = new int;
|
|
|
|
|
|
|
|
// Should warn that *n is uninitialized.
|
|
|
|
if (*n) { // no-warning
|
2013-03-25 09:35:45 +08:00
|
|
|
delete n;
|
2012-07-17 07:38:09 +08:00
|
|
|
return 0;
|
2012-06-20 13:34:32 +08:00
|
|
|
}
|
2013-03-25 09:35:45 +08:00
|
|
|
delete n;
|
2012-07-17 07:38:09 +08:00
|
|
|
return 1;
|
2012-06-20 13:34:32 +08:00
|
|
|
}
|
|
|
|
|
2012-07-17 07:38:09 +08:00
|
|
|
int testNoInitializationPlacement() {
|
|
|
|
int n;
|
|
|
|
new (&n) int;
|
2012-06-20 13:34:32 +08:00
|
|
|
|
2018-01-25 04:59:40 +08:00
|
|
|
if (n) { // expected-warning{{Branch condition evaluates to a garbage value}}
|
2012-07-17 07:38:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2012-06-20 13:34:32 +08:00
|
|
|
}
|
2013-09-26 00:06:17 +08:00
|
|
|
|
|
|
|
// Test modelling destructor call on call to delete
|
|
|
|
class IntPair{
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
IntPair() {};
|
|
|
|
~IntPair() {x = x/y;}; //expected-warning {{Division by zero}}
|
|
|
|
};
|
|
|
|
|
|
|
|
void testCallToDestructor() {
|
|
|
|
IntPair *b = new IntPair();
|
|
|
|
b->x = 1;
|
|
|
|
b->y = 0;
|
|
|
|
delete b; // This results in divide by zero in destructor
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Deleting a value that's passed as an argument.
|
|
|
|
class DerefClass{
|
|
|
|
public:
|
|
|
|
int *x;
|
|
|
|
DerefClass() {};
|
|
|
|
~DerefClass() {*x = 1;}; //expected-warning {{Dereference of null pointer (loaded from field 'x')}}
|
|
|
|
};
|
|
|
|
|
|
|
|
void testDestCall(DerefClass *arg) {
|
|
|
|
delete arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_delete_dtor_Arg() {
|
|
|
|
DerefClass *pair = new DerefClass();
|
|
|
|
pair->x = 0;
|
|
|
|
testDestCall(pair);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Deleting the address of a local variable, null pointer
|
|
|
|
void abort(void) __attribute__((noreturn));
|
|
|
|
|
|
|
|
class NoReturnDtor {
|
|
|
|
public:
|
|
|
|
NoReturnDtor() {}
|
|
|
|
~NoReturnDtor() {abort();}
|
|
|
|
};
|
|
|
|
|
|
|
|
void test_delete_dtor_LocalVar() {
|
|
|
|
NoReturnDtor test;
|
|
|
|
delete &test; // no warn or crash
|
|
|
|
}
|
|
|
|
|
|
|
|
class DerivedNoReturn:public NoReturnDtor {
|
|
|
|
public:
|
|
|
|
DerivedNoReturn() {};
|
|
|
|
~DerivedNoReturn() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
void testNullDtorDerived() {
|
|
|
|
DerivedNoReturn *p = new DerivedNoReturn();
|
|
|
|
delete p; // Calls the base destructor which aborts, checked below
|
|
|
|
clang_analyzer_eval(true); // no warn
|
|
|
|
}
|
|
|
|
|
2013-12-05 12:47:09 +08:00
|
|
|
//Deleting a non-class pointer should not crash/warn
|
2013-09-26 00:06:17 +08:00
|
|
|
void test_var_delete() {
|
|
|
|
int *v = new int;
|
|
|
|
delete v; // no crash/warn
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2018-04-27 10:16:03 +08:00
|
|
|
void test_array_delete() {
|
|
|
|
class C {
|
|
|
|
public:
|
|
|
|
~C() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto c1 = new C[2][3];
|
|
|
|
delete[] c1; // no-crash // no-warning
|
|
|
|
|
|
|
|
C c2[4];
|
|
|
|
// FIXME: Should warn.
|
|
|
|
delete[] &c2; // no-crash
|
|
|
|
|
|
|
|
C c3[7][6];
|
|
|
|
// FIXME: Should warn.
|
|
|
|
delete[] &c3; // no-crash
|
|
|
|
}
|
|
|
|
|
2013-09-26 00:06:17 +08:00
|
|
|
void testDeleteNull() {
|
|
|
|
NoReturnDtor *foo = 0;
|
|
|
|
delete foo; // should not call destructor, checked below
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNullAssigneddtor() {
|
|
|
|
NoReturnDtor *p = 0;
|
|
|
|
NoReturnDtor *s = p;
|
|
|
|
delete s; // should not call destructor, checked below
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteArg(NoReturnDtor *test) {
|
|
|
|
delete test;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNulldtorArg() {
|
|
|
|
NoReturnDtor *p = 0;
|
|
|
|
deleteArg(p);
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testDeleteUnknown(NoReturnDtor *foo) {
|
|
|
|
delete foo; // should assume non-null and call noreturn destructor
|
|
|
|
clang_analyzer_eval(true); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void testArrayNull() {
|
|
|
|
NoReturnDtor *fooArray = 0;
|
|
|
|
delete[] fooArray; // should not call destructor, checked below
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testArrayDestr() {
|
|
|
|
NoReturnDtor *p = new NoReturnDtor[2];
|
|
|
|
delete[] p; // Calls the base destructor which aborts, checked below
|
2018-02-15 10:51:58 +08:00
|
|
|
//TODO: clang_analyzer_eval should not be called
|
|
|
|
clang_analyzer_eval(true); // expected-warning{{TRUE}}
|
2013-09-26 00:06:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate Region even in case of default destructor
|
|
|
|
class InvalidateDestTest {
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int *y;
|
|
|
|
~InvalidateDestTest();
|
|
|
|
};
|
|
|
|
|
|
|
|
int test_member_invalidation() {
|
|
|
|
|
|
|
|
//test invalidation of member variable
|
|
|
|
InvalidateDestTest *test = new InvalidateDestTest();
|
|
|
|
test->x = 5;
|
|
|
|
int *k = &(test->x);
|
|
|
|
clang_analyzer_eval(*k == 5); // expected-warning{{TRUE}}
|
|
|
|
delete test;
|
|
|
|
clang_analyzer_eval(*k == 5); // expected-warning{{UNKNOWN}}
|
|
|
|
|
|
|
|
//test invalidation of member pointer
|
|
|
|
int localVar = 5;
|
|
|
|
test = new InvalidateDestTest();
|
|
|
|
test->y = &localVar;
|
|
|
|
delete test;
|
|
|
|
clang_analyzer_eval(localVar == 5); // expected-warning{{UNKNOWN}}
|
|
|
|
|
|
|
|
// Test aray elements are invalidated.
|
|
|
|
int Var1 = 5;
|
|
|
|
int Var2 = 5;
|
|
|
|
InvalidateDestTest *a = new InvalidateDestTest[2];
|
|
|
|
a[0].y = &Var1;
|
|
|
|
a[1].y = &Var2;
|
|
|
|
delete[] a;
|
|
|
|
clang_analyzer_eval(Var1 == 5); // expected-warning{{UNKNOWN}}
|
|
|
|
clang_analyzer_eval(Var2 == 5); // expected-warning{{UNKNOWN}}
|
|
|
|
return 0;
|
|
|
|
}
|