Skip to content

Commit c983643

Browse files
committed
emmalloc: use __heap_end instead of sbrk(0) (WebAssembly/wasi-libc#462)
1 parent 7ff6144 commit c983643

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

packages/emnapi/src/malloc/emmalloc/emmalloc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ _Noreturn void __assert_fail(const char *expr, const char *file, int line, const
7777

7878
// Defind by the linker to have the address of the start of the heap.
7979
extern unsigned char __heap_base;
80+
extern unsigned char __heap_end;
8081

8182
// Behavior of right shifting a signed integer is compiler implementation defined.
8283
static_assert((((int32_t)0x80000000U) >> 31) == -1, "This malloc implementation requires that right-shifting a signed integer produces a sign-extending (arithmetic) shift!");
@@ -566,9 +567,13 @@ static bool claim_more_memory(size_t numBytes)
566567
// If this is the first time we're called, see if we can use
567568
// the initial heap memory set up by wasm-ld.
568569
if (!listOfAllRegions) {
569-
unsigned char *heap_end = sbrk(0);
570-
if (numBytes <= (size_t)(heap_end - &__heap_base)) {
571-
startPtr = &__heap_base;
570+
unsigned char *heap_base = &__heap_base;
571+
unsigned char *heap_end = &__heap_end;
572+
if (heap_end < heap_base) {
573+
__builtin_trap();
574+
}
575+
if (numBytes <= (size_t)(heap_end - heap_base)) {
576+
startPtr = heap_base;
572577
endPtr = heap_end;
573578
break;
574579
}

0 commit comments

Comments
 (0)