public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] _FORTIFY_SOURCE fixes
@ 2022-01-05  1:39 Siddhesh Poyarekar
  2022-01-05  1:39 ` [PATCH 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
                   ` (5 more replies)
  0 siblings, 6 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  1:39 UTC (permalink / raw)
  To: libc-alpha

The main patch is 2/2, which fixes some fortified headers to match
feature macros with their corresponding main headers, without which
functions would fail to get fortified.  Patch 1/2 is a restructuring of
the tests so that it is easier to add different configurations to
_FORTIFY_SOURCE tests at various levels.

Tested on x86_64 to confirm that there were no new regressions due to
this and that the new tests also run clean.

Siddhesh Poyarekar (2):
  debug: Refactor and expand _FORTIFY_SOURCE tests
  debug: Synchronize feature guards in fortified functions [BZ #28746]

 debug/Makefile                              |   80 +-
 debug/tst-chk0-lfs.c                        |    2 +
 debug/tst-chk0-nongnu.c                     |    3 +
 debug/tst-chk0.c                            | 1759 +++++++++++++++++++
 debug/tst-chk0cc-lfs.cc                     |    2 +
 debug/tst-chk0cc-nongnu.cc                  |    3 +
 debug/tst-chk0cc.cc                         |    1 +
 debug/{tst-lfschk1.c => tst-chk1-lfs.c}     |    0
 debug/tst-chk1-nongnu.c                     |    3 +
 debug/tst-chk1.c                            | 1728 +-----------------
 debug/{tst-lfschk4.cc => tst-chk1cc-lfs.cc} |    0
 debug/tst-chk1cc-nongnu.cc                  |    3 +
 debug/{tst-chk5.cc => tst-chk1cc.cc}        |    2 +-
 debug/{tst-lfschk2.c => tst-chk2-lfs.c}     |    0
 debug/tst-chk2-nongnu.c                     |    3 +
 debug/tst-chk2.c                            |    4 +-
 debug/{tst-lfschk5.cc => tst-chk2cc-lfs.cc} |    0
 debug/tst-chk2cc-nongnu.cc                  |    3 +
 debug/{tst-chk6.cc => tst-chk2cc.cc}        |    2 +-
 debug/{tst-lfschk3.c => tst-chk3-lfs.c}     |    0
 debug/tst-chk3-nongnu.c                     |    3 +
 debug/tst-chk3.c                            |    4 +-
 debug/{tst-lfschk6.cc => tst-chk3cc-lfs.cc} |    0
 debug/tst-chk3cc-nongnu.cc                  |    3 +
 debug/{tst-chk8.cc => tst-chk3cc.cc}        |    2 +-
 debug/tst-chk4.cc                           |    1 -
 debug/tst-chk7.c                            |    2 -
 posix/bits/unistd.h                         |    2 +-
 string/bits/string_fortified.h              |    8 +-
 support/xsignal.h                           |    2 +
 wcsmbs/bits/wchar2.h                        |    2 +-
 31 files changed, 1844 insertions(+), 1783 deletions(-)
 create mode 100644 debug/tst-chk0-lfs.c
 create mode 100644 debug/tst-chk0-nongnu.c
 create mode 100644 debug/tst-chk0.c
 create mode 100644 debug/tst-chk0cc-lfs.cc
 create mode 100644 debug/tst-chk0cc-nongnu.cc
 create mode 100644 debug/tst-chk0cc.cc
 rename debug/{tst-lfschk1.c => tst-chk1-lfs.c} (100%)
 create mode 100644 debug/tst-chk1-nongnu.c
 rename debug/{tst-lfschk4.cc => tst-chk1cc-lfs.cc} (100%)
 create mode 100644 debug/tst-chk1cc-nongnu.cc
 rename debug/{tst-chk5.cc => tst-chk1cc.cc} (54%)
 rename debug/{tst-lfschk2.c => tst-chk2-lfs.c} (100%)
 create mode 100644 debug/tst-chk2-nongnu.c
 rename debug/{tst-lfschk5.cc => tst-chk2cc-lfs.cc} (100%)
 create mode 100644 debug/tst-chk2cc-nongnu.cc
 rename debug/{tst-chk6.cc => tst-chk2cc.cc} (54%)
 rename debug/{tst-lfschk3.c => tst-chk3-lfs.c} (100%)
 create mode 100644 debug/tst-chk3-nongnu.c
 rename debug/{tst-lfschk6.cc => tst-chk3cc-lfs.cc} (100%)
 create mode 100644 debug/tst-chk3cc-nongnu.cc
 rename debug/{tst-chk8.cc => tst-chk3cc.cc} (54%)
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk7.c

-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
@ 2022-01-05  1:39 ` Siddhesh Poyarekar
  2022-01-05  1:39 ` [PATCH 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  1:39 UTC (permalink / raw)
  To: libc-alpha

Rename all debug/tst-chk* tests to reflect the fortification level
they're testing and any additional macros so that rules for them can be
autogenerated.  tst-chk0* are without fortification, tst-chk1 for
_FORTIFY_SOURCE=1 and so on.  This allows easier replication of the
tests to check additional macros.

The change also expands the -lfs tests to include _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                              |   75 +-
 debug/tst-chk0-lfs.c                        |    2 +
 debug/tst-chk0.c                            | 1726 ++++++++++++++++++
 debug/tst-chk0cc-lfs.cc                     |    2 +
 debug/tst-chk0cc.cc                         |    1 +
 debug/{tst-lfschk1.c => tst-chk1-lfs.c}     |    0
 debug/tst-chk1.c                            | 1728 +------------------
 debug/{tst-lfschk4.cc => tst-chk1cc-lfs.cc} |    0
 debug/{tst-chk5.cc => tst-chk1cc.cc}        |    2 +-
 debug/{tst-lfschk2.c => tst-chk2-lfs.c}     |    0
 debug/tst-chk2.c                            |    4 +-
 debug/{tst-lfschk5.cc => tst-chk2cc-lfs.cc} |    0
 debug/{tst-chk6.cc => tst-chk2cc.cc}        |    2 +-
 debug/{tst-lfschk3.c => tst-chk3-lfs.c}     |    0
 debug/tst-chk3.c                            |    4 +-
 debug/{tst-lfschk6.cc => tst-chk3cc-lfs.cc} |    0
 debug/{tst-chk8.cc => tst-chk3cc.cc}        |    2 +-
 debug/tst-chk4.cc                           |    1 -
 debug/tst-chk7.c                            |    2 -
 19 files changed, 1773 insertions(+), 1778 deletions(-)
 create mode 100644 debug/tst-chk0-lfs.c
 create mode 100644 debug/tst-chk0.c
 create mode 100644 debug/tst-chk0cc-lfs.cc
 create mode 100644 debug/tst-chk0cc.cc
 rename debug/{tst-lfschk1.c => tst-chk1-lfs.c} (100%)
 rename debug/{tst-lfschk4.cc => tst-chk1cc-lfs.cc} (100%)
 rename debug/{tst-chk5.cc => tst-chk1cc.cc} (54%)
 rename debug/{tst-lfschk2.c => tst-chk2-lfs.c} (100%)
 rename debug/{tst-lfschk5.cc => tst-chk2cc-lfs.cc} (100%)
 rename debug/{tst-chk6.cc => tst-chk2cc.cc} (54%)
 rename debug/{tst-lfschk3.c => tst-chk3-lfs.c} (100%)
 rename debug/{tst-lfschk6.cc => tst-chk3cc-lfs.cc} (100%)
 rename debug/{tst-chk8.cc => tst-chk3cc.cc} (54%)
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk7.c

diff --git a/debug/Makefile b/debug/Makefile
index bc37e466ee..fe94cd3072 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -1,4 +1,5 @@
 # Copyright (C) 1998-2021 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -110,32 +111,31 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
 
+# _FORTIFY_SOURCE tests.
+tests-chk = $(addprefix tst-chk, 0 1 2 3)
+tests-chk-cc = $(addsuffix cc, $(tests-chk))
+tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
+tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
 # And they also generate warnings from warning attributes, which
 # cannot be disabled via pragmas, so require -Wno-error to be used.
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-LDLIBS-tst-chk4 = -lstdc++
-LDLIBS-tst-chk5 = -lstdc++
-LDLIBS-tst-chk6 = -lstdc++
-LDLIBS-tst-chk8 = -lstdc++
-LDLIBS-tst-lfschk4 = -lstdc++
-LDLIBS-tst-lfschk5 = -lstdc++
-LDLIBS-tst-lfschk6 = -lstdc++
+define disable-warnings
+CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
+endef
+
+$(foreach t,$(tests-chk) $(tests-chk-lfs), \
+	  $(eval $(call disable-warnings,$(t),c)))
+
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
+	  $(eval $(call disable-warnings,$(t),cc)))
+
+define link-cc
+LDLIBS-$(1) = -lstdc++
+endef
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -152,19 +152,18 @@ LDFLAGS-tst-backtrace6 = -rdynamic
 
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
+tests = backtrace-tst tst-longjmp_chk \
+	test-strcpy_chk test-stpcpy_chk \
+	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
+	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
+	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
+tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
 endif
 
 extra-libs = libSegFault libpcprofile
@@ -191,20 +190,12 @@ ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
 
-$(objpfx)tst-chk1.out: $(gen-locales)
-$(objpfx)tst-chk2.out: $(gen-locales)
-$(objpfx)tst-chk3.out: $(gen-locales)
-$(objpfx)tst-chk4.out: $(gen-locales)
-$(objpfx)tst-chk5.out: $(gen-locales)
-$(objpfx)tst-chk6.out: $(gen-locales)
-$(objpfx)tst-chk7.out: $(gen-locales)
-$(objpfx)tst-chk8.out: $(gen-locales)
-$(objpfx)tst-lfschk1.out: $(gen-locales)
-$(objpfx)tst-lfschk2.out: $(gen-locales)
-$(objpfx)tst-lfschk3.out: $(gen-locales)
-$(objpfx)tst-lfschk4.out: $(gen-locales)
-$(objpfx)tst-lfschk5.out: $(gen-locales)
-$(objpfx)tst-lfschk6.out: $(gen-locales)
+define chk-gen-locales
+$(objpfx)$(1).out: $(gen-locales)
+endef
+$(foreach t, \
+	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
+	  $(eval $(call link-cc,$(t))))
 endif
 
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
diff --git a/debug/tst-chk0-lfs.c b/debug/tst-chk0-lfs.c
new file mode 100644
index 0000000000..2460b6baad
--- /dev/null
+++ b/debug/tst-chk0-lfs.c
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk0.c"
diff --git a/debug/tst-chk0.c b/debug/tst-chk0.c
new file mode 100644
index 0000000000..d11e536f87
--- /dev/null
+++ b/debug/tst-chk0.c
@@ -0,0 +1,1726 @@
+/* Copyright (C) 2004-2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* This file tests gets.  Force it to be declared.  */
+#include <features.h>
+#undef __GLIBC_USE_DEPRECATED_GETS
+#define __GLIBC_USE_DEPRECATED_GETS 1
+
+#include <assert.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <obstack.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <wchar.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+
+#define obstack_chunk_alloc malloc
+#define obstack_chunk_free free
+
+char *temp_filename;
+static void do_prepare (void);
+static int do_test (void);
+#define PREPARE(argc, argv) do_prepare ()
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
+static void
+do_prepare (void)
+{
+  int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
+  if (temp_fd == -1)
+    {
+      printf ("cannot create temporary file: %m\n");
+      exit (1);
+    }
+
+  const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
+  if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
+    {
+      puts ("could not write test strings into file");
+      unlink (temp_filename);
+      exit (1);
+    }
+}
+
+volatile int chk_fail_ok;
+volatile int ret;
+jmp_buf chk_fail_buf;
+
+static void
+handler (int sig)
+{
+  if (chk_fail_ok)
+    {
+      chk_fail_ok = 0;
+      longjmp (chk_fail_buf, 1);
+    }
+  else
+    _exit (127);
+}
+
+#if __USE_FORTIFY_LEVEL == 3
+volatile size_t buf_size = 10;
+#else
+char buf[10];
+wchar_t wbuf[10];
+#define buf_size sizeof (buf)
+#endif
+
+volatile size_t l0;
+volatile char *p;
+volatile wchar_t *wp;
+const char *str1 = "JIHGFEDCBA";
+const char *str2 = "F";
+const char *str3 = "%s%n%s%n";
+const char *str4 = "Hello, ";
+const char *str5 = "World!\n";
+const wchar_t *wstr1 = L"JIHGFEDCBA";
+const wchar_t *wstr2 = L"F";
+const wchar_t *wstr3 = L"%s%n%s%n";
+const wchar_t *wstr4 = L"Hello, ";
+const wchar_t *wstr5 = L"World!\n";
+char buf2[10] = "%s";
+int num1 = 67;
+int num2 = 987654;
+
+#define FAIL() \
+  do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
+#define CHK_FAIL_START \
+  chk_fail_ok = 1;				\
+  if (! setjmp (chk_fail_buf))			\
+    {
+#define CHK_FAIL_END \
+      chk_fail_ok = 0;				\
+      FAIL ();					\
+    }
+#if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
+# define CHK_FAIL2_START CHK_FAIL_START
+# define CHK_FAIL2_END CHK_FAIL_END
+#else
+# define CHK_FAIL2_START
+# define CHK_FAIL2_END
+#endif
+
+static int
+do_test (void)
+{
+#if __USE_FORTIFY_LEVEL == 3
+  char *buf = (char *) malloc (buf_size);
+  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
+#endif
+  set_fortify_handler (handler);
+
+  struct A { char buf1[9]; char buf2[1]; } a;
+  struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
+
+  printf ("Test checking routines at fortify level %d\n",
+#ifdef __USE_FORTIFY_LEVEL
+	  (int) __USE_FORTIFY_LEVEL
+#else
+	  0
+#endif
+	  );
+
+#if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
+  printf ("Test skipped");
+  if (l0 == 0)
+    return 0;
+#endif
+
+  /* These ops can be done without runtime checking of object size.  */
+  memcpy (buf, "abcdefghij", 10);
+  memmove (buf + 1, buf, 9);
+  if (memcmp (buf, "aabcdefghi", 10))
+    FAIL ();
+
+  memcpy (buf, "abcdefghij", 10);
+  bcopy (buf, buf + 1, 9);
+  if (memcmp (buf, "aabcdefghi", 10))
+    FAIL ();
+
+  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+      || memcmp (buf, "aabcdabcde", 10))
+    FAIL ();
+
+  memset (buf + 8, 'j', 2);
+  if (memcmp (buf, "aabcdabcjj", 10))
+    FAIL ();
+
+  bzero (buf + 8, 2);
+  if (memcmp (buf, "aabcdabc\0\0", 10))
+    FAIL ();
+
+  explicit_bzero (buf + 6, 4);
+  if (memcmp (buf, "aabcda\0\0\0\0", 10))
+    FAIL ();
+
+  strcpy (buf + 4, "EDCBA");
+  if (memcmp (buf, "aabcEDCBA", 10))
+    FAIL ();
+
+  if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
+    FAIL ();
+
+  strncpy (buf + 6, "X", 4);
+  if (memcmp (buf, "aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
+    FAIL ();
+
+  if (snprintf (buf + 7, 3, "%s", "987654") != 6
+      || memcmp (buf, "aabcEDX98", 10))
+    FAIL ();
+
+  /* These ops need runtime checking, but shouldn't __chk_fail.  */
+  memcpy (buf, "abcdefghij", l0 + 10);
+  memmove (buf + 1, buf, l0 + 9);
+  if (memcmp (buf, "aabcdefghi", 10))
+    FAIL ();
+
+  memcpy (buf, "abcdefghij", l0 + 10);
+  bcopy (buf, buf + 1, l0 + 9);
+  if (memcmp (buf, "aabcdefghi", 10))
+    FAIL ();
+
+  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+      || memcmp (buf, "aabcdabcde", 10))
+    FAIL ();
+
+  memset (buf + 8, 'j', l0 + 2);
+  if (memcmp (buf, "aabcdabcjj", 10))
+    FAIL ();
+
+  bzero (buf + 8, l0 + 2);
+  if (memcmp (buf, "aabcdabc\0\0", 10))
+    FAIL ();
+
+  explicit_bzero (buf + 6, l0 + 4);
+  if (memcmp (buf, "aabcda\0\0\0\0", 10))
+    FAIL ();
+
+  strcpy (buf + 4, str1 + 5);
+  if (memcmp (buf, "aabcEDCBA", 10))
+    FAIL ();
+
+  if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
+    FAIL ();
+
+  strncpy (buf + 6, "X", l0 + 4);
+  if (memcmp (buf, "aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
+      || memcmp (buf, "aabcEcd\0\0", 10))
+    FAIL ();
+
+  if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
+    FAIL ();
+
+  if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
+    FAIL ();
+
+  buf[l0 + 8] = '\0';
+  strcat (buf, "A");
+  if (memcmp (buf, "aabcEcd9A", 10))
+    FAIL ();
+
+  buf[l0 + 7] = '\0';
+  strncat (buf, "ZYXWV", l0 + 2);
+  if (memcmp (buf, "aabcEcdZY", 10))
+    FAIL ();
+
+  /* The following tests are supposed to succeed at all fortify
+     levels, even though they overflow a.buf1 into a.buf2.  */
+  memcpy (a.buf1, "abcdefghij", l0 + 10);
+  memmove (a.buf1 + 1, a.buf1, l0 + 9);
+  if (memcmp (a.buf1, "aabcdefghi", 10))
+    FAIL ();
+
+  memcpy (a.buf1, "abcdefghij", l0 + 10);
+  bcopy (a.buf1, a.buf1 + 1, l0 + 9);
+  if (memcmp (a.buf1, "aabcdefghi", 10))
+    FAIL ();
+
+  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+      || memcmp (a.buf1, "aabcdabcde", 10))
+    FAIL ();
+
+  memset (a.buf1 + 8, 'j', l0 + 2);
+  if (memcmp (a.buf1, "aabcdabcjj", 10))
+    FAIL ();
+
+  bzero (a.buf1 + 8, l0 + 2);
+  if (memcmp (a.buf1, "aabcdabc\0\0", 10))
+    FAIL ();
+
+  explicit_bzero (a.buf1 + 6, l0 + 4);
+  if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL < 2
+  /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
+     and sufficient GCC support, as the string operations overflow
+     from a.buf1 into a.buf2.  */
+  strcpy (a.buf1 + 4, str1 + 5);
+  if (memcmp (a.buf1, "aabcEDCBA", 10))
+    FAIL ();
+
+  if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
+      || memcmp (a.buf1, "aabcEDCBF", 10))
+    FAIL ();
+
+  strncpy (a.buf1 + 6, "X", l0 + 4);
+  if (memcmp (a.buf1, "aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (sprintf (a.buf1 + 7, "%d", num1) != 2
+      || memcmp (a.buf1, "aabcEDX67", 10))
+    FAIL ();
+
+  if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
+      || memcmp (a.buf1, "aabcEDX98", 10))
+    FAIL ();
+
+  a.buf1[l0 + 8] = '\0';
+  strcat (a.buf1, "A");
+  if (memcmp (a.buf1, "aabcEDX9A", 10))
+    FAIL ();
+
+  a.buf1[l0 + 7] = '\0';
+  strncat (a.buf1, "ZYXWV", l0 + 2);
+  if (memcmp (a.buf1, "aabcEDXZY", 10))
+    FAIL ();
+
+#endif
+
+#if __USE_FORTIFY_LEVEL >= 1
+  /* Now check if all buffer overflows are caught at runtime.
+     N.B. All tests involving a length parameter need to be done
+     twice: once with the length a compile-time constant, once without.  */
+
+  CHK_FAIL_START
+  memcpy (buf + 1, "abcdefghij", 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memcpy (buf + 1, "abcdefghij", l0 + 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memmove (buf + 2, buf + 1, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memmove (buf + 2, buf + 1, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bcopy (buf + 1, buf + 2, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bcopy (buf + 1, buf + 2, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = (char *) mempcpy (buf + 6, "abcde", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memset (buf + 9, 'j', 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memset (buf + 9, 'j', l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bzero (buf + 9, 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bzero (buf + 9, l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  explicit_bzero (buf + 9, 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  explicit_bzero (buf + 9, l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  strcpy (buf + 5, str1 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = stpcpy (buf + 9, str2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  strncpy (buf + 7, "X", 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  strncpy (buf + 7, "X", l0 + 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  stpncpy (buf + 6, "cd", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  stpncpy (buf + 6, "cd", l0 + 5);
+  CHK_FAIL_END
+
+# if !defined __cplusplus || defined __va_arg_pack
+  CHK_FAIL_START
+  sprintf (buf + 8, "%d", num1);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  snprintf (buf + 8, 3, "%d", num2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  snprintf (buf + 8, l0 + 3, "%d", num2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  swprintf (wbuf + 8, 3, L"%d", num1);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  swprintf (wbuf + 8, l0 + 3, L"%d", num1);
+  CHK_FAIL_END
+# endif
+
+  memcpy (buf, str1 + 2, 9);
+  CHK_FAIL_START
+  strcat (buf, "AB");
+  CHK_FAIL_END
+
+  memcpy (buf, str1 + 3, 8);
+  CHK_FAIL_START
+  strncat (buf, "ZYXWV", 3);
+  CHK_FAIL_END
+
+  memcpy (buf, str1 + 3, 8);
+  CHK_FAIL_START
+  strncat (buf, "ZYXWV", l0 + 3);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memcpy (a.buf1 + 1, "abcdefghij", 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memmove (a.buf1 + 2, a.buf1 + 1, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bcopy (a.buf1 + 1, a.buf1 + 2, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memset (a.buf1 + 9, 'j', 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  memset (a.buf1 + 9, 'j', l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bzero (a.buf1 + 9, 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  bzero (a.buf1 + 9, l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  explicit_bzero (a.buf1 + 9, 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  explicit_bzero (a.buf1 + 9, l0 + 2);
+  CHK_FAIL_END
+
+# if __USE_FORTIFY_LEVEL >= 2
+#  define O 0
+# else
+#  define O 1
+# endif
+
+  CHK_FAIL_START
+  strcpy (a.buf1 + (O + 4), str1 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  p = stpcpy (a.buf1 + (O + 8), str2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  strncpy (a.buf1 + (O + 6), "X", 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  strncpy (a.buf1 + (O + 6), "X", l0 + 4);
+  CHK_FAIL_END
+
+# if !defined __cplusplus || defined __va_arg_pack
+  CHK_FAIL_START
+  sprintf (a.buf1 + (O + 7), "%d", num1);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  snprintf (a.buf1 + (O + 7), 3, "%d", num2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
+  CHK_FAIL_END
+# endif
+
+  memcpy (a.buf1, str1 + (3 - O), 8 + O);
+  CHK_FAIL_START
+  strcat (a.buf1, "AB");
+  CHK_FAIL_END
+
+  memcpy (a.buf1, str1 + (4 - O), 7 + O);
+  CHK_FAIL_START
+  strncat (a.buf1, "ZYXWV", l0 + 3);
+  CHK_FAIL_END
+#endif
+
+
+  /* These ops can be done without runtime checking of object size.  */
+  wmemcpy (wbuf, L"abcdefghij", 10);
+  wmemmove (wbuf + 1, wbuf, 9);
+  if (wmemcmp (wbuf, L"aabcdefghi", 10))
+    FAIL ();
+
+  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+      || wmemcmp (wbuf, L"aabcdabcde", 10))
+    FAIL ();
+
+  wmemset (wbuf + 8, L'j', 2);
+  if (wmemcmp (wbuf, L"aabcdabcjj", 10))
+    FAIL ();
+
+  wcscpy (wbuf + 4, L"EDCBA");
+  if (wmemcmp (wbuf, L"aabcEDCBA", 10))
+    FAIL ();
+
+  if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
+    FAIL ();
+
+  wcsncpy (wbuf + 6, L"X", 4);
+  if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
+      || wmemcmp (wbuf, L"aabcEDX98", 10))
+    FAIL ();
+
+  if (swprintf (wbuf + 7, 3, L"64") != 2
+      || wmemcmp (wbuf, L"aabcEDX64", 10))
+    FAIL ();
+
+  /* These ops need runtime checking, but shouldn't __chk_fail.  */
+  wmemcpy (wbuf, L"abcdefghij", l0 + 10);
+  wmemmove (wbuf + 1, wbuf, l0 + 9);
+  if (wmemcmp (wbuf, L"aabcdefghi", 10))
+    FAIL ();
+
+  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+      || wmemcmp (wbuf, L"aabcdabcde", 10))
+    FAIL ();
+
+  wmemset (wbuf + 8, L'j', l0 + 2);
+  if (wmemcmp (wbuf, L"aabcdabcjj", 10))
+    FAIL ();
+
+  wcscpy (wbuf + 4, wstr1 + 5);
+  if (wmemcmp (wbuf, L"aabcEDCBA", 10))
+    FAIL ();
+
+  if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
+    FAIL ();
+
+  wcsncpy (wbuf + 6, L"X", l0 + 4);
+  if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
+      || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
+    FAIL ();
+
+  if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
+      || wmemcmp (wbuf, L"aabcEcd98", 10))
+    FAIL ();
+
+  wbuf[l0 + 8] = L'\0';
+  wcscat (wbuf, L"A");
+  if (wmemcmp (wbuf, L"aabcEcd9A", 10))
+    FAIL ();
+
+  wbuf[l0 + 7] = L'\0';
+  wcsncat (wbuf, L"ZYXWV", l0 + 2);
+  if (wmemcmp (wbuf, L"aabcEcdZY", 10))
+    FAIL ();
+
+  wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
+  wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
+  if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
+    FAIL ();
+
+  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+      || wmemcmp (wa.buf1, L"aabcdabcde", 10))
+    FAIL ();
+
+  wmemset (wa.buf1 + 8, L'j', l0 + 2);
+  if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL < 2
+  /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
+     and sufficient GCC support, as the string operations overflow
+     from a.buf1 into a.buf2.  */
+  wcscpy (wa.buf1 + 4, wstr1 + 5);
+  if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
+    FAIL ();
+
+  if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
+      || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
+    FAIL ();
+
+  wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
+  if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
+    FAIL ();
+
+  if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
+      || wmemcmp (wa.buf1, L"aabcEDX98", 10))
+    FAIL ();
+
+  wa.buf1[l0 + 8] = L'\0';
+  wcscat (wa.buf1, L"A");
+  if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
+    FAIL ();
+
+  wa.buf1[l0 + 7] = L'\0';
+  wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
+  if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
+    FAIL ();
+
+#endif
+
+#if __USE_FORTIFY_LEVEL >= 1
+  /* Now check if all buffer overflows are caught at runtime.
+     N.B. All tests involving a length parameter need to be done
+     twice: once with the length a compile-time constant, once without.  */
+
+  CHK_FAIL_START
+  wmemcpy (wbuf + 1, L"abcdefghij", 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemcpy (wbuf + 9, L"abcdefghij", 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemmove (wbuf + 2, wbuf + 1, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wmempcpy (wbuf + 6, L"abcde", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemset (wbuf + 9, L'j', 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemset (wbuf + 9, L'j', l0 + 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcscpy (wbuf + 5, wstr1 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wcpcpy (wbuf + 9, wstr2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcsncpy (wbuf + 7, L"X", 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcsncpy (wbuf + 7, L"X", l0 + 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcpncpy (wbuf + 6, L"cd", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcpncpy (wbuf + 6, L"cd", l0 + 5);
+  CHK_FAIL_END
+
+  wmemcpy (wbuf, wstr1 + 2, 9);
+  CHK_FAIL_START
+  wcscat (wbuf, L"AB");
+  CHK_FAIL_END
+
+  wmemcpy (wbuf, wstr1 + 3, 8);
+  CHK_FAIL_START
+  wcsncat (wbuf, L"ZYXWV", l0 + 3);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemset (wa.buf1 + 9, L'j', 2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wmemset (wa.buf1 + 9, L'j', l0 + 2);
+  CHK_FAIL_END
+
+#if __USE_FORTIFY_LEVEL >= 2
+# define O 0
+#else
+# define O 1
+#endif
+
+  CHK_FAIL_START
+  wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcsncpy (wa.buf1 + (O + 6), L"X", 4);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
+  CHK_FAIL_END
+
+  wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
+  CHK_FAIL_START
+  wcscat (wa.buf1, L"AB");
+  CHK_FAIL_END
+
+  wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
+  CHK_FAIL_START
+  wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
+  CHK_FAIL_END
+#endif
+
+
+  /* Now checks for %n protection.  */
+
+  /* Constant literals passed directly are always ok
+     (even with warnings about possible bugs from GCC).  */
+  int n1, n2;
+  if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
+      || n1 != 1 || n2 != 2)
+    FAIL ();
+
+  /* In this case the format string is not known at compile time,
+     but resides in read-only memory, so is ok.  */
+  if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
+      || n1 != 1 || n2 != 2)
+    FAIL ();
+
+  strcpy (buf2 + 2, "%n%s%n");
+  /* When the format string is writable and contains %n,
+     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
+  CHK_FAIL2_START
+  if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
+    FAIL ();
+  CHK_FAIL2_END
+
+  CHK_FAIL2_START
+  if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
+    FAIL ();
+  CHK_FAIL2_END
+
+  /* But if there is no %n, even writable format string
+     should work.  */
+  buf2[6] = '\0';
+  if (sprintf (buf, buf2 + 4, str2) != 1)
+    FAIL ();
+
+  /* Constant literals passed directly are always ok
+     (even with warnings about possible bugs from GCC).  */
+  if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
+      || n1 != 7 || n2 != 14)
+    FAIL ();
+
+  /* In this case the format string is not known at compile time,
+     but resides in read-only memory, so is ok.  */
+  if (printf (str3, str4, &n1, str5, &n2) != 14
+      || n1 != 7 || n2 != 14)
+    FAIL ();
+
+  strcpy (buf2 + 2, "%n%s%n");
+  /* When the format string is writable and contains %n,
+     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
+  CHK_FAIL2_START
+  if (printf (buf2, str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  CHK_FAIL2_END
+
+  /* But if there is no %n, even writable format string
+     should work.  */
+  buf2[6] = '\0';
+  if (printf (buf2 + 4, str5) != 7)
+    FAIL ();
+
+  FILE *fp = stdout;
+
+  /* Constant literals passed directly are always ok
+     (even with warnings about possible bugs from GCC).  */
+  if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
+      || n1 != 7 || n2 != 14)
+    FAIL ();
+
+  /* In this case the format string is not known at compile time,
+     but resides in read-only memory, so is ok.  */
+  if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
+      || n1 != 7 || n2 != 14)
+    FAIL ();
+
+  strcpy (buf2 + 2, "%n%s%n");
+  /* When the format string is writable and contains %n,
+     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
+  CHK_FAIL2_START
+  if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  CHK_FAIL2_END
+
+  /* But if there is no %n, even writable format string
+     should work.  */
+  buf2[6] = '\0';
+  if (fprintf (fp, buf2 + 4, str5) != 7)
+    FAIL ();
+
+  char *my_ptr = NULL;
+  strcpy (buf2 + 2, "%n%s%n");
+  /* When the format string is writable and contains %n,
+     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
+  CHK_FAIL2_START
+  if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  else
+    free (my_ptr);
+  CHK_FAIL2_END
+
+  struct obstack obs;
+  obstack_init (&obs);
+  CHK_FAIL2_START
+  if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  CHK_FAIL2_END
+  obstack_free (&obs, NULL);
+
+  my_ptr = NULL;
+  if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  else
+    free (my_ptr);
+
+  obstack_init (&obs);
+  if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
+    FAIL ();
+  obstack_free (&obs, NULL);
+
+  if (freopen (temp_filename, "r", stdin) == NULL)
+    {
+      puts ("could not open temporary file");
+      exit (1);
+    }
+
+  if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
+    FAIL ();
+  if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (gets (buf) != buf)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  rewind (stdin);
+
+  if (fgets (buf, buf_size, stdin) != buf
+      || memcmp (buf, "abcdefgh\n", 10))
+    FAIL ();
+  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
+    FAIL ();
+
+  rewind (stdin);
+
+  if (fgets (buf, l0 + buf_size, stdin) != buf
+      || memcmp (buf, "abcdefgh\n", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (fgets (buf, buf_size + 1, stdin) != buf)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  rewind (stdin);
+
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
+      || memcmp (buf, "abcdefgh\n", 10))
+    FAIL ();
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
+      || memcmp (buf, "ABCDEFGHI", 10))
+    FAIL ();
+
+  rewind (stdin);
+
+  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
+      || memcmp (buf, "abcdefgh\n", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  rewind (stdin);
+
+  if (fread (buf, 1, buf_size, stdin) != buf_size
+      || memcmp (buf, "abcdefgh\nA", 10))
+    FAIL ();
+  if (fread (buf, buf_size, 1, stdin) != 1
+      || memcmp (buf, "BCDEFGHI\na", 10))
+    FAIL ();
+
+  rewind (stdin);
+
+  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
+      || memcmp (buf, "abcdefgh\nA", 10))
+    FAIL ();
+  if (fread (buf, buf_size, l0 + 1, stdin) != 1
+      || memcmp (buf, "BCDEFGHI\na", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  rewind (stdin);
+
+  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
+      || memcmp (buf, "abcdefgh\nA", 10))
+    FAIL ();
+  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
+      || memcmp (buf, "BCDEFGHI\na", 10))
+    FAIL ();
+
+  rewind (stdin);
+
+  if (fread_unlocked (buf, 1, 4, stdin) != 4
+      || memcmp (buf, "abcdFGHI\na", 10))
+    FAIL ();
+  if (fread_unlocked (buf, 4, 1, stdin) != 1
+      || memcmp (buf, "efghFGHI\na", 10))
+    FAIL ();
+
+  rewind (stdin);
+
+  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
+      || memcmp (buf, "abcdefgh\nA", 10))
+    FAIL ();
+  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
+      || memcmp (buf, "BCDEFGHI\na", 10))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  lseek (fileno (stdin), 0, SEEK_SET);
+
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
+      || memcmp (buf, "abcdefgh\n", 9))
+    FAIL ();
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
+      || memcmp (buf, "ABCDEFGHI", 9))
+    FAIL ();
+
+  lseek (fileno (stdin), 0, SEEK_SET);
+
+  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
+      || memcmp (buf, "abcdefgh\n", 9))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
+      || memcmp (buf, "\nABCDEFGH", 9))
+    FAIL ();
+  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
+      || memcmp (buf, "abcdefgh\n", 9))
+    FAIL ();
+  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
+      || memcmp (buf, "h\nABCDEFG", 9))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
+      != buf_size - 1
+      || memcmp (buf, "\nABCDEFGH", 9))
+    FAIL ();
+  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
+      || memcmp (buf, "abcdefgh\n", 9))
+    FAIL ();
+  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
+      != buf_size - 1
+      || memcmp (buf, "h\nABCDEFG", 9))
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
+      != buf_size + 1)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  if (freopen (temp_filename, "r", stdin) == NULL)
+    {
+      puts ("could not open temporary file");
+      exit (1);
+    }
+
+  if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
+    {
+      puts ("could not seek in test file");
+      exit (1);
+    }
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (gets (buf) != buf)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  /* Check whether missing N$ formats are detected.  */
+  CHK_FAIL2_START
+  printf ("%3$d\n", 1, 2, 3, 4);
+  CHK_FAIL2_END
+
+  CHK_FAIL2_START
+  fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
+  CHK_FAIL2_END
+
+  CHK_FAIL2_START
+  sprintf (buf, "%3$d\n", 1, 2, 3, 4);
+  CHK_FAIL2_END
+
+  CHK_FAIL2_START
+  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
+  CHK_FAIL2_END
+
+  int sp[2];
+  if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
+    FAIL ();
+  else
+    {
+      const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
+      if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
+	  != strlen (sendstr))
+	FAIL ();
+
+      char recvbuf[12];
+      if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
+	  != sizeof recvbuf
+	  || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
+	FAIL ();
+
+      if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
+	  != sizeof recvbuf - 7
+	  || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
+	  != sizeof recvbuf)
+	FAIL ();
+      CHK_FAIL_END
+
+      CHK_FAIL_START
+      if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
+	  != sizeof recvbuf - 3)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+
+      socklen_t sl;
+      struct sockaddr_un sa_un;
+
+      sl = sizeof (sa_un);
+      if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
+		    (struct sockaddr *) &sa_un, &sl)
+	  != sizeof recvbuf
+	  || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
+	FAIL ();
+
+      sl = sizeof (sa_un);
+      if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
+		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
+	  || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      sl = sizeof (sa_un);
+      if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
+		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
+	FAIL ();
+      CHK_FAIL_END
+
+      CHK_FAIL_START
+      sl = sizeof (sa_un);
+      if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
+		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+
+      close (sp[0]);
+      close (sp[1]);
+    }
+
+  char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
+  char *enddir = strchr (fname, '\0');
+  if (mkdtemp (fname) == NULL)
+    {
+      printf ("mkdtemp failed: %m\n");
+      return 1;
+    }
+  *enddir = '/';
+  if (symlink ("bar", fname) != 0)
+    FAIL ();
+
+  char readlinkbuf[4];
+  if (readlink (fname, readlinkbuf, 4) != 3
+      || memcmp (readlinkbuf, "bar", 3) != 0)
+    FAIL ();
+  if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
+      || memcmp (readlinkbuf, "bbar", 4) != 0)
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (readlink (fname, readlinkbuf + 3, 4) != 3)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
+  if (tmpfd < 0)
+    FAIL ();
+
+  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
+      || memcmp (readlinkbuf, "bar", 3) != 0)
+    FAIL ();
+  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
+		  l0 + 3) != 3
+      || memcmp (readlinkbuf, "bbar", 4) != 0)
+    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
+		  l0 + 3) != 3)
+    FAIL ();
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
+		  4) != 3)
+    FAIL ();
+  CHK_FAIL_END
+#endif
+
+  close (tmpfd);
+
+  char *cwd1 = getcwd (NULL, 0);
+  if (cwd1 == NULL)
+    FAIL ();
+
+  char *cwd2 = getcwd (NULL, 250);
+  if (cwd2 == NULL)
+    FAIL ();
+
+  if (cwd1 && cwd2)
+    {
+      if (strcmp (cwd1, cwd2) != 0)
+	FAIL ();
+
+      *enddir = '\0';
+      if (chdir (fname))
+	FAIL ();
+
+      char *cwd3 = getcwd (NULL, 0);
+      if (cwd3 == NULL)
+	FAIL ();
+      if (strcmp (fname, cwd3) != 0)
+	printf ("getcwd after chdir is '%s' != '%s',"
+		"get{c,}wd tests skipped\n", cwd3, fname);
+      else
+	{
+	  char getcwdbuf[sizeof fname - 3];
+
+	  char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
+	  if (cwd4 != getcwdbuf
+	      || strcmp (getcwdbuf, fname) != 0)
+	    FAIL ();
+
+	  cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
+	  if (cwd4 != getcwdbuf + 1
+	      || getcwdbuf[0] != fname[0]
+	      || strcmp (getcwdbuf + 1, fname) != 0)
+	    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+	  CHK_FAIL_START
+	  if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
+	      != getcwdbuf + 2)
+	    FAIL ();
+	  CHK_FAIL_END
+
+	  CHK_FAIL_START
+	  if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
+	      != getcwdbuf + 2)
+	    FAIL ();
+	  CHK_FAIL_END
+#endif
+
+	  if (getwd (getcwdbuf) != getcwdbuf
+	      || strcmp (getcwdbuf, fname) != 0)
+	    FAIL ();
+
+	  if (getwd (getcwdbuf + 1) != getcwdbuf + 1
+	      || strcmp (getcwdbuf + 1, fname) != 0)
+	    FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+	  CHK_FAIL_START
+	  if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
+	    FAIL ();
+	  CHK_FAIL_END
+#endif
+	}
+
+      if (chdir (cwd1) != 0)
+	FAIL ();
+      free (cwd3);
+    }
+
+  free (cwd1);
+  free (cwd2);
+  *enddir = '/';
+  if (unlink (fname) != 0)
+    FAIL ();
+
+  *enddir = '\0';
+  if (rmdir (fname) != 0)
+    FAIL ();
+
+
+#if PATH_MAX > 0
+  char largebuf[PATH_MAX];
+  char *realres = realpath (".", largebuf);
+  if (realres != largebuf)
+    FAIL ();
+
+# if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char realbuf[1];
+  realres = realpath (".", realbuf);
+  if (realres != realbuf)
+    FAIL ();
+  CHK_FAIL_END
+# endif
+#endif
+
+  if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
+    {
+      assert (MB_CUR_MAX <= 10);
+
+      /* First a simple test.  */
+      char enough[10];
+      if (wctomb (enough, L'A') != 1)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      /* We know the wchar_t encoding is ISO 10646.  So pick a
+	 character which has a multibyte representation which does not
+	 fit.  */
+      CHK_FAIL_START
+      char smallbuf[2];
+      if (wctomb (smallbuf, L'\x100') != 2)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+
+      mbstate_t s;
+      memset (&s, '\0', sizeof (s));
+      if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      /* We know the wchar_t encoding is ISO 10646.  So pick a
+	 character which has a multibyte representation which does not
+	 fit.  */
+      CHK_FAIL_START
+      char smallbuf[2];
+      if (wcrtomb (smallbuf, L'\x100', &s) != 2)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+
+      wchar_t wenough[10];
+      memset (&s, '\0', sizeof (s));
+      const char *cp = "A";
+      if (mbsrtowcs (wenough, &cp, 10, &s) != 1
+	  || wcscmp (wenough, L"A") != 0)
+	FAIL ();
+
+      cp = "BC";
+      if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
+	  || wcscmp (wenough, L"BC") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      wchar_t wsmallbuf[2];
+      cp = "ABC";
+      mbsrtowcs (wsmallbuf, &cp, 10, &s);
+      CHK_FAIL_END
+#endif
+
+      cp = "A";
+      if (mbstowcs (wenough, cp, 10) != 1
+	  || wcscmp (wenough, L"A") != 0)
+	FAIL ();
+
+      cp = "DEF";
+      if (mbstowcs (wenough, cp, l0 + 10) != 3
+	  || wcscmp (wenough, L"DEF") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      wchar_t wsmallbuf[2];
+      cp = "ABC";
+      mbstowcs (wsmallbuf, cp, 10);
+      CHK_FAIL_END
+#endif
+
+      memset (&s, '\0', sizeof (s));
+      cp = "ABC";
+      wcscpy (wenough, L"DEF");
+      if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
+	  || wcscmp (wenough, L"AEF") != 0)
+	FAIL ();
+
+      cp = "IJ";
+      if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
+	  || wcscmp (wenough, L"IEF") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      wchar_t wsmallbuf[2];
+      cp = "ABC";
+      mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
+      CHK_FAIL_END
+#endif
+
+      memset (&s, '\0', sizeof (s));
+      const wchar_t *wcp = L"A";
+      if (wcsrtombs (enough, &wcp, 10, &s) != 1
+	  || strcmp (enough, "A") != 0)
+	FAIL ();
+
+      wcp = L"BC";
+      if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
+	  || strcmp (enough, "BC") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      char smallbuf[2];
+      wcp = L"ABC";
+      wcsrtombs (smallbuf, &wcp, 10, &s);
+      CHK_FAIL_END
+#endif
+
+      memset (enough, 'Z', sizeof (enough));
+      wcp = L"EF";
+      if (wcstombs (enough, wcp, 10) != 2
+	  || strcmp (enough, "EF") != 0)
+	FAIL ();
+
+      wcp = L"G";
+      if (wcstombs (enough, wcp, l0 + 10) != 1
+	  || strcmp (enough, "G") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      char smallbuf[2];
+      wcp = L"ABC";
+      wcstombs (smallbuf, wcp, 10);
+      CHK_FAIL_END
+#endif
+
+      memset (&s, '\0', sizeof (s));
+      wcp = L"AB";
+      if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
+	  || strcmp (enough, "A") != 0)
+	FAIL ();
+
+      wcp = L"BCD";
+      if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
+	  || strcmp (enough, "B") != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      char smallbuf[2];
+      wcp = L"ABC";
+      wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
+      CHK_FAIL_END
+#endif
+    }
+  else
+    {
+      puts ("cannot set locale");
+      ret = 1;
+    }
+
+  int fd = posix_openpt (O_RDWR);
+  if (fd != -1)
+    {
+      char enough[1000];
+      if (ptsname_r (fd, enough, sizeof (enough)) != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      char smallbuf[2];
+      if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+      close (fd);
+    }
+
+#if PATH_MAX > 0
+  confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
+# if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char smallbuf[1];
+  confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
+  CHK_FAIL_END
+# endif
+#endif
+
+  gid_t grpslarge[5];
+  int ngr = getgroups (5, grpslarge);
+  asm volatile ("" : : "r" (ngr));
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char smallbuf[1];
+  ngr = getgroups (5, (gid_t *) smallbuf);
+  asm volatile ("" : : "r" (ngr));
+  CHK_FAIL_END
+#endif
+
+  fd = open (_PATH_TTY, O_RDONLY);
+  if (fd != -1)
+    {
+      char enough[1000];
+      if (ttyname_r (fd, enough, sizeof (enough)) != 0)
+	FAIL ();
+
+#if __USE_FORTIFY_LEVEL >= 1
+      CHK_FAIL_START
+      char smallbuf[2];
+      if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
+	FAIL ();
+      CHK_FAIL_END
+#endif
+      close (fd);
+    }
+
+  char hostnamelarge[1000];
+  gethostname (hostnamelarge, sizeof (hostnamelarge));
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char smallbuf[1];
+  gethostname (smallbuf, sizeof (hostnamelarge));
+  CHK_FAIL_END
+#endif
+
+  char loginlarge[1000];
+  getlogin_r (loginlarge, sizeof (hostnamelarge));
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char smallbuf[1];
+  getlogin_r (smallbuf, sizeof (loginlarge));
+  CHK_FAIL_END
+#endif
+
+  char domainnamelarge[1000];
+  int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
+  asm volatile ("" : : "r" (res));
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  char smallbuf[1];
+  res = getdomainname (smallbuf, sizeof (domainnamelarge));
+  asm volatile ("" : : "r" (res));
+  CHK_FAIL_END
+#endif
+
+  fd_set s;
+  FD_ZERO (&s);
+
+  FD_SET (FD_SETSIZE - 1, &s);
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  FD_SET (FD_SETSIZE, &s);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  FD_SET (l0 + FD_SETSIZE, &s);
+  CHK_FAIL_END
+#endif
+
+  FD_CLR (FD_SETSIZE - 1, &s);
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  FD_CLR (FD_SETSIZE, &s);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  FD_SET (l0 + FD_SETSIZE, &s);
+  CHK_FAIL_END
+#endif
+
+  FD_ISSET (FD_SETSIZE - 1, &s);
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  FD_ISSET (FD_SETSIZE, &s);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  FD_ISSET (l0 + FD_SETSIZE, &s);
+  CHK_FAIL_END
+#endif
+
+  struct pollfd fds[1];
+  fds[0].fd = STDOUT_FILENO;
+  fds[0].events = POLLOUT;
+  poll (fds, 1, 0);
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  poll (fds, 2, 0);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  poll (fds, l0 + 2, 0);
+  CHK_FAIL_END
+#endif
+  ppoll (fds, 1, NULL, NULL);
+#if __USE_FORTIFY_LEVEL >= 1
+  CHK_FAIL_START
+  ppoll (fds, 2, NULL, NULL);
+  CHK_FAIL_END
+
+  CHK_FAIL_START
+  ppoll (fds, l0 + 2, NULL, NULL);
+  CHK_FAIL_END
+#endif
+
+  return ret;
+}
diff --git a/debug/tst-chk0cc-lfs.cc b/debug/tst-chk0cc-lfs.cc
new file mode 100644
index 0000000000..2460b6baad
--- /dev/null
+++ b/debug/tst-chk0cc-lfs.cc
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk0.c"
diff --git a/debug/tst-chk0cc.cc b/debug/tst-chk0cc.cc
new file mode 100644
index 0000000000..c467a9f84b
--- /dev/null
+++ b/debug/tst-chk0cc.cc
@@ -0,0 +1 @@
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk1.c b/debug/tst-chk1-lfs.c
similarity index 100%
rename from debug/tst-lfschk1.c
rename to debug/tst-chk1-lfs.c
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
index d11e536f87..c09b01df34 100644
--- a/debug/tst-chk1.c
+++ b/debug/tst-chk1.c
@@ -1,1726 +1,2 @@
-/* Copyright (C) 2004-2021 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-/* This file tests gets.  Force it to be declared.  */
-#include <features.h>
-#undef __GLIBC_USE_DEPRECATED_GETS
-#define __GLIBC_USE_DEPRECATED_GETS 1
-
-#include <assert.h>
-#include <fcntl.h>
-#include <locale.h>
-#include <obstack.h>
-#include <setjmp.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <wchar.h>
-#include <sys/poll.h>
-#include <sys/select.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-
-
-#define obstack_chunk_alloc malloc
-#define obstack_chunk_free free
-
-char *temp_filename;
-static void do_prepare (void);
-static int do_test (void);
-#define PREPARE(argc, argv) do_prepare ()
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
-
-static void
-do_prepare (void)
-{
-  int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
-  if (temp_fd == -1)
-    {
-      printf ("cannot create temporary file: %m\n");
-      exit (1);
-    }
-
-  const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
-  if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
-    {
-      puts ("could not write test strings into file");
-      unlink (temp_filename);
-      exit (1);
-    }
-}
-
-volatile int chk_fail_ok;
-volatile int ret;
-jmp_buf chk_fail_buf;
-
-static void
-handler (int sig)
-{
-  if (chk_fail_ok)
-    {
-      chk_fail_ok = 0;
-      longjmp (chk_fail_buf, 1);
-    }
-  else
-    _exit (127);
-}
-
-#if __USE_FORTIFY_LEVEL == 3
-volatile size_t buf_size = 10;
-#else
-char buf[10];
-wchar_t wbuf[10];
-#define buf_size sizeof (buf)
-#endif
-
-volatile size_t l0;
-volatile char *p;
-volatile wchar_t *wp;
-const char *str1 = "JIHGFEDCBA";
-const char *str2 = "F";
-const char *str3 = "%s%n%s%n";
-const char *str4 = "Hello, ";
-const char *str5 = "World!\n";
-const wchar_t *wstr1 = L"JIHGFEDCBA";
-const wchar_t *wstr2 = L"F";
-const wchar_t *wstr3 = L"%s%n%s%n";
-const wchar_t *wstr4 = L"Hello, ";
-const wchar_t *wstr5 = L"World!\n";
-char buf2[10] = "%s";
-int num1 = 67;
-int num2 = 987654;
-
-#define FAIL() \
-  do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
-#define CHK_FAIL_START \
-  chk_fail_ok = 1;				\
-  if (! setjmp (chk_fail_buf))			\
-    {
-#define CHK_FAIL_END \
-      chk_fail_ok = 0;				\
-      FAIL ();					\
-    }
-#if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
-# define CHK_FAIL2_START CHK_FAIL_START
-# define CHK_FAIL2_END CHK_FAIL_END
-#else
-# define CHK_FAIL2_START
-# define CHK_FAIL2_END
-#endif
-
-static int
-do_test (void)
-{
-#if __USE_FORTIFY_LEVEL == 3
-  char *buf = (char *) malloc (buf_size);
-  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
-#endif
-  set_fortify_handler (handler);
-
-  struct A { char buf1[9]; char buf2[1]; } a;
-  struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
-
-  printf ("Test checking routines at fortify level %d\n",
-#ifdef __USE_FORTIFY_LEVEL
-	  (int) __USE_FORTIFY_LEVEL
-#else
-	  0
-#endif
-	  );
-
-#if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
-  printf ("Test skipped");
-  if (l0 == 0)
-    return 0;
-#endif
-
-  /* These ops can be done without runtime checking of object size.  */
-  memcpy (buf, "abcdefghij", 10);
-  memmove (buf + 1, buf, 9);
-  if (memcmp (buf, "aabcdefghi", 10))
-    FAIL ();
-
-  memcpy (buf, "abcdefghij", 10);
-  bcopy (buf, buf + 1, 9);
-  if (memcmp (buf, "aabcdefghi", 10))
-    FAIL ();
-
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
-      || memcmp (buf, "aabcdabcde", 10))
-    FAIL ();
-
-  memset (buf + 8, 'j', 2);
-  if (memcmp (buf, "aabcdabcjj", 10))
-    FAIL ();
-
-  bzero (buf + 8, 2);
-  if (memcmp (buf, "aabcdabc\0\0", 10))
-    FAIL ();
-
-  explicit_bzero (buf + 6, 4);
-  if (memcmp (buf, "aabcda\0\0\0\0", 10))
-    FAIL ();
-
-  strcpy (buf + 4, "EDCBA");
-  if (memcmp (buf, "aabcEDCBA", 10))
-    FAIL ();
-
-  if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
-    FAIL ();
-
-  strncpy (buf + 6, "X", 4);
-  if (memcmp (buf, "aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
-    FAIL ();
-
-  if (snprintf (buf + 7, 3, "%s", "987654") != 6
-      || memcmp (buf, "aabcEDX98", 10))
-    FAIL ();
-
-  /* These ops need runtime checking, but shouldn't __chk_fail.  */
-  memcpy (buf, "abcdefghij", l0 + 10);
-  memmove (buf + 1, buf, l0 + 9);
-  if (memcmp (buf, "aabcdefghi", 10))
-    FAIL ();
-
-  memcpy (buf, "abcdefghij", l0 + 10);
-  bcopy (buf, buf + 1, l0 + 9);
-  if (memcmp (buf, "aabcdefghi", 10))
-    FAIL ();
-
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
-      || memcmp (buf, "aabcdabcde", 10))
-    FAIL ();
-
-  memset (buf + 8, 'j', l0 + 2);
-  if (memcmp (buf, "aabcdabcjj", 10))
-    FAIL ();
-
-  bzero (buf + 8, l0 + 2);
-  if (memcmp (buf, "aabcdabc\0\0", 10))
-    FAIL ();
-
-  explicit_bzero (buf + 6, l0 + 4);
-  if (memcmp (buf, "aabcda\0\0\0\0", 10))
-    FAIL ();
-
-  strcpy (buf + 4, str1 + 5);
-  if (memcmp (buf, "aabcEDCBA", 10))
-    FAIL ();
-
-  if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
-    FAIL ();
-
-  strncpy (buf + 6, "X", l0 + 4);
-  if (memcmp (buf, "aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
-      || memcmp (buf, "aabcEcd\0\0", 10))
-    FAIL ();
-
-  if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
-    FAIL ();
-
-  if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
-    FAIL ();
-
-  buf[l0 + 8] = '\0';
-  strcat (buf, "A");
-  if (memcmp (buf, "aabcEcd9A", 10))
-    FAIL ();
-
-  buf[l0 + 7] = '\0';
-  strncat (buf, "ZYXWV", l0 + 2);
-  if (memcmp (buf, "aabcEcdZY", 10))
-    FAIL ();
-
-  /* The following tests are supposed to succeed at all fortify
-     levels, even though they overflow a.buf1 into a.buf2.  */
-  memcpy (a.buf1, "abcdefghij", l0 + 10);
-  memmove (a.buf1 + 1, a.buf1, l0 + 9);
-  if (memcmp (a.buf1, "aabcdefghi", 10))
-    FAIL ();
-
-  memcpy (a.buf1, "abcdefghij", l0 + 10);
-  bcopy (a.buf1, a.buf1 + 1, l0 + 9);
-  if (memcmp (a.buf1, "aabcdefghi", 10))
-    FAIL ();
-
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
-      || memcmp (a.buf1, "aabcdabcde", 10))
-    FAIL ();
-
-  memset (a.buf1 + 8, 'j', l0 + 2);
-  if (memcmp (a.buf1, "aabcdabcjj", 10))
-    FAIL ();
-
-  bzero (a.buf1 + 8, l0 + 2);
-  if (memcmp (a.buf1, "aabcdabc\0\0", 10))
-    FAIL ();
-
-  explicit_bzero (a.buf1 + 6, l0 + 4);
-  if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL < 2
-  /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
-     and sufficient GCC support, as the string operations overflow
-     from a.buf1 into a.buf2.  */
-  strcpy (a.buf1 + 4, str1 + 5);
-  if (memcmp (a.buf1, "aabcEDCBA", 10))
-    FAIL ();
-
-  if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
-      || memcmp (a.buf1, "aabcEDCBF", 10))
-    FAIL ();
-
-  strncpy (a.buf1 + 6, "X", l0 + 4);
-  if (memcmp (a.buf1, "aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (sprintf (a.buf1 + 7, "%d", num1) != 2
-      || memcmp (a.buf1, "aabcEDX67", 10))
-    FAIL ();
-
-  if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
-      || memcmp (a.buf1, "aabcEDX98", 10))
-    FAIL ();
-
-  a.buf1[l0 + 8] = '\0';
-  strcat (a.buf1, "A");
-  if (memcmp (a.buf1, "aabcEDX9A", 10))
-    FAIL ();
-
-  a.buf1[l0 + 7] = '\0';
-  strncat (a.buf1, "ZYXWV", l0 + 2);
-  if (memcmp (a.buf1, "aabcEDXZY", 10))
-    FAIL ();
-
-#endif
-
-#if __USE_FORTIFY_LEVEL >= 1
-  /* Now check if all buffer overflows are caught at runtime.
-     N.B. All tests involving a length parameter need to be done
-     twice: once with the length a compile-time constant, once without.  */
-
-  CHK_FAIL_START
-  memcpy (buf + 1, "abcdefghij", 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memcpy (buf + 1, "abcdefghij", l0 + 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memmove (buf + 2, buf + 1, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memmove (buf + 2, buf + 1, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bcopy (buf + 1, buf + 2, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bcopy (buf + 1, buf + 2, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = (char *) mempcpy (buf + 6, "abcde", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memset (buf + 9, 'j', 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memset (buf + 9, 'j', l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bzero (buf + 9, 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bzero (buf + 9, l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  explicit_bzero (buf + 9, 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  explicit_bzero (buf + 9, l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  strcpy (buf + 5, str1 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = stpcpy (buf + 9, str2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  strncpy (buf + 7, "X", 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  strncpy (buf + 7, "X", l0 + 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  stpncpy (buf + 6, "cd", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  stpncpy (buf + 6, "cd", l0 + 5);
-  CHK_FAIL_END
-
-# if !defined __cplusplus || defined __va_arg_pack
-  CHK_FAIL_START
-  sprintf (buf + 8, "%d", num1);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  snprintf (buf + 8, 3, "%d", num2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  snprintf (buf + 8, l0 + 3, "%d", num2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  swprintf (wbuf + 8, 3, L"%d", num1);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  swprintf (wbuf + 8, l0 + 3, L"%d", num1);
-  CHK_FAIL_END
-# endif
-
-  memcpy (buf, str1 + 2, 9);
-  CHK_FAIL_START
-  strcat (buf, "AB");
-  CHK_FAIL_END
-
-  memcpy (buf, str1 + 3, 8);
-  CHK_FAIL_START
-  strncat (buf, "ZYXWV", 3);
-  CHK_FAIL_END
-
-  memcpy (buf, str1 + 3, 8);
-  CHK_FAIL_START
-  strncat (buf, "ZYXWV", l0 + 3);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memcpy (a.buf1 + 1, "abcdefghij", 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memmove (a.buf1 + 2, a.buf1 + 1, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bcopy (a.buf1 + 1, a.buf1 + 2, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memset (a.buf1 + 9, 'j', 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  memset (a.buf1 + 9, 'j', l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bzero (a.buf1 + 9, 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  bzero (a.buf1 + 9, l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  explicit_bzero (a.buf1 + 9, 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  explicit_bzero (a.buf1 + 9, l0 + 2);
-  CHK_FAIL_END
-
-# if __USE_FORTIFY_LEVEL >= 2
-#  define O 0
-# else
-#  define O 1
-# endif
-
-  CHK_FAIL_START
-  strcpy (a.buf1 + (O + 4), str1 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  p = stpcpy (a.buf1 + (O + 8), str2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  strncpy (a.buf1 + (O + 6), "X", 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  strncpy (a.buf1 + (O + 6), "X", l0 + 4);
-  CHK_FAIL_END
-
-# if !defined __cplusplus || defined __va_arg_pack
-  CHK_FAIL_START
-  sprintf (a.buf1 + (O + 7), "%d", num1);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  snprintf (a.buf1 + (O + 7), 3, "%d", num2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
-  CHK_FAIL_END
-# endif
-
-  memcpy (a.buf1, str1 + (3 - O), 8 + O);
-  CHK_FAIL_START
-  strcat (a.buf1, "AB");
-  CHK_FAIL_END
-
-  memcpy (a.buf1, str1 + (4 - O), 7 + O);
-  CHK_FAIL_START
-  strncat (a.buf1, "ZYXWV", l0 + 3);
-  CHK_FAIL_END
-#endif
-
-
-  /* These ops can be done without runtime checking of object size.  */
-  wmemcpy (wbuf, L"abcdefghij", 10);
-  wmemmove (wbuf + 1, wbuf, 9);
-  if (wmemcmp (wbuf, L"aabcdefghi", 10))
-    FAIL ();
-
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
-      || wmemcmp (wbuf, L"aabcdabcde", 10))
-    FAIL ();
-
-  wmemset (wbuf + 8, L'j', 2);
-  if (wmemcmp (wbuf, L"aabcdabcjj", 10))
-    FAIL ();
-
-  wcscpy (wbuf + 4, L"EDCBA");
-  if (wmemcmp (wbuf, L"aabcEDCBA", 10))
-    FAIL ();
-
-  if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
-    FAIL ();
-
-  wcsncpy (wbuf + 6, L"X", 4);
-  if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
-      || wmemcmp (wbuf, L"aabcEDX98", 10))
-    FAIL ();
-
-  if (swprintf (wbuf + 7, 3, L"64") != 2
-      || wmemcmp (wbuf, L"aabcEDX64", 10))
-    FAIL ();
-
-  /* These ops need runtime checking, but shouldn't __chk_fail.  */
-  wmemcpy (wbuf, L"abcdefghij", l0 + 10);
-  wmemmove (wbuf + 1, wbuf, l0 + 9);
-  if (wmemcmp (wbuf, L"aabcdefghi", 10))
-    FAIL ();
-
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
-      || wmemcmp (wbuf, L"aabcdabcde", 10))
-    FAIL ();
-
-  wmemset (wbuf + 8, L'j', l0 + 2);
-  if (wmemcmp (wbuf, L"aabcdabcjj", 10))
-    FAIL ();
-
-  wcscpy (wbuf + 4, wstr1 + 5);
-  if (wmemcmp (wbuf, L"aabcEDCBA", 10))
-    FAIL ();
-
-  if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
-    FAIL ();
-
-  wcsncpy (wbuf + 6, L"X", l0 + 4);
-  if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
-      || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
-    FAIL ();
-
-  if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
-      || wmemcmp (wbuf, L"aabcEcd98", 10))
-    FAIL ();
-
-  wbuf[l0 + 8] = L'\0';
-  wcscat (wbuf, L"A");
-  if (wmemcmp (wbuf, L"aabcEcd9A", 10))
-    FAIL ();
-
-  wbuf[l0 + 7] = L'\0';
-  wcsncat (wbuf, L"ZYXWV", l0 + 2);
-  if (wmemcmp (wbuf, L"aabcEcdZY", 10))
-    FAIL ();
-
-  wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
-  wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
-  if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
-    FAIL ();
-
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
-      || wmemcmp (wa.buf1, L"aabcdabcde", 10))
-    FAIL ();
-
-  wmemset (wa.buf1 + 8, L'j', l0 + 2);
-  if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL < 2
-  /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
-     and sufficient GCC support, as the string operations overflow
-     from a.buf1 into a.buf2.  */
-  wcscpy (wa.buf1 + 4, wstr1 + 5);
-  if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
-    FAIL ();
-
-  if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
-      || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
-    FAIL ();
-
-  wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
-  if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
-    FAIL ();
-
-  if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
-      || wmemcmp (wa.buf1, L"aabcEDX98", 10))
-    FAIL ();
-
-  wa.buf1[l0 + 8] = L'\0';
-  wcscat (wa.buf1, L"A");
-  if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
-    FAIL ();
-
-  wa.buf1[l0 + 7] = L'\0';
-  wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
-  if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
-    FAIL ();
-
-#endif
-
-#if __USE_FORTIFY_LEVEL >= 1
-  /* Now check if all buffer overflows are caught at runtime.
-     N.B. All tests involving a length parameter need to be done
-     twice: once with the length a compile-time constant, once without.  */
-
-  CHK_FAIL_START
-  wmemcpy (wbuf + 1, L"abcdefghij", 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemcpy (wbuf + 9, L"abcdefghij", 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemmove (wbuf + 2, wbuf + 1, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wmempcpy (wbuf + 6, L"abcde", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemset (wbuf + 9, L'j', 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemset (wbuf + 9, L'j', l0 + 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcscpy (wbuf + 5, wstr1 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wcpcpy (wbuf + 9, wstr2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcsncpy (wbuf + 7, L"X", 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcsncpy (wbuf + 7, L"X", l0 + 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcpncpy (wbuf + 6, L"cd", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcpncpy (wbuf + 6, L"cd", l0 + 5);
-  CHK_FAIL_END
-
-  wmemcpy (wbuf, wstr1 + 2, 9);
-  CHK_FAIL_START
-  wcscat (wbuf, L"AB");
-  CHK_FAIL_END
-
-  wmemcpy (wbuf, wstr1 + 3, 8);
-  CHK_FAIL_START
-  wcsncat (wbuf, L"ZYXWV", l0 + 3);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemset (wa.buf1 + 9, L'j', 2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wmemset (wa.buf1 + 9, L'j', l0 + 2);
-  CHK_FAIL_END
-
-#if __USE_FORTIFY_LEVEL >= 2
-# define O 0
-#else
-# define O 1
-#endif
-
-  CHK_FAIL_START
-  wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcsncpy (wa.buf1 + (O + 6), L"X", 4);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
-  CHK_FAIL_END
-
-  wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
-  CHK_FAIL_START
-  wcscat (wa.buf1, L"AB");
-  CHK_FAIL_END
-
-  wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
-  CHK_FAIL_START
-  wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
-  CHK_FAIL_END
-#endif
-
-
-  /* Now checks for %n protection.  */
-
-  /* Constant literals passed directly are always ok
-     (even with warnings about possible bugs from GCC).  */
-  int n1, n2;
-  if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
-      || n1 != 1 || n2 != 2)
-    FAIL ();
-
-  /* In this case the format string is not known at compile time,
-     but resides in read-only memory, so is ok.  */
-  if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
-      || n1 != 1 || n2 != 2)
-    FAIL ();
-
-  strcpy (buf2 + 2, "%n%s%n");
-  /* When the format string is writable and contains %n,
-     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
-  CHK_FAIL2_START
-  if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
-    FAIL ();
-  CHK_FAIL2_END
-
-  CHK_FAIL2_START
-  if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
-    FAIL ();
-  CHK_FAIL2_END
-
-  /* But if there is no %n, even writable format string
-     should work.  */
-  buf2[6] = '\0';
-  if (sprintf (buf, buf2 + 4, str2) != 1)
-    FAIL ();
-
-  /* Constant literals passed directly are always ok
-     (even with warnings about possible bugs from GCC).  */
-  if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
-      || n1 != 7 || n2 != 14)
-    FAIL ();
-
-  /* In this case the format string is not known at compile time,
-     but resides in read-only memory, so is ok.  */
-  if (printf (str3, str4, &n1, str5, &n2) != 14
-      || n1 != 7 || n2 != 14)
-    FAIL ();
-
-  strcpy (buf2 + 2, "%n%s%n");
-  /* When the format string is writable and contains %n,
-     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
-  CHK_FAIL2_START
-  if (printf (buf2, str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  CHK_FAIL2_END
-
-  /* But if there is no %n, even writable format string
-     should work.  */
-  buf2[6] = '\0';
-  if (printf (buf2 + 4, str5) != 7)
-    FAIL ();
-
-  FILE *fp = stdout;
-
-  /* Constant literals passed directly are always ok
-     (even with warnings about possible bugs from GCC).  */
-  if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
-      || n1 != 7 || n2 != 14)
-    FAIL ();
-
-  /* In this case the format string is not known at compile time,
-     but resides in read-only memory, so is ok.  */
-  if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
-      || n1 != 7 || n2 != 14)
-    FAIL ();
-
-  strcpy (buf2 + 2, "%n%s%n");
-  /* When the format string is writable and contains %n,
-     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
-  CHK_FAIL2_START
-  if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  CHK_FAIL2_END
-
-  /* But if there is no %n, even writable format string
-     should work.  */
-  buf2[6] = '\0';
-  if (fprintf (fp, buf2 + 4, str5) != 7)
-    FAIL ();
-
-  char *my_ptr = NULL;
-  strcpy (buf2 + 2, "%n%s%n");
-  /* When the format string is writable and contains %n,
-     with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
-  CHK_FAIL2_START
-  if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  else
-    free (my_ptr);
-  CHK_FAIL2_END
-
-  struct obstack obs;
-  obstack_init (&obs);
-  CHK_FAIL2_START
-  if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  CHK_FAIL2_END
-  obstack_free (&obs, NULL);
-
-  my_ptr = NULL;
-  if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  else
-    free (my_ptr);
-
-  obstack_init (&obs);
-  if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
-    FAIL ();
-  obstack_free (&obs, NULL);
-
-  if (freopen (temp_filename, "r", stdin) == NULL)
-    {
-      puts ("could not open temporary file");
-      exit (1);
-    }
-
-  if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
-    FAIL ();
-  if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (gets (buf) != buf)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  rewind (stdin);
-
-  if (fgets (buf, buf_size, stdin) != buf
-      || memcmp (buf, "abcdefgh\n", 10))
-    FAIL ();
-  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
-    FAIL ();
-
-  rewind (stdin);
-
-  if (fgets (buf, l0 + buf_size, stdin) != buf
-      || memcmp (buf, "abcdefgh\n", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (fgets (buf, buf_size + 1, stdin) != buf)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  rewind (stdin);
-
-  if (fgets_unlocked (buf, buf_size, stdin) != buf
-      || memcmp (buf, "abcdefgh\n", 10))
-    FAIL ();
-  if (fgets_unlocked (buf, buf_size, stdin) != buf
-      || memcmp (buf, "ABCDEFGHI", 10))
-    FAIL ();
-
-  rewind (stdin);
-
-  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
-      || memcmp (buf, "abcdefgh\n", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  rewind (stdin);
-
-  if (fread (buf, 1, buf_size, stdin) != buf_size
-      || memcmp (buf, "abcdefgh\nA", 10))
-    FAIL ();
-  if (fread (buf, buf_size, 1, stdin) != 1
-      || memcmp (buf, "BCDEFGHI\na", 10))
-    FAIL ();
-
-  rewind (stdin);
-
-  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
-      || memcmp (buf, "abcdefgh\nA", 10))
-    FAIL ();
-  if (fread (buf, buf_size, l0 + 1, stdin) != 1
-      || memcmp (buf, "BCDEFGHI\na", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  rewind (stdin);
-
-  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
-      || memcmp (buf, "abcdefgh\nA", 10))
-    FAIL ();
-  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
-      || memcmp (buf, "BCDEFGHI\na", 10))
-    FAIL ();
-
-  rewind (stdin);
-
-  if (fread_unlocked (buf, 1, 4, stdin) != 4
-      || memcmp (buf, "abcdFGHI\na", 10))
-    FAIL ();
-  if (fread_unlocked (buf, 4, 1, stdin) != 1
-      || memcmp (buf, "efghFGHI\na", 10))
-    FAIL ();
-
-  rewind (stdin);
-
-  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
-      || memcmp (buf, "abcdefgh\nA", 10))
-    FAIL ();
-  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
-      || memcmp (buf, "BCDEFGHI\na", 10))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  lseek (fileno (stdin), 0, SEEK_SET);
-
-  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
-      || memcmp (buf, "abcdefgh\n", 9))
-    FAIL ();
-  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
-      || memcmp (buf, "ABCDEFGHI", 9))
-    FAIL ();
-
-  lseek (fileno (stdin), 0, SEEK_SET);
-
-  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
-      || memcmp (buf, "abcdefgh\n", 9))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
-      != buf_size - 1
-      || memcmp (buf, "\nABCDEFGH", 9))
-    FAIL ();
-  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
-      || memcmp (buf, "abcdefgh\n", 9))
-    FAIL ();
-  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
-      != buf_size - 1
-      || memcmp (buf, "h\nABCDEFG", 9))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
-      != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
-      != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
-      != buf_size - 1
-      || memcmp (buf, "\nABCDEFGH", 9))
-    FAIL ();
-  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
-      || memcmp (buf, "abcdefgh\n", 9))
-    FAIL ();
-  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
-      != buf_size - 1
-      || memcmp (buf, "h\nABCDEFG", 9))
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
-      != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
-      != buf_size + 1)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  if (freopen (temp_filename, "r", stdin) == NULL)
-    {
-      puts ("could not open temporary file");
-      exit (1);
-    }
-
-  if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
-    {
-      puts ("could not seek in test file");
-      exit (1);
-    }
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (gets (buf) != buf)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  /* Check whether missing N$ formats are detected.  */
-  CHK_FAIL2_START
-  printf ("%3$d\n", 1, 2, 3, 4);
-  CHK_FAIL2_END
-
-  CHK_FAIL2_START
-  fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
-  CHK_FAIL2_END
-
-  CHK_FAIL2_START
-  sprintf (buf, "%3$d\n", 1, 2, 3, 4);
-  CHK_FAIL2_END
-
-  CHK_FAIL2_START
-  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
-  CHK_FAIL2_END
-
-  int sp[2];
-  if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
-    FAIL ();
-  else
-    {
-      const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
-      if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
-	  != strlen (sendstr))
-	FAIL ();
-
-      char recvbuf[12];
-      if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
-	  != sizeof recvbuf
-	  || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
-	FAIL ();
-
-      if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
-	  != sizeof recvbuf - 7
-	  || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
-	  != sizeof recvbuf)
-	FAIL ();
-      CHK_FAIL_END
-
-      CHK_FAIL_START
-      if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
-	  != sizeof recvbuf - 3)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-
-      socklen_t sl;
-      struct sockaddr_un sa_un;
-
-      sl = sizeof (sa_un);
-      if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
-		    (struct sockaddr *) &sa_un, &sl)
-	  != sizeof recvbuf
-	  || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
-	FAIL ();
-
-      sl = sizeof (sa_un);
-      if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
-		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
-	  || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      sl = sizeof (sa_un);
-      if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
-		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
-	FAIL ();
-      CHK_FAIL_END
-
-      CHK_FAIL_START
-      sl = sizeof (sa_un);
-      if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
-		    (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-
-      close (sp[0]);
-      close (sp[1]);
-    }
-
-  char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
-  char *enddir = strchr (fname, '\0');
-  if (mkdtemp (fname) == NULL)
-    {
-      printf ("mkdtemp failed: %m\n");
-      return 1;
-    }
-  *enddir = '/';
-  if (symlink ("bar", fname) != 0)
-    FAIL ();
-
-  char readlinkbuf[4];
-  if (readlink (fname, readlinkbuf, 4) != 3
-      || memcmp (readlinkbuf, "bar", 3) != 0)
-    FAIL ();
-  if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
-      || memcmp (readlinkbuf, "bbar", 4) != 0)
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (readlink (fname, readlinkbuf + 3, 4) != 3)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
-  if (tmpfd < 0)
-    FAIL ();
-
-  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
-      || memcmp (readlinkbuf, "bar", 3) != 0)
-    FAIL ();
-  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
-		  l0 + 3) != 3
-      || memcmp (readlinkbuf, "bbar", 4) != 0)
-    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
-		  l0 + 3) != 3)
-    FAIL ();
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
-		  4) != 3)
-    FAIL ();
-  CHK_FAIL_END
-#endif
-
-  close (tmpfd);
-
-  char *cwd1 = getcwd (NULL, 0);
-  if (cwd1 == NULL)
-    FAIL ();
-
-  char *cwd2 = getcwd (NULL, 250);
-  if (cwd2 == NULL)
-    FAIL ();
-
-  if (cwd1 && cwd2)
-    {
-      if (strcmp (cwd1, cwd2) != 0)
-	FAIL ();
-
-      *enddir = '\0';
-      if (chdir (fname))
-	FAIL ();
-
-      char *cwd3 = getcwd (NULL, 0);
-      if (cwd3 == NULL)
-	FAIL ();
-      if (strcmp (fname, cwd3) != 0)
-	printf ("getcwd after chdir is '%s' != '%s',"
-		"get{c,}wd tests skipped\n", cwd3, fname);
-      else
-	{
-	  char getcwdbuf[sizeof fname - 3];
-
-	  char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
-	  if (cwd4 != getcwdbuf
-	      || strcmp (getcwdbuf, fname) != 0)
-	    FAIL ();
-
-	  cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
-	  if (cwd4 != getcwdbuf + 1
-	      || getcwdbuf[0] != fname[0]
-	      || strcmp (getcwdbuf + 1, fname) != 0)
-	    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-	  CHK_FAIL_START
-	  if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
-	      != getcwdbuf + 2)
-	    FAIL ();
-	  CHK_FAIL_END
-
-	  CHK_FAIL_START
-	  if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
-	      != getcwdbuf + 2)
-	    FAIL ();
-	  CHK_FAIL_END
-#endif
-
-	  if (getwd (getcwdbuf) != getcwdbuf
-	      || strcmp (getcwdbuf, fname) != 0)
-	    FAIL ();
-
-	  if (getwd (getcwdbuf + 1) != getcwdbuf + 1
-	      || strcmp (getcwdbuf + 1, fname) != 0)
-	    FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-	  CHK_FAIL_START
-	  if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
-	    FAIL ();
-	  CHK_FAIL_END
-#endif
-	}
-
-      if (chdir (cwd1) != 0)
-	FAIL ();
-      free (cwd3);
-    }
-
-  free (cwd1);
-  free (cwd2);
-  *enddir = '/';
-  if (unlink (fname) != 0)
-    FAIL ();
-
-  *enddir = '\0';
-  if (rmdir (fname) != 0)
-    FAIL ();
-
-
-#if PATH_MAX > 0
-  char largebuf[PATH_MAX];
-  char *realres = realpath (".", largebuf);
-  if (realres != largebuf)
-    FAIL ();
-
-# if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char realbuf[1];
-  realres = realpath (".", realbuf);
-  if (realres != realbuf)
-    FAIL ();
-  CHK_FAIL_END
-# endif
-#endif
-
-  if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
-    {
-      assert (MB_CUR_MAX <= 10);
-
-      /* First a simple test.  */
-      char enough[10];
-      if (wctomb (enough, L'A') != 1)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      /* We know the wchar_t encoding is ISO 10646.  So pick a
-	 character which has a multibyte representation which does not
-	 fit.  */
-      CHK_FAIL_START
-      char smallbuf[2];
-      if (wctomb (smallbuf, L'\x100') != 2)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-
-      mbstate_t s;
-      memset (&s, '\0', sizeof (s));
-      if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      /* We know the wchar_t encoding is ISO 10646.  So pick a
-	 character which has a multibyte representation which does not
-	 fit.  */
-      CHK_FAIL_START
-      char smallbuf[2];
-      if (wcrtomb (smallbuf, L'\x100', &s) != 2)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-
-      wchar_t wenough[10];
-      memset (&s, '\0', sizeof (s));
-      const char *cp = "A";
-      if (mbsrtowcs (wenough, &cp, 10, &s) != 1
-	  || wcscmp (wenough, L"A") != 0)
-	FAIL ();
-
-      cp = "BC";
-      if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
-	  || wcscmp (wenough, L"BC") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      wchar_t wsmallbuf[2];
-      cp = "ABC";
-      mbsrtowcs (wsmallbuf, &cp, 10, &s);
-      CHK_FAIL_END
-#endif
-
-      cp = "A";
-      if (mbstowcs (wenough, cp, 10) != 1
-	  || wcscmp (wenough, L"A") != 0)
-	FAIL ();
-
-      cp = "DEF";
-      if (mbstowcs (wenough, cp, l0 + 10) != 3
-	  || wcscmp (wenough, L"DEF") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      wchar_t wsmallbuf[2];
-      cp = "ABC";
-      mbstowcs (wsmallbuf, cp, 10);
-      CHK_FAIL_END
-#endif
-
-      memset (&s, '\0', sizeof (s));
-      cp = "ABC";
-      wcscpy (wenough, L"DEF");
-      if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
-	  || wcscmp (wenough, L"AEF") != 0)
-	FAIL ();
-
-      cp = "IJ";
-      if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
-	  || wcscmp (wenough, L"IEF") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      wchar_t wsmallbuf[2];
-      cp = "ABC";
-      mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
-      CHK_FAIL_END
-#endif
-
-      memset (&s, '\0', sizeof (s));
-      const wchar_t *wcp = L"A";
-      if (wcsrtombs (enough, &wcp, 10, &s) != 1
-	  || strcmp (enough, "A") != 0)
-	FAIL ();
-
-      wcp = L"BC";
-      if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
-	  || strcmp (enough, "BC") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      char smallbuf[2];
-      wcp = L"ABC";
-      wcsrtombs (smallbuf, &wcp, 10, &s);
-      CHK_FAIL_END
-#endif
-
-      memset (enough, 'Z', sizeof (enough));
-      wcp = L"EF";
-      if (wcstombs (enough, wcp, 10) != 2
-	  || strcmp (enough, "EF") != 0)
-	FAIL ();
-
-      wcp = L"G";
-      if (wcstombs (enough, wcp, l0 + 10) != 1
-	  || strcmp (enough, "G") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      char smallbuf[2];
-      wcp = L"ABC";
-      wcstombs (smallbuf, wcp, 10);
-      CHK_FAIL_END
-#endif
-
-      memset (&s, '\0', sizeof (s));
-      wcp = L"AB";
-      if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
-	  || strcmp (enough, "A") != 0)
-	FAIL ();
-
-      wcp = L"BCD";
-      if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
-	  || strcmp (enough, "B") != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      char smallbuf[2];
-      wcp = L"ABC";
-      wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
-      CHK_FAIL_END
-#endif
-    }
-  else
-    {
-      puts ("cannot set locale");
-      ret = 1;
-    }
-
-  int fd = posix_openpt (O_RDWR);
-  if (fd != -1)
-    {
-      char enough[1000];
-      if (ptsname_r (fd, enough, sizeof (enough)) != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      char smallbuf[2];
-      if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-      close (fd);
-    }
-
-#if PATH_MAX > 0
-  confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
-# if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char smallbuf[1];
-  confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
-  CHK_FAIL_END
-# endif
-#endif
-
-  gid_t grpslarge[5];
-  int ngr = getgroups (5, grpslarge);
-  asm volatile ("" : : "r" (ngr));
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char smallbuf[1];
-  ngr = getgroups (5, (gid_t *) smallbuf);
-  asm volatile ("" : : "r" (ngr));
-  CHK_FAIL_END
-#endif
-
-  fd = open (_PATH_TTY, O_RDONLY);
-  if (fd != -1)
-    {
-      char enough[1000];
-      if (ttyname_r (fd, enough, sizeof (enough)) != 0)
-	FAIL ();
-
-#if __USE_FORTIFY_LEVEL >= 1
-      CHK_FAIL_START
-      char smallbuf[2];
-      if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
-	FAIL ();
-      CHK_FAIL_END
-#endif
-      close (fd);
-    }
-
-  char hostnamelarge[1000];
-  gethostname (hostnamelarge, sizeof (hostnamelarge));
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char smallbuf[1];
-  gethostname (smallbuf, sizeof (hostnamelarge));
-  CHK_FAIL_END
-#endif
-
-  char loginlarge[1000];
-  getlogin_r (loginlarge, sizeof (hostnamelarge));
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char smallbuf[1];
-  getlogin_r (smallbuf, sizeof (loginlarge));
-  CHK_FAIL_END
-#endif
-
-  char domainnamelarge[1000];
-  int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
-  asm volatile ("" : : "r" (res));
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  char smallbuf[1];
-  res = getdomainname (smallbuf, sizeof (domainnamelarge));
-  asm volatile ("" : : "r" (res));
-  CHK_FAIL_END
-#endif
-
-  fd_set s;
-  FD_ZERO (&s);
-
-  FD_SET (FD_SETSIZE - 1, &s);
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  FD_SET (FD_SETSIZE, &s);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  FD_SET (l0 + FD_SETSIZE, &s);
-  CHK_FAIL_END
-#endif
-
-  FD_CLR (FD_SETSIZE - 1, &s);
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  FD_CLR (FD_SETSIZE, &s);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  FD_SET (l0 + FD_SETSIZE, &s);
-  CHK_FAIL_END
-#endif
-
-  FD_ISSET (FD_SETSIZE - 1, &s);
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  FD_ISSET (FD_SETSIZE, &s);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  FD_ISSET (l0 + FD_SETSIZE, &s);
-  CHK_FAIL_END
-#endif
-
-  struct pollfd fds[1];
-  fds[0].fd = STDOUT_FILENO;
-  fds[0].events = POLLOUT;
-  poll (fds, 1, 0);
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  poll (fds, 2, 0);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  poll (fds, l0 + 2, 0);
-  CHK_FAIL_END
-#endif
-  ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
-  CHK_FAIL_START
-  ppoll (fds, 2, NULL, NULL);
-  CHK_FAIL_END
-
-  CHK_FAIL_START
-  ppoll (fds, l0 + 2, NULL, NULL);
-  CHK_FAIL_END
-#endif
-
-  return ret;
-}
+#define _FORTIFY_SOURCE 1
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk4.cc b/debug/tst-chk1cc-lfs.cc
similarity index 100%
rename from debug/tst-lfschk4.cc
rename to debug/tst-chk1cc-lfs.cc
diff --git a/debug/tst-chk5.cc b/debug/tst-chk1cc.cc
similarity index 54%
rename from debug/tst-chk5.cc
rename to debug/tst-chk1cc.cc
index be37ce2d22..c09b01df34 100644
--- a/debug/tst-chk5.cc
+++ b/debug/tst-chk1cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk2.c b/debug/tst-chk2-lfs.c
similarity index 100%
rename from debug/tst-lfschk2.c
rename to debug/tst-chk2-lfs.c
diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
index be37ce2d22..983cb8aaa8 100644
--- a/debug/tst-chk2.c
+++ b/debug/tst-chk2.c
@@ -1,2 +1,2 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
+#define _FORTIFY_SOURCE 2
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk5.cc b/debug/tst-chk2cc-lfs.cc
similarity index 100%
rename from debug/tst-lfschk5.cc
rename to debug/tst-chk2cc-lfs.cc
diff --git a/debug/tst-chk6.cc b/debug/tst-chk2cc.cc
similarity index 54%
rename from debug/tst-chk6.cc
rename to debug/tst-chk2cc.cc
index 38b8e4fb36..983cb8aaa8 100644
--- a/debug/tst-chk6.cc
+++ b/debug/tst-chk2cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk3.c b/debug/tst-chk3-lfs.c
similarity index 100%
rename from debug/tst-lfschk3.c
rename to debug/tst-chk3-lfs.c
diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
index 38b8e4fb36..2f87061b00 100644
--- a/debug/tst-chk3.c
+++ b/debug/tst-chk3.c
@@ -1,2 +1,2 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
+#define _FORTIFY_SOURCE 3
+#include "tst-chk0.c"
diff --git a/debug/tst-lfschk6.cc b/debug/tst-chk3cc-lfs.cc
similarity index 100%
rename from debug/tst-lfschk6.cc
rename to debug/tst-chk3cc-lfs.cc
diff --git a/debug/tst-chk8.cc b/debug/tst-chk3cc.cc
similarity index 54%
rename from debug/tst-chk8.cc
rename to debug/tst-chk3cc.cc
index 2a7b323812..2f87061b00 100644
--- a/debug/tst-chk8.cc
+++ b/debug/tst-chk3cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
+#include "tst-chk0.c"
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
deleted file mode 100644
index c82e6aac86..0000000000
--- a/debug/tst-chk4.cc
+++ /dev/null
@@ -1 +0,0 @@
-#include "tst-chk1.c"
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk7.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
  2022-01-05  1:39 ` [PATCH 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-05  1:39 ` Siddhesh Poyarekar
  2022-01-05  4:45 ` [PATCH v2 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  1:39 UTC (permalink / raw)
  To: libc-alpha

Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
main headers as they got incorporated into the standard, but their
fortified variants remained under __USE_GNU.  As a result, these
functions did not get fortified when _GNU_SOURCE was not defined.

Add test wrappers that check all functions tested in tst-chk0 at all
levels with _GNU_SOURCE undefined and then use the failures to (1)
exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
feature macro guards in the fortified function headers so that they're
the same as the ones in the main headers.

This fixes BZ #28746.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                 | 17 +++++++-----
 debug/tst-chk0-nongnu.c        |  3 +++
 debug/tst-chk0.c               | 49 ++++++++++++++++++++++++++++------
 debug/tst-chk0cc-nongnu.cc     |  3 +++
 debug/tst-chk1-nongnu.c        |  3 +++
 debug/tst-chk1cc-nongnu.cc     |  3 +++
 debug/tst-chk2-nongnu.c        |  3 +++
 debug/tst-chk2cc-nongnu.cc     |  3 +++
 debug/tst-chk3-nongnu.c        |  3 +++
 debug/tst-chk3cc-nongnu.cc     |  3 +++
 posix/bits/unistd.h            |  2 +-
 string/bits/string_fortified.h |  8 +++---
 support/xsignal.h              |  2 ++
 wcsmbs/bits/wchar2.h           |  2 +-
 14 files changed, 85 insertions(+), 19 deletions(-)
 create mode 100644 debug/tst-chk0-nongnu.c
 create mode 100644 debug/tst-chk0cc-nongnu.cc
 create mode 100644 debug/tst-chk1-nongnu.c
 create mode 100644 debug/tst-chk1cc-nongnu.cc
 create mode 100644 debug/tst-chk2-nongnu.c
 create mode 100644 debug/tst-chk2cc-nongnu.cc
 create mode 100644 debug/tst-chk3-nongnu.c
 create mode 100644 debug/tst-chk3cc-nongnu.cc

diff --git a/debug/Makefile b/debug/Makefile
index fe94cd3072..cae00593a8 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -116,6 +116,8 @@ tests-chk = $(addprefix tst-chk, 0 1 2 3)
 tests-chk-cc = $(addsuffix cc, $(tests-chk))
 tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
 tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
+tests-chk-nongnu = $(addsuffix -nongnu, $(tests-chk))
+tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
 
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
@@ -126,16 +128,17 @@ define disable-warnings
 CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
 endef
 
-$(foreach t,$(tests-chk) $(tests-chk-lfs), \
+$(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
 	  $(eval $(call disable-warnings,$(t),c)))
 
-$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
 	  $(eval $(call disable-warnings,$(t),cc)))
 
 define link-cc
 LDLIBS-$(1) = -lstdc++
 endef
-$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
+	  $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -156,14 +159,15 @@ tests = backtrace-tst tst-longjmp_chk \
 	test-strcpy_chk test-stpcpy_chk \
 	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
 	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
-	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
+	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
+	$(tests-chk-nongnu) $(tests-chk-cc-nongnu)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
+tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu)
 endif
 
 extra-libs = libSegFault libpcprofile
@@ -194,7 +198,8 @@ define chk-gen-locales
 $(objpfx)$(1).out: $(gen-locales)
 endef
 $(foreach t, \
-	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
+	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
+	  $(tests-chk-nongnu) $(tests-chk-cc-nongnu), \
 	  $(eval $(call link-cc,$(t))))
 endif
 
diff --git a/debug/tst-chk0-nongnu.c b/debug/tst-chk0-nongnu.c
new file mode 100644
index 0000000000..8f7dd80228
--- /dev/null
+++ b/debug/tst-chk0-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk0.c"
diff --git a/debug/tst-chk0.c b/debug/tst-chk0.c
index d11e536f87..6b2ec33f87 100644
--- a/debug/tst-chk0.c
+++ b/debug/tst-chk0.c
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004-2021 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -36,6 +37,17 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#ifndef _GNU_SOURCE
+# define MEMPCPY memcpy
+# define WMEMPCPY wmemcpy
+# define MEMPCPY_RET(x) 0
+# define WMEMPCPY_RET(x) 0
+#else
+# define MEMPCPY mempcpy
+# define WMEMPCPY wmempcpy
+# define MEMPCPY_RET(x) __builtin_strlen (x)
+# define WMEMPCPY_RET(x) wcslen (x)
+#endif
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
@@ -162,7 +174,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -207,7 +219,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -266,7 +278,8 @@ do_test (void)
   if (memcmp (a.buf1, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
       || memcmp (a.buf1, "aabcdabcde", 10))
     FAIL ();
 
@@ -347,6 +360,7 @@ do_test (void)
   bcopy (buf + 1, buf + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", 5);
   CHK_FAIL_END
@@ -354,6 +368,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (buf + 9, 'j', 2);
@@ -464,6 +479,7 @@ do_test (void)
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
   CHK_FAIL_END
@@ -471,6 +487,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (a.buf1 + 9, 'j', 2);
@@ -550,7 +567,7 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -583,7 +600,8 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -625,7 +643,8 @@ do_test (void)
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
     FAIL ();
 
@@ -694,6 +713,7 @@ do_test (void)
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -701,6 +721,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wbuf + 9, L'j', 2);
@@ -768,6 +789,7 @@ do_test (void)
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -775,6 +797,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wa.buf1 + 9, L'j', 2);
@@ -906,6 +929,7 @@ do_test (void)
   if (fprintf (fp, buf2 + 4, str5) != 7)
     FAIL ();
 
+#ifdef _GNU_SOURCE
   char *my_ptr = NULL;
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
@@ -935,6 +959,7 @@ do_test (void)
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
     FAIL ();
   obstack_free (&obs, NULL);
+#endif
 
   if (freopen (temp_filename, "r", stdin) == NULL)
     {
@@ -982,6 +1007,7 @@ do_test (void)
 
   rewind (stdin);
 
+#ifdef _GNU_SOURCE
   if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
@@ -1008,6 +1034,7 @@ do_test (void)
 #endif
 
   rewind (stdin);
+#endif
 
   if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
@@ -1578,7 +1605,10 @@ do_test (void)
       ret = 1;
     }
 
-  int fd = posix_openpt (O_RDWR);
+  int fd;
+
+#ifdef _GNU_SOURCE
+  fd = posix_openpt (O_RDWR);
   if (fd != -1)
     {
       char enough[1000];
@@ -1594,6 +1624,7 @@ do_test (void)
 #endif
       close (fd);
     }
+#endif
 
 #if PATH_MAX > 0
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
@@ -1711,8 +1742,9 @@ do_test (void)
   poll (fds, l0 + 2, 0);
   CHK_FAIL_END
 #endif
+#ifdef _GNU_SOURCE
   ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
+# if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
   ppoll (fds, 2, NULL, NULL);
   CHK_FAIL_END
@@ -1720,6 +1752,7 @@ do_test (void)
   CHK_FAIL_START
   ppoll (fds, l0 + 2, NULL, NULL);
   CHK_FAIL_END
+# endif
 #endif
 
   return ret;
diff --git a/debug/tst-chk0cc-nongnu.cc b/debug/tst-chk0cc-nongnu.cc
new file mode 100644
index 0000000000..8f7dd80228
--- /dev/null
+++ b/debug/tst-chk0cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk0.c"
diff --git a/debug/tst-chk1-nongnu.c b/debug/tst-chk1-nongnu.c
new file mode 100644
index 0000000000..ed953070dd
--- /dev/null
+++ b/debug/tst-chk1-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk1.c"
diff --git a/debug/tst-chk1cc-nongnu.cc b/debug/tst-chk1cc-nongnu.cc
new file mode 100644
index 0000000000..ed953070dd
--- /dev/null
+++ b/debug/tst-chk1cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk1.c"
diff --git a/debug/tst-chk2-nongnu.c b/debug/tst-chk2-nongnu.c
new file mode 100644
index 0000000000..506f7d0396
--- /dev/null
+++ b/debug/tst-chk2-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk2.c"
diff --git a/debug/tst-chk2cc-nongnu.cc b/debug/tst-chk2cc-nongnu.cc
new file mode 100644
index 0000000000..506f7d0396
--- /dev/null
+++ b/debug/tst-chk2cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk2.c"
diff --git a/debug/tst-chk3-nongnu.c b/debug/tst-chk3-nongnu.c
new file mode 100644
index 0000000000..44fea0ea0d
--- /dev/null
+++ b/debug/tst-chk3-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk3.c"
diff --git a/debug/tst-chk3cc-nongnu.cc b/debug/tst-chk3cc-nongnu.cc
new file mode 100644
index 0000000000..44fea0ea0d
--- /dev/null
+++ b/debug/tst-chk3cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk3.c"
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 697dcbbf7b..1df7e5ceef 100644
--- a/posix/bits/unistd.h
+++ b/posix/bits/unistd.h
@@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
 			  __fd, __buf, __nbytes);
 }
 
-#ifdef __USE_UNIX98
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
 			    __off_t __offset, size_t __bufsize)
   __wur __attr_access ((__write_only__, 2, 3));
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 5731274848..fac5794c14 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
 }
 
-#ifdef __USE_GNU
+#ifdef __USE_XOPEN2K8
 __fortify_function char *
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
 {
@@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
 				  __glibc_objsize (__dest));
 }
 
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+#if __USE_XOPEN2K8
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
   return __builtin___stpncpy_chk (__dest, __src, __n,
 				  __glibc_objsize (__dest));
 }
-#else
+# else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __fortified_attr_access ((__write_only__, 1, 3))
@@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+# endif
 #endif
 
 
diff --git a/support/xsignal.h b/support/xsignal.h
index 8ee1fa6b4d..692e0f2c42 100644
--- a/support/xsignal.h
+++ b/support/xsignal.h
@@ -28,7 +28,9 @@ __BEGIN_DECLS
    terminate the process on error.  */
 
 void xraise (int sig);
+#ifdef _GNU_SOURCE
 sighandler_t xsignal (int sig, sighandler_t handler);
+#endif
 void xsigaction (int sig, const struct sigaction *newact,
                  struct sigaction *oldact);
 
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index 26012ef936..88c1fdfcd3 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 }
 
 
-#ifdef __USE_GNU
+#ifdef	__USE_XOPEN2K8
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
 				const char **__restrict __src, size_t __nmc,
 				size_t __len, mbstate_t *__restrict __ps,
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v2 0/2] _FORTIFY_SOURCE fixes [BZ #28746]
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
  2022-01-05  1:39 ` [PATCH 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
  2022-01-05  1:39 ` [PATCH 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
@ 2022-01-05  4:45 ` Siddhesh Poyarekar
  2022-01-05  4:45   ` [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
  2022-01-05  4:45   ` [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  2022-01-10 15:34 ` [PATCH v3 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
                   ` (2 subsequent siblings)
  5 siblings, 2 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  4:45 UTC (permalink / raw)
  To: libc-alpha

The main patch is 2/2, which fixes some fortified headers to match
feature macros with their corresponding main headers, without which
functions would fail to get fortified.  Patch 1/2 is a restructuring of
the tests so that it is easier to add different configurations to
_FORTIFY_SOURCE tests at various levels.

Tested on x86_64 to confirm that there were no new regressions due to
this and that the new tests also run clean.

Changes from v1:
- Rebased against latest trunk so that the patches actually apply.
- Renamed files so that the diffs are easier to compare and also make a
  bit more sense.

Siddhesh Poyarekar (2):
  debug: Refactor and expand _FORTIFY_SOURCE tests
  debug: Synchronize feature guards in fortified functions [BZ #28746]

 debug/Makefile                               | 80 ++++++++++----------
 debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} |  2 +-
 debug/tst-chk-0-cc-nongnu.cc                 |  3 +
 debug/tst-chk-0-cc.cc                        |  1 +
 debug/{tst-lfschk2.c => tst-chk-0-lfs.c}     |  2 +-
 debug/tst-chk-0-nongnu.c                     |  3 +
 debug/{tst-chk1.c => tst-chk-0.c}            | 49 ++++++++++--
 debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} |  2 +-
 debug/tst-chk-1-cc-nongnu.cc                 |  3 +
 debug/{tst-chk5.cc => tst-chk-1-cc.cc}       |  2 +-
 debug/{tst-lfschk4.cc => tst-chk-1-lfs.c}    |  2 +-
 debug/tst-chk-1-nongnu.c                     |  3 +
 debug/{tst-chk2.c => tst-chk-1.c}            |  2 +-
 debug/tst-chk-2-cc-lfs.cc                    |  2 +
 debug/tst-chk-2-cc-nongnu.cc                 |  3 +
 debug/{tst-chk6.cc => tst-chk-2-cc.cc}       |  2 +-
 debug/tst-chk-2-lfs.c                        |  2 +
 debug/tst-chk-2-nongnu.c                     |  3 +
 debug/{tst-chk3.c => tst-chk-2.c}            |  2 +-
 debug/tst-chk-3-cc-lfs.cc                    |  2 +
 debug/tst-chk-3-cc-nongnu.cc                 |  3 +
 debug/{tst-chk7.c => tst-chk-3-cc.cc}        |  2 +-
 debug/tst-chk-3-lfs.c                        |  2 +
 debug/tst-chk-3-nongnu.c                     |  3 +
 debug/{tst-chk8.cc => tst-chk-3.c}           |  2 +-
 debug/tst-chk4.cc                            |  1 -
 debug/tst-lfschk5.cc                         |  2 -
 debug/tst-lfschk6.cc                         |  2 -
 posix/bits/unistd.h                          |  2 +-
 string/bits/string_fortified.h               |  8 +-
 support/xsignal.h                            |  2 +
 wcsmbs/bits/wchar2.h                         |  2 +-
 32 files changed, 131 insertions(+), 70 deletions(-)
 rename debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} (55%)
 create mode 100644 debug/tst-chk-0-cc-nongnu.cc
 create mode 100644 debug/tst-chk-0-cc.cc
 rename debug/{tst-lfschk2.c => tst-chk-0-lfs.c} (55%)
 create mode 100644 debug/tst-chk-0-nongnu.c
 rename debug/{tst-chk1.c => tst-chk-0.c} (97%)
 rename debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} (55%)
 create mode 100644 debug/tst-chk-1-cc-nongnu.cc
 rename debug/{tst-chk5.cc => tst-chk-1-cc.cc} (53%)
 rename debug/{tst-lfschk4.cc => tst-chk-1-lfs.c} (55%)
 create mode 100644 debug/tst-chk-1-nongnu.c
 rename debug/{tst-chk2.c => tst-chk-1.c} (53%)
 create mode 100644 debug/tst-chk-2-cc-lfs.cc
 create mode 100644 debug/tst-chk-2-cc-nongnu.cc
 rename debug/{tst-chk6.cc => tst-chk-2-cc.cc} (53%)
 create mode 100644 debug/tst-chk-2-lfs.c
 create mode 100644 debug/tst-chk-2-nongnu.c
 rename debug/{tst-chk3.c => tst-chk-2.c} (53%)
 create mode 100644 debug/tst-chk-3-cc-lfs.cc
 create mode 100644 debug/tst-chk-3-cc-nongnu.cc
 rename debug/{tst-chk7.c => tst-chk-3-cc.cc} (53%)
 create mode 100644 debug/tst-chk-3-lfs.c
 create mode 100644 debug/tst-chk-3-nongnu.c
 rename debug/{tst-chk8.cc => tst-chk-3.c} (53%)
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests
  2022-01-05  4:45 ` [PATCH v2 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-05  4:45   ` Siddhesh Poyarekar
  2022-01-06 20:51     ` Adhemerval Zanella
  2022-01-05  4:45   ` [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  4:45 UTC (permalink / raw)
  To: libc-alpha

Rename all debug/tst-chk* tests to reflect the fortification level
they're testing and any additional macros so that rules for them can be
autogenerated.  tst-chk0* are without fortification, tst-chk1 for
_FORTIFY_SOURCE=1 and so on.  This allows easier replication of the
tests to check additional macros.

The change also expands the -lfs to include _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                               | 75 +++++++++-----------
 debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} |  2 +-
 debug/tst-chk-0-cc.cc                        |  1 +
 debug/{tst-lfschk2.c => tst-chk-0-lfs.c}     |  2 +-
 debug/{tst-chk1.c => tst-chk-0.c}            |  0
 debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} |  2 +-
 debug/{tst-chk2.c => tst-chk-1-cc.cc}        |  2 +-
 debug/{tst-lfschk4.cc => tst-chk-1-lfs.c}    |  2 +-
 debug/{tst-chk5.cc => tst-chk-1.c}           |  2 +-
 debug/tst-chk-2-cc-lfs.cc                    |  2 +
 debug/{tst-chk6.cc => tst-chk-2-cc.cc}       |  2 +-
 debug/tst-chk-2-lfs.c                        |  2 +
 debug/{tst-chk3.c => tst-chk-2.c}            |  2 +-
 debug/tst-chk-3-cc-lfs.cc                    |  2 +
 debug/{tst-chk7.c => tst-chk-3-cc.cc}        |  2 +-
 debug/tst-chk-3-lfs.c                        |  2 +
 debug/{tst-chk8.cc => tst-chk-3.c}           |  2 +-
 debug/tst-chk4.cc                            |  1 -
 debug/tst-lfschk5.cc                         |  2 -
 debug/tst-lfschk6.cc                         |  2 -
 20 files changed, 52 insertions(+), 57 deletions(-)
 rename debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} (55%)
 create mode 100644 debug/tst-chk-0-cc.cc
 rename debug/{tst-lfschk2.c => tst-chk-0-lfs.c} (55%)
 rename debug/{tst-chk1.c => tst-chk-0.c} (100%)
 rename debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} (55%)
 rename debug/{tst-chk2.c => tst-chk-1-cc.cc} (53%)
 rename debug/{tst-lfschk4.cc => tst-chk-1-lfs.c} (55%)
 rename debug/{tst-chk5.cc => tst-chk-1.c} (53%)
 create mode 100644 debug/tst-chk-2-cc-lfs.cc
 rename debug/{tst-chk6.cc => tst-chk-2-cc.cc} (53%)
 create mode 100644 debug/tst-chk-2-lfs.c
 rename debug/{tst-chk3.c => tst-chk-2.c} (53%)
 create mode 100644 debug/tst-chk-3-cc-lfs.cc
 rename debug/{tst-chk7.c => tst-chk-3-cc.cc} (53%)
 create mode 100644 debug/tst-chk-3-lfs.c
 rename debug/{tst-chk8.cc => tst-chk-3.c} (53%)
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

diff --git a/debug/Makefile b/debug/Makefile
index 9c2ce61a86..9aa27eb00c 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -1,4 +1,5 @@
 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -110,32 +111,31 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
 
+# _FORTIFY_SOURCE tests.
+tests-chk = $(addprefix tst-chk-, 0 1 2 3)
+tests-chk-cc = $(addsuffix -cc, $(tests-chk))
+tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
+tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
 # And they also generate warnings from warning attributes, which
 # cannot be disabled via pragmas, so require -Wno-error to be used.
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-LDLIBS-tst-chk4 = -lstdc++
-LDLIBS-tst-chk5 = -lstdc++
-LDLIBS-tst-chk6 = -lstdc++
-LDLIBS-tst-chk8 = -lstdc++
-LDLIBS-tst-lfschk4 = -lstdc++
-LDLIBS-tst-lfschk5 = -lstdc++
-LDLIBS-tst-lfschk6 = -lstdc++
+define disable-warnings
+CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
+endef
+
+$(foreach t,$(tests-chk) $(tests-chk-lfs), \
+	  $(eval $(call disable-warnings,$(t),c)))
+
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
+	  $(eval $(call disable-warnings,$(t),cc)))
+
+define link-cc
+LDLIBS-$(1) = -lstdc++
+endef
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -152,19 +152,18 @@ LDFLAGS-tst-backtrace6 = -rdynamic
 
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
+tests = backtrace-tst tst-longjmp_chk \
+	test-strcpy_chk test-stpcpy_chk \
+	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
+	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
+	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
+tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
 endif
 
 extra-libs = libSegFault libpcprofile
@@ -191,20 +190,12 @@ ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
 
-$(objpfx)tst-chk1.out: $(gen-locales)
-$(objpfx)tst-chk2.out: $(gen-locales)
-$(objpfx)tst-chk3.out: $(gen-locales)
-$(objpfx)tst-chk4.out: $(gen-locales)
-$(objpfx)tst-chk5.out: $(gen-locales)
-$(objpfx)tst-chk6.out: $(gen-locales)
-$(objpfx)tst-chk7.out: $(gen-locales)
-$(objpfx)tst-chk8.out: $(gen-locales)
-$(objpfx)tst-lfschk1.out: $(gen-locales)
-$(objpfx)tst-lfschk2.out: $(gen-locales)
-$(objpfx)tst-lfschk3.out: $(gen-locales)
-$(objpfx)tst-lfschk4.out: $(gen-locales)
-$(objpfx)tst-lfschk5.out: $(gen-locales)
-$(objpfx)tst-lfschk6.out: $(gen-locales)
+define chk-gen-locales
+$(objpfx)$(1).out: $(gen-locales)
+endef
+$(foreach t, \
+	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
+	  $(eval $(call link-cc,$(t))))
 endif
 
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
diff --git a/debug/tst-lfschk1.c b/debug/tst-chk-0-cc-lfs.cc
similarity index 55%
rename from debug/tst-lfschk1.c
rename to debug/tst-chk-0-cc-lfs.cc
index f3e6d47d5e..db699b2391 100644
--- a/debug/tst-lfschk1.c
+++ b/debug/tst-chk-0-cc-lfs.cc
@@ -1,2 +1,2 @@
 #define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-0-cc.cc b/debug/tst-chk-0-cc.cc
new file mode 100644
index 0000000000..be76fc69d4
--- /dev/null
+++ b/debug/tst-chk-0-cc.cc
@@ -0,0 +1 @@
+#include "tst-chk-0.c"
diff --git a/debug/tst-lfschk2.c b/debug/tst-chk-0-lfs.c
similarity index 55%
rename from debug/tst-lfschk2.c
rename to debug/tst-chk-0-lfs.c
index 95d4db1d32..db699b2391 100644
--- a/debug/tst-lfschk2.c
+++ b/debug/tst-chk-0-lfs.c
@@ -1,2 +1,2 @@
 #define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk1.c b/debug/tst-chk-0.c
similarity index 100%
rename from debug/tst-chk1.c
rename to debug/tst-chk-0.c
diff --git a/debug/tst-lfschk3.c b/debug/tst-chk-1-cc-lfs.cc
similarity index 55%
rename from debug/tst-lfschk3.c
rename to debug/tst-chk-1-cc-lfs.cc
index 50a1ae1258..0d67643e9e 100644
--- a/debug/tst-lfschk3.c
+++ b/debug/tst-chk-1-cc-lfs.cc
@@ -1,2 +1,2 @@
 #define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
+#include "tst-chk-1.c"
diff --git a/debug/tst-chk2.c b/debug/tst-chk-1-cc.cc
similarity index 53%
rename from debug/tst-chk2.c
rename to debug/tst-chk-1-cc.cc
index be37ce2d22..945c1f8d5b 100644
--- a/debug/tst-chk2.c
+++ b/debug/tst-chk-1-cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-lfschk4.cc b/debug/tst-chk-1-lfs.c
similarity index 55%
rename from debug/tst-lfschk4.cc
rename to debug/tst-chk-1-lfs.c
index f3e6d47d5e..0d67643e9e 100644
--- a/debug/tst-lfschk4.cc
+++ b/debug/tst-chk-1-lfs.c
@@ -1,2 +1,2 @@
 #define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
+#include "tst-chk-1.c"
diff --git a/debug/tst-chk5.cc b/debug/tst-chk-1.c
similarity index 53%
rename from debug/tst-chk5.cc
rename to debug/tst-chk-1.c
index be37ce2d22..945c1f8d5b 100644
--- a/debug/tst-chk5.cc
+++ b/debug/tst-chk-1.c
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-2-cc-lfs.cc b/debug/tst-chk-2-cc-lfs.cc
new file mode 100644
index 0000000000..97c83def5c
--- /dev/null
+++ b/debug/tst-chk-2-cc-lfs.cc
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk-2.c"
diff --git a/debug/tst-chk6.cc b/debug/tst-chk-2-cc.cc
similarity index 53%
rename from debug/tst-chk6.cc
rename to debug/tst-chk-2-cc.cc
index 38b8e4fb36..f178340bbd 100644
--- a/debug/tst-chk6.cc
+++ b/debug/tst-chk-2-cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-2-lfs.c b/debug/tst-chk-2-lfs.c
new file mode 100644
index 0000000000..97c83def5c
--- /dev/null
+++ b/debug/tst-chk-2-lfs.c
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk-2.c"
diff --git a/debug/tst-chk3.c b/debug/tst-chk-2.c
similarity index 53%
rename from debug/tst-chk3.c
rename to debug/tst-chk-2.c
index 38b8e4fb36..f178340bbd 100644
--- a/debug/tst-chk3.c
+++ b/debug/tst-chk-2.c
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-3-cc-lfs.cc b/debug/tst-chk-3-cc-lfs.cc
new file mode 100644
index 0000000000..d5d8c28bc7
--- /dev/null
+++ b/debug/tst-chk-3-cc-lfs.cc
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk-3.c"
diff --git a/debug/tst-chk7.c b/debug/tst-chk-3-cc.cc
similarity index 53%
rename from debug/tst-chk7.c
rename to debug/tst-chk-3-cc.cc
index 2a7b323812..c1ddac1261 100644
--- a/debug/tst-chk7.c
+++ b/debug/tst-chk-3-cc.cc
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-3-lfs.c b/debug/tst-chk-3-lfs.c
new file mode 100644
index 0000000000..d5d8c28bc7
--- /dev/null
+++ b/debug/tst-chk-3-lfs.c
@@ -0,0 +1,2 @@
+#define _FILE_OFFSET_BITS 64
+#include "tst-chk-3.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk-3.c
similarity index 53%
rename from debug/tst-chk8.cc
rename to debug/tst-chk-3.c
index 2a7b323812..c1ddac1261 100644
--- a/debug/tst-chk8.cc
+++ b/debug/tst-chk-3.c
@@ -1,2 +1,2 @@
 #define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
deleted file mode 100644
index c82e6aac86..0000000000
--- a/debug/tst-chk4.cc
+++ /dev/null
@@ -1 +0,0 @@
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-05  4:45 ` [PATCH v2 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  2022-01-05  4:45   ` [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-05  4:45   ` Siddhesh Poyarekar
  2022-01-06 21:15     ` Adhemerval Zanella
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-05  4:45 UTC (permalink / raw)
  To: libc-alpha

Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
main headers as they got incorporated into the standard, but their
fortified variants remained under __USE_GNU.  As a result, these
functions did not get fortified when _GNU_SOURCE was not defined.

Add test wrappers that check all functions tested in tst-chk0 at all
levels with _GNU_SOURCE undefined and then use the failures to (1)
exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
feature macro guards in the fortified function headers so that they're
the same as the ones in the main headers.

This fixes BZ #28746.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                 | 17 +++++++-----
 debug/tst-chk-0-cc-nongnu.cc   |  3 +++
 debug/tst-chk-0-nongnu.c       |  3 +++
 debug/tst-chk-0.c              | 49 ++++++++++++++++++++++++++++------
 debug/tst-chk-1-cc-nongnu.cc   |  3 +++
 debug/tst-chk-1-nongnu.c       |  3 +++
 debug/tst-chk-2-cc-nongnu.cc   |  3 +++
 debug/tst-chk-2-nongnu.c       |  3 +++
 debug/tst-chk-3-cc-nongnu.cc   |  3 +++
 debug/tst-chk-3-nongnu.c       |  3 +++
 posix/bits/unistd.h            |  2 +-
 string/bits/string_fortified.h |  8 +++---
 support/xsignal.h              |  2 ++
 wcsmbs/bits/wchar2.h           |  2 +-
 14 files changed, 85 insertions(+), 19 deletions(-)
 create mode 100644 debug/tst-chk-0-cc-nongnu.cc
 create mode 100644 debug/tst-chk-0-nongnu.c
 create mode 100644 debug/tst-chk-1-cc-nongnu.cc
 create mode 100644 debug/tst-chk-1-nongnu.c
 create mode 100644 debug/tst-chk-2-cc-nongnu.cc
 create mode 100644 debug/tst-chk-2-nongnu.c
 create mode 100644 debug/tst-chk-3-cc-nongnu.cc
 create mode 100644 debug/tst-chk-3-nongnu.c

diff --git a/debug/Makefile b/debug/Makefile
index 9aa27eb00c..876d10e521 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -116,6 +116,8 @@ tests-chk = $(addprefix tst-chk-, 0 1 2 3)
 tests-chk-cc = $(addsuffix -cc, $(tests-chk))
 tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
 tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
+tests-chk-nongnu = $(addsuffix -nongnu, $(tests-chk))
+tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
 
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
@@ -126,16 +128,17 @@ define disable-warnings
 CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
 endef
 
-$(foreach t,$(tests-chk) $(tests-chk-lfs), \
+$(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
 	  $(eval $(call disable-warnings,$(t),c)))
 
-$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
 	  $(eval $(call disable-warnings,$(t),cc)))
 
 define link-cc
 LDLIBS-$(1) = -lstdc++
 endef
-$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
+$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
+	  $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -156,14 +159,15 @@ tests = backtrace-tst tst-longjmp_chk \
 	test-strcpy_chk test-stpcpy_chk \
 	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
 	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
-	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
+	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
+	$(tests-chk-nongnu) $(tests-chk-cc-nongnu)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
+tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu)
 endif
 
 extra-libs = libSegFault libpcprofile
@@ -194,7 +198,8 @@ define chk-gen-locales
 $(objpfx)$(1).out: $(gen-locales)
 endef
 $(foreach t, \
-	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
+	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
+	  $(tests-chk-nongnu) $(tests-chk-cc-nongnu), \
 	  $(eval $(call link-cc,$(t))))
 endif
 
diff --git a/debug/tst-chk-0-cc-nongnu.cc b/debug/tst-chk-0-cc-nongnu.cc
new file mode 100644
index 0000000000..24b1306e16
--- /dev/null
+++ b/debug/tst-chk-0-cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-0-nongnu.c b/debug/tst-chk-0-nongnu.c
new file mode 100644
index 0000000000..24b1306e16
--- /dev/null
+++ b/debug/tst-chk-0-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-0.c"
diff --git a/debug/tst-chk-0.c b/debug/tst-chk-0.c
index 9288610fe6..d65a2fe6e1 100644
--- a/debug/tst-chk-0.c
+++ b/debug/tst-chk-0.c
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -36,6 +37,17 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#ifndef _GNU_SOURCE
+# define MEMPCPY memcpy
+# define WMEMPCPY wmemcpy
+# define MEMPCPY_RET(x) 0
+# define WMEMPCPY_RET(x) 0
+#else
+# define MEMPCPY mempcpy
+# define WMEMPCPY wmempcpy
+# define MEMPCPY_RET(x) __builtin_strlen (x)
+# define WMEMPCPY_RET(x) wcslen (x)
+#endif
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
@@ -162,7 +174,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -207,7 +219,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -266,7 +278,8 @@ do_test (void)
   if (memcmp (a.buf1, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
       || memcmp (a.buf1, "aabcdabcde", 10))
     FAIL ();
 
@@ -347,6 +360,7 @@ do_test (void)
   bcopy (buf + 1, buf + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", 5);
   CHK_FAIL_END
@@ -354,6 +368,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (buf + 9, 'j', 2);
@@ -464,6 +479,7 @@ do_test (void)
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
   CHK_FAIL_END
@@ -471,6 +487,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (a.buf1 + 9, 'j', 2);
@@ -550,7 +567,7 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -583,7 +600,8 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -625,7 +643,8 @@ do_test (void)
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
     FAIL ();
 
@@ -694,6 +713,7 @@ do_test (void)
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -701,6 +721,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wbuf + 9, L'j', 2);
@@ -768,6 +789,7 @@ do_test (void)
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -775,6 +797,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wa.buf1 + 9, L'j', 2);
@@ -906,6 +929,7 @@ do_test (void)
   if (fprintf (fp, buf2 + 4, str5) != 7)
     FAIL ();
 
+#ifdef _GNU_SOURCE
   char *my_ptr = NULL;
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
@@ -935,6 +959,7 @@ do_test (void)
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
     FAIL ();
   obstack_free (&obs, NULL);
+#endif
 
   if (freopen (temp_filename, "r", stdin) == NULL)
     {
@@ -982,6 +1007,7 @@ do_test (void)
 
   rewind (stdin);
 
+#ifdef _GNU_SOURCE
   if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
@@ -1008,6 +1034,7 @@ do_test (void)
 #endif
 
   rewind (stdin);
+#endif
 
   if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
@@ -1578,7 +1605,10 @@ do_test (void)
       ret = 1;
     }
 
-  int fd = posix_openpt (O_RDWR);
+  int fd;
+
+#ifdef _GNU_SOURCE
+  fd = posix_openpt (O_RDWR);
   if (fd != -1)
     {
       char enough[1000];
@@ -1594,6 +1624,7 @@ do_test (void)
 #endif
       close (fd);
     }
+#endif
 
 #if PATH_MAX > 0
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
@@ -1711,8 +1742,9 @@ do_test (void)
   poll (fds, l0 + 2, 0);
   CHK_FAIL_END
 #endif
+#ifdef _GNU_SOURCE
   ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
+# if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
   ppoll (fds, 2, NULL, NULL);
   CHK_FAIL_END
@@ -1720,6 +1752,7 @@ do_test (void)
   CHK_FAIL_START
   ppoll (fds, l0 + 2, NULL, NULL);
   CHK_FAIL_END
+# endif
 #endif
 
   return ret;
diff --git a/debug/tst-chk-1-cc-nongnu.cc b/debug/tst-chk-1-cc-nongnu.cc
new file mode 100644
index 0000000000..cce36ff617
--- /dev/null
+++ b/debug/tst-chk-1-cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-1.c"
diff --git a/debug/tst-chk-1-nongnu.c b/debug/tst-chk-1-nongnu.c
new file mode 100644
index 0000000000..cce36ff617
--- /dev/null
+++ b/debug/tst-chk-1-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-1.c"
diff --git a/debug/tst-chk-2-cc-nongnu.cc b/debug/tst-chk-2-cc-nongnu.cc
new file mode 100644
index 0000000000..149af283c4
--- /dev/null
+++ b/debug/tst-chk-2-cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-2.c"
diff --git a/debug/tst-chk-2-nongnu.c b/debug/tst-chk-2-nongnu.c
new file mode 100644
index 0000000000..149af283c4
--- /dev/null
+++ b/debug/tst-chk-2-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-2.c"
diff --git a/debug/tst-chk-3-cc-nongnu.cc b/debug/tst-chk-3-cc-nongnu.cc
new file mode 100644
index 0000000000..a306b3290c
--- /dev/null
+++ b/debug/tst-chk-3-cc-nongnu.cc
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-3.c"
diff --git a/debug/tst-chk-3-nongnu.c b/debug/tst-chk-3-nongnu.c
new file mode 100644
index 0000000000..a306b3290c
--- /dev/null
+++ b/debug/tst-chk-3-nongnu.c
@@ -0,0 +1,3 @@
+#undef _GNU_SOURCE
+#define _LARGEFILE64_SOURCE 1
+#include "tst-chk-3.c"
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 01df84b5f9..f53a7a06eb 100644
--- a/posix/bits/unistd.h
+++ b/posix/bits/unistd.h
@@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
 			  __fd, __buf, __nbytes);
 }
 
-#ifdef __USE_UNIX98
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
 			    __off_t __offset, size_t __bufsize)
   __wur __attr_access ((__write_only__, 2, 3));
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 51ad480401..d0188b5ba8 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
 }
 
-#ifdef __USE_GNU
+#ifdef __USE_XOPEN2K8
 __fortify_function char *
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
 {
@@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
 				  __glibc_objsize (__dest));
 }
 
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+#if __USE_XOPEN2K8
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
   return __builtin___stpncpy_chk (__dest, __src, __n,
 				  __glibc_objsize (__dest));
 }
-#else
+# else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __fortified_attr_access ((__write_only__, 1, 3))
@@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+# endif
 #endif
 
 
diff --git a/support/xsignal.h b/support/xsignal.h
index 973f532495..d868bb336c 100644
--- a/support/xsignal.h
+++ b/support/xsignal.h
@@ -28,7 +28,9 @@ __BEGIN_DECLS
    terminate the process on error.  */
 
 void xraise (int sig);
+#ifdef _GNU_SOURCE
 sighandler_t xsignal (int sig, sighandler_t handler);
+#endif
 void xsigaction (int sig, const struct sigaction *newact,
                  struct sigaction *oldact);
 
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index 25d06d433f..0e017f458b 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 }
 
 
-#ifdef __USE_GNU
+#ifdef	__USE_XOPEN2K8
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
 				const char **__restrict __src, size_t __nmc,
 				size_t __len, mbstate_t *__restrict __ps,
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests
  2022-01-05  4:45   ` [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-06 20:51     ` Adhemerval Zanella
  2022-01-07  2:09       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-06 20:51 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
> Rename all debug/tst-chk* tests to reflect the fortification level
> they're testing and any additional macros so that rules for them can be
> autogenerated.  tst-chk0* are without fortification, tst-chk1 for
> _FORTIFY_SOURCE=1 and so on.  This allows easier replication of the
> tests to check additional macros.
> 
> The change also expands the -lfs to include _FORTIFY_SOURCE=3.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Looks good in general, some comments below.

> ---
>  debug/Makefile                               | 75 +++++++++-----------
>  debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} |  2 +-
>  debug/tst-chk-0-cc.cc                        |  1 +
>  debug/{tst-lfschk2.c => tst-chk-0-lfs.c}     |  2 +-
>  debug/{tst-chk1.c => tst-chk-0.c}            |  0
>  debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} |  2 +-
>  debug/{tst-chk2.c => tst-chk-1-cc.cc}        |  2 +-
>  debug/{tst-lfschk4.cc => tst-chk-1-lfs.c}    |  2 +-
>  debug/{tst-chk5.cc => tst-chk-1.c}           |  2 +-
>  debug/tst-chk-2-cc-lfs.cc                    |  2 +
>  debug/{tst-chk6.cc => tst-chk-2-cc.cc}       |  2 +-
>  debug/tst-chk-2-lfs.c                        |  2 +
>  debug/{tst-chk3.c => tst-chk-2.c}            |  2 +-
>  debug/tst-chk-3-cc-lfs.cc                    |  2 +
>  debug/{tst-chk7.c => tst-chk-3-cc.cc}        |  2 +-
>  debug/tst-chk-3-lfs.c                        |  2 +
>  debug/{tst-chk8.cc => tst-chk-3.c}           |  2 +-
>  debug/tst-chk4.cc                            |  1 -
>  debug/tst-lfschk5.cc                         |  2 -
>  debug/tst-lfschk6.cc                         |  2 -
>  20 files changed, 52 insertions(+), 57 deletions(-)
>  rename debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} (55%)
>  create mode 100644 debug/tst-chk-0-cc.cc
>  rename debug/{tst-lfschk2.c => tst-chk-0-lfs.c} (55%)
>  rename debug/{tst-chk1.c => tst-chk-0.c} (100%)
>  rename debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} (55%)
>  rename debug/{tst-chk2.c => tst-chk-1-cc.cc} (53%)
>  rename debug/{tst-lfschk4.cc => tst-chk-1-lfs.c} (55%)
>  rename debug/{tst-chk5.cc => tst-chk-1.c} (53%)
>  create mode 100644 debug/tst-chk-2-cc-lfs.cc
>  rename debug/{tst-chk6.cc => tst-chk-2-cc.cc} (53%)
>  create mode 100644 debug/tst-chk-2-lfs.c
>  rename debug/{tst-chk3.c => tst-chk-2.c} (53%)
>  create mode 100644 debug/tst-chk-3-cc-lfs.cc
>  rename debug/{tst-chk7.c => tst-chk-3-cc.cc} (53%)
>  create mode 100644 debug/tst-chk-3-lfs.c
>  rename debug/{tst-chk8.cc => tst-chk-3.c} (53%)
>  delete mode 100644 debug/tst-chk4.cc
>  delete mode 100644 debug/tst-lfschk5.cc
>  delete mode 100644 debug/tst-lfschk6.cc
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index 9c2ce61a86..9aa27eb00c 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -1,4 +1,5 @@
>  # Copyright (C) 1998-2022 Free Software Foundation, Inc.
> +# Copyright The GNU Toolchain Authors.
>  # This file is part of the GNU C Library.
>  
>  # The GNU C Library is free software; you can redistribute it and/or
> @@ -110,32 +111,31 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
>  CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
>  CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
>  
> +# _FORTIFY_SOURCE tests.
> +tests-chk = $(addprefix tst-chk-, 0 1 2 3)
> +tests-chk-cc = $(addsuffix -cc, $(tests-chk))
> +tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
> +tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
> +
>  # We know these tests have problems with format strings, this is what
>  # we are testing.  Disable that warning.  They are also testing
>  # deprecated functions (notably gets) so disable that warning as well.
>  # And they also generate warnings from warning attributes, which
>  # cannot be disabled via pragmas, so require -Wno-error to be used.
> -CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -LDLIBS-tst-chk4 = -lstdc++
> -LDLIBS-tst-chk5 = -lstdc++
> -LDLIBS-tst-chk6 = -lstdc++
> -LDLIBS-tst-chk8 = -lstdc++
> -LDLIBS-tst-lfschk4 = -lstdc++
> -LDLIBS-tst-lfschk5 = -lstdc++
> -LDLIBS-tst-lfschk6 = -lstdc++
> +define disable-warnings
> +CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
> +endef
> +
> +$(foreach t,$(tests-chk) $(tests-chk-lfs), \
> +	  $(eval $(call disable-warnings,$(t),c)))
> +
> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
> +	  $(eval $(call disable-warnings,$(t),cc)))
> +
> +define link-cc
> +LDLIBS-$(1) = -lstdc++
> +endef
> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
>  
>  # backtrace_symbols only works if we link with -rdynamic.  backtrace
>  # requires unwind tables on most architectures.
> @@ -152,19 +152,18 @@ LDFLAGS-tst-backtrace6 = -rdynamic
>  
>  CFLAGS-tst-ssp-1.c += -fstack-protector-all
>  

Nice, it does simplify the required rules.  Maybe you could also extend it to
LFS as well:

diff --git a/debug/Makefile b/debug/Makefile
index dc73600213..019046391b 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -127,6 +127,9 @@ tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
 define disable-warnings
 CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
 endef
+define enable-lfs
+CFLAGS-$(1).$(2) += -D_FILE_OFFSET_BITS=64
+endef
 
 $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
 	  $(eval $(call disable-warnings,$(t),c)))
@@ -134,6 +137,9 @@ $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
 $(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
 	  $(eval $(call disable-warnings,$(t),cc)))
 
+$(foreach t,$(tests-chk-lfs), $(eval $(call enable-lfs,$(t),c)))
+$(foreach t,$(tests-chk-cc-lfs), $(eval $(call enable-lfs,$(t),cc)))
+
 define link-cc
 LDLIBS-$(1) = -lstdc++
 endef

And then remove the '#define _FILE_OFFSET_BITS 64' so the rule is applied
regardless whether the file defines or not.

> -tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
> -	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
> -	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
> -	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
> -	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
> +tests = backtrace-tst tst-longjmp_chk \
> +	test-strcpy_chk test-stpcpy_chk \
> +	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
> +	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
> +	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)

Since you are touching, put one file per line.

>  
>  ifeq ($(have-ssp),yes)
>  tests += tst-ssp-1
>  endif
>  
>  ifeq (,$(CXX))
> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
> -		    tst-lfschk4 tst-lfschk5 tst-lfschk6
> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)

Same.

>  endif
>  
>  extra-libs = libSegFault libpcprofile
> @@ -191,20 +190,12 @@ ifeq ($(run-built-tests),yes)
>  LOCALES := de_DE.UTF-8
>  include ../gen-locales.mk
>  
> -$(objpfx)tst-chk1.out: $(gen-locales)
> -$(objpfx)tst-chk2.out: $(gen-locales)
> -$(objpfx)tst-chk3.out: $(gen-locales)
> -$(objpfx)tst-chk4.out: $(gen-locales)
> -$(objpfx)tst-chk5.out: $(gen-locales)
> -$(objpfx)tst-chk6.out: $(gen-locales)
> -$(objpfx)tst-chk7.out: $(gen-locales)
> -$(objpfx)tst-chk8.out: $(gen-locales)
> -$(objpfx)tst-lfschk1.out: $(gen-locales)
> -$(objpfx)tst-lfschk2.out: $(gen-locales)
> -$(objpfx)tst-lfschk3.out: $(gen-locales)
> -$(objpfx)tst-lfschk4.out: $(gen-locales)
> -$(objpfx)tst-lfschk5.out: $(gen-locales)
> -$(objpfx)tst-lfschk6.out: $(gen-locales)
> +define chk-gen-locales
> +$(objpfx)$(1).out: $(gen-locales)
> +endef
> +$(foreach t, \
> +	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
> +	  $(eval $(call link-cc,$(t))))
>  endif
>  
>  sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')

Same.

> diff --git a/debug/tst-lfschk1.c b/debug/tst-chk-0-cc-lfs.cc
> similarity index 55%
> rename from debug/tst-lfschk1.c
> rename to debug/tst-chk-0-cc-lfs.cc
> index f3e6d47d5e..db699b2391 100644
> --- a/debug/tst-lfschk1.c
> +++ b/debug/tst-chk-0-cc-lfs.cc
> @@ -1,2 +1,2 @@
>  #define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-0-cc.cc b/debug/tst-chk-0-cc.cc
> new file mode 100644
> index 0000000000..be76fc69d4
> --- /dev/null
> +++ b/debug/tst-chk-0-cc.cc
> @@ -0,0 +1 @@
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-lfschk2.c b/debug/tst-chk-0-lfs.c
> similarity index 55%
> rename from debug/tst-lfschk2.c
> rename to debug/tst-chk-0-lfs.c
> index 95d4db1d32..db699b2391 100644
> --- a/debug/tst-lfschk2.c
> +++ b/debug/tst-chk-0-lfs.c
> @@ -1,2 +1,2 @@
>  #define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk1.c b/debug/tst-chk-0.c
> similarity index 100%
> rename from debug/tst-chk1.c
> rename to debug/tst-chk-0.c
> diff --git a/debug/tst-lfschk3.c b/debug/tst-chk-1-cc-lfs.cc
> similarity index 55%
> rename from debug/tst-lfschk3.c
> rename to debug/tst-chk-1-cc-lfs.cc
> index 50a1ae1258..0d67643e9e 100644
> --- a/debug/tst-lfschk3.c
> +++ b/debug/tst-chk-1-cc-lfs.cc
> @@ -1,2 +1,2 @@
>  #define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"
> +#include "tst-chk-1.c"
> diff --git a/debug/tst-chk2.c b/debug/tst-chk-1-cc.cc
> similarity index 53%
> rename from debug/tst-chk2.c
> rename to debug/tst-chk-1-cc.cc
> index be37ce2d22..945c1f8d5b 100644
> --- a/debug/tst-chk2.c
> +++ b/debug/tst-chk-1-cc.cc
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-lfschk4.cc b/debug/tst-chk-1-lfs.c
> similarity index 55%
> rename from debug/tst-lfschk4.cc
> rename to debug/tst-chk-1-lfs.c
> index f3e6d47d5e..0d67643e9e 100644
> --- a/debug/tst-lfschk4.cc
> +++ b/debug/tst-chk-1-lfs.c
> @@ -1,2 +1,2 @@
>  #define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> +#include "tst-chk-1.c"
> diff --git a/debug/tst-chk5.cc b/debug/tst-chk-1.c
> similarity index 53%
> rename from debug/tst-chk5.cc
> rename to debug/tst-chk-1.c
> index be37ce2d22..945c1f8d5b 100644
> --- a/debug/tst-chk5.cc
> +++ b/debug/tst-chk-1.c
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-2-cc-lfs.cc b/debug/tst-chk-2-cc-lfs.cc
> new file mode 100644
> index 0000000000..97c83def5c
> --- /dev/null
> +++ b/debug/tst-chk-2-cc-lfs.cc
> @@ -0,0 +1,2 @@
> +#define _FILE_OFFSET_BITS 64
> +#include "tst-chk-2.c"
> diff --git a/debug/tst-chk6.cc b/debug/tst-chk-2-cc.cc
> similarity index 53%
> rename from debug/tst-chk6.cc
> rename to debug/tst-chk-2-cc.cc
> index 38b8e4fb36..f178340bbd 100644
> --- a/debug/tst-chk6.cc
> +++ b/debug/tst-chk-2-cc.cc
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-2-lfs.c b/debug/tst-chk-2-lfs.c
> new file mode 100644
> index 0000000000..97c83def5c
> --- /dev/null
> +++ b/debug/tst-chk-2-lfs.c
> @@ -0,0 +1,2 @@
> +#define _FILE_OFFSET_BITS 64
> +#include "tst-chk-2.c"
> diff --git a/debug/tst-chk3.c b/debug/tst-chk-2.c
> similarity index 53%
> rename from debug/tst-chk3.c
> rename to debug/tst-chk-2.c
> index 38b8e4fb36..f178340bbd 100644
> --- a/debug/tst-chk3.c
> +++ b/debug/tst-chk-2.c
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-3-cc-lfs.cc b/debug/tst-chk-3-cc-lfs.cc
> new file mode 100644
> index 0000000000..d5d8c28bc7
> --- /dev/null
> +++ b/debug/tst-chk-3-cc-lfs.cc
> @@ -0,0 +1,2 @@
> +#define _FILE_OFFSET_BITS 64
> +#include "tst-chk-3.c"
> diff --git a/debug/tst-chk7.c b/debug/tst-chk-3-cc.cc
> similarity index 53%
> rename from debug/tst-chk7.c
> rename to debug/tst-chk-3-cc.cc
> index 2a7b323812..c1ddac1261 100644
> --- a/debug/tst-chk7.c
> +++ b/debug/tst-chk-3-cc.cc
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-3-lfs.c b/debug/tst-chk-3-lfs.c
> new file mode 100644
> index 0000000000..d5d8c28bc7
> --- /dev/null
> +++ b/debug/tst-chk-3-lfs.c
> @@ -0,0 +1,2 @@
> +#define _FILE_OFFSET_BITS 64
> +#include "tst-chk-3.c"
> diff --git a/debug/tst-chk8.cc b/debug/tst-chk-3.c
> similarity index 53%
> rename from debug/tst-chk8.cc
> rename to debug/tst-chk-3.c
> index 2a7b323812..c1ddac1261 100644
> --- a/debug/tst-chk8.cc
> +++ b/debug/tst-chk-3.c
> @@ -1,2 +1,2 @@
>  #define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
> deleted file mode 100644
> index c82e6aac86..0000000000
> --- a/debug/tst-chk4.cc
> +++ /dev/null
> @@ -1 +0,0 @@
> -#include "tst-chk1.c"
> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
> deleted file mode 100644
> index 95d4db1d32..0000000000
> --- a/debug/tst-lfschk5.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
> deleted file mode 100644
> index 50a1ae1258..0000000000
> --- a/debug/tst-lfschk6.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-05  4:45   ` [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
@ 2022-01-06 21:15     ` Adhemerval Zanella
  2022-01-07  2:12       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-06 21:15 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
> Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
> main headers as they got incorporated into the standard, but their
> fortified variants remained under __USE_GNU.  As a result, these
> functions did not get fortified when _GNU_SOURCE was not defined.
> 
> Add test wrappers that check all functions tested in tst-chk0 at all
> levels with _GNU_SOURCE undefined and then use the failures to (1)
> exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
> feature macro guards in the fortified function headers so that they're
> the same as the ones in the main headers.
> 
> This fixes BZ #28746.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Patch look ok in general, some comments below.

> ---
>  debug/Makefile                 | 17 +++++++-----
>  debug/tst-chk-0-cc-nongnu.cc   |  3 +++
>  debug/tst-chk-0-nongnu.c       |  3 +++
>  debug/tst-chk-0.c              | 49 ++++++++++++++++++++++++++++------
>  debug/tst-chk-1-cc-nongnu.cc   |  3 +++
>  debug/tst-chk-1-nongnu.c       |  3 +++
>  debug/tst-chk-2-cc-nongnu.cc   |  3 +++
>  debug/tst-chk-2-nongnu.c       |  3 +++
>  debug/tst-chk-3-cc-nongnu.cc   |  3 +++
>  debug/tst-chk-3-nongnu.c       |  3 +++
>  posix/bits/unistd.h            |  2 +-
>  string/bits/string_fortified.h |  8 +++---
>  support/xsignal.h              |  2 ++
>  wcsmbs/bits/wchar2.h           |  2 +-
>  14 files changed, 85 insertions(+), 19 deletions(-)
>  create mode 100644 debug/tst-chk-0-cc-nongnu.cc
>  create mode 100644 debug/tst-chk-0-nongnu.c
>  create mode 100644 debug/tst-chk-1-cc-nongnu.cc
>  create mode 100644 debug/tst-chk-1-nongnu.c
>  create mode 100644 debug/tst-chk-2-cc-nongnu.cc
>  create mode 100644 debug/tst-chk-2-nongnu.c
>  create mode 100644 debug/tst-chk-3-cc-nongnu.cc
>  create mode 100644 debug/tst-chk-3-nongnu.c
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index 9aa27eb00c..876d10e521 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -116,6 +116,8 @@ tests-chk = $(addprefix tst-chk-, 0 1 2 3)
>  tests-chk-cc = $(addsuffix -cc, $(tests-chk))
>  tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
>  tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
> +tests-chk-nongnu = $(addsuffix -nongnu, $(tests-chk))
> +tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
>  
>  # We know these tests have problems with format strings, this is what
>  # we are testing.  Disable that warning.  They are also testing
> @@ -126,16 +128,17 @@ define disable-warnings
>  CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>  endef
>  
> -$(foreach t,$(tests-chk) $(tests-chk-lfs), \
> +$(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>  	  $(eval $(call disable-warnings,$(t),c)))
>  
> -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>  	  $(eval $(call disable-warnings,$(t),cc)))
>  
>  define link-cc
>  LDLIBS-$(1) = -lstdc++
>  endef
> -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
> +	  $(eval $(call link-cc,$(t))))
>  
>  # backtrace_symbols only works if we link with -rdynamic.  backtrace
>  # requires unwind tables on most architectures.

Ok.

> @@ -156,14 +159,15 @@ tests = backtrace-tst tst-longjmp_chk \
>  	test-strcpy_chk test-stpcpy_chk \
>  	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>  	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
> -	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
> +	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
> +	$(tests-chk-nongnu) $(tests-chk-cc-nongnu)
>  
>  ifeq ($(have-ssp),yes)
>  tests += tst-ssp-1
>  endif
>  
>  ifeq (,$(CXX))
> -tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu)
>  endif
>  
>  extra-libs = libSegFault libpcprofile
> @@ -194,7 +198,8 @@ define chk-gen-locales
>  $(objpfx)$(1).out: $(gen-locales)
>  endef
>  $(foreach t, \
> -	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
> +	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
> +	  $(tests-chk-nongnu) $(tests-chk-cc-nongnu), \
>  	  $(eval $(call link-cc,$(t))))
>  endif
>  

Similar to LFS suggestion I think you can simplify it further by using:

  define enable-largefile
  CFLAGS-$(1).$(2) += -U_GNU_SOURCE -D_LARGEFILE64_SOURCE=1
  endef

  $(foreach t,$(tests-chk-nongnu), $(eval $(call enable-largefile,$(t),c)))
  $(foreach t,$(tests-chk-cc-nongnu), $(eval $(call enable-largefile,$(t),cc)))

And remove _LARGEFILE64_SOURCE and _GNU_SOURCE on each new test.


> diff --git a/debug/tst-chk-0-cc-nongnu.cc b/debug/tst-chk-0-cc-nongnu.cc
> new file mode 100644
> index 0000000000..24b1306e16
> --- /dev/null
> +++ b/debug/tst-chk-0-cc-nongnu.cc
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-0-nongnu.c b/debug/tst-chk-0-nongnu.c
> new file mode 100644
> index 0000000000..24b1306e16
> --- /dev/null
> +++ b/debug/tst-chk-0-nongnu.c
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-0.c"
> diff --git a/debug/tst-chk-0.c b/debug/tst-chk-0.c
> index 9288610fe6..d65a2fe6e1 100644
> --- a/debug/tst-chk-0.c
> +++ b/debug/tst-chk-0.c
> @@ -1,4 +1,5 @@
>  /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
> +   Copyright The GNU Toolchain Authors.
>     This file is part of the GNU C Library.
>  
>     The GNU C Library is free software; you can redistribute it and/or
> @@ -36,6 +37,17 @@
>  #include <sys/socket.h>
>  #include <sys/un.h>
>  
> +#ifndef _GNU_SOURCE
> +# define MEMPCPY memcpy
> +# define WMEMPCPY wmemcpy
> +# define MEMPCPY_RET(x) 0
> +# define WMEMPCPY_RET(x) 0
> +#else
> +# define MEMPCPY mempcpy
> +# define WMEMPCPY wmempcpy
> +# define MEMPCPY_RET(x) __builtin_strlen (x)
> +# define WMEMPCPY_RET(x) wcslen (x)
> +#endif
>  
>  #define obstack_chunk_alloc malloc
>  #define obstack_chunk_free free
> @@ -162,7 +174,7 @@ do_test (void)
>    if (memcmp (buf, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
> +  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (buf, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -207,7 +219,7 @@ do_test (void)
>    if (memcmp (buf, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
> +  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (buf, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -266,7 +278,8 @@ do_test (void)
>    if (memcmp (a.buf1, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
> +  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
> +      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (a.buf1, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -347,6 +360,7 @@ do_test (void)
>    bcopy (buf + 1, buf + 2, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    p = (char *) mempcpy (buf + 6, "abcde", 5);
>    CHK_FAIL_END
> @@ -354,6 +368,7 @@ do_test (void)
>    CHK_FAIL_START
>    p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    memset (buf + 9, 'j', 2);
> @@ -464,6 +479,7 @@ do_test (void)
>    bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
>    CHK_FAIL_END
> @@ -471,6 +487,7 @@ do_test (void)
>    CHK_FAIL_START
>    p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    memset (a.buf1 + 9, 'j', 2);
> @@ -550,7 +567,7 @@ do_test (void)
>    if (wmemcmp (wbuf, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
> +  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wbuf, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -583,7 +600,8 @@ do_test (void)
>    if (wmemcmp (wbuf, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
> +  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
> +      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wbuf, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -625,7 +643,8 @@ do_test (void)
>    if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
> +  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
> +      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wa.buf1, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -694,6 +713,7 @@ do_test (void)
>    wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    wp = wmempcpy (wbuf + 6, L"abcde", 5);
>    CHK_FAIL_END
> @@ -701,6 +721,7 @@ do_test (void)
>    CHK_FAIL_START
>    wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    wmemset (wbuf + 9, L'j', 2);
> @@ -768,6 +789,7 @@ do_test (void)
>    wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
>    CHK_FAIL_END
> @@ -775,6 +797,7 @@ do_test (void)
>    CHK_FAIL_START
>    wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    wmemset (wa.buf1 + 9, L'j', 2);
> @@ -906,6 +929,7 @@ do_test (void)
>    if (fprintf (fp, buf2 + 4, str5) != 7)
>      FAIL ();
>  
> +#ifdef _GNU_SOURCE
>    char *my_ptr = NULL;
>    strcpy (buf2 + 2, "%n%s%n");
>    /* When the format string is writable and contains %n,
> @@ -935,6 +959,7 @@ do_test (void)
>    if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
>      FAIL ();
>    obstack_free (&obs, NULL);
> +#endif
>  
>    if (freopen (temp_filename, "r", stdin) == NULL)
>      {
> @@ -982,6 +1007,7 @@ do_test (void)
>  
>    rewind (stdin);
>  
> +#ifdef _GNU_SOURCE
>    if (fgets_unlocked (buf, buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
> @@ -1008,6 +1034,7 @@ do_test (void)
>  #endif
>  
>    rewind (stdin);
> +#endif
>  
>    if (fread (buf, 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
> @@ -1578,7 +1605,10 @@ do_test (void)
>        ret = 1;
>      }
>  
> -  int fd = posix_openpt (O_RDWR);
> +  int fd;
> +
> +#ifdef _GNU_SOURCE
> +  fd = posix_openpt (O_RDWR);
>    if (fd != -1)
>      {
>        char enough[1000];
> @@ -1594,6 +1624,7 @@ do_test (void)
>  #endif
>        close (fd);
>      }
> +#endif
>  
>  #if PATH_MAX > 0
>    confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
> @@ -1711,8 +1742,9 @@ do_test (void)
>    poll (fds, l0 + 2, 0);
>    CHK_FAIL_END
>  #endif
> +#ifdef _GNU_SOURCE
>    ppoll (fds, 1, NULL, NULL);
> -#if __USE_FORTIFY_LEVEL >= 1
> +# if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
>    ppoll (fds, 2, NULL, NULL);
>    CHK_FAIL_END
> @@ -1720,6 +1752,7 @@ do_test (void)
>    CHK_FAIL_START
>    ppoll (fds, l0 + 2, NULL, NULL);
>    CHK_FAIL_END
> +# endif
>  #endif
>  
>    return ret;
> diff --git a/debug/tst-chk-1-cc-nongnu.cc b/debug/tst-chk-1-cc-nongnu.cc
> new file mode 100644
> index 0000000000..cce36ff617
> --- /dev/null
> +++ b/debug/tst-chk-1-cc-nongnu.cc
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-1.c"
> diff --git a/debug/tst-chk-1-nongnu.c b/debug/tst-chk-1-nongnu.c
> new file mode 100644
> index 0000000000..cce36ff617
> --- /dev/null
> +++ b/debug/tst-chk-1-nongnu.c
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-1.c"
> diff --git a/debug/tst-chk-2-cc-nongnu.cc b/debug/tst-chk-2-cc-nongnu.cc
> new file mode 100644
> index 0000000000..149af283c4
> --- /dev/null
> +++ b/debug/tst-chk-2-cc-nongnu.cc
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-2.c"
> diff --git a/debug/tst-chk-2-nongnu.c b/debug/tst-chk-2-nongnu.c
> new file mode 100644
> index 0000000000..149af283c4
> --- /dev/null
> +++ b/debug/tst-chk-2-nongnu.c
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-2.c"
> diff --git a/debug/tst-chk-3-cc-nongnu.cc b/debug/tst-chk-3-cc-nongnu.cc
> new file mode 100644
> index 0000000000..a306b3290c
> --- /dev/null
> +++ b/debug/tst-chk-3-cc-nongnu.cc
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-3.c"
> diff --git a/debug/tst-chk-3-nongnu.c b/debug/tst-chk-3-nongnu.c
> new file mode 100644
> index 0000000000..a306b3290c
> --- /dev/null
> +++ b/debug/tst-chk-3-nongnu.c
> @@ -0,0 +1,3 @@
> +#undef _GNU_SOURCE
> +#define _LARGEFILE64_SOURCE 1
> +#include "tst-chk-3.c"
> diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
> index 01df84b5f9..f53a7a06eb 100644
> --- a/posix/bits/unistd.h
> +++ b/posix/bits/unistd.h
> @@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
>  			  __fd, __buf, __nbytes);
>  }
>  
> -#ifdef __USE_UNIX98
> +#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
>  extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
>  			    __off_t __offset, size_t __bufsize)
>    __wur __attr_access ((__write_only__, 2, 3));

Ok.

> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
> index 51ad480401..d0188b5ba8 100644
> --- a/string/bits/string_fortified.h
> +++ b/string/bits/string_fortified.h
> @@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
>    return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
>  }
>  
> -#ifdef __USE_GNU
> +#ifdef __USE_XOPEN2K8
>  __fortify_function char *
>  __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
>  {
> @@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
>  				  __glibc_objsize (__dest));
>  }
>  

Ok.

> -#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
> +#if __USE_XOPEN2K8

Should it be #ifdef here?

> +# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>  __fortify_function char *
>  __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>  {
>    return __builtin___stpncpy_chk (__dest, __src, __n,
>  				  __glibc_objsize (__dest));
>  }
> -#else
> +# else
>  extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>  			    size_t __destlen) __THROW
>    __fortified_attr_access ((__write_only__, 1, 3))
> @@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>      return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
>    return __stpncpy_alias (__dest, __src, __n);
>  }
> +# endif
>  #endif
>  
>  

Ok.

> diff --git a/support/xsignal.h b/support/xsignal.h
> index 973f532495..d868bb336c 100644
> --- a/support/xsignal.h
> +++ b/support/xsignal.h
> @@ -28,7 +28,9 @@ __BEGIN_DECLS
>     terminate the process on error.  */
>  
>  void xraise (int sig);
> +#ifdef _GNU_SOURCE
>  sighandler_t xsignal (int sig, sighandler_t handler);
> +#endif
>  void xsigaction (int sig, const struct sigaction *newact,
>                   struct sigaction *oldact);
>  
> diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
> index 25d06d433f..0e017f458b 100644
> --- a/wcsmbs/bits/wchar2.h
> +++ b/wcsmbs/bits/wchar2.h
> @@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  }
>  
>  
> -#ifdef __USE_GNU
> +#ifdef	__USE_XOPEN2K8
>  extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
>  				const char **__restrict __src, size_t __nmc,
>  				size_t __len, mbstate_t *__restrict __ps,

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests
  2022-01-06 20:51     ` Adhemerval Zanella
@ 2022-01-07  2:09       ` Siddhesh Poyarekar
  2022-01-07 13:00         ` Adhemerval Zanella
  0 siblings, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-07  2:09 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 07/01/2022 02:21, Adhemerval Zanella wrote:
> 
> 
> On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
>> Rename all debug/tst-chk* tests to reflect the fortification level
>> they're testing and any additional macros so that rules for them can be
>> autogenerated.  tst-chk0* are without fortification, tst-chk1 for
>> _FORTIFY_SOURCE=1 and so on.  This allows easier replication of the
>> tests to check additional macros.
>>
>> The change also expands the -lfs to include _FORTIFY_SOURCE=3.
>>
>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> 
> Looks good in general, some comments below.
> 
>> ---
>>   debug/Makefile                               | 75 +++++++++-----------
>>   debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} |  2 +-
>>   debug/tst-chk-0-cc.cc                        |  1 +
>>   debug/{tst-lfschk2.c => tst-chk-0-lfs.c}     |  2 +-
>>   debug/{tst-chk1.c => tst-chk-0.c}            |  0
>>   debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} |  2 +-
>>   debug/{tst-chk2.c => tst-chk-1-cc.cc}        |  2 +-
>>   debug/{tst-lfschk4.cc => tst-chk-1-lfs.c}    |  2 +-
>>   debug/{tst-chk5.cc => tst-chk-1.c}           |  2 +-
>>   debug/tst-chk-2-cc-lfs.cc                    |  2 +
>>   debug/{tst-chk6.cc => tst-chk-2-cc.cc}       |  2 +-
>>   debug/tst-chk-2-lfs.c                        |  2 +
>>   debug/{tst-chk3.c => tst-chk-2.c}            |  2 +-
>>   debug/tst-chk-3-cc-lfs.cc                    |  2 +
>>   debug/{tst-chk7.c => tst-chk-3-cc.cc}        |  2 +-
>>   debug/tst-chk-3-lfs.c                        |  2 +
>>   debug/{tst-chk8.cc => tst-chk-3.c}           |  2 +-
>>   debug/tst-chk4.cc                            |  1 -
>>   debug/tst-lfschk5.cc                         |  2 -
>>   debug/tst-lfschk6.cc                         |  2 -
>>   20 files changed, 52 insertions(+), 57 deletions(-)
>>   rename debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} (55%)
>>   create mode 100644 debug/tst-chk-0-cc.cc
>>   rename debug/{tst-lfschk2.c => tst-chk-0-lfs.c} (55%)
>>   rename debug/{tst-chk1.c => tst-chk-0.c} (100%)
>>   rename debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} (55%)
>>   rename debug/{tst-chk2.c => tst-chk-1-cc.cc} (53%)
>>   rename debug/{tst-lfschk4.cc => tst-chk-1-lfs.c} (55%)
>>   rename debug/{tst-chk5.cc => tst-chk-1.c} (53%)
>>   create mode 100644 debug/tst-chk-2-cc-lfs.cc
>>   rename debug/{tst-chk6.cc => tst-chk-2-cc.cc} (53%)
>>   create mode 100644 debug/tst-chk-2-lfs.c
>>   rename debug/{tst-chk3.c => tst-chk-2.c} (53%)
>>   create mode 100644 debug/tst-chk-3-cc-lfs.cc
>>   rename debug/{tst-chk7.c => tst-chk-3-cc.cc} (53%)
>>   create mode 100644 debug/tst-chk-3-lfs.c
>>   rename debug/{tst-chk8.cc => tst-chk-3.c} (53%)
>>   delete mode 100644 debug/tst-chk4.cc
>>   delete mode 100644 debug/tst-lfschk5.cc
>>   delete mode 100644 debug/tst-lfschk6.cc
>>
>> diff --git a/debug/Makefile b/debug/Makefile
>> index 9c2ce61a86..9aa27eb00c 100644
>> --- a/debug/Makefile
>> +++ b/debug/Makefile
>> @@ -1,4 +1,5 @@
>>   # Copyright (C) 1998-2022 Free Software Foundation, Inc.
>> +# Copyright The GNU Toolchain Authors.
>>   # This file is part of the GNU C Library.
>>   
>>   # The GNU C Library is free software; you can redistribute it and/or
>> @@ -110,32 +111,31 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
>>   CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
>>   CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
>>   
>> +# _FORTIFY_SOURCE tests.
>> +tests-chk = $(addprefix tst-chk-, 0 1 2 3)
>> +tests-chk-cc = $(addsuffix -cc, $(tests-chk))
>> +tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
>> +tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
>> +
>>   # We know these tests have problems with format strings, this is what
>>   # we are testing.  Disable that warning.  They are also testing
>>   # deprecated functions (notably gets) so disable that warning as well.
>>   # And they also generate warnings from warning attributes, which
>>   # cannot be disabled via pragmas, so require -Wno-error to be used.
>> -CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>> -LDLIBS-tst-chk4 = -lstdc++
>> -LDLIBS-tst-chk5 = -lstdc++
>> -LDLIBS-tst-chk6 = -lstdc++
>> -LDLIBS-tst-chk8 = -lstdc++
>> -LDLIBS-tst-lfschk4 = -lstdc++
>> -LDLIBS-tst-lfschk5 = -lstdc++
>> -LDLIBS-tst-lfschk6 = -lstdc++
>> +define disable-warnings
>> +CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>> +endef
>> +
>> +$(foreach t,$(tests-chk) $(tests-chk-lfs), \
>> +	  $(eval $(call disable-warnings,$(t),c)))
>> +
>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
>> +	  $(eval $(call disable-warnings,$(t),cc)))
>> +
>> +define link-cc
>> +LDLIBS-$(1) = -lstdc++
>> +endef
>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
>>   
>>   # backtrace_symbols only works if we link with -rdynamic.  backtrace
>>   # requires unwind tables on most architectures.
>> @@ -152,19 +152,18 @@ LDFLAGS-tst-backtrace6 = -rdynamic
>>   
>>   CFLAGS-tst-ssp-1.c += -fstack-protector-all
>>   
> 
> Nice, it does simplify the required rules.  Maybe you could also extend it to
> LFS as well:
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index dc73600213..019046391b 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -127,6 +127,9 @@ tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
>   define disable-warnings
>   CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>   endef
> +define enable-lfs
> +CFLAGS-$(1).$(2) += -D_FILE_OFFSET_BITS=64
> +endef
>   
>   $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>   	  $(eval $(call disable-warnings,$(t),c)))
> @@ -134,6 +137,9 @@ $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>   $(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>   	  $(eval $(call disable-warnings,$(t),cc)))
>   
> +$(foreach t,$(tests-chk-lfs), $(eval $(call enable-lfs,$(t),c)))
> +$(foreach t,$(tests-chk-cc-lfs), $(eval $(call enable-lfs,$(t),cc)))
> +
>   define link-cc
>   LDLIBS-$(1) = -lstdc++
>   endef
> 
> And then remove the '#define _FILE_OFFSET_BITS 64' so the rule is applied
> regardless whether the file defines or not.

While it works for _FILE_OFFSET_BITS, I doubt if it'll work for 
_GNU_SOURCE since it will get overridden by libc-symbols.h.

At some point I want to do a cleanup where libc-symbols.h undefines 
_GNU_SOURCE for tests and only tests that require it, define it 
explicitly.  Then we can properly auto-generate _FORTIFY_SOURCE tests 
out of tst-chk-0.c just like we do for mcheck and malloc-check tests.

May I keep it like this for now and do this additional cleanup later?

> 
>> -tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
>> -	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
>> -	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
>> -	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>> -	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
>> +tests = backtrace-tst tst-longjmp_chk \
>> +	test-strcpy_chk test-stpcpy_chk \
>> +	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>> +	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
>> +	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
> 
> Since you are touching, put one file per line.

OK.

> 
>>   
>>   ifeq ($(have-ssp),yes)
>>   tests += tst-ssp-1
>>   endif
>>   
>>   ifeq (,$(CXX))
>> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
>> -		    tst-lfschk4 tst-lfschk5 tst-lfschk6
>> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
> 
> Same.
> 
>>   endif
>>   
>>   extra-libs = libSegFault libpcprofile
>> @@ -191,20 +190,12 @@ ifeq ($(run-built-tests),yes)
>>   LOCALES := de_DE.UTF-8
>>   include ../gen-locales.mk
>>   
>> -$(objpfx)tst-chk1.out: $(gen-locales)
>> -$(objpfx)tst-chk2.out: $(gen-locales)
>> -$(objpfx)tst-chk3.out: $(gen-locales)
>> -$(objpfx)tst-chk4.out: $(gen-locales)
>> -$(objpfx)tst-chk5.out: $(gen-locales)
>> -$(objpfx)tst-chk6.out: $(gen-locales)
>> -$(objpfx)tst-chk7.out: $(gen-locales)
>> -$(objpfx)tst-chk8.out: $(gen-locales)
>> -$(objpfx)tst-lfschk1.out: $(gen-locales)
>> -$(objpfx)tst-lfschk2.out: $(gen-locales)
>> -$(objpfx)tst-lfschk3.out: $(gen-locales)
>> -$(objpfx)tst-lfschk4.out: $(gen-locales)
>> -$(objpfx)tst-lfschk5.out: $(gen-locales)
>> -$(objpfx)tst-lfschk6.out: $(gen-locales)
>> +define chk-gen-locales
>> +$(objpfx)$(1).out: $(gen-locales)
>> +endef
>> +$(foreach t, \
>> +	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
>> +	  $(eval $(call link-cc,$(t))))
>>   endif
>>   
>>   sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
> 
> Same.

OK.

> 
>> diff --git a/debug/tst-lfschk1.c b/debug/tst-chk-0-cc-lfs.cc
>> similarity index 55%
>> rename from debug/tst-lfschk1.c
>> rename to debug/tst-chk-0-cc-lfs.cc
>> index f3e6d47d5e..db699b2391 100644
>> --- a/debug/tst-lfschk1.c
>> +++ b/debug/tst-chk-0-cc-lfs.cc
>> @@ -1,2 +1,2 @@
>>   #define _FILE_OFFSET_BITS 64
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-0-cc.cc b/debug/tst-chk-0-cc.cc
>> new file mode 100644
>> index 0000000000..be76fc69d4
>> --- /dev/null
>> +++ b/debug/tst-chk-0-cc.cc
>> @@ -0,0 +1 @@
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-lfschk2.c b/debug/tst-chk-0-lfs.c
>> similarity index 55%
>> rename from debug/tst-lfschk2.c
>> rename to debug/tst-chk-0-lfs.c
>> index 95d4db1d32..db699b2391 100644
>> --- a/debug/tst-lfschk2.c
>> +++ b/debug/tst-chk-0-lfs.c
>> @@ -1,2 +1,2 @@
>>   #define _FILE_OFFSET_BITS 64
>> -#include "tst-chk2.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk1.c b/debug/tst-chk-0.c
>> similarity index 100%
>> rename from debug/tst-chk1.c
>> rename to debug/tst-chk-0.c
>> diff --git a/debug/tst-lfschk3.c b/debug/tst-chk-1-cc-lfs.cc
>> similarity index 55%
>> rename from debug/tst-lfschk3.c
>> rename to debug/tst-chk-1-cc-lfs.cc
>> index 50a1ae1258..0d67643e9e 100644
>> --- a/debug/tst-lfschk3.c
>> +++ b/debug/tst-chk-1-cc-lfs.cc
>> @@ -1,2 +1,2 @@
>>   #define _FILE_OFFSET_BITS 64
>> -#include "tst-chk3.c"
>> +#include "tst-chk-1.c"
>> diff --git a/debug/tst-chk2.c b/debug/tst-chk-1-cc.cc
>> similarity index 53%
>> rename from debug/tst-chk2.c
>> rename to debug/tst-chk-1-cc.cc
>> index be37ce2d22..945c1f8d5b 100644
>> --- a/debug/tst-chk2.c
>> +++ b/debug/tst-chk-1-cc.cc
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 1
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-lfschk4.cc b/debug/tst-chk-1-lfs.c
>> similarity index 55%
>> rename from debug/tst-lfschk4.cc
>> rename to debug/tst-chk-1-lfs.c
>> index f3e6d47d5e..0d67643e9e 100644
>> --- a/debug/tst-lfschk4.cc
>> +++ b/debug/tst-chk-1-lfs.c
>> @@ -1,2 +1,2 @@
>>   #define _FILE_OFFSET_BITS 64
>> -#include "tst-chk1.c"
>> +#include "tst-chk-1.c"
>> diff --git a/debug/tst-chk5.cc b/debug/tst-chk-1.c
>> similarity index 53%
>> rename from debug/tst-chk5.cc
>> rename to debug/tst-chk-1.c
>> index be37ce2d22..945c1f8d5b 100644
>> --- a/debug/tst-chk5.cc
>> +++ b/debug/tst-chk-1.c
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 1
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-2-cc-lfs.cc b/debug/tst-chk-2-cc-lfs.cc
>> new file mode 100644
>> index 0000000000..97c83def5c
>> --- /dev/null
>> +++ b/debug/tst-chk-2-cc-lfs.cc
>> @@ -0,0 +1,2 @@
>> +#define _FILE_OFFSET_BITS 64
>> +#include "tst-chk-2.c"
>> diff --git a/debug/tst-chk6.cc b/debug/tst-chk-2-cc.cc
>> similarity index 53%
>> rename from debug/tst-chk6.cc
>> rename to debug/tst-chk-2-cc.cc
>> index 38b8e4fb36..f178340bbd 100644
>> --- a/debug/tst-chk6.cc
>> +++ b/debug/tst-chk-2-cc.cc
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 2
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-2-lfs.c b/debug/tst-chk-2-lfs.c
>> new file mode 100644
>> index 0000000000..97c83def5c
>> --- /dev/null
>> +++ b/debug/tst-chk-2-lfs.c
>> @@ -0,0 +1,2 @@
>> +#define _FILE_OFFSET_BITS 64
>> +#include "tst-chk-2.c"
>> diff --git a/debug/tst-chk3.c b/debug/tst-chk-2.c
>> similarity index 53%
>> rename from debug/tst-chk3.c
>> rename to debug/tst-chk-2.c
>> index 38b8e4fb36..f178340bbd 100644
>> --- a/debug/tst-chk3.c
>> +++ b/debug/tst-chk-2.c
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 2
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-3-cc-lfs.cc b/debug/tst-chk-3-cc-lfs.cc
>> new file mode 100644
>> index 0000000000..d5d8c28bc7
>> --- /dev/null
>> +++ b/debug/tst-chk-3-cc-lfs.cc
>> @@ -0,0 +1,2 @@
>> +#define _FILE_OFFSET_BITS 64
>> +#include "tst-chk-3.c"
>> diff --git a/debug/tst-chk7.c b/debug/tst-chk-3-cc.cc
>> similarity index 53%
>> rename from debug/tst-chk7.c
>> rename to debug/tst-chk-3-cc.cc
>> index 2a7b323812..c1ddac1261 100644
>> --- a/debug/tst-chk7.c
>> +++ b/debug/tst-chk-3-cc.cc
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 3
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-3-lfs.c b/debug/tst-chk-3-lfs.c
>> new file mode 100644
>> index 0000000000..d5d8c28bc7
>> --- /dev/null
>> +++ b/debug/tst-chk-3-lfs.c
>> @@ -0,0 +1,2 @@
>> +#define _FILE_OFFSET_BITS 64
>> +#include "tst-chk-3.c"
>> diff --git a/debug/tst-chk8.cc b/debug/tst-chk-3.c
>> similarity index 53%
>> rename from debug/tst-chk8.cc
>> rename to debug/tst-chk-3.c
>> index 2a7b323812..c1ddac1261 100644
>> --- a/debug/tst-chk8.cc
>> +++ b/debug/tst-chk-3.c
>> @@ -1,2 +1,2 @@
>>   #define _FORTIFY_SOURCE 3
>> -#include "tst-chk1.c"
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
>> deleted file mode 100644
>> index c82e6aac86..0000000000
>> --- a/debug/tst-chk4.cc
>> +++ /dev/null
>> @@ -1 +0,0 @@
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
>> deleted file mode 100644
>> index 95d4db1d32..0000000000
>> --- a/debug/tst-lfschk5.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk2.c"
>> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
>> deleted file mode 100644
>> index 50a1ae1258..0000000000
>> --- a/debug/tst-lfschk6.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk3.c"
> 

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-06 21:15     ` Adhemerval Zanella
@ 2022-01-07  2:12       ` Siddhesh Poyarekar
  2022-01-07 13:02         ` Adhemerval Zanella
  0 siblings, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-07  2:12 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 07/01/2022 02:45, Adhemerval Zanella wrote:
> 
> 
> On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
>> Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
>> main headers as they got incorporated into the standard, but their
>> fortified variants remained under __USE_GNU.  As a result, these
>> functions did not get fortified when _GNU_SOURCE was not defined.
>>
>> Add test wrappers that check all functions tested in tst-chk0 at all
>> levels with _GNU_SOURCE undefined and then use the failures to (1)
>> exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
>> feature macro guards in the fortified function headers so that they're
>> the same as the ones in the main headers.
>>
>> This fixes BZ #28746.
>>
>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> 
> Patch look ok in general, some comments below.
> 
>> ---
>>   debug/Makefile                 | 17 +++++++-----
>>   debug/tst-chk-0-cc-nongnu.cc   |  3 +++
>>   debug/tst-chk-0-nongnu.c       |  3 +++
>>   debug/tst-chk-0.c              | 49 ++++++++++++++++++++++++++++------
>>   debug/tst-chk-1-cc-nongnu.cc   |  3 +++
>>   debug/tst-chk-1-nongnu.c       |  3 +++
>>   debug/tst-chk-2-cc-nongnu.cc   |  3 +++
>>   debug/tst-chk-2-nongnu.c       |  3 +++
>>   debug/tst-chk-3-cc-nongnu.cc   |  3 +++
>>   debug/tst-chk-3-nongnu.c       |  3 +++
>>   posix/bits/unistd.h            |  2 +-
>>   string/bits/string_fortified.h |  8 +++---
>>   support/xsignal.h              |  2 ++
>>   wcsmbs/bits/wchar2.h           |  2 +-
>>   14 files changed, 85 insertions(+), 19 deletions(-)
>>   create mode 100644 debug/tst-chk-0-cc-nongnu.cc
>>   create mode 100644 debug/tst-chk-0-nongnu.c
>>   create mode 100644 debug/tst-chk-1-cc-nongnu.cc
>>   create mode 100644 debug/tst-chk-1-nongnu.c
>>   create mode 100644 debug/tst-chk-2-cc-nongnu.cc
>>   create mode 100644 debug/tst-chk-2-nongnu.c
>>   create mode 100644 debug/tst-chk-3-cc-nongnu.cc
>>   create mode 100644 debug/tst-chk-3-nongnu.c
>>
>> diff --git a/debug/Makefile b/debug/Makefile
>> index 9aa27eb00c..876d10e521 100644
>> --- a/debug/Makefile
>> +++ b/debug/Makefile
>> @@ -116,6 +116,8 @@ tests-chk = $(addprefix tst-chk-, 0 1 2 3)
>>   tests-chk-cc = $(addsuffix -cc, $(tests-chk))
>>   tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
>>   tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
>> +tests-chk-nongnu = $(addsuffix -nongnu, $(tests-chk))
>> +tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
>>   
>>   # We know these tests have problems with format strings, this is what
>>   # we are testing.  Disable that warning.  They are also testing
>> @@ -126,16 +128,17 @@ define disable-warnings
>>   CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>>   endef
>>   
>> -$(foreach t,$(tests-chk) $(tests-chk-lfs), \
>> +$(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>>   	  $(eval $(call disable-warnings,$(t),c)))
>>   
>> -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>>   	  $(eval $(call disable-warnings,$(t),cc)))
>>   
>>   define link-cc
>>   LDLIBS-$(1) = -lstdc++
>>   endef
>> -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>> +	  $(eval $(call link-cc,$(t))))
>>   
>>   # backtrace_symbols only works if we link with -rdynamic.  backtrace
>>   # requires unwind tables on most architectures.
> 
> Ok.
> 
>> @@ -156,14 +159,15 @@ tests = backtrace-tst tst-longjmp_chk \
>>   	test-strcpy_chk test-stpcpy_chk \
>>   	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>>   	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
>> -	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
>> +	$(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
>> +	$(tests-chk-nongnu) $(tests-chk-cc-nongnu)
>>   
>>   ifeq ($(have-ssp),yes)
>>   tests += tst-ssp-1
>>   endif
>>   
>>   ifeq (,$(CXX))
>> -tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
>> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu)
>>   endif
>>   
>>   extra-libs = libSegFault libpcprofile
>> @@ -194,7 +198,8 @@ define chk-gen-locales
>>   $(objpfx)$(1).out: $(gen-locales)
>>   endef
>>   $(foreach t, \
>> -	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
>> +	  $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
>> +	  $(tests-chk-nongnu) $(tests-chk-cc-nongnu), \
>>   	  $(eval $(call link-cc,$(t))))
>>   endif
>>   
> 
> Similar to LFS suggestion I think you can simplify it further by using:
> 
>    define enable-largefile
>    CFLAGS-$(1).$(2) += -U_GNU_SOURCE -D_LARGEFILE64_SOURCE=1
>    endef
> 
>    $(foreach t,$(tests-chk-nongnu), $(eval $(call enable-largefile,$(t),c)))
>    $(foreach t,$(tests-chk-cc-nongnu), $(eval $(call enable-largefile,$(t),cc)))
> 
> And remove _LARGEFILE64_SOURCE and _GNU_SOURCE on each new test.

It won't work for _GNU_SOURCE at the moment since _GNU_SOURCE gets 
defined again in libc-symbols.h.  May I leave this cleanup for later?  I 
don't think I can do it in time for 2.35 and it may become too invasive 
at this stage.

> 
> 
>> diff --git a/debug/tst-chk-0-cc-nongnu.cc b/debug/tst-chk-0-cc-nongnu.cc
>> new file mode 100644
>> index 0000000000..24b1306e16
>> --- /dev/null
>> +++ b/debug/tst-chk-0-cc-nongnu.cc
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-0-nongnu.c b/debug/tst-chk-0-nongnu.c
>> new file mode 100644
>> index 0000000000..24b1306e16
>> --- /dev/null
>> +++ b/debug/tst-chk-0-nongnu.c
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-0.c"
>> diff --git a/debug/tst-chk-0.c b/debug/tst-chk-0.c
>> index 9288610fe6..d65a2fe6e1 100644
>> --- a/debug/tst-chk-0.c
>> +++ b/debug/tst-chk-0.c
>> @@ -1,4 +1,5 @@
>>   /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
>> +   Copyright The GNU Toolchain Authors.
>>      This file is part of the GNU C Library.
>>   
>>      The GNU C Library is free software; you can redistribute it and/or
>> @@ -36,6 +37,17 @@
>>   #include <sys/socket.h>
>>   #include <sys/un.h>
>>   
>> +#ifndef _GNU_SOURCE
>> +# define MEMPCPY memcpy
>> +# define WMEMPCPY wmemcpy
>> +# define MEMPCPY_RET(x) 0
>> +# define WMEMPCPY_RET(x) 0
>> +#else
>> +# define MEMPCPY mempcpy
>> +# define WMEMPCPY wmempcpy
>> +# define MEMPCPY_RET(x) __builtin_strlen (x)
>> +# define WMEMPCPY_RET(x) wcslen (x)
>> +#endif
>>   
>>   #define obstack_chunk_alloc malloc
>>   #define obstack_chunk_free free
>> @@ -162,7 +174,7 @@ do_test (void)
>>     if (memcmp (buf, "aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
>> +  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
>>         || memcmp (buf, "aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -207,7 +219,7 @@ do_test (void)
>>     if (memcmp (buf, "aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
>> +  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
>>         || memcmp (buf, "aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -266,7 +278,8 @@ do_test (void)
>>     if (memcmp (a.buf1, "aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
>> +  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
>> +      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
>>         || memcmp (a.buf1, "aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -347,6 +360,7 @@ do_test (void)
>>     bcopy (buf + 1, buf + 2, l0 + 9);
>>     CHK_FAIL_END
>>   
>> +#ifdef _GNU_SOURCE
>>     CHK_FAIL_START
>>     p = (char *) mempcpy (buf + 6, "abcde", 5);
>>     CHK_FAIL_END
>> @@ -354,6 +368,7 @@ do_test (void)
>>     CHK_FAIL_START
>>     p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
>>     CHK_FAIL_END
>> +#endif
>>   
>>     CHK_FAIL_START
>>     memset (buf + 9, 'j', 2);
>> @@ -464,6 +479,7 @@ do_test (void)
>>     bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
>>     CHK_FAIL_END
>>   
>> +#ifdef _GNU_SOURCE
>>     CHK_FAIL_START
>>     p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
>>     CHK_FAIL_END
>> @@ -471,6 +487,7 @@ do_test (void)
>>     CHK_FAIL_START
>>     p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
>>     CHK_FAIL_END
>> +#endif
>>   
>>     CHK_FAIL_START
>>     memset (a.buf1 + 9, 'j', 2);
>> @@ -550,7 +567,7 @@ do_test (void)
>>     if (wmemcmp (wbuf, L"aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
>> +  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>>         || wmemcmp (wbuf, L"aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -583,7 +600,8 @@ do_test (void)
>>     if (wmemcmp (wbuf, L"aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
>> +  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
>> +      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>>         || wmemcmp (wbuf, L"aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -625,7 +643,8 @@ do_test (void)
>>     if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
>>       FAIL ();
>>   
>> -  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
>> +  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
>> +      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
>>         || wmemcmp (wa.buf1, L"aabcdabcde", 10))
>>       FAIL ();
>>   
>> @@ -694,6 +713,7 @@ do_test (void)
>>     wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
>>     CHK_FAIL_END
>>   
>> +#ifdef _GNU_SOURCE
>>     CHK_FAIL_START
>>     wp = wmempcpy (wbuf + 6, L"abcde", 5);
>>     CHK_FAIL_END
>> @@ -701,6 +721,7 @@ do_test (void)
>>     CHK_FAIL_START
>>     wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
>>     CHK_FAIL_END
>> +#endif
>>   
>>     CHK_FAIL_START
>>     wmemset (wbuf + 9, L'j', 2);
>> @@ -768,6 +789,7 @@ do_test (void)
>>     wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
>>     CHK_FAIL_END
>>   
>> +#ifdef _GNU_SOURCE
>>     CHK_FAIL_START
>>     wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
>>     CHK_FAIL_END
>> @@ -775,6 +797,7 @@ do_test (void)
>>     CHK_FAIL_START
>>     wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
>>     CHK_FAIL_END
>> +#endif
>>   
>>     CHK_FAIL_START
>>     wmemset (wa.buf1 + 9, L'j', 2);
>> @@ -906,6 +929,7 @@ do_test (void)
>>     if (fprintf (fp, buf2 + 4, str5) != 7)
>>       FAIL ();
>>   
>> +#ifdef _GNU_SOURCE
>>     char *my_ptr = NULL;
>>     strcpy (buf2 + 2, "%n%s%n");
>>     /* When the format string is writable and contains %n,
>> @@ -935,6 +959,7 @@ do_test (void)
>>     if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
>>       FAIL ();
>>     obstack_free (&obs, NULL);
>> +#endif
>>   
>>     if (freopen (temp_filename, "r", stdin) == NULL)
>>       {
>> @@ -982,6 +1007,7 @@ do_test (void)
>>   
>>     rewind (stdin);
>>   
>> +#ifdef _GNU_SOURCE
>>     if (fgets_unlocked (buf, buf_size, stdin) != buf
>>         || memcmp (buf, "abcdefgh\n", 10))
>>       FAIL ();
>> @@ -1008,6 +1034,7 @@ do_test (void)
>>   #endif
>>   
>>     rewind (stdin);
>> +#endif
>>   
>>     if (fread (buf, 1, buf_size, stdin) != buf_size
>>         || memcmp (buf, "abcdefgh\nA", 10))
>> @@ -1578,7 +1605,10 @@ do_test (void)
>>         ret = 1;
>>       }
>>   
>> -  int fd = posix_openpt (O_RDWR);
>> +  int fd;
>> +
>> +#ifdef _GNU_SOURCE
>> +  fd = posix_openpt (O_RDWR);
>>     if (fd != -1)
>>       {
>>         char enough[1000];
>> @@ -1594,6 +1624,7 @@ do_test (void)
>>   #endif
>>         close (fd);
>>       }
>> +#endif
>>   
>>   #if PATH_MAX > 0
>>     confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
>> @@ -1711,8 +1742,9 @@ do_test (void)
>>     poll (fds, l0 + 2, 0);
>>     CHK_FAIL_END
>>   #endif
>> +#ifdef _GNU_SOURCE
>>     ppoll (fds, 1, NULL, NULL);
>> -#if __USE_FORTIFY_LEVEL >= 1
>> +# if __USE_FORTIFY_LEVEL >= 1
>>     CHK_FAIL_START
>>     ppoll (fds, 2, NULL, NULL);
>>     CHK_FAIL_END
>> @@ -1720,6 +1752,7 @@ do_test (void)
>>     CHK_FAIL_START
>>     ppoll (fds, l0 + 2, NULL, NULL);
>>     CHK_FAIL_END
>> +# endif
>>   #endif
>>   
>>     return ret;
>> diff --git a/debug/tst-chk-1-cc-nongnu.cc b/debug/tst-chk-1-cc-nongnu.cc
>> new file mode 100644
>> index 0000000000..cce36ff617
>> --- /dev/null
>> +++ b/debug/tst-chk-1-cc-nongnu.cc
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-1.c"
>> diff --git a/debug/tst-chk-1-nongnu.c b/debug/tst-chk-1-nongnu.c
>> new file mode 100644
>> index 0000000000..cce36ff617
>> --- /dev/null
>> +++ b/debug/tst-chk-1-nongnu.c
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-1.c"
>> diff --git a/debug/tst-chk-2-cc-nongnu.cc b/debug/tst-chk-2-cc-nongnu.cc
>> new file mode 100644
>> index 0000000000..149af283c4
>> --- /dev/null
>> +++ b/debug/tst-chk-2-cc-nongnu.cc
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-2.c"
>> diff --git a/debug/tst-chk-2-nongnu.c b/debug/tst-chk-2-nongnu.c
>> new file mode 100644
>> index 0000000000..149af283c4
>> --- /dev/null
>> +++ b/debug/tst-chk-2-nongnu.c
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-2.c"
>> diff --git a/debug/tst-chk-3-cc-nongnu.cc b/debug/tst-chk-3-cc-nongnu.cc
>> new file mode 100644
>> index 0000000000..a306b3290c
>> --- /dev/null
>> +++ b/debug/tst-chk-3-cc-nongnu.cc
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-3.c"
>> diff --git a/debug/tst-chk-3-nongnu.c b/debug/tst-chk-3-nongnu.c
>> new file mode 100644
>> index 0000000000..a306b3290c
>> --- /dev/null
>> +++ b/debug/tst-chk-3-nongnu.c
>> @@ -0,0 +1,3 @@
>> +#undef _GNU_SOURCE
>> +#define _LARGEFILE64_SOURCE 1
>> +#include "tst-chk-3.c"
>> diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
>> index 01df84b5f9..f53a7a06eb 100644
>> --- a/posix/bits/unistd.h
>> +++ b/posix/bits/unistd.h
>> @@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
>>   			  __fd, __buf, __nbytes);
>>   }
>>   
>> -#ifdef __USE_UNIX98
>> +#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
>>   extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
>>   			    __off_t __offset, size_t __bufsize)
>>     __wur __attr_access ((__write_only__, 2, 3));
> 
> Ok.
> 
>> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
>> index 51ad480401..d0188b5ba8 100644
>> --- a/string/bits/string_fortified.h
>> +++ b/string/bits/string_fortified.h
>> @@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
>>     return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
>>   }
>>   
>> -#ifdef __USE_GNU
>> +#ifdef __USE_XOPEN2K8
>>   __fortify_function char *
>>   __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
>>   {
>> @@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
>>   				  __glibc_objsize (__dest));
>>   }
>>   
> 
> Ok.
> 
>> -#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>> +#if __USE_XOPEN2K8
> 
> Should it be #ifdef here?
> 
>> +# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>>   __fortify_function char *
>>   __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>>   {
>>     return __builtin___stpncpy_chk (__dest, __src, __n,
>>   				  __glibc_objsize (__dest));
>>   }
>> -#else
>> +# else
>>   extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>>   			    size_t __destlen) __THROW
>>     __fortified_attr_access ((__write_only__, 1, 3))
>> @@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>>       return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
>>     return __stpncpy_alias (__dest, __src, __n);
>>   }
>> +# endif
>>   #endif
>>   
>>   
> 
> Ok.
> 
>> diff --git a/support/xsignal.h b/support/xsignal.h
>> index 973f532495..d868bb336c 100644
>> --- a/support/xsignal.h
>> +++ b/support/xsignal.h
>> @@ -28,7 +28,9 @@ __BEGIN_DECLS
>>      terminate the process on error.  */
>>   
>>   void xraise (int sig);
>> +#ifdef _GNU_SOURCE
>>   sighandler_t xsignal (int sig, sighandler_t handler);
>> +#endif
>>   void xsigaction (int sig, const struct sigaction *newact,
>>                    struct sigaction *oldact);
>>   
>> diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
>> index 25d06d433f..0e017f458b 100644
>> --- a/wcsmbs/bits/wchar2.h
>> +++ b/wcsmbs/bits/wchar2.h
>> @@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>>   }
>>   
>>   
>> -#ifdef __USE_GNU
>> +#ifdef	__USE_XOPEN2K8
>>   extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
>>   				const char **__restrict __src, size_t __nmc,
>>   				size_t __len, mbstate_t *__restrict __ps,
> 

Thanks,
Siddhesh

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests
  2022-01-07  2:09       ` Siddhesh Poyarekar
@ 2022-01-07 13:00         ` Adhemerval Zanella
  0 siblings, 0 replies; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-07 13:00 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 06/01/2022 23:09, Siddhesh Poyarekar wrote:
> On 07/01/2022 02:21, Adhemerval Zanella wrote:
>>
>>
>> On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
>>> Rename all debug/tst-chk* tests to reflect the fortification level
>>> they're testing and any additional macros so that rules for them can be
>>> autogenerated.  tst-chk0* are without fortification, tst-chk1 for
>>> _FORTIFY_SOURCE=1 and so on.  This allows easier replication of the
>>> tests to check additional macros.
>>>
>>> The change also expands the -lfs to include _FORTIFY_SOURCE=3.
>>>
>>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
>>
>> Looks good in general, some comments below.
>>
>>> ---
>>>   debug/Makefile                               | 75 +++++++++-----------
>>>   debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} |  2 +-
>>>   debug/tst-chk-0-cc.cc                        |  1 +
>>>   debug/{tst-lfschk2.c => tst-chk-0-lfs.c}     |  2 +-
>>>   debug/{tst-chk1.c => tst-chk-0.c}            |  0
>>>   debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} |  2 +-
>>>   debug/{tst-chk2.c => tst-chk-1-cc.cc}        |  2 +-
>>>   debug/{tst-lfschk4.cc => tst-chk-1-lfs.c}    |  2 +-
>>>   debug/{tst-chk5.cc => tst-chk-1.c}           |  2 +-
>>>   debug/tst-chk-2-cc-lfs.cc                    |  2 +
>>>   debug/{tst-chk6.cc => tst-chk-2-cc.cc}       |  2 +-
>>>   debug/tst-chk-2-lfs.c                        |  2 +
>>>   debug/{tst-chk3.c => tst-chk-2.c}            |  2 +-
>>>   debug/tst-chk-3-cc-lfs.cc                    |  2 +
>>>   debug/{tst-chk7.c => tst-chk-3-cc.cc}        |  2 +-
>>>   debug/tst-chk-3-lfs.c                        |  2 +
>>>   debug/{tst-chk8.cc => tst-chk-3.c}           |  2 +-
>>>   debug/tst-chk4.cc                            |  1 -
>>>   debug/tst-lfschk5.cc                         |  2 -
>>>   debug/tst-lfschk6.cc                         |  2 -
>>>   20 files changed, 52 insertions(+), 57 deletions(-)
>>>   rename debug/{tst-lfschk1.c => tst-chk-0-cc-lfs.cc} (55%)
>>>   create mode 100644 debug/tst-chk-0-cc.cc
>>>   rename debug/{tst-lfschk2.c => tst-chk-0-lfs.c} (55%)
>>>   rename debug/{tst-chk1.c => tst-chk-0.c} (100%)
>>>   rename debug/{tst-lfschk3.c => tst-chk-1-cc-lfs.cc} (55%)
>>>   rename debug/{tst-chk2.c => tst-chk-1-cc.cc} (53%)
>>>   rename debug/{tst-lfschk4.cc => tst-chk-1-lfs.c} (55%)
>>>   rename debug/{tst-chk5.cc => tst-chk-1.c} (53%)
>>>   create mode 100644 debug/tst-chk-2-cc-lfs.cc
>>>   rename debug/{tst-chk6.cc => tst-chk-2-cc.cc} (53%)
>>>   create mode 100644 debug/tst-chk-2-lfs.c
>>>   rename debug/{tst-chk3.c => tst-chk-2.c} (53%)
>>>   create mode 100644 debug/tst-chk-3-cc-lfs.cc
>>>   rename debug/{tst-chk7.c => tst-chk-3-cc.cc} (53%)
>>>   create mode 100644 debug/tst-chk-3-lfs.c
>>>   rename debug/{tst-chk8.cc => tst-chk-3.c} (53%)
>>>   delete mode 100644 debug/tst-chk4.cc
>>>   delete mode 100644 debug/tst-lfschk5.cc
>>>   delete mode 100644 debug/tst-lfschk6.cc
>>>
>>> diff --git a/debug/Makefile b/debug/Makefile
>>> index 9c2ce61a86..9aa27eb00c 100644
>>> --- a/debug/Makefile
>>> +++ b/debug/Makefile
>>> @@ -1,4 +1,5 @@
>>>   # Copyright (C) 1998-2022 Free Software Foundation, Inc.
>>> +# Copyright The GNU Toolchain Authors.
>>>   # This file is part of the GNU C Library.
>>>     # The GNU C Library is free software; you can redistribute it and/or
>>> @@ -110,32 +111,31 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
>>>   CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
>>>   CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
>>>   +# _FORTIFY_SOURCE tests.
>>> +tests-chk = $(addprefix tst-chk-, 0 1 2 3)
>>> +tests-chk-cc = $(addsuffix -cc, $(tests-chk))
>>> +tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
>>> +tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
>>> +
>>>   # We know these tests have problems with format strings, this is what
>>>   # we are testing.  Disable that warning.  They are also testing
>>>   # deprecated functions (notably gets) so disable that warning as well.
>>>   # And they also generate warnings from warning attributes, which
>>>   # cannot be disabled via pragmas, so require -Wno-error to be used.
>>> -CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> -LDLIBS-tst-chk4 = -lstdc++
>>> -LDLIBS-tst-chk5 = -lstdc++
>>> -LDLIBS-tst-chk6 = -lstdc++
>>> -LDLIBS-tst-chk8 = -lstdc++
>>> -LDLIBS-tst-lfschk4 = -lstdc++
>>> -LDLIBS-tst-lfschk5 = -lstdc++
>>> -LDLIBS-tst-lfschk6 = -lstdc++
>>> +define disable-warnings
>>> +CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>>> +endef
>>> +
>>> +$(foreach t,$(tests-chk) $(tests-chk-lfs), \
>>> +      $(eval $(call disable-warnings,$(t),c)))
>>> +
>>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
>>> +      $(eval $(call disable-warnings,$(t),cc)))
>>> +
>>> +define link-cc
>>> +LDLIBS-$(1) = -lstdc++
>>> +endef
>>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
>>>     # backtrace_symbols only works if we link with -rdynamic.  backtrace
>>>   # requires unwind tables on most architectures.
>>> @@ -152,19 +152,18 @@ LDFLAGS-tst-backtrace6 = -rdynamic
>>>     CFLAGS-tst-ssp-1.c += -fstack-protector-all
>>>   
>>
>> Nice, it does simplify the required rules.  Maybe you could also extend it to
>> LFS as well:
>>
>> diff --git a/debug/Makefile b/debug/Makefile
>> index dc73600213..019046391b 100644
>> --- a/debug/Makefile
>> +++ b/debug/Makefile
>> @@ -127,6 +127,9 @@ tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
>>   define disable-warnings
>>   CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>>   endef
>> +define enable-lfs
>> +CFLAGS-$(1).$(2) += -D_FILE_OFFSET_BITS=64
>> +endef
>>     $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>>         $(eval $(call disable-warnings,$(t),c)))
>> @@ -134,6 +137,9 @@ $(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>>   $(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>>         $(eval $(call disable-warnings,$(t),cc)))
>>   +$(foreach t,$(tests-chk-lfs), $(eval $(call enable-lfs,$(t),c)))
>> +$(foreach t,$(tests-chk-cc-lfs), $(eval $(call enable-lfs,$(t),cc)))
>> +
>>   define link-cc
>>   LDLIBS-$(1) = -lstdc++
>>   endef
>>
>> And then remove the '#define _FILE_OFFSET_BITS 64' so the rule is applied
>> regardless whether the file defines or not.
> 
> While it works for _FILE_OFFSET_BITS, I doubt if it'll work for _GNU_SOURCE since it will get overridden by libc-symbols.h.
> 
> At some point I want to do a cleanup where libc-symbols.h undefines _GNU_SOURCE for tests and only tests that require it, define it explicitly.  Then we can properly auto-generate _FORTIFY_SOURCE tests out of tst-chk-0.c just like we do for mcheck and malloc-check tests.
> 
> May I keep it like this for now and do this additional cleanup later?

Indeed for _GNU_SOURCE it will get overridden, but I think it is still
useful to use with _FILE_OFFSET_BITS.

> 
>>
>>> -tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
>>> -    tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
>>> -    tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
>>> -    tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>>> -    tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
>>> +tests = backtrace-tst tst-longjmp_chk \
>>> +    test-strcpy_chk test-stpcpy_chk \
>>> +    tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>>> +    tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
>>> +    $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
>>
>> Since you are touching, put one file per line.
> 
> OK.
> 
>>
>>>     ifeq ($(have-ssp),yes)
>>>   tests += tst-ssp-1
>>>   endif
>>>     ifeq (,$(CXX))
>>> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
>>> -            tst-lfschk4 tst-lfschk5 tst-lfschk6
>>> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
>>
>> Same.
>>
>>>   endif
>>>     extra-libs = libSegFault libpcprofile
>>> @@ -191,20 +190,12 @@ ifeq ($(run-built-tests),yes)
>>>   LOCALES := de_DE.UTF-8
>>>   include ../gen-locales.mk
>>>   -$(objpfx)tst-chk1.out: $(gen-locales)
>>> -$(objpfx)tst-chk2.out: $(gen-locales)
>>> -$(objpfx)tst-chk3.out: $(gen-locales)
>>> -$(objpfx)tst-chk4.out: $(gen-locales)
>>> -$(objpfx)tst-chk5.out: $(gen-locales)
>>> -$(objpfx)tst-chk6.out: $(gen-locales)
>>> -$(objpfx)tst-chk7.out: $(gen-locales)
>>> -$(objpfx)tst-chk8.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk1.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk2.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk3.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk4.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk5.out: $(gen-locales)
>>> -$(objpfx)tst-lfschk6.out: $(gen-locales)
>>> +define chk-gen-locales
>>> +$(objpfx)$(1).out: $(gen-locales)
>>> +endef
>>> +$(foreach t, \
>>> +      $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
>>> +      $(eval $(call link-cc,$(t))))
>>>   endif
>>>     sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
>>
>> Same.
> 
> OK.
> 
>>
>>> diff --git a/debug/tst-lfschk1.c b/debug/tst-chk-0-cc-lfs.cc
>>> similarity index 55%
>>> rename from debug/tst-lfschk1.c
>>> rename to debug/tst-chk-0-cc-lfs.cc
>>> index f3e6d47d5e..db699b2391 100644
>>> --- a/debug/tst-lfschk1.c
>>> +++ b/debug/tst-chk-0-cc-lfs.cc
>>> @@ -1,2 +1,2 @@
>>>   #define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-0-cc.cc b/debug/tst-chk-0-cc.cc
>>> new file mode 100644
>>> index 0000000000..be76fc69d4
>>> --- /dev/null
>>> +++ b/debug/tst-chk-0-cc.cc
>>> @@ -0,0 +1 @@
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-lfschk2.c b/debug/tst-chk-0-lfs.c
>>> similarity index 55%
>>> rename from debug/tst-lfschk2.c
>>> rename to debug/tst-chk-0-lfs.c
>>> index 95d4db1d32..db699b2391 100644
>>> --- a/debug/tst-lfschk2.c
>>> +++ b/debug/tst-chk-0-lfs.c
>>> @@ -1,2 +1,2 @@
>>>   #define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk2.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk1.c b/debug/tst-chk-0.c
>>> similarity index 100%
>>> rename from debug/tst-chk1.c
>>> rename to debug/tst-chk-0.c
>>> diff --git a/debug/tst-lfschk3.c b/debug/tst-chk-1-cc-lfs.cc
>>> similarity index 55%
>>> rename from debug/tst-lfschk3.c
>>> rename to debug/tst-chk-1-cc-lfs.cc
>>> index 50a1ae1258..0d67643e9e 100644
>>> --- a/debug/tst-lfschk3.c
>>> +++ b/debug/tst-chk-1-cc-lfs.cc
>>> @@ -1,2 +1,2 @@
>>>   #define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk3.c"
>>> +#include "tst-chk-1.c"
>>> diff --git a/debug/tst-chk2.c b/debug/tst-chk-1-cc.cc
>>> similarity index 53%
>>> rename from debug/tst-chk2.c
>>> rename to debug/tst-chk-1-cc.cc
>>> index be37ce2d22..945c1f8d5b 100644
>>> --- a/debug/tst-chk2.c
>>> +++ b/debug/tst-chk-1-cc.cc
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 1
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-lfschk4.cc b/debug/tst-chk-1-lfs.c
>>> similarity index 55%
>>> rename from debug/tst-lfschk4.cc
>>> rename to debug/tst-chk-1-lfs.c
>>> index f3e6d47d5e..0d67643e9e 100644
>>> --- a/debug/tst-lfschk4.cc
>>> +++ b/debug/tst-chk-1-lfs.c
>>> @@ -1,2 +1,2 @@
>>>   #define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-1.c"
>>> diff --git a/debug/tst-chk5.cc b/debug/tst-chk-1.c
>>> similarity index 53%
>>> rename from debug/tst-chk5.cc
>>> rename to debug/tst-chk-1.c
>>> index be37ce2d22..945c1f8d5b 100644
>>> --- a/debug/tst-chk5.cc
>>> +++ b/debug/tst-chk-1.c
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 1
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-2-cc-lfs.cc b/debug/tst-chk-2-cc-lfs.cc
>>> new file mode 100644
>>> index 0000000000..97c83def5c
>>> --- /dev/null
>>> +++ b/debug/tst-chk-2-cc-lfs.cc
>>> @@ -0,0 +1,2 @@
>>> +#define _FILE_OFFSET_BITS 64
>>> +#include "tst-chk-2.c"
>>> diff --git a/debug/tst-chk6.cc b/debug/tst-chk-2-cc.cc
>>> similarity index 53%
>>> rename from debug/tst-chk6.cc
>>> rename to debug/tst-chk-2-cc.cc
>>> index 38b8e4fb36..f178340bbd 100644
>>> --- a/debug/tst-chk6.cc
>>> +++ b/debug/tst-chk-2-cc.cc
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 2
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-2-lfs.c b/debug/tst-chk-2-lfs.c
>>> new file mode 100644
>>> index 0000000000..97c83def5c
>>> --- /dev/null
>>> +++ b/debug/tst-chk-2-lfs.c
>>> @@ -0,0 +1,2 @@
>>> +#define _FILE_OFFSET_BITS 64
>>> +#include "tst-chk-2.c"
>>> diff --git a/debug/tst-chk3.c b/debug/tst-chk-2.c
>>> similarity index 53%
>>> rename from debug/tst-chk3.c
>>> rename to debug/tst-chk-2.c
>>> index 38b8e4fb36..f178340bbd 100644
>>> --- a/debug/tst-chk3.c
>>> +++ b/debug/tst-chk-2.c
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 2
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-3-cc-lfs.cc b/debug/tst-chk-3-cc-lfs.cc
>>> new file mode 100644
>>> index 0000000000..d5d8c28bc7
>>> --- /dev/null
>>> +++ b/debug/tst-chk-3-cc-lfs.cc
>>> @@ -0,0 +1,2 @@
>>> +#define _FILE_OFFSET_BITS 64
>>> +#include "tst-chk-3.c"
>>> diff --git a/debug/tst-chk7.c b/debug/tst-chk-3-cc.cc
>>> similarity index 53%
>>> rename from debug/tst-chk7.c
>>> rename to debug/tst-chk-3-cc.cc
>>> index 2a7b323812..c1ddac1261 100644
>>> --- a/debug/tst-chk7.c
>>> +++ b/debug/tst-chk-3-cc.cc
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 3
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-3-lfs.c b/debug/tst-chk-3-lfs.c
>>> new file mode 100644
>>> index 0000000000..d5d8c28bc7
>>> --- /dev/null
>>> +++ b/debug/tst-chk-3-lfs.c
>>> @@ -0,0 +1,2 @@
>>> +#define _FILE_OFFSET_BITS 64
>>> +#include "tst-chk-3.c"
>>> diff --git a/debug/tst-chk8.cc b/debug/tst-chk-3.c
>>> similarity index 53%
>>> rename from debug/tst-chk8.cc
>>> rename to debug/tst-chk-3.c
>>> index 2a7b323812..c1ddac1261 100644
>>> --- a/debug/tst-chk8.cc
>>> +++ b/debug/tst-chk-3.c
>>> @@ -1,2 +1,2 @@
>>>   #define _FORTIFY_SOURCE 3
>>> -#include "tst-chk1.c"
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
>>> deleted file mode 100644
>>> index c82e6aac86..0000000000
>>> --- a/debug/tst-chk4.cc
>>> +++ /dev/null
>>> @@ -1 +0,0 @@
>>> -#include "tst-chk1.c"
>>> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
>>> deleted file mode 100644
>>> index 95d4db1d32..0000000000
>>> --- a/debug/tst-lfschk5.cc
>>> +++ /dev/null
>>> @@ -1,2 +0,0 @@
>>> -#define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk2.c"
>>> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
>>> deleted file mode 100644
>>> index 50a1ae1258..0000000000
>>> --- a/debug/tst-lfschk6.cc
>>> +++ /dev/null
>>> @@ -1,2 +0,0 @@
>>> -#define _FILE_OFFSET_BITS 64
>>> -#include "tst-chk3.c"
>>
> 
> Thanks,
> Siddhesh

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-07  2:12       ` Siddhesh Poyarekar
@ 2022-01-07 13:02         ` Adhemerval Zanella
  0 siblings, 0 replies; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-07 13:02 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 06/01/2022 23:12, Siddhesh Poyarekar wrote:
> On 07/01/2022 02:45, Adhemerval Zanella wrote:
>>
>>
>> On 05/01/2022 01:45, Siddhesh Poyarekar via Libc-alpha wrote:
>>> Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
>>> main headers as they got incorporated into the standard, but their
>>> fortified variants remained under __USE_GNU.  As a result, these
>>> functions did not get fortified when _GNU_SOURCE was not defined.
>>>
>>> Add test wrappers that check all functions tested in tst-chk0 at all
>>> levels with _GNU_SOURCE undefined and then use the failures to (1)
>>> exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
>>> feature macro guards in the fortified function headers so that they're
>>> the same as the ones in the main headers.
>>>
>>> This fixes BZ #28746.
>>>
>>> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
>>
>> Patch look ok in general, some comments below.
>>
>>> ---
>>>   debug/Makefile                 | 17 +++++++-----
>>>   debug/tst-chk-0-cc-nongnu.cc   |  3 +++
>>>   debug/tst-chk-0-nongnu.c       |  3 +++
>>>   debug/tst-chk-0.c              | 49 ++++++++++++++++++++++++++++------
>>>   debug/tst-chk-1-cc-nongnu.cc   |  3 +++
>>>   debug/tst-chk-1-nongnu.c       |  3 +++
>>>   debug/tst-chk-2-cc-nongnu.cc   |  3 +++
>>>   debug/tst-chk-2-nongnu.c       |  3 +++
>>>   debug/tst-chk-3-cc-nongnu.cc   |  3 +++
>>>   debug/tst-chk-3-nongnu.c       |  3 +++
>>>   posix/bits/unistd.h            |  2 +-
>>>   string/bits/string_fortified.h |  8 +++---
>>>   support/xsignal.h              |  2 ++
>>>   wcsmbs/bits/wchar2.h           |  2 +-
>>>   14 files changed, 85 insertions(+), 19 deletions(-)
>>>   create mode 100644 debug/tst-chk-0-cc-nongnu.cc
>>>   create mode 100644 debug/tst-chk-0-nongnu.c
>>>   create mode 100644 debug/tst-chk-1-cc-nongnu.cc
>>>   create mode 100644 debug/tst-chk-1-nongnu.c
>>>   create mode 100644 debug/tst-chk-2-cc-nongnu.cc
>>>   create mode 100644 debug/tst-chk-2-nongnu.c
>>>   create mode 100644 debug/tst-chk-3-cc-nongnu.cc
>>>   create mode 100644 debug/tst-chk-3-nongnu.c
>>>
>>> diff --git a/debug/Makefile b/debug/Makefile
>>> index 9aa27eb00c..876d10e521 100644
>>> --- a/debug/Makefile
>>> +++ b/debug/Makefile
>>> @@ -116,6 +116,8 @@ tests-chk = $(addprefix tst-chk-, 0 1 2 3)
>>>   tests-chk-cc = $(addsuffix -cc, $(tests-chk))
>>>   tests-chk-lfs = $(addsuffix -lfs, $(tests-chk))
>>>   tests-chk-cc-lfs = $(addsuffix -lfs, $(tests-chk-cc))
>>> +tests-chk-nongnu = $(addsuffix -nongnu, $(tests-chk))
>>> +tests-chk-cc-nongnu = $(addsuffix -nongnu, $(tests-chk-cc))
>>>     # We know these tests have problems with format strings, this is what
>>>   # we are testing.  Disable that warning.  They are also testing
>>> @@ -126,16 +128,17 @@ define disable-warnings
>>>   CFLAGS-$(1).$(2) += -Wno-format -Wno-deprecated-declarations -Wno-error
>>>   endef
>>>   -$(foreach t,$(tests-chk) $(tests-chk-lfs), \
>>> +$(foreach t,$(tests-chk) $(tests-chk-lfs) $(tests-chk-nongnu), \
>>>         $(eval $(call disable-warnings,$(t),c)))
>>>   -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), \
>>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>>>         $(eval $(call disable-warnings,$(t),cc)))
>>>     define link-cc
>>>   LDLIBS-$(1) = -lstdc++
>>>   endef
>>> -$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs), $(eval $(call link-cc,$(t))))
>>> +$(foreach t,$(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu), \
>>> +      $(eval $(call link-cc,$(t))))
>>>     # backtrace_symbols only works if we link with -rdynamic.  backtrace
>>>   # requires unwind tables on most architectures.
>>
>> Ok.
>>
>>> @@ -156,14 +159,15 @@ tests = backtrace-tst tst-longjmp_chk \
>>>       test-strcpy_chk test-stpcpy_chk \
>>>       tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
>>>       tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk \
>>> -    $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs)
>>> +    $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
>>> +    $(tests-chk-nongnu) $(tests-chk-cc-nongnu)
>>>     ifeq ($(have-ssp),yes)
>>>   tests += tst-ssp-1
>>>   endif
>>>     ifeq (,$(CXX))
>>> -tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs)
>>> +tests-unsupported = $(tests-chk-cc) $(tests-chk-cc-lfs) $(tests-chk-cc-nongnu)
>>>   endif
>>>     extra-libs = libSegFault libpcprofile
>>> @@ -194,7 +198,8 @@ define chk-gen-locales
>>>   $(objpfx)$(1).out: $(gen-locales)
>>>   endef
>>>   $(foreach t, \
>>> -      $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs), \
>>> +      $(tests-chk) $(tests-chk-cc) $(tests-chk-lfs) $(tests-chk-cc-lfs) \
>>> +      $(tests-chk-nongnu) $(tests-chk-cc-nongnu), \
>>>         $(eval $(call link-cc,$(t))))
>>>   endif
>>>   
>>
>> Similar to LFS suggestion I think you can simplify it further by using:
>>
>>    define enable-largefile
>>    CFLAGS-$(1).$(2) += -U_GNU_SOURCE -D_LARGEFILE64_SOURCE=1
>>    endef
>>
>>    $(foreach t,$(tests-chk-nongnu), $(eval $(call enable-largefile,$(t),c)))
>>    $(foreach t,$(tests-chk-cc-nongnu), $(eval $(call enable-largefile,$(t),cc)))
>>
>> And remove _LARGEFILE64_SOURCE and _GNU_SOURCE on each new test.
> 
> It won't work for _GNU_SOURCE at the moment since _GNU_SOURCE gets defined again in libc-symbols.h.  May I leave this cleanup for later?  I don't think I can do it in time for 2.35 and it may become too invasive at this stage.

Indeed _GNU_SOURCE won't work, but I think the _LARGEFILE64_SOURCE should be simple
enough (it is a matter to just remove the -U_GNU_SOURCE on the snipper I pasted).

I think the more generic solution better, it makes issues with missing test
flags or configuration less prone to happen.

> 
>>
>>
>>> diff --git a/debug/tst-chk-0-cc-nongnu.cc b/debug/tst-chk-0-cc-nongnu.cc
>>> new file mode 100644
>>> index 0000000000..24b1306e16
>>> --- /dev/null
>>> +++ b/debug/tst-chk-0-cc-nongnu.cc
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-0-nongnu.c b/debug/tst-chk-0-nongnu.c
>>> new file mode 100644
>>> index 0000000000..24b1306e16
>>> --- /dev/null
>>> +++ b/debug/tst-chk-0-nongnu.c
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-0.c"
>>> diff --git a/debug/tst-chk-0.c b/debug/tst-chk-0.c
>>> index 9288610fe6..d65a2fe6e1 100644
>>> --- a/debug/tst-chk-0.c
>>> +++ b/debug/tst-chk-0.c
>>> @@ -1,4 +1,5 @@
>>>   /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
>>> +   Copyright The GNU Toolchain Authors.
>>>      This file is part of the GNU C Library.
>>>        The GNU C Library is free software; you can redistribute it and/or
>>> @@ -36,6 +37,17 @@
>>>   #include <sys/socket.h>
>>>   #include <sys/un.h>
>>>   +#ifndef _GNU_SOURCE
>>> +# define MEMPCPY memcpy
>>> +# define WMEMPCPY wmemcpy
>>> +# define MEMPCPY_RET(x) 0
>>> +# define WMEMPCPY_RET(x) 0
>>> +#else
>>> +# define MEMPCPY mempcpy
>>> +# define WMEMPCPY wmempcpy
>>> +# define MEMPCPY_RET(x) __builtin_strlen (x)
>>> +# define WMEMPCPY_RET(x) wcslen (x)
>>> +#endif
>>>     #define obstack_chunk_alloc malloc
>>>   #define obstack_chunk_free free
>>> @@ -162,7 +174,7 @@ do_test (void)
>>>     if (memcmp (buf, "aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
>>> +  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
>>>         || memcmp (buf, "aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -207,7 +219,7 @@ do_test (void)
>>>     if (memcmp (buf, "aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
>>> +  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
>>>         || memcmp (buf, "aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -266,7 +278,8 @@ do_test (void)
>>>     if (memcmp (a.buf1, "aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
>>> +  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
>>> +      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
>>>         || memcmp (a.buf1, "aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -347,6 +360,7 @@ do_test (void)
>>>     bcopy (buf + 1, buf + 2, l0 + 9);
>>>     CHK_FAIL_END
>>>   +#ifdef _GNU_SOURCE
>>>     CHK_FAIL_START
>>>     p = (char *) mempcpy (buf + 6, "abcde", 5);
>>>     CHK_FAIL_END
>>> @@ -354,6 +368,7 @@ do_test (void)
>>>     CHK_FAIL_START
>>>     p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
>>>     CHK_FAIL_END
>>> +#endif
>>>       CHK_FAIL_START
>>>     memset (buf + 9, 'j', 2);
>>> @@ -464,6 +479,7 @@ do_test (void)
>>>     bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
>>>     CHK_FAIL_END
>>>   +#ifdef _GNU_SOURCE
>>>     CHK_FAIL_START
>>>     p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
>>>     CHK_FAIL_END
>>> @@ -471,6 +487,7 @@ do_test (void)
>>>     CHK_FAIL_START
>>>     p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
>>>     CHK_FAIL_END
>>> +#endif
>>>       CHK_FAIL_START
>>>     memset (a.buf1 + 9, 'j', 2);
>>> @@ -550,7 +567,7 @@ do_test (void)
>>>     if (wmemcmp (wbuf, L"aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
>>> +  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>>>         || wmemcmp (wbuf, L"aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -583,7 +600,8 @@ do_test (void)
>>>     if (wmemcmp (wbuf, L"aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
>>> +  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
>>> +      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>>>         || wmemcmp (wbuf, L"aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -625,7 +643,8 @@ do_test (void)
>>>     if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
>>>       FAIL ();
>>>   -  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
>>> +  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
>>> +      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
>>>         || wmemcmp (wa.buf1, L"aabcdabcde", 10))
>>>       FAIL ();
>>>   @@ -694,6 +713,7 @@ do_test (void)
>>>     wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
>>>     CHK_FAIL_END
>>>   +#ifdef _GNU_SOURCE
>>>     CHK_FAIL_START
>>>     wp = wmempcpy (wbuf + 6, L"abcde", 5);
>>>     CHK_FAIL_END
>>> @@ -701,6 +721,7 @@ do_test (void)
>>>     CHK_FAIL_START
>>>     wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
>>>     CHK_FAIL_END
>>> +#endif
>>>       CHK_FAIL_START
>>>     wmemset (wbuf + 9, L'j', 2);
>>> @@ -768,6 +789,7 @@ do_test (void)
>>>     wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
>>>     CHK_FAIL_END
>>>   +#ifdef _GNU_SOURCE
>>>     CHK_FAIL_START
>>>     wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
>>>     CHK_FAIL_END
>>> @@ -775,6 +797,7 @@ do_test (void)
>>>     CHK_FAIL_START
>>>     wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
>>>     CHK_FAIL_END
>>> +#endif
>>>       CHK_FAIL_START
>>>     wmemset (wa.buf1 + 9, L'j', 2);
>>> @@ -906,6 +929,7 @@ do_test (void)
>>>     if (fprintf (fp, buf2 + 4, str5) != 7)
>>>       FAIL ();
>>>   +#ifdef _GNU_SOURCE
>>>     char *my_ptr = NULL;
>>>     strcpy (buf2 + 2, "%n%s%n");
>>>     /* When the format string is writable and contains %n,
>>> @@ -935,6 +959,7 @@ do_test (void)
>>>     if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
>>>       FAIL ();
>>>     obstack_free (&obs, NULL);
>>> +#endif
>>>       if (freopen (temp_filename, "r", stdin) == NULL)
>>>       {
>>> @@ -982,6 +1007,7 @@ do_test (void)
>>>       rewind (stdin);
>>>   +#ifdef _GNU_SOURCE
>>>     if (fgets_unlocked (buf, buf_size, stdin) != buf
>>>         || memcmp (buf, "abcdefgh\n", 10))
>>>       FAIL ();
>>> @@ -1008,6 +1034,7 @@ do_test (void)
>>>   #endif
>>>       rewind (stdin);
>>> +#endif
>>>       if (fread (buf, 1, buf_size, stdin) != buf_size
>>>         || memcmp (buf, "abcdefgh\nA", 10))
>>> @@ -1578,7 +1605,10 @@ do_test (void)
>>>         ret = 1;
>>>       }
>>>   -  int fd = posix_openpt (O_RDWR);
>>> +  int fd;
>>> +
>>> +#ifdef _GNU_SOURCE
>>> +  fd = posix_openpt (O_RDWR);
>>>     if (fd != -1)
>>>       {
>>>         char enough[1000];
>>> @@ -1594,6 +1624,7 @@ do_test (void)
>>>   #endif
>>>         close (fd);
>>>       }
>>> +#endif
>>>     #if PATH_MAX > 0
>>>     confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
>>> @@ -1711,8 +1742,9 @@ do_test (void)
>>>     poll (fds, l0 + 2, 0);
>>>     CHK_FAIL_END
>>>   #endif
>>> +#ifdef _GNU_SOURCE
>>>     ppoll (fds, 1, NULL, NULL);
>>> -#if __USE_FORTIFY_LEVEL >= 1
>>> +# if __USE_FORTIFY_LEVEL >= 1
>>>     CHK_FAIL_START
>>>     ppoll (fds, 2, NULL, NULL);
>>>     CHK_FAIL_END
>>> @@ -1720,6 +1752,7 @@ do_test (void)
>>>     CHK_FAIL_START
>>>     ppoll (fds, l0 + 2, NULL, NULL);
>>>     CHK_FAIL_END
>>> +# endif
>>>   #endif
>>>       return ret;
>>> diff --git a/debug/tst-chk-1-cc-nongnu.cc b/debug/tst-chk-1-cc-nongnu.cc
>>> new file mode 100644
>>> index 0000000000..cce36ff617
>>> --- /dev/null
>>> +++ b/debug/tst-chk-1-cc-nongnu.cc
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-1.c"
>>> diff --git a/debug/tst-chk-1-nongnu.c b/debug/tst-chk-1-nongnu.c
>>> new file mode 100644
>>> index 0000000000..cce36ff617
>>> --- /dev/null
>>> +++ b/debug/tst-chk-1-nongnu.c
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-1.c"
>>> diff --git a/debug/tst-chk-2-cc-nongnu.cc b/debug/tst-chk-2-cc-nongnu.cc
>>> new file mode 100644
>>> index 0000000000..149af283c4
>>> --- /dev/null
>>> +++ b/debug/tst-chk-2-cc-nongnu.cc
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-2.c"
>>> diff --git a/debug/tst-chk-2-nongnu.c b/debug/tst-chk-2-nongnu.c
>>> new file mode 100644
>>> index 0000000000..149af283c4
>>> --- /dev/null
>>> +++ b/debug/tst-chk-2-nongnu.c
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-2.c"
>>> diff --git a/debug/tst-chk-3-cc-nongnu.cc b/debug/tst-chk-3-cc-nongnu.cc
>>> new file mode 100644
>>> index 0000000000..a306b3290c
>>> --- /dev/null
>>> +++ b/debug/tst-chk-3-cc-nongnu.cc
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-3.c"
>>> diff --git a/debug/tst-chk-3-nongnu.c b/debug/tst-chk-3-nongnu.c
>>> new file mode 100644
>>> index 0000000000..a306b3290c
>>> --- /dev/null
>>> +++ b/debug/tst-chk-3-nongnu.c
>>> @@ -0,0 +1,3 @@
>>> +#undef _GNU_SOURCE
>>> +#define _LARGEFILE64_SOURCE 1
>>> +#include "tst-chk-3.c"
>>> diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
>>> index 01df84b5f9..f53a7a06eb 100644
>>> --- a/posix/bits/unistd.h
>>> +++ b/posix/bits/unistd.h
>>> @@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
>>>                 __fd, __buf, __nbytes);
>>>   }
>>>   -#ifdef __USE_UNIX98
>>> +#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
>>>   extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
>>>                   __off_t __offset, size_t __bufsize)
>>>     __wur __attr_access ((__write_only__, 2, 3));
>>
>> Ok.
>>
>>> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
>>> index 51ad480401..d0188b5ba8 100644
>>> --- a/string/bits/string_fortified.h
>>> +++ b/string/bits/string_fortified.h
>>> @@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
>>>     return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
>>>   }
>>>   -#ifdef __USE_GNU
>>> +#ifdef __USE_XOPEN2K8
>>>   __fortify_function char *
>>>   __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
>>>   {
>>> @@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
>>>                     __glibc_objsize (__dest));
>>>   }
>>>   
>>
>> Ok.
>>
>>> -#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>>> +#if __USE_XOPEN2K8
>>
>> Should it be #ifdef here?
>>
>>> +# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>>>   __fortify_function char *
>>>   __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>>>   {
>>>     return __builtin___stpncpy_chk (__dest, __src, __n,
>>>                     __glibc_objsize (__dest));
>>>   }
>>> -#else
>>> +# else
>>>   extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>>>                   size_t __destlen) __THROW
>>>     __fortified_attr_access ((__write_only__, 1, 3))
>>> @@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>>>       return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
>>>     return __stpncpy_alias (__dest, __src, __n);
>>>   }
>>> +# endif
>>>   #endif
>>>     
>>
>> Ok.
>>
>>> diff --git a/support/xsignal.h b/support/xsignal.h
>>> index 973f532495..d868bb336c 100644
>>> --- a/support/xsignal.h
>>> +++ b/support/xsignal.h
>>> @@ -28,7 +28,9 @@ __BEGIN_DECLS
>>>      terminate the process on error.  */
>>>     void xraise (int sig);
>>> +#ifdef _GNU_SOURCE
>>>   sighandler_t xsignal (int sig, sighandler_t handler);
>>> +#endif
>>>   void xsigaction (int sig, const struct sigaction *newact,
>>>                    struct sigaction *oldact);
>>>   diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
>>> index 25d06d433f..0e017f458b 100644
>>> --- a/wcsmbs/bits/wchar2.h
>>> +++ b/wcsmbs/bits/wchar2.h
>>> @@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>>>   }
>>>     -#ifdef __USE_GNU
>>> +#ifdef    __USE_XOPEN2K8
>>>   extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
>>>                   const char **__restrict __src, size_t __nmc,
>>>                   size_t __len, mbstate_t *__restrict __ps,
>>
> 
> Thanks,
> Siddhesh

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3 0/2] _FORTIFY_SOURCE fixes [BZ #28746]
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
                   ` (2 preceding siblings ...)
  2022-01-05  4:45 ` [PATCH v2 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-10 15:34 ` Siddhesh Poyarekar
  2022-01-10 15:34   ` [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
  2022-01-10 15:34   ` [PATCH v3 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  2022-01-10 16:29 ` [PATCH v4 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  2022-01-12  5:51 ` [PATCH v5 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  5 siblings, 2 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 15:34 UTC (permalink / raw)
  To: libc-alpha

The main patch is 2/2, which fixes some fortified headers to match
feature macros with their corresponding main headers, without which
functions would fail to get fortified.  Patch 1/2 is a restructuring of
the tests so that it is easier to add different configurations to
_FORTIFY_SOURCE tests at various levels.

Tested on x86_64 to confirm that there were no new regressions due to
this and that the new tests also run clean.

Changes from v1:
- Rebased against latest trunk so that the patches actually apply.
- Renamed files so that the diffs are easier to compare and also make a
  bit more sense.

Changes from v2:
- Renamed tst-chk-0.c to tst-fortify.c and autogenerate all tests and
  include it.
- Set all the flags we can as -D in CFLAGS and autogenerate test cases
  if needed.
- Fix other review comments.


Siddhesh Poyarekar (2):
  debug: Autogenerate _FORTIFY_SOURCE tests
  debug: Synchronize feature guards in fortified functions [BZ #28746]

 debug/Makefile                      | 115 ++++++++++++++++++----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/tst-fortify-c-default-1.c     |   3 +
 debug/tst-fortify-c-default-2.c     |   3 +
 debug/tst-fortify-c-default-3.c     |   3 +
 debug/tst-fortify-c-lfs-1.c         |   3 +
 debug/tst-fortify-c-lfs-2.c         |   3 +
 debug/tst-fortify-c-lfs-3.c         |   3 +
 debug/tst-fortify-c-nongnu-1.c      |   3 +
 debug/tst-fortify-c-nongnu-2.c      |   3 +
 debug/tst-fortify-c-nongnu-3.c      |   3 +
 debug/tst-fortify-cc-default-1.cc   |   3 +
 debug/tst-fortify-cc-default-2.cc   |   3 +
 debug/tst-fortify-cc-default-3.cc   |   3 +
 debug/tst-fortify-cc-lfs-1.cc       |   3 +
 debug/tst-fortify-cc-lfs-2.cc       |   3 +
 debug/tst-fortify-cc-lfs-3.cc       |   3 +
 debug/tst-fortify-cc-nongnu-1.cc    |   3 +
 debug/tst-fortify-cc-nongnu-2.cc    |   3 +
 debug/tst-fortify-cc-nongnu-3.cc    |   3 +
 debug/{tst-chk1.c => tst-fortify.c} |  49 ++++++++++--
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 posix/bits/unistd.h                 |   2 +-
 string/bits/string_fortified.h      |   8 +-
 support/xsignal.h                   |   2 +
 wcsmbs/bits/wchar2.h                |   2 +-
 37 files changed, 177 insertions(+), 80 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 create mode 100644 debug/tst-fortify-c-default-1.c
 create mode 100644 debug/tst-fortify-c-default-2.c
 create mode 100644 debug/tst-fortify-c-default-3.c
 create mode 100644 debug/tst-fortify-c-lfs-1.c
 create mode 100644 debug/tst-fortify-c-lfs-2.c
 create mode 100644 debug/tst-fortify-c-lfs-3.c
 create mode 100644 debug/tst-fortify-c-nongnu-1.c
 create mode 100644 debug/tst-fortify-c-nongnu-2.c
 create mode 100644 debug/tst-fortify-c-nongnu-3.c
 create mode 100644 debug/tst-fortify-cc-default-1.cc
 create mode 100644 debug/tst-fortify-cc-default-2.cc
 create mode 100644 debug/tst-fortify-cc-default-3.cc
 create mode 100644 debug/tst-fortify-cc-lfs-1.cc
 create mode 100644 debug/tst-fortify-cc-lfs-2.cc
 create mode 100644 debug/tst-fortify-cc-lfs-3.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-1.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-2.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-3.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (97%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-10 15:34 ` [PATCH v3 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-10 15:34   ` Siddhesh Poyarekar
  2022-01-10 16:30     ` Siddhesh Poyarekar
  2022-01-10 15:34   ` [PATCH v3 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 15:34 UTC (permalink / raw)
  To: libc-alpha

Rename all debug/tst-chk* tests to reflect the fortification level
they're testing and any additional macros so that rules for them can be
autogenerated.  tst-fortify.c is the default test without fortification
and is included in all auto-generated tests.  This allows easier
replication of the tests to check additional macros.

The autogenerated tests get updated with every update to debug/Makefile.

The change also expands the -lfs to include _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                      | 109 +++++++++++++++++-----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/tst-fortify-c-default-1.c     |   3 +
 debug/tst-fortify-c-default-2.c     |   3 +
 debug/tst-fortify-c-default-3.c     |   3 +
 debug/tst-fortify-c-lfs-1.c         |   3 +
 debug/tst-fortify-c-lfs-2.c         |   3 +
 debug/tst-fortify-c-lfs-3.c         |   3 +
 debug/tst-fortify-cc-default-1.cc   |   3 +
 debug/tst-fortify-cc-default-2.cc   |   3 +
 debug/tst-fortify-cc-default-3.cc   |   3 +
 debug/tst-fortify-cc-lfs-1.cc       |   3 +
 debug/tst-fortify-cc-lfs-2.cc       |   3 +
 debug/tst-fortify-cc-lfs-3.cc       |   3 +
 debug/{tst-chk1.c => tst-fortify.c} |   0
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 27 files changed, 103 insertions(+), 67 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 create mode 100644 debug/tst-fortify-c-default-1.c
 create mode 100644 debug/tst-fortify-c-default-2.c
 create mode 100644 debug/tst-fortify-c-default-3.c
 create mode 100644 debug/tst-fortify-c-lfs-1.c
 create mode 100644 debug/tst-fortify-c-lfs-2.c
 create mode 100644 debug/tst-fortify-c-lfs-3.c
 create mode 100644 debug/tst-fortify-cc-default-1.cc
 create mode 100644 debug/tst-fortify-cc-default-2.cc
 create mode 100644 debug/tst-fortify-cc-default-3.cc
 create mode 100644 debug/tst-fortify-cc-lfs-1.cc
 create mode 100644 debug/tst-fortify-cc-lfs-2.cc
 create mode 100644 debug/tst-fortify-cc-lfs-3.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (100%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

diff --git a/debug/Makefile b/debug/Makefile
index 5818d751c9..ebc9c4e459 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -1,4 +1,5 @@
 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
 
+# _FORTIFY_SOURCE tests.
+# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
+# preprocessor conditions based on tst-fortify.c.
+#
+# To add a new test condition, define a cflags-$(cond) make variable to set
+# CFLAGS for the file.
+
+tests-all-chk = tst-fortify
+tests-c-chk =
+tests-cc-chk =
+
+CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
+
+# No additional flags for the default tests.
+define cflags-default
+endef
+
+define cflags-lfs
+CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
+endef
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
 # And they also generate warnings from warning attributes, which
 # cannot be disabled via pragmas, so require -Wno-error to be used.
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-LDLIBS-tst-chk4 = -lstdc++
-LDLIBS-tst-chk5 = -lstdc++
-LDLIBS-tst-chk6 = -lstdc++
-LDLIBS-tst-chk8 = -lstdc++
-LDLIBS-tst-lfschk4 = -lstdc++
-LDLIBS-tst-lfschk5 = -lstdc++
-LDLIBS-tst-lfschk6 = -lstdc++
+define gen-chk-test
+tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
+CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
+					  -Wno-deprecated-declarations \
+					  -Wno-error
+$(eval $(call cflags-$(2),$(1),$(3)))
+tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
+	( echo "/* Autogenerated from Makefile.  */"; \
+	  echo ""; \
+	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
+	mv $$@.tmp $$@
+endef
+
+chk-extensions = c cc
+chk-types = default lfs
+chk-levels = 1 2 3
+
+$(foreach e,$(chk-extensions), \
+  $(foreach t,$(chk-types), \
+    $(foreach l,$(chk-levels), \
+      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
+
+tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
+
+define link-cc
+LDLIBS-$(1) = -lstdc++
+endef
+$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
 
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
+tests = backtrace-tst \
+	tst-longjmp_chk \
+	test-strcpy_chk \
+	test-stpcpy_chk \
+	tst-longjmp_chk2 \
+	tst-backtrace2 \
+	tst-backtrace3 \
+	tst-backtrace4 \
+	tst-backtrace5 \
+	tst-backtrace6 \
+	tst-realpath-chk \
+	$(tests-all-chk)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
+tests-unsupported = $(tests-cc-chk)
 endif
 
 extra-libs = libpcprofile
@@ -185,20 +220,10 @@ ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
 
-$(objpfx)tst-chk1.out: $(gen-locales)
-$(objpfx)tst-chk2.out: $(gen-locales)
-$(objpfx)tst-chk3.out: $(gen-locales)
-$(objpfx)tst-chk4.out: $(gen-locales)
-$(objpfx)tst-chk5.out: $(gen-locales)
-$(objpfx)tst-chk6.out: $(gen-locales)
-$(objpfx)tst-chk7.out: $(gen-locales)
-$(objpfx)tst-chk8.out: $(gen-locales)
-$(objpfx)tst-lfschk1.out: $(gen-locales)
-$(objpfx)tst-lfschk2.out: $(gen-locales)
-$(objpfx)tst-lfschk3.out: $(gen-locales)
-$(objpfx)tst-lfschk4.out: $(gen-locales)
-$(objpfx)tst-lfschk5.out: $(gen-locales)
-$(objpfx)tst-lfschk6.out: $(gen-locales)
+define chk-gen-locales
+$(objpfx)$(1).out: $(gen-locales)
+endef
+$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locals,$(t))))
 endif
 
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
deleted file mode 100644
index c82e6aac86..0000000000
--- a/debug/tst-chk4.cc
+++ /dev/null
@@ -1 +0,0 @@
-#include "tst-chk1.c"
diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk7.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk8.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
similarity index 100%
rename from debug/tst-chk1.c
rename to debug/tst-fortify.c
diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk1.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk4.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-10 15:34 ` [PATCH v3 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  2022-01-10 15:34   ` [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-10 15:34   ` Siddhesh Poyarekar
  1 sibling, 0 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 15:34 UTC (permalink / raw)
  To: libc-alpha

Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
main headers as they got incorporated into the standard, but their
fortified variants remained under __USE_GNU.  As a result, these
functions did not get fortified when _GNU_SOURCE was not defined.

Add test wrappers that check all functions tested in tst-chk0 at all
levels with _GNU_SOURCE undefined and then use the failures to (1)
exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
feature macro guards in the fortified function headers so that they're
the same as the ones in the main headers.

This fixes BZ #28746.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                   | 10 +++++--
 debug/tst-fortify-c-nongnu-1.c   |  3 ++
 debug/tst-fortify-c-nongnu-2.c   |  3 ++
 debug/tst-fortify-c-nongnu-3.c   |  3 ++
 debug/tst-fortify-cc-nongnu-1.cc |  3 ++
 debug/tst-fortify-cc-nongnu-2.cc |  3 ++
 debug/tst-fortify-cc-nongnu-3.cc |  3 ++
 debug/tst-fortify.c              | 49 ++++++++++++++++++++++++++------
 posix/bits/unistd.h              |  2 +-
 string/bits/string_fortified.h   |  8 ++++--
 support/xsignal.h                |  2 ++
 wcsmbs/bits/wchar2.h             |  2 +-
 12 files changed, 76 insertions(+), 15 deletions(-)
 create mode 100644 debug/tst-fortify-c-nongnu-1.c
 create mode 100644 debug/tst-fortify-c-nongnu-2.c
 create mode 100644 debug/tst-fortify-c-nongnu-3.c
 create mode 100644 debug/tst-fortify-cc-nongnu-1.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-2.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-3.cc

diff --git a/debug/Makefile b/debug/Makefile
index ebc9c4e459..388c5eb506 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -132,6 +132,12 @@ define cflags-lfs
 CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
 endef
 
+define cflags-nongnu
+CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
+endef
+
+src-chk-nongnu = \#undef _GNU_SOURCE
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
@@ -145,13 +151,13 @@ CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
 $(eval $(call cflags-$(2),$(1),$(3)))
 tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
 	( echo "/* Autogenerated from Makefile.  */"; \
-	  echo ""; \
+	  echo "$(src-chk-$(2))"; \
 	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
 	mv $$@.tmp $$@
 endef
 
 chk-extensions = c cc
-chk-types = default lfs
+chk-types = default lfs nongnu
 chk-levels = 1 2 3
 
 $(foreach e,$(chk-extensions), \
diff --git a/debug/tst-fortify-c-nongnu-1.c b/debug/tst-fortify-c-nongnu-1.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-2.c b/debug/tst-fortify-c-nongnu-2.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-3.c b/debug/tst-fortify-c-nongnu-3.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-1.cc b/debug/tst-fortify-cc-nongnu-1.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-2.cc b/debug/tst-fortify-cc-nongnu-2.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-3.cc b/debug/tst-fortify-cc-nongnu-3.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
index 9288610fe6..d65a2fe6e1 100644
--- a/debug/tst-fortify.c
+++ b/debug/tst-fortify.c
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -36,6 +37,17 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#ifndef _GNU_SOURCE
+# define MEMPCPY memcpy
+# define WMEMPCPY wmemcpy
+# define MEMPCPY_RET(x) 0
+# define WMEMPCPY_RET(x) 0
+#else
+# define MEMPCPY mempcpy
+# define WMEMPCPY wmempcpy
+# define MEMPCPY_RET(x) __builtin_strlen (x)
+# define WMEMPCPY_RET(x) wcslen (x)
+#endif
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
@@ -162,7 +174,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -207,7 +219,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -266,7 +278,8 @@ do_test (void)
   if (memcmp (a.buf1, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
       || memcmp (a.buf1, "aabcdabcde", 10))
     FAIL ();
 
@@ -347,6 +360,7 @@ do_test (void)
   bcopy (buf + 1, buf + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", 5);
   CHK_FAIL_END
@@ -354,6 +368,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (buf + 9, 'j', 2);
@@ -464,6 +479,7 @@ do_test (void)
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
   CHK_FAIL_END
@@ -471,6 +487,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (a.buf1 + 9, 'j', 2);
@@ -550,7 +567,7 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -583,7 +600,8 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -625,7 +643,8 @@ do_test (void)
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
     FAIL ();
 
@@ -694,6 +713,7 @@ do_test (void)
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -701,6 +721,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wbuf + 9, L'j', 2);
@@ -768,6 +789,7 @@ do_test (void)
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -775,6 +797,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wa.buf1 + 9, L'j', 2);
@@ -906,6 +929,7 @@ do_test (void)
   if (fprintf (fp, buf2 + 4, str5) != 7)
     FAIL ();
 
+#ifdef _GNU_SOURCE
   char *my_ptr = NULL;
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
@@ -935,6 +959,7 @@ do_test (void)
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
     FAIL ();
   obstack_free (&obs, NULL);
+#endif
 
   if (freopen (temp_filename, "r", stdin) == NULL)
     {
@@ -982,6 +1007,7 @@ do_test (void)
 
   rewind (stdin);
 
+#ifdef _GNU_SOURCE
   if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
@@ -1008,6 +1034,7 @@ do_test (void)
 #endif
 
   rewind (stdin);
+#endif
 
   if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
@@ -1578,7 +1605,10 @@ do_test (void)
       ret = 1;
     }
 
-  int fd = posix_openpt (O_RDWR);
+  int fd;
+
+#ifdef _GNU_SOURCE
+  fd = posix_openpt (O_RDWR);
   if (fd != -1)
     {
       char enough[1000];
@@ -1594,6 +1624,7 @@ do_test (void)
 #endif
       close (fd);
     }
+#endif
 
 #if PATH_MAX > 0
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
@@ -1711,8 +1742,9 @@ do_test (void)
   poll (fds, l0 + 2, 0);
   CHK_FAIL_END
 #endif
+#ifdef _GNU_SOURCE
   ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
+# if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
   ppoll (fds, 2, NULL, NULL);
   CHK_FAIL_END
@@ -1720,6 +1752,7 @@ do_test (void)
   CHK_FAIL_START
   ppoll (fds, l0 + 2, NULL, NULL);
   CHK_FAIL_END
+# endif
 #endif
 
   return ret;
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 01df84b5f9..f53a7a06eb 100644
--- a/posix/bits/unistd.h
+++ b/posix/bits/unistd.h
@@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
 			  __fd, __buf, __nbytes);
 }
 
-#ifdef __USE_UNIX98
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
 			    __off_t __offset, size_t __bufsize)
   __wur __attr_access ((__write_only__, 2, 3));
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 51ad480401..f4a5dfc2e5 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
 }
 
-#ifdef __USE_GNU
+#ifdef __USE_XOPEN2K8
 __fortify_function char *
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
 {
@@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
 				  __glibc_objsize (__dest));
 }
 
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+#ifdef __USE_XOPEN2K8
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
   return __builtin___stpncpy_chk (__dest, __src, __n,
 				  __glibc_objsize (__dest));
 }
-#else
+# else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __fortified_attr_access ((__write_only__, 1, 3))
@@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+# endif
 #endif
 
 
diff --git a/support/xsignal.h b/support/xsignal.h
index 973f532495..d868bb336c 100644
--- a/support/xsignal.h
+++ b/support/xsignal.h
@@ -28,7 +28,9 @@ __BEGIN_DECLS
    terminate the process on error.  */
 
 void xraise (int sig);
+#ifdef _GNU_SOURCE
 sighandler_t xsignal (int sig, sighandler_t handler);
+#endif
 void xsigaction (int sig, const struct sigaction *newact,
                  struct sigaction *oldact);
 
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index 25d06d433f..0e017f458b 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 }
 
 
-#ifdef __USE_GNU
+#ifdef	__USE_XOPEN2K8
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
 				const char **__restrict __src, size_t __nmc,
 				size_t __len, mbstate_t *__restrict __ps,
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v4 0/2] _FORTIFY_SOURCE fixes [BZ #28746]
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
                   ` (3 preceding siblings ...)
  2022-01-10 15:34 ` [PATCH v3 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-10 16:29 ` Siddhesh Poyarekar
  2022-01-10 16:29   ` [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
  2022-01-10 16:29   ` [PATCH v4 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  2022-01-12  5:51 ` [PATCH v5 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  5 siblings, 2 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 16:29 UTC (permalink / raw)
  To: libc-alpha

The main patch is 2/2, which fixes some fortified headers to match
feature macros with their corresponding main headers, without which
functions would fail to get fortified.  Patch 1/2 is a restructuring of
the tests so that it is easier to add different configurations to
_FORTIFY_SOURCE tests at various levels.

Tested on x86_64 to confirm that there were no new regressions due to
this and that the new tests also run clean.

Changes from v1:
- Rebased against latest trunk so that the patches actually apply.
- Renamed files so that the diffs are easier to compare and also make a
  bit more sense.

Changes from v2:
- Renamed tst-chk-0.c to tst-fortify.c and autogenerate all tests and
  include it.
- Set all the flags we can as -D in CFLAGS and autogenerate test cases
  if needed.
- Fix other review comments.

Changes from v3:
- Fixed typo (chk-gen-locals -> chk-gen-locales)

Siddhesh Poyarekar (2):
  debug: Autogenerate _FORTIFY_SOURCE tests
  debug: Synchronize feature guards in fortified functions [BZ #28746]

 debug/Makefile                      | 115 ++++++++++++++++++----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/tst-fortify-c-default-1.c     |   3 +
 debug/tst-fortify-c-default-2.c     |   3 +
 debug/tst-fortify-c-default-3.c     |   3 +
 debug/tst-fortify-c-lfs-1.c         |   3 +
 debug/tst-fortify-c-lfs-2.c         |   3 +
 debug/tst-fortify-c-lfs-3.c         |   3 +
 debug/tst-fortify-c-nongnu-1.c      |   3 +
 debug/tst-fortify-c-nongnu-2.c      |   3 +
 debug/tst-fortify-c-nongnu-3.c      |   3 +
 debug/tst-fortify-cc-default-1.cc   |   3 +
 debug/tst-fortify-cc-default-2.cc   |   3 +
 debug/tst-fortify-cc-default-3.cc   |   3 +
 debug/tst-fortify-cc-lfs-1.cc       |   3 +
 debug/tst-fortify-cc-lfs-2.cc       |   3 +
 debug/tst-fortify-cc-lfs-3.cc       |   3 +
 debug/tst-fortify-cc-nongnu-1.cc    |   3 +
 debug/tst-fortify-cc-nongnu-2.cc    |   3 +
 debug/tst-fortify-cc-nongnu-3.cc    |   3 +
 debug/{tst-chk1.c => tst-fortify.c} |  49 ++++++++++--
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 posix/bits/unistd.h                 |   2 +-
 string/bits/string_fortified.h      |   8 +-
 support/xsignal.h                   |   2 +
 wcsmbs/bits/wchar2.h                |   2 +-
 37 files changed, 177 insertions(+), 80 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 create mode 100644 debug/tst-fortify-c-default-1.c
 create mode 100644 debug/tst-fortify-c-default-2.c
 create mode 100644 debug/tst-fortify-c-default-3.c
 create mode 100644 debug/tst-fortify-c-lfs-1.c
 create mode 100644 debug/tst-fortify-c-lfs-2.c
 create mode 100644 debug/tst-fortify-c-lfs-3.c
 create mode 100644 debug/tst-fortify-c-nongnu-1.c
 create mode 100644 debug/tst-fortify-c-nongnu-2.c
 create mode 100644 debug/tst-fortify-c-nongnu-3.c
 create mode 100644 debug/tst-fortify-cc-default-1.cc
 create mode 100644 debug/tst-fortify-cc-default-2.cc
 create mode 100644 debug/tst-fortify-cc-default-3.cc
 create mode 100644 debug/tst-fortify-cc-lfs-1.cc
 create mode 100644 debug/tst-fortify-cc-lfs-2.cc
 create mode 100644 debug/tst-fortify-cc-lfs-3.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-1.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-2.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-3.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (97%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-10 16:29 ` [PATCH v4 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-10 16:29   ` Siddhesh Poyarekar
  2022-01-11 13:27     ` Adhemerval Zanella
  2022-01-10 16:29   ` [PATCH v4 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 16:29 UTC (permalink / raw)
  To: libc-alpha

Rename all debug/tst-chk* tests to reflect the fortification level
they're testing and any additional macros so that rules for them can be
autogenerated.  tst-fortify.c is the default test without fortification
and is included in all auto-generated tests.  This allows easier
replication of the tests to check additional macros.

The autogenerated tests get updated with every update to debug/Makefile.

The change also expands the -lfs to include _FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                      | 109 +++++++++++++++++-----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/tst-fortify-c-default-1.c     |   3 +
 debug/tst-fortify-c-default-2.c     |   3 +
 debug/tst-fortify-c-default-3.c     |   3 +
 debug/tst-fortify-c-lfs-1.c         |   3 +
 debug/tst-fortify-c-lfs-2.c         |   3 +
 debug/tst-fortify-c-lfs-3.c         |   3 +
 debug/tst-fortify-cc-default-1.cc   |   3 +
 debug/tst-fortify-cc-default-2.cc   |   3 +
 debug/tst-fortify-cc-default-3.cc   |   3 +
 debug/tst-fortify-cc-lfs-1.cc       |   3 +
 debug/tst-fortify-cc-lfs-2.cc       |   3 +
 debug/tst-fortify-cc-lfs-3.cc       |   3 +
 debug/{tst-chk1.c => tst-fortify.c} |   0
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 27 files changed, 103 insertions(+), 67 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 create mode 100644 debug/tst-fortify-c-default-1.c
 create mode 100644 debug/tst-fortify-c-default-2.c
 create mode 100644 debug/tst-fortify-c-default-3.c
 create mode 100644 debug/tst-fortify-c-lfs-1.c
 create mode 100644 debug/tst-fortify-c-lfs-2.c
 create mode 100644 debug/tst-fortify-c-lfs-3.c
 create mode 100644 debug/tst-fortify-cc-default-1.cc
 create mode 100644 debug/tst-fortify-cc-default-2.cc
 create mode 100644 debug/tst-fortify-cc-default-3.cc
 create mode 100644 debug/tst-fortify-cc-lfs-1.cc
 create mode 100644 debug/tst-fortify-cc-lfs-2.cc
 create mode 100644 debug/tst-fortify-cc-lfs-3.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (100%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

diff --git a/debug/Makefile b/debug/Makefile
index 5818d751c9..15359da99f 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -1,4 +1,5 @@
 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
 
+# _FORTIFY_SOURCE tests.
+# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
+# preprocessor conditions based on tst-fortify.c.
+#
+# To add a new test condition, define a cflags-$(cond) make variable to set
+# CFLAGS for the file.
+
+tests-all-chk = tst-fortify
+tests-c-chk =
+tests-cc-chk =
+
+CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
+
+# No additional flags for the default tests.
+define cflags-default
+endef
+
+define cflags-lfs
+CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
+endef
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
 # And they also generate warnings from warning attributes, which
 # cannot be disabled via pragmas, so require -Wno-error to be used.
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-LDLIBS-tst-chk4 = -lstdc++
-LDLIBS-tst-chk5 = -lstdc++
-LDLIBS-tst-chk6 = -lstdc++
-LDLIBS-tst-chk8 = -lstdc++
-LDLIBS-tst-lfschk4 = -lstdc++
-LDLIBS-tst-lfschk5 = -lstdc++
-LDLIBS-tst-lfschk6 = -lstdc++
+define gen-chk-test
+tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
+CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
+					  -Wno-deprecated-declarations \
+					  -Wno-error
+$(eval $(call cflags-$(2),$(1),$(3)))
+tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
+	( echo "/* Autogenerated from Makefile.  */"; \
+	  echo ""; \
+	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
+	mv $$@.tmp $$@
+endef
+
+chk-extensions = c cc
+chk-types = default lfs
+chk-levels = 1 2 3
+
+$(foreach e,$(chk-extensions), \
+  $(foreach t,$(chk-types), \
+    $(foreach l,$(chk-levels), \
+      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
+
+tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
+
+define link-cc
+LDLIBS-$(1) = -lstdc++
+endef
+$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
 
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
+tests = backtrace-tst \
+	tst-longjmp_chk \
+	test-strcpy_chk \
+	test-stpcpy_chk \
+	tst-longjmp_chk2 \
+	tst-backtrace2 \
+	tst-backtrace3 \
+	tst-backtrace4 \
+	tst-backtrace5 \
+	tst-backtrace6 \
+	tst-realpath-chk \
+	$(tests-all-chk)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
+tests-unsupported = $(tests-cc-chk)
 endif
 
 extra-libs = libpcprofile
@@ -185,20 +220,10 @@ ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
 
-$(objpfx)tst-chk1.out: $(gen-locales)
-$(objpfx)tst-chk2.out: $(gen-locales)
-$(objpfx)tst-chk3.out: $(gen-locales)
-$(objpfx)tst-chk4.out: $(gen-locales)
-$(objpfx)tst-chk5.out: $(gen-locales)
-$(objpfx)tst-chk6.out: $(gen-locales)
-$(objpfx)tst-chk7.out: $(gen-locales)
-$(objpfx)tst-chk8.out: $(gen-locales)
-$(objpfx)tst-lfschk1.out: $(gen-locales)
-$(objpfx)tst-lfschk2.out: $(gen-locales)
-$(objpfx)tst-lfschk3.out: $(gen-locales)
-$(objpfx)tst-lfschk4.out: $(gen-locales)
-$(objpfx)tst-lfschk5.out: $(gen-locales)
-$(objpfx)tst-lfschk6.out: $(gen-locales)
+define chk-gen-locales
+$(objpfx)$(1).out: $(gen-locales)
+endef
+$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
 endif
 
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
deleted file mode 100644
index c82e6aac86..0000000000
--- a/debug/tst-chk4.cc
+++ /dev/null
@@ -1 +0,0 @@
-#include "tst-chk1.c"
diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk7.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk8.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-default-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-c-lfs-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-default-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
new file mode 100644
index 0000000000..08a5a758bb
--- /dev/null
+++ b/debug/tst-fortify-cc-lfs-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+
+#include "tst-fortify.c"
diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
similarity index 100%
rename from debug/tst-chk1.c
rename to debug/tst-fortify.c
diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk1.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk4.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v4 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-10 16:29 ` [PATCH v4 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  2022-01-10 16:29   ` [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-10 16:29   ` Siddhesh Poyarekar
  1 sibling, 0 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 16:29 UTC (permalink / raw)
  To: libc-alpha

Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
main headers as they got incorporated into the standard, but their
fortified variants remained under __USE_GNU.  As a result, these
functions did not get fortified when _GNU_SOURCE was not defined.

Add test wrappers that check all functions tested in tst-chk0 at all
levels with _GNU_SOURCE undefined and then use the failures to (1)
exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
feature macro guards in the fortified function headers so that they're
the same as the ones in the main headers.

This fixes BZ #28746.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                   | 10 +++++--
 debug/tst-fortify-c-nongnu-1.c   |  3 ++
 debug/tst-fortify-c-nongnu-2.c   |  3 ++
 debug/tst-fortify-c-nongnu-3.c   |  3 ++
 debug/tst-fortify-cc-nongnu-1.cc |  3 ++
 debug/tst-fortify-cc-nongnu-2.cc |  3 ++
 debug/tst-fortify-cc-nongnu-3.cc |  3 ++
 debug/tst-fortify.c              | 49 ++++++++++++++++++++++++++------
 posix/bits/unistd.h              |  2 +-
 string/bits/string_fortified.h   |  8 ++++--
 support/xsignal.h                |  2 ++
 wcsmbs/bits/wchar2.h             |  2 +-
 12 files changed, 76 insertions(+), 15 deletions(-)
 create mode 100644 debug/tst-fortify-c-nongnu-1.c
 create mode 100644 debug/tst-fortify-c-nongnu-2.c
 create mode 100644 debug/tst-fortify-c-nongnu-3.c
 create mode 100644 debug/tst-fortify-cc-nongnu-1.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-2.cc
 create mode 100644 debug/tst-fortify-cc-nongnu-3.cc

diff --git a/debug/Makefile b/debug/Makefile
index 15359da99f..b5e13eec94 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -132,6 +132,12 @@ define cflags-lfs
 CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
 endef
 
+define cflags-nongnu
+CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
+endef
+
+src-chk-nongnu = \#undef _GNU_SOURCE
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
@@ -145,13 +151,13 @@ CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
 $(eval $(call cflags-$(2),$(1),$(3)))
 tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
 	( echo "/* Autogenerated from Makefile.  */"; \
-	  echo ""; \
+	  echo "$(src-chk-$(2))"; \
 	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
 	mv $$@.tmp $$@
 endef
 
 chk-extensions = c cc
-chk-types = default lfs
+chk-types = default lfs nongnu
 chk-levels = 1 2 3
 
 $(foreach e,$(chk-extensions), \
diff --git a/debug/tst-fortify-c-nongnu-1.c b/debug/tst-fortify-c-nongnu-1.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-1.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-2.c b/debug/tst-fortify-c-nongnu-2.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-2.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-3.c b/debug/tst-fortify-c-nongnu-3.c
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-c-nongnu-3.c
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-1.cc b/debug/tst-fortify-cc-nongnu-1.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-1.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-2.cc b/debug/tst-fortify-cc-nongnu-2.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-2.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-3.cc b/debug/tst-fortify-cc-nongnu-3.cc
new file mode 100644
index 0000000000..5266984fa3
--- /dev/null
+++ b/debug/tst-fortify-cc-nongnu-3.cc
@@ -0,0 +1,3 @@
+/* Autogenerated from Makefile.  */
+#undef _GNU_SOURCE
+#include "tst-fortify.c"
diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
index 9288610fe6..d65a2fe6e1 100644
--- a/debug/tst-fortify.c
+++ b/debug/tst-fortify.c
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -36,6 +37,17 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#ifndef _GNU_SOURCE
+# define MEMPCPY memcpy
+# define WMEMPCPY wmemcpy
+# define MEMPCPY_RET(x) 0
+# define WMEMPCPY_RET(x) 0
+#else
+# define MEMPCPY mempcpy
+# define WMEMPCPY wmempcpy
+# define MEMPCPY_RET(x) __builtin_strlen (x)
+# define WMEMPCPY_RET(x) wcslen (x)
+#endif
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
@@ -162,7 +174,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -207,7 +219,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -266,7 +278,8 @@ do_test (void)
   if (memcmp (a.buf1, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
       || memcmp (a.buf1, "aabcdabcde", 10))
     FAIL ();
 
@@ -347,6 +360,7 @@ do_test (void)
   bcopy (buf + 1, buf + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", 5);
   CHK_FAIL_END
@@ -354,6 +368,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (buf + 9, 'j', 2);
@@ -464,6 +479,7 @@ do_test (void)
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
   CHK_FAIL_END
@@ -471,6 +487,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (a.buf1 + 9, 'j', 2);
@@ -550,7 +567,7 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -583,7 +600,8 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -625,7 +643,8 @@ do_test (void)
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
     FAIL ();
 
@@ -694,6 +713,7 @@ do_test (void)
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -701,6 +721,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wbuf + 9, L'j', 2);
@@ -768,6 +789,7 @@ do_test (void)
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -775,6 +797,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wa.buf1 + 9, L'j', 2);
@@ -906,6 +929,7 @@ do_test (void)
   if (fprintf (fp, buf2 + 4, str5) != 7)
     FAIL ();
 
+#ifdef _GNU_SOURCE
   char *my_ptr = NULL;
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
@@ -935,6 +959,7 @@ do_test (void)
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
     FAIL ();
   obstack_free (&obs, NULL);
+#endif
 
   if (freopen (temp_filename, "r", stdin) == NULL)
     {
@@ -982,6 +1007,7 @@ do_test (void)
 
   rewind (stdin);
 
+#ifdef _GNU_SOURCE
   if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
@@ -1008,6 +1034,7 @@ do_test (void)
 #endif
 
   rewind (stdin);
+#endif
 
   if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
@@ -1578,7 +1605,10 @@ do_test (void)
       ret = 1;
     }
 
-  int fd = posix_openpt (O_RDWR);
+  int fd;
+
+#ifdef _GNU_SOURCE
+  fd = posix_openpt (O_RDWR);
   if (fd != -1)
     {
       char enough[1000];
@@ -1594,6 +1624,7 @@ do_test (void)
 #endif
       close (fd);
     }
+#endif
 
 #if PATH_MAX > 0
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
@@ -1711,8 +1742,9 @@ do_test (void)
   poll (fds, l0 + 2, 0);
   CHK_FAIL_END
 #endif
+#ifdef _GNU_SOURCE
   ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
+# if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
   ppoll (fds, 2, NULL, NULL);
   CHK_FAIL_END
@@ -1720,6 +1752,7 @@ do_test (void)
   CHK_FAIL_START
   ppoll (fds, l0 + 2, NULL, NULL);
   CHK_FAIL_END
+# endif
 #endif
 
   return ret;
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 01df84b5f9..f53a7a06eb 100644
--- a/posix/bits/unistd.h
+++ b/posix/bits/unistd.h
@@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
 			  __fd, __buf, __nbytes);
 }
 
-#ifdef __USE_UNIX98
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
 			    __off_t __offset, size_t __bufsize)
   __wur __attr_access ((__write_only__, 2, 3));
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 51ad480401..f4a5dfc2e5 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
 }
 
-#ifdef __USE_GNU
+#ifdef __USE_XOPEN2K8
 __fortify_function char *
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
 {
@@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
 				  __glibc_objsize (__dest));
 }
 
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+#ifdef __USE_XOPEN2K8
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
   return __builtin___stpncpy_chk (__dest, __src, __n,
 				  __glibc_objsize (__dest));
 }
-#else
+# else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __fortified_attr_access ((__write_only__, 1, 3))
@@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+# endif
 #endif
 
 
diff --git a/support/xsignal.h b/support/xsignal.h
index 973f532495..d868bb336c 100644
--- a/support/xsignal.h
+++ b/support/xsignal.h
@@ -28,7 +28,9 @@ __BEGIN_DECLS
    terminate the process on error.  */
 
 void xraise (int sig);
+#ifdef _GNU_SOURCE
 sighandler_t xsignal (int sig, sighandler_t handler);
+#endif
 void xsigaction (int sig, const struct sigaction *newact,
                  struct sigaction *oldact);
 
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index 25d06d433f..0e017f458b 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 }
 
 
-#ifdef __USE_GNU
+#ifdef	__USE_XOPEN2K8
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
 				const char **__restrict __src, size_t __nmc,
 				size_t __len, mbstate_t *__restrict __ps,
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-10 15:34   ` [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-10 16:30     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-10 16:30 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha

On 10/01/2022 21:04, Siddhesh Poyarekar via Libc-alpha wrote:
> +define chk-gen-locales
> +$(objpfx)$(1).out: $(gen-locales)
> +endef
> +$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locals,$(t))))

Typo, sorry.  I've sent v4.

Siddhesh

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-10 16:29   ` [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-11 13:27     ` Adhemerval Zanella
  2022-01-11 15:28       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-11 13:27 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 10/01/2022 13:29, Siddhesh Poyarekar wrote:
> Rename all debug/tst-chk* tests to reflect the fortification level
> they're testing and any additional macros so that rules for them can be
> autogenerated.  tst-fortify.c is the default test without fortification
> and is included in all auto-generated tests.  This allows easier
> replication of the tests to check additional macros.
> 
> The autogenerated tests get updated with every update to debug/Makefile.
> 
> The change also expands the -lfs to include _FORTIFY_SOURCE=3.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> ---
>  debug/Makefile                      | 109 +++++++++++++++++-----------
>  debug/tst-chk2.c                    |   2 -
>  debug/tst-chk3.c                    |   2 -
>  debug/tst-chk4.cc                   |   1 -
>  debug/tst-chk5.cc                   |   2 -
>  debug/tst-chk6.cc                   |   2 -
>  debug/tst-chk7.c                    |   2 -
>  debug/tst-chk8.cc                   |   2 -
>  debug/tst-fortify-c-default-1.c     |   3 +
>  debug/tst-fortify-c-default-2.c     |   3 +
>  debug/tst-fortify-c-default-3.c     |   3 +
>  debug/tst-fortify-c-lfs-1.c         |   3 +
>  debug/tst-fortify-c-lfs-2.c         |   3 +
>  debug/tst-fortify-c-lfs-3.c         |   3 +
>  debug/tst-fortify-cc-default-1.cc   |   3 +
>  debug/tst-fortify-cc-default-2.cc   |   3 +
>  debug/tst-fortify-cc-default-3.cc   |   3 +
>  debug/tst-fortify-cc-lfs-1.cc       |   3 +
>  debug/tst-fortify-cc-lfs-2.cc       |   3 +
>  debug/tst-fortify-cc-lfs-3.cc       |   3 +
>  debug/{tst-chk1.c => tst-fortify.c} |   0
>  debug/tst-lfschk1.c                 |   2 -
>  debug/tst-lfschk2.c                 |   2 -
>  debug/tst-lfschk3.c                 |   2 -
>  debug/tst-lfschk4.cc                |   2 -
>  debug/tst-lfschk5.cc                |   2 -
>  debug/tst-lfschk6.cc                |   2 -
>  27 files changed, 103 insertions(+), 67 deletions(-)
>  delete mode 100644 debug/tst-chk2.c
>  delete mode 100644 debug/tst-chk3.c
>  delete mode 100644 debug/tst-chk4.cc
>  delete mode 100644 debug/tst-chk5.cc
>  delete mode 100644 debug/tst-chk6.cc
>  delete mode 100644 debug/tst-chk7.c
>  delete mode 100644 debug/tst-chk8.cc
>  create mode 100644 debug/tst-fortify-c-default-1.c
>  create mode 100644 debug/tst-fortify-c-default-2.c
>  create mode 100644 debug/tst-fortify-c-default-3.c
>  create mode 100644 debug/tst-fortify-c-lfs-1.c
>  create mode 100644 debug/tst-fortify-c-lfs-2.c
>  create mode 100644 debug/tst-fortify-c-lfs-3.c
>  create mode 100644 debug/tst-fortify-cc-default-1.cc
>  create mode 100644 debug/tst-fortify-cc-default-2.cc
>  create mode 100644 debug/tst-fortify-cc-default-3.cc
>  create mode 100644 debug/tst-fortify-cc-lfs-1.cc
>  create mode 100644 debug/tst-fortify-cc-lfs-2.cc
>  create mode 100644 debug/tst-fortify-cc-lfs-3.cc
>  rename debug/{tst-chk1.c => tst-fortify.c} (100%)
>  delete mode 100644 debug/tst-lfschk1.c
>  delete mode 100644 debug/tst-lfschk2.c
>  delete mode 100644 debug/tst-lfschk3.c
>  delete mode 100644 debug/tst-lfschk4.cc
>  delete mode 100644 debug/tst-lfschk5.cc
>  delete mode 100644 debug/tst-lfschk6.cc
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index 5818d751c9..15359da99f 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -1,4 +1,5 @@
>  # Copyright (C) 1998-2022 Free Software Foundation, Inc.
> +# Copyright The GNU Toolchain Authors.
>  # This file is part of the GNU C Library.
>  
>  # The GNU C Library is free software; you can redistribute it and/or
> @@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
>  CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
>  CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
>  
> +# _FORTIFY_SOURCE tests.
> +# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
> +# preprocessor conditions based on tst-fortify.c.
> +#
> +# To add a new test condition, define a cflags-$(cond) make variable to set
> +# CFLAGS for the file.
> +
> +tests-all-chk = tst-fortify
> +tests-c-chk =
> +tests-cc-chk =
> +
> +CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> +
> +# No additional flags for the default tests.
> +define cflags-default
> +endef
> +
> +define cflags-lfs
> +CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
> +endef
> +
>  # We know these tests have problems with format strings, this is what
>  # we are testing.  Disable that warning.  They are also testing
>  # deprecated functions (notably gets) so disable that warning as well.
>  # And they also generate warnings from warning attributes, which
>  # cannot be disabled via pragmas, so require -Wno-error to be used.
> -CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -LDLIBS-tst-chk4 = -lstdc++
> -LDLIBS-tst-chk5 = -lstdc++
> -LDLIBS-tst-chk6 = -lstdc++
> -LDLIBS-tst-chk8 = -lstdc++
> -LDLIBS-tst-lfschk4 = -lstdc++
> -LDLIBS-tst-lfschk5 = -lstdc++
> -LDLIBS-tst-lfschk6 = -lstdc++
> +define gen-chk-test
> +tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
> +CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
> +					  -Wno-deprecated-declarations \
> +					  -Wno-error
> +$(eval $(call cflags-$(2),$(1),$(3)))
> +tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
> +	( echo "/* Autogenerated from Makefile.  */"; \
> +	  echo ""; \
> +	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
> +	mv $$@.tmp $$@
> +endef
> +
> +chk-extensions = c cc
> +chk-types = default lfs
> +chk-levels = 1 2 3
> +
> +$(foreach e,$(chk-extensions), \
> +  $(foreach t,$(chk-types), \
> +    $(foreach l,$(chk-levels), \
> +      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
> +
> +tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
> +
> +define link-cc
> +LDLIBS-$(1) = -lstdc++
> +endef
> +$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
>  
>  # backtrace_symbols only works if we link with -rdynamic.  backtrace
>  # requires unwind tables on most architectures.
> @@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
>  
>  CFLAGS-tst-ssp-1.c += -fstack-protector-all
>  
> -tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
> -	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
> -	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
> -	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
> -	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
> +tests = backtrace-tst \
> +	tst-longjmp_chk \
> +	test-strcpy_chk \
> +	test-stpcpy_chk \
> +	tst-longjmp_chk2 \
> +	tst-backtrace2 \
> +	tst-backtrace3 \
> +	tst-backtrace4 \
> +	tst-backtrace5 \
> +	tst-backtrace6 \
> +	tst-realpath-chk \
> +	$(tests-all-chk)
>  
>  ifeq ($(have-ssp),yes)
>  tests += tst-ssp-1
>  endif
>  
>  ifeq (,$(CXX))
> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
> -		    tst-lfschk4 tst-lfschk5 tst-lfschk6
> +tests-unsupported = $(tests-cc-chk)
>  endif
>  
>  extra-libs = libpcprofile
> @@ -185,20 +220,10 @@ ifeq ($(run-built-tests),yes)
>  LOCALES := de_DE.UTF-8
>  include ../gen-locales.mk
>  
> -$(objpfx)tst-chk1.out: $(gen-locales)
> -$(objpfx)tst-chk2.out: $(gen-locales)
> -$(objpfx)tst-chk3.out: $(gen-locales)
> -$(objpfx)tst-chk4.out: $(gen-locales)
> -$(objpfx)tst-chk5.out: $(gen-locales)
> -$(objpfx)tst-chk6.out: $(gen-locales)
> -$(objpfx)tst-chk7.out: $(gen-locales)
> -$(objpfx)tst-chk8.out: $(gen-locales)
> -$(objpfx)tst-lfschk1.out: $(gen-locales)
> -$(objpfx)tst-lfschk2.out: $(gen-locales)
> -$(objpfx)tst-lfschk3.out: $(gen-locales)
> -$(objpfx)tst-lfschk4.out: $(gen-locales)
> -$(objpfx)tst-lfschk5.out: $(gen-locales)
> -$(objpfx)tst-lfschk6.out: $(gen-locales)
> +define chk-gen-locales
> +$(objpfx)$(1).out: $(gen-locales)
> +endef
> +$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
>  endif
>  
>  sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')

I see no much point in regenerate adding the auto-generated files on
repository, I think it would be better to move them to build directory
instead.

The changes below based on your patch should do it. I had to do a special
iterator to handle the C++ files.

diff --git a/debug/Makefile b/debug/Makefile
index b5e13eec94..7c9023b55a 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -148,12 +148,6 @@ tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
 CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
 					  -Wno-deprecated-declarations \
 					  -Wno-error
-$(eval $(call cflags-$(2),$(1),$(3)))
-tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
-	( echo "/* Autogenerated from Makefile.  */"; \
-	  echo "$(src-chk-$(2))"; \
-	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
-	mv $$@.tmp $$@
 endef
 
 chk-extensions = c cc
@@ -167,6 +161,32 @@ $(foreach e,$(chk-extensions), \
 
 tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
 
+generated += \
+  $(addsuffix .c, $(tests-c-chk)) \
+  $(addsuffix .cc, $(tests-cc-chk)) \
+  gen-debug-templates
+
+$(objpfx)gen-debug-templates.stmp: tst-fortify.c Makefile
+	$(make-target-directory)
+	for e in $(chk-extensions); do                           \
+	  for t in $(chk-types); do                              \
+	    for l in $(chk-levels); do                           \
+	      file=$(objpfx)tst-fortify-$${e}-$${t}-$${l}.$${e}; \
+	      (                                                  \
+	        echo "/* Autogenerated from Makefile.  */";      \
+		echo "#include \"tst-fortify.c\""                \
+	      ) > $${file};                                      \
+	    done;                                                \
+	  done;                                                  \
+	done;                                                    \
+	echo > $(@)
+
+# Add dependency to ensure the generator runs prior.
+$(foreach t, $(tests-c-chk), $(objpfx)$(t).c): \
+  $(objpfx)gen-debug-templates.stmp
+$(foreach t, $(tests-cc-chk), $(objpfx)$(t).cc): \
+  $(objpfx)gen-debug-templates.stmp
+
 define link-cc
 LDLIBS-$(1) = -lstdc++
 endef
@@ -222,6 +242,12 @@ generated += xtrace
 
 include ../Rules
 
+define o-iterator-doit
+$(objpfx)${o}.o: $(objpfx)$(o).cc $(before-compile); $$(compile-command.cc)
+endef
+object-suffixes-left := $(tests-cc-chk)
+include $(o-iterator)
+
 ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-default-1.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-default-2.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-default-3.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-lfs-1.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-lfs-2.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-c-lfs-3.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-1.c b/debug/tst-fortify-c-nongnu-1.c
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-c-nongnu-1.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-2.c b/debug/tst-fortify-c-nongnu-2.c
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-c-nongnu-2.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-c-nongnu-3.c b/debug/tst-fortify-c-nongnu-3.c
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-c-nongnu-3.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-default-1.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-default-2.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-default-3.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-lfs-1.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-lfs-2.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
deleted file mode 100644
index 08a5a758bb..0000000000
--- a/debug/tst-fortify-cc-lfs-3.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-1.cc b/debug/tst-fortify-cc-nongnu-1.cc
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-cc-nongnu-1.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-2.cc b/debug/tst-fortify-cc-nongnu-2.cc
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-cc-nongnu-2.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"
diff --git a/debug/tst-fortify-cc-nongnu-3.cc b/debug/tst-fortify-cc-nongnu-3.cc
deleted file mode 100644
index 5266984fa3..0000000000
--- a/debug/tst-fortify-cc-nongnu-3.cc
+++ /dev/null
@@ -1,3 +0,0 @@
-/* Autogenerated from Makefile.  */
-#undef _GNU_SOURCE
-#include "tst-fortify.c"

> diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
> deleted file mode 100644
> index be37ce2d22..0000000000
> --- a/debug/tst-chk2.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
> deleted file mode 100644
> index 38b8e4fb36..0000000000
> --- a/debug/tst-chk3.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
> deleted file mode 100644
> index c82e6aac86..0000000000
> --- a/debug/tst-chk4.cc
> +++ /dev/null
> @@ -1 +0,0 @@
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
> deleted file mode 100644
> index be37ce2d22..0000000000
> --- a/debug/tst-chk5.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
> deleted file mode 100644
> index 38b8e4fb36..0000000000
> --- a/debug/tst-chk6.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
> deleted file mode 100644
> index 2a7b323812..0000000000
> --- a/debug/tst-chk7.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
> deleted file mode 100644
> index 2a7b323812..0000000000
> --- a/debug/tst-chk8.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-default-1.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-default-2.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-default-3.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-lfs-1.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-lfs-2.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-c-lfs-3.c
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-default-1.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-default-2.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-default-3.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-lfs-1.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-lfs-2.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
> new file mode 100644
> index 0000000000..08a5a758bb
> --- /dev/null
> +++ b/debug/tst-fortify-cc-lfs-3.cc
> @@ -0,0 +1,3 @@
> +/* Autogenerated from Makefile.  */
> +
> +#include "tst-fortify.c"
> diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
> similarity index 100%
> rename from debug/tst-chk1.c
> rename to debug/tst-fortify.c
> diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
> deleted file mode 100644
> index f3e6d47d5e..0000000000
> --- a/debug/tst-lfschk1.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
> deleted file mode 100644
> index 95d4db1d32..0000000000
> --- a/debug/tst-lfschk2.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
> deleted file mode 100644
> index 50a1ae1258..0000000000
> --- a/debug/tst-lfschk3.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"
> diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
> deleted file mode 100644
> index f3e6d47d5e..0000000000
> --- a/debug/tst-lfschk4.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
> deleted file mode 100644
> index 95d4db1d32..0000000000
> --- a/debug/tst-lfschk5.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
> deleted file mode 100644
> index 50a1ae1258..0000000000
> --- a/debug/tst-lfschk6.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-11 13:27     ` Adhemerval Zanella
@ 2022-01-11 15:28       ` Siddhesh Poyarekar
  2022-01-11 16:41         ` Adhemerval Zanella
  0 siblings, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-11 15:28 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 11/01/2022 18:57, Adhemerval Zanella via Libc-alpha wrote:
> I see no much point in regenerate adding the auto-generated files on
> repository, I think it would be better to move them to build directory
> instead.

Fair point.

> The changes below based on your patch should do it. I had to do a special
> iterator to handle the C++ files.
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index b5e13eec94..7c9023b55a 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -148,12 +148,6 @@ tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
>   CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
>   					  -Wno-deprecated-declarations \
>   					  -Wno-error
> -$(eval $(call cflags-$(2),$(1),$(3)))
> -tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
> -	( echo "/* Autogenerated from Makefile.  */"; \
> -	  echo "$(src-chk-$(2))"; \
> -	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
> -	mv $$@.tmp $$@
>   endef
>   
>   chk-extensions = c cc
> @@ -167,6 +161,32 @@ $(foreach e,$(chk-extensions), \
>   
>   tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
>   
> +generated += \
> +  $(addsuffix .c, $(tests-c-chk)) \
> +  $(addsuffix .cc, $(tests-cc-chk)) \
> +  gen-debug-templates
> +
> +$(objpfx)gen-debug-templates.stmp: tst-fortify.c Makefile
> +	$(make-target-directory)
> +	for e in $(chk-extensions); do                           \
> +	  for t in $(chk-types); do                              \
> +	    for l in $(chk-levels); do                           \
> +	      file=$(objpfx)tst-fortify-$${e}-$${t}-$${l}.$${e}; \
> +	      (                                                  \
> +	        echo "/* Autogenerated from Makefile.  */";      \
> +		echo "#include \"tst-fortify.c\""                \
> +	      ) > $${file};                                      \
> +	    done;                                                \
> +	  done;                                                  \
> +	done;                                                    \
> +	echo > $(@)

Trouble here is that if one of the sources goes missing, there's no rule 
to regenerate it, so an incremental build with a new autogenerated test 
won't work.

Maybe ditch the generator and keep the above rules to generate sources, 
except that they generate the sources in $(objpfx) instead of the source 
directory?

> +
> +# Add dependency to ensure the generator runs prior.
> +$(foreach t, $(tests-c-chk), $(objpfx)$(t).c): \
> +  $(objpfx)gen-debug-templates.stmp
> +$(foreach t, $(tests-cc-chk), $(objpfx)$(t).cc): \
> +  $(objpfx)gen-debug-templates.stmp
> +
>   define link-cc
>   LDLIBS-$(1) = -lstdc++
>   endef
> @@ -222,6 +242,12 @@ generated += xtrace
>   
>   include ../Rules
>   
> +define o-iterator-doit
> +$(objpfx)${o}.o: $(objpfx)$(o).cc $(before-compile); $$(compile-command.cc)
> +endef
> +object-suffixes-left := $(tests-cc-chk)
> +include $(o-iterator)
> +
>   ifeq ($(run-built-tests),yes)
>   LOCALES := de_DE.UTF-8
>   include ../gen-locales.mk
> diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-default-1.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-default-2.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-default-3.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-lfs-1.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-lfs-2.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-c-lfs-3.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-nongnu-1.c b/debug/tst-fortify-c-nongnu-1.c
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-c-nongnu-1.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-nongnu-2.c b/debug/tst-fortify-c-nongnu-2.c
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-c-nongnu-2.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-c-nongnu-3.c b/debug/tst-fortify-c-nongnu-3.c
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-c-nongnu-3.c
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-default-1.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-default-2.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-default-3.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-lfs-1.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-lfs-2.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
> deleted file mode 100644
> index 08a5a758bb..0000000000
> --- a/debug/tst-fortify-cc-lfs-3.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-nongnu-1.cc b/debug/tst-fortify-cc-nongnu-1.cc
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-cc-nongnu-1.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-nongnu-2.cc b/debug/tst-fortify-cc-nongnu-2.cc
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-cc-nongnu-2.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> diff --git a/debug/tst-fortify-cc-nongnu-3.cc b/debug/tst-fortify-cc-nongnu-3.cc
> deleted file mode 100644
> index 5266984fa3..0000000000
> --- a/debug/tst-fortify-cc-nongnu-3.cc
> +++ /dev/null
> @@ -1,3 +0,0 @@
> -/* Autogenerated from Makefile.  */
> -#undef _GNU_SOURCE
> -#include "tst-fortify.c"
> 
>> diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
>> deleted file mode 100644
>> index be37ce2d22..0000000000
>> --- a/debug/tst-chk2.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 1
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
>> deleted file mode 100644
>> index 38b8e4fb36..0000000000
>> --- a/debug/tst-chk3.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 2
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
>> deleted file mode 100644
>> index c82e6aac86..0000000000
>> --- a/debug/tst-chk4.cc
>> +++ /dev/null
>> @@ -1 +0,0 @@
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
>> deleted file mode 100644
>> index be37ce2d22..0000000000
>> --- a/debug/tst-chk5.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 1
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
>> deleted file mode 100644
>> index 38b8e4fb36..0000000000
>> --- a/debug/tst-chk6.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 2
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
>> deleted file mode 100644
>> index 2a7b323812..0000000000
>> --- a/debug/tst-chk7.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 3
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
>> deleted file mode 100644
>> index 2a7b323812..0000000000
>> --- a/debug/tst-chk8.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FORTIFY_SOURCE 3
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-fortify-c-default-1.c b/debug/tst-fortify-c-default-1.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-default-1.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-c-default-2.c b/debug/tst-fortify-c-default-2.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-default-2.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-c-default-3.c b/debug/tst-fortify-c-default-3.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-default-3.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-c-lfs-1.c b/debug/tst-fortify-c-lfs-1.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-lfs-1.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-c-lfs-2.c b/debug/tst-fortify-c-lfs-2.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-lfs-2.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-c-lfs-3.c b/debug/tst-fortify-c-lfs-3.c
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-c-lfs-3.c
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-default-1.cc b/debug/tst-fortify-cc-default-1.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-default-1.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-default-2.cc b/debug/tst-fortify-cc-default-2.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-default-2.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-default-3.cc b/debug/tst-fortify-cc-default-3.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-default-3.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-lfs-1.cc b/debug/tst-fortify-cc-lfs-1.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-lfs-1.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-lfs-2.cc b/debug/tst-fortify-cc-lfs-2.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-lfs-2.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-fortify-cc-lfs-3.cc b/debug/tst-fortify-cc-lfs-3.cc
>> new file mode 100644
>> index 0000000000..08a5a758bb
>> --- /dev/null
>> +++ b/debug/tst-fortify-cc-lfs-3.cc
>> @@ -0,0 +1,3 @@
>> +/* Autogenerated from Makefile.  */
>> +
>> +#include "tst-fortify.c"
>> diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
>> similarity index 100%
>> rename from debug/tst-chk1.c
>> rename to debug/tst-fortify.c
>> diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
>> deleted file mode 100644
>> index f3e6d47d5e..0000000000
>> --- a/debug/tst-lfschk1.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
>> deleted file mode 100644
>> index 95d4db1d32..0000000000
>> --- a/debug/tst-lfschk2.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk2.c"
>> diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
>> deleted file mode 100644
>> index 50a1ae1258..0000000000
>> --- a/debug/tst-lfschk3.c
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk3.c"
>> diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
>> deleted file mode 100644
>> index f3e6d47d5e..0000000000
>> --- a/debug/tst-lfschk4.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk1.c"
>> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
>> deleted file mode 100644
>> index 95d4db1d32..0000000000
>> --- a/debug/tst-lfschk5.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk2.c"
>> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
>> deleted file mode 100644
>> index 50a1ae1258..0000000000
>> --- a/debug/tst-lfschk6.cc
>> +++ /dev/null
>> @@ -1,2 +0,0 @@
>> -#define _FILE_OFFSET_BITS 64
>> -#include "tst-chk3.c"
> 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-11 15:28       ` Siddhesh Poyarekar
@ 2022-01-11 16:41         ` Adhemerval Zanella
  0 siblings, 0 replies; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-11 16:41 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 11/01/2022 12:28, Siddhesh Poyarekar wrote:
> On 11/01/2022 18:57, Adhemerval Zanella via Libc-alpha wrote:
>> I see no much point in regenerate adding the auto-generated files on
>> repository, I think it would be better to move them to build directory
>> instead.
> 
> Fair point.
> 
>> The changes below based on your patch should do it. I had to do a special
>> iterator to handle the C++ files.
>>
>> diff --git a/debug/Makefile b/debug/Makefile
>> index b5e13eec94..7c9023b55a 100644
>> --- a/debug/Makefile
>> +++ b/debug/Makefile
>> @@ -148,12 +148,6 @@ tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
>>   CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
>>                         -Wno-deprecated-declarations \
>>                         -Wno-error
>> -$(eval $(call cflags-$(2),$(1),$(3)))
>> -tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
>> -    ( echo "/* Autogenerated from Makefile.  */"; \
>> -      echo "$(src-chk-$(2))"; \
>> -      echo "#include \"tst-fortify.c\"" ) > $$@.tmp
>> -    mv $$@.tmp $$@
>>   endef
>>     chk-extensions = c cc
>> @@ -167,6 +161,32 @@ $(foreach e,$(chk-extensions), \
>>     tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
>>   +generated += \
>> +  $(addsuffix .c, $(tests-c-chk)) \
>> +  $(addsuffix .cc, $(tests-cc-chk)) \
>> +  gen-debug-templates
>> +
>> +$(objpfx)gen-debug-templates.stmp: tst-fortify.c Makefile
>> +    $(make-target-directory)
>> +    for e in $(chk-extensions); do                           \
>> +      for t in $(chk-types); do                              \
>> +        for l in $(chk-levels); do                           \
>> +          file=$(objpfx)tst-fortify-$${e}-$${t}-$${l}.$${e}; \
>> +          (                                                  \
>> +            echo "/* Autogenerated from Makefile.  */";      \
>> +        echo "#include \"tst-fortify.c\""                \
>> +          ) > $${file};                                      \
>> +        done;                                                \
>> +      done;                                                  \
>> +    done;                                                    \
>> +    echo > $(@)
> 
> Trouble here is that if one of the sources goes missing, there's no rule to regenerate it, so an incremental build with a new autogenerated test won't work.
> 
> Maybe ditch the generator and keep the above rules to generate sources, except that they generate the sources in $(objpfx) instead of the source directory?

Yeah, that the same issue we have for math autogenerated files.  In such cases
I either trigger a full rebuild or just remove the stmp file to regenerate
the files.

But if you have a better idea I think we can use it and maybe also adapt on
math.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v5 0/2] _FORTIFY_SOURCE fixes [BZ #28746]
  2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
                   ` (4 preceding siblings ...)
  2022-01-10 16:29 ` [PATCH v4 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-12  5:51 ` Siddhesh Poyarekar
  2022-01-12  5:51   ` [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
  2022-01-12  5:51   ` [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  5 siblings, 2 replies; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-12  5:51 UTC (permalink / raw)
  To: libc-alpha

The main patch is 2/2, which fixes some fortified headers to match
feature macros with their corresponding main headers, without which
functions would fail to get fortified.  Patch 1/2 is a restructuring of
the tests so that it is easier to add different configurations to
_FORTIFY_SOURCE tests at various levels.

Tested on x86_64 to confirm that there were no new regressions due to
this and that the new tests also run clean.

Changes from v1:
- Rebased against latest trunk so that the patches actually apply.
- Renamed files so that the diffs are easier to compare and also make a
  bit more sense.

Changes from v2:
- Renamed tst-chk-0.c to tst-fortify.c and autogenerate all tests and
  include it.
- Set all the flags we can as -D in CFLAGS and autogenerate test cases
  if needed.
- Fix other review comments.

Changes from v3:
- Fixed typo (chk-gen-locals -> chk-gen-locales)

Changes from v4:
- Generate test sources in $(objpfx).

Siddhesh Poyarekar (2):
  debug: Autogenerate _FORTIFY_SOURCE tests
  debug: Synchronize feature guards in fortified functions [BZ #28746]

 Makerules                           |   6 ++
 debug/Makefile                      | 115 ++++++++++++++++++----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/{tst-chk1.c => tst-fortify.c} |  49 ++++++++++--
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 posix/bits/unistd.h                 |   2 +-
 string/bits/string_fortified.h      |   8 +-
 support/xsignal.h                   |   2 +
 wcsmbs/bits/wchar2.h                |   2 +-
 20 files changed, 129 insertions(+), 80 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (97%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-12  5:51 ` [PATCH v5 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
@ 2022-01-12  5:51   ` Siddhesh Poyarekar
  2022-01-12 13:27     ` Adhemerval Zanella
  2022-01-12  5:51   ` [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-12  5:51 UTC (permalink / raw)
  To: libc-alpha

Rename debug/tst-chk1.c to debug/tst-fortify.c and add make hackery to
autogenerate tests with different macros enabled to build and run the
same test with different configurations as well as different
fortification levels.

The change also ends up expanding the -lfs tests to include
_FORTIFY_SOURCE=3.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 Makerules                           |   6 ++
 debug/Makefile                      | 109 +++++++++++++++++-----------
 debug/tst-chk2.c                    |   2 -
 debug/tst-chk3.c                    |   2 -
 debug/tst-chk4.cc                   |   1 -
 debug/tst-chk5.cc                   |   2 -
 debug/tst-chk6.cc                   |   2 -
 debug/tst-chk7.c                    |   2 -
 debug/tst-chk8.cc                   |   2 -
 debug/{tst-chk1.c => tst-fortify.c} |   0
 debug/tst-lfschk1.c                 |   2 -
 debug/tst-lfschk2.c                 |   2 -
 debug/tst-lfschk3.c                 |   2 -
 debug/tst-lfschk4.cc                |   2 -
 debug/tst-lfschk5.cc                |   2 -
 debug/tst-lfschk6.cc                |   2 -
 16 files changed, 73 insertions(+), 67 deletions(-)
 delete mode 100644 debug/tst-chk2.c
 delete mode 100644 debug/tst-chk3.c
 delete mode 100644 debug/tst-chk4.cc
 delete mode 100644 debug/tst-chk5.cc
 delete mode 100644 debug/tst-chk6.cc
 delete mode 100644 debug/tst-chk7.c
 delete mode 100644 debug/tst-chk8.cc
 rename debug/{tst-chk1.c => tst-fortify.c} (100%)
 delete mode 100644 debug/tst-lfschk1.c
 delete mode 100644 debug/tst-lfschk2.c
 delete mode 100644 debug/tst-lfschk3.c
 delete mode 100644 debug/tst-lfschk4.cc
 delete mode 100644 debug/tst-lfschk5.cc
 delete mode 100644 debug/tst-lfschk6.cc

diff --git a/Makerules b/Makerules
index 9a56cbdead..81307b670e 100644
--- a/Makerules
+++ b/Makerules
@@ -424,6 +424,12 @@ $(objpfx)%$o: $(objpfx)%.c $(before-compile); $$(compile-command.c)
 endef
 object-suffixes-left := $(all-object-suffixes)
 include $(o-iterator)
+
+define o-iterator-doit
+$(objpfx)%$o: $(objpfx)%.cc $(before-compile); $$(compile-command.cc)
+endef
+object-suffixes-left := $(all-object-suffixes)
+include $(o-iterator)
 endif
 
 # Generate .dT files as we compile.
diff --git a/debug/Makefile b/debug/Makefile
index 5818d751c9..880477632e 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -1,4 +1,5 @@
 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
 
+# _FORTIFY_SOURCE tests.
+# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
+# preprocessor conditions based on tst-fortify.c.
+#
+# To add a new test condition, define a cflags-$(cond) make variable to set
+# CFLAGS for the file.
+
+tests-all-chk = tst-fortify
+tests-c-chk =
+tests-cc-chk =
+
+CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
+
+# No additional flags for the default tests.
+define cflags-default
+endef
+
+define cflags-lfs
+CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
+endef
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
 # And they also generate warnings from warning attributes, which
 # cannot be disabled via pragmas, so require -Wno-error to be used.
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
-LDLIBS-tst-chk4 = -lstdc++
-LDLIBS-tst-chk5 = -lstdc++
-LDLIBS-tst-chk6 = -lstdc++
-LDLIBS-tst-chk8 = -lstdc++
-LDLIBS-tst-lfschk4 = -lstdc++
-LDLIBS-tst-lfschk5 = -lstdc++
-LDLIBS-tst-lfschk6 = -lstdc++
+define gen-chk-test
+tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
+CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
+					  -Wno-deprecated-declarations \
+					  -Wno-error
+$(eval $(call cflags-$(2),$(1),$(3)))
+$(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
+	( echo "/* Autogenerated from Makefile.  */"; \
+	  echo ""; \
+	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
+	mv $$@.tmp $$@
+endef
+
+chk-extensions = c cc
+chk-types = default lfs
+chk-levels = 1 2 3
+
+$(foreach e,$(chk-extensions), \
+  $(foreach t,$(chk-types), \
+    $(foreach l,$(chk-levels), \
+      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
+
+tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
+
+define link-cc
+LDLIBS-$(1) = -lstdc++
+endef
+$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
 
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
 # requires unwind tables on most architectures.
@@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
 
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
 
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
+tests = backtrace-tst \
+	tst-longjmp_chk \
+	test-strcpy_chk \
+	test-stpcpy_chk \
+	tst-longjmp_chk2 \
+	tst-backtrace2 \
+	tst-backtrace3 \
+	tst-backtrace4 \
+	tst-backtrace5 \
+	tst-backtrace6 \
+	tst-realpath-chk \
+	$(tests-all-chk)
 
 ifeq ($(have-ssp),yes)
 tests += tst-ssp-1
 endif
 
 ifeq (,$(CXX))
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
+tests-unsupported = $(tests-cc-chk)
 endif
 
 extra-libs = libpcprofile
@@ -185,20 +220,10 @@ ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.UTF-8
 include ../gen-locales.mk
 
-$(objpfx)tst-chk1.out: $(gen-locales)
-$(objpfx)tst-chk2.out: $(gen-locales)
-$(objpfx)tst-chk3.out: $(gen-locales)
-$(objpfx)tst-chk4.out: $(gen-locales)
-$(objpfx)tst-chk5.out: $(gen-locales)
-$(objpfx)tst-chk6.out: $(gen-locales)
-$(objpfx)tst-chk7.out: $(gen-locales)
-$(objpfx)tst-chk8.out: $(gen-locales)
-$(objpfx)tst-lfschk1.out: $(gen-locales)
-$(objpfx)tst-lfschk2.out: $(gen-locales)
-$(objpfx)tst-lfschk3.out: $(gen-locales)
-$(objpfx)tst-lfschk4.out: $(gen-locales)
-$(objpfx)tst-lfschk5.out: $(gen-locales)
-$(objpfx)tst-lfschk6.out: $(gen-locales)
+define chk-gen-locales
+$(objpfx)$(1).out: $(gen-locales)
+endef
+$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
 endif
 
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
deleted file mode 100644
index c82e6aac86..0000000000
--- a/debug/tst-chk4.cc
+++ /dev/null
@@ -1 +0,0 @@
-#include "tst-chk1.c"
diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
deleted file mode 100644
index be37ce2d22..0000000000
--- a/debug/tst-chk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 1
-#include "tst-chk1.c"
diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
deleted file mode 100644
index 38b8e4fb36..0000000000
--- a/debug/tst-chk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 2
-#include "tst-chk1.c"
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk7.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
deleted file mode 100644
index 2a7b323812..0000000000
--- a/debug/tst-chk8.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FORTIFY_SOURCE 3
-#include "tst-chk1.c"
diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
similarity index 100%
rename from debug/tst-chk1.c
rename to debug/tst-fortify.c
diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk1.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk2.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk3.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
deleted file mode 100644
index f3e6d47d5e..0000000000
--- a/debug/tst-lfschk4.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk1.c"
diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
deleted file mode 100644
index 95d4db1d32..0000000000
--- a/debug/tst-lfschk5.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk2.c"
diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
deleted file mode 100644
index 50a1ae1258..0000000000
--- a/debug/tst-lfschk6.cc
+++ /dev/null
@@ -1,2 +0,0 @@
-#define _FILE_OFFSET_BITS 64
-#include "tst-chk3.c"
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-12  5:51 ` [PATCH v5 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
  2022-01-12  5:51   ` [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-12  5:51   ` Siddhesh Poyarekar
  2022-01-12 13:28     ` Adhemerval Zanella
  1 sibling, 1 reply; 27+ messages in thread
From: Siddhesh Poyarekar @ 2022-01-12  5:51 UTC (permalink / raw)
  To: libc-alpha

Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
main headers as they got incorporated into the standard, but their
fortified variants remained under __USE_GNU.  As a result, these
functions did not get fortified when _GNU_SOURCE was not defined.

Add test wrappers that check all functions tested in tst-chk0 at all
levels with _GNU_SOURCE undefined and then use the failures to (1)
exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
feature macro guards in the fortified function headers so that they're
the same as the ones in the main headers.

This fixes BZ #28746.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 debug/Makefile                 | 10 +++++--
 debug/tst-fortify.c            | 49 ++++++++++++++++++++++++++++------
 posix/bits/unistd.h            |  2 +-
 string/bits/string_fortified.h |  8 +++---
 support/xsignal.h              |  2 ++
 wcsmbs/bits/wchar2.h           |  2 +-
 6 files changed, 58 insertions(+), 15 deletions(-)

diff --git a/debug/Makefile b/debug/Makefile
index 880477632e..96029f32ee 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -132,6 +132,12 @@ define cflags-lfs
 CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
 endef
 
+define cflags-nongnu
+CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
+endef
+
+src-chk-nongnu = \#undef _GNU_SOURCE
+
 # We know these tests have problems with format strings, this is what
 # we are testing.  Disable that warning.  They are also testing
 # deprecated functions (notably gets) so disable that warning as well.
@@ -145,13 +151,13 @@ CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
 $(eval $(call cflags-$(2),$(1),$(3)))
 $(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
 	( echo "/* Autogenerated from Makefile.  */"; \
-	  echo ""; \
+	  echo "$(src-chk-$(2))"; \
 	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
 	mv $$@.tmp $$@
 endef
 
 chk-extensions = c cc
-chk-types = default lfs
+chk-types = default lfs nongnu
 chk-levels = 1 2 3
 
 $(foreach e,$(chk-extensions), \
diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
index 9288610fe6..d65a2fe6e1 100644
--- a/debug/tst-fortify.c
+++ b/debug/tst-fortify.c
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -36,6 +37,17 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#ifndef _GNU_SOURCE
+# define MEMPCPY memcpy
+# define WMEMPCPY wmemcpy
+# define MEMPCPY_RET(x) 0
+# define WMEMPCPY_RET(x) 0
+#else
+# define MEMPCPY mempcpy
+# define WMEMPCPY wmempcpy
+# define MEMPCPY_RET(x) __builtin_strlen (x)
+# define WMEMPCPY_RET(x) wcslen (x)
+#endif
 
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free
@@ -162,7 +174,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -207,7 +219,7 @@ do_test (void)
   if (memcmp (buf, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
+  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
       || memcmp (buf, "aabcdabcde", 10))
     FAIL ();
 
@@ -266,7 +278,8 @@ do_test (void)
   if (memcmp (a.buf1, "aabcdefghi", 10))
     FAIL ();
 
-  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
+  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
+      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
       || memcmp (a.buf1, "aabcdabcde", 10))
     FAIL ();
 
@@ -347,6 +360,7 @@ do_test (void)
   bcopy (buf + 1, buf + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", 5);
   CHK_FAIL_END
@@ -354,6 +368,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (buf + 9, 'j', 2);
@@ -464,6 +479,7 @@ do_test (void)
   bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
   CHK_FAIL_END
@@ -471,6 +487,7 @@ do_test (void)
   CHK_FAIL_START
   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   memset (a.buf1 + 9, 'j', 2);
@@ -550,7 +567,7 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -583,7 +600,8 @@ do_test (void)
   if (wmemcmp (wbuf, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
+  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
+      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wbuf, L"aabcdabcde", 10))
     FAIL ();
 
@@ -625,7 +643,8 @@ do_test (void)
   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
     FAIL ();
 
-  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
+  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
+      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
     FAIL ();
 
@@ -694,6 +713,7 @@ do_test (void)
   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -701,6 +721,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wbuf + 9, L'j', 2);
@@ -768,6 +789,7 @@ do_test (void)
   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
   CHK_FAIL_END
 
+#ifdef _GNU_SOURCE
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
   CHK_FAIL_END
@@ -775,6 +797,7 @@ do_test (void)
   CHK_FAIL_START
   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
   CHK_FAIL_END
+#endif
 
   CHK_FAIL_START
   wmemset (wa.buf1 + 9, L'j', 2);
@@ -906,6 +929,7 @@ do_test (void)
   if (fprintf (fp, buf2 + 4, str5) != 7)
     FAIL ();
 
+#ifdef _GNU_SOURCE
   char *my_ptr = NULL;
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
@@ -935,6 +959,7 @@ do_test (void)
   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
     FAIL ();
   obstack_free (&obs, NULL);
+#endif
 
   if (freopen (temp_filename, "r", stdin) == NULL)
     {
@@ -982,6 +1007,7 @@ do_test (void)
 
   rewind (stdin);
 
+#ifdef _GNU_SOURCE
   if (fgets_unlocked (buf, buf_size, stdin) != buf
       || memcmp (buf, "abcdefgh\n", 10))
     FAIL ();
@@ -1008,6 +1034,7 @@ do_test (void)
 #endif
 
   rewind (stdin);
+#endif
 
   if (fread (buf, 1, buf_size, stdin) != buf_size
       || memcmp (buf, "abcdefgh\nA", 10))
@@ -1578,7 +1605,10 @@ do_test (void)
       ret = 1;
     }
 
-  int fd = posix_openpt (O_RDWR);
+  int fd;
+
+#ifdef _GNU_SOURCE
+  fd = posix_openpt (O_RDWR);
   if (fd != -1)
     {
       char enough[1000];
@@ -1594,6 +1624,7 @@ do_test (void)
 #endif
       close (fd);
     }
+#endif
 
 #if PATH_MAX > 0
   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
@@ -1711,8 +1742,9 @@ do_test (void)
   poll (fds, l0 + 2, 0);
   CHK_FAIL_END
 #endif
+#ifdef _GNU_SOURCE
   ppoll (fds, 1, NULL, NULL);
-#if __USE_FORTIFY_LEVEL >= 1
+# if __USE_FORTIFY_LEVEL >= 1
   CHK_FAIL_START
   ppoll (fds, 2, NULL, NULL);
   CHK_FAIL_END
@@ -1720,6 +1752,7 @@ do_test (void)
   CHK_FAIL_START
   ppoll (fds, l0 + 2, NULL, NULL);
   CHK_FAIL_END
+# endif
 #endif
 
   return ret;
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
index 01df84b5f9..f53a7a06eb 100644
--- a/posix/bits/unistd.h
+++ b/posix/bits/unistd.h
@@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
 			  __fd, __buf, __nbytes);
 }
 
-#ifdef __USE_UNIX98
+#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
 extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
 			    __off_t __offset, size_t __bufsize)
   __wur __attr_access ((__write_only__, 2, 3));
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index 51ad480401..f4a5dfc2e5 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
 }
 
-#ifdef __USE_GNU
+#ifdef __USE_XOPEN2K8
 __fortify_function char *
 __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
 {
@@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
 				  __glibc_objsize (__dest));
 }
 
-#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+#ifdef __USE_XOPEN2K8
+# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
   return __builtin___stpncpy_chk (__dest, __src, __n,
 				  __glibc_objsize (__dest));
 }
-#else
+# else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __fortified_attr_access ((__write_only__, 1, 3))
@@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+# endif
 #endif
 
 
diff --git a/support/xsignal.h b/support/xsignal.h
index 973f532495..d868bb336c 100644
--- a/support/xsignal.h
+++ b/support/xsignal.h
@@ -28,7 +28,9 @@ __BEGIN_DECLS
    terminate the process on error.  */
 
 void xraise (int sig);
+#ifdef _GNU_SOURCE
 sighandler_t xsignal (int sig, sighandler_t handler);
+#endif
 void xsigaction (int sig, const struct sigaction *newact,
                  struct sigaction *oldact);
 
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
index 25d06d433f..0e017f458b 100644
--- a/wcsmbs/bits/wchar2.h
+++ b/wcsmbs/bits/wchar2.h
@@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
 }
 
 
-#ifdef __USE_GNU
+#ifdef	__USE_XOPEN2K8
 extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
 				const char **__restrict __src, size_t __nmc,
 				size_t __len, mbstate_t *__restrict __ps,
-- 
2.33.1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests
  2022-01-12  5:51   ` [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
@ 2022-01-12 13:27     ` Adhemerval Zanella
  0 siblings, 0 replies; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-12 13:27 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 12/01/2022 02:51, Siddhesh Poyarekar wrote:
> Rename debug/tst-chk1.c to debug/tst-fortify.c and add make hackery to
> autogenerate tests with different macros enabled to build and run the
> same test with different configurations as well as different
> fortification levels.
> 
> The change also ends up expanding the -lfs tests to include
> _FORTIFY_SOURCE=3.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  Makerules                           |   6 ++
>  debug/Makefile                      | 109 +++++++++++++++++-----------
>  debug/tst-chk2.c                    |   2 -
>  debug/tst-chk3.c                    |   2 -
>  debug/tst-chk4.cc                   |   1 -
>  debug/tst-chk5.cc                   |   2 -
>  debug/tst-chk6.cc                   |   2 -
>  debug/tst-chk7.c                    |   2 -
>  debug/tst-chk8.cc                   |   2 -
>  debug/{tst-chk1.c => tst-fortify.c} |   0
>  debug/tst-lfschk1.c                 |   2 -
>  debug/tst-lfschk2.c                 |   2 -
>  debug/tst-lfschk3.c                 |   2 -
>  debug/tst-lfschk4.cc                |   2 -
>  debug/tst-lfschk5.cc                |   2 -
>  debug/tst-lfschk6.cc                |   2 -
>  16 files changed, 73 insertions(+), 67 deletions(-)
>  delete mode 100644 debug/tst-chk2.c
>  delete mode 100644 debug/tst-chk3.c
>  delete mode 100644 debug/tst-chk4.cc
>  delete mode 100644 debug/tst-chk5.cc
>  delete mode 100644 debug/tst-chk6.cc
>  delete mode 100644 debug/tst-chk7.c
>  delete mode 100644 debug/tst-chk8.cc
>  rename debug/{tst-chk1.c => tst-fortify.c} (100%)
>  delete mode 100644 debug/tst-lfschk1.c
>  delete mode 100644 debug/tst-lfschk2.c
>  delete mode 100644 debug/tst-lfschk3.c
>  delete mode 100644 debug/tst-lfschk4.cc
>  delete mode 100644 debug/tst-lfschk5.cc
>  delete mode 100644 debug/tst-lfschk6.cc
> 
> diff --git a/Makerules b/Makerules
> index 9a56cbdead..81307b670e 100644
> --- a/Makerules
> +++ b/Makerules
> @@ -424,6 +424,12 @@ $(objpfx)%$o: $(objpfx)%.c $(before-compile); $$(compile-command.c)
>  endef
>  object-suffixes-left := $(all-object-suffixes)
>  include $(o-iterator)
> +
> +define o-iterator-doit
> +$(objpfx)%$o: $(objpfx)%.cc $(before-compile); $$(compile-command.cc)
> +endef
> +object-suffixes-left := $(all-object-suffixes)
> +include $(o-iterator)
>  endif
>  
>  # Generate .dT files as we compile.
> diff --git a/debug/Makefile b/debug/Makefile
> index 5818d751c9..880477632e 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -1,4 +1,5 @@
>  # Copyright (C) 1998-2022 Free Software Foundation, Inc.
> +# Copyright The GNU Toolchain Authors.
>  # This file is part of the GNU C Library.
>  
>  # The GNU C Library is free software; you can redistribute it and/or
> @@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
>  CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
>  CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
>  
> +# _FORTIFY_SOURCE tests.
> +# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
> +# preprocessor conditions based on tst-fortify.c.
> +#
> +# To add a new test condition, define a cflags-$(cond) make variable to set
> +# CFLAGS for the file.
> +
> +tests-all-chk = tst-fortify
> +tests-c-chk =
> +tests-cc-chk =
> +
> +CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> +
> +# No additional flags for the default tests.
> +define cflags-default
> +endef
> +
> +define cflags-lfs
> +CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
> +endef
> +
>  # We know these tests have problems with format strings, this is what
>  # we are testing.  Disable that warning.  They are also testing
>  # deprecated functions (notably gets) so disable that warning as well.
>  # And they also generate warnings from warning attributes, which
>  # cannot be disabled via pragmas, so require -Wno-error to be used.
> -CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
> -LDLIBS-tst-chk4 = -lstdc++
> -LDLIBS-tst-chk5 = -lstdc++
> -LDLIBS-tst-chk6 = -lstdc++
> -LDLIBS-tst-chk8 = -lstdc++
> -LDLIBS-tst-lfschk4 = -lstdc++
> -LDLIBS-tst-lfschk5 = -lstdc++
> -LDLIBS-tst-lfschk6 = -lstdc++
> +define gen-chk-test
> +tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
> +CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
> +					  -Wno-deprecated-declarations \
> +					  -Wno-error
> +$(eval $(call cflags-$(2),$(1),$(3)))
> +$(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
> +	( echo "/* Autogenerated from Makefile.  */"; \
> +	  echo ""; \
> +	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
> +	mv $$@.tmp $$@
> +endef
> +
> +chk-extensions = c cc
> +chk-types = default lfs
> +chk-levels = 1 2 3
> +
> +$(foreach e,$(chk-extensions), \
> +  $(foreach t,$(chk-types), \
> +    $(foreach l,$(chk-levels), \
> +      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
> +
> +tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
> +
> +define link-cc
> +LDLIBS-$(1) = -lstdc++
> +endef
> +$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
>  
>  # backtrace_symbols only works if we link with -rdynamic.  backtrace
>  # requires unwind tables on most architectures.
> @@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
>  
>  CFLAGS-tst-ssp-1.c += -fstack-protector-all
>  
> -tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
> -	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
> -	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
> -	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
> -	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
> +tests = backtrace-tst \
> +	tst-longjmp_chk \
> +	test-strcpy_chk \
> +	test-stpcpy_chk \
> +	tst-longjmp_chk2 \
> +	tst-backtrace2 \
> +	tst-backtrace3 \
> +	tst-backtrace4 \
> +	tst-backtrace5 \
> +	tst-backtrace6 \
> +	tst-realpath-chk \
> +	$(tests-all-chk)
>  
>  ifeq ($(have-ssp),yes)
>  tests += tst-ssp-1
>  endif
>  
>  ifeq (,$(CXX))
> -tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
> -		    tst-lfschk4 tst-lfschk5 tst-lfschk6
> +tests-unsupported = $(tests-cc-chk)
>  endif
>  
>  extra-libs = libpcprofile
> @@ -185,20 +220,10 @@ ifeq ($(run-built-tests),yes)
>  LOCALES := de_DE.UTF-8
>  include ../gen-locales.mk
>  
> -$(objpfx)tst-chk1.out: $(gen-locales)
> -$(objpfx)tst-chk2.out: $(gen-locales)
> -$(objpfx)tst-chk3.out: $(gen-locales)
> -$(objpfx)tst-chk4.out: $(gen-locales)
> -$(objpfx)tst-chk5.out: $(gen-locales)
> -$(objpfx)tst-chk6.out: $(gen-locales)
> -$(objpfx)tst-chk7.out: $(gen-locales)
> -$(objpfx)tst-chk8.out: $(gen-locales)
> -$(objpfx)tst-lfschk1.out: $(gen-locales)
> -$(objpfx)tst-lfschk2.out: $(gen-locales)
> -$(objpfx)tst-lfschk3.out: $(gen-locales)
> -$(objpfx)tst-lfschk4.out: $(gen-locales)
> -$(objpfx)tst-lfschk5.out: $(gen-locales)
> -$(objpfx)tst-lfschk6.out: $(gen-locales)
> +define chk-gen-locales
> +$(objpfx)$(1).out: $(gen-locales)
> +endef
> +$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
>  endif
>  
>  sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
> diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
> deleted file mode 100644
> index be37ce2d22..0000000000
> --- a/debug/tst-chk2.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
> deleted file mode 100644
> index 38b8e4fb36..0000000000
> --- a/debug/tst-chk3.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
> deleted file mode 100644
> index c82e6aac86..0000000000
> --- a/debug/tst-chk4.cc
> +++ /dev/null
> @@ -1 +0,0 @@
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
> deleted file mode 100644
> index be37ce2d22..0000000000
> --- a/debug/tst-chk5.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 1
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
> deleted file mode 100644
> index 38b8e4fb36..0000000000
> --- a/debug/tst-chk6.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 2
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
> deleted file mode 100644
> index 2a7b323812..0000000000
> --- a/debug/tst-chk7.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
> deleted file mode 100644
> index 2a7b323812..0000000000
> --- a/debug/tst-chk8.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FORTIFY_SOURCE 3
> -#include "tst-chk1.c"
> diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
> similarity index 100%
> rename from debug/tst-chk1.c
> rename to debug/tst-fortify.c
> diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
> deleted file mode 100644
> index f3e6d47d5e..0000000000
> --- a/debug/tst-lfschk1.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
> deleted file mode 100644
> index 95d4db1d32..0000000000
> --- a/debug/tst-lfschk2.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
> deleted file mode 100644
> index 50a1ae1258..0000000000
> --- a/debug/tst-lfschk3.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"
> diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
> deleted file mode 100644
> index f3e6d47d5e..0000000000
> --- a/debug/tst-lfschk4.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk1.c"
> diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
> deleted file mode 100644
> index 95d4db1d32..0000000000
> --- a/debug/tst-lfschk5.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk2.c"
> diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
> deleted file mode 100644
> index 50a1ae1258..0000000000
> --- a/debug/tst-lfschk6.cc
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define _FILE_OFFSET_BITS 64
> -#include "tst-chk3.c"

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746]
  2022-01-12  5:51   ` [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
@ 2022-01-12 13:28     ` Adhemerval Zanella
  0 siblings, 0 replies; 27+ messages in thread
From: Adhemerval Zanella @ 2022-01-12 13:28 UTC (permalink / raw)
  To: Siddhesh Poyarekar, libc-alpha



On 12/01/2022 02:51, Siddhesh Poyarekar wrote:
> Some functions (e.g. stpcpy, pread64, etc.) had moved to POSIX in the
> main headers as they got incorporated into the standard, but their
> fortified variants remained under __USE_GNU.  As a result, these
> functions did not get fortified when _GNU_SOURCE was not defined.
> 
> Add test wrappers that check all functions tested in tst-chk0 at all
> levels with _GNU_SOURCE undefined and then use the failures to (1)
> exclude checks for _GNU_SOURCE functions in these tests and (2) Fix
> feature macro guards in the fortified function headers so that they're
> the same as the ones in the main headers.
> 
> This fixes BZ #28746.
> 
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  debug/Makefile                 | 10 +++++--
>  debug/tst-fortify.c            | 49 ++++++++++++++++++++++++++++------
>  posix/bits/unistd.h            |  2 +-
>  string/bits/string_fortified.h |  8 +++---
>  support/xsignal.h              |  2 ++
>  wcsmbs/bits/wchar2.h           |  2 +-
>  6 files changed, 58 insertions(+), 15 deletions(-)
> 
> diff --git a/debug/Makefile b/debug/Makefile
> index 880477632e..96029f32ee 100644
> --- a/debug/Makefile
> +++ b/debug/Makefile
> @@ -132,6 +132,12 @@ define cflags-lfs
>  CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
>  endef
>  
> +define cflags-nongnu
> +CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
> +endef
> +
> +src-chk-nongnu = \#undef _GNU_SOURCE
> +
>  # We know these tests have problems with format strings, this is what
>  # we are testing.  Disable that warning.  They are also testing
>  # deprecated functions (notably gets) so disable that warning as well.
> @@ -145,13 +151,13 @@ CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
>  $(eval $(call cflags-$(2),$(1),$(3)))
>  $(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
>  	( echo "/* Autogenerated from Makefile.  */"; \
> -	  echo ""; \
> +	  echo "$(src-chk-$(2))"; \
>  	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
>  	mv $$@.tmp $$@
>  endef
>  
>  chk-extensions = c cc
> -chk-types = default lfs
> +chk-types = default lfs nongnu
>  chk-levels = 1 2 3
>  
>  $(foreach e,$(chk-extensions), \
> diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
> index 9288610fe6..d65a2fe6e1 100644
> --- a/debug/tst-fortify.c
> +++ b/debug/tst-fortify.c
> @@ -1,4 +1,5 @@
>  /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
> +   Copyright The GNU Toolchain Authors.
>     This file is part of the GNU C Library.
>  
>     The GNU C Library is free software; you can redistribute it and/or
> @@ -36,6 +37,17 @@
>  #include <sys/socket.h>
>  #include <sys/un.h>
>  
> +#ifndef _GNU_SOURCE
> +# define MEMPCPY memcpy
> +# define WMEMPCPY wmemcpy
> +# define MEMPCPY_RET(x) 0
> +# define WMEMPCPY_RET(x) 0
> +#else
> +# define MEMPCPY mempcpy
> +# define WMEMPCPY wmempcpy
> +# define MEMPCPY_RET(x) __builtin_strlen (x)
> +# define WMEMPCPY_RET(x) wcslen (x)
> +#endif
>  
>  #define obstack_chunk_alloc malloc
>  #define obstack_chunk_free free
> @@ -162,7 +174,7 @@ do_test (void)
>    if (memcmp (buf, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (buf + 5, "abcde", 5) != buf + 10
> +  if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (buf, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -207,7 +219,7 @@ do_test (void)
>    if (memcmp (buf, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
> +  if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (buf, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -266,7 +278,8 @@ do_test (void)
>    if (memcmp (a.buf1, "aabcdefghi", 10))
>      FAIL ();
>  
> -  if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
> +  if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
> +      != a.buf1 + 5 + MEMPCPY_RET ("abcde")
>        || memcmp (a.buf1, "aabcdabcde", 10))
>      FAIL ();
>  
> @@ -347,6 +360,7 @@ do_test (void)
>    bcopy (buf + 1, buf + 2, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    p = (char *) mempcpy (buf + 6, "abcde", 5);
>    CHK_FAIL_END
> @@ -354,6 +368,7 @@ do_test (void)
>    CHK_FAIL_START
>    p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    memset (buf + 9, 'j', 2);
> @@ -464,6 +479,7 @@ do_test (void)
>    bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
>    CHK_FAIL_END
> @@ -471,6 +487,7 @@ do_test (void)
>    CHK_FAIL_START
>    p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    memset (a.buf1 + 9, 'j', 2);
> @@ -550,7 +567,7 @@ do_test (void)
>    if (wmemcmp (wbuf, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
> +  if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wbuf, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -583,7 +600,8 @@ do_test (void)
>    if (wmemcmp (wbuf, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
> +  if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
> +      != wbuf + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wbuf, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -625,7 +643,8 @@ do_test (void)
>    if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
>      FAIL ();
>  
> -  if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
> +  if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
> +      != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
>        || wmemcmp (wa.buf1, L"aabcdabcde", 10))
>      FAIL ();
>  
> @@ -694,6 +713,7 @@ do_test (void)
>    wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    wp = wmempcpy (wbuf + 6, L"abcde", 5);
>    CHK_FAIL_END
> @@ -701,6 +721,7 @@ do_test (void)
>    CHK_FAIL_START
>    wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    wmemset (wbuf + 9, L'j', 2);
> @@ -768,6 +789,7 @@ do_test (void)
>    wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
>    CHK_FAIL_END
>  
> +#ifdef _GNU_SOURCE
>    CHK_FAIL_START
>    wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
>    CHK_FAIL_END
> @@ -775,6 +797,7 @@ do_test (void)
>    CHK_FAIL_START
>    wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
>    CHK_FAIL_END
> +#endif
>  
>    CHK_FAIL_START
>    wmemset (wa.buf1 + 9, L'j', 2);
> @@ -906,6 +929,7 @@ do_test (void)
>    if (fprintf (fp, buf2 + 4, str5) != 7)
>      FAIL ();
>  
> +#ifdef _GNU_SOURCE
>    char *my_ptr = NULL;
>    strcpy (buf2 + 2, "%n%s%n");
>    /* When the format string is writable and contains %n,
> @@ -935,6 +959,7 @@ do_test (void)
>    if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
>      FAIL ();
>    obstack_free (&obs, NULL);
> +#endif
>  
>    if (freopen (temp_filename, "r", stdin) == NULL)
>      {
> @@ -982,6 +1007,7 @@ do_test (void)
>  
>    rewind (stdin);
>  
> +#ifdef _GNU_SOURCE
>    if (fgets_unlocked (buf, buf_size, stdin) != buf
>        || memcmp (buf, "abcdefgh\n", 10))
>      FAIL ();
> @@ -1008,6 +1034,7 @@ do_test (void)
>  #endif
>  
>    rewind (stdin);
> +#endif
>  
>    if (fread (buf, 1, buf_size, stdin) != buf_size
>        || memcmp (buf, "abcdefgh\nA", 10))
> @@ -1578,7 +1605,10 @@ do_test (void)
>        ret = 1;
>      }
>  
> -  int fd = posix_openpt (O_RDWR);
> +  int fd;
> +
> +#ifdef _GNU_SOURCE
> +  fd = posix_openpt (O_RDWR);
>    if (fd != -1)
>      {
>        char enough[1000];
> @@ -1594,6 +1624,7 @@ do_test (void)
>  #endif
>        close (fd);
>      }
> +#endif
>  
>  #if PATH_MAX > 0
>    confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
> @@ -1711,8 +1742,9 @@ do_test (void)
>    poll (fds, l0 + 2, 0);
>    CHK_FAIL_END
>  #endif
> +#ifdef _GNU_SOURCE
>    ppoll (fds, 1, NULL, NULL);
> -#if __USE_FORTIFY_LEVEL >= 1
> +# if __USE_FORTIFY_LEVEL >= 1
>    CHK_FAIL_START
>    ppoll (fds, 2, NULL, NULL);
>    CHK_FAIL_END
> @@ -1720,6 +1752,7 @@ do_test (void)
>    CHK_FAIL_START
>    ppoll (fds, l0 + 2, NULL, NULL);
>    CHK_FAIL_END
> +# endif
>  #endif
>  
>    return ret;
> diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
> index 01df84b5f9..f53a7a06eb 100644
> --- a/posix/bits/unistd.h
> +++ b/posix/bits/unistd.h
> @@ -40,7 +40,7 @@ read (int __fd, void *__buf, size_t __nbytes)
>  			  __fd, __buf, __nbytes);
>  }
>  
> -#ifdef __USE_UNIX98
> +#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
>  extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
>  			    __off_t __offset, size_t __bufsize)
>    __wur __attr_access ((__write_only__, 2, 3));
> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
> index 51ad480401..f4a5dfc2e5 100644
> --- a/string/bits/string_fortified.h
> +++ b/string/bits/string_fortified.h
> @@ -79,7 +79,7 @@ __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
>    return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
>  }
>  
> -#ifdef __USE_GNU
> +#ifdef __USE_XOPEN2K8
>  __fortify_function char *
>  __NTH (stpcpy (char *__restrict __dest, const char *__restrict __src))
>  {
> @@ -96,14 +96,15 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
>  				  __glibc_objsize (__dest));
>  }
>  
> -#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
> +#ifdef __USE_XOPEN2K8
> +# if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
>  __fortify_function char *
>  __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>  {
>    return __builtin___stpncpy_chk (__dest, __src, __n,
>  				  __glibc_objsize (__dest));
>  }
> -#else
> +# else
>  extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>  			    size_t __destlen) __THROW
>    __fortified_attr_access ((__write_only__, 1, 3))
> @@ -119,6 +120,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>      return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
>    return __stpncpy_alias (__dest, __src, __n);
>  }
> +# endif
>  #endif
>  
>  
> diff --git a/support/xsignal.h b/support/xsignal.h
> index 973f532495..d868bb336c 100644
> --- a/support/xsignal.h
> +++ b/support/xsignal.h
> @@ -28,7 +28,9 @@ __BEGIN_DECLS
>     terminate the process on error.  */
>  
>  void xraise (int sig);
> +#ifdef _GNU_SOURCE
>  sighandler_t xsignal (int sig, sighandler_t handler);
> +#endif
>  void xsigaction (int sig, const struct sigaction *newact,
>                   struct sigaction *oldact);
>  
> diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
> index 25d06d433f..0e017f458b 100644
> --- a/wcsmbs/bits/wchar2.h
> +++ b/wcsmbs/bits/wchar2.h
> @@ -457,7 +457,7 @@ __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
>  }
>  
>  
> -#ifdef __USE_GNU
> +#ifdef	__USE_XOPEN2K8
>  extern size_t __mbsnrtowcs_chk (wchar_t *__restrict __dst,
>  				const char **__restrict __src, size_t __nmc,
>  				size_t __len, mbstate_t *__restrict __ps,

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2022-01-12 13:28 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-05  1:39 [PATCH 0/2] _FORTIFY_SOURCE fixes Siddhesh Poyarekar
2022-01-05  1:39 ` [PATCH 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
2022-01-05  1:39 ` [PATCH 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
2022-01-05  4:45 ` [PATCH v2 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
2022-01-05  4:45   ` [PATCH v2 1/2] debug: Refactor and expand _FORTIFY_SOURCE tests Siddhesh Poyarekar
2022-01-06 20:51     ` Adhemerval Zanella
2022-01-07  2:09       ` Siddhesh Poyarekar
2022-01-07 13:00         ` Adhemerval Zanella
2022-01-05  4:45   ` [PATCH v2 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
2022-01-06 21:15     ` Adhemerval Zanella
2022-01-07  2:12       ` Siddhesh Poyarekar
2022-01-07 13:02         ` Adhemerval Zanella
2022-01-10 15:34 ` [PATCH v3 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
2022-01-10 15:34   ` [PATCH v3 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
2022-01-10 16:30     ` Siddhesh Poyarekar
2022-01-10 15:34   ` [PATCH v3 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
2022-01-10 16:29 ` [PATCH v4 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
2022-01-10 16:29   ` [PATCH v4 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
2022-01-11 13:27     ` Adhemerval Zanella
2022-01-11 15:28       ` Siddhesh Poyarekar
2022-01-11 16:41         ` Adhemerval Zanella
2022-01-10 16:29   ` [PATCH v4 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
2022-01-12  5:51 ` [PATCH v5 0/2] _FORTIFY_SOURCE fixes " Siddhesh Poyarekar
2022-01-12  5:51   ` [PATCH v5 1/2] debug: Autogenerate _FORTIFY_SOURCE tests Siddhesh Poyarekar
2022-01-12 13:27     ` Adhemerval Zanella
2022-01-12  5:51   ` [PATCH v5 2/2] debug: Synchronize feature guards in fortified functions [BZ #28746] Siddhesh Poyarekar
2022-01-12 13:28     ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).