6ca6e8
commit 2a44960cbc78713c6a2721683a4319d50e71a01f
6ca6e8
Author: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
6ca6e8
Date:   Thu Jul 7 18:12:58 2022 -0300
6ca6e8
6ca6e8
    Apply asm redirections in stdio.h before first use [BZ #27087]
6ca6e8
    
6ca6e8
    Compilers may not be able to apply asm redirections to functions after
6ca6e8
    these functions are used for the first time, e.g. clang 13.
6ca6e8
    Fix [BZ #27087] by applying all long double-related asm redirections
6ca6e8
    before using functions in bits/stdio.h.
6ca6e8
    However, as these asm redirections depend on the declarations provided
6ca6e8
    by libio/bits/stdio2.h, this header was split in 2:
6ca6e8
    
6ca6e8
     - libio/bits/stdio2-decl.h contains all function declarations;
6ca6e8
     - libio/bits/stdio2.h remains with the remaining contents, including
6ca6e8
       redirections.
6ca6e8
    
6ca6e8
    This also adds the access attribute to __vsnprintf_chk that was missing.
6ca6e8
    
6ca6e8
    Tested with build-many-glibcs.py.
6ca6e8
    
6ca6e8
    Reviewed-by: Paul E. Murphy <murphyp@linux.ibm.com>
6ca6e8
    (cherry picked from commit d0fa09a7701956036ff36f8ca188e9fff81553d8)
6ca6e8
6ca6e8
diff --git a/include/bits/stdio2-decl.h b/include/bits/stdio2-decl.h
6ca6e8
new file mode 100644
6ca6e8
index 0000000000000000..bbb052f192218219
6ca6e8
--- /dev/null
6ca6e8
+++ b/include/bits/stdio2-decl.h
6ca6e8
@@ -0,0 +1 @@
6ca6e8
+#include <libio/bits/stdio2-decl.h>
6ca6e8
diff --git a/libio/Makefile b/libio/Makefile
6ca6e8
index 5336b7d59584927f..981c876940f67fbf 100644
6ca6e8
--- a/libio/Makefile
6ca6e8
+++ b/libio/Makefile
6ca6e8
@@ -23,7 +23,7 @@ subdir	:= libio
6ca6e8
 include ../Makeconfig
6ca6e8
 
6ca6e8
 headers	:= stdio.h \
6ca6e8
-	   bits/stdio.h bits/stdio2.h bits/stdio-ldbl.h \
6ca6e8
+	   bits/stdio.h bits/stdio2.h bits/stdio2-decl.h bits/stdio-ldbl.h \
6ca6e8
 	   bits/types/FILE.h bits/types/__FILE.h bits/types/struct_FILE.h \
6ca6e8
 	   bits/types/__fpos_t.h bits/types/__fpos64_t.h \
6ca6e8
 	   bits/types/cookie_io_functions_t.h
6ca6e8
diff --git a/libio/bits/stdio2-decl.h b/libio/bits/stdio2-decl.h
6ca6e8
new file mode 100644
6ca6e8
index 0000000000000000..e398f7182b98e4d7
6ca6e8
--- /dev/null
6ca6e8
+++ b/libio/bits/stdio2-decl.h
6ca6e8
@@ -0,0 +1,111 @@
6ca6e8
+/* Checking macros for stdio functions. Declarations only.
6ca6e8
+   Copyright (C) 2004-2022 Free Software Foundation, Inc.
6ca6e8
+   This file is part of the GNU C Library.
6ca6e8
+
6ca6e8
+   The GNU C Library is free software; you can redistribute it and/or
6ca6e8
+   modify it under the terms of the GNU Lesser General Public
6ca6e8
+   License as published by the Free Software Foundation; either
6ca6e8
+   version 2.1 of the License, or (at your option) any later version.
6ca6e8
+
6ca6e8
+   The GNU C Library is distributed in the hope that it will be useful,
6ca6e8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6ca6e8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6ca6e8
+   Lesser General Public License for more details.
6ca6e8
+
6ca6e8
+   You should have received a copy of the GNU Lesser General Public
6ca6e8
+   License along with the GNU C Library; if not, see
6ca6e8
+   <https://www.gnu.org/licenses/>.  */
6ca6e8
+
6ca6e8
+#ifndef _BITS_STDIO2_DEC_H
6ca6e8
+#define _BITS_STDIO2_DEC_H 1
6ca6e8
+
6ca6e8
+#ifndef _STDIO_H
6ca6e8
+# error "Never include <bits/stdio2-decl.h> directly; use <stdio.h> instead."
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
6ca6e8
+			  const char *__restrict __format, ...) __THROW
6ca6e8
+    __attr_access ((__write_only__, 1, 3));
6ca6e8
+extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
6ca6e8
+			   const char *__restrict __format,
6ca6e8
+			   __gnuc_va_list __ap) __THROW
6ca6e8
+    __attr_access ((__write_only__, 1, 3));
6ca6e8
+
6ca6e8
+#if defined __USE_ISOC99 || defined __USE_UNIX98
6ca6e8
+
6ca6e8
+extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
6ca6e8
+			   size_t __slen, const char *__restrict __format,
6ca6e8
+			   ...) __THROW
6ca6e8
+    __attr_access ((__write_only__, 1, 2));
6ca6e8
+extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
6ca6e8
+			    size_t __slen, const char *__restrict __format,
6ca6e8
+			    __gnuc_va_list __ap) __THROW
6ca6e8
+    __attr_access ((__write_only__, 1, 2));
6ca6e8
+
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+#if __USE_FORTIFY_LEVEL > 1
6ca6e8
+
6ca6e8
+extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
6ca6e8
+			  const char *__restrict __format, ...);
6ca6e8
+extern int __printf_chk (int __flag, const char *__restrict __format, ...);
6ca6e8
+extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
6ca6e8
+			   const char *__restrict __format, __gnuc_va_list __ap);
6ca6e8
+extern int __vprintf_chk (int __flag, const char *__restrict __format,
6ca6e8
+			  __gnuc_va_list __ap);
6ca6e8
+
6ca6e8
+# ifdef __USE_XOPEN2K8
6ca6e8
+extern int __dprintf_chk (int __fd, int __flag, const char *__restrict __fmt,
6ca6e8
+			  ...) __attribute__ ((__format__ (__printf__, 3, 4)));
6ca6e8
+extern int __vdprintf_chk (int __fd, int __flag,
6ca6e8
+			   const char *__restrict __fmt, __gnuc_va_list __arg)
6ca6e8
+     __attribute__ ((__format__ (__printf__, 3, 0)));
6ca6e8
+# endif
6ca6e8
+
6ca6e8
+# ifdef __USE_GNU
6ca6e8
+
6ca6e8
+extern int __asprintf_chk (char **__restrict __ptr, int __flag,
6ca6e8
+			   const char *__restrict __fmt, ...)
6ca6e8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
6ca6e8
+extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
6ca6e8
+			    const char *__restrict __fmt, __gnuc_va_list __arg)
6ca6e8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
6ca6e8
+extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
6ca6e8
+				 int __flag, const char *__restrict __format,
6ca6e8
+				 ...)
6ca6e8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
6ca6e8
+extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
6ca6e8
+				  int __flag,
6ca6e8
+				  const char *__restrict __format,
6ca6e8
+				  __gnuc_va_list __args)
6ca6e8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
6ca6e8
+
6ca6e8
+# endif
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+#if __GLIBC_USE (DEPRECATED_GETS)
6ca6e8
+extern char *__gets_chk (char *__str, size_t) __wur;
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
6ca6e8
+			  FILE *__restrict __stream)
6ca6e8
+    __wur __attr_access ((__write_only__, 1, 3));
6ca6e8
+
6ca6e8
+extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
6ca6e8
+			   size_t __size, size_t __n,
6ca6e8
+			   FILE *__restrict __stream) __wur;
6ca6e8
+
6ca6e8
+#ifdef __USE_GNU
6ca6e8
+extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
6ca6e8
+				   int __n, FILE *__restrict __stream)
6ca6e8
+    __wur __attr_access ((__write_only__, 1, 3));
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+#ifdef __USE_MISC
6ca6e8
+# undef fread_unlocked
6ca6e8
+extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
6ca6e8
+				    size_t __size, size_t __n,
6ca6e8
+				    FILE *__restrict __stream) __wur;
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+#endif /* bits/stdio2-decl.h.  */
6ca6e8
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
6ca6e8
index 40ff16b01b4f4876..4570f86a4496c1ee 100644
6ca6e8
--- a/libio/bits/stdio2.h
6ca6e8
+++ b/libio/bits/stdio2.h
6ca6e8
@@ -23,14 +23,6 @@
6ca6e8
 # error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
