2011-11-17 01:09:15 +00:00
|
|
|
//== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-11-17 01:09:15 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines CheckerContext that provides contextual info for
|
|
|
|
// path-sensitive checkers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2012-01-18 02:45:07 +00:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2012-01-20 00:11:12 +00:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2023-06-17 13:18:23 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-01-18 02:45:07 +00:00
|
|
|
|
2011-11-17 01:09:15 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2011-12-01 05:57:37 +00:00
|
|
|
const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
|
2021-06-18 14:20:17 +03:00
|
|
|
const FunctionDecl *D = CE->getDirectCallee();
|
|
|
|
if (D)
|
|
|
|
return D;
|
|
|
|
|
2011-11-17 01:09:15 +00:00
|
|
|
const Expr *Callee = CE->getCallee();
|
2018-01-17 20:27:29 +00:00
|
|
|
SVal L = Pred->getSVal(Callee);
|
2011-12-01 05:57:37 +00:00
|
|
|
return L.getAsFunctionDecl();
|
|
|
|
}
|
2011-11-17 01:09:15 +00:00
|
|
|
|
2012-01-18 02:45:07 +00:00
|
|
|
StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
|
|
|
|
if (!FunDecl)
|
2011-11-17 01:09:15 +00:00
|
|
|
return StringRef();
|
2012-01-18 02:45:07 +00:00
|
|
|
IdentifierInfo *funI = FunDecl->getIdentifier();
|
2011-11-17 01:09:15 +00:00
|
|
|
if (!funI)
|
|
|
|
return StringRef();
|
|
|
|
return funI->getName();
|
|
|
|
}
|
2012-01-18 02:45:07 +00:00
|
|
|
|
2016-01-29 18:43:15 +00:00
|
|
|
StringRef CheckerContext::getDeclDescription(const Decl *D) {
|
2021-10-20 17:43:31 +02:00
|
|
|
if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
|
2016-01-29 18:43:15 +00:00
|
|
|
return "method";
|
|
|
|
if (isa<BlockDecl>(D))
|
|
|
|
return "anonymous block";
|
|
|
|
return "function";
|
|
|
|
}
|
2012-01-18 02:45:07 +00:00
|
|
|
|
|
|
|
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
|
2012-01-31 19:33:39 +00:00
|
|
|
StringRef Name) {
|
2012-01-18 02:45:07 +00:00
|
|
|
// To avoid false positives (Ex: finding user defined functions with
|
|
|
|
// similar names), only perform fuzzy name matching when it's a builtin.
|
|
|
|
// Using a string compare is slow, we might want to switch on BuiltinID here.
|
|
|
|
unsigned BId = FD->getBuiltinID();
|
|
|
|
if (BId != 0) {
|
2012-11-02 23:49:29 +00:00
|
|
|
if (Name.empty())
|
|
|
|
return true;
|
2015-08-06 01:01:12 +00:00
|
|
|
StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
|
2022-02-11 10:45:18 +01:00
|
|
|
size_t start = BName.find(Name);
|
|
|
|
if (start != StringRef::npos) {
|
|
|
|
// Accept exact match.
|
|
|
|
if (BName.size() == Name.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// v-- match starts here
|
|
|
|
// ...xxxxx...
|
|
|
|
// _xxxxx_
|
|
|
|
// ^ ^ lookbehind and lookahead characters
|
|
|
|
|
|
|
|
const auto MatchPredecessor = [=]() -> bool {
|
|
|
|
return start <= 0 || !llvm::isAlpha(BName[start - 1]);
|
|
|
|
};
|
|
|
|
const auto MatchSuccessor = [=]() -> bool {
|
|
|
|
std::size_t LookbehindPlace = start + Name.size();
|
|
|
|
return LookbehindPlace >= BName.size() ||
|
|
|
|
!llvm::isAlpha(BName[LookbehindPlace]);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (MatchPredecessor() && MatchSuccessor())
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-18 02:45:07 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 19:16:20 +00:00
|
|
|
const IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
// If this is a special C++ name without IdentifierInfo, it can't be a
|
|
|
|
// C library function.
|
|
|
|
if (!II)
|
|
|
|
return false;
|
|
|
|
|
2024-03-25 12:43:51 +01:00
|
|
|
// C library functions are either declared directly within a TU (the common
|
|
|
|
// case) or they are accessed through the namespace `std` (when they are used
|
|
|
|
// in C++ via headers like <cstdlib>).
|
|
|
|
const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
|
|
|
|
if (!(DC->isTranslationUnit() || DC->isStdNamespace()))
|
2012-11-02 23:49:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If this function is not externally visible, it is not a C library function.
|
2012-11-02 23:49:29 +00:00
|
|
|
// Note that we make an exception for inline functions, which may be
|
|
|
|
// declared in header files without external linkage.
|
2013-05-13 00:12:11 +00:00
|
|
|
if (!FD->isInlined() && !FD->isExternallyVisible())
|
2012-11-02 23:49:24 +00:00
|
|
|
return false;
|
|
|
|
|
2012-11-02 23:49:29 +00:00
|
|
|
if (Name.empty())
|
|
|
|
return true;
|
|
|
|
|
2012-02-01 19:16:20 +00:00
|
|
|
StringRef FName = II->getName();
|
2024-05-11 11:38:52 -07:00
|
|
|
if (FName == Name)
|
2012-02-17 22:35:26 +00:00
|
|
|
return true;
|
|
|
|
|
2023-12-13 08:54:13 -08:00
|
|
|
if (FName.starts_with("__inline") && FName.contains(Name))
|
2012-02-17 22:35:26 +00:00
|
|
|
return true;
|
2012-01-31 19:33:39 +00:00
|
|
|
|
2012-01-18 02:45:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-20 00:11:12 +00:00
|
|
|
|
2024-04-05 11:20:27 +02:00
|
|
|
bool CheckerContext::isHardenedVariantOf(const FunctionDecl *FD,
|
|
|
|
StringRef Name) {
|
|
|
|
const IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto CompletelyMatchesParts = [II](auto... Parts) -> bool {
|
|
|
|
StringRef FName = II->getName();
|
|
|
|
return (FName.consume_front(Parts) && ...) && FName.empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
return CompletelyMatchesParts("__", Name, "_chk") ||
|
|
|
|
CompletelyMatchesParts("__builtin_", "__", Name, "_chk");
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:11:12 +00:00
|
|
|
StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
|
2012-01-20 21:50:17 +00:00
|
|
|
if (Loc.isMacroID())
|
2012-01-20 00:11:12 +00:00
|
|
|
return Lexer::getImmediateMacroName(Loc, getSourceManager(),
|
2012-03-11 07:00:24 +00:00
|
|
|
getLangOpts());
|
2020-11-17 13:02:58 +00:00
|
|
|
SmallString<16> buf;
|
2012-03-11 07:00:24 +00:00
|
|
|
return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
|
2012-01-20 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:49:35 +00:00
|
|
|
/// Evaluate comparison and return true if it's known that condition is true
|
|
|
|
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
|
|
|
|
SVal RHSVal, ProgramStateRef State) {
|
|
|
|
if (LHSVal.isUnknownOrUndef())
|
|
|
|
return false;
|
|
|
|
ProgramStateManager &Mgr = State->getStateManager();
|
2022-06-15 16:58:08 +02:00
|
|
|
if (!isa<NonLoc>(LHSVal)) {
|
2017-10-11 14:49:35 +00:00
|
|
|
LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
|
|
|
|
LHSVal.castAs<Loc>());
|
2022-06-15 16:58:08 +02:00
|
|
|
if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
|
2017-10-11 14:49:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SValBuilder &Bldr = Mgr.getSValBuilder();
|
|
|
|
SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
|
|
|
|
Bldr.getConditionType());
|
|
|
|
if (Eval.isUnknownOrUndef())
|
|
|
|
return false;
|
|
|
|
ProgramStateRef StTrue, StFalse;
|
|
|
|
std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
|
|
|
|
return StTrue && !StFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
|
|
|
|
DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
|
|
|
|
return evalComparison(getSVal(E), BO_GE, V, getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckerContext::isNegative(const Expr *E) {
|
|
|
|
DefinedSVal V = getSValBuilder().makeIntVal(0, false);
|
|
|
|
return evalComparison(getSVal(E), BO_LT, V, getState());
|
|
|
|
}
|