clang 18.0.0git
BuiltinFunctionChecker.cpp
Go to the documentation of this file.
1//=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
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 clang builtin functions.
10//
11//===----------------------------------------------------------------------===//
12
20
21using namespace clang;
22using namespace ento;
23
24namespace {
25
26class BuiltinFunctionChecker : public Checker<eval::Call> {
27public:
28 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
29};
30
31}
32
33bool BuiltinFunctionChecker::evalCall(const CallEvent &Call,
34 CheckerContext &C) const {
35 ProgramStateRef state = C.getState();
36 const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
37 if (!FD)
38 return false;
39
40 const LocationContext *LCtx = C.getLocationContext();
41 const Expr *CE = Call.getOriginExpr();
42
43 switch (FD->getBuiltinID()) {
44 default:
45 return false;
46
47 case Builtin::BI__builtin_assume: {
48 assert (Call.getNumArgs() > 0);
49 SVal Arg = Call.getArgSVal(0);
50 if (Arg.isUndef())
51 return true; // Return true to model purity.
52
53 state = state->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
54 // FIXME: do we want to warn here? Not right now. The most reports might
55 // come from infeasible paths, thus being false positives.
56 if (!state) {
57 C.generateSink(C.getState(), C.getPredecessor());
58 return true;
59 }
60
61 C.addTransition(state);
62 return true;
63 }
64
65 case Builtin::BI__builtin_unpredictable:
66 case Builtin::BI__builtin_expect:
67 case Builtin::BI__builtin_expect_with_probability:
68 case Builtin::BI__builtin_assume_aligned:
69 case Builtin::BI__builtin_addressof:
70 case Builtin::BI__builtin_function_start: {
71 // For __builtin_unpredictable, __builtin_expect,
72 // __builtin_expect_with_probability and __builtin_assume_aligned,
73 // just return the value of the subexpression.
74 // __builtin_addressof is going from a reference to a pointer, but those
75 // are represented the same way in the analyzer.
76 assert (Call.getNumArgs() > 0);
77 SVal Arg = Call.getArgSVal(0);
78 C.addTransition(state->BindExpr(CE, LCtx, Arg));
79 return true;
80 }
81
82 case Builtin::BI__builtin_alloca_with_align:
83 case Builtin::BI__builtin_alloca: {
84 SValBuilder &SVB = C.getSValBuilder();
85 const loc::MemRegionVal R =
86 SVB.getAllocaRegionVal(CE, C.getLocationContext(), C.blockCount());
87
88 // Set the extent of the region in bytes. This enables us to use the SVal
89 // of the argument directly. If we saved the extent in bits, it'd be more
90 // difficult to reason about values like symbol*8.
91 auto Size = Call.getArgSVal(0);
92 if (auto DefSize = Size.getAs<DefinedOrUnknownSVal>()) {
93 // This `getAs()` is mostly paranoia, because core.CallAndMessage reports
94 // undefined function arguments (unless it's disabled somehow).
95 state = setDynamicExtent(state, R.getRegion(), *DefSize, SVB);
96 }
97 C.addTransition(state->BindExpr(CE, LCtx, R));
98 return true;
99 }
100
101 case Builtin::BI__builtin_dynamic_object_size:
102 case Builtin::BI__builtin_object_size:
103 case Builtin::BI__builtin_constant_p: {
104 // This must be resolvable at compile time, so we defer to the constant
105 // evaluator for a value.
106 SValBuilder &SVB = C.getSValBuilder();
107 SVal V = UnknownVal();
108 Expr::EvalResult EVResult;
109 if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) {
110 // Make sure the result has the correct type.
111 llvm::APSInt Result = EVResult.Val.getInt();
113 BVF.getAPSIntType(CE->getType()).apply(Result);
114 V = SVB.makeIntVal(Result);
115 }
116
117 if (FD->getBuiltinID() == Builtin::BI__builtin_constant_p) {
118 // If we didn't manage to figure out if the value is constant or not,
119 // it is safe to assume that it's not constant and unsafe to assume
120 // that it's constant.
121 if (V.isUnknown())
122 V = SVB.makeIntVal(0, CE->getType());
123 }
124
125 C.addTransition(state->BindExpr(CE, LCtx, V));
126 return true;
127 }
128 }
129}
130
131void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
132 mgr.registerChecker<BuiltinFunctionChecker>();
133}
134
135bool ento::shouldRegisterBuiltinFunctionChecker(const CheckerManager &mgr) {
136 return true;
137}
#define V(N, I)
Definition: ASTContext.h:3241
Defines enum values for all the target-independent builtin functions.
APSInt & getInt()
Definition: APValue.h:423
This represents one expression.
Definition: Expr.h:110
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
@ SE_NoSideEffects
Strictly evaluate the expression.
Definition: Expr.h:655
QualType getType() const
Definition: Expr.h:142
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
void apply(llvm::APSInt &Value) const
Convert a given APSInt, in place, to match this type.
Definition: APSIntType.h:37
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:152
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:157
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:286
loc::MemRegionVal getAllocaRegionVal(const Expr *E, const LocationContext *LCtx, unsigned Count)
Create an SVal representing the result of an alloca()-like call, that is, an AllocaRegion on the stac...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
bool isUndef() const
Definition: SVals.h:104
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:82
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getRegion() const
Get the underlining region.
Definition: SVals.h:449
ProgramStateRef setDynamicExtent(ProgramStateRef State, const MemRegion *MR, DefinedOrUnknownSVal Extent, SValBuilder &SVB)
Set the dynamic extent Extent of the region MR.
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:629
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:631