[v2,6/8] Add __vsyslog_internal, with same flags as __v*printf_internal.

Message ID 20181029121650.24544-7-gabriel@inconstante.eti.br
State Superseded
Headers

Commit Message

Gabriel F. T. Gomes Oct. 29, 2018, 12:16 p.m. UTC
  From: Zack Weinberg <zackw@panix.com>

Changed since v1:

  - Fixed white-space errors.
  - Removed internal declaration of __vsyslog_chk, because it's no
    longer called from within libc.  There's a call to it in the inline
    definition of vsyslog in bits/syslog.h, but that definition is only
    inlined in user code with:
      #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  - Removed libc_hidden_def and libc_hidden_proto for vsyslog, as it is
    never called internally (it never was).
  - Added attribute_hidden to the internal declaration of
    __vsyslog_internal.  Here are the objdumps of one internal call,
    before and after this change, on a 32-bits powerpc machine:
    Without attribute_hidden:
      $ objdump -d --reloc SYSLOG-PRISTINE-glibc/libc.so | grep "<vsyslog@@GLIBC_2.4>:" -A 17
      000fc790 <vsyslog@@GLIBC_2.4>:
         fc790:       94 21 ff f0     stwu    r1,-16(r1)
         fc794:       38 c0 00 00     li      r6,0
         fc798:       7c 08 02 a6     mflr    r0
         fc79c:       42 9f 00 05     bcl     20,4*cr7+so,fc7a0 <vsyslog@@GLIBC_2.4+0x10>
         fc7a0:       93 c1 00 08     stw     r30,8(r1)
         fc7a4:       90 01 00 14     stw     r0,20(r1)
         fc7a8:       7f c8 02 a6     mflr    r30
         fc7ac:       3f de 00 0a     addis   r30,r30,10
         fc7b0:       3b de 38 54     addi    r30,r30,14420
         fc7b4:       4b ff f9 9d     bl      fc150 <__vsyslog_internal>
         fc7b8:       80 01 00 14     lwz     r0,20(r1)
         fc7bc:       83 c1 00 08     lwz     r30,8(r1)
         fc7c0:       38 21 00 10     addi    r1,r1,16
         fc7c4:       7c 08 03 a6     mtlr    r0
         fc7c8:       4e 80 00 20     blr
         fc7cc:       60 00 00 00     nop
    With attribute_hidden:
      $ objdump -d --reloc SYSLOG-PATCHED-glibc/libc.so | grep "<vsyslog@@GLIBC_2.4>:" -A 5
      000fc780 <vsyslog@@GLIBC_2.4>:
         fc780:       38 c0 00 00     li      r6,0
         fc784:       4b ff f9 cc     b       fc150 <__vsyslog_internal>
         fc788:       60 00 00 00     nop
         fc78c:       60 00 00 00     nop

-- 8< --
__nldbl___vsyslog_chk will ultimately want to pass PRINTF_LDBL_IS_DBL
down to __vfprintf_internal *as well as* possibly setting PRINTF_FORTIFY.
To make that possible, we need a __vsyslog_internal that takes the
same flags as printf.  The code in misc/syslog.c does also get a
little simpler.

Tested for powerpc and powerpc64le.

2018-10-16  Zack Weinberg  <zackw@panix.com>
	    Gabriel F. T. Gomes  <gabriel@inconstante.eti.br>

	* misc/syslog.c: Include libioP.h, not iolibio.h.
	(__vsyslog_internal): New function with the former body of
	__vsyslog_chk; takes mode_flags argument same as
	__v*printf_internal.  Call __vfprintf_internal directly.

	(__vsyslog_chk): Now a wrapper around __vsyslog_internal.
	Remove libc_hidden_def.
	(__syslog, __syslog_chk): Use __vsyslog_internal.
	(__vsyslog): Move to just below __syslog.  Use __vsyslog_internal.

	* include/sys/syslog.h: Add multiple inclusion guard.
	Add prototype for __vsyslog_internal.
	Remove declaration and libc_hidden_proto for __vsyslog_chk.

	* sysdeps/ieee754/ldbl-opt/nldbl-compat.c (__nldbl___vsyslog_chk):
	Use __vsyslog_internal.

Signed-off-by: Zack Weinberg <zackw@panix.com>
Signed-off-by: Gabriel F. T. Gomes <gabriel@inconstante.eti.br>
---
 include/sys/syslog.h                    | 19 ++++++++++-------
 misc/syslog.c                           | 36 +++++++++++++++++----------------
 sysdeps/ieee754/ldbl-opt/nldbl-compat.c |  2 +-
 3 files changed, 32 insertions(+), 25 deletions(-)
  

Comments

