如何使用Clang的静态分析仪跟踪变量?

时间:2014-05-03 18:51:58

标签: clang static-analysis clang-static-analyzer

假设我正在使用以下C片段:

void inc(int *num) {*num++;}
void dec(int *num) {*num--;}

void f(int var) {
    inc(&var);
    dec(&var);
}

通过使用静态分析器,我希望能够判断 var 的值是否在函数执行期间没有改变。我知道我必须自己保持状态(这是编写一个Clang检查器的重点),但是我很难找到这个变量的唯一引用。

例如:如果我使用以下API

void MySimpleChecker::checkPostCall(const CallEvent &Call,
                                    CheckerContext &C) const {
    SymbolRef MyArg = Call.getArgSVal(0).getAsSymbol();
}

我希望它在我的检查器上下文中返回一个指向此符号表示的指针。但是,我总是通过这种方式使用 0 进入 MyArg pre post 回调中的 inc dec 函数都会发生这种情况。

我在这里缺少什么?我错了什么概念?

注意:我目前正在阅读Clang CFE Internals Manual,并且我已阅读了优秀的How to Write a Checker in 24 Hours资料。到目前为止,我还是找不到答案。

2 个答案:

答案 0 :(得分:1)

问题解释

具体来说,您要计算对每个变量所用的incdec的调用,并在函数中某些路径不平衡时报告。

通常,您想知道如何将抽象值(此处为数字)与程序变量相关联,并能够沿每个执行路径更新和查询该值。

高级答案

鉴于教程检查器SimpleStreamChecker.cpp将抽象值与变量存储在中的值相关联,而在这里我们希望将抽象值与变量本身相关联。这就是IteratorChecker.cpp在跟踪容器时所做的,因此我的解决方案基于此。

在静态分析器的抽象状态下,每个变量由MemRegion对象表示。因此,第一步是制作一个以MemRegion为键的地图:

REGISTER_MAP_WITH_PROGRAMSTATE(TrackVarMap, MemRegion const *, int)

接下来,当我们有一个SVal对应于指向变量的指针时,我们可以使用SVal::getAsRegion来获取相应的MemRegion。例如,给定CallEventcall,并且第一个参数是指针,我们可以执行以下操作:

    if (MemRegion const *region = call.getArgSVal(0).getAsRegion()) {

获取指针指向的region

然后,我们可以使用该region作为其键来访问我们的地图:

      state = state->set<TrackVarMap>(region, newValue);

最后,在checkDeadSymbols中,我们使用SymbolReaper::isLiveRegion来检测区域(变量)何时超出范围:

  const TrackVarMapTy &Map = state->get<TrackVarMap>();
  for (auto const &I : Map) {
    MemRegion const *region = I.first;
    int delta = I.second;
    if (SymReaper.isLiveRegion(region) || (delta==0))
      continue;              // Not dead, or unchanged; skip.

完整示例

为了演示,这里有一个完整的检查器,报告了incdec的不均衡使用:

// TrackVarChecker.cpp
// https://stackoverflow.com/questions/23448540/how-to-keep-track-of-a-variable-with-clangs-static-analyzer

#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/CallEvent.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 TrackVarChecker
  : public Checker< check::PostCall,
                    check::DeadSymbols >
{
  mutable IdentifierInfo *II_inc, *II_dec;
  mutable std::unique_ptr<BuiltinBug> BT_modified;

public:
  TrackVarChecker() : II_inc(nullptr), II_dec(nullptr) {}

  void checkPostCall(CallEvent const &Call, CheckerContext &C) const;
  void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
};
} // end anonymous namespace

// Map from memory region corresponding to a variable (that is, the
// variable itself, not its current value) to the difference between its
// current and original value.
REGISTER_MAP_WITH_PROGRAMSTATE(TrackVarMap, MemRegion const *, int)

void TrackVarChecker::checkPostCall(CallEvent const &call, CheckerContext &C) const
{
  const FunctionDecl *FD = dyn_cast<FunctionDecl>(call.getDecl());
  if (!FD || FD->getKind() != Decl::Function) {
    return;
  }

  ASTContext &Ctx = C.getASTContext();
  if (!II_inc) {
    II_inc = &Ctx.Idents.get("inc");
  }
  if (!II_dec) {
    II_dec = &Ctx.Idents.get("dec");
  }

  if (FD->getIdentifier() == II_inc || FD->getIdentifier() == II_dec) {
    // We expect the argument to be a pointer.  Get the memory region
    // that the pointer points at.
    if (MemRegion const *region = call.getArgSVal(0).getAsRegion()) {
      // Increment the associated value, creating it first if needed.
      ProgramStateRef state = C.getState();
      int delta = (FD->getIdentifier() == II_inc)? +1 : -1;
      int const *curp = state->get<TrackVarMap>(region);
      int newValue = (curp? *curp : 0) + delta;
      state = state->set<TrackVarMap>(region, newValue);
      C.addTransition(state);
    }
  }
}

void TrackVarChecker::checkDeadSymbols(
  SymbolReaper &SymReaper, CheckerContext &C) const
{
  ProgramStateRef state = C.getState();
  const TrackVarMapTy &Map = state->get<TrackVarMap>();
  for (auto const &I : Map) {
    // Check for a memory region (variable) going out of scope that has
    // a non-zero delta.
    MemRegion const *region = I.first;
    int delta = I.second;
    if (SymReaper.isLiveRegion(region) || (delta==0)) {
      continue;              // Not dead, or unchanged; skip.
    }

    //llvm::errs() << region << " dead with delta " << delta << "\n";
    if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
      if (!BT_modified) {
        BT_modified.reset(
          new BuiltinBug(this, "Delta not zero",
                         "Variable changed from its original value."));
      }
      C.emitReport(llvm::make_unique<BugReport>(
        *BT_modified, BT_modified->getDescription(), N));
    }
  }
}

void ento::registerTrackVarChecker(CheckerManager &mgr) {
  mgr.registerChecker<TrackVarChecker>();
}

bool ento::shouldRegisterTrackVarChecker(const LangOptions &LO) {
  return true;
}

要将其连接到Clang的其余部分,请在以下项中添加条目:

  • clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
  • clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt

示例输入进行测试:

// trackvar.c
// Test for TrackVarChecker.

// The behavior of these functions is hardcoded in the checker.
void inc(int *num);
void dec(int *num);

void call_inc(int var) {
  inc(&var);
} // reported

void call_inc_dec(int var) {
  inc(&var);
  dec(&var);
} // NOT reported

void if_inc(int var) {
  if (var > 2) {
    inc(&var);
  }
} // reported

void indirect_inc(int val) {
  int *p = &val;
  inc(p);
} // reported

样品运行:

$ gcc -E -o trackvar.i trackvar.c
$ ~/bld/llvm-project/build/bin/clang -cc1 -analyze -analyzer-checker=alpha.core.TrackVar trackvar.i
trackvar.c:10:1: warning: Variable changed from its original value
}
^
trackvar.c:21:1: warning: Variable changed from its original value
}
^
trackvar.c:26:1: warning: Variable changed from its original value
}
^
3 warnings generated.

答案 1 :(得分:0)

我认为您错过了此次通话事件是对您的功能inc / dec的调用的检查。你应该有像

这样的东西
void MySimpleChecker::checkPostCall(const CallEvent &Call,
                                CheckerContext &C) const {
    const IdentifierInfo* callee = Call.getCalleeIdentifier();
    if (callee->getName().str() == "inc" || callee->getName().str() == "dec")
        SymbolRef MyArg = Call.getArgSVal(0).getAsSymbol();
}