1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-07 06:43:00 +03:00

Add some more tests to malloc to detect corruptions.

This commit is contained in:
Ulrich Drepper
2009-06-18 22:37:31 -07:00
parent d0a2af7106
commit f6887a0d9a
2 changed files with 31 additions and 3 deletions

View File

@@ -1,5 +1,8 @@
2009-06-18 Ulrich Drepper <drepper@redhat.com> 2009-06-18 Ulrich Drepper <drepper@redhat.com>
* malloc/malloc.c (_int_malloc): Add some consistency checks.
(_int_free): Likewise.
* sysdeps/unix/sysv/linux/bits/socket.h: Define PF_IEEE802154 and * sysdeps/unix/sysv/linux/bits/socket.h: Define PF_IEEE802154 and
AF_IEEE802154. AF_IEEE802154.
* sysdeps/unix/sysv/linux/sparc/bits/socket.h: Likewise. * sysdeps/unix/sysv/linux/sparc/bits/socket.h: Likewise.
@@ -83,7 +86,7 @@
Patch by Arnaud Ebalard <arno@natisbad.org>. Patch by Arnaud Ebalard <arno@natisbad.org>.
[BZ #10207] [BZ #10207]
* nss/getent.c: Add support for print gshadow data. * nss/getent.c: Add support for printing gshadow data.
[BZ #10203] [BZ #10203]
* nis/nss_nis/nis-pwd.c (internal_nis_endpwent): Free all buffers, * nis/nss_nis/nis-pwd.c (internal_nis_endpwent): Free all buffers,

View File

@@ -4241,6 +4241,8 @@ _int_malloc(mstate av, size_t bytes)
mchunkptr fwd; /* misc temp for linking */ mchunkptr fwd; /* misc temp for linking */
mchunkptr bck; /* misc temp for linking */ mchunkptr bck; /* misc temp for linking */
const char *errstr = NULL;
/* /*
Convert request size to internal form by adding SIZE_SZ bytes Convert request size to internal form by adding SIZE_SZ bytes
overhead plus possibly more to obtain necessary alignment and/or overhead plus possibly more to obtain necessary alignment and/or
@@ -4276,8 +4278,11 @@ _int_malloc(mstate av, size_t bytes)
#endif #endif
if (victim != 0) { if (victim != 0) {
if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0)) if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0))
malloc_printerr (check_action, "malloc(): memory corruption (fast)", {
chunk2mem (victim)); errstr = "malloc(): memory corruption (fast)";
errout:
malloc_printerr (check_action, errstr, chunk2mem (victim));
}
#ifndef ATOMIC_FASTBINS #ifndef ATOMIC_FASTBINS
*fb = victim->fd; *fb = victim->fd;
#endif #endif
@@ -4306,6 +4311,11 @@ _int_malloc(mstate av, size_t bytes)
malloc_consolidate(av); malloc_consolidate(av);
else { else {
bck = victim->bk; bck = victim->bk;
if (__builtin_expect (bck->fd != victim, 0))
{
errstr = "malloc(): smallbin double linked list corrupted";
goto errout;
}
set_inuse_bit_at_offset(victim, nb); set_inuse_bit_at_offset(victim, nb);
bin->bk = bck; bin->bk = bck;
bck->fd = bin; bck->fd = bin;
@@ -4515,6 +4525,11 @@ _int_malloc(mstate av, size_t bytes)
have to perform a complete insert here. */ have to perform a complete insert here. */
bck = unsorted_chunks(av); bck = unsorted_chunks(av);
fwd = bck->fd; fwd = bck->fd;
if (__builtin_expect (fwd->bk != bck, 0))
{
errstr = "malloc(): corrupted unsorted chunks";
goto errout;
}
remainder->bk = bck; remainder->bk = bck;
remainder->fd = fwd; remainder->fd = fwd;
bck->fd = remainder; bck->fd = remainder;
@@ -4610,6 +4625,11 @@ _int_malloc(mstate av, size_t bytes)
have to perform a complete insert here. */ have to perform a complete insert here. */
bck = unsorted_chunks(av); bck = unsorted_chunks(av);
fwd = bck->fd; fwd = bck->fd;
if (__builtin_expect (fwd->bk != bck, 0))
{
errstr = "malloc(): corrupted unsorted chunks 2";
goto errout;
}
remainder->bk = bck; remainder->bk = bck;
remainder->fd = fwd; remainder->fd = fwd;
bck->fd = remainder; bck->fd = remainder;
@@ -4901,6 +4921,11 @@ _int_free(mstate av, mchunkptr p)
bck = unsorted_chunks(av); bck = unsorted_chunks(av);
fwd = bck->fd; fwd = bck->fd;
if (__builtin_expect (fwd->bk != bck, 0))
{
errstr = "free(): corrupted unsorted chunks";
goto errout;
}
p->fd = fwd; p->fd = fwd;
p->bk = bck; p->bk = bck;
if (!in_smallbin_range(size)) if (!in_smallbin_range(size))