Adhemerval Zanella Nov. 7, 2018, 7:56 p.m. UTC | #1
On 29/10/2018 09:16, Gabriel F. T. Gomes wrote:
> From: Zack Weinberg <zackw@panix.com>
> 
> Changed since v1:
> 
>   - Fixed white-space errors.
>   - Removed internal declaration of __vsyslog_chk, because it's no
>     longer called from within libc.  There's a call to it in the inline
>     definition of vsyslog in bits/syslog.h, but that definition is only
>     inlined in user code with:
>       #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
>   - Removed libc_hidden_def and libc_hidden_proto for vsyslog, as it is
>     never called internally (it never was).
>   - Added attribute_hidden to the internal declaration of
>     __vsyslog_internal.  Here are the objdumps of one internal call,
>     before and after this change, on a 32-bits powerpc machine:
>     Without attribute_hidden:
>       $ objdump -d --reloc SYSLOG-PRISTINE-glibc/libc.so | grep "<vsyslog@@GLIBC_2.4>:" -A 17
>       000fc790 <vsyslog@@GLIBC_2.4>:
>          fc790:       94 21 ff f0     stwu    r1,-16(r1)
>          fc794:       38 c0 00 00     li      r6,0
>          fc798:       7c 08 02 a6     mflr    r0
>          fc79c:       42 9f 00 05     bcl     20,4*cr7+so,fc7a0 <vsyslog@@GLIBC_2.4+0x10>
>          fc7a0:       93 c1 00 08     stw     r30,8(r1)
>          fc7a4:       90 01 00 14     stw     r0,20(r1)
>          fc7a8:       7f c8 02 a6     mflr    r30
>          fc7ac:       3f de 00 0a     addis   r30,r30,10
>          fc7b0:       3b de 38 54     addi    r30,r30,14420
>          fc7b4:       4b ff f9 9d     bl      fc150 <__vsyslog_internal>
>          fc7b8:       80 01 00 14     lwz     r0,20(r1)
>          fc7bc:       83 c1 00 08     lwz     r30,8(r1)
>          fc7c0:       38 21 00 10     addi    r1,r1,16
>          fc7c4:       7c 08 03 a6     mtlr    r0
>          fc7c8:       4e 80 00 20     blr
>          fc7cc:       60 00 00 00     nop
>     With attribute_hidden:
>       $ objdump -d --reloc SYSLOG-PATCHED-glibc/libc.so | grep "<vsyslog@@GLIBC_2.4>:" -A 5
>       000fc780 <vsyslog@@GLIBC_2.4>:
>          fc780:       38 c0 00 00     li      r6,0
>          fc784:       4b ff f9 cc     b       fc150 <__vsyslog_internal>
>          fc788:       60 00 00 00     nop
>          fc78c:       60 00 00 00     nop
> 
> -- 8< --
> __nldbl___vsyslog_chk will ultimately want to pass PRINTF_LDBL_IS_DBL
> down to __vfprintf_internal *as well as* possibly setting PRINTF_FORTIFY.
> To make that possible, we need a __vsyslog_internal that takes the
> same flags as printf.  The code in misc/syslog.c does also get a
> little simpler.
> 
> Tested for powerpc and powerpc64le.

LGTM.

> 
> 2018-10-16  Zack Weinberg  <zackw@panix.com>
> 	    Gabriel F. T. Gomes  <gabriel@inconstante.eti.br>
> 
> 	* misc/syslog.c: Include libioP.h, not iolibio.h.
> 	(__vsyslog_internal): New function with the former body of
> 	__vsyslog_chk; takes mode_flags argument same as
> 	__v*printf_internal.  Call __vfprintf_internal directly.
> 
> 	(__vsyslog_chk): Now a wrapper around __vsyslog_internal.
> 	Remove libc_hidden_def.
> 	(__syslog, __syslog_chk): Use __vsyslog_internal.
> 	(__vsyslog): Move to just below __syslog.  Use __vsyslog_internal.
> 
> 	* include/sys/syslog.h: Add multiple inclusion guard.
> 	Add prototype for __vsyslog_internal.
> 	Remove declaration and libc_hidden_proto for __vsyslog_chk.
> 
> 	* sysdeps/ieee754/ldbl-opt/nldbl-compat.c (__nldbl___vsyslog_chk):
> 	Use __vsyslog_internal.
> 
> Signed-off-by: Zack Weinberg <zackw@panix.com>
> Signed-off-by: Gabriel F. T. Gomes <gabriel@inconstante.eti.br>

We don't use DCO, but copyright assignments.

