Clang Project

clang_source_code/test/CodeGen/ubsan-pass-object-size.c
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(
4int 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 %{{.*}}, i64* [[SIZE_ALLOCA]], align 8
9  // CHECK: [[LOAD_SIZE:%.*]] = load i64, i64* [[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 **p = &p; // Shadow the parameter. The pass_object_size info is lost.
18    // CHECK-NOT: __ubsan_handle_out_of_bounds
19    x = *p[n];
20  }
21
22  // CHECK: ret i32
23  return x;
24}
25
26typedef struct {} ZeroSizedType;
27
28// CHECK-LABEL: define void @bar(
29ZeroSizedType bar(ZeroSizedType *const p __attribute__((pass_object_size(0))), int n) {
30  // CHECK-NOT: __ubsan_handle_out_of_bounds
31  // CHECK: ret void
32  return p[n];
33}
34
35// CHECK-LABEL: define i32 @baz(
36int baz(int *const p __attribute__((pass_object_size(1))), int n) {
37  // CHECK: __ubsan_handle_out_of_bounds
38  // CHECK: ret i32
39  return p[n];
40}
41
42// CHECK-LABEL: define i32 @mat(
43int mat(int *const p __attribute__((pass_object_size(2))), int n) {
44  // CHECK-NOT: __ubsan_handle_out_of_bounds
45  // CHECK: ret i32
46  return p[n];
47}
48
49// CHECK-LABEL: define i32 @pat(
50int pat(int *const p __attribute__((pass_object_size(3))), int n) {
51  // CHECK-NOT: __ubsan_handle_out_of_bounds
52  // CHECK: ret i32
53  return p[n];
54}
55
56// CHECK-LABEL: define i32 @cat(
57int cat(int p[static 10], int n) {
58  // CHECK-NOT: __ubsan_handle_out_of_bounds
59  // CHECK: ret i32
60  return p[n];
61}
62
63// CHECK-LABEL: define i32 @bat(
64int bat(int n, int p[n]) {
65  // CHECK-NOT: __ubsan_handle_out_of_bounds
66  // CHECK: ret i32
67  return p[n];
68}
69