a41fe4
commit db27f1251b008280a29d540b4f8ab2a38a0d80af
a41fe4
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
Date:   Wed Jan 12 23:34:23 2022 +0530
a41fe4
a41fe4
    debug: Autogenerate _FORTIFY_SOURCE tests
a41fe4
    
a41fe4
    Rename debug/tst-chk1.c to debug/tst-fortify.c and add make hackery to
a41fe4
    autogenerate tests with different macros enabled to build and run the
a41fe4
    same test with different configurations as well as different
a41fe4
    fortification levels.
a41fe4
    
a41fe4
    The change also ends up expanding the -lfs tests to include
a41fe4
    _FORTIFY_SOURCE=3.
a41fe4
    
a41fe4
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
a41fe4
a41fe4
# Conflicts:
a41fe4
#	debug/Makefile
a41fe4
a41fe4
diff --git a/Makerules b/Makerules
a41fe4
index 5d6434c74bf9bfe5..05a549eb0f259113 100644
a41fe4
--- a/Makerules
a41fe4
+++ b/Makerules
a41fe4
@@ -444,6 +444,12 @@ $(objpfx)%$o: $(objpfx)%.c $(before-compile); $$(compile-command.c)
a41fe4
 endef
a41fe4
 object-suffixes-left := $(all-object-suffixes)
a41fe4
 include $(o-iterator)
a41fe4
+
a41fe4
+define o-iterator-doit
a41fe4
+$(objpfx)%$o: $(objpfx)%.cc $(before-compile); $$(compile-command.cc)
a41fe4
+endef
a41fe4
+object-suffixes-left := $(all-object-suffixes)
a41fe4
+include $(o-iterator)
a41fe4
 endif
a41fe4
 
a41fe4
 # Generate version maps, but wait until sysdep-subdirs is known
a41fe4
diff --git a/debug/Makefile b/debug/Makefile
a41fe4
index b43f42ee3851f360..c92fd23dda1a7279 100644
a41fe4
--- a/debug/Makefile
a41fe4
+++ b/debug/Makefile
a41fe4
@@ -1,4 +1,5 @@
a41fe4
-# Copyright (C) 1998-2018 Free Software Foundation, Inc.
a41fe4
+# Copyright (C) 1998-2022 Free Software Foundation, Inc.
a41fe4
+# Copyright The GNU Toolchain Authors.
a41fe4
 # This file is part of the GNU C Library.
a41fe4
 
a41fe4
 # The GNU C Library is free software; you can redistribute it and/or
a41fe4
@@ -110,32 +111,60 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
a41fe4
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
a41fe4
 CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
a41fe4
 
a41fe4
+# _FORTIFY_SOURCE tests.
a41fe4
+# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
a41fe4
+# preprocessor conditions based on tst-fortify.c.
a41fe4
+#
a41fe4
+# To add a new test condition, define a cflags-$(cond) make variable to set
a41fe4
+# CFLAGS for the file.
a41fe4
+
a41fe4
+tests-all-chk = tst-fortify
a41fe4
+tests-c-chk =
a41fe4
+tests-cc-chk =
a41fe4
+
a41fe4
+CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
+
a41fe4
+# No additional flags for the default tests.
a41fe4
+define cflags-default
a41fe4
+endef
a41fe4
+
a41fe4
+define cflags-lfs
a41fe4
+CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
a41fe4
+endef
a41fe4
+
a41fe4
 # We know these tests have problems with format strings, this is what
a41fe4
 # we are testing.  Disable that warning.  They are also testing
a41fe4
 # deprecated functions (notably gets) so disable that warning as well.
a41fe4
 # And they also generate warnings from warning attributes, which
a41fe4
 # cannot be disabled via pragmas, so require -Wno-error to be used.
