False path due to 'constant << symbol value' not being modeled accurately
| Bugzilla Link | 8006 |
| Version | trunk |
| OS | All |
| Reporter | LLVM Bugzilla Contributor |
| CC | @belkadan,@tkremenek,@xuzhongxing |
Extended Description
$ cat test.c
void bar( int x ) { }
void foo( int n )
{
int x[4096], i;
for ( i = 0; i < (1 << n); i++ )
{
x[i] = i;
}
for ( i = 0; i < (1 << n); i++ )
{
bar( x[i] );
}
}
$ clang test.c --analyze
test.c:15:3: warning: Pass-by-value argument in function call is undefined
bar( x[i] );
^ ~~~~
According to Ted Kremenek, the problem is that 1 << n is not getting accurately modeled, resulting in a false path that triggers the warning.
According to Ted Kremenek, the problem is that '1 << n' is not getting accurately modeled, resulting in a false path that triggers the warning.
More specifically, 'n' is a symbolic value. '1 << n' isn't being modeled correctly by the constraint manager.
assigned to @tkremenek
Bisected: the reported behavior stops reproducing since https://github.com/llvm/llvm-project/commit/e254b0f8c756
Godbolt: https://godbolt.org/z/Y3KPdPsGW
- Clang 6.0.0: Reports an uninitialized argument at
bar( x[i] );on line 14 - Clang 7.0.0: No longer reports an uninitialized argument at
bar( x[i] ); - Clang (trunk): Reports no analyzer warning at
bar( x[i] );
This is only an observed change in behavior. I have not checked whether the change in this range addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.
Prepared with assistance from Codex.
I think this case works now. I played around it and I think it's in a good shape. Initially I was surprised by a constraint, but thinking about it makes sense. /cc @benedekaibas - he might be interested in my thinking. https://godbolt.org/z/9x9Ye9rWd
void bar(int x);
void clang_analyzer_dump(int);
void clang_analyzer_value(int);
void foo(int n) {
int x[4096];
clang_analyzer_dump(1 << n); // 1 << $n
clang_analyzer_value(1 << n); // 32s:{ [-2147483648, 2147483647] }
for (int i = 0; i < (1 << n); i++) { // the loop unrolling materializes specific 'n's.
x[i] = i; // we write each index value.
}
clang_analyzer_dump(1 << n); // 1 << $n
clang_analyzer_value(1 << n); // we see: 1, 2 and surprisingly: 32s:{ [-2147483648, 0] }
for (int i = 0; i < (1 << n); i++) {
clang_analyzer_value(x[i]); // 1, 2: we read back what we have written - good.
bar(x[i]);
}
}
Basically, I was surprised by the 32s:{ [-2147483648, 0] } range (constraint) of 1 << n on one execution branch, especially in contrast to the full INT range of the same dump before the first loop.
However, it's actually what it should be because before the first loop we don't know anything about N, thus also don't know anything about 1 << N. Technically, we could deduce/infer a better (tighter) range on the expression 1 << N but overapproximating it to INT min-max is also fine - this is what happens here.
Then the loop models a couple of iterations, well, exactly {0, 1, 2}. Each exact choice brings in the loop condition assumption, and because we only have constants and N, then the constraints propagate (simplify) for N. - think of reorganizing the equasions of assume(/*i=*/0 < (1 << N), /*to=*/true) etc.
Each will have a separate execution path.
So the second clang_analyzer_value is hit on 3 paths with exactly the right loop constraints.
Then we hit the next loop, and we know exactly how many times loop, thus we hit the "loop-unrolling" mode - and never split because we always know where we need to leave the loop...
@steakhal Thank you for mentioning! I have looked through the example and the relevant parts in the analyzer that are needed to understand this situation. This was very interesting. Your explanation makes sense to me, so I have nothing to add to it. Very interesting case though!