Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
#include "clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
Expand Down Expand Up @@ -69,7 +70,8 @@ struct UncheckedStatusOrAccessModelOptions {};

// Dataflow analysis that discovers unsafe uses of StatusOr values.
class UncheckedStatusOrAccessModel
: public DataflowAnalysis<UncheckedStatusOrAccessModel, NoopLattice> {
: public DataflowAnalysis<UncheckedStatusOrAccessModel,
CachedConstAccessorsLattice<NoopLattice>> {
public:
explicit UncheckedStatusOrAccessModel(ASTContext &Ctx, Environment &Env);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,49 @@ static auto isAsStatusCallWithStatusOr() {
hasArgument(0, hasType(statusOrType())));
}

static auto possiblyReferencedStatusOrType() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return anyOf(statusOrType(), referenceType(pointee(statusOrType())));
}

static auto isConstStatusOrAccessorMemberCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxMemberCallExpr(callee(
cxxMethodDecl(parameterCountIs(0), isConst(),
returns(qualType(possiblyReferencedStatusOrType())))));
}

static auto isConstStatusOrAccessorMemberOperatorCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxOperatorCallExpr(
callee(cxxMethodDecl(parameterCountIs(0), isConst(),
returns(possiblyReferencedStatusOrType()))));
}

static auto isConstStatusOrPointerAccessorMemberCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxMemberCallExpr(callee(cxxMethodDecl(
parameterCountIs(0), isConst(),
returns(pointerType(pointee(possiblyReferencedStatusOrType()))))));
}

static auto isConstStatusOrPointerAccessorMemberOperatorCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxOperatorCallExpr(callee(cxxMethodDecl(
parameterCountIs(0), isConst(),
returns(pointerType(pointee(possiblyReferencedStatusOrType()))))));
}

static auto isNonConstMemberCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxMemberCallExpr(callee(cxxMethodDecl(unless(isConst()))));
}

static auto isNonConstMemberOperatorCall() {
using namespace ::clang::ast_matchers; // NOLINT: Too many names
return cxxOperatorCallExpr(callee(cxxMethodDecl(unless(isConst()))));
}

