Skip to content

Commit f10a870

Browse files
authored
[analyzer] Sink false [[assume]] execution paths (#130418)
This PR splits the existing modeling of builtin assume from the BuiltinFunctionChecker. We just sink the execution path if we are about to leave the assume expression with a false assumption. Assumptions with side-effects are skipped, and ignored. Their values are "UnknownVal" anyway.
1 parent d22d143 commit f10a870

File tree

7 files changed

+100
-33
lines changed

7 files changed

+100
-33
lines changed

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def TrustReturnsNonnullChecker : Checker<"TrustReturnsNonnull">,
385385
} // end "apiModeling"
386386

387387
//===----------------------------------------------------------------------===//
388-
// Evaluate "builtin" functions.
388+
// Evaluate "builtin" functions and assumptions.
389389
//===----------------------------------------------------------------------===//
390390

391391
let ParentPackage = CoreBuiltin in {
@@ -399,6 +399,10 @@ def BuiltinFunctionChecker : Checker<"BuiltinFunctions">,
399399
HelpText<"Evaluate compiler builtin functions (e.g., alloca())">,
400400
Documentation<NotDocumented>;
401401

402+
def AssumeModeling : Checker<"AssumeModeling">,
403+
HelpText<"Model compiler builtin assume functions and the assume attribute">,
404+
Documentation<NotDocumented>;
405+
402406
} // end "core.builtin"
403407

404408
//===----------------------------------------------------------------------===//
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//=== AssumeModeling.cpp --------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This checker evaluates the builting assume functions.
10+
// This checker also sinks execution paths leaving [[assume]] attributes with
11+
// false assumptions.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "clang/AST/AttrIterator.h"
16+
#include "clang/Basic/Builtins.h"
17+
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
18+
#include "clang/StaticAnalyzer/Core/Checker.h"
19+
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
20+
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21+
#include "llvm/ADT/STLExtras.h"
22+
23+
using namespace clang;
24+
using namespace ento;
25+
26+
namespace {
27+
class AssumeModelingChecker
28+
: public Checker<eval::Call, check::PostStmt<AttributedStmt>> {
29+
public:
30+
void checkPostStmt(const AttributedStmt *A, CheckerContext &C) const;
31+
bool evalCall(const CallEvent &Call, CheckerContext &C) const;
32+
};
33+
} // namespace
34+
35+
void AssumeModelingChecker::checkPostStmt(const AttributedStmt *A,
36+
CheckerContext &C) const {
37+
if (!hasSpecificAttr<CXXAssumeAttr>(A->getAttrs()))
38+
return;
39+
40+
for (const auto *Attr : getSpecificAttrs<CXXAssumeAttr>(A->getAttrs())) {
41+
SVal AssumptionVal = C.getSVal(Attr->getAssumption());
42+
43+
// The assumption is not evaluated at all if it had sideffects; skip them.
44+
if (AssumptionVal.isUnknown())
45+
continue;
46+
47+
const auto *Assumption = AssumptionVal.getAsInteger();
48+
assert(Assumption && "We should know the exact outcome of an assume expr");
49+
if (Assumption && Assumption->isZero()) {
50+
C.addSink();
51+
}
52+
}
53+
}
54+
55+
bool AssumeModelingChecker::evalCall(const CallEvent &Call,
56+
CheckerContext &C) const {
57+
ProgramStateRef State = C.getState();
58+
const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
59+
if (!FD)
60+
return false;
61+
62+
if (!llvm::is_contained({Builtin::BI__builtin_assume, Builtin::BI__assume},
63+
FD->getBuiltinID())) {
64+
return false;
65+
}
66+
67+
assert(Call.getNumArgs() > 0);
68+
SVal Arg = Call.getArgSVal(0);
69+
if (Arg.isUndef())
70+
return true; // Return true to model purity.
71+
72+
State = State->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
73+
if (!State) {
74+
C.addSink();
75+
return true;
76+
}
77+
78+
C.addTransition(State);
79+
return true;
80+
}
81+
82+
void ento::registerAssumeModeling(CheckerManager &Mgr) {
83+
Mgr.registerChecker<AssumeModelingChecker>();
84+
}
85+
86+
bool ento::shouldRegisterAssumeModeling(const CheckerManager &) { return true; }

clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2424
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
2525
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
26-
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
2726
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
2827

