Replace __bzero with memset

Message ID AM5PR0802MB2610A62A60E00B406466E76983CD0@AM5PR0802MB2610.eurprd08.prod.outlook.com
State Committed
Headers

Commit Message

Wilco Dijkstra June 12, 2017, 10:45 a.m. UTC
  Replace all internal uses of __bzero with memset.  This removes the need
to redirect it to a builtin and means memset is inlined whenever possible,
including with -Os.

ChangeLog:
2017-06-12  Wilco Dijkstra  <wdijkstr@arm.com>

        * string/bits/string2.h (__bzero): Remove define.
        * sunrpc/bindrsvprt.c (bindresvport): Change __bzero to memset.
        * sunrpc/clnt_gen.c (clnt_create): Likewise.
        * sunrpc/des_impl.c (_des_crypt): Likewise.
        * sunrpc/key_call.c (key_gendes): Likewise.
        * sunrpc/pmap_rmt.c (clnt_broadcast): Likewise.
        * sunrpc/svc_simple.c (universal): Likewise.
        * sunrpc/svc_tcp.c (svctcp_create): Likewise.
        * sunrpc/svc_udp.c (svcudp_bufcreate): Likewise.
        * sysdeps/arm/aeabi_memclr.c (__aeabi_memclr): Likewise.

--
  

Comments

Andreas Schwab June 12, 2017, 11:42 a.m. UTC | #1
On Jun 12 2017, Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:

> diff --git a/string/bits/string2.h b/string/bits/string2.h
> index 591533658dcef1dbde3d0579c795b72b8ef72f43..c3fc3eae592ecef5d2a0fdd94c0d65cd1407c9d6 100644
> --- a/string/bits/string2.h
> +++ b/string/bits/string2.h
> @@ -52,9 +52,6 @@
>  #define __string2_1bptr_p(__x) \
>    ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
>  
> -/* Set N bytes of S to 0.  */
> -#define __bzero(s, n) __builtin_memset (s, '\0', n)
> -
>  /* Copy SRC to DEST, returning pointer to final NUL byte.  */
>  #ifndef __stpcpy
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)

Does this depend on some other patch?

Andreas.
  
Adhemerval Zanella Netto June 12, 2017, 11:54 a.m. UTC | #2
On 12/06/2017 08:42, Andreas Schwab wrote:
> On Jun 12 2017, Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:
> 
>> diff --git a/string/bits/string2.h b/string/bits/string2.h
>> index 591533658dcef1dbde3d0579c795b72b8ef72f43..c3fc3eae592ecef5d2a0fdd94c0d65cd1407c9d6 100644
>> --- a/string/bits/string2.h
>> +++ b/string/bits/string2.h
>> @@ -52,9 +52,6 @@
>>  #define __string2_1bptr_p(__x) \
>>    ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
>>  
>> -/* Set N bytes of S to 0.  */
>> -#define __bzero(s, n) __builtin_memset (s, '\0', n)
>> -
>>  /* Copy SRC to DEST, returning pointer to final NUL byte.  */
>>  #ifndef __stpcpy
>>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
> 
> Does this depend on some other patch?
> 
> Andreas.
> 

It would be good also if you could update this based on your own patch [1],
which was already acked by me an Zack, since they seems to overlap.

[1] https://sourceware.org/ml/libc-alpha/2017-06/msg00113.html
  
Wilco Dijkstra June 12, 2017, 12:12 p.m. UTC | #3
Andreas Schwab wrote:
> -/* Set N bytes of S to 0.  */
> -#define __bzero(s, n) __builtin_memset (s, '\0', n)
> -
>  /* Copy SRC to DEST, returning pointer to final NUL byte.  */
>  #ifndef __stpcpy
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)

> Does this depend on some other patch?

No, it's independent, but the patch tool often has difficulty if there are
unrelated changes nearby. There should not be a merge issue if the only
change is removal of lines which themselves match unchanged...

Wilco
  
Andreas Schwab June 12, 2017, 12:32 p.m. UTC | #4
On Jun 12 2017, Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:

> Andreas Schwab wrote:
>> -/* Set N bytes of S to 0.  */
>> -#define __bzero(s, n) __builtin_memset (s, '\0', n)
>> -
>>  /* Copy SRC to DEST, returning pointer to final NUL byte.  */
>>  #ifndef __stpcpy
>>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
>
>> Does this depend on some other patch?
>
> No, it's independent, but the patch tool often has difficulty if there are
> unrelated changes nearby. There should not be a merge issue if the only
> change is removal of lines which themselves match unchanged...

It doesn't match.

Andreas.
  
