Re: Possible false valgrind error reports - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Possible false valgrind error reports
Date
Msg-id 1921895.1676406105@sss.pgh.pa.us
Whole thread Raw
In response to Possible false valgrind error reports  (Karina Litskevich <litskevichkarina@gmail.com>)
Responses Re: Possible false valgrind error reports
List pgsql-hackers
Karina Litskevich <litskevichkarina@gmail.com> writes:
> In 82d0a46ea32 AllocSetRealloc() was changed to allow decreasing size of
> external chunks and give memory back to the malloc pool. Two
> VALGRIND_MAKE_MEM_UNDEFINED() calls were not changed to work properly in the
> case of decreasing size: they can mark memory behind the new allocated
> memory
> UNDEFINED. If this memory was already allocated and initialized, it's
> expected
> to be DEFINED. So it can cause false valgrind error reports. I fixed it in
> 0001 patch.

Hmm, I see the concern: adjusting the Valgrind marking of bytes beyond the
newly-realloced block is wrong because it might tromp on memory allocated
in another way.  However, I'm not sure about the details of your patch.

The first hunk in 0001 doesn't seem quite right yet:

          * old allocation.
          */
 #ifdef USE_VALGRIND
-        if (oldsize > chunk->requested_size)
+        if (size > chunk->requested_size && oldsize > chunk->requested_size)
             VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
                                         oldsize - chunk->requested_size);
 #endif

If size < oldsize, aren't we still doing the wrong thing?  Seems like
maybe it has to be like

         if (size > chunk->requested_size && oldsize > chunk->requested_size)
             VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
                                         Min(size, oldsize) - chunk->requested_size);

          * allocation; it could have been as small as one byte.  We have to be
          * conservative and just mark the entire old portion DEFINED.
          */
-        VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
+        if (size >= oldsize)
+            VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
+        else
+            VALGRIND_MAKE_MEM_DEFINED(pointer, size);
 #endif

This is OK, though I wonder if it'd read better as

+        VALGRIND_MAKE_MEM_DEFINED(pointer, Min(size, oldsize));


I've not thought hard about whether I like the variable renaming proposed
in 0002.  I do suggest though that those comment changes are an integral
part of the bug fix and hence belong in 0001.

            regards, tom lane



pgsql-hackers by date:

Previous
From: "Jonathan S. Katz"
Date:
Subject: Re: User functions for building SCRAM secrets
Next
From: Andres Freund
Date:
Subject: We shouldn't signal process groups with SIGQUIT