i386: Fix va_arg access alignment for over-aligned stack arguments
Prevents misaligned memory accesses when using va_arg with over-aligned stack arguments on i386.
On i386 architectures, this commit corrects how va_arg accesses arguments on the stack when those arguments are over-aligned. Previously, GCC could generate code that read from misaligned addresses, leading to incorrect behavior or crashes, especially with optimizations. The fix ensures that the alignment claimed for accesses does not exceed the actual alignment of the stack slot.
In Details
This patch addresses an alignment issue in ix86_gimplify_va_arg on i386. The ix86_function_arg_boundary calculation uses TYPE_MAIN_VARIANT for alignment, but build_aligned_type in the GIMPLE construction for va_arg dereferences uses the declared alignment of a typedef, which can be greater. This leads to misaligned accesses. The commit clamps the alignment used in build_aligned_type to the actual arg_boundary, also handling cases where the declared alignment exceeds MAX_SUPPORTED_STACK_ALIGNMENT.
- va_arg
- A macro used in C to access arguments passed to a variadic function (a function that accepts a variable number of arguments).
- stack argument
- A function argument passed to a function via the call stack. The way arguments are passed (stack vs. registers) is part of the Application Binary Interface (ABI).
- over-aligned
- A data type whose required memory alignment is greater than the default alignment provided by the architecture or ABI for typical stack slots or structure members.
- GIMPLE
- GCC's internal three-address intermediate representation used for performing optimizations. Code is transformed into GIMPLE before optimization passes.
- typedef
- A keyword in C and C++ used to create an alias or a synonym for an existing type.