> ---
>  include/sys/syslog.h                    | 19 ++++++++++-------
>  misc/syslog.c                           | 36 +++++++++++++++++----------------
>  sysdeps/ieee754/ldbl-opt/nldbl-compat.c |  2 +-
>  3 files changed, 32 insertions(+), 25 deletions(-)
> 
> diff --git a/include/sys/syslog.h b/include/sys/syslog.h
> index 3be3189ed1..89d3479ebc 100644
> --- a/include/sys/syslog.h
> +++ b/include/sys/syslog.h
> @@ -1,11 +1,16 @@
> +#ifndef _LIBC_SYS_SYSLOG_H
> +#define _LIBC_SYS_SYSLOG_H 1
>  #include <misc/sys/syslog.h>
> -
>  #ifndef _ISOMAC
> +
>  libc_hidden_proto (syslog)
> -libc_hidden_proto (vsyslog)
>  
> -extern void __vsyslog_chk (int __pri, int __flag, const char *__fmt,
> -			   __gnuc_va_list __ap)
> -     __attribute__ ((__format__ (__printf__, 3, 0)));
> -libc_hidden_proto (__vsyslog_chk)
> -#endif
> +/* __vsyslog_internal uses the same mode_flags bits as
> +   __v*printf_internal; see libio/libioP.h.  */
> +extern void __vsyslog_internal (int pri, const char *fmt, __gnuc_va_list ap,
> +				unsigned int mode_flags)
> +     attribute_hidden
> +     __attribute__ ((__format__ (__printf__, 2, 0)));
> +
> +#endif /* _ISOMAC */
> +#endif /* syslog.h */

Ok.

> diff --git a/misc/syslog.c b/misc/syslog.c
> index 644dbe80ec..3a15da41ce 100644
> --- a/misc/syslog.c
> +++ b/misc/syslog.c
> @@ -53,7 +53,7 @@ static char sccsid[] = "@(#)syslog.c	8.4 (Berkeley) 3/18/94";
>  
>  #include <stdarg.h>
>  
> -#include <libio/iolibio.h>
> +#include <libio/libioP.h>
>  #include <math_ldbl_opt.h>
>  
>  #include <kernel-features.h>
> @@ -114,24 +114,38 @@ __syslog(int pri, const char *fmt, ...)
>  	va_list ap;
>  
>  	va_start(ap, fmt);
> -	__vsyslog_chk(pri, -1, fmt, ap);
> +	__vsyslog_internal(pri, fmt, ap, 0);
>  	va_end(ap);
>  }
>  ldbl_hidden_def (__syslog, syslog)
>  ldbl_strong_alias (__syslog, syslog)
>  
> +void
> +__vsyslog(int pri, const char *fmt, va_list ap)
> +{
> +	__vsyslog_internal(pri, fmt, ap, 0);
> +}
> +ldbl_weak_alias (__vsyslog, vsyslog)
> +

Ok.

>  void
>  __syslog_chk(int pri, int flag, const char *fmt, ...)
>  {
>  	va_list ap;
>  
>  	va_start(ap, fmt);
> -	__vsyslog_chk(pri, flag, fmt, ap);
> +	__vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
>  	va_end(ap);
>  }
>  
>  void
>  __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
> +{
> +	__vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
> +}
> +
> +void
> +__vsyslog_internal(int pri, const char *fmt, va_list ap,
> +		   unsigned int mode_flags)
>  {
>  	struct tm now_tm;
>  	time_t now;
> @@ -215,11 +229,8 @@ __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
>  	    __set_errno (saved_errno);
>  
>  	    /* We have the header.  Print the user's format into the
> -               buffer.  */
> -	    if (flag == -1)
> -	      vfprintf (f, fmt, ap);
> -	    else
> -	      __vfprintf_chk (f, flag, fmt, ap);
> +	       buffer.  */
> +	    __vfprintf_internal (f, fmt, ap, mode_flags);
>  
>  	    /* Close the memory stream; this will finalize the data
>  	       into a malloc'd buffer in BUF.  */
> @@ -316,15 +327,6 @@ __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
>  	if (buf != failbuf)
>  		free (buf);
>  }
> -libc_hidden_def (__vsyslog_chk)
> -
> -void
> -__vsyslog(int pri, const char *fmt, va_list ap)
> -{
> -  __vsyslog_chk (pri, -1, fmt, ap);
> -}
> -ldbl_hidden_def (__vsyslog, vsyslog)
> -ldbl_weak_alias (__vsyslog, vsyslog)
>  
>  static struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
>  

Ok.

> diff --git a/sysdeps/ieee754/ldbl-opt/nldbl-compat.c b/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
> index bda84af0bb..958bbc1834 100644
> --- a/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
> +++ b/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
> @@ -843,7 +843,7 @@ attribute_compat_text_section
>  __nldbl___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
>  {
>    set_no_long_double ();
> -  __vsyslog_chk (pri, flag, fmt, ap);
> +  __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
>    clear_no_long_double ();
>  }
>  libc_hidden_def (__nldbl___vsyslog_chk)
> 

