1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-19 13:42:17 +03:00

Adjust Valgrind macro usage to protect chunk headers

Prior to this commit we only ever protected MemoryChunk's requested_size
field with Valgrind NOACCESS.  This means that if the hdrmask field is
ever accessed accidentally then we're not going to get any warnings from
Valgrind about it.  Valgrind would have warned us about the problem fixed
in 92957ed98 had we already been doing this.

Per suggestion from Tom Lane

Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/1650235.1672694719@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAApHDvr=FZNGbj252Z6M9BSFKoq6BMxgkQ2yEAGUYoo7RquqZg@mail.gmail.com
This commit is contained in:
David Rowley
2023-04-15 11:59:52 +12:00
parent 43a33ef54e
commit 414d66220a
5 changed files with 170 additions and 74 deletions

View File

@@ -31,6 +31,8 @@ AlignedAllocFree(void *pointer)
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
void *unaligned;
VALGRIND_MAKE_MEM_DEFINED(chunk, sizeof(MemoryChunk));
Assert(!MemoryChunkIsExternal(chunk));
/* obtain the original (unaligned) allocated pointer */
@@ -58,12 +60,17 @@ void *
AlignedAllocRealloc(void *pointer, Size size)
{
MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
Size alignto = MemoryChunkGetValue(redirchunk);
void *unaligned = MemoryChunkGetBlock(redirchunk);
Size alignto;
void *unaligned;
MemoryContext ctx;
Size old_size;
void *newptr;
VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
alignto = MemoryChunkGetValue(redirchunk);
unaligned = MemoryChunkGetBlock(redirchunk);
/* sanity check this is a power of 2 value */
Assert((alignto & (alignto - 1)) == 0);
@@ -110,11 +117,18 @@ AlignedAllocRealloc(void *pointer, Size size)
MemoryContext
AlignedAllocGetChunkContext(void *pointer)
{
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
MemoryContext cxt;
Assert(!MemoryChunkIsExternal(chunk));
VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
return GetMemoryChunkContext(MemoryChunkGetBlock(chunk));
Assert(!MemoryChunkIsExternal(redirchunk));
cxt = GetMemoryChunkContext(MemoryChunkGetBlock(redirchunk));
VALGRIND_MAKE_MEM_NOACCESS(redirchunk, sizeof(MemoryChunk));
return cxt;
}
/*
@@ -126,7 +140,15 @@ Size
AlignedAllocGetChunkSpace(void *pointer)
{
MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
void *unaligned = MemoryChunkGetBlock(redirchunk);
void *unaligned;
Size space;
return GetMemoryChunkSpace(unaligned);
VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
unaligned = MemoryChunkGetBlock(redirchunk);
space = GetMemoryChunkSpace(unaligned);
VALGRIND_MAKE_MEM_NOACCESS(redirchunk, sizeof(MemoryChunk));
return space;
}