]> git.proxmox.com Git - wasi-libc.git/commit
Don't use sbrk(0) to determine the initial heap size (#377)
authorAlex Crichton <alex@alexcrichton.com>
Mon, 9 Jan 2023 16:33:05 +0000 (10:33 -0600)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 2 Aug 2023 10:24:08 +0000 (12:24 +0200)
commite74d5f4330b376472384232de93a227f101ecb41
tree31091d123907e2014eed4e9308555198140e2521
parent22c1c5e8a57ad52eedc67a4708b06f46ef1562e7
Don't use sbrk(0) to determine the initial heap size (#377)

* Don't use sbrk(0) to determine the initial heap size

This commit changes the `try_init_allocator` function as part of
dlmalloc to not use `sbrk(0)` to determine the initial heap size. The
purpose of this function is to use the extra memory at the end of linear
memory for the initial allocation heap before `memory.grow` is used to
allocate more memory. To learn the extent of this region the code
previously would use `sbrk(0)` to find the current size of linear
memory. This does not work, however, when other systems have called
`memory.grow` before this function is called. For example if another
allocator is used or if another component of a wasm binary grows memory
for its own purposes then that memory will be incorrectly claimed to be
owned by dlmalloc.

Instead this commit rounds up the `__heap_base` address to the nearest
page size, since that must be allocatable. Otherwise anything above this
rounded address is assumed to be used by something else, even if it's
addressable.

* Use `__heap_end` if defined

* Move mstate initialization earlier
dlmalloc/src/malloc.c