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 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 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 37 int f4(union U *u, int i) { 38 // a and b bounds are treated as flexible array members, but they are inside a union 39 // and that prevent them from being considered as flexible array members. 40 // NONLOCAL: @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 53 __attribute__((no_sanitize("bounds"))) 54 int f6(int i) { 55 int b[64]; 56 // CHECK-NOT: call void @llvm.trap() 57 // CHECK-NOT: trap: 58 // CHECK-NOT: cont: 59 return b[i]; 60 } 61