-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Reapply "[analyzer] Handle [[assume(cond)]] as __builtin_assume(cond)" #129234
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
Changes from 2 commits
67d6420
2e7c4e7
bed6511
f493f9b
54f3aea
e1625b8
d3790ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -433,7 +433,7 @@ class reverse_children { | |
ArrayRef<Stmt *> children; | ||
|
||
public: | ||
reverse_children(Stmt *S); | ||
reverse_children(Stmt *S, ASTContext &Ctx); | ||
|
||
using iterator = ArrayRef<Stmt *>::reverse_iterator; | ||
|
||
|
@@ -443,21 +443,44 @@ class reverse_children { | |
|
||
} // namespace | ||
|
||
reverse_children::reverse_children(Stmt *S) { | ||
if (CallExpr *CE = dyn_cast<CallExpr>(S)) { | ||
children = CE->getRawSubExprs(); | ||
reverse_children::reverse_children(Stmt *S, ASTContext &Ctx) { | ||
switch (S->getStmtClass()) { | ||
case Stmt::CallExprClass: { | ||
children = cast<CallExpr>(S)->getRawSubExprs(); | ||
return; | ||
} | ||
switch (S->getStmtClass()) { | ||
// Note: Fill in this switch with more cases we want to optimize. | ||
case Stmt::InitListExprClass: { | ||
InitListExpr *IE = cast<InitListExpr>(S); | ||
children = llvm::ArrayRef(reinterpret_cast<Stmt **>(IE->getInits()), | ||
IE->getNumInits()); | ||
return; | ||
|
||
// Note: Fill in this switch with more cases we want to optimize. | ||
case Stmt::InitListExprClass: { | ||
InitListExpr *IE = cast<InitListExpr>(S); | ||
children = llvm::ArrayRef(reinterpret_cast<Stmt **>(IE->getInits()), | ||
IE->getNumInits()); | ||
return; | ||
} | ||
|
||
case Stmt::AttributedStmtClass: { | ||
// for an attributed stmt, the "children()" returns only the NullStmt | ||
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. Nit: start sentence with a capital letter. 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. Fixed. |
||
// (;) but semantically the "children" are supposed to be the | ||
// expressions _within_ i.e. the two square brackets i.e. [[ HERE ]] | ||
// so we add the subexpressions first, _then_ add the "children" | ||
auto *AS = cast<AttributedStmt>(S); | ||
for (const auto *Attr : AS->getAttrs()) { | ||
if (const auto *AssumeAttr = dyn_cast<CXXAssumeAttr>(Attr)) { | ||
Expr *AssumeExpr = AssumeAttr->getAssumption(); | ||
if (!AssumeExpr->HasSideEffects(Ctx)) { | ||
Xazax-hun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
childrenBuf.push_back(AssumeExpr); | ||
} | ||
} | ||
} | ||
default: | ||
break; | ||
|
||
// Visit the actual children AST nodes. | ||
// For CXXAssumeAttrs, this is always a NullStmt. | ||
llvm::append_range(childrenBuf, AS->children()); | ||
children = childrenBuf; | ||
return; | ||
} | ||
default: | ||
break; | ||
} | ||
|
||
// Default case for all other statements. | ||
|
@@ -2433,7 +2456,7 @@ CFGBlock *CFGBuilder::VisitChildren(Stmt *S) { | |
|
||
// Visit the children in their reverse order so that they appear in | ||
// left-to-right (natural) order in the CFG. | ||
reverse_children RChildren(S); | ||
reverse_children RChildren(S, *Context); | ||
for (Stmt *Child : RChildren) { | ||
if (Child) | ||
if (CFGBlock *R = Visit(Child)) | ||
|
@@ -2449,7 +2472,7 @@ CFGBlock *CFGBuilder::VisitInitListExpr(InitListExpr *ILE, AddStmtChoice asc) { | |
} | ||
CFGBlock *B = Block; | ||
|
||
reverse_children RChildren(ILE); | ||
reverse_children RChildren(ILE, *Context); | ||
for (Stmt *Child : RChildren) { | ||
if (!Child) | ||
continue; | ||
|
@@ -2484,6 +2507,14 @@ static bool isFallthroughStatement(const AttributedStmt *A) { | |
return isFallthrough; | ||
} | ||
|
||
static bool isCXXAssumeAttr(const AttributedStmt *A) { | ||
bool hasAssumeAttr = hasSpecificAttr<CXXAssumeAttr>(A->getAttrs()); | ||
|
||
assert((!hasAssumeAttr || isa<NullStmt>(A->getSubStmt())) && | ||
"expected [[assume]] not to have children"); | ||
return hasAssumeAttr; | ||
} | ||
|
||
CFGBlock *CFGBuilder::VisitAttributedStmt(AttributedStmt *A, | ||
AddStmtChoice asc) { | ||
// AttributedStmts for [[likely]] can have arbitrary statements as children, | ||
|
@@ -2494,7 +2525,8 @@ CFGBlock *CFGBuilder::VisitAttributedStmt(AttributedStmt *A, | |
// So only add the AttributedStmt for FallThrough, which has CFG effects and | ||
// also no children, and omit the others. None of the other current StmtAttrs | ||
// have semantic meaning for the CFG. | ||
if (isFallthroughStatement(A) && asc.alwaysAdd(*this, A)) { | ||
bool isInterestingAttribute = isFallthroughStatement(A) || isCXXAssumeAttr(A); | ||
if (isInterestingAttribute && asc.alwaysAdd(*this, A)) { | ||
autoCreateBlock(); | ||
appendStmt(Block, A); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -796,17 +796,18 @@ void ExprEngine::VisitGuardedExpr(const Expr *Ex, | |
|
||
// Find the predecessor block. | ||
ProgramStateRef SrcState = state; | ||
|
||
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. Nit: do we want this new line here? I have no strong feelings. 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. Removed. |
||
for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) { | ||
ProgramPoint PP = N->getLocation(); | ||
if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) { | ||
auto Edge = N->getLocationAs<BlockEdge>(); | ||
if (!Edge.has_value()) { | ||
// If the state N has multiple predecessors P, it means that successors | ||
// of P are all equivalent. | ||
// In turn, that means that all nodes at P are equivalent in terms | ||
// of observable behavior at N, and we can follow any of them. | ||
// FIXME: a more robust solution which does not walk up the tree. | ||
continue; | ||
} | ||
SrcBlock = PP.castAs<BlockEdge>().getSrc(); | ||
SrcBlock = Edge->getSrc(); | ||
SrcState = N->getState(); | ||
break; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// RUN: %clang_analyze_cc1 -std=c++23 -triple x86_64-pc-linux-gnu \ | ||
// RUN: -analyzer-checker=core,debug.ExprInspection -verify %s | ||
|
||
template <typename T> void clang_analyzer_dump(T); | ||
template <typename T> void clang_analyzer_value(T); | ||
|
||
int ternary_in_builtin_assume(int a, int b) { | ||
__builtin_assume(a > 10 ? b == 4 : b == 10); | ||
|
||
clang_analyzer_value(a); | ||
// expected-warning@-1 {{[-2147483648, 10]}} | ||
// expected-warning@-2 {{[11, 2147483647]}} | ||
|
||
clang_analyzer_dump(b); // expected-warning{{4}} expected-warning{{10}} | ||
|
||
if (a > 20) { | ||
clang_analyzer_dump(b + 100); // expected-warning {{104}} | ||
return 2; | ||
} | ||
if (a > 10) { | ||
clang_analyzer_dump(b + 200); // expected-warning {{204}} | ||
return 1; | ||
} | ||
clang_analyzer_dump(b + 300); // expected-warning {{310}} | ||
return 0; | ||
} | ||
|
||
// From: https://github.com/llvm/llvm-project/pull/116462#issuecomment-2517853226 | ||
int ternary_in_assume(int a, int b) { | ||
// FIXME notes | ||
// Currently, if this test is run without the core.builtin.Builtin checker, the above function with the __builtin_assume behaves identically to the following test | ||
// i.e. calls to `clang_analyzer_dump` result in "extraneous" prints of the SVal(s) `reg<int b> ...` | ||
// as opposed to 4 or 10 | ||
// which likely implies the Program State(s) did not get narrowed. | ||
// A new checker is likely needed to be implemented to properly handle the expressions within `[[assume]]` to eliminate the states where `b` is not narrowed. | ||
|
||
[[assume(a > 10 ? b == 4 : b == 10)]]; | ||
clang_analyzer_value(a); | ||
// expected-warning@-1 {{[-2147483648, 10]}} | ||
// expected-warning@-2 {{[11, 2147483647]}} | ||
|
||
clang_analyzer_dump(b); // expected-warning {{4}} expected-warning {{10}} | ||
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int b>}} FIXME: We shouldn't have this dump. | ||
|
||
if (a > 20) { | ||
clang_analyzer_dump(b + 100); // expected-warning {{104}} | ||
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 100}} FIXME: We shouldn't have this dump. | ||
return 2; | ||
} | ||
if (a > 10) { | ||
clang_analyzer_dump(b + 200); // expected-warning {{204}} | ||
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 200}} FIXME: We shouldn't have this dump. | ||
return 1; | ||
} | ||
clang_analyzer_dump(b + 300); // expected-warning {{310}} | ||
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<int b>) + 300}} FIXME: We shouldn't have this dump. | ||
return 0; | ||
} | ||
|
||
int assume_and_fallthrough_at_the_same_attrstmt(int a, int b) { | ||
[[assume(a == 2)]]; | ||
clang_analyzer_dump(a); // expected-warning {{2 S32b}} | ||
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int a>}} FIXME: We shouldn't have this dump. | ||
switch (a) { | ||
case 2: | ||
[[fallthrough, assume(b == 30)]]; | ||
case 4: { | ||
clang_analyzer_dump(b); // expected-warning {{30 S32b}} | ||
// expected-warning-re@-1 {{reg_${{[0-9]+}}<int b>}} FIXME: We shouldn't have this dump. | ||
return b; | ||
} | ||
} | ||
// This code should be unreachable. | ||
[[assume(false)]]; // This should definitely make it so. | ||
clang_analyzer_dump(33); // expected-warning {{33 S32b}} | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -analyzer-checker=unix,core,security.ArrayBound -verify %s | ||
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -verify %s \ | ||
// RUN: -analyzer-checker=unix,core,security.ArrayBound,debug.ExprInspection | ||
|
||
void clang_analyzer_eval(bool); | ||
|
||
// Tests doing an out-of-bounds access after the end of an array using: | ||
// - constant integer index | ||
|
@@ -180,3 +183,58 @@ int test_reference_that_might_be_after_the_end(int idx) { | |
return ref; | ||
} | ||
|
||
// From: https://github.com/llvm/llvm-project/issues/100762 | ||
extern int arrOf10[10]; | ||
void using_builtin(int x) { | ||
__builtin_assume(x > 101); // CallExpr | ||
arrOf10[x] = 404; // expected-warning {{Out of bound access to memory}} | ||
} | ||
|
||
void using_assume_attr(int ax) { | ||
[[assume(ax > 100)]]; // NullStmt with an "assume" attribute. | ||
arrOf10[ax] = 405; // expected-warning {{Out of bound access to memory}} | ||
} | ||
|
||
void using_many_assume_attr(int yx) { | ||
[[assume(yx > 104), assume(yx > 200), assume(yx < 300)]]; // NullStmt with an attribute | ||
arrOf10[yx] = 406; // expected-warning{{Out of bound access to memory}} | ||
} | ||
|
||
|
||
int using_builtin_assume_has_no_sideeffects(int y) { | ||
// We should not apply sideeffects of the argument of [[assume(...)]]. | ||
// "y" should not get incremented; | ||
__builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}} | ||
clang_analyzer_eval(y == 42); // expected-warning {{FALSE}} | ||
return y; | ||
} | ||
|
||
|
||
|
||
int using_assume_attr_has_no_sideeffects(int y) { | ||
|
||
// We should not apply sideeffects of the argument of [[assume(...)]]. | ||
// "y" should not get incremented; | ||
[[assume(++y == 43)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} | ||
|
||
clang_analyzer_eval(y == 42); // expected-warning {{TRUE}} expected-warning {{FALSE}} FIXME: This should be only TRUE. | ||
|
||
clang_analyzer_eval(y == 43); // expected-warning {{FALSE}} expected-warning {{TRUE}} FIXME: This should be only FALSE. | ||
|
||
return y; | ||
} | ||
|
||
|
||
int using_builtinassume_has_no_sideeffects(int u) { | ||
// We should not apply sideeffects of the argument of __builtin_assume(...) | ||
// "u" should not get incremented; | ||
__builtin_assume(++u == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}} | ||
|
||
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. I am a bit confused why is this not addressed by the 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. Actually, in the test above we had the right outcome: Speaking of the test with the builtin assume, it was slightly different. |
||
// FIXME: evaluate this to true | ||
clang_analyzer_eval(u == 42); // expected-warning {{FALSE}} current behavior | ||
|
||
// FIXME: evaluate this to false | ||
clang_analyzer_eval(u == 43); // expected-warning {{TRUE}} current behavior | ||
|
||
return u; | ||
} |
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.
Is this equivalent what we had before? What about the different subclasses of
CallExpr
?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.
In the past it was implicit in the
dyn_cast<CallExpr>
. So it's the same as before.Uh oh!
There was an error while loading. Please reload this page.
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.
I think
dyn_cast
would succeed forCXXMemberCallExpr
and similar subclasses becauseclassof
inCallExpr
is implemented like:On the other hand, I'd expect the stmt class to be
CallExprClass
only when it is not a derived type.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.
Thanks, fixed.