Skip to content

Commit 0fd7c49

Browse files
authored
[ubsan] Add runtime test for -fsanitize=local-bounds (#120038)
1 parent e34cc7c commit 0fd7c49

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && %run %t 1
2+
// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && not --crash %run %t 3
3+
4+
// FIXME: it's always trap for now.
5+
6+
#include <cstdlib>
7+
8+
struct S {
9+
int k;
10+
int l;
11+
};
12+
13+
__attribute__((noinline)) void init(S *s) {
14+
__asm__ __volatile__("" : : "r"(s) : "memory");
15+
}
16+
17+
__attribute__((noinline, no_sanitize("memory"))) int test(char i) {
18+
S a;
19+
init(&a);
20+
S b;
21+
init(&b);
22+
return ((int *)(&a))[i];
23+
}
24+
25+
int main(int argc, char **argv) {
26+
test(argv[1][0] - '0');
27+
return 0;
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && %run %t 1
2+
// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && not --crash %run %t 3
3+
4+
// FIXME: it's always trap for now.
5+
6+
#include <cstdlib>
7+
8+
struct S {
9+
int k;
10+
int l;
11+
};
12+
13+
__attribute__((noinline)) void init(S *s) {
14+
__asm__ __volatile__("" : : "r"(s) : "memory");
15+
}
16+
17+
__attribute__((noinline, no_sanitize("memory"))) int test(char i) {
18+
S a;
19+
init(&a);
20+
S b;
21+
init(&b);
22+
return ((int *)(&a))[i];
23+
}
24+
25+
int main(int argc, char **argv) {
26+
test(argv[1][0] - '0');
27+
return 0;
28+
}

0 commit comments

Comments
 (0)