Commit Graph

17 Commits

Author SHA1 Message Date
YAMAMOTO Takashi
a3ef1520ec
dlmalloc: account the footprint of the initial heap (#496)
While malloc_stats and friends are disabled and unused for wasi-libc,
it's neater to be consistent.

Background: My colleagues for some reasons enabled malloc_stats
and asked me why it reports negative values.

Note: Depending __heap_base, init_top() adjusts the address for
alignment. I think the amount of this adjustment is reported as
"used" by malloc_stats. I don't bother to "fix" it.
2024-05-17 10:53:56 -07:00
Moritz Sichert
ba5318e9a4
Acquire the global lock before initializing malloc (#410)
In a multi-threaded execution we need to make sure that only exactly one
thread initializes malloc. The function try_init_allocator() can't
easily be made thread-safe, so just move the call to
try_init_allocator() inside the block that holds the lock.
2023-07-11 15:33:07 -07:00
YAMAMOTO Takashi
d8abbaac1b
dlmalloc: require __heap_end (#394)
This commit effectively drops the support of older wasm-ld. (LLVM <15.0.7).

We have two relevant use cases:

* `memory.grow` use outside of malloc
  (eg. used by polyfill preview1 binaries)

* `--init-memory` to somehow preallocate heap
  (eg. avoid dynamic allocations, especially on small environments)

While https://github.com/WebAssembly/wasi-libc/pull/377
fixed the former, it broke the latter if you are using
an older LLVM, which doesn't provide the `__heap_end` symbol,
to link your module.

As we couldn't come up with a solution which satisfies all parties,
this commit simply makes it require new enough LLVM which provides
`__heap_end`. After all, a link-time failure is more friendly to users
than failing later in a subtle way.
2023-07-11 15:32:08 -07:00
Cheng Shao
f2a35a454e
Use __builtin_ctz and __builtin_clz in dlmalloc (#401)
Co-authored-by: Ben Smith <binjimin@gmail.com>
2023-03-06 08:37:38 -08:00
Alex Crichton
f2aac5f3b1
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
2023-01-09 08:33:05 -08:00
Dan Gohman
322bd4ff9e Update to musl 1.2.2.
See the WHATSNEW file for details.
2021-03-18 11:18:41 -07:00
vms
d2482b786a
activate the use of __heap_base (#215) 2020-09-23 18:14:32 -07:00
Dan Gohman
0cc57ac7b4
Multi-license wasi-libc under Apache and MIT licenses. (#174)
Multi-license wasi-libc under the Apache-2.0 WITH LLVM-exception,
Apache-2.0, and MIT licenses.
2020-02-29 10:52:28 +01:00
Dan Gohman
12f5832b45
Convert more wasi-libc code to //-style comments. (#153)
This is purely a style change, in accordance with #116.
2020-01-16 16:00:37 -08:00
Dan Gohman
ec4549dabc
Temporarily disable the use of __heap_base. (#132)
This temporarily disables the feature in
a214f1c0b1, since it hits LLVM bug
43613, which is fixed on LLVM master and awaiting a backport to
the 9.0 branch.

https://bugs.llvm.org/show_bug.cgi?id=43613
2019-11-07 13:09:31 -08:00
vms
a214f1c0b1 Use __heap_base by dlmalloc (#114)
* add explicit allocator initialization

* move init to a better place

* fix warnings

* add __wasilibc_try_init_allocator

* move initialization to dlmalloc()

* fix typos

* fix identations

* follow style guide

* review changes
2019-11-05 12:52:51 -08:00
Dan Gohman
78b8651224
Remove -fno-builtin. (#104)
* Remove -fno-builtin.

-fno-builtin suppresses optimizations such as turning calls to `sqrt`
or `fabs` into `f64.sqrt` or `f64.abs` instructions inline. Libc code
itself benefits from these optimizations.

I originally added this flag because historically it was needed when
building libc to avoid the compiler pattern-matching the body of memcpy
into a memcpy call, however clang no longer requires this.

* Expand the comment about why we use USE_DL_PREFIX in dlmalloc.
2019-10-11 05:07:34 -07:00
Dan Gohman
c9a573306f Set the MALLOC_ALIGNMENT to 16.
This isn't strictly required, as wasm SIMD loads and stores work on
unaligned memory. However, it may provide better performance. That said,
this isn't currently studied by any benchmarking.
2019-09-07 07:22:18 -07:00
Dan Gohman
0df6243d52 Make calloc set ENOMEM when failing due to overflow.
This fixes a bug in upstream dlmalloc, where it doesn't set errno to
ENOMEM in overflow or footprint overrun cases.
2019-04-23 15:00:40 -07:00
Dan Gohman
7ba6adfc61 Fix miscellaneous lint warnings. 2019-04-15 09:47:16 -07:00
Dan Gohman
320054e84f WASI libc prototype implementation.
This incoporates pieces from musl-libc, cloudlibc, cloudabi, libpreopen,
and dlmalloc, as well as a significant amount of new code.
2019-03-27 07:59:55 -07:00
Dan Gohman
dbfccac2e4 Add a malloc implementation using dlmalloc. 2019-01-10 15:42:34 -08:00