Interpretation of question
You want to report whenever there is a path that calls open_transaction
twice without an intervening close_transaction
.
Overview
As noted in a comment, this is sort of like the tutorial checker SimpleStreamChecker.cpp
. However, that checker is tracking the state of multiple objects, while here the state is global to the program. That makes it more similar to BlockInCriticalSectionChecker.cpp
, so we'll imitate that one.
Whereas the tutorial checker uses a map, here we only need to keep track of a single value. I'll use an unsigned
counter:
REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledTwiceCounter, unsigned)
When we see a call to open_transaction
, increment the counter:
if (FD->getIdentifier() == II_open) {
// Update the abstract state to reflect the number of calls.
unsigned counter = state->get<CalledTwiceCounter>();
counter++;
state = state->set<CalledTwiceCounter>(counter);
C.addTransition(state);
and then report a defect if the counter exceeds 2.
Similarly, decrement it when we see close_transaction
.
Complete example
CalledTwiceChecker.cpp:
// CalledTwiceChecker.cpp
// https://stackoverflow.com/questions/48241792/clang-static-analyzer-check-if-a-function-was-called-twice
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
using namespace clang;
using namespace ento;
namespace {
class CalledTwiceChecker : public Checker< eval::Call > {
mutable IdentifierInfo *II_open, *II_close;
mutable std::unique_ptr<BuiltinBug> BT_calledTwice;
public:
CalledTwiceChecker()
: II_open(nullptr), II_close(nullptr) {}
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
};
} // end anonymous namespace
// Number of times the function of interest has been called on the
// current path. Automatically initialized to zero.
//
// Based on similar code in BlockInCriticalSectionChecker.cpp.
REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledTwiceCounter, unsigned)
bool CalledTwiceChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
const FunctionDecl *FD = C.getCalleeDecl(CE);
if (!FD || FD->getKind() != Decl::Function) {
return false;
}
ASTContext &Ctx = C.getASTContext();
if (!II_open) {
II_open = &Ctx.Idents.get("open_transaction");
}
if (!II_close) {
II_close = &Ctx.Idents.get("close_transaction");
}
ProgramStateRef state = C.getState();
if (FD->getIdentifier() == II_open) {
// Update the abstract state to reflect the number of calls.
unsigned counter = state->get<CalledTwiceCounter>();
counter++;
state = state->set<CalledTwiceCounter>(counter);
C.addTransition(state);
//llvm::errs() << "incremented counter to " << counter << "\n";
// Note: It is questionable to allow the counter to increase without
// bound in a static analysis, but the Clang SA engine seems to cap
// the number of loop iterations at 4, so this is evidently not
// immediately catastrophic.
// Possibly report a defect.
if (counter >= 2) {
ExplodedNode *N = C.generateErrorNode();
if (N) {
if (!BT_calledTwice) {
BT_calledTwice.reset(new BuiltinBug(
this, "Called twice", "open_transaction called twice."));
}
C.emitReport(llvm::make_unique<BugReport>(
*BT_calledTwice, BT_calledTwice->getDescription(), N));
}
}
return true;
}
if (FD->getIdentifier() == II_close) {
unsigned counter = state->get<CalledTwiceCounter>();
if (counter > 0) {
counter--;
state = state->set<CalledTwiceCounter>(counter);
C.addTransition(state);
return true;
}
else {
return false;
}
}
return false;
}
void ento::registerCalledTwiceChecker(CheckerManager &mgr) {
mgr.registerChecker<CalledTwiceChecker>();
}
bool ento::shouldRegisterCalledTwiceChecker(const LangOptions &LO) {
return true;
}
To hook this in to the rest of Clang, add entries to:
clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
and
clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
Example input to test it:
// calltwice.c
// Tests for CalledTwiceChecker.
void open_transaction();
void close_transaction();
void open_once()
{
open_transaction(); // not reported
}
void open_twice()
{
open_transaction();
open_transaction(); // reported
}
void open_one_each_path(int x)
{
if (x) {
open_transaction();
}
else {
open_transaction(); // not reported
}
}
void open_close_open()
{
open_transaction();
close_transaction();
open_transaction(); // not reported
}
void open_close_open_open()
{
open_transaction();
close_transaction();
open_transaction();
open_transaction(); // reported
}
int something();
void open_loop()
{
while (something()) {
open_transaction(); // reported
}
}
Analysis run on that input:
$ gcc -E -o calltwice.i calltwice.c
$ ~/bld/llvm-project/build/bin/clang -cc1 -analyze -analyzer-checker=alpha.core.CalledTwice calltwice.i
calltwice.c:15:3: warning: open_transaction called twice
open_transaction();
^~~~~~~~~~~~~~~~~~
calltwice.c:40:3: warning: open_transaction called twice
open_transaction();
^~~~~~~~~~~~~~~~~~
calltwice.c:48:5: warning: open_transaction called twice
open_transaction();
^~~~~~~~~~~~~~~~~~
3 warnings generated.