Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6a55c1

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 memorybeyond the new end of the chunk: it would mark data UNDEFINED as faras 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-validDEFINED data as UNDEFINED, causing false-positive valgrind reportslater. Also, any malloc bookkeeping data placed within this areamight 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, becausewhatever remains in the new chunk size will be marked NOACCESS below,and we expect realloc() to have taken care of marking the memorybeyond the new official end of the chunk.While we're here, also rename the function's "oldsize" variableto "oldchksize" to more clearly explain what it actually holds,namely the distance to the end of the chunk (that is, requested sizeplus 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 actuallytalking about the old requested size.Oversight in commitc477f3e. Back-patch to all supported branches,as that was, just in case anybody wants to do valgrind testing on backbranches.Karina LitskevichDiscussion:https://postgr.es/m/CACiT8iaAET-fmzjjZLjaJC4zwSJmrFyL7LAdHwaYyjjQOQ4hcg@mail.gmail.com
1 parent5bace41 commitf6a55c1

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
AllocSetset= (AllocSet)context;
10781078
AllocChunkchunk=AllocPointerGetChunk(pointer);
1079-
Sizeoldsize;
1079+
Sizeoldchksize;
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
#ifdefMEMORY_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
#ifdefMEMORY_CONTEXT_CHECKING
11561156
#ifdefRANDOMIZE_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
#ifdefUSE_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-
elseif (oldsize >=size)
1214+
elseif (oldchksize >=size)
12041215
{
12051216
#ifdefMEMORY_CONTEXT_CHECKING
12061217
Sizeoldrequest=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
AllocPointernewPointer;
1272+
Sizeoldsize;
12611273

12621274
/* allocate new chunk */
12631275
newPointer=AllocSetAlloc((MemoryContext)set,size);
@@ -1282,6 +1294,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
12821294
#ifdefMEMORY_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)

[8]ページ先頭

©2009-2025 Movatter.jp