/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- MemRegion.cpp - Abstract memory regions for static analysis --------===// |
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 MemRegion and its subclasses. MemRegion defines a |
10 | | // partially-typed abstraction of memory useful for path-sensitive dataflow |
11 | | // analyses. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/Attr.h" |
18 | | #include "clang/AST/CharUnits.h" |
19 | | #include "clang/AST/Decl.h" |
20 | | #include "clang/AST/DeclCXX.h" |
21 | | #include "clang/AST/DeclObjC.h" |
22 | | #include "clang/AST/Expr.h" |
23 | | #include "clang/AST/PrettyPrinter.h" |
24 | | #include "clang/AST/RecordLayout.h" |
25 | | #include "clang/AST/Type.h" |
26 | | #include "clang/Analysis/AnalysisDeclContext.h" |
27 | | #include "clang/Analysis/Support/BumpVector.h" |
28 | | #include "clang/Basic/IdentifierTable.h" |
29 | | #include "clang/Basic/LLVM.h" |
30 | | #include "clang/Basic/SourceManager.h" |
31 | | #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" |
32 | | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
33 | | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
34 | | #include "llvm/ADT/APInt.h" |
35 | | #include "llvm/ADT/FoldingSet.h" |
36 | | #include "llvm/ADT/Optional.h" |
37 | | #include "llvm/ADT/PointerUnion.h" |
38 | | #include "llvm/ADT/SmallString.h" |
39 | | #include "llvm/ADT/StringRef.h" |
40 | | #include "llvm/ADT/Twine.h" |
41 | | #include "llvm/Support/Allocator.h" |
42 | | #include "llvm/Support/Casting.h" |
43 | | #include "llvm/Support/CheckedArithmetic.h" |
44 | | #include "llvm/Support/Compiler.h" |
45 | | #include "llvm/Support/Debug.h" |
46 | | #include "llvm/Support/ErrorHandling.h" |
47 | | #include "llvm/Support/raw_ostream.h" |
48 | | #include <cassert> |
49 | | #include <cstdint> |
50 | | #include <functional> |
51 | | #include <iterator> |
52 | | #include <string> |
53 | | #include <tuple> |
54 | | #include <utility> |
55 | | |
56 | | using namespace clang; |
57 | | using namespace ento; |
58 | | |
59 | | #define DEBUG_TYPE "MemRegion" |
60 | | |
61 | | //===----------------------------------------------------------------------===// |
62 | | // MemRegion Construction. |
63 | | //===----------------------------------------------------------------------===// |
64 | | |
65 | | template <typename RegionTy, typename SuperTy, typename Arg1Ty> |
66 | | RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, |
67 | 508k | const SuperTy *superRegion) { |
68 | 508k | llvm::FoldingSetNodeID ID; |
69 | 508k | RegionTy::ProfileRegion(ID, arg1, superRegion); |
70 | 508k | void *InsertPos; |
71 | 508k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); |
72 | | |
73 | 508k | if (!R) { |
74 | 109k | R = A.Allocate<RegionTy>(); |
75 | 109k | new (R) RegionTy(arg1, superRegion); |
76 | 109k | Regions.InsertNode(R, InsertPos); |
77 | 109k | } |
78 | | |
79 | 508k | return R; |
80 | 508k | } clang::ento::StringRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::StringRegion, clang::ento::GlobalInternalSpaceRegion, clang::StringLiteral const*>(clang::StringLiteral const*, clang::ento::GlobalInternalSpaceRegion const*) Line | Count | Source | 67 | 8.78k | const SuperTy *superRegion) { | 68 | 8.78k | llvm::FoldingSetNodeID ID; | 69 | 8.78k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 8.78k | void *InsertPos; | 71 | 8.78k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 8.78k | if (!R) { | 74 | 3.87k | R = A.Allocate<RegionTy>(); | 75 | 3.87k | new (R) RegionTy(arg1, superRegion); | 76 | 3.87k | Regions.InsertNode(R, InsertPos); | 77 | 3.87k | } | 78 | | | 79 | 8.78k | return R; | 80 | 8.78k | } |
clang::ento::ObjCStringRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ObjCStringRegion, clang::ento::GlobalInternalSpaceRegion, clang::ObjCStringLiteral const*>(clang::ObjCStringLiteral const*, clang::ento::GlobalInternalSpaceRegion const*) Line | Count | Source | 67 | 1.11k | const SuperTy *superRegion) { | 68 | 1.11k | llvm::FoldingSetNodeID ID; | 69 | 1.11k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 1.11k | void *InsertPos; | 71 | 1.11k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 1.11k | if (!R) { | 74 | 285 | R = A.Allocate<RegionTy>(); | 75 | 285 | new (R) RegionTy(arg1, superRegion); | 76 | 285 | Regions.InsertNode(R, InsertPos); | 77 | 285 | } | 78 | | | 79 | 1.11k | return R; | 80 | 1.11k | } |
clang::ento::NonParamVarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::NonParamVarRegion, clang::ento::MemRegion, clang::VarDecl const*>(clang::VarDecl const*, clang::ento::MemRegion const*) Line | Count | Source | 67 | 171k | const SuperTy *superRegion) { | 68 | 171k | llvm::FoldingSetNodeID ID; | 69 | 171k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 171k | void *InsertPos; | 71 | 171k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 171k | if (!R) { | 74 | 28.6k | R = A.Allocate<RegionTy>(); | 75 | 28.6k | new (R) RegionTy(arg1, superRegion); | 76 | 28.6k | Regions.InsertNode(R, InsertPos); | 77 | 28.6k | } | 78 | | | 79 | 171k | return R; | 80 | 171k | } |
clang::ento::CXXTempObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXTempObjectRegion, clang::ento::GlobalsSpaceRegion, clang::Expr const*>(clang::Expr const*, clang::ento::GlobalsSpaceRegion const*) Line | Count | Source | 67 | 15 | const SuperTy *superRegion) { | 68 | 15 | llvm::FoldingSetNodeID ID; | 69 | 15 | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 15 | void *InsertPos; | 71 | 15 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 15 | if (!R) { | 74 | 15 | R = A.Allocate<RegionTy>(); | 75 | 15 | new (R) RegionTy(arg1, superRegion); | 76 | 15 | Regions.InsertNode(R, InsertPos); | 77 | 15 | } | 78 | | | 79 | 15 | return R; | 80 | 15 | } |
clang::ento::CompoundLiteralRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CompoundLiteralRegion, clang::ento::MemSpaceRegion, clang::CompoundLiteralExpr const*>(clang::CompoundLiteralExpr const*, clang::ento::MemSpaceRegion const*) Line | Count | Source | 67 | 56 | const SuperTy *superRegion) { | 68 | 56 | llvm::FoldingSetNodeID ID; | 69 | 56 | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 56 | void *InsertPos; | 71 | 56 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 56 | if (!R) { | 74 | 52 | R = A.Allocate<RegionTy>(); | 75 | 52 | new (R) RegionTy(arg1, superRegion); | 76 | 52 | Regions.InsertNode(R, InsertPos); | 77 | 52 | } | 78 | | | 79 | 56 | return R; | 80 | 56 | } |
clang::ento::FunctionCodeRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::FunctionCodeRegion, clang::ento::CodeSpaceRegion, clang::NamedDecl const*>(clang::NamedDecl const*, clang::ento::CodeSpaceRegion const*) Line | Count | Source | 67 | 69.5k | const SuperTy *superRegion) { | 68 | 69.5k | llvm::FoldingSetNodeID ID; | 69 | 69.5k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 69.5k | void *InsertPos; | 71 | 69.5k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 69.5k | if (!R) { | 74 | 25.1k | R = A.Allocate<RegionTy>(); | 75 | 25.1k | new (R) RegionTy(arg1, superRegion); | 76 | 25.1k | Regions.InsertNode(R, InsertPos); | 77 | 25.1k | } | 78 | | | 79 | 69.5k | return R; | 80 | 69.5k | } |
clang::ento::SymbolicRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::SymbolicRegion, clang::ento::UnknownSpaceRegion, clang::ento::SymExpr const*>(clang::ento::SymExpr const*, clang::ento::UnknownSpaceRegion const*) Line | Count | Source | 67 | 65.5k | const SuperTy *superRegion) { | 68 | 65.5k | llvm::FoldingSetNodeID ID; | 69 | 65.5k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 65.5k | void *InsertPos; | 71 | 65.5k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 65.5k | if (!R) { | 74 | 16.0k | R = A.Allocate<RegionTy>(); | 75 | 16.0k | new (R) RegionTy(arg1, superRegion); | 76 | 16.0k | Regions.InsertNode(R, InsertPos); | 77 | 16.0k | } | 78 | | | 79 | 65.5k | return R; | 80 | 65.5k | } |
clang::ento::SymbolicRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::SymbolicRegion, clang::ento::HeapSpaceRegion, clang::ento::SymExpr const*>(clang::ento::SymExpr const*, clang::ento::HeapSpaceRegion const*) Line | Count | Source | 67 | 1.36k | const SuperTy *superRegion) { | 68 | 1.36k | llvm::FoldingSetNodeID ID; | 69 | 1.36k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 1.36k | void *InsertPos; | 71 | 1.36k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 1.36k | if (!R) { | 74 | 1.34k | R = A.Allocate<RegionTy>(); | 75 | 1.34k | new (R) RegionTy(arg1, superRegion); | 76 | 1.34k | Regions.InsertNode(R, InsertPos); | 77 | 1.34k | } | 78 | | | 79 | 1.36k | return R; | 80 | 1.36k | } |
clang::ento::FieldRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::FieldRegion, clang::ento::SubRegion, clang::FieldDecl const*>(clang::FieldDecl const*, clang::ento::SubRegion const*) Line | Count | Source | 67 | 109k | const SuperTy *superRegion) { | 68 | 109k | llvm::FoldingSetNodeID ID; | 69 | 109k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 109k | void *InsertPos; | 71 | 109k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 109k | if (!R) { | 74 | 16.6k | R = A.Allocate<RegionTy>(); | 75 | 16.6k | new (R) RegionTy(arg1, superRegion); | 76 | 16.6k | Regions.InsertNode(R, InsertPos); | 77 | 16.6k | } | 78 | | | 79 | 109k | return R; | 80 | 109k | } |
clang::ento::ObjCIvarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ObjCIvarRegion, clang::ento::SubRegion, clang::ObjCIvarDecl const*>(clang::ObjCIvarDecl const*, clang::ento::SubRegion const*) Line | Count | Source | 67 | 1.45k | const SuperTy *superRegion) { | 68 | 1.45k | llvm::FoldingSetNodeID ID; | 69 | 1.45k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 1.45k | void *InsertPos; | 71 | 1.45k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 1.45k | if (!R) { | 74 | 660 | R = A.Allocate<RegionTy>(); | 75 | 660 | new (R) RegionTy(arg1, superRegion); | 76 | 660 | Regions.InsertNode(R, InsertPos); | 77 | 660 | } | 78 | | | 79 | 1.45k | return R; | 80 | 1.45k | } |
clang::ento::CXXTempObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXTempObjectRegion, clang::ento::StackLocalsSpaceRegion, clang::Expr const*>(clang::Expr const*, clang::ento::StackLocalsSpaceRegion const*) Line | Count | Source | 67 | 3.66k | const SuperTy *superRegion) { | 68 | 3.66k | llvm::FoldingSetNodeID ID; | 69 | 3.66k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 3.66k | void *InsertPos; | 71 | 3.66k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 3.66k | if (!R) { | 74 | 2.79k | R = A.Allocate<RegionTy>(); | 75 | 2.79k | new (R) RegionTy(arg1, superRegion); | 76 | 2.79k | Regions.InsertNode(R, InsertPos); | 77 | 2.79k | } | 78 | | | 79 | 3.66k | return R; | 80 | 3.66k | } |
clang::ento::CXXDerivedObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXDerivedObjectRegion, clang::ento::SubRegion, clang::CXXRecordDecl const*>(clang::CXXRecordDecl const*, clang::ento::SubRegion const*) Line | Count | Source | 67 | 21 | const SuperTy *superRegion) { | 68 | 21 | llvm::FoldingSetNodeID ID; | 69 | 21 | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 21 | void *InsertPos; | 71 | 21 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 21 | if (!R) { | 74 | 20 | R = A.Allocate<RegionTy>(); | 75 | 20 | new (R) RegionTy(arg1, superRegion); | 76 | 20 | Regions.InsertNode(R, InsertPos); | 77 | 20 | } | 78 | | | 79 | 21 | return R; | 80 | 21 | } |
clang::ento::CXXThisRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXThisRegion, clang::ento::StackArgumentsSpaceRegion, clang::PointerType const*>(clang::PointerType const*, clang::ento::StackArgumentsSpaceRegion const*) Line | Count | Source | 67 | 76.4k | const SuperTy *superRegion) { | 68 | 76.4k | llvm::FoldingSetNodeID ID; | 69 | 76.4k | RegionTy::ProfileRegion(ID, arg1, superRegion); | 70 | 76.4k | void *InsertPos; | 71 | 76.4k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 72 | | | 73 | 76.4k | if (!R) { | 74 | 13.9k | R = A.Allocate<RegionTy>(); | 75 | 13.9k | new (R) RegionTy(arg1, superRegion); | 76 | 13.9k | Regions.InsertNode(R, InsertPos); | 77 | 13.9k | } | 78 | | | 79 | 76.4k | return R; | 80 | 76.4k | } |
|
81 | | |
82 | | template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty> |
83 | | RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2, |
84 | 85.6k | const SuperTy *superRegion) { |
85 | 85.6k | llvm::FoldingSetNodeID ID; |
86 | 85.6k | RegionTy::ProfileRegion(ID, arg1, arg2, superRegion); |
87 | 85.6k | void *InsertPos; |
88 | 85.6k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); |
89 | | |
90 | 85.6k | if (!R) { |
91 | 19.9k | R = A.Allocate<RegionTy>(); |
92 | 19.9k | new (R) RegionTy(arg1, arg2, superRegion); |
93 | 19.9k | Regions.InsertNode(R, InsertPos); |
94 | 19.9k | } |
95 | | |
96 | 85.6k | return R; |
97 | 85.6k | } clang::ento::ParamVarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ParamVarRegion, clang::ento::StackArgumentsSpaceRegion, clang::Expr const*, unsigned int>(clang::Expr const*, unsigned int, clang::ento::StackArgumentsSpaceRegion const*) Line | Count | Source | 84 | 83.5k | const SuperTy *superRegion) { | 85 | 83.5k | llvm::FoldingSetNodeID ID; | 86 | 83.5k | RegionTy::ProfileRegion(ID, arg1, arg2, superRegion); | 87 | 83.5k | void *InsertPos; | 88 | 83.5k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 89 | | | 90 | 83.5k | if (!R) { | 91 | 18.8k | R = A.Allocate<RegionTy>(); | 92 | 18.8k | new (R) RegionTy(arg1, arg2, superRegion); | 93 | 18.8k | Regions.InsertNode(R, InsertPos); | 94 | 18.8k | } | 95 | | | 96 | 83.5k | return R; | 97 | 83.5k | } |
clang::ento::CXXBaseObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXBaseObjectRegion, clang::ento::SubRegion, clang::CXXRecordDecl const*, bool>(clang::CXXRecordDecl const*, bool, clang::ento::SubRegion const*) Line | Count | Source | 84 | 1.99k | const SuperTy *superRegion) { | 85 | 1.99k | llvm::FoldingSetNodeID ID; | 86 | 1.99k | RegionTy::ProfileRegion(ID, arg1, arg2, superRegion); | 87 | 1.99k | void *InsertPos; | 88 | 1.99k | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 89 | | | 90 | 1.99k | if (!R) { | 91 | 1.06k | R = A.Allocate<RegionTy>(); | 92 | 1.06k | new (R) RegionTy(arg1, arg2, superRegion); | 93 | 1.06k | Regions.InsertNode(R, InsertPos); | 94 | 1.06k | } | 95 | | | 96 | 1.99k | return R; | 97 | 1.99k | } |
clang::ento::AllocaRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::AllocaRegion, clang::ento::StackLocalsSpaceRegion, clang::Expr const*, unsigned int>(clang::Expr const*, unsigned int, clang::ento::StackLocalsSpaceRegion const*) Line | Count | Source | 84 | 91 | const SuperTy *superRegion) { | 85 | 91 | llvm::FoldingSetNodeID ID; | 86 | 91 | RegionTy::ProfileRegion(ID, arg1, arg2, superRegion); | 87 | 91 | void *InsertPos; | 88 | 91 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 89 | | | 90 | 91 | if (!R) { | 91 | 91 | R = A.Allocate<RegionTy>(); | 92 | 91 | new (R) RegionTy(arg1, arg2, superRegion); | 93 | 91 | Regions.InsertNode(R, InsertPos); | 94 | 91 | } | 95 | | | 96 | 91 | return R; | 97 | 91 | } |
|
98 | | |
99 | | template <typename RegionTy, typename SuperTy, |
100 | | typename Arg1Ty, typename Arg2Ty, typename Arg3Ty> |
101 | | RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2, |
102 | | const Arg3Ty arg3, |
103 | 781 | const SuperTy *superRegion) { |
104 | 781 | llvm::FoldingSetNodeID ID; |
105 | 781 | RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion); |
106 | 781 | void *InsertPos; |
107 | 781 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); |
108 | | |
109 | 781 | if (!R) { |
110 | 733 | R = A.Allocate<RegionTy>(); |
111 | 733 | new (R) RegionTy(arg1, arg2, arg3, superRegion); |
112 | 733 | Regions.InsertNode(R, InsertPos); |
113 | 733 | } |
114 | | |
115 | 781 | return R; |
116 | 781 | } clang::ento::BlockDataRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::BlockDataRegion, clang::ento::MemSpaceRegion, clang::ento::BlockCodeRegion const*, clang::LocationContext const*, unsigned int>(clang::ento::BlockCodeRegion const*, clang::LocationContext const*, unsigned int, clang::ento::MemSpaceRegion const*) Line | Count | Source | 103 | 384 | const SuperTy *superRegion) { | 104 | 384 | llvm::FoldingSetNodeID ID; | 105 | 384 | RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion); | 106 | 384 | void *InsertPos; | 107 | 384 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 108 | | | 109 | 384 | if (!R) { | 110 | 370 | R = A.Allocate<RegionTy>(); | 111 | 370 | new (R) RegionTy(arg1, arg2, arg3, superRegion); | 112 | 370 | Regions.InsertNode(R, InsertPos); | 113 | 370 | } | 114 | | | 115 | 384 | return R; | 116 | 384 | } |
clang::ento::BlockCodeRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::BlockCodeRegion, clang::ento::CodeSpaceRegion, clang::BlockDecl const*, clang::CanQual<clang::Type>, clang::AnalysisDeclContext*>(clang::BlockDecl const*, clang::CanQual<clang::Type>, clang::AnalysisDeclContext*, clang::ento::CodeSpaceRegion const*) Line | Count | Source | 103 | 397 | const SuperTy *superRegion) { | 104 | 397 | llvm::FoldingSetNodeID ID; | 105 | 397 | RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion); | 106 | 397 | void *InsertPos; | 107 | 397 | auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos)); | 108 | | | 109 | 397 | if (!R) { | 110 | 363 | R = A.Allocate<RegionTy>(); | 111 | 363 | new (R) RegionTy(arg1, arg2, arg3, superRegion); | 112 | 363 | Regions.InsertNode(R, InsertPos); | 113 | 363 | } | 114 | | | 115 | 397 | return R; | 116 | 397 | } |
|
117 | | |
118 | | //===----------------------------------------------------------------------===// |
119 | | // Object destruction. |
120 | | //===----------------------------------------------------------------------===// |
121 | | |
122 | 0 | MemRegion::~MemRegion() = default; |
123 | | |
124 | | // All regions and their data are BumpPtrAllocated. No need to call their |
125 | | // destructors. |
126 | 13.8k | MemRegionManager::~MemRegionManager() = default; |
127 | | |
128 | | //===----------------------------------------------------------------------===// |
129 | | // Basic methods. |
130 | | //===----------------------------------------------------------------------===// |
131 | | |
132 | 6.73k | bool SubRegion::isSubRegionOf(const MemRegion* R) const { |
133 | 6.73k | const MemRegion* r = this; |
134 | 11.7k | do { |
135 | 11.7k | if (r == R) |
136 | 2.88k | return true; |
137 | 8.88k | if (const auto *sr = dyn_cast<SubRegion>(r)) |
138 | 5.03k | r = sr->getSuperRegion(); |
139 | 3.84k | else |
140 | 3.84k | break; |
141 | 5.03k | } while (r != nullptr); |
142 | 3.84k | return false; |
143 | 6.73k | } |
144 | | |
145 | 660k | MemRegionManager &SubRegion::getMemRegionManager() const { |
146 | 660k | const SubRegion* r = this; |
147 | 906k | do { |
148 | 906k | const MemRegion *superRegion = r->getSuperRegion(); |
149 | 906k | if (const auto *sr = dyn_cast<SubRegion>(superRegion)) { |
150 | 245k | r = sr; |
151 | 245k | continue; |
152 | 245k | } |
153 | 660k | return superRegion->getMemRegionManager(); |
154 | 245k | } while (true); |
155 | 660k | } |
156 | | |
157 | 1.69M | const StackFrameContext *VarRegion::getStackFrame() const { |
158 | 1.69M | const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace()); |
159 | 1.69M | return SSR ? SSR->getStackFrame()1.60M : nullptr87.6k ; |
160 | 1.69M | } |
161 | | |
162 | | ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg) |
163 | 660 | : DeclRegion(sReg, ObjCIvarRegionKind), IVD(ivd) {} |
164 | | |
165 | 17.7k | const ObjCIvarDecl *ObjCIvarRegion::getDecl() const { return IVD; } |
166 | | |
167 | 15.7k | QualType ObjCIvarRegion::getValueType() const { |
168 | 15.7k | return getDecl()->getType(); |
169 | 15.7k | } |
170 | | |
171 | 6.83k | QualType CXXBaseObjectRegion::getValueType() const { |
172 | 6.83k | return QualType(getDecl()->getTypeForDecl(), 0); |
173 | 6.83k | } |
174 | | |
175 | 41 | QualType CXXDerivedObjectRegion::getValueType() const { |
176 | 41 | return QualType(getDecl()->getTypeForDecl(), 0); |
177 | 41 | } |
178 | | |
179 | 346k | QualType ParamVarRegion::getValueType() const { |
180 | 346k | assert(getDecl() && |
181 | 346k | "`ParamVarRegion` support functions without `Decl` not implemented" |
182 | 346k | " yet."); |
183 | 0 | return getDecl()->getType(); |
184 | 346k | } |
185 | | |
186 | 795k | const ParmVarDecl *ParamVarRegion::getDecl() const { |
187 | 795k | const Decl *D = getStackFrame()->getDecl(); |
188 | | |
189 | 795k | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
190 | 791k | assert(Index < FD->param_size()); |
191 | 0 | return FD->parameters()[Index]; |
192 | 4.08k | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
193 | 1.31k | assert(Index < BD->param_size()); |
194 | 0 | return BD->parameters()[Index]; |
195 | 2.77k | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
196 | 2.77k | assert(Index < MD->param_size()); |
197 | 0 | return MD->parameters()[Index]; |
198 | 0 | } else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) { |
199 | 0 | assert(Index < CD->param_size()); |
200 | 0 | return CD->parameters()[Index]; |
201 | 0 | } else { |
202 | 0 | llvm_unreachable("Unexpected Decl kind!"); |
203 | 0 | } |
204 | 795k | } |
205 | | |
206 | | //===----------------------------------------------------------------------===// |
207 | | // FoldingSet profiling. |
208 | | //===----------------------------------------------------------------------===// |
209 | | |
210 | 0 | void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
211 | 0 | ID.AddInteger(static_cast<unsigned>(getKind())); |
212 | 0 | } |
213 | | |
214 | 0 | void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
215 | 0 | ID.AddInteger(static_cast<unsigned>(getKind())); |
216 | 0 | ID.AddPointer(getStackFrame()); |
217 | 0 | } |
218 | | |
219 | 0 | void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
220 | 0 | ID.AddInteger(static_cast<unsigned>(getKind())); |
221 | 0 | ID.AddPointer(getCodeRegion()); |
222 | 0 | } |
223 | | |
224 | | void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
225 | | const StringLiteral *Str, |
226 | 14.8k | const MemRegion *superRegion) { |
227 | 14.8k | ID.AddInteger(static_cast<unsigned>(StringRegionKind)); |
228 | 14.8k | ID.AddPointer(Str); |
229 | 14.8k | ID.AddPointer(superRegion); |
230 | 14.8k | } |
231 | | |
232 | | void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
233 | | const ObjCStringLiteral *Str, |
234 | 2.03k | const MemRegion *superRegion) { |
235 | 2.03k | ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind)); |
236 | 2.03k | ID.AddPointer(Str); |
237 | 2.03k | ID.AddPointer(superRegion); |
238 | 2.03k | } |
239 | | |
240 | | void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
241 | | const Expr *Ex, unsigned cnt, |
242 | 227 | const MemRegion *superRegion) { |
243 | 227 | ID.AddInteger(static_cast<unsigned>(AllocaRegionKind)); |
244 | 227 | ID.AddPointer(Ex); |
245 | 227 | ID.AddInteger(cnt); |
246 | 227 | ID.AddPointer(superRegion); |
247 | 227 | } |
248 | | |
249 | 136 | void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
250 | 136 | ProfileRegion(ID, Ex, Cnt, superRegion); |
251 | 136 | } |
252 | | |
253 | 9 | void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
254 | 9 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
255 | 9 | } |
256 | | |
257 | | void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
258 | | const CompoundLiteralExpr *CL, |
259 | 65 | const MemRegion* superRegion) { |
260 | 65 | ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind)); |
261 | 65 | ID.AddPointer(CL); |
262 | 65 | ID.AddPointer(superRegion); |
263 | 65 | } |
264 | | |
265 | | void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
266 | | const PointerType *PT, |
267 | 155k | const MemRegion *sRegion) { |
268 | 155k | ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind)); |
269 | 155k | ID.AddPointer(PT); |
270 | 155k | ID.AddPointer(sRegion); |
271 | 155k | } |
272 | | |
273 | 79.4k | void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
274 | 79.4k | CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion); |
275 | 79.4k | } |
276 | | |
277 | 152k | void FieldRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
278 | 152k | ProfileRegion(ID, getDecl(), superRegion); |
279 | 152k | } |
280 | | |
281 | | void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
282 | | const ObjCIvarDecl *ivd, |
283 | 2.44k | const MemRegion* superRegion) { |
284 | 2.44k | ID.AddInteger(static_cast<unsigned>(ObjCIvarRegionKind)); |
285 | 2.44k | ID.AddPointer(ivd); |
286 | 2.44k | ID.AddPointer(superRegion); |
287 | 2.44k | } |
288 | | |
289 | 985 | void ObjCIvarRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
290 | 985 | ProfileRegion(ID, getDecl(), superRegion); |
291 | 985 | } |
292 | | |
293 | | void NonParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
294 | | const VarDecl *VD, |
295 | 330k | const MemRegion *superRegion) { |
296 | 330k | ID.AddInteger(static_cast<unsigned>(NonParamVarRegionKind)); |
297 | 330k | ID.AddPointer(VD); |
298 | 330k | ID.AddPointer(superRegion); |
299 | 330k | } |
300 | | |
301 | 158k | void NonParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
302 | 158k | ProfileRegion(ID, getDecl(), superRegion); |
303 | 158k | } |
304 | | |
305 | | void ParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, const Expr *OE, |
306 | 203k | unsigned Idx, const MemRegion *SReg) { |
307 | 203k | ID.AddInteger(static_cast<unsigned>(ParamVarRegionKind)); |
308 | 203k | ID.AddPointer(OE); |
309 | 203k | ID.AddInteger(Idx); |
310 | 203k | ID.AddPointer(SReg); |
311 | 203k | } |
312 | | |
313 | 120k | void ParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
314 | 120k | ProfileRegion(ID, getOriginExpr(), getIndex(), superRegion); |
315 | 120k | } |
316 | | |
317 | | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym, |
318 | 128k | const MemRegion *sreg) { |
319 | 128k | ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind)); |
320 | 128k | ID.Add(sym); |
321 | 128k | ID.AddPointer(sreg); |
322 | 128k | } |
323 | | |
324 | 61.3k | void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
325 | 61.3k | SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion()); |
326 | 61.3k | } |
327 | | |
328 | | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
329 | | QualType ElementType, SVal Idx, |
330 | 151k | const MemRegion* superRegion) { |
331 | 151k | ID.AddInteger(MemRegion::ElementRegionKind); |
332 | 151k | ID.Add(ElementType); |
333 | 151k | ID.AddPointer(superRegion); |
334 | 151k | Idx.Profile(ID); |
335 | 151k | } |
336 | | |
337 | 104k | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
338 | 104k | ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion); |
339 | 104k | } |
340 | | |
341 | | void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
342 | | const NamedDecl *FD, |
343 | 133k | const MemRegion*) { |
344 | 133k | ID.AddInteger(MemRegion::FunctionCodeRegionKind); |
345 | 133k | ID.AddPointer(FD); |
346 | 133k | } |
347 | | |
348 | 63.9k | void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
349 | 63.9k | FunctionCodeRegion::ProfileRegion(ID, FD, superRegion); |
350 | 63.9k | } |
351 | | |
352 | | void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
353 | | const BlockDecl *BD, CanQualType, |
354 | | const AnalysisDeclContext *AC, |
355 | 466 | const MemRegion*) { |
356 | 466 | ID.AddInteger(MemRegion::BlockCodeRegionKind); |
357 | 466 | ID.AddPointer(BD); |
358 | 466 | } |
359 | | |
360 | 69 | void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
361 | 69 | BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion); |
362 | 69 | } |
363 | | |
364 | | void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
365 | | const BlockCodeRegion *BC, |
366 | | const LocationContext *LC, |
367 | | unsigned BlkCount, |
368 | 428 | const MemRegion *sReg) { |
369 | 428 | ID.AddInteger(MemRegion::BlockDataRegionKind); |
370 | 428 | ID.AddPointer(BC); |
371 | 428 | ID.AddPointer(LC); |
372 | 428 | ID.AddInteger(BlkCount); |
373 | 428 | ID.AddPointer(sReg); |
374 | 428 | } |
375 | | |
376 | 44 | void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
377 | 44 | BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion()); |
378 | 44 | } |
379 | | |
380 | | void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
381 | | Expr const *Ex, |
382 | 6.14k | const MemRegion *sReg) { |
383 | 6.14k | ID.AddPointer(Ex); |
384 | 6.14k | ID.AddPointer(sReg); |
385 | 6.14k | } |
386 | | |
387 | 2.46k | void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
388 | 2.46k | ProfileRegion(ID, Ex, getSuperRegion()); |
389 | 2.46k | } |
390 | | |
391 | | void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
392 | | const CXXRecordDecl *RD, |
393 | | bool IsVirtual, |
394 | 3.29k | const MemRegion *SReg) { |
395 | 3.29k | ID.AddPointer(RD); |
396 | 3.29k | ID.AddBoolean(IsVirtual); |
397 | 3.29k | ID.AddPointer(SReg); |
398 | 3.29k | } |
399 | | |
400 | 1.29k | void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
401 | 1.29k | ProfileRegion(ID, getDecl(), isVirtual(), superRegion); |
402 | 1.29k | } |
403 | | |
404 | | void CXXDerivedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
405 | | const CXXRecordDecl *RD, |
406 | 32 | const MemRegion *SReg) { |
407 | 32 | ID.AddPointer(RD); |
408 | 32 | ID.AddPointer(SReg); |
409 | 32 | } |
410 | | |
411 | 11 | void CXXDerivedObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
412 | 11 | ProfileRegion(ID, getDecl(), superRegion); |
413 | 11 | } |
414 | | |
415 | | //===----------------------------------------------------------------------===// |
416 | | // Region anchors. |
417 | | //===----------------------------------------------------------------------===// |
418 | | |
419 | 0 | void GlobalsSpaceRegion::anchor() {} |
420 | | |
421 | 0 | void NonStaticGlobalSpaceRegion::anchor() {} |
422 | | |
423 | 0 | void StackSpaceRegion::anchor() {} |
424 | | |
425 | 0 | void TypedRegion::anchor() {} |
426 | | |
427 | 0 | void TypedValueRegion::anchor() {} |
428 | | |
429 | 0 | void CodeTextRegion::anchor() {} |
430 | | |
431 | 0 | void SubRegion::anchor() {} |
432 | | |
433 | | //===----------------------------------------------------------------------===// |
434 | | // Region pretty-printing. |
435 | | //===----------------------------------------------------------------------===// |
436 | | |
437 | 0 | LLVM_DUMP_METHOD void MemRegion::dump() const { |
438 | 0 | dumpToStream(llvm::errs()); |
439 | 0 | } |
440 | | |
441 | 230 | std::string MemRegion::getString() const { |
442 | 230 | std::string s; |
443 | 230 | llvm::raw_string_ostream os(s); |
444 | 230 | dumpToStream(os); |
445 | 230 | return os.str(); |
446 | 230 | } |
447 | | |
448 | 0 | void MemRegion::dumpToStream(raw_ostream &os) const { |
449 | 0 | os << "<Unknown Region>"; |
450 | 0 | } |
451 | | |
452 | 0 | void AllocaRegion::dumpToStream(raw_ostream &os) const { |
453 | 0 | os << "alloca{S" << Ex->getID(getContext()) << ',' << Cnt << '}'; |
454 | 0 | } |
455 | | |
456 | 40 | void FunctionCodeRegion::dumpToStream(raw_ostream &os) const { |
457 | 40 | os << "code{" << getDecl()->getDeclName().getAsString() << '}'; |
458 | 40 | } |
459 | | |
460 | 0 | void BlockCodeRegion::dumpToStream(raw_ostream &os) const { |
461 | 0 | os << "block_code{" << static_cast<const void *>(this) << '}'; |
462 | 0 | } |
463 | | |
464 | 0 | void BlockDataRegion::dumpToStream(raw_ostream &os) const { |
465 | 0 | os << "block_data{" << BC; |
466 | 0 | os << "; "; |
467 | 0 | for (BlockDataRegion::referenced_vars_iterator |
468 | 0 | I = referenced_vars_begin(), |
469 | 0 | E = referenced_vars_end(); I != E; ++I) |
470 | 0 | os << "(" << I.getCapturedRegion() << "<-" << |
471 | 0 | I.getOriginalRegion() << ") "; |
472 | 0 | os << '}'; |
473 | 0 | } |
474 | | |
475 | 0 | void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const { |
476 | | // FIXME: More elaborate pretty-printing. |
477 | 0 | os << "{ S" << CL->getID(getContext()) << " }"; |
478 | 0 | } |
479 | | |
480 | 4 | void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const { |
481 | 4 | os << "temp_object{" << getValueType().getAsString() << ", " |
482 | 4 | << "S" << Ex->getID(getContext()) << '}'; |
483 | 4 | } |
484 | | |
485 | 0 | void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const { |
486 | 0 | os << "Base{" << superRegion << ',' << getDecl()->getName() << '}'; |
487 | 0 | } |
488 | | |
489 | 0 | void CXXDerivedObjectRegion::dumpToStream(raw_ostream &os) const { |
490 | 0 | os << "Derived{" << superRegion << ',' << getDecl()->getName() << '}'; |
491 | 0 | } |
492 | | |
493 | 24 | void CXXThisRegion::dumpToStream(raw_ostream &os) const { |
494 | 24 | os << "this"; |
495 | 24 | } |
496 | | |
497 | 49 | void ElementRegion::dumpToStream(raw_ostream &os) const { |
498 | 49 | os << "Element{" << superRegion << ',' |
499 | 49 | << Index << ',' << getElementType().getAsString() << '}'; |
500 | 49 | } |
501 | | |
502 | 25 | void FieldRegion::dumpToStream(raw_ostream &os) const { |
503 | 25 | os << superRegion << "." << *getDecl(); |
504 | 25 | } |
505 | | |
506 | 0 | void ObjCIvarRegion::dumpToStream(raw_ostream &os) const { |
507 | 0 | os << "Ivar{" << superRegion << ',' << *getDecl() << '}'; |
508 | 0 | } |
509 | | |
510 | 51 | void StringRegion::dumpToStream(raw_ostream &os) const { |
511 | 51 | assert(Str != nullptr && "Expecting non-null StringLiteral"); |
512 | 0 | Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts())); |
513 | 51 | } |
514 | | |
515 | 0 | void ObjCStringRegion::dumpToStream(raw_ostream &os) const { |
516 | 0 | assert(Str != nullptr && "Expecting non-null ObjCStringLiteral"); |
517 | 0 | Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts())); |
518 | 0 | } |
519 | | |
520 | 113 | void SymbolicRegion::dumpToStream(raw_ostream &os) const { |
521 | 113 | if (isa<HeapSpaceRegion>(getSuperRegion())) |
522 | 26 | os << "Heap"; |
523 | 113 | os << "SymRegion{" << sym << '}'; |
524 | 113 | } |
525 | | |
526 | 317 | void NonParamVarRegion::dumpToStream(raw_ostream &os) const { |
527 | 317 | if (const IdentifierInfo *ID = VD->getIdentifier()) |
528 | 317 | os << ID->getName(); |
529 | 0 | else |
530 | 0 | os << "NonParamVarRegion{D" << VD->getID() << '}'; |
531 | 317 | } |
532 | | |
533 | 0 | LLVM_DUMP_METHOD void RegionRawOffset::dump() const { |
534 | 0 | dumpToStream(llvm::errs()); |
535 | 0 | } |
536 | | |
537 | 0 | void RegionRawOffset::dumpToStream(raw_ostream &os) const { |
538 | 0 | os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}'; |
539 | 0 | } |
540 | | |
541 | 0 | void CodeSpaceRegion::dumpToStream(raw_ostream &os) const { |
542 | 0 | os << "CodeSpaceRegion"; |
543 | 0 | } |
544 | | |
545 | 0 | void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const { |
546 | 0 | os << "StaticGlobalsMemSpace{" << CR << '}'; |
547 | 0 | } |
548 | | |
549 | 5 | void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const { |
550 | 5 | os << "GlobalInternalSpaceRegion"; |
551 | 5 | } |
552 | | |
553 | 30 | void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const { |
554 | 30 | os << "GlobalSystemSpaceRegion"; |
555 | 30 | } |
556 | | |
557 | 0 | void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const { |
558 | 0 | os << "GlobalImmutableSpaceRegion"; |
559 | 0 | } |
560 | | |
561 | 0 | void HeapSpaceRegion::dumpToStream(raw_ostream &os) const { |
562 | 0 | os << "HeapSpaceRegion"; |
563 | 0 | } |
564 | | |
565 | 0 | void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const { |
566 | 0 | os << "UnknownSpaceRegion"; |
567 | 0 | } |
568 | | |
569 | 0 | void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const { |
570 | 0 | os << "StackArgumentsSpaceRegion"; |
571 | 0 | } |
572 | | |
573 | 0 | void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const { |
574 | 0 | os << "StackLocalsSpaceRegion"; |
575 | 0 | } |
576 | | |
577 | 0 | void ParamVarRegion::dumpToStream(raw_ostream &os) const { |
578 | 0 | const ParmVarDecl *PVD = getDecl(); |
579 | 0 | assert(PVD && |
580 | 0 | "`ParamVarRegion` support functions without `Decl` not implemented" |
581 | 0 | " yet."); |
582 | 0 | if (const IdentifierInfo *ID = PVD->getIdentifier()) { |
583 | 0 | os << ID->getName(); |
584 | 0 | } else { |
585 | 0 | os << "ParamVarRegion{P" << PVD->getID() << '}'; |
586 | 0 | } |
587 | 0 | } |
588 | | |
589 | 5.90k | bool MemRegion::canPrintPretty() const { |
590 | 5.90k | return canPrintPrettyAsExpr(); |
591 | 5.90k | } |
592 | | |
593 | 132 | bool MemRegion::canPrintPrettyAsExpr() const { |
594 | 132 | return false; |
595 | 132 | } |
596 | | |
597 | 1.92k | void MemRegion::printPretty(raw_ostream &os) const { |
598 | 1.92k | assert(canPrintPretty() && "This region cannot be printed pretty."); |
599 | 0 | os << "'"; |
600 | 1.92k | printPrettyAsExpr(os); |
601 | 1.92k | os << "'"; |
602 | 1.92k | } |
603 | | |
604 | 0 | void MemRegion::printPrettyAsExpr(raw_ostream &) const { |
605 | 0 | llvm_unreachable("This region cannot be printed pretty."); |
606 | 0 | } |
607 | | |
608 | 5.73k | bool NonParamVarRegion::canPrintPrettyAsExpr() const { return true; } |
609 | | |
610 | 1.92k | void NonParamVarRegion::printPrettyAsExpr(raw_ostream &os) const { |
611 | 1.92k | os << getDecl()->getName(); |
612 | 1.92k | } |
613 | | |
614 | 235 | bool ParamVarRegion::canPrintPrettyAsExpr() const { return true; } |
615 | | |
616 | 116 | void ParamVarRegion::printPrettyAsExpr(raw_ostream &os) const { |
617 | 116 | assert(getDecl() && |
618 | 116 | "`ParamVarRegion` support functions without `Decl` not implemented" |
619 | 116 | " yet."); |
620 | 0 | os << getDecl()->getName(); |
621 | 116 | } |
622 | | |
623 | 39 | bool ObjCIvarRegion::canPrintPrettyAsExpr() const { |
624 | 39 | return true; |
625 | 39 | } |
626 | | |
627 | 14 | void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const { |
628 | 14 | os << getDecl()->getName(); |
629 | 14 | } |
630 | | |
631 | 130 | bool FieldRegion::canPrintPretty() const { |
632 | 130 | return true; |
633 | 130 | } |
634 | | |
635 | 170 | bool FieldRegion::canPrintPrettyAsExpr() const { |
636 | 170 | return superRegion->canPrintPrettyAsExpr(); |
637 | 170 | } |
638 | | |
639 | 68 | void FieldRegion::printPrettyAsExpr(raw_ostream &os) const { |
640 | 68 | assert(canPrintPrettyAsExpr()); |
641 | 0 | superRegion->printPrettyAsExpr(os); |
642 | 68 | os << "." << getDecl()->getName(); |
643 | 68 | } |
644 | | |
645 | 89 | void FieldRegion::printPretty(raw_ostream &os) const { |
646 | 89 | if (canPrintPrettyAsExpr()) { |
647 | 59 | os << "\'"; |
648 | 59 | printPrettyAsExpr(os); |
649 | 59 | os << "'"; |
650 | 30 | } else { |
651 | 30 | os << "field " << "\'" << getDecl()->getName() << "'"; |
652 | 30 | } |
653 | 89 | } |
654 | | |
655 | 6 | bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const { |
656 | 6 | return superRegion->canPrintPrettyAsExpr(); |
657 | 6 | } |
658 | | |
659 | 3 | void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const { |
660 | 3 | superRegion->printPrettyAsExpr(os); |
661 | 3 | } |
662 | | |
663 | 0 | bool CXXDerivedObjectRegion::canPrintPrettyAsExpr() const { |
664 | 0 | return superRegion->canPrintPrettyAsExpr(); |
665 | 0 | } |
666 | | |
667 | 0 | void CXXDerivedObjectRegion::printPrettyAsExpr(raw_ostream &os) const { |
668 | 0 | superRegion->printPrettyAsExpr(os); |
669 | 0 | } |
670 | | |
671 | 77 | std::string MemRegion::getDescriptiveName(bool UseQuotes) const { |
672 | 77 | std::string VariableName; |
673 | 77 | std::string ArrayIndices; |
674 | 77 | const MemRegion *R = this; |
675 | 77 | SmallString<50> buf; |
676 | 77 | llvm::raw_svector_ostream os(buf); |
677 | | |
678 | | // Obtain array indices to add them to the variable name. |
679 | 77 | const ElementRegion *ER = nullptr; |
680 | 104 | while ((ER = R->getAs<ElementRegion>())) { |
681 | | // Index is a ConcreteInt. |
682 | 27 | if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) { |
683 | 27 | llvm::SmallString<2> Idx; |
684 | 27 | CI->getValue().toString(Idx); |
685 | 27 | ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str(); |
686 | 27 | } |
687 | | // If not a ConcreteInt, try to obtain the variable |
688 | | // name by calling 'getDescriptiveName' recursively. |
689 | 0 | else { |
690 | 0 | std::string Idx = ER->getDescriptiveName(false); |
691 | 0 | if (!Idx.empty()) { |
692 | 0 | ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str(); |
693 | 0 | } |
694 | 0 | } |
695 | 27 | R = ER->getSuperRegion(); |
696 | 27 | } |
697 | | |
698 | | // Get variable name. |
699 | 77 | if (R && R->canPrintPrettyAsExpr()) { |
700 | 71 | R->printPrettyAsExpr(os); |
701 | 71 | if (UseQuotes) |
702 | 71 | return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str(); |
703 | 0 | else |
704 | 0 | return (llvm::Twine(os.str()) + ArrayIndices).str(); |
705 | 71 | } |
706 | | |
707 | 6 | return VariableName; |
708 | 77 | } |
709 | | |
710 | 34 | SourceRange MemRegion::sourceRange() const { |
711 | 34 | const auto *const VR = dyn_cast<VarRegion>(this->getBaseRegion()); |
712 | 34 | const auto *const FR = dyn_cast<FieldRegion>(this); |
713 | | |
714 | | // Check for more specific regions first. |
715 | | // FieldRegion |
716 | 34 | if (FR) { |
717 | 3 | return FR->getDecl()->getSourceRange(); |
718 | 3 | } |
719 | | // VarRegion |
720 | 31 | else if (VR) { |
721 | 31 | return VR->getDecl()->getSourceRange(); |
722 | 31 | } |
723 | | // Return invalid source range (can be checked by client). |
724 | 0 | else |
725 | 0 | return {}; |
726 | 34 | } |
727 | | |
728 | | //===----------------------------------------------------------------------===// |
729 | | // MemRegionManager methods. |
730 | | //===----------------------------------------------------------------------===// |
731 | | |
732 | | static DefinedOrUnknownSVal getTypeSize(QualType Ty, ASTContext &Ctx, |
733 | 25.5k | SValBuilder &SVB) { |
734 | 25.5k | CharUnits Size = Ctx.getTypeSizeInChars(Ty); |
735 | 25.5k | QualType SizeTy = SVB.getArrayIndexType(); |
736 | 25.5k | return SVB.makeIntVal(Size.getQuantity(), SizeTy); |
737 | 25.5k | } |
738 | | |
739 | | DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR, |
740 | 27.6k | SValBuilder &SVB) const { |
741 | 27.6k | const auto *SR = cast<SubRegion>(MR); |
742 | 27.6k | SymbolManager &SymMgr = SVB.getSymbolManager(); |
743 | | |
744 | 27.6k | switch (SR->getKind()) { |
745 | 93 | case MemRegion::AllocaRegionKind: |
746 | 1.93k | case MemRegion::SymbolicRegionKind: |
747 | 1.93k | return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR)); |
748 | 8 | case MemRegion::StringRegionKind: |
749 | 8 | return SVB.makeIntVal( |
750 | 8 | cast<StringRegion>(SR)->getStringLiteral()->getByteLength() + 1, |
751 | 8 | SVB.getArrayIndexType()); |
752 | 1 | case MemRegion::CompoundLiteralRegionKind: |
753 | 19 | case MemRegion::CXXBaseObjectRegionKind: |
754 | 19 | case MemRegion::CXXDerivedObjectRegionKind: |
755 | 99 | case MemRegion::CXXTempObjectRegionKind: |
756 | 99 | case MemRegion::CXXThisRegionKind: |
757 | 157 | case MemRegion::ObjCIvarRegionKind: |
758 | 2.45k | case MemRegion::NonParamVarRegionKind: |
759 | 3.14k | case MemRegion::ParamVarRegionKind: |
760 | 10.0k | case MemRegion::ElementRegionKind: |
761 | 10.0k | case MemRegion::ObjCStringRegionKind: { |
762 | 10.0k | QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx); |
763 | 10.0k | if (isa<VariableArrayType>(Ty)) |
764 | 53 | return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR)); |
765 | | |
766 | 10.0k | if (Ty->isIncompleteType()) |
767 | 2 | return UnknownVal(); |
768 | | |
769 | 10.0k | return getTypeSize(Ty, Ctx, SVB); |
770 | 10.0k | } |
771 | 15.5k | case MemRegion::FieldRegionKind: { |
772 | | // Force callers to deal with bitfields explicitly. |
773 | 15.5k | if (cast<FieldRegion>(SR)->getDecl()->isBitField()) |
774 | 37 | return UnknownVal(); |
775 | | |
776 | 15.5k | QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx); |
777 | 15.5k | DefinedOrUnknownSVal Size = getTypeSize(Ty, Ctx, SVB); |
778 | | |
779 | | // A zero-length array at the end of a struct often stands for dynamically |
780 | | // allocated extra memory. |
781 | 15.5k | if (Size.isZeroConstant()) { |
782 | 5 | if (isa<ConstantArrayType>(Ty)) |
783 | 3 | return UnknownVal(); |
784 | 5 | } |
785 | | |
786 | 15.5k | return Size; |
787 | 15.5k | } |
788 | | // FIXME: The following are being used in 'SimpleSValBuilder' and in |
789 | | // 'ArrayBoundChecker::checkLocation' because there is no symbol to |
790 | | // represent the regions more appropriately. |
791 | 0 | case MemRegion::BlockDataRegionKind: |
792 | 0 | case MemRegion::BlockCodeRegionKind: |
793 | 6 | case MemRegion::FunctionCodeRegionKind: |
794 | 6 | return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR)); |
795 | 0 | default: |
796 | 0 | llvm_unreachable("Unhandled region"); |
797 | 27.6k | } |
798 | 27.6k | } |
799 | | |
800 | | template <typename REG> |
801 | 218k | const REG *MemRegionManager::LazyAllocate(REG*& region) { |
802 | 218k | if (!region) { |
803 | 32.9k | region = A.Allocate<REG>(); |
804 | 32.9k | new (region) REG(*this); |
805 | 32.9k | } |
806 | | |
807 | 218k | return region; |
808 | 218k | } clang::ento::GlobalSystemSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalSystemSpaceRegion>(clang::ento::GlobalSystemSpaceRegion*&) Line | Count | Source | 801 | 33.5k | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 33.5k | if (!region) { | 803 | 6.94k | region = A.Allocate<REG>(); | 804 | 6.94k | new (region) REG(*this); | 805 | 6.94k | } | 806 | | | 807 | 33.5k | return region; | 808 | 33.5k | } |
clang::ento::GlobalImmutableSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalImmutableSpaceRegion>(clang::ento::GlobalImmutableSpaceRegion*&) Line | Count | Source | 801 | 366 | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 366 | if (!region) { | 803 | 217 | region = A.Allocate<REG>(); | 804 | 217 | new (region) REG(*this); | 805 | 217 | } | 806 | | | 807 | 366 | return region; | 808 | 366 | } |
clang::ento::GlobalInternalSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalInternalSpaceRegion>(clang::ento::GlobalInternalSpaceRegion*&) Line | Count | Source | 801 | 45.1k | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 45.1k | if (!region) { | 803 | 7.12k | region = A.Allocate<REG>(); | 804 | 7.12k | new (region) REG(*this); | 805 | 7.12k | } | 806 | | | 807 | 45.1k | return region; | 808 | 45.1k | } |
clang::ento::HeapSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::HeapSpaceRegion>(clang::ento::HeapSpaceRegion*&) Line | Count | Source | 801 | 1.36k | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 1.36k | if (!region) { | 803 | 1.09k | region = A.Allocate<REG>(); | 804 | 1.09k | new (region) REG(*this); | 805 | 1.09k | } | 806 | | | 807 | 1.36k | return region; | 808 | 1.36k | } |
clang::ento::UnknownSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::UnknownSpaceRegion>(clang::ento::UnknownSpaceRegion*&) Line | Count | Source | 801 | 67.9k | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 67.9k | if (!region) { | 803 | 8.23k | region = A.Allocate<REG>(); | 804 | 8.23k | new (region) REG(*this); | 805 | 8.23k | } | 806 | | | 807 | 67.9k | return region; | 808 | 67.9k | } |
clang::ento::CodeSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::CodeSpaceRegion>(clang::ento::CodeSpaceRegion*&) Line | Count | Source | 801 | 69.9k | const REG *MemRegionManager::LazyAllocate(REG*& region) { | 802 | 69.9k | if (!region) { | 803 | 9.33k | region = A.Allocate<REG>(); | 804 | 9.33k | new (region) REG(*this); | 805 | 9.33k | } | 806 | | | 807 | 69.9k | return region; | 808 | 69.9k | } |
|
809 | | |
810 | | template <typename REG, typename ARG> |
811 | | const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) { |
812 | | if (!region) { |
813 | | region = A.Allocate<REG>(); |
814 | | new (region) REG(this, a); |
815 | | } |
816 | | |
817 | | return region; |
818 | | } |
819 | | |
820 | | const StackLocalsSpaceRegion* |
821 | 134k | MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) { |
822 | 134k | assert(STC); |
823 | 0 | StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC]; |
824 | | |
825 | 134k | if (R) |
826 | 123k | return R; |
827 | | |
828 | 11.4k | R = A.Allocate<StackLocalsSpaceRegion>(); |
829 | 11.4k | new (R) StackLocalsSpaceRegion(*this, STC); |
830 | 11.4k | return R; |
831 | 134k | } |
832 | | |
833 | | const StackArgumentsSpaceRegion * |
834 | 191k | MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) { |
835 | 191k | assert(STC); |
836 | 0 | StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC]; |
837 | | |
838 | 191k | if (R) |
839 | 162k | return R; |
840 | | |
841 | 29.0k | R = A.Allocate<StackArgumentsSpaceRegion>(); |
842 | 29.0k | new (R) StackArgumentsSpaceRegion(*this, STC); |
843 | 29.0k | return R; |
844 | 191k | } |
845 | | |
846 | | const GlobalsSpaceRegion |
847 | | *MemRegionManager::getGlobalsRegion(MemRegion::Kind K, |
848 | 79.6k | const CodeTextRegion *CR) { |
849 | 79.6k | if (!CR) { |
850 | 79.0k | if (K == MemRegion::GlobalSystemSpaceRegionKind) |
851 | 33.5k | return LazyAllocate(SystemGlobals); |
852 | 45.4k | if (K == MemRegion::GlobalImmutableSpaceRegionKind) |
853 | 366 | return LazyAllocate(ImmutableGlobals); |
854 | 45.1k | assert(K == MemRegion::GlobalInternalSpaceRegionKind); |
855 | 0 | return LazyAllocate(InternalGlobals); |
856 | 45.4k | } |
857 | | |
858 | 644 | assert(K == MemRegion::StaticGlobalSpaceRegionKind); |
859 | 0 | StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR]; |
860 | 644 | if (R) |
861 | 472 | return R; |
862 | | |
863 | 172 | R = A.Allocate<StaticGlobalSpaceRegion>(); |
864 | 172 | new (R) StaticGlobalSpaceRegion(*this, CR); |
865 | 172 | return R; |
866 | 644 | } |
867 | | |
868 | 1.36k | const HeapSpaceRegion *MemRegionManager::getHeapRegion() { |
869 | 1.36k | return LazyAllocate(heap); |
870 | 1.36k | } |
871 | | |
872 | 67.9k | const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() { |
873 | 67.9k | return LazyAllocate(unknown); |
874 | 67.9k | } |
875 | | |
876 | 69.9k | const CodeSpaceRegion *MemRegionManager::getCodeRegion() { |
877 | 69.9k | return LazyAllocate(code); |
878 | 69.9k | } |
879 | | |
880 | | //===----------------------------------------------------------------------===// |
881 | | // Constructing regions. |
882 | | //===----------------------------------------------------------------------===// |
883 | | |
884 | 8.78k | const StringRegion *MemRegionManager::getStringRegion(const StringLiteral *Str){ |
885 | 8.78k | return getSubRegion<StringRegion>( |
886 | 8.78k | Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion())); |
887 | 8.78k | } |
888 | | |
889 | | const ObjCStringRegion * |
890 | 1.11k | MemRegionManager::getObjCStringRegion(const ObjCStringLiteral *Str){ |
891 | 1.11k | return getSubRegion<ObjCStringRegion>( |
892 | 1.11k | Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion())); |
893 | 1.11k | } |
894 | | |
895 | | /// Look through a chain of LocationContexts to either find the |
896 | | /// StackFrameContext that matches a DeclContext, or find a VarRegion |
897 | | /// for a variable captured by a block. |
898 | | static llvm::PointerUnion<const StackFrameContext *, const VarRegion *> |
899 | | getStackOrCaptureRegionForDeclContext(const LocationContext *LC, |
900 | | const DeclContext *DC, |
901 | 163k | const VarDecl *VD) { |
902 | 163k | while (LC) { |
903 | 163k | if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) { |
904 | 163k | if (cast<DeclContext>(SFC->getDecl()) == DC) |
905 | 162k | return SFC; |
906 | 163k | } |
907 | 653 | if (const auto *BC = dyn_cast<BlockInvocationContext>(LC)) { |
908 | 162 | const auto *BR = static_cast<const BlockDataRegion *>(BC->getData()); |
909 | | // FIXME: This can be made more efficient. |
910 | 162 | for (BlockDataRegion::referenced_vars_iterator |
911 | 162 | I = BR->referenced_vars_begin(), |
912 | 204 | E = BR->referenced_vars_end(); I != E; ++I42 ) { |
913 | 204 | const TypedValueRegion *OrigR = I.getOriginalRegion(); |
914 | 204 | if (const auto *VR = dyn_cast<VarRegion>(OrigR)) { |
915 | 204 | if (VR->getDecl() == VD) |
916 | 162 | return cast<VarRegion>(I.getCapturedRegion()); |
917 | 204 | } |
918 | 204 | } |
919 | 162 | } |
920 | | |
921 | 491 | LC = LC->getParent(); |
922 | 491 | } |
923 | 221 | return (const StackFrameContext *)nullptr; |
924 | 163k | } |
925 | | |
926 | | const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D, |
927 | 211k | const LocationContext *LC) { |
928 | 211k | const auto *PVD = dyn_cast<ParmVarDecl>(D); |
929 | 211k | if (PVD) { |
930 | 64.9k | unsigned Index = PVD->getFunctionScopeIndex(); |
931 | 64.9k | const StackFrameContext *SFC = LC->getStackFrame(); |
932 | 64.9k | const Stmt *CallSite = SFC->getCallSite(); |
933 | 64.9k | if (CallSite) { |
934 | 40.0k | const Decl *D = SFC->getDecl(); |
935 | 40.0k | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
936 | 39.7k | if (Index < FD->param_size() && FD->parameters()[Index] == PVD39.7k ) |
937 | 39.7k | return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index, |
938 | 39.7k | getStackArgumentsRegion(SFC)); |
939 | 249 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
940 | 88 | if (Index < BD->param_size() && BD->parameters()[Index] == PVD66 ) |
941 | 59 | return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index, |
942 | 59 | getStackArgumentsRegion(SFC)); |
943 | 161 | } else { |
944 | 161 | return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index, |
945 | 161 | getStackArgumentsRegion(SFC)); |
946 | 161 | } |
947 | 40.0k | } |
948 | 64.9k | } |
949 | | |
950 | 171k | D = D->getCanonicalDecl(); |
951 | 171k | const MemRegion *sReg = nullptr; |
952 | | |
953 | 171k | if (D->hasGlobalStorage() && !D->isStaticLocal()8.83k ) { |
954 | | |
955 | | // First handle the globals defined in system headers. |
956 | 8.16k | if (Ctx.getSourceManager().isInSystemHeader(D->getLocation())) { |
957 | | // Whitelist the system globals which often DO GET modified, assume the |
958 | | // rest are immutable. |
959 | 58 | if (D->getName().find("errno") != StringRef::npos) |
960 | 6 | sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind); |
961 | 52 | else |
962 | 52 | sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); |
963 | | |
964 | | // Treat other globals as GlobalInternal unless they are constants. |
965 | 8.11k | } else { |
966 | 8.11k | QualType GQT = D->getType(); |
967 | 8.11k | const Type *GT = GQT.getTypePtrOrNull(); |
968 | | // TODO: We could walk the complex types here and see if everything is |
969 | | // constified. |
970 | 8.11k | if (GT && GQT.isConstQualified() && GT->isArithmeticType()420 ) |
971 | 160 | sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); |
972 | 7.95k | else |
973 | 7.95k | sReg = getGlobalsRegion(); |
974 | 8.11k | } |
975 | | |
976 | | // Finally handle static locals. |
977 | 163k | } else { |
978 | | // FIXME: Once we implement scope handling, we will need to properly lookup |
979 | | // 'D' to the proper LocationContext. |
980 | 163k | const DeclContext *DC = D->getDeclContext(); |
981 | 163k | llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V = |
982 | 163k | getStackOrCaptureRegionForDeclContext(LC, DC, D); |
983 | | |
984 | 163k | if (V.is<const VarRegion*>()) |
985 | 162 | return V.get<const VarRegion*>(); |
986 | | |
987 | 162k | const auto *STC = V.get<const StackFrameContext *>(); |
988 | | |
989 | 162k | if (!STC) { |
990 | | // FIXME: Assign a more sensible memory space to static locals |
991 | | // we see from within blocks that we analyze as top-level declarations. |
992 | 221 | sReg = getUnknownRegion(); |
993 | 162k | } else { |
994 | 162k | if (D->hasLocalStorage()) { |
995 | 162k | sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)137k |
996 | 31.5k | ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC)) |
997 | 130k | : static_cast<const MemRegion*>(getStackLocalsRegion(STC)); |
998 | 162k | } |
999 | 644 | else { |
1000 | 644 | assert(D->isStaticLocal()); |
1001 | 0 | const Decl *STCD = STC->getDecl(); |
1002 | 644 | if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD)82 ) |
1003 | 631 | sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, |
1004 | 631 | getFunctionCodeRegion(cast<NamedDecl>(STCD))); |
1005 | 13 | else if (const auto *BD = dyn_cast<BlockDecl>(STCD)) { |
1006 | | // FIXME: The fallback type here is totally bogus -- though it should |
1007 | | // never be queried, it will prevent uniquing with the real |
1008 | | // BlockCodeRegion. Ideally we'd fix the AST so that we always had a |
1009 | | // signature. |
1010 | 13 | QualType T; |
1011 | 13 | if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten()) |
1012 | 11 | T = TSI->getType(); |
1013 | 13 | if (T.isNull()) |
1014 | 2 | T = getContext().VoidTy; |
1015 | 13 | if (!T->getAs<FunctionType>()) |
1016 | 5 | T = getContext().getFunctionNoProtoType(T); |
1017 | 13 | T = getContext().getBlockPointerType(T); |
1018 | | |
1019 | 13 | const BlockCodeRegion *BTR = |
1020 | 13 | getBlockCodeRegion(BD, Ctx.getCanonicalType(T), |
1021 | 13 | STC->getAnalysisDeclContext()); |
1022 | 13 | sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, |
1023 | 13 | BTR); |
1024 | 13 | } |
1025 | 0 | else { |
1026 | 0 | sReg = getGlobalsRegion(); |
1027 | 0 | } |
1028 | 644 | } |
1029 | 162k | } |
1030 | 162k | } |
1031 | | |
1032 | 171k | return getSubRegion<NonParamVarRegion>(D, sReg); |
1033 | 171k | } |
1034 | | |
1035 | | const NonParamVarRegion * |
1036 | | MemRegionManager::getNonParamVarRegion(const VarDecl *D, |
1037 | 195 | const MemRegion *superR) { |
1038 | 195 | D = D->getCanonicalDecl(); |
1039 | 195 | return getSubRegion<NonParamVarRegion>(D, superR); |
1040 | 195 | } |
1041 | | |
1042 | | const ParamVarRegion * |
1043 | | MemRegionManager::getParamVarRegion(const Expr *OriginExpr, unsigned Index, |
1044 | 43.5k | const LocationContext *LC) { |
1045 | 43.5k | const StackFrameContext *SFC = LC->getStackFrame(); |
1046 | 43.5k | assert(SFC); |
1047 | 0 | return getSubRegion<ParamVarRegion>(OriginExpr, Index, |
1048 | 43.5k | getStackArgumentsRegion(SFC)); |
1049 | 43.5k | } |
1050 | | |
1051 | | const BlockDataRegion * |
1052 | | MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC, |
1053 | | const LocationContext *LC, |
1054 | 384 | unsigned blockCount) { |
1055 | 384 | const MemSpaceRegion *sReg = nullptr; |
1056 | 384 | const BlockDecl *BD = BC->getDecl(); |
1057 | 384 | if (!BD->hasCaptures()) { |
1058 | | // This handles 'static' blocks. |
1059 | 154 | sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); |
1060 | 154 | } |
1061 | 230 | else { |
1062 | 230 | if (LC) { |
1063 | | // FIXME: Once we implement scope handling, we want the parent region |
1064 | | // to be the scope. |
1065 | 230 | const StackFrameContext *STC = LC->getStackFrame(); |
1066 | 230 | assert(STC); |
1067 | 0 | sReg = getStackLocalsRegion(STC); |
1068 | 230 | } |
1069 | 0 | else { |
1070 | | // We allow 'LC' to be NULL for cases where want BlockDataRegions |
1071 | | // without context-sensitivity. |
1072 | 0 | sReg = getUnknownRegion(); |
1073 | 0 | } |
1074 | 230 | } |
1075 | | |
1076 | 0 | return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg); |
1077 | 384 | } |
1078 | | |
1079 | | const CXXTempObjectRegion * |
1080 | 15 | MemRegionManager::getCXXStaticTempObjectRegion(const Expr *Ex) { |
1081 | 15 | return getSubRegion<CXXTempObjectRegion>( |
1082 | 15 | Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr)); |
1083 | 15 | } |
1084 | | |
1085 | | const CompoundLiteralRegion* |
1086 | | MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL, |
1087 | 56 | const LocationContext *LC) { |
1088 | 56 | const MemSpaceRegion *sReg = nullptr; |
1089 | | |
1090 | 56 | if (CL->isFileScope()) |
1091 | 0 | sReg = getGlobalsRegion(); |
1092 | 56 | else { |
1093 | 56 | const StackFrameContext *STC = LC->getStackFrame(); |
1094 | 56 | assert(STC); |
1095 | 0 | sReg = getStackLocalsRegion(STC); |
1096 | 56 | } |
1097 | | |
1098 | 0 | return getSubRegion<CompoundLiteralRegion>(CL, sReg); |
1099 | 56 | } |
1100 | | |
1101 | | const ElementRegion* |
1102 | | MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx, |
1103 | | const SubRegion* superRegion, |
1104 | 46.9k | ASTContext &Ctx){ |
1105 | 46.9k | QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType(); |
1106 | | |
1107 | 46.9k | llvm::FoldingSetNodeID ID; |
1108 | 46.9k | ElementRegion::ProfileRegion(ID, T, Idx, superRegion); |
1109 | | |
1110 | 46.9k | void *InsertPos; |
1111 | 46.9k | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
1112 | 46.9k | auto *R = cast_or_null<ElementRegion>(data); |
1113 | | |
1114 | 46.9k | if (!R) { |
1115 | 13.3k | R = A.Allocate<ElementRegion>(); |
1116 | 13.3k | new (R) ElementRegion(T, Idx, superRegion); |
1117 | 13.3k | Regions.InsertNode(R, InsertPos); |
1118 | 13.3k | } |
1119 | | |
1120 | 46.9k | return R; |
1121 | 46.9k | } |
1122 | | |
1123 | | const FunctionCodeRegion * |
1124 | 69.5k | MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) { |
1125 | | // To think: should we canonicalize the declaration here? |
1126 | 69.5k | return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion()); |
1127 | 69.5k | } |
1128 | | |
1129 | | const BlockCodeRegion * |
1130 | | MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy, |
1131 | 397 | AnalysisDeclContext *AC) { |
1132 | 397 | return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion()); |
1133 | 397 | } |
1134 | | |
1135 | | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
1136 | 65.5k | const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) { |
1137 | 65.5k | return getSubRegion<SymbolicRegion>(sym, getUnknownRegion()); |
1138 | 65.5k | } |
1139 | | |
1140 | 1.36k | const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) { |
1141 | 1.36k | return getSubRegion<SymbolicRegion>(Sym, getHeapRegion()); |
1142 | 1.36k | } |
1143 | | |
1144 | | const FieldRegion* |
1145 | | MemRegionManager::getFieldRegion(const FieldDecl *d, |
1146 | 109k | const SubRegion* superRegion){ |
1147 | 109k | return getSubRegion<FieldRegion>(d, superRegion); |
1148 | 109k | } |
1149 | | |
1150 | | const ObjCIvarRegion* |
1151 | | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d, |
1152 | 1.45k | const SubRegion* superRegion) { |
1153 | 1.45k | return getSubRegion<ObjCIvarRegion>(d, superRegion); |
1154 | 1.45k | } |
1155 | | |
1156 | | const CXXTempObjectRegion* |
1157 | | MemRegionManager::getCXXTempObjectRegion(Expr const *E, |
1158 | 3.66k | LocationContext const *LC) { |
1159 | 3.66k | const StackFrameContext *SFC = LC->getStackFrame(); |
1160 | 3.66k | assert(SFC); |
1161 | 0 | return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC)); |
1162 | 3.66k | } |
1163 | | |
1164 | | /// Checks whether \p BaseClass is a valid virtual or direct non-virtual base |
1165 | | /// class of the type of \p Super. |
1166 | | static bool isValidBaseClass(const CXXRecordDecl *BaseClass, |
1167 | | const TypedValueRegion *Super, |
1168 | 1.55k | bool IsVirtual) { |
1169 | 1.55k | BaseClass = BaseClass->getCanonicalDecl(); |
1170 | | |
1171 | 1.55k | const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl(); |
1172 | 1.55k | if (!Class) |
1173 | 0 | return true; |
1174 | | |
1175 | 1.55k | if (IsVirtual) |
1176 | 157 | return Class->isVirtuallyDerivedFrom(BaseClass); |
1177 | | |
1178 | 1.56k | for (const auto &I : Class->bases())1.39k { |
1179 | 1.56k | if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass) |
1180 | 1.39k | return true; |
1181 | 1.56k | } |
1182 | | |
1183 | 0 | return false; |
1184 | 1.39k | } |
1185 | | |
1186 | | const CXXBaseObjectRegion * |
1187 | | MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD, |
1188 | | const SubRegion *Super, |
1189 | 1.99k | bool IsVirtual) { |
1190 | 1.99k | if (isa<TypedValueRegion>(Super)) { |
1191 | 1.55k | assert(isValidBaseClass(RD, cast<TypedValueRegion>(Super), IsVirtual)); |
1192 | 0 | (void)&isValidBaseClass; |
1193 | | |
1194 | 1.55k | if (IsVirtual) { |
1195 | | // Virtual base regions should not be layered, since the layout rules |
1196 | | // are different. |
1197 | 195 | while (const auto *Base = dyn_cast<CXXBaseObjectRegion>(Super)) |
1198 | 38 | Super = cast<SubRegion>(Base->getSuperRegion()); |
1199 | 157 | assert(Super && !isa<MemSpaceRegion>(Super)); |
1200 | 157 | } |
1201 | 1.55k | } |
1202 | | |
1203 | 0 | return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super); |
1204 | 1.99k | } |
1205 | | |
1206 | | const CXXDerivedObjectRegion * |
1207 | | MemRegionManager::getCXXDerivedObjectRegion(const CXXRecordDecl *RD, |
1208 | 21 | const SubRegion *Super) { |
1209 | 21 | return getSubRegion<CXXDerivedObjectRegion>(RD, Super); |
1210 | 21 | } |
1211 | | |
1212 | | const CXXThisRegion* |
1213 | | MemRegionManager::getCXXThisRegion(QualType thisPointerTy, |
1214 | 76.4k | const LocationContext *LC) { |
1215 | 76.4k | const auto *PT = thisPointerTy->getAs<PointerType>(); |
1216 | 76.4k | assert(PT); |
1217 | | // Inside the body of the operator() of a lambda a this expr might refer to an |
1218 | | // object in one of the parent location contexts. |
1219 | 0 | const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl()); |
1220 | | // FIXME: when operator() of lambda is analyzed as a top level function and |
1221 | | // 'this' refers to a this to the enclosing scope, there is no right region to |
1222 | | // return. |
1223 | 76.5k | while (!LC->inTopFrame() && (72.9k !D72.9k || D->isStatic()72.9k || |
1224 | 72.9k | PT != D->getThisType()->getAs<PointerType>())) { |
1225 | 80 | LC = LC->getParent(); |
1226 | 80 | D = dyn_cast<CXXMethodDecl>(LC->getDecl()); |
1227 | 80 | } |
1228 | 76.4k | const StackFrameContext *STC = LC->getStackFrame(); |
1229 | 76.4k | assert(STC); |
1230 | 0 | return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC)); |
1231 | 76.4k | } |
1232 | | |
1233 | | const AllocaRegion* |
1234 | | MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt, |
1235 | 91 | const LocationContext *LC) { |
1236 | 91 | const StackFrameContext *STC = LC->getStackFrame(); |
1237 | 91 | assert(STC); |
1238 | 0 | return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC)); |
1239 | 91 | } |
1240 | | |
1241 | 2.44M | const MemSpaceRegion *MemRegion::getMemorySpace() const { |
1242 | 2.44M | const MemRegion *R = this; |
1243 | 2.44M | const auto *SR = dyn_cast<SubRegion>(this); |
1244 | | |
1245 | 4.97M | while (SR) { |
1246 | 2.52M | R = SR->getSuperRegion(); |
1247 | 2.52M | SR = dyn_cast<SubRegion>(R); |
1248 | 2.52M | } |
1249 | | |
1250 | 2.44M | return dyn_cast<MemSpaceRegion>(R); |
1251 | 2.44M | } |
1252 | | |
1253 | 75.4k | bool MemRegion::hasStackStorage() const { |
1254 | 75.4k | return isa<StackSpaceRegion>(getMemorySpace()); |
1255 | 75.4k | } |
1256 | | |
1257 | 66.6k | bool MemRegion::hasStackNonParametersStorage() const { |
1258 | 66.6k | return isa<StackLocalsSpaceRegion>(getMemorySpace()); |
1259 | 66.6k | } |
1260 | | |
1261 | 65.0k | bool MemRegion::hasStackParametersStorage() const { |
1262 | 65.0k | return isa<StackArgumentsSpaceRegion>(getMemorySpace()); |
1263 | 65.0k | } |
1264 | | |
1265 | 0 | bool MemRegion::hasGlobalsOrParametersStorage() const { |
1266 | 0 | const MemSpaceRegion *MS = getMemorySpace(); |
1267 | 0 | return isa<StackArgumentsSpaceRegion>(MS) || |
1268 | 0 | isa<GlobalsSpaceRegion>(MS); |
1269 | 0 | } |
1270 | | |
1271 | | // getBaseRegion strips away all elements and fields, and get the base region |
1272 | | // of them. |
1273 | 7.84M | const MemRegion *MemRegion::getBaseRegion() const { |
1274 | 7.84M | const MemRegion *R = this; |
1275 | 10.1M | while (true) { |
1276 | 10.1M | switch (R->getKind()) { |
1277 | 1.17M | case MemRegion::ElementRegionKind: |
1278 | 2.32M | case MemRegion::FieldRegionKind: |
1279 | 2.33M | case MemRegion::ObjCIvarRegionKind: |
1280 | 2.34M | case MemRegion::CXXBaseObjectRegionKind: |
1281 | 2.34M | case MemRegion::CXXDerivedObjectRegionKind: |
1282 | 2.34M | R = cast<SubRegion>(R)->getSuperRegion(); |
1283 | 2.34M | continue; |
1284 | 7.84M | default: |
1285 | 7.84M | break; |
1286 | 10.1M | } |
1287 | 7.84M | break; |
1288 | 10.1M | } |
1289 | 7.84M | return R; |
1290 | 7.84M | } |
1291 | | |
1292 | | // getgetMostDerivedObjectRegion gets the region of the root class of a C++ |
1293 | | // class hierarchy. |
1294 | 34.1k | const MemRegion *MemRegion::getMostDerivedObjectRegion() const { |
1295 | 34.1k | const MemRegion *R = this; |
1296 | 34.2k | while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R)) |
1297 | 102 | R = BR->getSuperRegion(); |
1298 | 34.1k | return R; |
1299 | 34.1k | } |
1300 | | |
1301 | 0 | bool MemRegion::isSubRegionOf(const MemRegion *) const { |
1302 | 0 | return false; |
1303 | 0 | } |
1304 | | |
1305 | | //===----------------------------------------------------------------------===// |
1306 | | // View handling. |
1307 | | //===----------------------------------------------------------------------===// |
1308 | | |
1309 | 316k | const MemRegion *MemRegion::StripCasts(bool StripBaseAndDerivedCasts) const { |
1310 | 316k | const MemRegion *R = this; |
1311 | 346k | while (true) { |
1312 | 346k | switch (R->getKind()) { |
1313 | 60.4k | case ElementRegionKind: { |
1314 | 60.4k | const auto *ER = cast<ElementRegion>(R); |
1315 | 60.4k | if (!ER->getIndex().isZeroConstant()) |
1316 | 33.2k | return R; |
1317 | 27.2k | R = ER->getSuperRegion(); |
1318 | 27.2k | break; |
1319 | 60.4k | } |
1320 | 2.59k | case CXXBaseObjectRegionKind: |
1321 | 2.67k | case CXXDerivedObjectRegionKind: |
1322 | 2.67k | if (!StripBaseAndDerivedCasts) |
1323 | 0 | return R; |
1324 | 2.67k | R = cast<TypedValueRegion>(R)->getSuperRegion(); |
1325 | 2.67k | break; |
1326 | 283k | default: |
1327 | 283k | return R; |
1328 | 346k | } |
1329 | 346k | } |
1330 | 316k | } |
1331 | | |
1332 | 1.01M | const SymbolicRegion *MemRegion::getSymbolicBase() const { |
1333 | 1.01M | const auto *SubR = dyn_cast<SubRegion>(this); |
1334 | | |
1335 | 2.10M | while (SubR) { |
1336 | 1.36M | if (const auto *SymR = dyn_cast<SymbolicRegion>(SubR)) |
1337 | 270k | return SymR; |
1338 | 1.09M | SubR = dyn_cast<SubRegion>(SubR->getSuperRegion()); |
1339 | 1.09M | } |
1340 | 745k | return nullptr; |
1341 | 1.01M | } |
1342 | | |
1343 | 13.1k | RegionRawOffset ElementRegion::getAsArrayOffset() const { |
1344 | 13.1k | int64_t offset = 0; |
1345 | 13.1k | const ElementRegion *ER = this; |
1346 | 13.1k | const MemRegion *superR = nullptr; |
1347 | 13.1k | ASTContext &C = getContext(); |
1348 | | |
1349 | | // FIXME: Handle multi-dimensional arrays. |
1350 | | |
1351 | 24.8k | while (ER) { |
1352 | 13.6k | superR = ER->getSuperRegion(); |
1353 | | |
1354 | | // FIXME: generalize to symbolic offsets. |
1355 | 13.6k | SVal index = ER->getIndex(); |
1356 | 13.6k | if (auto CI = index.getAs<nonloc::ConcreteInt>()) { |
1357 | | // Update the offset. |
1358 | 11.6k | int64_t i = CI->getValue().getSExtValue(); |
1359 | | |
1360 | 11.6k | if (i != 0) { |
1361 | 3.75k | QualType elemType = ER->getElementType(); |
1362 | | |
1363 | | // If we are pointing to an incomplete type, go no further. |
1364 | 3.75k | if (elemType->isIncompleteType()) { |
1365 | 0 | superR = ER; |
1366 | 0 | break; |
1367 | 0 | } |
1368 | | |
1369 | 3.75k | int64_t size = C.getTypeSizeInChars(elemType).getQuantity(); |
1370 | 3.75k | if (auto NewOffset = llvm::checkedMulAdd(i, size, offset)) { |
1371 | 3.74k | offset = *NewOffset; |
1372 | 6 | } else { |
1373 | 6 | LLVM_DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: " |
1374 | 6 | << "offset overflowing, returning unknown\n"); |
1375 | | |
1376 | 6 | return nullptr; |
1377 | 6 | } |
1378 | 3.75k | } |
1379 | | |
1380 | | // Go to the next ElementRegion (if any). |
1381 | 11.6k | ER = dyn_cast<ElementRegion>(superR); |
1382 | 11.6k | continue; |
1383 | 11.6k | } |
1384 | | |
1385 | 1.94k | return nullptr; |
1386 | 13.6k | } |
1387 | | |
1388 | 11.1k | assert(superR && "super region cannot be NULL"); |
1389 | 0 | return RegionRawOffset(superR, CharUnits::fromQuantity(offset)); |
1390 | 13.1k | } |
1391 | | |
1392 | | /// Returns true if \p Base is an immediate base class of \p Child |
1393 | | static bool isImmediateBase(const CXXRecordDecl *Child, |
1394 | 216 | const CXXRecordDecl *Base) { |
1395 | 216 | assert(Child && "Child must not be null"); |
1396 | | // Note that we do NOT canonicalize the base class here, because |
1397 | | // ASTRecordLayout doesn't either. If that leads us down the wrong path, |
1398 | | // so be it; at least we won't crash. |
1399 | 248 | for (const auto &I : Child->bases()) { |
1400 | 248 | if (I.getType()->getAsCXXRecordDecl() == Base) |
1401 | 214 | return true; |
1402 | 248 | } |
1403 | | |
1404 | 2 | return false; |
1405 | 216 | } |
1406 | | |
1407 | 111k | static RegionOffset calculateOffset(const MemRegion *R) { |
1408 | 111k | const MemRegion *SymbolicOffsetBase = nullptr; |
1409 | 111k | int64_t Offset = 0; |
1410 | | |
1411 | 138k | while (true) { |
1412 | 138k | switch (R->getKind()) { |
1413 | 0 | case MemRegion::CodeSpaceRegionKind: |
1414 | 2.26k | case MemRegion::StackLocalsSpaceRegionKind: |
1415 | 2.69k | case MemRegion::StackArgumentsSpaceRegionKind: |
1416 | 2.72k | case MemRegion::HeapSpaceRegionKind: |
1417 | 4.64k | case MemRegion::UnknownSpaceRegionKind: |
1418 | 4.65k | case MemRegion::StaticGlobalSpaceRegionKind: |
1419 | 10.3k | case MemRegion::GlobalInternalSpaceRegionKind: |
1420 | 17.2k | case MemRegion::GlobalSystemSpaceRegionKind: |
1421 | 17.2k | case MemRegion::GlobalImmutableSpaceRegionKind: |
1422 | | // Stores can bind directly to a region space to set a default value. |
1423 | 17.2k | assert(Offset == 0 && !SymbolicOffsetBase); |
1424 | 0 | goto Finish; |
1425 | | |
1426 | 72 | case MemRegion::FunctionCodeRegionKind: |
1427 | 72 | case MemRegion::BlockCodeRegionKind: |
1428 | 76 | case MemRegion::BlockDataRegionKind: |
1429 | | // These will never have bindings, but may end up having values requested |
1430 | | // if the user does some strange casting. |
1431 | 76 | if (Offset != 0) |
1432 | 6 | SymbolicOffsetBase = R; |
1433 | 76 | goto Finish; |
1434 | | |
1435 | 14.8k | case MemRegion::SymbolicRegionKind: |
1436 | 14.8k | case MemRegion::AllocaRegionKind: |
1437 | 14.9k | case MemRegion::CompoundLiteralRegionKind: |
1438 | 28.9k | case MemRegion::CXXThisRegionKind: |
1439 | 29.3k | case MemRegion::StringRegionKind: |
1440 | 29.4k | case MemRegion::ObjCStringRegionKind: |
1441 | 67.7k | case MemRegion::NonParamVarRegionKind: |
1442 | 87.9k | case MemRegion::ParamVarRegionKind: |
1443 | 93.3k | case MemRegion::CXXTempObjectRegionKind: |
1444 | | // Usual base regions. |
1445 | 93.3k | goto Finish; |
1446 | | |
1447 | 649 | case MemRegion::ObjCIvarRegionKind: |
1448 | | // This is a little strange, but it's a compromise between |
1449 | | // ObjCIvarRegions having unknown compile-time offsets (when using the |
1450 | | // non-fragile runtime) and yet still being distinct, non-overlapping |
1451 | | // regions. Thus we treat them as "like" base regions for the purposes |
1452 | | // of computing offsets. |
1453 | 649 | goto Finish; |
1454 | | |
1455 | 1.46k | case MemRegion::CXXBaseObjectRegionKind: { |
1456 | 1.46k | const auto *BOR = cast<CXXBaseObjectRegion>(R); |
1457 | 1.46k | R = BOR->getSuperRegion(); |
1458 | | |
1459 | 1.46k | QualType Ty; |
1460 | 1.46k | bool RootIsSymbolic = false; |
1461 | 1.46k | if (const auto *TVR = dyn_cast<TypedValueRegion>(R)) { |
1462 | 1.21k | Ty = TVR->getDesugaredValueType(R->getContext()); |
1463 | 251 | } else if (const auto *SR = dyn_cast<SymbolicRegion>(R)) { |
1464 | | // If our base region is symbolic, we don't know what type it really is. |
1465 | | // Pretend the type of the symbol is the true dynamic type. |
1466 | | // (This will at least be self-consistent for the life of the symbol.) |
1467 | 251 | Ty = SR->getSymbol()->getType()->getPointeeType(); |
1468 | 251 | RootIsSymbolic = true; |
1469 | 251 | } |
1470 | | |
1471 | 1.46k | const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl(); |
1472 | 1.46k | if (!Child) { |
1473 | | // We cannot compute the offset of the base class. |
1474 | 0 | SymbolicOffsetBase = R; |
1475 | 1.46k | } else { |
1476 | 1.46k | if (RootIsSymbolic) { |
1477 | | // Base layers on symbolic regions may not be type-correct. |
1478 | | // Double-check the inheritance here, and revert to a symbolic offset |
1479 | | // if it's invalid (e.g. due to a reinterpret_cast). |
1480 | 251 | if (BOR->isVirtual()) { |
1481 | 35 | if (!Child->isVirtuallyDerivedFrom(BOR->getDecl())) |
1482 | 0 | SymbolicOffsetBase = R; |
1483 | 216 | } else { |
1484 | 216 | if (!isImmediateBase(Child, BOR->getDecl())) |
1485 | 2 | SymbolicOffsetBase = R; |
1486 | 216 | } |
1487 | 251 | } |
1488 | 1.46k | } |
1489 | | |
1490 | | // Don't bother calculating precise offsets if we already have a |
1491 | | // symbolic offset somewhere in the chain. |
1492 | 1.46k | if (SymbolicOffsetBase) |
1493 | 5 | continue; |
1494 | | |
1495 | 1.45k | CharUnits BaseOffset; |
1496 | 1.45k | const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(Child); |
1497 | 1.45k | if (BOR->isVirtual()) |
1498 | 96 | BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl()); |
1499 | 1.36k | else |
1500 | 1.36k | BaseOffset = Layout.getBaseClassOffset(BOR->getDecl()); |
1501 | | |
1502 | | // The base offset is in chars, not in bits. |
1503 | 1.45k | Offset += BaseOffset.getQuantity() * R->getContext().getCharWidth(); |
1504 | 1.45k | break; |
1505 | 1.46k | } |
1506 | | |
1507 | 4 | case MemRegion::CXXDerivedObjectRegionKind: { |
1508 | | // TODO: Store the base type in the CXXDerivedObjectRegion and use it. |
1509 | 4 | goto Finish; |
1510 | 1.46k | } |
1511 | | |
1512 | 7.70k | case MemRegion::ElementRegionKind: { |
1513 | 7.70k | const auto *ER = cast<ElementRegion>(R); |
1514 | 7.70k | R = ER->getSuperRegion(); |
1515 | | |
1516 | 7.70k | QualType EleTy = ER->getValueType(); |
1517 | 7.70k | if (EleTy->isIncompleteType()) { |
1518 | | // We cannot compute the offset of the base class. |
1519 | 10 | SymbolicOffsetBase = R; |
1520 | 10 | continue; |
1521 | 10 | } |
1522 | | |
1523 | 7.69k | SVal Index = ER->getIndex(); |
1524 | 7.69k | if (Optional<nonloc::ConcreteInt> CI = |
1525 | 7.24k | Index.getAs<nonloc::ConcreteInt>()) { |
1526 | | // Don't bother calculating precise offsets if we already have a |
1527 | | // symbolic offset somewhere in the chain. |
1528 | 7.24k | if (SymbolicOffsetBase) |
1529 | 3 | continue; |
1530 | | |
1531 | 7.24k | int64_t i = CI->getValue().getSExtValue(); |
1532 | | // This type size is in bits. |
1533 | 7.24k | Offset += i * R->getContext().getTypeSize(EleTy); |
1534 | 447 | } else { |
1535 | | // We cannot compute offset for non-concrete index. |
1536 | 447 | SymbolicOffsetBase = R; |
1537 | 447 | } |
1538 | 7.69k | break; |
1539 | 7.69k | } |
1540 | 17.5k | case MemRegion::FieldRegionKind: { |
1541 | 17.5k | const auto *FR = cast<FieldRegion>(R); |
1542 | 17.5k | R = FR->getSuperRegion(); |
1543 | 17.5k | assert(R); |
1544 | | |
1545 | 0 | const RecordDecl *RD = FR->getDecl()->getParent(); |
1546 | 17.5k | if (RD->isUnion() || !RD->isCompleteDefinition()17.4k ) { |
1547 | | // We cannot compute offset for incomplete type. |
1548 | | // For unions, we could treat everything as offset 0, but we'd rather |
1549 | | // treat each field as a symbolic offset so they aren't stored on top |
1550 | | // of each other, since we depend on things in typed regions actually |
1551 | | // matching their types. |
1552 | 108 | SymbolicOffsetBase = R; |
1553 | 108 | } |
1554 | | |
1555 | | // Don't bother calculating precise offsets if we already have a |
1556 | | // symbolic offset somewhere in the chain. |
1557 | 17.5k | if (SymbolicOffsetBase) |
1558 | 245 | continue; |
1559 | | |
1560 | | // Get the field number. |
1561 | 17.3k | unsigned idx = 0; |
1562 | 17.3k | for (RecordDecl::field_iterator FI = RD->field_begin(), |
1563 | 23.4k | FE = RD->field_end(); FI != FE; ++FI, ++idx6.15k ) { |
1564 | 23.4k | if (FR->getDecl() == *FI) |
1565 | 17.3k | break; |
1566 | 23.4k | } |
1567 | 17.3k | const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(RD); |
1568 | | // This is offset in bits. |
1569 | 17.3k | Offset += Layout.getFieldOffset(idx); |
1570 | 17.3k | break; |
1571 | 17.5k | } |
1572 | 138k | } |
1573 | 138k | } |
1574 | | |
1575 | 111k | Finish: |
1576 | 111k | if (SymbolicOffsetBase) |
1577 | 565 | return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic); |
1578 | 110k | return RegionOffset(R, Offset); |
1579 | 111k | } |
1580 | | |
1581 | 1.52M | RegionOffset MemRegion::getAsOffset() const { |
1582 | 1.52M | if (!cachedOffset) |
1583 | 111k | cachedOffset = calculateOffset(this); |
1584 | 1.52M | return *cachedOffset; |
1585 | 1.52M | } |
1586 | | |
1587 | | //===----------------------------------------------------------------------===// |
1588 | | // BlockDataRegion |
1589 | | //===----------------------------------------------------------------------===// |
1590 | | |
1591 | | std::pair<const VarRegion *, const VarRegion *> |
1592 | 297 | BlockDataRegion::getCaptureRegions(const VarDecl *VD) { |
1593 | 297 | MemRegionManager &MemMgr = getMemRegionManager(); |
1594 | 297 | const VarRegion *VR = nullptr; |
1595 | 297 | const VarRegion *OriginalVR = nullptr; |
1596 | | |
1597 | 297 | if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()235 ) { |
1598 | 195 | VR = MemMgr.getNonParamVarRegion(VD, this); |
1599 | 195 | OriginalVR = MemMgr.getVarRegion(VD, LC); |
1600 | 195 | } |
1601 | 102 | else { |
1602 | 102 | if (LC) { |
1603 | 102 | VR = MemMgr.getVarRegion(VD, LC); |
1604 | 102 | OriginalVR = VR; |
1605 | 102 | } |
1606 | 0 | else { |
1607 | 0 | VR = MemMgr.getNonParamVarRegion(VD, MemMgr.getUnknownRegion()); |
1608 | 0 | OriginalVR = MemMgr.getVarRegion(VD, LC); |
1609 | 0 | } |
1610 | 102 | } |
1611 | 297 | return std::make_pair(VR, OriginalVR); |
1612 | 297 | } |
1613 | | |
1614 | 6.02k | void BlockDataRegion::LazyInitializeReferencedVars() { |
1615 | 6.02k | if (ReferencedVars) |
1616 | 5.65k | return; |
1617 | | |
1618 | 370 | AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext(); |
1619 | 370 | const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl()); |
1620 | 370 | auto NumBlockVars = |
1621 | 370 | std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end()); |
1622 | | |
1623 | 370 | if (NumBlockVars == 0) { |
1624 | 134 | ReferencedVars = (void*) 0x1; |
1625 | 134 | return; |
1626 | 134 | } |
1627 | | |
1628 | 236 | MemRegionManager &MemMgr = getMemRegionManager(); |
1629 | 236 | llvm::BumpPtrAllocator &A = MemMgr.getAllocator(); |
1630 | 236 | BumpVectorContext BC(A); |
1631 | | |
1632 | 236 | using VarVec = BumpVector<const MemRegion *>; |
1633 | | |
1634 | 236 | auto *BV = A.Allocate<VarVec>(); |
1635 | 236 | new (BV) VarVec(BC, NumBlockVars); |
1636 | 236 | auto *BVOriginal = A.Allocate<VarVec>(); |
1637 | 236 | new (BVOriginal) VarVec(BC, NumBlockVars); |
1638 | | |
1639 | 297 | for (const auto *VD : ReferencedBlockVars) { |
1640 | 297 | const VarRegion *VR = nullptr; |
1641 | 297 | const VarRegion *OriginalVR = nullptr; |
1642 | 297 | std::tie(VR, OriginalVR) = getCaptureRegions(VD); |
1643 | 297 | assert(VR); |
1644 | 0 | assert(OriginalVR); |
1645 | 0 | BV->push_back(VR, BC); |
1646 | 297 | BVOriginal->push_back(OriginalVR, BC); |
1647 | 297 | } |
1648 | | |
1649 | 236 | ReferencedVars = BV; |
1650 | 236 | OriginalVars = BVOriginal; |
1651 | 236 | } |
1652 | | |
1653 | | BlockDataRegion::referenced_vars_iterator |
1654 | 3.01k | BlockDataRegion::referenced_vars_begin() const { |
1655 | 3.01k | const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); |
1656 | | |
1657 | 3.01k | auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars); |
1658 | | |
1659 | 3.01k | if (Vec == (void*) 0x1) |
1660 | 727 | return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr); |
1661 | | |
1662 | 2.28k | auto *VecOriginal = |
1663 | 2.28k | static_cast<BumpVector<const MemRegion *> *>(OriginalVars); |
1664 | | |
1665 | 2.28k | return BlockDataRegion::referenced_vars_iterator(Vec->begin(), |
1666 | 2.28k | VecOriginal->begin()); |
1667 | 3.01k | } |
1668 | | |
1669 | | BlockDataRegion::referenced_vars_iterator |
1670 | 3.01k | BlockDataRegion::referenced_vars_end() const { |
1671 | 3.01k | const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); |
1672 | | |
1673 | 3.01k | auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars); |
1674 | | |
1675 | 3.01k | if (Vec == (void*) 0x1) |
1676 | 727 | return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr); |
1677 | | |
1678 | 2.28k | auto *VecOriginal = |
1679 | 2.28k | static_cast<BumpVector<const MemRegion *> *>(OriginalVars); |
1680 | | |
1681 | 2.28k | return BlockDataRegion::referenced_vars_iterator(Vec->end(), |
1682 | 2.28k | VecOriginal->end()); |
1683 | 3.01k | } |
1684 | | |
1685 | 4 | const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const { |
1686 | 4 | for (referenced_vars_iterator I = referenced_vars_begin(), |
1687 | 4 | E = referenced_vars_end(); |
1688 | 6 | I != E; ++I2 ) { |
1689 | 6 | if (I.getCapturedRegion() == R) |
1690 | 4 | return I.getOriginalRegion(); |
1691 | 6 | } |
1692 | 0 | return nullptr; |
1693 | 4 | } |
1694 | | |
1695 | | //===----------------------------------------------------------------------===// |
1696 | | // RegionAndSymbolInvalidationTraits |
1697 | | //===----------------------------------------------------------------------===// |
1698 | | |
1699 | | void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym, |
1700 | 2.31k | InvalidationKinds IK) { |
1701 | 2.31k | SymTraitsMap[Sym] |= IK; |
1702 | 2.31k | } |
1703 | | |
1704 | | void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR, |
1705 | 10.2k | InvalidationKinds IK) { |
1706 | 10.2k | assert(MR); |
1707 | 10.2k | if (const auto *SR = dyn_cast<SymbolicRegion>(MR)) |
1708 | 2.19k | setTrait(SR->getSymbol(), IK); |
1709 | 8.01k | else |
1710 | 8.01k | MRTraitsMap[MR] |= IK; |
1711 | 10.2k | } |
1712 | | |
1713 | | bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym, |
1714 | 118k | InvalidationKinds IK) const { |
1715 | 118k | const_symbol_iterator I = SymTraitsMap.find(Sym); |
1716 | 118k | if (I != SymTraitsMap.end()) |
1717 | 8.72k | return I->second & IK; |
1718 | | |
1719 | 109k | return false; |
1720 | 118k | } |
1721 | | |
1722 | | bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR, |
1723 | 184k | InvalidationKinds IK) const { |
1724 | 184k | if (!MR) |
1725 | 0 | return false; |
1726 | | |
1727 | 184k | if (const auto *SR = dyn_cast<SymbolicRegion>(MR)) |
1728 | 18.9k | return hasTrait(SR->getSymbol(), IK); |
1729 | | |
1730 | 165k | const_region_iterator I = MRTraitsMap.find(MR); |
1731 | 165k | if (I != MRTraitsMap.end()) |
1732 | 13.7k | return I->second & IK; |
1733 | | |
1734 | 151k | return false; |
1735 | 165k | } |