Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - | FileCheck %s |
Benjamin Kramer | cc179cb | 2009-10-27 12:19:13 +0000 | [diff] [blame] | 2 | |
| 3 | #define strcpy(dest, src) \ |
| 4 | ((__builtin_object_size(dest, 0) != -1ULL) \ |
| 5 | ? __builtin___strcpy_chk (dest, src, __builtin_object_size(dest, 1)) \ |
| 6 | : __inline_strcpy_chk(dest, src)) |
| 7 | |
| 8 | static char *__inline_strcpy_chk (char *dest, const char *src) { |
| 9 | return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1)); |
| 10 | } |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 11 | |
| 12 | char gbuf[63]; |
| 13 | char *gp; |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 14 | int gi, gj; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 15 | |
| 16 | void test1() { |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 17 | // CHECK: = call i8* @__strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i32 0, i64 4), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 59) |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 18 | strcpy(&gbuf[4], "Hi there"); |
| 19 | } |
| 20 | |
| 21 | void test2() { |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 22 | // CHECK: = call i8* @__strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 63) |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 23 | strcpy(gbuf, "Hi there"); |
| 24 | } |
| 25 | |
Mike Stump | 06bc9bc | 2009-10-26 21:38:39 +0000 | [diff] [blame] | 26 | void test3() { |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 27 | // CHECK: = call i8* @__strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i64 1, i64 37), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 0) |
Mike Stump | 06bc9bc | 2009-10-26 21:38:39 +0000 | [diff] [blame] | 28 | strcpy(&gbuf[100], "Hi there"); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Mike Stump | 06bc9bc | 2009-10-26 21:38:39 +0000 | [diff] [blame] | 31 | void test4() { |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 32 | // CHECK: = call i8* @__strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i32 0, i64 -1), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 0) |
Mike Stump | 06bc9bc | 2009-10-26 21:38:39 +0000 | [diff] [blame] | 33 | strcpy((char*)(void*)&gbuf[-1], "Hi there"); |
| 34 | } |
| 35 | |
| 36 | void test5() { |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 37 | // CHECK: = load i8** @gp |
| 38 | // CHECK-NEXT:= call i64 @llvm.objectsize.i64(i8* %{{.*}}, i1 false) |
Mike Stump | 06bc9bc | 2009-10-26 21:38:39 +0000 | [diff] [blame] | 39 | strcpy(gp, "Hi there"); |
| 40 | } |
| 41 | |
| 42 | void test6() { |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 43 | char buf[57]; |
| 44 | |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 45 | // CHECK: = call i8* @__strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 53) |
Mike Stump | 660e6f7 | 2009-10-26 23:05:19 +0000 | [diff] [blame] | 46 | strcpy(&buf[4], "Hi there"); |
| 47 | } |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 48 | |
| 49 | void test7() { |
| 50 | int i; |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 51 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 52 | // CHECK: = call i8* @__inline_strcpy_chk(i8* getelementptr inbounds ([63 x i8]* @gbuf, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 53 | strcpy((++i, gbuf), "Hi there"); |
| 54 | } |
| 55 | |
| 56 | void test8() { |
| 57 | char *buf[50]; |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 58 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 59 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 60 | strcpy(buf[++gi], "Hi there"); |
| 61 | } |
| 62 | |
| 63 | void test9() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 64 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 65 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 66 | strcpy((char *)((++gi) + gj), "Hi there"); |
| 67 | } |
| 68 | |
| 69 | char **p; |
| 70 | void test10() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 71 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 72 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 48c2af8 | 2009-10-29 23:22:14 +0000 | [diff] [blame] | 73 | strcpy(*(++p), "Hi there"); |
| 74 | } |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 75 | |
| 76 | void test11() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 77 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 78 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 79 | strcpy(gp = gbuf, "Hi there"); |
| 80 | } |
| 81 | |
| 82 | void test12() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 83 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 84 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 85 | strcpy(++gp, "Hi there"); |
| 86 | } |
| 87 | |
| 88 | void test13() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 89 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 90 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 91 | strcpy(gp++, "Hi there"); |
| 92 | } |
| 93 | |
| 94 | void test14() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 95 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 96 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 97 | strcpy(--gp, "Hi there"); |
| 98 | } |
| 99 | |
| 100 | void test15() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 101 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 102 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{..*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 2623aa2 | 2009-10-29 23:29:54 +0000 | [diff] [blame] | 103 | strcpy(gp--, "Hi there"); |
| 104 | } |
Mike Stump | 3f0147e | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 105 | |
| 106 | void test16() { |
Mike Stump | bc773a0 | 2009-12-07 19:22:29 +0000 | [diff] [blame] | 107 | // CHECK-NOT: __strcpy_chk |
Mike Stump | d2635a4 | 2010-01-13 19:40:37 +0000 | [diff] [blame] | 108 | // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0)) |
Mike Stump | 3f0147e | 2009-10-29 23:34:20 +0000 | [diff] [blame] | 109 | strcpy(gp += 1, "Hi there"); |
| 110 | } |
Benjamin Kramer | 3f27b38 | 2010-01-03 18:18:37 +0000 | [diff] [blame] | 111 | |
| 112 | void test17() { |
| 113 | // CHECK: store i32 -1 |
| 114 | gi = __builtin_object_size(gp++, 0); |
| 115 | // CHECK: store i32 -1 |
| 116 | gi = __builtin_object_size(gp++, 1); |
| 117 | // CHECK: store i32 0 |
| 118 | gi = __builtin_object_size(gp++, 2); |
| 119 | // CHECK: store i32 0 |
| 120 | gi = __builtin_object_size(gp++, 3); |
| 121 | } |