From d674f0ef3898c0d1fd5cec76c1c736d9cd9bc7d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ond=C5=99ej=20B=C3=ADlka?= Date: Wed, 4 Dec 2013 02:02:25 +0100 Subject: [PATCH] Refactor several debug routines. To simplify additions of debug routines we replace a custom function implementation by a simple call. --- ChangeLog | 10 +++++ debug/memcpy_chk.c | 31 +-------------- debug/memmove_chk.c | 63 +----------------------------- debug/mempcpy_chk.c | 31 +-------------- debug/{strncpy_chk.c => memset_chk.c} | 72 ++++------------------------------- debug/stpncpy_chk.c | 51 +------------------------ debug/strncpy_chk.c | 57 +-------------------------- 7 files changed, 23 insertions(+), 292 deletions(-) copy debug/{strncpy_chk.c => memset_chk.c} (50%) diff --git a/ChangeLog b/ChangeLog index de64f3f71b..6320d1969e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2013-12-04 Ondřej Bílka + + * debug/memcpy_chk.c (__memcpy_chk): Use call instead of custom + implementation. + * debug/memmove_chk.c (MEMMOVE_CHK): Likewise. + * debug/mempcpy_chk.c (__mempcpy_chk): Likewise. + * debug/memset_chk.c (__memset_chk): Likewise. + * debug/stpncpy_chk.c (__stpncpy_chk): Likewise. + * debug/strncpy_chk.c: Likewise. + 2013-12-03 Joseph Myers [BZ #15268] diff --git a/debug/memcpy_chk.c b/debug/memcpy_chk.c index a53dd7642b..5bbc44f57a 100644 --- a/debug/memcpy_chk.c +++ b/debug/memcpy_chk.c @@ -32,34 +32,5 @@ __memcpy_chk (dstpp, srcpp, len, dstlen) if (__builtin_expect (dstlen < len, 0)) __chk_fail (); - unsigned long int dstp = (long int) dstpp; - unsigned long int srcp = (long int) srcpp; - - /* Copy from the beginning to the end. */ - - /* If there not too few bytes to copy, use word copy. */ - if (len >= OP_T_THRES) - { - /* Copy just a few bytes to make DSTP aligned. */ - len -= (-dstp) % OPSIZ; - BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ); - - /* Copy whole pages from SRCP to DSTP by virtual address manipulation, - as much as possible. */ - - PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); - - /* Copy from SRCP to DSTP taking advantage of the known alignment of - DSTP. Number of bytes remaining is put in the third argument, - i.e. in LEN. This number may vary from machine to machine. */ - - WORD_COPY_FWD (dstp, srcp, len, len); - - /* Fall out and copy the tail. */ - } - - /* There are just a few bytes to copy. Use byte memory operations. */ - BYTE_COPY_FWD (dstp, srcp, len); - - return dstpp; + return memcpy (dstpp, srcpp, len); } diff --git a/debug/memmove_chk.c b/debug/memmove_chk.c index 3ea34c6d04..6337e76ec2 100644 --- a/debug/memmove_chk.c +++ b/debug/memmove_chk.c @@ -36,66 +36,5 @@ MEMMOVE_CHK (dest, src, len, destlen) if (__builtin_expect (destlen < len, 0)) __chk_fail (); - unsigned long int dstp = (long int) dest; - unsigned long int srcp = (long int) src; - - /* This test makes the forward copying code be used whenever possible. - Reduces the working set. */ - if (dstp - srcp >= len) /* *Unsigned* compare! */ - { - /* Copy from the beginning to the end. */ - - /* If there not too few bytes to copy, use word copy. */ - if (len >= OP_T_THRES) - { - /* Copy just a few bytes to make DSTP aligned. */ - len -= (-dstp) % OPSIZ; - BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ); - - /* Copy whole pages from SRCP to DSTP by virtual address - manipulation, as much as possible. */ - - PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); - - /* Copy from SRCP to DSTP taking advantage of the known - alignment of DSTP. Number of bytes remaining is put - in the third argument, i.e. in LEN. This number may - vary from machine to machine. */ - - WORD_COPY_FWD (dstp, srcp, len, len); - - /* Fall out and copy the tail. */ - } - - /* There are just a few bytes to copy. Use byte memory operations. */ - BYTE_COPY_FWD (dstp, srcp, len); - } - else - { - /* Copy from the end to the beginning. */ - srcp += len; - dstp += len; - - /* If there not too few bytes to copy, use word copy. */ - if (len >= OP_T_THRES) - { - /* Copy just a few bytes to make DSTP aligned. */ - len -= dstp % OPSIZ; - BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ); - - /* Copy from SRCP to DSTP taking advantage of the known - alignment of DSTP. Number of bytes remaining is put - in the third argument, i.e. in LEN. This number may - vary from machine to machine. */ - - WORD_COPY_BWD (dstp, srcp, len, len); - - /* Fall out and copy the tail. */ - } - - /* There are just a few bytes to copy. Use byte memory operations. */ - BYTE_COPY_BWD (dstp, srcp, len); - } - - return dest; + return memmove (dest, src, len); } diff --git a/debug/mempcpy_chk.c b/debug/mempcpy_chk.c index 6895883841..1573a29d02 100644 --- a/debug/mempcpy_chk.c +++ b/debug/mempcpy_chk.c @@ -33,34 +33,5 @@ __mempcpy_chk (dstpp, srcpp, len, dstlen) if (__builtin_expect (dstlen < len, 0)) __chk_fail (); - unsigned long int dstp = (long int) dstpp; - unsigned long int srcp = (long int) srcpp; - - /* Copy from the beginning to the end. */ - - /* If there not too few bytes to copy, use word copy. */ - if (len >= OP_T_THRES) - { - /* Copy just a few bytes to make DSTP aligned. */ - len -= (-dstp) % OPSIZ; - BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ); - - /* Copy whole pages from SRCP to DSTP by virtual address manipulation, - as much as possible. */ - - PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); - - /* Copy from SRCP to DSTP taking advantage of the known alignment of - DSTP. Number of bytes remaining is put in the third argument, - i.e. in LEN. This number may vary from machine to machine. */ - - WORD_COPY_FWD (dstp, srcp, len, len); - - /* Fall out and copy the tail. */ - } - - /* There are just a few bytes to copy. Use byte memory operations. */ - BYTE_COPY_FWD (dstp, srcp, len); - - return (void *) dstp; + return __mempcpy (dstpp, srcpp, len); } diff --git a/debug/strncpy_chk.c b/debug/memset_chk.c similarity index 50% copy from debug/strncpy_chk.c copy to debug/memset_chk.c index d067bd9ac3..ef1cadb60f 100644 --- a/debug/strncpy_chk.c +++ b/debug/memset_chk.c @@ -18,71 +18,15 @@ #include #include - -char * -__strncpy_chk (s1, s2, n, s1len) - char *s1; - const char *s2; - size_t n; - size_t s1len; +void * +__memset_chk (dstpp, c, len, dstlen) + void *dstpp; + int c; + size_t len; + size_t dstlen; { - char c; - char *s = s1; - - if (__builtin_expect (s1len < n, 0)) + if (__builtin_expect (dstlen < len, 0)) __chk_fail (); - --s1; - - if (n >= 4) - { - size_t n4 = n >> 2; - - for (;;) - { - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - if (--n4 == 0) - goto last_chars; - } - n = n - (s1 - s) - 1; - if (n == 0) - return s; - goto zero_fill; - } - - last_chars: - n &= 3; - if (n == 0) - return s; - - do - { - c = *s2++; - *++s1 = c; - if (--n == 0) - return s; - } - while (c != '\0'); - - zero_fill: - do - *++s1 = '\0'; - while (--n > 0); - - return s; + return memset (dstpp, c, len); } diff --git a/debug/stpncpy_chk.c b/debug/stpncpy_chk.c index 35a2c23508..f9fa66ccaf 100644 --- a/debug/stpncpy_chk.c +++ b/debug/stpncpy_chk.c @@ -31,54 +31,5 @@ __stpncpy_chk (char *dest, const char *src, size_t n, size_t destlen) if (__builtin_expect (destlen < n, 0)) __chk_fail (); - if (n >= 4) - { - size_t n4 = n >> 2; - - for (;;) - { - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - if (--n4 == 0) - goto last_chars; - } - n -= dest - s; - goto zero_fill; - } - - last_chars: - n &= 3; - if (n == 0) - return dest; - - for (;;) - { - c = *src++; - --n; - *dest++ = c; - if (c == '\0') - break; - if (n == 0) - return dest; - } - - zero_fill: - while (n-- > 0) - dest[n] = '\0'; - - return dest - 1; + return __stpncpy (dest, src, n); } diff --git a/debug/strncpy_chk.c b/debug/strncpy_chk.c index d067bd9ac3..2e078b1e4f 100644 --- a/debug/strncpy_chk.c +++ b/debug/strncpy_chk.c @@ -26,63 +26,8 @@ __strncpy_chk (s1, s2, n, s1len) size_t n; size_t s1len; { - char c; - char *s = s1; - if (__builtin_expect (s1len < n, 0)) __chk_fail (); - --s1; - - if (n >= 4) - { - size_t n4 = n >> 2; - - for (;;) - { - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - c = *s2++; - *++s1 = c; - if (c == '\0') - break; - if (--n4 == 0) - goto last_chars; - } - n = n - (s1 - s) - 1; - if (n == 0) - return s; - goto zero_fill; - } - - last_chars: - n &= 3; - if (n == 0) - return s; - - do - { - c = *s2++; - *++s1 = c; - if (--n == 0) - return s; - } - while (c != '\0'); - - zero_fill: - do - *++s1 = '\0'; - while (--n > 0); - - return s; + return strncpy (s1, s2, n); } -- 2.11.4.GIT