[analyzer] Fails to load from a bitfield that was covered by a type-punned write before
The commit causing false positives: Minimal reproducer:
// Minimal reproducer for core.uninitialized.Assign false positive
// introduced by LLVM commit a6a34333a113 (present in main).
//
// main: reports "Assigned value is uninitialized" on *out = s.val
// Reverted: no finding (correct)
//
// Reproduce:
// clang --analyze -Xanalyzer -analyzer-checker=core single.c
typedef struct {
unsigned pad : 4;
unsigned val : 4;
} S1;
unsigned* extern_func(unsigned*);
int callee_func(unsigned* out)
{
unsigned v;
unsigned* p = extern_func(&v);
if (p != &v)
return 1;
*out = *p;
return 0;
}
void caller_func(unsigned char* out)
{
S1 s = {0};
if (callee_func((unsigned*)&s) == 0)
*out = s.val;
}
@llvm/issue-subscribers-clang-static-analyzer
Author: earnol
// Minimal reproducer for core.uninitialized.Assign false positive
// introduced by LLVM commit a6a34333a113 (present in main).
//
// main: reports "Assigned value is uninitialized" on *out = s.val
// Reverted: no finding (correct)
//
// Reproduce:
// clang --analyze -Xanalyzer -analyzer-checker=core single.c
typedef struct {
unsigned pad : 4;
unsigned val : 4;
} S1;
unsigned* extern_func(unsigned*);
int callee_func(unsigned* out)
{
unsigned v;
unsigned* p = extern_func(&v);
if (p != &v)
return 1;
*out = *p;
return 0;
}
void caller_func(unsigned char* out)
{
S1 s = {0};
if (callee_func((unsigned*)&s) == 0)
*out = s.val;
}
At first glance, the blamed commit may surfaced this bug but it has nothing to do with this FP. To me, I think the engine simply couldn't follow the reinterpretcast and the subsequent store to a typepunned bitfield.
I strongly suspect this issue is about the Store and bitfields and reinterpretcats (btw its also UB of course).
I'll have a deeper look tomorrow. Btw thank you for the swift report and for the carefully reduced reproducer. It is really valuable and actionable!
At first glance, the blamed commit may surfaced this bug but it has nothing to do with this FP.
You are correct. This commit is totally correct. This commit just exposed pre-existing latent bug in clang/lib/StaticAnalyzer/Core/RegionStore.cpp file. I already have tentative fix for it which is currently being tested.
This example can be reduced into: https://godbolt.org/z/EKrbE9oKP
struct S1 {
unsigned first : 4;
unsigned second : 4;
unsigned unused_trailing_bits : 24;
};
static_assert(sizeof(unsigned) == 4);
static_assert(sizeof(S1) == 4);
void clang_analyzer_printState();
int top() {
S1 s{};
*((unsigned*)&s) = 8; // type-punning
clang_analyzer_printState();
// s: direct at offset 0 of value 8 U32b
return s.second; // FP: uninitialized read
}
So the printState demonstrates that the store actually goes through - which is correct.
So the problem is with reading from the bitfield, because it should realize that there is a conflict that the value at bit offset 0 of 8 U32b overlaps with the bit offset of second, thus partially clobbers it.
What it probably does is that it looks for a direct binding at the desired bit offset (which is 4 because that's the bit where the second bitfield starts), and it should also see that it only wants to load 4 bits (because that is the bitwidth of the bitfield second, however, because there is no direct binding at that offset, and the structure also didn't have a default binding it fails. Because the variable is on the stack, it implies that it's uninitialized, so it returns UndefinedVal.
This is of course wrong. It should have realized that the direct binding we had partially covered the requested bits, and return UnknownVal instead.
TBH given the current Store implementation, it's not so easy to answer the question: do we have any bindings covering the bits [begin,end]? And without this primitive, the fix can't be implemented in a clean way.
This points me to my earlier post where an alternative Store model was proposed alongside a PoC implementation that would/should cover this bitfield case efficiently. /cc @NagyDonat
This example can be reduced into: https://godbolt.org/z/EKrbE9oKP
Later yesterday i got even more compact reproducer:
typedef struct {
unsigned pad : 4;
unsigned val : 4;
} S1;
void callee_func(unsigned* out)
{
*out = 42;
}
void caller_func(unsigned char* out)
{
S1 s = {0};
callee_func((unsigned*)&s);
*out = s.val; // false positive: "Assigned value is uninitialized"
}
@earnol or anybody else Do you understand how is this issue related to my commit Don't rule out symbolic pointer pointing to stack which made it appear?
Are these more recent simplified reproducers still affected by the reverting of that commit?
(I don't see a logical connection between my changes and these symptoms 🤔 which of course doesn't mean that there is no connection.)
(I don't see a logical connection between my changes and these symptoms 🤔 which of course doesn't mean that there is no connection.)
I don't think there is. It's incidental.
@NagyDonat Well. The connection is actually direct. Your change exposed existing latent bug in Region store. The problem is in your commit you removed check for the stack region. This check you removed was incorrect because the same memory can belonging Stack space region. But it caused the situation i fixed in my change to manifest.
@steakhal This what my fix does. However it does not do it on read, but on each and every write. I believe it allows solving the current problem without such a radical solution you proposing. My solution is based on careful analysis of bit ranges affected and it's working.
PS: And i agree my solution is imperfect and contains bug, but i was able to track those down and will address those in a new version.
EDIT: New version is available for the review.