2928
using namespace clang;
@@ -288,25 +287,6 @@ bool BuiltinFunctionChecker::evalCall(const CallEvent &Call,
288287
handleOverflowBuiltin(Call, C, BO_Add,
289288
getOverflowBuiltinResultType(Call, C, BI));
290289
return true;
291-
case Builtin::BI__builtin_assume:
292-
case Builtin::BI__assume: {
293-
assert (Call.getNumArgs() > 0);
294-
SVal Arg = Call.getArgSVal(0);
295-
if (Arg.isUndef())
296-
return true; // Return true to model purity.
297-
298-
state = state->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
299-
// FIXME: do we want to warn here? Not right now. The most reports might
300-
// come from infeasible paths, thus being false positives.
301-
if (!state) {
302-
C.generateSink(C.getState(), C.getPredecessor());
303-
return true;
304-
}
305-
306-
C.addTransition(state);
307-
return true;
308-
}
309-
310290
case Builtin::BI__builtin_unpredictable:
311291
case Builtin::BI__builtin_expect:
312292
case Builtin::BI__builtin_expect_with_probability:

clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_clang_library(clangStaticAnalyzerCheckers
88
AnalysisOrderChecker.cpp
99
AnalyzerStatsChecker.cpp
1010
ArrayBoundChecker.cpp
11+
AssumeModeling.cpp
1112
BasicObjCFoundationChecks.cpp
1213
BitwiseShiftChecker.cpp
1314
BlockInCriticalSectionChecker.cpp

clang/test/Analysis/analyzer-enabled-checkers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// CHECK-NEXT: core.StackAddressEscape
2525
// CHECK-NEXT: core.UndefinedBinaryOperatorResult
2626
// CHECK-NEXT: core.VLASize
27+
// CHECK-NEXT: core.builtin.AssumeModeling
2728
// CHECK-NEXT: core.builtin.BuiltinFunctions
2829
// CHECK-NEXT: core.builtin.NoReturnFunctions
2930
// CHECK-NEXT: core.uninitialized.ArraySubscript

clang/test/Analysis/cxx23-assume-attribute.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_analyze_cc1 -std=c++23 -triple x86_64-pc-linux-gnu \
22
// RUN: -analyzer-checker=core,debug.ExprInspection -verify %s
33

4+
void clang_analyzer_warnIfReached();
45
template <typename T> void clang_analyzer_dump(T);
56
template <typename T> void clang_analyzer_value(T);
67

@@ -27,33 +28,22 @@ int ternary_in_builtin_assume(int a, int b) {
2728

2829
// From: https://github.com/llvm/llvm-project/pull/116462#issuecomment-2517853226
2930
int ternary_in_assume(int a, int b) {
30-
// FIXME notes
31-
// Currently, if this test is run without the core.builtin.Builtin checker, the above function with the __builtin_assume behaves identically to the following test
32-
// i.e. calls to `clang_analyzer_dump` result in "extraneous" prints of the SVal(s) `reg<int b> ...`
33-
// as opposed to 4 or 10
34-
// which likely implies the Program State(s) did not get narrowed.
35-
// A new checker is likely needed to be implemented to properly handle the expressions within `[[assume]]` to eliminate the states where `b` is not narrowed.
36-
3731
[[assume(a > 10 ? b == 4 : b == 10)]];
3832
clang_analyzer_value(a);
3933
// expected-warning@-1 {{[-2147483648, 10]}}
4034
// expected-warning@-2 {{[11, 2147483647]}}
4135

4236
clang_analyzer_dump(b); // expected-warning {{4}} expected-warning {{10}}
43-
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int b>}} FIXME: We shouldn't have this dump.
4437

4538
if (a > 20) {
4639
clang_analyzer_dump(b + 100); // expected-warning {{104}}
47-
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 100}} FIXME: We shouldn't have this dump.
4840
return 2;
4941
}
5042
if (a > 10) {
5143
clang_analyzer_dump(b + 200); // expected-warning {{204}}
52-
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 200}} FIXME: We shouldn't have this dump.
5344
return 1;
5445
}
5546
clang_analyzer_dump(b + 300); // expected-warning {{310}}
56-
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 300}} FIXME: We shouldn't have this dump.
5747
return 0;
5848
}
5949

@@ -70,8 +60,12 @@ int assume_and_fallthrough_at_the_same_attrstmt(int a, int b) {
7060
return b;
7161
}
7262
}
63+
7364
// This code should be unreachable.
65+
clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
66+
7467
[[assume(false)]]; // This should definitely make it so.
75-
clang_analyzer_dump(33); // expected-warning {{33 S32b}}
68+
clang_analyzer_warnIfReached(); // no-warning
69+
7670
return 0;
7771
}

clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
// CHECK-NEXT: core.StackAddressEscape
3333
// CHECK-NEXT: core.UndefinedBinaryOperatorResult
3434
// CHECK-NEXT: core.VLASize
35+
// CHECK-NEXT: core.builtin.AssumeModeling
3536
// CHECK-NEXT: core.builtin.BuiltinFunctions
3637
// CHECK-NEXT: core.builtin.NoReturnFunctions
3738
// CHECK-NEXT: core.uninitialized.ArraySubscript

0 commit comments

Comments
 (0)