Skip to content

Commit e1625b8

Browse files
committed
Fix buildin_assume callexprs with sideeffects, pin and clean tests
1 parent 54f3aea commit e1625b8

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

clang/lib/Analysis/CFG.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,16 @@ static bool CanThrow(Expr *E, ASTContext &Ctx) {
27322732
return true;
27332733
}
27342734

2735+
static bool isBuiltinAssumeWithSideEffects(const ASTContext &Ctx,
2736+
const CallExpr *CE) {
2737+
unsigned BuiltinID = CE->getBuiltinCallee();
2738+
if (BuiltinID != Builtin::BI__assume &&
2739+
BuiltinID != Builtin::BI__builtin_assume)
2740+
return false;
2741+
2742+
return CE->getArg(0)->HasSideEffects(Ctx);
2743+
}
2744+
27352745
CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
27362746
// Compute the callee type.
27372747
QualType calleeType = C->getCallee()->getType();
@@ -2770,7 +2780,8 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
27702780
NoReturn = true;
27712781
if (FD->hasAttr<NoThrowAttr>())
27722782
AddEHEdge = false;
2773-
if (FD->getBuiltinID() == Builtin::BI__builtin_object_size ||
2783+
if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) ||
2784+
FD->getBuiltinID() == Builtin::BI__builtin_object_size ||
27742785
FD->getBuiltinID() == Builtin::BI__builtin_dynamic_object_size)
27752786
OmitArguments = true;
27762787
}

clang/test/Analysis/out-of-bounds-new.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -verify %s \
2+
// RUN: -triple=x86_64-unknown-linux-gnu \
23
// RUN: -analyzer-checker=unix,core,security.ArrayBound,debug.ExprInspection
34

45
void clang_analyzer_eval(bool);
6+
void clang_analyzer_value(int);
57

68
// Tests doing an out-of-bounds access after the end of an array using:
79
// - constant integer index
@@ -200,41 +202,34 @@ void using_many_assume_attr(int yx) {
200202
arrOf10[yx] = 406; // expected-warning{{Out of bound access to memory}}
201203
}
202204

203-
204-
int using_builtin_assume_has_no_sideeffects(int y) {
205-
// We should not apply sideeffects of the argument of [[assume(...)]].
206-
// "y" should not get incremented;
207-
__builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
208-
clang_analyzer_eval(y == 42); // expected-warning {{FALSE}}
209-
return y;
210-
}
211-
212-
213-
214205
int using_assume_attr_has_no_sideeffects(int y) {
206+
int orig_y = y;
207+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
208+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
215209

216210
// We should not apply sideeffects of the argument of [[assume(...)]].
217211
// "y" should not get incremented;
218212
[[assume(++y == 43)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
219213

220-
clang_analyzer_eval(y == 42); // expected-warning {{TRUE}} expected-warning {{FALSE}} FIXME: This should be only TRUE.
221-
222-
clang_analyzer_eval(y == 43); // expected-warning {{FALSE}} expected-warning {{TRUE}} FIXME: This should be only FALSE.
214+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
215+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
216+
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
223217

224218
return y;
225219
}
226220

221+
int using_builtin_assume_has_no_sideeffects(int y) {
222+
int orig_y = y;
223+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
224+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
227225

228-
int using_builtinassume_has_no_sideeffects(int u) {
229226
// We should not apply sideeffects of the argument of __builtin_assume(...)
230227
// "u" should not get incremented;
231-
__builtin_assume(++u == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
228+
__builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
232229

233-
// FIXME: evaluate this to true
234-
clang_analyzer_eval(u == 42); // expected-warning {{FALSE}} current behavior
235-
236-
// FIXME: evaluate this to false
237-
clang_analyzer_eval(u == 43); // expected-warning {{TRUE}} current behavior
230+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
231+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
232+
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
238233

239-
return u;
234+
return y;
240235
}

0 commit comments

Comments
 (0)