Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 1 | //===- CastValueChecker - Model implementation of custom RTTIs --*- 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 | // |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 9 | // This defines CastValueChecker which models casts of custom RTTIs. |
| 10 | // |
| 11 | // TODO list: |
| 12 | // - It only allows one succesful cast between two types however in the wild |
| 13 | // the object could be casted to multiple types. |
| 14 | // - It needs to check the most likely type information from the dynamic type |
| 15 | // map to increase precision of dynamic casting. |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h" |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Optional.h" |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 26 | #include <utility> |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | using namespace ento; |
| 30 | |
| 31 | namespace { |
| 32 | class CastValueChecker : public Checker<eval::Call> { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 33 | enum class CallKind { Function, Method }; |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 34 | |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 35 | using CastCheck = |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 36 | std::function<void(const CastValueChecker *, const CallEvent &Call, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 37 | DefinedOrUnknownSVal, CheckerContext &)>; |
| 38 | |
| 39 | public: |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 40 | // We have five cases to evaluate a cast: |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 41 | // 1) The parameter is non-null, the return value is non-null. |
| 42 | // 2) The parameter is non-null, the return value is null. |
| 43 | // 3) The parameter is null, the return value is null. |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 44 | // cast: 1; dyn_cast: 1, 2; cast_or_null: 1, 3; dyn_cast_or_null: 1, 2, 3. |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 45 | // |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 46 | // 4) castAs: Has no parameter, the return value is non-null. |
| 47 | // 5) getAs: Has no parameter, the return value is null or non-null. |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 48 | bool evalCall(const CallEvent &Call, CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 49 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 50 | |
| 51 | private: |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 52 | // These are known in the LLVM project. The pairs are in the following form: |
| 53 | // {{{namespace, call}, argument-count}, {callback, kind}} |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 54 | const CallDescriptionMap<std::pair<CastCheck, CallKind>> CDM = { |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 55 | {{{"llvm", "cast"}, 1}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 56 | {&CastValueChecker::evalCast, CallKind::Function}}, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 57 | {{{"llvm", "dyn_cast"}, 1}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 58 | {&CastValueChecker::evalDynCast, CallKind::Function}}, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 59 | {{{"llvm", "cast_or_null"}, 1}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 60 | {&CastValueChecker::evalCastOrNull, CallKind::Function}}, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 61 | {{{"llvm", "dyn_cast_or_null"}, 1}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 62 | {&CastValueChecker::evalDynCastOrNull, CallKind::Function}}, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 63 | {{{"clang", "castAs"}, 0}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 64 | {&CastValueChecker::evalCastAs, CallKind::Method}}, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 65 | {{{"clang", "getAs"}, 0}, |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 66 | {&CastValueChecker::evalGetAs, CallKind::Method}}}; |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 67 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 68 | void evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 69 | CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 70 | void evalDynCast(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 71 | CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 72 | void evalCastOrNull(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 73 | CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 74 | void evalDynCastOrNull(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 75 | CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 76 | void evalCastAs(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 77 | CheckerContext &C) const; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 78 | void evalGetAs(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 79 | CheckerContext &C) const; |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 80 | }; |
| 81 | } // namespace |
| 82 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 83 | static QualType getRecordType(QualType Ty) { |
| 84 | Ty = Ty.getCanonicalType(); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 85 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 86 | if (Ty->isPointerType()) |
| 87 | Ty = Ty->getPointeeType(); |
| 88 | |
| 89 | if (Ty->isReferenceType()) |
| 90 | Ty = Ty.getNonReferenceType(); |
| 91 | |
| 92 | return Ty.getUnqualifiedType(); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 95 | static bool isInfeasibleCast(const DynamicCastInfo *CastInfo, |
| 96 | bool CastSucceeds) { |
| 97 | if (!CastInfo) |
| 98 | return false; |
| 99 | |
| 100 | return CastSucceeds ? CastInfo->fails() : CastInfo->succeeds(); |
| 101 | } |
| 102 | |
| 103 | static const NoteTag *getNoteTag(CheckerContext &C, |
| 104 | const DynamicCastInfo *CastInfo, |
| 105 | QualType CastToTy, const Expr *Object, |
| 106 | bool CastSucceeds, bool IsKnownCast) { |
| 107 | std::string CastToName = |
| 108 | CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString() |
| 109 | : CastToTy->getAsCXXRecordDecl()->getNameAsString(); |
| 110 | Object = Object->IgnoreParenImpCasts(); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 111 | |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 112 | return C.getNoteTag( |
Csaba Dabis | 22dc44f | 2019-08-22 01:41:06 +0000 | [diff] [blame^] | 113 | [=]() -> std::string { |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 114 | SmallString<128> Msg; |
| 115 | llvm::raw_svector_ostream Out(Msg); |
| 116 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 117 | if (!IsKnownCast) |
| 118 | Out << "Assuming "; |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 119 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 120 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Object)) { |
| 121 | Out << '\'' << DRE->getDecl()->getNameAsString() << '\''; |
| 122 | } else if (const auto *ME = dyn_cast<MemberExpr>(Object)) { |
| 123 | Out << (IsKnownCast ? "Field '" : "field '") |
| 124 | << ME->getMemberDecl()->getNameAsString() << '\''; |
| 125 | } else { |
| 126 | Out << (IsKnownCast ? "The object" : "the object"); |
| 127 | } |
| 128 | |
| 129 | Out << ' ' << (CastSucceeds ? "is a" : "is not a") << " '" << CastToName |
| 130 | << '\''; |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 131 | |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 132 | return Out.str(); |
| 133 | }, |
| 134 | /*IsPrunable=*/true); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 135 | } |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 136 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // Main logic to evaluate a cast. |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
| 141 | static void addCastTransition(const CallEvent &Call, DefinedOrUnknownSVal DV, |
| 142 | CheckerContext &C, bool IsNonNullParam, |
| 143 | bool IsNonNullReturn, |
| 144 | bool IsCheckedCast = false) { |
| 145 | ProgramStateRef State = C.getState()->assume(DV, IsNonNullParam); |
| 146 | if (!State) |
| 147 | return; |
| 148 | |
| 149 | const Expr *Object; |
| 150 | QualType CastFromTy; |
| 151 | QualType CastToTy = getRecordType(Call.getResultType()); |
| 152 | |
| 153 | if (Call.getNumArgs() > 0) { |
| 154 | Object = Call.getArgExpr(0); |
| 155 | CastFromTy = getRecordType(Call.parameters()[0]->getType()); |
| 156 | } else { |
| 157 | Object = cast<CXXInstanceCall>(&Call)->getCXXThisExpr(); |
| 158 | CastFromTy = getRecordType(Object->getType()); |
| 159 | } |
| 160 | |
| 161 | const MemRegion *MR = DV.getAsRegion(); |
| 162 | const DynamicCastInfo *CastInfo = |
| 163 | getDynamicCastInfo(State, MR, CastFromTy, CastToTy); |
| 164 | |
| 165 | // We assume that every checked cast succeeds. |
| 166 | bool CastSucceeds = IsCheckedCast || CastFromTy == CastToTy; |
| 167 | if (!CastSucceeds) { |
| 168 | if (CastInfo) |
| 169 | CastSucceeds = IsNonNullReturn && CastInfo->succeeds(); |
| 170 | else |
| 171 | CastSucceeds = IsNonNullReturn; |
| 172 | } |
| 173 | |
| 174 | // Check for infeasible casts. |
| 175 | if (isInfeasibleCast(CastInfo, CastSucceeds)) { |
| 176 | C.generateSink(State, C.getPredecessor()); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // Store the type and the cast information. |
| 181 | bool IsKnownCast = CastInfo || IsCheckedCast || CastFromTy == CastToTy; |
| 182 | if (!IsKnownCast || IsCheckedCast) |
| 183 | State = setDynamicTypeAndCastInfo(State, MR, CastFromTy, CastToTy, |
| 184 | Call.getResultType(), CastSucceeds); |
| 185 | |
| 186 | SVal V = CastSucceeds ? DV : C.getSValBuilder().makeNull(); |
| 187 | C.addTransition( |
| 188 | State->BindExpr(Call.getOriginExpr(), C.getLocationContext(), V, false), |
| 189 | getNoteTag(C, CastInfo, CastToTy, Object, CastSucceeds, IsKnownCast)); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | // Evaluating cast, dyn_cast, cast_or_null, dyn_cast_or_null. |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 196 | static void evalNonNullParamNonNullReturn(const CallEvent &Call, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 197 | DefinedOrUnknownSVal DV, |
| 198 | CheckerContext &C, |
| 199 | bool IsCheckedCast = false) { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 200 | addCastTransition(Call, DV, C, /*IsNonNullParam=*/true, |
| 201 | /*IsNonNullReturn=*/true, IsCheckedCast); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 204 | static void evalNonNullParamNullReturn(const CallEvent &Call, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 205 | DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 206 | CheckerContext &C) { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 207 | addCastTransition(Call, DV, C, /*IsNonNullParam=*/true, |
| 208 | /*IsNonNullReturn=*/false); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 211 | static void evalNullParamNullReturn(const CallEvent &Call, |
| 212 | DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 213 | CheckerContext &C) { |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 214 | if (ProgramStateRef State = C.getState()->assume(DV, false)) |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 215 | C.addTransition(State->BindExpr(Call.getOriginExpr(), |
| 216 | C.getLocationContext(), |
| 217 | C.getSValBuilder().makeNull(), false), |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 218 | C.getNoteTag("Assuming null pointer is passed into cast", |
| 219 | /*IsPrunable=*/true)); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 222 | void CastValueChecker::evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 223 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 224 | evalNonNullParamNonNullReturn(Call, DV, C, /*IsCheckedCast=*/true); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 227 | void CastValueChecker::evalDynCast(const CallEvent &Call, |
| 228 | DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 229 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 230 | evalNonNullParamNonNullReturn(Call, DV, C); |
| 231 | evalNonNullParamNullReturn(Call, DV, C); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 234 | void CastValueChecker::evalCastOrNull(const CallEvent &Call, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 235 | DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 236 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 237 | evalNonNullParamNonNullReturn(Call, DV, C); |
| 238 | evalNullParamNullReturn(Call, DV, C); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 241 | void CastValueChecker::evalDynCastOrNull(const CallEvent &Call, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 242 | DefinedOrUnknownSVal DV, |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 243 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 244 | evalNonNullParamNonNullReturn(Call, DV, C); |
| 245 | evalNonNullParamNullReturn(Call, DV, C); |
| 246 | evalNullParamNullReturn(Call, DV, C); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | //===----------------------------------------------------------------------===// |
| 250 | // Evaluating castAs, getAs. |
| 251 | //===----------------------------------------------------------------------===// |
| 252 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 253 | static void evalZeroParamNonNullReturn(const CallEvent &Call, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 254 | DefinedOrUnknownSVal DV, |
| 255 | CheckerContext &C, |
| 256 | bool IsCheckedCast = false) { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 257 | addCastTransition(Call, DV, C, /*IsNonNullParam=*/true, |
| 258 | /*IsNonNullReturn=*/true, IsCheckedCast); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 261 | static void evalZeroParamNullReturn(const CallEvent &Call, |
| 262 | DefinedOrUnknownSVal DV, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 263 | CheckerContext &C) { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 264 | addCastTransition(Call, DV, C, /*IsNonNullParam=*/true, |
| 265 | /*IsNonNullReturn=*/false); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 268 | void CastValueChecker::evalCastAs(const CallEvent &Call, |
| 269 | DefinedOrUnknownSVal DV, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 270 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 271 | evalZeroParamNonNullReturn(Call, DV, C, /*IsCheckedCast=*/true); |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 274 | void CastValueChecker::evalGetAs(const CallEvent &Call, DefinedOrUnknownSVal DV, |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 275 | CheckerContext &C) const { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 276 | evalZeroParamNonNullReturn(Call, DV, C); |
| 277 | evalZeroParamNullReturn(Call, DV, C); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
| 281 | // Main logic to evaluate a call. |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 284 | bool CastValueChecker::evalCall(const CallEvent &Call, |
| 285 | CheckerContext &C) const { |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 286 | const auto *Lookup = CDM.lookup(Call); |
| 287 | if (!Lookup) |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 288 | return false; |
| 289 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 290 | // We need to obtain the record type of the call's result to model it. |
| 291 | if (!getRecordType(Call.getResultType())->isRecordType()) |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 292 | return false; |
| 293 | |
| 294 | const CastCheck &Check = Lookup->first; |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 295 | CallKind Kind = Lookup->second; |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 296 | Optional<DefinedOrUnknownSVal> DV; |
| 297 | |
| 298 | switch (Kind) { |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 299 | case CallKind::Function: { |
| 300 | // We need to obtain the record type of the call's parameter to model it. |
| 301 | if (!getRecordType(Call.parameters()[0]->getType())->isRecordType()) |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 302 | return false; |
| 303 | |
| 304 | DV = Call.getArgSVal(0).getAs<DefinedOrUnknownSVal>(); |
| 305 | break; |
| 306 | } |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 307 | case CallKind::Method: |
Csaba Dabis | cf229d5 | 2019-08-09 02:24:42 +0000 | [diff] [blame] | 308 | const auto *InstanceCall = dyn_cast<CXXInstanceCall>(&Call); |
| 309 | if (!InstanceCall) |
| 310 | return false; |
| 311 | |
| 312 | DV = InstanceCall->getCXXThisVal().getAs<DefinedOrUnknownSVal>(); |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | if (!DV) |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 317 | return false; |
| 318 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 319 | Check(this, Call, *DV, C); |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 320 | return true; |
| 321 | } |
| 322 | |
Csaba Dabis | 0202c35 | 2019-08-22 00:20:36 +0000 | [diff] [blame] | 323 | void CastValueChecker::checkDeadSymbols(SymbolReaper &SR, |
| 324 | CheckerContext &C) const { |
| 325 | C.addTransition(removeDeadCasts(C.getState(), SR)); |
| 326 | } |
| 327 | |
Csaba Dabis | 693936a | 2019-07-10 00:20:03 +0000 | [diff] [blame] | 328 | void ento::registerCastValueChecker(CheckerManager &Mgr) { |
| 329 | Mgr.registerChecker<CastValueChecker>(); |
| 330 | } |
| 331 | |
| 332 | bool ento::shouldRegisterCastValueChecker(const LangOptions &LO) { |
| 333 | return true; |
| 334 | } |