| 1 |
// RUN: %clang_cc1 %s -emit-llvm -w -triple x86_64-apple-darwin10 -fsanitize=array-bounds -o - | FileCheck %s |
| 2 |
|
| 3 |
// CHECK-LABEL: define{{.*}} i32 @foo( |
| 4 |
int foo(int *const p __attribute__((pass_object_size(0))), int n) { |
| 5 |
int x = (p)[n]; |
| 6 |
|
| 7 |
// CHECK: [[SIZE_ALLOCA:%.*]] = alloca i64, align 8 |
| 8 |
// CHECK: store i64 %{{.*}}, ptr [[SIZE_ALLOCA]], align 8 |
| 9 |
// CHECK: [[LOAD_SIZE:%.*]] = load i64, ptr [[SIZE_ALLOCA]], align 8, !nosanitize |
| 10 |
// CHECK-NEXT: [[SCALED_SIZE:%.*]] = udiv i64 [[LOAD_SIZE]], 4, !nosanitize |
| 11 |
// CHECK-NEXT: [[SEXT_N:%.*]] = sext i32 %{{.*}} to i64, !nosanitize |
| 12 |
// CHECK-NEXT: [[ICMP:%.*]] = icmp ult i64 [[SEXT_N]], [[SCALED_SIZE]], !nosanitize |
| 13 |
// CHECK-NEXT: br i1 [[ICMP]], {{.*}} !nosanitize |
| 14 |
// CHECK: __ubsan_handle_out_of_bounds |
| 15 |
|
| 16 |
{ |
| 17 |
int **q = &p; |
| 18 |
int **p = q; // Shadow the parameter. The pass_object_size info is lost. |
| 19 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 20 |
x = *p[n]; |
| 21 |
} |
| 22 |
|
| 23 |
// CHECK: ret i32 |
| 24 |
return x; |
| 25 |
} |
| 26 |
|
| 27 |
typedef struct {} ZeroSizedType; |
| 28 |
|
| 29 |
// CHECK-LABEL: define{{.*}} void @bar( |
| 30 |
ZeroSizedType bar(ZeroSizedType *const p __attribute__((pass_object_size(0))), int n) { |
| 31 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 32 |
// CHECK: ret void |
| 33 |
return p[n]; |
| 34 |
} |
| 35 |
|
| 36 |
// CHECK-LABEL: define{{.*}} i32 @baz( |
| 37 |
int baz(int *const p __attribute__((pass_object_size(1))), int n) { |
| 38 |
// CHECK: __ubsan_handle_out_of_bounds |
| 39 |
// CHECK: ret i32 |
| 40 |
return p[n]; |
| 41 |
} |
| 42 |
|
| 43 |
// CHECK-LABEL: define{{.*}} i32 @mat( |
| 44 |
int mat(int *const p __attribute__((pass_object_size(2))), int n) { |
| 45 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 46 |
// CHECK: ret i32 |
| 47 |
return p[n]; |
| 48 |
} |
| 49 |
|
| 50 |
// CHECK-LABEL: define{{.*}} i32 @pat( |
| 51 |
int pat(int *const p __attribute__((pass_object_size(3))), int n) { |
| 52 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 53 |
// CHECK: ret i32 |
| 54 |
return p[n]; |
| 55 |
} |
| 56 |
|
| 57 |
// CHECK-LABEL: define{{.*}} i32 @cat( |
| 58 |
int cat(int p[static 10], int n) { |
| 59 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 60 |
// CHECK: ret i32 |
| 61 |
return p[n]; |
| 62 |
} |
| 63 |
|
| 64 |
// CHECK-LABEL: define{{.*}} i32 @bat( |
| 65 |
int bat(int n, int p[n]) { |
| 66 |
// CHECK-NOT: __ubsan_handle_out_of_bounds |
| 67 |
// CHECK: ret i32 |
| 68 |
return p[n]; |
| 69 |
} |