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