diff mbox

Add valgrind support to alloc-pool.c

Message ID CA+=Sn1n46jJ6WYFBpXaPyyUf7Unsobw_9Q1D+MqeJ8=iNMGB6Q@mail.gmail.com
State New
Headers show

Commit Message

Andrew Pinski Aug. 18, 2012, 4:17 a.m. UTC
Hi,
  I implemented this patch almost 6 years ago when the df branch was
being worked on.  It adds valgrind support to  alloc-pool.c to catch
cases of using memory after free the memory.

OK?  Bootstrapped and tested on x86_64-linux-gnu with no regressions.

Thanks,
Andrew Pinski

ChangeLog:
* alloc-pool.c (pool_alloc): Add valgrind markers.
(pool_free): Likewise.

Comments

Richard Biener Aug. 18, 2012, 7:56 a.m. UTC | #1
On Sat, Aug 18, 2012 at 6:17 AM, Andrew Pinski <pinskia@gmail.com> wrote:
> Hi,
>   I implemented this patch almost 6 years ago when the df branch was
> being worked on.  It adds valgrind support to  alloc-pool.c to catch
> cases of using memory after free the memory.
>
> OK?  Bootstrapped and tested on x86_64-linux-gnu with no regressions.

Ok.

Thanks,
Richard.

> Thanks,
> Andrew Pinski
>
> ChangeLog:
> * alloc-pool.c (pool_alloc): Add valgrind markers.
> (pool_free): Likewise.
Richard Biener Aug. 21, 2012, 10:17 a.m. UTC | #2
On Sat, Aug 18, 2012 at 9:56 AM, Richard Guenther
<richard.guenther@gmail.com> wrote:
> On Sat, Aug 18, 2012 at 6:17 AM, Andrew Pinski <pinskia@gmail.com> wrote:
>> Hi,
>>   I implemented this patch almost 6 years ago when the df branch was
>> being worked on.  It adds valgrind support to  alloc-pool.c to catch
>> cases of using memory after free the memory.
>>
>> OK?  Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> Ok.

It doesn't work.  Did you check with valgrind checking?

/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c: In function 'void*
pool_alloc(alloc_pool)':
/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c:250:3: error: expected
primary-expression before 'int'
/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c:250:3: error: expected
')' before 'int'
/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c:250:3: error: expected
')' before ';' token
/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c:263:3: error: 'size'
was not declared in this scope
/space/rguenther/tramp3d/trunk/gcc/alloc-pool.c:303:7: error: 'size'
was not declared in this scope

that's because VALGRIND_DISCARD is not what you think it is.

Testing a fix ...

Richard.

> Thanks,
> Richard.
>
>> Thanks,
>> Andrew Pinski
>>
>> ChangeLog:
>> * alloc-pool.c (pool_alloc): Add valgrind markers.
>> (pool_free): Likewise.
diff mbox

Patch

Index: alloc-pool.c
===================================================================
--- alloc-pool.c	(revision 190500)
+++ alloc-pool.c	(working copy)
@@ -247,6 +247,7 @@  void *
 pool_alloc (alloc_pool pool)
 {
   alloc_pool_list header;
+  VALGRIND_DISCARD (int size);
 
   if (GATHER_STATISTICS)
     {
@@ -259,6 +260,7 @@  pool_alloc (alloc_pool pool)
     }
 
   gcc_checking_assert (pool);
+  VALGRIND_DISCARD (size = pool->elt_size - offsetof (allocation_object, u.data));
 
   /* If there are no more free elements, make some more!.  */
   if (!pool->returned_free_list)
@@ -298,6 +300,7 @@  pool_alloc (alloc_pool pool)
       /* Mark the element to be free.  */
       ((allocation_object *) block)->id = 0;
 #endif
+      VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header,size));
       pool->returned_free_list = header;
       pool->virgin_free_list += pool->elt_size;
       pool->virgin_elts_remaining--;
@@ -306,6 +309,7 @@  pool_alloc (alloc_pool pool)
 
   /* Pull the first free element from the free list, and return it.  */
   header = pool->returned_free_list;
+  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header, sizeof(*header)));
   pool->returned_free_list = header->next;
   pool->elts_free--;
 
@@ -313,6 +317,7 @@  pool_alloc (alloc_pool pool)
   /* Set the ID for element.  */
   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
 #endif
+  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header, size));
 
   return ((void *) header);
 }
@@ -322,6 +327,10 @@  void
 pool_free (alloc_pool pool, void *ptr)
 {
   alloc_pool_list header;
+#if defined(ENABLE_VALGRIND_CHECKING) || defined(ENABLE_CHECKING)
+  int size;
+  size = pool->elt_size - offsetof (allocation_object, u.data);
+#endif
 
 #ifdef ENABLE_CHECKING
   gcc_assert (ptr
@@ -330,7 +339,7 @@  pool_free (alloc_pool pool, void *ptr)
 	      /* Check whether the PTR was allocated from POOL.  */
 	      && pool->id == ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id);
 
-  memset (ptr, 0xaf, pool->elt_size - offsetof (allocation_object, u.data));
+  memset (ptr, 0xaf, size);
 
   /* Mark the element to be free.  */
   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id = 0;
@@ -339,6 +348,7 @@  pool_free (alloc_pool pool, void *ptr)
   header = (alloc_pool_list) ptr;
   header->next = pool->returned_free_list;
   pool->returned_free_list = header;
+  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr, size));
   pool->elts_free++;
 
   if (GATHER_STATISTICS)