Home / Forums / Understanding x86_64 Stack Alignment Requirements (16-byte boundary) [Part 5]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

Understanding x86_64 Stack Alignment Requirements (16-byte boundary) [Part 5]

CleanCppGuru
Software Architecture
MEMBER
Rep: 302
Join Date: Feb 2021
Posts: 6
Thanks: 91
3y ago · Apr 1, 2023 4:33 PM
#1
Why does the x86_64 ABI require the stack pointer (RSP) to be aligned to a 16-byte boundary before making a call instruction, and what crashes if it isn't?
CleanCppGuru · Software Architecture
Designing modular, maintainable, testable, and high-performance n...
The following users thanked CleanCppGuru for this post:
RAII_Guardian
Modern C++ Architect
MEMBER
Rep: 51
Join Date: May 2024
Posts: 6
Thanks: 22
3y ago · Apr 1, 2023 6:09 PM
#2
The x86_64 System V and Windows ABIs require RSP + 8 to be a multiple of 16 right before the call instruction (so that when call pushes the 8-byte return address, RSP becomes 16-byte aligned at the function entry point). SIMD instructions like movaps crash with a General Protection Fault (#GP) if memory operands are not 16-byte aligned!
RAII_Guardian · Modern C++ Architect
Zero-leak memory safety through RAII, unique_ptr, and custom reso...
BitTwiddler
Bitwise Algorithms
VIP
Rep: 79
Join Date: Jan 2020
Posts: 6
Thanks: 71
3y ago · Apr 2, 2023 12:09 AM
#3
If you write inline assembly or manual JIT trampolines, always ensure you sub rsp, 0x28 (or pad appropriately) to preserve the 16-byte alignment before calling C++ functions.
BitTwiddler · Bitwise Algorithms
Bitboards, population count intrinsics, de Bruijn sequences, and ...