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