08c3a6
commit 536910724d30a38eeecd14cdbe2c93c78ea67646
08c3a6
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
08c3a6
Date:   Wed Oct 20 18:13:05 2021 +0530
08c3a6
08c3a6
    debug: Add tests for _FORTIFY_SOURCE=3
08c3a6
    
08c3a6
    Add some testing coverage for _FORTIFY_SOURCE=3.
08c3a6
    
08c3a6
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
08c3a6
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
08c3a6
    (cherry picked from commit ad6f2a010c2ce759936de4747f6e0d53991912f8)
08c3a6
08c3a6
diff --git a/debug/Makefile b/debug/Makefile
08c3a6
index 6893111cbfaa0900..357f888246061e15 100644
08c3a6
--- a/debug/Makefile
08c3a6
+++ b/debug/Makefile
08c3a6
@@ -120,6 +120,8 @@ CFLAGS-tst-chk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-chk4.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-chk5.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-chk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
+CFLAGS-tst-chk7.c += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
+CFLAGS-tst-chk8.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-lfschk1.c += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-lfschk2.c += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 CFLAGS-tst-lfschk3.c += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
@@ -129,6 +131,7 @@ CFLAGS-tst-lfschk6.cc += -Wno-format -Wno-deprecated-declarations -Wno-error
08c3a6
 LDLIBS-tst-chk4 = -lstdc++
08c3a6
 LDLIBS-tst-chk5 = -lstdc++
08c3a6
 LDLIBS-tst-chk6 = -lstdc++
08c3a6
+LDLIBS-tst-chk8 = -lstdc++
08c3a6
 LDLIBS-tst-lfschk4 = -lstdc++
08c3a6
 LDLIBS-tst-lfschk5 = -lstdc++
08c3a6
 LDLIBS-tst-lfschk6 = -lstdc++
08c3a6
@@ -150,16 +153,16 @@ CFLAGS-tst-ssp-1.c += -fstack-protector-all
08c3a6
 
08c3a6
 tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
08c3a6
 	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
08c3a6
-	tst-chk4 tst-chk5 tst-chk6 tst-lfschk4 tst-lfschk5 tst-lfschk6 \
08c3a6
-	tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \
08c3a6
-	tst-backtrace5 tst-backtrace6
08c3a6
+	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
08c3a6
+	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
08c3a6
+	tst-backtrace4 tst-backtrace5 tst-backtrace6
08c3a6
 
08c3a6
 ifeq ($(have-ssp),yes)
08c3a6
 tests += tst-ssp-1
08c3a6
 endif
08c3a6
 
08c3a6
 ifeq (,$(CXX))
08c3a6
-tests-unsupported = tst-chk4 tst-chk5 tst-chk6 \
08c3a6
+tests-unsupported = tst-chk4 tst-chk5 tst-chk6 tst-chk8 \
08c3a6
 		    tst-lfschk4 tst-lfschk5 tst-lfschk6
08c3a6
 endif
08c3a6
 
08c3a6
@@ -193,6 +196,8 @@ $(objpfx)tst-chk3.out: $(gen-locales)
08c3a6
 $(objpfx)tst-chk4.out: $(gen-locales)
08c3a6
 $(objpfx)tst-chk5.out: $(gen-locales)
08c3a6
 $(objpfx)tst-chk6.out: $(gen-locales)
08c3a6
+$(objpfx)tst-chk7.out: $(gen-locales)
08c3a6
+$(objpfx)tst-chk8.out: $(gen-locales)
08c3a6
 $(objpfx)tst-lfschk1.out: $(gen-locales)
08c3a6
 $(objpfx)tst-lfschk2.out: $(gen-locales)
08c3a6
 $(objpfx)tst-lfschk3.out: $(gen-locales)
08c3a6
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
08c3a6
index 6c1d32cc62c3a964..68ac00d1808382b8 100644
08c3a6
--- a/debug/tst-chk1.c
08c3a6
+++ b/debug/tst-chk1.c
08c3a6
@@ -83,8 +83,14 @@ handler (int sig)
08c3a6
     _exit (127);
08c3a6
 }
08c3a6
 
08c3a6
+#if __USE_FORTIFY_LEVEL == 3
08c3a6
+volatile size_t buf_size = 10;
08c3a6
+#else
08c3a6
 char buf[10];
08c3a6
 wchar_t wbuf[10];
08c3a6
+#define buf_size sizeof (buf)
08c3a6
+#endif
08c3a6
+
08c3a6
 volatile size_t l0;
08c3a6
 volatile char *p;
08c3a6
 volatile wchar_t *wp;
08c3a6
@@ -123,6 +129,10 @@ int num2 = 987654;
08c3a6
 static int
08c3a6
 do_test (void)
