cb589a
commit 262275da43425ba2b8c240e47063e36b39167996
cb589a
Author: Mark Wielaard <mark@klomp.org>
cb589a
Date:   Wed Dec 12 13:55:01 2018 +0100
cb589a
cb589a
    Fix memcheck/tests/undef_malloc_args testcase.
cb589a
cb589a
diff --git a/coregrind/m_replacemalloc/vg_replace_malloc.c b/coregrind/m_replacemalloc/vg_replace_malloc.c
cb589a
index 28bdb4a..564829a 100644
cb589a
--- a/coregrind/m_replacemalloc/vg_replace_malloc.c
cb589a
+++ b/coregrind/m_replacemalloc/vg_replace_malloc.c
cb589a
@@ -216,9 +216,19 @@ static void init(void);
cb589a
    Apart of allowing memcheck to detect an error, the macro
cb589a
    TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED has no effect and
cb589a
    has a minimal cost for other tools replacing malloc functions.
cb589a
+
cb589a
+   Creating an "artificial" use of _x that works reliably is not entirely
cb589a
+   straightforward.  Simply comparing it against zero often produces no
cb589a
+   warning if _x contains at least one nonzero bit is defined, because
cb589a
+   Memcheck knows that the result of the comparison will be defined (cf
cb589a
+   expensiveCmpEQorNE).
cb589a
+
cb589a
+   Really we want to PCast _x, so as to create a value which is entirely
cb589a
+   undefined if any bit of _x is undefined.  But there's no portable way to do
cb589a
+   that.
cb589a
 */
cb589a
-#define TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED(x) \
cb589a
-   if ((ULong)x == 0) __asm__ __volatile__( "" ::: "memory" )
cb589a
+#define TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED(_x) \
cb589a
+   if ((UWord)(_x) == 0) __asm__ __volatile__( "" ::: "memory" )
cb589a
 
cb589a
 /*---------------------- malloc ----------------------*/
cb589a
 
cb589a
@@ -504,7 +514,7 @@ static void init(void);
cb589a
    void VG_REPLACE_FUNCTION_EZU(10040,soname,fnname) (void *zone, void *p)  \
cb589a
    { \
cb589a
       DO_INIT; \
cb589a
-      TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED((UWord) zone);	\
cb589a
+      TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED((UWord)zone ^ (UWord)p); \
cb589a
       MALLOC_TRACE(#fnname "(%p, %p)\n", zone, p ); \
cb589a
       if (p == NULL)  \
cb589a
          return; \
cb589a
diff --git a/memcheck/tests/undef_malloc_args.c b/memcheck/tests/undef_malloc_args.c
cb589a
index 99e2799..654d70d 100644
cb589a
--- a/memcheck/tests/undef_malloc_args.c
cb589a
+++ b/memcheck/tests/undef_malloc_args.c
cb589a
@@ -11,29 +11,29 @@ int main (int argc, char*argv[])
cb589a
 
cb589a
    {
cb589a
       size_t size = def_size;
cb589a
-      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, 1);
cb589a
+      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, sizeof(size));
cb589a
       p = malloc(size);
cb589a
    }
cb589a
 
cb589a
-   (void) VALGRIND_MAKE_MEM_UNDEFINED(&p, 1);
cb589a
+   (void) VALGRIND_MAKE_MEM_UNDEFINED(&p, sizeof(p));
cb589a
    new_p = realloc(p, def_size);
cb589a
 
cb589a
-   (void) VALGRIND_MAKE_MEM_UNDEFINED(&new_p, 1);
cb589a
+   (void) VALGRIND_MAKE_MEM_UNDEFINED(&new_p, sizeof(new_p));
cb589a
    new_p = realloc(new_p, def_size);
cb589a
 
cb589a
-   (void) VALGRIND_MAKE_MEM_UNDEFINED(&new_p, 1);
cb589a
+   (void) VALGRIND_MAKE_MEM_UNDEFINED(&new_p, sizeof(new_p));
cb589a
    free (new_p);
cb589a
 
cb589a
    {
cb589a
       size_t nmemb = 1;
cb589a
-      (void) VALGRIND_MAKE_MEM_UNDEFINED(&nmemb, 1);
cb589a
+      (void) VALGRIND_MAKE_MEM_UNDEFINED(&nmemb, sizeof(nmemb));
cb589a
       new_p = calloc(nmemb, def_size);
cb589a
       free (new_p);
cb589a
    }
cb589a
 #if 0
cb589a
    {
cb589a
       size_t alignment = 1;
cb589a
-      (void) VALGRIND_MAKE_MEM_UNDEFINED(&alignment, 1);
cb589a
+      (void) VALGRIND_MAKE_MEM_UNDEFINED(&alignment, sizeof(alignment));
cb589a
       new_p = memalign(alignment, def_size);
cb589a
       free(new_p);
cb589a
    }
cb589a
@@ -41,14 +41,14 @@ int main (int argc, char*argv[])
cb589a
    {
cb589a
       size_t nmemb = 16;
cb589a
       size_t size = def_size;
cb589a
-      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, 1);
cb589a
+      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, sizeof(size));
cb589a
       new_p = memalign(nmemb, size);
cb589a
       free(new_p);
cb589a
    }
cb589a
 
cb589a
    {
cb589a
       size_t size = def_size;
cb589a
-      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, 1);
cb589a
+      (void) VALGRIND_MAKE_MEM_UNDEFINED(&size, sizeof(size));
cb589a
       new_p = valloc(size);
cb589a
       free (new_p);
cb589a
    }