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// RUN: %clangxx -fsanitize=local-bounds -fno-sanitize-trap=local-bounds %s -O3 -o %t && not %run %t 3 2>&1 | FileCheck %s
4// RUN: %clangxx -fsanitize=local-bounds -fno-sanitize-trap=local-bounds -fsanitize-recover=local-bounds %s -O3 -o %t && %run %t 3 2>&1 | FileCheck %s
5// RUN: %clangxx -fsanitize=local-bounds -fno-sanitize-trap=local-bounds -fsanitize-recover=local-bounds -g %s -O3 -o %t && %run %t 3 2>&1 | FileCheck %s --check-prefixes=LINE
6
7#include <cstdlib>
8
9struct S {
10 int k;
11 int l;
12};
13
14__attribute__((noinline)) void init(S *s) {
15 __asm__ __volatile__("" : : "r"(s) : "memory");
16}
17
18__attribute__((noinline, no_sanitize("memory", "address", "hwaddress"))) int
19test(char i) {
20 S a;
21 init(s: &a);
22 S b;
23 init(s: &b);
24 return ((int *)(&a))[i];
25 // CHECK: error: access out of bounds
26 // CHECK: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
27 // LINE: local_bounds.cpp:[[#@LINE-3]]:{{.*}}runtime error: access out of bounds
28 // LINE: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior {{.*}}local_bounds.cpp:[[#@LINE-4]]
29}
30
31int main(int argc, char **argv) {
32 test(i: argv[1][0] - '0');
33 return 0;
34}
35

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of compiler-rt/test/ubsan/TestCases/Misc/local_bounds.cpp