@@ -188,14 +188,6 @@ typedef struct AllocBlockData
188188char * endptr ;/* end of space in this block */
189189}AllocBlockData ;
190190
191- /*
192- * Only the "hdrmask" field should be accessed outside this module.
193- * We keep the rest of an allocated chunk's header marked NOACCESS when using
194- * valgrind. But note that chunk headers that are in a freelist are kept
195- * accessible, for simplicity.
196- */
197- #define ALLOCCHUNK_PRIVATE_LEN offsetof(MemoryChunk, hdrmask)
198-
199191/*
200192 * AllocPointerIsValid
201193 *True iff pointer is valid allocation pointer.
@@ -777,8 +769,8 @@ AllocSetAlloc(MemoryContext context, Size size)
777769VALGRIND_MAKE_MEM_NOACCESS ((char * )MemoryChunkGetPointer (chunk )+ size ,
778770chunk_size - size );
779771
780- /* Disallowexternal access toprivate part of chunk header. */
781- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
772+ /* Disallow access tothe chunk header. */
773+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
782774
783775return MemoryChunkGetPointer (chunk );
784776}
@@ -801,6 +793,9 @@ AllocSetAlloc(MemoryContext context, Size size)
801793{
802794AllocFreeListLink * link = GetFreeListLink (chunk );
803795
796+ /* Allow access to the chunk header. */
797+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
798+
804799Assert (fidx == MemoryChunkGetValue (chunk ));
805800
806801/* pop this chunk off the freelist */
@@ -823,8 +818,8 @@ AllocSetAlloc(MemoryContext context, Size size)
823818VALGRIND_MAKE_MEM_NOACCESS ((char * )MemoryChunkGetPointer (chunk )+ size ,
824819GetChunkSizeFromFreeListIdx (fidx )- size );
825820
826- /* Disallowexternal access toprivate part of chunk header. */
827- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
821+ /* Disallow access tothe chunk header. */
822+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
828823
829824return MemoryChunkGetPointer (chunk );
830825}
@@ -989,8 +984,8 @@ AllocSetAlloc(MemoryContext context, Size size)
989984VALGRIND_MAKE_MEM_NOACCESS ((char * )MemoryChunkGetPointer (chunk )+ size ,
990985chunk_size - size );
991986
992- /* Disallowexternal access toprivate part of chunk header. */
993- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
987+ /* Disallow access tothe chunk header. */
988+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
994989
995990return MemoryChunkGetPointer (chunk );
996991}
@@ -1005,8 +1000,8 @@ AllocSetFree(void *pointer)
10051000AllocSet set ;
10061001MemoryChunk * chunk = PointerGetMemoryChunk (pointer );
10071002
1008- /* Allow access toprivate part of chunk header. */
1009- VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1003+ /* Allow access tothe chunk header. */
1004+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
10101005
10111006if (MemoryChunkIsExternal (chunk ))
10121007{
@@ -1115,8 +1110,8 @@ AllocSetRealloc(void *pointer, Size size)
11151110Size oldchksize ;
11161111int fidx ;
11171112
1118- /* Allow access toprivate part of chunk header. */
1119- VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1113+ /* Allow access tothe chunk header. */
1114+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
11201115
11211116if (MemoryChunkIsExternal (chunk ))
11221117{
@@ -1164,8 +1159,8 @@ AllocSetRealloc(void *pointer, Size size)
11641159block = (AllocBlock )realloc (block ,blksize );
11651160if (block == NULL )
11661161{
1167- /* Disallowexternal access toprivate part of chunk header. */
1168- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1162+ /* Disallow access tothe chunk header. */
1163+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
11691164return NULL ;
11701165}
11711166
@@ -1232,8 +1227,8 @@ AllocSetRealloc(void *pointer, Size size)
12321227/* Ensure any padding bytes are marked NOACCESS. */
12331228VALGRIND_MAKE_MEM_NOACCESS ((char * )pointer + size ,chksize - size );
12341229
1235- /* Disallowexternal access toprivate part of chunk header. */
1236- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1230+ /* Disallow access tothe chunk header . */
1231+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
12371232
12381233return pointer ;
12391234}
@@ -1305,8 +1300,8 @@ AllocSetRealloc(void *pointer, Size size)
13051300VALGRIND_MAKE_MEM_DEFINED (pointer ,size );
13061301#endif
13071302
1308- /* Disallowexternal access toprivate part of chunk header. */
1309- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1303+ /* Disallow access tothe chunk header. */
1304+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
13101305
13111306return pointer ;
13121307}
@@ -1332,8 +1327,8 @@ AllocSetRealloc(void *pointer, Size size)
13321327/* leave immediately if request was not completed */
13331328if (newPointer == NULL )
13341329{
1335- /* Disallowexternal access toprivate part of chunk header. */
1336- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1330+ /* Disallow access tothe chunk header. */
1331+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
13371332return NULL ;
13381333}
13391334
@@ -1374,11 +1369,17 @@ AllocSetGetChunkContext(void *pointer)
13741369AllocBlock block ;
13751370AllocSet set ;
13761371
1372+ /* Allow access to the chunk header. */
1373+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
1374+
13771375if (MemoryChunkIsExternal (chunk ))
13781376block = ExternalChunkGetBlock (chunk );
13791377else
13801378block = (AllocBlock )MemoryChunkGetBlock (chunk );
13811379
1380+ /* Disallow access to the chunk header. */
1381+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
1382+
13821383Assert (AllocBlockIsValid (block ));
13831384set = block -> aset ;
13841385
@@ -1396,16 +1397,27 @@ AllocSetGetChunkSpace(void *pointer)
13961397MemoryChunk * chunk = PointerGetMemoryChunk (pointer );
13971398int fidx ;
13981399
1400+ /* Allow access to the chunk header. */
1401+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
1402+
13991403if (MemoryChunkIsExternal (chunk ))
14001404{
14011405AllocBlock block = ExternalChunkGetBlock (chunk );
14021406
1407+ /* Disallow access to the chunk header. */
1408+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
1409+
14031410Assert (AllocBlockIsValid (block ));
1411+
14041412return block -> endptr - (char * )chunk ;
14051413}
14061414
14071415fidx = MemoryChunkGetValue (chunk );
14081416Assert (FreeListIdxIsValid (fidx ));
1417+
1418+ /* Disallow access to the chunk header. */
1419+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
1420+
14091421return GetChunkSizeFromFreeListIdx (fidx )+ ALLOC_CHUNKHDRSZ ;
14101422}
14111423
@@ -1471,7 +1483,10 @@ AllocSetStats(MemoryContext context,
14711483{
14721484AllocFreeListLink * link = GetFreeListLink (chunk );
14731485
1486+ /* Allow access to the chunk header. */
1487+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
14741488Assert (MemoryChunkGetValue (chunk )== fidx );
1489+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
14751490
14761491freechunks ++ ;
14771492freespace += chksz + ALLOC_CHUNKHDRSZ ;
@@ -1566,8 +1581,8 @@ AllocSetCheck(MemoryContext context)
15661581Size chsize ,
15671582dsize ;
15681583
1569- /* Allow access toprivate part of chunk header. */
1570- VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1584+ /* Allow access tothe chunk header. */
1585+ VALGRIND_MAKE_MEM_DEFINED (chunk ,ALLOC_CHUNKHDRSZ );
15711586
15721587if (MemoryChunkIsExternal (chunk ))
15731588{
@@ -1617,12 +1632,9 @@ AllocSetCheck(MemoryContext context)
16171632elog (WARNING ,"problem in alloc set %s: detected write past chunk end in block %p, chunk %p" ,
16181633name ,block ,chunk );
16191634
1620- /*
1621- * If chunk is allocated, disallow external access to private part
1622- * of chunk header.
1623- */
1635+ /* if chunk is allocated, disallow access to the chunk header */
16241636if (dsize != InvalidAllocSize )
1625- VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOCCHUNK_PRIVATE_LEN );
1637+ VALGRIND_MAKE_MEM_NOACCESS (chunk ,ALLOC_CHUNKHDRSZ );
16261638
16271639blk_data += chsize ;
16281640nchunks ++ ;