From 9aa0e64b7cbdc14d740fb20752fd5d19dd9b4cee Mon Sep 17 00:00:00 2001 From: Will Newton Date: Tue, 15 Oct 2013 14:43:26 +0100 Subject: tests: Update tests from glibc. --- Makefile.am | 9 ++++ tests/tst-memalign.c | 114 +++++++++++++++++++++++++++++++++++++++++++ tests/tst-posix_memalign.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ tests/tst-pvalloc.c | 99 +++++++++++++++++++++++++++++++++++++ tests/tst-realloc.c | 53 ++++++++++++++++++-- tests/tst-valloc.c | 108 +++++++++++++++++++++++++++++++++++------ 6 files changed, 480 insertions(+), 21 deletions(-) create mode 100644 tests/tst-memalign.c create mode 100644 tests/tst-posix_memalign.c create mode 100644 tests/tst-pvalloc.c diff --git a/Makefile.am b/Makefile.am index 04d7719..352f058 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,6 +38,9 @@ check_PROGRAMS = \ tests/tst-mallocfork \ tests/tst-mallocstate \ tests/tst-malloc-usable \ + tests/tst-memalign \ + tests/tst-posix_memalign \ + tests/tst-pvalloc \ tests/tst-realloc \ tests/tst-trim1 \ tests/tst-valloc @@ -55,6 +58,12 @@ tests_tst_mallocstate_LDADD = $(tests_ldadd) tests_tst_mallocstate_CFLAGS = $(tests_cflags) tests_tst_malloc_usable_LDADD = $(tests_ldadd) tests_tst_malloc_usable_CFLAGS = $(tests_cflags) +tests_tst_memalign_LDADD = $(tests_ldadd) +tests_tst_memalign_CFLAGS = $(tests_cflags) +tests_tst_posix_memalign_LDADD = $(tests_ldadd) +tests_tst_posix_memalign_CFLAGS = $(tests_cflags) +tests_tst_pvalloc_LDADD = $(tests_ldadd) +tests_tst_pvalloc_CFLAGS = $(tests_cflags) tests_tst_realloc_LDADD = $(tests_ldadd) tests_tst_realloc_CFLAGS = $(tests_cflags) tests_tst_trim1_LDADD = $(tests_ldadd) diff --git a/tests/tst-memalign.c b/tests/tst-memalign.c new file mode 100644 index 0000000..4c6b6c9 --- /dev/null +++ b/tests/tst-memalign.c @@ -0,0 +1,114 @@ +/* Test for memalign. + Copyright (C) 2013 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +static int errors = 0; + +static void +merror (const char *msg) +{ + ++errors; + printf ("Error: %s\n", msg); +} + +static int +do_test (void) +{ + void *p; + unsigned long pagesize = getpagesize (); + unsigned long ptrval; + int save; + + errno = 0; + + /* An attempt to allocate a huge value should return NULL and set + errno to ENOMEM. */ + p = memalign (sizeof (void *), -1); + + save = errno; + + if (p != NULL) + merror ("memalign (sizeof (void *), -1) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("memalign (sizeof (void *), -1) errno is not set correctly"); + + free (p); + + errno = 0; + + /* Test to expose integer overflow in malloc internals from BZ #15857. */ + p = memalign (pagesize, -pagesize); + + save = errno; + + if (p != NULL) + merror ("memalign (pagesize, -pagesize) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("memalign (pagesize, -pagesize) errno is not set correctly"); + + free (p); + + errno = 0; + + /* Test to expose integer overflow in malloc internals from BZ #16038. */ + p = memalign (-1, pagesize); + + save = errno; + + if (p != NULL) + merror ("memalign (-1, pagesize) succeeded."); + + if (p == NULL && save != EINVAL) + merror ("memalign (-1, pagesize) errno is not set correctly"); + + free (p); + + /* A zero-sized allocation should succeed with glibc, returning a + non-NULL value. */ + p = memalign (sizeof (void *), 0); + + if (p == NULL) + merror ("memalign (sizeof (void *), 0) failed."); + + free (p); + + /* Check the alignment of the returned pointer is correct. */ + p = memalign (0x100, 10); + + if (p == NULL) + merror ("memalign (0x100, 10) failed."); + + ptrval = (unsigned long) p; + + if ((ptrval & 0xff) != 0) + merror ("pointer is not aligned to 0x100"); + + free (p); + + return errors != 0; +} + +#define TEST_FUNCTION do_test () +#include "test-skeleton.c" diff --git a/tests/tst-posix_memalign.c b/tests/tst-posix_memalign.c new file mode 100644 index 0000000..6782378 --- /dev/null +++ b/tests/tst-posix_memalign.c @@ -0,0 +1,118 @@ +/* Test for posix_memalign. + Copyright (C) 2013 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +static int errors = 0; + +static void +merror (const char *msg) +{ + ++errors; + printf ("Error: %s\n", msg); +} + +static int +do_test (void) +{ + void *p; + int ret; + unsigned long pagesize = getpagesize (); + unsigned long ptrval; + + p = NULL; + + /* An attempt to allocate a huge value should return ENOMEM and + p should remain NULL. */ + ret = posix_memalign (&p, sizeof (void *), -1); + + if (ret != ENOMEM) + merror ("posix_memalign (&p, sizeof (void *), -1) succeeded."); + + if (ret == ENOMEM && p != NULL) + merror ("returned an error but pointer was modified"); + + free (p); + + p = NULL; + + /* Test to expose integer overflow in malloc internals from BZ #15857. */ + ret = posix_memalign (&p, pagesize, -pagesize); + + if (ret != ENOMEM) + merror ("posix_memalign (&p, pagesize, -pagesize) succeeded."); + + free (p); + + p = NULL; + + /* Test to expose integer overflow in malloc internals from BZ #16038. */ + ret = posix_memalign (&p, -1, pagesize); + + if (ret != EINVAL) + merror ("posix_memalign (&p, -1, pagesize) succeeded."); + + free (p); + + p = NULL; + + /* A zero-sized allocation should succeed with glibc, returning zero + and setting p to a non-NULL value. */ + ret = posix_memalign (&p, sizeof (void *), 0); + + if (ret != 0 || p == NULL) + merror ("posix_memalign (&p, sizeof (void *), 0) failed."); + + free (p); + + ret = posix_memalign (&p, 0x300, 10); + + if (ret != EINVAL) + merror ("posix_memalign (&p, 0x300, 10) succeeded."); + + ret = posix_memalign (&p, 0, 10); + + if (ret != EINVAL) + merror ("posix_memalign (&p, 0, 10) succeeded."); + + p = NULL; + + ret = posix_memalign (&p, 0x100, 10); + + if (ret != 0) + merror ("posix_memalign (&p, 0x100, 10) failed."); + + if (ret == 0 && p == NULL) + merror ("returned success but pointer is NULL"); + + ptrval = (unsigned long) p; + + if (ret == 0 && (ptrval & 0xff) != 0) + merror ("pointer is not aligned to 0x100"); + + free (p); + + return errors != 0; +} + +#define TEST_FUNCTION do_test () +#include "test-skeleton.c" diff --git a/tests/tst-pvalloc.c b/tests/tst-pvalloc.c new file mode 100644 index 0000000..7b741d0 --- /dev/null +++ b/tests/tst-pvalloc.c @@ -0,0 +1,99 @@ +/* Test for pvalloc. + Copyright (C) 2013 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +static int errors = 0; + +static void +merror (const char *msg) +{ + ++errors; + printf ("Error: %s\n", msg); +} + +static int +do_test (void) +{ + void *p; + unsigned long pagesize = getpagesize (); + unsigned long ptrval; + int save; + + errno = 0; + + /* An attempt to allocate a huge value should return NULL and set + errno to ENOMEM. */ + p = pvalloc (-1); + + save = errno; + + if (p != NULL) + merror ("pvalloc (-1) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("pvalloc (-1) errno is not set correctly"); + + free (p); + + errno = 0; + + /* Test to expose integer overflow in malloc internals from BZ #15855. */ + p = pvalloc (-pagesize); + + save = errno; + + if (p != NULL) + merror ("pvalloc (-pagesize) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("pvalloc (-pagesize) errno is not set correctly"); + + free (p); + + /* A zero-sized allocation should succeed with glibc, returning a + non-NULL value. */ + p = pvalloc (0); + + if (p == NULL) + merror ("pvalloc (0) failed."); + + free (p); + + /* Check the alignment of the returned pointer is correct. */ + p = pvalloc (32); + + if (p == NULL) + merror ("pvalloc (32) failed."); + + ptrval = (unsigned long) p; + + if ((ptrval & (pagesize - 1)) != 0) + merror ("returned pointer is not page aligned."); + + free (p); + + return errors != 0; +} + +#define TEST_FUNCTION do_test () +#include "test-skeleton.c" diff --git a/tests/tst-realloc.c b/tests/tst-realloc.c index e49c0c8..410c317 100644 --- a/tests/tst-realloc.c +++ b/tests/tst-realloc.c @@ -29,8 +29,8 @@ merror (const char *msg) printf ("Error: %s\n", msg); } -int -main (void) +static int +do_test (void) { void *p; unsigned char *c; @@ -38,25 +38,37 @@ main (void) errno = 0; + /* realloc (NULL, ...) behaves similarly to malloc (C89). */ p = realloc (NULL, -1); save = errno; if (p != NULL) - merror ("realloc (-1) succeeded."); + merror ("realloc (NULL, -1) succeeded."); + /* errno should be set to ENOMEM on failure (POSIX). */ if (p == NULL && save != ENOMEM) merror ("errno is not set correctly"); + errno = 0; + + /* realloc (NULL, ...) behaves similarly to malloc (C89). */ p = realloc (NULL, 10); + save = errno; + if (p == NULL) merror ("realloc (NULL, 10) failed."); + /* errno should be clear on success (POSIX). */ + if (p != NULL && save != 0) + merror ("errno is set but should not be"); + free (p); p = calloc (20, 1); if (p == NULL) merror ("calloc (20, 1) failed."); + /* Check increasing size preserves contents (C89). */ p = realloc (p, 200); if (p == NULL) merror ("realloc (p, 200) failed."); @@ -75,12 +87,13 @@ main (void) free (p); - p = malloc (100); + p = realloc (NULL, 100); if (p == NULL) - merror ("malloc (100) failed."); + merror ("realloc (NULL, 100) failed."); memset (p, 0xff, 100); + /* Check decreasing size preserves contents (C89). */ p = realloc (p, 16); if (p == NULL) merror ("realloc (p, 16) failed."); @@ -97,7 +110,37 @@ main (void) if (ok == 0) merror ("first 16 bytes were not correct"); + /* Check failed realloc leaves original untouched (C89). */ + c = realloc (p, -1); + if (c != NULL) + merror ("realloc (p, -1) succeeded."); + + c = p; + ok = 1; + + for (i = 0; i < 16; i++) + { + if (c[i] != 0xff) + ok = 0; + } + + if (ok == 0) + merror ("first 16 bytes were not correct after failed realloc"); + + /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */ + p = realloc (p, 0); + if (p != NULL) + merror ("realloc (p, 0) returned non-NULL."); + + /* realloc (NULL, 0) acts like malloc (0) (glibc). */ + p = realloc (NULL, 0); + if (p == NULL) + merror ("realloc (NULL, 0) returned NULL."); + free (p); return errors != 0; } + +#define TEST_FUNCTION do_test () +#include "test-skeleton.c" diff --git a/tests/tst-valloc.c b/tests/tst-valloc.c index 643a0dd..a126b40 100644 --- a/tests/tst-valloc.c +++ b/tests/tst-valloc.c @@ -1,23 +1,99 @@ -/* Test case by Stephen Tweedie . */ -#include -#include +/* Test for valloc. + Copyright (C) 2013 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include #include +#include +#include +#include -int -main (void) +static int errors = 0; + +static void +merror (const char *msg) +{ + ++errors; + printf ("Error: %s\n", msg); +} + +static int +do_test (void) { - char *p; - int pagesize = getpagesize (); - int i; + void *p; + unsigned long pagesize = getpagesize (); + unsigned long ptrval; + int save; + + errno = 0; + + /* An attempt to allocate a huge value should return NULL and set + errno to ENOMEM. */ + p = valloc (-1); + + save = errno; + + if (p != NULL) + merror ("valloc (-1) succeeded."); - p = valloc (pagesize); - i = (long int) p; + if (p == NULL && save != ENOMEM) + merror ("valloc (-1) errno is not set correctly"); - if ((i & (pagesize-1)) != 0) - { - fprintf (stderr, "Alignment problem: valloc returns %p\n", p); - exit (1); - } + free (p); - return 0; + errno = 0; + + /* Test to expose integer overflow in malloc internals from BZ #15856. */ + p = valloc (-pagesize); + + save = errno; + + if (p != NULL) + merror ("valloc (-pagesize) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("valloc (-pagesize) errno is not set correctly"); + + free (p); + + /* A zero-sized allocation should succeed with glibc, returning a + non-NULL value. */ + p = valloc (0); + + if (p == NULL) + merror ("valloc (0) failed."); + + free (p); + + /* Check the alignment of the returned pointer is correct. */ + p = valloc (32); + + if (p == NULL) + merror ("valloc (32) failed."); + + ptrval = (unsigned long) p; + + if ((ptrval & (pagesize - 1)) != 0) + merror ("returned pointer is not page aligned."); + + free (p); + + return errors != 0; } + +#define TEST_FUNCTION do_test () +#include "test-skeleton.c" -- cgit v1.2.3