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