Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This checker evaluates clang builtin functions.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/Builtins.h"
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/StaticAnalyzer/Core/Checker.h"
16
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h"
20
21
using namespace clang;
22
using namespace ento;
23
24
namespace {
25
26
class BuiltinFunctionChecker : public Checker<eval::Call> {
27
public:
28
  bool evalCall(const CallEvent &Call, CheckerContext &C) const;
29
};
30
31
}
32
33
bool BuiltinFunctionChecker::evalCall(const CallEvent &Call,
34
76.9k
                                      CheckerContext &C) const {
35
76.9k
  ProgramStateRef state = C.getState();
36
76.9k
  const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
37
76.9k
  if (!FD)
38
205
    return false;
39
40
76.7k
  const LocationContext *LCtx = C.getLocationContext();
41
76.7k
  const Expr *CE = Call.getOriginExpr();
42
43
76.7k
  switch (FD->getBuiltinID()) {
44
76.5k
  default:
45
76.5k
    return false;
46
47
7
  case Builtin::BI__builtin_assume: {
48
7
    assert (Call.getNumArgs() > 0);
49
7
    SVal Arg = Call.getArgSVal(0);
50
7
    if (Arg.isUndef())
51
0
      return true; // Return true to model purity.
52
53
7
    state = state->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
54
    // FIXME: do we want to warn here? Not right now. The most reports might
55
    // come from infeasible paths, thus being false positives.
56
7
    if (!state) {
57
2
      C.generateSink(C.getState(), C.getPredecessor());
58
2
      return true;
59
2
    }
60
61
5
    C.addTransition(state);
62
5
    return true;
63
5
  }
64
65
26
  case Builtin::BI__builtin_unpredictable:
66
26
  case Builtin::BI__builtin_expect:
67
26
  case Builtin::BI__builtin_expect_with_probability:
68
26
  case Builtin::BI__builtin_assume_aligned:
69
26
  case Builtin::BI__builtin_addressof: {
70
    // For __builtin_unpredictable, __builtin_expect,
71
    // __builtin_expect_with_probability and __builtin_assume_aligned,
72
    // just return the value of the subexpression.
73
    // __builtin_addressof is going from a reference to a pointer, but those
74
    // are represented the same way in the analyzer.
75
26
    assert (Call.getNumArgs() > 0);
76
26
    SVal Arg = Call.getArgSVal(0);
77
26
    C.addTransition(state->BindExpr(CE, LCtx, Arg));
78
26
    return true;
79
26
  }
80
81
87
  case Builtin::BI__builtin_alloca_with_align:
82
87
  case Builtin::BI__builtin_alloca: {
83
    // FIXME: Refactor into StoreManager itself?
84
87
    MemRegionManager& RM = C.getStoreManager().getRegionManager();
85
87
    const AllocaRegion* R =
86
87
      RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
87
88
    // Set the extent of the region in bytes. This enables us to use the
89
    // SVal of the argument directly. If we save the extent in bits, we
90
    // cannot represent values like symbol*8.
91
87
    auto Size = Call.getArgSVal(0);
92
87
    if (Size.isUndef())
93
0
      return true; // Return true to model purity.
94
95
87
    SValBuilder& svalBuilder = C.getSValBuilder();
96
87
    DefinedOrUnknownSVal DynSize = getDynamicSize(state, R, svalBuilder);
97
87
    DefinedOrUnknownSVal DynSizeMatchesSizeArg =
98
87
        svalBuilder.evalEQ(state, DynSize, Size.castAs<DefinedOrUnknownSVal>());
99
87
    state = state->assume(DynSizeMatchesSizeArg, true);
100
87
    assert(state && "The region should not have any previous constraints");
101
102
87
    C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
103
87
    return true;
104
87
  }
105
106
29
  case Builtin::BI__builtin_dynamic_object_size:
107
29
  case Builtin::BI__builtin_object_size:
108
29
  case Builtin::BI__builtin_constant_p: {
109
    // This must be resolvable at compile time, so we defer to the constant
110
    // evaluator for a value.
111
29
    SValBuilder &SVB = C.getSValBuilder();
112
29
    SVal V = UnknownVal();
113
29
    Expr::EvalResult EVResult;
114
29
    if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) {
115
      // Make sure the result has the correct type.
116
22
      llvm::APSInt Result = EVResult.Val.getInt();
117
22
      BasicValueFactory &BVF = SVB.getBasicValueFactory();
118
22
      BVF.getAPSIntType(CE->getType()).apply(Result);
119
22
      V = SVB.makeIntVal(Result);
120
22
    }
121
122
29
    if (FD->getBuiltinID() == Builtin::BI__builtin_constant_p) {
123
      // If we didn't manage to figure out if the value is constant or not,
124
      // it is safe to assume that it's not constant and unsafe to assume
125
      // that it's constant.
126
13
      if (V.isUnknown())
127
5
        V = SVB.makeIntVal(0, CE->getType());
128
13
    }
129
130
29
    C.addTransition(state->BindExpr(CE, LCtx, V));
131
29
    return true;
132
29
  }
133
76.7k
  }
134
76.7k
}
135
136
1.08k
void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
137
1.08k
  mgr.registerChecker<BuiltinFunctionChecker>();
138
1.08k
}
139
140
2.16k
bool ento::shouldRegisterBuiltinFunctionChecker(const CheckerManager &mgr) {
141
2.16k
  return true;
142
2.16k
}