6ca6e8
 #endif
6ca6e8
 
6ca6e8
-extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
6ca6e8
-			  const char *__restrict __format, ...) __THROW
6ca6e8
-    __attr_access ((__write_only__, 1, 3));
6ca6e8
-extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
6ca6e8
-			   const char *__restrict __format,
6ca6e8
-			   __gnuc_va_list __ap) __THROW
6ca6e8
-    __attr_access ((__write_only__, 1, 3));
6ca6e8
-
6ca6e8
 #ifdef __va_arg_pack
6ca6e8
 __fortify_function int
6ca6e8
 __NTH (sprintf (char *__restrict __s, const char *__restrict __fmt, ...))
6ca6e8
@@ -54,15 +46,6 @@ __NTH (vsprintf (char *__restrict __s, const char *__restrict __fmt,
6ca6e8
 }
6ca6e8
 
6ca6e8
 #if defined __USE_ISOC99 || defined __USE_UNIX98
6ca6e8
-
6ca6e8
-extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
6ca6e8
-			   size_t __slen, const char *__restrict __format,
6ca6e8
-			   ...) __THROW
6ca6e8
-    __attr_access ((__write_only__, 1, 2));
6ca6e8
-extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
6ca6e8
-			    size_t __slen, const char *__restrict __format,
6ca6e8
-			    __gnuc_va_list __ap) __THROW;
6ca6e8
-
6ca6e8
 # ifdef __va_arg_pack
