Skip to content

Commit afe73f4

Browse files
[analyzer][NFC] Explain why operator new/delete should never be eval-called (#161370)
Downstream, some change triggered an investigation if we could move a checker callback from check::PostCall to eval::Call. After a lengthy investigation that lead to ExprEngine::VisitCXXNewExpr we realized that CXXNewExprs only trigger a PreCall and PostCall, but never an EvalCall. It also had a FIXME that maybe it should trigger it. Remember, it called `defaultEvalCall` which either inlines or conservatively evaluates aka. invalidates the call. But never probes the checker eval-calls to see if any would step in. After implementing the changes to trigger the eval call for the checkers, I realized that it doesn't really make sense because we are eval-calling user-provided functions, that we can't be really sure about their semantics, thus there is no generic way to properly implement the eval call callback. This touches on an important point. It only ever makes sense to eval call functions that has a clear spec. such as standard functions, as implementing the callback would prevent the inlining of that function, risking regressing analysis quality if the implemented model is not complete/correct enough. As a conclusion, I opted for not exposing the eval call event to checkers, in other words, keep everything as-is, but document my journey. CPP-6585
1 parent 82efd72 commit afe73f4

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/AnalysisOrderChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ class AnalysisOrderChecker
129129
llvm::errs() << " {argno: " << Call.getNumArgs() << '}';
130130
llvm::errs() << " [" << Call.getKindAsString() << ']';
131131
llvm::errs() << '\n';
132-
return true;
132+
// We can't return `true` from this callback without binding the return
133+
// value. Let's just fallthrough here and return `false`.
133134
}
134135
return false;
135136
}

clang/lib/StaticAnalyzer/Checkers/CheckerDocumentation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ class CheckerDocumentation
262262
/// state. This callback allows a checker to provide domain specific knowledge
263263
/// about the particular functions it knows about.
264264
///
265+
/// Note that to evaluate a call, the handler MUST bind the return value if
266+
/// its a non-void function. Invalidate the arguments if necessary.
267+
///
268+
/// Note that in general, user-provided functions should not be eval-called
269+
/// because the checker can't predict the exact semantics/contract of the
270+
/// callee, and by having the eval::Call callback, we also prevent it from
271+
/// getting inlined, potentially regressing analysis quality.
272+
/// Consider using check::PreCall or check::PostCall to allow inlining.
273+
///
265274
/// \returns true if the call has been successfully evaluated
266275
/// and false otherwise. Note, that only one checker can evaluate a call. If
267276
/// more than one checker claims that they can evaluate the same call the

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,14 @@ void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
909909
ExplodedNodeSet DstPostCall;
910910
StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
911911
for (ExplodedNode *I : DstPreCall) {
912-
// FIXME: Provide evalCall for checkers?
912+
// Operator new calls (CXXNewExpr) are intentionally not eval-called,
913+
// because it does not make sense to eval-call user-provided functions.
914+
// 1) If the new operator can be inlined, then don't prevent it from
915+
// inlining by having an eval-call of that operator.
916+
// 2) If it can't be inlined, then the default conservative modeling
917+
// is what we want anyway.
918+
// So the best is to not allow eval-calling CXXNewExprs from checkers.
919+
// Checkers can provide their pre/post-call callbacks if needed.
913920
defaultEvalCall(CallBldr, I, *Call);
914921
}
915922
// If the call is inlined, DstPostCall will be empty and we bail out now.
@@ -1110,6 +1117,10 @@ void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
11101117
if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
11111118
StmtNodeBuilder Bldr(DstPreCall, DstPostCall, *currBldrCtx);
11121119
for (ExplodedNode *I : DstPreCall) {
1120+
// Intentionally either inline or conservative eval-call the operator
1121+
// delete, but avoid triggering an eval-call event for checkers.
1122+
// As detailed at handling CXXNewExprs, in short, because it does not
1123+
// really make sense to eval-call user-provided functions.
11131124
defaultEvalCall(Bldr, I, *Call);
11141125
}
11151126
} else {

clang/test/Analysis/cxxctr-evalcall-analysis-order.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,33 @@ void foo() {
1818
C C0;
1919
C C1(42);
2020
C *C2 = new C{2, 3};
21+
delete C2;
2122
}
2223

2324
// CHECK: PreCall (C::C) [CXXConstructorCall]
2425
// CHECK-NEXT: EvalCall (C::C) {argno: 0} [CXXConstructorCall]
2526
// CHECK-NEXT: PostCall (C::C) [CXXConstructorCall]
27+
2628
// CHECK-NEXT: PreCall (C::C) [CXXConstructorCall]
2729
// CHECK-NEXT: EvalCall (C::C) {argno: 1} [CXXConstructorCall]
2830
// CHECK-NEXT: PostCall (C::C) [CXXConstructorCall]
31+
2932
// CHECK-NEXT: PreCall (operator new) [CXXAllocatorCall]
33+
// COMMENT: Operator new calls (CXXNewExpr) are intentionally not eval-called,
34+
// COMMENT: because it does not make sense to eval call user-provided functions.
35+
// COMMENT: 1) If the new operator can be inlined, then don't prevent it from
36+
// COMMENT: inlining by having an eval-call of that operator.
37+
// COMMENT: 2) If it can't be inlined, then the default conservative modeling
38+
// COMMENT: is what we anyways want anyway.
39+
// COMMENT: So the EvalCall event will not be triggered for operator new calls.
40+
// CHECK-NOT: EvalCall
3041
// CHECK-NEXT: PostCall (operator new) [CXXAllocatorCall]
42+
3143
// CHECK-NEXT: PreCall (C::C) [CXXConstructorCall]
3244
// CHECK-NEXT: EvalCall (C::C) {argno: 2} [CXXConstructorCall]
3345
// CHECK-NEXT: PostCall (C::C) [CXXConstructorCall]
46+
47+
// CHECK-NEXT: PreCall (operator delete) [CXXDeallocatorCall]
48+
// COMMENT: Same reasoning as for CXXNewExprs above.
49+
// CHECK-NOT: EvalCall
50+
// CHECK-NEXT: PostCall (operator delete) [CXXDeallocatorCall]

0 commit comments

Comments
 (0)