Skip to content

Commit d8abbaa

Browse files
authored
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 #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.
1 parent bd950eb commit d8abbaa

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

.github/workflows/main.yml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,26 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest, macos-latest, windows-latest]
11-
# oldest and newest supported LLVM version
12-
clang_version: [10.0.0, 14.0.0]
11+
clang_version: [10.0.0]
12+
# use different LLVM versions among oses because of the lack of
13+
# official assets on github.
14+
include:
15+
- os: ubuntu-latest
16+
clang_version: 10.0.0
17+
llvm_asset_suffix: x86_64-linux-gnu-ubuntu-18.04
18+
- os: macos-latest
19+
clang_version: 10.0.0
20+
llvm_asset_suffix: x86_64-apple-darwin
21+
- os: windows-latest
22+
clang_version: 10.0.0
23+
- os: ubuntu-latest
24+
clang_version: 16.0.0
25+
llvm_asset_suffix: x86_64-linux-gnu-ubuntu-18.04
26+
- os: macos-latest
27+
clang_version: 15.0.7
28+
llvm_asset_suffix: x86_64-apple-darwin21.0
29+
- os: windows-latest
30+
clang_version: 16.0.0
1331
steps:
1432
- uses: actions/checkout@v1
1533
with:
@@ -44,8 +62,8 @@ jobs:
4462
- name: Install LLVM tools (MacOS)
4563
shell: bash
4664
run: |
47-
curl -sSfL https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.clang_version }}/clang+llvm-${{ matrix.clang_version }}-x86_64-apple-darwin.tar.xz | tar xJf -
48-
export CLANG_DIR=`pwd`/clang+llvm-${{ matrix.clang_version }}-x86_64-apple-darwin/bin
65+
curl -sSfL https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.clang_version }}/clang+llvm-${{ matrix.clang_version }}-${{ matrix.llvm_asset_suffix }}.tar.xz | tar xJf -
66+
export CLANG_DIR=`pwd`/clang+llvm-${{ matrix.clang_version }}-${{ matrix.llvm_asset_suffix }}/bin
4967
echo "$CLANG_DIR" >> $GITHUB_PATH
5068
echo "CC=$CLANG_DIR/clang" >> $GITHUB_ENV
5169
echo "AR=$CLANG_DIR/llvm-ar" >> $GITHUB_ENV
@@ -55,8 +73,8 @@ jobs:
5573
- name: Install LLVM tools (Linux)
5674
shell: bash
5775
run: |
58-
curl -sSfL https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.clang_version }}/clang+llvm-${{ matrix.clang_version }}-x86_64-linux-gnu-ubuntu-18.04.tar.xz | tar xJf -
59-
export CLANG_DIR=`pwd`/clang+llvm-${{ matrix.clang_version }}-x86_64-linux-gnu-ubuntu-18.04/bin
76+
curl -sSfL https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.clang_version }}/clang+llvm-${{ matrix.clang_version }}-${{ matrix.llvm_asset_suffix }}.tar.xz | tar xJf -
77+
export CLANG_DIR=`pwd`/clang+llvm-${{ matrix.clang_version }}-${{ matrix.llvm_asset_suffix }}/bin
6078
echo "$CLANG_DIR" >> $GITHUB_PATH
6179
echo "CLANG_DIR=$CLANG_DIR" >> $GITHUB_ENV
6280
echo "CC=$CLANG_DIR/clang" >> $GITHUB_ENV
@@ -76,12 +94,14 @@ jobs:
7694
run: |
7795
cd test
7896
make download
79-
export WASI_DIR=$(realpath $CLANG_DIR/../lib/clang/${{ matrix.clang_version }}/lib/wasi/)
97+
export WASI_DIR=$(realpath $($CLANG_DIR/clang -print-resource-dir)/lib/wasi/)
8098
mkdir -p $WASI_DIR
8199
cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR
82100
make test
83101
# The older version of Clang does not provide the expected symbol for the
84102
# test entrypoints: `undefined symbol: __main_argc_argv`.
103+
# The older (<15.0.7) version of wasm-ld does not provide `__heap_end`,
104+
# which is required by our malloc implementation.
85105
if: matrix.os == 'ubuntu-latest' && matrix.clang_version != '10.0.0'
86106

87107
- uses: actions/upload-artifact@v1

dlmalloc/src/malloc.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5215,7 +5215,7 @@ static void internal_inspect_all(mstate m,
52155215

52165216
/* Symbol marking the end of data, bss and explicit stack, provided by wasm-ld. */
52175217
extern char __heap_base;
5218-
extern char __heap_end __attribute__((__weak__));
5218+
extern char __heap_end;
52195219

52205220
/* Initialize the initial state of dlmalloc to be able to use free memory between __heap_base and initial. */
52215221
static void try_init_allocator(void) {
@@ -5227,23 +5227,18 @@ static void try_init_allocator(void) {
52275227

52285228
char *base = &__heap_base;
52295229
// Try to use the linker pseudo-symbol `__heap_end` for the initial size of
5230-
// the heap, but if that's not defined due to LLVM being too old perhaps then
5231-
// round up `base` to the nearest `PAGESIZE`. The initial size of linear
5232-
// memory will be at least the heap base to this page boundary, and it's then
5233-
// assumed that the initial linear memory image was truncated at that point.
5234-
// While this reflects the default behavior of `wasm-ld` it is also possible
5235-
// for users to craft larger linear memories by passing options to extend
5236-
// beyond this threshold. In this situation the memory will not be used for
5237-
// dlmalloc.
5238-
//
5239-
// Note that `sbrk(0)`, or in dlmalloc-ese `CALL_MORECORE(0)`, is specifically
5240-
// not used here. That captures the current size of the heap but is only
5241-
// correct if the we're the first to try to grow the heap. If the heap has
5242-
// grown elsewhere, such as a different allocator in place, then this would
5243-
// incorrectly claim such memroy as our own.
5230+
// the heap.
52445231
char *end = &__heap_end;
5245-
if (end == NULL)
5246-
end = (char*) page_align((size_t) base);
5232+
if (end < base) {
5233+
// "end" can be NULL when 1. you are using an old wasm-ld which doesn't
5234+
// provide `__heap_end` (< 15.0.7) and 2. something (other libraries
5235+
// or maybe your app?) includes a weak reference to `__heap_end` and
5236+
// 3. the weak reference is found by the linker before this strong
5237+
// reference.
5238+
//
5239+
// Note: This is a linker bug: https://github.com/llvm/llvm-project/issues/60829
5240+
__builtin_trap();
5241+
}
52475242
size_t initial_heap_size = end - base;
52485243

52495244
/* Check that initial heap is long enough to serve a minimal allocation request. */

expected/wasm32-wasi-threads/undefined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ __getf2
1313
__global_base
1414
__gttf2
1515
__heap_base
16+
__heap_end
1617
__imported_wasi_snapshot_preview1_args_get
1718
__imported_wasi_snapshot_preview1_args_sizes_get
1819
__imported_wasi_snapshot_preview1_clock_res_get

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ __floatunsitf
1111
__getf2
1212
__gttf2
1313
__heap_base
14+
__heap_end
1415
__imported_wasi_snapshot_preview1_args_get
1516
__imported_wasi_snapshot_preview1_args_sizes_get
1617
__imported_wasi_snapshot_preview1_clock_res_get

0 commit comments

Comments
 (0)