Zack Weinberg June 12, 2017, 1:28 p.m. UTC | #5
On Mon, Jun 12, 2017 at 6:45 AM, Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:
> Replace all internal uses of __bzero with memset.  This removes the need
> to redirect it to a builtin and means memset is inlined whenever possible,
> including with -Os.

+1 from me.

>         * string/bits/string2.h (__bzero): Remove define.

You've already got a patch outstanding (and approved, I think) that
removes this header entirely, and people seem to be having problems
with this piece, so I suggest you scrap this bit for now ...

>         * sunrpc/bindrsvprt.c (bindresvport): Change __bzero to memset.
>         * sunrpc/clnt_gen.c (clnt_create): Likewise.
>         * sunrpc/des_impl.c (_des_crypt): Likewise.
>         * sunrpc/key_call.c (key_gendes): Likewise.
>         * sunrpc/pmap_rmt.c (clnt_broadcast): Likewise.
>         * sunrpc/svc_simple.c (universal): Likewise.
>         * sunrpc/svc_tcp.c (svctcp_create): Likewise.
>         * sunrpc/svc_udp.c (svcudp_bufcreate): Likewise.
>         * sysdeps/arm/aeabi_memclr.c (__aeabi_memclr): Likewise.

... and just go ahead and commit these, then rev the removal of
bits/string2.h and maybe land that too?

> +/* Set memory like memset, but different argument order and no return
> +   value required.  Also only integer caller-saves may be used.  */
>  void
>  __aeabi_memclr (void *dest, size_t n)
>  {
> -  __bzero (dest, n);
> +  memset (dest, 0, n);
>  }

It's a pre-existing condition, so it shouldn't hold up your patch, but
this comment concerns me; there is no guarantee that memset will avoid
using FP or vector registers.  The existing arm/memset.S *doesn't*,
but it doesn't look like it's all that fine-tuned (compare
arm/armv7/multiarch/memcpy_impl.S).

zw
  
Joseph Myers June 12, 2017, 3:18 p.m. UTC | #6
On Mon, 12 Jun 2017, Zack Weinberg wrote:

> > +/* Set memory like memset, but different argument order and no return
> > +   value required.  Also only integer caller-saves may be used.  */
> >  void
> >  __aeabi_memclr (void *dest, size_t n)
> >  {
> > -  __bzero (dest, n);
> > +  memset (dest, 0, n);
> >  }
> 
> It's a pre-existing condition, so it shouldn't hold up your patch, but
> this comment concerns me; there is no guarantee that memset will avoid
> using FP or vector registers.  The existing arm/memset.S *doesn't*,
> but it doesn't look like it's all that fine-tuned (compare
> arm/armv7/multiarch/memcpy_impl.S).

__aeabi_memcpy* are (in the multiarch case) aliased to __memcpy_arm to 
avoid such issues.  (There is still an issue for the dynamic linker, see 
bug 15792.  Even with the dynamic linker built to use only core registers 
there might be issues with e.g. interposed malloc called from the dynamic 
linker, unless all relevant registers are saved and restored.)
  
Joseph Myers June 12, 2017, 5:51 p.m. UTC | #7
The string2.h removal breaks build for 32-bit x86.

In file included from ../string/string-inlines.c:32:0,
                 from ../sysdeps/i386/string-inlines.c:21:
../sysdeps/x86/bits/string.h:678:0: error: "__stpcpy" redefined [-Werror]
 #  define __stpcpy(dest, src) \
 
In file included from ../string/string-inlines.c:27:0,
                 from ../sysdeps/i386/string-inlines.c:21:
../include/string.h:167:0: note: this is the location of the previous definition
 # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
 
cc1: all warnings being treated as errors

Also, Wilco, please use git-style commit messages which start with a 
single line with a short summary of the change, then a blank line, then 
any further details (as in the mailing list message proposing a patch).  
"2017-06-12  Wilco Dijkstra  <wdijkstr@arm.com>" is not a good patch 
summary.
  
Zack Weinberg June 12, 2017, 6:06 p.m. UTC | #8
On Mon, Jun 12, 2017 at 1:51 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> The string2.h removal breaks build for 32-bit x86.
>
> In file included from ../string/string-inlines.c:32:0,
>                  from ../sysdeps/i386/string-inlines.c:21:
> ../sysdeps/x86/bits/string.h:678:0: error: "__stpcpy" redefined [-Werror]
>  #  define __stpcpy(dest, src) \
>
> In file included from ../string/string-inlines.c:27:0,
>                  from ../sysdeps/i386/string-inlines.c:21:
> ../include/string.h:167:0: note: this is the location of the previous definition
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
>
> cc1: all warnings being treated as errors

