-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][analyzer] Add notes to PointerSubChecker #95899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -43,8 +43,6 @@ class PointerSubChecker | |||||||||||||||
bool checkArrayBounds(CheckerContext &C, const Expr *E, | ||||||||||||||||
const ElementRegion *ElemReg, | ||||||||||||||||
const MemRegion *Reg) const; | ||||||||||||||||
void reportBug(CheckerContext &C, const Expr *E, | ||||||||||||||||
const llvm::StringLiteral &Msg) const; | ||||||||||||||||
|
||||||||||||||||
public: | ||||||||||||||||
void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; | ||||||||||||||||
|
@@ -57,14 +55,22 @@ bool PointerSubChecker::checkArrayBounds(CheckerContext &C, const Expr *E, | |||||||||||||||
if (!ElemReg) | ||||||||||||||||
return true; | ||||||||||||||||
|
||||||||||||||||
auto ReportBug = [&](const llvm::StringLiteral &Msg) { | ||||||||||||||||
if (ExplodedNode *N = C.generateNonFatalErrorNode()) { | ||||||||||||||||
auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, N); | ||||||||||||||||
R->addRange(E->getSourceRange()); | ||||||||||||||||
C.emitReport(std::move(R)); | ||||||||||||||||
} | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
ProgramStateRef State = C.getState(); | ||||||||||||||||
const MemRegion *SuperReg = ElemReg->getSuperRegion(); | ||||||||||||||||
SValBuilder &SVB = C.getSValBuilder(); | ||||||||||||||||
|
||||||||||||||||
if (SuperReg == Reg) { | ||||||||||||||||
if (const llvm::APSInt *I = SVB.getKnownValue(State, ElemReg->getIndex()); | ||||||||||||||||
I && (!I->isOne() && !I->isZero())) | ||||||||||||||||
reportBug(C, E, Msg_BadVarIndex); | ||||||||||||||||
ReportBug(Msg_BadVarIndex); | ||||||||||||||||
return false; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -77,7 +83,7 @@ bool PointerSubChecker::checkArrayBounds(CheckerContext &C, const Expr *E, | |||||||||||||||
ProgramStateRef S1, S2; | ||||||||||||||||
std::tie(S1, S2) = C.getState()->assume(*IndexTooLarge); | ||||||||||||||||
if (S1 && !S2) { | ||||||||||||||||
reportBug(C, E, Msg_LargeArrayIndex); | ||||||||||||||||
ReportBug(Msg_LargeArrayIndex); | ||||||||||||||||
return false; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
@@ -89,22 +95,13 @@ bool PointerSubChecker::checkArrayBounds(CheckerContext &C, const Expr *E, | |||||||||||||||
ProgramStateRef S1, S2; | ||||||||||||||||
std::tie(S1, S2) = State->assume(*IndexTooSmall); | ||||||||||||||||
if (S1 && !S2) { | ||||||||||||||||
reportBug(C, E, Msg_NegativeArrayIndex); | ||||||||||||||||
ReportBug(Msg_NegativeArrayIndex); | ||||||||||||||||
return false; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
return true; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
void PointerSubChecker::reportBug(CheckerContext &C, const Expr *E, | ||||||||||||||||
const llvm::StringLiteral &Msg) const { | ||||||||||||||||
if (ExplodedNode *N = C.generateNonFatalErrorNode()) { | ||||||||||||||||
auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, N); | ||||||||||||||||
R->addRange(E->getSourceRange()); | ||||||||||||||||
C.emitReport(std::move(R)); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
void PointerSubChecker::checkPreStmt(const BinaryOperator *B, | ||||||||||||||||
CheckerContext &C) const { | ||||||||||||||||
// When doing pointer subtraction, if the two pointers do not point to the | ||||||||||||||||
|
@@ -136,6 +133,9 @@ void PointerSubChecker::checkPreStmt(const BinaryOperator *B, | |||||||||||||||
if (!checkArrayBounds(C, B->getRHS(), ElemRR, LR)) | ||||||||||||||||
return; | ||||||||||||||||
|
||||||||||||||||
const ValueDecl *DiffDeclL = nullptr; | ||||||||||||||||
const ValueDecl *DiffDeclR = nullptr; | ||||||||||||||||
|
||||||||||||||||
if (ElemLR && ElemRR) { | ||||||||||||||||
const MemRegion *SuperLR = ElemLR->getSuperRegion(); | ||||||||||||||||
const MemRegion *SuperRR = ElemRR->getSuperRegion(); | ||||||||||||||||
|
@@ -144,9 +144,30 @@ void PointerSubChecker::checkPreStmt(const BinaryOperator *B, | |||||||||||||||
// Allow arithmetic on different symbolic regions. | ||||||||||||||||
if (isa<SymbolicRegion>(SuperLR) || isa<SymbolicRegion>(SuperRR)) | ||||||||||||||||
return; | ||||||||||||||||
if (const auto *SuperDLR = dyn_cast<DeclRegion>(SuperLR)) | ||||||||||||||||
DiffDeclL = SuperDLR->getDecl(); | ||||||||||||||||
if (const auto *SuperDRR = dyn_cast<DeclRegion>(SuperRR)) | ||||||||||||||||
DiffDeclR = SuperDRR->getDecl(); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
reportBug(C, B, Msg_MemRegionDifferent); | ||||||||||||||||
if (ExplodedNode *N = C.generateNonFatalErrorNode()) { | ||||||||||||||||
auto R = | ||||||||||||||||
std::make_unique<PathSensitiveBugReport>(BT, Msg_MemRegionDifferent, N); | ||||||||||||||||
R->addRange(B->getSourceRange()); | ||||||||||||||||
// The declarations may be identical even if the regions are different: | ||||||||||||||||
// struct { int array[10]; } a, b; | ||||||||||||||||
// do_something(&a.array[5] - &b.array[5]); | ||||||||||||||||
// In this case don't emit notes. | ||||||||||||||||
if (DiffDeclL != DiffDeclR) { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Add a comment that explains this |
||||||||||||||||
if (DiffDeclL) | ||||||||||||||||
R->addNote("Array at the left-hand side of subtraction", | ||||||||||||||||
{DiffDeclL, C.getSourceManager()}); | ||||||||||||||||
if (DiffDeclR) | ||||||||||||||||
R->addNote("Array at the right-hand side of subtraction", | ||||||||||||||||
{DiffDeclR, C.getSourceManager()}); | ||||||||||||||||
} | ||||||||||||||||
C.emitReport(std::move(R)); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
void ento::registerPointerSubChecker(CheckerManager &mgr) { | ||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.PointerSub -analyzer-output=text -verify %s | ||
|
||
void negative_1() { | ||
int a[3]; | ||
int x = -1; | ||
// FIXME: should indicate that 'x' is -1 | ||
int d = &a[x] - &a[0]; // expected-warning{{Using a negative array index at pointer subtraction is undefined behavior}} \ | ||
// expected-note{{Using a negative array index at pointer subtraction is undefined behavior}} | ||
} | ||
|
||
void negative_2() { | ||
int a[3]; | ||
int *p1 = a, *p2 = a; | ||
--p2; | ||
// FIXME: should indicate that 'p2' is negative | ||
int d = p1 - p2; // expected-warning{{Using a negative array index at pointer subtraction is undefined behavior}} \ | ||
// expected-note{{Using a negative array index at pointer subtraction is undefined behavior}} | ||
} | ||
|
||
void different_1() { | ||
int a[3]; // expected-note{{Array at the left-hand side of subtraction}} | ||
int b[3]; // expected-note{{Array at the right-hand side of subtraction}} | ||
int d = &a[2] - &b[0]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
} | ||
|
||
void different_2() { | ||
int a[3]; // expected-note{{Array at the right-hand side of subtraction}} | ||
int b[3]; // expected-note{{Array at the left-hand side of subtraction}} | ||
int *p1 = a + 1; | ||
int *p2 = b; | ||
int d = p2 - p1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
} | ||
|
||
int different_3() { | ||
struct { | ||
int array[5]; | ||
} a, b; | ||
return &a.array[3] - &b.array[2]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
} | ||
|
||
int different_4() { | ||
struct { | ||
int array1[5]; // expected-note{{Array at the left-hand side of subtraction}} | ||
int array2[5]; // expected-note{{Array at the right-hand side of subtraction}} | ||
} a; | ||
return &a.array1[3] - &a.array2[4]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
} | ||
|
||
void different_5() { | ||
int d; | ||
static int x[10][10]; // expected-note2{{Array at the left-hand side of subtraction}} | ||
int *y1 = &(x[3][5]); | ||
char *z = ((char *) y1) + 2; | ||
int *y2 = (int *)(z - 2); | ||
int *y3 = ((int *)x) + 35; // This is offset for [3][5]. | ||
|
||
d = y2 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
d = y3 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \ | ||
// expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}} | ||
d = y3 - y2; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
FieldRegion
s areDeclRegion
s, so these declarations may be data member declarations within astruct
orclass
. This is usually not a problem, but there is a corner case whereSuperLR != SuperRR
, but the corresponding declarations are identical:In this case the current code would place both notes onto the declaration of
field
, which would be confusing for the user.Consider adding some code that handles this situation explicitly. (Either simply skip note creation when
DiffDeclL == DiffDeclR
, or create a specialized note for this case.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning is now omitted if both would be at the same declaration.