Re: Possible false valgrind error reports

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Karina Litskevich <litskevichkarina(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Noah Misch <noah(at)leadboat(dot)com>
Subject: Re: Possible false valgrind error reports
Date: 2023-02-14 20:21:45
Message-ID: 1921895.1676406105@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Karina Litskevich <litskevichkarina(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2023-02-14 20:29:27 We shouldn't signal process groups with SIGQUIT
Previous Message Jonathan S. Katz 2023-02-14 20:19:22 Re: User functions for building SCRAM secrets