public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gmail.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Add valgrind support to alloc-pool.c
Date: Sat, 18 Aug 2012 04:17:00 -0000	[thread overview]
Message-ID: <CA+=Sn1n46jJ6WYFBpXaPyyUf7Unsobw_9Q1D+MqeJ8=iNMGB6Q@mail.gmail.com> (raw)

[-- 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)

             reply	other threads:[~2012-08-18  4:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-18  4:17 Andrew Pinski [this message]
2012-08-18  7:57 ` Richard Guenther
2012-08-21 10:18   ` Richard Guenther

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+=Sn1n46jJ6WYFBpXaPyyUf7Unsobw_9Q1D+MqeJ8=iNMGB6Q@mail.gmail.com' \
    --to=pinskia@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).