]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
cfgexpand: Align the whole asan var block to crtl->stack_alignment_needed [PR120201]
authorJakub Jelinek <jakub@redhat.com>
Tue, 14 Jul 2026 08:37:03 +0000 (10:37 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 14 Jul 2026 08:39:17 +0000 (10:39 +0200)
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  <jakub@redhat.com>
    H.J. Lu  <hjl.tools@gmail.com>

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 <hjl.tools@gmail.com>
gcc/cfgexpand.cc
gcc/testsuite/g++.dg/asan/pr120201-1.C [new file with mode: 0644]

index e51d5a82ce81d74720a46283f4eedc2374da0480..c5b4a62ace2e765d74c7c360f89dde20f5022767 100644 (file)
@@ -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 (file)
index 0000000..7a1a748
--- /dev/null
@@ -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;
+}