|
11 | 11 | * Portions Copyright (c) 1994, Regents of the University of California
|
12 | 12 | *
|
13 | 13 | * IDENTIFICATION
|
14 |
| - * $PostgreSQL: pgsql/src/backend/utils/mmgr/aset.c,v 1.76 2008/01/01 19:45:55 momjian Exp $ |
| 14 | + * $PostgreSQL: pgsql/src/backend/utils/mmgr/aset.c,v 1.77 2008/04/11 22:54:23 tgl Exp $ |
15 | 15 | *
|
16 | 16 | * NOTE:
|
17 | 17 | * This is a new (Feb. 05, 1999) implementation of the allocation set
|
@@ -282,6 +282,32 @@ AllocSetFreeIndex(Size size)
|
282 | 282 | return idx;
|
283 | 283 | }
|
284 | 284 |
|
| 285 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 286 | + |
| 287 | +/* |
| 288 | + * Fill a just-allocated piece of memory with "random" data. It's not really |
| 289 | + * very random, just a repeating sequence with a length that's prime. What |
| 290 | + * we mainly want out of it is to have a good probability that two palloc's |
| 291 | + * of the same number of bytes start out containing different data. |
| 292 | + */ |
| 293 | +static void |
| 294 | +randomize_mem(char *ptr, size_t size) |
| 295 | +{ |
| 296 | + static int save_ctr = 1; |
| 297 | + int ctr; |
| 298 | + |
| 299 | + ctr = save_ctr; |
| 300 | + while (size-- > 0) |
| 301 | + { |
| 302 | + *ptr++ = ctr; |
| 303 | + if (++ctr > 251) |
| 304 | + ctr = 1; |
| 305 | + } |
| 306 | + save_ctr = ctr; |
| 307 | +} |
| 308 | + |
| 309 | +#endif /* RANDOMIZE_ALLOCATED_MEMORY */ |
| 310 | + |
285 | 311 |
|
286 | 312 | /*
|
287 | 313 | * Public routines
|
@@ -552,6 +578,10 @@ AllocSetAlloc(MemoryContext context, Size size)
|
552 | 578 | if (size < chunk_size)
|
553 | 579 | ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
|
554 | 580 | #endif
|
| 581 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 582 | + /* fill the allocated space with junk */ |
| 583 | + randomize_mem((char *) AllocChunkGetPointer(chunk), size); |
| 584 | +#endif |
555 | 585 |
|
556 | 586 | /*
|
557 | 587 | * Stick the new block underneath the active allocation block, so that
|
@@ -596,6 +626,10 @@ AllocSetAlloc(MemoryContext context, Size size)
|
596 | 626 | if (size < chunk->size)
|
597 | 627 | ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
|
598 | 628 | #endif
|
| 629 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 630 | + /* fill the allocated space with junk */ |
| 631 | + randomize_mem((char *) AllocChunkGetPointer(chunk), size); |
| 632 | +#endif |
599 | 633 |
|
600 | 634 | /* isReset must be false already */
|
601 | 635 | Assert(!set->isReset);
|
@@ -752,6 +786,10 @@ AllocSetAlloc(MemoryContext context, Size size)
|
752 | 786 | if (size < chunk->size)
|
753 | 787 | ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
|
754 | 788 | #endif
|
| 789 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 790 | + /* fill the allocated space with junk */ |
| 791 | + randomize_mem((char *) AllocChunkGetPointer(chunk), size); |
| 792 | +#endif |
755 | 793 |
|
756 | 794 | set->isReset = false;
|
757 | 795 |
|
@@ -864,6 +902,13 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
|
864 | 902 | if (oldsize >= size)
|
865 | 903 | {
|
866 | 904 | #ifdef MEMORY_CONTEXT_CHECKING
|
| 905 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 906 | + /* We can only fill the extra space if we know the prior request */ |
| 907 | + if (size > chunk->requested_size) |
| 908 | + randomize_mem((char *) AllocChunkGetPointer(chunk) + chunk->requested_size, |
| 909 | + size - chunk->requested_size); |
| 910 | +#endif |
| 911 | + |
867 | 912 | chunk->requested_size = size;
|
868 | 913 | /* set mark to catch clobber of "unused" space */
|
869 | 914 | if (size < oldsize)
|
@@ -921,6 +966,12 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
|
921 | 966 | chunk->size = chksize;
|
922 | 967 |
|
923 | 968 | #ifdef MEMORY_CONTEXT_CHECKING
|
| 969 | +#ifdef RANDOMIZE_ALLOCATED_MEMORY |
| 970 | + /* We can only fill the extra space if we know the prior request */ |
| 971 | + randomize_mem((char *) AllocChunkGetPointer(chunk) + chunk->requested_size, |
| 972 | + size - chunk->requested_size); |
| 973 | +#endif |
| 974 | + |
924 | 975 | chunk->requested_size = size;
|
925 | 976 | /* set mark to catch clobber of "unused" space */
|
926 | 977 | if (size < chunk->size)
|
|
0 commit comments