malloc: Fix issue with calloc memory possibly being non-zero
PATCH 1/1 malloc: Fix issue with calloc memory possibly being non-zero
Since we set #define MORECORE_CLEARS 1, the code assumes 'sbrk' always returns zero'd out memory. However since its possible that free() returns memory ack to sbrk() via malloc_trim we could possible get non-zero'd memory from sbrk() if it allocates back memory that was previously freed to it.
There are two possible solutions to this problem.
1. change #define MORECORE_CLEARS 0
2. memset to zero memory returned to sbrk.
We go with the second since the sbrk being called to free up memory should be pretty rare.
The following code problems an example test to show the issue. This test code was inserted right after the call to mem_malloc_init(). we could possible get non-zero'd memory from sbrk() if it allocates back memory that was previously freed to it.
There are two possible solutions to this problem.
1. change #define MORECORE_CLEARS 0
2. memset to zero memory returned to sbrk.
We go with the second since the sbrk being called to free up memory should be pretty rare.
The following code problems an example test to show the issue. This test code was inserted right after the call to mem_malloc_init().
...
u8 *p2;
int i;
printf("MALLOC TEST\n");
p1 = malloc(135176);
printf("P1 = %p\n", p1);
memset(p1, 0xab, 135176);
free(p1);
p2 = calloc(4097, 1);
printf("P2 = %p %p\n", p2, p2 + 4097);
for (i = 0; i < 4097; i++) {
if (p2[i] != 0)
printf("miscompare at byte %d got %x\n", i, p2[i]);
free(p2);
printf("END MALLOC TEST\n\n");...
--- common/dlmalloc.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
common/dlmalloc.c +7 -0
@@ -1511,6 +1511,13 @@ void *sbrk(ptrdiff_t increment)ulong old = mem_malloc_brk;ulong new = old + increment; +/* +* if we are giving memory back make sure we clear it out since +* we set MORECORE_CLEARS to 1 +*/ +if (increment <0 ) +memset ((void *)new,0 , -increment); +if ((new < mem_malloc_start) || (new > mem_malloc_end))return (void *)MORECORE_FAILURE ;
Signature · 1 line
1.7.2.3
PATCH 1/1 malloc: Fix issue with calloc memory possibly being non-zero
Quoted 1 line · On Monday, November 15, 2010 15:51:14 Kumar Gala wrote:
returns memory ack to
sbrk()viamalloc_trimwe could possible get
"ack" -> "back"
Quoted 1 line
The following code problems an example test to show the issue.
i dont understand this sentence
Quoted 8 lines
we
could possible get non-zero'd memory fromsbrk()if it allocates back memory that was previously freed to it.There are two possible solutions to this problem.
1. change #defineMORECORE_CLEARS0
2. memset to zero memory returned to sbrk.
Quoted 5 lines
We go with the second since the sbrk being called to free up memory should be pretty rare.
The following code problems an example test to show the issue. This test code was inserted right after the call to
mem_malloc_init().
this looks like the changelog is partially duplicated
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101115/1238cc42/attachment.pgp
PATCH 1/1 malloc: Fix issue with calloc memory possibly being non-zero
In reply to Kumar GalaDear Kumar Gala,
Quoted 5 lines · In message <[email protected]> you…
Since we set #define
MORECORE_CLEARS1, the code assumes 'sbrk' always returns zero'd out memory. However since its possible thatfree()returns memory ack tosbrk()viamalloc_trimwe could possible get non-zero'd memory fromsbrk()if it allocates back memory that was previously freed to it.
I confirm that the test case works fine here, too. So:
But the commit message needs serious rework before this can get applied.
Best regards,
Wolfgang Denk
Signature · 4 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de If ignorance is bliss, why aren't there more happy people?
PATCH 1/1 malloc: Fix issue with calloc memory possibly being non-zero
Quoted 9 lines · Wolfgang Denk
Dear Kumar Gala,
Quoted 5 lines · In message <[email protected]> you…
Since we set #define
MORECORE_CLEARS1, the code assumes 'sbrk' always returns zero'd out memory. However since its possible thatfree()returns memory ack tosbrk()viamalloc_trimwe could possible get non-zero'd memory fromsbrk()if it allocates back memory that was previously freed to it.
Quoted 7 lines
I confirm that the test case works fine here, too. So:
Tested-by Wolfgang Denk <[email protected]>But the commit message needs serious rework before this can get applied.
Oops, will fix and repost.
- k