clang: lib/StaticAnalyzer/Checkers/StoreToImmutableChecker.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
20
21using namespace clang;
22using namespace ento;
23
24namespace {
25class StoreToImmutableChecker : public Checkercheck::Bind {
26 const BugType BT{this, "Write to immutable memory", "CERT Environment (ENV)"};
27
28public:
29 void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit,
30 CheckerContext &C) const;
31};
32}
33
36 return true;
37
38
39 if (const auto *TR = dyn_cast(MR)) {
40 QualType LocationType = TR->getDesugaredLocationType(C.getASTContext());
44 return true;
45 }
46
47
48 if (const auto *SR = dyn_cast(MR)) {
49 QualType PointeeType = SR->getPointeeStaticType();
51 return true;
52 }
53
54
55
56
57 return false;
58}
59
62 while (true) {
64 return MR;
65 if (auto *SR = dyn_cast(MR))
66 MR = SR->getSuperRegion();
67 else
68 return nullptr;
69 }
70}
71
74 while (true) {
75 if (const auto *DR = dyn_cast(MR)) {
76 const ValueDecl *D = DR->getDecl();
79 return DR;
80 }
81 if (auto *SR = dyn_cast(MR))
82 MR = SR->getSuperRegion();
83 else
84 return nullptr;
85 }
86}
87
88void StoreToImmutableChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
89 bool AtDeclInit,
91
93 if (!MR)
94 return;
95
96
97
98 if (AtDeclInit)
99 return;
100
101
102 const MemSpaceRegion *MS = MR->getMemorySpace(C.getState());
104
106 if (!IsGlobalImmutableSpace && !InnermostConstRegion)
107 return;
108
109 SmallString<64> WarningMessage{"Trying to write to immutable memory"};
110 if (IsGlobalImmutableSpace)
111 WarningMessage += " in global read-only storage";
112
113
114 ExplodedNode *N = C.generateNonFatalErrorNode();
115 if (!N)
116 return;
117
118 auto R = std::make_unique(BT, WarningMessage, N);
120
121
122
123 const DeclRegion *DR =
125 if (DR) {
126 const char *NoteMessage =
127 (DR != MR) ? "Enclosing memory region is declared as immutable here"
128 : "Memory region is declared as immutable here";
130 DR->getDecl(), C.getSourceManager()));
131 }
132
133
134
135
136 C.emitReport(std::move(R));
137}
138
139void ento::registerStoreToImmutableChecker(CheckerManager &mgr) {
141}
142
143bool ento::shouldRegisterStoreToImmutableChecker(const CheckerManager &mgr) {
144 return true;
145}
static const MemRegion * getInnermostConstRegion(const MemRegion *MR, CheckerContext &C)
Definition StoreToImmutableChecker.cpp:60
static const DeclRegion * getInnermostEnclosingConstDeclRegion(const MemRegion *MR, CheckerContext &C)
Definition StoreToImmutableChecker.cpp:73
static bool isEffectivelyConstRegion(const MemRegion *MR, CheckerContext &C)
Definition StoreToImmutableChecker.cpp:34
A (possibly-)qualified type.
bool isConstQualified() const
Determine whether this type is const-qualified.
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isPointerOrReferenceType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
virtual const ValueDecl * getDecl() const =0
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace(ProgramStateRef State) const
Returns the most specific memory space for this memory region in the given ProgramStateRef.
static PathDiagnosticLocation create(const Decl *D, const SourceManager &SM)
Create a location corresponding to the given declaration.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
const MemRegion * getAsRegion() const
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)