This should get fixed, but FYI, in about two hours I'm going to
propose a patch to zap bits/string.h. :)

zw
  
Wilco Dijkstra June 12, 2017, 6:17 p.m. UTC | #9
Zack Weinberg wrote:
On Mon, Jun 12, 2017 at 1:51 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> The string2.h removal breaks build for 32-bit x86.
>
> In file included from ../string/string-inlines.c:32:0,
>                  from ../sysdeps/i386/string-inlines.c:21:
> ../sysdeps/x86/bits/string.h:678:0: error: "__stpcpy" redefined [-Werror]
>  #  define __stpcpy(dest, src) \
>
> In file included from ../string/string-inlines.c:27:0,
>                  from ../sysdeps/i386/string-inlines.c:21:
> ../include/string.h:167:0: note: this is the location of the previous definition
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
>
> cc1: all warnings being treated as errors

Oops, it seems the include order is different in string-inlines.c.

> This should get fixed, but FYI, in about two hours I'm going to
> propose a patch to zap bits/string.h. :)

I think adding a #undef next to the existing #undefs in string-inlines.c should do it.

Wilco
  
Wilco Dijkstra June 12, 2017, 7:08 p.m. UTC | #10
Wilco Dijkstra wrote:
> I think adding a #undef next to the existing #undefs in string-inlines.c should do it.

I have committed this as a trivial fix, x86 now builds:

Add an undef of __stpcpy in string-inlines.c to avoid a redefinition
error on x86.

2017-06-12  Wilco Dijkstra  <wdijkstr@arm.com>

        * string/string-inlines.c: Add undef of __stpcpy to fix build issue.

Wilco
  

Patch

diff --git a/string/bits/string2.h b/string/bits/string2.h
index 591533658dcef1dbde3d0579c795b72b8ef72f43..c3fc3eae592ecef5d2a0fdd94c0d65cd1407c9d6 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -52,9 +52,6 @@ 
 #define __string2_1bptr_p(__x) \
   ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
 
-/* Set N bytes of S to 0.  */
-#define __bzero(s, n) __builtin_memset (s, '\0', n)
-
 /* Copy SRC to DEST, returning pointer to final NUL byte.  */
 #ifndef __stpcpy
 # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
diff --git a/sunrpc/bindrsvprt.c b/sunrpc/bindrsvprt.c
index 13bcb27d4d5fdf26de7e95c3d4d4023b3af3d270..da33c05101d9430530461e4941ca7cdc81a0e46f 100644
--- a/sunrpc/bindrsvprt.c
+++ b/sunrpc/bindrsvprt.c
@@ -61,7 +61,7 @@  bindresvport (int sd, struct sockaddr_in *sin)
   if (sin == (struct sockaddr_in *) 0)
     {
       sin = &myaddr;
-      __bzero (sin, sizeof (*sin));
+      memset (sin, 0, sizeof (*sin));
       sin->sin_family = AF_INET;
     }
   else if (sin->sin_family != AF_INET)
diff --git a/sunrpc/clnt_gen.c b/sunrpc/clnt_gen.c
index 8dffaa9fa6b2e6cb1438c07a15f1831c92d5c810..62f56ea68c541806fd43d433aa5125c1a7449600 100644
--- a/sunrpc/clnt_gen.c
+++ b/sunrpc/clnt_gen.c
@@ -56,7 +56,7 @@  clnt_create (const char *hostname, u_long prog, u_long vers,
 
   if (strcmp (proto, "unix") == 0)
     {
-      __bzero ((char *)&sun, sizeof (sun));
+      memset ((char *)&sun, 0, sizeof (sun));
       sun.sun_family = AF_UNIX;
       strcpy (sun.sun_path, hostname);
       sock = RPC_ANYSOCK;
diff --git a/sunrpc/des_impl.c b/sunrpc/des_impl.c
index 1757dc1fb2c3f66514dae000a1aea67bccd6439c..da0b8cee1536392005b159b89ccb0823af1ee493 100644
--- a/sunrpc/des_impl.c
+++ b/sunrpc/des_impl.c
@@ -590,7 +590,7 @@  _des_crypt (char *buf, unsigned len, struct desparams *desp)
     }
   tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
   tbuf[0] = tbuf[1] = 0;
-  __bzero (schedule, sizeof (schedule));
+  memset (schedule, 0, sizeof (schedule));
 
   return (1);
 }
