Skip to content

Commit dc44180

Browse files
committed
Fix erroneous Valgrind markings in AllocSetRealloc.
If asked to decrease the size of a large (>8K) palloc chunk, AllocSetRealloc could improperly change the Valgrind state of memory beyond the new end of the chunk: it would mark data UNDEFINED as far as the old end of the chunk after having done the realloc(3) call, thus tromping on the state of memory that no longer belongs to it. One would normally expect that memory to now be marked NOACCESS, so that this mislabeling might prevent detection of later errors. If realloc() had chosen to move the chunk someplace else (unlikely, but well within its rights) we could also mismark perfectly-valid DEFINED data as UNDEFINED, causing false-positive valgrind reports later. Also, any malloc bookkeeping data placed within this area might now be wrongly marked, causing additional problems. Fix by replacing relevant uses of "oldsize" with "Min(size, oldsize)". It's sufficient to mark as far as "size" when that's smaller, because whatever remains in the new chunk size will be marked NOACCESS below, and we expect realloc() to have taken care of marking the memory beyond the new official end of the chunk. While we're here, also rename the function's "oldsize" variable to "oldchksize" to more clearly explain what it actually holds, namely the distance to the end of the chunk (that is, requested size plus trailing padding). This is more consistent with the use of "size" and "chksize" to hold the new requested size and chunk size. Add a new variable "oldsize" in the one stanza where we're actually talking about the old requested size. Oversight in commit c477f3e. Back-patch to all supported branches, as that was, just in case anybody wants to do valgrind testing on back branches. Karina Litskevich Discussion: https://postgr.es/m/CACiT8iaAET-fmzjjZLjaJC4zwSJmrFyL7LAdHwaYyjjQOQ4hcg@mail.gmail.com
1 parent 663e50e commit dc44180

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/backend/utils/mmgr/aset.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,22 +1076,22 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
10761076
{
10771077
AllocSet set = (AllocSet) context;
10781078
AllocChunk chunk = AllocPointerGetChunk(pointer);
1079-
Size oldsize;
1079+
Size oldchksize;
10801080

10811081
/* Allow access to private part of chunk header. */
10821082
VALGRIND_MAKE_MEM_DEFINED(chunk, ALLOCCHUNK_PRIVATE_LEN);
10831083

1084-
oldsize = chunk->size;
1084+
oldchksize = chunk->size;
10851085

10861086
#ifdef MEMORY_CONTEXT_CHECKING
10871087
/* Test for someone scribbling on unused space in chunk */
1088-
if (chunk->requested_size < oldsize)
1088+
if (chunk->requested_size < oldchksize)
10891089
if (!sentinel_ok(pointer, chunk->requested_size))
10901090
elog(WARNING, "detected write past chunk end in %s %p",
10911091
set->header.name, chunk);
10921092
#endif
10931093

1094-
if (oldsize > set->allocChunkLimit)
1094+
if (oldchksize > set->allocChunkLimit)
10951095
{
10961096
/*
10971097
* The chunk must have been allocated as a single-chunk block. Use
@@ -1111,7 +1111,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11111111
if (block->aset != set ||
11121112
block->freeptr != block->endptr ||
11131113
block->freeptr != ((char *) block) +
1114-
(oldsize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
1114+
(oldchksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
11151115
elog(ERROR, "could not find block containing chunk %p", chunk);
11161116

11171117
/*
@@ -1154,21 +1154,29 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11541154

11551155
#ifdef MEMORY_CONTEXT_CHECKING
11561156
#ifdef RANDOMIZE_ALLOCATED_MEMORY
1157-
/* We can only fill the extra space if we know the prior request */
1157+
1158+
/*
1159+
* We can only randomize the extra space if we know the prior request.
1160+
* When using Valgrind, randomize_mem() also marks memory UNDEFINED.
1161+
*/
11581162
if (size > chunk->requested_size)
11591163
randomize_mem((char *) pointer + chunk->requested_size,
11601164
size - chunk->requested_size);
1161-
#endif
1165+
#else
11621166

11631167
/*
1164-
* realloc() (or randomize_mem()) will have left any newly-allocated
1165-
* part UNDEFINED, but we may need to adjust trailing bytes from the
1166-
* old allocation.
1168+
* If this is an increase, realloc() will have marked any
1169+
* newly-allocated part (from oldchksize to chksize) UNDEFINED, but we
1170+
* also need to adjust trailing bytes from the old allocation (from
1171+
* chunk->requested_size to oldchksize) as they are marked NOACCESS.
1172+
* Make sure not to mark too many bytes in case chunk->requested_size
1173+
* < size < oldchksize.
11671174
*/
11681175
#ifdef USE_VALGRIND
1169-
if (oldsize > chunk->requested_size)
1176+
if (Min(size, oldchksize) > chunk->requested_size)
11701177
VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
1171-
oldsize - chunk->requested_size);
1178+
Min(size, oldchksize) - chunk->requested_size);
1179+
#endif
11721180
#endif
11731181

