public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add valgrind support to alloc-pool.c
@ 2012-08-18  4:17 Andrew Pinski
  2012-08-18  7:57 ` Richard Guenther
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2012-08-18  4:17 UTC (permalink / raw)
  To: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

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.

[-- Attachment #2: implement-alloc-pool-valgrind.diff.txt --]
[-- Type: text/plain, Size: 2462 bytes --]

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)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add valgrind support to alloc-pool.c
  2012-08-18  4:17 [PATCH] Add valgrind support to alloc-pool.c Andrew Pinski
@ 2012-08-18  7:57 ` Richard Guenther
  2012-08-21 10:18   ` Richard Guenther
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Guenther @ 2012-08-18  7:57 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add valgrind support to alloc-pool.c
  2012-08-18  7:57 ` Richard Guenther
@ 2012-08-21 10:18   ` Richard Guenther
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Guenther @ 2012-08-21 10:18 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-08-21 10:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-18  4:17 [PATCH] Add valgrind support to alloc-pool.c Andrew Pinski
2012-08-18  7:57 ` Richard Guenther
2012-08-21 10:18   ` Richard Guenther

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).