a41fe4
-CFLAGS-tst-chk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
a41fe4
-LDLIBS-tst-chk4 = -lstdc++
a41fe4
-LDLIBS-tst-chk5 = -lstdc++
a41fe4
-LDLIBS-tst-chk6 = -lstdc++
a41fe4
-LDLIBS-tst-chk8 = -lstdc++
a41fe4
-LDLIBS-tst-lfschk4 = -lstdc++
a41fe4
-LDLIBS-tst-lfschk5 = -lstdc++
a41fe4
-LDLIBS-tst-lfschk6 = -lstdc++
a41fe4
+define gen-chk-test
a41fe4
+tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
a41fe4
+CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
a41fe4
+					  -Wno-deprecated-declarations \
a41fe4
+					  -Wno-error
a41fe4
+$(eval $(call cflags-$(2),$(1),$(3)))
a41fe4
+$(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
a41fe4
+	( echo "/* Autogenerated from Makefile.  */"; \
a41fe4
+	  echo ""; \
a41fe4
+	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
a41fe4
+	mv $$@.tmp $$@
a41fe4
+endef
a41fe4
+
a41fe4
+chk-extensions = c cc
a41fe4
+chk-types = default lfs
a41fe4
+chk-levels = 1 2 3
a41fe4
+
a41fe4
+$(foreach e,$(chk-extensions), \
a41fe4
+  $(foreach t,$(chk-types), \
a41fe4
+    $(foreach l,$(chk-levels), \
a41fe4
+      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
a41fe4
+
a41fe4
+tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
a41fe4
+
a41fe4
+define link-cc
a41fe4
+LDLIBS-$(1) = -lstdc++
a41fe4
+endef
a41fe4
+$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
a41fe4
 
a41fe4
 # backtrace_symbols only works if we link with -rdynamic.  backtrace
a41fe4
 # requires unwind tables on most architectures.
a41fe4
@@ -152,19 +181,25 @@ LDFLAGS-tst-backtrace6 = -rdynamic
a41fe4
 
a41fe4
 CFLAGS-tst-ssp-1.c += -fstack-protector-all
a41fe4
 
a41fe4
-tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
a41fe4
-	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
a41fe4
-	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
a41fe4
-	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
a41fe4
-	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
a41fe4
+tests = backtrace-tst \
a41fe4
+	tst-longjmp_chk \
a41fe4
+	test-strcpy_chk \
a41fe4
+	test-stpcpy_chk \
a41fe4
+	tst-longjmp_chk2 \
a41fe4
+	tst-backtrace2 \
a41fe4
+	tst-backtrace3 \
a41fe4
+	tst-backtrace4 \
a41fe4
+	tst-backtrace5 \
a41fe4
+	tst-backtrace6 \
a41fe4
+	tst-realpath-chk \
a41fe4
+	$(tests-all-chk)
a41fe4
 
a41fe4
 ifeq ($(have-ssp),yes)
a41fe4
 tests += tst-ssp-1
a41fe4
 endif
a41fe4
 
a41fe4
 ifeq (,$(CXX))
a41fe4
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
a41fe4
-		    tst-lfschk4 tst-lfschk5 tst-lfschk6
a41fe4
+tests-unsupported = $(tests-cc-chk)
a41fe4
 endif
a41fe4
 
a41fe4
 extra-libs = libSegFault libpcprofile
a41fe4
@@ -191,20 +226,10 @@ ifeq ($(run-built-tests),yes)
a41fe4
 LOCALES := de_DE.UTF-8
a41fe4
 include ../gen-locales.mk
a41fe4
 
a41fe4
-$(objpfx)tst-chk1.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk2.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk3.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk4.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk5.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk6.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk7.out: $(gen-locales)
a41fe4
-$(objpfx)tst-chk8.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk1.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk2.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk3.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk4.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk5.out: $(gen-locales)
a41fe4
-$(objpfx)tst-lfschk6.out: $(gen-locales)
a41fe4
+define chk-gen-locales
a41fe4
+$(objpfx)$(1).out: $(gen-locales)
a41fe4
+endef
a41fe4
+$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
a41fe4
 endif
a41fe4
 
a41fe4
 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
a41fe4
diff --git a/debug/tst-chk2.c b/debug/tst-chk2.c
a41fe4
deleted file mode 100644
a41fe4
index be37ce2d22f0760a..0000000000000000
a41fe4
--- a/debug/tst-chk2.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 1
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk3.c b/debug/tst-chk3.c
a41fe4
deleted file mode 100644
a41fe4
index 38b8e4fb360ba722..0000000000000000
a41fe4
--- a/debug/tst-chk3.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 2
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk4.cc b/debug/tst-chk4.cc
a41fe4
deleted file mode 100644
a41fe4
index c82e6aac86038791..0000000000000000
a41fe4
--- a/debug/tst-chk4.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1 +0,0 @@
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk5.cc b/debug/tst-chk5.cc
a41fe4
deleted file mode 100644
a41fe4
index be37ce2d22f0760a..0000000000000000
a41fe4
--- a/debug/tst-chk5.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 1
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk6.cc b/debug/tst-chk6.cc
a41fe4
deleted file mode 100644
a41fe4
index 38b8e4fb360ba722..0000000000000000
a41fe4
--- a/debug/tst-chk6.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 2
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
a41fe4
deleted file mode 100644
a41fe4
index 2a7b32381268135c..0000000000000000
a41fe4
--- a/debug/tst-chk7.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 3
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
a41fe4
deleted file mode 100644
a41fe4
index 2a7b32381268135c..0000000000000000
a41fe4
--- a/debug/tst-chk8.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FORTIFY_SOURCE 3
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk1.c b/debug/tst-fortify.c
a41fe4
similarity index 100%
a41fe4
rename from debug/tst-chk1.c
a41fe4
rename to debug/tst-fortify.c
a41fe4
diff --git a/debug/tst-lfschk1.c b/debug/tst-lfschk1.c
a41fe4
deleted file mode 100644
a41fe4
index f3e6d47d5e4484c3..0000000000000000
a41fe4
--- a/debug/tst-lfschk1.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-lfschk2.c b/debug/tst-lfschk2.c
a41fe4
deleted file mode 100644
a41fe4
index 95d4db1d32d2eeb3..0000000000000000
a41fe4
--- a/debug/tst-lfschk2.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk2.c"
a41fe4
diff --git a/debug/tst-lfschk3.c b/debug/tst-lfschk3.c
a41fe4
deleted file mode 100644
a41fe4
index 50a1ae1258f1553d..0000000000000000
a41fe4
--- a/debug/tst-lfschk3.c
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk3.c"
a41fe4
diff --git a/debug/tst-lfschk4.cc b/debug/tst-lfschk4.cc
a41fe4
deleted file mode 100644
a41fe4
index f3e6d47d5e4484c3..0000000000000000
a41fe4
--- a/debug/tst-lfschk4.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-lfschk5.cc b/debug/tst-lfschk5.cc
a41fe4
deleted file mode 100644
a41fe4
index 95d4db1d32d2eeb3..0000000000000000
a41fe4
--- a/debug/tst-lfschk5.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk2.c"
a41fe4
diff --git a/debug/tst-lfschk6.cc b/debug/tst-lfschk6.cc
a41fe4
deleted file mode 100644
a41fe4
index 50a1ae1258f1553d..0000000000000000
a41fe4
--- a/debug/tst-lfschk6.cc
a41fe4
+++ /dev/null
a41fe4
@@ -1,2 +0,0 @@
a41fe4
-#define _FILE_OFFSET_BITS 64
a41fe4
-#include "tst-chk3.c"