Skip to content

Commit 1095870

Browse files
sunfishcodetstellar
authored andcommitted
[wasm-ld] Define a __heap_end symbol marking the end of allocated memory.
Define a `__heap_end` symbol that marks the end of the memory region that starts at `__heap_base`. This will allow malloc implementations to know how much memory they can use at `__heap_base` even if someone has done a `memory.grow` before they can initialize their state. Differential Revision: https://reviews.llvm.org/D136110
1 parent 67fd0d2 commit 1095870

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

lld/test/wasm/export-all.s

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ foo:
4040
# CHECK-NEXT: - Name: __heap_base
4141
# CHECK-NEXT: Kind: GLOBAL
4242
# CHECK-NEXT: Index: 4
43-
# CHECK-NEXT: - Name: __memory_base
43+
# CHECK-NEXT: - Name: __heap_end
4444
# CHECK-NEXT: Kind: GLOBAL
4545
# CHECK-NEXT: Index: 5
46-
# CHECK-NEXT: - Name: __table_base
46+
# CHECK-NEXT: - Name: __memory_base
4747
# CHECK-NEXT: Kind: GLOBAL
4848
# CHECK-NEXT: Index: 6
49+
# CHECK-NEXT: - Name: __table_base
50+
# CHECK-NEXT: Kind: GLOBAL
51+
# CHECK-NEXT: Index: 7

lld/test/wasm/mutable-global-exports.s

+5-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ _start:
7979
# CHECK-ALL-NEXT: - Name: __heap_base
8080
# CHECK-ALL-NEXT: Kind: GLOBAL
8181
# CHECK-ALL-NEXT: Index: 5
82-
# CHECK-ALL-NEXT: - Name: __memory_base
82+
# CHECK-ALL-NEXT: - Name: __heap_end
8383
# CHECK-ALL-NEXT: Kind: GLOBAL
8484
# CHECK-ALL-NEXT: Index: 6
85-
# CHECK-ALL-NEXT: - Name: __table_base
85+
# CHECK-ALL-NEXT: - Name: __memory_base
8686
# CHECK-ALL-NEXT: Kind: GLOBAL
8787
# CHECK-ALL-NEXT: Index: 7
88+
# CHECK-ALL-NEXT: - Name: __table_base
89+
# CHECK-ALL-NEXT: Kind: GLOBAL
90+
# CHECK-ALL-NEXT: Index: 8
8891
# CHECK-ALL-NEXT: - Type: CODE

lld/wasm/Driver.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ static void createOptionalSymbols() {
681681
if (!config->isPic) {
682682
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
683683
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
684+
WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
684685
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
685686
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
686687
if (config->is64.value_or(false))

lld/wasm/Symbols.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ DefinedData *WasmSym::dsoHandle;
8383
DefinedData *WasmSym::dataEnd;
8484
DefinedData *WasmSym::globalBase;
8585
DefinedData *WasmSym::heapBase;
86+
DefinedData *WasmSym::heapEnd;
8687
DefinedData *WasmSym::initMemoryFlag;
8788
GlobalSymbol *WasmSym::stackPointer;
8889
GlobalSymbol *WasmSym::tlsBase;

lld/wasm/Symbols.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,14 @@ struct WasmSym {
538538
// Symbol marking the end of the data and bss.
539539
static DefinedData *dataEnd;
540540

541-
// __heap_base
542-
// Symbol marking the end of the data, bss and explicit stack. Any linear
543-
// memory following this address is not used by the linked code and can
544-
// therefore be used as a backing store for brk()/malloc() implementations.
541+
// __heap_base/__heap_end
542+
// Symbols marking the beginning and end of the "heap". It starts at the end
543+
// of the data, bss and explicit stack, and extends to the end of the linear
544+
// memory allocated by wasm-ld. This region of memory is not used by the
545+
// linked code, so it may be used as a backing store for `sbrk` or `malloc`
546+
// implementations.
545547
static DefinedData *heapBase;
548+
static DefinedData *heapEnd;
546549

547550
// __wasm_init_memory_flag
548551
// Symbol whose contents are nonzero iff memory has already been initialized.

lld/wasm/Writer.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,20 @@ void Writer::layoutMemory() {
340340
Twine(maxMemorySetting));
341341
memoryPtr = config->initialMemory;
342342
}
343-
out.memorySec->numMemoryPages =
344-
alignTo(memoryPtr, WasmPageSize) / WasmPageSize;
343+
344+
memoryPtr = alignTo(memoryPtr, WasmPageSize);
345+
346+
out.memorySec->numMemoryPages = memoryPtr / WasmPageSize;
345347
log("mem: total pages = " + Twine(out.memorySec->numMemoryPages));
346348

349+
if (WasmSym::heapEnd) {
350+
// Set `__heap_end` to follow the end of the statically allocated linear
351+
// memory. The fact that this comes last means that a malloc/brk
352+
// implementation can grow the heap at runtime.
353+
log("mem: heap end = " + Twine(memoryPtr));
354+
WasmSym::heapEnd->setVA(memoryPtr);
355+
}
356+
347357
if (config->maxMemory != 0) {
348358
if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize))
349359
error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
@@ -363,7 +373,7 @@ void Writer::layoutMemory() {
363373
if (config->isPic)
364374
max = maxMemorySetting;
365375
else
366-
max = alignTo(memoryPtr, WasmPageSize);
376+
max = memoryPtr;
367377
}
368378
out.memorySec->maxMemoryPages = max / WasmPageSize;
369379
log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages));

0 commit comments

Comments
 (0)