6ca6e8
 __fortify_function int
6ca6e8
 __NTH (snprintf (char *__restrict __s, size_t __n,
6ca6e8
@@ -89,15 +72,6 @@ __NTH (vsnprintf (char *__restrict __s, size_t __n,
6ca6e8
 #endif
6ca6e8
 
6ca6e8
 #if __USE_FORTIFY_LEVEL > 1
6ca6e8
-
6ca6e8
-extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
6ca6e8
-			  const char *__restrict __format, ...);
6ca6e8
-extern int __printf_chk (int __flag, const char *__restrict __format, ...);
6ca6e8
-extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
6ca6e8
-			   const char *__restrict __format, __gnuc_va_list __ap);
6ca6e8
-extern int __vprintf_chk (int __flag, const char *__restrict __format,
6ca6e8
-			  __gnuc_va_list __ap);
6ca6e8
-
6ca6e8
 # ifdef __va_arg_pack
6ca6e8
 __fortify_function int
6ca6e8
 fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
6ca6e8
@@ -136,12 +110,6 @@ vfprintf (FILE *__restrict __stream,
6ca6e8
 }
6ca6e8
 
6ca6e8
 # ifdef __USE_XOPEN2K8
6ca6e8
-extern int __dprintf_chk (int __fd, int __flag, const char *__restrict __fmt,
6ca6e8
-			  ...) __attribute__ ((__format__ (__printf__, 3, 4)));
6ca6e8
-extern int __vdprintf_chk (int __fd, int __flag,
6ca6e8
-			   const char *__restrict __fmt, __gnuc_va_list __arg)
6ca6e8
-     __attribute__ ((__format__ (__printf__, 3, 0)));
6ca6e8
-
6ca6e8
 #  ifdef __va_arg_pack
6ca6e8
 __fortify_function int
6ca6e8
 dprintf (int __fd, const char *__restrict __fmt, ...)
6ca6e8
@@ -162,23 +130,6 @@ vdprintf (int __fd, const char *__restrict __fmt, __gnuc_va_list __ap)
6ca6e8
 # endif
6ca6e8
 
6ca6e8
 # ifdef __USE_GNU
6ca6e8
-
6ca6e8
-extern int __asprintf_chk (char **__restrict __ptr, int __flag,
6ca6e8
-			   const char *__restrict __fmt, ...)
6ca6e8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
6ca6e8
-extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
6ca6e8
-			    const char *__restrict __fmt, __gnuc_va_list __arg)
6ca6e8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
6ca6e8
-extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
6ca6e8
-				 int __flag, const char *__restrict __format,
6ca6e8
-				 ...)
6ca6e8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
6ca6e8
-extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
6ca6e8
-				  int __flag,
6ca6e8
-				  const char *__restrict __format,
6ca6e8
-				  __gnuc_va_list __args)
6ca6e8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
6ca6e8
-
6ca6e8
 #  ifdef __va_arg_pack
6ca6e8
 __fortify_function int
6ca6e8
 __NTH (asprintf (char **__restrict __ptr, const char *__restrict __fmt, ...))
