llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

CFG for destructors in '||'/'&&' lacks precision

Open tkremenek opened this issue 14 years ago • 52 comments

Bugzilla Link 11645
Resolution FIXED
Resolved on Aug 21, 2018 11:43
Version unspecified
OS All
CC @belkadan,@tkremenek,@labath,@haoNoQ,@xuzhongxing

Extended Description

The source-level CFG currently is very imprecise for modeling C++ destructors and '||'/'&&' short-circuit operations.

Consider:

$ cat /tmp/test.cpp int foo(); int bar();

class A { public: A(int x); ~A(); operator bool(); };

class B { public: B(int x); ~B(); operator bool(); };

class C { public: C(int x); ~C(); operator bool(); };

int test(int x, int y) { if (C b = A(x) || B(y)) return foo(); return bar(); }

Let's look at the CFG without destructors:

$ clang -cc1 -analyze -analyzer-checker=debug.DumpCFG /tmp/test.cpp [B6 (ENTRY)] Succs (1): B4

[B1] 1: bar 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 3: B1.2 4: return [B1.3]; Preds (1): B3 Succs (1): B0

[B2] 1: foo 2: [B2.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 3: B2.2 4: return [B2.3]; Preds (1): B3 Succs (1): B0

[B3] 1: [B4.8] || [B5.8] 2: [B3.1] (ImplicitCastExpr, IntegralCast, int) 3: [B3.2] (CXXConstructExpr, class C) 4: [B3.3] (ImplicitCastExpr, ConstructorConversion, class C) 5: [B3.4] (BindTemporary) 6: [B3.5] (ImplicitCastExpr, NoOp, const class C) 7: [B3.6] 8: C b = A(x).operator _Bool() || B(y).operator _Bool(); 9: b 10: [B3.9].operator _Bool 11: B3.10 12: [B3.11] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: if [B3.12] Preds (2): B5 B4 Succs (2): B2 B1

[B4] 1: x 2: [B4.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B4.2] (CXXConstructExpr, class A) 4: [B4.3] (BindTemporary) 5: A([B4.4]) (CXXFunctionalCastExpr, ConstructorConversion, class A) 6: [B4.5].operator _Bool 7: B4.6 8: [B4.7] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: [B4.8] || ... Preds (1): B6 Succs (2): B3 B5

[B5] 1: y 2: [B5.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B5.2] (CXXConstructExpr, class B) 4: [B5.3] (BindTemporary) 5: B([B5.4]) (CXXFunctionalCastExpr, ConstructorConversion, class B) 6: [B5.5].operator _Bool 7: B5.6 8: [B5.7] (ImplicitCastExpr, UserDefinedConversion, _Bool) Preds (1): B4 Succs (1): B3

[B0 (EXIT)] Preds (2): B1 B2

This CFG behaves as expected. We have a single block with the '||' operation as a terminator (block 4), and a single block that merges the values from the LHS and RHS expressions (block B3) which is then terminated with the 'if' branch terminator. My motivation for looking at this CFG was to further optimize this so that block B5 was seen as dominating block B2. This would improve the quality of some flow-sensitive (but path-insensitive) analyses in the frontend and also simplify the CFG.

The CFG gets significantly more complicated (and wrong) when we add implicit destructors:

$ clang -cc1 -analyze -cfg-add-implicit-dtors -analyzer-checker=debug.DumpCFG /tmp/test.cpp

[B8 (ENTRY)] Succs (1): B6

[B1] 1: [B3.2].~C() (Implicit destructor) 2: bar 3: [B1.2] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 4: B1.3 5: return [B1.4]; Preds (1): B3 Succs (1): B0

[B2] 1: foo 2: [B2.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 3: B2.2 4: return [B2.3]; 5: [B3.2].~C() (Implicit destructor) Preds (1): B3 Succs (1): B0

[B3] 1: ~A() (Temporary object destructor) 2: C b = A(x).operator _Bool() || B(y).operator _Bool(); 3: b 4: [B3.3].operator _Bool 5: B3.4 6: [B3.5] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: if [B3.6] Preds (2): B4 B5 Succs (2): B2 B1

[B4] 1: ~B() (Temporary object destructor) Preds (1): B5 Succs (1): B3

[B5] 1: [B6.8] || [B7.8] 2: [B5.1] (ImplicitCastExpr, IntegralCast, int) 3: [B5.2] (CXXConstructExpr, class C) 4: [B5.3] (ImplicitCastExpr, ConstructorConversion, class C) 5: [B5.4] (BindTemporary) 6: [B5.5] (ImplicitCastExpr, NoOp, const class C) 7: [B5.6] 8: ~C() (Temporary object destructor) T: [B6.8] || ... Preds (2): B7 B6 Succs (2): B3 B4

[B6] 1: x 2: [B6.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B6.2] (CXXConstructExpr, class A) 4: [B6.3] (BindTemporary) 5: A([B6.4]) (CXXFunctionalCastExpr, ConstructorConversion, class A) 6: [B6.5].operator _Bool 7: B6.6 8: [B6.7] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: [B6.8] || ... Preds (1): B8 Succs (2): B5 B7

[B7] 1: y 2: [B7.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B7.2] (CXXConstructExpr, class B) 4: [B7.3] (BindTemporary) 5: B([B7.4]) (CXXFunctionalCastExpr, ConstructorConversion, class B) 6: [B7.5].operator _Bool 7: B7.6 8: [B7.7] (ImplicitCastExpr, UserDefinedConversion, _Bool) Preds (1): B6 Succs (1): B5

[B0 (EXIT)] Preds (2): B1 B2

Notice that blocks B6 and B5 have the same terminator, which itself is suspect. The rationale is that the temporary for 'B(y)' should only be destructed when we evaluated the RHS of the logical operation. This is good enough for the static analyzer, but the CFG itself doesn't explicitly capture this dependency. This can be seen by observing that there is a path in the CFG from the entry block to the exit block where destructor ~B is called but the constructor B() is NOT called. While the static analyzer will likely get this right and recover that information (since it does enough value flow analysis to understand control-dependent branches), this unnecessarily drops a bunch of control-dependencies from the CFG which will degrade the quality of CFG-based analyses in the frontend.

The second, and more glaring problem, is that ~C is called twice along a path, both as an implicit destructor (in B1 and B2 respectively) and a destructor for a temporary (in B5). I may be missing something here, but this doesn't show up in the LLVM IR (which clearly shows a single destructor call to ~C).

Also, look at where the destructors are called. In block B1, ~C is correctly called before the call to bar(), but in block B2, the destructor appears after the return statement. Instead, it should appear immediately after the call to foo().

tkremenek avatar Dec 23 '11 03:12 tkremenek

We're doing pretty well here, i guess the CFG was fixed by Manuel Klimek in 2014.

I attached the current CFG.svg for easier reading.

The CFG contains all three temporary destructors and one automatic destructor. There's an extra correlated branch that controls conditional execution of ~B() which makes the CFG look like a triple-diamond instead of a double-diamond. The analyzer understands such branching it by remembering which temporaries were created in a special path-sensitive state trait.

Additionally, i made sure that copy elision is marked up in the CFG: the construct-expression on [B5.2] contains a forward reference to the future actual constructor on [B5.8], alongside with heads-up on future bind-temporary at [B5.5] and materialize-temporary at [B5.7]. Other constructors are not elidable, so they only have forward references to bind/materialize temporary (for A(x) and B(y)) and to the variable declaration (for 'C b'). The analyzer was taught to make use of this information.

There are other problems of this sort that were not addressed, eg. GNU binary ?: operator is broken, but && and || seem fine.

Additionally, complexity of the CFG seems to grow linearly, and every temporary destructor appears in the CFG exactly once; i didn't look deeply into this, but it seems to be the case. Automatic destructor for 'C b' in our example will appear twice (depending on which branch of the if-statement is taken), but this doesn't seem to be exploitable. I'm attaching a CFG-chained-destructors.svg in which the initializer for 'C b' is replaced with (A(1) && A(2)) || (A(3) && A(4)) || (A(5) && A(6)) || (A(7) && A(8)).

==================

Raw dump of the current CFG for easier comparing:

int test(int x, int y) [B8 (ENTRY)] Succs (1): B7

[B1] 1: [B5.9].~C() (Implicit destructor) 2: bar 3: [B1.2] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 4: B1.3 5: return [B1.4]; Preds (1): B3 Succs (1): B0

[B2] 1: foo 2: [B2.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void)) 3: B2.2 4: return [B2.3]; 5: [B5.9].~C() (Implicit destructor) Preds (1): B3 Succs (1): B0

[B3] 1: ~A() (Temporary object destructor) 2: b 3: [B3.2].operator bool 4: [B3.2] 5: [B3.4] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: if [B3.5] Preds (2): B4 B5 Succs (2): B2 B1

[B4] 1: ~B() (Temporary object destructor) Preds (1): B5 Succs (1): B3

[B5] 1: [B7.9] || [B6.9] 2: [B5.1] (ImplicitCastExpr, IntegralCast, int) 3: [B5.2] (CXXConstructExpr, [B5.5], [B5.7], [B5.8], class C) 4: [B5.3] (ImplicitCastExpr, ConstructorConversion, class C) 5: [B5.4] (BindTemporary) 6: [B5.5] (ImplicitCastExpr, NoOp, const class C) 7: [B5.6] 8: [B5.7] (CXXConstructExpr, [B5.9], class C) 9: C b = A(x) || B(y); 10: ~C() (Temporary object destructor) T: (Temp Dtor) [B6.4] Preds (2): B6 B7 Succs (2): B4 B3

[B6] 1: y 2: [B6.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B6.2] (CXXConstructExpr, [B6.4], [B6.6], class B) 4: [B6.3] (BindTemporary) 5: B([B6.4]) (CXXFunctionalCastExpr, ConstructorConversion, class B) 6: [B6.5] 7: [B6.6].operator bool 8: [B6.6] 9: [B6.8] (ImplicitCastExpr, UserDefinedConversion, _Bool) Preds (1): B7 Succs (1): B5

[B7] 1: x 2: [B7.1] (ImplicitCastExpr, LValueToRValue, int) 3: [B7.2] (CXXConstructExpr, [B7.4], [B7.6], class A) 4: [B7.3] (BindTemporary) 5: A([B7.4]) (CXXFunctionalCastExpr, ConstructorConversion, class A) 6: [B7.5] 7: [B7.6].operator bool 8: [B7.6] 9: [B7.8] (ImplicitCastExpr, UserDefinedConversion, _Bool) T: [B7.9] || ... Preds (1): B8 Succs (2): B5 B6

[B0 (EXIT)] Preds (2): B1 B2

haoNoQ avatar Aug 21 '18 18:08 haoNoQ

Current CFG

haoNoQ avatar Aug 21 '18 18:08 haoNoQ

My back of the napkin calculations show that the amount of duplication of destructor sets should be linear if we want to expand all control flow. Consider:

e_1 && e_2 && ... && e_N

For each expression, e_i, we have a set of associated destructors, Destructors(e_i). The question is how many times will Destructors(e_i) be duplicated in the CFG?

Suppose we are looking at e_i, and we can take either the false branch or the true branch. If we take the true branch, we move on to evaluate e_i+1. If we take the false branch, we need to evaluate Destructors(e_1), Destructors(e_2), ... , Destructors(e_i), in reverse order.

Inductively, Destructors(e_1) will be evaluated in N places (or N+1, depending on how we want to handle the last expression, e_n). Similarly, Destructors(e_2) will be evaluated in N-1 places, Destructors(e_3) in N-2 places, etc. Assuming the cost of evaluating each destructor set is the same, we're looking at a linear series:

N + N - 1 + N - 2 + ... + 1

or:

n(n+1)/2

which is O(n^2).

Thus we are looking at quadratic complexity, in the worst case.

I do not believe this is true. I think you can easily get an exponential function if you alternate &&'s and ||'s a bit.

Suppose I just take two sets of expressions e1 && e2 && ... && en, and connect them with an ||. This way, you get O(n) ways that the first sub-expression can evaluate to false, and another O(n) for the second one. This results in O(n^2) code paths and each one contains O(n) destructors, so the CFG size already reaches O(n^3). If I put more of these subexpressions in the ||, I can easily reach any exponent I want...

You've convinced me. That's a valid argument. Thus the only way to model this efficiently would be to have separate branches for the destructors. Ideally groups of destructors that would be guaranteed to be executed together would be guarded under the same branch (if a branch is needed).

tkremenek avatar Aug 06 '13 02:08 tkremenek

My back of the napkin calculations show that the amount of duplication of destructor sets should be linear if we want to expand all control flow. Consider:

e_1 && e_2 && ... && e_N

For each expression, e_i, we have a set of associated destructors, Destructors(e_i). The question is how many times will Destructors(e_i) be duplicated in the CFG?

Suppose we are looking at e_i, and we can take either the false branch or the true branch. If we take the true branch, we move on to evaluate e_i+1. If we take the false branch, we need to evaluate Destructors(e_1), Destructors(e_2), ... , Destructors(e_i), in reverse order.

Inductively, Destructors(e_1) will be evaluated in N places (or N+1, depending on how we want to handle the last expression, e_n). Similarly, Destructors(e_2) will be evaluated in N-1 places, Destructors(e_3) in N-2 places, etc. Assuming the cost of evaluating each destructor set is the same, we're looking at a linear series:

N + N - 1 + N - 2 + ... + 1

or:

n(n+1)/2

which is O(n^2).

Thus we are looking at quadratic complexity, in the worst case.

I do not believe this is true. I think you can easily get an exponential function if you alternate &&'s and ||'s a bit.

Suppose I just take two sets of expressions e1 && e2 && ... && en, and connect them with an ||. This way, you get O(n) ways that the first sub-expression can evaluate to false, and another O(n) for the second one. This results in O(n^2) code paths and each one contains O(n) destructors, so the CFG size already reaches O(n^3). If I put more of these subexpressions in the ||, I can easily reach any exponent I want...

labath avatar Jul 26 '13 11:07 labath

It seems to me that the right solution is to model the destructor "branches" in the CFG, and for analyses that care they will need to track the predicates of which constructors were called to properly match up with the feasibility of those branches.

This is something that will naturally be handled by the static analyzer, just like other predicates.

For analyses in the frontend, we should perhaps consider a generalized dataflow worklist that keeps predicates for some dataflow facts, for the analyses that need a small amount of path-sensitivity (e.g., -Wuninitialized).

tkremenek avatar Jun 29 '12 21:06 tkremenek

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

Looks reasonable.

I'm not fully convinced: the drawback of a hybrid approach is that algorithms that use the CFG will now have to cope with 2 possible graphs, and thereby not simplifying them at all. Also, if N==5 is reasonable, then I think that would cover a lot more than 90%, but that's my subjective experience.

I don't see how there are 2 possible graphs that an algorithm would need to cope with. Essentially I see two constructs:

(1) A terminator (indicating to take a branch to conditionally execute a destructor) and a target basic block with a destructor call.

(2) Unrolled code where there is no terminator and there is a basic block with a destructor call.

Consider that (2) is just a special case of (1) where the branch is eliminated. I don't see how this complicates algorithms at all. Algorithms will need support (1) because that will be our fallback in the general case. (2) is just a case where the CFG is simplified/refined to not contain the branch.

My proposal of a hybrid approach doesn't complicate analysis algorithms at all. It just potentially makes them a bit smarter by having the CFG incorporate a bit of path-sensisitivity where it isn't too costly. I think this would directly benefit algorithms such as -Wuninitialized, etc., which themselves shouldn't be engineered to understand predicated logic if we can help it.

Jordan convinced me in person that duplicating the CFG is a bad idea, at least for tackling this problem. At the worst case we starting get exponential duplication of the CFG, as destructors can be separated between their constructors by large amounts. Consider:

const &A a = flag ? existing : A(); ... // lots of stuff with control flow destructor gets called, conditionally for A()

We wil need to model the in the CFG whether or not to call the destructor for the temporary. That's unavoidable. But duplicating everything between the constructor and destructor to elide that branch is infeasible in practice.

tkremenek avatar Jun 29 '12 21:06 tkremenek

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

Looks reasonable.

I'm not fully convinced: the drawback of a hybrid approach is that algorithms that use the CFG will now have to cope with 2 possible graphs, and thereby not simplifying them at all. Also, if N==5 is reasonable, then I think that would cover a lot more than 90%, but that's my subjective experience.

I don't see how there are 2 possible graphs that an algorithm would need to cope with. Essentially I see two constructs:

(1) A terminator (indicating to take a branch to conditionally execute a destructor) and a target basic block with a destructor call.

(2) Unrolled code where there is no terminator and there is a basic block with a destructor call.

Consider that (2) is just a special case of (1) where the branch is eliminated. I don't see how this complicates algorithms at all. Algorithms will need support (1) because that will be our fallback in the general case. (2) is just a case where the CFG is simplified/refined to not contain the branch.

My proposal of a hybrid approach doesn't complicate analysis algorithms at all. It just potentially makes them a bit smarter by having the CFG incorporate a bit of path-sensisitivity where it isn't too costly. I think this would directly benefit algorithms such as -Wuninitialized, etc., which themselves shouldn't be engineered to understand predicated logic if we can help it.

tkremenek avatar Jun 29 '12 06:06 tkremenek

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

Looks reasonable.

To clarify, I was saying that the fully expanded approach looks reasonable. I don't like the hybrid approach.

xuzhongxing avatar Feb 06 '12 16:02 xuzhongxing

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

Looks reasonable.

I'm not fully convinced: the drawback of a hybrid approach is that algorithms that use the CFG will now have to cope with 2 possible graphs, and thereby not simplifying them at all. Also, if N==5 is reasonable, then I think that would cover a lot more than 90%, but that's my subjective experience.

That notwithstanding...:

Then the hard part is implementation. In the first try, I met with roughly three problems. (Consider x = ex1 op ex2 op ...op exn)

  1. We don't know the number of branches the BinaryOperators would end up with.
  2. Each of the components of the full expression should be unconditionally appended to each of the branch. And we need to mark the subexpr whose value should be taken as the outmost BinaryOperator's.
  3. Different destructors are to be appended to difference branches.

One approach is to build the CFG as we currently do, and when that is done, "unfold" as many paths as wanted/needed. Another approach is to build the CFG recursively, and use a continuation-like object to specify what to generate for the successor blocks of an expression.

llvmbot avatar Feb 06 '12 14:02 llvmbot

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

Looks reasonable. Then the hard part is implementation. In the first try, I met with roughly three problems. (Consider x = ex1 op ex2 op ...op exn)

  1. We don't know the number of branches the BinaryOperators would end up with.
  2. Each of the components of the full expression should be unconditionally appended to each of the branch. And we need to mark the subexpr whose value should be taken as the outmost BinaryOperator's.
  3. Different destructors are to be appended to difference branches.

xuzhongxing avatar Feb 06 '12 07:02 xuzhongxing

Quadratic complexity is nothing the sneeze at. It isn't prohibitive, if N is small. For example, N == 5 leads to 5(5 + 1)/2 = 15. That's probably pretty reasonable. Larger N starts to get more questionable.

We could possibly take a hybrid approach, expanding for small N, and having a cleanup condition value for larger N. That would likely give higher precision to the 90% of cases.

tkremenek avatar Feb 06 '12 06:02 tkremenek

If it can be represented accurately in the LLVM IR at -O0, I think there shouldn't be any scalability concerns about representing this well in the CFG.

Clang CodeGen does what the current CFG does. It uses a "cleanup dest slot" or "cleanup condition value" to remember the branch that was taken, and switch on that value when it is time to do cleanups. So there is no control flow expansion in the LLVM IR.

Interesting. Good to know.

My back of the napkin calculations show that the amount of duplication of destructor sets should be linear if we want to expand all control flow. Consider:

e_1 && e_2 && ... && e_N

For each expression, e_i, we have a set of associated destructors, Destructors(e_i). The question is how many times will Destructors(e_i) be duplicated in the CFG?

Suppose we are looking at e_i, and we can take either the false branch or the true branch. If we take the true branch, we move on to evaluate e_i+1. If we take the false branch, we need to evaluate Destructors(e_1), Destructors(e_2), ... , Destructors(e_i), in reverse order.

Inductively, Destructors(e_1) will be evaluated in N places (or N+1, depending on how we want to handle the last expression, e_n). Similarly, Destructors(e_2) will be evaluated in N-1 places, Destructors(e_3) in N-2 places, etc. Assuming the cost of evaluating each destructor set is the same, we're looking at a linear series:

N + N - 1 + N - 2 + ... + 1

or:

n(n+1)/2

which is O(n^2).

Thus we are looking at quadratic complexity, in the worst case.

tkremenek avatar Feb 06 '12 06:02 tkremenek

If it can be represented accurately in the LLVM IR at -O0, I think there shouldn't be any scalability concerns about representing this well in the CFG.

Clang CodeGen does what the current CFG does. It uses a "cleanup dest slot" or "cleanup condition value" to remember the branch that was taken, and switch on that value when it is time to do cleanups. So there is no control flow expansion in the LLVM IR.

xuzhongxing avatar Feb 06 '12 04:02 xuzhongxing

Despite we agreed that having multiple occurrences of an Expr in the CFG is not a blocking problem, I found a few other concerns.

First having all control paths of a BinaryOperator of '&&' or '||' explicitly generated in the CFG might be not scalable. We would have branch numbers in proportion to the number of embedded '&&' or '||' in a full expr. (I haven't thought about if it is exponential.)

Second with the gneration of multiple branches of a BinaryOperator, we have to append different destructors to each branches, after the full expression is created in the CFG. This is not an easy problem to solve as I tried. Again we don't know how many branches a full expr with BinaryOperator may end up with.

That said, I still would like to have correct destructors in the CFG that are not dependant on the value of some flow control slot.

If it can be represented accurately in the LLVM IR at -O0, I think there shouldn't be any scalability concerns about representing this well in the CFG. The approach I would take is to draw this out, such as on graph paper or a whiteboard, and see where we can ideally optimize the expansion of a complex ||/&& expression. My gut is that this is going to be tricky, but that it still can be done. It seemed very tractable to reduce the size of the CFG for short circuit operations, and make the CFG more accurate, when one doesn't consider destructors. I think destructors reflect a bit of extra complexity, but I don't see how in worst case they wouldn't need to be duplicated more than once.

tkremenek avatar Feb 06 '12 03:02 tkremenek

Despite we agreed that having multiple occurrences of an Expr in the CFG is not a blocking problem, I found a few other concerns.

First having all control paths of a BinaryOperator of '&&' or '||' explicitly generated in the CFG might be not scalable. We would have branch numbers in proportion to the number of embedded '&&' or '||' in a full expr. (I haven't thought about if it is exponential.)

Second with the gneration of multiple branches of a BinaryOperator, we have to append different destructors to each branches, after the full expression is created in the CFG. This is not an easy problem to solve as I tried. Again we don't know how many branches a full expr with BinaryOperator may end up with.

That said, I still would like to have correct destructors in the CFG that are not dependant on the value of some flow control slot.

xuzhongxing avatar Feb 06 '12 03:02 xuzhongxing

A question for my understanding: should a ProgramPoint only get a CFGStmt*, or also an Stmt* or Expr*? For example, CheckObjCMessageContext passes an Expr to ProgramPoint::getProgramPoint(..), which can easily be a sub-expression. Or is each (sub-)expression already a separate CFGStmt in the CFG?

For the static analyzer, the CFG is linearized. This means that every Expr* (except a few things like ParenExprs) appear in the CFG as a CFGStmt.

If we made the change I proposed, we would likely need to move the frontend to using a linearized CFG as well. We need to evaluate the potential impact of this change to the frontend analyses.

When (and how) would you like to evaluate that? Or rephrasing: how would you like to proceed?

I am asking because to understand the impact of the changes you mentioned in comment #​29, I did the changes to the ProgramPoint. However, to get it compiled, changes cascade down the whole list you mention in item (1).

Changing the frontend to use a linearized CFG is completely orthogonal to item (1). ProgramPoint is also only used by the analyzer. What I am proposing is:

(a) Make the changes to ProgramPoint, and also make the changes to the analyzer needed to pick up those changes. This is a big project, but it only impacts the static analyzer.

(b) Evaluate the compile-time cost of always linearizing the CFG in AnalysisBasedWarnings.cpp. If the cost can be made small, switch to always using a linearized CFG. Currently we only create a linearized CFG for the static analyzer, but not frontend warnings.

(c) Once we are using a linearized CFG, and the analyzer has been changed to use the updated ProgramPoint, we can consider modifying the CFG. (a) and (b) do not include any CFG changes at all.

tkremenek avatar Jan 31 '12 21:01 tkremenek

A question for my understanding: should a ProgramPoint only get a CFGStmt*, or also an Stmt* or Expr*? For example, CheckObjCMessageContext passes an Expr to ProgramPoint::getProgramPoint(..), which can easily be a sub-expression. Or is each (sub-)expression already a separate CFGStmt in the CFG?

For the static analyzer, the CFG is linearized. This means that every Expr* (except a few things like ParenExprs) appear in the CFG as a CFGStmt.

If we made the change I proposed, we would likely need to move the frontend to using a linearized CFG as well. We need to evaluate the potential impact of this change to the frontend analyses.

When (and how) would you like to evaluate that? Or rephrasing: how would you like to proceed?

I am asking because to understand the impact of the changes you mentioned in comment #​29, I did the changes to the ProgramPoint. However, to get it compiled, changes cascade down the whole list you mention in item (1).

llvmbot avatar Jan 31 '12 13:01 llvmbot

A question for my understanding: should a ProgramPoint only get a CFGStmt*, or also an Stmt* or Expr*? For example, CheckObjCMessageContext passes an Expr to ProgramPoint::getProgramPoint(..), which can easily be a sub-expression. Or is each (sub-)expression already a separate CFGStmt in the CFG?

For the static analyzer, the CFG is linearized. This means that every Expr* (except a few things like ParenExprs) appear in the CFG as a CFGStmt.

If we made the change I proposed, we would likely need to move the frontend to using a linearized CFG as well. We need to evaluate the potential impact of this change to the frontend analyses.

tkremenek avatar Jan 30 '12 22:01 tkremenek

A question for my understanding: should a ProgramPoint only get a CFGStmt*, or also an Stmt* or Expr*? For example, CheckObjCMessageContext passes an Expr to ProgramPoint::getProgramPoint(..), which can easily be a sub-expression. Or is each (sub-)expression already a separate CFGStmt in the CFG?

For the static analyzer, the CFG is linearized. This means that every Expr* (except a few things like ParenExprs) appear in the CFG as a CFGStmt.

tkremenek avatar Jan 30 '12 22:01 tkremenek