FixedAddressChecker.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This files defines FixedAddressChecker, a builtin checker that checks for
  11. // assignment of a fixed address to a pointer.
  12. // This check corresponds to CWE-587.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "InternalChecks.h"
  16. #include "clang/StaticAnalyzer/BugReporter/BugType.h"
  17. #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
  18. using namespace clang;
  19. using namespace ento;
  20. namespace {
  21. class FixedAddressChecker
  22. : public CheckerVisitor<FixedAddressChecker> {
  23. BuiltinBug *BT;
  24. public:
  25. FixedAddressChecker() : BT(0) {}
  26. static void *getTag();
  27. void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
  28. };
  29. }
  30. void *FixedAddressChecker::getTag() {
  31. static int x;
  32. return &x;
  33. }
  34. void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
  35. const BinaryOperator *B) {
  36. // Using a fixed address is not portable because that address will probably
  37. // not be valid in all environments or platforms.
  38. if (B->getOpcode() != BO_Assign)
  39. return;
  40. QualType T = B->getType();
  41. if (!T->isPointerType())
  42. return;
  43. const GRState *state = C.getState();
  44. SVal RV = state->getSVal(B->getRHS());
  45. if (!RV.isConstant() || RV.isZeroConstant())
  46. return;
  47. if (ExplodedNode *N = C.generateNode()) {
  48. if (!BT)
  49. BT = new BuiltinBug("Use fixed address",
  50. "Using a fixed address is not portable because that "
  51. "address will probably not be valid in all "
  52. "environments or platforms.");
  53. RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
  54. R->addRange(B->getRHS()->getSourceRange());
  55. C.EmitReport(R);
  56. }
  57. }
  58. void ento::RegisterFixedAddressChecker(ExprEngine &Eng) {
  59. Eng.registerCheck(new FixedAddressChecker());
  60. }