blob: 038d8f98e5d53187d37690a9163eb37c0a952889 [file] [log] [blame]
Benjamin Kramercc179cb2009-10-27 12:19:13 +00001// RUN: clang-cc -triple x86_64-apple-darwin -S %s -o - | FileCheck %s
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
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() {
17 // CHECK: movabsq $59, %rdx
Jakob Stoklund Olesen936d2a82009-11-17 01:47:01 +000018 // CHECK-NEXT: movq
19 // CHECK-NEXT: movq
Mike Stump64eda9e2009-10-26 18:35:08 +000020 // CHECK-NEXT: call ___strcpy_chk
21 strcpy(&gbuf[4], "Hi there");
22}
23
24void test2() {
25 // CHECK: movabsq $63, %rdx
Jakob Stoklund Olesen936d2a82009-11-17 01:47:01 +000026 // CHECK-NEXT: movq
27 // CHECK-NEXT: movq
Mike Stump64eda9e2009-10-26 18:35:08 +000028 // CHECK-NEXT: call ___strcpy_chk
29 strcpy(gbuf, "Hi there");
30}
31
Mike Stump06bc9bc2009-10-26 21:38:39 +000032void test3() {
33 // CHECK: movabsq $0, %rdx
Jakob Stoklund Olesen936d2a82009-11-17 01:47:01 +000034 // CHECK-NEXT: movq
35 // CHECK-NEXT: movq
Mike Stump06bc9bc2009-10-26 21:38:39 +000036 // CHECK-NEXT: call ___strcpy_chk
37 strcpy(&gbuf[100], "Hi there");
Mike Stump64eda9e2009-10-26 18:35:08 +000038}
39
Mike Stump06bc9bc2009-10-26 21:38:39 +000040void test4() {
41 // CHECK: movabsq $0, %rdx
Jakob Stoklund Olesen936d2a82009-11-17 01:47:01 +000042 // CHECK-NEXT: movq
43 // CHECK-NEXT: movq
Mike Stump06bc9bc2009-10-26 21:38:39 +000044 // CHECK-NEXT: call ___strcpy_chk
45 strcpy((char*)(void*)&gbuf[-1], "Hi there");
46}
47
48void test5() {
Mike Stump3ada2df2009-11-09 22:40:09 +000049 // CHECK: movq $-1, %rax
50 // CHECK-NEXT: cmpq $-1, %rax
Mike Stump06bc9bc2009-10-26 21:38:39 +000051 // CHECK: call ___inline_strcpy_chk
52 strcpy(gp, "Hi there");
53}
54
55void test6() {
Mike Stump660e6f72009-10-26 23:05:19 +000056 char buf[57];
57
58 // CHECK: movabsq $53, %rdx
Jakob Stoklund Olesen936d2a82009-11-17 01:47:01 +000059 // CHECK-NEXT: movq
60 // CHECK-NEXT: movq
Mike Stump660e6f72009-10-26 23:05:19 +000061 // CHECK-NEXT: call ___strcpy_chk
62 strcpy(&buf[4], "Hi there");
63}
Mike Stump48c2af82009-10-29 23:22:14 +000064
65void test7() {
66 int i;
67 // CHECK-NOT: call ___strcpy_chk
68 // CHECK: call ___inline_strcpy_chk
69 strcpy((++i, gbuf), "Hi there");
70}
71
72void test8() {
73 char *buf[50];
74 // CHECK-NOT: call ___strcpy_chk
75 // CHECK: call ___inline_strcpy_chk
76 strcpy(buf[++gi], "Hi there");
77}
78
79void test9() {
80 // CHECK-NOT: call ___strcpy_chk
81 // CHECK: call ___inline_strcpy_chk
82 strcpy((char *)((++gi) + gj), "Hi there");
83}
84
85char **p;
86void test10() {
87 // CHECK-NOT: call ___strcpy_chk
88 // CHECK: call ___inline_strcpy_chk
89 strcpy(*(++p), "Hi there");
90}
Mike Stump2623aa22009-10-29 23:29:54 +000091
92void test11() {
93 // CHECK-NOT: call ___strcpy_chk
94 // CHECK: call ___inline_strcpy_chk
95 strcpy(gp = gbuf, "Hi there");
96}
97
98void test12() {
99 // CHECK-NOT: call ___strcpy_chk
100 // CHECK: call ___inline_strcpy_chk
101 strcpy(++gp, "Hi there");
102}
103
104void test13() {
105 // CHECK-NOT: call ___strcpy_chk
106 // CHECK: call ___inline_strcpy_chk
107 strcpy(gp++, "Hi there");
108}
109
110void test14() {
111 // CHECK-NOT: call ___strcpy_chk
112 // CHECK: call ___inline_strcpy_chk
113 strcpy(--gp, "Hi there");
114}
115
116void test15() {
117 // CHECK-NOT: call ___strcpy_chk
118 // CHECK: call ___inline_strcpy_chk
119 strcpy(gp--, "Hi there");
120}
Mike Stump3f0147e2009-10-29 23:34:20 +0000121
122void test16() {
123 // CHECK-NOT: call ___strcpy_chk
124 // CHECK: call ___inline_strcpy_chk
125 strcpy(gp += 1, "Hi there");
126}