1 // RUN: %clang_cc1 -fsanitize=local-bounds -emit-llvm -triple x86_64-apple-darwin10 %s -o - | FileCheck %s
2 // RUN: %clang_cc1 -fsanitize=local-bounds -fexperimental-new-pass-manager -emit-llvm -triple x86_64-apple-darwin10 %s -o - | FileCheck %s
3 // RUN: %clang_cc1 -fsanitize=array-bounds -O -fsanitize-trap=array-bounds -emit-llvm -triple x86_64-apple-darwin10 -DNO_DYNAMIC %s -o - | FileCheck %s --check-prefixes=CHECK,NONLOCAL
4 // RUN: %clang_cc1 -fsanitize=array-bounds -O -fsanitize-trap=array-bounds -fexperimental-new-pass-manager -emit-llvm -triple x86_64-apple-darwin10 -DNO_DYNAMIC %s -o - | FileCheck %s --check-prefixes=CHECK,NONLOCAL
5 //
6 // REQUIRES: x86-registered-target
7 
8 // CHECK-LABEL: @f
9 double f(int b, int i) {
10   double a[b];
11   // CHECK: call {{.*}} @llvm.{{(ubsan)?trap}}
12   return a[i];
13 }
14 
15 // CHECK-LABEL: @f2
16 void f2() {
17   // everything is constant; no trap possible
18   // CHECK-NOT: call {{.*}} @llvm.{{(ubsan)?trap}}
19   int a[2];
20   a[1] = 42;
21 
22 #ifndef NO_DYNAMIC
23   short *b = malloc(64);
24   b[5] = *a + a[1] + 2;
25 #endif
26 }
27 
28 // CHECK-LABEL: @f3
29 void f3() {
30   int a[1];
31   // CHECK: call {{.*}} @llvm.{{(ubsan)?trap}}
32   a[2] = 1;
33 }
34 
35 union U { int a[0]; int b[1]; int c[2]; };
36 
37 // CHECK-LABEL: define {{.*}} @f4
38 int f4(union U *u, int i) {
39   // a and b are treated as flexible array members.
40   // CHECK-NOT: @llvm.ubsantrap
41   return u->a[i] + u->b[i];
42   // CHECK: }
43 }
44 
45 // CHECK-LABEL: define {{.*}} @f5
46 int f5(union U *u, int i) {
47   // c is not a flexible array member.
48   // NONLOCAL: call {{.*}} @llvm.ubsantrap
49   return u->c[i];
50   // CHECK: }
51 }
52