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 13 #include "clang/Basic/Builtins.h" 14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 15 #include "clang/StaticAnalyzer/Core/Checker.h" 16 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h" 20 21 using namespace clang; 22 using namespace ento; 23 24 namespace { 25 26 class BuiltinFunctionChecker : public Checker<eval::Call> { 27 public: 28 bool evalCall(const CallEvent &Call, CheckerContext &C) const; 29 }; 30 31 } 32 33 bool 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_assume_aligned: 68 case Builtin::BI__builtin_addressof: { 69 // For __builtin_unpredictable, __builtin_expect, and 70 // __builtin_assume_aligned, just return the value of the subexpression. 71 // __builtin_addressof is going from a reference to a pointer, but those 72 // are represented the same way in the analyzer. 73 assert (Call.getNumArgs() > 0); 74 SVal Arg = Call.getArgSVal(0); 75 C.addTransition(state->BindExpr(CE, LCtx, Arg)); 76 return true; 77 } 78 79 case Builtin::BI__builtin_alloca_with_align: 80 case Builtin::BI__builtin_alloca: { 81 // FIXME: Refactor into StoreManager itself? 82 MemRegionManager& RM = C.getStoreManager().getRegionManager(); 83 const AllocaRegion* R = 84 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext()); 85 86 // Set the extent of the region in bytes. This enables us to use the 87 // SVal of the argument directly. If we save the extent in bits, we 88 // cannot represent values like symbol*8. 89 auto Size = Call.getArgSVal(0); 90 if (Size.isUndef()) 91 return true; // Return true to model purity. 92 93 SValBuilder& svalBuilder = C.getSValBuilder(); 94 DefinedOrUnknownSVal DynSize = getDynamicSize(state, R, svalBuilder); 95 DefinedOrUnknownSVal DynSizeMatchesSizeArg = 96 svalBuilder.evalEQ(state, DynSize, Size.castAs<DefinedOrUnknownSVal>()); 97 state = state->assume(DynSizeMatchesSizeArg, true); 98 assert(state && "The region should not have any previous constraints"); 99 100 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R))); 101 return true; 102 } 103 104 case Builtin::BI__builtin_dynamic_object_size: 105 case Builtin::BI__builtin_object_size: 106 case Builtin::BI__builtin_constant_p: { 107 // This must be resolvable at compile time, so we defer to the constant 108 // evaluator for a value. 109 SValBuilder &SVB = C.getSValBuilder(); 110 SVal V = UnknownVal(); 111 Expr::EvalResult EVResult; 112 if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) { 113 // Make sure the result has the correct type. 114 llvm::APSInt Result = EVResult.Val.getInt(); 115 BasicValueFactory &BVF = SVB.getBasicValueFactory(); 116 BVF.getAPSIntType(CE->getType()).apply(Result); 117 V = SVB.makeIntVal(Result); 118 } 119 120 if (FD->getBuiltinID() == Builtin::BI__builtin_constant_p) { 121 // If we didn't manage to figure out if the value is constant or not, 122 // it is safe to assume that it's not constant and unsafe to assume 123 // that it's constant. 124 if (V.isUnknown()) 125 V = SVB.makeIntVal(0, CE->getType()); 126 } 127 128 C.addTransition(state->BindExpr(CE, LCtx, V)); 129 return true; 130 } 131 } 132 } 133 134 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) { 135 mgr.registerChecker<BuiltinFunctionChecker>(); 136 } 137 138 bool ento::shouldRegisterBuiltinFunctionChecker(const LangOptions &LO) { 139 return true; 140 } 141