blob: 3920ec5934de2330c397dcee0226c9c06114ee96 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - | FileCheck %s
Benjamin Kramercc179cb2009-10-27 12:19:13 +00002
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
8static char *__inline_strcpy_chk (char *dest, const char *src) {
9 return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1));
10}
Mike Stump64eda9e2009-10-26 18:35:08 +000011
12char gbuf[63];
13char *gp;
Mike Stump48c2af82009-10-29 23:22:14 +000014int gi, gj;
Mike Stump64eda9e2009-10-26 18:35:08 +000015
16void test1() {
Mike Stumpd2635a42010-01-13 19:40:37 +000017 // 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 Stump64eda9e2009-10-26 18:35:08 +000018 strcpy(&gbuf[4], "Hi there");
19}
20
21void test2() {
Mike Stumpd2635a42010-01-13 19:40:37 +000022 // 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 Stump64eda9e2009-10-26 18:35:08 +000023 strcpy(gbuf, "Hi there");
24}
25
Mike Stump06bc9bc2009-10-26 21:38:39 +000026void test3() {
Mike Stumpd2635a42010-01-13 19:40:37 +000027 // 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 Stump06bc9bc2009-10-26 21:38:39 +000028 strcpy(&gbuf[100], "Hi there");
Mike Stump64eda9e2009-10-26 18:35:08 +000029}
30
Mike Stump06bc9bc2009-10-26 21:38:39 +000031void test4() {
Mike Stumpd2635a42010-01-13 19:40:37 +000032 // 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 Stump06bc9bc2009-10-26 21:38:39 +000033 strcpy((char*)(void*)&gbuf[-1], "Hi there");
34}
35
36void test5() {
Mike Stumpd2635a42010-01-13 19:40:37 +000037 // CHECK: = load i8** @gp
38 // CHECK-NEXT:= call i64 @llvm.objectsize.i64(i8* %{{.*}}, i1 false)
Mike Stump06bc9bc2009-10-26 21:38:39 +000039 strcpy(gp, "Hi there");
40}
41
42void test6() {
Mike Stump660e6f72009-10-26 23:05:19 +000043 char buf[57];
44
Mike Stumpd2635a42010-01-13 19:40:37 +000045 // CHECK: = call i8* @__strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i64 53)
Mike Stump660e6f72009-10-26 23:05:19 +000046 strcpy(&buf[4], "Hi there");
47}
Mike Stump48c2af82009-10-29 23:22:14 +000048
49void test7() {
50 int i;
Mike Stumpbc773a02009-12-07 19:22:29 +000051 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000052 // 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 Stump48c2af82009-10-29 23:22:14 +000053 strcpy((++i, gbuf), "Hi there");
54}
55
56void test8() {
57 char *buf[50];
Mike Stumpbc773a02009-12-07 19:22:29 +000058 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000059 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump48c2af82009-10-29 23:22:14 +000060 strcpy(buf[++gi], "Hi there");
61}
62
63void test9() {
Mike Stumpbc773a02009-12-07 19:22:29 +000064 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000065 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump48c2af82009-10-29 23:22:14 +000066 strcpy((char *)((++gi) + gj), "Hi there");
67}
68
69char **p;
70void test10() {
Mike Stumpbc773a02009-12-07 19:22:29 +000071 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000072 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump48c2af82009-10-29 23:22:14 +000073 strcpy(*(++p), "Hi there");
74}
Mike Stump2623aa22009-10-29 23:29:54 +000075
76void test11() {
Mike Stumpbc773a02009-12-07 19:22:29 +000077 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000078 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump2623aa22009-10-29 23:29:54 +000079 strcpy(gp = gbuf, "Hi there");
80}
81
82void test12() {
Mike Stumpbc773a02009-12-07 19:22:29 +000083 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000084 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump2623aa22009-10-29 23:29:54 +000085 strcpy(++gp, "Hi there");
86}
87
88void test13() {
Mike Stumpbc773a02009-12-07 19:22:29 +000089 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000090 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump2623aa22009-10-29 23:29:54 +000091 strcpy(gp++, "Hi there");
92}
93
94void test14() {
Mike Stumpbc773a02009-12-07 19:22:29 +000095 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000096 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump2623aa22009-10-29 23:29:54 +000097 strcpy(--gp, "Hi there");
98}
99
100void test15() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000101 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000102 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{..*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump2623aa22009-10-29 23:29:54 +0000103 strcpy(gp--, "Hi there");
104}
Mike Stump3f0147e2009-10-29 23:34:20 +0000105
106void test16() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000107 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000108 // CHECK: = call i8* @__inline_strcpy_chk(i8* %{{.*}}, i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0))
Mike Stump3f0147e2009-10-29 23:34:20 +0000109 strcpy(gp += 1, "Hi there");
110}
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000111
112void 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}