Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/DynamicExtent.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DynamicExtent.cpp - Dynamic extent related APIs ----------*- 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 file defines APIs that track and query dynamic extent information.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
14
#include "clang/AST/Expr.h"
15
#include "clang/Basic/LLVM.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
21
22
REGISTER_MAP_WITH_PROGRAMSTATE(DynamicExtentMap, const clang::ento::MemRegion *,
23
                               clang::ento::DefinedOrUnknownSVal)
24
25
namespace clang {
26
namespace ento {
27
28
DefinedOrUnknownSVal getDynamicExtent(ProgramStateRef State,
29
3.21k
                                      const MemRegion *MR, SValBuilder &SVB) {
30
3.21k
  MR = MR->StripCasts();
31
32
3.21k
  if (const DefinedOrUnknownSVal *Size = State->get<DynamicExtentMap>(MR))
33
470
    return *Size;
34
35
2.74k
  return MR->getMemRegionManager().getStaticSize(MR, SVB);
36
3.21k
}
37
38
27.4k
DefinedOrUnknownSVal getElementExtent(QualType Ty, SValBuilder &SVB) {
39
27.4k
  return SVB.makeIntVal(SVB.getContext().getTypeSizeInChars(Ty).getQuantity(),
40
27.4k
                        SVB.getArrayIndexType());
41
27.4k
}
42
43
DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State,
44
                                            const MemRegion *MR,
45
                                            SValBuilder &SVB,
46
448
                                            QualType ElementTy) {
47
448
  MR = MR->StripCasts();
48
49
448
  DefinedOrUnknownSVal Size = getDynamicExtent(State, MR, SVB);
50
448
  SVal ElementSize = getElementExtent(ElementTy, SVB);
51
52
448
  SVal ElementCount =
53
448
      SVB.evalBinOp(State, BO_Div, Size, ElementSize, SVB.getArrayIndexType());
54
55
448
  return ElementCount.castAs<DefinedOrUnknownSVal>();
56
448
}
57
58
125
SVal getDynamicExtentWithOffset(ProgramStateRef State, SVal BufV) {
59
125
  SValBuilder &SvalBuilder = State->getStateManager().getSValBuilder();
60
125
  const MemRegion *MRegion = BufV.getAsRegion();
61
125
  if (!MRegion)
62
0
    return UnknownVal();
63
125
  RegionOffset Offset = MRegion->getAsOffset();
64
125
  if (Offset.hasSymbolicOffset())
65
0
    return UnknownVal();
66
125
  const MemRegion *BaseRegion = MRegion->getBaseRegion();
67
125
  if (!BaseRegion)
68
0
    return UnknownVal();
69
70
125
  NonLoc OffsetInBytes = SvalBuilder.makeArrayIndex(
71
125
      Offset.getOffset() /
72
125
      MRegion->getMemRegionManager().getContext().getCharWidth());
73
125
  DefinedOrUnknownSVal ExtentInBytes =
74
125
      getDynamicExtent(State, BaseRegion, SvalBuilder);
75
76
125
  return SvalBuilder.evalBinOp(State, BinaryOperator::Opcode::BO_Sub,
77
125
                               ExtentInBytes, OffsetInBytes,
78
125
                               SvalBuilder.getArrayIndexType());
79
125
}
80
81
ProgramStateRef setDynamicExtent(ProgramStateRef State, const MemRegion *MR,
82
1.71k
                                 DefinedOrUnknownSVal Size, SValBuilder &SVB) {
83
1.71k
  MR = MR->StripCasts();
84
85
1.71k
  if (Size.isUnknown())
86
32
    return State;
87
88
1.68k
  return State->set<DynamicExtentMap>(MR->StripCasts(), Size);
89
1.71k
}
90
91
} // namespace ento
92
} // namespace clang