Skip to content

Commit cbbf8c8

Browse files
authored
wasi-libc: use __heap_end if available (#14273)
The symbol was introduced in LLD 15.0.7, as a way to know how much memory can be allocated: llvm/llvm-project@1095870 WebAssembly/wasi-libc#377
1 parent 4b32917 commit cbbf8c8

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/libc/wasi/emmalloc/emmalloc.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
// Defind by the linker to have the address of the start of the heap.
5858
extern unsigned char __heap_base;
59+
extern unsigned char __heap_end __attribute__((__weak__));
5960

6061
// Behavior of right shifting a signed integer is compiler implementation defined.
6162
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)
545546
// If this is the first time we're called, see if we can use
546547
// the initial heap memory set up by wasm-ld.
547548
if (!listOfAllRegions) {
548-
unsigned char *heap_end = sbrk(0);
549+
unsigned char *heap_end = &__heap_end;
550+
if (heap_end == NULL)
551+
heap_end = sbrk(0);
552+
549553
if (numBytes <= (size_t)(heap_end - &__heap_base)) {
550554
startPtr = &__heap_base;
551555
endPtr = heap_end;

0 commit comments

Comments
 (0)