[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-store=region -verify \
|
[analyzer][MallocChecker][NFC] Document and reorganize some functions
This patch merely reorganizes some things, and features no functional change.
In detail:
* Provided documentation, or moved existing documentation in more obvious
places.
* Added dividers. (the //===----------===// thing).
* Moved getAllocationFamily, printAllocDeallocName, printExpectedAllocName and
printExpectedDeallocName in the global namespace on top of the file where
AllocationFamily is declared, as they are very strongly related.
* Moved isReleased and MallocUpdateRefState near RefState's definition for the
same reason.
* Realloc modeling was very poor in terms of variable and structure naming, as
well as documentation, so I renamed some of them and added much needed docs.
* Moved function IdentifierInfos to a separate struct, and moved isMemFunction,
isCMemFunction adn isStandardNewDelete inside it. This makes the patch affect
quite a lot of lines, should I extract it to a separate one?
* Moved MallocBugVisitor out of MallocChecker.
* Preferred switches to long else-if branches in some places.
* Neatly organized some RUN: lines.
Differential Revision: https://reviews.llvm.org/D54823
llvm-svn: 349281
2018-12-16 02:34:00 +08:00
|
|
|
// RUN: -analyzer-checker=core \
|
|
|
|
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
|
[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
// RUN: -analyzer-checker=alpha.core.CastSize \
|
|
|
|
// RUN: -analyzer-checker=unix.Malloc \
|
|
|
|
// RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true %s
|
|
|
|
|
2012-02-09 07:16:52 +08:00
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
void *malloc(size_t);
|
|
|
|
void free(void *);
|
|
|
|
void *realloc(void *ptr, size_t size);
|
|
|
|
void *calloc(size_t nmemb, size_t size);
|
|
|
|
void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
|
|
|
|
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
|
2012-03-02 06:06:06 +08:00
|
|
|
void my_freeBoth(void *, void *)
|
|
|
|
__attribute((ownership_holds(malloc, 1, 2)));
|
2012-02-09 07:16:52 +08:00
|
|
|
void __attribute((ownership_returns(malloc, 1))) *my_malloc2(size_t);
|
|
|
|
void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
|
|
|
|
|
|
|
|
// Duplicate attributes are silly, but not an error.
|
|
|
|
// Duplicate attribute has no extra effect.
|
|
|
|
// If two are of different kinds, that is an error and reported as such.
|
|
|
|
void __attribute((ownership_holds(malloc, 1)))
|
|
|
|
__attribute((ownership_holds(malloc, 1)))
|
|
|
|
__attribute((ownership_holds(malloc, 3))) my_hold2(void *, void *, void *);
|
|
|
|
void *my_malloc3(size_t);
|
|
|
|
void *myglobalpointer;
|
|
|
|
struct stuff {
|
|
|
|
void *somefield;
|
|
|
|
};
|
|
|
|
struct stuff myglobalstuff;
|
|
|
|
|
|
|
|
void f1() {
|
|
|
|
int *p = malloc(12);
|
2013-04-06 08:41:36 +08:00
|
|
|
return; // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void f2() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
free(p); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void f2_realloc_0() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
realloc(p,0);
|
2012-02-17 06:26:12 +08:00
|
|
|
realloc(p,0); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void f2_realloc_1() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
int *q = realloc(p,0); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// ownership attributes tests
|
|
|
|
void naf1() {
|
|
|
|
int *p = my_malloc3(12);
|
|
|
|
return; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void n2af1() {
|
|
|
|
int *p = my_malloc2(12);
|
2013-04-06 08:41:36 +08:00
|
|
|
return; // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void af1() {
|
|
|
|
int *p = my_malloc(12);
|
2013-04-06 08:41:36 +08:00
|
|
|
return; // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void af1_b() {
|
2012-11-16 03:11:43 +08:00
|
|
|
int *p = my_malloc(12);
|
2013-04-06 08:41:36 +08:00
|
|
|
} // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
|
|
|
|
void af1_c() {
|
|
|
|
myglobalpointer = my_malloc(12); // no-warning
|
|
|
|
}
|
|
|
|
|
2013-03-21 04:35:57 +08:00
|
|
|
void af1_d() {
|
|
|
|
struct stuff mystuff;
|
|
|
|
mystuff.somefield = my_malloc(12);
|
2013-04-06 08:41:36 +08:00
|
|
|
} // expected-warning{{Potential leak of memory pointed to by}}
|
2013-03-21 04:35:57 +08:00
|
|
|
|
2012-02-09 07:16:52 +08:00
|
|
|
// Test that we can pass out allocated memory via pointer-to-pointer.
|
|
|
|
void af1_e(void **pp) {
|
|
|
|
*pp = my_malloc(42); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void af1_f(struct stuff *somestuff) {
|
|
|
|
somestuff->somefield = my_malloc(12); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocating memory for a field via multiple indirections to our arguments is OK.
|
|
|
|
void af1_g(struct stuff **pps) {
|
|
|
|
*pps = my_malloc(sizeof(struct stuff)); // no-warning
|
|
|
|
(*pps)->somefield = my_malloc(42); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void af2() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
my_free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
free(p); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void af2b() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
my_free(p); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void af2c() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
my_hold(p); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void af2d() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
my_hold2(0, 0, p); // expected-warning{{Attempt to free released memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// No leak if malloc returns null.
|
|
|
|
void af2e() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
if (!p)
|
|
|
|
return; // no-warning
|
|
|
|
free(p); // no-warning
|
|
|
|
}
|
|
|
|
|
2012-06-21 04:57:46 +08:00
|
|
|
// This case inflicts a possible double-free.
|
2012-02-09 07:16:52 +08:00
|
|
|
void af3() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
my_hold(p);
|
2012-06-22 10:04:31 +08:00
|
|
|
free(p); // expected-warning{{Attempt to free non-owned memory}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int * af4() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
my_free(p);
|
2012-02-17 06:26:12 +08:00
|
|
|
return p; // expected-warning{{Use of memory after it is freed}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This case is (possibly) ok, be conservative
|
|
|
|
int * af5() {
|
|
|
|
int *p = my_malloc(12);
|
|
|
|
my_hold(p);
|
|
|
|
return p; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This case tests that storing malloc'ed memory to a static variable which is
|
|
|
|
// then returned is not leaked. In the absence of known contracts for functions
|
|
|
|
// or inter-procedural analysis, this is a conservative answer.
|
|
|
|
int *f3() {
|
|
|
|
static int *p = 0;
|
|
|
|
p = malloc(12);
|
|
|
|
return p; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// This case tests that storing malloc'ed memory to a static global variable
|
|
|
|
// which is then returned is not leaked. In the absence of known contracts for
|
|
|
|
// functions or inter-procedural analysis, this is a conservative answer.
|
|
|
|
static int *p_f4 = 0;
|
|
|
|
int *f4() {
|
|
|
|
p_f4 = malloc(12);
|
|
|
|
return p_f4; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
int *f5() {
|
|
|
|
int *q = malloc(12);
|
|
|
|
q = realloc(q, 20);
|
|
|
|
return q; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void f6() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
if (!p)
|
|
|
|
return; // no-warning
|
|
|
|
else
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f6_realloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
if (!p)
|
|
|
|
return; // no-warning
|
|
|
|
else
|
|
|
|
realloc(p,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *doit2();
|
|
|
|
void pr6069() {
|
|
|
|
char *buf = doit2();
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pr6293() {
|
|
|
|
free(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f7() {
|
|
|
|
char *x = (char*) malloc(4);
|
|
|
|
free(x);
|
2012-02-17 06:26:12 +08:00
|
|
|
x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void f7_realloc() {
|
|
|
|
char *x = (char*) malloc(4);
|
|
|
|
realloc(x,0);
|
2012-02-17 06:26:12 +08:00
|
|
|
x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PR6123() {
|
Allow multiple PathDiagnosticConsumers to be used with a BugReporter at the same time.
This fixes several issues:
- removes egregious hack where PlistDiagnosticConsumer would forward to HTMLDiagnosticConsumer,
but diagnostics wouldn't be generated consistently in the same way if PlistDiagnosticConsumer
was used by itself.
- emitting diagnostics to the terminal (using clang's diagnostic machinery) is no longer a special
case, just another PathDiagnosticConsumer. This also magically resolved some duplicate warnings,
as we now use PathDiagnosticConsumer's diagnostic pruning, which has scope for the entire translation
unit, not just the scope of a BugReporter (which is limited to a particular ExprEngine).
As an interesting side-effect, diagnostics emitted to the terminal also have their trailing "." stripped,
just like with diagnostics emitted to plists and HTML. This required some tests to be updated, but now
the tests have higher fidelity with what users will see.
There are some inefficiencies in this patch. We currently generate the report graph (from the ExplodedGraph)
once per PathDiagnosticConsumer, which is a bit wasteful, but that could be pulled up higher in the
logic stack. There is some intended duplication, however, as we now generate different PathDiagnostics (for the same issue)
for different PathDiagnosticConsumers. This is necessary to produce the diagnostics that a particular
consumer expects.
llvm-svn: 162028
2012-08-17 01:45:23 +08:00
|
|
|
int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PR7217() {
|
Allow multiple PathDiagnosticConsumers to be used with a BugReporter at the same time.
This fixes several issues:
- removes egregious hack where PlistDiagnosticConsumer would forward to HTMLDiagnosticConsumer,
but diagnostics wouldn't be generated consistently in the same way if PlistDiagnosticConsumer
was used by itself.
- emitting diagnostics to the terminal (using clang's diagnostic machinery) is no longer a special
case, just another PathDiagnosticConsumer. This also magically resolved some duplicate warnings,
as we now use PathDiagnosticConsumer's diagnostic pruning, which has scope for the entire translation
unit, not just the scope of a BugReporter (which is limited to a particular ExprEngine).
As an interesting side-effect, diagnostics emitted to the terminal also have their trailing "." stripped,
just like with diagnostics emitted to plists and HTML. This required some tests to be updated, but now
the tests have higher fidelity with what users will see.
There are some inefficiencies in this patch. We currently generate the report graph (from the ExplodedGraph)
once per PathDiagnosticConsumer, which is a bit wasteful, but that could be pulled up higher in the
logic stack. There is some intended duplication, however, as we now generate different PathDiagnostics (for the same issue)
for different PathDiagnosticConsumers. This is necessary to produce the diagnostics that a particular
consumer expects.
llvm-svn: 162028
2012-08-17 01:45:23 +08:00
|
|
|
int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
|
2012-02-09 07:16:52 +08:00
|
|
|
buf[1] = 'c'; // not crash
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocCastToVoid() {
|
|
|
|
void *p = malloc(2);
|
|
|
|
const void *cp = p; // not crash
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocCastToFP() {
|
|
|
|
void *p = malloc(2);
|
|
|
|
void (*fp)() = p; // not crash
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests that malloc() buffers are undefined by default
|
|
|
|
char mallocGarbage () {
|
|
|
|
char *buf = malloc(2);
|
|
|
|
char result = buf[1]; // expected-warning{{undefined}}
|
|
|
|
free(buf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests that calloc() buffers need to be freed
|
|
|
|
void callocNoFree () {
|
|
|
|
char *buf = calloc(2,2);
|
2013-04-06 08:41:36 +08:00
|
|
|
return; // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// These test that calloc() buffers are zeroed by default
|
|
|
|
char callocZeroesGood () {
|
|
|
|
char *buf = calloc(2,2);
|
|
|
|
char result = buf[3]; // no-warning
|
|
|
|
if (buf[1] == 0) {
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
return result; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
char callocZeroesBad () {
|
|
|
|
char *buf = calloc(2,2);
|
|
|
|
char result = buf[3]; // no-warning
|
|
|
|
if (buf[1] != 0) {
|
|
|
|
free(buf); // expected-warning{{never executed}}
|
|
|
|
}
|
2013-04-06 08:41:36 +08:00
|
|
|
return result; // expected-warning{{Potential leak of memory pointed to by}}
|
2012-02-09 07:16:52 +08:00
|
|
|
}
|
2012-03-02 06:06:06 +08:00
|
|
|
|
|
|
|
void testMultipleFreeAnnotations() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
int *q = malloc(12);
|
|
|
|
my_freeBoth(p, q);
|
|
|
|
}
|
|
|
|
|