sentinel-tweak.patch
text/x-patch
Filename: sentinel-tweak.patch
Type: text/x-patch
Part: 0
Patch
Format: unified
| File | + | − |
|---|---|---|
| src/backend/utils/mmgr/aset.c | 71 | 10 |
| src/backend/utils/mmgr/generation.c | 15 | 0 |
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index b6eeb8abab..e6d86a54d7 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -674,6 +674,15 @@ AllocSetDelete(MemoryContext context)
free(set);
}
+static Size pow2_size(Size size)
+{
+ Size s = 1;
+ while (s < size)
+ s *= 2;
+
+ return s;
+}
+
/*
* AllocSetAlloc
* Returns pointer to allocated memory of given size or NULL if
@@ -699,13 +708,16 @@ AllocSetAlloc(MemoryContext context, Size size)
AssertArg(AllocSetIsValid(set));
+ fprintf(stderr, "AllocSetAlloc %ld %ld %ld\n", size, pow2_size(size), pow2_size(size+1));
+ fflush(stderr);
+
/*
* If requested size exceeds maximum for chunks, allocate an entire block
* for this request.
*/
- if (size > set->allocChunkLimit)
+ if (size + 1 > set->allocChunkLimit)
{
- chunk_size = MAXALIGN(size);
+ chunk_size = MAXALIGN(size + 1);
blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
block = (AllocBlock) malloc(blksize);
if (block == NULL)
@@ -725,7 +737,16 @@ AllocSetAlloc(MemoryContext context, Size size)
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk_size)
+ {
set_sentinel(MemoryChunkGetPointer(chunk), size);
+ fprintf(stderr, "sentinel added\n");
+ fflush(stderr);
+ }
+ else
+ {
+ fprintf(stderr, "A sentinel not added\n");
+ fflush(stderr);
+ }
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
@@ -767,7 +788,7 @@ AllocSetAlloc(MemoryContext context, Size size)
* If one is found, remove it from the free list, make it again a member
* of the alloc set and return its data address.
*/
- fidx = AllocSetFreeIndex(size);
+ fidx = AllocSetFreeIndex(size+1);
chunk = set->freelist[fidx];
if (chunk != NULL)
{
@@ -784,7 +805,16 @@ AllocSetAlloc(MemoryContext context, Size size)
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < GetChunkSizeFromFreeListIdx(fidx))
+ {
set_sentinel(MemoryChunkGetPointer(chunk), size);
+ fprintf(stderr, "sentinel added\n");
+ fflush(stderr);
+ }
+ else
+ {
+ fprintf(stderr, "B sentinel not added\n");
+ fflush(stderr);
+ }
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
@@ -950,7 +980,16 @@ AllocSetAlloc(MemoryContext context, Size size)
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk_size)
+ {
set_sentinel(MemoryChunkGetPointer(chunk), size);
+ fprintf(stderr, "sentinel added\n");
+ fflush(stderr);
+ }
+ else
+ {
+ fprintf(stderr, "C sentinel not added\n");
+ fflush(stderr);
+ }
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
@@ -994,7 +1033,7 @@ AllocSetFree(void *pointer)
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < chunk_size)
if (!sentinel_ok(pointer, chunk->requested_size))
- elog(WARNING, "detected write past chunk end in %s %p",
+ elog(ERROR, "detected write past chunk end in %s %p",
set->header.name, chunk);
}
#endif
@@ -1034,7 +1073,7 @@ AllocSetFree(void *pointer)
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < GetChunkSizeFromFreeListIdx(fidx))
if (!sentinel_ok(pointer, chunk->requested_size))
- elog(WARNING, "detected write past chunk end in %s %p",
+ elog(ERROR, "detected write past chunk end in %s %p",
set->header.name, chunk);
#endif
@@ -1078,6 +1117,9 @@ AllocSetRealloc(void *pointer, Size size)
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
Size oldsize;
+ fprintf(stderr, "AllocSetRelloc %ld %ld %ld\n", size, pow2_size(size), pow2_size(size+1));
+ fflush(stderr);
+
/* Allow access to private part of chunk header. */
VALGRIND_MAKE_MEM_DEFINED(chunk, ALLOCCHUNK_PRIVATE_LEN);
@@ -1100,7 +1142,7 @@ AllocSetRealloc(void *pointer, Size size)
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < oldsize)
if (!sentinel_ok(pointer, chunk->requested_size))
- elog(WARNING, "detected write past chunk end in %s %p",
+ elog(ERROR, "detected write past chunk end in %s %p",
set->header.name, chunk);
#endif
@@ -1111,7 +1153,7 @@ AllocSetRealloc(void *pointer, Size size)
if (block->freeptr != block->endptr)
elog(ERROR, "could not find block containing chunk %p", chunk);
- chksize = MAXALIGN(size);
+ chksize = MAXALIGN(size+1);
/* Do the realloc */
blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
@@ -1163,7 +1205,16 @@ AllocSetRealloc(void *pointer, Size size)
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chksize)
+ {
set_sentinel(pointer, size);
+ fprintf(stderr, "sentinel added\n");
+ fflush(stderr);
+ }
+ else
+ {
+ fprintf(stderr, "D sentinel not added\n");
+ fflush(stderr);
+ }
#else /* !MEMORY_CONTEXT_CHECKING */
/*
@@ -1191,7 +1242,7 @@ AllocSetRealloc(void *pointer, Size size)
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < oldsize)
if (!sentinel_ok(pointer, chunk->requested_size))
- elog(WARNING, "detected write past chunk end in %s %p",
+ elog(ERROR, "detected write past chunk end in %s %p",
set->header.name, chunk);
#endif
@@ -1200,7 +1251,7 @@ AllocSetRealloc(void *pointer, Size size)
* allocated area already is >= the new size. (In particular, we will
* fall out here if the requested size is a decrease.)
*/
- if (oldsize >= size)
+ if (oldsize >= size + 1)
{
#ifdef MEMORY_CONTEXT_CHECKING
Size oldrequest = chunk->requested_size;
@@ -1227,7 +1278,17 @@ AllocSetRealloc(void *pointer, Size size)
/* set mark to catch clobber of "unused" space */
if (size < oldsize)
+ {
set_sentinel(pointer, size);
+
+ fprintf(stderr, "sentinel added\n");
+ fflush(stderr);
+ }
+ else
+ {
+ fprintf(stderr, "E sentinel not added\n");
+ fflush(stderr);
+ }
#else /* !MEMORY_CONTEXT_CHECKING */
/*
@@ -1532,7 +1593,7 @@ AllocSetCheck(MemoryContext context)
*/
if (dsize != InvalidAllocSize && dsize < chsize &&
!sentinel_ok(chunk, ALLOC_CHUNKHDRSZ + dsize))
- elog(WARNING, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
+ elog(ERROR, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
name, block, chunk);
/*
diff --git a/src/backend/utils/mmgr/generation.c b/src/backend/utils/mmgr/generation.c
index b39894ec94..2f1363967d 100644
--- a/src/backend/utils/mmgr/generation.c
+++ b/src/backend/utils/mmgr/generation.c
@@ -323,6 +323,15 @@ GenerationDelete(MemoryContext context)
free(context);
}
+static Size pow2_size(Size size)
+{
+ Size s = 1;
+ while (s < size)
+ s *= 2;
+
+ return s;
+}
+
/*
* GenerationAlloc
* Returns pointer to allocated memory of given size or NULL if
@@ -345,6 +354,9 @@ GenerationAlloc(MemoryContext context, Size size)
Size chunk_size = MAXALIGN(size);
Size required_size = chunk_size + Generation_CHUNKHDRSZ;
+ fprintf(stderr, "GenerationAlloc %ld %ld %ld\n", size, pow2_size(size), pow2_size(size+1));
+ fflush(stderr);
+
/* is it an over-sized chunk? if yes, allocate special block */
if (chunk_size > set->allocChunkLimit)
{
@@ -709,6 +721,9 @@ GenerationRealloc(void *pointer, Size size)
GenerationPointer newPointer;
Size oldsize;
+ fprintf(stderr, "GenerationRealloc %ld %ld %ld\n", size, pow2_size(size), pow2_size(size+1));
+ fflush(stderr);
+
/* Allow access to private part of chunk header. */
VALGRIND_MAKE_MEM_DEFINED(chunk, GENERATIONCHUNK_PRIVATE_LEN);