08c3a6
 {
08c3a6
+#if __USE_FORTIFY_LEVEL == 3
08c3a6
+  char *buf = (char *) malloc (buf_size);
08c3a6
+  wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
08c3a6
+#endif
08c3a6
   set_fortify_handler (handler);
08c3a6
 
08c3a6
   struct A { char buf1[9]; char buf2[1]; } a;
08c3a6
@@ -947,93 +957,93 @@ do_test (void)
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fgets (buf, sizeof (buf), stdin) != buf
08c3a6
+  if (fgets (buf, buf_size, stdin) != buf
08c3a6
       || memcmp (buf, "abcdefgh\n", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
08c3a6
+  if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fgets (buf, l0 + sizeof (buf), stdin) != buf
08c3a6
+  if (fgets (buf, l0 + buf_size, stdin) != buf
08c3a6
       || memcmp (buf, "abcdefgh\n", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
08c3a6
+  if (fgets (buf, buf_size + 1, stdin) != buf)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
08c3a6
+  if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
08c3a6
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
08c3a6
       || memcmp (buf, "abcdefgh\n", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
08c3a6
+  if (fgets_unlocked (buf, buf_size, stdin) != buf
08c3a6
       || memcmp (buf, "ABCDEFGHI", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
08c3a6
+  if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
08c3a6
       || memcmp (buf, "abcdefgh\n", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
08c3a6
+  if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
08c3a6
+  if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
08c3a6
+  if (fread (buf, 1, buf_size, stdin) != buf_size
08c3a6
       || memcmp (buf, "abcdefgh\nA", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fread (buf, sizeof (buf), 1, stdin) != 1
08c3a6
+  if (fread (buf, buf_size, 1, stdin) != 1
08c3a6
       || memcmp (buf, "BCDEFGHI\na", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
08c3a6
+  if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
08c3a6
       || memcmp (buf, "abcdefgh\nA", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
08c3a6
+  if (fread (buf, buf_size, l0 + 1, stdin) != 1
08c3a6
       || memcmp (buf, "BCDEFGHI\na", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
08c3a6
+  if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
08c3a6
+  if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
08c3a6
+  if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
08c3a6
       || memcmp (buf, "abcdefgh\nA", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
08c3a6
+  if (fread_unlocked (buf, buf_size, 1, stdin) != 1
08c3a6
       || memcmp (buf, "BCDEFGHI\na", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
@@ -1048,100 +1058,100 @@ do_test (void)
08c3a6
 
08c3a6
   rewind (stdin);
08c3a6
 
08c3a6
-  if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
08c3a6
+  if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
08c3a6
       || memcmp (buf, "abcdefgh\nA", 10))
08c3a6
     FAIL ();
08c3a6
-  if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
08c3a6
+  if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
08c3a6
       || memcmp (buf, "BCDEFGHI\na", 10))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
08c3a6
+  if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
08c3a6
+  if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
   lseek (fileno (stdin), 0, SEEK_SET);
08c3a6
 
08c3a6
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
08c3a6
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
08c3a6
       || memcmp (buf, "abcdefgh\n", 9))
08c3a6
     FAIL ();
08c3a6
-  if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
08c3a6
+  if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
08c3a6
       || memcmp (buf, "ABCDEFGHI", 9))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
   lseek (fileno (stdin), 0, SEEK_SET);
08c3a6
 
08c3a6
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
08c3a6
+  if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
08c3a6
       || memcmp (buf, "abcdefgh\n", 9))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
08c3a6
+  if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) != sizeof (buf) + 1)
08c3a6
+  if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
08c3a6
-      != sizeof (buf) - 1
08c3a6
+  if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
08c3a6
+      != buf_size - 1
08c3a6
       || memcmp (buf, "\nABCDEFGH", 9))
08c3a6
     FAIL ();
08c3a6
-  if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
08c3a6
+  if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
08c3a6
       || memcmp (buf, "abcdefgh\n", 9))
08c3a6
     FAIL ();
08c3a6
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
08c3a6
-      != sizeof (buf) - 1
08c3a6
+  if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
08c3a6
+      != buf_size - 1
08c3a6
       || memcmp (buf, "h\nABCDEFG", 9))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
08c3a6
-      != sizeof (buf) + 1)
08c3a6
+  if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
08c3a6
+      != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
08c3a6
-      != sizeof (buf) + 1)
08c3a6
+  if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
08c3a6
+      != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
 
08c3a6
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
08c3a6
-      != sizeof (buf) - 1
08c3a6
+  if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
08c3a6
+      != buf_size - 1
08c3a6
       || memcmp (buf, "\nABCDEFGH", 9))
08c3a6
     FAIL ();
08c3a6
-  if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
08c3a6
+  if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
08c3a6
       || memcmp (buf, "abcdefgh\n", 9))
08c3a6
     FAIL ();
08c3a6
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
08c3a6
-      != sizeof (buf) - 1
08c3a6
+  if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
08c3a6
+      != buf_size - 1
08c3a6
       || memcmp (buf, "h\nABCDEFG", 9))
08c3a6
     FAIL ();
08c3a6
 
08c3a6
 #if __USE_FORTIFY_LEVEL >= 1
08c3a6
   CHK_FAIL_START
08c3a6
-  if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
08c3a6
-      != sizeof (buf) + 1)
08c3a6
+  if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
08c3a6
+      != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 
08c3a6
   CHK_FAIL_START
08c3a6
-  if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf))
08c3a6
-      != sizeof (buf) + 1)
08c3a6
+  if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
08c3a6
+      != buf_size + 1)
08c3a6
     FAIL ();
08c3a6
   CHK_FAIL_END
08c3a6
 #endif
08c3a6
@@ -1179,7 +1189,7 @@ do_test (void)
08c3a6
   CHK_FAIL2_END
08c3a6
 
08c3a6
   CHK_FAIL2_START
08c3a6
-  snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
08c3a6
+  snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
08c3a6
   CHK_FAIL2_END
08c3a6
 
08c3a6
   int sp[2];
08c3a6
diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..2a7b32381268135c
08c3a6
--- /dev/null
08c3a6
+++ b/debug/tst-chk7.c
08c3a6
@@ -0,0 +1,2 @@
08c3a6
+#define _FORTIFY_SOURCE 3
08c3a6
+#include "tst-chk1.c"
08c3a6
diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..2a7b32381268135c
08c3a6
--- /dev/null
08c3a6
+++ b/debug/tst-chk8.cc
08c3a6
@@ -0,0 +1,2 @@
08c3a6
+#define _FORTIFY_SOURCE 3
08c3a6
+#include "tst-chk1.c"