| 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 |
| 3 |
// RUN: %clang_cc1 -fsanitize=local-bounds -fsanitize-trap=local-bounds -O3 -mllvm -bounds-checking-unique-traps -emit-llvm -triple x86_64-apple-darwin10 %s -o - | FileCheck %s --check-prefixes=NOOPTLOCAL |
| 4 |
// RUN: %clang_cc1 -fsanitize=array-bounds -fsanitize-trap=array-bounds -O3 -mllvm -ubsan-unique-traps -emit-llvm -triple x86_64-apple-darwin10 %s -o - | FileCheck %s --check-prefixes=NOOPTARRAY |
| 5 |
// |
| 6 |
// REQUIRES: x86-registered-target |
| 7 |
|
| 8 |
// CHECK-LABEL: @f1 |
| 9 |
double f1(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(void) { |
| 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 |
extern void *malloc(__typeof__(sizeof(0))); |
| 24 |
short *b = malloc(64); |
| 25 |
b[5] = *a + a[1] + 2; |
| 26 |
#endif |
| 27 |
} |
| 28 |
|
| 29 |
// CHECK-LABEL: @f3 |
| 30 |
void f3(void) { |
| 31 |
int a[1]; |
| 32 |
// CHECK: call {{.*}} @llvm.{{(ubsan)?trap}} |
| 33 |
a[2] = 1; |
| 34 |
} |
| 35 |
|
| 36 |
// CHECK-LABEL: @f4 |
| 37 |
__attribute__((no_sanitize("bounds"))) |
| 38 |
int f4(int i) { |
| 39 |
int b[64]; |
| 40 |
// CHECK-NOT: call void @llvm.trap() |
| 41 |
// CHECK-NOT: trap: |
| 42 |
// CHECK-NOT: cont: |
| 43 |
return b[i]; |
| 44 |
} |
| 45 |
|
| 46 |
// Union flexible-array memebers are a C99 extension. All array members with a |
| 47 |
// constant size should be considered FAMs. |
| 48 |
|
| 49 |
union U { int a[0]; int b[1]; int c[2]; }; |
| 50 |
|
| 51 |
// CHECK-LABEL: @f5 |
| 52 |
int f5(union U *u, int i) { |
| 53 |
// a is treated as a flexible array member. |
| 54 |
// CHECK-NOT: @llvm.ubsantrap |
| 55 |
return u->a[i]; |
| 56 |
} |
| 57 |
|
| 58 |
// CHECK-LABEL: @f6 |
| 59 |
int f6(union U *u, int i) { |
| 60 |
// b is treated as a flexible array member. |
| 61 |
// CHECK-NOT: call {{.*}} @llvm.{{(ubsan)?trap}} |
| 62 |
return u->b[i]; |
| 63 |
} |
| 64 |
|
| 65 |
// CHECK-LABEL: @f7 |
| 66 |
int f7(union U *u, int i) { |
| 67 |
// c is treated as a flexible array member. |
| 68 |
// CHECK-NOT: @llvm.ubsantrap |
| 69 |
return u->c[i]; |
| 70 |
} |
| 71 |
|
| 72 |
char B[10]; |
| 73 |
char B2[10]; |
| 74 |
// CHECK-LABEL: @f8 |
| 75 |
void f8(int i, int k) { |
| 76 |
// NOOPTLOCAL: call void @llvm.ubsantrap(i8 3) |
| 77 |
// NOOPTARRAY: call void @llvm.ubsantrap(i8 2) |
| 78 |
B[i] = '\0'; |
| 79 |
|
| 80 |
// NOOPTLOCAL: call void @llvm.ubsantrap(i8 5) |
| 81 |
// NOOPTARRAY: call void @llvm.ubsantrap(i8 4) |
| 82 |
B2[k] = '\0'; |
| 83 |
} |
| 84 |
|
| 85 |
// See commit 9a954c6 that caused a SEGFAULT in this code. |
| 86 |
struct S { |
| 87 |
__builtin_va_list ap; |
| 88 |
} *s; |
| 89 |
// CHECK-LABEL: @f9 |
| 90 |
struct S *f9(int i) { |
| 91 |
return &s[i]; |
| 92 |
} |