Skip to content

Commit

Permalink
[wasm-ld] Define a __heap_end symbol marking the end of allocated m…
Browse files Browse the repository at this point in the history
…emory.

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
  • Loading branch information
sunfishcode authored and tstellar committed Jan 11, 2023
1 parent 67fd0d2 commit 1095870
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lld/test/wasm/export-all.s
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ foo:
# CHECK-NEXT: - Name: __heap_base
# CHECK-NEXT: Kind: GLOBAL
# CHECK-NEXT: Index: 4
# CHECK-NEXT: - Name: __memory_base
# CHECK-NEXT: - Name: __heap_end
# CHECK-NEXT: Kind: GLOBAL
# CHECK-NEXT: Index: 5
# CHECK-NEXT: - Name: __table_base
# CHECK-NEXT: - Name: __memory_base
# CHECK-NEXT: Kind: GLOBAL
# CHECK-NEXT: Index: 6
# CHECK-NEXT: - Name: __table_base
# CHECK-NEXT: Kind: GLOBAL
# CHECK-NEXT: Index: 7
7 changes: 5 additions & 2 deletions lld/test/wasm/mutable-global-exports.s
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ _start:
# CHECK-ALL-NEXT: - Name: __heap_base
# CHECK-ALL-NEXT: Kind: GLOBAL
# CHECK-ALL-NEXT: Index: 5
# CHECK-ALL-NEXT: - Name: __memory_base
# CHECK-ALL-NEXT: - Name: __heap_end
# CHECK-ALL-NEXT: Kind: GLOBAL
# CHECK-ALL-NEXT: Index: 6
# CHECK-ALL-NEXT: - Name: __table_base
# CHECK-ALL-NEXT: - Name: __memory_base
# CHECK-ALL-NEXT: Kind: GLOBAL
# CHECK-ALL-NEXT: Index: 7
# CHECK-ALL-NEXT: - Name: __table_base
# CHECK-ALL-NEXT: Kind: GLOBAL
# CHECK-ALL-NEXT: Index: 8
# CHECK-ALL-NEXT: - Type: CODE
1 change: 1 addition & 0 deletions lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ static void createOptionalSymbols() {
if (!config->isPic) {
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
if (config->is64.value_or(false))
Expand Down
1 change: 1 addition & 0 deletions lld/wasm/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ DefinedData *WasmSym::dsoHandle;
DefinedData *WasmSym::dataEnd;
DefinedData *WasmSym::globalBase;
DefinedData *WasmSym::heapBase;
DefinedData *WasmSym::heapEnd;
DefinedData *WasmSym::initMemoryFlag;
GlobalSymbol *WasmSym::stackPointer;
GlobalSymbol *WasmSym::tlsBase;
Expand Down
11 changes: 7 additions & 4 deletions lld/wasm/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,14 @@ struct WasmSym {
// Symbol marking the end of the data and bss.
static DefinedData *dataEnd;

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

// __wasm_init_memory_flag
// Symbol whose contents are nonzero iff memory has already been initialized.
Expand Down
16 changes: 13 additions & 3 deletions lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,20 @@ void Writer::layoutMemory() {
Twine(maxMemorySetting));
memoryPtr = config->initialMemory;
}
out.memorySec->numMemoryPages =
alignTo(memoryPtr, WasmPageSize) / WasmPageSize;

memoryPtr = alignTo(memoryPtr, WasmPageSize);

out.memorySec->numMemoryPages = memoryPtr / WasmPageSize;
log("mem: total pages = " + Twine(out.memorySec->numMemoryPages));

if (WasmSym::heapEnd) {
// Set `__heap_end` to follow the end of the statically allocated linear
// memory. The fact that this comes last means that a malloc/brk
// implementation can grow the heap at runtime.
log("mem: heap end = " + Twine(memoryPtr));
WasmSym::heapEnd->setVA(memoryPtr);
}

if (config->maxMemory != 0) {
if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize))
error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
Expand All @@ -363,7 +373,7 @@ void Writer::layoutMemory() {
if (config->isPic)
max = maxMemorySetting;
else
max = alignTo(memoryPtr, WasmPageSize);
max = memoryPtr;
}
out.memorySec->maxMemoryPages = max / WasmPageSize;
log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages));
Expand Down

0 comments on commit 1095870

Please sign in to comment.