From f001698bc1a1523bcbc039c8e7b28f8dec67e91c Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Mon, 3 Nov 2014 23:36:41 +0000 Subject: [PATCH] Port lib/libc/ssp to FreeBSD In most cases, the buffers and data were resized, but when dealing with the helpers, some of the code was adjusted to fail more reliably Submitted by: pho --- contrib/netbsd-tests/lib/libc/ssp/h_memset.c | 4 + contrib/netbsd-tests/lib/libc/ssp/h_read.c | 20 ++- .../netbsd-tests/lib/libc/ssp/h_readlink.c | 19 +++ .../netbsd-tests/lib/libc/ssp/h_snprintf.c | 6 + contrib/netbsd-tests/lib/libc/ssp/t_ssp.sh | 151 ++++++++++++++++++ 5 files changed, 199 insertions(+), 1 deletion(-) diff --git a/contrib/netbsd-tests/lib/libc/ssp/h_memset.c b/contrib/netbsd-tests/lib/libc/ssp/h_memset.c index 65ed5f2ee7d2..c5ab607090a7 100644 --- a/contrib/netbsd-tests/lib/libc/ssp/h_memset.c +++ b/contrib/netbsd-tests/lib/libc/ssp/h_memset.c @@ -41,5 +41,9 @@ main(int argc, char *argv[]) char b[10]; size_t len = atoi(argv[1]); (void)memset(b, 0, len); +#ifdef __FreeBSD__ + return b[0]; /* keeps optimizer from zapping the call to memset() */ +#else return 0; +#endif } diff --git a/contrib/netbsd-tests/lib/libc/ssp/h_read.c b/contrib/netbsd-tests/lib/libc/ssp/h_read.c index 5240af291eb7..1197b6cece08 100644 --- a/contrib/netbsd-tests/lib/libc/ssp/h_read.c +++ b/contrib/netbsd-tests/lib/libc/ssp/h_read.c @@ -36,12 +36,30 @@ __RCSID("$NetBSD: h_read.c,v 1.1 2010/12/27 02:04:19 pgoyette Exp $"); #include #include +#ifdef __FreeBSD__ +#include + +int +main(int argc, char *argv[]) +{ + char b[MAXPATHLEN]; + int fd, n; + size_t len = atoi(argv[1]); + + fd = open("/dev/zero", O_RDONLY); + if ((n = read(fd, b, len)) == -1) + abort(); + (void)printf("%s\n", b); + return (0); +} +#else int main(int argc, char *argv[]) { char b[MAXPATHLEN]; size_t len = atoi(argv[1]); - (void)read(0, b, len); + (void)printf("%s\n", b); return 0; } +#endif diff --git a/contrib/netbsd-tests/lib/libc/ssp/h_readlink.c b/contrib/netbsd-tests/lib/libc/ssp/h_readlink.c index c18faedc2f5b..7e8bff6de888 100644 --- a/contrib/netbsd-tests/lib/libc/ssp/h_readlink.c +++ b/contrib/netbsd-tests/lib/libc/ssp/h_readlink.c @@ -36,12 +36,31 @@ __RCSID("$NetBSD: h_readlink.c,v 1.1 2010/12/27 02:04:19 pgoyette Exp $"); #include #include +#ifdef __FreeBSD__ +#include +#include +#endif + int main(int argc, char *argv[]) { +#ifdef __FreeBSD__ + char b[512], *sl; + int n; + size_t len = atoi(argv[1]); + sl = malloc(len); + memset(sl, 'a', len); + sl[len - 1] = 0; + unlink("symlink"); + if (symlink(sl, "symlink") == -1) + err(1, "symlink()"); + n = readlink("symlink", b, len); + unlink("symlink"); +#else char b[MAXPATHLEN]; size_t len = atoi(argv[1]); (void)readlink("/", b, len); +#endif (void)printf("%s\n", b); return 0; } diff --git a/contrib/netbsd-tests/lib/libc/ssp/h_snprintf.c b/contrib/netbsd-tests/lib/libc/ssp/h_snprintf.c index 93575a81d6d9..75767887f5d7 100644 --- a/contrib/netbsd-tests/lib/libc/ssp/h_snprintf.c +++ b/contrib/netbsd-tests/lib/libc/ssp/h_snprintf.c @@ -39,7 +39,13 @@ main(int argc, char *argv[]) { char b[10]; size_t len = atoi(argv[1]); +#ifdef __FreeBSD__ + char c[] = "01234567890123456789"; + c[len] = 0; + (void)snprintf(b, len, "%s", c); +#else (void)snprintf(b, len, "%s", "0123456789"); +#endif (void)printf("%s\n", b); return 0; } diff --git a/contrib/netbsd-tests/lib/libc/ssp/t_ssp.sh b/contrib/netbsd-tests/lib/libc/ssp/t_ssp.sh index 0514348fa5cf..04adc6779553 100755 --- a/contrib/netbsd-tests/lib/libc/ssp/t_ssp.sh +++ b/contrib/netbsd-tests/lib/libc/ssp/t_ssp.sh @@ -34,7 +34,15 @@ h_pass() h_fail() { echo "Executing command [ $2$1 ]" + # Begin FreeBSD + if true; then + eval $2 atf_check -s signal -o ignore -e ignore $1 + else + # End FreeBSD eval $2 atf_check -s signal:6 -o ignore -e ignore $1 + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case sprintf @@ -47,7 +55,15 @@ sprintf_body() prog="$(atf_get_srcdir)/h_sprintf" h_pass "$prog ok" + # Begin FreeBSD + if true; then + h_fail "$prog 0123456789ab" + else + # End FreeBSD h_fail "$prog 0123456789" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case vsprintf @@ -60,7 +76,15 @@ vsprintf_body() prog="$(atf_get_srcdir)/h_vsprintf" h_pass "$prog ok" + # Begin FreeBSD + if true; then + h_fail "$prog 0123456789ab" + else + # End FreeBSD h_fail "$prog 0123456789" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case snprintf @@ -73,7 +97,15 @@ snprintf_body() prog="$(atf_get_srcdir)/h_snprintf" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case vsnprintf @@ -86,7 +118,15 @@ vsnprintf_body() prog="$(atf_get_srcdir)/h_vsnprintf" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case gets @@ -99,7 +139,15 @@ gets_body() prog="$(atf_get_srcdir)/h_gets" h_pass "$prog" "echo ok |" + # Begin FreeBSD + if true; then + h_fail "$prog" "echo 0123456789ab |" + else + # End FreeBSD h_fail "$prog" "echo 0123456789 |" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case fgets @@ -112,7 +160,15 @@ fgets_body() prog="$(atf_get_srcdir)/h_fgets" h_pass "$prog 10" "echo ok |" + # Begin FreeBSD + if true; then + h_fail "$prog 13" "echo 0123456789abc |" + else + # End FreeBSD h_fail "$prog 11" "echo busted |" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case memcpy @@ -125,7 +181,15 @@ memcpy_body() prog="$(atf_get_srcdir)/h_memcpy" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case memmove @@ -138,7 +202,15 @@ memmove_body() prog="$(atf_get_srcdir)/h_memmove" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case memset @@ -151,7 +223,15 @@ memset_body() prog="$(atf_get_srcdir)/h_memset" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case strcpy @@ -164,7 +244,15 @@ strcpy_body() prog="$(atf_get_srcdir)/h_strcpy" h_pass "$prog 0123456" + # Begin FreeBSD + if true; then + h_fail "$prog 0123456789ab" + else + # End FreeBSD h_fail "$prog 0123456789" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case stpcpy @@ -177,7 +265,15 @@ stpcpy_body() prog="$(atf_get_srcdir)/h_stpcpy" h_pass "$prog 0123456" + # Begin FreeBSD + if true; then + h_fail "$prog 0123456789ab" + else + # End FreeBSD h_fail "$prog 0123456789" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case strcat @@ -203,7 +299,15 @@ strncpy_body() prog="$(atf_get_srcdir)/h_strncpy" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case stpncpy @@ -216,7 +320,15 @@ stpncpy_body() prog="$(atf_get_srcdir)/h_stpncpy" h_pass "$prog 10" + # Begin FreeBSD + if true; then + h_fail "$prog 13" + else + # End FreeBSD h_fail "$prog 11" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case strncat @@ -228,8 +340,16 @@ strncat_body() { prog="$(atf_get_srcdir)/h_strncat" + # Begin FreeBSD h_pass "$prog 8" + if true; then + h_fail "$prog 11" + else + # End FreeBSD h_fail "$prog 9" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case raw @@ -242,7 +362,15 @@ raw_body() prog="$(atf_get_srcdir)/h_raw" h_pass "$prog 9" + # Begin FreeBSD + if true; then + h_fail "$prog 12" + else + # End FreeBSD h_fail "$prog 10" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case read @@ -255,7 +383,15 @@ read_body() prog="$(atf_get_srcdir)/h_read" h_pass "$prog 1024" "echo foo |" + # Begin FreeBSD + if true; then + h_fail "$prog 1027" "echo bar |" + else + # End FreeBSD h_fail "$prog 1025" "echo bar |" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case readlink @@ -267,8 +403,17 @@ readlink_body() { prog="$(atf_get_srcdir)/h_readlink" + # Begin FreeBSD + if true; then + h_pass "$prog 512" + h_fail "$prog 523" + else + # End FreeBSD h_pass "$prog 1024" h_fail "$prog 1025" + # Begin FreeBSD + fi + # End FreeBSD } atf_test_case getcwd @@ -281,7 +426,13 @@ getcwd_body() prog="$(atf_get_srcdir)/h_getcwd" h_pass "$prog 1024" + # Begin FreeBSD + if false; then + # End FreeBSD h_fail "$prog 1025" + # Begin FreeBSD + fi + # End FreeBSD } atf_init_test_cases()