diff --git a/sunrpc/key_call.c b/sunrpc/key_call.c
index a0d9a2a0d157288a419b044a2d24816266d01417..70599761009aae481b667a2772842d5aed7df2ac 100644
--- a/sunrpc/key_call.c
+++ b/sunrpc/key_call.c
@@ -218,7 +218,7 @@  key_gendes (des_block *key)
   sin.sin_family = AF_INET;
   sin.sin_port = 0;
   sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
-  __bzero (sin.sin_zero, sizeof (sin.sin_zero));
+  memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
   socket = RPC_ANYSOCK;
   client = clntudp_bufcreate (&sin, (u_long) KEY_PROG, (u_long) KEY_VERS,
 			      trytimeout, &socket, RPCSMALLMSGSIZE,
diff --git a/sunrpc/pmap_rmt.c b/sunrpc/pmap_rmt.c
index e54fe1447004c6b12e2af7884d4174661988333c..9fec6c4d354b3b8c782c60bed6ea162662f39969 100644
--- a/sunrpc/pmap_rmt.c
+++ b/sunrpc/pmap_rmt.c
@@ -256,7 +256,7 @@  clnt_broadcast (/* program number */
   fd.fd = sock;
   fd.events = POLLIN;
   nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
-  __bzero ((char *) &baddr, sizeof (baddr));
+  memset ((char *) &baddr, 0, sizeof (baddr));
   baddr.sin_family = AF_INET;
   baddr.sin_port = htons (PMAPPORT);
   baddr.sin_addr.s_addr = htonl (INADDR_ANY);
diff --git a/sunrpc/svc_simple.c b/sunrpc/svc_simple.c
index acc9b9db14fa24b4b04e4ddca7d10980e661fe88..f12ed31441f8ca094ffebff3af973dea4a2413d7 100644
--- a/sunrpc/svc_simple.c
+++ b/sunrpc/svc_simple.c
@@ -154,7 +154,7 @@  universal (struct svc_req *rqstp, SVCXPRT *transp_l)
     if (pl->p_prognum == prog && pl->p_procnum == proc)
       {
 	/* decode arguments into a CLEAN buffer */
-	__bzero (xdrbuf, sizeof (xdrbuf));	/* required ! */
+	memset (xdrbuf, 0, sizeof (xdrbuf));	/* required ! */
 	if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf))
 	  {
 	    svcerr_decode (transp_l);
diff --git a/sunrpc/svc_tcp.c b/sunrpc/svc_tcp.c
index db855a3f51e8195b3f6cc05d20c5c5b6741fd1bf..ac75d5c86c6c2cc8fc4e923c92eece2e60f41ac8 100644
--- a/sunrpc/svc_tcp.c
+++ b/sunrpc/svc_tcp.c
@@ -166,7 +166,7 @@  svctcp_create (int sock, u_int sendsize, u_int recvsize)
 	}
       madesock = TRUE;
     }
-  __bzero ((char *) &addr, sizeof (addr));
+  memset ((char *) &addr, 0, sizeof (addr));
   addr.sin_family = AF_INET;
   if (bindresvport (sock, &addr))
     {
diff --git a/sunrpc/svc_udp.c b/sunrpc/svc_udp.c
index 5f6219f91ceb3f094439b83aa964cd698bef9a57..122bad830b5d2ea9881b5bb71b4d78193ac07106 100644
--- a/sunrpc/svc_udp.c
+++ b/sunrpc/svc_udp.c
@@ -137,7 +137,7 @@  svcudp_bufcreate (int sock, u_int sendsz, u_int recvsz)
 	}
       madesock = TRUE;
     }
-  __bzero ((char *) &addr, sizeof (addr));
+  memset ((char *) &addr, 0, sizeof (addr));
   addr.sin_family = AF_INET;
   if (bindresvport (sock, &addr))
     {
diff --git a/sysdeps/arm/aeabi_memclr.c b/sysdeps/arm/aeabi_memclr.c
index 6687e49c9ee1891516643bd339ffc8ced3596f27..03263ea8037a7e542fde1b9f27e957fdb5d57f21 100644
--- a/sysdeps/arm/aeabi_memclr.c
+++ b/sysdeps/arm/aeabi_memclr.c
@@ -17,12 +17,12 @@ 
 
 #include <string.h>
 
-/* Clear memory.  Can't alias to bzero because it's not defined in the
-   same translation unit.  */
+/* Set memory like memset, but different argument order and no return
+   value required.  Also only integer caller-saves may be used.  */
 void
 __aeabi_memclr (void *dest, size_t n)
 {
-  __bzero (dest, n);
+  memset (dest, 0, n);
 }
 
 /* Versions of the above which may assume memory alignment.  */