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