Skip to content
Merged
Changes from 1 commit
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
55 changes: 53 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/StoreToImmutableChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "clang/AST/ParentMap.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
Expand All @@ -29,11 +30,59 @@ class StoreToImmutableChecker : public Checker<check::Bind> {
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;

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

bool StoreToImmutableChecker::isInitializationContext(const Stmt *S,
CheckerContext &C) const {
// Check if this is a DeclStmt (variable declaration)
if (isa<DeclStmt>(S))
return true;

// This part is specific for initialization of const lambdas pre-C++17.
// Lets look at the AST of the statement:
// ```
// const auto lambda = [](){};
// ```
//
// The relevant part of the AST for this case prior to C++17 is:
// ...
// `-DeclStmt
// `-VarDecl
// `-ExprWithCleanups
// `-CXXConstructExpr
// ...
// In C++17 and later, the AST is different:
// ...
// `-DeclStmt
// `-VarDecl
// `-ImplicitCastExpr
// `-LambdaExpr
// |-CXXRecordDecl
// `-CXXConstructExpr
// ...
// And even beside this, the statement `S` that is given to the checkBind
// callback is the VarDecl in C++17 and later, and the CXXConstructExpr in
// C++14 and before. So in order to support the C++14 we need the following
// ugly hack to detect whether this construction is used to initialize a
// variable.
//
// FIXME: This should be eliminated once the API of checkBind would allow to
// distinguish between initialization and assignment, because this information
// is already available in the engine, it is just not passed to the checker
// API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// FIXME: This should be eliminated once the API of checkBind would allow to
// distinguish between initialization and assignment, because this information
// is already available in the engine, it is just not passed to the checker
// API.
// FIXME: This should be eliminated by improving the API of checkBind to
// ensure that it consistently passes the `VarDecl` (instead of the
// `CXXConstructExpr`) when the constructor call denotes the initialization
// of a variable with a lambda.

As we discussed in person, the most probable solution to this corner case is slightly different from what you originally wrote here. I hope that this will become irrelevant soon (if you can restore consistency in the engine), but it is still slightly better to document this more accurately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this with a sidenote.

if (!isa<CXXConstructExpr>(S))
return false;

// We use elidable construction to detect initialization.
if (cast<CXXConstructExpr>(S)->isElidable())
return true;

return false;
}

static bool isEffectivelyConstRegionAux(const MemRegion *MR,
CheckerContext &C) {
// Check if the region is in the global immutable space
Expand Down Expand Up @@ -95,7 +144,9 @@ void StoreToImmutableChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,

// Skip variable declarations and initializations - we only want to catch
// actual writes
if (isa<DeclStmt, DeclRefExpr>(S))
// FIXME: If the API of checkBind would allow to distinguish between
// initialization and assignment, we could use that instead.
if (isInitializationContext(S, C))
return;

// Check if the region corresponds to a const variable
Expand Down