Files
clang-p2996/clang/test/Analysis/unions.cpp
Artem Dergachev bbc6d68297 [analyzer] Fix the "Zombie Symbols" bug.
It's an old bug that consists in stale references to symbols remaining in the
GDM if they disappear from other program state sections as a result of any
operation that isn't the actual dead symbol collection. The most common example
here is:

   FILE *fp = fopen("myfile.txt", "w");
   fp = 0; // leak of file descriptor

In this example the leak were not detected previously because the symbol
disappears from the public part of the program state due to evaluating
the assignment. For that reason the checker never receives a notification
that the symbol is dead, and never reports a leak.

This patch not only causes leak false negatives, but also a number of other
problems, including false positives on some checkers.

What's worse, even though the program state contains a finite number of symbols,
the set of symbols that dies is potentially infinite. This means that is
impossible to compute the set of all dead symbols to pass off to the checkers
for cleaning up their part of the GDM.

No longer compute the dead set at all. Disallow iterating over dead symbols.
Disallow querying if any symbols are dead. Remove the API for marking symbols
as dead, as it is no longer necessary. Update checkers accordingly.

Differential Revision: https://reviews.llvm.org/D18860

llvm-svn: 347953
2018-11-30 03:27:50 +00:00

124 lines
2.8 KiB
C++

// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,debug.ExprInspection %s -analyzer-config eagerly-assume=false -verify
extern void clang_analyzer_eval(bool);
extern "C" char *strdup(const char *s);
namespace PR14054_reduced {
struct Definition;
struct ParseNode {
union {
Definition *lexdef;
ParseNode *data;
} pn_u;
};
struct Definition : public ParseNode { };
void CloneParseTree(ParseNode *opn, ParseNode *pn, ParseNode *x) {
// This used to cause an assertion failure because:
// 1. The implicit operator= for unions assigns all members of the union,
// not just the active one (b/c there's no way to know which is active).
// 2. RegionStore dutifully stored all the variants at the same offset;
// the last one won.
// 3. We asked for the value of the first variant but got back a conjured
// symbol for the second variant.
// 4. We ended up trying to add a base cast to a region of the wrong type.
//
// Now (at the time this test was added), we instead treat all variants of
// a union as different offsets, but only allow one to be active at a time.
*pn = *opn;
x = pn->pn_u.lexdef->pn_u.lexdef;
}
}
namespace PR14054_original {
struct Definition;
struct ParseNode {
union {
struct {
union {};
Definition *lexdef;
} name;
class {
int *target;
ParseNode *data;
} xmlpi;
} pn_u;
};
struct Definition : public ParseNode { };
void CloneParseTree(ParseNode *opn, ParseNode *pn, ParseNode *x) {
pn->pn_u = opn->pn_u;
x = pn->pn_u.name.lexdef->pn_u.name.lexdef;
}
}
namespace PR17596 {
union IntOrString {
int i;
char *s;
};
extern void process(IntOrString);
void test() {
IntOrString uu;
uu.s = strdup("");
process(uu);
}
void testPositive() {
IntOrString uu;
uu.s = strdup("");
} // expected-warning{{leak}}
void testCopy() {
IntOrString uu;
uu.i = 4;
clang_analyzer_eval(uu.i == 4); // expected-warning{{TRUE}}
IntOrString vv;
vv.i = 5;
uu = vv;
clang_analyzer_eval(uu.i == 5); // expected-warning{{TRUE}}
}
void testInvalidation() {
IntOrString uu;
uu.s = strdup("");
IntOrString vv;
char str[] = "abc";
vv.s = str;
uu = vv;
} // expected-warning{{leak}}
void testIndirectInvalidation() {
IntOrString uu;
char str[] = "abc";
uu.s = str;
clang_analyzer_eval(uu.s[0] == 'a'); // expected-warning{{TRUE}}
process(uu);
clang_analyzer_eval(uu.s[0] == 'a'); // expected-warning{{UNKNOWN}}
}
}
namespace assume_union_contents {
union U {
int x;
};
U get();
void test() {
U u = get();
int y = 0;
if (u.x)
y = 1;
if (u.x)
y = 1 / y; // no-warning
}
} // end namespace assume_union_contents