blob: 287d742b877d51f7fd86b47a08f38c74ef469752 [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
Daniel Dunbard7f7d082010-06-29 22:00:45 +000016// CHECK: define void @test1
Mike Stump64eda9e2009-10-26 18:35:08 +000017void test1() {
Mike Stumpd2635a42010-01-13 19:40:37 +000018 // 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 +000019 strcpy(&gbuf[4], "Hi there");
20}
21
Daniel Dunbard7f7d082010-06-29 22:00:45 +000022// CHECK: define void @test2
Mike Stump64eda9e2009-10-26 18:35:08 +000023void test2() {
Mike Stumpd2635a42010-01-13 19:40:37 +000024 // 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 +000025 strcpy(gbuf, "Hi there");
26}
27
Daniel Dunbard7f7d082010-06-29 22:00:45 +000028// CHECK: define void @test3
Mike Stump06bc9bc2009-10-26 21:38:39 +000029void test3() {
Mike Stumpd2635a42010-01-13 19:40:37 +000030 // 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 +000031 strcpy(&gbuf[100], "Hi there");
Mike Stump64eda9e2009-10-26 18:35:08 +000032}
33
Daniel Dunbard7f7d082010-06-29 22:00:45 +000034// CHECK: define void @test4
Mike Stump06bc9bc2009-10-26 21:38:39 +000035void test4() {
Mike Stumpd2635a42010-01-13 19:40:37 +000036 // 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 +000037 strcpy((char*)(void*)&gbuf[-1], "Hi there");
38}
39
Daniel Dunbard7f7d082010-06-29 22:00:45 +000040// CHECK: define void @test5
Mike Stump06bc9bc2009-10-26 21:38:39 +000041void test5() {
Mike Stumpd2635a42010-01-13 19:40:37 +000042 // CHECK: = load i8** @gp
43 // CHECK-NEXT:= call i64 @llvm.objectsize.i64(i8* %{{.*}}, i1 false)
Mike Stump06bc9bc2009-10-26 21:38:39 +000044 strcpy(gp, "Hi there");
45}
46
Daniel Dunbard7f7d082010-06-29 22:00:45 +000047// CHECK: define void @test6
Mike Stump06bc9bc2009-10-26 21:38:39 +000048void test6() {
Mike Stump660e6f72009-10-26 23:05:19 +000049 char buf[57];
50
Mike Stumpd2635a42010-01-13 19:40:37 +000051 // 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 +000052 strcpy(&buf[4], "Hi there");
53}
Mike Stump48c2af82009-10-29 23:22:14 +000054
Daniel Dunbard7f7d082010-06-29 22:00:45 +000055// CHECK: define void @test7
Mike Stump48c2af82009-10-29 23:22:14 +000056void test7() {
57 int i;
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* 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 +000060 strcpy((++i, gbuf), "Hi there");
61}
62
Daniel Dunbard7f7d082010-06-29 22:00:45 +000063// CHECK: define void @test8
Mike Stump48c2af82009-10-29 23:22:14 +000064void test8() {
65 char *buf[50];
Mike Stumpbc773a02009-12-07 19:22:29 +000066 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000067 // 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 +000068 strcpy(buf[++gi], "Hi there");
69}
70
Daniel Dunbard7f7d082010-06-29 22:00:45 +000071// CHECK: define void @test9
Mike Stump48c2af82009-10-29 23:22:14 +000072void test9() {
Mike Stumpbc773a02009-12-07 19:22:29 +000073 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000074 // 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 +000075 strcpy((char *)((++gi) + gj), "Hi there");
76}
77
Daniel Dunbard7f7d082010-06-29 22:00:45 +000078// CHECK: define void @test10
Mike Stump48c2af82009-10-29 23:22:14 +000079char **p;
80void test10() {
Mike Stumpbc773a02009-12-07 19:22:29 +000081 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000082 // 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 +000083 strcpy(*(++p), "Hi there");
84}
Mike Stump2623aa22009-10-29 23:29:54 +000085
Daniel Dunbard7f7d082010-06-29 22:00:45 +000086// CHECK: define void @test11
Mike Stump2623aa22009-10-29 23:29:54 +000087void test11() {
Mike Stumpbc773a02009-12-07 19:22:29 +000088 // CHECK-NOT: __strcpy_chk
Daniel Dunbard7f7d082010-06-29 22:00:45 +000089 // 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 Stump2623aa22009-10-29 23:29:54 +000090 strcpy(gp = gbuf, "Hi there");
91}
92
Daniel Dunbard7f7d082010-06-29 22:00:45 +000093// CHECK: define void @test12
Mike Stump2623aa22009-10-29 23:29:54 +000094void test12() {
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
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000100// CHECK: define void @test13
Mike Stump2623aa22009-10-29 23:29:54 +0000101void test13() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000102 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000103 // 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 +0000104 strcpy(gp++, "Hi there");
105}
106
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000107// CHECK: define void @test14
Mike Stump2623aa22009-10-29 23:29:54 +0000108void test14() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000109 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000110 // 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 +0000111 strcpy(--gp, "Hi there");
112}
113
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000114// CHECK: define void @test15
Mike Stump2623aa22009-10-29 23:29:54 +0000115void test15() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000116 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000117 // 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 +0000118 strcpy(gp--, "Hi there");
119}
Mike Stump3f0147e2009-10-29 23:34:20 +0000120
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000121// CHECK: define void @test16
Mike Stump3f0147e2009-10-29 23:34:20 +0000122void test16() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000123 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000124 // 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 +0000125 strcpy(gp += 1, "Hi there");
126}
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000127
128void test17() {
129 // CHECK: store i32 -1
130 gi = __builtin_object_size(gp++, 0);
131 // CHECK: store i32 -1
132 gi = __builtin_object_size(gp++, 1);
133 // CHECK: store i32 0
134 gi = __builtin_object_size(gp++, 2);
135 // CHECK: store i32 0
136 gi = __builtin_object_size(gp++, 3);
137}