object-size.c revision 3f0147e161df4725ff15fbb731f4f727afcc229f
1cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer// RUN: clang-cc -triple x86_64-apple-darwin -S %s -o - | FileCheck %s
2cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer
3cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer#define strcpy(dest, src) \
4cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer  ((__builtin_object_size(dest, 0) != -1ULL) \
5cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer   ? __builtin___strcpy_chk (dest, src, __builtin_object_size(dest, 1)) \
6cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer   : __inline_strcpy_chk(dest, src))
7cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer
8cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramerstatic char *__inline_strcpy_chk (char *dest, const char *src) {
9cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer  return __builtin___strcpy_chk(dest, src, __builtin_object_size(dest, 1));
10cc179cb24ffc6686c788c6849e59fc1b04ecf03eBenjamin Kramer}
1164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
1264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stumpchar gbuf[63];
1364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stumpchar *gp;
1448c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpint gi, gj;
1564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
1664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stumpvoid test1() {
1764eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK:       movabsq $59, %rdx
1864eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  movq    %rax, %rdi
1964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  movq    %rcx, %rsi
2064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  call    ___strcpy_chk
2164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  strcpy(&gbuf[4], "Hi there");
2264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump}
2364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
2464eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stumpvoid test2() {
2564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK:       movabsq $63, %rdx
2664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  movq    %rax, %rdi
2764eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  movq    %rcx, %rsi
2864eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK-NEXT:  call    ___strcpy_chk
2964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  strcpy(gbuf, "Hi there");
3064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump}
3164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
3206bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stumpvoid test3() {
3306bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK:       movabsq $0, %rdx
3406bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  movq    %rax, %rdi
3506bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  movq    %rcx, %rsi
3606bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  call    ___strcpy_chk
3706bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  strcpy(&gbuf[100], "Hi there");
3806bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump}
3906bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump
4064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stumpvoid test4() {
4106bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK:       movabsq $0, %rdx
4206bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  movq    %rax, %rdi
4306bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  movq    %rcx, %rsi
4406bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  // CHECK-NEXT:  call    ___strcpy_chk
4506bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  strcpy((char*)(void*)&gbuf[-1], "Hi there");
4606bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump}
4706bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump
4806bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stumpvoid test5() {
4948c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK:       movb    $0, %al
5048c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK-NEXT   testb   %al, %al
5164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  // CHECK:       call    ___inline_strcpy_chk
5206bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stump  strcpy(gp, "Hi there");
5364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump}
5464eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
5506bc9bcc705e5fee714d4b00c3c3c9f01715c195Mike Stumpvoid test6() {
56660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  char buf[57];
57660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
58660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  // CHECK:       movabsq $53, %rdx
59660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  // CHECK-NEXT:  movq    %rax, %rdi
60660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  // CHECK-NEXT:  movq    %rcx, %rsi
61660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  // CHECK-NEXT:  call    ___strcpy_chk
62660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  strcpy(&buf[4], "Hi there");
63660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump}
6448c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump
6548c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpvoid test7() {
6648c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  int i;
6748c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK-NOT:   call    ___strcpy_chk
6848c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK:       call    ___inline_strcpy_chk
6948c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  strcpy((++i, gbuf), "Hi there");
7048c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump}
7148c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump
7248c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpvoid test8() {
7348c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  char *buf[50];
7448c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK-NOT:   call    ___strcpy_chk
7548c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK:       call    ___inline_strcpy_chk
7648c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  strcpy(buf[++gi], "Hi there");
7748c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump}
7848c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump
7948c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpvoid test9() {
8048c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK-NOT:   call    ___strcpy_chk
8148c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK:       call    ___inline_strcpy_chk
8248c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  strcpy((char *)((++gi) + gj), "Hi there");
8348c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump}
8448c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump
8548c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpchar **p;
8648c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stumpvoid test10() {
8748c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK-NOT:   call    ___strcpy_chk
8848c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  // CHECK:       call    ___inline_strcpy_chk
8948c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump  strcpy(*(++p), "Hi there");
9048c2af821ef9ffb51499f37ac1d332766fc20f1eMike Stump}
912623aa283772f629189620a993e2343f2525d262Mike Stump
922623aa283772f629189620a993e2343f2525d262Mike Stumpvoid test11() {
932623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK-NOT:   call    ___strcpy_chk
942623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK:       call    ___inline_strcpy_chk
952623aa283772f629189620a993e2343f2525d262Mike Stump  strcpy(gp = gbuf, "Hi there");
962623aa283772f629189620a993e2343f2525d262Mike Stump}
972623aa283772f629189620a993e2343f2525d262Mike Stump
982623aa283772f629189620a993e2343f2525d262Mike Stumpvoid test12() {
992623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK-NOT:   call    ___strcpy_chk
1002623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK:       call    ___inline_strcpy_chk
1012623aa283772f629189620a993e2343f2525d262Mike Stump  strcpy(++gp, "Hi there");
1022623aa283772f629189620a993e2343f2525d262Mike Stump}
1032623aa283772f629189620a993e2343f2525d262Mike Stump
1042623aa283772f629189620a993e2343f2525d262Mike Stumpvoid test13() {
1052623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK-NOT:   call    ___strcpy_chk
1062623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK:       call    ___inline_strcpy_chk
1072623aa283772f629189620a993e2343f2525d262Mike Stump  strcpy(gp++, "Hi there");
1082623aa283772f629189620a993e2343f2525d262Mike Stump}
1092623aa283772f629189620a993e2343f2525d262Mike Stump
1102623aa283772f629189620a993e2343f2525d262Mike Stumpvoid test14() {
1112623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK-NOT:   call    ___strcpy_chk
1122623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK:       call    ___inline_strcpy_chk
1132623aa283772f629189620a993e2343f2525d262Mike Stump  strcpy(--gp, "Hi there");
1142623aa283772f629189620a993e2343f2525d262Mike Stump}
1152623aa283772f629189620a993e2343f2525d262Mike Stump
1162623aa283772f629189620a993e2343f2525d262Mike Stumpvoid test15() {
1172623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK-NOT:   call    ___strcpy_chk
1182623aa283772f629189620a993e2343f2525d262Mike Stump  // CHECK:       call    ___inline_strcpy_chk
1192623aa283772f629189620a993e2343f2525d262Mike Stump  strcpy(gp--, "Hi there");
1202623aa283772f629189620a993e2343f2525d262Mike Stump}
1213f0147e161df4725ff15fbb731f4f727afcc229fMike Stump
1223f0147e161df4725ff15fbb731f4f727afcc229fMike Stumpvoid test16() {
1233f0147e161df4725ff15fbb731f4f727afcc229fMike Stump  // CHECK-NOT:   call    ___strcpy_chk
1243f0147e161df4725ff15fbb731f4f727afcc229fMike Stump  // CHECK:       call    ___inline_strcpy_chk
1253f0147e161df4725ff15fbb731f4f727afcc229fMike Stump  strcpy(gp += 1, "Hi there");
1263f0147e161df4725ff15fbb731f4f727afcc229fMike Stump}
127