| 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 | |
| 9 | struct 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 |
| 19 | test(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 | |
| 31 | int main(int argc, char **argv) { |
| 32 | test(i: argv[1][0] - '0'); |
| 33 | return 0; |
| 34 | } |
| 35 | |