a41fe4
commit ad6f2a010c2ce759936de4747f6e0d53991912f8
a41fe4
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
Date:   Wed Oct 20 18:13:05 2021 +0530
a41fe4
a41fe4
    debug: Add tests for _FORTIFY_SOURCE=3
a41fe4
    
a41fe4
    Add some testing coverage for _FORTIFY_SOURCE=3.
a41fe4
    
a41fe4
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
a41fe4
a41fe4
diff --git a/debug/Makefile b/debug/Makefile
a41fe4
index 5e45c9b41077f2fd..81361438fc3d2aa9 100644
a41fe4
--- a/debug/Makefile
a41fe4
+++ b/debug/Makefile
a41fe4
@@ -120,6 +120,8 @@ 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
@@ -129,6 +131,7 @@ 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
@@ -150,16 +153,16 @@ 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-lfschk4 tst-lfschk5 tst-lfschk6 \
a41fe4
-	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \
a41fe4
-	tst-backtrace5 tst-backtrace6
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
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 \
a41fe4
+tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
a41fe4
 		    tst-lfschk4 tst-lfschk5 tst-lfschk6
a41fe4
 endif
a41fe4
 
a41fe4
@@ -193,6 +196,8 @@ $(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
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
a41fe4
index ca2b524b2fa6404c..5e76081255316a93 100644
a41fe4
--- a/debug/tst-chk1.c
a41fe4
+++ b/debug/tst-chk1.c
a41fe4
@@ -83,8 +83,14 @@ handler (int sig)
a41fe4
     _exit (127);
a41fe4
 }
a41fe4
 
a41fe4
+#if __USE_FORTIFY_LEVEL == 3
a41fe4
+volatile size_t buf_size = 10;
a41fe4
+#else
a41fe4
 char buf[10];
a41fe4
 wchar_t wbuf[10];
a41fe4
+#define buf_size sizeof (buf)
a41fe4
+#endif
a41fe4
+
a41fe4
 volatile size_t l0;
a41fe4
 volatile char *p;
a41fe4
 volatile wchar_t *wp;
a41fe4
@@ -123,6 +129,10 @@ int num2 = 987654;
a41fe4
 static int
a41fe4
 do_test (void)
