Static analyzer incorrectly models a synthesized assignment operator
| Bugzilla Link | 16745 |
| Resolution | FIXED |
| Resolved on | Nov 06, 2016 13:16 |
| Version | trunk |
| OS | Linux |
| CC | @belkadan |
Extended Description
$ cat small.cc struct A { A& operator=(A&); };
struct B { int a; A b[1]; };
void fn1() { B v, u; u = v; }
$ clang -std=c++11 --analyze small.cc -Xanalyzer -analyzer-output=text small.cc:5:8: warning: Array subscript is undefined struct B { ^ small.cc:12:3: note: Calling implicit copy assignment operator for 'B' u = v; ^ small.cc:5:8: note: Loop condition is true. Entering loop body struct B { ^ small.cc:5:8: note: Uninitialized value stored to '__i0' small.cc:5:8: note: Array subscript is undefined 1 warning generated.
I haven't looked into how the synthesized function looks like, but obviously it uses a loop to copy the b sub-array and somehow the loop variable ends up undefined.
This false positive no longer manifests and it looks like Sema was indeed changed to create distinct DeclRefExprs for __i0. I'm going to mark this as RESOLVED.
Here is an AST dump from ToT:
| -CXXMethodDecl 0x7ffd2d869bd0 <col:8> col:8 implicit used operator= 'struct B &(struct B &) noexcept(false)' inline | |-ParmVarDecl 0x7ffd2d869d00 <col:8> col:8 used 'struct B &' | -CompoundStmt 0x7ffd2d86a378 col:8
| |-BinaryOperator 0x7ffd2d869ee0 col:8 'int' lvalue '='
| | |-MemberExpr 0x7ffd2d869e30 col:8 'int' lvalue ->a 0x7ffd2d84a9e0
| | | -CXXThisExpr 0x7ffd2d869e18 <col:8> 'struct B *' this | | -ImplicitCastExpr 0x7ffd2d869ec8 col:8 'int' -MemberExpr 0x7ffd2d869e90 <col:8> 'int' lvalue .a 0x7ffd2d84a9e0 | | -DeclRefExpr 0x7ffd2d869e68 col:8 'struct B' lvalue ParmVar 0x7ffd2d869d00 '' 'struct B &'
| |-ForStmt 0x7ffd2d86a2f0 col:8
| | |-DeclStmt 0x7ffd2d869fa0 col:8
| | | -VarDecl 0x7ffd2d869f20 <col:8> col:8 used __i0 'unsigned long' cinit | | | -IntegerLiteral 0x7ffd2d869f80 col:8 'unsigned long' 0
| | |-<<-DeclRefExpr 0x7ffd2d86a248 <col:8> 'unsigned long' lvalue Var 0x7ffd2d869f20 '__i0' 'unsigned long' | | | -IntegerLiteral 0x7ffd2d86a288 col:8 'unsigned long' 1
| | |-UnaryOperator 0x7ffd2d86a2a8 col:8 'unsigned long' lvalue prefix '++'
| | | -DeclRefExpr 0x7ffd2d86a2c8 <col:8> 'unsigned long' lvalue Var 0x7ffd2d869f20 '__i0' 'unsigned long' | | -CXXMemberCallExpr 0x7ffd2d86a1f0 col:8 'struct A' lvalue
| | |-MemberExpr 0x7ffd2d86a0c0 col:8 '-ArraySubscriptExpr 0x7ffd2d86a088 <col:8> 'struct A' lvalue | | | |-ImplicitCastExpr 0x7ffd2d86a070 <col:8> 'struct A *' <ArrayToPointerDecay> | | | | -MemberExpr 0x7ffd2d869ff8 col:8 'struct A [1]' lvalue ->b 0x7ffd2d84ab58
| | | | -CXXThisExpr 0x7ffd2d869fe0 <col:8> 'struct B *' this | | | -ImplicitCastExpr 0x7ffd2d86a058 col:8 'unsigned long' -DeclRefExpr 0x7ffd2d86a030 <col:8> 'unsigned long' lvalue Var 0x7ffd2d869f20 '__i0' 'unsigned long' | | -ArraySubscriptExpr 0x7ffd2d86a1c8 col:8 'struct A' lvalue
| | |-ImplicitCastExpr 0x7ffd2d86a1b0 col:8 'struct A ' -MemberExpr 0x7ffd2d86a138 <col:8> 'struct A [1]' lvalue .b 0x7ffd2d84ab58 | | | -DeclRefExpr 0x7ffd2d86a110 col:8 'struct B' lvalue ParmVar 0x7ffd2d869d00 '' 'struct B &'
| | -ImplicitCastExpr 0x7ffd2d86a198 <col:8> 'unsigned long' <LValueToRValue> | | -DeclRefExpr 0x7ffd2d86a170 col:8 'unsigned long' lvalue Var 0x7ffd2d869f20 '__i0' 'unsigned long'
| -ReturnStmt 0x7ffd2d86a360 <col:8> | -UnaryOperator 0x7ffd2d86a340 col:8 'struct B' lvalue prefix '
The (admittedly slight) difference with bug 11645 is that the statements are not evaluated twice, merely used twice as terminators. That /ought/ to be safe...but you're right that it's also an invariant that hasn't been violated before.
Yes, I was wrong in my diagnosis -- good job finding the real problem. However, repeating a statement messes up more than just liveness analysis -- the CFG also doesn't expect us to repeat statements. We have a lot of places in the analyzer that assumes statements are not evaluated more than once (other than loops, of course).
I think we should just submit a patch to change Sema to not reuse Stmts even in synthetic ASTs.
Changing Sema will definitely solve this problem, but I was also thinking about bug #11645, where we have the same problem -- the same conditional expressions are reused both for the normal evaluation, and for running destructors. And there is no way to disambiguate that now, since they are generated from the same statement. Right now, I believe I can make it work also in the current settings, but in the long run, I think it would be preferable to have distinguish the cases somehow.
But you are right that this sharing of statements is dangerous. If one is not careful, it could cause all sorts of nasty bugs. I will try to prepare a patch to Sema and see what they say.
Makes sense - thx for explaining.
Yeah. The analyzer uses some per-function analyses, like the liveness analysis, and some cross-functional state. All of that state is keyed with a Stmt/LocationContext pair, though -- even if statements were never shared, we'd still have a problem with recursive functions if that weren't true.
And I assume this holds even in the face of inlining? (sorry if this is obvious, I don't know the implementation details here)
Yes, but not within the same function body.
Stmts are also reused for template instantiations: the AST of multiple instantiations (and the original template definition) will share the same statements below the points where they are not template dependent any more.
Yes, I was wrong in my diagnosis -- good job finding the real problem. However, repeating a statement messes up more than just liveness analysis -- the CFG also doesn't expect us to repeat statements. We have a lot of places in the analyzer that assumes statements are not evaluated more than once (other than loops, of course).
I think we should just submit a patch to change Sema to not reuse Stmts even in synthetic ASTs.
I started looking into this. I did not get what the commit you mentioned does completely (except the explicitly silencing the warning for implicit constructors part), but I first wanted to see why is the warning generated in the first place.
So I crafted an assignment operator with the exact same AST as the implicit one, and, surprisingly, the warning disappeared. Then I took a closer at the implicit AST, and noticed that it is actually sharing all the DeclRefExprs that refer to __i0 (whereas the hand-written one has one Expr for each occurence of __i0). This then throws the liveness analysis off, as it uses Stmt* for specifying the location in the code. This can be confirmed by running the analyzer in the debugger and seeing that the SymbolReaper considers __i0 dead, even though there are clearly references to it further down the line.
To me, this is one more proof that using Stmt* as a key in the liveness analysis is a bad idea, and I would suggest changing it, even if it turns out it's not necessary to solve this bug.
Whoops. r178602 fixed this for copy constructors, but not assignment operators.
assigned to @devincoughlin