30 lines · cpp
1// RUN: %clangxx_min_runtime -fsanitize=local-bounds %s -O3 -o %t && %run %t 12// RUN: %clangxx_min_runtime -fsanitize=local-bounds %s -O3 -o %t && not --crash %run %t 33// RUN: %clangxx_min_runtime -fsanitize=local-bounds -fno-sanitize-trap=local-bounds %s -O3 -o %t && not --crash %run %t 3 2>&1 | FileCheck %s4// RUN: %clangxx_min_runtime -fsanitize=local-bounds -fno-sanitize-trap=local-bounds -fsanitize-recover=local-bounds %s -O3 -o %t && %run %t 3 2>&1 | FileCheck %s5 6#include <cstdlib>7 8struct 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 // CHECK: ubsan: local-out-of-bounds by 0x{{[[:xdigit:]]+$}}24}25 26int main(int argc, char **argv) {27 test(argv[1][0] - '0');28 return 0;29}30