Skip to content

Commit de398bd

Browse files
committed
[CHERI_CSA] PointerAlignmentChecker: use declaration as uniquing location
1 parent e75cfa3 commit de398bd

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

clang/lib/StaticAnalyzer/Checkers/PointerAlignmentChecker.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
//===----------------------------------------------------------------------===//
3030

3131
#include "CHERI/CHERIUtils.h"
32+
#include <clang/ASTMatchers/ASTMatchFinder.h>
3233
#include "clang/ASTMatchers/ASTMatchers.h"
3334
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
34-
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
35-
#include <clang/ASTMatchers/ASTMatchFinder.h>
3635
#include <clang/StaticAnalyzer/Core/BugReporter/BugType.h>
3736
#include <clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h>
37+
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
3838

3939
using namespace clang;
4040
using namespace ento;
@@ -425,11 +425,18 @@ void printAlign(raw_ostream &OS, unsigned TZC) {
425425
OS << ")";
426426
}
427427

428-
void describeOriginalAllocation(const MemRegion *MR, PathSensitiveBugReport &W,
429-
const SourceManager &SM,
428+
const DeclRegion *getOriginalAllocation(const MemRegion *MR) {
429+
if (const DeclRegion *DR = MR->getAs<DeclRegion>())
430+
return DR;
431+
if (const ElementRegion *ER = MR->getAs<ElementRegion>())
432+
return getOriginalAllocation(ER->getSuperRegion());
433+
return nullptr;
434+
}
435+
436+
void describeOriginalAllocation(const ValueDecl *SrcDecl,
437+
PathDiagnosticLocation SrcLoc,
438+
PathSensitiveBugReport &W,
430439
ASTContext &ASTCtx) {
431-
if (const DeclRegion *DR = MR->getAs<DeclRegion>()) {
432-
const ValueDecl *SrcDecl = DR->getDecl();
433440
SmallString<350> Note;
434441
llvm::raw_svector_ostream OS2(Note);
435442
const QualType &AllocType = SrcDecl->getType().getCanonicalType();
@@ -438,9 +445,7 @@ void describeOriginalAllocation(const MemRegion *MR, PathSensitiveBugReport &W,
438445
OS2 << " which has an alignment requirement ";
439446
OS2 << ASTCtx.getTypeAlignInChars(AllocType).getQuantity();
440447
OS2 << " bytes";
441-
W.addNote(Note, PathDiagnosticLocation::create(SrcDecl, SM));
442-
} else if (const ElementRegion *ER = MR->getAs<ElementRegion>())
443-
describeOriginalAllocation(ER->getSuperRegion(), W, SM, ASTCtx);
448+
W.addNote(Note, SrcLoc);
444449
}
445450

446451
} // namespace
@@ -467,16 +472,27 @@ PointerAlignmentChecker::emitCastAlignWarn(
467472
OS << " alignment " << DstReqAlign;
468473
OS << " bytes";
469474

475+
const SVal &SrcVal = C.getSVal(CE->getSubExpr());
476+
const ValueDecl *MRDecl = nullptr;
477+
PathDiagnosticLocation MRDeclLoc;
478+
if (const MemRegion *MR = SrcVal.getAsRegion()) {
479+
if (const DeclRegion *OriginalAlloc = getOriginalAllocation(MR)) {
480+
MRDecl = OriginalAlloc->getDecl();
481+
MRDeclLoc = PathDiagnosticLocation::create(MRDecl, C.getSourceManager());
482+
}
483+
}
484+
470485
auto W = std::make_unique<PathSensitiveBugReport>(
471-
DstAlignIsCap ? *CapCastAlignBug : *CastAlignBug, ErrorMessage, ErrNode);
472-
W->addRange(CE->getSourceRange());
486+
DstAlignIsCap ? *CapCastAlignBug : *CastAlignBug,
487+
ErrorMessage, ErrNode,
488+
MRDeclLoc, MRDecl);
473489

474-
const SVal &SrcVal = C.getSVal(CE->getSubExpr());
475490
W->markInteresting(SrcVal);
476491
if (SymbolRef S = SrcVal.getAsSymbol())
477492
W->addVisitor(std::make_unique<AlignmentBugVisitor>(S));
478-
else if (const MemRegion *MR = SrcVal.getAsRegion()) {
479-
describeOriginalAllocation(MR, *W, C.getSourceManager(), C.getASTContext());
493+
494+
if (MRDecl) {
495+
describeOriginalAllocation(MRDecl, MRDeclLoc, *W, C.getASTContext());
480496
}
481497

482498
C.emitReport(std::move(W));

0 commit comments

Comments
 (0)