6ca6e8
@@ -231,7 +182,6 @@ __NTH (obstack_vprintf (struct obstack *__restrict __obstack,
6ca6e8
 #endif
6ca6e8
 
6ca6e8
 #if __GLIBC_USE (DEPRECATED_GETS)
6ca6e8
-extern char *__gets_chk (char *__str, size_t) __wur;
6ca6e8
 extern char *__REDIRECT (__gets_warn, (char *__str), gets)
6ca6e8
      __wur __warnattr ("please use fgets or getline instead, gets can't "
6ca6e8
 		       "specify buffer size");
6ca6e8
@@ -245,9 +195,6 @@ gets (char *__str)
6ca6e8
 }
6ca6e8
 #endif
6ca6e8
 
6ca6e8
-extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
6ca6e8
-			  FILE *__restrict __stream)
6ca6e8
-    __wur __attr_access ((__write_only__, 1, 3));
6ca6e8
 extern char *__REDIRECT (__fgets_alias,
6ca6e8
 			 (char *__restrict __s, int __n,
6ca6e8
 			  FILE *__restrict __stream), fgets)
6ca6e8
@@ -269,9 +216,6 @@ fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
6ca6e8
   return __fgets_chk (__s, sz, __n, __stream);
6ca6e8
 }
6ca6e8
 
6ca6e8
-extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
6ca6e8
-			   size_t __size, size_t __n,
6ca6e8
-			   FILE *__restrict __stream) __wur;
6ca6e8
 extern size_t __REDIRECT (__fread_alias,
6ca6e8
 			  (void *__restrict __ptr, size_t __size,
6ca6e8
 			   size_t __n, FILE *__restrict __stream),
6ca6e8
@@ -297,9 +241,6 @@ fread (void *__restrict __ptr, size_t __size, size_t __n,
6ca6e8
 }
6ca6e8
 
6ca6e8
 #ifdef __USE_GNU
6ca6e8
-extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
6ca6e8
-				   int __n, FILE *__restrict __stream)
6ca6e8
-    __wur __attr_access ((__write_only__, 1, 3));
6ca6e8
 extern char *__REDIRECT (__fgets_unlocked_alias,
6ca6e8
 			 (char *__restrict __s, int __n,
6ca6e8
 			  FILE *__restrict __stream), fgets_unlocked)
6ca6e8
@@ -324,9 +265,6 @@ fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
6ca6e8
 
6ca6e8
 #ifdef __USE_MISC
6ca6e8
 # undef fread_unlocked
6ca6e8
-extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
6ca6e8
-				    size_t __size, size_t __n,
6ca6e8
-				    FILE *__restrict __stream) __wur;
6ca6e8
 extern size_t __REDIRECT (__fread_unlocked_alias,
6ca6e8
 			  (void *__restrict __ptr, size_t __size,
6ca6e8
 			   size_t __n, FILE *__restrict __stream),
6ca6e8
diff --git a/libio/stdio.h b/libio/stdio.h
6ca6e8
index abefe640e52d18d5..d36e61c56bbb3117 100644
6ca6e8
--- a/libio/stdio.h
6ca6e8
+++ b/libio/stdio.h
6ca6e8
@@ -879,20 +879,27 @@ extern void funlockfile (FILE *__stream) __THROW;
6ca6e8
 extern int __uflow (FILE *);
6ca6e8
 extern int __overflow (FILE *, int);
6ca6e8
 
6ca6e8
+#if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
6ca6e8
+/* Declare all functions from bits/stdio2-decl.h first.  */
6ca6e8
+# include <bits/stdio2-decl.h>
6ca6e8
+#endif
6ca6e8
+
6ca6e8
+/* The following headers provide asm redirections.  These redirections must
6ca6e8
+   appear before the first usage of these functions, e.g. in bits/stdio.h.  */
6ca6e8
+#if defined __LDBL_COMPAT || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
6ca6e8
+# include <bits/stdio-ldbl.h>
6ca6e8
+#endif
6ca6e8
+
6ca6e8
 /* If we are compiling with optimizing read this file.  It contains
6ca6e8
    several optimizing inline functions and macros.  */
6ca6e8
 #ifdef __USE_EXTERN_INLINES
6ca6e8
 # include <bits/stdio.h>
6ca6e8
 #endif
6ca6e8
 #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
6ca6e8
+/* Now include the function definitions and redirects too.  */
6ca6e8
 # include <bits/stdio2.h>
6ca6e8
 #endif
6ca6e8
 
6ca6e8
-#include <bits/floatn.h>
6ca6e8
-#if defined __LDBL_COMPAT || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
6ca6e8
-# include <bits/stdio-ldbl.h>
6ca6e8
-#endif
6ca6e8
-
6ca6e8
 __END_DECLS
6ca6e8
 
6ca6e8
 #endif /* <stdio.h> included.  */