From f3631c348f11f8adecc670fa61695c26c6d1360b Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 9 Jan 2023 09:51:04 -0800 Subject: [PATCH] Port the fix from #377 to emmmalloc. Avoid using sbrk(0) in emmalloc too. --- emmalloc/emmalloc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/emmalloc/emmalloc.c b/emmalloc/emmalloc.c index c98e42eb8..4d227b409 100644 --- a/emmalloc/emmalloc.c +++ b/emmalloc/emmalloc.c @@ -56,6 +56,7 @@ // Defind by the linker to have the address of the start of the heap. extern unsigned char __heap_base; +extern unsigned char __heap_end __attribute__((__weak__)); // Behavior of right shifting a signed integer is compiler implementation defined. static_assert((((int32_t)0x80000000U) >> 31) == -1, "This malloc implementation requires that right-shifting a signed integer produces a sign-extending (arithmetic) shift!"); @@ -545,7 +546,10 @@ static bool claim_more_memory(size_t numBytes) // If this is the first time we're called, see if we can use // the initial heap memory set up by wasm-ld. if (!listOfAllRegions) { - unsigned char *heap_end = sbrk(0); + unsigned char *heap_end = &__heap_end; + if (heap_end == NULL) + heap_end = (unsigned char*) ((size_t) &__heap_base + (PAGE_SIZE-1) & -PAGE_SIZE); + if (numBytes <= (size_t)(heap_end - &__heap_base)) { startPtr = &__heap_base; endPtr = heap_end;