blob: f6c7db835bf3b681f79e6680661c4350219197e1 [file] [log] [blame]
Nuno Lopesf195f2c2012-07-13 20:48:52 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - 2>&1 | 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
Nuno Lopes3e86a042012-05-22 15:26:48 +000043 // 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;
Richard Smithc6794852012-05-23 04:13:20 +000058 // Ensure we only evaluate the side-effect once.
59 // CHECK: = add
60 // CHECK-NOT: = add
61 // 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 Stump48c2af82009-10-29 23:22:14 +000062 strcpy((++i, gbuf), "Hi there");
63}
64
Daniel Dunbard7f7d082010-06-29 22:00:45 +000065// CHECK: define void @test8
Mike Stump48c2af82009-10-29 23:22:14 +000066void test8() {
67 char *buf[50];
Mike Stumpbc773a02009-12-07 19:22:29 +000068 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000069 // 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 +000070 strcpy(buf[++gi], "Hi there");
71}
72
Daniel Dunbard7f7d082010-06-29 22:00:45 +000073// CHECK: define void @test9
Mike Stump48c2af82009-10-29 23:22:14 +000074void test9() {
Mike Stumpbc773a02009-12-07 19:22:29 +000075 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000076 // 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 +000077 strcpy((char *)((++gi) + gj), "Hi there");
78}
79
Daniel Dunbard7f7d082010-06-29 22:00:45 +000080// CHECK: define void @test10
Mike Stump48c2af82009-10-29 23:22:14 +000081char **p;
82void test10() {
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 Stump48c2af82009-10-29 23:22:14 +000085 strcpy(*(++p), "Hi there");
86}
Mike Stump2623aa22009-10-29 23:29:54 +000087
Daniel Dunbard7f7d082010-06-29 22:00:45 +000088// CHECK: define void @test11
Mike Stump2623aa22009-10-29 23:29:54 +000089void test11() {
Mike Stumpbc773a02009-12-07 19:22:29 +000090 // CHECK-NOT: __strcpy_chk
Daniel Dunbard7f7d082010-06-29 22:00:45 +000091 // 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 +000092 strcpy(gp = gbuf, "Hi there");
93}
94
Daniel Dunbard7f7d082010-06-29 22:00:45 +000095// CHECK: define void @test12
Mike Stump2623aa22009-10-29 23:29:54 +000096void test12() {
Mike Stumpbc773a02009-12-07 19:22:29 +000097 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +000098 // 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 +000099 strcpy(++gp, "Hi there");
100}
101
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000102// CHECK: define void @test13
Mike Stump2623aa22009-10-29 23:29:54 +0000103void test13() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000104 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000105 // 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 +0000106 strcpy(gp++, "Hi there");
107}
108
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000109// CHECK: define void @test14
Mike Stump2623aa22009-10-29 23:29:54 +0000110void test14() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000111 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000112 // 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 +0000113 strcpy(--gp, "Hi there");
114}
115
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000116// CHECK: define void @test15
Mike Stump2623aa22009-10-29 23:29:54 +0000117void test15() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000118 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000119 // 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 +0000120 strcpy(gp--, "Hi there");
121}
Mike Stump3f0147e2009-10-29 23:34:20 +0000122
Daniel Dunbard7f7d082010-06-29 22:00:45 +0000123// CHECK: define void @test16
Mike Stump3f0147e2009-10-29 23:34:20 +0000124void test16() {
Mike Stumpbc773a02009-12-07 19:22:29 +0000125 // CHECK-NOT: __strcpy_chk
Mike Stumpd2635a42010-01-13 19:40:37 +0000126 // 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 +0000127 strcpy(gp += 1, "Hi there");
128}
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000129
Nuno Lopesf195f2c2012-07-13 20:48:52 +0000130// CHECK: @test17
Benjamin Kramer3f27b382010-01-03 18:18:37 +0000131void test17() {
132 // CHECK: store i32 -1
133 gi = __builtin_object_size(gp++, 0);
134 // CHECK: store i32 -1
135 gi = __builtin_object_size(gp++, 1);
136 // CHECK: store i32 0
137 gi = __builtin_object_size(gp++, 2);
138 // CHECK: store i32 0
139 gi = __builtin_object_size(gp++, 3);
140}
Nuno Lopesf195f2c2012-07-13 20:48:52 +0000141
142// CHECK: @test18
143unsigned test18(int cond) {
144 int a[4], b[4];
145 // CHECK: phi i32*
146 // CHECK: call i64 @llvm.objectsize.i64
147 return __builtin_object_size(cond ? a : b, 0);
148}