From 281297db9f4544c48a811442fce691dc91ca11a3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 24 Aug 2005 22:38:00 +0000 Subject: [PATCH] Fix a problem I introduced in r4208 when reducing the space used by heap blocks. The minimum size for redzones is now sizeof(void*), but I forgot to ensure this. Massif was asking for 0 byte redzones, and this was screwing things up on 64-bit platforms, and Massif was dying very quickly. This should fix bugs #111090 and #111285. The fact that Massif was this badly broken but there were only 2 bug reports indicates that not many people are using it, at least not on AMD64. I also added a regtest that does some basic malloc/realloc/free testing for Massif, which would have caught this problem. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4492 --- coregrind/m_mallocfree.c | 11 +++++++--- massif/tests/Makefile.am | 6 ++++++ massif/tests/basic_malloc.c | 31 ++++++++++++++++++++++++++++ massif/tests/basic_malloc.stderr.exp | 6 ++++++ massif/tests/basic_malloc.vgtest | 2 ++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 massif/tests/basic_malloc.c create mode 100644 massif/tests/basic_malloc.stderr.exp create mode 100644 massif/tests/basic_malloc.vgtest diff --git a/coregrind/m_mallocfree.c b/coregrind/m_mallocfree.c index 9a4a6dd23e..0bd0136599 100644 --- a/coregrind/m_mallocfree.c +++ b/coregrind/m_mallocfree.c @@ -59,9 +59,9 @@ typedef UChar UByte; /* Layout of an in-use block: this block total szB (sizeof(SizeT) bytes) - red zone bytes (depends on Arena.rz_szB, but > sizeof(void*)) + red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*)) (payload bytes) - red zone bytes (depends on Arena.rz_szB, but > sizeof(void*)) + red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*)) this block total szB (sizeof(SizeT) bytes) Layout of a block on the free list: @@ -375,7 +375,12 @@ void arena_init ( ArenaId aid, Char* name, SizeT rz_szB, SizeT min_sblock_szB ) SizeT i; Arena* a = arenaId_to_ArenaP(aid); - vg_assert(rz_szB < 128); // ensure reasonable size + // Ensure redzones are a reasonable size. They must always be at least + // the size of a pointer, for holding the prev/next pointer (see the layout + // details at the top of this file). + vg_assert(rz_szB < 128); + if (rz_szB < sizeof(void*)) rz_szB = sizeof(void*); + vg_assert((min_sblock_szB % VKI_PAGE_SIZE) == 0); a->name = name; a->clientmem = ( VG_AR_CLIENT == aid ? True : False ); diff --git a/massif/tests/Makefile.am b/massif/tests/Makefile.am index 0616d78024..025acc0dd2 100644 --- a/massif/tests/Makefile.am +++ b/massif/tests/Makefile.am @@ -1,7 +1,13 @@ noinst_SCRIPTS = filter_stderr EXTRA_DIST = $(noinst_SCRIPTS) \ + basic_malloc.stderr.exp basic_malloc.vgtest \ toobig-allocs.stderr.exp toobig-allocs.vgtest \ true_html.stderr.exp true_html.vgtest \ true_text.stderr.exp true_text.vgtest +AM_CFLAGS = $(WERROR) -Winline -Wall -Wshadow -g + +check_PROGRAMS = \ + basic_malloc + diff --git a/massif/tests/basic_malloc.c b/massif/tests/basic_malloc.c new file mode 100644 index 0000000000..7cb452c0a9 --- /dev/null +++ b/massif/tests/basic_malloc.c @@ -0,0 +1,31 @@ +// In 3.0.0, Massif was badly broken on 64-bit platforms because it asked +// zero-sized redzones, and the allocator was forgetting to round the size +// up to sizeof(void*), which is the minimum. This caused bugs #111090 and +// #111285. This test is just a gentle allocation exercise which was +// failing. + +#include +#include + +#define NN 100 + +int main(void) +{ + int i; + char* a[NN]; + + for (i = i; i < NN; i++) { + a[i] = malloc(i); + } + + for (i = i; i < NN; i++) { + a[i] = realloc(a[i], NN - i); + } + + for (i = i; i < NN; i++) { + free(a[i]); + } + + return 0; +} + diff --git a/massif/tests/basic_malloc.stderr.exp b/massif/tests/basic_malloc.stderr.exp new file mode 100644 index 0000000000..c3baf21931 --- /dev/null +++ b/massif/tests/basic_malloc.stderr.exp @@ -0,0 +1,6 @@ + + +Total spacetime: +heap: +heap admin: +stack(s): diff --git a/massif/tests/basic_malloc.vgtest b/massif/tests/basic_malloc.vgtest new file mode 100644 index 0000000000..c63d4817d0 --- /dev/null +++ b/massif/tests/basic_malloc.vgtest @@ -0,0 +1,2 @@ +prog: basic_malloc +cleanup: rm massif.*.* -- 2.47.3