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
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
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   short *b = malloc(64);
22   b[5] = *a + a[1] + 2;
23 #endif
24 }
25 
26 // CHECK-LABEL: @f3
27 void f3(void) {
28   int a[1];
29   // CHECK: call {{.*}} @llvm.{{(ubsan)?trap}}
30   a[2] = 1;
31 }
32 
33 union U { int a[0]; int b[1]; int c[2]; };
34 
35 // CHECK-LABEL: define {{.*}} @f4
36 int f4(union U *u, int i) {
37   // a and b are treated as flexible array members.
38   // CHECK-NOT: @llvm.ubsantrap
39   return u->a[i] + u->b[i];
40   // CHECK: }
41 }
42 
43 // CHECK-LABEL: define {{.*}} @f5
44 int f5(union U *u, int i) {
45   // c is not a flexible array member.
46   // NONLOCAL: call {{.*}} @llvm.ubsantrap
47   return u->c[i];
48   // CHECK: }
49 }
50 
51 __attribute__((no_sanitize("bounds")))
52 int f6(int i) {
53 	int b[64];
54 	// CHECK-NOT: call void @llvm.trap()
55 	// CHECK-NOT: trap:
56 	// CHECK-NOT: cont:
57 	return b[i];
58 }
59