a41fe4
 {
a41fe4
+#if __USE_FORTIFY_LEVEL == 3
a41fe4
+  char *buf = (char *) malloc (buf_size);
a41fe4
+  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
a41fe4
+#endif
a41fe4
   set_fortify_handler (handler);
a41fe4
 
a41fe4
   struct A { char buf1[9]; char buf2[1]; } a;
a41fe4
@@ -947,93 +957,93 @@ do_test (void)
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fgets (buf, sizeof (buf), stdin) != buf
a41fe4
+  if (fgets (buf, buf_size, stdin) != buf
a41fe4
       || memcmp (buf, "abcdefgh\n", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
a41fe4
+  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fgets (buf, l0 + sizeof (buf), stdin) != buf
a41fe4
+  if (fgets (buf, l0 + buf_size, stdin) != buf
a41fe4
       || memcmp (buf, "abcdefgh\n", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
a41fe4
+  if (fgets (buf, buf_size + 1, stdin) != buf)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
a41fe4
+  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
a41fe4
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
a41fe4
       || memcmp (buf, "abcdefgh\n", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
a41fe4
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
a41fe4
       || memcmp (buf, "ABCDEFGHI", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
a41fe4
+  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
a41fe4
       || memcmp (buf, "abcdefgh\n", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
a41fe4
+  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
a41fe4
+  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
a41fe4
+  if (fread (buf, 1, buf_size, stdin) != buf_size
a41fe4
       || memcmp (buf, "abcdefgh\nA", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fread (buf, sizeof (buf), 1, stdin) != 1
a41fe4
+  if (fread (buf, buf_size, 1, stdin) != 1
a41fe4
       || memcmp (buf, "BCDEFGHI\na", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
a41fe4
+  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
a41fe4
       || memcmp (buf, "abcdefgh\nA", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
a41fe4
+  if (fread (buf, buf_size, l0 + 1, stdin) != 1
a41fe4
       || memcmp (buf, "BCDEFGHI\na", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
a41fe4
+  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
a41fe4
+  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
a41fe4
+  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
a41fe4
       || memcmp (buf, "abcdefgh\nA", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
a41fe4
+  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
a41fe4
       || memcmp (buf, "BCDEFGHI\na", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
@@ -1048,100 +1058,100 @@ do_test (void)
a41fe4
 
a41fe4
   rewind (stdin);
a41fe4
 
a41fe4
-  if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
a41fe4
+  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
a41fe4
       || memcmp (buf, "abcdefgh\nA", 10))
a41fe4
     FAIL ();
a41fe4
-  if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
a41fe4
+  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
a41fe4
       || memcmp (buf, "BCDEFGHI\na", 10))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
a41fe4
+  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
a41fe4
+  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
   lseek (fileno (stdin), 0, SEEK_SET);
a41fe4
 
a41fe4
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
a41fe4
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
a41fe4
       || memcmp (buf, "abcdefgh\n", 9))
a41fe4
     FAIL ();
a41fe4
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
a41fe4
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
a41fe4
       || memcmp (buf, "ABCDEFGHI", 9))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
   lseek (fileno (stdin), 0, SEEK_SET);
a41fe4
 
a41fe4
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
a41fe4
+  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
a41fe4
       || memcmp (buf, "abcdefgh\n", 9))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
a41fe4
+  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
a41fe4
+  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
a41fe4
-      != sizeof (buf) - 1
a41fe4
+  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
a41fe4
+      != buf_size - 1
a41fe4
       || memcmp (buf, "\nABCDEFGH", 9))
a41fe4
     FAIL ();
a41fe4
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
a41fe4
+  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
a41fe4
       || memcmp (buf, "abcdefgh\n", 9))
a41fe4
     FAIL ();
a41fe4
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
a41fe4
-      != sizeof (buf) - 1
a41fe4
+  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
a41fe4
+      != buf_size - 1
a41fe4
       || memcmp (buf, "h\nABCDEFG", 9))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
a41fe4
-      != sizeof (buf) + 1)
a41fe4
+  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
a41fe4
+      != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
a41fe4
-      != sizeof (buf) + 1)
a41fe4
+  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
a41fe4
+      != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
 
a41fe4
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
a41fe4
-      != sizeof (buf) - 1
a41fe4
+  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
a41fe4
+      != buf_size - 1
a41fe4
       || memcmp (buf, "\nABCDEFGH", 9))
a41fe4
     FAIL ();
a41fe4
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
a41fe4
+  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
a41fe4
       || memcmp (buf, "abcdefgh\n", 9))
a41fe4
     FAIL ();
a41fe4
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
a41fe4
-      != sizeof (buf) - 1
a41fe4
+  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
a41fe4
+      != buf_size - 1
a41fe4
       || memcmp (buf, "h\nABCDEFG", 9))
a41fe4
     FAIL ();
a41fe4
 
a41fe4
 #if __USE_FORTIFY_LEVEL >= 1
a41fe4
   CHK_FAIL_START
a41fe4
-  if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
a41fe4
-      != sizeof (buf) + 1)
a41fe4
+  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
a41fe4
+      != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 
a41fe4
   CHK_FAIL_START
a41fe4
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
a41fe4
-      != sizeof (buf) + 1)
a41fe4
+  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
a41fe4
+      != buf_size + 1)
a41fe4
     FAIL ();
a41fe4
   CHK_FAIL_END
a41fe4
 #endif
a41fe4
@@ -1179,7 +1189,7 @@ do_test (void)
a41fe4
   CHK_FAIL2_END
a41fe4
 
a41fe4
   CHK_FAIL2_START
a41fe4
-  snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
a41fe4
+  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
a41fe4
   CHK_FAIL2_END
a41fe4
 
a41fe4
   int sp[2];
a41fe4
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
a41fe4
new file mode 100644
a41fe4
index 0000000000000000..2a7b32381268135c
a41fe4
--- /dev/null
a41fe4
+++ b/debug/tst-chk7.c
a41fe4
@@ -0,0 +1,2 @@
a41fe4
+#define _FORTIFY_SOURCE 3
a41fe4
+#include "tst-chk1.c"
a41fe4
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
a41fe4
new file mode 100644
a41fe4
index 0000000000000000..2a7b32381268135c
a41fe4
--- /dev/null
a41fe4
+++ b/debug/tst-chk8.cc
a41fe4
@@ -0,0 +1,2 @@
a41fe4
+#define _FORTIFY_SOURCE 3
a41fe4
+#include "tst-chk1.c"