1349cc55cSDimitry Andric //=== StringChecker.cpp -------------------------------------------*- C++ -*--// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8349cc55cSDimitry Andric // 9349cc55cSDimitry Andric // This file implements the modeling of the std::basic_string type. 10349cc55cSDimitry Andric // This involves checking preconditions of the operations and applying the 11349cc55cSDimitry Andric // effects of the operations, e.g. their post-conditions. 12349cc55cSDimitry Andric // 13349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 14349cc55cSDimitry Andric 15349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 16349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 17349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h" 18349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 19349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 20349cc55cSDimitry Andric 21349cc55cSDimitry Andric using namespace clang; 22349cc55cSDimitry Andric using namespace ento; 23349cc55cSDimitry Andric 24349cc55cSDimitry Andric namespace { 25349cc55cSDimitry Andric class StringChecker : public Checker<check::PreCall> { 26349cc55cSDimitry Andric BugType BT_Null{this, "Dereference of null pointer", categories::LogicError}; 27349cc55cSDimitry Andric mutable const FunctionDecl *StringConstCharPtrCtor = nullptr; 28349cc55cSDimitry Andric mutable CanQualType SizeTypeTy; 29349cc55cSDimitry Andric const CallDescription TwoParamStdStringCtor = { 30*0fca6ea1SDimitry Andric CDM::CXXMethod, {"std", "basic_string", "basic_string"}, 2, 2}; 31349cc55cSDimitry Andric 32349cc55cSDimitry Andric bool isCharToStringCtor(const CallEvent &Call, const ASTContext &ACtx) const; 33349cc55cSDimitry Andric 34349cc55cSDimitry Andric public: 35349cc55cSDimitry Andric void checkPreCall(const CallEvent &Call, CheckerContext &C) const; 36349cc55cSDimitry Andric }; 37349cc55cSDimitry Andric 38349cc55cSDimitry Andric bool StringChecker::isCharToStringCtor(const CallEvent &Call, 39349cc55cSDimitry Andric const ASTContext &ACtx) const { 40349cc55cSDimitry Andric if (!TwoParamStdStringCtor.matches(Call)) 41349cc55cSDimitry Andric return false; 42349cc55cSDimitry Andric const auto *FD = dyn_cast<FunctionDecl>(Call.getDecl()); 43349cc55cSDimitry Andric assert(FD); 44349cc55cSDimitry Andric 45349cc55cSDimitry Andric // See if we already cached it. 46349cc55cSDimitry Andric if (StringConstCharPtrCtor && StringConstCharPtrCtor == FD) 47349cc55cSDimitry Andric return true; 48349cc55cSDimitry Andric 49349cc55cSDimitry Andric // Verify that the parameters have the expected types: 50349cc55cSDimitry Andric // - arg 1: `const CharT *` 51349cc55cSDimitry Andric // - arg 2: some allocator - which is definately not `size_t`. 52349cc55cSDimitry Andric const QualType Arg1Ty = Call.getArgExpr(0)->getType().getCanonicalType(); 53349cc55cSDimitry Andric const QualType Arg2Ty = Call.getArgExpr(1)->getType().getCanonicalType(); 54349cc55cSDimitry Andric 55349cc55cSDimitry Andric if (!Arg1Ty->isPointerType()) 56349cc55cSDimitry Andric return false; 57349cc55cSDimitry Andric 58349cc55cSDimitry Andric // It makes sure that we don't select the `string(const char* p, size_t len)` 59349cc55cSDimitry Andric // overload accidentally. 60349cc55cSDimitry Andric if (Arg2Ty.getCanonicalType() == ACtx.getSizeType()) 61349cc55cSDimitry Andric return false; 62349cc55cSDimitry Andric 63349cc55cSDimitry Andric StringConstCharPtrCtor = FD; // Cache the decl of the right overload. 64349cc55cSDimitry Andric return true; 65349cc55cSDimitry Andric } 66349cc55cSDimitry Andric 67349cc55cSDimitry Andric void StringChecker::checkPreCall(const CallEvent &Call, 68349cc55cSDimitry Andric CheckerContext &C) const { 69349cc55cSDimitry Andric if (!isCharToStringCtor(Call, C.getASTContext())) 70349cc55cSDimitry Andric return; 71349cc55cSDimitry Andric const auto Param = Call.getArgSVal(0).getAs<Loc>(); 7281ad6265SDimitry Andric if (!Param) 73349cc55cSDimitry Andric return; 74349cc55cSDimitry Andric 75349cc55cSDimitry Andric // We managed to constrain the parameter to non-null. 76349cc55cSDimitry Andric ProgramStateRef NotNull, Null; 77349cc55cSDimitry Andric std::tie(NotNull, Null) = C.getState()->assume(*Param); 78349cc55cSDimitry Andric 79349cc55cSDimitry Andric if (NotNull) { 80349cc55cSDimitry Andric const auto Callback = [Param](PathSensitiveBugReport &BR) -> std::string { 81349cc55cSDimitry Andric return BR.isInteresting(*Param) ? "Assuming the pointer is not null." 82349cc55cSDimitry Andric : ""; 83349cc55cSDimitry Andric }; 84349cc55cSDimitry Andric 85349cc55cSDimitry Andric // Emit note only if this operation constrained the pointer to be null. 86349cc55cSDimitry Andric C.addTransition(NotNull, Null ? C.getNoteTag(Callback) : nullptr); 87349cc55cSDimitry Andric return; 88349cc55cSDimitry Andric } 89349cc55cSDimitry Andric 90349cc55cSDimitry Andric // We found a path on which the parameter is NULL. 91349cc55cSDimitry Andric if (ExplodedNode *N = C.generateErrorNode(C.getState())) { 92349cc55cSDimitry Andric auto R = std::make_unique<PathSensitiveBugReport>( 93349cc55cSDimitry Andric BT_Null, "The parameter must not be null", N); 94349cc55cSDimitry Andric bugreporter::trackExpressionValue(N, Call.getArgExpr(0), *R); 95349cc55cSDimitry Andric C.emitReport(std::move(R)); 96349cc55cSDimitry Andric } 97349cc55cSDimitry Andric } 98349cc55cSDimitry Andric 99349cc55cSDimitry Andric } // end anonymous namespace 100349cc55cSDimitry Andric 101349cc55cSDimitry Andric void ento::registerStringChecker(CheckerManager &Mgr) { 102349cc55cSDimitry Andric Mgr.registerChecker<StringChecker>(); 103349cc55cSDimitry Andric } 104349cc55cSDimitry Andric 105349cc55cSDimitry Andric bool ento::shouldRegisterStringChecker(const CheckerManager &) { return true; } 106