[clang] [clang][PowerPC] Keep a by-reference va_arg slot word-aligned on 32-bit SVR4 (PR #246992)

Matt Turner via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 29 08:40:31 PST 2026


https://github.com/mattst88 created https://github.com/llvm/llvm-project/pull/246992

A struct passed to a variadic function goes by reference: the overflow
area holds a 4-byte pointer to a copy. va_arg still rounded the overflow
pointer up to the struct's own alignment, so after an odd number of words
on the stack, va_arg of a struct containing a double read the slot after
the right one, and got the next argument's word as the pointer.

GCC and clang callers both store the pointer in the next word. Calling
vf(fmt, 1, 2, 3, 4, 5, 6, 7, 8, (struct { double d; }){9}), whose eighth
int is the one word on the stack before the pointer, read the wrong word
as the pointer whether the caller was built by GCC or clang.

Assisted-by: Claude Code



>From e32663233081fb3fe787a351e193e2726f6968ad Mon Sep 17 00:00:00 2001
From: Matt Turner <mattst88 at gmail.com>
Date: Wed, 25 Nov 2026 00:20:16 -0500
Subject: [PATCH] [clang][PowerPC] Keep a by-reference va_arg slot word-aligned
 on 32-bit SVR4

A struct passed to a variadic function goes by reference: the overflow
area holds a 4-byte pointer to a copy. va_arg still rounded the overflow
pointer up to the struct's own alignment, so after an odd number of words
on the stack, va_arg of a struct containing a double read the slot after
the right one, and got the next argument's word as the pointer.

GCC and clang callers both store the pointer in the next word. Calling
vf(fmt, 1, 2, 3, 4, 5, 6, 7, 8, (struct { double d; }){9}), whose eighth
int is the one word on the stack before the pointer, read the wrong word
as the pointer whether the caller was built by GCC or clang.

Assisted-by: Claude Code
---
 clang/lib/CodeGen/Targets/PPC.cpp               | 5 ++++-
 clang/test/CodeGen/PowerPC/ppc-varargs-struct.c | 7 +++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index de296680ce6af2..0a779588cc2f18 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -593,8 +593,11 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
     Address OverflowArea =
         Address(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), CGF.Int8Ty,
                 OverflowAreaAlign);
-    // Round up address of argument to alignment
+    // Round up address of argument to alignment. An indirect argument's slot
+    // holds only a pointer to it.
     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
+    if (isIndirect)
+      Align = OverflowAreaAlign;
     if (Align > OverflowAreaAlign) {
       llvm::Value *Ptr = OverflowArea.emitRawPointer(CGF);
       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
diff --git a/clang/test/CodeGen/PowerPC/ppc-varargs-struct.c b/clang/test/CodeGen/PowerPC/ppc-varargs-struct.c
index 119c5318428f83..fdc420f87d34ad 100644
--- a/clang/test/CodeGen/PowerPC/ppc-varargs-struct.c
+++ b/clang/test/CodeGen/PowerPC/ppc-varargs-struct.c
@@ -37,14 +37,13 @@ void testva (int n, ...)
 // CHECK-PPC-NEXT:  store i8 8, ptr [[GPRPTR]], align 4
 // CHECK-PPC-NEXT:  [[OVERFLOW_AREA_P:%[0-9]+]] = getelementptr inbounds nuw %struct.__va_list_tag, ptr [[ARRAYDECAY]], i32 0, i32 3
 // CHECK-PPC-NEXT:  [[OVERFLOW_AREA:%.+]] = load ptr, ptr [[OVERFLOW_AREA_P]], align 4
-// CHECK-PPC-NEXT: [[GEP_ALIGN:%[0-9]+]] = getelementptr inbounds i8, ptr %argp.cur, i32 7
-// CHECK-PPC-NEXT: %argp.cur.aligned = call ptr @llvm.ptrmask.p0.i32(ptr [[GEP_ALIGN]], i32 -8)
-// CHECK-PPC-NEXT:  [[NEW_OVERFLOW_AREA:%[0-9]+]] = getelementptr inbounds i8, ptr %argp.cur.aligned, i32 4
+// The overflow slot holds a pointer to the struct, so it is only word-aligned.
+// CHECK-PPC-NEXT:  [[NEW_OVERFLOW_AREA:%[0-9]+]] = getelementptr inbounds i8, ptr %argp.cur, i32 4
 // CHECK-PPC-NEXT:  store ptr [[NEW_OVERFLOW_AREA:%[0-9]+]], ptr [[OVERFLOW_AREA_P]], align 4
 // CHECK-PPC-NEXT:  br label %[[CONT]]
 //
 // CHECK-PPC:[[CONT]]
-// CHECK-PPC-NEXT:  [[VAARG_ADDR:%[a-z.0-9]+]] = phi ptr [ [[RAW_REGADDR]], %[[USING_REGS]] ], [ %argp.cur.aligned, %[[USING_OVERFLOW]] ]
+// CHECK-PPC-NEXT:  [[VAARG_ADDR:%[a-z.0-9]+]] = phi ptr [ [[RAW_REGADDR]], %[[USING_REGS]] ], [ %argp.cur, %[[USING_OVERFLOW]] ]
 // CHECK-PPC-NEXT:  [[AGGR:%[a-z0-9]+]] = load ptr, ptr [[VAARG_ADDR]]
 // CHECK-PPC-NEXT:  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %t, ptr align 8 [[AGGR]], i32 16, i1 false)
 



More information about the cfe-commits mailing list