|
From: <sv...@va...> - 2007-10-09 01:05:03
|
Author: njn
Date: 2007-10-09 02:05:04 +0100 (Tue, 09 Oct 2007)
New Revision: 6966
Log:
Fix a bug involving custom allocators. Added a test for it.
Added:
branches/MASSIF2/massif/tests/custom_alloc.c
branches/MASSIF2/massif/tests/custom_alloc.post.exp
branches/MASSIF2/massif/tests/custom_alloc.stderr.exp
branches/MASSIF2/massif/tests/custom_alloc.vgtest
Modified:
branches/MASSIF2/massif/ms_main.c
branches/MASSIF2/massif/tests/Makefile.am
Modified: branches/MASSIF2/massif/ms_main.c
===================================================================
--- branches/MASSIF2/massif/ms_main.c 2007-10-09 00:40:15 UTC (rev 6965)
+++ branches/MASSIF2/massif/ms_main.c 2007-10-09 01:05:04 UTC (rev 6966)
@@ -48,7 +48,6 @@
// (it aborts -- should give a warning and do something less drastic?)
// - write a good basic test that shows how the tool works, suitable for
// documentation
-// - Check MALLOCLIKE_BLOCK works, write regtest
// - make everything configurable, eg. min/max number of snapshots (which
// also determine culling proportion), frequency of detailed snapshots,
// etc.
@@ -149,7 +148,6 @@
//
// Tests:
// - tests/overloaded_new.cpp is there
-// - one involving MALLOCLIKE
//
//---------------------------------------------------------------------------
@@ -657,7 +655,7 @@
// Eg: alloc-fn1 / alloc-fn2 / a / b / main / (below main) / c
// becomes: a / b / main
static
-Int get_IPs( ThreadId tid, Bool is_custom_malloc, Addr ips[], Int max_ips)
+Int get_IPs( ThreadId tid, Bool is_custom_alloc, Addr ips[], Int max_ips)
{
Int n_ips, i, n_alloc_fns_removed = 0;
Int overestimate;
@@ -739,9 +737,9 @@
}
}
- // There must be at least one alloc function, unless client used
+ // There must be at least one alloc function, unless the client used
// MALLOCLIKE_BLOCK.
- if (!is_custom_malloc)
+ if (!is_custom_alloc)
tl_assert2(n_alloc_fns_removed > 0,
"n_alloc_fns_removed = %s\n", n_alloc_fns_removed);
@@ -763,14 +761,14 @@
}
// Gets an XCon and puts it in the tree. Returns the XCon's bottom-XPt.
-static XPt* get_XCon( ThreadId tid, Bool is_custom_malloc )
+static XPt* get_XCon( ThreadId tid, Bool is_custom_alloc )
{
static Addr ips[MAX_IPS]; // Static to minimise stack size.
Int i;
XPt* xpt = alloc_xpt;
// After this call, the IPs we want are in ips[0]..ips[n_ips-1].
- Int n_ips = get_IPs(tid, is_custom_malloc, ips, MAX_IPS);
+ Int n_ips = get_IPs(tid, is_custom_alloc, ips, MAX_IPS);
// Now do the search/insertion of the XCon. 'L' is the loop counter,
// being the index into ips[].
@@ -1326,7 +1324,7 @@
Bool is_zeroed )
{
HP_Chunk* hc;
- Bool custom_alloc = (NULL == p);
+ Bool is_custom_alloc = (NULL != p);
if (szB < 0) return NULL;
VERB(2, "<<< new_mem_heap (%lu)", szB);
@@ -1356,7 +1354,7 @@
// Update XTree, if necessary
if (clo_heap) {
- hc->where = get_XCon( tid, custom_alloc );
+ hc->where = get_XCon( tid, is_custom_alloc );
update_XCon(hc->where, szB);
}
VG_(HT_add_node)(malloc_list, hc);
@@ -1605,16 +1603,15 @@
void* res;
void* p = (void*)argv[1];
SizeT szB = argv[2];
- *ret = 0;
- res =
- new_block( tid, p, szB, /*alignB--ignored*/0, /*is_zeroed*/False );
+ res = new_block( tid, p, szB, /*alignB--ignored*/0, /*is_zeroed*/False );
tl_assert(res == p);
+ *ret = 0;
return True;
}
case VG_USERREQ__FREELIKE_BLOCK: {
- void* p = (void*)argv[1];
- *ret = 0;
+ void* p = (void*)argv[1];
die_block( p, /*custom_free*/True );
+ *ret = 0;
return True;
}
default:
Modified: branches/MASSIF2/massif/tests/Makefile.am
===================================================================
--- branches/MASSIF2/massif/tests/Makefile.am 2007-10-09 00:40:15 UTC (rev 6965)
+++ branches/MASSIF2/massif/tests/Makefile.am 2007-10-09 01:05:04 UTC (rev 6966)
@@ -10,6 +10,7 @@
basic.post.exp basic.stderr.exp basic.vgtest \
culling1.stderr.exp culling1.vgtest \
culling2.stderr.exp culling2.vgtest \
+ custom_alloc.post.exp custom_alloc.stderr.exp custom_alloc.vgtest
ignoring.post.exp ignoring.stderr.exp ignoring.vgtest \
long-time.post.exp long-time.stderr.exp long-time.vgtest \
null.post.exp null.stderr.exp null.vgtest \
@@ -28,10 +29,15 @@
AM_CFLAGS = $(WERROR) -Winline -Wall -Wshadow -g $(AM_FLAG_M3264_PRI)
+AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/include \
+ -I$(top_srcdir)/coregrind -I$(top_builddir)/include \
+ -I@VEX_DIR@/pub
+
check_PROGRAMS = \
alloc-fns \
basic \
culling1 culling2 \
+ custom_alloc \
ignoring \
long-time \
null \
Added: branches/MASSIF2/massif/tests/custom_alloc.c
===================================================================
--- branches/MASSIF2/massif/tests/custom_alloc.c (rev 0)
+++ branches/MASSIF2/massif/tests/custom_alloc.c 2007-10-09 01:05:04 UTC (rev 6966)
@@ -0,0 +1,73 @@
+#include <unistd.h>
+#include <sys/mman.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include "valgrind.h"
+
+#define SUPERBLOCK_SIZE 100000
+
+//-------------------------------------------------------------------------
+// Allocator
+//-------------------------------------------------------------------------
+
+void* get_superblock(void)
+{
+ void* p = mmap( 0, SUPERBLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
+ MAP_PRIVATE|MAP_ANON, -1, 0 );
+
+ assert(p != ((void*)(-1)));
+
+ return p;
+}
+
+// has a redzone
+static void* custom_alloc(int size)
+{
+#define RZ 8
+ static void* hp = 0; // current heap pointer
+ static void* hp_lim = 0; // maximum usable byte in current block
+ int size2 = size + RZ*2;
+ void* p;
+
+ if (hp + size2 > hp_lim) {
+ hp = get_superblock();
+ hp_lim = hp + SUPERBLOCK_SIZE - 1;
+ }
+
+ p = hp + RZ;
+ hp += size2;
+
+ VALGRIND_MALLOCLIKE_BLOCK( p, size, RZ, /*is_zeroed*/1 );
+ return (void*)p;
+}
+
+static void custom_free(void* p)
+{
+ // don't actually free any memory... but mark it as freed
+ VALGRIND_FREELIKE_BLOCK( p, RZ );
+}
+#undef RZ
+
+
+
+//-------------------------------------------------------------------------
+// Rest
+//-------------------------------------------------------------------------
+
+int main(void)
+{
+ int* a = custom_alloc(100);
+ custom_free(a);
+
+ a = custom_alloc(200);
+ custom_free(a);
+
+ a = malloc(100);
+ free(a);
+
+ a = malloc(200);
+ free(a);
+
+ return 0;
+}
Added: branches/MASSIF2/massif/tests/custom_alloc.post.exp
===================================================================
--- branches/MASSIF2/massif/tests/custom_alloc.post.exp (rev 0)
+++ branches/MASSIF2/massif/tests/custom_alloc.post.exp 2007-10-09 01:05:04 UTC (rev 6966)
@@ -0,0 +1,63 @@
+--------------------------------------------------------------------------------
+Command: ./custom_alloc
+Massif arguments: --stacks=no --time-unit=B
+ms_print arguments: massif.out
+--------------------------------------------------------------------------------
+
+
+ B
+ 208^ # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | # :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ | @ # : :
+ 0 +------@-----------------#---------------------------------------------->KB
+ 0 1.234
+
+Number of snapshots: 11
+ Detailed snapshots: [2, 5 (peak)]
+--------------------------------------------------------------------------------
+ n time(B) total(B) useful-heap(B) admin-heap(B) stacks(B)
+--------------------------------------------------------------------------------
+ 0 0 0 0 0 0
+ 1 108 108 100 8 0
+ 2 108 108 100 8 0
+92.59% (100B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
+->92.59% (100B) 0x80485A9: custom_alloc (custom_alloc.c:41)
+ ->92.59% (100B) 0x804862D: main (custom_alloc.c:60)
+
+--------------------------------------------------------------------------------
+ n time(B) total(B) useful-heap(B) admin-heap(B) stacks(B)
+--------------------------------------------------------------------------------
+ 3 216 0 0 0 0
+ 4 424 208 200 8 0
+ 5 424 208 200 8 0
+96.15% (200B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
+->96.15% (200B) 0x80485A9: custom_alloc (custom_alloc.c:41)
+ ->96.15% (200B) 0x804864B: main (custom_alloc.c:63)
+ |
+ ->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
+
+--------------------------------------------------------------------------------
+ n time(B) total(B) useful-heap(B) admin-heap(B) stacks(B)
+--------------------------------------------------------------------------------
+ 6 632 0 0 0 0
+ 7 740 108 100 8 0
+ 8 848 0 0 0 0
+ 9 1,056 208 200 8 0
+ 10 1,264 0 0 0 0
Added: branches/MASSIF2/massif/tests/custom_alloc.stderr.exp
===================================================================
--- branches/MASSIF2/massif/tests/custom_alloc.stderr.exp (rev 0)
+++ branches/MASSIF2/massif/tests/custom_alloc.stderr.exp 2007-10-09 01:05:04 UTC (rev 6966)
@@ -0,0 +1,2 @@
+
+
Added: branches/MASSIF2/massif/tests/custom_alloc.vgtest
===================================================================
--- branches/MASSIF2/massif/tests/custom_alloc.vgtest (rev 0)
+++ branches/MASSIF2/massif/tests/custom_alloc.vgtest 2007-10-09 01:05:04 UTC (rev 6966)
@@ -0,0 +1,4 @@
+prog: custom_alloc
+vgopts: --stacks=no --time-unit=B
+post: perl ../../massif/ms_print massif.out
+cleanup: rm massif.out
|