Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0630d81
[clang][analyzer] Add StoreToImmutable checker
gamesh411 Jul 24, 2025
fa3f84f
Apply review suggestiongs by @steakhal
gamesh411 Jul 25, 2025
b022182
[review-fix] fix test files
gamesh411 Jul 28, 2025
5190ee0
[review-fix] add test case for complex memory hierarchy
gamesh411 Jul 28, 2025
856a865
[review-fix] remove isInSystemMacro check
gamesh411 Jul 28, 2025
d8f3456
[review-fix] add example note on string literal limitation
gamesh411 Jul 28, 2025
01d0521
[review-fix] implement hierarchical memregion handling
gamesh411 Jul 28, 2025
2aacf92
[cornercase] Lambda initialization gives a false positive in C++14 an…
gamesh411 Jul 29, 2025
6e8a332
[format] fixed example file code formatting
gamesh411 Jul 29, 2025
d65aa88
[review-fix] don't repeat type names
gamesh411 Jul 29, 2025
7e94b10
[cornercase] fix false positive cornercase
gamesh411 Jul 29, 2025
4db2804
[review-fix] streamline example file
gamesh411 Jul 30, 2025
7e73177
[review-fix] add more C++ standard versions
gamesh411 Jul 30, 2025
cac94fe
[review-fix] streamline implementation
gamesh411 Jul 30, 2025
373679b
[review-fix] support SubRegions not just ElementRegions
gamesh411 Jul 30, 2025
7cbabf8
[review-fix] fix typo
gamesh411 Jul 30, 2025
e377b19
[review-fix] delete stray whitespace
gamesh411 Jul 30, 2025
cfedf88
[review-fix] more elaborate notes
gamesh411 Aug 1, 2025
fa0a379
[review-fix] remove redundant comments from example
gamesh411 Aug 1, 2025
4e6c988
[review-fix] document our options for the fixme
gamesh411 Aug 1, 2025
17a0e9c
Merge branch 'main' into store-to-immutable-checker
gamesh411 Aug 1, 2025
89389a5
[review-fix] clarify wording
gamesh411 Aug 2, 2025
4d883f1
fix formatting
gamesh411 Aug 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/analyzer/checkers/storetoimmutable_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void test_global_const() {
}

// String literal
// NOTE: This only works in C++, not in C, as the analyzer treats string literals as non-const char arrays in C mode.
void test_string_literal() {
char *str = (char *)"hello";
str[0] = 'H'; // warn: Writing to immutable memory
Expand Down
58 changes: 32 additions & 26 deletions clang/lib/StaticAnalyzer/Checkers/StoreToImmutableChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,24 @@ class StoreToImmutableChecker : public Checker<check::Bind> {
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;

private:
bool isConstVariable(const MemRegion *MR, CheckerContext &C) const;
bool isEffectivelyConstRegion(const MemRegion *MR, CheckerContext &C) const;
bool isConstQualifiedType(const MemRegion *MR, CheckerContext &C) const;
};
} // end anonymous namespace

bool StoreToImmutableChecker::isConstVariable(const MemRegion *MR,
CheckerContext &C) const {
static bool isEffectivelyConstRegionAux(const MemRegion *MR,
CheckerContext &C) {
// Check if the region is in the global immutable space
const MemSpaceRegion *MS = MR->getMemorySpace(C.getState());
if (isa<GlobalImmutableSpaceRegion>(MS))
return true;

// Check if this is a VarRegion with a const-qualified type
if (const VarRegion *VR = dyn_cast<VarRegion>(MR)) {
const VarDecl *VD = VR->getDecl();
if (VD && VD->getType().isConstQualified())
return true;
}

// Check if this is a FieldRegion with a const-qualified type
if (const FieldRegion *FR = dyn_cast<FieldRegion>(MR)) {
const FieldDecl *FD = FR->getDecl();
if (FD && FD->getType().isConstQualified())
// Check if this is a TypedRegion with a const-qualified type
if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR)) {
QualType LocationType = TR->getDesugaredLocationType(C.getASTContext());
if (LocationType->isPointerOrReferenceType())
LocationType = LocationType->getPointeeType();
if (LocationType.isConstQualified())
return true;
}

Expand All @@ -62,22 +57,33 @@ bool StoreToImmutableChecker::isConstVariable(const MemRegion *MR,
return true;
}

// Check if this is an ElementRegion accessing a const array
if (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) {
return isConstQualifiedType(ER->getSuperRegion(), C);
}
// NOTE: The only kind of region that is not checked by the above branches is
// AllocaRegion. We do not need to check AllocaRegion, as it models untyped
// memory, that is allocated on the stack.

return false;
}

bool StoreToImmutableChecker::isConstQualifiedType(const MemRegion *MR,
CheckerContext &C) const {
// Check if the region has a const-qualified type
if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR)) {
QualType Ty = TVR->getValueType();
return Ty.isConstQualified();
bool StoreToImmutableChecker::isEffectivelyConstRegion(
const MemRegion *MR, CheckerContext &C) const {
// If the region is an ElementRegion, we need to check if any of the super
// regions have const-qualified type.
if (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) {
SmallVector<const MemRegion *, 8> SuperRegions;
const MemRegion *Current = MR;
const MemRegion *Base = ER->getBaseRegion();
while (Current != Base) {
SuperRegions.push_back(Current);
assert(isa<SubRegion>(Current));
Current = cast<SubRegion>(Current)->getSuperRegion();
}
SuperRegions.push_back(Base);
return llvm::any_of(SuperRegions, [&C](const MemRegion *MR) {
return isEffectivelyConstRegionAux(MR, C);
});
}
return false;

return isEffectivelyConstRegionAux(MR, C);
}

void StoreToImmutableChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
Expand All @@ -93,7 +99,7 @@ void StoreToImmutableChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
return;

// Check if the region corresponds to a const variable
if (!isConstVariable(MR, C))
if (!isEffectivelyConstRegion(MR, C))
return;

// Generate the bug report
Expand Down
3 changes: 1 addition & 2 deletions clang/test/Analysis/store-to-immutable-basic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -verify %s

// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -std=c++17 -verify %s

void test_write_to_const_ref_param(const int &param) {
*(int*)&param = 100; // expected-warning {{Writing to immutable memory is undefined behavior. This memory region is marked as immutable and should not be modified}}
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Analysis/store-to-immutable-lambda-init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -std=c++14 -verify %s

// expected-no-diagnostics

// In C++14 and before, when initializing a lambda, the statement given in the checkBind callback is not the whole DeclExpr, but the CXXConstructExpr of the lambda object.
// FIXME: Once the API of checkBind provides more information about the statement, the checker should be simplified, and this this test case will no longer be a cornercase in the checker.

void test_const_lambda_initialization_pre_cpp17() {
const auto lambda = [](){}; // No warning expected
}
Loading