wasmtime icon indicating copy to clipboard operation
wasmtime copied to clipboard

Support specifying stack slot alignments

Open bjorn3 opened this issue 2 years ago • 3 comments

Feature

See title.

Benefit

Correct alignment are necessary to avoid crashes on some architectures and code may depend on correct alignment. In addition rustc checks that the right alignment is used when dereferencing a raw pointer in debug mode. This breaks rayon which allocates a stack value with cacheline alignment and then takes a raw pointer which it later dereferences. See https://github.com/bjorn3/rustc_codegen_cranelift/issues/1381.

Implementation

Sort stackslots by alignment for better packing and then add padding as necessary and if the alignment of a stack slot exceeds the ABI stack alignment realign the stack at runtime.

Alternatives

Doing dynamic alignment at runtime in the cranelift ir producer. This is slower and has higher stack usage overhead.

bjorn3 avatar Jul 12 '23 19:07 bjorn3

Also necessary to fix https://github.com/bjorn3/rustc_codegen_cranelift/issues/1258.

bjorn3 avatar Jul 12 '23 19:07 bjorn3

Would you mind reopening this? It hasn't been fully resolved yet: https://github.com/bytecodealliance/wasmtime/pull/8635/files#r1603954465

bjorn3 avatar May 16 '24 19:05 bjorn3

Sure; to summarize here, the issue is that alignment is now correct with respect to the start of stack frame, but start of stack frame is only aligned as per ABI (e.g. 16 bytes on x86-64 and aarch64) so we need to dynamically align SP (it must be dynamic, we can't know statically anything more than the ABI guarantee).

cfallin avatar May 16 '24 21:05 cfallin