mirror of
https://github.com/intel/llvm.git
synced 2026-01-23 16:06:39 +08:00
[lld][WebAssembly] Align __heap_base
__heap_base was not aligned. In practice, it will often be aligned
simply because it follows the stack, but when the stack is placed at the
beginning (with the --stack-first option), the __heap_base might be
unaligned. It could even be byte-aligned.
At least wasi-libc appears to expect that __heap_base is aligned:
659ff41456/dlmalloc/src/malloc.c (L5224)
While WebAssembly itself does not appear to require any alignment for
memory accesses, it is sometimes required when sharing a pointer
externally. For example, WASI might expect alignment up to 8:
https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-timestamp-u64
This issue got introduced with the addition of the --stack-first flag:
https://reviews.llvm.org/D46141
I suspect the lack of alignment wasn't intentional here.
Differential Revision: https://reviews.llvm.org/D106499
This commit is contained in:
@@ -43,6 +43,7 @@ using namespace llvm::wasm;
|
||||
namespace lld {
|
||||
namespace wasm {
|
||||
static constexpr int stackAlignment = 16;
|
||||
static constexpr int heapAlignment = 16;
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -310,9 +311,12 @@ void Writer::layoutMemory() {
|
||||
placeStack();
|
||||
|
||||
if (WasmSym::heapBase) {
|
||||
// Set `__heap_base` to directly follow the end of the stack or global data.
|
||||
// The fact that this comes last means that a malloc/brk implementation
|
||||
// can grow the heap at runtime.
|
||||
// Set `__heap_base` to follow the end of the stack or global data. The
|
||||
// fact that this comes last means that a malloc/brk implementation can
|
||||
// grow the heap at runtime.
|
||||
// We'll align the heap base here because memory allocators might expect
|
||||
// __heap_base to be aligned already.
|
||||
memoryPtr = alignTo(memoryPtr, heapAlignment);
|
||||
log("mem: heap base = " + Twine(memoryPtr));
|
||||
WasmSym::heapBase->setVA(memoryPtr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user