[gcc r16-9286] cfgexpand: Align the whole asan var block to crtl->stack_alignment_needed [PR120201]

Jakub Jelinek via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:db1b9f9d978a0708e684cd33b5ba8da3accf541d

commit r16-9286-gdb1b9f9d978a0708e684cd33b5ba8da3accf541d
Author: Jakub Jelinek <[email protected]>
Date:   Tue Jul 14 10:37:03 2026 +0200

    cfgexpand: Align the whole asan var block to crtl->stack_alignment_needed [PR120201]
    
    If crtl->stack_alignment_needed / BITS_PER_UNIT is larger than
    data.asan_alignb, we can end up with misaligned stack for further
    allocations (e.g. to spill SSA_NAMEs with vector modes and large
    alignment).
    
    This patch increments data.asan_alignb in that case.
    
    2026-07-14  Jakub Jelinek  <[email protected]>
                H.J. Lu  <[email protected]>
    
            PR tree-optimization/120201
            * cfgexpand.cc (expand_used_vars): Set data.asan_alignb to
            maximum of itself and crtl->stack_alignment_needed
            / BITS_PER_UNIT.
    
            * g++.dg/asan/pr120201-1.C: New test.
    
    Signed-off-by: H.J. Lu <[email protected]>
    (cherry picked from commit 0f485cf70171a41ea39deec0a8b65aad0b9d2a9b)

Diff:
---
 gcc/cfgexpand.cc                       |  3 +++
 gcc/testsuite/g++.dg/asan/pr120201-1.C | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/gcc/cfgexpand.cc b/gcc/cfgexpand.cc
index 5f14a934e842..576a8e33f10f 100644
--- a/gcc/cfgexpand.cc
+++ b/gcc/cfgexpand.cc
@@ -2676,6 +2676,9 @@ expand_used_vars (bitmap forced_stack_vars)
 	  HOST_WIDE_INT offset, sz, redzonesz;
 	  redzonesz = ASAN_RED_ZONE_SIZE;
 	  sz = data.asan_vec[0] - prev_offset;
+	  data.asan_alignb = MAX (data.asan_alignb,
+				  crtl->stack_alignment_needed
+				  / BITS_PER_UNIT);
 	  if (data.asan_alignb > ASAN_RED_ZONE_SIZE
 	      && data.asan_alignb <= 4096
 	      && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
diff --git a/gcc/testsuite/g++.dg/asan/pr120201-1.C b/gcc/testsuite/g++.dg/asan/pr120201-1.C
new file mode 100644
index 000000000000..7a1a748f014d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/asan/pr120201-1.C
@@ -0,0 +1,42 @@
+// { dg-do run }
+// { dg-options "-O3 -std=c++23 -fsanitize=address" }
+
+using size_t = decltype (sizeof 0);
+
+float* shaderLightData;
+
+using ShaderShadowTransform = float[4z * 4z * 6z];
+using Transform = float[4 * 4];
+
+__attribute__ ((noipa))
+#if defined(__i386__) || defined(__x86_64__)
+__attribute__ ((target ("arch=x86-64-v4")))
+#endif
+static void
+do_test (void)
+{
+  int lightCount = 3;
+  ShaderShadowTransform* shaderShadowData = new ShaderShadowTransform[lightCount];
+  for (int index = 0; index < lightCount; index++)
+    {
+      Transform transforms[6];
+
+      const size_t matSize = 4z * 4z;
+      float* transformBlockStart = shaderShadowData[index];
+      for (int face = 0; face < 6; face++)
+	__builtin_memcpy(transformBlockStart + (matSize * face),
+			 &transforms[face][0], matSize * sizeof(float));
+  }
+
+  delete [] shaderShadowData;
+}
+
+int
+main (void)
+{
+#if defined(__i386__) || defined(__x86_64__)
+  if (__builtin_cpu_supports ("x86-64-v4"))
+#endif
+    do_test ();
+  return 0;
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.