mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-22 21:10:07 +00:00
ea1bd74def
explicit_bzero(s, n) is the same as memset(s, 0, n), except that the compiler is not allowed to delete a call to explicit_bzero even if the memory pointed to by 's' is dead after the call. Right now, this effect is achieved externally by having explicit_bzero be a function whose semantics are unknown to the compiler, and internally, with a no-op asm statement that clobbers memory. This does mean that small explicit_bzero operations cannot be expanded inline as small memset operations can, but on the other hand, small memset operations do get deleted by the compiler. Hopefully full compiler support for explicit_bzero will happen relatively soon. There are two new tests: test-explicit_bzero.c verifies the visible semantics in the same way as the existing test-bzero.c, and tst-xbzero-opt.c verifies the not-being-optimized-out property. The latter is conceptually based on a test written by Matthew Dempsky for the OpenBSD regression suite. The crypt() implementation has an immediate use for this new feature. We avoid having to add a GLIBC_PRIVATE alias for explicit_bzero by running all of libcrypt's calls through the fortified variant, __explicit_bzero_chk, which is in the impl namespace anyway. Currently I'm not aware of anything in libc proper that needs this, but the glue is all in place if it does become necessary. The legacy DES implementation wasn't bothering to clear its buffers, so I added that, mostly for consistency's sake. * string/explicit_bzero.c: New routine. * string/test-explicit_bzero.c, string/tst-xbzero-opt.c: New tests. * string/Makefile (routines, strop-tests, tests): Add them. * string/test-memset.c: Add ifdeffage for testing explicit_bzero. * string/string.h [__USE_MISC]: Declare explicit_bzero. * debug/explicit_bzero_chk.c: New routine. * debug/Makefile (routines): Add it. * debug/tst-chk1.c: Test fortification of explicit_bzero. * string/bits/string3.h: Fortify explicit_bzero. * manual/string.texi: Document explicit_bzero. * NEWS: Mention addition of explicit_bzero. * crypt/crypt-entry.c (__crypt_r): Clear key-dependent intermediate data before returning, using explicit_bzero. * crypt/md5-crypt.c (__md5_crypt_r): Likewise. * crypt/sha256-crypt.c (__sha256_crypt_r): Likewise. * crypt/sha512-crypt.c (__sha512_crypt_r): Likewise. * include/string.h: Redirect internal uses of explicit_bzero to __explicit_bzero_chk[_internal]. * string/Versions [GLIBC_2.25]: Add explicit_bzero. * debug/Versions [GLIBC_2.25]: Add __explicit_bzero_chk. * sysdeps/arm/nacl/libc.abilist * sysdeps/unix/sysv/linux/aarch64/libc.abilist * sysdeps/unix/sysv/linux/alpha/libc.abilist * sysdeps/unix/sysv/linux/arm/libc.abilist * sysdeps/unix/sysv/linux/hppa/libc.abilist * sysdeps/unix/sysv/linux/i386/libc.abilist * sysdeps/unix/sysv/linux/ia64/libc.abilist * sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist * sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist * sysdeps/unix/sysv/linux/microblaze/libc.abilist * sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist * sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist * sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist * sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist * sysdeps/unix/sysv/linux/nios2/libc.abilist * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist * sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist * sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist * sysdeps/unix/sysv/linux/sh/libc.abilist * sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist * sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist * sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist * sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist * sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist * sysdeps/unix/sysv/linux/x86_64/64/libc.abilist * sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist: Add entries for explicit_bzero and __explicit_bzero_chk.
268 lines
5.7 KiB
C
268 lines
5.7 KiB
C
/* Test memset functions.
|
|
Copyright (C) 1999-2016 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Written by Jakub Jelinek <jakub@redhat.com>, 1999.
|
|
|
|
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
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#define TEST_MAIN
|
|
#ifdef TEST_BZERO
|
|
# ifdef TEST_EXPLICIT_BZERO
|
|
# define TEST_NAME "explicit_bzero"
|
|
# else
|
|
# define TEST_NAME "bzero"
|
|
# endif
|
|
#else
|
|
# ifndef WIDE
|
|
# define TEST_NAME "memset"
|
|
# else
|
|
# define TEST_NAME "wmemset"
|
|
# endif /* WIDE */
|
|
#endif /* !TEST_BZERO */
|
|
#define MIN_PAGE_SIZE 131072
|
|
#include "test-string.h"
|
|
|
|
#ifndef WIDE
|
|
# define MEMSET memset
|
|
# define CHAR char
|
|
# define UCHAR unsigned char
|
|
# define SIMPLE_MEMSET simple_memset
|
|
# define MEMCMP memcmp
|
|
# define BIG_CHAR CHAR_MAX
|
|
#else
|
|
# include <wchar.h>
|
|
# define MEMSET wmemset
|
|
# define CHAR wchar_t
|
|
# define UCHAR wchar_t
|
|
# define SIMPLE_MEMSET simple_wmemset
|
|
# define MEMCMP wmemcmp
|
|
# define BIG_CHAR WCHAR_MAX
|
|
#endif /* WIDE */
|
|
|
|
CHAR *SIMPLE_MEMSET (CHAR *, int, size_t);
|
|
|
|
#ifdef TEST_BZERO
|
|
typedef void (*proto_t) (char *, size_t);
|
|
void simple_bzero (char *, size_t);
|
|
void builtin_bzero (char *, size_t);
|
|
|
|
IMPL (simple_bzero, 0)
|
|
IMPL (builtin_bzero, 0)
|
|
#ifdef TEST_EXPLICIT_BZERO
|
|
IMPL (explicit_bzero, 1)
|
|
#else
|
|
IMPL (bzero, 1)
|
|
#endif
|
|
|
|
void
|
|
simple_bzero (char *s, size_t n)
|
|
{
|
|
SIMPLE_MEMSET (s, 0, n);
|
|
}
|
|
|
|
void
|
|
builtin_bzero (char *s, size_t n)
|
|
{
|
|
__builtin_bzero (s, n);
|
|
}
|
|
#else
|
|
typedef CHAR *(*proto_t) (CHAR *, int, size_t);
|
|
|
|
IMPL (SIMPLE_MEMSET, 0)
|
|
# ifndef WIDE
|
|
char *builtin_memset (char *, int, size_t);
|
|
IMPL (builtin_memset, 0)
|
|
# endif /* !WIDE */
|
|
IMPL (MEMSET, 1)
|
|
|
|
# ifndef WIDE
|
|
char *
|
|
builtin_memset (char *s, int c, size_t n)
|
|
{
|
|
return __builtin_memset (s, c, n);
|
|
}
|
|
# endif /* !WIDE */
|
|
#endif /* !TEST_BZERO */
|
|
|
|
CHAR *
|
|
inhibit_loop_to_libcall
|
|
SIMPLE_MEMSET (CHAR *s, int c, size_t n)
|
|
{
|
|
CHAR *r = s, *end = s + n;
|
|
while (r < end)
|
|
*r++ = c;
|
|
return s;
|
|
}
|
|
|
|
static void
|
|
do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n)
|
|
{
|
|
CHAR tstbuf[n];
|
|
#ifdef TEST_BZERO
|
|
simple_bzero (tstbuf, n);
|
|
CALL (impl, s, n);
|
|
if (memcmp (s, tstbuf, n) != 0)
|
|
#else
|
|
CHAR *res = CALL (impl, s, c, n);
|
|
if (res != s
|
|
|| SIMPLE_MEMSET (tstbuf, c, n) != tstbuf
|
|
|| MEMCMP (s, tstbuf, n) != 0)
|
|
#endif /* !TEST_BZERO */
|
|
{
|
|
error (0, 0, "Wrong result in function %s", impl->name);
|
|
ret = 1;
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void
|
|
do_test (size_t align, int c, size_t len)
|
|
{
|
|
align &= 7;
|
|
if ((align + len) * sizeof (CHAR) > page_size)
|
|
return;
|
|
|
|
FOR_EACH_IMPL (impl, 0)
|
|
do_one_test (impl, (CHAR *) (buf1) + align, c, len);
|
|
}
|
|
|
|
#ifndef TEST_BZERO
|
|
static void
|
|
do_random_tests (void)
|
|
{
|
|
size_t i, j, k, n, align, len, size;
|
|
int c, o;
|
|
UCHAR *p, *res;
|
|
UCHAR *p2 = (UCHAR *) buf2;
|
|
|
|
for (i = 0; i < 65536 / sizeof (CHAR); ++i)
|
|
p2[i] = random () & BIG_CHAR;
|
|
|
|
for (n = 0; n < ITERATIONS; n++)
|
|
{
|
|
if ((random () & 31) == 0)
|
|
size = 65536 / sizeof (CHAR);
|
|
else
|
|
size = 512;
|
|
p = (UCHAR *) (buf1 + page_size) - size;
|
|
len = random () & (size - 1);
|
|
align = size - len - (random () & 31);
|
|
if (align > size)
|
|
align = size - len;
|
|
if ((random () & 7) == 0)
|
|
align &= ~63;
|
|
if ((random () & 7) == 0)
|
|
c = 0;
|
|
else
|
|
c = random () & BIG_CHAR;
|
|
o = random () & BIG_CHAR;
|
|
if (o == c)
|
|
o = (c + 1) & BIG_CHAR;
|
|
j = len + align + 128;
|
|
if (j > size)
|
|
j = size;
|
|
if (align >= 128)
|
|
k = align - 128;
|
|
else
|
|
k = 0;
|
|
for (i = k; i < align; ++i)
|
|
p[i] = o;
|
|
for (i = align + len; i < j; ++i)
|
|
p[i] = o;
|
|
|
|
FOR_EACH_IMPL (impl, 1)
|
|
{
|
|
for (i = 0; i < len; ++i)
|
|
{
|
|
p[i + align] = p2[i];
|
|
if (p[i + align] == c)
|
|
p[i + align] = o;
|
|
}
|
|
res = (UCHAR *) CALL (impl, (CHAR *) p + align, c, len);
|
|
if (res != p + align)
|
|
{
|
|
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
|
|
n, impl->name, align, c, len, res, p + align);
|
|
ret = 1;
|
|
}
|
|
for (i = k; i < align; ++i)
|
|
if (p[i] != o)
|
|
{
|
|
error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
|
|
n, impl->name, align, c, len);
|
|
ret = 1;
|
|
break;
|
|
}
|
|
for (; i < align + len; ++i)
|
|
if (p[i] != c)
|
|
{
|
|
error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
|
|
n, impl->name, align, c, len);
|
|
ret = 1;
|
|
break;
|
|
}
|
|
for (; i < j; ++i)
|
|
if (p[i] != o)
|
|
{
|
|
error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
|
|
n, impl->name, align, c, len);
|
|
ret = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif /* !TEST_BZERO */
|
|
|
|
int
|
|
test_main (void)
|
|
{
|
|
size_t i;
|
|
int c = 0;
|
|
|
|
test_init ();
|
|
|
|
printf ("%24s", "");
|
|
FOR_EACH_IMPL (impl, 0)
|
|
printf ("\t%s", impl->name);
|
|
putchar ('\n');
|
|
|
|
#ifndef TEST_BZERO
|
|
for (c = -65; c <= 130; c += 65)
|
|
#endif
|
|
{
|
|
for (i = 0; i < 18; ++i)
|
|
do_test (0, c, 1 << i);
|
|
for (i = 1; i < 32; ++i)
|
|
{
|
|
do_test (i, c, i);
|
|
if (i & (i - 1))
|
|
do_test (0, c, i);
|
|
}
|
|
do_test (1, c, 14);
|
|
do_test (3, c, 1024);
|
|
do_test (4, c, 64);
|
|
do_test (2, c, 25);
|
|
}
|
|
|
|
#ifndef TEST_BZERO
|
|
do_random_tests ();
|
|
#endif
|
|
|
|
return ret;
|
|
}
|
|
|
|
#include "../test-skeleton.c"
|