Skip to content

Commit 405c28b

Browse files
authored
[NFC][analyzer] Split [[assume]] tests to a separate file (#130763)
Recently commit 7e5821b (which is a re-application of 89da344) added some tests to `out-of-bounds-new.cpp`, which use a very simple out of bounds report to reveal the internal state of the analyzer, but are otherwise completely unrelated to the checker `security.ArrayBound`, which is tested in `out-of-bounds-new.cpp`. (Instead, they test handling of `__builtin_assume` and `[[assume()]]` annotations.) This commit reverts `out-of-bounds-new.cpp` to its previous state and moves the new tests to a separate test file.
1 parent 5c8760b commit 405c28b

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %clang_analyze_cc1 -std=c++11 -verify %s \
2+
// RUN: -triple=x86_64-unknown-linux-gnu \
3+
// RUN: -analyzer-checker=core,security.ArrayBound,debug.ExprInspection
4+
5+
void clang_analyzer_eval(bool);
6+
void clang_analyzer_value(int);
7+
void clang_analyzer_dump(int);
8+
9+
// From: https://github.com/llvm/llvm-project/issues/100762
10+
extern int arrOf10[10];
11+
void using_builtin(int x) {
12+
__builtin_assume(x > 101); // CallExpr
13+
arrOf10[x] = 404; // expected-warning {{Out of bound access to memory}}
14+
}
15+
16+
void using_assume_attr(int ax) {
17+
[[assume(ax > 100)]]; // NullStmt with an "assume" attribute.
18+
arrOf10[ax] = 405; // expected-warning {{Out of bound access to memory}}
19+
}
20+
21+
void using_many_assume_attr(int yx) {
22+
[[assume(yx > 104), assume(yx > 200), assume(yx < 300)]]; // NullStmt with an attribute
23+
arrOf10[yx] = 406; // expected-warning{{Out of bound access to memory}}
24+
}
25+
26+
int using_assume_attr_has_no_sideeffects(int y) {
27+
int orig_y = y;
28+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
29+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
30+
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
31+
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
32+
33+
// We should not apply sideeffects of the argument of [[assume(...)]].
34+
// "y" should not get incremented;
35+
[[assume(++y == 43)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
36+
37+
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
38+
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
39+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
40+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
41+
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
42+
43+
return y;
44+
}
45+
46+
int using_builtin_assume_has_no_sideeffects(int y) {
47+
int orig_y = y;
48+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
49+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
50+
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
51+
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
52+
53+
// We should not apply sideeffects of the argument of __builtin_assume(...)
54+
// "u" should not get incremented;
55+
__builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
56+
57+
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
58+
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
59+
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
60+
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
61+
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
62+
63+
return y;
64+
}

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

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -verify %s \
2-
// RUN: -triple=x86_64-unknown-linux-gnu \
3-
// RUN: -analyzer-checker=unix,core,security.ArrayBound,debug.ExprInspection
4-
5-
void clang_analyzer_eval(bool);
6-
void clang_analyzer_value(int);
7-
void clang_analyzer_dump(int);
1+
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -analyzer-checker=unix,core,security.ArrayBound -verify %s
82

93
// Tests doing an out-of-bounds access after the end of an array using:
104
// - constant integer index
@@ -186,59 +180,3 @@ int test_reference_that_might_be_after_the_end(int idx) {
186180
return ref;
187181
}
188182

189-
// From: https://github.com/llvm/llvm-project/issues/100762
190-
extern int arrOf10[10];
191-
void using_builtin(int x) {
192-
__builtin_assume(x > 101); // CallExpr
193-
arrOf10[x] = 404; // expected-warning {{Out of bound access to memory}}
194-
}
195-
196-
void using_assume_attr(int ax) {
197-
[[assume(ax > 100)]]; // NullStmt with an "assume" attribute.
198-
arrOf10[ax] = 405; // expected-warning {{Out of bound access to memory}}
199-
}
200-
201-
void using_many_assume_attr(int yx) {
202-
[[assume(yx > 104), assume(yx > 200), assume(yx < 300)]]; // NullStmt with an attribute
203-
arrOf10[yx] = 406; // expected-warning{{Out of bound access to memory}}
204-
}
205-
206-
int using_assume_attr_has_no_sideeffects(int y) {
207-
int orig_y = y;
208-
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
209-
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
210-
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
211-
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
212-
213-
// We should not apply sideeffects of the argument of [[assume(...)]].
214-
// "y" should not get incremented;
215-
[[assume(++y == 43)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
216-
217-
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
218-
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
219-
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
220-
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
221-
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
222-
223-
return y;
224-
}
225-
226-
int using_builtin_assume_has_no_sideeffects(int y) {
227-
int orig_y = y;
228-
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
229-
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
230-
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
231-
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
232-
233-
// We should not apply sideeffects of the argument of __builtin_assume(...)
234-
// "u" should not get incremented;
235-
__builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
236-
237-
clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
238-
clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
239-
clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
240-
clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
241-
clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
242-
243-
return y;
244-
}

0 commit comments

Comments
 (0)