static auto
buildDiagnoseMatchSwitch(const UncheckedStatusOrAccessModelOptions &Options) {
return CFGMatchSwitchBuilder<const Environment,
Expand Down Expand Up @@ -698,6 +741,114 @@ static void transferPointerToBoolean(const ImplicitCastExpr *Expr,
State.Env.setValue(*Expr, *SubExprVal);
}

static void transferStatusOrReturningCall(const CallExpr *Expr,
LatticeTransferState &State) {
RecordStorageLocation *StatusOrLoc =
Expr->isPRValue() ? &State.Env.getResultObjectLocation(*Expr)
: State.Env.get<RecordStorageLocation>(*Expr);
if (StatusOrLoc != nullptr &&
State.Env.getValue(locForOk(locForStatus(*StatusOrLoc))) == nullptr)
initializeStatusOr(*StatusOrLoc, State.Env);
}

static bool doHandleConstStatusOrAccessorMemberCall(
const CallExpr *Expr, RecordStorageLocation *RecordLoc,
const MatchFinder::MatchResult &Result, LatticeTransferState &State) {
assert(isStatusOrType(Expr->getType()));
if (RecordLoc == nullptr)
return false;
const FunctionDecl *DirectCallee = Expr->getDirectCallee();
if (DirectCallee == nullptr)
return false;
StorageLocation &Loc =
State.Lattice.getOrCreateConstMethodReturnStorageLocation(
*RecordLoc, DirectCallee, State.Env, [&](StorageLocation &Loc) {
initializeStatusOr(cast<RecordStorageLocation>(Loc), State.Env);
});
if (Expr->isPRValue()) {
auto &ResultLoc = State.Env.getResultObjectLocation(*Expr);
copyRecord(cast<RecordStorageLocation>(Loc), ResultLoc, State.Env);
} else {
State.Env.setStorageLocation(*Expr, Loc);
}
return true;
}

static void handleConstStatusOrAccessorMemberCall(
const CallExpr *Expr, RecordStorageLocation *RecordLoc,
const MatchFinder::MatchResult &Result, LatticeTransferState &State) {
if (!doHandleConstStatusOrAccessorMemberCall(Expr, RecordLoc, Result, State))
transferStatusOrReturningCall(Expr, State);
}
static void handleConstStatusOrPointerAccessorMemberCall(
const CallExpr *Expr, RecordStorageLocation *RecordLoc,
const MatchFinder::MatchResult &Result, LatticeTransferState &State) {
if (RecordLoc == nullptr)
return;
auto *Val = State.Lattice.getOrCreateConstMethodReturnValue(*RecordLoc, Expr,
State.Env);
State.Env.setValue(*Expr, *Val);
}

static void
transferConstStatusOrAccessorMemberCall(const CXXMemberCallExpr *Expr,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
handleConstStatusOrAccessorMemberCall(
Expr, getImplicitObjectLocation(*Expr, State.Env), Result, State);
}

static void transferConstStatusOrAccessorMemberOperatorCall(
const CXXOperatorCallExpr *Expr, const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
auto *RecordLoc = cast_or_null<RecordStorageLocation>(
State.Env.getStorageLocation(*Expr->getArg(0)));
handleConstStatusOrAccessorMemberCall(Expr, RecordLoc, Result, State);
}

static void transferConstStatusOrPointerAccessorMemberCall(
const CXXMemberCallExpr *Expr, const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
handleConstStatusOrPointerAccessorMemberCall(
Expr, getImplicitObjectLocation(*Expr, State.Env), Result, State);
}

static void transferConstStatusOrPointerAccessorMemberOperatorCall(
const CXXOperatorCallExpr *Expr, const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
auto *RecordLoc = cast_or_null<RecordStorageLocation>(
State.Env.getStorageLocation(*Expr->getArg(0)));
handleConstStatusOrPointerAccessorMemberCall(Expr, RecordLoc, Result, State);
}

static void handleNonConstMemberCall(const CallExpr *Expr,
RecordStorageLocation *RecordLoc,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
if (RecordLoc) {
State.Lattice.clearConstMethodReturnValues(*RecordLoc);
State.Lattice.clearConstMethodReturnStorageLocations(*RecordLoc);
}
if (isStatusOrType(Expr->getType()))
transferStatusOrReturningCall(Expr, State);
}

static void transferNonConstMemberCall(const CXXMemberCallExpr *Expr,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
handleNonConstMemberCall(Expr, getImplicitObjectLocation(*Expr, State.Env),
Result, State);
}

static void
transferNonConstMemberOperatorCall(const CXXOperatorCallExpr *Expr,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
auto *RecordLoc = cast_or_null<RecordStorageLocation>(
State.Env.getStorageLocation(*Expr->getArg(0)));
handleNonConstMemberCall(Expr, RecordLoc, Result, State);
}

CFGMatchSwitch<LatticeTransferState>
buildTransferMatchSwitch(ASTContext &Ctx,
CFGMatchSwitchBuilder<LatticeTransferState> Builder) {
Expand Down Expand Up @@ -755,6 +906,23 @@ buildTransferMatchSwitch(ASTContext &Ctx,
transferLoggingGetReferenceableValueCall)
.CaseOfCFGStmt<CallExpr>(isLoggingCheckEqImpl(),
transferLoggingCheckEqImpl)
// const accessor calls
.CaseOfCFGStmt<CXXMemberCallExpr>(isConstStatusOrAccessorMemberCall(),
transferConstStatusOrAccessorMemberCall)
.CaseOfCFGStmt<CXXOperatorCallExpr>(
isConstStatusOrAccessorMemberOperatorCall(),
transferConstStatusOrAccessorMemberOperatorCall)
.CaseOfCFGStmt<CXXMemberCallExpr>(
isConstStatusOrPointerAccessorMemberCall(),
transferConstStatusOrPointerAccessorMemberCall)
.CaseOfCFGStmt<CXXOperatorCallExpr>(
isConstStatusOrPointerAccessorMemberOperatorCall(),
transferConstStatusOrPointerAccessorMemberOperatorCall)
// non-const member calls that may modify the state of an object.
.CaseOfCFGStmt<CXXMemberCallExpr>(isNonConstMemberCall(),
transferNonConstMemberCall)
.CaseOfCFGStmt<CXXOperatorCallExpr>(isNonConstMemberOperatorCall(),
transferNonConstMemberOperatorCall)
// N.B. These need to come after all other CXXConstructExpr.
// These are there to make sure that every Status and StatusOr object
// have their ok boolean initialized when constructed. If we were to
Expand Down
90 changes: 90 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,95 @@ using testing::AssertionResult;
#endif // TESTING_DEFS_H
)cc";

constexpr const char StdUniquePtrHeader[] = R"cc(
namespace std {

template <typename T>
struct default_delete {};

template <typename T, typename D = default_delete<T>>
class unique_ptr {
public:
using element_type = T;
using deleter_type = D;

constexpr unique_ptr();
constexpr unique_ptr(nullptr_t) noexcept;
unique_ptr(unique_ptr&&);
explicit unique_ptr(T*);
template <typename U, typename E>
unique_ptr(unique_ptr<U, E>&&);

~unique_ptr();

unique_ptr& operator=(unique_ptr&&);
template <typename U, typename E>
unique_ptr& operator=(unique_ptr<U, E>&&);
unique_ptr& operator=(nullptr_t);

void reset(T* = nullptr) noexcept;
T* release();
T* get() const;

T& operator*() const;
T* operator->() const;
explicit operator bool() const noexcept;
};

template <typename T, typename D>
class unique_ptr<T[], D> {
public:
T* get() const;
T& operator[](size_t i);
const T& operator[](size_t i) const;
};

template <typename T, typename... Args>
unique_ptr<T> make_unique(Args&&...);

template <class T, class D>
void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;

template <class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);

template <class T, class D>
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template <class T, class D>
bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
template <class T, class D>
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template <class T, class D>
bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
template <class T, class D>
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator<(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator>(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
}
)cc";

std::vector<std::pair<std::string, std::string>> getMockHeaders() {
std::vector<std::pair<std::string, std::string>> Headers;
Headers.emplace_back("cstddef.h", CStdDefHeader);
Expand All @@ -2249,6 +2338,7 @@ std::vector<std::pair<std::string, std::string>> getMockHeaders() {
Headers.emplace_back("statusor_defs.h", StatusOrDefsHeader);
Headers.emplace_back("absl_log.h", AbslLogHeader);
Headers.emplace_back("testing_defs.h", TestingDefsHeader);
Headers.emplace_back("std_unique_ptr.h", StdUniquePtrHeader);
return Headers;
}

Expand Down
Loading
Loading