11741182
chunk->requested_size = size;
@@ -1179,11 +1187,14 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11791187
#else /* !MEMORY_CONTEXT_CHECKING */
11801188

11811189
/*
1182-
* We don't know how much of the old chunk size was the actual
1183-
* allocation; it could have been as small as one byte. We have to be
1184-
* conservative and just mark the entire old portion DEFINED.
1190+
* We may need to adjust marking of bytes from the old allocation as
1191+
* some of them may be marked NOACCESS. We don't know how much of the
1192+
* old chunk size was the requested size; it could have been as small
1193+
* as one byte. We have to be conservative and just mark the entire
1194+
* old portion DEFINED. Make sure not to mark memory beyond the new
1195+
* allocation in case it's smaller than the old one.
11851196
*/
1186-
VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
1197+
VALGRIND_MAKE_MEM_DEFINED(pointer, Min(size, oldchksize));
11871198
#endif
11881199

11891200
/* Ensure any padding bytes are marked NOACCESS. */
@@ -1200,7 +1211,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12001211
* allocated area already is >= the new size. (In particular, we will
12011212
* fall out here if the requested size is a decrease.)
12021213
*/
1203-
else if (oldsize >= size)
1214+
else if (oldchksize >= size)
12041215
{
12051216
#ifdef MEMORY_CONTEXT_CHECKING
12061217
Size oldrequest = chunk->requested_size;
@@ -1223,10 +1234,10 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12231234
size - oldrequest);
12241235
else
12251236
VALGRIND_MAKE_MEM_NOACCESS((char *) pointer + size,
1226-
oldsize - size);
1237+
oldchksize - size);
12271238

12281239
/* set mark to catch clobber of "unused" space */
1229-
if (size < oldsize)
1240+
if (size < oldchksize)
12301241
set_sentinel(pointer, size);
12311242
#else /* !MEMORY_CONTEXT_CHECKING */
12321243

@@ -1235,7 +1246,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12351246
* the old request or shrinking it, so we conservatively mark the
12361247
* entire new allocation DEFINED.
12371248
*/
1238-
VALGRIND_MAKE_MEM_NOACCESS(pointer, oldsize);
1249+
VALGRIND_MAKE_MEM_NOACCESS(pointer, oldchksize);
12391250
VALGRIND_MAKE_MEM_DEFINED(pointer, size);
12401251
#endif
12411252

@@ -1258,6 +1269,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12581269
* memory indefinitely. See pgsql-hackers archives for 2007-08-11.)
12591270
*/
12601271
AllocPointer newPointer;
1272+
Size oldsize;
12611273

12621274
/* allocate new chunk */
12631275
newPointer = AllocSetAlloc((MemoryContext) set, size);
@@ -1282,6 +1294,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12821294
#ifdef MEMORY_CONTEXT_CHECKING
12831295
oldsize = chunk->requested_size;
12841296
#else
1297+
oldsize = oldchksize;
12851298
VALGRIND_MAKE_MEM_DEFINED(pointer, oldsize);
12861299
#endif
12871300

0 commit comments

Comments
 (0)