Ok.
  

Patch

diff --git a/include/sys/syslog.h b/include/sys/syslog.h
index 3be3189ed1..89d3479ebc 100644
--- a/include/sys/syslog.h
+++ b/include/sys/syslog.h
@@ -1,11 +1,16 @@ 
+#ifndef _LIBC_SYS_SYSLOG_H
+#define _LIBC_SYS_SYSLOG_H 1
 #include <misc/sys/syslog.h>
-
 #ifndef _ISOMAC
+
 libc_hidden_proto (syslog)
-libc_hidden_proto (vsyslog)
 
-extern void __vsyslog_chk (int __pri, int __flag, const char *__fmt,
-			   __gnuc_va_list __ap)
-     __attribute__ ((__format__ (__printf__, 3, 0)));
-libc_hidden_proto (__vsyslog_chk)
-#endif
+/* __vsyslog_internal uses the same mode_flags bits as
+   __v*printf_internal; see libio/libioP.h.  */
+extern void __vsyslog_internal (int pri, const char *fmt, __gnuc_va_list ap,
+				unsigned int mode_flags)
+     attribute_hidden
+     __attribute__ ((__format__ (__printf__, 2, 0)));
+
+#endif /* _ISOMAC */
+#endif /* syslog.h */
diff --git a/misc/syslog.c b/misc/syslog.c
index 644dbe80ec..3a15da41ce 100644
--- a/misc/syslog.c
+++ b/misc/syslog.c
@@ -53,7 +53,7 @@  static char sccsid[] = "@(#)syslog.c	8.4 (Berkeley) 3/18/94";
 
 #include <stdarg.h>
 
-#include <libio/iolibio.h>
+#include <libio/libioP.h>
 #include <math_ldbl_opt.h>
 
 #include <kernel-features.h>
@@ -114,24 +114,38 @@  __syslog(int pri, const char *fmt, ...)
 	va_list ap;
 
 	va_start(ap, fmt);
-	__vsyslog_chk(pri, -1, fmt, ap);
+	__vsyslog_internal(pri, fmt, ap, 0);
 	va_end(ap);
 }
 ldbl_hidden_def (__syslog, syslog)
 ldbl_strong_alias (__syslog, syslog)
 
+void
+__vsyslog(int pri, const char *fmt, va_list ap)
+{
+	__vsyslog_internal(pri, fmt, ap, 0);
+}
+ldbl_weak_alias (__vsyslog, vsyslog)
+
 void
 __syslog_chk(int pri, int flag, const char *fmt, ...)
 {
 	va_list ap;
 
 	va_start(ap, fmt);
-	__vsyslog_chk(pri, flag, fmt, ap);
+	__vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
 	va_end(ap);
 }
 
 void
 __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
+{
+	__vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
+}
+
+void
+__vsyslog_internal(int pri, const char *fmt, va_list ap,
+		   unsigned int mode_flags)
 {
 	struct tm now_tm;
 	time_t now;
@@ -215,11 +229,8 @@  __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
 	    __set_errno (saved_errno);
 
 	    /* We have the header.  Print the user's format into the
-               buffer.  */
-	    if (flag == -1)
-	      vfprintf (f, fmt, ap);
-	    else
-	      __vfprintf_chk (f, flag, fmt, ap);
+	       buffer.  */
+	    __vfprintf_internal (f, fmt, ap, mode_flags);
 
 	    /* Close the memory stream; this will finalize the data
 	       into a malloc'd buffer in BUF.  */
@@ -316,15 +327,6 @@  __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap)
 	if (buf != failbuf)
 		free (buf);
 }
-libc_hidden_def (__vsyslog_chk)
-
-void
-__vsyslog(int pri, const char *fmt, va_list ap)
-{
-  __vsyslog_chk (pri, -1, fmt, ap);
-}
-ldbl_hidden_def (__vsyslog, vsyslog)
-ldbl_weak_alias (__vsyslog, vsyslog)
 
 static struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
 
diff --git a/sysdeps/ieee754/ldbl-opt/nldbl-compat.c b/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
index bda84af0bb..958bbc1834 100644
--- a/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
+++ b/sysdeps/ieee754/ldbl-opt/nldbl-compat.c
@@ -843,7 +843,7 @@  attribute_compat_text_section
 __nldbl___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
 {
   set_no_long_double ();
-  __vsyslog_chk (pri, flag, fmt, ap);
+  __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
   clear_no_long_double ();
 }
 libc_hidden_def (__nldbl___vsyslog_chk)